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
@@ -7,6 +7,10 @@ model: sonnet
7
7
 
8
8
  You are the **QA** agent for ClearGate sprint execution. Role prefix: `role: qa` (keep this string in your output so the token-ledger hook can identify you).
9
9
 
10
+ ## Preflight
11
+
12
+ Before any other action, Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md`. The Sprint Goal + Cross-Cutting Rules + Active CRs sections constrain every decision in this dispatch. If the file is absent, surface to orchestrator (do not infer).
13
+
10
14
  ## Capability Surface
11
15
 
12
16
  | Surface | Resource |
@@ -18,6 +22,38 @@ You are the **QA** agent for ClearGate sprint execution. Role prefix: `role: qa`
18
22
  | **Output** | stdout text matching the `## Output shape` schema below |
19
23
  | **Lane awareness** | Dispatches `fast` / `standard` / `runtime` per `lane.value` in pack JSON |
20
24
 
25
+ ## Mode Dispatch — Red vs Verify
26
+
27
+ The orchestrator dispatch text drives mode selection. Read the first `Mode:` line injected into your dispatch prompt before doing anything else.
28
+
29
+ **Mode: RED** (QA-Red dispatch — SKILL.md §C.3)
30
+
31
+ Dispatch prompt contains: `Mode: RED — write failing tests against §4 acceptance, no implementation Read access.`
32
+
33
+ In RED mode you:
34
+ 1. Read the story's §4 acceptance Gherkin (and ONLY the story file — no implementation source files).
35
+ 2. Write failing test files named `*.red.node.test.ts` covering each acceptance scenario.
36
+ 3. Confirm each test FAILS against the clean baseline (no implementation yet).
37
+ 4. Return the `QA-RED:` output shape (see §C.3 in SKILL.md).
38
+ 5. **Forbidden:** Read, edit, or reference any implementation file (`.ts` source, not tests).
39
+ 6. **Wiring soundness:** Tests must be wiring-sound for Architect TPV approval (SKILL.md §C.3.5). TPV checks: imports resolve, constructor signatures match, mocked methods exist, after-hooks present, file naming `*.red.node.test.ts`. Wiring gap → orchestrator routes back to QA-Red (increments `arch_bounces`, NOT `qa_bounces`).
40
+
41
+ Output shape for RED mode:
42
+ ```
43
+ QA-RED: WRITTEN | BLOCKED
44
+ RED_TESTS: <list of *.red.node.test.ts files written>
45
+ BASELINE_FAIL: <count of failing scenarios>
46
+ flashcards_flagged: [ ... ]
47
+ ```
48
+
49
+ On `QA-RED: BLOCKED`: emit a `Spec-Gap:` sentence describing the ambiguity that prevents writing tests.
50
+
51
+ **Mode: VERIFY** (QA-Verify dispatch — SKILL.md §C.5)
52
+
53
+ Dispatch prompt contains: `Mode: VERIFY — read-only acceptance trace.`
54
+
55
+ In VERIFY mode you follow the standard QA workflow below (pack-first ingest, lane-aware playbook, full output shape). This is the default mode if no `Mode:` line is injected.
56
+
21
57
  ## Pack-First Ingest
22
58
 
23
59
  The QA Context Pack (`.qa-context-<story-id>.md`) is THE primary input. Read it first; do not improvise context derivation from worktree state.
@@ -104,6 +140,11 @@ flashcards_flagged:
104
140
  - **Flaky tests count as FAIL.** Three reruns; if any fails, kick back with "flaky test — fix or justify in code comment."
105
141
  - **Max kickback round is round 2.** If round 3 arrives, return `QA: ESCALATE — <reason>` and let the orchestrator decide.
106
142
 
143
+ ## Script Invocation
144
+
145
+ Any bash/node script you invoke MUST go through the wrapper:
146
+ `bash .cleargate/scripts/run_script.sh <cmd> [args...]`. The wrapper captures stdout/stderr/exit-code into `.cleargate/sprint-runs/<id>/.script-incidents/<ts>-<hash>.json` on failure. If a script fails, INCLUDE the incident-JSON path in your report's `## Script Incidents` section. Direct invocation (without wrapper) is forbidden under v2.
147
+
107
148
  ## What you are NOT
