cclaw-cli 0.10.1 → 0.12.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 (55) hide show
  1. package/README.md +4 -3
  2. package/dist/cli.d.ts +6 -0
  3. package/dist/cli.js +297 -9
  4. package/dist/config.js +83 -3
  5. package/dist/content/core-agents.d.ts +44 -0
  6. package/dist/content/core-agents.js +225 -0
  7. package/dist/content/doctor-references.d.ts +2 -0
  8. package/dist/content/doctor-references.js +144 -0
  9. package/dist/content/examples.js +1 -1
  10. package/dist/content/harnesses-doc.d.ts +1 -0
  11. package/dist/content/harnesses-doc.js +95 -0
  12. package/dist/content/hook-events.d.ts +4 -0
  13. package/dist/content/hook-events.js +42 -0
  14. package/dist/content/hooks.js +81 -11
  15. package/dist/content/meta-skill.d.ts +0 -8
  16. package/dist/content/meta-skill.js +51 -341
  17. package/dist/content/next-command.js +2 -1
  18. package/dist/content/protocols.d.ts +7 -0
  19. package/dist/content/protocols.js +123 -0
  20. package/dist/content/research-playbooks.d.ts +8 -0
  21. package/dist/content/research-playbooks.js +135 -0
  22. package/dist/content/skills.js +202 -312
  23. package/dist/content/stage-common-guidance.d.ts +2 -0
  24. package/dist/content/stage-common-guidance.js +71 -0
  25. package/dist/content/stage-schema.d.ts +11 -1
  26. package/dist/content/stage-schema.js +155 -52
  27. package/dist/content/start-command.js +19 -13
  28. package/dist/content/subagents.d.ts +1 -1
  29. package/dist/content/subagents.js +23 -38
  30. package/dist/content/templates.d.ts +1 -1
  31. package/dist/content/templates.js +49 -11
  32. package/dist/delegation.d.ts +1 -0
  33. package/dist/delegation.js +27 -1
  34. package/dist/doctor-registry.d.ts +8 -0
  35. package/dist/doctor-registry.js +127 -0
  36. package/dist/doctor.d.ts +5 -0
  37. package/dist/doctor.js +133 -27
  38. package/dist/flow-state.d.ts +4 -0
  39. package/dist/flow-state.js +4 -1
  40. package/dist/gate-evidence.d.ts +9 -1
  41. package/dist/gate-evidence.js +121 -17
  42. package/dist/harness-adapters.d.ts +7 -0
  43. package/dist/harness-adapters.js +53 -9
  44. package/dist/init-detect.d.ts +2 -0
  45. package/dist/init-detect.js +45 -0
  46. package/dist/install.js +73 -1
  47. package/dist/policy.js +21 -13
  48. package/dist/runs.js +21 -4
  49. package/dist/track-heuristics.d.ts +12 -0
  50. package/dist/track-heuristics.js +144 -0
  51. package/dist/types.d.ts +26 -3
  52. package/dist/types.js +6 -3
  53. package/package.json +2 -1
  54. package/dist/content/agents.d.ts +0 -48
  55. package/dist/content/agents.js +0 -411
@@ -1,116 +1,34 @@
1
1
  import { RUNTIME_ROOT } from "../constants.js";
2
2
  import { STAGE_EXAMPLES_REFERENCE_DIR, stageDomainExamples, stageExamples, stageGoodBadExamples } from "./examples.js";
3
- import { selfImprovementBlock } from "./learnings.js";
3
+ import { STAGE_COMMON_GUIDANCE_REL_PATH } from "./stage-common-guidance.js";
4
4
  import { stageAutoSubagentDispatch, stageSchema } from "./stage-schema.js";
