cleargate 0.10.0 → 0.11.1

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 (72) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +11 -1
  3. package/dist/MANIFEST.json +40 -26
  4. package/dist/chunk-HZPJ5QX4.js +459 -0
  5. package/dist/chunk-HZPJ5QX4.js.map +1 -0
  6. package/dist/cli.cjs +421 -204
  7. package/dist/cli.cjs.map +1 -1
  8. package/dist/cli.js +389 -515
  9. package/dist/cli.js.map +1 -1
  10. package/dist/lib/lifecycle-reconcile.cjs +497 -0
  11. package/dist/lib/lifecycle-reconcile.cjs.map +1 -0
  12. package/dist/lib/lifecycle-reconcile.d.cts +136 -0
  13. package/dist/lib/lifecycle-reconcile.d.ts +136 -0
  14. package/dist/lib/lifecycle-reconcile.js +20 -0
  15. package/dist/lib/lifecycle-reconcile.js.map +1 -0
  16. package/dist/templates/cleargate-planning/.claude/agents/architect.md +55 -2
  17. package/dist/templates/cleargate-planning/.claude/agents/developer.md +22 -0
  18. package/dist/templates/cleargate-planning/.claude/agents/devops.md +249 -0
  19. package/dist/templates/cleargate-planning/.claude/agents/qa.md +41 -0
  20. package/dist/templates/cleargate-planning/.claude/agents/reporter.md +44 -8
  21. package/dist/templates/cleargate-planning/.claude/hooks/pre-commit-surface-gate.sh +21 -0
  22. package/dist/templates/cleargate-planning/.claude/hooks/stamp-and-gate.sh +12 -1
  23. package/dist/templates/cleargate-planning/.claude/hooks/token-ledger.sh +21 -1
  24. package/dist/templates/cleargate-planning/.claude/skills/sprint-execution/SKILL.md +200 -29
  25. package/dist/templates/cleargate-planning/.cleargate/knowledge/mid-sprint-triage-rubric.md +160 -0
  26. package/dist/templates/cleargate-planning/.cleargate/knowledge/readiness-gates.md +41 -9
  27. package/dist/templates/cleargate-planning/.cleargate/scripts/close_sprint.mjs +98 -16
  28. package/dist/templates/cleargate-planning/.cleargate/scripts/gate-checks.json +3 -3
  29. package/dist/templates/cleargate-planning/.cleargate/scripts/init_sprint.mjs +86 -10
  30. package/dist/templates/cleargate-planning/.cleargate/scripts/run_script.sh +173 -87
  31. package/dist/templates/cleargate-planning/.cleargate/scripts/suggest_improvements.mjs +150 -22
  32. package/dist/templates/cleargate-planning/.cleargate/scripts/test/test_flashcard_gate.sh +20 -20
  33. package/dist/templates/cleargate-planning/.cleargate/scripts/validate_state.mjs +32 -8
  34. package/dist/templates/cleargate-planning/.cleargate/scripts/write_dispatch.sh +12 -1
  35. package/dist/templates/cleargate-planning/.cleargate/templates/Bug.md +3 -0
  36. package/dist/templates/cleargate-planning/.cleargate/templates/CR.md +3 -0
  37. package/dist/templates/cleargate-planning/.cleargate/templates/epic.md +3 -0
  38. package/dist/templates/cleargate-planning/.cleargate/templates/hotfix.md +3 -0
  39. package/dist/templates/cleargate-planning/.cleargate/templates/initiative.md +1 -1
  40. package/dist/templates/cleargate-planning/.cleargate/templates/sprint_context.md +8 -0
  41. package/dist/templates/cleargate-planning/.cleargate/templates/story.md +3 -0
  42. package/dist/templates/cleargate-planning/CLAUDE.md +3 -1
  43. package/dist/templates/cleargate-planning/MANIFEST.json +40 -26
  44. package/package.json +8 -5
  45. package/templates/cleargate-planning/.claude/agents/architect.md +55 -2
  46. package/templates/cleargate-planning/.claude/agents/developer.md +22 -0
  47. package/templates/cleargate-planning/.claude/agents/devops.md +249 -0
  48. package/templates/cleargate-planning/.claude/agents/qa.md +41 -0
  49. package/templates/cleargate-planning/.claude/agents/reporter.md +44 -8
  50. package/templates/cleargate-planning/.claude/hooks/pre-commit-surface-gate.sh +21 -0
  51. package/templates/cleargate-planning/.claude/hooks/stamp-and-gate.sh +12 -1
  52. package/templates/cleargate-planning/.claude/hooks/token-ledger.sh +21 -1
  53. package/templates/cleargate-planning/.claude/skills/sprint-execution/SKILL.md +200 -29
  54. package/templates/cleargate-planning/.cleargate/knowledge/mid-sprint-triage-rubric.md +160 -0
  55. package/templates/cleargate-planning/.cleargate/knowledge/readiness-gates.md +41 -9
  56. package/templates/cleargate-planning/.cleargate/scripts/close_sprint.mjs +98 -16
  57. package/templates/cleargate-planning/.cleargate/scripts/gate-checks.json +3 -3
  58. package/templates/cleargate-planning/.cleargate/scripts/init_sprint.mjs +86 -10
  59. package/templates/cleargate-planning/.cleargate/scripts/run_script.sh +173 -87
  60. package/templates/cleargate-planning/.cleargate/scripts/suggest_improvements.mjs +150 -22
  61. package/templates/cleargate-planning/.cleargate/scripts/test/test_flashcard_gate.sh +20 -20
  62. package/templates/cleargate-planning/.cleargate/scripts/validate_state.mjs +32 -8
  63. package/templates/cleargate-planning/.cleargate/scripts/write_dispatch.sh +12 -1
  64. package/templates/cleargate-planning/.cleargate/templates/Bug.md +3 -0
  65. package/templates/cleargate-planning/.cleargate/templates/CR.md +3 -0
  66. package/templates/cleargate-planning/.cleargate/templates/epic.md +3 -0
  67. package/templates/cleargate-planning/.cleargate/templates/hotfix.md +3 -0
  68. package/templates/cleargate-planning/.cleargate/templates/initiative.md +1 -1
  69. package/templates/cleargate-planning/.cleargate/templates/sprint_context.md +8 -0
  70. package/templates/cleargate-planning/.cleargate/templates/story.md +3 -0
  71. package/templates/cleargate-planning/CLAUDE.md +3 -1
  72. package/templates/cleargate-planning/MANIFEST.json +40 -26
