Skip to content

fix(codex): preserve short-window observation provenance for routing - #3605

Merged
lidge-jun merged 2 commits into
devfrom
codex/260905-short-quota-provenance
Sep 5, 2026
Merged

lidge-jun merged 2 commits into
devfrom
codex/260905-short-quota-provenance

Conversation

@lidge-jun

@lidge-jun lidge-jun commented Sep 5, 2026 •

Copy link
Copy Markdown
Owner

Summary

Addresses the missing-reset short-window selection case reported in #3425. A recent terminal short-window reading can exclude an account even without a reset timestamp. Plain 502 remains transient.

The initial candidate used the whole snapshot's updatedAt, which credits-only updates also refresh. Independent inspection caught that regression before merge. This revision records shortObservedAt only when shortPercent is actually observed, preserves its age through unrelated updates and disk serialization, and rejects future timestamps. Old snapshots without provenance remain unknown.

Refs #3425. This targets a demonstrated selection mechanism; it does not claim every cause of the reporter's 502s is proven.

Verification

  • Typecheck exit 0 and whitespace check passed.
  • Independent Astra static re-review: both initial blockers resolved, no remaining blockers.
  • Regression assertions cover stale, missing, future and partial-update provenance plus active/bound account selection.
  • No local tests run for this correction; hosted final dev Linux CI is the batch gate.

Checklist

  • Bounded account-selection correction.
  • Persistence creation/carry/serialization/consumption chain inspected.
  • No token or credential exposure changes.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of exhausted short-term usage windows when reset times are unavailable.
    • Fresh, valid usage observations now correctly identify unavailable accounts, while stale or invalid data remains unclassified.
    • Partial quota updates preserve usage observation data, and reset timestamps clear outdated observations.
    • Accounts with freshly exhausted usage windows are bypassed for new and bound threads.
    • HTTP 502 responses continue to be treated as temporary failures.

jun added 2 commits September 5, 2026 12:29
…set timestamp

An exhausted Codex account kept serving 118 consecutive 502s over 23 minutes while
a healthy account sat at 3% (#3425). Two mechanisms reinforce each other:
isTerminalShortWindow requires a FUTURE shortResetAt, so a 100% snapshot with no
timestamp scores unknown and stays selectable; and 502 is classified transient, so
the failures produce no quota signal to correct it.

Gate on snapshot FRESHNESS instead of reset presence. StoredAccountQuota.updatedAt
already exists, and a 100% window observed seconds ago is a measured refusal while a
stale one still falls back to unknown. That preserves #3029 in both directions: the
existing assertion that a bare {shortPercent: 100} stays unknown passes no updatedAt
and is untouched and still green.

502 stays classified transient - demoting every gateway blip would strand accounts on
ordinary upstream noise, so the exclusion comes from the snapshot instead.

Closes #3425
Record shortObservedAt only for newly observed shortPercent. Preserve it across credits-only and unrelated window updates; old disk rows without provenance remain unknown. Reject future timestamps. Addresses both independent review findings on the #3425 candidate.
@lidge-jun
lidge-jun requested a review from Ingwannu as a code owner September 5, 2026 03:44
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 5, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-05T03:47:18.355600Z 6fbd8de PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@lidge-jun
lidge-jun merged commit e1b9ec8 into dev Sep 5, 2026
5 of 7 checks passed
@lidge-jun
lidge-jun deleted the codex/260905-short-quota-provenance branch September 5, 2026 03:45
@coderabbitai

coderabbitai Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Team

Run ID: 1608d1e8-d2c9-4f26-bb45-34f565261d93

📥 Commits

Reviewing files that changed from the base of the PR and between 850afb2 and 6fbd8de.

📒 Files selected for processing (3)
  • src/codex/quota.ts
  • src/codex/routing.ts
  • tests/codex-integration/codex-routing.test.ts

📝 Walkthrough

Walkthrough

The change records when finite short-window quota usage was observed. Routing uses observations from the last five minutes to classify 100% usage as terminal when no valid reset timestamp exists. Tests cover metadata preservation and thread routing.

Changes

Short-window quota routing

Layer / File(s) Summary
Record short-window observation provenance
src/codex/quota.ts
StoredAccountQuota adds shortObservedAt. Finite short-window updates record the timestamp, and credits-only, partial, and general updates preserve it.
Classify fresh exhausted windows
src/codex/routing.ts, tests/codex-integration/codex-routing.test.ts
Routing treats a 100% short-window reading as terminal without shortResetAt only when shortObservedAt is valid and no older than five minutes. Tests cover stale and invalid timestamps, thread routing, metadata clearing, and transient HTTP 502 responses.

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

Sequence Diagram(s)

sequenceDiagram
  participant QuotaUpdate
  participant StoredAccountQuota
  participant CodexRouting
  participant ThreadSelection
  QuotaUpdate->>StoredAccountQuota: Store shortPercent with shortObservedAt
  StoredAccountQuota->>CodexRouting: Provide short-window quota metadata
  CodexRouting->>CodexRouting: Validate observation freshness
  CodexRouting->>ThreadSelection: Exclude freshly exhausted account
Loading

Suggested reviewers: luvs01

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/260905-short-quota-provenance

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

✅ Deterministic PR hygiene checks passed.

@github-actions github-actions Bot added the bug Something isn't working label Sep 5, 2026
agentHits pushed a commit to agentHits/opencodex that referenced this pull request Sep 17, 2026
…idge-jun#3605)

Owner-authorized admin squash. Typecheck/static verification completed; no local suite. Final dev Linux CI is the batch gate. Contributor credit is preserved in commits. Container execution, where applicable, remains unverified locally.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant