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: base orphan-cleanup skip on the FIRST attempt error, not the final one
[._.]/ Adam Eivy9a075c0eaaaf
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.jsindex 57439a4da8..709d25ca3c 100644--- a/server/services/worktreeManager.js+++ b/server/services/worktreeManager.js@@ -50,13 +50,24 @@ export function isGitLockError(message) {* error it backs off and recurses until WORKTREE_ADD_MAX_ATTEMPTS, then lets* the final error reject. Non-lock errors (bad base ref, etc.) reject* immediately — retrying them just wastes time.+ *+ * The FIRST attempt's error is preserved on the final rejection as+ * `err.firstAttemptError` so orphan-branch cleanup can reason about the+ * ORIGINAL failure cause: a `-b` add that created the branch and then hit a+ * lock error would fail attempt 2 with a retry-induced `branch already+ * exists`, and cleanup must NOT mistake that for "the branch pre-dated us"+ * (#2193) — the branch is exactly the orphan it needs to delete.*/-export function addWorktreeWithRetry(args, repo, attempt = 1) {+export function addWorktreeWithRetry(args, repo, attempt = 1, firstError = null) {return execGit(args, repo).catch((err) => {- if (attempt >= WORKTREE_ADD_MAX_ATTEMPTS || !isGitLockError(err.message)) throw err;+ const originalError = firstError || err;+ if (attempt >= WORKTREE_ADD_MAX_ATTEMPTS || !isGitLockError(err.message)) {+ err.firstAttemptError = originalError;+ throw err;+ }console.log(`🌳 Worktree add lock contention (attempt ${attempt}/${WORKTREE_ADD_MAX_ATTEMPTS}), retrying in ${WORKTREE_ADD_RETRY_DELAY_MS}ms: ${err.message}`);return new Promise(resolve => setTimeout(resolve, WORKTREE_ADD_RETRY_DELAY_MS))- .then(() => addWorktreeWithRetry(args, repo, attempt + 1));+ .then(() => addWorktreeWithRetry(args, repo, attempt + 1, originalError));});}@@ -109,9 +120,13 @@ export function isPreexistingRefError(message) {* didn't create.** Takes the causing error so it can:- * - SKIP entirely when the add failed because the branch/path already existed+ * - SKIP entirely when the add failed because the branch already existed* (`isPreexistingRefError`) — that branch pre-dated us and may hold real- * commits; deleting it would be data loss.+ * commits; deleting it would be data loss. This keys off the FIRST+ * attempt's error (`err.firstAttemptError`), not the final one: a retried+ * `-b` add that created the branch on attempt 1 then failed with a+ * retry-induced `branch already exists` on attempt 2 must still clean up+ * the orphan branch it created (#2193).* - PRUNE before deleting: a partial add can register a stale worktree AND* create the branch, so a bare `git branch -D` fails with "used by* worktree". `worktree prune` first clears that registration so the delete@@ -119,7 +134,10 @@ export function isPreexistingRefError(message) {*/async function cleanupOrphanBranch(repo, branchName, err) {if (!branchName) return;- if (isPreexistingRefError(err?.message)) return;+ // The original failure cause decides whether the branch pre-dated us — a+ // later attempt's `already exists` can be self-inflicted by attempt 1.+ const causeError = err?.firstAttemptError || err;+ if (isPreexistingRefError(causeError?.message)) return;await execGit(['worktree', 'prune'], repo).catch(() => {});await execGit(['branch', '-D', branchName], repo).catch(pruneErr => {console.log(`⚠️ Orphan branch cleanup failed for ${branchName}: ${pruneErr.message}`);diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.jsindex 7d3ffddac3..08b2c43bc7 100644--- a/server/services/worktreeManager.test.js+++ b/server/services/worktreeManager.test.js@@ -520,6 +520,28 @@ describe('addWorktreeWithRetry (lock-contention retry, #2193)', () => {.rejects.toThrow(/already exists/);expect(execGitMock).toHaveBeenCalledTimes(1);});++ it('preserves the FIRST attempt error so a retry-induced "already exists" cannot mask the real cause', async () => {+ // Attempt 1 creates the branch then fails on a lock error; attempt 2 then+ // fails with a self-inflicted "branch already exists". The final rejection+ // must carry the ORIGINAL lock error so orphan cleanup still runs (#2193).+ execGitMock+ .mockRejectedValueOnce(new Error('error: cannot lock ref (attempt 1 created the branch)'))+ .mockRejectedValueOnce(new Error("fatal: a branch named 'cos/task/agent' already exists"));+ const settled = addWorktreeWithRetry(['worktree', 'add', '-b', 'cos/task/agent', '/wt', 'main'], '/repo').catch(e => e);+ await vi.runAllTimersAsync();+ const err = await settled;+ expect(err.message).toMatch(/already exists/);+ expect(err.firstAttemptError.message).toMatch(/cannot lock ref/);+ expect(execGitMock).toHaveBeenCalledTimes(2);+ });++ it('sets firstAttemptError to the sole error when the first attempt is non-retryable', async () => {+ const only = new Error("fatal: a branch named 'cos/task/agent' already exists");+ execGitMock.mockRejectedValueOnce(only);+ const err = await addWorktreeWithRetry(['worktree', 'add', '-b', 'cos/task/agent', '/wt', 'main'], '/repo').catch(e => e);+ expect(err.firstAttemptError).toBe(only);+ });});describe('isPreexistingRefError (orphan-cleanup guard, #2193)', () => {