@@ -1,8 +1,8 @@
1
1
  ---
2
2
  name: sprint-execution
3
3
  description: |
4
- Use to orchestrate the four-agent sprint loop end-to-end — Architect → Developer
5
- QA → Reporter — across one ClearGate sprint. Activates from sprint kickoff
4
+ Use to orchestrate the five-dispatch sprint loop end-to-end — Architect → QA-Red
5
+ Developer → QA-Verify → Reporter — across one ClearGate sprint. Activates from sprint kickoff
6
6
  (preflight + cut sprint branch + state init) through per-story execution
7
7
  (worktree → dev → QA → flashcard gate → merge) into walkthrough and Gate-4 close.
8
8
  Triggers: SessionStart banner mentioning an active sprint; explicit user phrases
@@ -40,7 +40,7 @@ Five touchpoints where the goal is the tiebreaker:
40
40
 
41
41
  1. **Kickoff (§A.5).** Surface the sprint goal verbatim in chat before any Architect dispatch. State it as the explicit acceptance condition for the sprint.
42
42
  2. **Architect dispatch (§B).** Pass the sprint goal in the dispatch prompt. The milestone plan should reference how each story advances the goal, not only what files it changes.
43
- 3. **Mid-sprint CR triage (§C.9).** When classifying `CR:scope-change`, evaluate goal alignment before quarantining. If the new requirement is critical to the goal, escalate to the human with "this may need to land THIS sprint, not the next."
43
+ 3. **Mid-sprint CR triage (§C.10 rubric → §C.11 routing).** When classifying `CR:scope-change`, evaluate goal alignment before quarantining. If the new requirement is critical to the goal, escalate to the human with "this may need to land THIS sprint, not the next."
44
44
  4. **Escalation (§8).** When `qa_bounces ≥ 3`, `arch_bounces ≥ 3`, or 3 circuit-breaker hits flip a story to `Escalated`, frame the human-decision question through the goal lens: "Drop this story → goal still met? Re-approach → goal still met by sprint end?"
45
45
  5. **Walkthrough + Reporter brief (§D, §E.2).** Walkthrough invitation leads with the goal, not the feature checklist. Reporter brief MUST include a goal-achievement verdict — `met / partial / missed` — as a first-class signal in the close-gate Brief.
46
46
 
@@ -60,8 +60,11 @@ It is pure framing: surface deviations from the goal as first-class events, not
60
60
  | `architect` | opus | (a) Sprint Design Review pre-confirm, (b) per-milestone plan | `sprint-runs/<id>/plans/M<N>.md` (per milestone); markdown block §2 (design review) |
61
61
  | `developer` | sonnet | One per story, inside its worktree | One commit `feat(<epic>): STORY-NNN-NN <desc>` + `STORY-NNN-NN-dev.md` report |
62
62
  | `qa` | sonnet | After Developer reports `STATUS=done` | `STORY-NNN-NN-qa.md` report (no code edits) |
63
+ | `devops` | sonnet | Per-story, after QA-Verify + Architect post-flight | One merge commit (no-ff) + `STORY-NNN-NN-devops.md` report |
63
64
  | `reporter` | sonnet | Once at sprint close, after all stories merged + walkthrough done | `sprint-runs/<id>/SPRINT-<#>_REPORT.md` |
64
65
 
66
+ **Registration constraint:** The Claude Code agent registry caches at session start. If an agent `.md` file under `.claude/agents/` was added or modified after the current session began, `Agent(subagent_type=<name>)` will return "Agent type '<name>' not found" even though the file exists. Fix: restart the Claude Code session. Verify `devops` reachable at sprint preflight (§A.1 check 6). If unavailable mid-sprint, use the §C.7 DevOps Escape Hatch.
67
+
65
68
  ### Wall-clock budgets
66
69
 
67
70
  Each agent dispatch has a target duration. Note the start time before each `Agent` spawn; after the call returns, compare elapsed against the budget. Ran-long stories get flagged in sprint §4 Execution Log even on success.
@@ -71,6 +74,7 @@ Each agent dispatch has a target duration. Note the start time before each `Agen
71
74
  | `architect` (per milestone) | ≤ 10 min | Plan-only output; long runs usually mean too many stories in the milestone |
72
75
  | `developer` (per story) | ≤ 30 min | Includes typecheck + tests in the worktree; long runs near the circuit-breaker threshold |
73
76
  | `qa` (per story) | ≤ 15 min | Read + re-run gates; should not edit code |
77
+ | `devops` (per story) | ≤ 5 min | Mechanical work only — merge, teardown, state; long runs indicate git/npm issue |
74
78
  | `reporter` (per sprint) | ≤ 20 min | Single file write; long runs mean ledger reconciliation issues |
75
79
 
76
80
  If a Task call has been pending for **>2× the budget** with no visible progress, surface it to the human and offer to interrupt. There is no automatic stall detection — the parent session blocks on `Agent` calls and cannot poll mid-run. The human's interrupt is the only reliable kill path until ambient watcher infra exists.
@@ -84,7 +88,7 @@ bash .cleargate/scripts/write_dispatch.sh <work_item_id> <agent_type>
84
88
  ```
85
89
 
86
90
  - `<work_item_id>`: e.g. `STORY-020-02`, `CR-016`, `BUG-021`. For the Reporter at sprint close use the sprint ID (e.g. `SPRINT-19`).
87
- - `<agent_type>`: exact string — `developer | architect | qa | reporter | cleargate-wiki-contradict`.
91
+ - `<agent_type>`: exact string — `developer | architect | qa | reporter | devops | cleargate-wiki-contradict`.
88
92
 
