Skip to content
Source
Sign inSign up
pingdotgg/t3code

fix(server): managed Codex renews a ChatGPT token the API reports as expired #29946

Closedjuliusmarmingewants to merge 5 commits intomain fromfix/issue-29927opened View on GitHub

juliusmarmingecommented

Problem

Managed ChatGPT Codex stays stuck when the Responses API rejects its access token with 401 token_expired before T3's recorded expiry. Codex gets its token from T3's helper command. On a 401 it runs that command once more and retries once. The server returned the saved token as long as it had more than 60 s left, so the retry reused the rejected token. Every turn and thread resume then failed until local expiry, about an hour later.

Reproduction: the new test runs the real helper the way Codex does: a first fetch, then the rerun after a 401. On main the rerun gets back rejected-access and no refresh happens.

Change

  • Codex runs the helper both when one of its auth managers has an empty cache (several per process) and after a 401, and it passes no signal saying which. The server therefore checks with the API itself.
  • Each managed Codex launch (runtime.resolve) registers a random launch id with the token broker for the life of its scope. The helper passes that id in its request. The broker remembers which token it last served to each launch.
  • A launch's first request takes the existing access path.
  • A repeat request whose token is still the saved one, and not inside the normal 60 s renewal window, triggers one GET /v1/models with that token. The request is scoped and outside the refresh lock, with a 10 s timeout.
  • The server renews only when that probe returns 401 with token_expired, in the body's error.code or the base64 JSON x-error-json header (the two places Codex reads it).
  • Renewal goes through the existing locked refresh flow. It compares the saved token again under the lock, so concurrent rejections from several launches spend the single-use refresh token once.
  • Any other status, another 401 code, a timeout or a network error returns the current token unchanged.
  • earliestRefreshAt, the profile check and the sharing check behave as before. Tokens are never logged.
  • access is unchanged for its other callers. Codex still bounds the retry to one, so a second 401 reaches the user as before.

Scope and approval

Fixes pingdotgg/t3code#29927

Verification

Focused tests in apps/server (vp test run src/provider/CodexManagedRuntime.test.ts src/provider/CodexChatGptAuth.test.ts src/provider/Drivers/CodexDriver.test.ts): 78 passed.

  • managed Codex helper renews a token the API rejected before its local expiry spawns the real helper through the broker with the launch args Codex receives. It fails on main with the rejected token returned.
  • two launches rejecting the same unexpired token share one renewal: both launches probe the rejected token before the first renewal is saved, and exactly one refresh exchange happens.
  • a launch asking again keeps its token when the probe returns 200 / 503 / a 401 that is not token_expired: there is no refresh exchange and the same token comes back. The invalid_api_key case fails against a version that renewed on any 401.
  • vp-check-changed passes: format, lint, knip, typecheck (3 packages), tests (4 files).
Test transcript
# Red: f3b944a72e (test only, implementation at main 61c3794c32)
   × managed Codex helper renews a token the API rejected before its local expiry 578ms
AssertionError: the helper returned the token the API just rejected instead of renewing it: expected { stdout: 'rejected-access', code: +0 } to deeply equal { stdout: 'renewed-access', code: +0 }
- Expected
+ Received
-   "stdout": "renewed-access",
+   "stdout": "rejected-access",

# Green: 5f55407ad0
 ✓ CodexChatGptAuth.test.ts > two launches rejecting the same unexpired token share one renewal
 ✓ CodexChatGptAuth.test.ts > a launch asking again keeps its token when the probe returns 200
 ✓ CodexChatGptAuth.test.ts > a launch asking again keeps its token when the probe returns 503
 ✓ CodexChatGptAuth.test.ts > a launch asking again keeps its token when the probe returns a 401 that is not token_expired
 ✓ CodexManagedRuntime.test.ts > managed Codex helper renews a token the API rejected before its local expiry
 Test Files  3 passed (3)
      Tests  78 passed (78)

This is a server-only change with no rendered surface, so there are no captures.

Not checked: a live run against OpenAI. No VM has first-party ChatGPT sign-in, so it is unverified that /v1/models reports token_expired for a token that /v1/responses rejects. If it does not, behaviour stays as on main. Codex's retry behaviour was read from the Codex 0.156 source (codex-rs/login/src/auth/external_bearer.rs, manager.rs UnauthorizedRecovery, model-provider/src/auth.rs).

