diff --git a/src/renderer/src/store/slices/bulk-worktree-purge-terminal-maps-leak.test.ts b/src/renderer/src/store/slices/bulk-worktree-purge-terminal-maps-leak.test.ts new file mode 100644 index 000000000..726e2b58f --- /dev/null +++ b/src/renderer/src/store/slices/bulk-worktree-purge-terminal-maps-leak.test.ts @@ -0,0 +1,127 @@ +/** + * Memory-leak regression: seven per-tab / per-pty terminal+agent store maps were + * evicted on the SINGLE removeWorktree path (via closeTab / shutdownWorktreeTerminals) + * but NOT on the BULK path — `buildWorktreePurgeState`, reached by removeProject, the + * external-worktree-removal authoritative-scan reconcile, and the hydration-stale + * reconcile. That path runs no terminal teardown, so before the fix each map stranded + * an entry per tab/pane of every externally-removed worktree for the renderer's whole + * session (unbounded across add/remove-repo cycles). tabId and ptyId are ephemeral, + * never-reused key spaces. + * + * Tab-keyed (evicted via the doomed-tab set): + * lastKnownRelayPtyIdByTabId, pendingInitialCwdByTabId, + * pendingIssueCommandSplitByTabId, pendingSetupSplitByTabId, pendingStartupByTabId + * Pty-keyed (evicted via the doomed-pty set derived from ptyIdsByTabId): + * codexRestartNoticeByPtyId, migrationUnsupportedByPtyId + */ +import { describe, it, expect, vi, beforeEach } from 'vitest' +import type * as AgentStatusModule from '@/lib/agent-status' + +vi.mock('sonner', () => ({ + toast: { info: vi.fn(), success: vi.fn(), error: vi.fn(), warning: vi.fn() } +})) + +vi.mock('@/components/terminal-pane/pty-dispatcher', () => ({ + restorePtyDataHandlersAfterFailedShutdown: vi.fn(), + unregisterPtyDataHandlers: vi.fn() +})) + +vi.mock('@/lib/agent-status', async (importOriginal) => { + const actual = await importOriginal() + return { ...actual, detectAgentStatusFromTitle: vi.fn().mockReturnValue(null) } +}) + +const mockApi = { + worktrees: { list: vi.fn().mockResolvedValue([]), remove: vi.fn().mockResolvedValue(undefined) }, + pty: { kill: vi.fn().mockResolvedValue(undefined) } +} + +// @ts-expect-error -- minimal window.api stub for the store under test +globalThis.window = { api: mockApi } + +import { createTestStore, seedStore, makeWorktree, makeTab } from './store-test-helpers' + +const WT1 = 'repo1::/path/wt1' +const WT2 = 'repo1::/path/wt2' +const TAB1 = 'tab-wt1' +const TAB2 = 'tab-wt2' +const PTY1 = 'pty-wt1' +const PTY2 = 'pty-wt2' + +function seedMaps(store: ReturnType): void { + seedStore(store, { + worktreesByRepo: { + repo1: [ + makeWorktree({ id: WT1, repoId: 'repo1', path: '/path/wt1' }), + makeWorktree({ id: WT2, repoId: 'repo1', path: '/path/wt2' }) + ] + }, + tabsByWorktree: { + [WT1]: [makeTab({ id: TAB1, worktreeId: WT1, ptyId: PTY1 })], + [WT2]: [makeTab({ id: TAB2, worktreeId: WT2, ptyId: PTY2 })] + }, + // Drives the doomed-ptyId derivation in buildWorktreePurgeState. + ptyIdsByTabId: { [TAB1]: [PTY1], [TAB2]: [PTY2] }, + lastKnownRelayPtyIdByTabId: { [TAB1]: PTY1, [TAB2]: PTY2 }, + pendingInitialCwdByTabId: { [TAB1]: '/path/wt1', [TAB2]: '/path/wt2' }, + pendingIssueCommandSplitByTabId: { + [TAB1]: { command: 'a' }, + [TAB2]: { command: 'b' } + }, + pendingSetupSplitByTabId: { + [TAB1]: { command: 'setup-a', direction: 'vertical' }, + [TAB2]: { command: 'setup-b', direction: 'vertical' } + }, + pendingStartupByTabId: { [TAB1]: { command: 'start-a' }, [TAB2]: { command: 'start-b' } }, + codexRestartNoticeByPtyId: { + [PTY1]: { previousAccountLabel: 'a1', nextAccountLabel: 'a2' }, + [PTY2]: { previousAccountLabel: 'b1', nextAccountLabel: 'b2' } + }, + migrationUnsupportedByPtyId: { + [PTY1]: { + ptyId: PTY1, + paneKey: `${TAB1}:leaf-1`, + reason: 'legacy-numeric-pane-key', + source: 'local', + updatedAt: 1 + }, + [PTY2]: { + ptyId: PTY2, + paneKey: `${TAB2}:leaf-1`, + reason: 'legacy-numeric-pane-key', + source: 'local', + updatedAt: 2 + } + } + }) +} + +describe('bulk worktree purge evicts the per-tab/per-pty terminal maps it previously leaked', () => { + beforeEach(() => vi.clearAllMocks()) + + it('drops removed worktree entries (both tabId- and ptyId-keyed), retains survivors', () => { + const store = createTestStore() + seedMaps(store) + + store.getState().purgeWorktreeTerminalState([WT1]) + const s = store.getState() + + // Removed worktree's tab/pty: every map evicted. + expect(s.lastKnownRelayPtyIdByTabId[TAB1]).toBeUndefined() + expect(s.pendingInitialCwdByTabId[TAB1]).toBeUndefined() + expect(s.pendingIssueCommandSplitByTabId[TAB1]).toBeUndefined() + expect(s.pendingSetupSplitByTabId[TAB1]).toBeUndefined() + expect(s.pendingStartupByTabId[TAB1]).toBeUndefined() + expect(s.codexRestartNoticeByPtyId[PTY1]).toBeUndefined() + expect(s.migrationUnsupportedByPtyId[PTY1]).toBeUndefined() + + // Surviving worktree's tab/pty: every entry retained (no over-eviction). + expect(s.lastKnownRelayPtyIdByTabId[TAB2]).toBe(PTY2) + expect(s.pendingInitialCwdByTabId[TAB2]).toBe('/path/wt2') + expect(s.pendingIssueCommandSplitByTabId[TAB2]).toEqual({ command: 'b' }) + expect(s.pendingSetupSplitByTabId[TAB2]).toEqual({ command: 'setup-b', direction: 'vertical' }) + expect(s.pendingStartupByTabId[TAB2]).toEqual({ command: 'start-b' }) + expect(s.codexRestartNoticeByPtyId[PTY2]).toBeDefined() + expect(s.migrationUnsupportedByPtyId[PTY2]).toBeDefined() + }) +}) diff --git a/src/renderer/src/store/slices/worktrees.ts b/src/renderer/src/store/slices/worktrees.ts index 077deec38..d6882728e 100644 --- a/src/renderer/src/store/slices/worktrees.ts +++ b/src/renderer/src/store/slices/worktrees.ts @@ -1829,12 +1829,20 @@ function buildWorktreePurgeState(s: AppState, worktreeIds: string[]): Partial() + // Why: some terminal/agent maps are keyed by ptyId, not tabId. Collect the + // doomed panes' ptyIds now (while ptyIdsByTabId is still populated) so this + // bulk path can evict them the same way shutdownWorktreeTerminals does on the + // single-removeWorktree path. + const doomedPtyIds = new Set() const doomedBrowserWorkspaceIds = new Set() const doomedPageIds = new Set() const removedFileIds = new Set() for (const id of worktreeIdSet) { for (const tab of s.tabsByWorktree[id] ?? []) { doomedTabIds.add(tab.id) + for (const ptyId of s.ptyIdsByTabId[tab.id] ?? []) { + doomedPtyIds.add(ptyId) + } // Why: a removed worktree's panes are gone for good, so drop their // hibernation output epochs from that module-level map (mirrors the // hosted-review prune above). A future pane mints a fresh leafId at epoch 0. @@ -1925,6 +1933,17 @@ function buildWorktreePurgeState(s: AppState, worktreeIds: string[]): Partial(obj: Record): Record => { + let changed = false + const out = { ...obj } + for (const ptyId of doomedPtyIds) { + if (ptyId in out) { + delete out[ptyId] + changed = true + } + } + return changed ? out : obj + } // Pane-scoped maps are keyed by `${tabId}:${leafId}` (stable-pane-id); tabId // never contains ":", so the segment before the first ":" is the owning tab. const omitByPaneKeyTabPrefix = (obj: Record): Record => { @@ -2018,6 +2037,21 @@ function buildWorktreePurgeState(s: AppState, worktreeIds: string[]): Partial