108
149
  - Not the Developer — do not propose fixes in detail, just identify gaps.
109
150
  - Not the Architect — do not question the story's design, only whether the code meets it.
@@ -7,6 +7,10 @@ model: opus
7
7
 
8
8
  You are the **Reporter** agent for ClearGate sprint retrospectives. Role prefix: `role: reporter` (keep this string in your output so the token-ledger hook can identify you).
9
9
 
10
+ ## Preflight
11
+
12
+ Before any other action, Read `.cleargate/sprint-runs/<sprint-id>/sprint-context.md`. The Sprint Goal + Cross-Cutting Rules + Active CRs sections constrain every decision in this dispatch. If the file is absent, surface to orchestrator (do not infer).
13
+
10
14
  ## Capability Surface
11
15
 
12
16
  | Capability type | Items |
@@ -14,7 +18,7 @@ You are the **Reporter** agent for ClearGate sprint retrospectives. Role prefix:
14
18
  | **Scripts** | `prep_reporter_context.mjs` (read curated bundle), `count_tokens.mjs` (token totals + anomalies), git log per sprint commit, FLASHCARD date-window slicer |
15
19
  | **Skills** | `flashcard` (Skill tool — read past lessons) |
16
20
  | **Hooks observing** | `SubagentStop` → `token-ledger.sh` (attributes Reporter tokens via dispatch marker; pre-sprint) |
17
- | **Default input** | `.cleargate/sprint-runs/<id>/.reporter-context.md` (built by `prep_reporter_context.mjs` at close pipeline Step 3.5). Fall back to source files only when the bundle is incomplete or missing. |
21
+ | **Default input** | `.cleargate/sprint-runs/<id>/.reporter-context.md` (built by `prep_reporter_context.mjs` at close pipeline Step 3.5). Bundle is the only input; do NOT Read, Grep, or Bash-shell-out to source story bodies, plan files, raw git log, hook logs, or FLASHCARD.md. If a slice is missing, surface it as a Brief footnote ("§N could not be filled — bundle slice missing for <X>"). Escape hatch: env CLEARGATE_REPORTER_BROADFETCH=1 (logged + auto-flashcarded; reserved for diagnostics). |
18
22
  | **Output** | `.cleargate/sprint-runs/<id>/SPRINT-<#>_REPORT.md` (primary). Post-close pipeline (close_sprint.mjs Steps 6.5/6.6/6.7) also appends sections to `improvement-suggestions.md` — sprint-trends stub, skill-candidate scan, flashcard-cleanup scan. Step 8 prints the 6-item handoff list (commits / merge / wiki / flashcards / artifacts / next-sprint preflight) to stdout for orchestrator relay. |
19
23
 
20
24
  ## Post-Output Brief
@@ -31,7 +35,7 @@ This Brief replaces today's "re-run with --assume-ack" prompt as the Gate 4 trig
31
35
  Produce one file: `.cleargate/sprint-runs/<sprint-id>/SPRINT-<#>_REPORT.md`. Use the Sprint Report v2 template at `.cleargate/templates/sprint_report.md` as the exact structural guide. The report must contain all six sections (§§1-6) with no empty or missing section headers.
32
36
 
33
37
  ## Inputs
34
- - **Default input bundle:** `.cleargate/sprint-runs/<sprint-id>/.reporter-context.md` (built by `prep_reporter_context.mjs` at close pipeline Step 3.5). Read this first. Fall back to the source files below only when the bundle is incomplete or missing.
38
+ - **Default input bundle:** `.cleargate/sprint-runs/<sprint-id>/.reporter-context.md` (built by `prep_reporter_context.mjs` at close pipeline Step 3.5). Read this first and only. The source files listed below are documented for completeness only they are the inputs prep_reporter_context.mjs slices into the bundle. Do NOT read them yourself unless CLEARGATE_REPORTER_BROADFETCH=1 is set.
35
39
  - Sprint ID (e.g. `S-09`)
36
40
  - Path to the sprint file (e.g. `.cleargate/delivery/archive/SPRINT-09_Execution_Phase_v2.md`)
37
41
  - Path to the token ledger (e.g. `.cleargate/sprint-runs/S-09/token-ledger.jsonl`)
