perf: scan large cleanup diff notes safely (#3741)

This commit is contained in:
Neil
2026-05-30 08:26:23 -07:00
committed by GitHub
parent f68bb8120d
commit 3d0c7b7fc4
2 changed files with 37 additions and 1 deletions
+27
View File
@@ -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({
+10 -1
View File
@@ -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 {