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([issue-12764]): fetch the merge target before taking the claim-ownership lock during worktree cleanup
[._.]/ Adam Eivy6c88310a0749
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.jsindex 8b006d72e8..025dca747e 100644--- a/server/services/worktreeManager.js+++ b/server/services/worktreeManager.js@@ -1307,7 +1307,7 @@ async function resolveMergeTarget(sourceWorkspace, defaultBranch) {// arrive after the first removed the directory but preserved unpublished work;// every successful removal path must apply the same branch-retention policy.async function cleanupWorktreeBranch(sourceWorkspace, branchName, {- hasUnmergedCommits = false, warnings, expectedHead,+ hasUnmergedCommits = false, warnings, expectedHead, target: resolvedTarget,}) {const present = await execGit(['show-ref', '--verify', '--quiet', `refs/heads/${branchName}`], sourceWorkspace, { ignoreExitCode: true }).catch(() => null);if (present?.exitCode === 1) return;@@ -1333,7 +1333,10 @@ async function cleanupWorktreeBranch(sourceWorkspace, branchName, {if (localRef?.exitCode === 1) return;const { getDefaultBranch, hasBranchMergeEvidence } = await import('./git.js');const defaultBranch = await getDefaultBranch(sourceWorkspace).catch(() => null) || 'main';- const target = await resolveMergeTarget(sourceWorkspace, defaultBranch);+ // A caller holding the claim-ownership lock passes a target it resolved earlier:+ // the fetch must not run under that lock (#12764), and merge evidence against a+ // ref fetched moments ago stays valid.+ const target = resolvedTarget || await resolveMergeTarget(sourceWorkspace, defaultBranch);// Patch-equivalence recognizes squash/rebase merges; a failed probe keeps// the branch. Ask the refreshed remote target so a landed PR is not resumed// just because this install's local default branch has not advanced.@@ -1414,6 +1417,9 @@ async function removeWorktreeNow(agentId, sourceWorkspace, branchName, options,warnings.push(`Worktree preserved — ${reason}`);return { merged, removed: false, cleanupIncomplete: true, uncommittedSaved: false, warnings };};+ // Fetched with the lock released (archive phase, or before the first section) and+ // reused by every check under the lock: a branch merged into origin/<default> stays merged.+ let mergeTarget;const completionHold = async (expectedHead) => {const owner = await ownershipHold();if (owner) return owner;@@ -1424,10 +1430,10 @@ async function removeWorktreeNow(agentId, sourceWorkspace, branchName, options,const base = await getDefaultBranch(sourceWorkspace, { strict: true }).catch(() => null);if (!base) return 'default-branch-unavailable';if (PROTECTED_BRANCHES.includes(branchName) || branchName === base) return 'protected-branch';- const target = await resolveMergeTarget(sourceWorkspace, base);+ mergeTarget ||= await resolveMergeTarget(sourceWorkspace, base);const head = (await execGit(['rev-parse', '--verify', `refs/heads/${branchName}`], sourceWorkspace).catch(() => null))?.stdout?.trim();if (!head || (expectedHead && head !== expectedHead)) return 'branch-head-changed-or-unavailable';- return await hasBranchMergeEvidence(sourceWorkspace, head, target).catch(() => false) ? null : 'merge-evidence-unavailable';+ return await hasBranchMergeEvidence(sourceWorkspace, head, mergeTarget).catch(() => false) ? null : 'merge-evidence-unavailable';};const preserve = async () => {const tree = (await listWorktrees(sourceWorkspace)).find(wt => canonicalPath(wt.path) === canonicalPath(worktreePath));@@ -1463,11 +1469,18 @@ async function removeWorktreeNow(agentId, sourceWorkspace, branchName, options,return false;}await cleanupWorktreeBranch(sourceWorkspace, branchName, {- hasUnmergedCommits: options.merge && !merged && (commitsAhead > 0 || mergeRefused), warnings, expectedHead,+ hasUnmergedCommits: options.merge && !merged && (commitsAhead > 0 || mergeRefused), warnings, expectedHead, target: mergeTarget,});return true;};+ if (!existsSync(worktreePath)) {+ // The already-gone path deletes the branch under the lock; fetch before taking it.+ const { getDefaultBranch } = await import('./git.js');+ const base = await getDefaultBranch(sourceWorkspace, { strict: true }).catch(() => null);+ if (base) mergeTarget = await resolveMergeTarget(sourceWorkspace, base);+ }+const inspected = await withClaimOwnershipLock(async () => {const { getDefaultBranch } = await import('./git.js');const defaultBranch = await getDefaultBranch(sourceWorkspace, { strict: true }).catch(() => null);diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.jsindex 91afc12457..1f9021f230 100644--- a/server/services/worktreeManager.test.js+++ b/server/services/worktreeManager.test.js@@ -1209,6 +1209,29 @@ describe('removeWorktree identity, dirt and branch preservation', () => {expect(checkoutRetirementFenceReason(fenced)).toBe(null);});+ // #12764: resolveMergeTarget's `git fetch origin --prune` still ran inside the+ // second critical section (completionHold via verifyRemoval, and the branch+ // cleanup), so a hung fetch could starve every claim bind queued behind the lock.+ it.each(['present', 'missing'])('fetches the merge target with the ownership queue free for a %s checkout', async state => {+ if (state === 'missing') existsSync.mockReturnValue(false);+ const { withClaimOwnershipLock } = await import('./cosClaimOwnershipState.js');+ const scripted = execGitMock.getMockImplementation();+ const fetches = [];+ execGitMock.mockImplementation(async (args, ...rest) => {+ if (args[0] === 'fetch') {+ const probe = withClaimOwnershipLock(() => 'free');+ fetches.push(await Promise.race([probe, new Promise(resolve => setTimeout(() => resolve('held'), 25))]));+ }+ return scripted(args, ...rest);+ });+ const result = await removeWorktree('agent-x', '/repo', 'cos/task-1/agent-x');+ expect(result.removed).toBe(true);+ expect(fetches.length).toBeGreaterThan(0);+ expect(fetches.every(outcome => outcome === 'free')).toBe(true);+ // One refresh serves the pre-removal check, the post-removal verify and the branch cleanup.+ expect(fetches).toHaveLength(1);+ });+it('keeps a lockfile-only checkout that changed while its pre-cleanup snapshot was archived', async () => {scriptGit({ porcelain: ' M package-lock.json' });const scripted = execGitMock.getMockImplementation();