89
93
  If you forget the marker, ledger attribution falls back to transcript-grep heuristics (unreliable). The hook deletes the file after consumption — write fresh per dispatch.
90
94
 
@@ -113,13 +117,14 @@ Run this sequence exactly once, when the human says "start sprint NN" or equival
113
117
  cleargate sprint preflight <sprint-id>
114
118
  ```
115
119
 
116
- Five checks, all must pass:
120
+ Six checks, all must pass:
117
121
 
118
122
  1. Previous sprint `sprint_status: "Completed"` in `state.json`.
119
123
  2. No leftover worktrees — `git worktree list` must not contain `.worktrees/STORY-*`.
120
124
  3. Sprint branch ref free — `git show-ref refs/heads/sprint/S-NN` returns nothing.
121
125
  4. `main` clean — `git status --porcelain` empty.
122
126
  5. Per-item readiness gates pass — every work-item ID in §1 Consolidated Deliverables has fresh `cached_gate_result.pass: true` (or terminal status). Under `execution_mode: v2` a failing item hard-blocks; under `v1` it warns. Failure punch-list names each item + its failing criteria.
127
+ 6. **Verify `devops` subagent reachable** — attempt `Agent(subagent_type=devops, prompt: "echo preflight-check")` immediately after session start. If it returns "Agent type 'devops' not found", halt and ask the human to restart the Claude Code session; do NOT proceed under assumption the escape hatch will cover every merge. This check is manual (not enforced by `cleargate sprint preflight`); document result in sprint §4 Execution Log.
123
128
 
124
129
  On failure, surface the punch list verbatim and halt. Per-item resolution:
125
130
 
@@ -128,6 +133,7 @@ On failure, surface the punch list verbatim and halt. Per-item resolution:
128
133
  - Branch ref exists → investigate; force-deletion only with explicit human approval.
129
134
  - Dirty main → human commits/stashes/discards. **Never `git reset --hard` or stash without explicit human approval.**
130
135
  - Per-item gate fail → run `cleargate gate check <file> -v` for the named item; fix the failing criterion (e.g., populate `context_source`, resolve TBDs); re-run preflight.
136
+ - Devops unreachable → restart Claude Code session; re-run all 6 preflight checks.
131
137
 
132
138
  ### A.2 Cut sprint branch
133
139
 
@@ -145,6 +151,8 @@ node .cleargate/scripts/init_sprint.mjs SPRINT-NN
145
151
 
146
152
  This writes `.cleargate/sprint-runs/SPRINT-NN/state.json` and flips `.cleargate/sprint-runs/.active` to `SPRINT-NN`. Without `state.json` the lane router, dispatch hook, and close pipeline all fail.
147
153
 
154
+ `init_sprint.mjs` also writes `<sprintDir>/sprint-context.md` from `.cleargate/templates/sprint_context.md`, populated with `sprint_id` + goal (extracted from sprint plan §0 `- **Sprint Goal:** …` bullet, or placeholder if absent) + active CR list. Every Dev/QA/Architect/DevOps dispatch reads this file as preflight (see §B + §C contracts + agent prompts `## Preflight`).
155
+
148
156
  ### A.4 Architect Sprint Design Review (v2 only)
149
157
 
150
158
  Mandatory under `execution_mode: v2`; optional but encouraged under `v1`. Spawn the Architect with all candidate stories' §3 Implementation Guides + ADRs + flashcards + sprint plan path:
@@ -177,13 +185,15 @@ Then `Agent(subagent_type=architect, ...)` with the milestone story IDs and inst
177
185
 
178
186
  **Skip per-milestone Architect for `lane: fast` stories** — they dispatch to Developer without a plan, per the lane contract (see `.claude/agents/architect.md` Lane Classification).
179
187
 
