@nathapp/nax 0.20.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/.claude/settings.json +15 -0
  2. package/.mcp.json +8 -0
  3. package/docs/20260304-review-nax.md +492 -0
  4. package/docs/ROADMAP.md +65 -18
  5. package/docs/adr/ADR-005-implementation-plan.md +655 -0
  6. package/docs/adr/ADR-005-pipeline-re-architecture.md +464 -0
  7. package/docs/specs/bug-039-orphan-processes.md +131 -0
  8. package/docs/specs/bug-040-review-rectification.md +82 -0
  9. package/docs/specs/bug-041-cross-story-test-isolation.md +88 -0
  10. package/docs/specs/bug-042-verifier-failure-capture.md +117 -0
  11. package/docs/specs/feat-010-smart-runner-git-history.md +96 -0
  12. package/docs/specs/feat-011-file-context-strategy.md +73 -0
  13. package/docs/specs/feat-012-tdd-writer-tier.md +79 -0
  14. package/docs/specs/feat-013-test-after-review.md +89 -0
  15. package/docs/specs/feat-014-heartbeat-observability.md +127 -0
  16. package/memory/topic/feat-010-baseref.md +28 -0
  17. package/memory/topic/feat-013-test-after-deprecation.md +22 -0
  18. package/nax/config.json +7 -4
  19. package/nax/features/bug-039-medium/prd.json +45 -0
  20. package/package.json +2 -2
  21. package/src/agents/claude.ts +109 -15
  22. package/src/config/types.ts +11 -0
  23. package/src/context/builder.ts +9 -1
  24. package/src/execution/dry-run.ts +81 -0
  25. package/src/execution/escalation/tier-outcome.ts +29 -44
  26. package/src/execution/executor-types.ts +65 -0
  27. package/src/execution/index.ts +0 -17
  28. package/src/execution/iteration-runner.ts +132 -0
  29. package/src/execution/lifecycle/index.ts +0 -1
  30. package/src/execution/lifecycle/run-regression.ts +5 -5
  31. package/src/execution/pipeline-result-handler.ts +51 -254
  32. package/src/execution/sequential-executor.ts +72 -315
  33. package/src/execution/story-selector.ts +75 -0
  34. package/src/pipeline/event-bus.ts +276 -0
  35. package/src/pipeline/runner.ts +51 -77
  36. package/src/pipeline/stages/autofix.ts +133 -0
  37. package/src/pipeline/stages/completion.ts +22 -30
  38. package/src/pipeline/stages/index.ts +30 -13
  39. package/src/pipeline/stages/rectify.ts +93 -0
  40. package/src/pipeline/stages/regression.ts +88 -0
  41. package/src/pipeline/stages/review.ts +19 -153
  42. package/src/pipeline/stages/verify.ts +19 -3
  43. package/src/pipeline/subscribers/hooks.ts +133 -0
  44. package/src/pipeline/subscribers/interaction.ts +68 -0
  45. package/src/pipeline/subscribers/reporters.ts +174 -0
  46. package/src/pipeline/types.ts +12 -1
  47. package/src/review/orchestrator.ts +105 -0
  48. package/src/review/runner.ts +39 -4
  49. package/src/routing/router.ts +3 -3
  50. package/src/routing/strategies/keyword.ts +5 -2
  51. package/src/routing/strategies/llm.ts +27 -1
  52. package/src/tdd/prompts.ts +1 -1
  53. package/src/utils/git.ts +49 -25
  54. package/src/verification/executor.ts +8 -2
  55. package/src/verification/index.ts +1 -1
  56. package/src/verification/orchestrator-types.ts +145 -0
  57. package/src/verification/orchestrator.ts +76 -0
  58. package/src/{execution/post-verify-rectification.ts → verification/rectification-loop.ts} +13 -20
  59. package/src/verification/{gate.ts → runners.ts} +17 -105
  60. package/src/verification/smart-runner.ts +6 -10
  61. package/src/verification/strategies/acceptance.ts +133 -0
  62. package/src/verification/strategies/regression.ts +90 -0
  63. package/src/verification/strategies/scoped.ts +123 -0
  64. package/test/COVERAGE-GAPS.md +333 -0
  65. package/test/{acceptance → e2e}/cm-003-default-view.test.ts +1 -0
  66. package/test/{integration/e2e.test.ts → e2e/plan-analyze-run.test.ts} +1 -0
  67. package/test/integration/{agent-validation.test.ts → cli/agent-validation.test.ts} +3 -3
  68. package/test/integration/{cli-config-default-edge-cases.test.ts → cli/cli-config-default-edge-cases.test.ts} +6 -5
  69. package/test/integration/{cli-config-default-view.test.ts → cli/cli-config-default-view.test.ts} +8 -7
  70. package/test/integration/{cli-config-diff.test.ts → cli/cli-config-diff.test.ts} +3 -2
  71. package/test/integration/{cli-config.test.ts → cli/cli-config.test.ts} +3 -2
  72. package/test/integration/{cli-diagnose.test.ts → cli/cli-diagnose.test.ts} +5 -4
  73. package/test/integration/{cli-logs.test.ts → cli/cli-logs.test.ts} +12 -3
  74. package/test/integration/{cli-plugins.test.ts → cli/cli-plugins.test.ts} +4 -3
  75. package/test/integration/{cli-precheck.test.ts → cli/cli-precheck.test.ts} +4 -3
  76. package/test/integration/{cli-run-headless.test.ts → cli/cli-run-headless.test.ts} +3 -2
  77. package/test/integration/{cli.test.ts → cli/cli.test.ts} +2 -1
  78. package/test/integration/{precheck-integration.test.ts → cli/precheck-integration.test.ts} +10 -9
  79. package/test/integration/{precheck-orchestrator.test.ts → cli/precheck-orchestrator.test.ts} +4 -3
  80. package/test/integration/{precheck.test.ts → cli/precheck.test.ts} +5 -4
  81. package/test/integration/{config-loader.test.ts → config/config-loader.test.ts} +2 -1
  82. package/test/integration/{config.test.ts → config/config.test.ts} +2 -2
  83. package/test/integration/config/merger.test.ts +1 -0
  84. package/test/integration/config/paths.test.ts +1 -0
  85. package/test/integration/{security-loader.test.ts → config/security-loader.test.ts} +2 -2
  86. package/test/integration/{context-integration.test.ts → context/context-integration.test.ts} +7 -6
  87. package/test/integration/{path-security.test.ts → context/context-path-security.test.ts} +2 -2
  88. package/test/integration/{context-provider-injection.test.ts → context/context-provider-injection.test.ts} +7 -6
  89. package/test/integration/{context-verification-integration.test.ts → context/context-verification-integration.test.ts} +5 -4
  90. package/test/integration/{s5-greenfield-fallback.test.ts → context/s5-greenfield-fallback.test.ts} +4 -3
  91. package/test/integration/{isolation.test.ts → execution/execution-isolation.test.ts} +1 -1
  92. package/test/integration/{execution.test.ts → execution/execution.test.ts} +8 -8
  93. package/test/integration/{parallel.test.ts → execution/parallel.test.ts} +2 -1
  94. package/test/integration/{prd-pause.test.ts → execution/prd-pause.test.ts} +2 -2
  95. package/test/integration/{prd-resolvers.test.ts → execution/prd-resolvers.test.ts} +3 -2
  96. package/test/integration/{progress.test.ts → execution/progress.test.ts} +1 -1
  97. package/test/integration/execution/runner-batching.test.ts +682 -0
  98. package/test/integration/{runner-config-plugins.test.ts → execution/runner-config-plugins.test.ts} +3 -2
  99. package/test/integration/execution/runner-escalation.test.ts +561 -0
  100. package/test/integration/{runner-fixes.test.ts → execution/runner-fixes.test.ts} +4 -3
  101. package/test/integration/{runner-plugin-integration.test.ts → execution/runner-plugin-integration.test.ts} +6 -5
  102. package/test/integration/execution/runner-queue-and-attempts.test.ts +476 -0
  103. package/test/integration/{status-file-integration.test.ts → execution/status-file-integration.test.ts} +9 -8
  104. package/test/integration/{status-file.test.ts → execution/status-file.test.ts} +3 -2
  105. package/test/integration/{status-writer.test.ts → execution/status-writer.test.ts} +5 -4
  106. package/test/integration/{story-id-in-events.test.ts → execution/story-id-in-events.test.ts} +9 -8
  107. package/test/integration/{interaction-chain-pipeline.test.ts → interaction/interaction-chain-pipeline.test.ts} +26 -14
  108. package/test/integration/{hooks.test.ts → pipeline/hooks.test.ts} +4 -2
  109. package/test/integration/{pipeline-acceptance.test.ts → pipeline/pipeline-acceptance.test.ts} +7 -6
  110. package/test/integration/{pipeline-events.test.ts → pipeline/pipeline-events.test.ts} +7 -6
  111. package/test/integration/{pipeline.test.ts → pipeline/pipeline.test.ts} +9 -7
  112. package/test/integration/{reporter-lifecycle.test.ts → pipeline/reporter-lifecycle.test.ts} +9 -7
  113. package/test/integration/{verify-stage.test.ts → pipeline/verify-stage.test.ts} +7 -5
  114. package/test/integration/{analyze-integration.test.ts → plan/analyze-integration.test.ts} +3 -2
  115. package/test/integration/{analyze-scanner.test.ts → plan/analyze-scanner.test.ts} +8 -7
  116. package/test/integration/{logger.test.ts → plan/logger.test.ts} +1 -1
  117. package/test/integration/{plan.test.ts → plan/plan.test.ts} +3 -3
  118. package/test/integration/plugins/config-integration.test.ts +1 -0
  119. package/test/integration/plugins/config-resolution.test.ts +1 -0
  120. package/test/integration/plugins/loader.test.ts +1 -0
  121. package/test/integration/plugins/{registry.test.ts → plugins-registry.test.ts} +1 -0
  122. package/test/integration/plugins/validator.test.ts +1 -0
  123. package/test/integration/{review-config-commands.test.ts → review/review-config-commands.test.ts} +4 -3
  124. package/test/integration/{review-config-schema.test.ts → review/review-config-schema.test.ts} +3 -2
  125. package/test/integration/{review-plugin-integration.test.ts → review/review-plugin-integration.test.ts} +5 -4
  126. package/test/integration/{review.test.ts → review/review.test.ts} +3 -2
  127. package/test/integration/routing/plugin-routing-advanced.test.ts +461 -0
  128. package/test/integration/{plugin-routing.test.ts → routing/plugin-routing-core.test.ts} +10 -404
  129. package/test/integration/{routing-stage-bug-021.test.ts → routing/routing-stage-bug-021.test.ts} +8 -7
  130. package/test/integration/{routing-stage-greenfield.test.ts → routing/routing-stage-greenfield.test.ts} +7 -6
  131. package/test/integration/{tdd-cleanup.test.ts → tdd/tdd-cleanup.test.ts} +1 -1
  132. package/test/integration/tdd/tdd-orchestrator-core.test.ts +565 -0
  133. package/test/integration/tdd/tdd-orchestrator-failureCategory.test.ts +355 -0
  134. package/test/integration/tdd/tdd-orchestrator-fallback.test.ts +311 -0
  135. package/test/integration/tdd/tdd-orchestrator-lite.test.ts +289 -0
  136. package/test/integration/tdd/tdd-orchestrator-prompts.test.ts +260 -0
  137. package/test/integration/tdd/tdd-orchestrator-verdict.test.ts +536 -0
  138. package/test/integration/tmp/headless-test/test.jsonl +30 -0
  139. package/test/integration/{test-scanner.test.ts → verification/test-scanner.test.ts} +1 -1
  140. package/test/integration/{verification-asset-check.test.ts → verification/verification-asset-check.test.ts} +3 -2
  141. package/test/unit/acceptance.test.ts +1 -0
  142. package/test/unit/agent-stderr-capture.test.ts +1 -0
  143. package/test/unit/agents/claude.test.ts +107 -0
  144. package/test/unit/analyze-classifier.test.ts +1 -0
  145. package/test/unit/auto-detect.test.ts +1 -0
  146. package/test/unit/cli-status.test.ts +1 -0
  147. package/test/unit/commands/common.test.ts +1 -0
  148. package/test/unit/commands/logs.test.ts +1 -0
  149. package/test/unit/commands/unlock.test.ts +1 -0
  150. package/test/unit/config/defaults.test.ts +1 -0
  151. package/test/unit/config/regression-gate-schema.test.ts +1 -0
  152. package/test/unit/config/smart-runner-flag.test.ts +1 -0
  153. package/test/unit/constitution-generators.test.ts +1 -0
  154. package/test/unit/constitution.test.ts +1 -0
  155. package/test/unit/context/context-autodetect.test.ts +297 -0
  156. package/test/unit/context/context-build.test.ts +575 -0
  157. package/test/unit/context/context-coverage.test.ts +236 -0
  158. package/test/unit/context/context-error.test.ts +93 -0
  159. package/test/unit/context/context-estimate-tokens.test.ts +201 -0
  160. package/test/unit/context/context-format.test.ts +302 -0
  161. package/test/unit/context/context-isolation.test.ts +267 -0
  162. package/test/unit/context/context-sort.test.ts +93 -0
  163. package/test/unit/context/context-story.test.ts +108 -0
  164. package/test/{context → unit/context}/prior-failures.test.ts +5 -4
  165. package/test/unit/context.test.ts +7 -3
  166. package/test/unit/crash-recovery.test.ts +1 -0
  167. package/test/unit/escalation.test.ts +1 -0
  168. package/test/unit/execution/lifecycle/run-completion.test.ts +1 -0
  169. package/test/unit/execution/lifecycle/run-regression.test.ts +2 -0
  170. package/test/{execution → unit/execution}/pid-registry.test.ts +2 -1
  171. package/test/{execution → unit/execution}/structured-failure.test.ts +3 -2
  172. package/test/unit/execution-logging-stderr.test.ts +1 -0
  173. package/test/unit/execution-stage.test.ts +1 -0
  174. package/test/unit/fix-generator.test.ts +1 -0
  175. package/test/unit/greenfield.test.ts +1 -0
  176. package/test/unit/interaction/human-review-trigger.test.ts +1 -0
  177. package/test/unit/interaction-network-failures.test.ts +1 -0
  178. package/test/unit/interaction-plugins.test.ts +1 -0
  179. package/test/unit/logging/formatter.test.ts +1 -0
  180. package/test/unit/merge.test.ts +1 -0
  181. package/test/unit/pipeline/event-bus.test.ts +105 -0
  182. package/test/unit/pipeline/routing-partial-override.test.ts +1 -0
  183. package/test/unit/pipeline/runner-retry.test.ts +89 -0
  184. package/test/unit/pipeline/stages/autofix.test.ts +97 -0
  185. package/test/unit/pipeline/stages/rectify.test.ts +101 -0
  186. package/test/unit/pipeline/stages/regression-stage.test.ts +69 -0
  187. package/test/unit/pipeline/stages/verify.test.ts +1 -0
  188. package/test/unit/pipeline/subscribers/hooks.test.ts +45 -0
  189. package/test/unit/pipeline/subscribers/interaction.test.ts +31 -0
  190. package/test/unit/pipeline/subscribers/reporters.test.ts +90 -0
  191. package/test/unit/pipeline/verify-smart-runner.test.ts +2 -1
  192. package/test/unit/prd-auto-default.test.ts +3 -2
  193. package/test/unit/prd-failure-category.test.ts +1 -0
  194. package/test/unit/prd-get-next-story.test.ts +1 -0
  195. package/test/unit/precheck-checks.test.ts +1 -0
  196. package/test/unit/precheck-story-size-gate.test.ts +1 -0
  197. package/test/unit/precheck-types.test.ts +1 -0
  198. package/test/unit/prompts.test.ts +1 -0
  199. package/test/unit/rectification.test.ts +2 -1
  200. package/test/unit/registry.test.ts +1 -0
  201. package/test/unit/routing/routing-stability.test.ts +2 -1
  202. package/test/unit/routing/strategies/llm.test.ts +251 -0
  203. package/test/unit/routing-advanced.test.ts +313 -0
  204. package/test/unit/routing-core.test.ts +341 -0
  205. package/test/unit/routing-strategies.test.ts +442 -0
  206. package/test/unit/storyid-events.test.ts +1 -0
  207. package/test/{ui → unit/ui}/tui-controls.test.ts +8 -7
  208. package/test/{ui → unit/ui}/tui-cost-and-pty.test.ts +4 -3
  209. package/test/{ui → unit/ui}/tui-layout.test.ts +5 -4
  210. package/test/{ui → unit/ui}/tui-stories.test.ts +5 -4
  211. package/test/unit/{isolation.test.ts → unit-isolation.test.ts} +1 -0
  212. package/test/unit/{helpers.test.ts → utils-helpers.test.ts} +1 -0
  213. package/test/unit/verdict.test.ts +1 -0
  214. package/test/unit/verification/orchestrator-types.test.ts +54 -0
  215. package/test/unit/verification/orchestrator.test.ts +66 -0
  216. package/test/unit/verification/smart-runner-config.test.ts +1 -0
  217. package/test/unit/verification/smart-runner-discovery.test.ts +8 -7
  218. package/test/unit/verification/strategies/acceptance.test.ts +33 -0
  219. package/test/unit/verification/strategies/regression.test.ts +87 -0
  220. package/test/unit/verification/strategies/scoped.test.ts +100 -0
  221. package/test/unit/worktree-manager.test.ts +1 -0
  222. package/src/execution/lifecycle/story-hooks.ts +0 -38
  223. package/src/execution/post-verify.ts +0 -193
  224. package/src/execution/rectification.ts +0 -13
  225. package/src/execution/verification.ts +0 -72
  226. package/test/integration/rectification-flow.test.ts +0 -512
  227. package/test/integration/runner.test.ts +0 -1679
  228. package/test/integration/tdd-orchestrator.test.ts +0 -1762
  229. package/test/unit/execution/post-verify-regression.test.ts +0 -362
  230. package/test/unit/execution/post-verify.test.ts +0 -236
  231. package/test/unit/routing.test.ts +0 -1039
  232. /package/test/{integration → helpers}/helpers.test.ts +0 -0
  233. /package/test/integration/worktree/{merge.test.ts → worktree-merge.test.ts} +0 -0
