Skip to content

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

fix: narrow orphan-cleanup skip to branch-exists, not path-exists

[._.]/ Adam Eivyabcfe6e8e493
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index b7c41f5d0d..57439a4da8 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -85,13 +85,20 @@ function queueWorktreeCreate(repo, fn) {
}
/**
- * A git error saying the branch or worktree path already exists — a permanent
- * precondition failure meaning the ref/dir PRE-DATED this add (git created
- * nothing), NOT an orphan this add left behind. cleanupOrphanBranch keys off
- * this to avoid `git branch -D`-ing a branch it didn't create (#2193).
+ * A git error saying the target BRANCH already exists (`fatal: a branch named
+ * 'X' already exists`) — a permanent precondition failure meaning the ref
+ * PRE-DATED this add and git created nothing, so it is NOT an orphan this add
+ * left behind. cleanupOrphanBranch keys off this to avoid `git branch -D`-ing
+ * a branch it didn't create (#2193).
+ *
+ * Deliberately NARROW: it must NOT match `'<path>' already exists` (the
+ * worktree DIRECTORY was occupied), because in that case `git worktree add -b`
+ * has ALREADY created the branch before failing on the path — so that branch
+ * IS an orphan and must be cleaned up. Only the branch-named wording is safe
+ * to skip on.
*/
export function isPreexistingRefError(message) {
- return /already exists/i.test(message || '');
+ return /branch (?:named )?['`]?[^'`\n]*['`]? ?already exists/i.test(message || '');
}
/**
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 035dc5b499..7d3ffddac3 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -523,9 +523,13 @@ describe('addWorktreeWithRetry (lock-contention retry, #2193)', () => {
});
describe('isPreexistingRefError (orphan-cleanup guard, #2193)', () => {
- it('is true for branch/path already-exists failures (add created nothing)', () => {
+ it('is true ONLY for a pre-existing BRANCH (git created nothing → skip cleanup)', () => {
expect(isPreexistingRefError("fatal: a branch named 'cos/task/agent' already exists")).toBe(true);
- expect(isPreexistingRefError("fatal: '/repo/data/cos/worktrees/agent-x' already exists")).toBe(true);
+ expect(isPreexistingRefError("fatal: a branch named 'main' already exists.")).toBe(true);
+ });
+
+ it('is FALSE for an occupied worktree PATH — git already created the branch there, so it IS an orphan to clean up', () => {
+ expect(isPreexistingRefError("fatal: '/repo/data/cos/worktrees/agent-x' already exists")).toBe(false);
});
it('is false for lock contention and other failures (add may have left an orphan)', () => {