perf(usage): the first usage scan skips decoding transcript lines that carry no usage #17255
main fromrefs/pull/17255/head·merged by maria-rcks View on GitHubProblem#
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.
TranscriptUsageFormatinprovider-corereplaces themightCarryUsage(line: string)predicate withusageMarkers, 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.transcriptLineGatein the reader turns the markers into a byte test. One marker is aBuffer.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
Buffersearches 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.
| Files | Size | Wall time (ms) | Max event-loop delay (ms) | |
|---|---|---|---|---|
Claude, main | 159 | 305 MB | 639, 699, 705, 642, 640 | 4.0, 4.5, 6.8, 5.3, 6.6 |
| Claude, this change | 385, 412, 376, 386, 398 | 5.9, 6.4, 3.0, 6.7, 7.7 | ||
Codex, main | 345 | 853 MB | 1920, 2046, 1966, 2024, 1954 | 9.5, 10.6, 10.3, 9.8, 10.3 |
| Codex, this change | 1165, 1046, 1015, 1000, 1039 | 12.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 runonusageTranscriptReader.test.ts,usageTranscriptStreaming.test.ts,usageScanCache.test.ts,claudeUsage.test.tsandcodexUsage.test.tsinapps/server: 53 passed.usage.test.tsinprovider-grok: 10 passed.usage.test.tsinprovider-core: 1 passed.tsc --noEmitinapps/server,packages/provider-coreandpackages/provider-grok: no errors.vp fmt --checkon the touched directories: clean.vp linton 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
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
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.
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.tsapps/server/src/provider/Drivers/codexUsage.tsapps/server/src/usage/usageTranscriptReader.test.tsapps/server/src/usage/usageTranscriptReader.tspackages/provider-core/src/server/usage.tspackages/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.tsTranscriptUsageFormatnow requires non-empty ASCIIusageMarkers. 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.tsThe 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
481ecThis 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
481ecThe 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 |
4✅ Passed checks (4 passed)
Check name Status Explanation Linked Issues check Check skipped because no linked issues were found for this pull request. Out of Scope Changes check Check skipped because no linked issues were found for this pull request. Title check The title clearly and concisely describes the main performance change: skipping decoding for transcript lines that do not carry usage. Description check The 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.coderabbitai[bot](GitHub profile, opens in a new tab) approved these changes
juliusmarminge added labelsmacroscope-review
maria-rcks merged this7f02b84
maria-rcks closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed