{"record":{"id":"9391af97b45e7560","repo":"Yeachan-Heo/oh-my-codex","slug":"worktree-add-failed","errorCode":"worktree_add_failed","errorMessage":"worktree_add_failed:${addArgs.join(' ')}","messagePattern":"worktree_add_failed:(.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/team/worktree.ts","lineNumber":471,"sourceCode":"    addArgs.push('--detach', plan.worktreePath, plan.baseRef);\n  } else if (branchAlreadyExisted) {\n    addArgs.push(plan.worktreePath, plan.branchName as string);\n  } else {\n    addArgs.push('-b', plan.branchName as string, plan.worktreePath, plan.baseRef);\n  }\n\n  const result = spawnSync('git', addArgs, {\n    cwd: plan.repoRoot,\n    encoding: 'utf-8',\n      windowsHide: true,\n    });\n\n  if (result.status !== 0) {\n    const stderr = (result.stderr || '').trim();\n    if (plan.branchName && BRANCH_IN_USE_PATTERN.test(stderr)) {\n      throw new Error(`branch_in_use:${plan.branchName}`);\n    }\n    throw new Error(stderr || `worktree_add_failed:${addArgs.join(' ')}`);\n  }\n\n  const ensured = {\n    enabled: true,\n    repoRoot: plan.repoRoot,\n    worktreePath: resolve(plan.worktreePath),\n    detached: plan.detached,\n    branchName: plan.branchName,\n    created: true,\n    reused: false,\n    createdBranch: Boolean(plan.branchName && !branchAlreadyExisted),\n  } satisfies EnsureWorktreeResult;\n\n  if (plan.branchName) {\n    upsertCurrentTaskBaseline(plan.repoRoot, {\n      branch_name: plan.branchName,\n      worktree_path: ensured.worktreePath,\n      base_ref: plan.baseRef,","sourceCodeStart":453,"sourceCodeEnd":489,"githubUrl":"https://github.com/Yeachan-Heo/oh-my-codex/blob/3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2/src/team/worktree.ts#L453-L489","documentation":"The spawned `git worktree add` exited non-zero and its stderr was empty (or the branch-in-use pattern did not match), so the library throws worktree_add_failed with the full argument list. This is the catch-all for git-side failures: bad refs, invalid paths, permission problems, corrupt git metadata, missing options on old git versions.","triggerScenarios":"ensureWorktree spawns `git worktree add <addArgs>` and git exits non-zero with no stderr captured — e.g. the base ref/commit does not exist, the target path is inside .git, the filesystem is read-only, or the installed git is too old to support a passed flag (like --orphan).","commonSituations":"Passing a start point like `origin/nonexistent-branch`; running as a user without write permission to the repo; git < 2.42 lacking --orphan support for worktrees; disk-full or read-only CI filesystems; detached-HEAD base with a bad HEAD sha.","solutions":["Reproduce manually: run the exact `git worktree add` command from the error message args in the repo to see the real git failure","Verify the base ref exists: `git rev-parse --verify <ref>` before calling ensureWorktree","Check git version and permissions: git --version and write access to repoRoot and the target path","If stderr was genuinely empty due to a crash/timeout, retry once and capture result.stderr explicitly"],"exampleFix":"// before\nensureWorktree({ repoRoot, branchName: 'x', worktreePath, startPoint: 'origin/nope' }); // worktree_add_failed\n\n// after\nexecSync('git rev-parse --verify origin/main'); // assert base ref first\nensureWorktree({ repoRoot, branchName: 'x', worktreePath, startPoint: 'origin/main' });","handlingStrategy":"try-catch","validationCode":"import { execSync } from 'node:child_process';\n\nfunction refExists(repoRoot: string, ref: string): boolean {\n  try { execSync(`git rev-parse --verify ${ref}`, { cwd: repoRoot, stdio: 'ignore' }); return true; }\n  catch { return false; }\n}","typeGuard":null,"tryCatchPattern":"try {\n  ensureWorktree(plan);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('worktree_add_failed:')) {\n    const args = e.message.slice('worktree_add_failed:'.length);\n    // rerun `git worktree add ${args}` manually to capture real git stderr\n  }\n  throw e;\n}","preventionTips":["Pre-verify the start-point ref exists with git rev-parse","Pin a modern git version (>= 2.42) if using newer flags like --orphan","Ensure write permissions and disk space in repoRoot and target path"],"tags":["git","worktree","subprocess","diagnostics"],"backgroundTag":"git-worktree-add-failed","analyzedSha":"3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2","analyzedAt":"2026-08-27T22:18:39.783Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}