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: tighten worktree lock-error matcher to skip permanent 'already exists' failures

[._.]/ Adam Eivyd81a21aff010
diff --git a/server/services/worktreeManager.js b/server/services/worktreeManager.js
index 050790f18e..d9bb568a5d 100644
--- a/server/services/worktreeManager.js
+++ b/server/services/worktreeManager.js
@@ -31,11 +31,17 @@ const WORKTREE_ADD_RETRY_DELAY_MS = 250;
/**
* True when a git error message indicates lock/contention on the worktree or
- * index lock (as opposed to a genuine, non-retryable failure like a bad ref).
- * Exported for unit testing against real git lock-error wording.
+ * index lock (as opposed to a genuine, non-retryable failure like a bad ref,
+ * a pre-existing branch, or an existing worktree path — retrying those just
+ * wastes the retry budget and spams misleading "lock contention" logs, #2193).
+ * Deliberately does NOT match bare "File exists" / "already exists": the
+ * canonical lock error is `Unable to create '…/index.lock': File exists`
+ * (caught by `index.lock`), whereas `a branch named 'X' already exists` and
+ * `'<path>' already exists` are permanent. Exported for unit testing against
+ * real git lock-error wording.
*/
export function isGitLockError(message) {
- return /\.lock|index\.lock|cannot lock|unable to (?:create|write|lock)|File exists|already (?:exists|locked)|another git process/i.test(message || '');
+ return /index\.lock|cannot lock|could not lock|unable to (?:create|write|lock)|already locked|another git process/i.test(message || '');
}
/**
@@ -237,7 +243,12 @@ export function shouldRefuseDefaultBranchMerge(currentBranch, defaultBranch) {
*/
export async function createWorktree(agentId, sourceWorkspace, taskId, options = {}) {
// Serialize per source repo so two adds in the same evaluation tick can't
- // race on git's per-repo worktree lock (#2193). See queueWorktreeCreate.
+ // race on git's per-repo worktree lock (#2193). The whole body runs inside
+ // the tail — not just the add — because the leading `git fetch origin` also
+ // touches the shared ref store and two concurrent fetches on the same repo
+ // can themselves hit `cannot lock ref` contention. Same-repo spawn prep is
+ // low-frequency (a handful of tasks per tick), so serializing the fetch is a
+ // cheap, safe trade for the race guarantee. See queueWorktreeCreate.
return queueWorktreeCreate(sourceWorkspace, () => createWorktreeUnlocked(agentId, sourceWorkspace, taskId, options));
}
diff --git a/server/services/worktreeManager.test.js b/server/services/worktreeManager.test.js
index 1220da6955..3ee00b2727 100644
--- a/server/services/worktreeManager.test.js
+++ b/server/services/worktreeManager.test.js
@@ -457,12 +457,15 @@ describe('isGitLockError (worktree add lock detection, #2193)', () => {
expect(isGitLockError('fatal: could not lock config file .git/config: File exists')).toBe(true);
expect(isGitLockError('error: cannot lock ref')).toBe(true);
expect(isGitLockError('Another git process seems to be running in this repository')).toBe(true);
- expect(isGitLockError("fatal: '.git/worktrees/foo' already exists")).toBe(true);
});
- it('does NOT flag genuine, non-retryable failures', () => {
+ it('does NOT flag permanent failures — including "already exists", which is NOT lock contention', () => {
+ // These fast-fail identically on every retry, so matching them would just
+ // burn the retry budget and spam misleading "lock contention" logs (#2193).
expect(isGitLockError("fatal: invalid reference: origin/nope")).toBe(false);
expect(isGitLockError('fatal: not a valid object name')).toBe(false);
+ expect(isGitLockError("fatal: '/repo/data/cos/worktrees/agent-x' already exists")).toBe(false);
+ expect(isGitLockError("fatal: a branch named 'cos/task/agent' already exists")).toBe(false);
expect(isGitLockError('')).toBe(false);
expect(isGitLockError(undefined)).toBe(false);
});