Skip to content

Keep the last Codex limits, marked stale, when a probe fails - #15249

Open
omarchybot wants to merge 6 commits into
quattrofrom
fix/issue-13425
Open

omarchybot wants to merge 6 commits into
quattrofrom
fix/issue-13425

Conversation

@omarchybot

Copy link
Copy Markdown
Collaborator

When a Codex rate-limit probe fails part way — the app-server times out, the usage backend answers with an error, or the app-server exits — omarchy-agent-usage-codex writes a record with empty limits, and the agents panel loses limits that were right one refresh ago. #14049 made those failures rarer and easier to diagnose, but a failed probe still leaves the panel with nothing. The Claude and Grok collectors already keep each account's last limits and mark them stale, and the panel already shows that as "Last updated … ago". Codex was the only one that didn't.

The Codex collector now keeps the last limits each home was given and serves them, marked stale, when a later probe fails part way. It emits limitsStale and limitsFetchedAt on the record and stale and fetchedAt on each account, the same fields the other collectors emit. A window that has reset since the numbers were taken reads as empty. A sign-out, a missing CLI or an app-server that exits without saying why is still reported, not hidden behind old numbers.

Cached numbers are only shown to the login that produced them. The cache is keyed by home and by the signed-in identity, which is the CODEX_ACCESS_TOKEN when one is set. Otherwise, under the file store, it is the workspace in auth.json paired with the user in the ID token's claims, read the way Codex reads them, because members of one workspace each have their own limits. A login that can't be told apart (a keyring or auto store, or no user in the token) keeps nothing. The identity is read before and after the probe, and nothing is kept if it changed in between.

Reviewed by Claude Opus 5.5 and by Codex Medium as a second opinion over six rounds. Its findings on identity, malformed auth.json and workspace members are fixed here, and its last round found nothing.

Fixes #13425

omarchybot and others added 6 commits October 13, 2026 06:10
A Codex rate-limit probe that times out, gets an error back from the usage backend, or loses its app-server replaced the record's limits with an empty list, so the panel lost limits that were right a refresh ago. The Claude and Grok collectors already keep each account's last limits and mark them stale; Codex now does the same, keyed by home and signed-in ChatGPT account so a home signed in to someone else never inherits them. A sign-out, a missing CLI, or an app-server that exits saying nothing are still reported rather than hidden behind old numbers, and windows that have reset since read as empty.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Keying the cache on auth.json's account alone let a keyring or auto-store login, which keeps no account there, share one cache across whoever signs in to the home, and let an access token in the environment fall back on auth.json's account. The cache is now keyed by the token when one is set, by auth.json's account only under the file store, and not kept at all when neither says who is signed in. Naive reset times read as UTC, as in the Claude collector.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
The cache key was read after the probe, so a home signed in to someone else while the app-server answered stored the first login's limits under the second's key. It is now read before and after, and the limits are kept and served only when both agree. The test no longer inherits CODEX_ACCESS_TOKEN from whoever runs it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
An auth.json whose tokens were not an object raised outside the parse's try, and nothing above it caught it any more, so the whole record was lost. Any shape that does not name an account now means the login can't be told apart. The login-change test now also checks the first login's limits survive.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
…ector

JSON and the environment can both hand Python a lone surrogate, which strict UTF-8 refuses to encode, so an account id or token carrying one aborted the record while its cache key was hashed.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
auth.json's account_id names the ChatGPT workspace, and members of one workspace each have their own limits, so a teammate signing in to the same home could fall back on the last member's numbers. The identity now pairs the workspace with the user from the ID token's claims, read as Codex reads them (chatgpt_user_id, else user_id), and a login that names no user keeps no limits.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Codex Medium <[email protected]>
@greptile-apps

greptile-apps Bot commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium impact] Adds caching for API rate limits when probes fail.

The PR appears safe to merge, with a non-blocking gap in timeout fallback tests.

Findings

  1. P2 Timeout fallback lacks coverage ▶

Summary

Codex keeps each identifiable login's last limits when a later probe fails and marks them stale.

  • Codex keeps the last limits for the same login after probe failures.