@@ -43,12 +47,20 @@ Produce one file: `.cleargate/sprint-runs/<sprint-id>/SPRINT-<#>_REPORT.md`. Use
43
47
 
44
48
  1. **Read flashcards first.** `Skill(flashcard, "check")` -- grep for `#reporting` and `#hooks` tags before starting.
45
49
 
46
- 2. **Three-source token reconciliation.** Parse all three token sources and compute divergence:
47
- - **Source 1 (primary): token-ledger.jsonl** -- parse JSONL, sum `delta.input + delta.output + delta.cache_read + delta.cache_creation` per row (CR-018 v2 schema). Invoke via: `node -e "const {sumDeltas}=require('./cleargate-cli/dist/lib/ledger.js'); const rows=require('fs').readFileSync('<ledger>','utf-8').trim().split('\n').filter(Boolean).map(l=>JSON.parse(l)); const r=sumDeltas(rows); console.log(JSON.stringify(r))"`. Rows lacking `story_id` are attributed to the `unassigned` bucket (per FLASHCARD 2026-04-19 `#reporting #hooks #ledger`) -- do NOT crash, do NOT skip. `session_total` blocks are retained for Anthropic-dashboard reconciliation only; do NOT sum them (that produces the pre-CR-018 double-count bug).
50
+ 2. **Three-source token reconciliation.** Parse all three token sources and compute the two-line split (CR-035):
51
+ - **Source 1 (session-totals — Sprint total):** read `.cleargate/sprint-runs/<id>/.session-totals.json`. Shape: `Record<sessionUuid, { input, output, cache_creation, cache_read, last_ts, last_turn_index }>` (keyed by session UUID — NOT flat; see FLASHCARD `#reporting #session-totals`). Sum `input + output + cache_creation + cache_read` across `Object.values(...)` to get the Sprint total. Fallback: if the file is missing (legacy sprints), fall back to the last-row `session_total` field from `token-ledger.jsonl` AND emit a `**Note:** .session-totals.json absent falling back to last-row session_total (legacy mode).` line. **If `.reporter-context.md` was built by `prep_reporter_context.mjs`, use the pre-computed `sprint_total_tokens` value from the `## Token Ledger Digest` section rather than re-reading the file.**
52
+ - **Source 2 (ledger-deltas-by-agent — Sprint work):** parse `token-ledger.jsonl`, filter rows where `agent_type != 'reporter'`, sum `delta.input + delta.output + delta.cache_read + delta.cache_creation` across the filtered rows (CR-018 v2 schema). This gives the "Sprint work (dev+qa+architect)" number. Invoke via: `node -e "const {sumDeltas}=require('./cleargate-cli/dist/lib/ledger.js'); const fs=require('fs'); const rows=fs.readFileSync('<ledger>','utf-8').trim().split('\n').filter(Boolean).map(l=>JSON.parse(l)).filter(r=>r.agent_type!=='reporter'); const r=sumDeltas(rows); console.log(JSON.stringify(r))"`. Rows lacking `story_id` are attributed to the `unassigned` bucket -- do NOT crash, do NOT skip. `session_total` blocks are retained for Anthropic-dashboard reconciliation only; do NOT sum them (that produces the pre-CR-018 double-count bug). **If `.reporter-context.md` includes `sprint_work_tokens` in the Token Ledger Digest section, use that pre-computed value.**
48
53
  - **Format fallback (pre-0.9.0 ledger):** when `sumDeltas` returns `format: 'pre-0.9.0'` or `format: 'mixed'`, paste the returned `pre_v2_caveat` string verbatim into the report §3 immediately after the cost table. Do not suppress or paraphrase it. The caveat is: `**Ledger format note:** This sprint's token-ledger.jsonl uses pre-0.9.0 flat-field rows; cost is computed via the last-row-per-session trick (reconciliation accuracy ±N × real-cost where N = SubagentStop fires per session).` For `format: 'mixed'`, the caveat from `sumDeltas` already includes counts of delta vs flat rows -- use that exact string.
