coleam00/Archon · warning

Cannot delete branch '${branchName}': branch is checked out

Error message

Cannot delete branch '${branchName}': branch is checked out elsewhere

What it means

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.

Source

Thrown at packages/isolation/src/providers/worktree.ts:401

    repoPath: string,
    branchName: string,
    result: DestroyResult
  ): Promise<boolean> {
    try {
      await execFileAsync('git', ['-C', repoPath, 'branch', '-D', branchName], {
        timeout: GIT_OPERATION_TIMEOUT_MS,
      });
      getLog().debug({ repoPath, branchName }, 'branch_deleted');
      return true;
    } catch (error) {
      const err = error as Error & { stderr?: string };
      const errorText = `${err.message} ${err.stderr ?? ''}`;

      if (errorText.includes('not found') || errorText.includes('did not match any')) {
        getLog().debug({ repoPath, branchName }, 'branch_already_deleted');
        return true; // Already gone counts as success
      } else if (errorText.includes('checked out at')) {
        const warning = `Cannot delete branch '${branchName}': branch is checked out elsewhere`;
        getLog().warn({ repoPath, branchName }, 'branch_checked_out_elsewhere');
        result.warnings.push(warning);
        return false;
      } else {
        const warning = `Unexpected error deleting branch '${branchName}': ${err.message}`;
        getLog().error({ err: error, repoPath, branchName }, 'branch_delete_failed');
        result.warnings.push(warning);
        return false;
      }
    }
  }

  /**
   * Delete a remote branch and track the result. Never throws - remote branch deletion is best-effort.
   * Returns true if branch was deleted or already gone, false if deletion failed.
   */
  private async deleteRemoteBranchTracked(
    repoPath: string,

View on GitHub (pinned to 0773b97458)

Solutions

  1. Check out a different branch (e.g. main) in the worktree holding it, then delete: git -C <otherPath> switch main && git branch -D <branchName>
  2. Find the holder with git worktree list --porcelain and git branch --points-at <branchName>, then tear that worktree down first
  3. Use unique per-run branch names so concurrent runs never share a branch
  4. If the branch must survive, accept the warning and keep the branch (it is a warning, not a failure)

Example fix

// before
git branch -D archon/issue-42 // error: checked out at ...
// after
git -C /repos/main switch main
git branch -D archon/issue-42
Defensive patterns

Strategy: validation

Validate before calling

import { execFile } from 'node:child_process';
const { stdout } = await promisify(execFile)('git', ['-C', repoPath, 'worktree', 'list', '--porcelain']);
const heldElsewhere = stdout.includes(`branch refs/heads/${branchName}`)
  && stdout.split('worktree ').length > 2;
if (heldElsewhere) console.warn(`Branch ${branchName} is checked out elsewhere; delete will be skipped`);

Type guard

function hasCheckedOutBranchWarning(result: DestroyResult): boolean {
  return result.warnings.some(w => w.includes('branch is checked out elsewhere'));
}

Try / catch

const result = await provider.destroy({ worktreePath, branchName, canonicalRepoPath: repoPath });
if (hasCheckedOutBranchWarning(result)) {
  getLog().warn({ branchName }, 'branch kept: checked out in another worktree');
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of coleam00/Archon@0773b97458 (2026-09-01). Data as JSON: /api/errors/4e56c5be03b772a6. Report an issue: GitHub.