spec-gen-cli 1.2.6 → 1.2.8

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 (198) hide show
  1. package/README.md +175 -55
  2. package/dist/api/analyze.d.ts.map +1 -1
  3. package/dist/api/analyze.js +6 -1
  4. package/dist/api/analyze.js.map +1 -1
  5. package/dist/api/audit.d.ts +10 -0
  6. package/dist/api/audit.d.ts.map +1 -0
  7. package/dist/api/audit.js +117 -0
  8. package/dist/api/audit.js.map +1 -0
  9. package/dist/api/generate.d.ts.map +1 -1
  10. package/dist/api/generate.js +10 -1
  11. package/dist/api/generate.js.map +1 -1
  12. package/dist/api/index.d.ts +3 -2
  13. package/dist/api/index.d.ts.map +1 -1
  14. package/dist/api/index.js +1 -0
  15. package/dist/api/index.js.map +1 -1
  16. package/dist/api/run.d.ts.map +1 -1
  17. package/dist/api/run.js +5 -1
  18. package/dist/api/run.js.map +1 -1
  19. package/dist/api/types.d.ts +15 -4
  20. package/dist/api/types.d.ts.map +1 -1
  21. package/dist/cli/commands/analyze.d.ts +3 -0
  22. package/dist/cli/commands/analyze.d.ts.map +1 -1
  23. package/dist/cli/commands/analyze.js +112 -17
  24. package/dist/cli/commands/analyze.js.map +1 -1
  25. package/dist/cli/commands/audit.d.ts +9 -0
  26. package/dist/cli/commands/audit.d.ts.map +1 -0
  27. package/dist/cli/commands/audit.js +98 -0
  28. package/dist/cli/commands/audit.js.map +1 -0
  29. package/dist/cli/commands/drift.d.ts.map +1 -1
  30. package/dist/cli/commands/drift.js +8 -10
  31. package/dist/cli/commands/drift.js.map +1 -1
  32. package/dist/cli/commands/generate.d.ts.map +1 -1
  33. package/dist/cli/commands/generate.js +15 -37
  34. package/dist/cli/commands/generate.js.map +1 -1
  35. package/dist/cli/commands/mcp.d.ts +102 -2
  36. package/dist/cli/commands/mcp.d.ts.map +1 -1
  37. package/dist/cli/commands/mcp.js +134 -2
  38. package/dist/cli/commands/mcp.js.map +1 -1
  39. package/dist/cli/commands/run.d.ts.map +1 -1
  40. package/dist/cli/commands/run.js +9 -47
  41. package/dist/cli/commands/run.js.map +1 -1
  42. package/dist/cli/commands/setup.d.ts +17 -0
  43. package/dist/cli/commands/setup.d.ts.map +1 -0
  44. package/dist/cli/commands/setup.js +201 -0
  45. package/dist/cli/commands/setup.js.map +1 -0
  46. package/dist/cli/commands/verify.d.ts.map +1 -1
  47. package/dist/cli/commands/verify.js +7 -8
  48. package/dist/cli/commands/verify.js.map +1 -1
  49. package/dist/cli/index.js +14 -8
  50. package/dist/cli/index.js.map +1 -1
  51. package/dist/constants.d.ts +14 -0
  52. package/dist/constants.d.ts.map +1 -1
  53. package/dist/constants.js +14 -0
  54. package/dist/constants.js.map +1 -1
  55. package/dist/core/analyzer/ai-config-generator.d.ts +54 -0
  56. package/dist/core/analyzer/ai-config-generator.d.ts.map +1 -0
  57. package/dist/core/analyzer/ai-config-generator.js +85 -0
  58. package/dist/core/analyzer/ai-config-generator.js.map +1 -0
  59. package/dist/core/analyzer/artifact-generator.d.ts +27 -2
  60. package/dist/core/analyzer/artifact-generator.d.ts.map +1 -1
  61. package/dist/core/analyzer/artifact-generator.js +86 -8
  62. package/dist/core/analyzer/artifact-generator.js.map +1 -1
  63. package/dist/core/analyzer/codebase-digest.d.ts.map +1 -1
  64. package/dist/core/analyzer/codebase-digest.js +12 -11
  65. package/dist/core/analyzer/codebase-digest.js.map +1 -1
  66. package/dist/core/analyzer/env-extractor.d.ts +33 -0
  67. package/dist/core/analyzer/env-extractor.d.ts.map +1 -0
  68. package/dist/core/analyzer/env-extractor.js +196 -0
  69. package/dist/core/analyzer/env-extractor.js.map +1 -0
  70. package/dist/core/analyzer/http-route-parser.d.ts +36 -1
  71. package/dist/core/analyzer/http-route-parser.d.ts.map +1 -1
  72. package/dist/core/analyzer/http-route-parser.js +276 -0
  73. package/dist/core/analyzer/http-route-parser.js.map +1 -1
  74. package/dist/core/analyzer/middleware-extractor.d.ts +29 -0
  75. package/dist/core/analyzer/middleware-extractor.d.ts.map +1 -0
  76. package/dist/core/analyzer/middleware-extractor.js +195 -0
  77. package/dist/core/analyzer/middleware-extractor.js.map +1 -0
  78. package/dist/core/analyzer/schema-extractor.d.ts +41 -0
  79. package/dist/core/analyzer/schema-extractor.d.ts.map +1 -0
  80. package/dist/core/analyzer/schema-extractor.js +229 -0
  81. package/dist/core/analyzer/schema-extractor.js.map +1 -0
  82. package/dist/core/analyzer/spec-snapshot-generator.d.ts +17 -0
  83. package/dist/core/analyzer/spec-snapshot-generator.d.ts.map +1 -0
  84. package/dist/core/analyzer/spec-snapshot-generator.js +201 -0
  85. package/dist/core/analyzer/spec-snapshot-generator.js.map +1 -0
  86. package/dist/core/analyzer/ui-component-extractor.d.ts +43 -0
  87. package/dist/core/analyzer/ui-component-extractor.d.ts.map +1 -0
  88. package/dist/core/analyzer/ui-component-extractor.js +245 -0
  89. package/dist/core/analyzer/ui-component-extractor.js.map +1 -0
  90. package/dist/core/generator/openspec-format-generator.d.ts.map +1 -1
  91. package/dist/core/generator/openspec-format-generator.js +8 -0
  92. package/dist/core/generator/openspec-format-generator.js.map +1 -1
  93. package/dist/core/generator/spec-pipeline.d.ts +9 -0
  94. package/dist/core/generator/spec-pipeline.d.ts.map +1 -1
  95. package/dist/core/generator/spec-pipeline.js +94 -2
  96. package/dist/core/generator/spec-pipeline.js.map +1 -1
  97. package/dist/core/generator/stages/stage1-survey.d.ts.map +1 -1
  98. package/dist/core/generator/stages/stage1-survey.js +43 -0
  99. package/dist/core/generator/stages/stage1-survey.js.map +1 -1
  100. package/dist/core/generator/stages/stage2-entities.d.ts.map +1 -1
  101. package/dist/core/generator/stages/stage2-entities.js +6 -2
  102. package/dist/core/generator/stages/stage2-entities.js.map +1 -1
  103. package/dist/core/generator/stages/stage3-services.d.ts.map +1 -1
  104. package/dist/core/generator/stages/stage3-services.js +9 -2
  105. package/dist/core/generator/stages/stage3-services.js.map +1 -1
  106. package/dist/core/generator/stages/stage4-api.d.ts.map +1 -1
  107. package/dist/core/generator/stages/stage4-api.js +6 -2
  108. package/dist/core/generator/stages/stage4-api.js.map +1 -1
  109. package/dist/core/services/llm-service.d.ts +26 -10
  110. package/dist/core/services/llm-service.d.ts.map +1 -1
  111. package/dist/core/services/llm-service.js +171 -16
  112. package/dist/core/services/llm-service.js.map +1 -1
  113. package/dist/core/services/mcp-handlers/analysis.d.ts +32 -1
  114. package/dist/core/services/mcp-handlers/analysis.d.ts.map +1 -1
  115. package/dist/core/services/mcp-handlers/analysis.js +185 -2
  116. package/dist/core/services/mcp-handlers/analysis.js.map +1 -1
  117. package/dist/core/verifier/verification-engine.d.ts +67 -6
  118. package/dist/core/verifier/verification-engine.d.ts.map +1 -1
  119. package/dist/core/verifier/verification-engine.js +316 -90
  120. package/dist/core/verifier/verification-engine.js.map +1 -1
  121. package/dist/types/index.d.ts +70 -1
  122. package/dist/types/index.d.ts.map +1 -1
  123. package/dist/types/pipeline.d.ts +9 -0
  124. package/dist/types/pipeline.d.ts.map +1 -1
  125. package/dist/utils/command-helpers.d.ts +30 -0
  126. package/dist/utils/command-helpers.d.ts.map +1 -1
  127. package/dist/utils/command-helpers.js +69 -1
  128. package/dist/utils/command-helpers.js.map +1 -1
  129. package/examples/bmad/README.md +113 -0
  130. package/examples/bmad/agents/architect.md +226 -0
  131. package/examples/bmad/agents/dev-brownfield.md +69 -0
  132. package/examples/bmad/setup/architect.customize.yaml +14 -0
  133. package/examples/bmad/tasks/implement-story.md +254 -0
  134. package/examples/bmad/tasks/onboarding.md +169 -0
  135. package/examples/bmad/tasks/refactor.md +178 -0
  136. package/examples/bmad/tasks/sprint-planning.md +168 -0
  137. package/examples/bmad/templates/story.md +108 -0
  138. package/examples/cline-workflows/spec-gen-analyze-codebase.md +100 -0
  139. package/examples/cline-workflows/spec-gen-check-spec-drift.md +102 -0
  140. package/examples/cline-workflows/spec-gen-execute-refactor.md +194 -0
  141. package/examples/cline-workflows/spec-gen-implement-feature.md +238 -0
  142. package/examples/cline-workflows/spec-gen-plan-refactor.md +255 -0
  143. package/examples/cline-workflows/spec-gen-refactor-codebase.md +16 -0
  144. package/examples/drift-demo/openspec/config.yaml +14 -0
  145. package/examples/drift-demo/openspec/specs/architecture/spec.md +30 -0
  146. package/examples/drift-demo/openspec/specs/auth/spec.md +71 -0
  147. package/examples/drift-demo/openspec/specs/database/spec.md +33 -0
  148. package/examples/drift-demo/openspec/specs/overview/spec.md +20 -0
  149. package/examples/drift-demo/openspec/specs/projects/spec.md +55 -0
  150. package/examples/drift-demo/openspec/specs/tasks/spec.md +78 -0
  151. package/examples/drift-demo/package.json +21 -0
  152. package/examples/drift-demo/src/auth/auth-middleware.ts +30 -0
  153. package/examples/drift-demo/src/auth/auth-routes.ts +29 -0
  154. package/examples/drift-demo/src/auth/auth-service.ts +45 -0
  155. package/examples/drift-demo/src/database/connection.ts +27 -0
  156. package/examples/drift-demo/src/index.ts +16 -0
  157. package/examples/drift-demo/src/projects/project-model.ts +15 -0
  158. package/examples/drift-demo/src/projects/project-service.ts +34 -0
  159. package/examples/drift-demo/src/tasks/task-model.ts +37 -0
  160. package/examples/drift-demo/src/tasks/task-routes.ts +53 -0
  161. package/examples/drift-demo/src/tasks/task-service.ts +60 -0
  162. package/examples/drift-demo/src/utils/validation.ts +11 -0
  163. package/examples/drift-demo/tests/auth.test.ts +4 -0
  164. package/examples/drift-demo/tests/tasks.test.ts +4 -0
  165. package/examples/drift-demo/tsconfig.json +10 -0
  166. package/examples/drift-test/run-drift-test.sh +1087 -0
  167. package/examples/gsd/README.md +119 -0
  168. package/examples/gsd/commands/gsd/spec-gen-drift.md +111 -0
  169. package/examples/gsd/commands/gsd/spec-gen-orient.md +191 -0
  170. package/examples/mistral-vibe/README.md +101 -0
  171. package/examples/mistral-vibe/antipatterns-template.md +18 -0
  172. package/examples/mistral-vibe/skills/spec-gen-analyze-codebase/SKILL.md +123 -0
  173. package/examples/mistral-vibe/skills/spec-gen-brainstorm/SKILL.md +379 -0
  174. package/examples/mistral-vibe/skills/spec-gen-debug/SKILL.md +320 -0
  175. package/examples/mistral-vibe/skills/spec-gen-execute-refactor/SKILL.md +210 -0
  176. package/examples/mistral-vibe/skills/spec-gen-generate/SKILL.md +245 -0
  177. package/examples/mistral-vibe/skills/spec-gen-implement-story/SKILL.md +274 -0
  178. package/examples/mistral-vibe/skills/spec-gen-plan-refactor/SKILL.md +251 -0
  179. package/examples/openspec-analysis/README.md +59 -0
  180. package/examples/openspec-analysis/SUMMARY.md +72 -0
  181. package/examples/openspec-analysis/config.json +16 -0
  182. package/examples/openspec-analysis/dependencies.mermaid +35 -0
  183. package/examples/openspec-analysis/dependency-graph.json +12116 -0
  184. package/examples/openspec-analysis/llm-context.json +119 -0
  185. package/examples/openspec-analysis/repo-structure.json +871 -0
  186. package/examples/openspec-cli/README.md +67 -0
  187. package/examples/openspec-cli/openspec/config.yaml +26 -0
  188. package/examples/openspec-cli/openspec/specs/architecture/spec.md +178 -0
  189. package/examples/openspec-cli/openspec/specs/artifact-graph/spec.md +143 -0
  190. package/examples/openspec-cli/openspec/specs/cli/spec.md +138 -0
  191. package/examples/openspec-cli/openspec/specs/overview/spec.md +60 -0
  192. package/examples/openspec-cli/openspec/specs/parsing/spec.md +123 -0
  193. package/examples/openspec-cli/openspec/specs/validation/spec.md +108 -0
  194. package/examples/spec-kit/README.md +104 -0
  195. package/examples/spec-kit/commands/drift.md +87 -0
  196. package/examples/spec-kit/commands/orient.md +138 -0
  197. package/examples/spec-kit/extension.yml +54 -0
  198. package/package.json +3 -6