188
+ - Cross-cutting rules: Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md` BEFORE any other action (Sprint Goal + Cross-Cutting Rules + Active CRs sections constrain every decision).
189
+
180
190
  > 🎯 **Goal check.** Pass the sprint goal verbatim in the Architect's dispatch prompt. The plan should explicitly tie each story to the goal under "Per-story blueprint" — e.g. *"STORY-NNN-NN advances goal by <one sentence>"*. Plans that don't reference the goal go back to the Architect with a re-dispatch.
181
191
 
182
192
  ---
183
193
 
184
194
  ## 5. Phase C — Per-Story Execution Loop
185
195
 
186
- Run this loop **per story**, in the order the milestone plan declares (parallel waves vs sequential chains). Each iteration: Worktree → Developer → QA → (Architect pass for `lane: standard` v2 only) → Merge → Flashcard Gate.
196
+ Run this loop **per story**, in the order the milestone plan declares (parallel waves vs sequential chains). Each iteration: Worktree → **QA-Red** → **TPV (Test Pattern Validation, Architect-only)** → Developer → QA-Verify → (Architect pass for `lane: standard` v2 only) → Merge → Flashcard Gate.
187
197
 
188
198
  > **Naming note.** State-machine values (`Bouncing`, `Ready to Bounce`), `state.json` counter fields (`qa_bounces`, `arch_bounces`), and script names (`validate_bounce_readiness.mjs`) retain the legacy "bounce" term because they are code-bound. The narrative in this skill uses "execution loop", "story cycle", and "rework" to describe the same mechanics.
189
199
 
@@ -212,14 +222,72 @@ git worktree list
212
222
 
213
223
  **Do not run `git worktree add` inside `mcp/`.** It is a nested git repo. If the story touches `mcp/`, the Developer edits `mcp/` from inside `.worktrees/STORY-NNN-NN/mcp/...` — visible as a subdirectory of the outer worktree. (`cleargate-enforcement.md` §1.3.)
214
224
 
215
- ### C.3 Spawn Developer
225
+ ### C.3 Spawn QA-Red (standard lane only — fast lane skips this step)
226
+
227
+ ```bash
228
+ bash .cleargate/scripts/write_dispatch.sh STORY-NNN-NN qa
229
+ ```
230
+
231
+ **Script invocation rule (CR-046):** Any bash/node script invoked within the QA-Red dispatch MUST go through the wrapper: `bash .cleargate/scripts/run_script.sh <cmd> [args...]`. Direct invocation without the wrapper is forbidden under v2.
232
+
233
+ Then spawn with `subagent_type=qa`. Dispatch prompt MUST inject:
234
+
235
+ - Cross-cutting rules: Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md` BEFORE any other action.
236
+
237
+ > `Mode: RED — write failing tests against §4 acceptance, no implementation Read access. Tests must fail with "not yet implemented" errors against the clean baseline. File-naming: *.red.node.test.ts (immutable post-Red). Forbidden: editing implementation files.`
238
+
239
+ QA-Red returns:
240
+
241
+ ```
242
+ QA-RED: WRITTEN | BLOCKED
243
+ RED_TESTS: <list of *.red.node.test.ts files written>
244
+ BASELINE_FAIL: <count of failing scenarios>
245
+ flashcards_flagged: [ ... ]
246
+ ```
247
+
248
+ On `QA-RED: BLOCKED`: surface Spec-Gap to human; do not proceed to §C.4 until resolved.
249
+
250
+ On `QA-RED: WRITTEN`: orchestrator commits the Red tests on the story branch with subject `qa-red(STORY-NNN-NN): write failing tests`, then proceeds to §C.3.5 TPV Gate.
251
+
252
+ **Fast lane skip:** if `state.json.stories[<id>].lane === "fast"`, skip this entire step AND §C.3.5 and proceed directly to §C.4 Spawn Developer.
253
+
254
+ ### C.3.5 TPV Gate (Architect-only — standard lane, v2 only)
255
+
256
+ **Skip if:** `state.json.stories[<id>].lane === 'fast'` OR `execution_mode: v1`.
257
+
258
+ After QA-Red commits Red tests, spawn the Architect in `Mode: TPV` to perform wiring-only validation before Dev dispatch. TPV does NOT evaluate test logic correctness — it verifies wiring so Dev does not waste a full dispatch on a mis-wired test harness.
259
+
260
+ ```bash
261
+ bash .cleargate/scripts/write_dispatch.sh STORY-NNN-NN architect
262
+ ```
263
+
264
+ Dispatch prompt MUST inject:
265
+
266
+ > `Mode: TPV — Test Pattern Validation only. You receive: story file, QA-Red commit SHA, list of *.red.node.test.ts files. Verify ONLY: (1) imports resolve to real paths, (2) constructor signatures match, (3) mocked methods exist on the mocked object, (4) after-hooks present when before-hooks write state, (5) file naming *.red.node.test.ts. Do NOT evaluate test logic. Return: TPV: APPROVED (Dev proceeds) or TPV: BLOCKED-WIRING-GAP — <one-sentence specific issue> (orchestrator routes back to QA-Red).`
267
+
268
+ Architect returns one of:
269
+
270
+ - `TPV: APPROVED` — proceed to §C.4 Spawn Developer.
271
+ - `TPV: BLOCKED-WIRING-GAP — <issue>` — orchestrator increments `arch_bounces` via:
272
+ ```bash
273
+ node .cleargate/scripts/update_state.mjs STORY-NNN-NN --arch-bounce
274
+ ```
275
+ Then routes back to §C.3 QA-Red with the gap description in the re-dispatch prompt. If `arch_bounces ≥ 3` → flip story to `Escalated`, halt.
276
+
277
+ TPV is a WIRING gate, not a correctness gate. A test that imports the right module but asserts incorrectly still gets `TPV: APPROVED` — Dev's TDD cycle handles assertion correctness.
278
+
279
+ ### C.4 Spawn Developer
216
280
 
217
281
  ```bash
218
282
  bash .cleargate/scripts/write_dispatch.sh STORY-NNN-NN developer
219
283
  ```
220
284
 
285
+ **Script invocation rule (CR-046):** Any bash/node script invoked within the Developer dispatch MUST go through the wrapper: `bash .cleargate/scripts/run_script.sh <cmd> [args...]`. Direct invocation without the wrapper is forbidden under v2.
286
+
221
287
  Then spawn with `subagent_type=developer`. Inputs the prompt must include verbatim:
222
288
 
289
+ - Cross-cutting rules: Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md` BEFORE any other action.
290
+
223
291
  - `STORY=NNN-NN` (Developer must echo this on its first response line).
224
292
  - Path to story file.
225
293
  - Path to milestone plan.
@@ -238,14 +306,20 @@ FILES_CHANGED: <list>
238
306
  flashcards_flagged: [ ... ]
239
307
  ```
240
308
 
241
- If `STATUS=blocked`: route per §C.7 (Blockers Triage).
309
+ If `STATUS=blocked`: route per §C.8 (Blockers Triage).
242
310
 
243
- ### C.4 Spawn QA
311
+ ### C.5 Spawn QA-Verify
244
312
 
245
313
  ```bash
246
314
  bash .cleargate/scripts/write_dispatch.sh STORY-NNN-NN qa
247
315
  ```
248
316
 
317
+ **Script invocation rule (CR-046):** Any bash/node script invoked within the QA-Verify dispatch MUST go through the wrapper: `bash .cleargate/scripts/run_script.sh <cmd> [args...]`. Direct invocation without the wrapper is forbidden under v2.
318
+
319
+ Dispatch prompt MUST inject: `Mode: VERIFY — read-only acceptance trace. Verify Developer's implementation against the story's §4 acceptance Gherkin. Do not write or modify any files.`
320
+
321
+ - Cross-cutting rules: Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md` BEFORE any other action.
322
+
249
323
  QA inputs: story file path, worktree path, Developer commit SHA. QA returns:
250
324
 
251
325
  ```
@@ -256,14 +330,18 @@ REGRESSIONS: <list>
256
330
  flashcards_flagged: [ ... ]
