Skip to content
Source
Sign inSign up
pingdotgg/t3code

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

Mergedjuliusmarmingemerged 18 commits intomain fromrefs/pull/21899/headmerged by juliusmarminge View on GitHub

juliusmarmingecommented

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.

    1. juliusmarminge changed the title

    2. juliusmarminge added labelsmacroscope-review

    3. macroscopeapp[bot](GitHub profile, opens in a new tab)commented

      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](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    4. github-actions[bot](GitHub profile, opens in a new tab)commented

      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.

      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 decoded20.9 KiB20.9 KiB0 B (0.0%)29.3 KiB✅
      CodexLive turn messages220 (0.0%)8✅
      ClaudeTotal thread wire5.0 KiB5.0 KiB0 B (0.0%)6.8 KiB✅
      ClaudeThread snapshot wire3.8 KiB3.8 KiB0 B (0.0%)4.9 KiB✅
      ClaudeLive turn WebSocket wire1.2 KiB1.2 KiB0 B (0.0%)2.0 KiB✅
      ClaudeLive turn WebSocket decoded21.2 KiB21.2 KiB0 B (0.0%)29.3 KiB✅
      ClaudeLive turn messages220 (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.

    5. coderabbitai[bot](GitHub profile, opens in a new tab)commented
      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 a73fdebec7260f138427353cd894acff8db522c1 and ee0b49027a78586e46e2c777c68e90a75c307bb3.

      📒 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

      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 nameStatusExplanation
      Title checkPassedThe title clearly and concisely describes the primary server-side change: recovering missing initial Codex usage tiers.
      Description checkPassedThe description includes all required sections and provides clear problem context, implementation details, scope approval, verification results, limitations, and testing evidence.
      Linked Issues checkPassedCheck skipped because no linked issues were found for this pull request.
      Out of Scope Changes checkPassedCheck 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.
    6. github-actions[bot](GitHub profile, opens in a new tab) added labels📱 Native Change

    7. github-actions[bot](GitHub profile, opens in a new tab) removed labels📱 Native Change

    8. juliusmarminge merged thisd243b09

    9. juliusmarminge closed this

Sign in to join this conversation.