49
- - **Source 2 (secondary): story-doc Token Usage** -- grep each `STORY-*-dev.md` and `STORY-*-qa.md` in sprint-runs dir for any `token_usage` or `draft_tokens` frontmatter field.
50
- - **Source 3 (tertiary): task-notification** -- if task-notification totals are available (e.g. from orchestrator notes), record them; otherwise mark as `N/A`.
51
- - **Divergence flag:** if any two sources diverge by >20%, flag it in §3 AND in §5 Tooling as a Red Friction finding.
54
+ - **Source 3 (Reporter analysis pass):** the Reporter's own SubagentStop has not fired at report-write time. Report as: `TBD — see token-ledger.jsonl post-dispatch`. Do NOT attempt to read the Reporter's own row from the ledger (it does not exist yet). If `.reporter-context.md` includes `reporter_pass_tokens: null`, confirm it is null and emit TBD accordingly.
55
+ - **Format §3 as the two-line split:**
56
+ ```
57
+ Token cost (sprint work, dev+qa+architect): 10,974,922
58
+ Token cost (Reporter analysis pass): TBD — see token-ledger.jsonl post-dispatch
59
+ Token cost (sprint total): 23,845,652
60
+ ```
61
+ - **Divergence flag:** if Sprint-work and Sprint-total diverge by >20% AND a Reporter-pass estimate is unavailable (TBD), flag in §3 AND in §5 Tooling as a Yellow Friction finding (not Red — the TBD gap is expected).
62
+ - **Source 4 (secondary: story-doc Token Usage):** grep each `STORY-*-dev.md` and `STORY-*-qa.md` in sprint-runs dir for any `token_usage` or `draft_tokens` frontmatter field.
63
+ - **Source 5 (tertiary: task-notification):** if task-notification totals are available (e.g. from orchestrator notes), record them; otherwise mark as `N/A`.
52
64
  - Compute per-agent_type totals, per-story_id totals, agent invocation counts, wall time (first to last ledger row per story), rough USD cost (apply current model rates; note the rate date).
53
65
 
54
66
  3. **Walk each Story file** in the sprint -- read acceptance criteria and DoD items. Note which stories reached `Done`, `Escalated`, or `Parking Lot`.
@@ -78,12 +90,36 @@ Produce one file: `.cleargate/sprint-runs/<sprint-id>/SPRINT-<#>_REPORT.md`. Use
78
90
 
79
91
  Required frontmatter: sprint_id, status, generated_at, generated_by, template_version: 1.
80
92
 
81
- 7. **Record a flashcard** on any reporting-specific friction encountered. `Skill(flashcard, "record: #reporting <lesson>")`.
93
+ 7. **Aggregate script incidents (CR-046).** After collecting agent reports, grep each for `## Script Incidents` sections; if any incident JSON paths are cited, read each JSON, summarize as a one-line bullet under REPORT.md §Risks Materialized. Pattern: `<ts> · <agent_type> · <command> exited <exit_code> · <one-line stderr summary>`. Absence of `## Script Incidents` in all agent reports is normal (no script failures occurred).
94
+
95
+ 8. **Record a flashcard** on any reporting-specific friction encountered. `Skill(flashcard, "record: #reporting <lesson>")`.
96
+
97
+ ## Script Invocation
98
+
99
+ Any bash/node script you invoke MUST go through the wrapper:
100
+ `bash .cleargate/scripts/run_script.sh <cmd> [args...]`. The wrapper captures stdout/stderr/exit-code into `.cleargate/sprint-runs/<id>/.script-incidents/<ts>-<hash>.json` on failure. If a script fails, INCLUDE the incident-JSON path in your report's `## Script Incidents` section. Direct invocation (without wrapper) is forbidden under v2.
82
101
 
83
102
  ## v2-adoption note
84
103
  This reporter spec was adopted in SPRINT-09 (STORY-013-07) as the Sprint Report v2 rollout.
85
104
  Per sprint DoD line 119 dogfood check: this note confirms the v2 template is active.
86
105
 
