gsd-code-first 1.0.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 (238) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja-JP.md +834 -0
  3. package/README.ko-KR.md +823 -0
  4. package/README.md +937 -0
  5. package/README.pt-BR.md +452 -0
  6. package/README.zh-CN.md +800 -0
  7. package/agents/gsd-advisor-researcher.md +104 -0
  8. package/agents/gsd-annotator.md +148 -0
  9. package/agents/gsd-arc-executor.md +537 -0
  10. package/agents/gsd-arc-planner.md +374 -0
  11. package/agents/gsd-assumptions-analyzer.md +105 -0
  12. package/agents/gsd-code-planner.md +155 -0
  13. package/agents/gsd-codebase-mapper.md +770 -0
  14. package/agents/gsd-debugger.md +1373 -0
  15. package/agents/gsd-executor.md +509 -0
  16. package/agents/gsd-integration-checker.md +443 -0
  17. package/agents/gsd-nyquist-auditor.md +176 -0
  18. package/agents/gsd-phase-researcher.md +698 -0
  19. package/agents/gsd-plan-checker.md +773 -0
  20. package/agents/gsd-planner.md +1354 -0
  21. package/agents/gsd-project-researcher.md +654 -0
  22. package/agents/gsd-prototyper.md +161 -0
  23. package/agents/gsd-research-synthesizer.md +247 -0
  24. package/agents/gsd-roadmapper.md +679 -0
  25. package/agents/gsd-ui-auditor.md +439 -0
  26. package/agents/gsd-ui-checker.md +300 -0
  27. package/agents/gsd-ui-researcher.md +357 -0
  28. package/agents/gsd-user-profiler.md +171 -0
  29. package/agents/gsd-verifier.md +700 -0
  30. package/bin/install.js +5009 -0
  31. package/commands/gsd/add-backlog.md +76 -0
  32. package/commands/gsd/add-phase.md +43 -0
  33. package/commands/gsd/add-tests.md +41 -0
  34. package/commands/gsd/add-todo.md +47 -0
  35. package/commands/gsd/annotate.md +54 -0
  36. package/commands/gsd/audit-milestone.md +36 -0
  37. package/commands/gsd/audit-uat.md +24 -0
  38. package/commands/gsd/autonomous.md +41 -0
  39. package/commands/gsd/check-todos.md +45 -0
  40. package/commands/gsd/cleanup.md +18 -0
  41. package/commands/gsd/complete-milestone.md +136 -0
  42. package/commands/gsd/debug.md +173 -0
  43. package/commands/gsd/deep-plan.md +52 -0
  44. package/commands/gsd/discuss-phase.md +64 -0
  45. package/commands/gsd/do.md +30 -0
  46. package/commands/gsd/execute-phase.md +59 -0
  47. package/commands/gsd/extract-plan.md +35 -0
  48. package/commands/gsd/fast.md +30 -0
  49. package/commands/gsd/forensics.md +56 -0
  50. package/commands/gsd/health.md +22 -0
  51. package/commands/gsd/help.md +22 -0
  52. package/commands/gsd/insert-phase.md +32 -0
  53. package/commands/gsd/iterate.md +124 -0
  54. package/commands/gsd/join-discord.md +18 -0
  55. package/commands/gsd/list-phase-assumptions.md +46 -0
  56. package/commands/gsd/list-workspaces.md +19 -0
  57. package/commands/gsd/manager.md +39 -0
  58. package/commands/gsd/map-codebase.md +71 -0
  59. package/commands/gsd/milestone-summary.md +51 -0
  60. package/commands/gsd/new-milestone.md +44 -0
  61. package/commands/gsd/new-project.md +42 -0
  62. package/commands/gsd/new-workspace.md +44 -0
  63. package/commands/gsd/next.md +24 -0
  64. package/commands/gsd/note.md +34 -0
  65. package/commands/gsd/pause-work.md +38 -0
  66. package/commands/gsd/plan-milestone-gaps.md +34 -0
  67. package/commands/gsd/plan-phase.md +47 -0
  68. package/commands/gsd/plant-seed.md +28 -0
  69. package/commands/gsd/pr-branch.md +25 -0
  70. package/commands/gsd/profile-user.md +46 -0
  71. package/commands/gsd/progress.md +24 -0
  72. package/commands/gsd/prototype.md +56 -0
  73. package/commands/gsd/quick.md +47 -0
  74. package/commands/gsd/reapply-patches.md +123 -0
  75. package/commands/gsd/remove-phase.md +31 -0
  76. package/commands/gsd/remove-workspace.md +26 -0
  77. package/commands/gsd/research-phase.md +195 -0
  78. package/commands/gsd/resume-work.md +40 -0
  79. package/commands/gsd/review-backlog.md +61 -0
  80. package/commands/gsd/review.md +37 -0
  81. package/commands/gsd/session-report.md +19 -0
  82. package/commands/gsd/set-mode.md +41 -0
  83. package/commands/gsd/set-profile.md +12 -0
  84. package/commands/gsd/settings.md +36 -0
  85. package/commands/gsd/ship.md +23 -0
  86. package/commands/gsd/stats.md +18 -0
  87. package/commands/gsd/thread.md +127 -0
  88. package/commands/gsd/ui-phase.md +34 -0
  89. package/commands/gsd/ui-review.md +32 -0
  90. package/commands/gsd/update.md +37 -0
  91. package/commands/gsd/validate-phase.md +35 -0
  92. package/commands/gsd/verify-work.md +38 -0
  93. package/commands/gsd/workstreams.md +63 -0
  94. package/get-shit-done/bin/gsd-tools.cjs +946 -0
  95. package/get-shit-done/bin/lib/arc-scanner.cjs +341 -0
  96. package/get-shit-done/bin/lib/commands.cjs +959 -0
  97. package/get-shit-done/bin/lib/config.cjs +466 -0
  98. package/get-shit-done/bin/lib/core.cjs +1230 -0
  99. package/get-shit-done/bin/lib/frontmatter.cjs +336 -0
  100. package/get-shit-done/bin/lib/init.cjs +1442 -0
  101. package/get-shit-done/bin/lib/milestone.cjs +252 -0
  102. package/get-shit-done/bin/lib/model-profiles.cjs +68 -0
  103. package/get-shit-done/bin/lib/phase.cjs +888 -0
  104. package/get-shit-done/bin/lib/profile-output.cjs +952 -0
  105. package/get-shit-done/bin/lib/profile-pipeline.cjs +539 -0
  106. package/get-shit-done/bin/lib/roadmap.cjs +329 -0
  107. package/get-shit-done/bin/lib/security.cjs +382 -0
  108. package/get-shit-done/bin/lib/state.cjs +1031 -0
  109. package/get-shit-done/bin/lib/template.cjs +222 -0
  110. package/get-shit-done/bin/lib/uat.cjs +282 -0
  111. package/get-shit-done/bin/lib/verify.cjs +888 -0
  112. package/get-shit-done/bin/lib/workstream.cjs +491 -0
  113. package/get-shit-done/commands/gsd/workstreams.md +63 -0
  114. package/get-shit-done/references/arc-standard.md +315 -0
  115. package/get-shit-done/references/checkpoints.md +778 -0
  116. package/get-shit-done/references/continuation-format.md +249 -0
  117. package/get-shit-done/references/decimal-phase-calculation.md +64 -0
  118. package/get-shit-done/references/git-integration.md +295 -0
  119. package/get-shit-done/references/git-planning-commit.md +38 -0
  120. package/get-shit-done/references/model-profile-resolution.md +36 -0
  121. package/get-shit-done/references/model-profiles.md +139 -0
  122. package/get-shit-done/references/phase-argument-parsing.md +61 -0
  123. package/get-shit-done/references/planning-config.md +202 -0
  124. package/get-shit-done/references/questioning.md +162 -0
  125. package/get-shit-done/references/tdd.md +263 -0
  126. package/get-shit-done/references/ui-brand.md +160 -0
  127. package/get-shit-done/references/user-profiling.md +681 -0
  128. package/get-shit-done/references/verification-patterns.md +612 -0
  129. package/get-shit-done/references/workstream-flag.md +58 -0
  130. package/get-shit-done/templates/DEBUG.md +164 -0
  131. package/get-shit-done/templates/UAT.md +265 -0
  132. package/get-shit-done/templates/UI-SPEC.md +100 -0
  133. package/get-shit-done/templates/VALIDATION.md +76 -0
  134. package/get-shit-done/templates/claude-md.md +122 -0
  135. package/get-shit-done/templates/codebase/architecture.md +255 -0
  136. package/get-shit-done/templates/codebase/concerns.md +310 -0
  137. package/get-shit-done/templates/codebase/conventions.md +307 -0
  138. package/get-shit-done/templates/codebase/integrations.md +280 -0
  139. package/get-shit-done/templates/codebase/stack.md +186 -0
  140. package/get-shit-done/templates/codebase/structure.md +285 -0
  141. package/get-shit-done/templates/codebase/testing.md +480 -0
  142. package/get-shit-done/templates/config.json +44 -0
  143. package/get-shit-done/templates/context.md +352 -0
  144. package/get-shit-done/templates/continue-here.md +78 -0
  145. package/get-shit-done/templates/copilot-instructions.md +7 -0
  146. package/get-shit-done/templates/debug-subagent-prompt.md +91 -0
  147. package/get-shit-done/templates/dev-preferences.md +21 -0
  148. package/get-shit-done/templates/discovery.md +146 -0
  149. package/get-shit-done/templates/discussion-log.md +63 -0
  150. package/get-shit-done/templates/milestone-archive.md +123 -0
  151. package/get-shit-done/templates/milestone.md +115 -0
  152. package/get-shit-done/templates/phase-prompt.md +610 -0
  153. package/get-shit-done/templates/planner-subagent-prompt.md +117 -0
  154. package/get-shit-done/templates/project.md +186 -0
  155. package/get-shit-done/templates/requirements.md +231 -0
  156. package/get-shit-done/templates/research-project/ARCHITECTURE.md +204 -0
  157. package/get-shit-done/templates/research-project/FEATURES.md +147 -0
  158. package/get-shit-done/templates/research-project/PITFALLS.md +200 -0
  159. package/get-shit-done/templates/research-project/STACK.md +120 -0
  160. package/get-shit-done/templates/research-project/SUMMARY.md +170 -0
  161. package/get-shit-done/templates/research.md +552 -0
  162. package/get-shit-done/templates/retrospective.md +54 -0
  163. package/get-shit-done/templates/roadmap.md +202 -0
  164. package/get-shit-done/templates/state.md +176 -0
  165. package/get-shit-done/templates/summary-complex.md +59 -0
  166. package/get-shit-done/templates/summary-minimal.md +41 -0
  167. package/get-shit-done/templates/summary-standard.md +48 -0
  168. package/get-shit-done/templates/summary.md +248 -0
  169. package/get-shit-done/templates/user-profile.md +146 -0
  170. package/get-shit-done/templates/user-setup.md +311 -0
  171. package/get-shit-done/templates/verification-report.md +322 -0
  172. package/get-shit-done/workflows/add-phase.md +112 -0
  173. package/get-shit-done/workflows/add-tests.md +351 -0
  174. package/get-shit-done/workflows/add-todo.md +158 -0
  175. package/get-shit-done/workflows/audit-milestone.md +340 -0
  176. package/get-shit-done/workflows/audit-uat.md +109 -0
  177. package/get-shit-done/workflows/autonomous.md +891 -0
  178. package/get-shit-done/workflows/check-todos.md +177 -0
  179. package/get-shit-done/workflows/cleanup.md +152 -0
  180. package/get-shit-done/workflows/complete-milestone.md +767 -0
  181. package/get-shit-done/workflows/diagnose-issues.md +231 -0
  182. package/get-shit-done/workflows/discovery-phase.md +289 -0
  183. package/get-shit-done/workflows/discuss-phase-assumptions.md +653 -0
  184. package/get-shit-done/workflows/discuss-phase.md +1049 -0
  185. package/get-shit-done/workflows/do.md +104 -0
  186. package/get-shit-done/workflows/execute-phase.md +846 -0
  187. package/get-shit-done/workflows/execute-plan.md +514 -0
  188. package/get-shit-done/workflows/fast.md +105 -0
  189. package/get-shit-done/workflows/forensics.md +265 -0
  190. package/get-shit-done/workflows/health.md +181 -0
  191. package/get-shit-done/workflows/help.md +634 -0
  192. package/get-shit-done/workflows/insert-phase.md +130 -0
  193. package/get-shit-done/workflows/list-phase-assumptions.md +178 -0
  194. package/get-shit-done/workflows/list-workspaces.md +56 -0
  195. package/get-shit-done/workflows/manager.md +362 -0
  196. package/get-shit-done/workflows/map-codebase.md +377 -0
  197. package/get-shit-done/workflows/milestone-summary.md +223 -0
  198. package/get-shit-done/workflows/new-milestone.md +486 -0
  199. package/get-shit-done/workflows/new-project.md +1250 -0
  200. package/get-shit-done/workflows/new-workspace.md +237 -0
  201. package/get-shit-done/workflows/next.md +97 -0
  202. package/get-shit-done/workflows/node-repair.md +92 -0
  203. package/get-shit-done/workflows/note.md +156 -0
  204. package/get-shit-done/workflows/pause-work.md +176 -0
  205. package/get-shit-done/workflows/plan-milestone-gaps.md +273 -0
  206. package/get-shit-done/workflows/plan-phase.md +859 -0
  207. package/get-shit-done/workflows/plant-seed.md +169 -0
  208. package/get-shit-done/workflows/pr-branch.md +129 -0
  209. package/get-shit-done/workflows/profile-user.md +450 -0
  210. package/get-shit-done/workflows/progress.md +507 -0
  211. package/get-shit-done/workflows/quick.md +757 -0
  212. package/get-shit-done/workflows/remove-phase.md +155 -0
  213. package/get-shit-done/workflows/remove-workspace.md +90 -0
  214. package/get-shit-done/workflows/research-phase.md +82 -0
  215. package/get-shit-done/workflows/resume-project.md +326 -0
  216. package/get-shit-done/workflows/review.md +228 -0
  217. package/get-shit-done/workflows/session-report.md +146 -0
  218. package/get-shit-done/workflows/settings.md +283 -0
  219. package/get-shit-done/workflows/ship.md +228 -0
  220. package/get-shit-done/workflows/stats.md +60 -0
  221. package/get-shit-done/workflows/transition.md +671 -0
  222. package/get-shit-done/workflows/ui-phase.md +302 -0
  223. package/get-shit-done/workflows/ui-review.md +165 -0
  224. package/get-shit-done/workflows/update.md +323 -0
  225. package/get-shit-done/workflows/validate-phase.md +174 -0
  226. package/get-shit-done/workflows/verify-phase.md +254 -0
  227. package/get-shit-done/workflows/verify-work.md +637 -0
  228. package/hooks/dist/gsd-check-update.js +114 -0
  229. package/hooks/dist/gsd-context-monitor.js +156 -0
  230. package/hooks/dist/gsd-prompt-guard.js +96 -0
  231. package/hooks/dist/gsd-statusline.js +119 -0
  232. package/hooks/dist/gsd-workflow-guard.js +94 -0
  233. package/package.json +52 -0
  234. package/scripts/base64-scan.sh +262 -0
  235. package/scripts/build-hooks.js +82 -0
  236. package/scripts/prompt-injection-scan.sh +198 -0
  237. package/scripts/run-tests.cjs +29 -0
  238. package/scripts/secret-scan.sh +227 -0
