@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,1784 @@
1
+ ---
2
+ name: init
3
+ description: "Initialize Draft project context for Context-Driven Development. Run once per project to create product.md, tech-stack.md, workflow.md, tracks.md, architecture.md (brownfield), .ai-context.md (derived), and .ai-profile.md (ultra-compact profile). Always performs deep analysis. Use when the user asks to 'init draft', 'set up Draft for this project', 'bootstrap context', or says 'start using Draft', 'I want to use Draft here'."
4
+ ---
5
+
6
+ # Draft Init
7
+
8
+ Initialize a Draft project for Context-Driven Development.
9
+
10
+ ## Red Flags - STOP if you're:
11
+
12
+ - Re-initializing a project that already has `draft/` without using `refresh` mode
13
+ - Skipping brownfield analysis for an existing codebase
14
+ - Rushing through product definition questions without probing for detail
15
+ - Auto-generating tech-stack.md without verifying detected dependencies
16
+ - Not presenting .ai-context.md for developer review before proceeding
17
+ - Overwriting existing tracks.md (this destroys track history)
18
+ - **Producing copy-paste module descriptions** — if 3+ modules share identical Responsibilities or description text, you have NOT analyzed the source files
19
+ - **Writing sequence diagrams under 15 lines** of Mermaid code — shallow diagrams without alt/opt blocks, payloads, and error paths are useless
20
+ - **Writing module deep-dives that ignore the graph or lack a workflow/state diagram** — the graph is ground truth; every significant module must have at least one synthesized diagram showing its primary flow or lifecycle. Prose volume without diagram fidelity is a failure.
21
+ - **Using "See X/" or "follow BUILD patterns"** as a substitute for reading actual source files and documenting real content
22
+ - **Ignoring detected high-quality existing agent context** (e.g. CLAUDE.md + docs/INVARIANTS.md written explicitly for AI agents) and regenerating large volumes of duplicative prose instead of a graph-primary overlay with strong cross-references and explicit Relationship section — this creates documentation debt and divergence risk in mature brownfield systems
23
+ - **Retaining any legacy 28-section or volume-oriented language** in generated architecture.md or in the reasoning process — the modern 10-section graph-primary template is the only accepted format.
24
+
25
+ **Initialize once, refresh to update. Never overwrite without confirmation.**
26
+
27
+ ---
28
+
29
+ ## MANDATORY SECTION CHECKLIST — architecture.md (Graph-Primary)
30
+
31
+ > **READ THIS BEFORE WRITING A SINGLE LINE OF architecture.md.**
32
+ > The document MUST use the EXACT modern graph-primary structure below. Freeform sections, renamed headings, or missing mandatory sections are FAILURES. This is the single forward-looking format — no legacy 28-section or volume-oriented material is accepted.
33
+
34
+ ```
35
+ ## 1. Executive Summary + Graph Health Dashboard
36
+ ## 2. Critical Invariants & Safety Rules (with provenance)
37
+ ## 3. Primary Control & Data Flows (Graph + Synthesis)
38
+ ## 4. Module & Dependency Map (Primarily Graph-Derived)
39
+ ## 5. Concurrency, Ownership & Isolation Model
40
+ ## 6. Error Handling & Failure Mode Catalog
41
+ ## 7. State & Data Truth Sources + Reconciliation
42
+ ## 8. Extension Points & Safe Mutation Patterns
43
+ ## 9. Graph Coverage Gaps & Known Limitations (MANDATORY)
44
+ ## 10. Relationship to Other Authoritative Documentation (MANDATORY on high/medium Context Audit)
45
+ ```
46
+
47
+ **Self-check before finalizing**: Confirm every one of the 10 sections above exists with the required fidelity declarations, provenance tags on claims, and (where applicable) Mermaid diagrams grounded in the graph. The Graph Health Dashboard + §9 Gaps + §10 Relationship are the highest-leverage sections for future agents.
48
+
49
+ > **If you are a subagent**: your prompt is a summary. The 10-section graph-primary structure above is authoritative. Use the exact headings. No legacy 28-section material is permitted.
50
+
51
+ ---
52
+
53
+ ## Graph Fidelity & Diagram-First Priority (MANDATORY)
54
+
55
+ The knowledge graph in `draft/graph/` (architecture.json with packages, languages, routes, and fan-in/out; hotspots.jsonl) is the **deterministic structural ground truth** for the system's actual architecture.
56
+
57
+ **You are running inside a powerful agentic coding environment** (Cursor, Claude Code, Copilot, Windsurf, etc.) that maintains its own rich, continuously updated index of the entire codebase. **Use that indexed knowledge aggressively** in addition to the explicit graph data and direct source reads. Your environment's index often captures higher-level intent, naming patterns, cross-file workflows, and architectural signals that the static graph may not fully express yet. Combine both sources:
58
+ - Graph = authoritative modules, edges, public surfaces, hotspots, call relationships.
59
+ - Your IDE/Agent index + full project understanding = semantic layer, workflow discovery, intent, and validation of the graph.
60
+
61
+ Cross-validate: if your index suggests a workflow, lifecycle, or design pattern that the graph does not yet surface, read the relevant source to confirm and then synthesize an accurate diagram that reflects reality.
62
+
63
+ **LLM role is faithful, high-fidelity synthesis** — not invention.
64
+
65
+ - Every structural claim must be consistent with the graph records. Contradiction = failure.
66
+ - **Diagrams are first-class deliverables.** For each major module or pipeline, produce at least one accurate Mermaid workflow, state, or sequence diagram.
67
+ - **Accuracy and correctness > document length.** Short, precise synthesis + good diagrams is superior to long prose or file lists.
68
+ - **Workflow and state focus.** Prioritize understanding primary control flows and state transitions so you can draw accurate diagrams.
69
+
70
+ This rule takes precedence over older volume-oriented language in this file.
71
+
72
+ ---
73
+
74
+ ## Standard File Metadata
75
+
76
+ **ALL files in `draft/` MUST include this metadata header.** This enables refresh tracking, sync verification, and traceability.
77
+
78
+ ### Gathering Git Information
79
+
80
+ Before generating any file, run these commands to gather metadata:
81
+
82
+ ```bash
83
+ # Project name (from manifest or directory)
84
+ basename "$(pwd)"
85
+
86
+ # Check if inside a git repository
87
+ if git rev-parse --is-inside-work-tree 2>/dev/null; then
88
+ # Git branch
89
+ git branch --show-current
90
+
91
+ # Git remote tracking branch
92
+ git rev-parse --abbrev-ref --symbolic-full-name @{upstream} 2>/dev/null || echo "none"
93
+
94
+ # Git commit SHA (full) — fails on repos with zero commits
95
+ git rev-parse HEAD 2>/dev/null || echo "none"
96
+
97
+ # Git commit SHA (short)
98
+ git rev-parse --short HEAD 2>/dev/null || echo "none"
99
+
100
+ # Git commit date
101
+ git log -1 --format="%ci" 2>/dev/null || echo "none"
102
+
103
+ # Git commit message (first line)
104
+ git log -1 --format="%s" 2>/dev/null || echo "none"
105
+
106
+ # Check for uncommitted changes
107
+ git status --porcelain | head -1
108
+ else
109
+ # Non-git project: use fallback values
110
+ echo "none" # branch
111
+ echo "none" # remote
112
+ echo "none" # commit
113
+ echo "none" # commit_short
114
+ echo "none" # commit_date
115
+ echo "none" # commit_message
116
+ # dirty: N/A for non-git projects
117
+ fi
118
+ ```
119
+
120
+ > **Non-git projects:** If the project is not a git repository, all git metadata fields will be set to `"none"` and `git.dirty` to `false`. Refresh mode's incremental sync (`synced_to_commit`) will not function — full re-analysis is required on each refresh.
121
+
122
+ ### Metadata Template
123
+
124
+ Insert this YAML frontmatter block at the **top of every draft/ file**:
125
+
126
+ ```yaml
127
+ ---
128
+ project: "{PROJECT_NAME}"
129
+ module: "{MODULE_NAME or 'root'}"
130
+ generated_by: "draft:{COMMAND_NAME}"
131
+ generated_at: "{ISO_TIMESTAMP}"
132
+ git:
133
+ branch: "{LOCAL_BRANCH}"
134
+ remote: "{REMOTE/BRANCH or 'none'}"
135
+ commit: "{FULL_SHA}"
136
+ commit_short: "{SHORT_SHA}"
137
+ commit_date: "{COMMIT_DATE}"
138
+ commit_message: "{FIRST_LINE_OF_COMMIT_MESSAGE}"
139
+ dirty: {true|false}
140
+ synced_to_commit: "{FULL_SHA}"
141
+ ---
142
+ ```
143
+
144
+ > **Note**: `generated_by` uses `draft:command` format (not `/draft:command`) for cross-platform compatibility.
145
+
146
+ ### Field Definitions
147
+
148
+ | Field | Description | Example |
149
+ |-------|-------------|---------|
150
+ | `project` | Project name from package.json/go.mod/Cargo.toml or directory name | `my-api-service` |
151
+ | `module` | Module name if in monorepo, otherwise `root` | `auth-service` |
152
+ | `generated_by` | The Draft command that created/updated this file | `draft:init` |
153
+ | `generated_at` | ISO 8601 timestamp when file was generated | `2024-01-15T14:30:00Z` |
154
+ | `git.branch` | Current local branch name | `main` |
155
+ | `git.remote` | Upstream tracking branch | `origin/main` |
156
+ | `git.commit` | Full SHA of HEAD when generated | `a1b2c3d4e5f6...` |
157
+ | `git.commit_short` | Short SHA (7 chars) | `a1b2c3d` |
158
+ | `git.commit_date` | Commit timestamp | `2024-01-15 10:00:00 -0500` |
159
+ | `git.commit_message` | First line of commit message | `feat: add user auth` |
160
+ | `git.dirty` | Were there uncommitted changes? | `true` or `false` |
161
+ | `synced_to_commit` | The commit SHA this doc is synchronized to | `a1b2c3d4e5f6...` |
162
+
163
+ ### Usage in Refresh
164
+
165
+ The `synced_to_commit` field is critical for incremental refresh:
166
+ - `/draft:init refresh` reads this field to find changed files since last sync
167
+ - If `git.dirty: true`, warn user that docs may not reflect committed state
168
+ - After refresh, update `synced_to_commit` to current HEAD
169
+
170
+ ### Example Header
171
+
172
+ ```yaml
173
+ ---
174
+ project: "payment-gateway"
175
+ module: "root"
176
+ generated_by: "draft:init"
177
+ generated_at: "2024-01-15T14:30:00Z"
178
+ git:
179
+ branch: "main"
180
+ remote: "origin/main"
181
+ commit: "a1b2c3d4e5f6789012345678901234567890abcd"
182
+ commit_short: "a1b2c3d"
183
+ commit_date: "2024-01-15 10:00:00 -0500"
184
+ commit_message: "feat: add stripe integration"
185
+ dirty: false
186
+ synced_to_commit: "a1b2c3d4e5f6789012345678901234567890abcd"
187
+ ---
188
+ ```
189
+
190
+ ---
191
+
192
+ ## Pre-Check
193
+
194
+ Check for arguments:
195
+ - `refresh`: Update existing context without full re-init
196
+ - `index`: Route to `/draft:index`
197
+ - `discover`: Route to `/draft:discover`
198
+
199
+ ### Route Explicit Modes Before Initialization
200
+
201
+ If the user explicitly invoked a specialist mode, route directly:
202
+
203
+ - `/draft:init index` → follow `/draft:index`
204
+ - `/draft:init discover` → follow `/draft:discover`
205
+
206
+ Explicit mode always wins. Do not perform standard initialization if an explicit mode is requested.
207
+
208
+ ### Standard Init Check
209
+
210
+ ```bash
211
+ ls draft/ 2>/dev/null
212
+ ```
213
+
214
+ If `draft/` exists with context files:
215
+ - Announce: "Project already initialized. Use `/draft:init refresh` to update context or `/draft:new-track` to create a feature."
216
+ - Stop here.
217
+
218
+ ### Atomic File Staging
219
+
220
+ To prevent partial initialization from leaving a broken `draft/` directory:
221
+
222
+ 1. **Stage all files** in a temporary directory (`draft.tmp/`) during init
223
+ 2. **On success**: `mv draft.tmp/ draft/` (atomic rename on POSIX)
224
+ 3. **On failure**: `rm -rf draft.tmp/` — no half-initialized state left behind
225
+
226
+ ```bash
227
+ # Before writing any files:
228
+ mkdir -p draft.tmp/tracks
229
+
230
+ # Write all files to draft.tmp/ instead of draft/
231
+ # ... (product.md, tech-stack.md, workflow.md, tracks.md, architecture.md, .ai-context.md)
232
+
233
+ # After all files are written and verified:
234
+ mv draft.tmp/ draft/
235
+ ```
236
+
237
+ > **Forced re-init:** If `draft/` exists and the user explicitly requests a fresh init (not refresh), confirm with user before removing the existing `draft/` directory.
238
+
239
+ ### Monorepo Detection
240
+
241
+ Check for monorepo indicators:
242
+ - Multiple `package.json` / `go.mod` / `Cargo.toml` in child directories
243
+ - `lerna.json`, `pnpm-workspace.yaml`, `nx.json`, or `turbo.json` at root
244
+ - `packages/`, `apps/`, `services/` directories with independent manifests
245
+
246
+ If monorepo detected:
247
+ - Announce: "Detected monorepo structure. Consider using `/draft:init index` at root level to aggregate service context, or run `/draft:init` within individual service directories."
248
+ - Ask user to confirm: initialize here (single service) or abort (use /draft:init index instead)
249
+
250
+ ### Migration Detection
251
+
252
+ If `draft/architecture.md` exists WITHOUT `draft/.ai-context.md`:
253
+ - Announce: "Detected architecture.md without .ai-context.md. Would you like to generate .ai-context.md? This will condense your existing architecture.md into a token-optimized AI context file."
254
+ - If user accepts: Run the Condensation Subroutine to derive `.ai-context.md` from existing `architecture.md`
255
+ - If user declines: Continue without .ai-context.md
256
+
257
+ If `draft/.ai-context.md` exists WITHOUT `draft/architecture.md`:
258
+ - Announce: "Detected .ai-context.md without its source architecture.md. The derived file exists but its primary source is missing (may have been accidentally deleted). Recommend running `/draft:init refresh` to regenerate architecture.md from codebase analysis."
259
+ - Do NOT delete the existing `.ai-context.md` — it still provides useful context until `architecture.md` is regenerated
260
+
261
+ ### Refresh Mode
262
+
263
+ If the user runs `/draft:init refresh`:
264
+
265
+ **0. State-Aware Pre-Check** (before any refresh work):
266
+
267
+ **a. Check for interrupted previous run:**
268
+ ```bash
269
+ cat draft/.state/run-memory.json 2>/dev/null
270
+ ```
271
+ If `status` is `"in_progress"`, offer to resume from `resumable_checkpoint` or start fresh.
272
+
273
+ **b. Load freshness state (if available):**
274
+ ```bash
275
+ cat draft/.state/freshness.json 2>/dev/null
276
+ ```
277
+ If `freshness.json` exists, compute current file hashes and diff against stored hashes:
278
+ - **Changed files**: Hash differs from stored → these files need re-analysis
279
+ - **New files**: Present in current tree but not in stored → new modules/components to document
280
+ - **Deleted files**: Present in stored but not in current tree → sections to prune
281
+ - **Unchanged files**: Hash matches → skip re-reading these files entirely
282
+
283
+ If NO files changed (all hashes match AND no new/deleted files), announce:
284
+ "No source file changes detected since last init/refresh ({generated_at}). Architecture context is current. Nothing to refresh."
285
+ Stop here unless the user insists.
286
+
287
+ **c. Load signal state (if available):**
288
+ ```bash
289
+ cat draft/.state/signals.json 2>/dev/null
290
+ ```
291
+ If `signals.json` exists, re-run signal classification (Phase 1 step 5) and diff against stored signals:
292
+ - **New signal categories** (0→N): A new architectural concern appeared (e.g., auth files added for the first time). Flag these — new architecture.md sections may need to be generated.
293
+ - **Removed signal categories** (N→0): An architectural concern was removed. Flag for section pruning.
294
+ - **Signal count changes**: Significant growth (>50% increase) suggests the section needs deeper treatment.
295
+
296
+ Report signal drift:
297
+ ```
298
+ Signal drift detected:
299
+ NEW: auth_files (0 → 5) — §16 Security Architecture needs generation
300
+ GROWN: backend_routes (12 → 24) — §12 API Definitions, §14 Cross-Module Integration need expansion
301
+ REMOVED: background_jobs (3 → 0) — §8 Concurrency can be simplified
302
+ STABLE: services (8 → 9), test_infra (15 → 16)
303
+ ```
304
+
305
+ **d. Create refresh run memory:**
306
+ If starting fresh: write new `draft/.state/run-memory.json` with `run_type: "refresh"` and `status: "in_progress"`.
307
+ If resuming from a checkpoint (step 0a): preserve existing fields (`phases_completed`, `resumable_checkpoint`, `active_focus_areas`) and only update `started_at` to current timestamp.
308
+
309
+ **e. Load previous unresolved questions:**
310
+ If the previous run had `unresolved_questions`, display them:
311
+ "Previous run flagged these unresolved questions: {list}. Keep these in mind during refresh."
312
+
313
+ 1. **Tech Stack Refresh**: Re-scan `package.json`, `go.mod`, etc. Compare with `draft/tech-stack.md`. Propose updates.
314
+
315
+ 2. **Architecture Refresh**: If `draft/architecture.md` exists, use metadata-based incremental analysis. If freshness state is available from step 0b, use file-level deltas to scope the refresh more precisely than git-diff alone:
316
+
317
+ **a. Read synced commit from metadata:**
318
+ ```bash
319
+ # Extract synced_to_commit from YAML frontmatter
320
+ SYNCED_SHA=$(grep "synced_to_commit:" draft/architecture.md | head -1 | sed 's/.*synced_to_commit:[[:space:]]*"\{0,1\}\([^"]*\)"\{0,1\}/\1/')
321
+
322
+ # Validate extracted SHA is a real git object
323
+ if [ -z "$SYNCED_SHA" ] || ! git cat-file -t "$SYNCED_SHA" 2>/dev/null; then
324
+ echo "Invalid or missing synced_to_commit — falling back to full refresh"
325
+ # Jump to step (i) — full refresh
326
+ fi
327
+ ```
328
+ This returns the commit SHA the docs were last synced to (more reliable than file modification time). The SHA is validated before use to prevent silent failures in `git diff`.
329
+
330
+ **b. Get changed files since that commit:**
331
+ ```bash
332
+ git diff --name-only <SYNCED_SHA> HEAD -- . ':!draft/'
333
+ ```
334
+ This lists all source files changed since the last architecture sync, excluding the draft/ directory itself.
335
+
336
+ **c. Check if docs were generated with dirty state:**
337
+ If the original `git.dirty: true`, warn: "Previous generation had uncommitted changes. Full refresh recommended."
338
+
339
+ **d. Categorize changes:**
340
+ - **Added files**: New modules, components, or features to document
341
+ - **Modified files**: Existing sections that may need updates
342
+ - **Deleted files**: Components to remove from documentation
343
+ - **Renamed files**: Update file references
344
+
345
+ **e. Targeted analysis (only changed files):**
346
+ > **Guardrail:** If more than 100 files changed since last sync, recommend full 5-phase refresh instead of incremental analysis. Too many changes means the incremental approach loses its token-efficiency advantage.
347
+
348
+ - Read each changed file to understand modifications (up to 100 files; if more, fall back to full refresh)
349
+ - Identify which architecture.md sections are affected:
350
+ - New files → Component Map, Implementation Catalog, File Structure
351
+ - Modified interfaces → API Definitions, Interface Contracts
352
+ - Changed dependencies → External Dependencies, Dependency Graph
353
+ - New tests → Testing Infrastructure
354
+ - Config changes → Configuration & Tuning
355
+ - Preserve unchanged sections exactly as-is
356
+ - Preserve modules added by `/draft:decompose` (planned modules)
357
+
358
+ **f. Present incremental diff:**
359
+ Show user:
360
+ - Files analyzed: `N changed files since <date>`
361
+ - Sections updated: list of affected sections
362
+ - Summary of changes per section
363
+
364
+ **g. On user approval:**
365
+ - Update only the affected sections in `draft/architecture.md`
366
+ - Regenerate `draft/.ai-context.md` and `draft/.ai-profile.md` using the Condensation Subroutine
367
+
368
+ **h. On user rejection:**
369
+ - No changes made to `draft/architecture.md`
370
+ - However, verify `.ai-context.md` consistency: if `.ai-context.md` is missing or its `synced_to_commit` differs from `architecture.md`, offer to regenerate it from the current (unchanged) `architecture.md`
371
+
372
+ **i. Fallback to full refresh:**
373
+ If `synced_to_commit` is missing from metadata, or the commit SHA doesn't exist in git history:
374
+ ```bash
375
+ git cat-file -t <SYNCED_SHA> 2>/dev/null || echo "not found"
376
+ ```
377
+ If this returns "not found", run full 5-phase architecture discovery instead.
378
+
379
+ - If `draft/architecture.md` does NOT exist and the project is brownfield, offer to generate it now
380
+
381
+ **j. Update metadata after refresh:**
382
+ After successful refresh, update the YAML frontmatter in all modified files:
383
+ - `generated_by`: `draft:init refresh`
384
+ - `generated_at`: current timestamp
385
+ - `git.*`: current git state
386
+ - `synced_to_commit`: current HEAD SHA
387
+
388
+ **k. Refresh state files:**
389
+ After successful architecture refresh, regenerate all state files:
390
+ - `draft/.state/facts.json` — re-extract atomic facts, perform contradiction detection (see step 2l)
391
+ - `draft/.state/freshness.json` — recompute hashes of all source files (new baseline)
392
+ - `draft/.state/signals.json` — re-run signal classification (update baseline)
393
+ - `draft/.state/run-memory.json` — set `status: "completed"`, `completed_at: "{ISO_TIMESTAMP}"`, preserve `unresolved_questions`
394
+
395
+ **l. Contradiction detection (if facts.json exists):**
396
+ If `draft/.state/facts.json` exists from a previous run, perform fact-level diff:
397
+
398
+ 1. **Re-extract facts** from changed files identified in step 2b
399
+ 2. **Compare against existing facts** sourced from those files:
400
+ - **CONFIRMED**: Fact still holds — update `last_verified_at` and `last_active_at`
401
+ - **UPDATED**: Fact changed (e.g., API endpoint renamed) — mark old fact with `superseded_by` edge, create new fact
402
+ - **EXTENDED**: Fact refined with new detail — add `extends` edge to original fact
403
+ - **NEW**: Fact not previously recorded — add with full timestamps
404
+ - **STALE**: Fact's source file was deleted — mark `last_active_at` as stale, reduce confidence
405
+ 3. **Generate Fact Evolution Report** — display summary to user:
406
+ ```
407
+ Fact Evolution Report:
408
+ CONFIRMED: N facts unchanged
409
+ UPDATED: N facts superseded (old → new)
410
+ EXTENDED: N facts refined
411
+ NEW: N facts discovered
412
+ STALE: N facts from deleted files
413
+ ```
414
+ 4. **Update relationship edges** in `facts.json` knowledge graph
415
+
416
+ 3. **Product Refinement**: Ask if product vision/goals in `draft/product.md` need updates.
417
+ 4. **Workflow Review**: Ask if `draft/workflow.md` settings (TDD, commits) need changing.
418
+ 5. **Preserve**: Do NOT modify `draft/tracks.md` unless explicitly requested.
419
+ 6. **Pattern Re-Discovery**: Run `/draft:learn` (no arguments — full codebase scan) to update `draft/guardrails.md` with any new or changed patterns since the last init/refresh. This keeps learned conventions and anti-patterns in sync with codebase evolution.
420
+
421
+ Stop here after refreshing. Continue to standard steps ONLY for fresh init.
422
+
423
+ ## Existing High-Quality Agent Context Audit (MANDATORY)
424
+
425
+ Before any architecture discovery or large document generation, scan for known high-signal, agent-optimized documentation that may already serve as authoritative source of truth:
426
+
427
+ ```bash
428
+ find . -maxdepth 4 \( -name "CLAUDE.md" -o -name "AGENTS.md" -o -name "INVARIANTS.md" -o -name "AUDIT_STANDARDS.md" -o -name "ARCHITECTURE.md" \) -not -path "./draft/*" -not -path "./node_modules/*" -not -path "./.git/*" 2>/dev/null | head -20
429
+ ls -d docs/ADRs 2>/dev/null && echo "ADR directory present ($(ls docs/ADRs/*.md 2>/dev/null | wc -l) records)" || true
430
+ ```
431
+
432
+ Classify and emit **Context Quality Report** (always, even if none found):
433
+
434
+ - High: Multiple strong files with explicit AI/agent focus (e.g. "written so future AI agents don't have to re-read source", machine-verified INVARIANTS with test backing).
435
+ - Medium: Partial (README + some docs/).
436
+ - Low/None: Standard project.
437
+
438
+ If High or Medium:
439
+ - Emit terminal report with file list, sizes/signals, and explicit warning:
440
+ ```
441
+ Context Quality Report:
442
+ High-quality agent-optimized docs detected:
443
+ - CLAUDE.md (10k+ lines, purpose-built for AI coding assistants)
444
+ - docs/INVARIANTS.md (single source of truth, test-referenced)
445
+ - docs/AUDIT_STANDARDS.md
446
+ Duplication risk: Generating a large parallel architecture.md can create divergence in safety-critical systems. Highest risk is inconsistent documentation, not insufficient volume.
447
+ Action: architecture.md will be graph-primary (Full mode) with mandatory "Graph Coverage Gaps" and "Relationship to Existing Authoritative Documentation" sections. Strong cross-references + provenance tags required. Prose duplication of existing high-fidelity material is a verification failure.
448
+ ```
449
+ - Set internal flag `EXISTING_CONTEXT_QUALITY=high` (propagate to synthesis, writing, and Completion Verification steps).
450
+ - Force §30 (Relationship) and §29 (Gaps) as non-skippable in later phases.
451
+ - In Completion Verification: add explicit check that Relationship section defers appropriately and adds only graph-derived or synthesized value.
452
+
453
+ This audit ensures Draft is safe and effective for mature brownfield projects that have already solved the "permanent AI agent context" problem at high fidelity.
454
+
455
+ ## Step 1: Project Discovery
456
+
457
+ Analyze the current directory to classify the project:
458
+
459
+ **Brownfield (Existing)** indicators:
460
+ - Has `package.json`, `requirements.txt`, `go.mod`, `Cargo.toml`, etc.
461
+ - Has `src/`, `lib/`, or similar code directories
462
+ - Has git history with commits
463
+
464
+ **Greenfield (New)** indicators:
465
+ - Empty or near-empty directory
466
+ - Only has README or basic config
467
+
468
+ Respect `.gitignore` and `.claudeignore` when scanning.
469
+
470
+ If **Brownfield**: proceed to Step 1.5 (Architecture Discovery).
471
+ If **Greenfield**: skip to Step 2 (Product Definition).
472
+
473
+ ---
474
+
475
+ ## Step 1.4: Graph Analysis (Automated, Before Manual Discovery)
476
+
477
+ **IMPORTANT**: Before reading any source files manually, run the graph builder to get precise structural data. This step is fast (seconds, not minutes) and dramatically accelerates all subsequent phases.
478
+
479
+ **CRITICAL ORDERING**: Phase 0 (this step) MUST complete before writing any section of architecture.md. The graph provides: (a) exhaustive module list, (b) hotspot-ranked module priority, (c) authoritative proto API surface, (d) mermaid diagrams ready for slot injection, (e) codebase tier for .ai-context.md budget.
480
+
481
+ ### 1. Build the graph snapshot
482
+
483
+ The knowledge-graph engine is `codebase-memory-mcp`, resolved by `scripts/tools/_lib.sh:find_memory_bin` (`DRAFT_MEMORY_BIN` > PATH > `~/.cache/draft/bin` > vendored `bin/<arch>/`). `draft install` fetches it (skip with `--no-graph`); install manually with `scripts/fetch-memory-engine.sh`. Set `DRAFT_MEMORY_DISABLE=1` to opt out.
484
+
485
+ One command resolves the engine, indexes the repo, and writes the committed snapshot under `draft/graph/`:
486
+
487
+ ```bash
488
+ if scripts/tools/graph-snapshot.sh --repo .; then
489
+ echo "Graph snapshot written to draft/graph/ (schema.yaml, architecture.json, hotspots.jsonl, *.mermaid)."
490
+ else
491
+ echo "Graph engine unavailable — skipping automated graph analysis. Downstream skills degrade gracefully."
492
+ fi
493
+ ```
494
+
495
+ Optionally record which engine was selected (usage-report contract):
496
+
497
+ ```bash
498
+ scripts/tools/verify-graph-binary.sh --repo . --json 2>/dev/null || true
499
+ ```
500
+
501
+ See `core/shared/graph-query.md` and `bin/README.md` for the query contract and engine resolution.
502
+
503
+ If the snapshot succeeds, `draft/graph/` is populated and later steps consume the always-load artifacts + injection slots.
504
+
505
+ ### 2. If graph build succeeds, load the always-load artifacts
506
+
507
+ Read these files to get structural context for all subsequent phases:
508
+ - `draft/graph/schema.yaml` — module count, file count, edge count, language stats per module
509
+ - `draft/graph/architecture.json` — module list (`.packages`) with fan-in/out
510
+ - `draft/graph/architecture.json` `.routes` — detected service endpoints
511
+ - `draft/graph/hotspots.jsonl` — all complexity hotspots (files ranked by lines + fanIn * 50)
512
+
513
+ ### 3. Use graph data to accelerate Step 1.5
514
+
515
+ - **Module boundaries**: Exact module list with file counts — skip manual directory tree mapping
516
+ - **Dependency wiring**: Exact inter-module edges with weights — skip manual `#include` / import tracing
517
+ - **Proto API surface**: Exact services, RPCs, and message definitions — skip manual proto discovery
518
+ - **Hotspots**: Know which high-complexity, high-fanIn files to prioritize reading
519
+ - **Language mix**: Exact `.cc`, `.h`, `.go`, `.proto`, `.py` counts per module
520
+ - **Cycle detection**: Circular dependency paths between modules — flag for architecture.md
521
+
522
+ ### 4. Compute codebase tier and module priority
523
+
524
+ **Step 1.4.5 — Compute Codebase Tier:**
525
+ Read `draft/graph/schema.yaml`. Extract:
526
+ - `M = stats.modules`
527
+ - `F = stats.go_functions + stats.py_functions`
528
+ - `P = stats.proto_rpcs`
529
+
530
+ Apply tier table:
531
+
532
+ | Tier | Label | Condition | .ai-context.md Budget |
533
+ |------|--------|----------------------------------------|-----------------------|
534
+ | 1 | micro | M≤5 AND F≤50 AND P≤10 | 100–180 lines |
535
+ | 2 | small | M≤15 AND F≤300 AND P≤30 | 180–280 lines |
536
+ | 3 | medium | M≤40 AND F≤1000 AND P≤100 | 280–400 lines |
537
+ | 4 | large | M≤100 AND F≤5000 AND P≤500 | 400–600 lines |
538
+ | 5 | XL | M>100 OR F>5000 OR P>500 | 600–900 lines |
539
+
540
+ Hold tier in memory. This governs: architecture.md length minimum, .ai-context.md budget, and module deep-dive depth.
541
+
542
+ **Step 1.4.6 — Build Module Priority List:**
543
+ From `draft/graph/hotspots.jsonl`: count hotspot files per module (group by `module` field).
544
+ From `draft/graph/architecture.json` `.packages[]`: read `fan_in` per module.
545
+ Rank modules by: `(hotspot_count × 2) + fan_in_count`.
546
+ Top-ranked modules drive Section 6 deep-dive ordering and depth. Modules ranked zero on both: summary treatment only.
547
+ Hold ranked list in memory — it replaces directory scanning for module discovery.
548
+
549
+ **Step 1.4.7 — Populate Graph Injection Slots:**
550
+ Query for diagram content and write into architecture.md slots using the standard marker format.
551
+
552
+ For Section 4.4 (module-deps slot):
553
+ ```bash
554
+ scripts/tools/mermaid-from-graph.sh --repo . --diagram module-deps
555
+ ```
556
+ The tool emits a ready-to-inject ` ```mermaid ``` ` block (or an empty stub on exit 2). Write between the markers:
557
+ ```
558
+ <!-- GRAPH:module-deps:START -->
559
+ {mermaid block from the tool}
560
+ <!-- GRAPH:module-deps:END -->
561
+ ```
562
+
563
+ For Section 20 (hotspots slot):
564
+ Read `draft/graph/hotspots.jsonl` (or run `scripts/tools/hotspot-rank.sh --repo . --top 10`), take the top 10 by fanIn, build a markdown table:
565
+ ```
566
+ <!-- GRAPH:hotspots:START -->
567
+ | Symbol | fanIn |
568
+ |--------|-------|
569
+ | {name} | {fanIn} |
570
+ ...
571
+ <!-- GRAPH:hotspots:END -->
572
+ ```
573
+
574
+ For Appendix E (proto-map slot):
575
+ ```bash
576
+ scripts/tools/mermaid-from-graph.sh --repo . --diagram proto-map
577
+ ```
578
+ The tool emits a ` ```mermaid ``` ` block from detected routes (empty stub if none). Write:
579
+ ```
580
+ <!-- GRAPH:proto-map:START -->
581
+ ```mermaid
582
+ {diagram content}
583
+ ```
584
+ <!-- GRAPH:proto-map:END -->
585
+ ```
586
+
587
+ **If slot markers are absent** (first run on a repo that has no prior slot structure): write the slot content at the designated location in the template. The markers are always present in `core/templates/architecture.md`, so this path is only hit if a user has an older pre-slot architecture.md.
588
+
589
+ ### 5. If graph binary not found or build fails
590
+
591
+ Proceed with standard Step 1.5 manual discovery. No degradation — the 5-phase analysis works as before. Architecture.md length minimum defaults to tier-2 guidance (medium-depth treatment).
592
+
593
+ See `core/shared/graph-query.md` for the full graph query subroutine reference.
594
+
595
+ ---
596
+
597
+ ## Step 1.5: Architecture Discovery (Brownfield Only)
598
+
599
+ Perform a **one-time, exhaustive analysis** of the existing codebase. This is NOT a summary — it is a comprehensive reference document that enables future AI agents and engineers to work without re-reading source files.
600
+
601
+ **Outputs**:
602
+ - `draft/architecture.md` — Human-readable, **comprehensive** engineering reference (PRIMARY)
603
+ - `draft/.ai-context.md` — Token-optimized, tier-scaled budget, condensed from architecture.md (DERIVED)
604
+ - `draft/.ai-profile.md` — Ultra-compact, 20-50 lines, always-injected project profile (DERIVED)
605
+ - `draft/graph/` — Knowledge graph artifacts (module-graph, proto-index, hotspots, per-module files) from Step 1.4
606
+
607
+ **Target output**: A single self-contained reference document designed for **dual consumption**:
608
+ 1. **LLM / AI-agent context** — enabling future code changes, Q&A, and onboarding without re-reading source files.
609
+ 2. **Engineer reference** — enabling debugging, extension, and operational understanding.
610
+
611
+ ### Graph + Indexed Knowledge Fidelity Mandate
612
+
613
+ **CRITICAL**: The output must be **faithful to the deterministic graph and your environment's full indexed understanding** of the project. This is not "read every file" exhaustiveness — it is correctness and completeness of the *model*.
614
+
615
+ - The knowledge graph (`draft/graph/`) + your agent/IDE's rich codebase index together form the authoritative view.
616
+ - Use direct source reads strategically (hotspots, interfaces, key implementation paths) to validate, enrich, and draw accurate diagrams — not as a brute-force enumeration exercise.
617
+ - **Prioritize synthesis of accurate workflow, state, sequence, and component diagrams** that make the graph's facts and the project's higher-level design immediately usable.
618
+ - **Include real, verified code snippets and invariants** only where they add understanding not already visible in the graph or diagrams.
619
+ - **Target: highest possible correctness** of the generated architecture model. A concise, diagram-rich document that an agent or engineer can trust is the goal. Volume without fidelity is noise.
620
+
621
+ If the codebase is large (200+ files), focus on the module boundaries but still enumerate exhaustively within each module.
622
+
623
+ > **Large codebase guardrail:** If the codebase exceeds 500 source files, limit Section 7 deep dives to the top 20 most-imported modules and summarize others in a table. Rank modules by the number of unique files that import/reference them (descending) — use `draft/graph/architecture.json` `.packages[].fan_in` if graph data is available. For dynamic languages where static import counting is impractical, rank by file count within each module directory (larger modules first). **Even for summarized modules, enumerate immediate sub-directories with file counts** (one-line per sub-dir) — this is cheap with graph data and provides essential navigation context.
624
+
625
+ ### Parallel Analysis Protocol (Tiers 3–5)
626
+
627
+ **MANDATORY for tiers 3–5 (medium / large / XL).** Uses Map → IR+Prose → Reduce: parallel reader agents each produce both structured IR metadata and full §7 deep-dive prose, then a synthesis agent composes the final document. Cuts wall clock by ~55% at XL tier while preserving depth — readers write the module narratives from source; synthesis assembles the cross-cutting sections.
628
+
629
+ **For tiers 1–2 (micro / small): skip this protocol entirely.** Use the Sequential Generation Protocol below. At small scale, parallelism adds overhead with no speed benefit, and the IR intermediate step discards source-level depth that a direct sequential pass produces cheaply.
630
+
631
+ > Full protocol details, IR schema, and prompt templates are in `core/shared/parallel-analysis.md`.
632
+
633
+ #### Tier-Adaptive Agent Counts
634
+
635
+ From the tier computed in Step 1.4.5, determine reader agent count:
636
+
637
+ | Tier | Label | Reader Agents | Strategy |
638
+ |------|--------|--------------------------------------|--------------------------------------|
639
+ | 1 | micro | 1 (all modules in one agent) | 1 reader → 1 synthesizer |
640
+ | 2 | small | 1–2 (all or half modules each) | 1–2 readers → 1 synthesizer |
641
+ | 3 | medium | 2–3 (ceil(M/6) agents) | parallel readers → 1 synthesizer |
642
+ | 4 | large | ceil(M/4) agents | parallel readers → 1 synthesizer + parallel finalizers |
643
+ | 5 | XL | ceil(M/4) agents | parallel readers → 1 synthesizer + parallel finalizers |
644
+
645
+ For tiers 1–2, the "parallel" phase is just a single reader agent — no overhead, same clean IR boundary.
646
+ For tier 3+, readers run simultaneously; wall clock = slowest reader, not the sum.
647
+
648
+ #### Phase 0: Graph Data (already done in Step 1.4)
649
+
650
+ The graph binary has already run. Use its output throughout this protocol:
651
+ - `draft.tmp/graph/schema.yaml` — module list, file counts, tier metrics
652
+ - `draft.tmp/graph/architecture.json` — `.packages[].fan_in` per module (for grouping)
653
+ - `draft.tmp/graph/hotspots.jsonl` — top hotspot files per module (feed to readers)
654
+
655
+ #### Phase 1: Spawn Parallel Module Readers
656
+
657
+ **Step 1: Group modules.**
658
+
659
+ From `draft.tmp/graph/architecture.json` `.packages[]`, extract all module names and their `fan_in` counts.
660
+ Apply dependency-aware grouping (see `core/shared/parallel-analysis.md`).
661
+ Use the modules-per-agent count from the tier table above (4 for tier 4/5; all modules in one agent for tier 1):
662
+ - Assign highest fan-in modules to separate readers (tier 3+)
663
+ - Co-locate coupled module pairs in the same reader
664
+ - Target balanced token budgets across groups
665
+
666
+ **Step 2: Build graph data summary per group.**
667
+
668
+ For each reader group, prepare a compact summary from graph artifacts:
669
+ ```
670
+ Modules: [execution, fill_processor, order_manager]
671
+ Hotspot files:
672
+ execution/engine.go (847 lines, fanIn=12)
673
+ execution/router.go (412 lines, fanIn=8)
674
+ fill_processor/handler.go (623 lines, fanIn=5)
675
+ Module edges (from architecture.json .packages fan-in/out):
676
+ execution → [risk, data, services]
677
+ fill_processor → [execution, persistence]
678
+ ```
679
+
680
+ **Step 3: Spawn all reader agents in parallel using the Agent tool.**
681
+
682
+ Spawn `ceil(module_count / 4)` agents simultaneously. Use the Module Reader Prompt Template from `core/shared/parallel-analysis.md`, replacing:
683
+ - `{MODULE_LIST}` — comma-separated module names for this agent
684
+ - `{REPO_ROOT}` — absolute path to repository root
685
+ - `{GRAPH_DATA_SUMMARY}` — the compact summary built in Step 2
686
+
687
+ Each reader agent:
688
+ - Reads source files in its assigned modules only
689
+ - Outputs a JSON array of IR objects (one per module)
690
+ - Produces NO prose, NO documentation
691
+
692
+ **Critical constraints to include in reader prompts:**
693
+ ```
694
+ MUST output IR JSON array only.
695
+ MUST NOT write any documentation or architecture sections.
696
+ MUST NOT read files outside assigned modules.
697
+ Token budget: max 600 tokens per module in IR output.
698
+ ```
699
+
700
+ **Step 4: Collect and validate reader outputs.**
701
+
702
+ Each reader produces two outputs, separated by `## IR` and `## Deep-Dives` headings.
703
+
704
+ After all readers complete:
705
+ 1. Extract the `## IR` section from each reader output and parse as JSON. If parse fails, retry that reader (see failure modes in `core/shared/parallel-analysis.md`).
706
+ 2. Check IR `token_budget_used` — if < 150 for a module with >20 files AND deep-dive for that module is < 100 lines, re-run that reader with explicit instruction to read more files.
707
+ 3. Concatenate all IR objects into a single JSON array → `draft.tmp/.state/reader-irs.json`
708
+ 4. Concatenate all `## Deep-Dives` sections from all readers → `draft.tmp/.state/reader-deep-dives.md`
709
+
710
+ #### Phase 2: Synthesis
711
+
712
+ Collect reader outputs before spawning synthesis:
713
+ 1. Parse and concatenate all IR JSON arrays → `draft.tmp/.state/reader-irs.json`
714
+ 2. Concatenate all reader deep-dive Markdown sections → `draft.tmp/.state/reader-deep-dives.md`
715
+
716
+ Spawn a **single synthesis agent** with the Synthesis Coordinator Prompt from `core/shared/parallel-analysis.md`, replacing:
717
+ - `{CONCATENATED_DEEP_DIVES}` — content of `draft.tmp/.state/reader-deep-dives.md`
718
+ - `{CONCATENATED_IRS}` — content of `draft.tmp/.state/reader-irs.json`
719
+ - `{GRAPH_DEPENDENCY_DIAGRAM}` — mermaid output from `--query --mode mermaid --symbol module-deps`
720
+ - `{ARCHITECTURE_TEMPLATE_STRUCTURE}` — the modern 10-section graph-primary outline from `core/templates/architecture.md` (the single source of truth)
721
+
722
+ The synthesis agent:
723
+ - Integrates the reader outputs (now graph + one high-quality workflow/state diagram + minimal notes per module) into §7 with light editing only for consistency and cross-references.
724
+ - Derives the true cross-cutting sections (§4 topology, §5 component map, §6 operational flows, §8 concurrency, §14 integration sequences, §15 invariants, etc.) by combining IR data, reader diagrams, and additional targeted source reads.
725
+ - Aggressively uses its own full indexed project knowledge (from the host Cursor/Claude Code/Copilot environment) to improve accuracy of workflows, state machines, and higher-level design synthesis beyond what the static graph snapshot provides.
726
+ - Produces a document whose primary value is faithful, visual, diagram-rich representation of the actual system design.
727
+
728
+ **Source reading policy for synthesis agent (enforce in prompt):**
729
+ ```
730
+ Read source (and aggressively use your full project index) for:
731
+ - §6 Core Operational Flows — the most important system-level workflows, lifecycles, and state machines (this is the highest-ROI section for future coding accuracy)
732
+ - §12 API / Interface surface
733
+ - §14 Cross-module integration sequences
734
+ - §15 Critical Invariants (verification against actual code)
735
+ - §18 Key Design Patterns
736
+
737
+ All other sections: compose primarily from the graph + reader outputs + IR, with light additional reads only where needed for diagram accuracy.
738
+ ```
739
+
740
+ #### Phase 3: Parallel Finalization
741
+
742
+ Once `draft.tmp/architecture.md` is written, spawn two agents simultaneously:
743
+
744
+ **Finalizer A — Context Derivation:**
745
+ Run the Condensation Subroutine (defined later in this skill) to generate:
746
+ - `draft.tmp/.ai-context.md`
747
+ - `draft.tmp/.ai-profile.md`
748
+
749
+ **Finalizer B — State Files:**
750
+ Write all `.state/` artifacts from the concatenated IRs:
751
+ - `draft.tmp/.state/facts.json` — extract atomic facts from IR fields (key_classes, invariants, state, error_handling)
752
+ - `draft.tmp/.state/freshness.json` — compute hashes of all source files read (baseline for incremental refresh)
753
+ - `draft.tmp/.state/signals.json` — derive signal classification from IR module roles and graph data
754
+ - `draft.tmp/.state/run-memory.json` — set `status: "completed"`, record phase timings
755
+
756
+ Finalizers A and B have no dependency on each other — run truly in parallel.
757
+
758
+ #### Phase 4: Quality Gate
759
+
760
+ After both finalizers complete, run the Completion Verification (defined later in this skill) against the standard hard minimum thresholds. If any metric fails:
761
+ 1. Identify the sparse sections (most likely cross-cutting sections: §14 Integration, §16 Security, §8 Concurrency)
762
+ 2. Request the synthesis agent to expand those sections, providing the relevant IR fields as targeted input
763
+ 3. Only proceed to atomic rename (`mv draft.tmp/ draft/`) after all metrics pass
764
+
765
+ #### Failure Recovery
766
+
767
+ If any reader agent fails to produce valid JSON after one retry:
768
+ - Log which modules failed: `draft.tmp/.state/failed-readers.json`
769
+ - Run those modules through the standard sequential analysis (Phase 3 in the Large Codebase Protocol below)
770
+ - Merge the resulting content into the IR set before synthesis
771
+ - The other readers' IRs remain valid — only the failed group needs re-work
772
+
773
+ ---
774
+
775
+ ### Sequential Fallback (when parallel IR pipeline unavailable)
776
+
777
+ When the Agent tool is unavailable or reader agents fail after retry, write `draft/architecture.md` using the **10-section graph-primary structure** (checklist above + `core/templates/architecture.md`). Do not use legacy 28-section or Pass 1/2/3 volume protocols.
778
+
779
+ 1. Use the ranked module list from Step 1.4.6 (graph-first — do not re-scan by directory if Phase 0 succeeded).
780
+ 2. For each top module (up to 20 by fan-in), read `draft/graph/modules/{name}.jsonl`, hotspot files, and 3–5 key sources; embed graph blocks and at least one workflow/state diagram per significant module inside §4–§8 as appropriate.
781
+ 3. Always include §9 Graph Coverage Gaps and §10 Relationship when the Context Audit requires them.
782
+ 4. Run Completion Verification (defined later in this skill) before condensation. Fidelity, provenance, and gap honesty block completion — not line counts.
783
+
784
+ ---
785
+
786
+ ### Execution Strategy for Depth
787
+
788
+ **Mindset**: You are creating a PERMANENT reference document. Future AI agents and engineers will use this instead of reading source code. Incomplete analysis means they'll make mistakes.
789
+
790
+ **File Reading Strategy**:
791
+ 1. **Read broadly first** (Phase 1-2): Map the entire codebase structure
792
+ 2. **Read deeply second** (Phase 3-4): For each major module, read the FULL implementation
793
+ 3. **Cross-reference** (Phase 5): Verify every component appears in all relevant sections
794
+
795
+ **Diagram Generation Strategy**:
796
+ 1. **Generate diagrams AFTER understanding** — not during exploration
797
+ 2. **Use proper Mermaid syntax** — validate mentally before writing
798
+ 3. **One diagram per concept** — don't combine unrelated flows
799
+ 4. **Annotate arrows** — show what data moves between nodes
800
+
801
+ **Iteration Guidance**:
802
+ - After initial generation, review each HIGH-priority section
803
+ - If any section is thin (< 1 page for HIGH priority), expand it
804
+ - If any required diagram is missing, add it
805
+ - If tables have < 5 rows, verify you've enumerated exhaustively
806
+
807
+ ---
808
+
809
+ ### Adaptive Sections
810
+
811
+ Not every codebase has every concept. Apply these rules:
812
+
813
+ | If the codebase... | Then... |
814
+ |---------------------|---------|
815
+ | Has no plugin / algorithm / handler system | Skip Section 9 (Framework & Extension Points) and Section 10 (Full Catalog) |
816
+ | Has no V1/V2 generational split | Skip Section 11 (Secondary Subsystem) |
817
+ | Has no RPC / proto / API definitions | Skip Section 12, or retitle to "API Definitions" and cover REST / GraphQL / OpenAPI |
818
+ | Is a library (no binary / process) | Adapt Section 4.2 (Process Lifecycle) to "Usage Lifecycle" — how consumers integrate it |
819
+ | Is a frontend / UI module | Add: Component hierarchy, route map, state management, styling system |
820
+ | Uses a database directly | Add to Section 19: schema definitions, migration system, ORM models |
821
+ | Is containerized / has infra config | Add: Dockerfile, Kubernetes manifests, Helm charts, Terraform, CI/CD pipeline |
822
+ | Is a single-threaded / simple module | Simplify Section 8 (Concurrency) to note "single-threaded" and skip detailed thread maps |
823
+ | Has no configuration flags | Adapt Section 22 to cover whatever config mechanism exists (env vars, YAML, JSON, TOML, .env) |
824
+
825
+ ---
826
+
827
+ ### Language-Specific Exploration Guide
828
+
829
+ #### C / C++
830
+ | What to Find | Where to Look |
831
+ |-------------|--------------|
832
+ | Build targets & deps | `BUILD`, `CMakeLists.txt`, `Makefile` |
833
+ | Entry point | `main()` in `*_exec.cc`, `main.cc`, `*_main.cc` |
834
+ | Interfaces | `.h` header files (class declarations, virtual methods) |
835
+ | Implementation | `.cc` / `.cpp` files |
836
+ | API definitions | `.proto` files (protobuf), `.thrift` files |
837
+ | Config / flags | gflags: `DEFINE_*` macros in `flags.cc` / `flags.h` |
838
+ | Tests | `*_test.cc`, `*_unittest.cc`, files in `test/` or `qa/` dirs |
839
+
840
+ #### Go
841
+ | What to Find | Where to Look |
842
+ |-------------|--------------|
843
+ | Build targets & deps | `go.mod`, `go.sum`, `BUILD` (if Bazel) |
844
+ | Entry point | `func main()` in `main.go` or `cmd/*/main.go` |
845
+ | Interfaces | `type XxxInterface interface` in `*.go` files |
846
+ | Implementation | `*.go` files (non-test) |
847
+ | API definitions | `.proto` files, or handler registrations in router setup |
848
+ | Config / flags | `flag.*`, Viper config, environment variables |
849
+ | Tests | `*_test.go` files |
850
+
851
+ #### Python
852
+ | What to Find | Where to Look |
853
+ |-------------|--------------|
854
+ | Build targets & deps | `requirements.txt`, `pyproject.toml`, `setup.py`, `setup.cfg`, `Pipfile` |
855
+ | Entry point | `if __name__ == "__main__"` blocks, `app.py`, `main.py`, CLI entry points in pyproject.toml |
856
+ | Interfaces | Abstract base classes (`ABC`), Protocol classes, type hints |
857
+ | Implementation | `.py` files |
858
+ | API definitions | FastAPI/Flask route decorators, OpenAPI spec, `.proto` files |
859
+ | Config / flags | `settings.py`, `.env`, `config.yaml`, `argparse`, Pydantic Settings |
860
+ | Tests | `test_*.py`, `*_test.py`, files in `tests/` dirs |
861
+
862
+ #### TypeScript / JavaScript
863
+ | What to Find | Where to Look |
864
+ |-------------|--------------|
865
+ | Build targets & deps | `package.json`, `tsconfig.json`, `yarn.lock` / `package-lock.json` |
866
+ | Entry point | `"main"` in package.json, `index.ts`, `app.ts`, `server.ts` |
867
+ | Interfaces | TypeScript `interface` / `type` definitions in `*.ts` / `*.d.ts` |
868
+ | Implementation | `*.ts` / `*.js` files |
869
+ | API definitions | Route files, OpenAPI spec, GraphQL `.graphql` / `.gql` files |
870
+ | Config / flags | `.env`, `config.ts`, environment variables, `process.env.*` |
871
+ | Tests | `*.test.ts`, `*.spec.ts`, files in `__tests__/` dirs |
872
+
873
+ #### Java / Kotlin
874
+ | What to Find | Where to Look |
875
+ |-------------|--------------|
876
+ | Build targets & deps | `pom.xml` (Maven), `build.gradle` / `build.gradle.kts` (Gradle), `BUILD` (Bazel) |
877
+ | Entry point | `public static void main(String[] args)`, Spring Boot `@SpringBootApplication` |
878
+ | Interfaces | Java `interface` declarations, abstract classes |
879
+ | Implementation | `*.java` / `*.kt` files in `src/main/` |
880
+ | API definitions | `@RestController` / `@RequestMapping` annotations, `.proto` files, OpenAPI |
881
+ | Config / flags | `application.yml` / `application.properties`, Spring `@Value`, env vars |
882
+ | Tests | `*Test.java`, `*Spec.kt`, files in `src/test/` |
883
+
884
+ #### Rust
885
+ | What to Find | Where to Look |
886
+ |-------------|--------------|
887
+ | Build targets & deps | `Cargo.toml`, `Cargo.lock` |
888
+ | Entry point | `fn main()` in `src/main.rs` or `src/bin/*.rs` |
889
+ | Interfaces | `trait` definitions |
890
+ | Implementation | `*.rs` files |
891
+ | API definitions | Handler registrations (Actix/Axum routes), `.proto` files |
892
+ | Config / flags | `clap` structs, `config` crate, `.env`, `config.toml` |
893
+ | Tests | `#[test]` functions, `tests/` directory, `#[cfg(test)]` modules |
894
+
895
+ ---
896
+
897
+ ### Analysis Strategy — How to Explore the Codebase
898
+
899
+ Follow these steps in order. The specific files to look for depend on the language — use the Language-Specific Exploration Guide above.
900
+
901
+ #### Phase 1: Discovery (Broad Scan)
902
+
903
+ 1. **Map the directory tree**: Recursively list the project to understand the file layout. Note subdirectory groupings. (If Step 1.4 graph analysis succeeded, use `draft.tmp/graph/schema.yaml` module list instead — it is exhaustive and includes file counts.)
904
+
905
+ 2. **Read build / dependency files**: These reveal the module structure, dependencies, and targets. (See language guide above for which files.)
906
+
907
+ 3. **Read API definition files**: These define the module's data model and service interfaces. (See language guide above for which files. If Step 1.4 succeeded, `draft.tmp/graph/architecture.json` `.routes` already has all detected service endpoints.)
908
+
909
+ 4. **Read interface / type definition files**: Class declarations, interface definitions, and type annotations reveal the public API and design intent.
910
+
911
+ 5. **Classify codebase signals**: Walk the file tree from step 1 and tag every file that matches one or more signal categories. This drives adaptive section depth in later phases — sections with strong signals get deep treatment, sections with no signals get marked SKIP. (If Step 1.4 succeeded, use module file counts and dependency edges to accelerate signal classification.)
912
+
913
+ | Signal Category | Detection Patterns | Drives Section(s) |
914
+ |----------------|-------------------|-------------------|
915
+ | `backend_routes` | `routes/`, `handlers/`, `controllers/`, `**/api/**`, route decorators (`@app.route`, `@router`, `@RequestMapping`) | §12 API Definitions, §14 Cross-Module Integration |
916
+ | `frontend_routes` | `pages/`, `views/`, `**/routes.*`, `**/router.*`, React Router, Next.js `app/` dir | §4 Architecture (add UI topology) |
917
+ | `components` | `components/`, `widgets/`, `*.component.ts`, `*.tsx` in component dirs | §7 Core Modules (add component hierarchy) |
918
+ | `services` | `services/`, `*Service.*`, `*_service.*`, `**/service/**` | §5 Component Map, §7 Core Modules |
919
+ | `data_models` | `models/`, `entities/`, `schemas/`, `*.model.*`, `*.entity.*`, `migrations/` | §19 State Management, §12 API Definitions |
920
+ | `auth_files` | `auth/`, `**/auth/**`, `middleware/auth*`, `guards/`, JWT/OAuth imports | §16 Security Architecture |
921
+ | `state_management` | `store/`, `reducers/`, `**/state/**`, Redux/Vuex/Zustand/Pinia imports | §19 State Management (frontend state) |
922
+ | `background_jobs` | `jobs/`, `workers/`, `tasks/`, `queues/`, `**/cron/**`, Celery/Sidekiq/Bull imports | §8 Concurrency, §22 Configuration |
923
+ | `persistence` | `repositories/`, `dao/`, `**/db/**`, ORM config files, migration directories | §19 State Management |
924
+ | `test_infra` | `test/`, `tests/`, `__tests__/`, `*.test.*`, `*.spec.*`, test config files | §26 Testing Infrastructure |
925
+ | `config_files` | `.env*`, `config/`, `*.config.*`, `application.yml`, `settings.*` | §22 Configuration |
926
+
927
+ **Procedure:**
928
+
929
+ ```bash
930
+ # Count files matching each signal category
931
+ # Example for backend_routes:
932
+ find . -type f \( -path "*/routes/*" -o -path "*/handlers/*" -o -path "*/controllers/*" -o -path "*/api/*" \) \
933
+ ! -path "*/node_modules/*" ! -path "*/.git/*" ! -path "*/vendor/*" ! -path "*/draft/*" | head -50
934
+
935
+ # Repeat for each category, adapting patterns to the detected language
936
+ ```
937
+
938
+ **Build a signal summary** (hold in memory for Phase 5):
939
+
940
+ ```
941
+ Signal Classification:
942
+ backend_routes: 12 files → §12, §14 HIGH
943
+ services: 8 files → §5, §7 HIGH
944
+ data_models: 6 files → §19, §12 HIGH
945
+ test_infra: 15 files → §26 HIGH
946
+ auth_files: 3 files → §16 HIGH
947
+ components: 0 files → §7 (skip component hierarchy)
948
+ frontend_routes: 0 files → §4 (skip UI topology)
949
+ state_management: 0 files → §19 (skip frontend state)
950
+ background_jobs: 0 files → §8 (simplify concurrency)
951
+ persistence: 4 files → §19 HIGH
952
+ config_files: 5 files → §22 HIGH
953
+ ```
954
+
955
+ **Integration with Adaptive Sections table (above):** Use signal counts to override the default skip rules. A signal count of 0 means the section should be skipped or simplified. A count ≥ 3 means the section warrants deep treatment. Between 1-2, include the section but keep it brief.
956
+
957
+ #### Phase 2: Wiring (Trace the Graph)
958
+
959
+ 6. **Find the entry point**: (See language guide above for common entry-point patterns.) Trace the initialization sequence.
960
+
961
+ 7. **Follow the orchestrator**: From the top-level controller / app / server, trace how it creates, initializes, and wires all owned components.
962
+
963
+ 8. **Find the registry / registration code**: Look for files that register handlers, plugins, routes, middleware, algorithms, etc. This reveals the full catalog.
964
+
965
+ 9. **Map the dependency wiring**: Find the DI container, context struct, module system, or import graph that connects components.
966
+
967
+ #### Phase 3: Depth (Trace the Flows)
968
+
969
+ 10. **Trace data flows end-to-end**: For each major flow, start at the data source / entry point and follow the code through processing stages to the output.
970
+
971
+ 11. **Read implementation files**: For core modules, read the implementation to understand algorithms, error handling, retry logic, and state management.
972
+
973
+ 12. **Identify concurrency model**: Find where thread pools, async executors, goroutines, or worker processes are created and what work is dispatched to each.
974
+
975
+ 13. **Find safety checks**: Look for invariant assertions, validation logic, auth checks, version checks, lock acquisitions, and transaction boundaries.
976
+
977
+ #### Phase 4: Periphery
978
+
979
+ 14. **Catalog external dependencies**: Check build/dependency files and import statements to map all external library and service dependencies.
980
+
981
+ 15. **Examine test infrastructure**: Read test files and test utilities to understand the testing approach, mock patterns, and test harness.
982
+
983
+ 16. **Scan for configuration**: Find all configuration mechanisms (flags, env vars, config files, feature gates, constants).
984
+
985
+ 17. **Look for documentation**: Check for existing README, docs/, architecture decision records (ADRs), or inline comments that provide architectural context.
986
+
987
+ #### Phase 5: Synthesis
988
+
989
+ 18. **Cross-reference**: Ensure every component mentioned in one section appears in all relevant sections (architecture, data flow, interaction matrix, etc.).
990
+
991
+ 19. **Validate completeness**: Confirm ALL handlers / endpoints / plugins / schemas / dependencies are listed. Do not sample — enumerate exhaustively.
992
+
993
+ 20. **Identify patterns**: Look for recurring design patterns and document them.
994
+
995
+ 21. **Generate diagrams**: Create Mermaid diagrams AFTER understanding the full picture, not during exploration.
996
+
997
+ ---
998
+
999
+ ## architecture.md Specification (Graph-Primary — Forward Only)
1000
+
1001
+ Generate `draft/architecture.md` using the modern 10-section graph-primary structure defined in the **MANDATORY SECTION CHECKLIST** above and in `core/templates/architecture.md`.
1002
+
1003
+ The document is:
1004
+ - Primarily derived from the deterministic knowledge graph (`draft/graph/`).
1005
+ - Explicit about fidelity (frontmatter `graph:` block + Dashboard).
1006
+ - Required to carry provenance/fidelity tags on all significant claims.
1007
+ - Duplication-aware when high-quality agent docs (CLAUDE.md, INVARIANTS.md, etc.) are detected by the Context Audit.
1008
+
1009
+ **Full details, per-section guidance, provenance rules, and examples** live in:
1010
+ - `core/templates/architecture.md` (the source of truth for the 10 sections + Generation Contract)
1011
+ - `docs/research/proposed-graph-backed-architecture-template.md` (design rationale and fidelity rules)
1012
+ - `references/architecture-spec.md` (deprecated legacy notes — **10-section template wins on any conflict**)
1013
+
1014
+ There is no legacy 28-section structure and no volume targets. The template itself is the contract.
1015
+
1016
+ **After completing analysis AND passing verification**, write to `draft/architecture.md`. This is the PRIMARY output. Then run the Condensation Subroutine.
1017
+
1018
+ ## .ai-context.md Specification
1019
+
1020
+ **Authoritative procedure:** [core/shared/condensation.md](../../core/shared/condensation.md). Git state lives in `draft/metadata.json` only — do not copy `git.*` into `.ai-context.md` frontmatter.
1021
+
1022
+ Generate `draft/.ai-context.md` — a **machine-optimized** context file for AI/LLM consumption (200-400 lines).
1023
+
1024
+ ### Design Principles
1025
+
1026
+ This file is **NOT for humans**. It is optimized for:
1027
+ 1. **Token efficiency** — minimize tokens while maximizing information density
1028
+ 2. **Machine parseability** — use consistent, structured formats that LLMs process efficiently
1029
+ 3. **Self-containment** — complete context without referencing other files
1030
+ 4. **Action-orientation** — everything an AI needs to make safe, correct code changes
1031
+
1032
+ **Format choices**:
1033
+ - Use YAML-like key-value pairs (not prose paragraphs)
1034
+ - Use arrow notation for graphs (not Mermaid)
1035
+ - Use compact tables with `|` separators
1036
+ - Use structured lists with consistent prefixes
1037
+ - Abbreviate common patterns (e.g., `fn` for function, `ret` for returns)
1038
+ - No markdown formatting for emphasis (no `**bold**` or `_italic_`)
1039
+
1040
+ ### MANDATORY Header Format
1041
+
1042
+ **CRITICAL**: Every .ai-context.md file MUST start with this exact structure:
1043
+
1044
+ ```markdown
1045
+ ---
1046
+ project: "{PROJECT_NAME}"
1047
+ module: "root"
1048
+ generated_by: "draft:init"
1049
+ generated_at: "{ISO_TIMESTAMP}"
1050
+ git:
1051
+ branch: "{LOCAL_BRANCH}"
1052
+ remote: "{REMOTE/BRANCH}"
1053
+ commit: "{FULL_SHA}"
1054
+ commit_short: "{SHORT_SHA}"
1055
+ commit_date: "{COMMIT_DATE}"
1056
+ commit_message: "{COMMIT_MESSAGE}"
1057
+ dirty: {true|false}
1058
+ synced_to_commit: "{FULL_SHA}"
1059
+ ---
1060
+ ```
1061
+
1062
+ **Do NOT skip the YAML frontmatter. It enables incremental refresh tracking.**
1063
+
1064
+ ---
1065
+
1066
+ ### Required Sections (all mandatory)
1067
+
1068
+ ```markdown
1069
+ # {PROJECT_NAME}
1070
+
1071
+ ## META
1072
+ type: {microservice|cli|library|daemon|webapp|api}
1073
+ lang: {language} {version}
1074
+ pattern: {Hexagonal|MVC|Pipeline|Event-driven|Layered}
1075
+ build: {exact command}
1076
+ test: {exact command}
1077
+ entry: {file}:{function|class}
1078
+ config: {mechanism}@{location}
1079
+
1080
+ ## GRAPH:COMPONENTS
1081
+ {ComponentA}
1082
+ ├─{SubComponentA1}: {5-word purpose}
1083
+ ├─{SubComponentA2}: {5-word purpose}
1084
+ └─{SubComponentA3}
1085
+ ├─{NestedComponent}: {purpose}
1086
+ └─{NestedComponent}: {purpose}
1087
+ {ComponentB}
1088
+ └─...
1089
+
1090
+ ## GRAPH:DEPENDENCIES
1091
+ {Internal} -[{protocol}]-> {External}
1092
+ {Internal} -[{protocol}]-> {External}
1093
+ Examples:
1094
+ AuthService -[gRPC]-> UserDB
1095
+ API -[HTTP/REST]-> PaymentGateway
1096
+ Worker -[AMQP]-> MessageQueue
1097
+
1098
+ ## GRAPH:DATAFLOW
1099
+ FLOW:{FlowName}
1100
+ {source} --{data_type}--> {stage1} --{data_type}--> {stage2} --> {sink}
1101
+ FLOW:{AnotherFlow}
1102
+ {source} --> {stage} --> {sink}
1103
+ FLOW:ERROR
1104
+ {component} --{error_type}--> {handler} --> {recovery_action}
1105
+
1106
+ ## WIRING
1107
+ mechanism: {constructor_injection|context_struct|module_imports|DI_container|singleton}
1108
+ tokens: [{token1}, {token2}, {token3}]
1109
+ getters: [{getter1}, {getter2}]
1110
+
1111
+ ## INVARIANTS
1112
+ [DATA] {name}: {rule} @{file}:{line}
1113
+ [DATA] {name}: {rule} @{file}:{line}
1114
+ [SEC] {name}: {rule} @{file}:{line}
1115
+ [CONC] {name}: {rule} @{file}:{line}
1116
+ [ORD] {name}: {rule} @{file}:{line}
1117
+ [COMPAT] {name}: {rule} @{file}:{line}
1118
+ [IDEM] {name}: {rule} @{file}:{line}
1119
+
1120
+ ## INTERFACES
1121
+ ```{language}
1122
+ // Condensed interface definitions - signatures only
1123
+ interface {Name} {
1124
+ {method}({params}): {return} // {one-line purpose}
1125
+ {method}?({params}): {return} // optional
1126
+ }
1127
+ ```
1128
+
1129
+ ## CATALOG:{Category}
1130
+ {id}|{type}|{file}|{purpose}
1131
+ {id}|{type}|{file}|{purpose}
1132
+
1133
+ ## CATALOG:{AnotherCategory}
1134
+ {id}|{type}|{file}|{purpose}
1135
+
1136
+ ## THREADS
1137
+ {pool_name}|{count}|{runs_what}
1138
+ {pool_name}|{count}|{runs_what}
1139
+
1140
+ ## CONFIG
1141
+ {param}|{default}|{critical:Y/N}|{purpose}
1142
+ {param}|{default}|{critical:Y/N}|{purpose}
1143
+
1144
+ ## ERRORS
1145
+ {scenario}: {recovery}
1146
+ {scenario}: {recovery}
1147
+ retry_policy: {policy}
1148
+ backoff: {strategy}
1149
+
1150
+ ## CONCURRENCY
1151
+ {component}: {rule} -> {violation_consequence}
1152
+ {component}: {rule} -> {violation_consequence}
1153
+ locks: [{lock1}@{file}, {lock2}@{file}]
1154
+ lock_order: {lock1} < {lock2} < {lock3}
1155
+
1156
+ ## EXTEND:{ExtensionType}
1157
+ create: {path/pattern}
1158
+ implement: {interface}@{file}
1159
+ required: [{method1}, {method2}]
1160
+ optional: [{method3}]
1161
+ register: {registry}@{file}:{function}
1162
+ deps: [{dep1}, {dep2}]
1163
+ test: {test_pattern}
1164
+
1165
+ ## EXTEND:{AnotherType}
1166
+ ...
1167
+
1168
+ ## TEST
1169
+ unit: {command}
1170
+ integration: {command}
1171
+ hooks: [{hook1}@{file}, {hook2}@{file}]
1172
+
1173
+ ## FILES
1174
+ entry: {path}
1175
+ config: {path}
1176
+ routes: {path}
1177
+ models: {path}
1178
+ services: {path}
1179
+ tests: {path}
1180
+ build: {path}
1181
+
1182
+ ## VOCAB
1183
+ {term}: {definition}
1184
+ {term}: {definition}
1185
+
1186
+ ## REFS
1187
+ tech_stack: draft/tech-stack.md
1188
+ workflow: draft/workflow.md
1189
+ product: draft/product.md
1190
+ ```
1191
+
1192
+ ### Machine-Readable Graph Notation
1193
+
1194
+ Use these consistent notations for graphs:
1195
+
1196
+ **Component hierarchy** (tree notation):
1197
+ ```
1198
+ Root
1199
+ ├─Child1: purpose
1200
+ ├─Child2: purpose
1201
+ │ ├─Grandchild1: purpose
1202
+ │ └─Grandchild2: purpose
1203
+ └─Child3: purpose
1204
+ ```
1205
+
1206
+ **Dependency arrows** (directed graph):
1207
+ ```
1208
+ A -[protocol]-> B # A depends on B via protocol
1209
+ A --> B # A depends on B (direct call)
1210
+ A -.-> B # A optionally depends on B
1211
+ A <--> B # bidirectional dependency
1212
+ ```
1213
+
1214
+ **Data flow** (pipeline notation):
1215
+ ```
1216
+ Source --{DataType}--> Transform --{DataType}--> Sink
1217
+ |
1218
+ +--> Branch --{DataType}--> AlternateSink
1219
+ ```
1220
+
1221
+ **State transitions**:
1222
+ ```
1223
+ State1 --(event)--> State2
1224
+ State2 --(event)--> State3 | State4 # conditional
1225
+ ```
1226
+
1227
+ ### Compression Techniques
1228
+
1229
+ Apply these to minimize tokens:
1230
+
1231
+ 1. **Abbreviate common words**:
1232
+ - `fn` = function, `ret` = returns, `req` = required, `opt` = optional
1233
+ - `cfg` = config, `impl` = implementation, `dep` = dependency
1234
+ - `auth` = authentication, `authz` = authorization
1235
+
1236
+ 2. **Use symbols**:
1237
+ - `@` = at/in file, `->` = leads to/calls, `|` = or/separator
1238
+ - `?` = optional, `!` = critical/required, `~` = approximate
1239
+
1240
+ 3. **Omit obvious context**:
1241
+ - Skip "The" and "This" at start of descriptions
1242
+ - Skip file extensions when unambiguous
1243
+ - Skip common prefixes (e.g., `src/` if all files are there)
1244
+
1245
+ 4. **Use consistent column formats**:
1246
+ - Tables: `col1|col2|col3` (no spaces around `|`)
1247
+ - Key-value: `key: value` (single space after colon)
1248
+ - Lists: `[item1, item2, item3]` (comma-space separator)
1249
+
1250
+ ### What to EXCLUDE from .ai-context.md
1251
+
1252
+ Exclude (belongs only in architecture.md):
1253
+ - Mermaid diagram syntax (use text graphs)
1254
+ - Full code implementations (use signatures only)
1255
+ - Prose explanations (use structured key-values)
1256
+ - Human formatting (bold, italic, headers beyond ##)
1257
+ - Redundant information (don't repeat across sections)
1258
+ - Historical context (focus on current state)
1259
+ - Performance details (unless critical for correctness)
1260
+ - Security details (unless needed for code changes)
1261
+
1262
+ ### Quality Checklist for .ai-context.md
1263
+
1264
+ Verify before writing:
1265
+ - [ ] Agent can implement new extension using ONLY this file
1266
+ - [ ] Agent knows correct thread pool for async work
1267
+ - [ ] Agent knows invariants to check before side effects
1268
+ - [ ] Agent knows error handling pattern
1269
+ - [ ] Agent can find correct file for any modification
1270
+ - [ ] Agent knows test command and patterns
1271
+ - [ ] Agent knows V1/V2 boundary (if applicable)
1272
+ - [ ] No prose paragraphs (all structured data)
1273
+ - [ ] No references to architecture.md
1274
+ - [ ] 200-400 lines total
1275
+
1276
+ ---
1277
+
1278
+ ## Architecture Discovery Output (End of Step 1.5)
1279
+
1280
+ After completing the 5-phase analysis:
1281
+
1282
+ 1. **Gather git metadata FIRST**: Run these commands to collect current state:
1283
+ ```bash
1284
+ PROJECT_NAME=$(basename "$(pwd)")
1285
+ GIT_BRANCH=$(git branch --show-current)
1286
+ GIT_REMOTE=$(git rev-parse --abbrev-ref --symbolic-full-name @{upstream} 2>/dev/null || echo "none")
1287
+ GIT_COMMIT=$(git rev-parse HEAD)
1288
+ GIT_COMMIT_SHORT=$(git rev-parse --short HEAD)
1289
+ GIT_COMMIT_DATE=$(git log -1 --format="%ci")
1290
+ GIT_COMMIT_MSG=$(git log -1 --format="%s")
1291
+ GIT_DIRTY=$([ -n "$(git status --porcelain)" ] && echo "true" || echo "false")
1292
+ ISO_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
1293
+ ```
1294
+
1295
+ 2. **Write `draft/architecture.md`** with this EXACT structure:
1296
+ ```markdown
1297
+ ---
1298
+ project: "{PROJECT_NAME from above}"
1299
+ module: "root"
1300
+ generated_by: "draft:init"
1301
+ generated_at: "{ISO_TIMESTAMP from above}"
1302
+ git:
1303
+ branch: "{GIT_BRANCH}"
1304
+ remote: "{GIT_REMOTE}"
1305
+ commit: "{GIT_COMMIT}"
1306
+ commit_short: "{GIT_COMMIT_SHORT}"
1307
+ commit_date: "{GIT_COMMIT_DATE}"
1308
+ commit_message: "{GIT_COMMIT_MSG}"
1309
+ dirty: {GIT_DIRTY}
1310
+ synced_to_commit: "{GIT_COMMIT}"
1311
+ ---
1312
+
1313
+ # Architecture: {PROJECT_NAME}
1314
+
1315
+ > Graph-primary high-signal engineering reference (10-section modern structure).
1316
+ > For token-optimized AI context, see `draft/.ai-context.md`.
1317
+
1318
+ ---
1319
+
1320
+ ## Table of Contents
1321
+ ... (the 10 sections from the current `core/templates/architecture.md`)
1322
+ ```
1323
+
1324
+ 3. **Run Completion Verification (MANDATORY)** — Before proceeding to `.ai-context.md`, verify architecture.md meets signal-quality, fidelity, and duplication-aware requirements (volume is now guidance only, secondary to provenance and honesty):
1325
+
1326
+ ```
1327
+ SIGNAL QUALITY & FIDELITY VERIFICATION (replaces volume proxy)
1328
+
1329
+ Hard (blocking) checks — all must PASS:
1330
+
1331
+ 1. Graph fidelity frontmatter block present and populated (graph: build_status, overall_fidelity, language_fidelity, stats, notes).
1332
+ → PASS / FAIL: ___
1333
+
1334
+ 2. Graph Health & Fidelity Dashboard table rendered in header area with real data from this run (no placeholders).
1335
+ → PASS / FAIL: ___
1336
+
1337
+ 3. §29 Graph Coverage Gaps & Known Limitations present, substantive (≥150 words or explicit "Full coverage — justification"), and enumerates the actual shortfalls observed (cross-refs Dashboard and frontmatter).
1338
+ → PASS / FAIL: ___
1339
+
1340
+ 4. §30 Relationship to Existing Authoritative Documentation present. When Context Audit = high/medium: contains concrete cross-references to detected files (CLAUDE.md, INVARIANTS.md, etc.), states what this doc adds (graph spine + diagrams + synthesis) vs. defers, and confirms no large prose duplication occurred.
1341
+ → PASS / FAIL: ___
1342
+
1343
+ 5. Sample of ≥5 critical claims (invariants §15, key flows §6, modules §7) carry explicit fidelity/provenance tags (e.g. [Graph:High], [Existing:CLAUDE.md §3], [Human:Synthesis]).
1344
+ → PASS / FAIL: ___
1345
+
1346
+ 6. All <!-- GRAPH:*:START/END --> injection slots either populated from graph or explicitly marked unavailable with fidelity impact note.
1347
+ → PASS / FAIL: ___
1348
+
1349
+ 7. Per-module Graph Fidelity & Diagram Report complete for all modules that have graph data; no synthesis contradictions with graph; low-fidelity areas explicitly called out in §9.
1350
+ → PASS / FAIL: ___
1351
+
1352
+ Soft / guidance (low-context runs only; high-context runs may legitimately be shorter when deferring to authoritative sources):
1353
+ - Lines / Mermaid / tables / file refs / invariants / glossary as historical targets (no longer hard gates).
1354
+ - For 500+ file low-context: still expect substantial depth in graph-covered areas.
1355
+
1356
+ OVERALL: If ANY hard check is FAIL, identify the weakest area (most often Gaps/Relationship or missing tags when audit was high) and expand/re-synthesize. Do NOT proceed to .ai-context.md until all hard checks PASS.
1357
+ ```
1358
+
1359
+ If any verification step fails:
1360
+ - Re-run synthesis (or the specific reader) with explicit instruction to address the failing check using the Context Audit flag + graph data + provenance requirements.
1361
+ - Re-run verification.
1362
+ - Repeat until all hard checks pass.
1363
+
1364
+ 4. **Derive `draft/.ai-context.md`** with the SAME metadata header, then use the Condensation Subroutine to transform architecture.md content into machine-optimized format.
1365
+
1366
+ 5. **Derive `draft/.ai-profile.md`** — ultra-compact 20-50 line always-injected profile using the Profile Generation Subroutine (defined at the end of this skill).
1367
+
1368
+ 6. **Present for review**: Show the user a summary of what was discovered, including the Completion Verification scores, before proceeding to Step 2.
1369
+
1370
+ **CRITICAL**:
1371
+ - Do NOT skip the YAML frontmatter metadata block — it enables incremental refresh
1372
+ - Do NOT skip the Completion Verification — it catches shallow output before it becomes permanent
1373
+ - Generate architecture.md FIRST, verify it meets thresholds, then derive .ai-context.md, then .ai-profile.md
1374
+ - All three files MUST have the metadata header at the very top
1375
+
1376
+ ---
1377
+
1378
+ > **Note:** After generating or updating `architecture.md`, run the **Completion Verification** above, then the **Condensation Subroutine** (defined at the end of this skill) to derive `.ai-context.md`.
1379
+
1380
+ ## Step 1.7: Persist State (Brownfield Only)
1381
+
1382
+ **Skip for Greenfield projects** — there are no source files to hash and no signals to classify. Greenfield projects only get `run-memory.json` (written during Completion).
1383
+
1384
+ After generating `architecture.md`, `.ai-context.md`, and `.ai-profile.md`, persist four state files to `draft/.state/` for incremental refresh and cross-session continuity.
1385
+
1386
+ ### 1.7.0 Fact Registry (`draft/.state/facts.json`)
1387
+
1388
+ Extract atomic architectural facts discovered during Phases 1-5. Each fact is a single, verifiable claim about the codebase with dual-layer timestamps and relationship edges.
1389
+
1390
+ ```json
1391
+ {
1392
+ "generated_at": "{ISO_TIMESTAMP}",
1393
+ "git_commit": "{FULL_SHA}",
1394
+ "total_facts": 0,
1395
+ "categories": ["data-flow", "architecture", "invariant", "dependency", "api", "security", "concurrency", "configuration", "testing", "convention"],
1396
+ "facts": [
1397
+ {
1398
+ "id": "fact-001",
1399
+ "category": "architecture",
1400
+ "statement": "Express app uses service layer pattern — routes delegate to services, services access repositories",
1401
+ "confidence": 0.95,
1402
+ "source_files": ["src/routes/users.ts", "src/services/user.service.ts", "src/repositories/user.repo.ts"],
1403
+ "discovered_at": "{ISO_TIMESTAMP}",
1404
+ "established_at": "{ISO_TIMESTAMP from git blame}",
1405
+ "last_verified_at": "{ISO_TIMESTAMP}",
1406
+ "last_active_at": "{ISO_TIMESTAMP from file modification}",
1407
+ "access_count": 0,
1408
+ "edges": {
1409
+ "updates": [],
1410
+ "extends": [],
1411
+ "derives": ["fact-003"],
1412
+ "superseded_by": null
1413
+ }
1414
+ }
1415
+ ]
1416
+ }
1417
+ ```
1418
+
1419
+ **Fact categories:**
1420
+ - `data-flow` — How data moves through the system
1421
+ - `architecture` — Structural patterns and module organization
1422
+ - `invariant` — Rules that must always hold true
1423
+ - `dependency` — External service and library dependencies
1424
+ - `api` — Endpoint definitions and contracts
1425
+ - `security` — Auth, authz, crypto, and access control patterns
1426
+ - `concurrency` — Thread safety, async patterns, lock ordering
1427
+ - `configuration` — Config mechanisms and critical settings
1428
+ - `testing` — Test infrastructure and patterns
1429
+ - `convention` — Coding conventions and naming patterns
1430
+
1431
+ **Target:** 50-150 facts per typical project. Focus on facts that are actionable for AI agents making code changes.
1432
+
1433
+ ### 1.7.1 Freshness State (`draft/.state/freshness.json`)
1434
+
1435
+ Compute SHA-256 hashes of all source files analyzed during Phases 1-5. This enables **file-level staleness detection** on subsequent refreshes — more granular than `synced_to_commit` which only detects that _some_ commits happened.
1436
+
1437
+ ```bash
1438
+ # Generate SHA-256 hashes for all analyzed source files (exclude draft/, node_modules/, .git/, vendor/)
1439
+ find . -type f \
1440
+ ! -path "./draft/*" ! -path "./.git/*" ! -path "*/node_modules/*" ! -path "*/vendor/*" \
1441
+ ! -path "*/__pycache__/*" ! -path "*/dist/*" ! -path "*/build/*" \
1442
+ \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \
1443
+ -o -name "*.py" -o -name "*.go" -o -name "*.rs" -o -name "*.java" -o -name "*.kt" \
1444
+ -o -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.h" \
1445
+ -o -name "*.rb" -o -name "*.php" -o -name "*.swift" -o -name "*.cs" \
1446
+ -o -name "*.proto" -o -name "*.graphql" -o -name "*.gql" \
1447
+ -o -name "*.yaml" -o -name "*.yml" -o -name "*.toml" -o -name "*.json" \
1448
+ -o -name "*.sql" -o -name "*.md" -o -name "Dockerfile" -o -name "Makefile" \) \
1449
+ -exec sha256sum {} \; 2>/dev/null | sort -k2
1450
+ ```
1451
+
1452
+ Write `draft/.state/freshness.json`:
1453
+
1454
+ ```json
1455
+ {
1456
+ "generated_at": "{ISO_TIMESTAMP}",
1457
+ "git_commit": "{FULL_SHA}",
1458
+ "total_files": 0,
1459
+ "files": {
1460
+ "src/index.ts": "sha256:a1b2c3d4...",
1461
+ "src/auth/login.ts": "sha256:e5f6a7b8...",
1462
+ "package.json": "sha256:c9d0e1f2..."
1463
+ }
1464
+ }
1465
+ ```
1466
+
1467
+ **On refresh:** Compare stored hashes against current file hashes. Files with changed/new/deleted hashes are the delta that drives targeted section updates.
1468
+
1469
+ ### 1.7.2 Signal State (`draft/.state/signals.json`)
1470
+
1471
+ Persist the signal classification from Phase 1 step 5:
1472
+
1473
+ ```json
1474
+ {
1475
+ "generated_at": "{ISO_TIMESTAMP}",
1476
+ "git_commit": "{FULL_SHA}",
1477
+ "total_files_scanned": 0,
1478
+ "signals": {
1479
+ "backend_routes": { "count": 12, "sample_files": ["src/routes/auth.ts", "src/routes/users.ts"] },
1480
+ "frontend_routes": { "count": 0, "sample_files": [] },
1481
+ "components": { "count": 0, "sample_files": [] },
1482
+ "services": { "count": 8, "sample_files": ["src/services/auth.service.ts"] },
1483
+ "data_models": { "count": 6, "sample_files": ["src/models/user.ts"] },
1484
+ "auth_files": { "count": 3, "sample_files": ["src/auth/guard.ts"] },
1485
+ "state_management": { "count": 0, "sample_files": [] },
1486
+ "background_jobs": { "count": 0, "sample_files": [] },
1487
+ "persistence": { "count": 4, "sample_files": ["src/db/repository.ts"] },
1488
+ "test_infra": { "count": 15, "sample_files": ["tests/auth.test.ts"] },
1489
+ "config_files": { "count": 5, "sample_files": [".env.example", "config/default.yml"] }
1490
+ }
1491
+ }
1492
+ ```
1493
+
1494
+ **Section relevance is derived at read-time**, not persisted. Use the signal counts and the "Drives Section(s)" column from the Phase 1 step 5 signal table to determine which architecture.md sections need deep treatment (signal count ≥ 3), brief treatment (1-2), or can be skipped (0).
1495
+
1496
+ **On refresh:** Compare current signals against stored signals. New signal categories appearing (e.g., `auth_files` going from 0→3) indicate **structural drift** — new architecture sections may need to be generated for the first time.
1497
+
1498
+ ### 1.7.3 Run Memory (`draft/.state/run-memory.json`)
1499
+
1500
+ Persist run state for cross-session continuity. If `draft:init` is interrupted mid-analysis, the next invocation can detect the incomplete run and offer to resume.
1501
+
1502
+ ```json
1503
+ {
1504
+ "run_id": "{UUID}",
1505
+ "started_at": "{ISO_TIMESTAMP}",
1506
+ "completed_at": null,
1507
+ "run_type": "init",
1508
+ "status": "in_progress",
1509
+ "phases_completed": ["phase_1", "phase_2", "phase_3"],
1510
+ "phases_remaining": ["phase_4", "phase_5"],
1511
+ "files_analyzed": 142,
1512
+ "files_generated": ["draft/architecture.md", "draft/.ai-context.md", "draft/.ai-profile.md"],
1513
+ "unresolved_questions": [
1514
+ "Could not determine if src/legacy/ is actively used or deprecated",
1515
+ "Multiple auth patterns detected — unclear which is canonical"
1516
+ ],
1517
+ "active_focus_areas": ["backend_routes", "services", "data_models"],
1518
+ "resumable_checkpoint": {
1519
+ "last_phase": "phase_3",
1520
+ "last_file_read": "src/services/billing.service.ts",
1521
+ "pending_sections": ["§14 Cross-Module Integration", "§15 Critical Invariants"]
1522
+ }
1523
+ }
1524
+ ```
1525
+
1526
+ **On completion:** Update `status` to `"completed"` and set `completed_at`. Keep `unresolved_questions` — these are surfaced to the user in the completion report and are valuable context for future refreshes.
1527
+
1528
+ **On next invocation:** If `run-memory.json` exists with `status: "in_progress"`:
1529
+ - Announce: "Detected incomplete previous run (started {started_at}, completed phases: {list}). Resume from {last_phase} or start fresh?"
1530
+ - If resume: Skip completed phases, continue from `resumable_checkpoint`
1531
+ - If fresh: Overwrite run memory and start from Phase 1
1532
+
1533
+ ---
1534
+
1535
+ ## Step 2: Product Definition
1536
+
1537
+ Create `draft/product.md` using the template from `core/templates/product.md`.
1538
+
1539
+ **Include the Standard File Metadata header at the top of the file.**
1540
+
1541
+ Engage in structured dialogue:
1542
+
1543
+ 1. **Vision**: "What does this product do and why does it matter?"
1544
+ 2. **Users**: "Who uses this? What are their primary needs?"
1545
+ 3. **Core Features**: "What are the must-have (P0), should-have (P1), and nice-to-have (P2) features?"
1546
+ 4. **Success Criteria**: "How will you measure if this product is successful?"
1547
+ 5. **Constraints**: "What technical, business, or timeline constraints exist?"
1548
+ 6. **Non-Goals**: "What is explicitly out of scope?"
1549
+
1550
+ Present for approval, iterate if needed, then write to `draft/product.md`.
1551
+
1552
+ ## Step 3: Tech Stack
1553
+
1554
+ For Brownfield projects, auto-detect from:
1555
+ - `package.json` → Node.js/TypeScript
1556
+ - `requirements.txt` / `pyproject.toml` → Python
1557
+ - `go.mod` → Go
1558
+ - `Cargo.toml` → Rust
1559
+
1560
+ Create `draft/tech-stack.md` using the template from `core/templates/tech-stack.md`.
1561
+
1562
+ **Include the Standard File Metadata header at the top of the file.**
1563
+
1564
+ Present detected stack for verification before writing.
1565
+
1566
+ ## Step 4: Workflow Configuration
1567
+
1568
+ Create `draft/workflow.md` using the template from `core/templates/workflow.md`.
1569
+
1570
+ **Include the Standard File Metadata header at the top of the file.**
1571
+
1572
+ Ask about:
1573
+ - TDD preference (strict/flexible/none)
1574
+ - Commit style and frequency
1575
+ - Validation settings (auto-validate, blocking behavior)
1576
+
1577
+ ## Step 4.1: Guardrails Configuration
1578
+
1579
+ Create `draft/guardrails.md` using the template from `core/templates/guardrails.md`.
1580
+
1581
+ **Include the Standard File Metadata header at the top of the file.**
1582
+
1583
+ The template includes general hard guardrails (Git, Code Quality, Security, Testing) — ask which to enable for this project. The Learned Conventions and Learned Anti-Patterns sections start empty — they are populated automatically by the learn step at the end of init (brownfield only) and by quality commands over time.
1584
+
1585
+ ## Step 5: Initialize Tracks
1586
+
1587
+ Create `draft/tracks.md` with metadata header:
1588
+
1589
+ ```markdown
1590
+ ---
1591
+ project: "{PROJECT_NAME}"
1592
+ module: "root"
1593
+ generated_by: "draft:init"
1594
+ generated_at: "{ISO_TIMESTAMP}"
1595
+ git:
1596
+ branch: "{LOCAL_BRANCH}"
1597
+ remote: "{REMOTE/BRANCH}"
1598
+ commit: "{FULL_SHA}"
1599
+ commit_short: "{SHORT_SHA}"
1600
+ commit_date: "{COMMIT_DATE}"
1601
+ commit_message: "{COMMIT_MESSAGE}"
1602
+ dirty: {true|false}
1603
+ synced_to_commit: "{FULL_SHA}"
1604
+ ---
1605
+
1606
+ # Tracks
1607
+
1608
+ ## Active
1609
+ <!-- No active tracks -->
1610
+
1611
+ ## Completed
1612
+ <!-- No completed tracks -->
1613
+
1614
+ ## Archived
1615
+ <!-- No archived tracks -->
1616
+ ```
1617
+
1618
+ ## Step 6: Create Directory Structure
1619
+
1620
+ ```bash
1621
+ mkdir -p draft/tracks draft/.state
1622
+ ```
1623
+
1624
+ ## Step 7: Pattern Discovery (Brownfield Only)
1625
+
1626
+ For **brownfield** projects, run `/draft:learn` (no arguments — full codebase scan) to populate `draft/guardrails.md` with initial learned conventions and anti-patterns. This ensures quality commands (`/draft:bughunt`, `/draft:review`, `/draft:deep-review`) have guardrails data from the first run.
1627
+
1628
+ **Skip this step for greenfield projects** — there is no existing codebase to scan.
1629
+
1630
+ > **Note:** This is the same full scan that `/draft:learn` performs when run standalone. The guardrails can be further refined later with `/draft:learn promote` or by quality commands that discover new patterns.
1631
+
1632
+ ---
1633
+
1634
+ ## Completion
1635
+
1636
+ **Finalize run memory:** Update `draft/.state/run-memory.json`:
1637
+ - `status`: `"completed"`
1638
+ - `completed_at`: current ISO timestamp
1639
+ - Preserve `unresolved_questions` — these are displayed in the completion report below
1640
+
1641
+ For **Brownfield** projects, announce:
1642
+ "Draft initialized successfully with comprehensive analysis!
1643
+
1644
+ Created:
1645
+ - draft/.ai-profile.md (20-50 lines — ultra-compact always-injected profile, Tier 0)
1646
+ - draft/.ai-context.md (200-400 lines — token-optimized AI context, self-contained, Tier 1)
1647
+ - draft/architecture.md (comprehensive human-readable engineering reference, Tier 2)
1648
+ - draft/product.md
1649
+ - draft/tech-stack.md
1650
+ - draft/workflow.md
1651
+ - draft/guardrails.md (populated with learned conventions and anti-patterns from codebase scan)
1652
+ - draft/tracks.md
1653
+ - draft/.state/facts.json (atomic fact registry with knowledge graph edges)
1654
+ - draft/.state/freshness.json (file-level hash baseline for incremental refresh)
1655
+ - draft/.state/signals.json (codebase signal classification)
1656
+ - draft/.state/run-memory.json (run metadata and unresolved questions)
1657
+
1658
+ {Include /draft:learn summary report here — conventions learned, anti-patterns detected, skipped entries}
1659
+
1660
+ {If unresolved_questions is non-empty, show:}
1661
+ Unresolved questions from analysis:
1662
+ {list each question — these are areas where the AI couldn't determine the answer with confidence}
1663
+
1664
+ Next steps:
1665
+ 1. Review draft/product.md — verify product vision, users, and goals reflect current reality
1666
+ 2. Review draft/tech-stack.md — verify languages, frameworks, and accepted patterns are accurate
1667
+ 3. Review draft/workflow.md — verify TDD, commit, and review settings match your team's process
1668
+ 4. Review draft/guardrails.md — verify learned conventions and anti-patterns are accurate
1669
+ 5. Review draft/.ai-context.md — verify the AI context is complete and accurate
1670
+ 6. Review draft/architecture.md — human-friendly version for team onboarding
1671
+ 7. Run `/draft:new-track` to start planning a feature
1672
+ 8. Run `/draft:init refresh` after significant codebase changes — refresh is now incremental (only stale files re-analyzed)
1673
+ 9. Run `/draft:learn promote` to promote high-confidence patterns to Hard Guardrails"
1674
+
1675
+ For **Greenfield** projects, announce:
1676
+ "Draft initialized successfully!
1677
+
1678
+ Created:
1679
+ - draft/product.md
1680
+ - draft/tech-stack.md
1681
+ - draft/workflow.md
1682
+ - draft/guardrails.md
1683
+ - draft/tracks.md
1684
+ - draft/.state/run-memory.json (run metadata)
1685
+
1686
+ Next steps:
1687
+ 1. Review draft/product.md — verify product vision, users, and goals reflect current reality
1688
+ 2. Review draft/tech-stack.md — verify languages, frameworks, and accepted patterns are accurate
1689
+ 3. Review draft/workflow.md — verify TDD, commit, and review settings match your team's process
1690
+ 4. Review draft/guardrails.md — configure hard guardrails for your project
1691
+ 5. Run `/draft:new-track` to start planning a feature
1692
+ 6. Run `/draft:init refresh` after adding substantial code — this will generate architecture context and auto-run `/draft:learn` to populate guardrails"
1693
+
1694
+ ---
1695
+
1696
+ ## Condensation Subroutine
1697
+
1698
+ A self-contained procedure for generating `draft/.ai-context.md` from `draft/architecture.md`. Any skill that mutates `architecture.md` should execute this subroutine afterward to keep derived context in sync.
1699
+
1700
+ **Authoritative definition** lives at `core/shared/condensation.md` (already inlined into integrations). It covers inputs, outputs, tier-scaled budgets, the META/GRAPH/INVARIANTS/INTERFACES/CATALOG/THREADS/CONFIG/ERRORS/EXTEND sections, and the GRAPH:MODULE-HOTSPOTS / GRAPH:FAN-IN / GRAPH:PROTO-MAP enrichments.
1701
+
1702
+ After running condensation, also run the **Profile Generation Subroutine** below to regenerate `draft/.ai-profile.md`.
1703
+
1704
+
1705
+ ## Profile Generation Subroutine
1706
+
1707
+ This is a self-contained procedure for generating `draft/.ai-profile.md` from `draft/.ai-context.md`. Run after every Condensation Subroutine execution.
1708
+
1709
+ ### Purpose
1710
+
1711
+ The profile is the **Tier 0 context** — an ultra-compact 20-50 line file always loaded by every Draft command. It provides the absolute minimum context needed for simple tasks (quick edits, config changes, small fixes) without requiring the full `.ai-context.md`.
1712
+
1713
+ ### Procedure
1714
+
1715
+ #### Step 1: Read Source
1716
+
1717
+ Read `draft/.ai-context.md`. Extract the YAML frontmatter metadata block.
1718
+
1719
+ #### Step 2: Write YAML Frontmatter
1720
+
1721
+ Start `draft/.ai-profile.md` with an updated YAML frontmatter block. Copy all `git.*` and `synced_to_commit` fields. Set:
1722
+ - `generated_by`: the calling command (e.g., `draft:init`, `draft:implement`)
1723
+ - `generated_at`: current ISO 8601 timestamp
1724
+
1725
+ #### Step 3: Extract Profile Content
1726
+
1727
+ From `.ai-context.md`, extract:
1728
+
1729
+ 1. **Stack** — Language, framework, database, auth method, API style, test framework, deploy target, build command, entry point (from `## META`)
1730
+ 2. **INVARIANTS** — Top 3-5 critical invariants with `file:line` references (from `## INVARIANTS`)
1731
+ 3. **NEVER** — 2-3 safety rules — the most dangerous things that must never happen (from `## INVARIANTS` or architecture.md safety rules)
1732
+ 4. **Active Tracks** — List of currently active track IDs and one-line descriptions (from `draft/tracks.md`)
1733
+ 5. **Recent Changes** — Last 3-5 significant commits (from `git log --oneline -5`)
1734
+
1735
+ #### Step 4: Write Output
1736
+
1737
+ Write to `draft/.ai-profile.md` using the template from `core/templates/ai-profile.md`.
1738
+
1739
+ #### Step 5: Size Check
1740
+
1741
+ - **Minimum**: 20 lines
1742
+ - **Maximum**: 50 lines
1743
+ - If over 50 lines, trim Recent Changes and reduce INVARIANTS to top 3
1744
+
1745
+ ---
1746
+
1747
+ ## Cross-Skill Dispatch
1748
+
1749
+ After initialization completes, suggest relevant follow-up skills based on project type:
1750
+
1751
+ ### Brownfield Projects (Debt Signals Detected)
1752
+
1753
+ If during architecture discovery (Step 1.5), anti-patterns or technical debt signals are detected in signal classification:
1754
+
1755
+ ```
1756
+ "Detected architectural debt patterns in this codebase. Consider running:
1757
+ → /draft:tech-debt — Catalog and prioritize existing technical debt"
1758
+ ```
1759
+
1760
+ ### All Projects (Post-Init Suggestions)
1761
+
1762
+ At completion (Step 6), after announcing next steps, present categorized follow-up skills:
1763
+
1764
+ ```
1765
+ What's Next:
1766
+ ─────────────────────────────
1767
+ Start building:
1768
+ → /draft:new-track "description" — Start a feature, bug fix, or refactor
1769
+
1770
+ Quality & Testing:
1771
+ → /draft:testing-strategy — Establish test coverage targets and testing pyramid
1772
+ → /draft:tech-debt — Catalog technical debt (recommended for brownfield projects)
1773
+
1774
+ Documentation:
1775
+ → /draft:documentation readme — Generate README from discovered context
1776
+
1777
+ Debugging & Operations:
1778
+ → /draft:debug — Investigate a specific bug
1779
+ → /draft:standup — Generate standup from recent activity
1780
+ ```
1781
+
1782
+ ### Jira Sync
1783
+
1784
+ If Jira MCP is available and a project ticket is linked, sync initialization artifacts via `core/shared/jira-sync.md`.