// Account pins and 429 handoff depend on OAuth identities and provider responses unavailable in E2E. // The login callback identity is internal; /auth replaces it with a public id. import albedo/clock import albedo/harness/credentials as credential_sdk import albedo/harness/extension import albedo/harness/extensions/codex/accounts import albedo/harness/extensions/codex/catalog import albedo/harness/extensions/codex/extension as codex import albedo/harness/extensions/codex/wire as codex_wire import albedo/harness/rotation import albedo/openai_api import albedo/openai_api/types import gleam/bit_array import gleam/dynamic/decode import gleam/erlang/process import gleam/int import gleam/json import gleam/list import gleam/option.{None, Some} import gleam/result import gleam/string import gleeunit/should const credentials = "{\"accounts\":{\"openai-codex\":[{\"type\":\"oauth\",\"access\":\"h.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoiYWNjb3VudC0xIiwiY2hhdGdwdF9hY2NvdW50X3VzZXJfaWQiOiJzZWF0LTEifX0.s\",\"refresh\":\"refresh-1\",\"expires\":9999999999999,\"accountId\":\"account-1\",\"accountUserId\":\"seat-1\"},{\"type\":\"oauth\",\"access\":\"h.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoiYWNjb3VudC0yIiwiY2hhdGdwdF9hY2NvdW50X3VzZXJfaWQiOiJzZWF0LTIifX0.s\",\"refresh\":\"refresh-2\",\"expires\":9999999999999,\"accountId\":\"account-2\",\"accountUserId\":\"seat-2\"}]}}" /// The access tokens `credentials` holds for each account. const account_1_access = "h.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoiYWNjb3VudC0xIiwiY2hhdGdwdF9hY2NvdW50X3VzZXJfaWQiOiJzZWF0LTEifX0.s" const account_2_access = "h.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoiYWNjb3VudC0yIiwiY2hhdGdwdF9hY2NvdW50X3VzZXJfaWQiOiJzZWF0LTIifX0.s" const reversed_credentials = "{\"accounts\":{\"openai-codex\":[{\"type\":\"oauth\",\"access\":\"h.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoiYWNjb3VudC0yIiwiY2hhdGdwdF9hY2NvdW50X3VzZXJfaWQiOiJzZWF0LTIifX0.s\",\"refresh\":\"refresh-2\",\"expires\":9999999999999,\"accountId\":\"account-2\",\"accountUserId\":\"seat-2\"},{\"type\":\"oauth\",\"access\":\"h.eyJodHRwczovL2FwaS5vcGVuYWkuY29tL2F1dGgiOnsiY2hhdGdwdF9hY2NvdW50X2lkIjoiYWNjb3VudC0xIiwiY2hhdGdwdF9hY2NvdW50X3VzZXJfaWQiOiJzZWF0LTEifX0.s\",\"refresh\":\"refresh-1\",\"expires\":9999999999999,\"accountId\":\"account-1\",\"accountUserId\":\"seat-1\"}]}}" pub fn unidentified_login_accounts_never_use_tokens_as_identities_test() -> Nil { let assert Ok(login) = codex.extension().plugins |> list.find_map(fn(plugin) { case plugin { extension.LoginPlugin(login) -> Ok(login) _ -> Error(Nil) } }) let assert Ok(first) = json.parse( "{\"refresh\":\"secret-refresh-one\",\"access\":\"secret-access\"}", decode.dynamic, ) let assert Ok(second) = json.parse("{\"refresh\":\"secret-refresh-two\"}", decode.dynamic) let first_id = login.account(first).id string.is_empty(first_id) |> should.be_false string.contains(first_id, "secret") |> should.be_false login.account(first).id |> should.equal(first_id) { login.account(second).id == first_id } |> should.be_false } pub fn sessions_stick_to_and_distribute_across_codex_accounts_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let first = access(home, "session-1") access(home, "session-1") |> should.equal(first) let accounts = [access(home, "session-1"), access(home, "session-2")] |> list.unique list.length(accounts) |> should.equal(2) cleanup(root) } pub fn session_pin_survives_credential_reordering_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let selected = access(home, "durable-session") let _ = write(home, "creds.json", reversed_credentials) access(home, "durable-session") |> should.equal(selected) cleanup(root) } pub fn revoked_codex_account_is_removed_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let #(revoked, client) = codex_client(home, "revoked-session") let assert Some(message) = codex.account_failure(home, client, types.HttpError(401, "token_revoked")) string.ends_with(message, "run /login") |> should.be_true codex.account_failure(home, client, types.HttpError(500, "")) |> should.equal(None) let remaining = access(home, "revoked-session") { remaining == revoked } |> should.be_false access(home, "another-session") |> should.equal(remaining) let #(_, last) = codex_client(home, "revoked-session") let assert Some(_) = codex.account_failure(home, last, types.HttpError(401, "token_revoked")) let assert Error(_) = selected_access(home, "revoked-session") cleanup(root) } pub fn usage_limit_moves_the_session_to_a_sibling_account_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let #(limited, client) = codex_client(home, "busy-session") // Ordinary rate limiting is transient and must not move the session. codex.account_failure(home, client, types.HttpError(429, "slow down")) |> should.equal(None) access(home, "busy-session") |> should.equal(limited) let body = "{\"error\":{\"type\":\"usage_limit_reached\",\"resets_in_seconds\":3600}}" let assert Some(message) = codex.account_failure(home, client, types.HttpError(429, body)) string.contains(message, "usage limit reached") |> should.be_true string.contains(message, "the next turn will use") |> should.be_true let next = access(home, "busy-session") { next == limited } |> should.be_false // Every session avoids the limited account while a sibling has usage. access(home, "fresh-session") |> should.equal(next) // With every account limited the message says so instead of naming one. let #(_, sibling) = codex_client(home, "busy-session") let assert Some(exhausted) = codex.account_failure(home, sibling, types.HttpError(429, body)) string.contains(exhausted, "no other ChatGPT account") |> should.be_true cleanup(root) } /// Replays a stream where the named tokens answer 429 with `body` and every /// other account fails plainly, recording which accounts were tried. fn limited_stream( limited: List(String), body: String, tried: process.Subject(String), ) -> fn(codex_wire.Client, a, b) -> Result(c, types.Error) { fn(client: codex_wire.Client, _request, _on_event) { process.send(tried, client.connection.api_key) case list.contains(limited, client.connection.api_key) { True -> Error(types.HttpError(429, body)) False -> Error(types.HttpError(500, "second account answered")) } } } fn rotate( home: String, session: String, first: codex.Connection, request: types.Request, stream: fn(codex_wire.Client, types.Request, fn(types.Event) -> types.Control) -> Result(types.Turn, types.Error), pause: fn(Int) -> Nil, ) -> Result(types.Turn, types.Error) { rotation.stream( codex.pool(home, session, False, stream), first, request, fn(_) { types.Continue }, rotation.Budget(8, 0, pause, False), fn(_) { Nil }, ) } fn drain(tried: process.Subject(String), found: List(String)) -> List(String) { case process.receive(tried, 0) { Ok(token) -> drain(tried, [token, ..found]) Error(_) -> list.reverse(found) } } pub fn fast_mode_is_resolved_again_for_each_rotated_account_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let session = "fast-rotation-session" let #(first_account, first) = codex_client(home, session) let second_account = case first_account { "account-1" -> "account-2" _ -> "account-1" } let now = clock.os_system_ms() let _ = write( home, "codex-models.json", json.to_string( json.object([ #("clientVersion", json.string("1.2.3")), #("versionCheckedAt", json.int(now)), #( "accounts", json.object([ #( "account-1", cached_fast_model(now, "priority", account_1_access), ), #("account-2", cached_fast_model(now, "fast", account_2_access)), ]), ), ]), ), ) let tried = process.new_subject() let usage_limit = "{\"error\":{\"type\":\"usage_limit_reached\",\"resets_in_seconds\":3600}}" let request = openai_api.request("gpt-5.5", [types.User("hi")]) rotation.stream( codex.pool(home, session, True, fn(client, request, _) { let account = client.account_id let tier = case request.options.service_tier { Some(tier) -> tier None -> "missing" } process.send(tried, account <> ":" <> tier) case account == first_account { True -> Error(types.HttpError(429, usage_limit)) False -> Error(types.HttpError(500, "second account answered")) } }), first, request, fn(_) { types.Continue }, rotation.Budget(8, 0, fn(_) { Nil }, False), fn(_) { Nil }, ) |> should.equal(Error(types.HttpError(500, "second account answered"))) let assert [first_tier, second_tier] = drain(tried, []) first_tier |> should.equal( first_account <> ":" <> case first_account { "account-1" -> "priority" _ -> "fast" }, ) second_tier |> should.equal( second_account <> ":" <> case second_account { "account-1" -> "priority" _ -> "fast" }, ) cleanup(root) } fn cached_fast_model(now: Int, tier_id: String, access: String) -> json.Json { json.object([ #("fetchedAt", json.int(now)), #("clientVersion", json.string("1.2.3")), #("accessHash", json.string(catalog.access_hash(access))), #( "models", json.array( [ json.object([ #("slug", json.string("gpt-5.5")), #( "service_tiers", json.array( [ json.object([ #("id", json.string(tier_id)), #("name", json.string("fast")), ]), ], fn(value) { value }, ), ), ]), ], fn(value) { value }, ), ), ]) } pub fn a_limited_account_hands_the_same_request_to_the_next_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let #(_, first) = codex_client(home, "swarm-session") let tried = process.new_subject() let body = "{\"error\":{\"type\":\"usage_limit_reached\",\"resets_in_seconds\":3600}}" let request = openai_api.request("gpt-5.5", [types.User("hi")]) rotate( home, "swarm-session", first, request, limited_stream([first.client.connection.api_key], body, tried), fn(_) { panic as "a sibling had room; nothing should wait" }, ) |> should.equal(Error(types.HttpError(500, "second account answered"))) let attempts = drain(tried, []) list.length(attempts) |> should.equal(2) list.first(attempts) |> should.equal(Ok(first.client.connection.api_key)) cleanup(root) } pub fn a_burst_rate_limit_body_rotates_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let #(_, first) = codex_client(home, "burst-session") let tried = process.new_subject() let request = openai_api.request("gpt-6-luna", [types.User("hi")]) rotate( home, "burst-session", first, request, limited_stream( [first.client.connection.api_key], "{\"detail\":\"Rate limit exceeded\"}", tried, ), fn(_) { panic as "a sibling had room; nothing should wait" }, ) |> should.equal(Error(types.HttpError(500, "second account answered"))) list.length(drain(tried, [])) |> should.equal(2) cleanup(root) } pub fn a_rate_limit_rotates_too_and_stops_when_all_are_busy_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let #(_, first) = codex_client(home, "busy-swarm") let tried = process.new_subject() let body = "{\"error\":{\"type\":\"rate_limit_exceeded\",\"resets_in_seconds\":20}}" let #(_, second) = codex_client(home, "other-busy-swarm") let both = list.unique([ first.client.connection.api_key, second.client.connection.api_key, ]) let request = openai_api.request("gpt-5.5", [types.User("hi")]) let paused = process.new_subject() rotate( home, "busy-swarm", first, request, limited_stream(both, body, tried), fn(delay) { process.send(paused, int.to_string(delay)) }, ) |> should.equal(Error(types.HttpError(429, body))) // Each account once, then four waits with a retry after each, then it stops. list.length(drain(tried, [])) |> should.equal(6) drain(paused, []) |> should.equal(["4000", "8000", "15000", "30000"]) cleanup(root) } pub fn a_usage_limit_on_every_account_does_not_wait_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let #(_, first) = codex_client(home, "spent-swarm") let #(_, second) = codex_client(home, "other-spent-swarm") let tried = process.new_subject() let body = "{\"error\":{\"type\":\"usage_limit_reached\",\"resets_in_seconds\":3600}}" let request = openai_api.request("gpt-6-luna", [types.User("hi")]) rotate( home, "spent-swarm", first, request, limited_stream( list.unique([ first.client.connection.api_key, second.client.connection.api_key, ]), body, tried, ), fn(_) { panic as "a usage limit lasts hours; it must not be waited on" }, ) |> should.equal(Error(types.HttpError(429, body))) list.length(drain(tried, [])) |> should.equal(2) cleanup(root) } pub fn selected_account_overrides_the_session_pin_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let pinned = access(home, "pinned-session") let other = case pinned { "account-1" -> "account-2" _ -> "account-1" } let _ = write( home, "creds.json", string.replace( credentials, "\"accountId\":\"" <> other <> "\"", "\"accountId\":\"" <> other <> "\",\"selected\":true", ), ) access(home, "pinned-session") |> should.equal(other) access(home, "any-session") |> should.equal(other) cleanup(root) } fn codex_client(home: String, session: String) -> #(String, codex.Connection) { let assert Ok(connection) = codex.pool(home, session, False, codex_wire.stream).current() #(connection.client.account_id, connection) } fn access(home: String, session: String) -> String { let assert Ok(access) = selected_access(home, session) access.account_id } fn selected_access( home: String, session: String, ) -> Result(codex.Access, String) { use stored <- result.map(accounts.select(home, session, "")) let value = credential_sdk.value(stored) codex.Access(value.access, accounts.account_id(value)) } @external(erlang, "albedo_skills_test_support", "fixture") fn fixture() -> #(String, String, String) @external(erlang, "albedo_skills_test_support", "write") fn write(base: String, relative: String, content: String) -> String @external(erlang, "albedo_skills_test_support", "cleanup") fn cleanup(root: String) -> Nil // A rejected in-flight token must not mark or delete a replacement saved under // the same public/provider identity. E2E cannot hold this exact mutation point. pub fn stale_codex_attempt_cannot_limit_or_revoke_replacement_test() -> Nil { let #(root, _, home) = fixture() let _ = write(home, "creds.json", credentials) let session = "replaced-token" let #(_, attempted) = codex_client(home, session) let _ = write( home, "creds.json", string.replace( credentials, attempted.client.connection.api_key, "replacement-access", ), ) accounts.limited( home, "", attempted.stored, "{\"error\":{\"type\":\"usage_limit_reached\"}}", ) |> should.be_error let assert Ok(_) = accounts.revoke(home, attempted.stored) let #(_, current) = codex_client(home, session) current.client.connection.api_key |> should.equal("replacement-access") cleanup(root) } // OpenAI may omit refresh_token and claims on renewal. Losing the stored // refresh token or account id here would make the next request signed out. pub fn codex_refresh_without_rotated_refresh_or_claims_keeps_identity_test() -> Nil { let assert Ok(previous) = json.parse( "{\"type\":\"oauth\",\"access\":\"old-access\",\"refresh\":\"retained-refresh\",\"accountId\":\"retained-account\",\"accountUserId\":\"retained-seat\",\"email\":\"stored@example.test\",\"expires\":0}", accounts.decoder(), ) let assert Ok(updated) = accounts.parse_refresh( bit_array.from_string( "{\"access_token\":\"opaque-new-access\",\"expires_in\":3600}", ), previous, ) updated.refresh |> should.equal("retained-refresh") accounts.identity(updated) |> should.equal("retained-seat") accounts.account_id(updated) |> should.equal("retained-account") accounts.email(updated) |> should.equal("stored@example.test") }