{
"meta": {
"agent": "planner",
"task_id": "270",
"title": "Make createWorktree() idempotent — reuse existing worktree/branch",
"created_at": "2026-04-03T12:00:00Z"
},
"inputs": [
{
"name": "GitHub issue #270",
"type": "context",
"ref": "https://github.com/AgentSDE/agent-core/issues/270",
"notes": "P0 fix for stuck retry loop when branch is checked out elsewhere"
},
{
"name": "worktree.service.ts",
"type": "file",
"ref": "src/worktree/worktree.service.ts",
"notes": "createWorktree() lines 24-143 — current destructive nuke-and-recreate logic"
},
{
"name": "worktree.service.spec.ts",
"type": "file",
"ref": "src/worktree/worktree.service.spec.ts",
"notes": "Existing unit tests using jest mocks for child_process/fs"
}
],
"outputs": [
{
"name": "Implementation plan",
"type": "plan",
"format": "md",
"content": "plan.md"
}
],
"files": [
{
"path": "src/worktree/worktree.service.ts",
"action": "modify",
"reason": "Replace destructive worktree/branch cleanup with idempotent detect-and-reuse logic"
},
{
"path": "src/worktree/worktree.service.spec.ts",
"action": "modify",
"reason": "Add 4 new unit tests for idempotent paths; update existing tests"
}
],
"steps": [
{
"id": "S1",
"summary": "Implement idempotent createWorktree() logic with isValidWorktree helper, three-path branching (reuse/existing-branch/fresh), and remove aggressive git branch -D cleanup",
"acceptance": [
"isValidWorktree() helper checks git worktree list for the given path",
"Valid existing worktree is reused with fetch+reset instead of removal",
"Invalid/corrupted worktree directory is removed and falls through to branch-check",
"Existing local branch without worktree uses git worktree add <path> <branch> (no -b)",
"Fresh start (no worktree, no branch) uses git worktree add -b <branch> <path> origin/master",
"git branch -D block (old lines 100-118) is completely removed",
"git fetch origin errors in reuse path are surfaced, not swallowed"
],
"depends_on": []
},
{
"id": "S2",
"summary": "Add unit tests for all four idempotent paths and update existing tests to match new flow",
"acceptance": [
"Test: worktree exists and is valid → reused without removal",
"Test: worktree exists but is invalid → removed, falls through to branch-check",
"Test: branch exists locally, no worktree → git worktree add without -b",
"Test: neither exists → fresh git worktree add -b (existing behavior)",
"Existing tests updated to remove assertions on git branch -D behavior",
"npm run test passes with zero failures",
"npm run lint passes with zero warnings"
],
"depends_on": [
"S1"
]
}
],
"risks": [
{
"risk": "git reset --hard in reuse path discards uncommitted changes",
"mitigation": "Agent worktrees are ephemeral and always start from origin/master; issue explicitly requests this behavior"
},
{
"risk": "isValidWorktree check via git worktree list may be slow on repos with many worktrees",
"mitigation": "Agent repos typically have <10 worktrees; performance impact negligible"
}
],
"assumptions": [
"Agent worktrees are always intended to start from origin/master — resetting on reuse is safe",
"The exec() helper already surfaces errors as rejected promises — no additional error handling wrapper needed"
],
"open_questions": []
}