Skip to content

fix(server): recover missing initial Codex usage tiers - #21899

Merged
juliusmarminge merged 18 commits into
mainfrom
codex-08/codex-initial-usage-tier
Oct 20, 2026
Merged

juliusmarminge merged 18 commits into
mainfrom
codex-08/codex-initial-usage-tier

Conversation

@juliusmarminge

@juliusmarminge juliusmarminge commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

Problem

Codex can omit initial rollout settings for a Fast or Ultrafast request. Those first records then use Standard pricing even when T3 retained the original selection.

Change

Recover the initial tier only when the own OpenAI session, native turn, model, and unambiguous first-running V2 selection match. Native rollout settings stay authoritative. Pending records retry without file growth and survive restarts; successful corrections persist without changing parser tier state. The provider reader owns historical resolution through an optional generic hook.

Cache v7 preserves v6/v5/original fallbacks, cold-parses available older Codex transcripts, and retains saved prices for deleted files. It preserves compatible v6 positions for other providers, including Pi, and retains cumulative-counter reconciliation.

Rebuilt from @PixPMusic's #15275. Depends on the historical lookup layer and #21863.

Scope and approval

Authorized by the maintainer takeover. Related to #15267, which stays open: imported V1 histories without retained V2 attempts are unchanged. Historical accounting, SQL, and cache changes still need human maintainer review. No client or wire contract changes; every client consumes the shared server summary.

Verification

Four fresh parallel review rounds with Opus 5.5 and GPT-6 Sol covered both layers. They caught and fixed writer-thread SQL execution and unnecessary v6 Pi cache invalidation. CI also exposed a stale migration-64 test expectation, which now includes migration 65. The final round found no blockers.

  • 124 passing tests across nine existing files, up from 116 before these two layers. Eight added cases replace 24 cases in the original proof of concept. They cover ambiguity, first-running history, exact provenance, native precedence, unchanged-file retry, SQL outage/recovery, restarts, committed/tentative records, resumed tails, projected large records, and cache compatibility.
  • Scoped server/provider-core typechecks, lint, formatting, and export checks passed. Production-only export audit still reports existing unrelated unused exports; it is not claimed clean.
  • Headless Linux app proof at c5b0b1ecde: a synthetic 240K-token rollout uses a Fast selection stored by normal orchestration commands, followed by explicit native Standard settings. The estimate changes from $4.00 to $6.00: Fast $4.00 plus Standard $2.00. A cold scan after removing the sandbox v7 cache and a reload preserve the result. Production is identical after the later migration-test expectation correction. This uses a mock provider and real stored request history; it does not verify a real Codex 0.160 rollout or provider invoice.
  • Windows, macOS, iOS, Android, and Electron were not run. V1 backfill remains outside this change.

Before — initial records priced Standard:

Before: 240K tokens estimated at $4.00

After — initial Fast recovered, later Standard preserved:

After: same 240K tokens estimated at $6.00, Fast $4.00 and Standard $2.00

Implementation: GPT-6.1 Sol; tests: GPT-6 Astra. Harness: Codex in T3 Code.

@juliusmarminge
juliusmarminge added this pull request to stack #21873 October 19, 2026 20:03
@github-actions github-actions Bot added the vouch:trusted PR author is trusted by repo permissions or the VOUCHED list. label Oct 19, 2026
@juliusmarminge juliusmarminge changed the title codex 08/codex initial usage tier fix(server): recover missing initial Codex usage tiers Oct 19, 2026
@juliusmarminge juliusmarminge added the macroscope-review Opt PRs made by unvouched contributors in for Macroscope review. Vouched contributors auto-reviews label Oct 19, 2026
@github-actions github-actions Bot added the size:L 100-499 changed lines (additions + deletions). label Oct 19, 2026
@macroscopeapp

macroscopeapp Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Needs Human Approval

Macroscope's review found this PR requires human approval — This PR changes production Codex usage parsing, historical reconciliation, and persisted cache data, potentially changing reported token costs for existing activity. The SQL lookup and cross-scan cache behavior warrant maintainer review of the accounting impact.

You can add or adjust custom eligibility rules. Learn more.

@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

ℹ️ The exact PR base did not have a successful artifact. Baseline uses the latest successful main measurement shown below.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 20.9 KiB 20.9 KiB 0 B (0.0%) 29.3 KiB ✅
Codex Live turn messages 2 2 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.2 KiB 21.2 KiB 0 B (0.0%) 29.3 KiB ✅
Claude Live turn messages 2 2 0 (0.0%) 8 ✅

Baseline: 348888a · PR result: ee0b490 · 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.

@coderabbitai

coderabbitai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Path: .coderabbit.config.ts
  • Review profile: CHILL
  • Plan: Team
  • Run ID: 216a2181-1ad6-4b5b-936f-cfb8c918cfda
📥 Commits

Reviewing files that changed from the base of the PR and between a73fdeb and ee0b490.

