Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix: re-check a detached worktree's current HEAD before removing it (#10825)
Adam Eivy9fa2dbe0a502
diff --git a/server/services/branchReconcile.js b/server/services/branchReconcile.jsindex 3ab288afd5..a4b54dc943 100644--- a/server/services/branchReconcile.js+++ b/server/services/branchReconcile.js@@ -1370,6 +1370,17 @@ const isCommitOnBranch = (repoPath, sha, branch) =>.then((r) => r?.exitCode === 0).catch(() => false);+const isStillDetachedOnMerged = async (repoPath, treePath, defaultBranch) => {+ const onBranch = await execGit(['symbolic-ref', '-q', 'HEAD'], treePath, { ignoreExitCode: true })+ .then((r) => r?.exitCode === 0)+ .catch(() => true);+ if (onBranch) return false;+ const head = await execGit(['rev-parse', 'HEAD'], treePath, { ignoreExitCode: true })+ .then((r) => (r?.stdout || '').trim())+ .catch(() => '');+ return Boolean(head) && isCommitOnBranch(repoPath, head, defaultBranch);+};+/*** Retire the detached worktrees `gatherDetachedWorktrees` found, with the same* gate order as `retireBranchNow` (protection → still clean → remove), minus the@@ -1414,8 +1425,9 @@ export async function reapDetachedWorktrees(repoPath, defaultBranch, detached, {hold(tree, 'worktree-dirty');continue;}- // Re-verify at action time — the HEAD may have moved since the gather.- if (!await isCommitOnBranch(repoPath, tree.head, defaultBranch)) continue;+ // Re-verify at action time against what the tree holds NOW: its HEAD may have+ // been switched to a branch or an unmerged commit since the gather.+ if (!await isStillDetachedOnMerged(repoPath, tree.path, defaultBranch)) continue;const removal = await forceRemoveWorktreeDir(repoPath, tree.path, { label: '🔀 branch-reconcile: remove detached worktree', log: 'all' });if (!removal.removed) {hold(tree, 'worktree-remove-failed');diff --git a/server/services/branchReconcile.test.js b/server/services/branchReconcile.test.jsindex 6da1827971..0d1d7b2c15 100644--- a/server/services/branchReconcile.test.js+++ b/server/services/branchReconcile.test.js@@ -1795,6 +1795,8 @@ describe('reconcile — detached-HEAD worktrees (#10825)', () => {// Only the ancestry probe cares about argv; status reads clean unless a test says otherwise.const stubGit = ({ merged = true, dirty = false } = {}) => execGit.mockImplementation(async (args) => {if (args[0] === 'merge-base') return { stdout: '', exitCode: merged ? 0 : 1 };+ if (args[0] === 'symbolic-ref') return { stdout: '', exitCode: 1 };+ if (args[0] === 'rev-parse') return { stdout: 'bbb222\n', exitCode: 0 };if (args[0] === 'status') return { stdout: dirty ? ' M src/a.js\n' : '', exitCode: 0 };return { stdout: '', exitCode: 0 };});@@ -1852,6 +1854,18 @@ describe('reconcile — detached-HEAD worktrees (#10825)', () => {expect(prompt).toContain(`git worktree remove ${path}`);});+ it('does not remove a tree whose HEAD moved onto a branch after the gather', async () => {+ wt.listWorktrees.mockResolvedValue([MAIN, detachedTree('/repo/data/cos/worktrees/agent-1234abcd')]);+ stubGit();+ const detachedStub = execGit.getMockImplementation();+ execGit.mockImplementation(async (args, ...rest) => args[0] === 'symbolic-ref'+ ? { stdout: 'refs/heads/feature/x\n', exitCode: 0 }+ : detachedStub(args, ...rest));+ const res = await reconcile('/repo');+ expect(wt.forceRemoveWorktreeDir).not.toHaveBeenCalled();+ expect(res.cleaned).toEqual([]);+ });+it('never gathers the main checkout or a detached tree at unmerged commits', async () => {wt.listWorktrees.mockResolvedValue([{ path: '/repo', head: 'aaa111', detached: true },