diff --git a/src/renderer/src/components/sidebar/WorkspaceKanbanDrawer.tsx b/src/renderer/src/components/sidebar/WorkspaceKanbanDrawer.tsx index eb9614f28d87..adfe1f3fb48b 100644 --- a/src/renderer/src/components/sidebar/WorkspaceKanbanDrawer.tsx +++ b/src/renderer/src/components/sidebar/WorkspaceKanbanDrawer.tsx @@ -418,8 +418,10 @@ function WorkspaceKanbanDrawerContent({ for (const group of boardDragGroups) { for (const worktreeId of group.worktreeIds) { const worktree = worktreeById.get(worktreeId) - if (worktree) { - ranks.set(worktreeId, worktree.manualOrder ?? worktree.sortOrder) + // Why: only real manualOrder ranks here — holes get backfilled by the + // ranker, not masked with the mutable sortOrder. + if (worktree?.manualOrder !== undefined) { + ranks.set(worktreeId, worktree.manualOrder) } } } diff --git a/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.test.ts b/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.test.ts index bdd9928ff178..9481d9e57a7a 100644 --- a/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.test.ts +++ b/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.test.ts @@ -597,9 +597,14 @@ describe('workspace kanban sidebar drop updates', () => { }) expect(result.shouldSwitchToManual).toBe(true) + // Why: with no prior manualOrder anywhere, the drop backfills every visible row + // so the manual order is stable (doing-a > todo-a > doing-b) instead of sparse- + // ranking only the dropped row onto the mutable sortOrder. expect(result.updates.get('todo-a')).toEqual({ workspaceStatus: 'doing', - manualOrder: 1500 + manualOrder: 9000 }) + expect(result.updates.get('doing-a')).toEqual({ manualOrder: 10_000 }) + expect(result.updates.get('doing-b')).toEqual({ manualOrder: 8000 }) }) }) diff --git a/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.ts b/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.ts index 51dd561f94e1..a962d6020fce 100644 --- a/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.ts +++ b/src/renderer/src/components/sidebar/workspace-kanban-sidebar-drop.ts @@ -237,8 +237,10 @@ export function buildWorkspaceKanbanSidebarDropUpdates(args: { for (const group of args.groups) { for (const worktreeId of group.worktreeIds) { const worktree = args.worktreeById.get(worktreeId) - if (worktree) { - ranks.set(worktreeId, worktree.manualOrder ?? worktree.sortOrder) + // Why: only real manualOrder ranks here — holes get backfilled by the ranker, + // not masked with the mutable sortOrder. + if (worktree?.manualOrder !== undefined) { + ranks.set(worktreeId, worktree.manualOrder) } } } diff --git a/src/renderer/src/components/sidebar/worktree-list/drag/use-status-mutations.ts b/src/renderer/src/components/sidebar/worktree-list/drag/use-status-mutations.ts index db3716a0d8c5..8d47a20aea67 100644 --- a/src/renderer/src/components/sidebar/worktree-list/drag/use-status-mutations.ts +++ b/src/renderer/src/components/sidebar/worktree-list/drag/use-status-mutations.ts @@ -25,8 +25,10 @@ function buildRankByWorktreeId( for (const group of groups) { for (const worktreeId of group.worktreeIds) { const worktree = worktreeMap.get(worktreeId) - if (worktree) { - rankByWorktreeId.set(worktreeId, worktree.manualOrder ?? worktree.sortOrder) + // Why: only real manualOrder ranks here — a row still lacking one is a hole + // the ranker backfills, rather than masking it with the mutable sortOrder. + if (worktree?.manualOrder !== undefined) { + rankByWorktreeId.set(worktreeId, worktree.manualOrder) } } } diff --git a/src/renderer/src/components/sidebar/worktree-manual-order-ranks.ts b/src/renderer/src/components/sidebar/worktree-manual-order-ranks.ts index cba76f383324..d460dfe9c955 100644 --- a/src/renderer/src/components/sidebar/worktree-manual-order-ranks.ts +++ b/src/renderer/src/components/sidebar/worktree-manual-order-ranks.ts @@ -41,6 +41,18 @@ export function buildSparseManualOrderUpdates(args: { return buildFallbackManualOrderUpdates(args.orderedIds, args.now) } + // Why: any non-moved row without a persisted manualOrder would otherwise keep + // falling back to the mutable sortOrder in the comparator and jump on later + // activity, so backfill the whole visible group instead of sparse-ranking only + // the moved rows. + const ranks = args.rankByWorktreeId + if ( + ranks !== undefined && + args.orderedIds.some((id) => !movedSet.has(id) && getManualOrderRank(ranks, id) === null) + ) { + return buildFallbackManualOrderUpdates(args.orderedIds, args.now) + } + const firstMovedIndex = args.orderedIds.findIndex((id) => movedSet.has(id)) const lastMovedIndex = args.orderedIds.findLastIndex((id) => movedSet.has(id)) const beforeId = args.orderedIds.slice(0, firstMovedIndex).findLast((id) => !movedSet.has(id)) diff --git a/src/renderer/src/components/sidebar/worktree-manual-order.test.ts b/src/renderer/src/components/sidebar/worktree-manual-order.test.ts index 5d7839956c59..8046d12071ef 100644 --- a/src/renderer/src/components/sidebar/worktree-manual-order.test.ts +++ b/src/renderer/src/components/sidebar/worktree-manual-order.test.ts @@ -283,6 +283,27 @@ describe('buildManualOrderUpdatesForVisibleGroups', () => { expect(Array.from(result.updates)).toEqual([['b', { manualOrder: 0 }]]) }) + it('backfills manualOrder for every visible row when an unmoved row lacks one', () => { + // Why: an unmoved row without a persisted manualOrder would otherwise keep + // falling back to the mutable sortOrder in the comparator and jump on later + // activity, so a reorder must stamp every visible row — not just the moved one. + const result = buildManualOrderUpdatesForVisibleGroups({ + groups: [{ key: 'repo:one', worktreeIds: ['a', 'b', 'c', 'd'] }], + sourceGroupKey: 'repo:one', + draggedIds: ['b'], + dropIndex: 4, + now: 10_000, + rankByWorktreeId: new Map([ + ['a', 4000], + ['b', 3000], + ['d', 1000] + ]) + }) + + expect(result.orderedIds).toEqual(['a', 'c', 'd', 'b']) + expect(Array.from(result.updates.keys()).sort()).toEqual(['a', 'b', 'c', 'd']) + }) + it('moves an expanded lineage cluster as one ranked unit', () => { const result = buildManualOrderUpdatesForVisibleGroups({ groups: [{ key: 'all', worktreeIds: ['parent', 'child', 'other'] }],