Skip to content

perf(native-chat): LRU-bound the pending-send and command-marker scope caches - #7643

Merged
nwparker merged 1 commit into
mainfrom
nwparker/leak-native-chat-scope-caches
Jul 7, 2026
Merged

nwparker merged 1 commit into
mainfrom
nwparker/leak-native-chat-scope-caches

Conversation

@nwparker

@nwparker nwparker commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Description

Two module-level caches in native-chat-pending.ts capped 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 by paneKey\0agent\0sessionId. sessionId changes on every /clear and paneKey embeds an ephemeral leafId, so each (pane, session) stranded a key. The only removal was a test-only clear().
  • 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 setBoundedScopeCacheEntry LRU 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-set keeps 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:

  • WITHOUT the cap: all 133 keys remain readable — the two eviction tests fail.
  • WITH the fix: the oldest keys evict, the most-recent 128 survive. ✅

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 /clear or 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 🐋

@coderabbitai

coderabbitai Bot commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 6d6f11f4-df87-4e57-bfc9-cd70c7248c64

📥 Commits

Reviewing files that changed from the base of the PR and between a56e5220bacce7f92952b4c83a5f2f3c12ae3e08 and ba0f9c8.

📒 Files selected for processing (2)
  • src/renderer/src/components/native-chat/native-chat-pending.test.ts
  • src/renderer/src/components/native-chat/native-chat-pending.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/components/native-chat/native-chat-pending.test.ts
  • src/renderer/src/components/native-chat/native-chat-pending.ts

📝 Walkthrough

Walkthrough

This 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

  • Import and use setBoundedScopeCacheEntry in place of direct .set() calls for pendingSendCache and commandMarkerCache.
  • Add inline comments documenting the ephemeral scope-key growth issue.
  • Add regression tests confirming key-count-based eviction for both caches.

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)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description covers the change and tests, but it does not follow the repository's required template sections. Rewrite the PR description using the required sections: Summary, Screenshots, Testing, AI Review Report, Security Audit, and Notes.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly reflects the main change: LRU-bounding the pending-send and command-marker scope caches.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
src/main/ipc/pty.test.ts (1)

3319-3430: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider covering the exit-flush and interactive-send droppedBacklog paths too.

These new tests exercise droppedBacklog only through the standard batched flushPendingData path. The PR also threads the flag through sendPtyExitToRenderer (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.ts
  • src/main/ipc/pty.ts
  • src/preload/api-types.ts
  • src/preload/index.ts
  • src/renderer/src/components/native-chat/native-chat-pending.test.ts
  • src/renderer/src/components/native-chat/native-chat-pending.ts
  • src/renderer/src/components/terminal-pane/pty-connection.ts
  • src/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.
@nwparker
nwparker force-pushed the nwparker/leak-native-chat-scope-caches branch from a56e522 to ba0f9c8 Compare July 7, 2026 04:15
@nwparker
nwparker merged commit 8936225 into main Jul 7, 2026
1 check passed
@nwparker
nwparker deleted the nwparker/leak-native-chat-scope-caches branch July 7, 2026 04:27
enrick-dev pushed a commit to enrick-dev/orca that referenced this pull request Oct 15, 2026
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant