diff --git a/src/main/ipc/workspace-cleanup.test.ts b/src/main/ipc/workspace-cleanup.test.ts index 2a1e5072183..635f6238501 100644 --- a/src/main/ipc/workspace-cleanup.test.ts +++ b/src/main/ipc/workspace-cleanup.test.ts @@ -344,6 +344,33 @@ describe('workspace cleanup scan', () => { }) }) + it('summarizes large diff-note lists without hitting argument limits', async () => { + const diffComments = Array.from( + { length: 150_000 }, + (_, index): DiffComment => ({ + id: `comment-${index}`, + worktreeId: 'repo-1::/repo-feature', + filePath: 'src/file.ts', + lineNumber: 12, + body: 'Follow up before deleting', + createdAt: NOW - index, + side: 'modified' + }) + ) + + const result = await scanWorkspaceCleanup( + makeStore({ + baseRef: undefined, + diffComments + }) + ) + + expect(result.candidates[0]?.localContext).toMatchObject({ + diffCommentCount: 150_000, + newestDiffCommentAt: NOW + }) + }) + it('does not expose PR cache state in inactivity cleanup results', async () => { const result = await scanWorkspaceCleanup( makeStore({ diff --git a/src/main/ipc/workspace-cleanup.ts b/src/main/ipc/workspace-cleanup.ts index 21c7e26819a..4e32f763d12 100644 --- a/src/main/ipc/workspace-cleanup.ts +++ b/src/main/ipc/workspace-cleanup.ts @@ -582,7 +582,16 @@ function getNewestDiffCommentAt(diffComments: Worktree['diffComments'] | undefin if (!diffComments || diffComments.length === 0) { return null } - return Math.max(...diffComments.map((comment) => comment.createdAt)) + // Why: persisted diff notes can grow large enough for spread-based Math.max + // to exceed the JavaScript argument limit during cleanup scans. + let newest = diffComments[0]?.createdAt ?? null + for (let index = 1; index < diffComments.length; index += 1) { + const createdAt = diffComments[index]?.createdAt + if (createdAt !== undefined && (newest === null || createdAt > newest)) { + newest = createdAt + } + } + return newest } function createEmptyGitEvidence(): GitEvidence {