Claude Opus 5.5 (Claude Code in T3 Code), with review by GPT-6.1 Sol (Codex) and Claude Opus 5.5.

🤖 Generated with Claude Code

    1. github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted

    2. Approvability

      Verdict: Approved at 5f55407

      Macroscope's review found this PR approvable — Managed Codex can renew a token that OpenAI reports as expired even before its recorded expiry time. The recovery path preserves existing credential protections and refresh restrictions, with bounded API probing and process-scoped tracking.

      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. Thread transfer impact

      ✅ Thread transfer remains within every enforced ceiling.

      ProviderMetricMain baselineThis PRImpactPR ceiling
      CodexTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      CodexThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      CodexLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      CodexLive turn WebSocket decoded21.6 KiB21.6 KiB0 B (0.0%)24.4 KiB✅
      CodexLive turn messages220 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB−18 B (−0.3%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB−18 B (−1.4%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.9 KiB21.9 KiB−41 B (−0.2%)24.4 KiB✅
      ClaudeLive turn messages21−1 (−50.0%)8✅

      Baseline: 61c3794 · PR result: 5f55407 · Source CI: success

      Scenario and decoded snapshot size

      10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

      • Codex decoded thread snapshot: 108.5 KiB
      • Claude decoded thread snapshot: 108.8 KiB

      Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

    4. juliusmarminge added labelsbugfix

    5. Review in Change Stack →

      No actionable comments were generated in the recent review.

      Walkthrough

      The managed token request now includes a launch ID. The broker tracks tokens served to each launch and can request renewal when a locally unexpired token is rejected as token_expired. Tests cover concurrent refreshes and other probe responses.

      Changes: Managed token renewal

      Layer / File(s)Summary
      Detect and renew rejected tokens
      apps/server/src/provider/CodexChatGptAuth.ts, apps/server/src/provider/CodexChatGptAuth.test.ts, apps/server/src/provider/CodexManagedRuntime.test.ts
      Auth decodes token_expired from the response body or x-error-json. renewRejected probes /models when the supplied token matches the saved, locally unexpired token. It renews only for a matching 401. Tests cover concurrent launches sharing a refresh and fallback responses that do not renew.
      Pass launch IDs through the token broker
      apps/server/src/provider/CodexManagedTokenProtocol.ts, apps/server/src/provider/CodexManagedTokenBroker.ts, apps/server/src/provider/CodexManagedRuntime.ts, apps/server/src/cli/managedChatGptToken.ts, apps/server/src/provider/Drivers/CodexDriver.test.ts
      The request schema requires a UUID v4 launchId. The runtime passes it to the CLI, which includes it in the broker request. The broker registers launch IDs, tracks each launch’s last-served token, and uses renewRejected for subsequent requests.

      Estimated code review effort: 3 (Moderate) | ~25 minutes

      Change: Bug fix · Severity of issue fixed: Medium

      Sequence Diagram(s)

      sequenceDiagram
        participant Runtime as CodexManagedRuntime
        participant CLI as managedChatGptToken
        participant Broker as CodexManagedTokenBroker
        participant Auth as CodexChatGptAuth
        participant Models as Models endpoint
        participant Refresh as Refresh-token endpoint
        Runtime->>Broker: Register launch and receive launchId
        Runtime->>CLI: Invoke token helper with launchId
        CLI->>Broker: Send token request
        Broker->>Auth: Request access token
        Auth-->>Broker: Return saved token
        Broker-->>CLI: Return token
        CLI-->>Runtime: Provide token to helper
        Runtime->>CLI: Invoke helper again after rejection
        CLI->>Broker: Send token request with launchId
        Broker->>Auth: Call renewRejected with last-served token
        Auth->>Models: Probe saved token
        Models-->>Auth: Return 401 token_expired
        Auth->>Refresh: Renew access token
        Refresh-->>Auth: Return renewed token
        Auth-->>Broker: Return renewed token
        Broker-->>CLI: Return renewed token

      Possibly related issues

      • pingdotgg/t3code#24536 — The broker now serves a renewed token on a later helper request for a registered launch. The issue also describes session and static environment-token cases that this change does not cover.

      Suggested labels: bugfix

      Suggested reviewers: maria-rcks, t3dotgg

      Merge Risk: Minimal · up to 5f554

      No demonstrated issue blocks merging. Live API behavior remains unverified.

      Pre-merge checks: Passed 4
      Check nameStatusExplanation
      Title checkPassedThe title "fix(server): managed Codex renews a ChatGPT token the API reports as expired" matches the main change. The PR adds server-side renewal when the Responses API rejects a managed Codex token w…
      Description checkPassedThe description follows the template's sections: Problem, Change, Scope and approval, and Verification. The Problem section states the bug, the expected behavior, and a reproduction path. The Change s…
      Linked Issues checkPassedIssue [#29927] requires renewal after a managed ChatGPT token is rejected with 401 token_expired, one bounded retry, locked refresh handling, preservation of existing access rules, and no token logg…
      Out of Scope Changes checkPassedThe changed files support issue [#29927]. They update the managed token protocol, launch lifecycle, broker behavior, authentication renewal, runtime arguments, and related automated tests. No unrelate…
      Full details: Title check

      Explanation

      The title "fix(server): managed Codex renews a ChatGPT token the API reports as expired" matches the main change. The PR adds server-side renewal when the Responses API rejects a managed Codex token with 401 token_expired before the local expiry. It follows the conventional commit style that the template asks for. The title is specific and plain. It is not vague.

      Full details: Description check

      Explanation

      The description follows the template's sections: Problem, Change, Scope and approval, and Verification. The Problem section states the bug, the expected behavior, and a reproduction path. The Change section explains the launch-ID broker, the rejected-token probe, and the locked refresh. The Verification section lists focused test commands, a red/green transcript, and what was not checked, including live OpenAI behavior. The Scope and approval section links issue #29927 with the fix reference. The template asks for triaged issue or maintainer approval, and the description gives only the issue link. It does not include an approval comment or a maintainer discussion link. This is a minor gap, so the check passes.

      Full details: Linked Issues check

      Explanation

      Issue [#29927] requires renewal after a managed ChatGPT token is rejected with 401 token_expired, one bounded retry, locked refresh handling, preservation of existing access rules, and no token logging. The PR adds launch-scoped token tracking, probes /v1/models on a repeat helper request, recognizes token_expired in the response body and x-error-json, and calls the existing locked renewal flow. The broker records the renewed token only for an active launch. Tests cover renewal, concurrent single-use refresh, non-renewal for 200, 503, and other 401 responses, plus the real helper flow. The reported focused tests pass.

      Full details: Out of Scope Changes check

      Explanation

      The changed files support issue [#29927]. They update the managed token protocol, launch lifecycle, broker behavior, authentication renewal, runtime arguments, and related automated tests. No unrelated product surface or unrelated behavior is shown. The reported live OpenAI check is absent, but that is a verification limitation and not an out-of-scope change.

      Finishing Touches
      Generate docstrings
      • Commit to this branch
      • Create a new PR
      Generate unit tests (beta)
      • Commit to this branch
      • Create a new PR
      • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Warning

      Some tools did not complete. Review the errors below.

      ast-grep (0.51.1)

      ast-grep could not parse rule config: /ast-grep-rules/ast-grep-essentials/rules/c/security/unchecked-memcpy-strlen-length-c.yml

      CodeRabbit Verify

      • Run CodeRabbit Verify to test the changes in this pull request.
      Recent review info
      Run configuration
      Commits

      Reviewing files that changed from the base of the PR and between 61c3794c32b0eeebfac09add43e403618c65d2d7 and 5f55407ad0923c92102e89ed491b13c864b66958.

      Files selected for processing: 8
      • apps/server/src/cli/managedChatGptToken.ts
      • apps/server/src/provider/CodexChatGptAuth.test.ts
      • apps/server/src/provider/CodexChatGptAuth.ts
      • apps/server/src/provider/CodexManagedRuntime.test.ts
      • apps/server/src/provider/CodexManagedRuntime.ts
      • apps/server/src/provider/CodexManagedTokenBroker.ts
      • apps/server/src/provider/CodexManagedTokenProtocol.ts
      • apps/server/src/provider/Drivers/CodexDriver.test.ts

      Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

    6. juliusmarminge added labelspr:needs-julius

    7. Thanks, Julius, for the earlier work on this. Closing as a duplicate of #29950 by voltcrash, which addresses the same early token_expired rejection and has a more recent update. This branch has not been updated in over a week, and main has changed the affected auth files since its base.

    8. juliusmarminge closed this

    9. juliusmarminge removed labelspr:needs-julius

Sign in to join this conversation.