257
331
  ```
258
332
 
259
- **On `QA: FAIL`:** increment `qa_bounces` via `update_state.mjs STORY-NNN-NN --qa-bounce`. If counter ≥ 3 → flip story to `Escalated`, surface to human, halt. Else → re-spawn Developer with QA's bug report as input. Return to §C.3.
333
+ **On `QA: FAIL`:** increment `qa_bounces` via `update_state.mjs STORY-NNN-NN --qa-bounce`. If counter ≥ 3 → flip story to `Escalated`, surface to human, halt. Else → re-spawn Developer with QA's bug report as input. Return to §C.4.
260
334
 
261
335
  **On `QA: PASS`:** update state to `QA Passed`, proceed.
262
336
 
263
- ### C.5 Architect Pass (v2, `lane: standard` only)
337
+ ### C.6 Architect Pass (v2, `lane: standard` only)
264
338
 
265
339
  `lane: fast` skips this step entirely.
266
340
 
341
+ - Cross-cutting rules: Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md` BEFORE any other action.
342
+
343
+ **Script invocation rule (CR-046):** Any bash/node script invoked within the Architect Pass dispatch MUST go through the wrapper: `bash .cleargate/scripts/run_script.sh <cmd> [args...]`. Direct invocation without the wrapper is forbidden under v2.
344
+
267
345
  ```bash
268
346
  bash .cleargate/scripts/pre_gate_runner.sh arch .worktrees/STORY-NNN-NN/ sprint/S-NN
269
347
  ```
@@ -272,24 +350,75 @@ If pre-gate scan reveals new dependencies / structural issues → return to Deve
272
350
 
273
351
  If pre-gate passes, spawn Architect for post-flight review. On `FAIL`: increment `arch_bounces`. ≥ 3 → escalate.
274
352
 
275
- ### C.6 Story Merge
353
+ ### C.7 Story Merge
276
354
 
277
- ```bash
278
- git checkout sprint/S-NN
279
- git merge story/STORY-NNN-NN --no-ff -m "merge(story/STORY-NNN-NN): STORY-NNN-NN <title>"
280
- git worktree remove .worktrees/STORY-NNN-NN
281
- git branch -d story/STORY-NNN-NN
282
- ```
283
-
284
- Verify all required reports exist before merge:
355
+ **DevOps-owned.** The orchestrator does NOT run `git merge`, `git worktree remove`, `git branch -d`, `update_state.mjs`, or `npm run prebuild` directly. All mechanical merge work is delegated to the DevOps agent.
285
356
 
286
- - `STORY-NNN-NN-dev.md` (always required, regardless of lane)
287
- - `STORY-NNN-NN-qa.md` (required unless lane=fast skipped QA)
288
- - `STORY-NNN-NN-arch.md` (required for v2 standard-lane only)
357
+ **Required reports verify before dispatch:**
289
358
 
