@drafthq/draft 2.7.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 (158) hide show
  1. package/.claude-plugin/marketplace.json +38 -0
  2. package/.claude-plugin/plugin.json +26 -0
  3. package/LICENSE +21 -0
  4. package/README.md +272 -0
  5. package/bin/README.md +49 -0
  6. package/cli/bin/draft.js +13 -0
  7. package/cli/src/cli.js +113 -0
  8. package/cli/src/hosts/claude-code.js +46 -0
  9. package/cli/src/hosts/codex.js +33 -0
  10. package/cli/src/hosts/cursor.js +50 -0
  11. package/cli/src/hosts/index.js +24 -0
  12. package/cli/src/hosts/opencode.js +39 -0
  13. package/cli/src/installer.js +61 -0
  14. package/cli/src/lib/fsx.js +34 -0
  15. package/cli/src/lib/graph.js +23 -0
  16. package/cli/src/lib/log.js +32 -0
  17. package/cli/src/lib/paths.js +14 -0
  18. package/core/agents/architect.md +338 -0
  19. package/core/agents/debugger.md +193 -0
  20. package/core/agents/ops.md +104 -0
  21. package/core/agents/planner.md +158 -0
  22. package/core/agents/rca.md +314 -0
  23. package/core/agents/reviewer.md +256 -0
  24. package/core/agents/writer.md +110 -0
  25. package/core/guardrails/README.md +4 -0
  26. package/core/guardrails/code-quality.md +4 -0
  27. package/core/guardrails/dependency-triage.md +4 -0
  28. package/core/guardrails/design-norms.md +4 -0
  29. package/core/guardrails/language-standards.md +4 -0
  30. package/core/guardrails/review-checks.md +4 -0
  31. package/core/guardrails/secure-patterns.md +4 -0
  32. package/core/guardrails/security.md +4 -0
  33. package/core/guardrails.md +22 -0
  34. package/core/knowledge-base.md +127 -0
  35. package/core/methodology.md +1221 -0
  36. package/core/shared/condensation.md +224 -0
  37. package/core/shared/context-verify.md +44 -0
  38. package/core/shared/cross-skill-dispatch.md +127 -0
  39. package/core/shared/discovery-schema.md +75 -0
  40. package/core/shared/draft-context-loading.md +282 -0
  41. package/core/shared/git-report-metadata.md +106 -0
  42. package/core/shared/graph-query.md +239 -0
  43. package/core/shared/graph-usage-report.md +22 -0
  44. package/core/shared/jira-sync.md +170 -0
  45. package/core/shared/parallel-analysis.md +386 -0
  46. package/core/shared/parallel-fanout.md +10 -0
  47. package/core/shared/pattern-learning.md +146 -0
  48. package/core/shared/red-flags.md +58 -0
  49. package/core/shared/template-contract.md +22 -0
  50. package/core/shared/template-hygiene.md +10 -0
  51. package/core/shared/tool-resolver.md +10 -0
  52. package/core/shared/vcs-commands.md +97 -0
  53. package/core/shared/verification-gates.md +47 -0
  54. package/core/templates/CHANGELOG.md +70 -0
  55. package/core/templates/ai-context-export.md +8 -0
  56. package/core/templates/ai-context.md +270 -0
  57. package/core/templates/ai-profile.md +41 -0
  58. package/core/templates/architecture.md +203 -0
  59. package/core/templates/dependency-graph.md +103 -0
  60. package/core/templates/discovery.md +79 -0
  61. package/core/templates/guardrails.md +143 -0
  62. package/core/templates/hld.md +327 -0
  63. package/core/templates/intake-questions.md +403 -0
  64. package/core/templates/jira.md +119 -0
  65. package/core/templates/lld.md +283 -0
  66. package/core/templates/metadata.json +66 -0
  67. package/core/templates/plan.md +130 -0
  68. package/core/templates/product.md +110 -0
  69. package/core/templates/rca.md +86 -0
  70. package/core/templates/root-architecture.md +127 -0
  71. package/core/templates/root-product.md +53 -0
  72. package/core/templates/root-tech-stack.md +117 -0
  73. package/core/templates/service-index.md +55 -0
  74. package/core/templates/session-summary.md +8 -0
  75. package/core/templates/spec.md +165 -0
  76. package/core/templates/tech-matrix.md +101 -0
  77. package/core/templates/tech-stack.md +169 -0
  78. package/core/templates/track-architecture.md +311 -0
  79. package/core/templates/workflow.md +187 -0
  80. package/integrations/agents/AGENTS.md +24384 -0
  81. package/integrations/copilot/.github/copilot-instructions.md +24384 -0
  82. package/integrations/gemini/.gemini.md +26 -0
  83. package/package.json +53 -0
  84. package/scripts/fetch-memory-engine.sh +116 -0
  85. package/scripts/lib.sh +256 -0
  86. package/scripts/tools/_lib.sh +220 -0
  87. package/scripts/tools/adr-index.sh +117 -0
  88. package/scripts/tools/check-graph-usage-report.sh +95 -0
  89. package/scripts/tools/check-scope-conflicts.sh +139 -0
  90. package/scripts/tools/check-skill-line-caps.sh +115 -0
  91. package/scripts/tools/check-template-noop.sh +87 -0
  92. package/scripts/tools/check-track-hygiene.sh +230 -0
  93. package/scripts/tools/classify-files.sh +231 -0
  94. package/scripts/tools/cycle-detect.sh +75 -0
  95. package/scripts/tools/detect-test-framework.sh +135 -0
  96. package/scripts/tools/diff-templates-vs-tracks.sh +176 -0
  97. package/scripts/tools/emit-skill-metrics.sh +71 -0
  98. package/scripts/tools/fix-whitespace.sh +192 -0
  99. package/scripts/tools/freshness-check.sh +143 -0
  100. package/scripts/tools/git-metadata.sh +203 -0
  101. package/scripts/tools/graph-callers.sh +74 -0
  102. package/scripts/tools/graph-impact.sh +93 -0
  103. package/scripts/tools/graph-snapshot.sh +102 -0
  104. package/scripts/tools/hotspot-rank.sh +75 -0
  105. package/scripts/tools/manage-symlinks.sh +85 -0
  106. package/scripts/tools/mermaid-from-graph.sh +92 -0
  107. package/scripts/tools/migrate-track-frontmatter.sh +241 -0
  108. package/scripts/tools/parse-git-log.sh +135 -0
  109. package/scripts/tools/parse-reports.sh +114 -0
  110. package/scripts/tools/render-track.sh +145 -0
  111. package/scripts/tools/run-coverage.sh +153 -0
  112. package/scripts/tools/scan-markers.sh +144 -0
  113. package/scripts/tools/skill-caps.conf +24 -0
  114. package/scripts/tools/validate-frontmatter.sh +125 -0
  115. package/scripts/tools/verify-citations.sh +250 -0
  116. package/scripts/tools/verify-doc-anchors.sh +204 -0
  117. package/scripts/tools/verify-graph-binary.sh +154 -0
  118. package/skills/GRAPH.md +332 -0
  119. package/skills/adr/SKILL.md +374 -0
  120. package/skills/assist-review/SKILL.md +49 -0
  121. package/skills/bughunt/SKILL.md +668 -0
  122. package/skills/bughunt/references/regression-tests.md +399 -0
  123. package/skills/change/SKILL.md +267 -0
  124. package/skills/coverage/SKILL.md +336 -0
  125. package/skills/debug/SKILL.md +201 -0
  126. package/skills/decompose/SKILL.md +656 -0
  127. package/skills/deep-review/SKILL.md +326 -0
  128. package/skills/deploy-checklist/SKILL.md +254 -0
  129. package/skills/discover/SKILL.md +66 -0
  130. package/skills/docs/SKILL.md +42 -0
  131. package/skills/documentation/SKILL.md +197 -0
  132. package/skills/draft/SKILL.md +177 -0
  133. package/skills/draft/context-files.md +57 -0
  134. package/skills/draft/intent-mapping.md +37 -0
  135. package/skills/draft/quality-guide.md +51 -0
  136. package/skills/graph/SKILL.md +107 -0
  137. package/skills/impact/SKILL.md +86 -0
  138. package/skills/implement/SKILL.md +794 -0
  139. package/skills/incident-response/SKILL.md +245 -0
  140. package/skills/index/SKILL.md +848 -0
  141. package/skills/init/SKILL.md +1784 -0
  142. package/skills/init/references/architecture-spec.md +1259 -0
  143. package/skills/integrations/SKILL.md +53 -0
  144. package/skills/jira/SKILL.md +577 -0
  145. package/skills/jira/references/review.md +1322 -0
  146. package/skills/learn/SKILL.md +478 -0
  147. package/skills/new-track/SKILL.md +841 -0
  148. package/skills/ops/SKILL.md +57 -0
  149. package/skills/plan/SKILL.md +60 -0
  150. package/skills/quick-review/SKILL.md +216 -0
  151. package/skills/revert/SKILL.md +178 -0
  152. package/skills/review/SKILL.md +1114 -0
  153. package/skills/standup/SKILL.md +183 -0
  154. package/skills/status/SKILL.md +183 -0
  155. package/skills/tech-debt/SKILL.md +318 -0
  156. package/skills/testing-strategy/SKILL.md +195 -0
  157. package/skills/tour/SKILL.md +38 -0
  158. package/skills/upload/SKILL.md +117 -0