@@ -0,0 +1,464 @@
1
+ # ADR-005: Pipeline Re-Architecture
2
+
3
+ **Status:** Proposed
4
+ **Date:** 2026-03-06
5
+ **Author:** William Khoo, Nax Dev
6
+
7
+ ---
8
+
9
+ ## Context
10
+
11
+ The current pipeline covers only part of the story lifecycle. Verification, rectification, regression gating, escalation, hooks, plugin reporters, and interaction triggers all happen outside the pipeline with ad-hoc orchestration scattered across `sequential-executor.ts`, `pipeline-result-handler.ts`, `post-verify.ts`, `run-regression.ts`, `tier-escalation.ts`, and `tier-outcome.ts`.
12
+
13
+ ### Current Architecture Problems
14
+
15
+ | # | Problem | Where |
16
+ |---|---------|-------|
17
+ | 1 | **Verification in 3+ places** | Pipeline `verify` (scoped), `post-verify.ts` (regression), `run-regression.ts` (deferred), `tdd/verdict.ts` (TDD internal) |
18
+ | 2 | **Post-verify outside pipeline** | `runPostAgentVerification()` called in `handlePipelineSuccess()` — not a stage, no events/logging |
19
+ | 3 | **Routing happens twice** | `routeTask()` in sequential-executor AND `routing` stage inside pipeline |
20
+ | 4 | **Acceptance in 2 places** | Pipeline `acceptance` stage + `acceptanceLoop()` after all stories |
21
+ | 5 | **Pipeline "success" but post-verify fails** | Pipeline returns success, then regression gate fails — misleading |
22
+ | 6 | **Escalation outside pipeline** | `handleTierEscalation()` in result handler + 7 `fireHook` calls in escalation/ |
23
+ | 7 | **No auto-fix** | Lint/typecheck fail → full tier escalation (wasteful) |
24
+ | 8 | **Hooks scattered everywhere** | 20+ `fireHook()` calls across 8 files — no central hook orchestration |
25
+ | 9 | **Plugin reporters scattered** | `getReporters()` called in 5+ places with inconsistent event data |
26
+ | 10 | **Interaction triggers ad-hoc** | `executeTrigger()` called from `pipeline-result-handler.ts`, `precheck-runner.ts` — triggers not tied to stage lifecycle |
27
+ | 11 | **Inconsistent failure reporting** | Verify logs `{exitCode: "TEST_FAILURE"}` (no counts); TDD logs `{remainingFailures: 6}` (has counts) |
28
+
29
+ ### Current Flow (showing all orchestration)
30
+
31
+ ```
32
+ sequential-executor.ts (main loop)
33
+ for each iteration:
34
+ 1. getNextStory()
35
+ 2. preIterationTierCheck()
36
+ 3. routeTask() ← DUPLICATE of pipeline routing stage
37
+ 4. fireHook("on-story-start") ← hook outside pipeline
38
+ 5. runPipeline(defaultPipeline):
39
+ queue-check → routing → constitution → context → prompt → optimizer
40
+ → execution → verify → review → completion → acceptance
41
+ (completion stage fires "on-story-complete" hook)
42
+ 6. if success → handlePipelineSuccess()
43
+ → runPostAgentVerification() ← regression OUTSIDE pipeline
44
+ → runRectificationLoop() ← rectification OUTSIDE pipeline
45
+ → pluginRegistry.getReporters() ← reporters OUTSIDE pipeline
46
+ 7. if fail → handlePipelineFailure()
47
+ → handleTierEscalation() ← escalation OUTSIDE pipeline
48
+ → fireHook("on-story-fail") ← hook OUTSIDE pipeline
49
+ → fireHook("on-pause") ← hook OUTSIDE pipeline
50
+ → executeTrigger("human-review") ← interaction OUTSIDE pipeline
51
+ 8. fireHook("on-session-end") ← hook OUTSIDE pipeline
52
+ after all stories:
53
+ 9. run-regression.ts ← deferred regression OUTSIDE pipeline
54
+ 10. acceptanceLoop() ← acceptance OUTSIDE pipeline
55
+ 11. fireHook("on-complete") ← hook OUTSIDE pipeline
56
+ 12. pluginRegistry.teardownAll() ← cleanup OUTSIDE pipeline
57
+ ```
58
+
59
+ **The pipeline only orchestrates steps 5. Everything else is ad-hoc.**
60
+
61
+ ---
62
+
63
+ ## Decision
64
+
65
+ ### Principle: Pipeline as Single Source of Truth
66
+
67
+ Everything that happens to a story goes through the pipeline. Hooks, plugins, interaction triggers, and verification all fire from well-defined stage boundaries — not from scattered call sites.
68
+
69
+ ### Architecture Overview
70
+
71
+ ```
72
+ ┌──────────────────────────────┐
73
+ │ Pipeline Runner │
74
+ │ (stages + event bus) │
75
+ └──────┬───────────────────────┘
76
+
77
+ ┌─────────────┼──────────────┐
78
+ │ │ │
79
+ ┌────────▼───┐ ┌─────▼─────┐ ┌─────▼──────┐
80
+ │ Stages │ │ Hooks │ │ Plugins │
81
+ │ (ordered) │ │ (events) │ │ (events) │
82
+ └────────────┘ └───────────┘ └────────────┘
83
+ ```
84
+
85
+ **Event Bus** — stages emit typed events. Hooks, reporters, and interaction triggers all subscribe to events instead of being called directly.
86
+
87
+ ### Pipeline Event Bus
88
+
89
+ ```ts
90
+ interface PipelineEventBus {
91
+ // Stage lifecycle (existing, enhanced)
92
+ on(event: "stage:enter", handler: (stage: string, ctx: PipelineContext) => void): void
93
+ on(event: "stage:exit", handler: (stage: string, result: StageResult) => void): void
94
+
95
+ // Story lifecycle (replaces scattered fireHook calls)
96
+ on(event: "story:start", handler: (story: UserStory, ctx: PipelineContext) => void): void
97
+ on(event: "story:complete", handler: (story: UserStory, metrics: StoryMetrics) => void): void
98
+ on(event: "story:fail", handler: (story: UserStory, reason: string) => void): void
99
+ on(event: "story:skip", handler: (story: UserStory, reason: string) => void): void
100
+
101
+ // Verification events (new)
102
+ on(event: "verify:start", handler: (strategy: string, storyId: string) => void): void
103
+ on(event: "verify:result", handler: (result: VerifyResult) => void): void
104
+ on(event: "rectify:attempt", handler: (attempt: number, maxAttempts: number, failures: number) => void): void
105
+
106
+ // Review events (new)
107
+ on(event: "review:start", handler: (checks: string[]) => void): void
108
+ on(event: "review:result", handler: (result: ReviewResult) => void): void
109
+ on(event: "autofix:attempt", handler: (fixType: string, command: string) => void): void
110
+
111
+ // Run lifecycle (replaces runner-level hooks)
112
+ on(event: "run:start", handler: (feature: string, totalStories: number) => void): void
113
+ on(event: "run:complete", handler: (summary: RunSummary) => void): void
114
+ on(event: "run:error", handler: (error: Error) => void): void
115
+ on(event: "run:pause", handler: (reason: string) => void): void
116
+
117
+ // Escalation events (new — replaces ad-hoc escalation hooks)
118
+ on(event: "escalation:tier-change", handler: (from: string, to: string, storyId: string) => void): void
119
+ on(event: "escalation:exhausted", handler: (storyId: string, attempts: number) => void): void
120
+
121
+ // Interaction events (new — replaces ad-hoc trigger calls)
122
+ on(event: "interaction:request", handler: (trigger: TriggerName, request: InteractionRequest) => void): void
123
+ on(event: "interaction:response", handler: (trigger: TriggerName, response: InteractionResponse) => void): void
124
+ }
125
+ ```
126
+
127
+ ### Subscribers
128
+
129
+ Each cross-cutting concern subscribes to events once at startup:
130
+
131
+ ```ts
132
+ // Hooks subscriber — replaces 20+ scattered fireHook() calls
133
+ function wireHooks(bus: PipelineEventBus, hooks: HooksConfig, workdir: string) {
134
+ bus.on("story:start", (story) => fireHook(hooks, "on-story-start", { storyId: story.id }, workdir))
135
+ bus.on("story:complete", (story) => fireHook(hooks, "on-story-complete", { storyId: story.id }, workdir))
136
+ bus.on("story:fail", (story, reason) => fireHook(hooks, "on-story-fail", { storyId: story.id, reason }, workdir))
137
+ bus.on("run:start", () => fireHook(hooks, "on-start", {}, workdir))
138
+ bus.on("run:complete", () => fireHook(hooks, "on-complete", {}, workdir))
139
+ bus.on("run:pause", (reason) => fireHook(hooks, "on-pause", { reason }, workdir))
140
+ bus.on("run:error", (error) => fireHook(hooks, "on-error", { reason: error.message }, workdir))
141
+ }
142
+
143
+ // Reporter subscriber — replaces 5+ scattered getReporters() calls
144
+ function wireReporters(bus: PipelineEventBus, registry: PluginRegistry) {
145
+ const reporters = registry.getReporters()
146
+ bus.on("run:start", (feature, total) => {
147
+ for (const r of reporters) r.onRunStart?.({ runId, feature, totalStories: total, startTime: new Date().toISOString() })
148
+ })
149
+ bus.on("story:complete", (story, metrics) => {
150
+ for (const r of reporters) r.onStoryComplete?.({ runId, storyId: story.id, status: "completed", ...metrics })
151
+ })
152
+ bus.on("run:complete", (summary) => {
153
+ for (const r of reporters) r.onRunEnd?.({ runId, ...summary })
154
+ })
155
+ }
156
+
157
+ // Interaction subscriber — replaces ad-hoc executeTrigger() calls
158
+ function wireInteraction(bus: PipelineEventBus, chain: InteractionChain, config: NaxConfig) {
159
+ bus.on("escalation:exhausted", async (storyId, attempts) => {
160
+ if (isTriggerEnabled("human-review", config)) {
161
+ await executeTrigger("human-review", { featureName, storyId }, config, chain)
162
+ }
163
+ })
164
+ bus.on("verify:result", async (result) => {
165
+ if (!result.success && isTriggerEnabled("review-gate", config)) {
166
+ await executeTrigger("review-gate", { featureName, storyId: result.storyId }, config, chain)
167
+ }
168
+ })
169
+ // ... other triggers wired to events
170
+ }
171
+ ```
172
+
173
+ ### New Pipeline Stage Sequence
174
+
175
+ ```
176
+ # Stage What it does Action on fail
177
+ --- ------------------ ------------------------------------------ ------------------
178
+ 1 queue-check Pause/abort/skip pause/skip
179
+ 2 routing Classify + select tier + strategy continue
180
+ 3 constitution Load coding standards continue
181
+ 4 context Gather relevant code (+ plugin providers) continue
182
+ 5 prompt Assemble prompt continue
183
+ 6 optimizer Reduce tokens (+ plugin optimizers) continue
184
+ 7 execution Agent session (TDD or test-after) escalate
185
+ 8 verify Scoped tests (smart-runner) -> rectify
186
+ 9 rectify Fix test failures (retry loop) escalate
187
+ 10 review Typecheck + lint (+ plugin reviewers) -> autofix
188
+ 11 autofix lintFix / formatFix / short agent fix escalate
189
+ 12 regression Full-suite regression gate (if inline) -> rectify
190
+ 13 completion Mark done, emit story:complete continue
191
+ ```
192
+
193
+ **Post-run pipeline** (after all stories):
194
+
195
+ ```
196
+ 1 deferred-regression Full suite if mode=deferred rectify or fail
197
+ 2 acceptance AC tests + fix story generation fail
198
+ ```
199
+
200
+ ### Plugin Integration Points (Consolidated)
201
+
202
+ Plugins currently integrate at specific stages. This doesn't change, but becomes explicit:
203
+
204
+ | Plugin Type | Stage | How |
205
+ |-------------|-------|-----|
206
+ | `context-provider` | `context` stage | Stage calls `registry.getContextProviders()` |
207
+ | `optimizer` | `optimizer` stage | Stage calls `registry.getOptimizers()` |
208
+ | `router` | `routing` stage | Stage calls `registry.getRouters()` into strategy chain |
209
+ | `reviewer` | `review` stage | Stage calls `registry.getReviewers()` after built-in checks |
210
+ | `reporter` | Event bus subscriber | Wired at startup, receives all lifecycle events |
211
+ | `agent` | `execution` stage | Stage resolves agent from registry |
212
+
213
+ ### New Stage Action: `retry`
214
+
215
+ ```ts
216
+ type StageAction =
217
+ | { action: "continue"; cost?: number }
218
+ | { action: "skip"; reason: string; cost?: number }
219
+ | { action: "fail"; reason: string; cost?: number }
220
+ | { action: "escalate"; reason?: string; cost?: number }
221
+ | { action: "pause"; reason: string; cost?: number }
222
+ | { action: "retry"; fromStage: string; reason: string; cost?: number } // NEW
223
+ ```
224
+
225
+ Pipeline runner handles retry:
226
+
227
+ ```ts
228
+ case "retry":
229
+ const targetIdx = stages.findIndex(s => s.name === result.fromStage);
230
+ i = targetIdx - 1;
231
+ retryCount++;
232
+ if (retryCount > MAX_STAGE_RETRIES) {
233
+ return { success: false, finalAction: "fail", reason: "Max stage retries exceeded" };
234
+ }
235
+ continue;
236
+ ```
237
+
238
+ ### Verification Orchestrator
239
+
240
+ Single orchestrator for ALL test-running:
241
+
242
+ ```
243
+ src/verification/
244
+ orchestrator.ts <- single entry point
245
+ strategies/
246
+ scoped.ts <- smart-runner scoped tests
247
+ regression.ts <- full-suite regression
248
+ acceptance.ts <- acceptance criteria tests
249
+ rectification.ts <- shared retry loop
250
+ executor.ts <- spawn test command (exists)
251
+ parser.ts <- test output parser (exists)
252
+ smart-runner.ts <- test file discovery (exists)
253
+ types.ts <- unified types
254
+ ```
255
+
256
+ Unified result (solves inconsistent failure reporting):
257
+
258
+ ```ts
259
+ interface VerifyResult {
260
+ success: boolean
261
+ status: 'PASS' | 'TEST_FAILURE' | 'TIMEOUT' | 'BUILD_ERROR'
262
+ storyId: string
263
+ strategy: 'scoped' | 'regression' | 'deferred-regression' | 'acceptance'
264
+ passCount: number
265
+ failCount: number
266
+ totalCount: number
267
+ failures: StructuredTestFailure[]
268
+ rawOutput: string
269
+ durationMs: number
270
+ }
271
+ ```
272
+
273
+ ### Review Orchestrator with Auto-Fix
274
+
275
+ ```
276
+ src/review/
277
+ orchestrator.ts <- single entry: typecheck + lint + plugin reviewers + auto-fix
278
+ runner.ts <- check execution (exists)
279
+ types.ts <- (exists)
280
+ ```
281
+
282
+ Language-agnostic auto-fix config:
283
+
284
+ ```ts
285
+ quality: {
286
+ commands: {
287
+ test: "bun test", // existing
288
+ lint: "biome check", // existing
289
+ typecheck: "tsc --noEmit", // existing
290
+ lintFix: "biome check --fix", // NEW
291
+ formatFix: "biome format --write", // NEW
292
+ },
293
+ autofix: {
294
+ enabled: true, // NEW: master switch
295
+ maxAttempts: 2, // NEW: max auto-fix retries
296
+ }
297
+ }
298
+ ```
299
+
300
+ ### Interaction Integration (Structured)
301
+
302
+ Interaction triggers fire from specific pipeline events instead of ad-hoc call sites:
303
+
304
+ | Trigger | Fires on event | Current location (ad-hoc) |
305
+ |---------|---------------|--------------------------|
306
+ | `human-review` | `escalation:exhausted` | `pipeline-result-handler.ts:222` |
307
+ | `cost-exceeded` | `run:cost-check` (new) | manually in sequential-executor |
308
+ | `cost-warning` | `run:cost-check` (new) | manually in sequential-executor |
309
+ | `security-review` | `review:result` (if security plugin fails) | not yet wired |
310
+ | `merge-conflict` | `verify:result` (if git conflict detected) | not yet wired |
311
+ | `max-retries` | `escalation:exhausted` | `pipeline-result-handler.ts` |
312
+ | `pre-merge` | `run:complete` (before merge) | not yet wired |
313
+ | `story-ambiguity` | `story:start` (if ambiguity detected) | not yet wired |
314
+ | `review-gate` | `review:result` (if enabled) | not yet wired |
315
+
316
+ ### Hooks Integration (Structured)
317
+
318
+ All hooks fire from events — zero direct `fireHook()` calls in stages or executor:
319
+
320
+ | Hook Event | Pipeline Event | Current call sites (to remove) |
321
+ |------------|---------------|-------------------------------|
322
+ | `on-start` | `run:start` | `run-setup.ts:182` |
323
+ | `on-story-start` | `story:start` | `sequential-executor.ts:120,204` |
324
+ | `on-story-complete` | `story:complete` | `completion.ts:73`, `pipeline-result-handler.ts:160` |
325
+ | `on-story-fail` | `story:fail` | `tier-outcome.ts:46,73,117,145` |
326
+ | `on-pause` | `run:pause` | `sequential-executor.ts:229,365`, `tier-escalation.ts:150` |
327
+ | `on-resume` | `run:resume` | (manual, stays) |
328
+ | `on-session-end` | `run:session-end` | `sequential-executor.ts` |
329
+ | `on-complete` | `run:complete` | `parallel-executor.ts:161` |
330
+ | `on-error` | `run:error` | (various catch blocks) |
331
+
332
+ ---
333
+
334
+ ## Migration Plan
335
+
336
+ ### Phase 1: Event Bus + Verification Orchestrator
337
+ - Create `PipelineEventBus` (extends existing `PipelineEventEmitter`)
338
+ - Create `VerificationOrchestrator` with unified `VerifyResult`
339
+ - Wire `verify` stage to use orchestrator
340
+ - Wire `review` stage to use orchestrator
341
+ - All existing tests must pass
342
+
343
+ ### Phase 2: New Stages
344
+ - Add `rectify` stage (extract from `post-verify-rectification.ts`)
345
+ - Add `autofix` stage (new)
346
+ - Add `regression` stage (extract from `post-verify.ts`)
347
+ - Add `retry` action to pipeline runner
348
+ - Remove `acceptance` from per-story pipeline; create post-run pipeline
349
+
350
+ ### Phase 3: Hook/Plugin/Interaction Consolidation
351
+ - Wire hooks subscriber to event bus
352
+ - Wire reporter subscriber to event bus
353
+ - Wire interaction subscriber to event bus
354
+ - Remove all direct `fireHook()` calls from stages and executors
355
+ - Remove all direct `getReporters()` calls from handlers
356
+ - Remove all direct `executeTrigger()` calls from handlers
357
+
358
+ ### Phase 4: Simplify Executor
359
+ - Remove `routeTask()` from sequential-executor (routing stage handles it)
360
+ - Remove `handlePipelineSuccess()`/`handlePipelineFailure()` — pipeline handles everything
361
+ - Remove `post-verify.ts`, `post-verify-rectification.ts`
362
+ - Remove deprecated shims (`execution/verification.ts`, `execution/rectification.ts`)
363
+ - Simplify `pipeline-result-handler.ts` to thin success/fail routing
364
+
365
+ ### Files to Delete (after full migration)
366
+
367
+ | File | Absorbed into |
368
+ |------|--------------|
369
+ | `src/execution/post-verify.ts` | `regression` stage |
370
+ | `src/execution/post-verify-rectification.ts` | `rectify` stage |
371
+ | `src/execution/verification.ts` | Deprecated shim → gone |
372
+ | `src/execution/rectification.ts` | Deprecated shim → gone |
373
+ | `src/verification/gate.ts` | `verification/orchestrator.ts` |
374
+ | `src/execution/escalation/tier-outcome.ts` | Event bus hooks subscriber |
375
+
376
+ ### New Files
377
+
378
+ | File | Purpose |
379
+ |------|--------|
380
+ | `src/pipeline/event-bus.ts` | Typed event bus for pipeline lifecycle |
381
+ | `src/pipeline/subscribers/hooks.ts` | Wires hooks to event bus |
382
+ | `src/pipeline/subscribers/reporters.ts` | Wires plugin reporters to event bus |
383
+ | `src/pipeline/subscribers/interaction.ts` | Wires interaction triggers to event bus |
384
+ | `src/pipeline/stages/rectify.ts` | Rectification stage |
385
+ | `src/pipeline/stages/autofix.ts` | Auto-fix stage |
386
+ | `src/pipeline/stages/regression.ts` | Regression gate stage |
387
+ | `src/verification/orchestrator.ts` | Unified verification entry point |
388
+ | `src/verification/strategies/scoped.ts` | Smart-runner scoped tests |
389
+ | `src/verification/strategies/regression.ts` | Full-suite regression |
390
+ | `src/verification/strategies/acceptance.ts` | Acceptance criteria tests |
391
+ | `src/review/orchestrator.ts` | Review + auto-fix orchestration |
392
+
393
+ ---
394
+
395
+ ## Simplified Executor (After Migration)
396
+
397
+ ```ts
398
+ // sequential-executor.ts — reduced to ~80 lines
399
+ async function executeSequential(ctx) {
400
+ const bus = createEventBus()
401
+ wireHooks(bus, ctx.hooks, ctx.workdir)
402
+ wireReporters(bus, ctx.pluginRegistry)
403
+ wireInteraction(bus, ctx.interactionChain, ctx.config)
404
+
405
+ bus.emit("run:start", ctx.feature, ctx.storiesToExecute.length)
406
+
407
+ for (let i = 0; i < ctx.config.execution.maxIterations; i++) {
408
+ const story = getNextStory(prd)
409
+ if (!story) break
410
+
411
+ bus.emit("story:start", story, pipelineContext)
412
+
413
+ const result = await runPipeline(defaultPipeline, pipelineContext, bus)
414
+
415
+ switch (result.finalAction) {
416
+ case "complete":
417
+ bus.emit("story:complete", story, result.context.storyMetrics)
418
+ break
419
+ case "escalate":
420
+ bus.emit("escalation:tier-change", currentTier, nextTier, story.id)
421
+ break
422
+ case "fail":
423
+ bus.emit("story:fail", story, result.reason)
424
+ break
425
+ case "pause":
426
+ bus.emit("run:pause", result.reason)
427
+ return
428
+ case "skip":
429
+ bus.emit("story:skip", story, result.reason)
430
+ break
431
+ }
432
+ }
433
+
434
+ // Post-run: deferred regression + acceptance
435
+ await runPipeline(postRunPipeline, postRunContext, bus)
436
+
437
+ bus.emit("run:complete", buildSummary())
438
+ }
439
+ ```
440
+
441
+ ---
442
+
443
+ ## Consequences
444
+
445
+ ### Positive
446
+ - **Single source of truth** — pipeline orchestrates everything, no ad-hoc code
447
+ - **Consistent logging** — unified `VerifyResult` with counts everywhere
448
+ - **Event-driven cross-cutting** — hooks, reporters, triggers subscribe once, fire from events
449
+ - **Auto-fix** saves costly tier escalations for trivial lint/typecheck failures
450
+ - **Truthful results** — pipeline success = everything passed including regression
451
+ - **Simpler executor** — ~80 lines instead of ~400
452
+ - **Testable** — event bus is mockable; stages are independently testable
453
+ - **Extensible** — new subscribers just wire to events; new stages slot into the pipeline
454
+
455
+ ### Negative
456
+ - Large refactor (4 phases) — high regression risk
457
+ - Event bus adds indirection — debugging hook failures requires tracing events
458
+ - Migration period with old + new coexisting
459
+
460
+ ### Risks & Mitigation
461
+ - **Regression risk:** Incremental phases; each phase must pass full test suite before proceeding
462
+ - **Retry loops:** Hard cap `MAX_STAGE_RETRIES` (default: 5)
463
+ - **Event ordering:** Events fire synchronously within stage boundaries; async subscribers use fire-and-forget with error logging
464
+ - **Backward compat:** Deprecated shims kept until phase 4 cleanup
@@ -0,0 +1,131 @@
1
+ # BUG-039 — Orphan Process Prevention
2
+
3
+ **Status:** Proposal
4
+ **Target:** v0.21.0
5
+ **Author:** Nax Dev
6
+ **Date:** 2026-03-06
7
+
8
+ ---
9
+
10
+ ## 1. Problem
11
+
12
+ When nax aborts a story (timeout, error, SIGTERM), several child processes are left running as orphans:
13
+ - `bun run lint` / `bun run typecheck` (review stage)
14
+ - `git diff`, `git log`, `git rev-parse` (smart runner, utils/git.ts)
15
+ - `claude` CLI (decompose — no timeout at all)
16
+
17
+ These processes accumulate across stories and survive after nax exits, consuming CPU/memory.
18
+
19
+ ---
20
+
21
+ ## 2. Root Causes
22
+
23
+ | Location | Process | Issue | Severity |
24
+ |---|---|---|---|
25
+ | `review/runner.ts:runCheck()` | `lint`, `typecheck` | No timeout, no kill, not in PidRegistry | 🔴 Critical |
26
+ | `agents/claude.ts:decompose()` | `claude` CLI | No timeout set | 🔴 Critical |
27
+ | `utils/git.ts:captureGitRef()` | `git rev-parse` | No timeout | 🟡 Medium |
28
+ | `utils/git.ts:hasCommitsForStory()` | `git log` | No timeout | 🟡 Medium |
29
+ | `verification/smart-runner.ts` | `git diff` | No timeout, not killed on verify abort | 🟡 Medium |
30
+ | `agents/claude.ts:runOnce()` | `claude` CLI | SIGTERM only — no SIGKILL follow-up after grace period | 🟡 Medium |
31
+ | `agents/claude.ts:runOnce()` | `claude` CLI | If timeout throws, `pidRegistry.unregister()` skipped → PID leaks | 🟡 Medium |
32
+ | `routing/strategies/llm.ts` | `claude` CLI | Timeout kills proc but streams not drained → `proc.exited` may hang | 🟡 Medium |
33
+ | `verification/executor.ts:drainWithDeadline()` | internal | `setTimeout` in drain race never cleared | 🟢 Minor |
34
+ | `execution/pid-registry.ts:killPid()` | `kill` binary | Spawns `kill` subprocesses without timeout | 🟢 Minor |
35
+
36
+ **Already correct:** `executor.ts:executeWithTimeout()` — SIGTERM + grace + SIGKILL process group. `crash-recovery.ts` — `pidRegistry.killAll()` on all signals. `pid-registry.ts:cleanupStale()` — kills orphans at startup.
37
+
38
+ ---
39
+
40
+ ## 3. Proposed Fixes
41
+
42
+ ### Fix 1 — Shared `gitWithTimeout()` helper
43
+
44
+ Replace all bare git spawns in `utils/git.ts` and `smart-runner.ts`:
45
+
46
+ ```typescript
47
+ // src/utils/git.ts
48
+ const GIT_TIMEOUT_MS = 10_000;
49
+
50
+ async function gitWithTimeout(args: string[], workdir: string): Promise<{ stdout: string; exitCode: number }> {
51
+ const proc = Bun.spawn(["git", ...args], { cwd: workdir, stdout: "pipe", stderr: "pipe" });
52
+ const timerId = setTimeout(() => proc.kill("SIGKILL"), GIT_TIMEOUT_MS);
53
+ const exitCode = await proc.exited;
54
+ clearTimeout(timerId);
55
+ const stdout = await new Response(proc.stdout).text();
56
+ return { stdout, exitCode };
57
+ }
58
+ ```
59
+
60
+ Apply to: `captureGitRef()`, `hasCommitsForStory()`, `getChangedSourceFiles()` in `smart-runner.ts`.
61
+
62
+ ### Fix 2 — Review `runCheck()` timeout
63
+
64
+ Wrap each check spawn with SIGTERM+SIGKILL pattern. Config: `review.checkTimeoutSeconds` (default: 120).
65
+
66
+ ### Fix 3 — `decompose()` timeout
67
+
68
+ Add `timeoutSeconds` to `DecomposeOptions` (default: 300). Apply same setTimeout → SIGTERM pattern as `runOnce()`.
69
+
70
+ ### Fix 4 — `runOnce()` SIGKILL follow-up + `finally` unregister
71
+
72
+ ```typescript
73
+ setTimeout(() => {
74
+ timedOut = true;
75
+ proc.kill("SIGTERM");
76
+ setTimeout(() => { try { proc.kill("SIGKILL"); } catch {} }, gracePeriodMs);
77
+ }, timeoutMs);
78
+
79
+ // Always unregister, even on exception:
80
+ try {
81
+ const exitCode = await proc.exited;
82
+ clearTimeout(timeoutId);
83
+ } finally {
84
+ await pidRegistry.unregister(processPid);
85
+ }
86
+ ```
87
+
88
+ ### Fix 5 — `llm.ts` stream drain on timeout
89
+
90
+ Cancel streams before killing to prevent `proc.exited` hang:
91
+
92
+ ```typescript
93
+ } catch (err) {
94
+ clearTimeout(timeoutId);
95
+ proc.stdout.cancel().catch(() => {});
96
+ proc.stderr.cancel().catch(() => {});
97
+ proc.kill();
98
+ throw err;
99
+ }
100
+ ```
101
+
102
+ ### Fix 6 — `drainWithDeadline()` timer cleanup
103
+
104
+ Store and clear the setTimeout handle after the race resolves.
105
+
106
+ ---
107
+
108
+ ## 4. Files Affected
109
+
110
+ | File | Change |
111
+ |---|---|
112
+ | `src/utils/git.ts` | Add `gitWithTimeout()` helper; use in all git spawns |
113
+ | `src/verification/smart-runner.ts` | Use `gitWithTimeout()` for `git diff` |
114
+ | `src/review/runner.ts` | Add timeout + SIGTERM/SIGKILL to `runCheck()` |
115
+ | `src/config/schemas.ts` | Add `review.checkTimeoutSeconds` (default: 120) |
116
+ | `src/config/types.ts` | Add `checkTimeoutSeconds` to `ReviewConfig` |
117
+ | `src/agents/claude.ts` | Fix `decompose()` timeout; `runOnce()` SIGKILL + finally |
118
+ | `src/agents/types.ts` | Add `timeoutSeconds` to `DecomposeOptions` |
119
+ | `src/routing/strategies/llm.ts` | Cancel streams before kill on timeout |
120
+ | `src/verification/executor.ts` | `clearTimeout` in `drainWithDeadline()` |
121
+
122
+ ---
123
+
124
+ ## 5. Test Plan
125
+
126
+ - `runCheck()` with hanging command → killed after `checkTimeoutSeconds`
127
+ - `decompose()` with hanging claude → times out, PID unregistered
128
+ - `runOnce()` timeout → SIGKILL after grace period; PID unregistered even on exception
129
+ - `gitWithTimeout()` → returns error after 10s (no hanging promise)
130
+ - `drainWithDeadline()` → no leaked setTimeout (verify with fake timers)
131
+ - `llm.ts` timeout → `proc.exited` resolves after kill (no hang)
@@ -0,0 +1,82 @@
1
+ # BUG-040 — Review Rectification Loop
2
+
3
+ **Status:** Proposal
4
+ **Target:** v0.21.0
5
+ **Author:** Nax Dev
6
+ **Date:** 2026-03-06
7
+
8
+ ---
9
+
10
+ ## 1. Problem
11
+
12
+ A story that fails lint or typecheck during the review stage is **permanently killed** — no retry, no agent fix, immediate escalation. This wastes an entire escalation slot on a trivial auto-fixable error.
13
+
14
+ **Example:** Agent implements story correctly. All tests pass (verify ✅). Review runs `biome check` → 3 lint errors. Review returns `{ action: "fail" }` → story marked failed → escalates to `balanced` → `balanced` agent re-implements from scratch → also gets lint errors → `powerful` tier used. All for a `biome --fix` that takes 2 seconds.
15
+
16
+ ---
17
+
18
+ ## 2. Root Cause
19
+
20
+ ```
21
+ review.ts:execute() → runReview() → { success: false }
22
+ → return { action: "fail" } ← always "fail" for any review failure
23
+
24
+ pipeline-result-handler.ts
25
+ case "fail": markStoryFailed() ← permanent, no retry
26
+ ```
27
+
28
+ The verify stage correctly returns `"escalate"` on test failure, enabling rectification. Review has no equivalent.
29
+
30
+ ---
31
+
32
+ ## 3. Proposed Architecture
33
+
34
+ ```
35
+ review.ts → runReview() fails
36
+ → return { action: "review-rectify", output } ← NEW
37
+
38
+ pipeline-result-handler.ts
39
+ case "review-rectify":
40
+ → runReviewRectification(story, reviewOutput, config, workdir)
41
+ → agent: "Fix these lint/typecheck errors:\n<output>"
42
+ → re-run review
43
+ → pass? → "continue"
44
+ → fail? → "escalate" (if attempts exhausted)
45
+ ```
46
+
47
+ ---
48
+
49
+ ## 4. Files Affected
50
+
51
+ | File | Change |
52
+ |---|---|
53
+ | `src/pipeline/stages/review.ts` | Return `{ action: "review-rectify", output }` instead of `"fail"` |
54
+ | `src/pipeline/types.ts` | Add `"review-rectify"` to `StageAction` union |
55
+ | `src/execution/pipeline-result-handler.ts` | Handle `"review-rectify"` → call `runReviewRectification()` |
56
+ | `src/execution/review-rectification.ts` | **New:** `runReviewRectification()` loop |
57
+ | `src/config/schemas.ts` | Add `review.maxRectificationAttempts` (default: 1) |
58
+ | `src/config/types.ts` | Add `maxRectificationAttempts` to `ReviewConfig` |
59
+
60
+ ---
61
+
62
+ ## 5. Config Changes
63
+
64
+ ```jsonc
65
+ {
66
+ "review": {
67
+ "enabled": true,
68
+ "checks": ["typecheck", "lint"],
69
+ "maxRectificationAttempts": 1 // 0 = revert to old "fail" behavior
70
+ }
71
+ }
72
+ ```
73
+
74
+ ---
75
+
76
+ ## 6. Test Plan
77
+
78
+ - Lint failure → `runReviewRectification()` called with lint output
79
+ - Rectification passes → story continues (not escalated)
80
+ - Rectification fails → story escalates (not permanently failed)
81
+ - `maxRectificationAttempts: 0` → old behavior (immediate escalate)
82
+ - Plugin reviewer rejection → still `"fail"` (not routed through rectification)