Commit Graph
2 Commits
Author SHA1 Message Date
yetone d3898ca8d3 provider: ChatGPT account calls go with Codex's headers, and saved accounts are listed once a day
Two follow-ups of 6e55914b6's audit of what magpie sends OpenAI for
idle saved Codex accounts (owl, Discord #general: three ChatGPT accounts
deactivated). Product call made by the session the owner delegated
product calls to; mimicking the official client is allowed for every
vendor but Claude Code. Identity, base_instructions and prompts are not
touched.

- Headers. GET /backend-api/wham/usage, /wham/rate-limit-reset-credits,
  POST .../consume and POST auth.openai.com/oauth/token (live and saved
  sign-ins, and the sign-in's code exchange) went with Go's
  "Go-http-client/1.1" and no originator. They now carry Codex's
  originator and User-Agent through codexClientHeaders, the part of
  codexSign that /codex/models and /responses already used. No version
  header on them: codex-rs sends it with model requests only
  (login/src/auth/default_client.rs default_headers is originator +
  User-Agent; backend-client/src/client.rs adds auth headers).
- Refresh body. codex-rs (main f32b6cfd and rust-v0.165.0,
  login/src/oauth/client.rs refresh, auth/manager.rs
  request_chatgpt_token_refresh) sends JSON client_id, grant_type,
  refresh_token and refresh_reason, and no scope. magpie sent
  scope "openid profile email" and no reason. It now sends codex-rs's
  fields: proactive_expiry near expiry, unauthorized_recovery after a
  401 (stale token), scheduled from the 8-day keep-alive, forced for an
  import or a revoked-sign-in check. The code exchange's form already
  matched exchange_code; only its headers changed.
- Model lists. The active Codex account's /codex/models fetch (hourly
  KeepAccountsListed, FetchNew, Refresh) also fetched every switched-on
  saved account's list each time. codexFetchSaved now asks one with no
  list at once, and the others once a day, one per call, oldest asked
  first; a failed ask is remembered so a deactivated account doesn't go
  first every hour. The active account's list is unchanged.

Docs: providers-accounts.md says both.

Tests (codex_quiet_test.go), each red on the old behaviour (broken by
hand: wham/refresh/exchange headers removed, scope back, the age check
and the one-a-look cap removed, the keep-alive's reason dropped, and
separately the failed-ask memory skipped):
- TestCodexAccountCallsGoAsCodex counts the requests to a fake (usage 1,
  credits 1, consume 1, token 2) and checks User-Agent/originator on
  each and the refresh body's keys. Old: Go-http-client/1.1, "" and
  scope in the body.
- TestCodexRefreshSaysWhy: scheduled, forced, unauthorized_recovery.
  Old: no reason.
- TestSavedCodexListsOnceADay: 2 asks on the first look, 0 an hour on,
  1 then 1 a day on, 0 again, and 0 within the day while failing. Old:
  2 on every look; without the memory, a failing one asked again.

Run (mote-run, at origin/main 9b4d0bda8 plus this change, the tree
pushed unless the rebase below names a newer base):
- gofmt -l internal/provider: clean
- go vet ./internal/{provider,agent,cli,gateway} on darwin, GOOS=linux
  -tags nogui and GOOS=windows: ok
- go test -count=1 -timeout 45m ./internal/provider ./internal/agent
  ./internal/cli ./internal/gateway: all ok (118s, 199s, 44s, 562s)
Not tried: a real ChatGPT account (none touched, per the session's
rules), so OpenAI's answer to the new headers and the scope-less
refresh is known only from codex-rs doing the same. The Sign in with
ChatGPT API path (auth.openai.com/api/accounts/oauth/token, chatgpt_api.go)
is another client and is left as it was. Refresh in the GUI also lists
saved accounts by the once-a-day rule now.
2026-10-30 02:28:10 +08:00
yetone 6e55914b6d provider: Codex accounts are read one at a time and renewed every 8 days, not all at once
owl (Discord #general) holds several ChatGPT accounts in magpie with
no agent wired and had three deactivated. Auditing what magpie sends
to OpenAI for such accounts found two bursts from one address that
the official Codex client never makes:

- KeepOnAnAccountWithRoom read /wham/usage for every saved Codex
  account, concurrently, every 5 minutes (every minute near the cap),
  though only the account Codex is on matters until it is spent.
  NextLogin now reads the account it is on and the one it goes back
  to, and the spares only once the one it is on is spent; CodexUsedUp
  reads the active account alone (codexReadingHeld only looks at it).
  The Claude side of the same loop gets the same rule.
- KeepLoginsAlive force-refreshed every idle saved ChatGPT sign-in
  once a day, all in the same hourly look, so after day one they were
  refreshed side by side for good. It now renews after 8 days (codex-rs
  TOKEN_REFRESH_INTERVAL in login/src/auth/manager.rs) and one account
  per hourly look, so they drift apart.

Not changed: headers, identity, originator, prompts. The refresh and
usage requests still go with Go's User-Agent and no originator, while
Codex sends codex_cli_rs and an originator; that is left for the
owner. The hourly /models list per saved account and the Usage page
reading every account when it is open are unchanged too.

Run (mote-run, at origin/main f5dd4e142 plus this change):
- gofmt -l internal/provider: clean
- go vet ./internal/provider (darwin), GOOS=linux -tags nogui,
  GOOS=windows: ok; earlier also ./internal/cli ./internal/gateway
- go test -count=1 -timeout 45m ./internal/provider ./internal/cli: ok
- go test -run of the 24 internal/gateway tests that reach NextLogin,
  CodexUsedUp or LoginUsageVia (allowance_stale, claude_renewed,
  codex_accounts, codex_app_out, quotas_remote): 24 PASS
- go test ./internal/agent: ok (before the stand-in copy fix below)
Broken by hand: with the old codex_switch.go, login_usage.go and
keepalive.go, TestCodexLookReadsTheActiveAccountOnly read all three
accounts in one look and TestKeepAliveRenewsOneALook failed on the 24h
interval; with keepAliveAtOnce = 0 it renewed both idle accounts in
one look.
Not tried: a real ChatGPT account; whether this traffic had anything
to do with owl's deactivations is not known.
2026-10-30 02:09:54 +08:00