Repository navigation
perf(native-chat): LRU-bound the pending-send and command-marker scope caches - #7643
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between a56e5220bacce7f92952b4c83a5f2f3c12ae3e08 and ba0f9c8. 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughThis change modifies native-chat-pending.ts to route cache writes for pending send data and command marker data through a new setBoundedScopeCacheEntry helper instead of direct map .set() calls, addressing unbounded growth of scope keys in the pendingSendCache and commandMarkerCache maps. Inline comments explain the rationale. A corresponding test file adds a regression test suite verifying that both caches evict the oldest scope entries once a key-count cap is exceeded, while retaining data for the most recently used scopes. Changes
Sequence Diagram(s)Included in the hidden review stack artifact above. Related PRs: None identified. Suggested labels: bug, memory-leak, tests Suggested reviewers: None identified. Poem: A rabbit hopped through caches deep, / Counting keys it meant to keep, / "Too many scopes!" it gave a shout, / And nudged the oldest ones right out, / Now bounded caches softly sleep. 🐇 🚥 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/ipc/pty.test.ts (1)
3319-3430: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider covering the exit-flush and interactive-send droppedBacklog paths too.
These new tests exercise
droppedBacklogonly through the standard batchedflushPendingDatapath. The PR also threads the flag throughsendPtyExitToRenderer(pty.ts lines 1774-1775) and the interactive immediate-send branch (pty.ts lines 1888-1889); neither is covered here, so a regression in either path (e.g., a dropped backlog right before a PTY exits, or during a keystroke-latency-sensitive burst) wouldn't be caught.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Repository UI
Review profile: CHILL
Plan: Pro
Run ID: 00cf525d-fba9-4773-851c-4e9eaa89bd4a
📥 Commits
Reviewing files that changed from the base of the PR and between 98d850a and a56e5220bacce7f92952b4c83a5f2f3c12ae3e08.
📒 Files selected for processing (8)
src/main/ipc/pty.test.tssrc/main/ipc/pty.tssrc/preload/api-types.tssrc/preload/index.tssrc/renderer/src/components/native-chat/native-chat-pending.test.tssrc/renderer/src/components/native-chat/native-chat-pending.tssrc/renderer/src/components/terminal-pane/pty-connection.tssrc/renderer/src/components/terminal-pane/pty-dispatcher.ts
…e caches Two module-level caches in native-chat-pending.ts capped their per-key arrays (8 entries) but never bounded the KEY count: - commandMarkerCache — keyed by paneKey\0agent\0sessionId; sessionId changes on every /clear and paneKey embeds an ephemeral leafId, so a distinct key was stranded per (pane, session) for the renderer's whole life. Only test-only clear() ever removed keys. - pendingSendCache — self-cleans on the normal empty-drain path, but a pane closed with an unconfirmed send (agent crash / early close) left its non-empty entry keyed forever. Route both writes through the existing setBoundedScopeCacheEntry LRU helper (cap 128) that #7566 already applied to the draft/attachment caches in the same folder — same pattern, same file family, was just missed here. Values are tiny so this is a slow leak, but real and unbounded. Tests (red->green): appending 133 distinct scopes evicts the oldest and keeps the most-recent 128; without the cap all 133 survive.
a56e522 to
ba0f9c8
Compare
…e caches (stablyai#7643) Two module-level caches in native-chat-pending.ts capped their per-key arrays (8 entries) but never bounded the KEY count: - commandMarkerCache — keyed by paneKey\0agent\0sessionId; sessionId changes on every /clear and paneKey embeds an ephemeral leafId, so a distinct key was stranded per (pane, session) for the renderer's whole life. Only test-only clear() ever removed keys. - pendingSendCache — self-cleans on the normal empty-drain path, but a pane closed with an unconfirmed send (agent crash / early close) left its non-empty entry keyed forever. Route both writes through the existing setBoundedScopeCacheEntry LRU helper (cap 128) that stablyai#7566 already applied to the draft/attachment caches in the same folder — same pattern, same file family, was just missed here. Values are tiny so this is a slow leak, but real and unbounded. Tests (red->green): appending 133 distinct scopes evicts the oldest and keeps the most-recent 128; without the cap all 133 survive.
Description
Two module-level caches in
native-chat-pending.tscapped their per-key arrays (8 entries) but never bounded the number of keys — and the keys are ephemeral, so distinct panes/sessions accumulated for the renderer's whole session:commandMarkerCache(:210) — keyed bypaneKey\0agent\0sessionId.sessionIdchanges on every/clearandpaneKeyembeds an ephemeralleafId, so each(pane, session)stranded a key. The only removal was a test-onlyclear().pendingSendCache(:28) — self-cleans on the normal empty-drain path, but a pane closed with an unconfirmed send (agent crash / pane closed before the transcript advances) left its non-empty entry keyed forever.Values are tiny (≤8 small objects) so this is a slow leak, but real and unbounded — the same class as the draft/attachment caches #7566 fixed in this exact folder.
Fix
Route both writes through the existing
setBoundedScopeCacheEntryLRU helper (native-chat-composer-scope-cache.ts, cap 128) that #7566 already applied to the sibling caches — same pattern, same file family, just missed here.delete-then-setkeeps the actively-used scope most-recent so only the oldest untouched scopes are shed.Evidence (red → green)
New tests append 133 distinct scopes to each cache:
native-chat-pending.test.ts— 29 tests green; oxlint clean.ELI5
The chat view keeps little sticky notes about each pane's queued messages and slash-command feedback. It limited how many notes per pane but not how many panes — and every
/clearor new session counted as a new pane, so the note pile grew forever. Now it keeps notes for only the 128 most-recent panes and quietly drops the oldest.Trade-offs / regression risk: none
Only the oldest untouched scopes are evicted; any actively-used pane stays most-recent and is never dropped. An evicted scope simply starts empty next time (these are convenience/optimistic-UI caches, not source of truth).
Made with Orca 🐋