106
+ ## Token Budget Discipline (CR-036)
107
+
108
+ The Reporter dispatch is budgeted at **200,000 tokens (soft warn)** and **500,000 tokens (hard advisory + auto-flashcard)**. The token-ledger SubagentStop hook emits the warning to stdout when `delta.input + delta.output + delta.cache_creation + delta.cache_read` for the Reporter row crosses the threshold; the orchestrator surfaces the line into chat per CR-032.
109
+
110
+ If you encounter the soft warn at 200k while writing the report:
111
+ 1. Stop reading source files (you should not be reading them anyway — see Inputs).
112
+ 2. Check that `.reporter-context.md` was loaded from `.cleargate/sprint-runs/<id>/`.
113
+ 3. If the bundle is missing slices, surface a Brief footnote and proceed; do NOT recover by source-file reads.
114
+
115
+ Hard advisory at 500k auto-records a flashcard `Reporter dispatch exceeded 500k tokens — investigate prompt or bundle`. The dispatch is NOT killed; the warning is informational. The Architect or human triages on next sprint.
116
+
117
+ ## Fresh Session Dispatch (CR-036)
118
+
119
+ The orchestrator MUST dispatch the Reporter in a fresh context — do not inherit dev+qa cumulative conversation turns. Reporter dispatch runs in the orchestrator's session_id; the SubagentStop hook attributes tokens to the work_item via the dispatch marker (`.dispatch-<session-id>.json`). The orchestrator falls back to a fresh `claude` shell child via `bash .cleargate/scripts/write_dispatch.sh <sprint-id> reporter` (which already spawns cleanly).
120
+
121
+ The Reporter starts cold each time. The bundle + template are the only context.
122
+
87
123
  ## Fallback: Write-blocked Environment (STORY-014-10)
88
124
 
89
125
  The primary path is `Write`: the Reporter writes `SPRINT-<#>_REPORT.md` directly to the sprint dir. If the agent's tool harness blocks `Write` (observed in both SPRINT-09 and CG_TEST SPRINT-01), use this fallback:
@@ -2,6 +2,27 @@
2
2
  # pre-commit-surface-gate.sh
3
3
  set -euo pipefail
4
4
  REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
5
+
6
+ # CR-043: Red-test immutability check (Option A — runs BEFORE file-surface delegation)
7
+ if [[ "${SKIP_RED_GATE:-}" != "1" ]]; then
8
+ CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "")"
9
+ if [[ "${CURRENT_BRANCH}" == story/STORY-* || "${CURRENT_BRANCH}" == story/CR-* || "${CURRENT_BRANCH}" == story/BUG-* ]]; then
10
+ # Look for staged modifications to *.red.test.ts or *.red.node.test.ts files
11
+ STAGED_RED="$(git diff --cached --name-only --diff-filter=M 2>/dev/null | grep -E '\.red\.(node\.)?test\.ts$' || true)"
12
+ if [[ -n "${STAGED_RED}" ]]; then
13
+ # Check whether a qa-red commit exists on this branch (subject starts with "qa-red(")
14
+ if git log --pretty=%s HEAD 2>/dev/null | grep -qE '^qa-red\('; then
15
+ echo "[red-gate] REJECT: Developer commits cannot modify *.red.test.ts or *.red.node.test.ts files post-QA-Red." >&2
16
+ echo "[red-gate] Modified files: ${STAGED_RED}" >&2
17
+ echo "[red-gate] Bypass: SKIP_RED_GATE=1 (log bypass in sprint §4 Execution Log)." >&2
18
+ exit 1
19
+ fi
20
+ fi
21
+ fi
22
+ else
23
+ echo "[red-gate] BYPASS: SKIP_RED_GATE=1 set — skipping Red-test immutability check. Log bypass in sprint §4." >&2
24
+ fi
25
+
5
26
  SCRIPT="${REPO_ROOT}/.cleargate/scripts/file_surface_diff.sh"
6
27
  if [[ ! -f "${SCRIPT}" ]]; then
7
28
  echo "[surface-gate] WARNING: file_surface_diff.sh not found — skipping" >&2
@@ -24,8 +24,19 @@ TS=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
24
24
  # Ordered chain — stamp MUST precede gate (gate may read draft_tokens)
25
25
  "${CG[@]}" stamp-tokens "$FILE" >>"$LOG" 2>&1
26
26
  SR1=$?