290
- Missing report return to spawn that agent. **Do not merge with missing reports.**
291
-
292
- ### C.7 Blockers Triage (Developer circuit breaker)
359
+ | Report | Required when |
360
+ |---|---|
361
+ | `STORY-NNN-NN-dev.md` | Always (all lanes) |
362
+ | `STORY-NNN-NN-qa.md` | Always, unless lane=fast explicitly skipped QA-Verify |
363
+ | `STORY-NNN-NN-arch.md` | v2 standard-lane only |
364
+ | `STORY-NNN-NN-devops.md` | Written BY DevOps during this step (not a prerequisite) |
365
+
366
+ Missing `dev.md` or `qa.md` (when required) → return to spawn that agent. **Do not dispatch DevOps with missing reports.**
367
+
368
+ **Orchestrator dispatch sequence:**
369
+
370
+ 1. Mark the dispatch:
371
+ ```bash
372
+ bash .cleargate/scripts/write_dispatch.sh STORY-NNN-NN devops
373
+ ```
374
+
375
+ 2. Spawn the DevOps agent with the following context (§3.1 Context Pack):
376
+ ```
377
+ SPRINT-{NN} — DevOps dispatch for {STORY-ID}.
378
+
379
+ INPUTS:
380
+ - Story ID: {STORY-ID}
381
+ - Sprint ID: SPRINT-{NN}
382
+ - Worktree path: .worktrees/{STORY-ID}/
383
+ - Story branch: story/{STORY-ID}
384
+ - Sprint branch: sprint/S-{NN}
385
+ - Dev commit SHA: {abc1234}
386
+ - QA commit SHA (if present): {def5678}
387
+ - Architect commit SHA (if present): {ghi9012}
388
+ - Files-changed manifest: {list from git show --stat <dev-sha>}
389
+ - Canonical scaffold touched? {yes|no}
390
+ - Lane: {standard | fast}
391
+ - Required reports present:
392
+ - {STORY-ID}-dev.md ✓
393
+ - {STORY-ID}-qa.md ✓ (or "skipped — fast lane")
394
+ - {STORY-ID}-arch.md ✓ (v2 standard lane only)
395
+
396
+ ACTIONS (in order):
397
+ 1. Verify all required reports exist; halt if any missing.
398
+ 2. Checkout sprint branch.
399
+ 3. git merge story/{STORY-ID} --no-ff -m "merge(story/{STORY-ID}): {commit subject}"
400
+ 4. If canonical scaffold touched: cd cleargate-cli && npm run prebuild
401
+ 5. Mirror parity audit: for each file in files-changed where canonical mirror exists,
402
+ diff live ↔ canonical. Report drift in §Mirror Parity (DO NOT auto-fix).
403
+ 6. Post-merge test verification: run only test files touched by this commit.
404
+ 7. git worktree remove .worktrees/{STORY-ID}
405
+ 8. git branch -d story/{STORY-ID}
406
+ 9. CLEARGATE_STATE_FILE=... node .cleargate/scripts/update_state.mjs {STORY-ID} Done
407
+ ```
408
+
409
+ 3. **Halt** and wait for DevOps to return `STATUS=done` or `STATUS=blocked`.
410
+
411
+ **On `STATUS=done`:** DevOps has written `.cleargate/sprint-runs/SPRINT-{NN}/reports/{STORY-ID}-devops.md`. If it notes live re-sync needed (mirror parity drift), address via `cleargate init` or hand-port at Gate-4 doc-refresh (see §C.9 Flashcard Gate for the next step, then proceed to the next story / wave).
412
+
413
+ **On `STATUS=blocked`:** DevOps has written `{STORY-ID}-devops-blockers.md`. Surface the blockers report to the human. DevOps does NOT auto-resolve conflicts — orchestrator escalates and waits for human resolution before re-dispatching DevOps.
414
+
415
+ **Forbidden orchestrator patterns (v2):** `git merge`, `git worktree remove`, `git branch -d`, `update_state.mjs`, `npm run prebuild` in the orchestrator's main session bash log. If any appear, classify as edge case and document in sprint §4 Execution Log.
416
+
417
+ #### DevOps Escape Hatch — subagent_type=devops unavailable
418
+
419
+ If spawning `Agent(subagent_type=devops)` returns "Agent type 'devops' not found" (registry miss — see §1 registration constraint note), the orchestrator executes the §C.7 ACTIONS steps 2-9 verbatim inline (in the orchestrator's own bash session). All rules still apply: no `git merge` short-circuit, no conflict auto-resolution. Write the same `STORY-NNN-NN-devops.md` report with an additional frontmatter field `operator: orchestrator-fallback` and note the session restart requirement. This path is exceptional — if it triggers, queue a session restart at the next natural break and verify `devops` appears in the next session's available agent list before continuing the sprint.
420
+
421
+ ### C.8 Blockers Triage (Developer circuit breaker)
293
422
 
294
423
  When Developer returns `BLOCKED: circuit breaker triggered`, read `.cleargate/sprint-runs/<id>/reports/STORY-NNN-NN-dev-blockers.md`. The report has three sections:
295
424
 
@@ -301,7 +430,7 @@ When Developer returns `BLOCKED: circuit breaker triggered`, read `.cleargate/sp
301
430
 
302
431
  3 consecutive circuit-breaker hits on the same story → `update_state.mjs STORY-NNN-NN Escalated`, halt.
303
432
 
304
- ### C.8 Flashcard Gate (v2 mandatory; v1 dogfood)
433
+ ### C.9 Flashcard Gate (v2 mandatory; v1 dogfood)
305
434
 
306
435
  After every story merge — **before creating story N+1's worktree** — process the merged `flashcards_flagged` list (union of dev + QA, dedupe by exact-string):
307
436
 
@@ -321,7 +450,24 @@ touch .cleargate/sprint-runs/<sprint-id>/.processed-${HASH}
321
450
 
322
451
  Under v2, the `pending-task-sentinel.sh` PreToolUse hook blocks the next `Task` spawn until every card has a `.processed-<hash>` marker. Bypass only with `SKIP_FLASHCARD_GATE=1` — log the bypass in sprint §4.
323
452
 
324
- ### C.9 Mid-cycle User Input — CR Triage
453
+ ### C.10 Mid-Sprint Triage
454
+
455
+ When the user injects new input mid-sprint (between story kickoff and merge), classify it before routing. Use the keyword-heuristic classifier (`classify()` from `cleargate-cli/src/lib/triage-classifier.ts`) as a first-pass advisory, then confirm. See authoritative rubric: `.cleargate/knowledge/mid-sprint-triage-rubric.md`.
456
+
457
+ | Class | Trigger | Counter | Human Approval | First-pass action |
458
+ |---|---|---|---|---|
459
+ | `bug` | Defect vs spec | `qa_bounces++` | No | Re-open story; Dev fix; QA re-verify |
460
+ | `clarification` | Ambiguity question | None | No (yes if spec gap) | Update §1.2 in place; re-run impacted test |
461
+ | `scope` | Net-new requirement | None | YES for mid-sprint add | Quarantine to next sprint (default); escalate if goal-critical |
462
+ | `approach` | Different impl, same spec | None | No (yes if cross-story impact) | Reset Dev context; re-spawn with updated approach |
463
+
464
+ **Confidence signal:** `classify()` returns `confidence: 'low'` when no keyword matched. LOW confidence classifications MUST be confirmed with the human before routing.
465
+
466
+ **Scope-only note:** This rubric covers USER input only. QA-FAIL bounces continue to use `qa_bounces` (existing §C.5 path). TPV-gap bounces use `arch_bounces` (§C.3.5 path).
467
+
468
+ For the operational routing table with per-type routing rules, see §C.11 (Mid-cycle User Input — CR Triage).
469
+
470
+ ### C.11 Mid-cycle User Input — CR Triage
325
471
 
326
472
  If the user injects new input mid-story, classify before routing:
327
473
 
@@ -334,7 +480,26 @@ If the user injects new input mid-story, classify before routing:
334
480
 
335
481
  Log every CR in sprint §4 Execution Log with date + ID.
336
482
 
337
- > 🎯 **Goal check on `CR:scope-change`.** Default routing is quarantine-into-next-sprint. **Override the default if the new requirement is critical to the active sprint goal** — escalate to the human with: *"This scope-change is goal-critical: the sprint goal is `<verbatim>` and without this change, the goal will not be met. Add to current sprint? (Adding mid-sprint requires explicit ack per §C.9.)"* Quarantine remains default for goal-incidental scope.
483
+ > 🎯 **Goal check on `CR:scope-change`.** Default routing is quarantine-into-next-sprint. **Override the default if the new requirement is critical to the active sprint goal** — escalate to the human with: *"This scope-change is goal-critical: the sprint goal is `<verbatim>` and without this change, the goal will not be met. Add to current sprint? (Adding mid-sprint requires explicit ack per §C.11.)"* Quarantine remains default for goal-incidental scope.
484
+
485
+ ### C.11 Script Invocation Contract (CR-046)
486
+
487
+ All bash/node script invocations dispatched FROM agents (Developer / QA / Architect / DevOps) MUST use the wrapper:
488
+
489
+ ```bash
490
+ bash .cleargate/scripts/run_script.sh <command> [args...]
491
+ ```
492
+
493
+ **What the wrapper does (on failure):**
494
+ - Captures stdout + stderr independently (≤4KB each; truncated with `... [truncated]` if exceeded).
495
+ - Writes a structured JSON incident to `.cleargate/sprint-runs/<sprint-id>/.script-incidents/<ts>-<hash>.json`.
496
+ - Propagates the original exit code to the caller.
497
+
498
+ **Self-exemption clause:** The wrapper itself (`run_script.sh`) is exempt from wrapping — it sets `RUN_SCRIPT_ACTIVE=1` before executing the wrapped command so recursive calls pass through directly. Orchestrator-direct invocations of `run_script.sh` are the canonical entry point; they are exempt by this contract.
499
+
500
+ **Agent reporting obligation:** If a wrapped script fails (incident JSON written), the dispatching agent MUST include the incident JSON path in its report's `## Script Incidents` section.
501
+
502
+ **Pre-sprint invocations (before `.active` sentinel exists):** Wrapper writes to `.cleargate/sprint-runs/_off-sprint/.script-incidents/` when no active sprint is detected. This is expected during preflight steps.
338
503
 
339
504
  ---
340
505
 
@@ -377,6 +542,12 @@ If the report stub does not exist, dispatch the Reporter:
377
542
  bash .cleargate/scripts/write_dispatch.sh SPRINT-NN reporter
378
543
  ```
379
544
 
545
+ - Cross-cutting rules: Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md` BEFORE any other action.
546
+
547
+ > **Fresh session.** The Reporter MUST dispatch in a fresh session — do not inherit dev+qa cumulative context. `write_dispatch.sh` already spawns a clean shell child; the `Agent` tool path requires no session-continuation flag. If the runtime offers a session-reset knob (e.g. `--resume false` or equivalent), use it. The Reporter starts cold and reads only `.reporter-context.md` + `sprint_report.md`.
548
+
549
+ > **Token budget.** Soft warn at 200k tokens, hard advisory at 500k (per CR-036). The token-ledger SubagentStop hook emits the warning to stdout; orchestrator surfaces the line into chat. Hard advisory auto-records a flashcard. The dispatch is NOT killed — the warning is informational; review the bundle slices on next sprint.
550
+
380
551
  Reporter writes `.cleargate/sprint-runs/<id>/SPRINT-<#>_REPORT.md` and returns the Brief:
381
552
 
382
553
  > **Goal:** `<verbatim sprint goal>` — **Verdict: met | partial | missed.**
@@ -0,0 +1,160 @@
1
+ # Mid-Sprint Triage Rubric
2
+
3
+ **CR-047 · SPRINT-23 · Authoritative Reference**
4
+
5
+ This document is the authoritative rubric for classifying mid-sprint user input. It complements the operational routing table in SKILL.md §C.10 (new rubric section). Read this doc to understand *why* a class exists; read SKILL.md §C.11 (post-CR-047 renumber) to see *how* routing works in practice.
6
+
7
+ ---
8
+
9
+ ## Overview
10
+
11
+ When a user injects input during an active sprint (between story kickoff and story merge), the orchestrator must classify it before routing. Unclassified input leads to either silent scope creep or unnecessary story restarts. The four classes below are mutually exclusive and exhaustive.
12
+
13
+ **Classifier aid:** `cleargate-cli/src/lib/triage-classifier.ts` exports a `classify()` pure function that performs keyword-heuristic pre-classification. Output is advisory — the orchestrator confirms before acting. `confidence: 'low'` always requires human verification.
14
+
15
+ ---
16
+
17
+ ## Class 1: Bug
18
+
19
+ **Definition:** The user reports that existing implemented behaviour is incorrect, broken, or regressed against the current story's acceptance spec. A bug does NOT introduce new requirements — it identifies a gap between the spec and the actual behaviour.
20
+
21
+ **Keywords (heuristic):** broken, crashes, doesn't work, does not work, regression, nothing works, not working, failed, failure, error, exception, bug, defect, broke.
22
+
23
+ **Boundary cases:**
24
+ - "The button is broken" → Bug (existing behaviour broken vs spec).
25
+ - "The button should also glow" → NOT Bug; this is Scope Change (new requirement).
26
+ - "After the deploy nothing works" → Bug (regression language).
27
+
28
+ **Worked examples:**
29
+
30
+ 1. "The login button is broken — it throws a 500 error instead of returning 401."
31
+ → Class: Bug · Route: re-open story, Dev fixes, QA re-verifies.
32
+
33
+ 2. "After the deploy the email sending stopped working."
34
+ → Class: Bug · Route: same as above.
35
+
36
+ **Routing rules:**
37
+
38
+ - Increment `qa_bounces` via `update_state.mjs <story-id> --qa-bounce`.
39
+ - Re-open the story; Developer fixes; QA re-verifies (full loop).
40
+ - Log in sprint §4 Execution Log: date + story ID + one-line description.
41
+ - Human approval: NOT required (orchestrator routes autonomously).
42
+
43
+ **Bounce-counter impact:** `qa_bounces++`. If `qa_bounces ≥ 3` → `Escalated`, halt.
44
+
45
+ ---
46
+
47
+ ## Class 2: Spec Clarification
48
+
49
+ **Definition:** The user asks a question or requests clarification about an existing requirement without adding new scope. The story's acceptance Gherkin remains unchanged after the clarification — at most, ambiguous language in §1.2 gets updated in place.
50
+
51
+ **Keywords (heuristic):** what does, what is, clarify, clarification, is the same as, the same as, same as, mean in, mean by, what do you mean, does it include, is this, should it.
52
+
53
+ **Boundary cases:**
54
+ - "What does 'eligible' mean in §3?" → Clarification (no new scope).
55
+ - "Is 'merged' the same as 'closed'?" → Clarification (terminology disambiguation).
56
+ - "What does 'eligible' mean — and should we also check for X?" → SPLIT: Clarification + Scope Change. Handle separately.
57
+
58
+ **Worked examples:**
59
+
60
+ 1. "What does 'eligible' mean in the eligibility check requirement?"
61
+ → Class: Clarification · Update §1.2 with the answer; no story restart.
62
+
63
+ 2. "Is 'merged' the same as 'closed' for the purposes of the state machine?"
64
+ → Class: Clarification · Add a definition note; no counter impact.
65
+
66
+ **Routing rules:**
67
+
68
+ - No counter increment.
69
+ - Update §1.2 (Acceptance Criteria) in place with the clarified definition.
70
+ - Re-run only the impacted test(s) (not full loop).
71
+ - Log in sprint §4 Execution Log: date + story ID + one-line clarification.
72
+ - Human approval: NOT required for terminology clarifications. Required if the clarification reveals a spec gap (surface to human before updating §1.2).
73
+
74
+ **Bounce-counter impact:** None.
75
+
76
+ ---
77
+
78
+ ## Class 3: Scope Change
79
+
80
+ **Definition:** The user introduces a net-new requirement that was not in the original story spec. Even if "obvious" or "related", if the Gherkin would need a new scenario, it is Scope Change.
81
+
82
+ **Keywords (heuristic):** also need, we also need, plus add, additionally, new requirement, add a, add an, plus, as well, in addition, new feature, extend with.
83
+
84
+ **Boundary cases:**
85
+ - "We also need a CSV export" → Scope Change (new feature).
86
+ - "Plus add audit logging" → Scope Change (additional requirement).
87
+ - "The export is broken" → NOT Scope Change; this is Bug.
88
+
89
+ **Worked examples:**
90
+
91
+ 1. "We also need a CSV export alongside the existing JSON export."
92
+ → Class: Scope Change · Quarantine into new story in `pending-sync/`. Current story unchanged.
93
+
94
+ 2. "Plus add audit logging for all admin actions."
95
+ → Class: Scope Change · Same quarantine routing.
96
+
97
+ **Routing rules:**
98
+
99
+ - **Quarantine by default.** Create a new Story file in `.cleargate/delivery/pending-sync/` for the next sprint.
100
+ - Current story proceeds UNCHANGED.
101
+ - **Goal-critical override:** if the new requirement is critical to the active sprint goal, escalate to human: *"This scope-change is goal-critical: the sprint goal is `<verbatim>` and without this change, the goal will not be met. Add to current sprint? (Adding mid-sprint requires explicit ack.)"*
102
+ - Log in sprint §4 Execution Log: date + new story ID + one-line description.
103
+ - Human approval: REQUIRED for mid-sprint addition; NOT required for quarantine.
104
+
105
+ **Bounce-counter impact:** None (quarantine path). If mid-sprint addition approved: treat as a new story dispatch (all counters reset for the new story).
106
+
107
+ ---
108
+
109
+ ## Class 4: Approach Change
110
+
111
+ **Definition:** The user proposes a different implementation technique or technology for the same spec. The acceptance Gherkin remains identical — only the *how* changes, not the *what*.
112
+
113
+ **Keywords (heuristic):** instead of, switch to, different way, different approach, replace with, rather than, alternative, migrate to.
114
+
115
+ **Boundary cases:**
116
+ - "Instead of polling, switch to websockets" → Approach Change (same behaviour spec, different mechanism).
117
+ - "Switch to Postgres instead of Redis for invite storage" → Approach Change (storage backend, same API).
118
+ - "Instead of storing invites, delete them" → NOT Approach Change; this is Scope Change (spec changes).
119
+
120
+ **Worked examples:**
121
+
122
+ 1. "Instead of polling the API every 5 seconds, switch to websockets for real-time updates."
123
+ → Class: Approach Change · No counter; reset Developer context; re-spawn with updated approach note.
124
+
125
+ 2. "Use a different algorithm — BFS instead of DFS for the graph traversal."
126
+ → Class: Approach Change · Same routing.
127
+
128
+ **Routing rules:**
129
+
130
+ - No counter increment.
131
+ - Reset Developer context (re-spawn Developer with the updated approach in the dispatch prompt).
132
+ - Story Gherkin and acceptance criteria remain UNCHANGED.
133
+ - Log in sprint §4 Execution Log: date + story ID + one-line approach delta.
134
+ - Human approval: NOT required if the approach is technically equivalent. Required if the approach change affects cost, timeline, or cross-story surfaces.
135
+
136
+ **Bounce-counter impact:** None.
137
+
138
+ ---
139
+
140
+ ## Routing Summary Table
141
+
142
+ | Class | Trigger | Counter | Human Approval | Routing Action |
143
+ |---|---|---|---|---|
144
+ | Bug | Defect vs spec | `qa_bounces++` | No | Re-open story; Dev fix; QA re-verify |
145
+ | Spec Clarification | Ambiguity question | None | No (yes if spec gap) | Update §1.2 in place; re-run impacted test |
146
+ | Scope Change | Net-new requirement | None | YES for mid-sprint add | Quarantine to next sprint (default); escalate if goal-critical |
147
+ | Approach Change | Different impl, same spec | None | No (yes if cross-story impact) | Reset Dev context; re-spawn with updated approach |
148
+
149
+ ---
150
+
151
+ ## Cross-References
152
+
153
+ - **SKILL.md §C.10** — NEW Mid-Sprint Triage section (operational routing table, added by CR-047).
154
+ - **SKILL.md §C.11** — Mid-cycle User Input table (pre-CR-047 §C.10; renumbered to §C.11 by this CR).
155
+ - **`cleargate-cli/src/lib/triage-classifier.ts`** — keyword-heuristic classifier (advisory, not authoritative).
156
+ - **SKILL.md §C.3.5** — TPV Gate (Architect-only wiring check between QA-Red and Developer).
157
+
158
+ ---
159
+
160
+ _This document is append-only. Add new examples or boundary cases at the bottom of the relevant class block. Do not restructure class ordering — it matches the classifier priority (bug → approach → scope → clarification)._