AI Agents SDE Task Viewer
      • Context
      • Plan
      • Prd
  1. Home
  2. AgentSDE
  3. agent-core
  4. gh-232
  5. plan
  6. plan.md
plan.md(4.1 KB)· Apr 1, 2026· 3 min read
  • Summary
  • Files
  • Steps
  • Verification
  • Risks

Plan: /agent replan Directive#

Summary#

Add a replan directive that resets an active task's phase statuses, closes any open PR (deleting the branch), records a replan_triggered event, restores in-refinement labels, and restarts the pipeline from refine. Includes guardrails for already-in-refine and completed/archived tasks.

Files#

FileActionDescription
src/directive/dto/directive.dto.tsmodifyAdd 'replan' to ALLOWED_DIRECTIVES array
src/directive/directive.service.tsmodifyAdd replan handler: validate state, close PR, delete branch, reset task, enqueue refine
src/phase-router/phase-router.service.tsmodifyAdd replan to resolvePhase() (return 'refine'), VALID_DIRECTIVES, and route handler block
src/github/github.service.tsmodifyAdd closePR(owner, repo, prNumber) and deleteBranch(owner, repo, branch) methods
src/hooks/phase-hooks.service.tsmodifyAdd cleanupLabelsForReplan() helper: remove refined, in-review, agent-blocked; add in-refinement
src/directive/directive.service.spec.tsmodifyAdd tests: normal replan, already-in-refine guard, completed-task guard
src/phase-router/phase-router.service.spec.tsmodifyAdd test: replan directive resolves to 'refine' phase
src/github/github.service.spec.tsmodifyAdd tests for closePR() and deleteBranch()

Steps#

  1. Add 'replan' to ALLOWED_DIRECTIVES in directive.dto.ts — the DirectiveValue union auto-derives.
  2. Add closePR() and deleteBranch() methods to GitHubService using the REST API (PATCH /pulls/:number with state: 'closed' and DELETE /git/refs/heads/:branch).
  3. Add cleanupLabelsForReplan() to PhaseHooksService that removes refined, in-review, agent-blocked and adds in-refinement. Swallow 404s on label removal.
  4. Add replan handler in DirectiveService.applyDirective():
    • Reject if task.status is succeeded, failed, or complete (terminal) — post explanatory comment.
    • No-op if task.currentPhase === 'refine' and task.status === 'active' — post explanatory comment.
    • If task.prNumber exists: close PR and delete branch via GitHubService. Catch "already closed/deleted" as success; on other errors, log warning and proceed.
    • Call taskStateService.resetForRestart(task) to reset all phases and set currentPhase='refine'.
    • Clear task.prNumber and task.prBranch (stale PR metadata).
    • Save task, record replan_triggered event via EventService.
    • Call cleanupLabelsForReplan() via PhaseHooksService.
    • Enqueue refine phase via InternalAdapterService.
    • Post confirmation comment with details (noting if a PR was closed or a run was interrupted).
  5. Add 'replan' to VALID_DIRECTIVES array and resolvePhase() in PhaseRouterService — return 'refine' for replan. Add replan case in the directive handling block (similar to restart but skip the "active process" rejection since replan should proceed regardless).
  6. Add 'replan' entry to buildConfirmationMessage() in DirectiveService.
  7. Write unit tests: directive.service.spec.ts (normal replan with PR cleanup, already-in-refine guard, completed-task rejection), phase-router.service.spec.ts (resolvePhase returns 'refine'), github.service.spec.ts (closePR, deleteBranch).

Verification#

  • npm run lint passes with zero warnings.
  • npm run test passes all existing + new unit tests.
  • npm run build compiles without errors.

Risks#

  • In-flight Claude run interruption: The current architecture does not track child process PIDs across the service boundary. The replan handler resets task state and enqueues refine, but the in-flight process may still be running. Mitigation: the phase-router's "phase already active" guard will prevent double-execution; the old process will eventually time out or complete to a no-op state. Document this limitation.
  • PR close/branch delete race: If the PR was merged between the check and the close call, the close will 404. Mitigation: treat 404/422 on close and delete as success.
ContextPrd