{"record":{"id":"4e56c5be03b772a6","repo":"coleam00/Archon","slug":"cannot-delete-branch-branchname-branch-is-ch","errorCode":null,"errorMessage":"Cannot delete branch '${branchName}': branch is checked out elsewhere","messagePattern":"Cannot delete branch '(.+?)': branch is checked out elsewhere","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/isolation/src/providers/worktree.ts","lineNumber":401,"sourceCode":"    repoPath: string,\n    branchName: string,\n    result: DestroyResult\n  ): Promise<boolean> {\n    try {\n      await execFileAsync('git', ['-C', repoPath, 'branch', '-D', branchName], {\n        timeout: GIT_OPERATION_TIMEOUT_MS,\n      });\n      getLog().debug({ repoPath, branchName }, 'branch_deleted');\n      return true;\n    } catch (error) {\n      const err = error as Error & { stderr?: string };\n      const errorText = `${err.message} ${err.stderr ?? ''}`;\n\n      if (errorText.includes('not found') || errorText.includes('did not match any')) {\n        getLog().debug({ repoPath, branchName }, 'branch_already_deleted');\n        return true; // Already gone counts as success\n      } else if (errorText.includes('checked out at')) {\n        const warning = `Cannot delete branch '${branchName}': branch is checked out elsewhere`;\n        getLog().warn({ repoPath, branchName }, 'branch_checked_out_elsewhere');\n        result.warnings.push(warning);\n        return false;\n      } else {\n        const warning = `Unexpected error deleting branch '${branchName}': ${err.message}`;\n        getLog().error({ err: error, repoPath, branchName }, 'branch_delete_failed');\n        result.warnings.push(warning);\n        return false;\n      }\n    }\n  }\n\n  /**\n   * Delete a remote branch and track the result. Never throws - remote branch deletion is best-effort.\n   * Returns true if branch was deleted or already gone, false if deletion failed.\n   */\n  private async deleteRemoteBranchTracked(\n    repoPath: string,","sourceCodeStart":383,"sourceCodeEnd":419,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/isolation/src/providers/worktree.ts#L383-L419","documentation":"A warning recorded by deleteBranchTracked (called from destroy) in packages/isolation/src/providers/worktree.ts. When 'git branch -d/-D' fails with output containing 'checked out at', the branch is currently checked out in another worktree, so git refuses deletion; the function records the warning and returns false instead of throwing.","triggerScenarios":"destroy() tries to delete options.branchName but that branch is checked out in a different worktree (often the main checkout or another agent's worktree), so git rejects the delete.","commonSituations":"A developer manually checked out the agent's branch in the main repo; two isolation runs configured with the same branch name; the branch was adopted into another worktree before teardown.","solutions":["Check out a different branch (e.g. main) in the worktree holding it, then delete: git -C <otherPath> switch main && git branch -D <branchName>","Find the holder with git worktree list --porcelain and git branch --points-at <branchName>, then tear that worktree down first","Use unique per-run branch names so concurrent runs never share a branch","If the branch must survive, accept the warning and keep the branch (it is a warning, not a failure)"],"exampleFix":"// before\ngit branch -D archon/issue-42 // error: checked out at ...\n// after\ngit -C /repos/main switch main\ngit branch -D archon/issue-42","handlingStrategy":"validation","validationCode":"import { execFile } from 'node:child_process';\nconst { stdout } = await promisify(execFile)('git', ['-C', repoPath, 'worktree', 'list', '--porcelain']);\nconst heldElsewhere = stdout.includes(`branch refs/heads/${branchName}`)\n  && stdout.split('worktree ').length > 2;\nif (heldElsewhere) console.warn(`Branch ${branchName} is checked out elsewhere; delete will be skipped`);","typeGuard":"function hasCheckedOutBranchWarning(result: DestroyResult): boolean {\n  return result.warnings.some(w => w.includes('branch is checked out elsewhere'));\n}","tryCatchPattern":"const result = await provider.destroy({ worktreePath, branchName, canonicalRepoPath: repoPath });\nif (hasCheckedOutBranchWarning(result)) {\n  getLog().warn({ branchName }, 'branch kept: checked out in another worktree');\n}","preventionTips":["Use unique branch names per isolation run to avoid cross-worktree checkout conflicts","Before deleting, run git worktree list to find any worktree holding the branch","Detach or switch the holding worktree to main before teardown","Treat the warning as informational when branch retention is acceptable"],"tags":["git-branch","worktree","cleanup","warning"],"backgroundTag":"branch-checked-out-elsewhere","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}