diff --git a/src/main/ipc/worktrees.test.ts b/src/main/ipc/worktrees.test.ts index d61b92852e0..f7af28221bb 100644 --- a/src/main/ipc/worktrees.test.ts +++ b/src/main/ipc/worktrees.test.ts @@ -540,6 +540,22 @@ describe('registerWorktreeHandlers', () => { expect(handlers['worktrees:getBranchRenameFailureOutput']).toBeDefined() }) + it('persistSortOrder only reorders existing worktrees and never mints meta for a stale id', () => { + const liveId = 'repo-1::/workspace/repo' + const staleId = 'removed-repo::/workspace/gone' + // Only the live worktree has meta; the stale id (e.g. a removed repo the + // renderer still lists) has none and must be skipped, not created. + store.getWorktreeMeta.mockImplementation((id: string) => + id === liveId ? ({ instanceId: 'x' } as never) : undefined + ) + + handlers['worktrees:persistSortOrder'](null, { orderedIds: [liveId, staleId] }) + + const orderedTargets = store.setWorktreeMeta.mock.calls.map((call) => call[0]) + expect(orderedTargets).toContain(liveId) + expect(orderedTargets).not.toContain(staleId) + }) + it('prefetches the local default create base through the runtime refresh cache', async () => { const repo = { id: 'repo-1', diff --git a/src/main/ipc/worktrees.ts b/src/main/ipc/worktrees.ts index 40cf7efd17d..5acd40c4f4d 100644 --- a/src/main/ipc/worktrees.ts +++ b/src/main/ipc/worktrees.ts @@ -2067,6 +2067,14 @@ export function registerWorktreeHandlers( } const now = Date.now() for (let i = 0; i < args.orderedIds.length; i++) { + // Why: a sidebar-order snapshot must only reorder worktrees that already + // exist — it must never create one. Without this guard a stale id the + // renderer still lists (e.g. a removed repo's `${repoId}::${path}`) gets a + // fresh worktreeMeta entry minted here, resurrecting an orphan/duplicate + // workspace on the next launch. setWorktreeMeta has no repo-existence check. + if (!store.getWorktreeMeta(args.orderedIds[i])) { + continue + } // Descending timestamps: first item gets highest sortOrder so b - a sorts first-wins on cold start. store.setWorktreeMeta(args.orderedIds[i], { sortOrder: now - i * 1000 }) } diff --git a/src/main/runtime/orca-runtime.ts b/src/main/runtime/orca-runtime.ts index 08303980060..66765544e79 100644 --- a/src/main/runtime/orca-runtime.ts +++ b/src/main/runtime/orca-runtime.ts @@ -19973,6 +19973,12 @@ export class OrcaRuntimeService { const now = Date.now() let updated = 0 for (let i = 0; i < orderedIds.length; i++) { + // Why: a sort-order snapshot must only reorder existing worktrees, never + // mint new meta — a stale id would otherwise resurrect an orphan workspace + // (setWorktreeMeta has no repo-existence check). + if (!this.store.getWorktreeMeta(orderedIds[i])) { + continue + } this.store.setWorktreeMeta(orderedIds[i], { sortOrder: now - i * 1000 }) updated++ }