{"record":{"id":"b369fcd2499d1628","repo":"coleam00/Archon","slug":"unexpected-error-deleting-branch-branchname","errorCode":null,"errorMessage":"Unexpected error deleting branch '${branchName}': ${err.message}","messagePattern":"Unexpected error deleting branch '(.+?)': (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"packages/isolation/src/providers/worktree.ts","lineNumber":405,"sourceCode":"    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,\n    branchName: string,\n    result: DestroyResult,\n    remote = 'origin'\n  ): Promise<boolean> {","sourceCodeStart":387,"sourceCodeEnd":423,"githubUrl":"https://github.com/coleam00/Archon/blob/0773b9745896ef0612e709c80845a0f7db315b19/packages/isolation/src/providers/worktree.ts#L387-L423","documentation":"In deleteBranchTracked, WorktreeProvider attempts `git branch -d` and classifies failures. 'Already gone' is success and 'checked out at' is a structured warning, but any other git failure is recorded as an 'Unexpected error deleting branch' warning and the deletion is reported as failed (return false). It is a soft failure: the branch survives and the warning is pushed onto result.warnings.","triggerScenarios":"Calling destroy() on a worktree whose tracked branch cannot be deleted because git rejects it for a reason other than 'already gone' or 'checked out at' — e.g. the branch is not fully merged and `-d` refuses without `-D`, or the ref is corrupted/locked.","commonSituations":"Deleting a worktree whose branch has unmerged commits (git refuses `branch -d`); a packed-refs permission problem; concurrent gc/repack locking the ref during teardown.","solutions":["Inspect the logged branch_delete_failed entry for the underlying git error.","Run `git branch -d <branch>` manually; if unmerged, merge it first or delete with `git branch -D <branch>` if you are sure.","Retry destroy() once the conflicting git operation (gc, another checkout) finishes.","If the branch must always be force-deleted on teardown, adjust the provider call to use `-D`."],"exampleFix":"// before\nresult.warnings.push(`Unexpected error deleting branch '${branchName}': ${err.message}`);\n// after\n// force-delete an unmerged branch after confirming it is safe\nawait exec(`git -C ${repoPath} branch -D ${branchName}`);","handlingStrategy":"try-catch","validationCode":"const merged = await git(['branch', '--no-merged', 'HEAD'], repoPath);\nconst willRefuse = merged.includes(branchName);\nif (willRefuse) console.warn(`branch ${branchName} has unmerged commits; -d will fail`);","typeGuard":null,"tryCatchPattern":"const ok = await provider.destroy(worktree);\nfor (const w of result.warnings) {\n  if (w.startsWith('Unexpected error deleting branch')) {\n    // inspect logs, decide on force delete\n  }\n}","preventionTips":["Check `git branch --no-merged` before destroying worktrees and merge or force-delete knowingly.","Avoid running concurrent git maintenance (gc/repack) during teardown.","Keep branch lifecycle (creation and deletion) in one owner to avoid surprise lineage."],"tags":["git","branch-deletion","worktree"],"backgroundTag":"git-branch-delete-failed","analyzedSha":"0773b9745896ef0612e709c80845a0f7db315b19","analyzedAt":"2026-09-01T02:28:07.064Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}