bub icon indicating copy to clipboard operation
bub copied to clipboard

Codex OAuth tokens without expires_at are treated as expiring one hour after last_refresh

Open Fengzdadi opened this issue 2 months ago • 1 comments

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

I hit this on Bub 0.4.2; the same code is in 0.4.4 and on main.

Steps to reproduce

  1. Log in with Codex (codex login, ChatGPT auth mode). auth.json gets tokens.{id_token, access_token, refresh_token, account_id} and last_refresh, but no expires_at. The access token's exp is 10 days after its iat.
  2. More than an hour after last_refresh, run Bub with an openai: model and no API key, so it uses the Codex OAuth provider.
  3. 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_at is absent, read exp from the access token's JWT payload before falling back to last_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 exp is still in the future, keep using the current access token instead of returning None.
  • Optionally, log the refresh failure's exception type, without token values, so the cause is visible.

Environment

  • Bub 0.4.2 with bub-acp-server 0.0.2, in a python:3.12-slim-bookworm Linux 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

Fengzdadi avatar Sep 27 '26 02:09 Fengzdadi

Tks!

Fengzdadi avatar Sep 27 '26 17:09 Fengzdadi