{
"meta": {
"agent": "planner",
"task_id": "272",
"title": "Persist worktree across phases, cleanup on task completion",
"created_at": "2026-04-03T12:00:00Z"
},
"inputs": [
{
"name": "issue-272",
"type": "context",
"ref": "https://github.com/AgentSDE/agent-core/issues/272",
"notes": "Worktree lifecycle refactor — persist across phases, cleanup on terminal state"
},
{
"name": "worktree.service.ts",
"type": "file",
"ref": "src/worktree/worktree.service.ts",
"notes": "Current createWorktree() with aggressive pre-cleanup (lines 78-118)"
},
{
"name": "phase-router.service.ts",
"type": "file",
"ref": "src/phase-router/phase-router.service.ts",
"notes": "Per-phase cleanup in finally block (lines 427-438)"
},
{
"name": "internal-adapter.service.ts",
"type": "file",
"ref": "src/internal-adapter/internal-adapter.service.ts",
"notes": "Terminal state set at line 361-362, no worktree cleanup"
}
],
"outputs": [
{
"name": "plan",
"type": "plan",
"format": "md",
"content": "plan.md"
}
],
"files": [
{
"path": "src/worktree/worktree.service.ts",
"action": "modify",
"reason": "Replace createWorktree with idempotent getOrCreateWorktree, remove aggressive pre-cleanup"
},
{
"path": "src/worktree/worktree.service.spec.ts",
"action": "modify",
"reason": "Update tests for getOrCreateWorktree three-way logic"
},
{
"path": "src/phase-router/phase-router.service.ts",
"action": "modify",
"reason": "Use getOrCreateWorktree, remove per-phase cleanup from finally block"
},
{
"path": "src/phase-router/phase-router.service.spec.ts",
"action": "modify",
"reason": "Update mocks and assertions for new worktree lifecycle"
},
{
"path": "src/internal-adapter/internal-adapter.service.ts",
"action": "modify",
"reason": "Inject WorktreeService, add cleanup on terminal state"
},
{
"path": "src/internal-adapter/internal-adapter.module.ts",
"action": "modify",
"reason": "Import WorktreeModule for DI"
},
{
"path": "src/internal-adapter/internal-adapter.service.spec.ts",
"action": "create",
"reason": "Unit tests for terminal-state worktree cleanup"
}
],
"steps": [
{
"id": "S1",
"summary": "Refactor WorktreeService: replace createWorktree with idempotent getOrCreateWorktree and remove aggressive pre-cleanup",
"acceptance": [
"getOrCreateWorktree() exists and is exported",
"If worktree dir exists and is valid, returns existing path without recreation",
"If branch exists but no worktree, creates worktree with existing branch (no -b flag)",
"If neither exists, creates fresh worktree + branch from origin/master",
"Aggressive git branch -D and git worktree remove --force pre-cleanup removed",
"Unit tests cover all three paths plus invalid-worktree-dir edge case"
],
"depends_on": []
},
{
"id": "S2",
"summary": "Update PhaseRouterService to use getOrCreateWorktree and remove per-phase cleanup",
"acceptance": [
"executePhase() calls getOrCreateWorktree() instead of createWorktree()",
"finally block no longer calls cleanupWorktree()",
"Existing phase-router unit tests updated and passing",
"New test asserts cleanupWorktree is NOT called after phase execution"
],
"depends_on": [
"S1"
]
},
{
"id": "S3",
"summary": "Add terminal-state worktree cleanup in InternalAdapterService",
"acceptance": [
"WorktreeModule imported in internal-adapter.module.ts",
"WorktreeService injected in InternalAdapterService constructor",
"cleanupWorktree() called when task reaches complete status in advanceAndEnqueue()",
"Cleanup is wrapped in try/catch (idempotent, does not fail the pipeline)",
"Unit test: cleanup called on terminal state",
"Unit test: cleanup NOT called on normal phase advancement"
],
"depends_on": [
"S1"
]
}
],
"risks": [
{
"risk": "Concurrent phase invocations on same worktree",
"mitigation": "Queue processes tasks serially per issue — only one phase runs at a time"
},
{
"risk": "Stale worktree state between phases",
"mitigation": "getOrCreateWorktree runs git fetch origin on reuse; Claude skill handles branch checkout"
}
],
"assumptions": [
"The job queue guarantees serial-per-issue processing — no two phases for the same issue run concurrently",
"ClaudeInvocationService skills handle checking out the correct branch within the worktree"
],
"open_questions": []
}