📒 Files selected for processing: 4
  • apps/server/src/server.ts
  • apps/server/src/usage/CodexUsageHistory.ts
  • apps/server/src/usage/UsageService.test.ts
  • apps/web/src/components/ChatMarkdown.tsx

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

📝 Walkthrough

Codex usage parsing now retains eligible unresolved service-tier evidence. UsageService resolves that evidence through Codex usage history, applies resolved speeds to aggregation, and persists corrections in scan caches. The cache format advances to version 7. Media-not-found handling also updates remote-open state.

Changes: Codex service-tier usage correction

Layer / File(s) Summary
Capture unresolved tier evidence
packages/provider-core/src/server/usage.ts, apps/server/src/provider/Drivers/codexUsage.ts, apps/server/src/provider/Drivers/codexUsage.test.ts, apps/server/src/usage/usageTranscriptStreaming.test.ts
Usage records and transcript readers support unresolved service-tier evidence. Codex parsing records provider, turn, model, and thread-settings context, then marks eligible records. Tests cover provider identity, context requirements, native tiers, and resumed transcripts.
Resolve evidence from Codex history
apps/server/src/provider/Drivers/CodexDriver.ts, apps/server/src/provider/Drivers/codexUsage.ts, apps/server/src/usage/CodexUsageHistory.ts, apps/server/src/server.ts, apps/server/src/usage/UsageService.test.ts
The Codex reader resolves marked records by session, turn, and model using Codex usage history. Server wiring provides the history service. History read errors report the requested turn count.
Apply corrections and persist scan data
apps/server/src/usage/UsageService.ts, apps/server/src/usage/usageScanCache.ts, apps/server/src/usage/usageScanCache.test.ts, apps/server/src/usage/UsageService.test.ts
UsageService retains corrections across resumed reads, applies them during aggregation, and updates cache entries. Scan-cache version 7 stores unresolved turn IDs and adjusts decoding and legacy-state rules. Tests cover cache upgrades and Codex pricing repair.

Remote media error state

Layer / File(s) Summary
Update remote-open target state
apps/web/src/components/ChatMarkdown.tsx
The media-not-found path now passes sshTarget: null when resolving remote-open state.

Priority: ➖ Normal

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

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant CodexTranscript
  participant UsageService
  participant codexUsageReader
  participant CodexUsageHistory
  participant ScanCache
  CodexTranscript->>UsageService: provide records with unresolved tier evidence
  UsageService->>codexUsageReader: resolveServiceTiers(records)
  codexUsageReader->>CodexUsageHistory: resolve session, turn, and model
  CodexUsageHistory-->>codexUsageReader: return resolved speeds
  codexUsageReader-->>UsageService: return record-to-speed map
  UsageService->>ScanCache: persist corrected records
Loading

Suggested reviewers: maria-rcks, t3dotgg, yash-singh1

Merge Risk: ⚪ Minimal · up to ee0b4

No issue requiring a fix before merge was identified in the inspected usage and cache paths.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary server-side change: recovering missing initial Codex usage tiers.
Description check Passed The description includes all required sections and provides clear problem context, implementation details, scope approval, verification results, limitations, and testing evidence.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ 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

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

@juliusmarminge
juliusmarminge force-pushed the codex-08/codex-initial-usage-tier branch from c5b0b1e to e5ef040 Compare October 19, 2026 20:28
# Conflicts:
#	apps/server/src/usage/UsageService.ts
@github-actions github-actions Bot added size:XXL 1,000+ changed lines (additions + deletions). and removed size:L 100-499 changed lines (additions + deletions). labels Oct 20, 2026
# Conflicts:
#	apps/server/src/usage/UsageService.test.ts
#	apps/server/src/usage/UsageService.ts
#	apps/server/src/usage/usageScanCache.test.ts
#	apps/server/src/usage/usageScanCache.ts
@github-actions github-actions Bot added the 📱 Native Change Changes the native fingerprint; merging blocks production OTAs until a new store build ships. label Oct 20, 2026
Base automatically changed from codex-08/codex-usage-history to main October 20, 2026 05:06
@github-actions github-actions Bot removed the 📱 Native Change Changes the native fingerprint; merging blocks production OTAs until a new store build ships. label Oct 20, 2026
Comment thread apps/server/src/usage/CodexUsageHistory.ts Outdated
@github-actions github-actions Bot added size:L 100-499 changed lines (additions + deletions). and removed size:XXL 1,000+ changed lines (additions + deletions). labels Oct 20, 2026
@juliusmarminge
juliusmarminge merged commit d243b09 into main Oct 20, 2026
31 checks passed
@juliusmarminge
juliusmarminge deleted the codex-08/codex-initial-usage-tier branch October 20, 2026 05:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

macroscope-review Opt PRs made by unvouched contributors in for Macroscope review. Vouched contributors auto-reviews size:L 100-499 changed lines (additions + deletions). vouch:trusted PR author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant