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,210 @@
1
+ ---
2
+ name: spec-gen-execute-refactor
3
+ description: Apply the refactoring plan produced by spec-gen-plan-refactor. Reads .spec-gen/refactor-plan.md and re-reads it before each change to stay on track. Requires a confirmed plan to exist before running.
4
+ license: MIT
5
+ compatibility: spec-gen MCP server
6
+ user-invocable: true
7
+ allowed-tools:
8
+ - use_mcp_tool
9
+ - read_file
10
+ - write_file
11
+ - str_replace_based_edit
12
+ - replace_in_file
13
+ - apply_diff
14
+ - run_command
15
+ - spec-gen-plan-refactor
16
+ ---
17
+
18
+ # spec-gen: Execute Refactor
19
+
20
+ ## When to use this skill
21
+
22
+ Trigger this skill when the user asks to **apply a refactoring plan**, with phrasings like:
23
+ - "apply the refactor plan"
24
+ - "execute the planned refactoring"
25
+ - explicit command `/spec-gen-execute-refactor`
26
+
27
+ **Prerequisite**: the `spec-gen-plan-refactor` skill must have been run and the plan confirmed.
28
+ The file `.spec-gen/refactor-plan.md` must exist.
29
+
30
+ ---
31
+
32
+ ## Step 1 — Read the plan
33
+
34
+ Read `.spec-gen/refactor-plan.md` from the project directory.
35
+
36
+ If the file does not exist, stop immediately:
37
+ > "No refactor plan found at `.spec-gen/refactor-plan.md`. Please run `/spec-gen-plan-refactor` first."
38
+
39
+ Extract and display a summary:
40
+ - Target function, file, and line range
41
+ - Strategy and risk score
42
+ - Number of changes planned
43
+ - Test command
44
+ - Acceptance criteria
45
+
46
+ **Ask the user to confirm before proceeding.**
47
+
48
+ ---
49
+
50
+ ## Step 2 — Establish a green baseline
51
+
52
+ Confirm the test suite is passing using the test command from the plan.
53
+
54
+ **If tests are already failing, stop and tell the user.** Do not refactor on a red baseline.
55
+
56
+ If a coverage tool is available, run it on the target file and compare against the coverage baseline in the plan.
57
+
58
+ **Coverage thresholds:**
59
+
60
+ | Coverage on files to touch | Recommendation |
61
+ |---|---|
62
+ | ≥ 70% lines | Safe — proceed |
63
+ | 40–69% lines | Caution — write characterisation tests first |
64
+ | < 40% lines | **Stop.** Strongly recommend writing tests first |
65
+ | 0% (no tests) | **Blocked.** Propose a minimal test harness, then restart |
66
+
67
+ If coverage is below 40%:
68
+ > "Coverage on the target file is X%. Refactoring without test coverage risks introducing silent regressions. Would you like me to suggest test cases based on the function signatures, or do you want to proceed at your own risk?"
69
+
70
+ Only continue past this point with **explicit user confirmation**.
71
+
72
+ **Large file warning**: if the target function spans more than 300 lines:
73
+ > "This function is X lines long. Small models (< 13B parameters) may lose code when editing files of this size in a single pass. Recommended approach: apply Change 1 from the plan (smallest extraction) first to reduce the target below 200 lines."
74
+
75
+ ---
76
+
77
+ ## Step 3 — Set the restore point
78
+
79
+ Verify the working tree is clean:
80
+
81
+ ```bash
82
+ git status # must show: nothing to commit, working tree clean
83
+ git log --oneline -1 # note this commit hash — your restore point
84
+ ```
85
+
86
+ If there are uncommitted changes, stop and ask the user to commit or stash them first.
87
+
88
+ Fill in the `Restore point` section of `.spec-gen/refactor-plan.md` with the current commit hash.
89
+
90
+ ---
91
+
92
+ ## Step 4 — Apply changes (one at a time)
93
+
94
+ **Before each change**, re-read `.spec-gen/refactor-plan.md` to confirm:
95
+ - Which change you are on
96
+ - Exactly what to extract, where to put it, and which call sites to update
97
+
98
+ ### Editing tool rule
99
+
100
+ Always prefer a targeted edit tool (`replace_in_file`, `str_replace_based_edit`, `apply_diff`) over a full-file rewrite (`write_to_file`). Only use `write_to_file` if the file is under 100 lines. If a change seems to require `write_to_file` on a larger file, stop and split it into smaller targeted edits.
101
+
102
+ **Small model constraint**: if the model is under 13B parameters (Mistral Small, Phi, Gemma…), each edit must touch a contiguous block of at most 50 lines. Split if needed.
103
+
104
+ ### For each change in the plan:
105
+
106
+ 1. **Read the source file** around the lines to extract (do not rely on memory).
107
+
108
+ 2. **Apply the edit**:
109
+ - Extract or move the identified block
110
+ - Place it in the target file and target class specified in the plan
111
+ - If the target file is new, create it with only the extracted code
112
+ - Update all call sites listed in the plan
113
+
114
+ 3. **Verify the diff** before running tests:
115
+ ```bash
116
+ git diff --stat # only the expected files should appear
117
+ git diff # confirm deleted lines are intentional — moved, not lost.
118
+ # If deleted lines >> added lines with no new file created,
119
+ # code was likely lost — abort immediately.
120
+ ```
121
+
122
+ 4. **Run the test suite** (command from the plan). If any test fails, restore immediately:
123
+ ```bash
124
+ git checkout HEAD -- <file>
125
+ ```
126
+ Do **not** accumulate broken state before restoring.
127
+
128
+ 5. **Mark the change as done** in `.spec-gen/refactor-plan.md` by appending `✅` to the change heading, then proceed to the next one.
129
+
130
+ Repeat until all changes in the plan are marked ✅.
131
+
132
+ ---
133
+
134
+ ## Step 5 — Verify improvement
135
+
136
+ ```xml
137
+ <use_mcp_tool>
138
+ <server_name>spec-gen</server_name>
139
+ <tool_name>analyze_codebase</tool_name>
140
+ <arguments>{"directory": "$DIRECTORY", "force": true}</arguments>
141
+ </use_mcp_tool>
142
+
143
+ <use_mcp_tool>
144
+ <server_name>spec-gen</server_name>
145
+ <tool_name>get_refactor_report</tool_name>
146
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
147
+ </use_mcp_tool>
148
+ ```
149
+
150
+ Check each acceptance criterion from the plan:
151
+ - Priority score dropped below the target
152
+ - Function is no longer in the top-5 list
153
+ - Full test suite passes
154
+
155
+ If not, investigate and iterate (add a new change to the plan if needed).
156
+
157
+ Run the full test suite one final time to confirm the refactored state is clean.
158
+
159
+ ---
160
+
161
+ ## Step 6 (optional — requires spec-gen generate to have been run)
162
+
163
+ > ⚠️ This step proposes irreversible changes (deletions, renames). Do not apply anything without explicit user confirmation at each sub-step.
164
+
165
+ ### 6a — Dead code: orphan functions
166
+
167
+ ```xml
168
+ <use_mcp_tool>
169
+ <server_name>spec-gen</server_name>
170
+ <tool_name>get_mapping</tool_name>
171
+ <arguments>{"directory": "$DIRECTORY", "orphansOnly": true}</arguments>
172
+ </use_mcp_tool>
173
+ ```
174
+
175
+ Present the orphan list (kind `function` or `class` only). For each one, check:
176
+ - Is it exported and potentially consumed by external code?
177
+ - Is it re-exported from an index file?
178
+ - Was it simply missed by the LLM?
179
+
180
+ **Do not delete anything without the user explicitly approving each function.**
181
+
182
+ ### 6b — Naming alignment: spec vocabulary vs actual names
183
+
184
+ ```xml
185
+ <use_mcp_tool>
186
+ <server_name>spec-gen</server_name>
187
+ <tool_name>get_mapping</tool_name>
188
+ <arguments>{"directory": "$DIRECTORY"}</arguments>
189
+ </use_mcp_tool>
190
+ ```
191
+
192
+ Build a table of mismatches and present it before touching any code:
193
+
194
+ | Current name | Proposed name | File | Confidence |
195
+ |---|---|---|---|
196
+
197
+ Only renames with `confidence: "llm"` should be proposed automatically. Flag `confidence: "heuristic"` entries for manual verification first.
198
+
199
+ **Wait for explicit user approval of the full rename table before applying any change. Apply renames one file at a time and run tests after each.**
200
+
201
+ ---
202
+
203
+ ## Absolute constraints
204
+
205
+ - Always re-read `.spec-gen/refactor-plan.md` before each change
206
+ - Never use `write_to_file` on a file > 100 lines
207
+ - Never accumulate broken state — restore immediately on any test failure
208
+ - Always verify the diff before running tests
209
+ - Never proceed to Step 6 without explicit user request
210
+ - Always flag potentially lost code (deleted lines >> added lines with no new file created)
@@ -0,0 +1,245 @@
1
+ ---
2
+ name: spec-gen-generate
3
+ description: Reverse-engineer OpenSpec specifications from an existing codebase. Performs "code archaeology" — extracting what code actually does and documenting it as structured OpenSpec specs across all detected domains.
4
+ license: MIT
5
+ version: 1.0.0
6
+ author: Clay Good
7
+ repository: https://github.com/clay-good/spec-gen
8
+ compatibility: spec-gen MCP server
9
+ user-invocable: true
10
+ allowed-tools:
11
+ - read_file
12
+ - write_file
13
+ - list_directory
14
+ - run_command
15
+ - use_mcp_tool
16
+ - spec-gen-analyze-codebase
17
+ - spec-gen-plan-refactor
18
+ ---
19
+
20
+ # spec-gen: Generate OpenSpec Specifications
21
+
22
+ ## When to use this skill
23
+
24
+ Trigger this skill when the user asks to **generate specs from an existing codebase**, with phrasings like:
25
+ - "run spec-gen on this codebase"
26
+ - "reverse-engineer the specs"
27
+ - "generate OpenSpec from my code"
28
+ - "document what this code does"
29
+ - explicit command `/spec-gen-generate`
30
+
31
+ ---
32
+
33
+ ## Philosophy
34
+
35
+ - **Archaeology over Creativity**: document what the code ACTUALLY does, not what you imagine it should do
36
+ - **Evidence-based**: every requirement and scenario must trace back to actual code
37
+ - **OpenSpec-native**: output follows OpenSpec conventions exactly
38
+
39
+ ---
40
+
41
+ ## Phase 1 — Codebase Survey
42
+
43
+ Understand the project structure before touching any files.
44
+
45
+ **1. Identify project type** by checking for:
46
+
47
+ | File | Stack |
48
+ |---|---|
49
+ | `package.json` | Node.js / TypeScript |
50
+ | `pyproject.toml` / `setup.py` | Python |
51
+ | `go.mod` | Go |
52
+ | `Cargo.toml` | Rust |
53
+ | `pom.xml` / `build.gradle` | Java |
54
+
55
+ **2. Find high-value files** — prioritize:
56
+ - Schema / model files (entities, types, interfaces)
57
+ - Service files (business logic)
58
+ - Route / controller files (API surface)
59
+ - Config files (settings, environment)
60
+ - Entry points (main, index, app)
61
+
62
+ **3. Identify domains** by looking for:
63
+ - Directory structure (`src/users/`, `src/orders/`, etc.)
64
+ - File naming patterns (`user-service`, `order-controller`)
65
+ - Import clusters (files that import each other heavily)
66
+
67
+ **4. Detect frameworks** from dependencies and patterns:
68
+ - Web: Express, NestJS, FastAPI, Django, etc.
69
+ - Database: PostgreSQL, MongoDB, etc.
70
+ - Auth: JWT, OAuth, etc.
71
+
72
+ ---
73
+
74
+ ## Phase 2 — Deep Analysis
75
+
76
+ For each identified domain, analyze the relevant files.
77
+
78
+ **Extract entities:**
79
+ - What data structures exist?
80
+ - What are their properties and types?
81
+ - How do they relate to each other?
82
+
83
+ **Extract behaviors:**
84
+ - What operations can be performed?
85
+ - What are the business rules / validations?
86
+ - What side effects occur (emails, payments, etc.)?
87
+
88
+ **Extract API surface** (if applicable):
89
+ - What endpoints exist?
90
+ - What are the request / response shapes?
91
+ - What authentication is required?
92
+
93
+ ---
94
+
95
+ ## Phase 3 — Generate OpenSpec Specifications
96
+
97
+ Create the OpenSpec directory structure if it doesn't exist:
98
+
99
+ ```
100
+ openspec/
101
+ ├── config.yaml
102
+ └── specs/
103
+ ├── overview/
104
+ │ └── spec.md
105
+ ├── {domain-1}/
106
+ │ └── spec.md
107
+ ├── {domain-2}/
108
+ │ └── spec.md
109
+ └── architecture/
110
+ └── spec.md
111
+ ```
112
+
113
+ ### Spec file format
114
+
115
+ Each `spec.md` MUST follow this exact format:
116
+
117
+ ```markdown
118
+ # {Domain} Specification
119
+
120
+ > Generated by spec-gen on {date}
121
+ > Source files: {list of files analyzed}
122
+
123
+ ## Purpose
124
+
125
+ {2-3 sentences describing what this domain handles}
126
+
127
+ ## Requirements
128
+
129
+ ### Requirement: {RequirementName}
130
+
131
+ {The system SHALL/MUST/SHOULD do X...}
132
+
133
+ Use RFC 2119 keywords:
134
+ - **SHALL/MUST**: Required behavior
135
+ - **SHOULD**: Recommended behavior
136
+ - **MAY**: Optional behavior
137
+
138
+ #### Scenario: {ScenarioName}
139
+ - **GIVEN** {precondition}
140
+ - **WHEN** {action}
141
+ - **THEN** {expected outcome}
142
+
143
+ ## Technical Notes
144
+
145
+ - **Implementation**: `{file paths}`
146
+ - **Dependencies**: {related domains/services}
147
+ ```
148
+
149
+ ### Critical formatting rules
150
+
151
+ 1. Requirements use RFC 2119 keywords (SHALL, MUST, SHOULD, MAY)
152
+ 2. Scenarios use exactly 4 hashtags (`####`)
153
+ 3. Scenarios follow Given/When/Then format with **bold** labels
154
+ 4. No delta markers (ADDED, MODIFIED, REMOVED) — these are baseline specs
155
+
156
+ ---
157
+
158
+ ## Phase 4 — Update OpenSpec Config
159
+
160
+ **If `openspec/config.yaml` exists**, preserve all existing content and append:
161
+
162
+ ```yaml
163
+ # Auto-detected by spec-gen
164
+ spec-gen:
165
+ generatedAt: "{timestamp}"
166
+ domains:
167
+ - {domain-1}
168
+ - {domain-2}
169
+ ```
170
+
171
+ **If it doesn't exist**, create a minimal config:
172
+
173
+ ```yaml
174
+ schema: spec-driven
175
+ context: |
176
+ {Brief project description based on analysis}
177
+
178
+ Tech stack: {detected technologies}
179
+ Architecture: {detected pattern}
180
+ ```
181
+
182
+ ---
183
+
184
+ ## Phase 5 — Drift Detection
185
+
186
+ When specs already exist and code has changed, check for **spec drift** — divergence between the codebase and its specifications.
187
+
188
+ **When to check:** before committing code, when reviewing PRs, or when explicitly asked to validate specs.
189
+
190
+ **Process:**
191
+
192
+ 1. **Identify what changed** — use git to find added, modified, deleted, or renamed source files compared to the base branch. Filter out: test files, generated files, lock files, static assets, CI configs.
193
+
194
+ 2. **Map changes to specs** — for each changed file, determine which spec domain covers it by checking:
195
+ - `> Source files:` header in each `spec.md`
196
+ - `**Implementation**:` references in Technical Notes
197
+ - Directory structure inference (e.g. `src/auth/` → auth domain)
198
+
199
+ 3. **Detect four categories of drift:**
200
+
201
+ | Category | Meaning |
202
+ |---|---|
203
+ | **Gap** | Code changed but its spec was not updated |
204
+ | **Stale** | Spec references a deleted or renamed file |
205
+ | **Uncovered** | New source file has no matching spec domain |
206
+ | **Orphaned Spec** | Spec declares source files that no longer exist |
207
+
208
+ 4. **Report** each issue with: affected file, domain, and a suggested resolution.
209
+
210
+ **CLI shorthand:** `spec-gen drift` runs this check. Use `spec-gen drift --install-hook` to add it as a git pre-commit hook.
211
+
212
+ ---
213
+
214
+ ## Output Checklist
215
+
216
+ Before finishing, verify every item:
217
+
218
+ - [ ] `openspec/specs/overview/spec.md` exists with system summary
219
+ - [ ] Each domain has `openspec/specs/{domain}/spec.md`
220
+ - [ ] `openspec/specs/architecture/spec.md` describes system structure
221
+ - [ ] All requirements use RFC 2119 keywords
222
+ - [ ] All scenarios use Given/When/Then format
223
+ - [ ] `openspec/config.yaml` is created or updated
224
+ - [ ] No spec drift — run `spec-gen drift` to verify specs match code
225
+
226
+ ---
227
+
228
+ ## Absolute constraints
229
+
230
+ - **Never invent requirements** — every item must be traceable to actual code
231
+ - Always preserve existing `openspec/config.yaml` content before appending
232
+ - Never use delta markers (ADDED, MODIFIED, REMOVED) in baseline specs
233
+ - Scenarios must use exactly 4 hashtags — never 3 or 5
234
+ - RFC 2119 keywords (SHALL, MUST, SHOULD, MAY) must be uppercase
235
+
236
+ ---
237
+
238
+ ## Suggested next steps after generation
239
+
240
+ Report what was created, then suggest:
241
+ - `openspec validate --all` — check spec structure
242
+ - `spec-gen drift --install-hook` — catch future drift automatically
243
+ - `openspec list --specs` — see all generated specs
244
+ - Manual review and refinement of generated specs
245
+ - Run `/spec-gen-plan-refactor` to identify refactoring targets now that specs exist