@@ -0,0 +1,1230 @@
1
+ /**
2
+ * Core — Shared utilities, constants, and internal helpers
3
+ */
4
+
5
+ const fs = require('fs');
6
+ const path = require('path');
7
+ const { execSync, execFileSync, spawnSync } = require('child_process');
8
+ const { MODEL_PROFILES } = require('./model-profiles.cjs');
9
+
10
+ // ─── Path helpers ────────────────────────────────────────────────────────────
11
+
12
+ /** Normalize a relative path to always use forward slashes (cross-platform). */
13
+ function toPosixPath(p) {
14
+ return p.split(path.sep).join('/');
15
+ }
16
+
17
+ /**
18
+ * Scan immediate child directories for separate git repos.
19
+ * Returns a sorted array of directory names that have their own `.git`.
20
+ * Excludes hidden directories and node_modules.
21
+ */
22
+ function detectSubRepos(cwd) {
23
+ const results = [];
24
+ try {
25
+ const entries = fs.readdirSync(cwd, { withFileTypes: true });
26
+ for (const entry of entries) {
27
+ if (!entry.isDirectory()) continue;
28
+ if (entry.name.startsWith('.') || entry.name === 'node_modules') continue;
29
+ const gitPath = path.join(cwd, entry.name, '.git');
30
+ try {
31
+ if (fs.existsSync(gitPath)) {
32
+ results.push(entry.name);
33
+ }
34
+ } catch {}
35
+ }
36
+ } catch {}
37
+ return results.sort();
38
+ }
39
+
40
+ /**
41
+ * Walk up from `startDir` to find the project root that owns `.planning/`.
42
+ *
43
+ * In multi-repo workspaces, Claude may open inside a sub-repo (e.g. `backend/`)
44
+ * instead of the project root. This function prevents `.planning/` from being
45
+ * created inside the sub-repo by locating the nearest ancestor that already has
46
+ * a `.planning/` directory.
47
+ *
48
+ * Detection strategy (checked in order for each ancestor):
49
+ * 1. Parent has `.planning/config.json` with `sub_repos` listing this directory
50
+ * 2. Parent has `.planning/config.json` with `multiRepo: true` (legacy format)
51
+ * 3. Parent has `.planning/` and current dir has its own `.git` (heuristic)
52
+ *
53
+ * Returns `startDir` unchanged when no ancestor `.planning/` is found (first-run
54
+ * or single-repo projects).
55
+ */
56
+ function findProjectRoot(startDir) {
57
+ const resolved = path.resolve(startDir);
58
+ const root = path.parse(resolved).root;
59
+ const homedir = require('os').homedir();
60
+
61
+ // If startDir already contains .planning/, it IS the project root.
62
+ // Do not walk up to a parent workspace that also has .planning/ (#1362).
63
+ const ownPlanning = path.join(resolved, '.planning');
64
+ if (fs.existsSync(ownPlanning) && fs.statSync(ownPlanning).isDirectory()) {
65
+ return startDir;
66
+ }
67
+
68
+ // Check if startDir or any of its ancestors (up to AND including the
69
+ // candidate project root) contains a .git directory. This handles both
70
+ // `backend/` (direct sub-repo) and `backend/src/modules/` (nested inside),
71
+ // as well as the common case where .git lives at the same level as .planning/.
72
+ function isInsideGitRepo(candidateParent) {
73
+ let d = resolved;
74
+ while (d !== root) {
75
+ if (fs.existsSync(path.join(d, '.git'))) return true;
76
+ if (d === candidateParent) break;
77
+ d = path.dirname(d);
78
+ }
79
+ return false;
80
+ }
81
+
82
+ let dir = resolved;
83
+ while (dir !== root) {
84
+ const parent = path.dirname(dir);
85
+ if (parent === dir) break; // filesystem root
86
+ if (parent === homedir) break; // never go above home
87
+
88
+ const parentPlanning = path.join(parent, '.planning');
89
+ if (fs.existsSync(parentPlanning) && fs.statSync(parentPlanning).isDirectory()) {
90
+ const configPath = path.join(parentPlanning, 'config.json');
91
+ try {
92
+ const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
93
+ const subRepos = config.sub_repos || config.planning?.sub_repos || [];
94
+
95
+ // Check explicit sub_repos list
96
+ if (Array.isArray(subRepos) && subRepos.length > 0) {
97
+ const relPath = path.relative(parent, resolved);
98
+ const topSegment = relPath.split(path.sep)[0];
99
+ if (subRepos.includes(topSegment)) {
100
+ return parent;
101
+ }
102
+ }
103
+
104
+ // Check legacy multiRepo flag
105
+ if (config.multiRepo === true && isInsideGitRepo(parent)) {
106
+ return parent;
107
+ }
108
+ } catch {
109
+ // config.json missing or malformed — fall back to .git heuristic
110
+ }
111
+
112
+ // Heuristic: parent has .planning/ and we're inside a git repo
113
+ if (isInsideGitRepo(parent)) {
114
+ return parent;
115
+ }
116
+ }
117
+ dir = parent;
118
+ }
119
+ return startDir;
120
+ }
121
+
122
+ // ─── Output helpers ───────────────────────────────────────────────────────────
123
+
124
+ /**
125
+ * Remove stale gsd-* temp files/dirs older than maxAgeMs (default: 5 minutes).
126
+ * Runs opportunistically before each new temp file write to prevent unbounded accumulation.
127
+ * @param {string} prefix - filename prefix to match (e.g., 'gsd-')
128
+ * @param {object} opts
129
+ * @param {number} opts.maxAgeMs - max age in ms before removal (default: 5 min)
130
+ * @param {boolean} opts.dirsOnly - if true, only remove directories (default: false)
131
+ */
132
+ function reapStaleTempFiles(prefix = 'gsd-', { maxAgeMs = 5 * 60 * 1000, dirsOnly = false } = {}) {
133
+ try {
134
+ const tmpDir = require('os').tmpdir();
135
+ const now = Date.now();
136
+ const entries = fs.readdirSync(tmpDir);
137
+ for (const entry of entries) {
138
+ if (!entry.startsWith(prefix)) continue;
139
+ const fullPath = path.join(tmpDir, entry);
140
+ try {
141
+ const stat = fs.statSync(fullPath);
142
+ if (now - stat.mtimeMs > maxAgeMs) {
143
+ if (stat.isDirectory()) {
144
+ fs.rmSync(fullPath, { recursive: true, force: true });
145
+ } else if (!dirsOnly) {
146
+ fs.unlinkSync(fullPath);
147
+ }
148
+ }
149
+ } catch {
150
+ // File may have been removed between readdir and stat — ignore
151
+ }
152
+ }
153
+ } catch {
154
+ // Non-critical — don't let cleanup failures break output
155
+ }
156
+ }
157
+
158
+ function output(result, raw, rawValue) {
159
+ let data;
160
+ if (raw && rawValue !== undefined) {
161
+ data = String(rawValue);
162
+ } else {
163
+ const json = JSON.stringify(result, null, 2);
164
+ // Large payloads exceed Claude Code's Bash tool buffer (~50KB).
165
+ // Write to tmpfile and output the path prefixed with @file: so callers can detect it.
166
+ if (json.length > 50000) {
167
+ reapStaleTempFiles();
168
+ const tmpPath = path.join(require('os').tmpdir(), `gsd-${Date.now()}.json`);
169
+ fs.writeFileSync(tmpPath, json, 'utf-8');
170
+ data = '@file:' + tmpPath;
171
+ } else {
172
+ data = json;
173
+ }
174
+ }
175
+ // process.stdout.write() is async when stdout is a pipe — process.exit()
176
+ // can tear down the process before the reader consumes the buffer.
177
+ // fs.writeSync(1, ...) blocks until the kernel accepts the bytes, and
178
+ // skipping process.exit() lets the event loop drain naturally.
179
+ fs.writeSync(1, data);
180
+ }
181
+
182
+ function error(message) {
183
+ fs.writeSync(2, 'Error: ' + message + '\n');
184
+ process.exit(1);
185
+ }
186
+
187
+ // ─── File & Config utilities ──────────────────────────────────────────────────
188
+
189
+ function safeReadFile(filePath) {
190
+ try {
191
+ return fs.readFileSync(filePath, 'utf-8');
192
+ } catch {
193
+ return null;
194
+ }
195
+ }
196
+
197
+ function loadConfig(cwd) {
198
+ const configPath = path.join(cwd, '.planning', 'config.json');
199
+ const defaults = {
200
+ model_profile: 'balanced',
201
+ commit_docs: true,
202
+ search_gitignored: false,
203
+ branching_strategy: 'none',
204
+ phase_branch_template: 'gsd/phase-{phase}-{slug}',
205
+ milestone_branch_template: 'gsd/{milestone}-{slug}',
206
+ quick_branch_template: null,
207
+ research: true,
208
+ plan_checker: true,
209
+ verifier: true,
210
+ nyquist_validation: true,
211
+ parallelization: true,
212
+ brave_search: false,
213
+ firecrawl: false,
214
+ exa_search: false,
215
+ text_mode: false, // when true, use plain-text numbered lists instead of AskUserQuestion menus
216
+ sub_repos: [],
217
+ resolve_model_ids: false, // false: return alias as-is | true: map to full Claude model ID | "omit": return '' (runtime uses its default)
218
+ context_window: 200000, // default 200k; set to 1000000 for Opus/Sonnet 4.6 1M models
219
+ phase_naming: 'sequential', // 'sequential' (default, auto-increment) or 'custom' (arbitrary string IDs)
220
+ };
221
+
222
+ try {
223
+ const raw = fs.readFileSync(configPath, 'utf-8');
224
+ const parsed = JSON.parse(raw);
225
+
226
+ // Migrate deprecated "depth" key to "granularity" with value mapping
227
+ if ('depth' in parsed && !('granularity' in parsed)) {
228
+ const depthToGranularity = { quick: 'coarse', standard: 'standard', comprehensive: 'fine' };
229
+ parsed.granularity = depthToGranularity[parsed.depth] || parsed.depth;
230
+ delete parsed.depth;
231
+ try { fs.writeFileSync(configPath, JSON.stringify(parsed, null, 2), 'utf-8'); } catch { /* intentionally empty */ }
232
+ }
233
+
234
+ // Auto-detect and sync sub_repos: scan for child directories with .git
235
+ let configDirty = false;
236
+
237
+ // Migrate legacy "multiRepo: true" boolean → sub_repos array
238
+ if (parsed.multiRepo === true && !parsed.sub_repos && !parsed.planning?.sub_repos) {
239
+ const detected = detectSubRepos(cwd);
240
+ if (detected.length > 0) {
241
+ parsed.sub_repos = detected;
242
+ if (!parsed.planning) parsed.planning = {};
243
+ parsed.planning.commit_docs = false;
244
+ delete parsed.multiRepo;
245
+ configDirty = true;
246
+ }
247
+ }
248
+
249
+ // Keep sub_repos in sync with actual filesystem
250
+ const currentSubRepos = parsed.sub_repos || parsed.planning?.sub_repos || [];
251
+ if (Array.isArray(currentSubRepos) && currentSubRepos.length > 0) {
252
+ const detected = detectSubRepos(cwd);
253
+ if (detected.length > 0) {
254
+ const sorted = [...currentSubRepos].sort();
255
+ if (JSON.stringify(sorted) !== JSON.stringify(detected)) {
256
+ parsed.sub_repos = detected;
257
+ configDirty = true;
258
+ }
259
+ }
260
+ }
261
+
262
+ // Persist sub_repos changes (migration or sync)
263
+ if (configDirty) {
264
+ try { fs.writeFileSync(configPath, JSON.stringify(parsed, null, 2), 'utf-8'); } catch {}
265
+ }
266
+
267
+ const get = (key, nested) => {
268
+ if (parsed[key] !== undefined) return parsed[key];
269
+ if (nested && parsed[nested.section] && parsed[nested.section][nested.field] !== undefined) {
270
+ return parsed[nested.section][nested.field];
271
+ }
272
+ return undefined;
273
+ };
274
+
275
+ const parallelization = (() => {
276
+ const val = get('parallelization');
277
+ if (typeof val === 'boolean') return val;
278
+ if (typeof val === 'object' && val !== null && 'enabled' in val) return val.enabled;
279
+ return defaults.parallelization;
280
+ })();
281
+
282
+ return {
283
+ model_profile: get('model_profile') ?? defaults.model_profile,
284
+ commit_docs: (() => {
285
+ const explicit = get('commit_docs', { section: 'planning', field: 'commit_docs' });
286
+ // If explicitly set in config, respect the user's choice
287
+ if (explicit !== undefined) return explicit;
288
+ // Auto-detection: when no explicit value and .planning/ is gitignored,
289
+ // default to false instead of true
290
+ if (isGitIgnored(cwd, '.planning/')) return false;
291
+ return defaults.commit_docs;
292
+ })(),
293
+ search_gitignored: get('search_gitignored', { section: 'planning', field: 'search_gitignored' }) ?? defaults.search_gitignored,
294
+ branching_strategy: get('branching_strategy', { section: 'git', field: 'branching_strategy' }) ?? defaults.branching_strategy,
295
+ phase_branch_template: get('phase_branch_template', { section: 'git', field: 'phase_branch_template' }) ?? defaults.phase_branch_template,
296
+ milestone_branch_template: get('milestone_branch_template', { section: 'git', field: 'milestone_branch_template' }) ?? defaults.milestone_branch_template,
297
+ quick_branch_template: get('quick_branch_template', { section: 'git', field: 'quick_branch_template' }) ?? defaults.quick_branch_template,
298
+ research: get('research', { section: 'workflow', field: 'research' }) ?? defaults.research,
299
+ plan_checker: get('plan_checker', { section: 'workflow', field: 'plan_check' }) ?? defaults.plan_checker,
300
+ verifier: get('verifier', { section: 'workflow', field: 'verifier' }) ?? defaults.verifier,
301
+ nyquist_validation: get('nyquist_validation', { section: 'workflow', field: 'nyquist_validation' }) ?? defaults.nyquist_validation,
302
+ parallelization,
303
+ brave_search: get('brave_search') ?? defaults.brave_search,
304
+ firecrawl: get('firecrawl') ?? defaults.firecrawl,
305
+ exa_search: get('exa_search') ?? defaults.exa_search,
306
+ text_mode: get('text_mode', { section: 'workflow', field: 'text_mode' }) ?? defaults.text_mode,
307
+ sub_repos: get('sub_repos', { section: 'planning', field: 'sub_repos' }) ?? defaults.sub_repos,
308
+ resolve_model_ids: get('resolve_model_ids') ?? defaults.resolve_model_ids,
309
+ context_window: get('context_window') ?? defaults.context_window,
310
+ phase_naming: get('phase_naming') ?? defaults.phase_naming,
311
+ model_overrides: parsed.model_overrides || null,
312
+ agent_skills: parsed.agent_skills || {},
313
+ };
314
+ } catch {
315
+ return defaults;
316
+ }
317
+ }
318
+
319
+ // ─── Git utilities ────────────────────────────────────────────────────────────
320
+
321
+ function isGitIgnored(cwd, targetPath) {
322
+ try {
323
+ // --no-index checks .gitignore rules regardless of whether the file is tracked.
324
+ // Without it, git check-ignore returns "not ignored" for tracked files even when
325
+ // .gitignore explicitly lists them — a common source of confusion when .planning/
326
+ // was committed before being added to .gitignore.
327
+ // Use execFileSync (array args) to prevent shell interpretation of special characters
328
+ // in file paths — avoids command injection via crafted path names.
329
+ execFileSync('git', ['check-ignore', '-q', '--no-index', '--', targetPath], {
330
+ cwd,
331
+ stdio: 'pipe',
332
+ });
333
+ return true;
334
+ } catch {
335
+ return false;
336
+ }
337
+ }
338
+
339
+ // ─── Markdown normalization ─────────────────────────────────────────────────
340
+
341
+ /**
342
+ * Normalize markdown to fix common markdownlint violations.
343
+ * Applied at write points so GSD-generated .planning/ files are IDE-friendly.
344
+ *
345
+ * Rules enforced:
346
+ * MD022 — Blank lines around headings
347
+ * MD031 — Blank lines around fenced code blocks
348
+ * MD032 — Blank lines around lists
349
+ * MD012 — No multiple consecutive blank lines (collapsed to 2 max)
350
+ * MD047 — Files end with a single newline
351
+ */
352
+ function normalizeMd(content) {
353
+ if (!content || typeof content !== 'string') return content;
354
+
355
+ // Normalize line endings to LF for consistent processing
356
+ let text = content.replace(/\r\n/g, '\n');
357
+
358
+ const lines = text.split('\n');
359
+ const result = [];
360
+
361
+ for (let i = 0; i < lines.length; i++) {
362
+ const line = lines[i];
363
+ const prev = i > 0 ? lines[i - 1] : '';
364
+ const prevTrimmed = prev.trimEnd();
365
+ const trimmed = line.trimEnd();
366
+
367
+ // MD022: Blank line before headings (skip first line and frontmatter delimiters)
368
+ if (/^#{1,6}\s/.test(trimmed) && i > 0 && prevTrimmed !== '' && prevTrimmed !== '---') {
369
+ result.push('');
370
+ }
371
+
372
+ // MD031: Blank line before fenced code blocks
373
+ if (/^```/.test(trimmed) && i > 0 && prevTrimmed !== '' && !isInsideFencedBlock(lines, i)) {
374
+ result.push('');
375
+ }
376
+
377
+ // MD032: Blank line before lists (- item, * item, N. item, - [ ] item)
378
+ if (/^(\s*[-*+]\s|\s*\d+\.\s)/.test(line) && i > 0 &&
379
+ prevTrimmed !== '' && !/^(\s*[-*+]\s|\s*\d+\.\s)/.test(prev) &&
380
+ prevTrimmed !== '---') {
381
+ result.push('');
382
+ }
383
+
384
+ result.push(line);
385
+
386
+ // MD022: Blank line after headings
387
+ if (/^#{1,6}\s/.test(trimmed) && i < lines.length - 1) {
388
+ const next = lines[i + 1];
389
+ if (next !== undefined && next.trimEnd() !== '') {
390
+ result.push('');
391
+ }
392
+ }
393
+
394
+ // MD031: Blank line after closing fenced code blocks
395
+ if (/^```\s*$/.test(trimmed) && isClosingFence(lines, i) && i < lines.length - 1) {
396
+ const next = lines[i + 1];
397
+ if (next !== undefined && next.trimEnd() !== '') {
398
+ result.push('');
399
+ }
400
+ }
401
+
402
+ // MD032: Blank line after last list item in a block
403
+ if (/^(\s*[-*+]\s|\s*\d+\.\s)/.test(line) && i < lines.length - 1) {
404
+ const next = lines[i + 1];
405
+ if (next !== undefined && next.trimEnd() !== '' &&
406
+ !/^(\s*[-*+]\s|\s*\d+\.\s)/.test(next) &&
407
+ !/^\s/.test(next)) {
408
+ // Only add blank line if next line is not a continuation/indented line
409
+ result.push('');
410
+ }
411
+ }
412
+ }
413
+
414
+ text = result.join('\n');
415
+
416
+ // MD012: Collapse 3+ consecutive blank lines to 2
417
+ text = text.replace(/\n{3,}/g, '\n\n');
418
+
419
+ // MD047: Ensure file ends with exactly one newline
420
+ text = text.replace(/\n*$/, '\n');
421
+
422
+ return text;
423
+ }
424
+
425
+ /** Check if line index i is inside an already-open fenced code block */
426
+ function isInsideFencedBlock(lines, i) {
427
+ let fenceCount = 0;
428
+ for (let j = 0; j < i; j++) {
429
+ if (/^```/.test(lines[j].trimEnd())) fenceCount++;
430
+ }
431
+ return fenceCount % 2 === 1;
432
+ }
433
+
434
+ /** Check if a ``` line is a closing fence (odd number of fences up to and including this one) */
435
+ function isClosingFence(lines, i) {
436
+ let fenceCount = 0;
437
+ for (let j = 0; j <= i; j++) {
438
+ if (/^```/.test(lines[j].trimEnd())) fenceCount++;
439
+ }
440
+ return fenceCount % 2 === 0;
441
+ }
442
+
443
+ function execGit(cwd, args) {
444
+ const result = spawnSync('git', args, {
445
+ cwd,
446
+ stdio: 'pipe',
447
+ encoding: 'utf-8',
448
+ });
449
+ return {
450
+ exitCode: result.status ?? 1,
451
+ stdout: (result.stdout ?? '').toString().trim(),
452
+ stderr: (result.stderr ?? '').toString().trim(),
453
+ };
454
+ }
455
+
456
+ // ─── Common path helpers ──────────────────────────────────────────────────────
457
+
458
+ /**
459
+ * Resolve the main worktree root when running inside a git worktree.
460
+ * In a linked worktree, .planning/ lives in the main worktree, not in the linked one.
461
+ * Returns the main worktree path, or cwd if not in a worktree.
462
+ */
463
+ function resolveWorktreeRoot(cwd) {
464
+ // If the current directory already has its own .planning/, respect it.
465
+ // This handles linked worktrees with independent planning state (e.g., Conductor workspaces).
466
+ if (fs.existsSync(path.join(cwd, '.planning'))) {
467
+ return cwd;
468
+ }
469
+
470
+ // Check if we're in a linked worktree
471
+ const gitDir = execGit(cwd, ['rev-parse', '--git-dir']);
472
+ const commonDir = execGit(cwd, ['rev-parse', '--git-common-dir']);
473
+
474
+ if (gitDir.exitCode !== 0 || commonDir.exitCode !== 0) return cwd;
475
+
476
+ // In a linked worktree, .git is a file pointing to .git/worktrees/<name>
477
+ // and git-common-dir points to the main repo's .git directory
478
+ const gitDirResolved = path.resolve(cwd, gitDir.stdout);
479
+ const commonDirResolved = path.resolve(cwd, commonDir.stdout);
480
+
481
+ if (gitDirResolved !== commonDirResolved) {
482
+ // We're in a linked worktree — resolve main worktree root
483
+ // The common dir is the main repo's .git, so its parent is the main worktree root
484
+ return path.dirname(commonDirResolved);
485
+ }
486
+
487
+ return cwd;
488
+ }
489
+
490
+ /**
491
+ * Acquire a file-based lock for .planning/ writes.
492
+ * Prevents concurrent worktrees from corrupting shared planning files.
493
+ * Lock is auto-released after the callback completes.
494
+ */
495
+ function withPlanningLock(cwd, fn) {
496
+ const lockPath = path.join(planningDir(cwd), '.lock');
497
+ const lockTimeout = 10000; // 10 seconds
498
+ const retryDelay = 100;
499
+ const start = Date.now();
500
+
501
+ // Ensure .planning/ exists
502
+ try { fs.mkdirSync(planningDir(cwd), { recursive: true }); } catch { /* ok */ }
503
+
504
+ while (Date.now() - start < lockTimeout) {
505
+ try {
506
+ // Atomic create — fails if file exists
507
+ fs.writeFileSync(lockPath, JSON.stringify({
508
+ pid: process.pid,
509
+ cwd,
510
+ acquired: new Date().toISOString(),
511
+ }), { flag: 'wx' });
512
+
513
+ // Lock acquired — run the function
514
+ try {
515
+ return fn();
516
+ } finally {
517
+ try { fs.unlinkSync(lockPath); } catch { /* already released */ }
518
+ }
519
+ } catch (err) {
520
+ if (err.code === 'EEXIST') {
521
+ // Lock exists — check if stale (>30s old)
522
+ try {
523
+ const stat = fs.statSync(lockPath);
524
+ if (Date.now() - stat.mtimeMs > 30000) {
525
+ fs.unlinkSync(lockPath);
526
+ continue; // retry
527
+ }
528
+ } catch { continue; }
529
+
530
+ // Wait and retry
531
+ spawnSync('sleep', ['0.1'], { stdio: 'ignore' });
532
+ continue;
533
+ }
534
+ throw err;
535
+ }
536
+ }
537
+ // Timeout — force acquire (stale lock recovery)
538
+ try { fs.unlinkSync(lockPath); } catch { /* ok */ }
539
+ return fn();
540
+ }
541
+
542
+ /**
543
+ * Get the .planning directory path, workstream-aware.
544
+ * When a workstream is active (via explicit ws arg or GSD_WORKSTREAM env var),
545
+ * returns `.planning/workstreams/{ws}/`. Otherwise returns `.planning/`.
546
+ *
547
+ * @param {string} cwd - project root
548
+ * @param {string} [ws] - explicit workstream name; if omitted, checks GSD_WORKSTREAM env var
549
+ */
550
+ function planningDir(cwd, ws) {
551
+ if (ws === undefined) ws = process.env.GSD_WORKSTREAM || null;
552
+ if (!ws) return path.join(cwd, '.planning');
553
+ return path.join(cwd, '.planning', 'workstreams', ws);
554
+ }
555
+
556
+ /** Always returns the root .planning/ path, ignoring workstreams. For shared resources. */
557
+ function planningRoot(cwd) {
558
+ return path.join(cwd, '.planning');
559
+ }
560
+
561
+ /**
562
+ * Get common .planning file paths, workstream-aware.
563
+ * Scoped paths (state, roadmap, phases, requirements) resolve to the active workstream.
564
+ * Shared paths (project, config) always resolve to the root .planning/.
565
+ */
566
+ function planningPaths(cwd, ws) {
567
+ const base = planningDir(cwd, ws);
568
+ const root = path.join(cwd, '.planning');
569
+ return {
570
+ planning: base,
571
+ state: path.join(base, 'STATE.md'),
572
+ roadmap: path.join(base, 'ROADMAP.md'),
573
+ project: path.join(root, 'PROJECT.md'),
574
+ config: path.join(root, 'config.json'),
575
+ phases: path.join(base, 'phases'),
576
+ requirements: path.join(base, 'REQUIREMENTS.md'),
577
+ };
578
+ }
579
+
580
+ // ─── Active Workstream Detection ─────────────────────────────────────────────
581
+
582
+ /**
583
+ * Get the active workstream name from .planning/active-workstream file.
584
+ * Returns null if no active workstream or file doesn't exist.
585
+ */
586
+ function getActiveWorkstream(cwd) {
587
+ const filePath = path.join(planningRoot(cwd), 'active-workstream');
588
+ try {
589
+ const name = fs.readFileSync(filePath, 'utf-8').trim();
590
+ if (!name || !/^[a-zA-Z0-9_-]+$/.test(name)) return null;
591
+ const wsDir = path.join(planningRoot(cwd), 'workstreams', name);
592
+ if (!fs.existsSync(wsDir)) return null;
593
+ return name;
594
+ } catch {
595
+ return null;
596
+ }
597
+ }
598
+
599
+ /**
600
+ * Set the active workstream. Pass null to clear.
601
+ */
602
+ function setActiveWorkstream(cwd, name) {
603
+ const filePath = path.join(planningRoot(cwd), 'active-workstream');
604
+ if (!name) {
605
+ try { fs.unlinkSync(filePath); } catch {}
606
+ return;
607
+ }
608
+ if (!/^[a-zA-Z0-9_-]+$/.test(name)) {
609
+ throw new Error('Invalid workstream name: must be alphanumeric, hyphens, and underscores only');
610
+ }
611
+ fs.writeFileSync(filePath, name + '\n', 'utf-8');
612
+ }
613
+
614
+ // ─── Phase utilities ──────────────────────────────────────────────────────────
615
+
616
+ function escapeRegex(value) {
617
+ return String(value).replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
618
+ }
619
+
620
+ function normalizePhaseName(phase) {
621
+ const str = String(phase);
622
+ // Standard numeric phases: 1, 01, 12A, 12.1
623
+ const match = str.match(/^(\d+)([A-Z])?((?:\.\d+)*)/i);
624
+ if (match) {
625
+ const padded = match[1].padStart(2, '0');
626
+ const letter = match[2] ? match[2].toUpperCase() : '';
627
+ const decimal = match[3] || '';
628
+ return padded + letter + decimal;
629
+ }
630
+ // Custom phase IDs (e.g. PROJ-42, AUTH-101): return as-is
631
+ return str;
632
+ }
633
+
634
+ function comparePhaseNum(a, b) {
635
+ const pa = String(a).match(/^(\d+)([A-Z])?((?:\.\d+)*)/i);
636
+ const pb = String(b).match(/^(\d+)([A-Z])?((?:\.\d+)*)/i);
637
+ // If either is non-numeric (custom ID), fall back to string comparison
638
+ if (!pa || !pb) return String(a).localeCompare(String(b));
639
+ const intDiff = parseInt(pa[1], 10) - parseInt(pb[1], 10);
640
+ if (intDiff !== 0) return intDiff;
641
+ // No letter sorts before letter: 12 < 12A < 12B
642
+ const la = (pa[2] || '').toUpperCase();
643
+ const lb = (pb[2] || '').toUpperCase();
644
+ if (la !== lb) {
645
+ if (!la) return -1;
646
+ if (!lb) return 1;
647
+ return la < lb ? -1 : 1;
648
+ }
649
+ // Segment-by-segment decimal comparison: 12A < 12A.1 < 12A.1.2 < 12A.2
650
+ const aDecParts = pa[3] ? pa[3].slice(1).split('.').map(p => parseInt(p, 10)) : [];
651
+ const bDecParts = pb[3] ? pb[3].slice(1).split('.').map(p => parseInt(p, 10)) : [];
652
+ const maxLen = Math.max(aDecParts.length, bDecParts.length);
653
+ if (aDecParts.length === 0 && bDecParts.length > 0) return -1;
654
+ if (bDecParts.length === 0 && aDecParts.length > 0) return 1;
655
+ for (let i = 0; i < maxLen; i++) {
656
+ const av = Number.isFinite(aDecParts[i]) ? aDecParts[i] : 0;
657
+ const bv = Number.isFinite(bDecParts[i]) ? bDecParts[i] : 0;
658
+ if (av !== bv) return av - bv;
659
+ }
660
+ return 0;
661
+ }
662
+
663
+ function searchPhaseInDir(baseDir, relBase, normalized) {
664
+ try {
665
+ const dirs = readSubdirectories(baseDir, true);
666
+ // Match: starts with normalized (numeric) OR contains normalized as prefix segment (custom ID)
667
+ const match = dirs.find(d => {
668
+ if (d.startsWith(normalized)) return true;
669
+ // For custom IDs like PROJ-42, match case-insensitively
670
+ if (d.toUpperCase().startsWith(normalized.toUpperCase())) return true;
671
+ return false;
672
+ });
673
+ if (!match) return null;
674
+
675
+ // Extract phase number and name — supports both numeric (01-name) and custom (PROJ-42-name)
676
+ const dirMatch = match.match(/^(\d+[A-Z]?(?:\.\d+)*)-?(.*)/i)
677
+ || match.match(/^([A-Z][A-Z0-9]*(?:-[A-Z0-9]+)*)-(.+)/i)
678
+ || [null, match, null];
679
+ const phaseNumber = dirMatch ? dirMatch[1] : normalized;
680
+ const phaseName = dirMatch && dirMatch[2] ? dirMatch[2] : null;
681
+ const phaseDir = path.join(baseDir, match);
682
+ const { plans: unsortedPlans, summaries: unsortedSummaries, hasResearch, hasContext, hasVerification, hasReviews } = getPhaseFileStats(phaseDir);
683
+ const plans = unsortedPlans.sort();
684
+ const summaries = unsortedSummaries.sort();
685
+
686
+ const completedPlanIds = new Set(
687
+ summaries.map(s => s.replace('-SUMMARY.md', '').replace('SUMMARY.md', ''))
688
+ );
689
+ const incompletePlans = plans.filter(p => {
690
+ const planId = p.replace('-PLAN.md', '').replace('PLAN.md', '');
691
+ return !completedPlanIds.has(planId);
692
+ });
693
+
694
+ return {
695
+ found: true,
696
+ directory: toPosixPath(path.join(relBase, match)),
697
+ phase_number: phaseNumber,
698
+ phase_name: phaseName,
699
+ phase_slug: phaseName ? phaseName.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '') : null,
700
+ plans,
701
+ summaries,
702
+ incomplete_plans: incompletePlans,
703
+ has_research: hasResearch,
704
+ has_context: hasContext,
705
+ has_verification: hasVerification,
706
+ has_reviews: hasReviews,
707
+ };
708
+ } catch {
709
+ return null;
710
+ }
711
+ }
712
+
713
+ function findPhaseInternal(cwd, phase) {
714
+ if (!phase) return null;
715
+
716
+ const phasesDir = path.join(planningDir(cwd), 'phases');
717
+ const normalized = normalizePhaseName(phase);
718
+
719
+ // Search current phases first
720
+ const relPhasesDir = toPosixPath(path.relative(cwd, phasesDir));
721
+ const current = searchPhaseInDir(phasesDir, relPhasesDir, normalized);
722
+ if (current) return current;
723
+
724
+ // Search archived milestone phases (newest first)
725
+ const milestonesDir = path.join(cwd, '.planning', 'milestones');
726
+ if (!fs.existsSync(milestonesDir)) return null;
727
+
728
+ try {
729
+ const milestoneEntries = fs.readdirSync(milestonesDir, { withFileTypes: true });
730
+ const archiveDirs = milestoneEntries
731
+ .filter(e => e.isDirectory() && /^v[\d.]+-phases$/.test(e.name))
732
+ .map(e => e.name)
733
+ .sort()
734
+ .reverse();
735
+
736
+ for (const archiveName of archiveDirs) {
737
+ const version = archiveName.match(/^(v[\d.]+)-phases$/)[1];
738
+ const archivePath = path.join(milestonesDir, archiveName);
739
+ const relBase = '.planning/milestones/' + archiveName;
740
+ const result = searchPhaseInDir(archivePath, relBase, normalized);
741
+ if (result) {
742
+ result.archived = version;
743
+ return result;
744
+ }
745
+ }
746
+ } catch { /* intentionally empty */ }
747
+
748
+ return null;
749
+ }
750
+
751
+ function getArchivedPhaseDirs(cwd) {
752
+ const milestonesDir = path.join(cwd, '.planning', 'milestones');
753
+ const results = [];
754
+
755
+ if (!fs.existsSync(milestonesDir)) return results;
756
+
757
+ try {
758
+ const milestoneEntries = fs.readdirSync(milestonesDir, { withFileTypes: true });
759
+ // Find v*-phases directories, sort newest first
760
+ const phaseDirs = milestoneEntries
761
+ .filter(e => e.isDirectory() && /^v[\d.]+-phases$/.test(e.name))
762
+ .map(e => e.name)
763
+ .sort()
764
+ .reverse();
765
+
766
+ for (const archiveName of phaseDirs) {
767
+ const version = archiveName.match(/^(v[\d.]+)-phases$/)[1];
768
+ const archivePath = path.join(milestonesDir, archiveName);
769
+ const dirs = readSubdirectories(archivePath, true);
770
+
771
+ for (const dir of dirs) {
772
+ results.push({
773
+ name: dir,
774
+ milestone: version,
775
+ basePath: path.join('.planning', 'milestones', archiveName),
776
+ fullPath: path.join(archivePath, dir),
777
+ });
778
+ }
779
+ }
780
+ } catch { /* intentionally empty */ }
781
+
782
+ return results;
783
+ }
784
+
785
+ // ─── Roadmap milestone scoping ───────────────────────────────────────────────
786
+
787
+ /**
788
+ * Strip shipped milestone content wrapped in <details> blocks.
789
+ * Used to isolate current milestone phases when searching ROADMAP.md
790
+ * for phase headings or checkboxes — prevents matching archived milestone
791
+ * phases that share the same numbers as current milestone phases.
792
+ */
793
+ function stripShippedMilestones(content) {
794
+ return content.replace(/<details>[\s\S]*?<\/details>/gi, '');
795
+ }
796
+
797
+ /**
798
+ * Extract the current milestone section from ROADMAP.md by positive lookup.
799
+ *
800
+ * Instead of stripping <details> blocks (negative heuristic that breaks if
801
+ * agents wrap the current milestone in <details>), this finds the section
802
+ * matching the current milestone version and returns only that content.
803
+ *
804
+ * Falls back to stripShippedMilestones() if:
805
+ * - cwd is not provided
806
+ * - STATE.md doesn't exist or has no milestone field
807
+ * - Version can't be found in ROADMAP.md
808
+ *
809
+ * @param {string} content - Full ROADMAP.md content
810
+ * @param {string} [cwd] - Working directory for reading STATE.md
811
+ * @returns {string} Content scoped to current milestone
812
+ */
813
+ function extractCurrentMilestone(content, cwd) {
814
+ if (!cwd) return stripShippedMilestones(content);
815
+
816
+ // 1. Get current milestone version from STATE.md frontmatter
817
+ let version = null;
818
+ try {
819
+ const statePath = path.join(planningDir(cwd), 'STATE.md');
820
+ if (fs.existsSync(statePath)) {
821
+ const stateRaw = fs.readFileSync(statePath, 'utf-8');
822
+ const milestoneMatch = stateRaw.match(/^milestone:\s*(.+)/m);
823
+ if (milestoneMatch) {
824
+ version = milestoneMatch[1].trim();
825
+ }
826
+ }
827
+ } catch {}
828
+
829
+ // 2. Fallback: derive version from getMilestoneInfo pattern in ROADMAP.md itself
830
+ if (!version) {
831
+ // Check for 🚧 in-progress marker
832
+ const inProgressMatch = content.match(/🚧\s*\*\*v(\d+\.\d+)\s/);
833
+ if (inProgressMatch) {
834
+ version = 'v' + inProgressMatch[1];
835
+ }
836
+ }
837
+
838
+ if (!version) return stripShippedMilestones(content);
839
+
840
+ // 3. Find the section matching this version
841
+ // Match headings like: ## Roadmap v3.0: Name, ## v3.0 Name, etc.
842
+ const escapedVersion = escapeRegex(version);
843
+ const sectionPattern = new RegExp(
844
+ `(^#{1,3}\\s+.*${escapedVersion}[^\\n]*)`,
845
+ 'mi'
846
+ );
847
+ const sectionMatch = content.match(sectionPattern);
848
+
849
+ if (!sectionMatch) return stripShippedMilestones(content);
850
+
851
+ const sectionStart = sectionMatch.index;
852
+
853
+ // Find the end: next milestone heading at same or higher level, or EOF
854
+ // Milestone headings look like: ## v2.0, ## Roadmap v2.0, ## ✅ v1.0, etc.
855
+ const headingLevel = sectionMatch[1].match(/^(#{1,3})\s/)[1].length;
856
+ const restContent = content.slice(sectionStart + sectionMatch[0].length);
857
+ const nextMilestonePattern = new RegExp(
858
+ `^#{1,${headingLevel}}\\s+(?:.*v\\d+\\.\\d+|✅|📋|🚧)`,
859
+ 'mi'
860
+ );
861
+ const nextMatch = restContent.match(nextMilestonePattern);
862
+
863
+ let sectionEnd;
864
+ if (nextMatch) {
865
+ sectionEnd = sectionStart + sectionMatch[0].length + nextMatch.index;
866
+ } else {
867
+ sectionEnd = content.length;
868
+ }
869
+
870
+ // Return everything before the current milestone section (non-milestone content
871
+ // like title, overview) plus the current milestone section
872
+ const beforeMilestones = content.slice(0, sectionStart);
873
+ const currentSection = content.slice(sectionStart, sectionEnd);
874
+
875
+ // Also include any content before the first milestone heading (title, overview, etc.)
876
+ // but strip any <details> blocks in it (these are definitely shipped)
877
+ const preamble = beforeMilestones.replace(/<details>[\s\S]*?<\/details>/gi, '');
878
+
879
+ return preamble + currentSection;
880
+ }
881
+
882
+ /**
883
+ * Replace a pattern only in the current milestone section of ROADMAP.md
884
+ * (everything after the last </details> close tag). Used for write operations
885
+ * that must not accidentally modify archived milestone checkboxes/tables.
886
+ */
887
+ function replaceInCurrentMilestone(content, pattern, replacement) {
888
+ const lastDetailsClose = content.lastIndexOf('</details>');
889
+ if (lastDetailsClose === -1) {
890
+ return content.replace(pattern, replacement);
891
+ }
892
+ const offset = lastDetailsClose + '</details>'.length;
893
+ const before = content.slice(0, offset);
894
+ const after = content.slice(offset);
895
+ return before + after.replace(pattern, replacement);
896
+ }
897
+
898
+ // ─── Roadmap & model utilities ────────────────────────────────────────────────
899
+
900
+ function getRoadmapPhaseInternal(cwd, phaseNum) {
901
+ if (!phaseNum) return null;
902
+ const roadmapPath = path.join(planningDir(cwd), 'ROADMAP.md');
903
+ if (!fs.existsSync(roadmapPath)) return null;
904
+
905
+ try {
906
+ const content = extractCurrentMilestone(fs.readFileSync(roadmapPath, 'utf-8'), cwd);
907
+ const escapedPhase = escapeRegex(phaseNum.toString());
908
+ // Match both numeric (Phase 1:) and custom (Phase PROJ-42:) headers
909
+ const phasePattern = new RegExp(`#{2,4}\\s*Phase\\s+${escapedPhase}:\\s*([^\\n]+)`, 'i');
910
+ const headerMatch = content.match(phasePattern);
911
+ if (!headerMatch) return null;
912
+
913
+ const phaseName = headerMatch[1].trim();
914
+ const headerIndex = headerMatch.index;
915
+ const restOfContent = content.slice(headerIndex);
916
+ const nextHeaderMatch = restOfContent.match(/\n#{2,4}\s+Phase\s+[\w]/i);
917
+ const sectionEnd = nextHeaderMatch ? headerIndex + nextHeaderMatch.index : content.length;
918
+ const section = content.slice(headerIndex, sectionEnd).trim();
919
+
920
+ const goalMatch = section.match(/\*\*Goal(?:\*\*:|\*?\*?:\*\*)\s*([^\n]+)/i);
921
+ const goal = goalMatch ? goalMatch[1].trim() : null;
922
+
923
+ return {
924
+ found: true,
925
+ phase_number: phaseNum.toString(),
926
+ phase_name: phaseName,
927
+ goal,
928
+ section,
929
+ };
930
+ } catch {
931
+ return null;
932
+ }
933
+ }
934
+
935
+ // ─── Agent installation validation (#1371) ───────────────────────────────────
936
+
937
+ /**
938
+ * Resolve the agents directory from the GSD install location.
939
+ * gsd-tools.cjs lives at <configDir>/get-shit-done/bin/gsd-tools.cjs,
940
+ * so agents/ is at <configDir>/agents/.
941
+ *
942
+ * @returns {string} Absolute path to the agents directory
943
+ */
944
+ function getAgentsDir() {
945
+ // __dirname is get-shit-done/bin/lib/ → go up 3 levels to configDir
946
+ return path.join(__dirname, '..', '..', '..', 'agents');
947
+ }
948
+
949
+ /**
950
+ * Check which GSD agents are installed on disk.
951
+ * Returns an object with installation status and details.
952
+ *
953
+ * @returns {{ agents_installed: boolean, missing_agents: string[], installed_agents: string[], agents_dir: string }}
954
+ */
955
+ function checkAgentsInstalled() {
956
+ const agentsDir = getAgentsDir();
957
+ const expectedAgents = Object.keys(MODEL_PROFILES);
958
+ const installed = [];
959
+ const missing = [];
960
+
961
+ if (!fs.existsSync(agentsDir)) {
962
+ return {
963
+ agents_installed: false,
964
+ missing_agents: expectedAgents,
965
+ installed_agents: [],
966
+ agents_dir: agentsDir,
967
+ };
968
+ }
969
+
970
+ for (const agent of expectedAgents) {
971
+ const agentFile = path.join(agentsDir, `${agent}.md`);
972
+ if (fs.existsSync(agentFile)) {
973
+ installed.push(agent);
974
+ } else {
975
+ missing.push(agent);
976
+ }
977
+ }
978
+
979
+ return {
980
+ agents_installed: installed.length > 0 && missing.length === 0,
981
+ missing_agents: missing,
982
+ installed_agents: installed,
983
+ agents_dir: agentsDir,
984
+ };
985
+ }
986
+
987
+ // ─── Model alias resolution ───────────────────────────────────────────────────
988
+
989
+ /**
990
+ * Map short model aliases to full model IDs.
991
+ * Updated each release to match current model versions.
992
+ * Users can override with model_overrides in config.json for custom/latest models.
993
+ */
994
+ const MODEL_ALIAS_MAP = {
995
+ 'opus': 'claude-opus-4-0',
996
+ 'sonnet': 'claude-sonnet-4-5',
997
+ 'haiku': 'claude-haiku-3-5',
998
+ };
999
+
1000
+ function resolveModelInternal(cwd, agentType) {
1001
+ const config = loadConfig(cwd);
1002
+
1003
+ // Check per-agent override first — always respected regardless of resolve_model_ids.
1004
+ // Users who set fully-qualified model IDs (e.g., "openai/gpt-5.4") get exactly that.
1005
+ const override = config.model_overrides?.[agentType];
1006
+ if (override) {
1007
+ return override;
1008
+ }
1009
+
1010
+ // resolve_model_ids: "omit" — return empty string so the runtime uses its configured
1011
+ // default model. For non-Claude runtimes (OpenCode, Codex, etc.) that don't recognize
1012
+ // Claude aliases (opus/sonnet/haiku/inherit). Set automatically during install. See #1156.
1013
+ if (config.resolve_model_ids === 'omit') {
1014
+ return '';
1015
+ }
1016
+
1017
+ // Fall back to profile lookup
1018
+ const profile = String(config.model_profile || 'balanced').toLowerCase();
1019
+ const agentModels = MODEL_PROFILES[agentType];
1020
+ if (!agentModels) return 'sonnet';
1021
+ if (profile === 'inherit') return 'inherit';
1022
+ const alias = agentModels[profile] || agentModels['balanced'] || 'sonnet';
1023
+
1024
+ // resolve_model_ids: true — map alias to full Claude model ID
1025
+ // Prevents 404s when the Task tool passes aliases directly to the API
1026
+ if (config.resolve_model_ids) {
1027
+ return MODEL_ALIAS_MAP[alias] || alias;
1028
+ }
1029
+
1030
+ return alias;
1031
+ }
1032
+
1033
+ // ─── Summary body helpers ─────────────────────────────────────────────────
1034
+
1035
+ /**
1036
+ * Extract a one-liner from the summary body when it's not in frontmatter.
1037
+ * The summary template defines one-liner as a bold markdown line after the heading:
1038
+ * # Phase X: Name Summary
1039
+ * **[substantive one-liner text]**
1040
+ */
1041
+ function extractOneLinerFromBody(content) {
1042
+ if (!content) return null;
1043
+ // Strip frontmatter first
1044
+ const body = content.replace(/^---\n[\s\S]*?\n---\n*/, '');
1045
+ // Find the first **...** line after a # heading
1046
+ const match = body.match(/^#[^\n]*\n+\*\*([^*]+)\*\*/m);
1047
+ return match ? match[1].trim() : null;
1048
+ }
1049
+
1050
+ // ─── Misc utilities ───────────────────────────────────────────────────────────
1051
+
1052
+ function pathExistsInternal(cwd, targetPath) {
1053
+ const fullPath = path.isAbsolute(targetPath) ? targetPath : path.join(cwd, targetPath);
1054
+ try {
1055
+ fs.statSync(fullPath);
1056
+ return true;
1057
+ } catch {
1058
+ return false;
1059
+ }
1060
+ }
1061
+
1062
+ function generateSlugInternal(text) {
1063
+ if (!text) return null;
1064
+ return text.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-+|-+$/g, '');
1065
+ }
1066
+
1067
+ function getMilestoneInfo(cwd) {
1068
+ try {
1069
+ const roadmap = fs.readFileSync(path.join(planningDir(cwd), 'ROADMAP.md'), 'utf-8');
1070
+
1071
+ // First: check for list-format roadmaps using 🚧 (in-progress) marker
1072
+ // e.g. "- 🚧 **v2.1 Belgium** — Phases 24-28 (in progress)"
1073
+ // e.g. "- 🚧 **v1.2.1 Tech Debt** — Phases 1-8 (in progress)"
1074
+ const inProgressMatch = roadmap.match(/🚧\s*\*\*v(\d+(?:\.\d+)+)\s+([^*]+)\*\*/);
1075
+ if (inProgressMatch) {
1076
+ return {
1077
+ version: 'v' + inProgressMatch[1],
1078
+ name: inProgressMatch[2].trim(),
1079
+ };
1080
+ }
1081
+
1082
+ // Second: heading-format roadmaps — strip shipped milestones in <details> blocks
1083
+ const cleaned = stripShippedMilestones(roadmap);
1084
+ // Extract version and name from the same ## heading for consistency
1085
+ // Supports 2+ segment versions: v1.2, v1.2.1, v2.0.1, etc.
1086
+ const headingMatch = cleaned.match(/## .*v(\d+(?:\.\d+)+)[:\s]+([^\n(]+)/);
1087
+ if (headingMatch) {
1088
+ return {
1089
+ version: 'v' + headingMatch[1],
1090
+ name: headingMatch[2].trim(),
1091
+ };
1092
+ }
1093
+ // Fallback: try bare version match (greedy — capture longest version string)
1094
+ const versionMatch = cleaned.match(/v(\d+(?:\.\d+)+)/);
1095
+ return {
1096
+ version: versionMatch ? versionMatch[0] : 'v1.0',
1097
+ name: 'milestone',
1098
+ };
1099
+ } catch {
1100
+ return { version: 'v1.0', name: 'milestone' };
1101
+ }
1102
+ }
1103
+
1104
+ /**
1105
+ * Returns a filter function that checks whether a phase directory belongs
1106
+ * to the current milestone based on ROADMAP.md phase headings.
1107
+ * If no ROADMAP exists or no phases are listed, returns a pass-all filter.
1108
+ */
1109
+ function getMilestonePhaseFilter(cwd) {
1110
+ const milestonePhaseNums = new Set();
1111
+ try {
1112
+ const roadmap = extractCurrentMilestone(fs.readFileSync(path.join(planningDir(cwd), 'ROADMAP.md'), 'utf-8'), cwd);
1113
+ // Match both numeric phases (Phase 1:) and custom IDs (Phase PROJ-42:)
1114
+ const phasePattern = /#{2,4}\s*Phase\s+([\w][\w.-]*)\s*:/gi;
1115
+ let m;
1116
+ while ((m = phasePattern.exec(roadmap)) !== null) {
1117
+ milestonePhaseNums.add(m[1]);
1118
+ }
1119
+ } catch { /* intentionally empty */ }
1120
+
1121
+ if (milestonePhaseNums.size === 0) {
1122
+ const passAll = () => true;
1123
+ passAll.phaseCount = 0;
1124
+ return passAll;
1125
+ }
1126
+
1127
+ const normalized = new Set(
1128
+ [...milestonePhaseNums].map(n => (n.replace(/^0+/, '') || '0').toLowerCase())
1129
+ );
1130
+
1131
+ function isDirInMilestone(dirName) {
1132
+ // Try numeric match first
1133
+ const m = dirName.match(/^0*(\d+[A-Za-z]?(?:\.\d+)*)/);
1134
+ if (m && normalized.has(m[1].toLowerCase())) return true;
1135
+ // Try custom ID match (e.g. PROJ-42-description → PROJ-42)
1136
+ const customMatch = dirName.match(/^([A-Za-z][A-Za-z0-9]*(?:-[A-Za-z0-9]+)*)/);
1137
+ if (customMatch && normalized.has(customMatch[1].toLowerCase())) return true;
1138
+ return false;
1139
+ }
1140
+ isDirInMilestone.phaseCount = milestonePhaseNums.size;
1141
+ return isDirInMilestone;
1142
+ }
1143
+
1144
+ // ─── Phase file helpers ──────────────────────────────────────────────────────
1145
+
1146
+ /** Filter a file list to just PLAN.md / *-PLAN.md entries. */
1147
+ function filterPlanFiles(files) {
1148
+ return files.filter(f => f.endsWith('-PLAN.md') || f === 'PLAN.md');
1149
+ }
1150
+
1151
+ /** Filter a file list to just SUMMARY.md / *-SUMMARY.md entries. */
1152
+ function filterSummaryFiles(files) {
1153
+ return files.filter(f => f.endsWith('-SUMMARY.md') || f === 'SUMMARY.md');
1154
+ }
1155
+
1156
+ /**
1157
+ * Read a phase directory and return counts/flags for common file types.
1158
+ * Returns an object with plans[], summaries[], and boolean flags for
1159
+ * research/context/verification files.
1160
+ */
1161
+ function getPhaseFileStats(phaseDir) {
1162
+ const files = fs.readdirSync(phaseDir);
1163
+ return {
1164
+ plans: filterPlanFiles(files),
1165
+ summaries: filterSummaryFiles(files),
1166
+ hasResearch: files.some(f => f.endsWith('-RESEARCH.md') || f === 'RESEARCH.md'),
1167
+ hasContext: files.some(f => f.endsWith('-CONTEXT.md') || f === 'CONTEXT.md'),
1168
+ hasVerification: files.some(f => f.endsWith('-VERIFICATION.md') || f === 'VERIFICATION.md'),
1169
+ hasReviews: files.some(f => f.endsWith('-REVIEWS.md') || f === 'REVIEWS.md'),
1170
+ };
1171
+ }
1172
+
1173
+ /**
1174
+ * Read immediate child directories from a path.
1175
+ * Returns [] if the path doesn't exist or can't be read.
1176
+ * Pass sort=true to apply comparePhaseNum ordering.
1177
+ */
1178
+ function readSubdirectories(dirPath, sort = false) {
1179
+ try {
1180
+ const entries = fs.readdirSync(dirPath, { withFileTypes: true });
1181
+ const dirs = entries.filter(e => e.isDirectory()).map(e => e.name);
1182
+ return sort ? dirs.sort((a, b) => comparePhaseNum(a, b)) : dirs;
1183
+ } catch {
1184
+ return [];
1185
+ }
1186
+ }
1187
+
1188
+ module.exports = {
1189
+ output,
1190
+ error,
1191
+ safeReadFile,
1192
+ loadConfig,
1193
+ isGitIgnored,
1194
+ execGit,
1195
+ normalizeMd,
1196
+ escapeRegex,
1197
+ normalizePhaseName,
1198
+ comparePhaseNum,
1199
+ searchPhaseInDir,
1200
+ findPhaseInternal,
1201
+ getArchivedPhaseDirs,
1202
+ getRoadmapPhaseInternal,
1203
+ resolveModelInternal,
1204
+ pathExistsInternal,
1205
+ generateSlugInternal,
1206
+ getMilestoneInfo,
1207
+ getMilestonePhaseFilter,
1208
+ stripShippedMilestones,
1209
+ extractCurrentMilestone,
1210
+ replaceInCurrentMilestone,
1211
+ toPosixPath,
1212
+ extractOneLinerFromBody,
1213
+ resolveWorktreeRoot,
1214
+ withPlanningLock,
1215
+ findProjectRoot,
1216
+ detectSubRepos,
1217
+ reapStaleTempFiles,
1218
+ MODEL_ALIAS_MAP,
1219
+ planningDir,
1220
+ planningRoot,
1221
+ planningPaths,
1222
+ getActiveWorkstream,
1223
+ setActiveWorkstream,
1224
+ filterPlanFiles,
1225
+ filterSummaryFiles,
1226
+ getPhaseFileStats,
1227
+ readSubdirectories,
1228
+ getAgentsDir,
1229
+ checkAgentsInstalled,
1230
+ };