From 2881a6bf02abbb0741b79522d81a8958b8dfb1d2 Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Fri, 19 Jun 2026 01:49:57 -0700 Subject: [PATCH] fix(store): evict PR & commit-message generation records on worktree removal (#5784) Co-authored-by: Orca --- ...tion-records-worktree-removal-leak.test.ts | 145 ++++++++++++++++++ src/renderer/src/store/slices/worktrees.ts | 13 ++ 2 files changed, 158 insertions(+) create mode 100644 src/renderer/src/store/slices/generation-records-worktree-removal-leak.test.ts diff --git a/src/renderer/src/store/slices/generation-records-worktree-removal-leak.test.ts b/src/renderer/src/store/slices/generation-records-worktree-removal-leak.test.ts new file mode 100644 index 000000000..b47381063 --- /dev/null +++ b/src/renderer/src/store/slices/generation-records-worktree-removal-leak.test.ts @@ -0,0 +1,145 @@ +/** + * Memory-leak regression: PR-generation and commit-message-generation records + * must be evicted when their worktree is removed. + * + * Both records are keyed by worktree (the PR map by a composite + * [repoId, worktreeKey, branch] key, the commit map by worktreeKey) and each + * retains generated title/body/message text. The slices ship tested + * `prunePullRequestGenerationRecords` / `pruneCommitMessageGenerationRecords` + * actions, but nothing in production called them on worktree removal, so the + * records accumulated one entry per worktree for the renderer session. + * `removeWorktree` now prunes both maps to the surviving worktree set. + */ +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), + forceDeletePreservedBranch: vi.fn().mockResolvedValue({ deleted: true }), + updateMeta: vi.fn().mockResolvedValue({}) + }, + pty: { kill: vi.fn().mockResolvedValue(undefined) }, + runtimeEnvironments: { call: vi.fn() } +} + +// @ts-expect-error -- minimal window.api stub for the store under test +globalThis.window = { api: mockApi } + +import { createTestStore, seedStore, makeWorktree } from './store-test-helpers' +import { + getPullRequestGenerationRecordKey, + type PullRequestGenerationRecord +} from './pull-request-generation' +import { + getCommitMessageGenerationRecordKey, + type CommitMessageGenerationRecord +} from './commit-message-generation' + +const REPO = 'repo1' +const WT = 'repo1::/path/wt1' +const WT_PATH = '/path/wt1' +const OTHER = 'repo1::/path/wt2' +const OTHER_PATH = '/path/wt2' +const BRANCH = 'refs/heads/feature' + +function prRecord(worktreeId: string, worktreePath: string): PullRequestGenerationRecord { + return { + context: { worktreeId, worktreePath, requestId: 1, repoId: REPO, branch: BRANCH }, + seed: { base: 'main', title: 'Title', body: 'Body', draft: false }, + seedFieldRevisions: { base: 0, title: 0, body: 0, draft: 0 }, + status: 'succeeded', + result: null, + error: null, + hydrated: true + } +} + +function commitRecord(worktreeId: string, worktreePath: string): CommitMessageGenerationRecord { + return { + context: { worktreeId, worktreePath, requestId: 1 }, + status: 'succeeded', + message: 'a generated commit message', + error: null, + hydrated: true + } +} + +function prKey(worktreeId: string, worktreePath: string): string { + const key = getPullRequestGenerationRecordKey({ + worktreeId, + worktreePath, + repoId: REPO, + branch: BRANCH + }) + if (!key) { + throw new Error('expected a PR generation key') + } + return key +} + +function commitKey(worktreeId: string, worktreePath: string): string { + const key = getCommitMessageGenerationRecordKey(worktreeId, worktreePath) + if (!key) { + throw new Error('expected a commit generation key') + } + return key +} + +describe('worktree removal evicts generation records (leak regression)', () => { + beforeEach(() => { + vi.clearAllMocks() + mockApi.worktrees.remove.mockResolvedValue(undefined) + }) + + it('removes PR + commit generation records for the removed worktree and keeps others', async () => { + const store = createTestStore() + seedStore(store, { + worktreesByRepo: { + repo1: [ + makeWorktree({ id: WT, repoId: REPO, path: WT_PATH }), + makeWorktree({ id: OTHER, repoId: REPO, path: OTHER_PATH }) + ] + } + }) + store.getState().setPullRequestGenerationRecord(prKey(WT, WT_PATH), prRecord(WT, WT_PATH)) + store + .getState() + .setPullRequestGenerationRecord(prKey(OTHER, OTHER_PATH), prRecord(OTHER, OTHER_PATH)) + store + .getState() + .setCommitMessageGenerationRecord(commitKey(WT, WT_PATH), commitRecord(WT, WT_PATH)) + store + .getState() + .setCommitMessageGenerationRecord( + commitKey(OTHER, OTHER_PATH), + commitRecord(OTHER, OTHER_PATH) + ) + + const result = await store.getState().removeWorktree(WT) + expect(result).toEqual({ ok: true }) + + const s = store.getState() + // Removed worktree's records are gone (the leak). + expect(s.pullRequestGenerationRecords[prKey(WT, WT_PATH)]).toBeUndefined() + expect(s.commitMessageGenerationRecords[commitKey(WT, WT_PATH)]).toBeUndefined() + // Surviving worktree's records are preserved (no over-pruning). + expect(s.pullRequestGenerationRecords[prKey(OTHER, OTHER_PATH)]).toBeDefined() + expect(s.commitMessageGenerationRecords[commitKey(OTHER, OTHER_PATH)]).toBeDefined() + }) +}) diff --git a/src/renderer/src/store/slices/worktrees.ts b/src/renderer/src/store/slices/worktrees.ts index e81607912..e1dbca326 100644 --- a/src/renderer/src/store/slices/worktrees.ts +++ b/src/renderer/src/store/slices/worktrees.ts @@ -2128,6 +2128,19 @@ export const createWorktreeSlice: StateCreator } }) get().removeWorkspaceSpaceWorktrees?.([worktreeId]) + // Why: PR/commit-message generation records are keyed by worktree and were + // never evicted on removal — they leaked one record (title/body text) per + // worktree for the session. Prune to the surviving worktree set, reusing + // the generation slices' tested prune actions. + const liveWorktreeKeys = new Set( + get() + .allWorktrees() + .map((w) => w.id) + ) + // Optional-chained like removeWorkspaceSpaceWorktrees above: minimal store + // assemblies (some unit tests) omit the generation slices. + get().prunePullRequestGenerationRecords?.(liveWorktreeKeys) + get().pruneCommitMessageGenerationRecords?.(liveWorktreeKeys) const preservedBranch = removalResult?.preservedBranch if (preservedBranch) { showPreservedBranchToast(removalResult, worktreeBeforeRemoval, (branch, expectedHead) => {