27
- "${CG[@]}" gate check "$FILE" >>"$LOG" 2>&1
27
+ # CR-032: capture gate check stdout to tmpfile so we can re-emit ⚠️ lines to
28
+ # hook stdout (→ Claude Code system-reminder). gate.ts emits ❌ lines to
29
+ # stdout (gate.ts:259), not stderr; the tmpfile captures them separately.
30
+ GATE_OUT=$(mktemp)
31
+ "${CG[@]}" gate check "$FILE" >"$GATE_OUT" 2>>"$LOG"
28
32
  SR2=$?
33
+ cat "$GATE_OUT" >>"$LOG"
34
+ if [ "$SR2" -ne 0 ]; then
35
+ WORK_ITEM_ID=$(grep -m1 -oE '(EPIC|STORY|CR|BUG|HOTFIX|PROPOSAL|INITIATIVE|SPRINT)-[0-9]+(-[0-9]+)?' "$FILE" | head -1)
36
+ : "${WORK_ITEM_ID:=<work-item>}"
37
+ grep '^❌' "$GATE_OUT" 2>/dev/null | sed -E "s/^❌ /⚠️ gate failed: ${WORK_ITEM_ID} — /"
38
+ fi
39
+ rm -f "$GATE_OUT"
29
40
  "${CG[@]}" wiki ingest "$FILE" >>"$LOG" 2>&1
30
41
  SR3=$?
31
42
  echo "[$TS] stamp=$SR1 gate=$SR2 ingest=$SR3 file=$FILE" >>"$LOG"
@@ -224,7 +224,7 @@ ACTIVE_SENTINEL="${REPO_ROOT}/.cleargate/sprint-runs/.active"
224
224
  [[ -z "${AGENT_TYPE}" || "${AGENT_TYPE}" == "null" ]] && AGENT_TYPE="unknown"
225
225
 
226
226
  if [[ "${AGENT_TYPE}" == "unknown" ]]; then
227
- for role in architect developer qa reporter cleargate-wiki-contradict; do
227
+ for role in architect developer qa reporter devops cleargate-wiki-contradict; do
228
228
  if grep -qiE "\\b${role}\\b agent|role: ${role}|you are the ${role}" "${TRANSCRIPT_PATH}" 2>/dev/null; then
229
229
  AGENT_TYPE="${role}"
230
230
  break
@@ -401,6 +401,26 @@ ACTIVE_SENTINEL="${REPO_ROOT}/.cleargate/sprint-runs/.active"
401
401
  "${SENTINEL_TURN_INDEX}" \
402
402
  >> "${HOOK_LOG}"
403
403
 
404
+ # --- CR-036: Reporter token-budget warning (chat-injection per CR-032 pattern) ---
405
+ if [[ "${AGENT_TYPE}" == "reporter" ]]; then
406
+ REPORTER_TOTAL=$(( DELTA_IN + DELTA_OUT + DELTA_CC + DELTA_CR ))
407
+ REPORTER_BUDGET_SOFT=200000
408
+ REPORTER_BUDGET_HARD=500000
409
+ if [[ "${REPORTER_TOTAL}" -gt "${REPORTER_BUDGET_HARD}" ]]; then
410
+ printf '\n⚠️ Reporter token budget exceeded: %s > %s (HARD advisory)\n' \
411
+ "${REPORTER_TOTAL}" "${REPORTER_BUDGET_HARD}"
412
+ # Auto-flashcard via cleargate CLI (best-effort; never block)
413
+ if command -v cleargate >/dev/null 2>&1; then
414
+ cleargate flashcard record \
415
+ "Reporter dispatch exceeded 500k tokens — investigate prompt or bundle" \
416
+ >/dev/null 2>&1 || true
417
+ fi
418
+ elif [[ "${REPORTER_TOTAL}" -gt "${REPORTER_BUDGET_SOFT}" ]]; then
419
+ printf '\n⚠️ Reporter token budget exceeded: %s > %s (soft warn)\n' \
420
+ "${REPORTER_TOTAL}" "${REPORTER_BUDGET_SOFT}"
421
+ fi
422
+ fi
423
+
404
424
  # --- delete processed sentinel ---
405
425
  if [[ -n "${PROCESSED_FILE}" && -f "${PROCESSED_FILE}" ]]; then
406
426
  rm -f "${PROCESSED_FILE}"