perf: prune per-thread client state and evict idle VCS status cache #15301
main fromrefs/pull/15301/head·opened View on GitHubProblem#
Long-lived sessions accumulated state that was never released: deleting a thread left entries behind in the right-panel, diff-panel, mini-player, and ui-state stores (the zustand removeThread helpers existed with no callers, and ui-state had no removal at all — these persist to localStorage, so the growth survives restarts), previewStateAtom used Atom.keepAlive so one registry node per thread ever visited lived for the tab's lifetime, a released browser surface left a tombstone in byTabId per preview tab ever opened, and the server's VcsStatusBroadcaster kept its per-cwd status cache for the process lifetime after the last poller subscriber released. Re-ports the still-applicable parts of #4176, which was closed when #2829 deleted its V1 base.
Change#
- Web, thread deletion (
useThreadActions.ts): both delete paths (live and archived) now prune every per-thread store — preview state, right panel, diff panel, preview mini player, and ui state — and the archived path additionally releases composer draft uploads before clearing the draft, matching the live path's order. - Web, preview atoms (
previewStateStore.ts):previewStateAtommoves fromAtom.keepAliveto a 5-minute idle TTL. Threads with live preview sessions stay resident because the observedactivePreviewSessionsAtomreads them (so the desktop webview host and automation hosts are unaffected); idle threads are collected and re-seed from EMPTY, repopulated by the next server snapshot viausePreviewSession'spreview.listreconcile. AddsremovePreviewThreadfor explicit cleanup on deletion. - Web, browser surfaces (
browserSurfaceStore.ts):releasedeletes the entry instead of writing a{ visible: false, owner: null }tombstone. All readers already treat a missing entry as not-visible/not-present, so this is behavior-preserving, and a stalepresent()from a released lease can no longer resurrect the entry. - Server, VCS cache (
VcsStatusBroadcaster.ts): when the last poller subscriber releases, the cwd's cached status is dropped inside the samemodifyEffectcritical section that removes the poller, so a concurrentretainRemotePollercannot have its freshly seeded entry wiped. A later subscriber re-loads and re-seeds the cache (its poller starts with an immediate refresh).
The orchestration read-model half of #4176 is intentionally not ported: orchestration V2 loads per-thread state from indexed SQLite inside each command's dispatch (per-thread keyed lock, one commit transaction) and holds no process-wide in-memory read model, so the O(N) scans and unbounded in-memory growth that change removed no longer exist.
No visible UI changes.
Scope and approval#
Triaged bug issue: #4178. Maintainer approval of direction and scope: juliusmarminge on #4176 — "Once #2829 merges, please rebase onto main, port the change to the V2 equivalent, and reopen (or open a fresh PR). Ping me and I'll prioritise the review. This is not a judgement on the change itself. Several of these are real gaps we still want fixed." These changes are the port of the parts whose underlying code still exists; the web and server changes are one fix because they are the client and server halves of the same unbounded-retention problem from that issue.
Verification#
apps/server/src/vcs/VcsStatusBroadcaster.test.ts— extended the existing last-subscriber-disconnects test: after the final poller releases, a new subscription reloads local status (localStatusCalls1 → 2). Verified against unmodifiedmainthat this assertion fails (cache retained, counter stays 1) and passes with the change.apps/web/src/previewStateStore.test.ts— new test:removePreviewThreadresets a deleted thread's state to EMPTY, leaves other threads intact, and a later event for the removed thread still lands.apps/web/src/uiStateStore.test.ts— new test:removeThreadUiStatedrops both per-thread records and is a reference-preserving no-op for unknown threads.apps/web/src/browser/browserSurfaceStore.test.ts— release now asserts the entry is gone (and a stalepresent()cannot resurrect it); the fitted-presentation release test updated to match.- Focused runs: 159/159 passing across the six touched test files;
tsc --noEmitclean for@t3tools/webandt3; targetedvp lintclean (one pre-existing warning onmain, untouched lines). - Not checked: no integrated pass in a running client — there are no visible UI changes to capture, and store semantics are covered by the unit tests above.
Closes #4178.
Done with GLM-5.3 via the Claude Code harness in T3 Code.
🤖 Generated with Claude Code
- 1
github-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
Note
Written by
claude-opus-5-5on behalf of MariaHi! We are cleaning up open PRs, and this one names a harness (Claude Code) but not which model version created it. If this change is really important, we recommend rebuilding the PR with a newer model and noting the model in the PR description.
maria-rcks closed this
Sign in to join this conversation.
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
🧰 Additional context used
📚 Code guidelines (1)
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
8dd7f264-f7b6-4970-a393-3b59d03c14f1📥 Commits
Reviewing files that changed from the base of the PR and between 71dbaf1f94b580491d033f2e347a9b1c507fd793 and 0e1ffe49dfd7fd8e8f6ff5e2f54186ca885cc1a3.
📒 Files selected for processing (9)
apps/server/src/vcs/VcsStatusBroadcaster.test.tsapps/server/src/vcs/VcsStatusBroadcaster.tsapps/web/src/browser/browserSurfaceStore.test.tsapps/web/src/browser/browserSurfaceStore.tsapps/web/src/hooks/useThreadActions.tsapps/web/src/previewStateStore.test.tsapps/web/src/previewStateStore.tsapps/web/src/uiStateStore.test.tsapps/web/src/uiStateStore.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough#
The changes remove cached VCS status when the final remote poller releases, clear per-thread client state after successful thread deletion, and remove browser surface entries when their owner releases them.
Changes#
VCS status cache
apps/server/src/vcs/VcsStatusBroadcaster.ts,apps/server/src/vcs/VcsStatusBroadcaster.test.tsPer-thread client state
apps/web/src/previewStateStore.ts,apps/web/src/previewStateStore.test.ts,apps/web/src/uiStateStore.ts,apps/web/src/uiStateStore.test.tsapps/web/src/hooks/useThreadActions.tsBrowser surface release
apps/web/src/browser/browserSurfaceStore.ts,apps/web/src/browser/browserSurfaceStore.test.tsPriority: ⬆️ High
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Bug fix · Severity of issue fixed: High
Suggested reviewers:
maria-rcks,juliusmarmingeMerge Risk: ⚪ Minimal · up to
0e1ffThe change prunes per-thread client state and evicts idle VCS status cache, and no concrete merge-blocking risk was identified. The author did not run an integrated check in a live client, but that is normal residual uncertainty.
Security Architecture Review#
Security architecture risk: 🔵 Low · up to
0e1ffThe cleanup generally reduces retained state and preserves thread isolation. One lifecycle question remains: discarded preview state also contains safeguards against stale updates, which could allow a previously closed preview to reappear. No new unauthorized access or privilege gain was established.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Hardening Proposals
🚥 Pre-merge checks | ✅ 3 | ❌ 1 | ❓ 1
❌ Failed checks (1 warning, 1 inconclusive)#
#4178], the diff wires cleanup into live and archived thread deletion, adds idle TTL and explicit preview cleanup, removes released browser-surface entries, and evicts VCS status when the last po…✅ Passed checks (3 passed)
#4178]'s retained client and VCS state. The browser-surface tombstone cleanup and archived draft-upload cleanup support the same thread and preview lifecycle cleanup. No unrelated…Full details: Linked Issues check
Explanation
For [
#4178], the diff wires cleanup into live and archived thread deletion, adds idle TTL and explicit preview cleanup, removes released browser-surface entries, and evicts VCS status when the last poller releases. The diff also adds focused tests for these behaviors. The PR says the orchestration V1 read-model problem no longer exists in V2, but the inspected evidence does not establish that V2 has no equivalent process-wide read model or lifetime-thread-count scaling.✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment `@coderabbitai help` to get the list of available commands.