{"record":{"id":"b3abf25b616c49ce","repo":"Yeachan-Heo/oh-my-codex","slug":"branch-in-use","errorCode":"branch_in_use","errorMessage":"branch_in_use:${plan.branchName}","messagePattern":"branch_in_use:(.+?)","errorType":"validation","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/team/worktree.ts","lineNumber":441,"sourceCode":"\n    if (plan.branchName) {\n      upsertCurrentTaskBaseline(plan.repoRoot, {\n        branch_name: plan.branchName,\n        worktree_path: reused.worktreePath,\n        base_ref: plan.baseRef,\n        status: 'active',\n      });\n    }\n\n    return reused;\n  }\n\n  if (existsSync(plan.worktreePath)) {\n    throw new Error(`worktree_path_conflict:${plan.worktreePath}`);\n  }\n\n  if (plan.branchName && hasBranchInUse(allWorktrees, plan.branchName, plan.worktreePath)) {\n    throw new Error(`branch_in_use:${plan.branchName}`);\n  }\n\n  if (plan.branchName) {\n    assertCurrentTaskBranchAvailable(plan.repoRoot, plan.branchName, plan.worktreePath);\n  }\n\n  mkdirSync(dirname(plan.worktreePath), { recursive: true });\n  const branchAlreadyExisted = plan.branchName ? branchExists(plan.repoRoot, plan.branchName) : false;\n\n  const addArgs = ['worktree', 'add'];\n  if (plan.detached) {\n    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","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/Yeachan-Heo/oh-my-codex/blob/3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2/src/team/worktree.ts#L423-L459","documentation":"Thrown by ensureWorktree when the requested branch name is already checked out in another worktree (git forbids the same branch in two worktrees). The library detects this up front via hasBranchInUse across all known worktrees so the failure is deterministic instead of surfacing as a cryptic git error later.","triggerScenarios":"Calling ensureWorktree with plan.branchName set to a branch that another worktree (other than plan.worktreePath itself) currently has checked out. Typical with shared, non-unique branch names like 'main', 'task', or a per-issue branch reused across parallel benches.","commonSituations":"Parallel agent benches all defaulting to the same branch name; a previous bench still holding the branch checked out in its worktree; re-running a team start after a partial cleanup that removed the directory but not the git worktree registration.","solutions":["Pick a unique branch name per worktree (append a run id or slug: `task-<id>` instead of `task`)","Find the holder: `git worktree list` in the repo, then either remove that worktree or reuse it instead of creating a new one","If the holding worktree is stale: `git worktree remove <path>` (or `git worktree prune`) then retry","Pass a falsy branchName if the plan does not need a specific branch, letting git create a detached/new ref"],"exampleFix":"// before\nensureWorktree({ repoRoot, branchName: 'agent', worktreePath: '.worktrees/a1' });\nensureWorktree({ repoRoot, branchName: 'agent', worktreePath: '.worktrees/a2' }); // branch_in_use:agent\n\n// after\nensureWorktree({ repoRoot, branchName: 'agent-a1', worktreePath: '.worktrees/a1' });\nensureWorktree({ repoRoot, branchName: 'agent-a2', worktreePath: '.worktrees/a2' });","handlingStrategy":"validation","validationCode":"import { execSync } from 'node:child_process';\n\nfunction branchIsFree(repoRoot: string, branch: string): boolean {\n  const list = execSync('git worktree list --porcelain', { cwd: repoRoot, encoding: 'utf8' });\n  return !list.split('\\n').some((l) => l.startsWith('branch ') && l.trim().endsWith(branch));\n}","typeGuard":null,"tryCatchPattern":"try {\n  ensureWorktree(plan);\n} catch (e) {\n  if (e instanceof Error && e.message.startsWith('branch_in_use:')) {\n    plan.branchName = `${plan.branchName}-${Date.now()}`; // uniquify and retry\n  } else throw e;\n}","preventionTips":["Generate unique branch names per bench/run (suffix with run id)","Check `git worktree list` before provisioning","Clean up stale worktrees with git worktree prune in CI setup"],"tags":["git","worktree","branch","conflict"],"backgroundTag":"git-branch-checked-out-elsewhere","analyzedSha":"3ad79a8a6fe6e95fdbb8c00e40716fffe4011ce2","analyzedAt":"2026-08-27T22:18:39.783Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}