Intentional limits stated by omarchybot: keyring and auto stores, and tokens without a user identity, do not keep limits because the login cannot be distinguished. A probe spanning a login change still shows its live answer but saves it for neither login.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  A[Read home and login identity] --> B[Probe Codex]
  B --> C{Identity unchanged and known?}
  C -->|No| D[Return probe answer without caching]
  C -->|Yes| E{Received limits?}
  E -->|Yes| F[Save limits and timestamp]
  F --> G[Return live limits]
  E -->|No| H{Failure allows fallback?}
  H -->|No| I[Return error without old limits]
  H -->|Yes| J[Read saved limits]
  J --> K[Zero windows that have reset]
  K --> L[Return stale limits and saved timestamp]
Loading

Reviews (1) · Last reviewed commit: "Tell Codex logins apart by user as well ..." · Reviewed by Greptile

Comment on lines +84 to +88
for mode in backend died; do
kept=$(collect "$mode")
[[ $(jq -c --argjson at "$fetched_at" '{limitsStale, at: (.limitsFetchedAt == $at), used: [.limits[].percent * 100 | round], status: .usageStatusText, tierLabel}' <<<"$kept") == '{"limitsStale":true,"at":true,"used":[40,0],"status":"","tierLabel":"pro"}' ]] ||
fail "a failed Codex probe ($mode) keeps the last limits, marked stale" "$kept"
pass "a failed Codex probe ($mode) keeps the last limits, marked stale"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Timeout fallback lacks coverage

These tests check saved limits after backend errors and process exits, but not after a timeout. The existing timeout tests check the error message without first saving limits, so a change that blanks saved limits on timeout could pass both suites.

Add a fake server that stays alive without answering account/rateLimits/read. After a successful probe, check that this timeout keeps the old limits and timestamp and marks them stale.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@jandrusk

Copy link
Copy Markdown

Linking #14298 ("Keep Codex meters through a transient account/read timeout"). It aims at the same blank-on-failure behaviour (#13158), and its review asked for the cache to be keyed by login identity, which this PR does. If this one lands, #14298 can probably close.

dhh added a commit that referenced this pull request Oct 15, 2026
Any failed app-server probe replaced the record's limits with an empty list, so the panel lost limits that were right a refresh ago, and every panel open started another app-server. Each signed-in login now keeps its last good answer in its own codex-limits-<key>.json, keyed by home and by the workspace and user in auth.json, or by CODEX_ACCESS_TOKEN; a keyring login, which can't be told apart, keeps nothing, and neither does a probe a login change ran across. A failed probe serves the kept limits as stale, with elapsed windows at zero, and records and accounts carry limitsStale/limitsFetchedAt and stale/fetchedAt. A probe within 15 seconds is reused unless --force asks again. A stalled app-server, or one that died unable to reach OpenAI, sets retryAdvised. A missing CLI reads as "Codex limits unavailable", the contract's word for it.

Builds on #15249 and #14298.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-authored-by: Omarchybot <[email protected]>
Co-authored-by: AnPod <[email protected]>
@Roriz

Roriz commented Oct 16, 2026

Copy link
Copy Markdown

Tested this branch locally on Omarchy with a signed-in Codex account. All 105 checks across four focused suites passed, and a live probe returned both quota windows.

Five additional integration tests passed, covering cached limits and timestamp preservation after a timeout, recovery, backend errors, auth errors, login changes, and reset windows. We’re also running the PR collector through our existing widget, where it looks good.

This addresses an intermittent failure we’ve been experiencing. We’d appreciate seeing it merged when possible. Thanks!

@omarchybot

Copy link
Copy Markdown
Collaborator Author

The classification of this pull request is unsettled, so it carries no class label yet and has not been run on a worker this round.

Classified separately by Claude Opus 5.5 and by Codex Medium, from the pull request alone, at ce6fff08. Claude read it as a bug: on quattro a Codex probe that fails part way writes limits: [], so the agents panel drops limits it showed one refresh earlier, which is the behaviour #13425 reports, and the Claude and Grok collectors already keep theirs. Codex read it as an enhancement: the fix works by adding a per-login limits cache and new limitsStale and limitsFetchedAt fields that the collector never had. Both readings fit the diff, which is why neither label went on.

#14298 is an older open pull request fixing the same blanking a different way; which of the two should land is for the maintainer.

Waiting on the maintainer to settle the classification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

omarchy-agent-usage-codex: 4s RPC timeout intermittently drops Codex usage/limits

3 participants