Codex OAuth tokens without expires_at are treated as expiring one hour after last_refresh
Summary
When CODEX_HOME/auth.json has no tokens.expires_at, Bub assumes the Codex OAuth access token expires one hour after last_refresh. The auth.json that Codex writes has no expires_at: the real expiry is the access token's JWT exp claim, 10 days after a fresh ChatGPT login in my case. Bub therefore treats a valid token as expired and refreshes it. When that refresh fails, it returns no API key although the token is still valid.
Where
_parse_expiryfalls back tolast_refresh + 3600whenexpires_atis absent.openai_codex_oauth_resolverreturnsNonewhen the refresh raises and that estimated expiry has passed.
I hit this on Bub 0.4.2; the same code is in 0.4.4 and on main.
Steps to reproduce
- Log in with Codex (
codex login, ChatGPT auth mode).auth.jsongetstokens.{id_token, access_token, refresh_token, account_id}andlast_refresh, but noexpires_at. The access token'sexpis 10 days after itsiat. - More than an hour after
last_refresh, run Bub with anopenai:model and no API key, so it uses the Codex OAuth provider. - Before the first model call, Bub refreshes the token although it is valid for days.
On a normal machine this costs one unnecessary refresh, because Bub writes the new tokens and an expires_at back to auth.json.
It breaks when CODEX_HOME does not persist between runs. I ran Bub in disposable containers that each received a copy of the host's auth.json (the PowerContext Harbor E2E harness does this). Every container refreshed from the same original refresh token, and the refreshed tokens were discarded with the container. For about an hour this still worked; after that every run failed before its first model call with MissingApiKeyError('openaicodex', 'OPENAI_CODEX_API_KEY'), while the access token still had about 7 days left. Runs started within an hour of a fresh codex login worked, because Bub did not refresh. I could not see why the refresh failed, because the resolver discards the exception.
Suggested fix
- When
expires_atis absent, readexpfrom the access token's JWT payload before falling back tolast_refresh + 3600. The value only schedules the refresh, so the signature does not need to be verified. - Optionally, if a refresh fails and the JWT
expis still in the future, keep using the current access token instead of returningNone. - Optionally, log the refresh failure's exception type, without token values, so the cause is visible.
Environment
- Bub 0.4.2 with
bub-acp-server0.0.2, in apython:3.12-slim-bookwormLinux container (arm64) - Host: macOS 26.4 (arm64), OrbStack with Docker 29.4.0
- Codex CLI 0.158.0-alpha.2.1 (bundled with the Codex app), ChatGPT auth mode
Tks!