fix: handle large manual worktree reorders (#3691)

This commit is contained in:
Neil
2026-05-30 06:20:02 -07:00
committed by GitHub
parent dc58dc7514
commit dd70ec2551
2 changed files with 52 additions and 6 deletions
@@ -55,6 +55,16 @@ describe('moveWorktreeIdsWithinGroup', () => {
'c'
])
})
it('moves a very large selected batch without overflowing argument limits', () => {
const ids = Array.from({ length: 130_000 }, (_, index) => `wt-${index}`)
const result = moveWorktreeIdsWithinGroup(ids, ids, 0)
expect(result).toHaveLength(ids.length)
expect(result[0]).toBe('wt-0')
expect(result.at(-1)).toBe('wt-129999')
})
})
describe('buildWorktreeDragPreviewOffsets', () => {
@@ -185,6 +195,28 @@ describe('buildManualOrderUpdatesForVisibleGroups', () => {
['child', { manualOrder: -1000 }]
])
})
it('reorders a very large visible group without overflowing argument limits', () => {
const ids = Array.from({ length: 130_000 }, (_, index) => `wt-${index}`)
const rankByWorktreeId = new Map(
ids.map((id, index) => [id, (ids.length - index) * 1000] as const)
)
const result = buildManualOrderUpdatesForVisibleGroups({
groups: [{ key: 'all', worktreeIds: ids }],
sourceGroupKey: 'all',
draggedIds: ['wt-0'],
dropIndex: ids.length,
now: 10_000,
rankByWorktreeId
})
expect(result.changed).toBe(true)
expect(result.orderedIds).toHaveLength(ids.length)
expect(result.orderedIds[0]).toBe('wt-1')
expect(result.orderedIds.at(-1)).toBe('wt-0')
expect(Array.from(result.updates)).toEqual([['wt-0', { manualOrder: 0 }]])
})
})
describe('buildManualOrderUpdatesForGroupDrop', () => {
@@ -26,6 +26,20 @@ function arraysEqual(a: readonly string[], b: readonly string[]): boolean {
return a.length === b.length && a.every((value, index) => value === b[index])
}
function appendWorktreeIds(target: string[], ids: readonly string[]): void {
// Why: generated workspace fleets can exceed V8's argument limit for
// `push(...ids)` while manual ordering still needs the full visible order.
for (const id of ids) {
target.push(id)
}
}
function insertWorktreeIds(target: string[], index: number, ids: readonly string[]): void {
const tail = target.splice(index)
appendWorktreeIds(target, ids)
appendWorktreeIds(target, tail)
}
export function expandDraggedWorktreeIdsForVisibleLineage(
rows: readonly WorktreeDragLineageRow[],
draggedIds: readonly string[]
@@ -101,7 +115,7 @@ export function moveWorktreeIdsWithinGroup(
const remaining = groupIds.filter((id) => !draggedSet.has(id))
const insertAt = Math.max(0, Math.min(remaining.length, boundedDropIndex - removedBeforeDrop))
const next = remaining.slice()
next.splice(insertAt, 0, ...orderedDraggedIds)
insertWorktreeIds(next, insertAt, orderedDraggedIds)
return next
}
@@ -128,7 +142,7 @@ export function buildManualOrderUpdatesForVisibleGroups(args: {
if (group.key === args.sourceGroupKey && !arraysEqual(ids, group.worktreeIds)) {
changed = true
}
orderedIds.push(...ids)
appendWorktreeIds(orderedIds, ids)
}
if (!changed) {
@@ -194,10 +208,10 @@ export function buildManualOrderUpdatesForGroupDrop(args: {
}
}
ids = group.worktreeIds.filter((id) => !draggedSet.has(id))
ids.splice(
insertWorktreeIds(
ids,
Math.max(0, Math.min(ids.length, boundedDropIndex - removedBeforeDrop)),
0,
...orderedDraggedIds
orderedDraggedIds
)
} else {
ids = group.worktreeIds.filter((id) => !draggedSet.has(id))
@@ -206,7 +220,7 @@ export function buildManualOrderUpdatesForGroupDrop(args: {
if (!arraysEqual(ids, group.worktreeIds)) {
changed = true
}
orderedIds.push(...ids)
appendWorktreeIds(orderedIds, ids)
}
if (!changed) {