@@ -0,0 +1,100 @@
1
+ # spec-gen: Analyze Codebase
2
+
3
+ Run a full static analysis of a project using spec-gen and summarise the results:
4
+ architecture, call graph, top refactoring issues, and duplicate code — no LLM required.
5
+
6
+ ## Step 1: Get the project directory
7
+
8
+ Ask the user which project to analyse, or confirm we should use the current workspace root.
9
+
10
+ <ask_followup_question>
11
+ <question>Which project directory should I analyse?</question>
12
+ <options>["Current workspace root", "Enter a different path"]</options>
13
+ </ask_followup_question>
14
+
15
+ ## Step 2: Run static analysis
16
+
17
+ Call analyze_codebase on the chosen directory.
18
+
19
+ <use_mcp_tool>
20
+ <server_name>spec-gen</server_name>
21
+ <tool_name>analyze_codebase</tool_name>
22
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
23
+ </use_mcp_tool>
24
+
25
+ ## Step 3: Get the architecture overview
26
+
27
+ Retrieve the high-level architecture map: clusters, cross-cluster dependencies,
28
+ entry points, and critical hub functions. This is the fastest way to orient
29
+ yourself before diving into details.
30
+
31
+ <use_mcp_tool>
32
+ <server_name>spec-gen</server_name>
33
+ <tool_name>get_architecture_overview</tool_name>
34
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
35
+ </use_mcp_tool>
36
+
37
+ Present a concise summary:
38
+ - Total files, clusters, edges, cycles, and layer violations
39
+ - Top 3 clusters sorted by file count, with their role (entry_layer / orchestrator /
40
+ hub_heavy / internal) and which clusters they depend on
41
+ - Global entry points (the public-facing roots of the codebase)
42
+ - Critical hub functions (high fan-in — touch with care)
43
+
44
+ ## Step 4: Summarise the refactor report
45
+
46
+ Present a concise summary:
47
+ - Project type and detected frameworks
48
+ - File count, function count, internal call count
49
+ - Top 5 refactoring issues (function name, file, issue type, priority score)
50
+ - Detected domains
51
+
52
+ ## Step 5: Show the call graph
53
+
54
+ Retrieve hub functions, entry points, and any layer violations.
55
+
56
+ <use_mcp_tool>
57
+ <server_name>spec-gen</server_name>
58
+ <tool_name>get_call_graph</tool_name>
59
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
60
+ </use_mcp_tool>
61
+
62
+ Highlight any hub functions (fanIn ≥ 8) or layer violations detected.
63
+
64
+ ## Step 6: Show duplicate code report
65
+
66
+ Retrieve the duplicate function analysis (Types 1–3, pure static analysis).
67
+
68
+ <use_mcp_tool>
69
+ <server_name>spec-gen</server_name>
70
+ <tool_name>get_duplicate_report</tool_name>
71
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
72
+ </use_mcp_tool>
73
+
74
+ Present a concise summary:
75
+ - Overall duplication ratio (e.g. "12% of functions are duplicated")
76
+ - Top 3 clone groups sorted by impact (instances × line count):
77
+ - Clone type (exact / structural / near) and similarity score
78
+ - List of instances (file + function name + line range)
79
+ - If no duplicates found, note this as a positive signal
80
+
81
+ ## Step 7: Suggest next steps
82
+
83
+ Based on the analysis, guide the user through the natural next steps in order:
84
+ 1. Call `get_signatures` on the modules that contain the top issues to understand their public API
85
+ 2. Call `get_subgraph` on the highest-priority function to map its callers and callees
86
+ 3. Call `suggest_insertion_points` with a brief feature description to find where new
87
+ logic should land — useful before starting any new work on this codebase
88
+ 4. If significant duplication was found, suggest consolidating clone groups before refactoring
89
+ 5. Suggest running `/spec-gen-plan-refactor` once the user has enough context to act,
90
+ then `/spec-gen-execute-refactor` to apply the plan
91
+ 6. If the project has OpenSpec specs:
92
+ - Call `list_spec_domains` to discover available spec domains
93
+ - Call `search_specs` to find requirements by intent — this enables spec-first reasoning
94
+ (question → requirements → linked source files) rather than code-first exploration
95
+ - Suggest `/spec-gen-implement-feature` for any new feature work
96
+ (integrates specs + insertion points + adversarial self-check + drift check)
97
+ - To enable `search_specs`, run `spec-gen analyze --embed` or `spec-gen analyze --reindex-specs`
98
+ (requires the embedding server)
99
+ 7. If `.claude/antipatterns.md` exists, mention that it is checked automatically
100
+ by `/spec-gen-implement-feature` during the adversarial self-check step
@@ -0,0 +1,102 @@
1
+ # spec-gen: Check Spec Drift
2
+
3
+ Detect code changes that are not reflected in the project's OpenSpec specifications.
4
+ Runs in static mode — no LLM required, no quota consumed.
5
+
6
+ **Prerequisites**: `spec-gen init` and `spec-gen generate` must have been run at least once
7
+ in the target project.
8
+
9
+ ## Step 1: Get the project directory
10
+
11
+ Ask the user which project to check, or confirm the current workspace root.
12
+
13
+ <ask_followup_question>
14
+ <question>Which project directory should I check for spec drift?</question>
15
+ <options>["Current workspace root", "Enter a different path"]</options>
16
+ </ask_followup_question>
17
+
18
+ ## Step 2: Run drift detection
19
+
20
+ <use_mcp_tool>
21
+ <server_name>spec-gen</server_name>
22
+ <tool_name>check_spec_drift</tool_name>
23
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
24
+ </use_mcp_tool>
25
+
26
+ If the result contains `{ "error": "..." }`, stop and tell the user what is missing
27
+ (not a git repo / no config / no specs) with the exact remediation command to run.
28
+
29
+ ## Step 3: Present the results
30
+
31
+ ### No drift
32
+
33
+ If `hasDrift` is false and `issues` is empty:
34
+
35
+ > ✅ **No spec drift detected.** All changed files are covered by up-to-date specs.
36
+
37
+ Show `baseRef` and `totalChangedFiles` for context.
38
+
39
+ ### Drift found
40
+
41
+ Present a summary line first:
42
+
43
+ ```
44
+ Drift detected on $totalChangedFiles changed file(s) vs $baseRef
45
+ Gaps: $gaps · Stale: $stale · Uncovered: $uncovered · Orphaned: $orphanedSpecs
46
+ ```
47
+
48
+ Then a table of issues, sorted by severity (error first):
49
+
50
+ | Severity | Kind | File | Domain | Spec | Lines changed |
51
+ |---|---|---|---|---|---|
52
+ | ⛔ error | gap | src/auth/login.ts | auth | specs/auth/spec.md | +45/-12 |
53
+ | ⚠️ warning | uncovered | src/billing/invoice.ts | — | — | +120/-0 |
54
+
55
+ For each issue, show the `suggestion` field on the next line in italic.
56
+
57
+ ## Step 4: Recommend actions
58
+
59
+ Based on the issue kinds found, guide the user:
60
+
61
+ ### gap (spec exists but is outdated)
62
+ > The spec for **$domain** covers `$filePath` but wasn't updated when the code changed.
63
+ > Run `spec-gen generate --domains $domain` to regenerate it, or edit the spec manually
64
+ > if the change is minor.
65
+
66
+ ### stale (spec references deleted/moved code)
67
+ > The **$domain** spec references code that no longer exists. Either the file was deleted
68
+ > or its structure changed significantly. Run `spec-gen generate --domains $domain` to
69
+ > rebuild the spec from the current state of the code.
70
+
71
+ ### uncovered (new file with no spec)
72
+ > `$filePath` has no matching spec. If this file introduces new domain logic, run
73
+ > `spec-gen generate` so that spec-gen can infer a spec from it. If it is a utility
74
+ > or test helper, it may not need a spec.
75
+
76
+ ### orphaned-spec (spec references non-existent files)
77
+ > A spec references files that no longer exist. The spec may need to be deleted or its
78
+ > `source-files` header updated. Review the spec at `$specPath` manually.
79
+
80
+ ## Step 5: Offer to drill down
81
+
82
+ If there are `gap` or `stale` issues, offer to show the signatures of the affected files
83
+ so the user can see exactly what changed in the public API:
84
+
85
+ <use_mcp_tool>
86
+ <server_name>spec-gen</server_name>
87
+ <tool_name>get_signatures</tool_name>
88
+ <arguments>{"directory": "$DIRECTORY", "filePattern": "$AFFECTED_FILE"}</arguments>
89
+ </use_mcp_tool>
90
+
91
+ ## Step 6: Suggest next steps
92
+
93
+ Present the following options and let the user choose:
94
+
95
+ 1. **Regenerate specs for affected domains** — run `spec-gen generate --domains <list>`
96
+ (fastest fix for `gap` and `stale` issues)
97
+ 2. **Review and edit specs manually** — appropriate for minor changes where the
98
+ existing spec is mostly correct
99
+ 3. **Install the pre-commit hook** — run `spec-gen drift --install-hook` so drift is
100
+ caught automatically before every commit
101
+ 4. **Run `/spec-gen-plan-refactor` then `/spec-gen-execute-refactor`** — if the drift
102
+ reveals structural issues in the changed files
@@ -0,0 +1,194 @@
1
+ # spec-gen: Execute Refactor
2
+
3
+ Apply the refactoring plan produced by `/spec-gen-plan-refactor`. This workflow
4
+ reads `.spec-gen/refactor-plan.md` at the start and re-reads it before each
5
+ change to stay on track — especially important with small or limited models.
6
+
7
+ **Prerequisites**: run `/spec-gen-plan-refactor` first and confirm the plan.
8
+
9
+ ## Step 1: Read the plan
10
+
11
+ Read `.spec-gen/refactor-plan.md` from the project directory.
12
+
13
+ If the file does not exist, stop immediately:
14
+ > "No refactor plan found at `.spec-gen/refactor-plan.md`. Please run
15
+ > `/spec-gen-plan-refactor` first to generate a plan."
16
+
17
+ Extract and display a summary:
18
+ - Target function, file, and line range
19
+ - Strategy and risk score
20
+ - Number of changes planned
21
+ - Test command
22
+ - Acceptance criteria
23
+
24
+ Ask the user to confirm before proceeding.
25
+
26
+ ## Step 2: Establish a green baseline
27
+
28
+ Confirm the test suite is passing. Use the test command from the plan.
29
+
30
+ **If tests are already failing, stop and tell the user.** Do not proceed with
31
+ refactoring on a red baseline — the failures must be fixed first or the user
32
+ must explicitly acknowledge them.
33
+
34
+ If a coverage tool is available, run it on the target file and compare against
35
+ the coverage baseline in the plan.
36
+
37
+ | Coverage on files to touch | Recommendation |
38
+ |---|---|
39
+ | ≥ 70% lines | Safe to refactor — proceed |
40
+ | 40–69% lines | Caution — write characterisation tests for the affected functions before starting |
41
+ | < 40% lines | **Stop.** Strongly recommend writing tests first. Refactoring untested code hides regressions. |
42
+ | 0% (no tests at all) | **Block.** Do not refactor. Propose writing a minimal test harness first, then restart the workflow. |
43
+
44
+ If coverage is below 40%, tell the user:
45
+ > "Coverage on the target file is X%. Refactoring without test coverage risks
46
+ > introducing silent regressions. I recommend writing tests for the affected
47
+ > functions before proceeding. Would you like me to suggest test cases based on
48
+ > the function signatures, or do you want to proceed at your own risk?"
49
+
50
+ Only continue past this point with explicit user confirmation.
51
+
52
+ **Large file warning**: If the target function spans more than 300 lines:
53
+ > "This function is X lines long. Small models (< 13B parameters) may lose
54
+ > code when editing files of this size in a single pass. Recommended approach:
55
+ > apply Change 1 from the plan (smallest extraction) first to reduce the target
56
+ > below 200 lines before the main refactor."
57
+
58
+ ## Step 3: Set the restore point
59
+
60
+ Verify the working tree is clean, then note the restore point:
61
+
62
+ ```bash
63
+ git status # must show: nothing to commit, working tree clean
64
+ git log --oneline -1 # note this commit hash — your restore point
65
+ ```
66
+
67
+ If there are uncommitted changes, stop and ask the user to commit or stash them
68
+ first. A clean tree guarantees that `git checkout HEAD -- <file>` will fully
69
+ restore any file to its pre-edit state.
70
+
71
+ Fill in the `Restore point` section of `.spec-gen/refactor-plan.md` with the
72
+ current commit hash.
73
+
74
+ ## Step 4: Apply changes (one at a time)
75
+
76
+ **Before each change**, re-read `.spec-gen/refactor-plan.md` to confirm:
77
+ - Which change you are on
78
+ - Exactly what to extract, where to put it, and which call sites to update
79
+
80
+ **Editing tool rule**: Always prefer a targeted edit tool (`replace_in_file`,
81
+ `str_replace_based_edit`, `apply_diff`, or equivalent) over a full-file
82
+ rewrite tool (`write_to_file`). Only use `write_to_file` if the file is under
83
+ 100 lines in total. If a change seems to require `write_to_file` on a larger
84
+ file, stop and split it into smaller targeted edits instead.
85
+
86
+ **Model capability note**: If using a small model (Mistral Small, Phi, Gemma,
87
+ or any model under 13B parameters), enforce an additional constraint: each edit
88
+ must touch a contiguous block of at most 50 lines. If the intended change
89
+ exceeds this, split it into multiple smaller extractions and apply them one
90
+ by one.
91
+
92
+ For each change in the plan:
93
+
94
+ 1. **Read the source file** around the lines to extract (do not rely on memory).
95
+
96
+ 2. **Apply the edit**:
97
+ - Extract or move the identified block
98
+ - Place it in the target file and target class specified in the plan
99
+ - If the target file is new, create it with only the extracted code
100
+ - Update all call sites listed in the plan
101
+
102
+ 3. **Verify the diff** before running tests:
103
+ ```bash
104
+ git diff --stat # only the expected files should appear
105
+ git diff # scan deleted lines (-) and confirm each removal is
106
+ # intentional — moved to a new function or file,
107
+ # not silently dropped.
108
+ # If deleted lines >> added lines with no new file
109
+ # created, code was likely lost — abort immediately.
110
+ ```
111
+
112
+ 4. **Run the test suite** (use the command from the plan). If any test fails,
113
+ restore the file right away:
114
+ ```bash
115
+ git checkout HEAD -- <file>
116
+ ```
117
+ Do NOT accumulate broken state before restoring.
118
+
119
+ 5. **Mark the change as done** in `.spec-gen/refactor-plan.md` by appending
120
+ `✅` to the change heading, then proceed to the next one.
121
+
122
+ Repeat until all changes in the plan are marked ✅.
123
+
124
+ ## Step 5: Verify improvement
125
+
126
+ Re-analyse to confirm the priority score dropped for the refactored function.
127
+
128
+ <use_mcp_tool>
129
+ <server_name>spec-gen</server_name>
130
+ <tool_name>analyze_codebase</tool_name>
131
+ <arguments>{"directory": "$DIRECTORY", "force": true}</arguments>
132
+ </use_mcp_tool>
133
+
134
+ <use_mcp_tool>
135
+ <server_name>spec-gen</server_name>
136
+ <tool_name>get_refactor_report</tool_name>
137
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
138
+ </use_mcp_tool>
139
+
140
+ Check each acceptance criterion from the plan:
141
+ - Priority score dropped below the target
142
+ - Function is no longer in the top-5 list
143
+ - Full test suite passes
144
+
145
+ If not, investigate why and iterate (add a new change to the plan if needed).
146
+
147
+ Run the full test suite one final time to confirm the refactored state is clean.
148
+
149
+ ## Step 6 (optional — requires spec-gen generate to have been run)
150
+
151
+ **Important**: this step proposes irreversible changes (deletions, renames).
152
+ Do not apply anything without explicit user confirmation at each sub-step.
153
+
154
+ ### 6a. Dead code: orphan functions
155
+
156
+ Check for functions not covered by any spec requirement.
157
+
158
+ <use_mcp_tool>
159
+ <server_name>spec-gen</server_name>
160
+ <tool_name>get_mapping</tool_name>
161
+ <arguments>{"directory": "$DIRECTORY", "orphansOnly": true}</arguments>
162
+ </use_mcp_tool>
163
+
164
+ Present the orphan list (kind `function` or `class` only). For each one, check:
165
+ - Is it exported and potentially consumed by external code?
166
+ - Is it re-exported from an index file?
167
+ - Was it simply missed by the LLM in Stage 3?
168
+
169
+ Only after the user has reviewed and confirmed each entry, propose deletion or
170
+ a documentation comment marking it as intentionally uncovered.
171
+
172
+ **Do not delete anything without the user explicitly approving each function.**
173
+
174
+ ### 6b. Naming alignment: spec vocabulary vs actual names
175
+
176
+ Find functions whose names diverge from the business vocabulary in the spec.
177
+
178
+ <use_mcp_tool>
179
+ <server_name>spec-gen</server_name>
180
+ <tool_name>get_mapping</tool_name>
181
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
182
+ </use_mcp_tool>
183
+
184
+ Build a table of mismatches (requirement name vs function name) and present it
185
+ to the user for review before touching any code:
186
+
187
+ | Current name | Proposed name | File | Confidence |
188
+ |---|---|---|---|
189
+
190
+ Only renames with `confidence: "llm"` should be proposed automatically.
191
+ Flag `confidence: "heuristic"` entries for manual verification first.
192
+
193
+ **Wait for explicit user approval of the full rename table before applying
194
+ any change. Apply renames one file at a time and run tests after each.**
@@ -0,0 +1,238 @@
1
+ # spec-gen: Implement Feature
2
+
3
+ Plan and implement a new feature with full architectural context:
4
+ architecture overview → OpenSpec requirements → insertion points → implementation → drift check.
5
+
6
+ No code is written until Step 6. Steps 1–5 are read-only analysis.
7
+
8
+ ## Step 1: Get the project directory and feature description
9
+
10
+ Ask the user:
11
+
12
+ <ask_followup_question>
13
+ <question>Which project directory and what feature should I implement?</question>
14
+ <options>["Current workspace root", "Enter a different path"]</options>
15
+ </ask_followup_question>
16
+
17
+ Also ask for a brief description of the feature if not already provided (1–3 sentences).
18
+ Store it as `$FEATURE_DESCRIPTION`.
19
+
20
+ If `.claude/antipatterns.md` exists in the project, read it and store as `$ANTIPATTERNS`.
21
+ This list will be cross-checked at Step 5b.
22
+
23
+ ## Step 2: Get the architecture overview
24
+
25
+ Orient yourself before touching any code.
26
+
27
+ <use_mcp_tool>
28
+ <server_name>spec-gen</server_name>
29
+ <tool_name>get_architecture_overview</tool_name>
30
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
31
+ </use_mcp_tool>
32
+
33
+ From the result, note:
34
+ - Which cluster(s) the feature most likely belongs to (based on role and name)
35
+ - Critical hub functions to avoid touching unnecessarily (high fan-in → high blast radius)
36
+ - Existing entry points — the feature may need to hook in at one of them
37
+
38
+ If analysis data is missing (`{ "error": "..." }`), run `analyze_codebase` first:
39
+
40
+ <use_mcp_tool>
41
+ <server_name>spec-gen</server_name>
42
+ <tool_name>analyze_codebase</tool_name>
43
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
44
+ </use_mcp_tool>
45
+
46
+ Then retry `get_architecture_overview`.
47
+
48
+ ## Step 2.5: Stack inventory (conditional)
49
+
50
+ Based on the feature description and architecture overview results, call the relevant inventory tool(s) before reading any source file. Skip if the feature clearly involves none of these areas.
51
+
52
+ | Feature involves | Tool |
53
+ |---|---|
54
+ | Data models / ORM / database / tables | `get_schema_inventory` |
55
+ | HTTP routes / API / endpoints | `get_route_inventory` |
56
+ | Config / env vars / secrets | `get_env_vars` |
57
+ | UI components | `get_ui_components` |
58
+
59
+ <use_mcp_tool>
60
+ <server_name>spec-gen</server_name>
61
+ <tool_name>get_schema_inventory</tool_name>
62
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
63
+ </use_mcp_tool>
64
+
65
+ Use results to ground the implementation plan in existing schemas/routes — don't re-create what already exists.
66
+
67
+ ---
68
+
69
+ ## Step 2.6: Audit spec coverage of the target domain
70
+
71
+ Run a parity audit to check if the domain you're about to touch has spec gaps.
72
+
73
+ <use_mcp_tool>
74
+ <server_name>spec-gen</server_name>
75
+ <tool_name>audit_spec_coverage</tool_name>
76
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
77
+ </use_mcp_tool>
78
+
79
+ From the result, check:
80
+ - `staleDomains` — if the target domain appears here, its spec is outdated.
81
+ Recommend running `spec-gen generate --domains $DOMAIN` before implementing.
82
+ - `hubGaps` — uncovered hub functions. If the feature touches one of these,
83
+ add it to the adversarial check in Step 5b (high blast radius + no spec = risk).
84
+
85
+ If both are clean, continue to Step 3 without action.
86
+
87
+ ## Step 3: Search the OpenSpec specifications (if available)
88
+
89
+ Discover which spec domains exist, then search for requirements relevant to the feature.
90
+
91
+ <use_mcp_tool>
92
+ <server_name>spec-gen</server_name>
93
+ <tool_name>list_spec_domains</tool_name>
94
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
95
+ </use_mcp_tool>
96
+
97
+ If domains are found, search the specs semantically:
98
+
99
+ <use_mcp_tool>
100
+ <server_name>spec-gen</server_name>
101
+ <tool_name>search_specs</tool_name>
102
+ <arguments>{"directory": "$DIRECTORY", "query": "$FEATURE_DESCRIPTION", "limit": 5}</arguments>
103
+ </use_mcp_tool>
104
+
105
+ From the results, extract:
106
+ - Existing requirements that relate to the feature (note their `id` for drift tracking)
107
+ - Any constraints or acceptance criteria already documented
108
+ - The `linkedFiles` — these are the source files already mapped to those requirements
109
+ (will be highlighted in the diagram viewer)
110
+
111
+ If `search_specs` returns an index-not-found error, fall back to reading the spec file
112
+ directly: `openspec/specs/<domain>/spec.md`.
113
+
114
+ If no spec exists yet, note it — the feature will be "uncovered" and `check_spec_drift`
115
+ will flag it after implementation. That is expected: propose running `spec-gen generate`
116
+ after the feature lands.
117
+
118
+ ## Step 4: Find insertion points
119
+
120
+ Identify the best functions and files to extend or hook into.
121
+
122
+ <use_mcp_tool>
123
+ <server_name>spec-gen</server_name>
124
+ <tool_name>suggest_insertion_points</tool_name>
125
+ <arguments>{"directory": "$DIRECTORY", "query": "$FEATURE_DESCRIPTION", "limit": 5}</arguments>
126
+ </use_mcp_tool>
127
+
128
+ For each candidate, present:
129
+ - Rank, name, file, role, strategy, reason
130
+ - Whether it appears in the relevant cluster identified in Step 2
131
+
132
+ Then pick the top 1–2 candidates and inspect their call neighbourhood:
133
+
134
+ <use_mcp_tool>
135
+ <server_name>spec-gen</server_name>
136
+ <tool_name>get_subgraph</tool_name>
137
+ <arguments>{"directory": "$DIRECTORY", "functionName": "$TOP_CANDIDATE", "direction": "both", "format": "mermaid"}</arguments>
138
+ </use_mcp_tool>
139
+
140
+ Show the Mermaid diagram so the user can confirm the chosen insertion point is correct.
141
+
142
+ ## Step 5: Read the skeleton of the target file(s)
143
+
144
+ Get a noise-stripped structural view of the file(s) you will modify.
145
+
146
+ <use_mcp_tool>
147
+ <server_name>spec-gen</server_name>
148
+ <tool_name>get_function_skeleton</tool_name>
149
+ <arguments>{"directory": "$DIRECTORY", "filePath": "$TARGET_FILE"}</arguments>
150
+ </use_mcp_tool>
151
+
152
+ Use the skeleton to:
153
+ - Confirm the insertion strategy (extend existing function vs. add new function)
154
+ - Identify the exact line range where new code will be added
155
+ - Spot any existing error handling or type patterns to follow
156
+
157
+ Ask the user to confirm the implementation approach before writing any code:
158
+
159
+ > "I plan to [extend / add / hook into] `$TOP_CANDIDATE` in `$TARGET_FILE` by [brief description].
160
+ > Does this match your intent?"
161
+
162
+ ## Step 5b: Adversarial self-check
163
+
164
+ Before writing any code, state explicitly what could break with this approach.
165
+ If `$ANTIPATTERNS` was loaded in Step 1, include any applicable patterns.
166
+
167
+ > "Risk check on `$TOP_CANDIDATE`:
168
+ > - `$CALLER_A` and `$CALLER_B` depend on this function — verify their assumptions hold after the change.
169
+ > - `$EDGE_CASE` is not covered by the current test suite — add it in Step 6.
170
+ > - [if antipatterns apply] AP-NNN (`$PATTERN_NAME`) — `$RULE` — applies here because `$REASON`."
171
+
172
+ This is not a gate — do not wait for user input. It is a mandatory self-check
173
+ that must appear in the output before the first line of code is written.
174
+
175
+ ## Step 6: Implement the feature
176
+
177
+ Apply the changes incrementally:
178
+
179
+ 1. **Add new types / interfaces first** (if needed) — separate commit
180
+ 2. **Implement the core logic** at the chosen insertion point
181
+ 3. **Update callers** if the insertion requires updating call sites
182
+ 4. **Add or update tests** — at minimum one test covering the new behaviour
183
+ 5. **Run the test suite** to confirm nothing is broken
184
+
185
+ Follow existing code style (naming conventions, error handling, import style) observed
186
+ in the skeleton from Step 5.
187
+
188
+ ## Step 7: Check spec drift
189
+
190
+ After implementing, verify the feature is covered by specs (or flag missing coverage).
191
+
192
+ <use_mcp_tool>
193
+ <server_name>spec-gen</server_name>
194
+ <tool_name>check_spec_drift</tool_name>
195
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
196
+ </use_mcp_tool>
197
+
198
+ ### If drift is detected
199
+
200
+ Present the issues table (see `/spec-gen-check-spec-drift` for format).
201
+
202
+ For `uncovered` issues on the new files: this is expected if no spec existed.
203
+ Offer to run `spec-gen generate` to create/update the spec:
204
+
205
+ > "The new file `$NEW_FILE` has no matching spec. Run `spec-gen generate` to infer
206
+ > one from the implementation, or edit the spec manually if the domain spec already
207
+ > partially covers it."
208
+
209
+ For `gap` issues on existing specs: the new code changed the public API of a covered
210
+ domain. Run `spec-gen generate --domains $DOMAIN` to regenerate.
211
+
212
+ ### If no drift
213
+
214
+ > ✅ All changed files are covered by up-to-date specs.
215
+
216
+ ## Step 8: Summarise
217
+
218
+ Present a brief implementation summary:
219
+
220
+ - **Feature**: $FEATURE_DESCRIPTION
221
+ - **Files changed**: list with line counts
222
+ - **Insertion point**: $TOP_CANDIDATE in $TARGET_FILE (role: $ROLE, strategy: $STRATEGY)
223
+ - **Tests**: N added / N updated
224
+ - **Spec drift**: ✅ clean / ⚠️ N issues (remediation: …)
225
+
226
+ Suggest follow-up actions if applicable:
227
+ - Regenerate specs (`spec-gen generate`)
228
+ - Re-run analysis to update call graph (`analyze_codebase`)
229
+ - If the feature touches a hub function, suggest `/spec-gen-plan-refactor` to
230
+ track growing complexity
231
+
232
+ ## Absolute constraints
233
+
234
+ - **No code written before Step 6** — analysis and user confirmation come first
235
+ - Always confirm the insertion point with the user before implementing
236
+ - Step 5b adversarial self-check is mandatory — never skip it
237
+ - Run tests after implementation — never skip
238
+ - Run `check_spec_drift` as the final verification step