5
- function rationalizationTable(stage) {
6
- const schema = stageSchema(stage);
7
- return `| Rationalization | Reality |
8
- |---|---|
9
- ${schema.rationalizations.map((e) => `| ${e.claim} | ${e.reality} |`).join("\n")}`;
10
- }
11
- function cognitivePatternsList(stage) {
12
- const schema = stageSchema(stage);
13
- if (schema.cognitivePatterns.length === 0)
14
- return "";
15
- const items = schema.cognitivePatterns
16
- .map((p, i) => `${i + 1}. **${p.name}** — ${p.description}`)
17
- .join("\n");
18
- return `## Cognitive Patterns\n\nThese are thinking instincts, not a checklist. Let them shape your perspective throughout the stage.\n\n${items}\n`;
19
- }
20
- function reviewSectionsBlock(stage) {
5
+ const VERIFICATION_STAGES = ["tdd", "review", "ship"];
6
+ const DECISION_PROTOCOL_PATH = `${RUNTIME_ROOT}/references/protocols/decision.md`;
7
+ const COMPLETION_PROTOCOL_PATH = `${RUNTIME_ROOT}/references/protocols/completion.md`;
8
+ function whenNotToUseBlock(stage) {
21
9
  const schema = stageSchema(stage);
22
- if (schema.reviewSections.length === 0)
10
+ if (schema.whenNotToUse.length === 0) {
23
11
  return "";
24
- const sections = schema.reviewSections.map((sec) => {
25
- const points = sec.evaluationPoints.map((p) => `- ${p}`).join("\n");
26
- const stop = sec.stopGate
27
- ? "\n\n**STOP.** Present the most important question from this section to the user, even if your recommendation is clear. If no issues are found, state your assessment in one sentence and ask the user to confirm before moving on. If issues exist, present them ONE AT A TIME: describe the problem concretely, present 2-3 options, state your recommendation, and explain WHY."
28
- : "";
29
- return `### ${sec.title}\n\nEvaluate:\n${points}${stop}`;
30
- }).join("\n\n");
31
- return `## Review Sections\n\n**Anti-skip rule:** Never condense, abbreviate, or skip any review section. If a section genuinely has zero findings, say "No issues found" and move on — but you must evaluate it.\n\n${sections}\n`;
32
- }
33
- function crossStageTraceBlock(stage) {
34
- const trace = stageSchema(stage).crossStageTrace;
35
- const reads = trace.readsFrom.length > 0
36
- ? trace.readsFrom.map((r) => `- ${r}`).join("\n")
37
- : "- (first stage — no upstream artifacts)";
38
- const writes = trace.writesTo.length > 0
39
- ? trace.writesTo.map((w) => `- ${w}`).join("\n")
40
- : "- (final stage — no downstream artifacts)";
41
- return `## Cross-Stage Traceability\n\n**Reads from:**\n${reads}\n\n**Writes to:**\n${writes}\n\n**Rule:** ${trace.traceabilityRule}\n`;
42
- }
43
- function artifactValidationBlock(stage) {
44
- const validations = stageSchema(stage).artifactValidation;
45
- if (validations.length === 0)
46
- return "";
47
- const rows = validations.map((v) => {
48
- const req = v.required ? "REQUIRED" : "optional";
49
- return `| ${v.section} | ${req} | ${v.validationRule} |`;
50
- }).join("\n");
51
- return `## Artifact Validation\n\n| Section | Status | Validation Rule |\n|---|---|---|\n${rows}\n`;
52
- }
53
- function completionStatusBlock(stage) {
54
- const statuses = stageSchema(stage).completionStatus;
55
- const items = statuses.map((s) => `- **${s}**`).join("\n");
56
- return `## Completion Status\n\nWhen this stage ends, report one of:\n${items}\n`;
57
- }
58
- function namedAntiPatternBlock(stage) {
59
- const nap = stageSchema(stage).namedAntiPattern;
60
- if (!nap)
61
- return "";
62
- return `## Anti-Pattern: "${nap.title}"\n\n${nap.description}\n`;
63
- }
64
- function decisionRecordBlock(stage) {
65
- const fmt = stageSchema(stage).decisionRecordFormat;
66
- if (!fmt)
67
- return "";
68
- return `## Decision Record Template\n\nUse this format for every non-trivial architecture or scope decision made during this stage:\n\n\`\`\`\n${fmt}\n\`\`\`\n`;
69
- }
70
- function visualCommunicationBlock(stage) {
71
- if (stage !== "design")
72
- return "";
73
- return `## Visual Communication Rules
74
-
75
- Diagrams are load-bearing artifacts in the design stage, not decoration. A diagram that encodes structure wrongly (or hides structure behind generic labels) misleads every downstream reader. Apply these rules to **every** diagram in the design artifact:
76
-
77
- 1. **Concrete names, never generic.** "Service A → Service B" is not a diagram; it is a shape. Every node must name a real component the team will build or touch (\`NotificationPublisher\`, \`FeedReadModel\`, \`Stripe webhook handler\`). If you cannot name it concretely, the design is not ready.
78
- 2. **Every arrow is labeled.** Label with the message, action, or protocol it carries (\`publishEvent(user_id, payload)\`, \`GET /snapshot\`, \`dedupe-key upsert\`). Unlabeled arrows silently lose the contract between components.
79
- 3. **Direction is explicit.** Use arrowheads, not bare lines; draw the flow of *data* (not "dependency") unless the diagram type is explicitly a dependency graph, in which case say so in a one-line caption.
80
- 4. **Distinguish sync vs async.** Use a convention and state it once in a legend: e.g. solid arrow = synchronous request/response, dashed arrow = async message via queue/bus, double arrow = two-way. Async edges always name the queue or topic.
81
- 5. **Show at least one failure edge.** Every non-trivial diagram needs one branch that represents the degraded or error path (timeout, reconnect, fallback to cache, poison-message routing). A diagram with only the happy path hides the interesting half of the design.
82
- 6. **One level of detail per diagram.** Do not mix "service-level" and "class-level" on the same canvas. If you need both, produce two diagrams — one at the system boundary, one at the internal module — and cross-reference them.
83
- 7. **Caption, not decoration.** Each diagram gets a one-sentence caption below it stating what the reader should take away ("*Publish path with idempotent outbox; SSE stream reads the projection, not the bus directly*"). If you cannot write the caption in one sentence, the diagram is doing two things at once.
84
- 8. **Prefer text-based formats** (Mermaid, ASCII) over binary images in \`.cclaw/artifacts/\` so diffs stay reviewable. Binary/SVG is allowed when the diagram is already the source of truth elsewhere (e.g. \`docs/architecture/\`) and the artifact embeds a link plus a text-based summary.
12
+ }
13
+ return `## When Not to Use
14
+ ${schema.whenNotToUse.map((item) => `- ${item}`).join("\n")}
85
15
 
86
- If a diagram cannot satisfy rules 1–5, do NOT include it — a missing diagram is honest; a misleading diagram is worse. Surface the gap in **Unresolved Decisions** and proceed without the diagram until the decisions that would populate it are locked.
87
16
  `;
88
17
  }
89
18
  function contextLoadingBlock(stage) {
90
19
  const trace = stageSchema(stage).crossStageTrace;
91
20
  const readLines = trace.readsFrom.length > 0
92
- ? trace.readsFrom
93
- .map((value) => `- \`${value}\``)
94
- .join("\n")
21
+ ? trace.readsFrom.map((value) => `- \`${value}\``).join("\n")
95
22
  : "- (first stage — no upstream artifacts)";
96
23
  return `## Context Loading
97
24
 
98
- Before starting stage execution:
25
+ Before execution:
99
26
  1. Read \`.cclaw/state/flow-state.json\`.
100
- 2. Resolve active artifact root: \`.cclaw/artifacts/\`.
27
+ 2. Load active artifacts from \`.cclaw/artifacts/\`.
101
28
  3. Load upstream artifacts required by this stage:
102
29
  ${readLines}
103
- 4. Stream \`.cclaw/knowledge.jsonl\` (strict-JSONL knowledge store) and apply relevant entries before making decisions.
104
- `;
105
- }
106
- function whenNotToUseBlock(stage) {
107
- const schema = stageSchema(stage);
108
- if (!schema.whenNotToUse || schema.whenNotToUse.length === 0) {
109
- return "";
110
- }
111
- return `## When Not to Use
112
- ${schema.whenNotToUse.map((item) => `- ${item}`).join("\n")}
113
-
30
+ 4. Use the injected knowledge digest from session-start; only fall back to full
31
+ \`.cclaw/knowledge.jsonl\` when the digest is insufficient.
114
32
  `;
115
33
  }
116
34
  function autoSubagentDispatchBlock(stage) {
@@ -120,60 +38,198 @@ function autoSubagentDispatchBlock(stage) {
120
38
  const rows = rules
121
39
  .map((rule) => {
122
40
  const userGate = rule.requiresUserGate ? "required" : "not required";
123
- return `| ${rule.agent} | ${rule.mode} | ${rule.when} | ${rule.purpose} | ${userGate} |`;
41
+ return `| ${rule.agent} | ${rule.mode} | ${userGate} | ${rule.when} |`;
124
42
  })
125
43
  .join("\n");
126
44
  const mandatory = stageSchema(stage).mandatoryDelegations;
127
- const mandatoryList = mandatory.length > 0 ? mandatory.map((a) => `\`${a}\``).join(", ") : "(none — only proactive dispatch applies)";
45
+ const mandatoryList = mandatory.length > 0 ? mandatory.map((a) => `\`${a}\``).join(", ") : "none";
128
46
  const delegationLogRel = `${RUNTIME_ROOT}/state/delegation-log.json`;
129
47
  return `## Automatic Subagent Dispatch
130
48
 
131
- Machine-only work should be delegated to specialist agents automatically according to the matrix below.
132
-
133
- | Agent | Mode | When | Purpose | User Gate |
134
- |---|---|---|---|---|
49
+ | Agent | Mode | User Gate | Trigger |
50
+ |---|---|---|---|
135
51
  ${rows}
136
52
 
137
- **Gate rule:** user interaction is required only for approval/override decisions. Do not ask the user to manually trigger machine-only specialist checks.
53
+ Mandatory delegations for this stage: ${mandatoryList}.
54
+ Record completion/waiver in \`${delegationLogRel}\` before stage completion.
55
+ `;
56
+ }
57
+ function researchPlaybooksBlock(stage) {
58
+ const playbooks = stageSchema(stage).researchPlaybooks ?? [];
59
+ if (playbooks.length === 0)
60
+ return "";
61
+ const rows = playbooks
62
+ .map((playbook) => `- \`${RUNTIME_ROOT}/skills/${playbook}\``)
63
+ .join("\n");
64
+ return `## Research Playbooks
138
65
 
139
- ## Delegation Enforcement
66
+ Use these in-thread research procedures before locking this stage. They are
67
+ playbooks (not delegated personas), so execute them in the primary agent context
68
+ and record outcomes in the stage artifact when relevant.
140
69
 
141
- Before completing this stage, verify that ALL mandatory delegations are recorded. If using a harness that supports Task/delegate tools, each mandatory agent must have been invoked. If the harness does not support delegation, record a waiver with reason \`harness_limitation\` in the delegation log.
70
+ ${rows}
71
+ `;
72
+ }
73
+ function reviewSectionsBlock(stage) {
74
+ const schema = stageSchema(stage);
75
+ if (schema.reviewSections.length === 0)
76
+ return "";
77
+ const sections = schema.reviewSections
78
+ .map((sec) => {
79
+ const points = sec.evaluationPoints.map((p) => `- ${p}`).join("\n");
80
+ const stop = sec.stopGate
81
+ ? "\n\n**STOP:** resolve findings in this section before moving forward."
82
+ : "";
83
+ return `### ${sec.title}\n${points}${stop}`;
84
+ })
85
+ .join("\n\n");
86
+ return `## Review Sections
142
87
 
143
- Mandatory agents for this stage: ${mandatoryList}. Stage transition is BLOCKED until all are **completed** or **explicitly waived** by the user (waived entries must name the agent and carry an explicit waiver reason).
88
+ ${sections}
89
+ `;
90
+ }
91
+ function verificationBlock(stage) {
92
+ if (!VERIFICATION_STAGES.includes(stage))
93
+ return "";
94
+ return `## Verification Before Completion
95
+
96
+ Provide fresh, stage-local verification evidence from this turn:
144
97
 
145
- On session stop or stage completion, the agent should write delegation entries to \`${delegationLogRel}\` for audit.
98
+ 1. Run verification commands (tests/build/lint/type-check) for the changed scope.
99
+ 2. Confirm output, do not infer success from prior runs.
100
+ 3. If this is a bug fix, capture RED -> GREEN evidence for the regression path.
146
101
  `;
147
102
  }
148
- const VERIFICATION_STAGES = ["tdd", "review", "ship"];
149
- /**
150
- * Short inline summary of Wave Execution Mode. The detailed 3-task
151
- * walkthrough (RED/GREEN/REFACTOR transcript per slice) lives in the
152
- * companion reference file so the always-rendered skill body stays under
153
- * the 400-line soft budget.
154
- */
155
103
  function waveExecutionModeBlock(stage) {
156
104
  const schema = stageSchema(stage);
157
- if (!schema.waveExecutionAllowed) {
105
+ if (!schema.waveExecutionAllowed)
158
106
  return "";
159
- }
160
107
  return `## Wave Execution Mode
161
108
 
162
- After plan approval (**WAIT_FOR_CONFIRM** / \`plan_wait_for_confirm\` satisfied), process **all tasks in the current dependency wave** sequentially: **RED → GREEN → REFACTOR** per task, recording evidence per slice. **Stop** only on **BLOCKED**, a test failure that **requires user input**, or **wave completion** (every task in the wave has the required RED / GREEN / REFACTOR evidence per the plan artifact).
109
+ Execute the current dependency wave task-by-task (RED -> GREEN -> REFACTOR).
110
+ Stop on BLOCKED status or when user input is required.
111
+ Apply preamble budget discipline: one preamble per wave, then continue without
112
+ repeating it for each task. Re-emit only when the wave boundary changes or the
113
+ plan changes materially.
114
+
115
+ Detailed walkthrough:
116
+ \`.cclaw/${STAGE_EXAMPLES_REFERENCE_DIR}/tdd-wave-walkthrough.md\`
117
+ `;
118
+ }
119
+ function crossStageTraceBlock(stage) {
120
+ const trace = stageSchema(stage).crossStageTrace;
121
+ const reads = trace.readsFrom.length > 0
122
+ ? trace.readsFrom.map((r) => `- ${r}`).join("\n")
123
+ : "- (first stage — no upstream artifacts)";
124
+ const writes = trace.writesTo.length > 0
125
+ ? trace.writesTo.map((w) => `- ${w}`).join("\n")
126
+ : "- (terminal stage)";
127
+ return `## Cross-Stage Traceability
163
128
 
164
- **Wave gate check (before marking a wave complete):**
129
+ Reads from:
130
+ ${reads}
165
131
 
166
- 1. Run the **full suite** one final time → PASS, captured as wave-exit evidence.
167
- 2. Verify the TDD artifact contains RED, GREEN, and REFACTOR evidence for every task in the wave. No partial waves.
168
- 3. Only then declare the wave complete. The next wave cannot start until this step.
132
+ Writes to:
133
+ ${writes}
169
134
 
170
- **When to stop mid-wave (do NOT push through):**
135
+ Rule: ${trace.traceabilityRule}
136
+ `;
137
+ }
138
+ function artifactValidationBlock(stage) {
139
+ const validations = stageSchema(stage).artifactValidation;
140
+ if (validations.length === 0)
141
+ return "";
142
+ const rows = validations
143
+ .map((v) => {
144
+ const req = v.required ? "REQUIRED" : "recommended";
145
+ return `| ${v.section} | ${req} | ${v.validationRule} |`;
146
+ })
147
+ .join("\n");
148
+ return `## Artifact Validation
171
149
 
172
- - A RED test fails for an unpredicted reason (e.g. an unrelated flaky test) → **pause**, diagnose, log an operational-self-improvement entry.
173
- - A GREEN step would require touching code outside the task's acceptance criterion → **pause**, the task is scoped wrong.
174
- - The same RED failure reappears after a GREEN change → **escalate** per the 3-attempts rule.
150
+ | Section | Tier | Validation rule |
151
+ |---|---|---|
152
+ ${rows}
153
+ `;
154
+ }
155
+ function mergedAntiPatterns(schema) {
156
+ const merged = [];
157
+ const seen = new Set();
158
+ for (const item of [...schema.antiPatterns, ...schema.blockers, ...schema.redFlags]) {
159
+ const key = item.trim().toLowerCase();
160
+ if (seen.has(key))
161
+ continue;
162
+ seen.add(key);
163
+ merged.push(item);
164
+ }
165
+ return merged.map((item) => `- ${item}`).join("\n");
166
+ }
167
+ function stageSpecificSeeAlso(stage) {
168
+ const refs = {
169
+ brainstorm: [
170
+ `- \`${RUNTIME_ROOT}/skills/learnings/SKILL.md\``,
171
+ `- \`${RUNTIME_ROOT}/references/stages/brainstorm-examples.md\``
172
+ ],
173
+ scope: [
174
+ `- \`${RUNTIME_ROOT}/skills/learnings/SKILL.md\``,
175
+ `- \`${RUNTIME_ROOT}/references/stages/scope-examples.md\``
176
+ ],
177
+ design: [
178
+ `- \`${RUNTIME_ROOT}/skills/security/SKILL.md\``,
179
+ `- \`${RUNTIME_ROOT}/skills/performance/SKILL.md\``
180
+ ],
181
+ spec: [
182
+ `- \`${RUNTIME_ROOT}/skills/docs/SKILL.md\``,
183
+ `- \`${RUNTIME_ROOT}/references/stages/spec-examples.md\``
184
+ ],
185
+ plan: [
186
+ `- \`${RUNTIME_ROOT}/skills/subagent-dev/SKILL.md\``,
187
+ `- \`${RUNTIME_ROOT}/skills/parallel-dispatch/SKILL.md\``
188
+ ],
189
+ tdd: [
190
+ `- \`${RUNTIME_ROOT}/skills/debugging/SKILL.md\``,
191
+ `- \`${RUNTIME_ROOT}/references/stages/tdd-wave-walkthrough.md\``
192
+ ],
193
+ review: [
194
+ `- \`${RUNTIME_ROOT}/skills/security/SKILL.md\``,
195
+ `- \`${RUNTIME_ROOT}/skills/parallel-dispatch/SKILL.md\``
196
+ ],
197
+ ship: [
198
+ `- \`${RUNTIME_ROOT}/skills/ci-cd/SKILL.md\``,
199
+ `- \`${RUNTIME_ROOT}/skills/docs/SKILL.md\``
200
+ ]
201
+ };
202
+ return refs[stage];
203
+ }
204
+ function completionParametersBlock(schema) {
205
+ const gateList = schema.requiredGates.map((g) => `\`${g.id}\``).join(", ");
206
+ const mandatory = schema.mandatoryDelegations.length > 0
207
+ ? schema.mandatoryDelegations.map((a) => `\`${a}\``).join(", ")
208
+ : "none";
209
+ const nextStage = schema.next === "done" ? "done" : schema.next;
210
+ const nextDescription = schema.next === "done"
211
+ ? "flow complete"
212
+ : stageSchema(schema.next).skillDescription;
213
+ return `## Completion Parameters
214
+
215
+ - \`stage\`: \`${schema.stage}\`
216
+ - \`next\`: \`${nextStage}\` (${nextDescription})
217
+ - \`gates\`: ${gateList}
218
+ - \`artifact\`: \`${RUNTIME_ROOT}/artifacts/${schema.artifactFile}\`
219
+ - \`mandatory delegations\`: ${mandatory}
220
+
221
+ Apply shared completion logic from:
222
+ \`${COMPLETION_PROTOCOL_PATH}\`
223
+ `;
224
+ }
225
+ function quickStartBlock(stage) {
226
+ const schema = stageSchema(stage);
227
+ const gatePreview = schema.requiredGates.slice(0, 3).map((g) => `\`${g.id}\``).join(", ");
228
+ return `## Quick Start
175
229
 
176
- > **Full 3-task walkthrough transcript** (RED/GREEN/REFACTOR per slice, with wave gate check): see \`.cclaw/${STAGE_EXAMPLES_REFERENCE_DIR}/tdd-wave-walkthrough.md\`.
230
+ 1. Obey HARD-GATE and Iron Law.
231
+ 2. Execute checklist in order and persist \`${RUNTIME_ROOT}/artifacts/${schema.artifactFile}\`.
232
+ 3. Satisfy gates (${gatePreview}${schema.requiredGates.length > 3 ? ` +${schema.requiredGates.length - 3}` : ""}).
177
233
  `;
178
234
  }
179
235
  /**
@@ -229,143 +285,9 @@ After T-3 REFACTOR, before declaring Wave 1 done:
229
285
  - A GREEN step would require touching code outside the task's acceptance criterion → **pause**, the task is scoped wrong; adjust the plan or open a follow-up task.
230
286
  - The same RED failure reappears after a GREEN change → **escalate** per the 3-attempts rule; do not keep patching.
231
287
  `;
232
- function stageCompletionProtocol(schema) {
233
- const stage = schema.stage;
234
- const gateIds = schema.requiredGates.map((g) => g.id);
235
- const gateList = gateIds.map((id) => `\`${id}\``).join(", ");
236
- const nextStage = schema.next === "done" ? "done" : schema.next;
237
- const mandatory = schema.mandatoryDelegations;
238
- const mandatoryList = mandatory.length > 0 ? mandatory.map((a) => `\`${a}\``).join(", ") : "none";
239
- const nextDescription = schema.next === "done"
240
- ? "flow complete — release cut and handoff signed off"
241
- : (() => {
242
- const nextSchema = stageSchema(schema.next);
243
- return nextSchema.skillDescription.charAt(0).toLowerCase() + nextSchema.skillDescription.slice(1);
244
- })();
245
- return `## Stage Completion Protocol
246
-
247
- Apply the **Shared Stage Completion Protocol** from \`.cclaw/skills/using-cclaw/SKILL.md\` with these parameters — do NOT re-derive the generic steps here.
248
-
249
- **Completion Parameters**
250
- - \`stage\` — \`${stage}\`
251
- - \`next\` — \`${nextStage}\` (${nextDescription})
252
- - \`gates\` — ${gateList}
253
- - \`artifact\` — \`${RUNTIME_ROOT}/artifacts/${schema.artifactFile}\`
254
- - \`mandatory\` — ${mandatoryList}
255
-
256
- When all required gates are satisfied and the artifact is written, execute the shared procedure (delegation pre-flight → flow-state update → artifact persistence → \`npx cclaw doctor\` → user handoff → STOP) using the parameters above. If any check fails, resolve the issue and re-run before proceeding.
257
-
258
- ## Resume Protocol
259
-
260
- When resuming this stage in a NEW session (artifact exists but not all of ${gateList} are passed), follow the **Shared Resume Protocol** in \`.cclaw/skills/using-cclaw/SKILL.md\` — confirm one gate at a time, update \`guardEvidence\` for each, never batch confirmations.
261
- `;
262
- }
263
- function stageTransitionAutoAdvanceBlock(schema) {
264
- return stageCompletionProtocol(schema);
265
- }
266
- function progressiveDisclosureBlock(stage) {
267
- const schema = stageSchema(stage);
268
- const stageSpecificRefs = {
269
- brainstorm: [
270
- "- `.cclaw/skills/learnings/SKILL.md` — to capture durable framing insights early"
271
- ],
272
- scope: [
273
- "- `.cclaw/skills/learnings/SKILL.md` — to persist rejected assumptions and constraints"
274
- ],
275
- design: [
276
- "- `.cclaw/skills/performance/SKILL.md` — when architectural choices carry perf trade-offs",
277
- "- `.cclaw/skills/security/SKILL.md` — when design choices touch auth/secrets/trust boundaries"
278
- ],
279
- spec: [
280
- "- `.cclaw/skills/docs/SKILL.md` — for API/contract wording quality and ADR-style decision capture",
281
- "- `.cclaw/skills/learnings/SKILL.md` — to preserve acceptance criteria traps and edge-case learnings"
282
- ],
283
- plan: [
284
- "- `.cclaw/skills/subagent-dev/SKILL.md` — for specialist delegation prompts by task slice",
285
- "- `.cclaw/skills/parallel-dispatch/SKILL.md` — for multi-agent review planning and reconciliation setup"
286
- ],
287
- tdd: [
288
- "- `.cclaw/skills/debugging/SKILL.md` — when RED behavior is unclear, flakes appear, or implementation fails tests",
289
- "- `.cclaw/skills/subagent-dev/SKILL.md` — for machine-only test-slice delegation",
290
- "- `.cclaw/skills/performance/SKILL.md` — when implementation choices impact latency/throughput"
291
- ],
292
- review: [
293
- "- `.cclaw/skills/security/SKILL.md` — mandatory lens for exploitable risk detection",
294
- "- `.cclaw/skills/parallel-dispatch/SKILL.md` — for review-army dispatch and reconciliation discipline"
295
- ],
296
- ship: [
297
- "- `.cclaw/skills/ci-cd/SKILL.md` — for release gates, pipeline health, and deployment guardrails",
298
- "- `.cclaw/skills/docs/SKILL.md` — for release docs, migration notes, and ADR/public API updates"
299
- ]
300
- };
301
- return `## Progressive Disclosure
302
-
303
- ### Depth
304
- - Primary stage procedure (this file): \`.cclaw/skills/${schema.skillFolder}/SKILL.md\`
305
- - Orchestrator contract (gate language and handoff): \`.cclaw/commands/${stage}.md\`
306
- - Artifact structure baseline: \`.cclaw/templates/${schema.artifactFile}\`
307
- - Runtime state truth source: \`.cclaw/state/flow-state.json\` + \`.cclaw/artifacts/\` + \`.cclaw/knowledge.jsonl\`
308
-
309
- ### See also
310
- - Meta routing and activation rules: \`.cclaw/skills/using-cclaw/SKILL.md\`
311
- - Session continuity and checkpoint behavior: \`.cclaw/skills/session/SKILL.md\`
312
- ${stageSpecificRefs[stage].join("\n")}
313
- - Progression command: \`/cc-next\` (reads flow-state, loads the next stage)
314
- `;
315
- }
316
- function verificationBlock(stage) {
317
- if (!VERIFICATION_STAGES.includes(stage))
318
- return "";
319
- return `## Verification Before Completion
320
-
321
- **Iron law:** Do not claim this stage is complete without fresh verification evidence from THIS message.
322
-
323
- ### Gate Function
324
- For every completion claim, follow this sequence:
325
- 1. **Identify** the verification command (test suite, build, lint, type-check).
326
- 2. **Run** the full command — not a subset, not a cached result.
327
- 3. **Read** the complete output — do not summarize without reading.
328
- 4. **Verify** the output matches the expected success state.
329
- 5. **Only then** make the completion claim with the evidence.
330
-
331
- ### Evidence Requirements
332
- | Claim | Requires | NOT Sufficient |
333
- |---|---|---|
334
- | "Tests pass" | Fresh test run output showing all pass | "I believe tests pass" or prior run |
335
- | "Build succeeds" | Fresh build output with exit code 0 | "Should build fine" |
336
- | "No lint errors" | Fresh linter output | "I didn't introduce any" |
337
- | "Bug is fixed" | Regression test: remove fix → fails, restore → passes | "The fix looks correct" |
338
-
339
- ### Forbidden Language
340
- Do not use these phrases before verification:
341
- - "Everything works," "All good," "Done," "Successfully completed"
342
- - "Should be fine," "I'm confident that," "This will work"
343
- - "Tests should pass," "The build should succeed"
344
-
345
- ### Regression Test Pattern
346
- When fixing a bug:
347
- 1. Write a test that reproduces the bug → verify it **fails**.
348
- 2. Apply the fix → verify the test **passes**.
349
- 3. Revert the fix → verify the test **fails again**.
350
- 4. Restore the fix → verify the full suite passes.
351
- `;
352
- }
353
288
  export function stageSkillFolder(stage) {
354
289
  return stageSchema(stage).skillFolder;
355
290
  }
356
- function quickStartBlock(stage) {
357
- const schema = stageSchema(stage);
358
- const topGates = schema.requiredGates.slice(0, 3).map((g) => `\`${g.id}\``).join(", ");
359
- return `## Quick Start (minimum compliance)
360
-
361
- > **Even if you read nothing else, do these 3 things:**
362
- > 1. Obey the HARD-GATE below — violating it invalidates the entire stage.
363
- > 2. Complete every checklist step in order and write the artifact to \`.cclaw/artifacts/${schema.artifactFile}\`.
364
- > 3. Do not claim completion without satisfying gates: ${topGates}${schema.requiredGates.length > 3 ? ` (+${schema.requiredGates.length - 3} more)` : ""}.
365
- >
366
- > **After this stage:** update \`flow-state.json\` and tell the user to run \`/cc-next\`.
367
- `;
368
- }
369
291
  export function stageSkillMarkdown(stage) {
370
292
  const schema = stageSchema(stage);
371
293
  const gateList = schema.requiredGates
@@ -377,6 +299,7 @@ export function stageSkillMarkdown(stage) {
377
299
  const checklistItems = schema.checklist
378
300
  .map((item, i) => `${i + 1}. ${item}`)
379
301
  .join("\n");
302
+ const stageRefs = stageSpecificSeeAlso(stage);
380
303
  return `---
381
304
  name: ${schema.skillName}
382
305
  description: "${schema.skillDescription}"
@@ -393,6 +316,7 @@ If you are about to violate the Iron Law, STOP. No amount of urgency, partial pr
393
316
  </EXTREMELY-IMPORTANT>
394
317
 
395
318
  ${quickStartBlock(stage)}
319
+
396
320
  ## Overview
397
321
  ${schema.purpose}
398
322
 
@@ -408,6 +332,8 @@ ${schema.requiredContext.length > 0 ? schema.requiredContext.map((item) => `- ${
408
332
 
409
333
  ${contextLoadingBlock(stage)}
410
334
  ${autoSubagentDispatchBlock(stage)}
335
+ ${researchPlaybooksBlock(stage)}
336
+
411
337
  ## Outputs
412
338
  ${schema.outputs.map((item) => `- ${item}`).join("\n")}
413
339
 
@@ -423,12 +349,12 @@ ${checklistItems}
423
349
  ${stageGoodBadExamples(stage)}
424
350
  ${stageDomainExamples(stage)}
425
351
  ${stageExamples(stage)}
426
- ${namedAntiPatternBlock(stage)}
427
- ${cognitivePatternsList(stage)}
352
+
428
353
  ## Interaction Protocol
429
354
  ${schema.interactionProtocol.map((item, i) => `${i + 1}. ${item}`).join("\n")}
430
355
 
431
- **See \`.cclaw/skills/using-cclaw/SKILL.md\` "Shared Decision + Tool-Use Protocol"** for the full AskUserQuestion format, error/retry budget, and the 3-attempt escalation rule. Do not duplicate those rules here — apply them verbatim.
356
+ Shared decision/ask-user protocol:
357
+ \`${DECISION_PROTOCOL_PATH}\`
432
358
 
433
359
  ${waveExecutionModeBlock(stage)}
434
360
  ## Required Gates
@@ -444,60 +370,24 @@ ${reviewSectionsBlock(stage)}
444
370
  ${verificationBlock(stage)}
445
371
  ${crossStageTraceBlock(stage)}
446
372
  ${artifactValidationBlock(stage)}
447
- ${visualCommunicationBlock(stage)}
448
- ${decisionRecordBlock(stage)}
449
- ## Common Rationalizations
450
- ${rationalizationTable(stage)}
451
373
 
452
374
  ## Anti-Patterns & Red Flags
375
+ ${mergedAntiPatterns(schema)}
453
376
 
454
- > One consolidated list of observable failure modes for this stage. Mix of
455
- > behavioural anti-patterns (things you might do wrong) and red-flag
456
- > signals (things you might notice going wrong). Dedup-merged so no item
457
- > appears twice.
458
-
459
- ${(() => {
460
- const merged = [];
461
- const seen = new Set();
462
- for (const item of [...schema.antiPatterns, ...schema.blockers, ...schema.redFlags]) {
463
- const key = item.trim().toLowerCase();
464
- if (seen.has(key))
465
- continue;
466
- seen.add(key);
467
- merged.push(item);
468
- }
469
- return merged.map((item) => `- ${item}`).join("\n");
470
- })()}
471
-
472
- ${completionStatusBlock(stage)}
473
377
  ## Verification
474
378
  ${schema.exitCriteria.map((item) => `- [ ] ${item}`).join("\n")}
475
379
 
476
- ${stageTransitionAutoAdvanceBlock(schema)}
477
- ${progressiveDisclosureBlock(stage)}
478
- ${selfImprovementBlock(stage)}
479
- ## Handoff
480
-
481
- Before closing the stage, announce the handoff explicitly so the user can steer. Use the **Handoff Menu** below; never auto-advance silently, even when \`/cc-next\` is available.
482
-
483
- ### Handoff Menu
484
-
485
- Offer the user a lettered choice at the end of the stage (use \`AskUserQuestion\` / \`AskQuestion\` when the harness supports it, otherwise plain lettered text):
486
-
487
- - **A) Advance** — run \`/cc-next\` and continue to the next stage. Default when all gates are satisfied and there are no open concerns.
488
- - **B) Revise this stage** — stay on the current stage; apply the user's feedback, then re-ask for handoff.
489
- - **C) Pause / park** — save state; stop here. Useful when the user wants to share the artifact with a human reviewer before continuing.
490
- - **D) Rewind** — move to a prior stage (user names which). Use when downstream work revealed that an earlier stage was wrong.
491
- - **E) Abandon** — mark the flow as cancelled; no further stages will run. Artifacts remain on disk.
492
-
493
- Recommendation rules:
494
- - If all required gates are satisfied AND the stage's completion status is \`DONE\`, recommend **A (Advance)**.
495
- - If completion status is \`DONE_WITH_CONCERNS\`, recommend **B (Revise)** and name the concern.
496
- - If completion status is \`BLOCKED\`, recommend **B (Revise)** or **C (Pause)** depending on whether the blocker is internal or external.
497
-
498
- Reference data for the user:
499
- - Next command: \`/cc-next\` (loads whatever stage is current in flow-state)
500
- - Required artifact: \`.cclaw/artifacts/${schema.artifactFile}\`
501
- - Stage stays blocked if any required gate is unsatisfied
380
+ ${completionParametersBlock(schema)}
381
+ ## Shared Stage Guidance
382
+ See:
383
+ - \`${STAGE_COMMON_GUIDANCE_REL_PATH}\`
384
+ - \`${DECISION_PROTOCOL_PATH}\`
385
+ - \`${COMPLETION_PROTOCOL_PATH}\`
386
+
387
+ ## See Also
388
+ - \`${RUNTIME_ROOT}/skills/using-cclaw/SKILL.md\`
389
+ - \`${RUNTIME_ROOT}/skills/session/SKILL.md\`
390
+ ${stageRefs.join("\n")}
391
+ - \`${RUNTIME_ROOT}/commands/${stage}.md\`
502
392
  `;
503
393
  }
@@ -0,0 +1,2 @@
1
+ export declare const STAGE_COMMON_GUIDANCE_REL_PATH = ".cclaw/references/stages/common-guidance.md";
2
+ export declare function stageCommonGuidanceMarkdown(): string;