Skip to content
Source
Sign inSign up
pingdotgg/t3code

perf(usage): the first usage scan skips decoding transcript lines that carry no usage #17255

Mergedtris203(GitHub profile, opens in a new tab)merged 1 commit intomain fromrefs/pull/17255/headmerged by maria-rcks View on GitHub

Problem

The first usage scan after install or a cache version bump reads every transcript in the window. readTranscriptRecords decoded each line to a UTF-8 string and only then ran the format's substring gate, which decides whether the line can carry usage. Most lines are tool output that fails the gate, so most of that decoding was thrown away.

For Codex the gate was also four separate substring passes over every non-matching line.

Change

A transcript format now declares the ASCII substrings its relevant lines contain, and the reader matches them against the raw line bytes. Only lines that pass are decoded and parsed. The markers are ASCII, so matching bytes selects exactly the lines the string check did.

  • TranscriptUsageFormat in provider-core replaces the mightCarryUsage(line: string) predicate with usageMarkers, a non-empty list of strings. The Claude, Codex and Grok formats list the same substrings their predicates searched for, so each format still has one definition of its gate.
  • transcriptLineGate in the reader turns the markers into a byte test. One marker is a Buffer.includes.
  • Several markers are matched in one pass, as an alternation over the line's latin1 view. Latin1 maps each byte to one character without validating it, so an ASCII marker matches exactly where its bytes do. Measured on an earlier revision of this branch, four Buffer searches brought Codex to about 1,230 ms and the single pass to about 980 ms. The same route was slower for Claude (about 510 ms against 335 ms), which is why a single marker keeps the plain search.
  • Lines that span stream chunks and the unterminated tail go through the same gate once their chunks are joined. The reader for lines over 8 MiB never had a gate and is unchanged.

Scope and approval

Focused performance fix with no intended behavior change. No prior issue or discussion. The format interface changes because the gate has to see bytes; the three built-in formats are its only implementers. It deliberately leaves out parse concurrency and any change to the OpenCode reader, aggregation, or caching. It only speeds up a scan that has to parse files; warm scans served from the cache are unaffected.

Verification

Environment: Linux, Node v24.3.0, real transcript directories read-only (~/.claude/projects, ~/.codex/sessions), 30 day window (sinceMs = now - 30d - 36h). Measured against main at b1ec4b3687: total time of readTranscriptRecords over every file from listTranscriptFiles, read sequentially, one process per run, with perf_hooks.monitorEventLoopDelay for the event-loop figure. A throwaway pass ran first, so these are warm file-cache numbers.

FilesSizeWall time (ms)Max event-loop delay (ms)
Claude, main159305 MB639, 699, 705, 642, 6404.0, 4.5, 6.8, 5.3, 6.6
Claude, this change385, 412, 376, 386, 3985.9, 6.4, 3.0, 6.7, 7.7
Codex, main345853 MB1920, 2046, 1966, 2024, 19549.5, 10.6, 10.3, 9.8, 10.3
Codex, this change1165, 1046, 1015, 1000, 103912.7, 5.4, 4.0, 6.3, 7.3

For every one of those files the full parse result is deeply equal between main's reader and this change: records, tail records, and the resume position including guard hash and saved Codex state (17,783 Claude records, 15,832 Codex records).

New tests in usageTranscriptReader.test.ts:

  • transcriptLineGate: single and multiple markers, a marker containing regex metacharacters, near-miss text, and markers next to multi-byte and malformed UTF-8.
  • keeps a CRLF Codex reducer line whose marker straddles a stream chunk: multi-byte filler places the 256 KiB chunk boundary inside "turn_context", and the following usage is still attributed to that model.

Checks:

  • vp test run on usageTranscriptReader.test.ts, usageTranscriptStreaming.test.ts, usageScanCache.test.ts, claudeUsage.test.ts and codexUsage.test.ts in apps/server: 53 passed. usage.test.ts in provider-grok: 10 passed. usage.test.ts in provider-core: 1 passed.
  • tsc --noEmit in apps/server, packages/provider-core and packages/provider-grok: no errors. vp fmt --check on the touched directories: clean. vp lint on them: three existing unused-variable warnings in files this PR does not touch.

Not checked: Grok transcripts on real data (covered by the unit tests only), a cold file cache, the end-to-end Usage page load time in a client, and macOS or Windows.

