agent-orchestrator icon indicating copy to clipboard operation
agent-orchestrator copied to clipboard

spawn: branch/worktree conflict gives no recovery path when re-spawning an existing issue

Open ubermensch19 opened this issue 7 months ago • 4 comments

Summary

When ao spawn <issue> is interrupted (process killed, terminal closed, tracker fetch fails mid-flow) after the worktree has already been created but before the session reaches a stable state, the next ao spawn <issue> attempt hard-fails with:

✗ Failed to checkout branch "feat/issue-9215" in worktree:
  Command failed: git checkout feat/issue-9215
  fatal: 'feat/issue-9215' is already used by worktree at '/home/priyanshu/.worktrees/sktime/skt-1'

There is no recovery path. The user is stuck:

  • ao spawn <issue> → fails with the worktree conflict error
  • The agent may have already done meaningful work in that worktree

Root Cause

The spawn flow creates the worktree early (step 4 of `session-manager.ts:spawn()`), but session metadata is written much later (step 8). If anything fails in between — or if the user interrupts the process — the worktree remains on disk with no corresponding session record.

On a retry:

  1. `sm.spawn()` computes the same branch name (e.g. `feat/issue-9215`) from the issue ID
  2. The workspace-worktree plugin's `create()` hits the `"already exists"` branch in its fallback path
  3. `git worktree add {newPath} {baseRef}` + `git checkout {branch}` — this fails if `{branch}` is already checked out in the orphaned worktree
  4. The plugin throws, spawn aborts — no cleanup, no guidance

Batch-spawn deduplication only checks existing session metadata for the issue ID. It does not check git for existing branches or worktrees, so orphaned worktrees are invisible to it.


Affected Scenarios

Scenario A — Orphaned worktree, no session metadata Scenario B — Session in terminal state, worktree still on disk Scenario C — Multiple stale worktrees for the same issue Scenario D — Tracker fetch fails after worktree is created


Steps to Reproduce

  1. Spawn an issue where the tracker repo has issues disabled
ao spawn 9215   # fails mid-way; worktree created at ~/.worktrees/sktime/skt-1
  1. Try again
ao spawn 9215
# ✗ Failed to checkout branch "feat/issue-9215" in worktree:
#   fatal: 'feat/issue-9215' is already used by worktree at '/home/priyanshu/.worktrees/sktime/skt-1'

Manual workaround until fixed:

git worktree remove ~/.worktrees/sktime/skt-1 --force
git branch -D feat/issue-9215
ao spawn 9215

ubermensch19 avatar Apr 08 '26 05:04 ubermensch19

@priyanshuharshbodhi1 any reason why we need to spawn two agent sessions for the same issue?

OrcInc avatar Apr 08 '26 17:04 OrcInc

@i-trytoohard I spawned an agent for an issue and there was an error in my yaml file, due to which it showed an error and exited. When I fixed the yaml file and retried to spawn agent for the same issue it shows error, it should continue from where it left of.

ubermensch19 avatar Apr 08 '26 17:04 ubermensch19

Adding a chain-spawn variant of this branch+worktree exclusivity issue, since the root cause is the same (AO assumes one worktree per branch but doesn't handle simultaneous-need cases).

Our case is not retry-after-failure — it's two intentional sibling sessions wanting the same branch:

  1. Parent implementer session has a worktree on branch feat/X, working on a PR.
  2. Implementer issues ao spawn for a sibling reviewer session (chain-spawn pattern).
  3. Reviewer's session-creation creates a worktree on the same branch — collision.

We worked around it by having the spawned reviewer fetch its diff via gh pr diff $PR_NUMBER > diff.patch in its own worktree (no gh pr checkout), so the reviewer never actually needs the implementer's branch checked out — but the underlying branch+worktree exclusivity assumption still bites if any future chain-spawn use case needs to actually run the branch's code (e.g., tests, build).

Two ways the upstream fix could cover both shapes:

  1. git worktree add --detach in the workspace plugin's fallback — gives the new worktree the branch HEAD without claiming exclusive ownership of the branch ref. Allows simultaneous worktrees on the same branch as long as nobody pushes to it from the secondary worktree.
  2. Branch ownership transfer primitive (ao spawn --claim-pr does some of this in claimPR() at session-manager.ts:2483-2580): a parent session relinquishes its branch claim, the new session claims it, after the new session exits the parent re-claims. Heavier mechanism; only worth it if simultaneous worktrees actually need write access to the branch.

(1) feels like the right default for read-mostly chain-spawn use cases. (2) is the right primitive when both halves need to commit.

Tonedeath avatar Apr 29 '26 10:04 Tonedeath

Closing this as part of the migration cleanup for the ground-up rewrite of Agent Orchestrator.

This item was opened more than a month before the migration batch and the codebase has been replaced, so this thread no longer maps cleanly onto the current source tree.

If something here still needs tracking, please reopen this item or open a fresh one against the new tree. Thank you for contributing.

harshitsinghbhandari avatar Jun 24 '26 14:06 harshitsinghbhandari