@@ -0,0 +1,794 @@
1
+ ---
2
+ name: implement
3
+ description: "Canonical implementation parent command. Executes the active track task-by-task using TDD and verification gates, and routes to status, coverage, or revert when the user asks for progress, measurement, or rollback explicitly."
4
+ ---
5
+
6
+ # Implement Track
7
+
8
+ Implement tasks from the active track's plan following the TDD workflow.
9
+
10
+ `/draft:implement` is the **canonical implementation parent**.
11
+
12
+ It owns the common execution loop and absorbs three adjacent commands when appropriate:
13
+
14
+ - `/draft:status`
15
+ - `/draft:coverage`
16
+ - `/draft:revert`
17
+
18
+ ## Red Flags - STOP if you're:
19
+
20
+ - Implementing without an approved spec and plan
21
+ - Skipping TDD cycle when workflow.md has TDD enabled
22
+ - Marking a task `[x]` without fresh verification evidence
23
+ - Batching multiple tasks into a single commit
24
+ - Proceeding past a phase boundary without running the three-stage review
25
+ - Writing production code before a failing test (when TDD is strict)
26
+ - Assuming a test passes without actually running it
27
+
28
+ **Verify before you mark complete. One task, one commit.**
29
+
30
+ ## Constraints
31
+
32
+ Draft skills are designed for single-agent, single-track execution. Do not run multiple Draft commands concurrently on the same track.
33
+
34
+ ---
35
+
36
+ ## Parent Contract
37
+
38
+ `/draft:implement` owns four execution jobs:
39
+
40
+ 1. **Build the next task** → baseline `/draft:implement`
41
+ 2. **Show current execution state** → `/draft:status`
42
+ 3. **Measure implementation completeness via coverage** → `/draft:coverage`
43
+ 4. **Undo implementation safely** → `/draft:revert`
44
+
45
+ Most developers should only need `/draft:implement` in the common path.
46
+
47
+ ### Explicit Child Modes
48
+
49
+ If the user invokes explicit child intent, route directly:
50
+
51
+ - `/draft:implement status` → follow `/draft:status`
52
+ - `/draft:implement coverage` → follow `/draft:coverage`
53
+ - `/draft:implement revert` → follow `/draft:revert`
54
+
55
+ Examples:
56
+
57
+ - `/draft:implement status`
58
+ - `/draft:implement coverage src/auth`
59
+ - `/draft:implement revert phase 2`
60
+
61
+ Explicit child mode always wins over the baseline implementation workflow.
62
+
63
+ ### Bare `/draft:implement`
64
+
65
+ Without an explicit child mode, `/draft:implement` should:
66
+
67
+ - continue the active track's next task
68
+ - surface blocked-task conditions immediately
69
+ - run review and verification gates at phase boundaries
70
+ - suggest or attach coverage only when the implementation state justifies it
71
+
72
+ Do not make the user remember `status` or `coverage` in the happy path just to continue safe execution.
73
+
74
+ ---
75
+
76
+ ## Step 0: Route Explicit Child Modes
77
+
78
+ Before loading implementation context, check whether the request is really:
79
+
80
+ - progress inspection
81
+ - coverage measurement
82
+ - rollback / undo
83
+
84
+ ### Route to `/draft:status`
85
+
86
+ Route when the user asks:
87
+
88
+ - `status`
89
+ - `progress`
90
+ - `what's left`
91
+ - `where am I`
92
+
93
+ ### Route to `/draft:coverage`
94
+
95
+ Route when the user asks:
96
+
97
+ - `coverage`
98
+ - `measure tests`
99
+ - `how much is covered`
100
+ - `coverage for <module/path>`
101
+
102
+ ### Route to `/draft:revert`
103
+
104
+ Route when the user asks:
105
+
106
+ - `revert`
107
+ - `undo`
108
+ - `roll back`
109
+ - `revert task`
110
+ - `revert phase`
111
+ - `revert track`
112
+
113
+ If one of these applies, route directly to the specialist workflow and stop this baseline implementation flow.
114
+
115
+ ---
116
+
117
+ ## Step 1: Load Context
118
+
119
+ 1. Find active track from `draft/tracks.md` (look for `[~] In Progress` or first `[ ]` track)
120
+ 2. Read the track's `spec.md` for requirements
121
+ 3. Read the track's `plan.md` for task list
122
+ 4. Read `draft/workflow.md` for TDD and commit preferences
123
+ 5. Read `draft/tech-stack.md` for technical context
124
+ 6. Read `draft/guardrails.md` (if exists) for hard guardrails and learned conventions
125
+ 7. **Check for architecture context:**
126
+ - Project-level: `draft/.ai-context.md` (preferred) or `draft/architecture.md` (graph-primary)
127
+ - Track-level design docs: `draft/tracks/<id>/hld.md` (+ `lld.md` when present)
128
+ - If relevant design context exists → **Enable architecture mode** (Story, Execution State, Skeletons)
129
+ - If neither exists → Standard TDD workflow
130
+ 8. **Load production invariants** (if `draft/.ai-context.md` exists):
131
+ - Read the `## INVARIANTS` section (and `## CONCURRENCY` if present)
132
+ - Identify which invariants reference files this task will modify (same file or same module)
133
+ - Keep matching invariants as **active constraints** for this task — these govern code generation, not just review
134
+ - If invariants reference lock ordering, fail-closed behavior, or data integrity rules: these are non-negotiable during implementation
135
+ 9. **Load graph context** (if `draft/graph/schema.yaml` exists):
136
+ - Read `draft/graph/hotspots.jsonl` — check if any files this task will modify appear as hotspots
137
+ - If modifying a hotspot file (high fanIn), warn: "This task modifies {file} (fanIn={N}). Changes here affect many downstream files. Consider running a graph impact query."
138
+ - Query `scripts/tools/graph-impact.sh`/`graph-callers.sh` for the module(s) being modified — gives file-level dependency context
139
+ - See `core/shared/graph-query.md` for on-demand query subroutines (callers, impact)
140
+ 10. Update the track's entry in `draft/tracks.md` from `[ ]` to `[~]` In Progress
141
+
142
+ If no active track found:
143
+ - Tell user: "No active track found. Run `/draft:plan` to create or resume planned work."
144
+
145
+ **Architecture / Design Mode Activation:**
146
+ - Automatically enabled when `.ai-context.md`, graph-primary `architecture.md`, or track `hld.md`/`lld.md` exists.
147
+ - Project-level context from `/draft:init`.
148
+ - Track-level design docs from `/draft:decompose`.
149
+
150
+ ## Step 1.5: Readiness Gate (Fresh Start Only)
151
+
152
+ **Skip if:** Any task in `plan.md` is already `[x]` — the track is in progress, this check has already passed.
153
+
154
+ Run once, before the first task of a new track:
155
+
156
+ ### AC Coverage Check
157
+
158
+ For each acceptance criterion in `spec.md`:
159
+ - Verify at least one task in `plan.md` references or addresses it
160
+ - If an AC has no corresponding task, flag it: "⚠️ AC: '[criterion]' has no task in plan.md"
161
+
162
+ ### Sync Check (if `.ai-context.md` exists)
163
+
164
+ Compare the `synced_to_commit` values in the YAML frontmatter of `spec.md` and `plan.md`.
165
+ - **Skip if** either file has no YAML frontmatter or no `synced_to_commit` field (quick-mode tracks omit it).
166
+ - If they differ: "⚠️ Spec and plan were synced to different commits — verify they are still aligned."
167
+
168
+ ### Result
169
+
170
+ **Issues found:** List them, then ask:
171
+ ```
172
+ Readiness issues found (see above). Proceed anyway or update first? [proceed/update]
173
+ ```
174
+ - `proceed` → add a `## Notes` entry in `plan.md` listing the issues, then continue to Step 2
175
+ - `update` → stop here and let the user refine spec or plan before re-running
176
+
177
+ **No issues:** Print `Readiness check passed.` and continue to Step 2.
178
+
179
+ ## Step 1.7: Testing Strategy Loading
180
+
181
+ Before starting TDD cycle for the first task:
182
+
183
+ 1. Check for testing strategy:
184
+ - Track-level: `draft/tracks/<id>/testing-strategy.md`
185
+ - Project-level: `draft/testing-strategy.md` or `draft/testing-strategy-latest.md`
186
+ 2. If found: load coverage targets, test boundaries, and strategy into TDD context
187
+ 3. If not found and TDD is enabled: suggest "Run `/draft:testing-strategy` to define test approach"
188
+
189
+ ### Bug Track Test Guardrail
190
+
191
+ If track type is `bugfix` (from metadata.json):
192
+ ```
193
+ BEFORE writing any test file:
194
+ ASK: "This is a bug fix track. Want me to write tests as part of the fix? [Y/n]"
195
+ If declined: skip TDD cycle, note in plan.md: "Tests: developer-handled"
196
+ ```
197
+
198
+ ## Step 2: Find Next Task
199
+
200
+ Scan `plan.md` for the first uncompleted task:
201
+ - `[ ]` = Pending (pick this one)
202
+ - `[~]` = In Progress (resume this one)
203
+ - `[x]` = Completed (skip)
204
+ - `[!]` = Blocked (skip - requires manual intervention)
205
+
206
+ **IMPORTANT:** If blocked task found, notify user:
207
+ - "Task [task description] is marked `[!]` Blocked"
208
+ - Show the blocked task details and recovery message
209
+ - "Resolve the blockage manually before continuing implementation"
210
+ - Do NOT attempt to implement blocked tasks
211
+
212
+ If resuming `[~]` task, check for partial work.
213
+
214
+ ### Implementation-State Escalations
215
+
216
+ After choosing the next task, check whether baseline implementation should remain in build mode or whether it should surface another execution helper.
217
+
218
+ #### Escalate to Status-Style Summary
219
+
220
+ If the track has:
221
+
222
+ - many blocked tasks
223
+ - no obvious next runnable task
224
+ - conflicting in-progress markers
225
+
226
+ then present a concise status summary before proceeding so the user can see the execution state clearly.
227
+
228
+ This does **not** require fully routing to `/draft:status`; it is an implementation-owned checkpoint.
229
+
230
+ #### Escalate to Coverage Guidance
231
+
232
+ If the implementation is at one of these points:
233
+
234
+ - phase just completed
235
+ - track just completed
236
+ - high-risk module just changed and tests exist but coverage is unknown
237
+
238
+ then `/draft:implement` should suggest coverage or auto-attach it when the workflow and user intent imply measurement is expected.
239
+
240
+ Examples:
241
+
242
+ ```text
243
+ Implementation checkpoint:
244
+ - Phase 2 is complete
245
+ - Next recommended action: /draft:implement coverage
246
+ - Reason: high-risk auth code changed and coverage has not been measured for this phase
247
+ ```
248
+
249
+ #### Escalate to Revert Guidance
250
+
251
+ If the selected task is blocked because earlier implementation appears invalid, conflicting, or partially reverted, `/draft:implement` should recommend `/draft:implement revert` explicitly rather than pushing forward blindly.
252
+
253
+ ## Step 2.5: Write Story (Architecture Mode Only)
254
+
255
+ **Activation:** Only runs when `.ai-context.md`, graph-primary `architecture.md`, or track `hld.md`/`lld.md` exists.
256
+
257
+ When the next task involves creating or substantially modifying a code file:
258
+
259
+ 1. **Check if file already has a Story comment** - If yes, skip this step
260
+ 2. **Skip for trivial tasks** - Config files, type definitions, simple one-liners
261
+ 3. **Write a natural-language algorithm description** as a comment block at the top of the target file
262
+
263
+ ### Story Format
264
+
265
+ ```
266
+ // Story: [Module/File Name]
267
+ //
268
+ // Input: [what this module/function receives]
269
+ // Process:
270
+ // 1. [first algorithmic step]
271
+ // 2. [second algorithmic step]
272
+ // 3. [third algorithmic step]
273
+ // Output: [what this module/function produces]
274
+ //
275
+ // Dependencies: [what this module relies on]
276
+ // Side effects: [any mutations, I/O, or external calls]
277
+ ```
278
+
279
+ Adapt comment syntax to the language (`#` for Python, `/* */` for CSS, etc.).
280
+
281
+ ### CHECKPOINT (MANDATORY)
282
+
283
+ **STOP.** Present the Story to the developer for review.
284
+
285
+ - Developer may refine, modify, or rewrite the Story
286
+ - **Do NOT proceed to execution state or implementation until Story is approved**
287
+ - Developer can say "skip" to bypass this checkpoint for the current task
288
+
289
+ See `core/agents/architect.md` for story writing guidelines.
290
+
291
+ ---
292
+
293
+ ## Step 3: Execute Task
294
+
295
+ ### Step 3.0: Design Before Code (Architecture Mode Only)
296
+
297
+ **Activation:** Only runs when `.ai-context.md`, graph-primary `architecture.md`, or track `hld.md`/`lld.md` exists.
298
+ **Skip for trivial tasks** - Config updates, type-only changes, single-function tasks where the design is obvious.
299
+
300
+ #### 3.0a. Execution State Design
301
+
302
+ Study the control flow for the task and propose intermediate state variables:
303
+
304
+ 1. Read the Story (from Step 2.5) to understand the Input -> Output path
305
+ 2. Study similar patterns in the existing codebase
306
+ 3. **Check `.ai-context.md` Data Lifecycle** — Align execution state with documented state machines (valid states/transitions), storage topology (which tier data targets), and data transformation chain (shape changes at boundaries)
307
+ 4. **Check `.ai-context.md` Critical Paths** — Identify where this task sits in documented write/read/async paths. Note consistency boundaries and failure recovery expectations.
308
+ 5. Propose execution state: input state, intermediate state, output state, error state
309
+
310
+ Present in this format:
311
+ ```
312
+ EXECUTION STATE: [Task/Module Name]
313
+ ─────────────────────────────────────────────────────────
314
+ Input State:
315
+ - variableName: Type — purpose
316
+
317
+ Intermediate State:
318
+ - variableName: Type — purpose
319
+
320
+ Output State:
321
+ - variableName: Type — purpose
322
+
323
+ Error State:
324
+ - variableName: Type — purpose
325
+ ```
326
+
327
+ **CHECKPOINT (MANDATORY):** Present execution state to developer. Wait for approval. Developer may add, remove, or modify state variables. Developer can say "skip" to bypass.
328
+
329
+ #### 3.0b. Function Skeleton Generation
330
+
331
+ Generate function/method stubs based on the approved execution state:
332
+
333
+ 1. Create stubs with complete signatures (all parameters, return types)
334
+ 2. Include a one-line docstring describing purpose and when it's called
335
+ 3. No implementation bodies — use `// TODO`, `pass`, `unimplemented!()`, etc.
336
+ 4. Order functions to match control flow sequence
337
+ 5. Follow naming conventions from `tech-stack.md`
338
+
339
+ **CHECKPOINT (MANDATORY):** Present skeletons to developer. Wait for approval. Developer may rename functions, change signatures, add/remove methods. Developer can say "skip" to bypass.
340
+
341
+ See `core/agents/architect.md` for execution state and skeleton guidelines.
342
+
343
+ ---
344
+
345
+ ### Step 3.0c: Production Robustness Patterns (REQUIRED)
346
+
347
+ **Applies to all code generation** — architecture mode or not. These patterns are generation directives, not a post-hoc checklist. Apply them **while writing code**, not after.
348
+
349
+ When your implementation hits any of these triggers, use the corresponding pattern. Do not write code that violates these and plan to "fix it later."
350
+
351
+ #### Atomicity
352
+
353
+ | Trigger | Required Pattern |
354
+ |---------|-----------------|
355
+ | Multi-step state mutation (DB + memory, multiple records) | Wrap in transaction or try/finally with rollback on failure |
356
+ | File write | Write to temp file + atomic rename to target path. Never write directly to the target. |
357
+ | DB write paired with in-memory state update | DB-first: persist to DB, update memory only on DB success. Never update memory optimistically. |
358
+ | Resource acquisition (locks, file handles, connections, capital) | Release in `finally` / `defer` / RAII — never rely on happy-path-only cleanup |
359
+
360
+ #### Isolation
361
+
362
+ | Trigger | Required Pattern |
363
+ |---------|-----------------|
364
+ | Method mutates shared/instance state | Acquire the class's or module's existing lock before mutation |
365
+ | Lifecycle operations (start/stop/reset/reconnect) | Use a dedicated lifecycle lock, separate from data locks |
366
+ | Returning internal state to callers | Return a deep copy or frozen snapshot — never a mutable reference to internal state |
367
+ | Acquiring a second lock while holding one | Follow documented lock ordering. If no ordering exists, do not nest locks — restructure to acquire sequentially. |
368
+ | DB I/O while holding a state lock | Move DB I/O outside the lock scope. Lock only the in-memory mutation, not the I/O. |
369
+
370
+ #### Durability
371
+
372
+ | Trigger | Required Pattern |
373
+ |---------|-----------------|
374
+ | Critical state that must survive crashes | Ensure state is recoverable from DB/disk alone — no reliance on in-memory-only state for recovery |
375
+ | Async DB write (fire-and-forget) | Await the write. Check return value or propagate exceptions. No fire-and-forget on data persistence. |
376
+ | Event log / audit trail / fill history | Use append-only pattern where specified by architecture |
377
+
378
+ #### Defensive Boundaries
379
+
380
+ | Trigger | Required Pattern |
381
+ |---------|-----------------|
382
+ | External numeric data used in arithmetic | Guard with `isFinite()` / `isnan()` / equivalent before any calculation |
383
+ | External API/webhook response consumed | Validate expected fields exist and have correct types before accessing nested properties |
384
+ | SQL query with dynamic values | Parameterized queries only — zero string interpolation for values |
385
+ | Dynamic column names, table names, or identifiers in SQL | Validate against an explicit allowlist — never pass user-controlled strings as identifiers |
386
+
387
+ #### Idempotency
388
+
389
+ | Trigger | Required Pattern |
390
+ |---------|-----------------|
391
+ | Operation that may be retried (network calls, queue consumers, webhook handlers) | Use a dedup key (UUID, request ID, fill ID) — check-before-write or upsert |
392
+ | State transition (status changes, lifecycle events) | Validate the transition is legal from the current state. Reject terminal→terminal transitions. |
393
+ | Alert / notification emission | Dedup on (alert_type, entity_id, time_window) to prevent re-firing on retries |
394
+
395
+ #### Fail-Closed
396
+
397
+ | Trigger | Required Pattern |
398
+ |---------|-----------------|
399
+ | Error path or exception handler that determines access/action | Default to the safe/restrictive/deny state — never default to permissive on error |
400
+ | Missing data, null, or undefined where a decision depends on it | Treat as deny/reject/skip — not as allow/proceed |
401
+ | Config or feature flag missing/unparseable | Use the restrictive default — system runs in safe mode, not open mode |
402
+
403
+ #### Resilience
404
+
405
+ | Trigger | Required Pattern |
406
+ |---------|-----------------|
407
+ | Any retry logic | Exponential backoff with jitter — never fixed-interval or immediate retries. Prevents retry storms. |
408
+ | Cache population under high concurrency | Cache stampede prevention: use probabilistic early expiration or request coalescing to prevent thundering herd |
409
+ | External dependency call (HTTP, RPC, DB to external service) | Circuit breaker pattern: track failure rate, open circuit on threshold, allow periodic probes to recover |
410
+ | Non-critical dependency failure | Graceful degradation: return cached/default/partial result rather than failing the entire request |
411
+
412
+ **Enforcement:** These patterns override convenience. If following a pattern makes the code more verbose, that's correct — the verbosity is the safety. If a pattern is genuinely N/A for the current task (e.g., no DB in a pure utility function), skip it — only apply relevant patterns.
413
+
414
+ **If project invariants were loaded in Step 1:** Cross-reference them here. Project-specific invariants (lock ordering, concurrency model, consistency boundaries) take precedence over these general patterns when they conflict.
415
+
416
+ ---
417
+
418
+ ### Step 3.1: Implement (TDD Workflow)
419
+
420
+ For each task, follow this workflow based on `workflow.md`. If skeletons were generated in Step 3.0b, fill them in using the TDD cycle below.
421
+
422
+ ### Characterization Testing (Refactoring Existing Code Without Tests)
423
+
424
+ When refactoring code that lacks tests, write characterization tests first to capture current behavior as a baseline. Identify seams (interfaces for test doubles, swappable imports), record actual outputs for representative inputs, then proceed with the TDD cycle for new behavior.
425
+
426
+ ### If TDD Enabled:
427
+
428
+ **Iron Law:** No production code without a failing test first.
429
+
430
+ **3a. RED - Write Failing Test**
431
+ ```
432
+ 1. Create/update test file as specified in task
433
+ 2. Write test that captures the requirement
434
+ 3. RUN test - VERIFY it FAILS (not syntax error, actual assertion failure)
435
+ 4. Show test output with failure
436
+ 5. Announce: "Test failing as expected: [failure message]"
437
+ ```
438
+
439
+ **Test Quality Checklist (REQUIRED for every test):**
440
+ - No shared mutable state between test cases — each test sets up its own state
441
+ - Assertion density: every test must have at least one meaningful assertion (not just `assertTrue(true)`)
442
+ - No logic in tests: no conditionals, loops, or try/catch in test code — tests should be trivially readable
443
+ - DAMP over DRY: prefer descriptive and meaningful test names and setup over deduplication
444
+ - Test behavior, not implementation: verify observable outcomes, not internal method calls
445
+ - One behavior per test: each test should verify exactly one logical behavior
446
+ - Reference: Google SWE Book Ch. 12, Google Testing Blog "Test Behavior, Not Implementation"
447
+
448
+ **Property-Based Testing Checkpoint:**
449
+ After writing example-based tests, consider property-based tests for pure functions (algebraic properties, round-trip serialization, sort invariants). Not mandatory — skip if properties are not obvious.
450
+
451
+ **3b. GREEN - Implement Minimum Code**
452
+ ```
453
+ 1. Write MINIMUM code to make test pass (no extras)
454
+ 2. RUN test - VERIFY it PASSES
455
+ 3. Show test output with pass
456
+ 4. Announce: "Test passing: [evidence]"
457
+ ```
458
+
459
+ **Observability Prompts (consider during implementation):**
460
+ Structured logging at decision points, metrics for latency-sensitive ops, tracing at service boundaries, error classification (transient vs permanent). Use engineering judgment — not mandatory for every task.
461
+
462
+ **Contract Testing Checkpoint (Service Boundaries Only):**
463
+ For new API endpoints or service-to-service interfaces, suggest consumer-driven contract tests. Skip for purely internal modules.
464
+
465
+ **3c. REFACTOR - Clean with Tests Green**
466
+ ```
467
+ 1. Review code for improvements
468
+ 2. Refactor while keeping tests green
469
+ 3. RUN all related tests after each change
470
+ 4. Show final test output
471
+ 5. Announce: "Refactoring complete, all tests passing: [evidence]"
472
+ ```
473
+
474
+ **Red Flags - STOP and restart the cycle if:**
475
+ - About to write code before test exists
476
+ - Test passes immediately (testing wrong thing)
477
+ - Thinking "just this once" or "too simple to test"
478
+ - Running tests mentally instead of actually executing
479
+
480
+ ### If TDD Not Enabled:
481
+
482
+ **3a. Implement**
483
+ ```
484
+ 1. Implement the task as specified
485
+ 2. Test manually or run existing tests
486
+ 3. Announce: "Implementation complete"
487
+ ```
488
+
489
+ ### Implementation Chunk Limit (Architecture Mode Only)
490
+
491
+ **Activation:** Only when `.ai-context.md` or `architecture.md` exists (track-level or project-level).
492
+
493
+ If the implementation diff for a task exceeds **~200 lines**:
494
+
495
+ 1. **STOP** after ~200 lines of implementation
496
+ 2. Present the chunk for developer review
497
+ 3. **CHECKPOINT (MANDATORY):** Wait for developer approval of the chunk
498
+ 4. Commit the approved chunk: `feat(<track_id>): <task description> (chunk N)`
499
+ 5. Continue with the next chunk
500
+ 6. Repeat until the task is fully implemented
501
+
502
+ This prevents large, unreviewable code drops. Each chunk should be a coherent, reviewable unit.
503
+
504
+ ---
505
+
506
+ ## Step 4: Update Progress & Commit
507
+
508
+ **Iron Law:** Every completed task gets its own commit. No batching. No skipping.
509
+
510
+ After completing each task:
511
+
512
+ 0. **Quick robustness scan** (30-second check before committing):
513
+ - Scan the code you just wrote against the Step 3.0c triggers
514
+ - If any trigger is present but the pattern wasn't applied: fix it now
515
+ - This is a rapid pattern-match, not a full review — you should have applied these during generation, this catches anything missed
516
+
517
+ 1. Commit FIRST (REQUIRED - non-negotiable):
518
+ - Stage only files changed by this task (never `git add .`)
519
+ - `git add <specific files>`
520
+ - Verify staged changes exist before committing: `git diff --cached --quiet`. If nothing staged, skip the commit step.
521
+ - `git commit -m "type(<track_id>): task description"` (Conventional Commits — see `core/shared/vcs-commands.md`)
522
+ - If a Jira ticket is linked in `spec.md`, reference it in the commit body: `Refs: <JIRA_ID>`.
523
+ - Get commit SHA: `git rev-parse --short HEAD`
524
+ - Do NOT proceed to the next task without committing
525
+ - Do NOT batch multiple tasks into one commit
526
+
527
+ 2. Update `plan.md`:
528
+ - Change `[ ]` to `[x]` for the completed task
529
+ - Add the commit SHA next to the task: `[x] Task description (abc1234)`
530
+
531
+ 3. Update `metadata.json`:
532
+ - Increment `tasks.completed`
533
+ - Update `updated` timestamp
534
+
535
+ 4. **Verify state updates (CRITICAL):**
536
+ - Read back `plan.md` - confirm task marked `[x]` with SHA
537
+ - Read back `metadata.json` - confirm `tasks.completed` incremented
538
+ - If EITHER verification fails:
539
+ - Mark task as `[!]` Blocked in plan.md
540
+ - Add recovery message: "State update failed after commit <SHA>. Recovery: manually edit plan.md line X to mark `[x]`, update metadata.json tasks.completed to Y"
541
+ - HALT - require manual intervention before continuing
542
+
543
+ 5. If `.ai-context.md` or graph-primary `architecture.md` (or track hld/lld) exists:
544
+ - Update module status markers where applicable.
545
+ - Fill in Story placeholders with the approved story from Step 2.5
546
+ - If updating project-level `draft/.ai-context.md`: also update YAML frontmatter and run the Condensation Subroutine to keep it in sync.
547
+
548
+ ## Verification Gate (REQUIRED)
549
+
550
+ **Iron Law:** No completion claims without fresh verification evidence.
551
+
552
+ Before marking ANY task/phase/track complete:
553
+
554
+ 1. **IDENTIFY:** What command proves this claim? (test, build, lint)
555
+ 2. **RUN:** Execute the FULL command (fresh, complete run)
556
+ 3. **READ:** Full output, check exit code
557
+ 4. **VERIFY:** Does output confirm the claim?
558
+ - If **NO**: Keep task as `[~]`, state actual status
559
+ - If **YES**: Show evidence, then mark `[x]`
560
+
561
+ **Red Flags - STOP if you're thinking:**
562
+ - "Should pass", "probably works"
563
+ - Satisfaction before running verification
564
+ - About to mark `[x]` without fresh evidence from this session
565
+ - "I already tested earlier"
566
+ - "This is a simple change, no need to verify"
567
+
568
+ ---
569
+
570
+ ## Step 5: Phase Boundary Check
571
+
572
+ When all tasks in a phase are `[x]`:
573
+
574
+ 1. Announce: "Phase N complete. Running three-stage review."
575
+
576
+ ### Three-Stage Review (REQUIRED)
577
+
578
+ **Stage 1: Automated Validation**
579
+ - Fast static checks: architecture conformance, dead code, circular dependencies, performance anti-patterns. Review for common security anti-patterns (OWASP top 10). For automated checks, use language-specific tools (e.g., `npm audit` for JS, `bandit` for Python, `cargo audit` for Rust).
580
+ - **If critical issues found:** List them, return to implementation
581
+
582
+ **Stage 2: Spec Compliance** (only if Stage 1 passes)
583
+ - Load track's `spec.md`
584
+ - Verify all requirements for this phase are implemented
585
+ - Check acceptance criteria coverage
586
+ - **If gaps found:** List them, return to implementation
587
+
588
+ **Stage 3: Code Quality** (only if Stage 2 passes)
589
+ - Verify code follows project patterns (tech-stack.md)
590
+ - Check error handling is appropriate
591
+ - Verify tests cover real logic
592
+ - Classify issues: Critical (must fix) > Important (should fix) > Minor (note)
593
+
594
+ See `core/agents/reviewer.md` for detailed review process.
595
+
596
+ ### Quick Review Alternative
597
+
598
+ At phase boundaries, offer the lightweight alternative:
599
+ ```
600
+ "Phase {N} complete. Review options:
601
+ 1. Full three-stage review (recommended) — spec compliance + security + quality
602
+ 2. /draft:quick-review — lightweight 4-dimension check (faster)
603
+ Choose [1/2, default: 1]:"
604
+ ```
605
+ If quick-review chosen, invoke `/draft:quick-review` with the phase's changed files.
606
+
607
+ 2. Run verification steps from plan (tests, builds)
608
+ 3. Present review findings to user
609
+ 4. If review passes (no Critical issues):
610
+ - Update phase status in plan
611
+ - Update `metadata.json` phases.completed
612
+ - **Refresh blast-radius memory** (see "Impact Memory" subsection below)
613
+ - Proceed to next phase
614
+ 5. If Critical/Important issues found:
615
+ - Document issues in plan.md
616
+ - Fix before proceeding (don't skip)
617
+
618
+ ### Impact Memory (blast-radius snapshot)
619
+
620
+ After a phase passes review, refresh `metadata.json.impact` so future tracks can detect overlap with this work.
621
+
622
+ 1. **Compute touched files:** From `plan.md`, find the first commit SHA recorded for this track (earliest `[x]` line with `(<sha>)`). Run:
623
+ ```bash
624
+ git diff --name-only <first_sha>^..HEAD
625
+ ```
626
+ That is the `files_touched` list. Derive `modules_touched` as the unique top-level path segments (e.g. `auth/login.go` → `auth`).
627
+
628
+ 2. **Compute downstream blast radius (graph-aware, optional):** If `draft/graph/schema.yaml` exists, for each file in `files_touched` query:
629
+ ```bash
630
+ scripts/tools/graph-impact.sh --repo . --file <path>
631
+ ```
632
+ Aggregate across all files: `downstream_files` = total unique downstream files (deduped), `downstream_modules` = union of `affected_modules`, `max_depth` = max across queries, `by_category` = sum of each query's `by_category`. If the graph is absent, leave these fields as zeros / empty arrays — the snapshot still records the directly-touched files.
633
+
634
+ 3. **Write metadata.json** with the populated `impact` block and `computed_at` set to the current timestamp.
635
+
636
+ This snapshot is consumed by `/draft:new-track` to surface overlap warnings when a new track touches the same modules as a recently completed track.
637
+
638
+ ## Step 6: Track Completion
639
+
640
+ When all phases complete:
641
+
642
+ 1. **Run review (if enabled):**
643
+ - Read `draft/workflow.md` review configuration
644
+ - Check if auto-review enabled:
645
+ ```markdown
646
+ ## Review Settings
647
+ - [x] Auto-review at track completion
648
+ ```
649
+ - If enabled, run `/draft:review track <track_id>`
650
+ - Check review results:
651
+ - If block-on-failure enabled AND critical issues found → HALT, require fixes
652
+ - Otherwise, document warnings and continue
653
+
654
+ 2. Update `plan.md` status to `[x] Completed`
655
+ 3. Update `metadata.json` status to `"completed"`
656
+ 4. Update `draft/tracks.md`:
657
+ - Move from Active to Completed section
658
+ - Add completion date
659
+
660
+ 5. **Verify completion state consistency (CRITICAL):**
661
+ - Read back `plan.md` - confirm status `[x] Completed`
662
+ - Read back `metadata.json` - confirm status `"completed"`
663
+ - Read back `draft/tracks.md` - confirm track in Completed section with completion date
664
+ - If ANY file shows inconsistent state:
665
+ - ERROR: "Track completion partially failed"
666
+ - Report: "plan.md: <status>, metadata.json: <status>, tracks.md: <section>"
667
+ - Provide recovery: "Manually complete updates: [list specific edits needed]"
668
+ - Do NOT announce completion until all three files verified consistent
669
+
670
+ 6. Announce:
671
+ "Track <track_id> completed!
672
+
673
+ Summary:
674
+ - Phases: N/N
675
+ - Tasks: M/M
676
+ - Duration: [if tracked]
677
+
678
+ [If review ran:]
679
+ Review: PASS | PASS WITH NOTES | FAIL
680
+ Report: draft/tracks/<track_id>/review-report-latest.md
681
+
682
+ All acceptance criteria from spec.md should be verified.
683
+
684
+ Next: Run `/draft:status` to see project overview."
685
+
686
+ ## Error Handling
687
+
688
+ **If blocked:**
689
+ - Mark task as `[!]` Blocked
690
+ - Add reason in plan.md
691
+ - **REQUIRED:** Follow systematic debugging process (see `core/agents/debugger.md`)
692
+ 1. **Investigate** - Read errors, reproduce, trace (NO fixes yet)
693
+ 2. **Analyze** - Find similar working code, list differences
694
+ 3. **Hypothesize** - Single hypothesis, smallest test
695
+ 4. **Implement** - Regression test first, then fix
696
+ - Do NOT attempt random fixes
697
+ - Document root cause when found
698
+
699
+ **Recommended:** Instead of inline debugging, invoke `/draft:debug` skill for a structured session:
700
+ ```
701
+ "Task blocked: {description}. Run /draft:debug for structured investigation? [Y/n]"
702
+ ```
703
+ The debug skill provides: Reproduce → Isolate → Diagnose → Fix methodology with debug report output.
704
+
705
+ **If test fails unexpectedly:**
706
+ - Don't mark complete
707
+ - Follow systematic debugging process above
708
+ - Announce failure details with root cause analysis
709
+ - Show evidence when resolved
710
+
711
+ **If unsure about implementation:**
712
+ - Ask clarifying questions
713
+ - Reference spec.md for requirements
714
+ - Don't proceed with assumptions
715
+
716
+ ## Tech Debt Log
717
+
718
+ During implementation, track technical debt decisions in the track's plan.md:
719
+
720
+ When you encounter a shortcut, workaround, or known-imperfect solution during implementation:
721
+
722
+ 1. Add an entry to the `## Tech Debt` section at the bottom of plan.md
723
+ 2. Use this format:
724
+
725
+ ```markdown
726
+ ## Tech Debt
727
+
728
+ | ID | Location | Description | Severity | Payback Trigger |
729
+ |----|----------|-------------|----------|-----------------|
730
+ | TD-1 | `src/api/handler.ts:45` | Hardcoded timeout instead of config | Low | When adding config system |
731
+ | TD-2 | `src/auth/session.ts:12` | In-memory session store | Medium | Before horizontal scaling |
732
+ ```
733
+
734
+ **Severity levels:**
735
+ - **Low** — Cosmetic or minor maintainability issue
736
+ - **Medium** — Will cause problems at scale or in specific scenarios
737
+ - **High** — Actively impeding development or risking production issues
738
+
739
+ **Payback Trigger** — The condition or event that should trigger debt repayment (e.g., "before launch", "when adding feature X", "before scaling past N users").
740
+
741
+ Only log genuine debt — intentional shortcuts with known consequences. Not everything imperfect is debt.
742
+
743
+ ---
744
+
745
+ ## Progress Reporting
746
+
747
+ After each task, report:
748
+ ```
749
+ Task: [description]
750
+ Status: Complete
751
+ Phase Progress: N/M tasks
752
+ Overall: X% complete
753
+ ```
754
+
755
+ ---
756
+
757
+ ## Cross-Skill Dispatch
758
+
759
+ ### At Track Completion (Step 6)
760
+
761
+ After announcing track completion, suggest relevant follow-ups based on context:
762
+
763
+ **If track modifies production code:**
764
+ ```
765
+ "Track complete! Consider:
766
+ → /draft:deploy-checklist — Pre-deployment verification"
767
+ ```
768
+
769
+ **If track added new APIs/services/components:**
770
+ ```
771
+ → /draft:documentation — Update documentation for new components"
772
+ ```
773
+
774
+ **If implementation contains TODO/FIXME/HACK comments:**
775
+ ```
776
+ → /draft:tech-debt — Catalog any new technical debt introduced"
777
+ ```
778
+
779
+ **If new patterns or dependencies not in tech-stack.md:**
780
+ ```
781
+ → /draft:adr — Document this design decision"
782
+ ```
783
+
784
+ ### Jira Sync at Completion
785
+
786
+ If Jira ticket linked, sync via `core/shared/jira-sync.md`:
787
+ - Post comment: "[draft] implementation-complete: All {n} tasks done. Ready for review."
788
+
789
+ ### Bug Track with rca.md
790
+
791
+ If implementing a bug track and `draft/tracks/<id>/rca.md` exists:
792
+ - Load rca.md as context for the implementation
793
+ - Reference root cause, blast radius, and prevention items during fix
794
+ - After fix: update rca.md "Proposed Fix" section with actual fix details