Model: Claude Opus 5.5. Harness: Claude Code in T3 Code. Reviewed by GPT-6.1-Sol via Codex before the rebase onto the driver-owned formats; not re-reviewed since.

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

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

      Approvability

      Verdict: Not approved

      Macroscope's review found this PR not approvable — This focused performance change modifies the shared production transcript reader to automatically skip decoding and parsing most lines, with provider declarations controlling that decision across Claude, Codex, and Grok. Tests cover the gate and streaming boundaries, but the change still controls substantial processing in real usage scans rather than an operator-selected path.

      Notes:

      • Diff unchanged. Approvability was decided on eligibility alone.
      You can add or adjust custom eligibility rules. [Learn more](https://docs.macroscope.com/approvability#example-custom-eligibility-rules).
    3. 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: Advanced
      • Run ID: 44410e2f-cff3-40a0-852b-a37328101760

      📥 Commits

      Reviewing files that changed from the base of the PR and between 228952494ec585ec375864a20e570d1ead44c5b4 and 481ec9cb2b44cd1bf363eb8d4a6f7de97ebc29b2.


      📒 Files selected for processing (6)
      • apps/server/src/provider/Drivers/claudeUsage.ts
      • apps/server/src/provider/Drivers/codexUsage.ts
      • apps/server/src/usage/usageTranscriptReader.test.ts
      • apps/server/src/usage/usageTranscriptReader.ts
      • packages/provider-core/src/server/usage.ts
      • packages/provider-grok/src/server/usage.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.



      📝 Walkthrough

      Walkthrough

      Transcript formats now declare usage markers. The transcript reader checks raw line bytes against those markers before decoding and parsing matching lines. Tests cover marker matching, byte edge cases, and a Codex chunk-boundary case.

      Changes

      Transcript line filtering

      Layer / File(s)Summary
      Usage marker contract and provider declarations
      packages/provider-core/src/server/usage.ts, apps/server/src/provider/Drivers/claudeUsage.ts, apps/server/src/provider/Drivers/codexUsage.ts, packages/provider-grok/src/server/usage.ts
      TranscriptUsageFormat now requires non-empty ASCII usageMarkers. Claude, Codex, and Grok formats declare the markers used by their usage parsers.
      Raw-buffer filtering and parsing
      apps/server/src/usage/usageTranscriptReader.ts, apps/server/src/usage/usageTranscriptReader.test.ts
      The reader checks raw line buffers before decoding matching lines for parsing. Tests cover exact and altered markers, multibyte and malformed UTF-8 bytes, and a Codex marker that crosses a stream-chunk boundary.

      Priority: ➖ Normal

      Estimated code review effort: 2 (Simple) | ~15 minutes

      Change: Refactor

      Suggested reviewers: juliusmarminge


      Merge Risk: ⚪ Minimal · up to 481ec

      This change speeds up the first usage scan by skipping lines that carry no usage, and the reported results show no change in parsed output. No merge-blocking risk is evident.

      Security Architecture Review

      Security architecture risk: 🔵 Low · up to 481ec

      The built-in providers preserve their existing line-selection and parser validation behavior. No introduced security concern was identified, but compatibility with external implementations of the changed interface remains unverified.

      Retained concerns No architecture-level concerns identified.

      Security review details

      Security Blast Radius

      • inferred — The changed processing affects usage-record production and the existing per-file scan cache for built-in provider transcripts. An attacker able to influence those files can trigger marker matching, as before; the comparison does not show expanded file-read authority or a new privileged outcome.

      Trust Boundaries and Controls

      • observed — A marker match is only a parsing selector, not authentication or proof of valid usage. Matching transcript bytes still enter the existing JSON and provider-shape checks. Malformed JSON is rejected, and record eligibility checks remain in the provider parsers.

      Resilience and Maintainability Implications

      • observed — Existing recovery controls preserve attribution and cached data: resume requires valid saved state and matching guard bytes, read failures return null rather than an empty successful result, and the caller retains cached records on failure. Concurrent cache-update ordering and serialized persistence are unchanged by this PR.


      Pre-merge checks | Passed 4
      ✅ Passed checks (4 passed)
      Check nameStatusExplanation
      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.
      Title checkPassedThe title clearly and concisely describes the main performance change: skipping decoding for transcript lines that do not carry usage.
      Description checkPassedThe description includes all required sections. It explains the problem, implementation, scope and approval rationale, detailed verification results, limitations, and the model and harness used.


      ✨ Finishing Touches
      🧪 Generate unit tests (beta)
      • Create a new PR


      • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

      Comment `@coderabbitai help` to get the list of available commands.
    4. juliusmarminge added labelsmacroscope-review

    5. maria-rcks merged this7f02b84

    6. maria-rcks closed this

Sign in to join this conversation.