Repository navigation
perf(native-chat): byte-cap the transcript read-cache to bound RSS (GB-scale leak) - #7625
Conversation
The process-global transcript read-cache (transcript-read-cache.ts) was capped only by entry count (MAX_CACHE_ENTRIES = 50). Each entry is a FULL, unwindowed transcript parse: readNativeChatTranscript applies no message cap and tool-result strings (whole file contents, command output, diffs) are kept in full. Heavy Claude/Codex sessions are 5-50 MB of JSONL -> 10-100 MB parsed, so 50 entries could retain 0.5-several GB in the one process that now serves desktop plus every paired web/mobile client. The file's own comment flagged a byte-aware cap as the follow-up. Fix: reuse the stat() already issued for the mtime check to also read .size (a cheap, monotonic proxy for the parsed footprint) and bound the cache by total cached file bytes (MAX_CACHE_BYTES = 128 MB) in addition to the count cap. Evict oldest until under budget, but always keep the most-recent entry so a single active transcript larger than the whole budget is never re-parsed on every read. This is distinct from #7341, which fixed the cache KEY (resolved path vs agent:sessionId); the entry SIZE dimension was untouched. Zero behavior change: it's a read-through cache, so an evicted entry is just re-read from disk on next access (already the cache-miss path). The only cost is extra re-parses if a user's simultaneously-hot transcripts exceed 128 MB - exactly the OOM-risk case this bounds; typical use is unaffected. Tests (red->green): without the byte condition the eviction test fails (oldest entry stays cached); with it, oldest large entries evict, the newest is always kept, and small entries under budget never evict.
📝 WalkthroughWalkthroughThe transcript cache used by Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant readNativeChatTranscriptCached
participant statHelper
participant setCached
Caller->>readNativeChatTranscriptCached: request transcript
readNativeChatTranscriptCached->>statHelper: get mtimeMs and bytes
statHelper-->>readNativeChatTranscriptCached: mtimeMs, bytes
readNativeChatTranscriptCached->>setCached: store result, mtimeMs, bytes
setCached->>setCached: evict oldest entries until maxCacheBytes and maxEntries satisfied
setCached-->>readNativeChatTranscriptCached: cache updated
Related issues: None linked in the provided context. Related PRs: None identified in the provided context. Suggested labels: cache, testing Suggested reviewers: None specified in the provided context. Poem 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
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. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/main/native-chat/transcript-read-cache.ts (1)
35-42: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low valueTrim the rationale comment to align with the comment-length guideline.
This 7-line block explains useful context, but as per path instructions, design-rationale comments should stay to "one or two lines, capturing only the non-obvious reason." Consider condensing to the core "why" (count cap alone let unbounded JSONL retain multi-GB memory; byte budget bounds it while keeping the most-recent entry).
As per path instructions for
**/*.{ts,tsx,js,jsx}: "Keep comments short — one or two lines, capturing only the non-obvious reason."Source: Path instructions
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: f29a6e90-1b75-4e49-a586-95b9c70ca2d7
📒 Files selected for processing (2)
src/main/native-chat/transcript-read-cache.test.tssrc/main/native-chat/transcript-read-cache.ts
Description
The process-global native-chat transcript read-cache (
src/main/native-chat/transcript-read-cache.ts) was capped only by entry count (MAX_CACHE_ENTRIES = 50) — with no byte cap. Each cached entry is a full, unwindowed transcript parse:readNativeChatTranscriptapplies no message cap (streams the entire JSONL session file).toolResultOutputreturns tool-result strings in full — whole file contents, command output, and diffs, which are the bulk of a real agent transcript.Heavy Claude/Codex coding sessions are routinely 5–50 MB of JSONL on disk → 10–100 MB parsed in memory. So 50 entries could retain 0.5–several GB in the single process that now serves desktop + every paired web/mobile client. The file's own comment already flagged this:
Reached from both the desktop IPC handler (
ipc/native-chat.ts) and the runtime RPC handler that serves every remote client (runtime/rpc/methods/native-chat.ts), and re-cached on every agent write during an active session (file mtime changes). No purge on session/pane/worktree removal — entries persist for the process lifetime, subject only to the 50-entry LRU.Fix
Reuse the
stat()already issued for the mtime check to also read.size(a cheap, monotonic proxy for the parsed footprint), and bound the cache by total cached file bytes (MAX_CACHE_BYTES = 128 MB) in addition to the count cap:cache.size > 1), so a single active transcript larger than the whole budget is never re-parsed on every read.Evidence (red → green)
New tests in
transcript-read-cache.test.ts(with a test-only budget override so the byte-eviction path is exercised without 100 MB fixtures):readSpy3 ≠ 4). Leak reproduced.Verification:
transcript-read-cache.test.ts— 8 tests ✅ (5 original + 3 new)ipc/native-chat.test.ts+runtime/rpc/methods/native-chat.test.ts— 18 total green ✅oxlintclean,tsgonode typecheck clean ✅ELI5
Orca keeps a shelf of the last 50 chat logs it read so it doesn't re-read them from disk. It counted logs, not their size — but a single AI coding log can be 100 MB. Fifty of those = a few GB just sitting on the shelf. Now it also watches the total size and clears out old big ones (always keeping the one you're currently looking at), so the shelf can't grow past ~128 MB.
Trade-offs / regression risk: near-zero
Note
This is one of two massive-scale candidates found this pass. The other — an uncapped unsent PTY backlog (
pendingDatainmain/ipc/pty.ts) — is under separate verification (its fix risks dropping terminal output, so reachability is being confirmed first) and will be a separate PR if it holds up.Made with Orca 🐋