Plan: Replan/Restart Must Invalidate Cached Plan and PR Association#
Summary#
When /agent replan or /agent restart fires, cached plan artefacts and PR associations must be invalidated so the next run generates a fresh plan and opens a new PR. Currently, replan clears prNumber/prBranch but does not clear plan artefacts or track attempt count; restart does not clear PR state at all.
Files#
| File | Action | Description |
|---|---|---|
src/database/entities/task.entity.ts | modify | Add replanAttempt column (integer, default 0) |
src/task-state/task-state.service.ts | modify | Add resetForReplan() method that clears PR state, increments replanAttempt, and resets phases |
src/directive/directive.service.ts | modify | Use resetForReplan() for replan; clear PR state for restart; add replan_attempt telemetry; close open PR with superseded message |
src/artefacts/artefacts.service.ts | modify | Add clearPlanArtefacts(taskDir) to remove plan/*.md/json files on replan |
src/task-state/task-state.service.spec.ts | modify | Add unit tests for resetForReplan() |
src/directive/directive.service.spec.ts | modify | Add tests: replan invalidates plan cache, replan forces new PR branch, restart clears PR state |
Steps#
- Add
replanAttemptto TaskEntity — new nullable integer column with default 0, snake_casereplan_attempt. - Add
resetForReplan()to TaskStateService — callsresetForRestart(), then nullifiesprNumber/prBranch, incrementsreplanAttempt. - Add
clearPlanArtefacts()to ArtefactsService — removes files in{taskDir}/plan/directory (plan.md, context.json, prd.json). - Update DirectiveService replan handler — use
resetForReplan()instead of manual field clearing; callclearPlanArtefacts(); recordreplan_attemptevent with attempt number; post superseded comment on open PR before closing. - Update DirectiveService restart handler — clear
prNumber/prBranch(currently missing); post superseded comment on open PR if exists. - Add unit tests — cover:
resetForReplan()clears PR + increments counter; replan clears plan artefacts; restart clears PR state; no-op when no prior artefacts;replan_attempttelemetry recorded.
Verification#
npm run testpasses with new unit tests covering replan invalidation, restart PR clearing, and idempotent no-op cases.npm run lintpasses with zero warnings.- Manually verify: after replan,
task.prNumber === null,task.prBranch === null,task.replanAttemptincremented, plan directory cleared.
Risks#
- SQLite migration: Adding
replanAttemptcolumn withDEFAULT 0is safe for TypeORM synchronize mode, but if the project uses explicit migrations, a migration file may be needed. - Restart scope expansion: Adding PR cleanup to
restartchanges existing behavior — the issue explicitly requires it, but it's a behavioral change to flag in the PR.