@opengsd/gsd-core 1.2.0-rc.1

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 (503) hide show
  1. package/LICENSE +21 -0
  2. package/README.ja-JP.md +870 -0
  3. package/README.ko-KR.md +861 -0
  4. package/README.md +301 -0
  5. package/README.pt-BR.md +492 -0
  6. package/README.zh-CN.md +842 -0
  7. package/agents/gsd-advisor-researcher.md +127 -0
  8. package/agents/gsd-ai-researcher.md +133 -0
  9. package/agents/gsd-assumptions-analyzer.md +105 -0
  10. package/agents/gsd-code-fixer.md +668 -0
  11. package/agents/gsd-code-reviewer.md +387 -0
  12. package/agents/gsd-codebase-mapper.md +853 -0
  13. package/agents/gsd-debug-session-manager.md +314 -0
  14. package/agents/gsd-debugger.md +1452 -0
  15. package/agents/gsd-doc-classifier.md +168 -0
  16. package/agents/gsd-doc-synthesizer.md +204 -0
  17. package/agents/gsd-doc-verifier.md +217 -0
  18. package/agents/gsd-doc-writer.md +615 -0
  19. package/agents/gsd-domain-researcher.md +153 -0
  20. package/agents/gsd-eval-auditor.md +191 -0
  21. package/agents/gsd-eval-planner.md +154 -0
  22. package/agents/gsd-executor.md +772 -0
  23. package/agents/gsd-framework-selector.md +160 -0
  24. package/agents/gsd-integration-checker.md +470 -0
  25. package/agents/gsd-intel-updater.md +342 -0
  26. package/agents/gsd-nyquist-auditor.md +203 -0
  27. package/agents/gsd-pattern-mapper.md +335 -0
  28. package/agents/gsd-phase-researcher.md +928 -0
  29. package/agents/gsd-plan-checker.md +978 -0
  30. package/agents/gsd-planner.md +1218 -0
  31. package/agents/gsd-project-researcher.md +677 -0
  32. package/agents/gsd-research-synthesizer.md +255 -0
  33. package/agents/gsd-roadmapper.md +688 -0
  34. package/agents/gsd-security-auditor.md +155 -0
  35. package/agents/gsd-ui-auditor.md +495 -0
  36. package/agents/gsd-ui-checker.md +309 -0
  37. package/agents/gsd-ui-researcher.md +380 -0
  38. package/agents/gsd-user-profiler.md +171 -0
  39. package/agents/gsd-verifier.md +917 -0
  40. package/bin/install.js +10936 -0
  41. package/bin/lib/ui-safety-gate.cjs +107 -0
  42. package/commands/gsd/add-tests.md +42 -0
  43. package/commands/gsd/ai-integration-phase.md +37 -0
  44. package/commands/gsd/audit-fix.md +34 -0
  45. package/commands/gsd/audit-milestone.md +37 -0
  46. package/commands/gsd/audit-uat.md +24 -0
  47. package/commands/gsd/autonomous.md +46 -0
  48. package/commands/gsd/capture.md +62 -0
  49. package/commands/gsd/cleanup.md +24 -0
  50. package/commands/gsd/code-review.md +59 -0
  51. package/commands/gsd/complete-milestone.md +143 -0
  52. package/commands/gsd/config.md +56 -0
  53. package/commands/gsd/debug.md +52 -0
  54. package/commands/gsd/discuss-phase.md +76 -0
  55. package/commands/gsd/docs-update.md +49 -0
  56. package/commands/gsd/eval-review.md +33 -0
  57. package/commands/gsd/execute-phase.md +64 -0
  58. package/commands/gsd/explore.md +27 -0
  59. package/commands/gsd/extract-learnings.md +23 -0
  60. package/commands/gsd/fast.md +31 -0
  61. package/commands/gsd/forensics.md +57 -0
  62. package/commands/gsd/graphify.md +199 -0
  63. package/commands/gsd/health.md +31 -0
  64. package/commands/gsd/help.md +28 -0
  65. package/commands/gsd/import.md +41 -0
  66. package/commands/gsd/inbox.md +39 -0
  67. package/commands/gsd/ingest-docs.md +42 -0
  68. package/commands/gsd/manager.md +45 -0
  69. package/commands/gsd/map-codebase.md +83 -0
  70. package/commands/gsd/milestone-summary.md +51 -0
  71. package/commands/gsd/mvp-phase.md +45 -0
  72. package/commands/gsd/new-milestone.md +45 -0
  73. package/commands/gsd/new-project.md +47 -0
  74. package/commands/gsd/ns-context.md +23 -0
  75. package/commands/gsd/ns-ideate.md +24 -0
  76. package/commands/gsd/ns-manage.md +29 -0
  77. package/commands/gsd/ns-project.md +22 -0
  78. package/commands/gsd/ns-review.md +26 -0
  79. package/commands/gsd/ns-workflow.md +28 -0
  80. package/commands/gsd/pause-work.md +43 -0
  81. package/commands/gsd/phase.md +56 -0
  82. package/commands/gsd/plan-phase.md +62 -0
  83. package/commands/gsd/plan-review-convergence.md +59 -0
  84. package/commands/gsd/pr-branch.md +26 -0
  85. package/commands/gsd/profile-user.md +46 -0
  86. package/commands/gsd/progress.md +47 -0
  87. package/commands/gsd/quick.md +174 -0
  88. package/commands/gsd/resume-work.md +30 -0
  89. package/commands/gsd/review-backlog.md +63 -0
  90. package/commands/gsd/review.md +41 -0
  91. package/commands/gsd/secure-phase.md +36 -0
  92. package/commands/gsd/settings.md +29 -0
  93. package/commands/gsd/ship.md +24 -0
  94. package/commands/gsd/sketch.md +60 -0
  95. package/commands/gsd/spec-phase.md +63 -0
  96. package/commands/gsd/spike.md +57 -0
  97. package/commands/gsd/stats.md +19 -0
  98. package/commands/gsd/surface.md +155 -0
  99. package/commands/gsd/thread.md +24 -0
  100. package/commands/gsd/ui-phase.md +35 -0
  101. package/commands/gsd/ui-review.md +33 -0
  102. package/commands/gsd/ultraplan-phase.md +34 -0
  103. package/commands/gsd/undo.md +35 -0
  104. package/commands/gsd/update.md +48 -0
  105. package/commands/gsd/validate-phase.md +36 -0
  106. package/commands/gsd/verify-work.md +39 -0
  107. package/commands/gsd/workspace.md +52 -0
  108. package/commands/gsd/workstreams.md +70 -0
  109. package/get-shit-done/bin/check-latest-version.cjs +106 -0
  110. package/get-shit-done/bin/gsd-tools.cjs +1676 -0
  111. package/get-shit-done/bin/lib/active-workstream-store.cjs +302 -0
  112. package/get-shit-done/bin/lib/adr-parser.cjs +394 -0
  113. package/get-shit-done/bin/lib/agent-command-router.cjs +65 -0
  114. package/get-shit-done/bin/lib/artifacts.cjs +53 -0
  115. package/get-shit-done/bin/lib/audit.cjs +755 -0
  116. package/get-shit-done/bin/lib/check-command-router.cjs +333 -0
  117. package/get-shit-done/bin/lib/cjs-command-router-adapter.cjs +118 -0
  118. package/get-shit-done/bin/lib/clock.cjs +96 -0
  119. package/get-shit-done/bin/lib/clusters.cjs +135 -0
  120. package/get-shit-done/bin/lib/code-review-flags.cjs +74 -0
  121. package/get-shit-done/bin/lib/command-aliases.cjs +815 -0
  122. package/get-shit-done/bin/lib/command-arg-projection.cjs +62 -0
  123. package/get-shit-done/bin/lib/command-routing-hub.cjs +388 -0
  124. package/get-shit-done/bin/lib/commands.cjs +1188 -0
  125. package/get-shit-done/bin/lib/config-schema.cjs +31 -0
  126. package/get-shit-done/bin/lib/config.cjs +728 -0
  127. package/get-shit-done/bin/lib/configuration.cjs +248 -0
  128. package/get-shit-done/bin/lib/context-utilization.cjs +47 -0
  129. package/get-shit-done/bin/lib/core.cjs +2121 -0
  130. package/get-shit-done/bin/lib/decisions.cjs +116 -0
  131. package/get-shit-done/bin/lib/docs.cjs +270 -0
  132. package/get-shit-done/bin/lib/drift.cjs +388 -0
  133. package/get-shit-done/bin/lib/fallow-runner.cjs +109 -0
  134. package/get-shit-done/bin/lib/frontmatter.cjs +389 -0
  135. package/get-shit-done/bin/lib/gap-checker.cjs +205 -0
  136. package/get-shit-done/bin/lib/graphify.cjs +592 -0
  137. package/get-shit-done/bin/lib/gsd2-import.cjs +514 -0
  138. package/get-shit-done/bin/lib/init-command-router.cjs +58 -0
  139. package/get-shit-done/bin/lib/init.cjs +2112 -0
  140. package/get-shit-done/bin/lib/install-profiles.cjs +603 -0
  141. package/get-shit-done/bin/lib/installer-migration-authoring.cjs +117 -0
  142. package/get-shit-done/bin/lib/installer-migration-report.cjs +354 -0
  143. package/get-shit-done/bin/lib/installer-migrations/000-first-time-baseline.cjs +220 -0
  144. package/get-shit-done/bin/lib/installer-migrations/001-legacy-orphan-files.cjs +41 -0
  145. package/get-shit-done/bin/lib/installer-migrations/002-codex-legacy-hooks-json.cjs +80 -0
  146. package/get-shit-done/bin/lib/installer-migrations.cjs +778 -0
  147. package/get-shit-done/bin/lib/intel.cjs +708 -0
  148. package/get-shit-done/bin/lib/learnings.cjs +421 -0
  149. package/get-shit-done/bin/lib/milestone.cjs +314 -0
  150. package/get-shit-done/bin/lib/model-catalog.cjs +212 -0
  151. package/get-shit-done/bin/lib/model-profiles.cjs +31 -0
  152. package/get-shit-done/bin/lib/observability/event.cjs +82 -0
  153. package/get-shit-done/bin/lib/observability/logger.cjs +174 -0
  154. package/get-shit-done/bin/lib/observability/redaction.cjs +50 -0
  155. package/get-shit-done/bin/lib/package-identity.cjs +31 -0
  156. package/get-shit-done/bin/lib/phase-command-router.cjs +191 -0
  157. package/get-shit-done/bin/lib/phase-lifecycle.cjs +80 -0
  158. package/get-shit-done/bin/lib/phase.cjs +1607 -0
  159. package/get-shit-done/bin/lib/phases-command-router.cjs +39 -0
  160. package/get-shit-done/bin/lib/plan-scan.cjs +97 -0
  161. package/get-shit-done/bin/lib/planning-workspace.cjs +238 -0
  162. package/get-shit-done/bin/lib/profile-output.cjs +1141 -0
  163. package/get-shit-done/bin/lib/profile-pipeline.cjs +539 -0
  164. package/get-shit-done/bin/lib/project-root.cjs +112 -0
  165. package/get-shit-done/bin/lib/prompt-budget.cjs +399 -0
  166. package/get-shit-done/bin/lib/review-reviewer-selection.cjs +125 -0
  167. package/get-shit-done/bin/lib/roadmap-command-router.cjs +28 -0
  168. package/get-shit-done/bin/lib/roadmap.cjs +650 -0
  169. package/get-shit-done/bin/lib/runtime-artifact-layout.cjs +301 -0
  170. package/get-shit-done/bin/lib/runtime-homes.cjs +222 -0
  171. package/get-shit-done/bin/lib/runtime-name-policy.cjs +83 -0
  172. package/get-shit-done/bin/lib/runtime-slash.cjs +112 -0
  173. package/get-shit-done/bin/lib/schema-detect.cjs +165 -0
  174. package/get-shit-done/bin/lib/secrets.cjs +32 -0
  175. package/get-shit-done/bin/lib/security.cjs +600 -0
  176. package/get-shit-done/bin/lib/semver-compare.cjs +35 -0
  177. package/get-shit-done/bin/lib/shell-command-projection.cjs +500 -0
  178. package/get-shit-done/bin/lib/state-command-router.cjs +252 -0
  179. package/get-shit-done/bin/lib/state-document.cjs +263 -0
  180. package/get-shit-done/bin/lib/state.cjs +2038 -0
  181. package/get-shit-done/bin/lib/surface.cjs +470 -0
  182. package/get-shit-done/bin/lib/task-command-router.cjs +81 -0
  183. package/get-shit-done/bin/lib/template.cjs +228 -0
  184. package/get-shit-done/bin/lib/uat.cjs +289 -0
  185. package/get-shit-done/bin/lib/update-context.cjs +209 -0
  186. package/get-shit-done/bin/lib/validate-command-router.cjs +83 -0
  187. package/get-shit-done/bin/lib/validate.cjs +92 -0
  188. package/get-shit-done/bin/lib/verify-command-router.cjs +40 -0
  189. package/get-shit-done/bin/lib/verify.cjs +1511 -0
  190. package/get-shit-done/bin/lib/workstream-inventory-builder.cjs +74 -0
  191. package/get-shit-done/bin/lib/workstream-inventory.cjs +146 -0
  192. package/get-shit-done/bin/lib/workstream-name-policy.cjs +94 -0
  193. package/get-shit-done/bin/lib/workstream.cjs +389 -0
  194. package/get-shit-done/bin/lib/worktree-safety.cjs +985 -0
  195. package/get-shit-done/bin/shared/config-defaults.manifest.json +97 -0
  196. package/get-shit-done/bin/shared/config-schema.manifest.json +175 -0
  197. package/get-shit-done/bin/shared/model-catalog.json +122 -0
  198. package/get-shit-done/bin/shared/runtime-aliases.manifest.json +75 -0
  199. package/get-shit-done/bin/verify-reapply-patches.cjs +352 -0
  200. package/get-shit-done/contexts/dev.md +21 -0
  201. package/get-shit-done/contexts/research.md +22 -0
  202. package/get-shit-done/contexts/review.md +23 -0
  203. package/get-shit-done/references/agent-contracts.md +79 -0
  204. package/get-shit-done/references/ai-evals.md +156 -0
  205. package/get-shit-done/references/ai-frameworks.md +186 -0
  206. package/get-shit-done/references/artifact-types.md +131 -0
  207. package/get-shit-done/references/autonomous-smart-discuss.md +277 -0
  208. package/get-shit-done/references/checkpoints.md +814 -0
  209. package/get-shit-done/references/common-bug-patterns.md +114 -0
  210. package/get-shit-done/references/context-budget.md +85 -0
  211. package/get-shit-done/references/continuation-format.md +253 -0
  212. package/get-shit-done/references/debugger-philosophy.md +76 -0
  213. package/get-shit-done/references/decimal-phase-calculation.md +64 -0
  214. package/get-shit-done/references/doc-conflict-engine.md +91 -0
  215. package/get-shit-done/references/domain-probes.md +125 -0
  216. package/get-shit-done/references/execute-mvp-tdd.md +81 -0
  217. package/get-shit-done/references/executor-examples.md +110 -0
  218. package/get-shit-done/references/few-shot-examples/plan-checker.md +73 -0
  219. package/get-shit-done/references/few-shot-examples/verifier.md +109 -0
  220. package/get-shit-done/references/gate-prompts.md +100 -0
  221. package/get-shit-done/references/gates.md +70 -0
  222. package/get-shit-done/references/git-integration.md +298 -0
  223. package/get-shit-done/references/git-planning-commit.md +40 -0
  224. package/get-shit-done/references/ios-scaffold.md +123 -0
  225. package/get-shit-done/references/mandatory-initial-read.md +2 -0
  226. package/get-shit-done/references/model-profile-resolution.md +38 -0
  227. package/get-shit-done/references/model-profiles.md +245 -0
  228. package/get-shit-done/references/mvp-concepts.md +49 -0
  229. package/get-shit-done/references/phase-argument-parsing.md +61 -0
  230. package/get-shit-done/references/planner-antipatterns.md +89 -0
  231. package/get-shit-done/references/planner-chunked.md +49 -0
  232. package/get-shit-done/references/planner-gap-closure.md +62 -0
  233. package/get-shit-done/references/planner-graphify-auto-update.md +67 -0
  234. package/get-shit-done/references/planner-human-verify-mode.md +57 -0
  235. package/get-shit-done/references/planner-interface-context.md +62 -0
  236. package/get-shit-done/references/planner-mvp-mode.md +53 -0
  237. package/get-shit-done/references/planner-reviews.md +39 -0
  238. package/get-shit-done/references/planner-revision.md +87 -0
  239. package/get-shit-done/references/planner-source-audit.md +73 -0
  240. package/get-shit-done/references/planning-config.md +471 -0
  241. package/get-shit-done/references/project-skills-discovery.md +19 -0
  242. package/get-shit-done/references/questioning.md +162 -0
  243. package/get-shit-done/references/revision-loop.md +97 -0
  244. package/get-shit-done/references/scout-codebase.md +51 -0
  245. package/get-shit-done/references/skeleton-template.md +48 -0
  246. package/get-shit-done/references/sketch-interactivity.md +41 -0
  247. package/get-shit-done/references/sketch-theme-system.md +94 -0
  248. package/get-shit-done/references/sketch-tooling.md +45 -0
  249. package/get-shit-done/references/sketch-variant-patterns.md +81 -0
  250. package/get-shit-done/references/spidr-splitting.md +69 -0
  251. package/get-shit-done/references/tdd.md +330 -0
  252. package/get-shit-done/references/thinking-models-debug.md +44 -0
  253. package/get-shit-done/references/thinking-models-execution.md +50 -0
  254. package/get-shit-done/references/thinking-models-planning.md +62 -0
  255. package/get-shit-done/references/thinking-models-research.md +50 -0
  256. package/get-shit-done/references/thinking-models-verification.md +55 -0
  257. package/get-shit-done/references/thinking-partner.md +96 -0
  258. package/get-shit-done/references/ui-brand.md +160 -0
  259. package/get-shit-done/references/universal-anti-patterns.md +63 -0
  260. package/get-shit-done/references/user-profiling.md +681 -0
  261. package/get-shit-done/references/user-story-template.md +58 -0
  262. package/get-shit-done/references/verification-overrides.md +227 -0
  263. package/get-shit-done/references/verification-patterns.md +612 -0
  264. package/get-shit-done/references/verify-mvp-mode.md +85 -0
  265. package/get-shit-done/references/workstream-flag.md +111 -0
  266. package/get-shit-done/references/worktree-path-safety.md +89 -0
  267. package/get-shit-done/templates/AI-SPEC.md +246 -0
  268. package/get-shit-done/templates/DEBUG.md +169 -0
  269. package/get-shit-done/templates/README.md +77 -0
  270. package/get-shit-done/templates/SECURITY.md +61 -0
  271. package/get-shit-done/templates/UAT.md +265 -0
  272. package/get-shit-done/templates/UI-SPEC.md +100 -0
  273. package/get-shit-done/templates/VALIDATION.md +76 -0
  274. package/get-shit-done/templates/claude-md.md +145 -0
  275. package/get-shit-done/templates/codebase/architecture.md +255 -0
  276. package/get-shit-done/templates/codebase/concerns.md +310 -0
  277. package/get-shit-done/templates/codebase/conventions.md +307 -0
  278. package/get-shit-done/templates/codebase/integrations.md +280 -0
  279. package/get-shit-done/templates/codebase/stack.md +186 -0
  280. package/get-shit-done/templates/codebase/structure.md +285 -0
  281. package/get-shit-done/templates/codebase/testing.md +480 -0
  282. package/get-shit-done/templates/config.json +62 -0
  283. package/get-shit-done/templates/context.md +352 -0
  284. package/get-shit-done/templates/continue-here.md +78 -0
  285. package/get-shit-done/templates/copilot-instructions.md +7 -0
  286. package/get-shit-done/templates/debug-subagent-prompt.md +91 -0
  287. package/get-shit-done/templates/dev-preferences.md +21 -0
  288. package/get-shit-done/templates/discovery.md +146 -0
  289. package/get-shit-done/templates/discussion-log.md +63 -0
  290. package/get-shit-done/templates/milestone-archive.md +123 -0
  291. package/get-shit-done/templates/milestone.md +115 -0
  292. package/get-shit-done/templates/phase-prompt.md +610 -0
  293. package/get-shit-done/templates/planner-subagent-prompt.md +117 -0
  294. package/get-shit-done/templates/project.md +186 -0
  295. package/get-shit-done/templates/requirements.md +231 -0
  296. package/get-shit-done/templates/research-project/ARCHITECTURE.md +204 -0
  297. package/get-shit-done/templates/research-project/FEATURES.md +147 -0
  298. package/get-shit-done/templates/research-project/PITFALLS.md +200 -0
  299. package/get-shit-done/templates/research-project/STACK.md +120 -0
  300. package/get-shit-done/templates/research-project/SUMMARY.md +170 -0
  301. package/get-shit-done/templates/research.md +592 -0
  302. package/get-shit-done/templates/retrospective.md +54 -0
  303. package/get-shit-done/templates/roadmap.md +202 -0
  304. package/get-shit-done/templates/spec.md +307 -0
  305. package/get-shit-done/templates/state.md +195 -0
  306. package/get-shit-done/templates/summary-complex.md +59 -0
  307. package/get-shit-done/templates/summary-minimal.md +41 -0
  308. package/get-shit-done/templates/summary-standard.md +48 -0
  309. package/get-shit-done/templates/summary.md +248 -0
  310. package/get-shit-done/templates/user-profile.md +146 -0
  311. package/get-shit-done/templates/user-setup.md +311 -0
  312. package/get-shit-done/templates/verification-report.md +322 -0
  313. package/get-shit-done/workflows/_runtime-launcher.snippet.sh +1 -0
  314. package/get-shit-done/workflows/add-backlog.md +91 -0
  315. package/get-shit-done/workflows/add-phase.md +113 -0
  316. package/get-shit-done/workflows/add-tests.md +355 -0
  317. package/get-shit-done/workflows/add-todo.md +161 -0
  318. package/get-shit-done/workflows/ai-integration-phase.md +295 -0
  319. package/get-shit-done/workflows/analyze-dependencies.md +96 -0
  320. package/get-shit-done/workflows/audit-fix.md +178 -0
  321. package/get-shit-done/workflows/audit-milestone.md +358 -0
  322. package/get-shit-done/workflows/audit-uat.md +110 -0
  323. package/get-shit-done/workflows/autonomous.md +795 -0
  324. package/get-shit-done/workflows/check-todos.md +180 -0
  325. package/get-shit-done/workflows/cleanup.md +155 -0
  326. package/get-shit-done/workflows/code-review-fix.md +502 -0
  327. package/get-shit-done/workflows/code-review.md +656 -0
  328. package/get-shit-done/workflows/complete-milestone.md +855 -0
  329. package/get-shit-done/workflows/debug.md +232 -0
  330. package/get-shit-done/workflows/diagnose-issues.md +241 -0
  331. package/get-shit-done/workflows/discovery-phase.md +291 -0
  332. package/get-shit-done/workflows/discuss-phase/modes/advisor.md +176 -0
  333. package/get-shit-done/workflows/discuss-phase/modes/all.md +28 -0
  334. package/get-shit-done/workflows/discuss-phase/modes/analyze.md +44 -0
  335. package/get-shit-done/workflows/discuss-phase/modes/auto.md +57 -0
  336. package/get-shit-done/workflows/discuss-phase/modes/batch.md +52 -0
  337. package/get-shit-done/workflows/discuss-phase/modes/chain.md +98 -0
  338. package/get-shit-done/workflows/discuss-phase/modes/default.md +141 -0
  339. package/get-shit-done/workflows/discuss-phase/modes/power.md +44 -0
  340. package/get-shit-done/workflows/discuss-phase/modes/text.md +55 -0
  341. package/get-shit-done/workflows/discuss-phase/templates/checkpoint.json +18 -0
  342. package/get-shit-done/workflows/discuss-phase/templates/context.md +136 -0
  343. package/get-shit-done/workflows/discuss-phase/templates/discussion-log.md +50 -0
  344. package/get-shit-done/workflows/discuss-phase-assumptions.md +675 -0
  345. package/get-shit-done/workflows/discuss-phase-power.md +291 -0
  346. package/get-shit-done/workflows/discuss-phase.md +499 -0
  347. package/get-shit-done/workflows/do.md +111 -0
  348. package/get-shit-done/workflows/docs-update.md +1162 -0
  349. package/get-shit-done/workflows/edit-phase.md +295 -0
  350. package/get-shit-done/workflows/eval-review.md +156 -0
  351. package/get-shit-done/workflows/execute-phase/steps/codebase-drift-gate.md +82 -0
  352. package/get-shit-done/workflows/execute-phase/steps/per-plan-worktree-gate.md +94 -0
  353. package/get-shit-done/workflows/execute-phase/steps/post-merge-gate.md +117 -0
  354. package/get-shit-done/workflows/execute-phase.md +1709 -0
  355. package/get-shit-done/workflows/execute-plan.md +526 -0
  356. package/get-shit-done/workflows/explore.md +144 -0
  357. package/get-shit-done/workflows/extract-learnings.md +243 -0
  358. package/get-shit-done/workflows/fast.md +124 -0
  359. package/get-shit-done/workflows/forensics.md +279 -0
  360. package/get-shit-done/workflows/graduation.md +196 -0
  361. package/get-shit-done/workflows/health.md +224 -0
  362. package/get-shit-done/workflows/help/modes/brief.md +22 -0
  363. package/get-shit-done/workflows/help/modes/default.md +50 -0
  364. package/get-shit-done/workflows/help/modes/full.md +784 -0
  365. package/get-shit-done/workflows/help/modes/topic.md +74 -0
  366. package/get-shit-done/workflows/help.md +24 -0
  367. package/get-shit-done/workflows/import.md +254 -0
  368. package/get-shit-done/workflows/inbox.md +387 -0
  369. package/get-shit-done/workflows/ingest-docs.md +339 -0
  370. package/get-shit-done/workflows/insert-phase.md +152 -0
  371. package/get-shit-done/workflows/list-phase-assumptions.md +178 -0
  372. package/get-shit-done/workflows/list-workspaces.md +57 -0
  373. package/get-shit-done/workflows/manager.md +393 -0
  374. package/get-shit-done/workflows/map-codebase.md +444 -0
  375. package/get-shit-done/workflows/milestone-summary.md +224 -0
  376. package/get-shit-done/workflows/mvp-phase.md +222 -0
  377. package/get-shit-done/workflows/new-milestone.md +635 -0
  378. package/get-shit-done/workflows/new-project.md +1555 -0
  379. package/get-shit-done/workflows/new-workspace.md +240 -0
  380. package/get-shit-done/workflows/next.md +299 -0
  381. package/get-shit-done/workflows/node-repair.md +92 -0
  382. package/get-shit-done/workflows/note.md +158 -0
  383. package/get-shit-done/workflows/pause-work.md +244 -0
  384. package/get-shit-done/workflows/plan-milestone-gaps.md +281 -0
  385. package/get-shit-done/workflows/plan-phase.md +1809 -0
  386. package/get-shit-done/workflows/plan-review-convergence.md +346 -0
  387. package/get-shit-done/workflows/plant-seed.md +230 -0
  388. package/get-shit-done/workflows/pr-branch.md +157 -0
  389. package/get-shit-done/workflows/profile-user.md +453 -0
  390. package/get-shit-done/workflows/progress.md +699 -0
  391. package/get-shit-done/workflows/quick.md +1039 -0
  392. package/get-shit-done/workflows/reapply-patches.md +426 -0
  393. package/get-shit-done/workflows/remove-phase.md +156 -0
  394. package/get-shit-done/workflows/remove-workspace.md +108 -0
  395. package/get-shit-done/workflows/resume-project.md +332 -0
  396. package/get-shit-done/workflows/review.md +623 -0
  397. package/get-shit-done/workflows/scan.md +105 -0
  398. package/get-shit-done/workflows/secure-phase.md +180 -0
  399. package/get-shit-done/workflows/session-report.md +146 -0
  400. package/get-shit-done/workflows/settings-advanced.md +620 -0
  401. package/get-shit-done/workflows/settings-integrations.md +312 -0
  402. package/get-shit-done/workflows/settings.md +552 -0
  403. package/get-shit-done/workflows/ship.md +356 -0
  404. package/get-shit-done/workflows/sketch-wrap-up.md +286 -0
  405. package/get-shit-done/workflows/sketch.md +361 -0
  406. package/get-shit-done/workflows/spec-phase.md +262 -0
  407. package/get-shit-done/workflows/spike-wrap-up.md +307 -0
  408. package/get-shit-done/workflows/spike.md +453 -0
  409. package/get-shit-done/workflows/stats.md +80 -0
  410. package/get-shit-done/workflows/sync-skills.md +182 -0
  411. package/get-shit-done/workflows/thread.md +222 -0
  412. package/get-shit-done/workflows/transition.md +694 -0
  413. package/get-shit-done/workflows/ui-phase.md +328 -0
  414. package/get-shit-done/workflows/ui-review.md +193 -0
  415. package/get-shit-done/workflows/ultraplan-phase.md +199 -0
  416. package/get-shit-done/workflows/undo.md +314 -0
  417. package/get-shit-done/workflows/update.md +443 -0
  418. package/get-shit-done/workflows/validate-phase.md +179 -0
  419. package/get-shit-done/workflows/verify-phase.md +544 -0
  420. package/get-shit-done/workflows/verify-work.md +781 -0
  421. package/hooks/dist/gsd-check-update-worker.js +95 -0
  422. package/hooks/dist/gsd-check-update.js +64 -0
  423. package/hooks/dist/gsd-context-monitor.js +195 -0
  424. package/hooks/dist/gsd-graphify-update.sh +158 -0
  425. package/hooks/dist/gsd-phase-boundary.sh +47 -0
  426. package/hooks/dist/gsd-prompt-guard.js +97 -0
  427. package/hooks/dist/gsd-read-guard.js +101 -0
  428. package/hooks/dist/gsd-read-injection-scanner.js +203 -0
  429. package/hooks/dist/gsd-session-state.sh +59 -0
  430. package/hooks/dist/gsd-statusline.js +548 -0
  431. package/hooks/dist/gsd-update-banner.js +134 -0
  432. package/hooks/dist/gsd-validate-commit.sh +57 -0
  433. package/hooks/dist/gsd-workflow-guard.js +166 -0
  434. package/hooks/dist/lib/git-cmd.js +150 -0
  435. package/hooks/dist/lib/gsd-graphify-rebuild.sh +65 -0
  436. package/hooks/gsd-check-update-worker.js +95 -0
  437. package/hooks/gsd-check-update.js +64 -0
  438. package/hooks/gsd-context-monitor.js +195 -0
  439. package/hooks/gsd-graphify-update.sh +158 -0
  440. package/hooks/gsd-phase-boundary.sh +47 -0
  441. package/hooks/gsd-prompt-guard.js +97 -0
  442. package/hooks/gsd-read-guard.js +101 -0
  443. package/hooks/gsd-read-injection-scanner.js +203 -0
  444. package/hooks/gsd-session-state.sh +59 -0
  445. package/hooks/gsd-statusline.js +548 -0
  446. package/hooks/gsd-update-banner.js +134 -0
  447. package/hooks/gsd-validate-commit.sh +57 -0
  448. package/hooks/gsd-workflow-guard.js +166 -0
  449. package/hooks/lib/git-cmd.js +150 -0
  450. package/hooks/lib/gsd-graphify-rebuild.sh +65 -0
  451. package/hooks/managed-hooks-registry.cjs +34 -0
  452. package/package.json +102 -0
  453. package/scripts/affected-tests-lib.cjs +541 -0
  454. package/scripts/audit-workflow-script-paths.cjs +73 -0
  455. package/scripts/base64-scan.sh +339 -0
  456. package/scripts/build-hooks.js +236 -0
  457. package/scripts/changeset/README.md +129 -0
  458. package/scripts/changeset/cli.cjs +392 -0
  459. package/scripts/changeset/github-release-notes.cjs +199 -0
  460. package/scripts/changeset/lint.cjs +110 -0
  461. package/scripts/changeset/new.cjs +137 -0
  462. package/scripts/changeset/parse.cjs +114 -0
  463. package/scripts/changeset/render.cjs +34 -0
  464. package/scripts/changeset/serialize.cjs +130 -0
  465. package/scripts/check-alias-drift.cjs +108 -0
  466. package/scripts/check-env.cjs +302 -0
  467. package/scripts/check-npm-integrity.cjs +209 -0
  468. package/scripts/ci-guard-runner.cjs +16 -0
  469. package/scripts/ci-prepare-test-scope.cjs +46 -0
  470. package/scripts/ci-rebase-check.cjs +85 -0
  471. package/scripts/ci-test-scope.cjs +302 -0
  472. package/scripts/command-contract-helpers.cjs +64 -0
  473. package/scripts/diff-touches-shipped-paths.cjs +147 -0
  474. package/scripts/fix-slash-commands.cjs +147 -0
  475. package/scripts/gen-inventory-manifest.cjs +109 -0
  476. package/scripts/generate-package-identity.cjs +104 -0
  477. package/scripts/lint-command-contract.cjs +108 -0
  478. package/scripts/lint-descriptions.cjs +83 -0
  479. package/scripts/lint-docs-required.cjs +222 -0
  480. package/scripts/lint-no-source-grep-extras.cjs +81 -0
  481. package/scripts/lint-no-source-grep.cjs +174 -0
  482. package/scripts/lint-package-identity-drift.cjs +141 -0
  483. package/scripts/lint-pr-check-project-dir.cjs +98 -0
  484. package/scripts/lint-shared-module-handsync.cjs +388 -0
  485. package/scripts/lint-shell-command-projection-drift.cjs +57 -0
  486. package/scripts/lint-skill-deps.cjs +180 -0
  487. package/scripts/lint-test-file-count.allowlist.json +36 -0
  488. package/scripts/lint-test-file-count.cjs +190 -0
  489. package/scripts/pr-template-policy.cjs +268 -0
  490. package/scripts/prompt-injection-scan.sh +203 -0
  491. package/scripts/release-tarball-smoke.cjs +627 -0
  492. package/scripts/run-affected-tests.cjs +6 -0
  493. package/scripts/run-cross-platform-tests.cjs +63 -0
  494. package/scripts/run-tests.cjs +282 -0
  495. package/scripts/secret-scan-lint.sh +231 -0
  496. package/scripts/secret-scan.sh +358 -0
  497. package/scripts/setup-branch-protection.sh +236 -0
  498. package/scripts/shared-module-handsync-allowlist.json +183 -0
  499. package/scripts/strip-prose-atrefs.cjs +106 -0
  500. package/scripts/sync-rulesets.sh +34 -0
  501. package/scripts/sync-runtime-launcher.cjs +402 -0
  502. package/scripts/test-failure-reasons.cjs +34 -0
  503. package/scripts/workflow-policy.cjs +450 -0
@@ -0,0 +1,1676 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * GSD Tools — CLI utility for GSD workflow operations.
5
+ *
6
+ * Replaces repetitive inline bash patterns across ~50 GSD command/workflow/agent files.
7
+ * Centralizes: config parsing, model resolution, phase lookup, git commits, summary verification.
8
+ *
9
+ * Usage: node gsd-tools.cjs <command> [args] [--raw] [--pick <field>]
10
+ *
11
+ * Atomic Commands:
12
+ * state load Load project config + state
13
+ * state json Output STATE.md frontmatter as JSON
14
+ * state update <field> <value> Update a STATE.md field
15
+ * state get [section] Get STATE.md content or section
16
+ * state patch --field val ... Batch update STATE.md fields
17
+ * state begin-phase --phase N --name S --plans C Update STATE.md for new phase start
18
+ * state signal-waiting --type T --question Q --options "A|B" --phase P Write WAITING.json signal
19
+ * state signal-resume Remove WAITING.json signal
20
+ * resolve-model <agent-type> Get model for agent based on profile
21
+ * find-phase <phase> Find phase directory by number
22
+ * commit <message> [--files f1 f2] [--no-verify] Commit planning docs
23
+ * commit-to-subrepo <msg> --files f1 f2 Route commits to sub-repos
24
+ * verify-summary <path> Verify a SUMMARY.md file
25
+ * generate-slug <text> Convert text to URL-safe slug
26
+ * current-timestamp [format] Get timestamp (full|date|filename)
27
+ * list-todos [area] Count and enumerate pending todos
28
+ * verify-path-exists <path> Check file/directory existence
29
+ * config-ensure-section Initialize .planning/config.json
30
+ * history-digest Aggregate all SUMMARY.md data
31
+ * summary-extract <path> [--fields] Extract structured data from SUMMARY.md
32
+ * state-snapshot Structured parse of STATE.md
33
+ * phase-plan-index <phase> Index plans with waves and status
34
+ * websearch <query> Search web via Brave API (if configured)
35
+ * [--limit N] [--freshness day|week|month]
36
+ *
37
+ * Phase Operations:
38
+ * phase next-decimal <phase> Calculate next decimal phase number
39
+ * phase add <description> [--id ID] Append new phase to roadmap + create dir
40
+ * phase insert <after> <description> Insert decimal phase after existing
41
+ * phase remove <phase> [--force] Remove phase, renumber all subsequent
42
+ * phase complete <phase> Mark phase done, update state + roadmap
43
+ *
44
+ * Roadmap Operations:
45
+ * roadmap get-phase <phase> Extract phase section from ROADMAP.md
46
+ * roadmap analyze Full roadmap parse with disk status
47
+ * roadmap update-plan-progress <N> Update progress table row from disk (PLAN vs SUMMARY counts)
48
+ * roadmap annotate-dependencies <N> Add wave dependency notes + cross-cutting constraints to ROADMAP.md
49
+ *
50
+ * Requirements Operations:
51
+ * requirements mark-complete <ids> Mark requirement IDs as complete in REQUIREMENTS.md
52
+ * Accepts: REQ-01,REQ-02 or REQ-01 REQ-02 or [REQ-01, REQ-02]
53
+ *
54
+ * Milestone Operations:
55
+ * milestone complete <version> Archive milestone, create MILESTONES.md
56
+ * [--name <name>]
57
+ * [--archive-phases] Move phase dirs to milestones/vX.Y-phases/
58
+ *
59
+ * Validation:
60
+ * validate consistency Check phase numbering, disk/roadmap sync
61
+ * validate health [--repair] Check .planning/ integrity, optionally repair
62
+ * validate agents Check GSD agent installation status
63
+ *
64
+ * Progress:
65
+ * progress [json|table|bar] Render progress in various formats
66
+ *
67
+ * Todos:
68
+ * todo complete <filename> Move todo from pending to completed
69
+ *
70
+ * UAT Audit:
71
+ * audit-uat Scan all phases for unresolved UAT/verification items
72
+ * uat render-checkpoint --file <path> Render the current UAT checkpoint block
73
+ *
74
+ * Open Artifact Audit:
75
+ * audit-open [--json] Scan all .planning/ artifact types for unresolved items
76
+ *
77
+ * Intel:
78
+ * intel query <term> Query intel files for a term
79
+ * intel status Show intel file freshness
80
+ * intel update Trigger intel refresh (returns agent spawn hint)
81
+ * intel diff Show changed intel entries since last snapshot
82
+ * intel snapshot Save current intel state as diff baseline
83
+ * intel patch-meta <file> Update _meta.updated_at in an intel file
84
+ * intel validate Validate intel file structure
85
+ * intel extract-exports <file> Extract exported symbols from a source file
86
+ * intel api-surface Render api-map.json into API-SURFACE.md
87
+ *
88
+ * Scaffolding:
89
+ * scaffold context --phase <N> Create CONTEXT.md template
90
+ * scaffold uat --phase <N> Create UAT.md template
91
+ * scaffold verification --phase <N> Create VERIFICATION.md template
92
+ * scaffold phase-dir --phase <N> Create phase directory
93
+ * --name <name>
94
+ *
95
+ * Frontmatter CRUD:
96
+ * frontmatter get <file> [--field k] Extract frontmatter as JSON
97
+ * frontmatter set <file> --field k Update single frontmatter field
98
+ * --value jsonVal
99
+ * frontmatter merge <file> Merge JSON into frontmatter
100
+ * --data '{json}'
101
+ * frontmatter validate <file> Validate required fields
102
+ * --schema plan|summary|verification
103
+ *
104
+ * Verification Suite:
105
+ * verify plan-structure <file> Check PLAN.md structure + tasks
106
+ * verify phase-completeness <phase> Check all plans have summaries
107
+ * verify references <file> Check @-refs + paths resolve
108
+ * verify commits <h1> [h2] ... Batch verify commit hashes
109
+ * verify artifacts <plan-file> Check must_haves.artifacts
110
+ * verify key-links <plan-file> Check must_haves.key_links
111
+ * verify schema-drift <phase> [--skip] Detect schema file changes without push
112
+ * verify codebase-drift Detect structural drift since last codebase map (#2003)
113
+ *
114
+ * Template Fill:
115
+ * template fill summary --phase N Create pre-filled SUMMARY.md
116
+ * [--plan M] [--name "..."]
117
+ * [--fields '{json}']
118
+ * template fill plan --phase N Create pre-filled PLAN.md
119
+ * [--plan M] [--type execute|tdd]
120
+ * [--wave N] [--fields '{json}']
121
+ * template fill verification Create pre-filled VERIFICATION.md
122
+ * --phase N [--fields '{json}']
123
+ *
124
+ * State Progression:
125
+ * state advance-plan Increment plan counter
126
+ * state record-metric --phase N Record execution metrics
127
+ * --plan M --duration Xmin
128
+ * [--tasks N] [--files N]
129
+ * state update-progress Recalculate progress bar
130
+ * state add-decision --summary "..." Add decision to STATE.md
131
+ * [--phase N] [--rationale "..."]
132
+ * [--summary-file path] [--rationale-file path]
133
+ * state add-blocker --text "..." Add blocker
134
+ * [--text-file path]
135
+ * state resolve-blocker --text "..." Remove blocker
136
+ * state record-session Update session continuity
137
+ * --stopped-at "..."
138
+ * [--resume-file path]
139
+ *
140
+ * Compound Commands (workflow-specific initialization):
141
+ * init execute-phase <phase> All context for execute-phase workflow
142
+ * init plan-phase <phase> All context for plan-phase workflow
143
+ * init new-project All context for new-project workflow
144
+ * init new-milestone All context for new-milestone workflow
145
+ * init quick <description> All context for quick workflow
146
+ * init resume All context for resume-project workflow
147
+ * init verify-work <phase> All context for verify-work workflow
148
+ * init phase-op <phase> Generic phase operation context
149
+ * init todos [area] All context for todo workflows
150
+ * init milestone-op All context for milestone operations
151
+ * init map-codebase All context for map-codebase workflow
152
+ * init progress All context for progress workflow
153
+ *
154
+ * Documentation:
155
+ * docs-init Project context for docs-update workflow
156
+ *
157
+ * Learnings:
158
+ * learnings list List all global learnings (JSON)
159
+ * learnings query --tag <tag> Query learnings by tag
160
+ * learnings copy Copy from current project's LEARNINGS.md
161
+ * learnings prune --older-than <dur> Remove entries older than duration (e.g. 90d)
162
+ * learnings delete <id> Delete a learning by ID
163
+ *
164
+ * GSD-2 Migration:
165
+ * from-gsd2 [--path <dir>] [--force] [--dry-run]
166
+ * Import a GSD-2 (.gsd/) project back to GSD v1 (.planning/) format
167
+ */
168
+
169
+ const fs = require('fs');
170
+ const path = require('path');
171
+ const core = require('./lib/core.cjs');
172
+ const { error, findProjectRoot, ERROR_REASON } = core;
173
+ const { getActiveWorkstream } = require('./lib/planning-workspace.cjs');
174
+ const { resolveActiveWorkstream, applyResolvedWorkstreamEnv } = require('./lib/active-workstream-store.cjs');
175
+ const state = require('./lib/state.cjs');
176
+ const phase = require('./lib/phase.cjs');
177
+ const roadmap = require('./lib/roadmap.cjs');
178
+ const verify = require('./lib/verify.cjs');
179
+ const config = require('./lib/config.cjs');
180
+ const template = require('./lib/template.cjs');
181
+ const milestone = require('./lib/milestone.cjs');
182
+ const commands = require('./lib/commands.cjs');
183
+ const init = require('./lib/init.cjs');
184
+ const frontmatter = require('./lib/frontmatter.cjs');
185
+ const profilePipeline = require('./lib/profile-pipeline.cjs');
186
+ const profileOutput = require('./lib/profile-output.cjs');
187
+ const workstream = require('./lib/workstream.cjs');
188
+ const docs = require('./lib/docs.cjs');
189
+ const learnings = require('./lib/learnings.cjs');
190
+ const gapChecker = require('./lib/gap-checker.cjs');
191
+ const { routeStateCommand } = require('./lib/state-command-router.cjs');
192
+ const { routeVerifyCommand } = require('./lib/verify-command-router.cjs');
193
+ const { routeInitCommand } = require('./lib/init-command-router.cjs');
194
+ const { routePhaseCommand } = require('./lib/phase-command-router.cjs');
195
+ const { routePhasesCommand } = require('./lib/phases-command-router.cjs');
196
+ const { routeValidateCommand } = require('./lib/validate-command-router.cjs');
197
+ const { routeRoadmapCommand } = require('./lib/roadmap-command-router.cjs');
198
+ const { routeAgentCommand } = require('./lib/agent-command-router.cjs');
199
+ const { routeCheckCommand } = require('./lib/check-command-router.cjs');
200
+ const { routeTaskCommand } = require('./lib/task-command-router.cjs');
201
+ const { parseNamedArgs, parseMultiwordArg } = require('./lib/command-arg-projection.cjs');
202
+
203
+ // ─── Bridge collapsed (Phase 4) ────────────────────────────────────────────────
204
+ // Non-family commands now run through their CJS handlers directly. Keep the
205
+ // helper contract so existing call sites remain unchanged during the phase
206
+ // sequence; it always returns false so callers fall through to CJS.
207
+
208
+ /**
209
+ * Retired bridge-era shim for non-family dispatch.
210
+ *
211
+ * Always returns false so command handlers continue down the CJS path.
212
+ * Kept only to avoid churn while legacy call sites are being deleted.
213
+ *
214
+ * @param {object} opts
215
+ * @param {string} opts.registryCommand - legacy bridge placeholder
216
+ * @param {string[]} opts.registryArgs - legacy bridge placeholder
217
+ * @param {string} opts.legacyCommand - original gsd-tools command name
218
+ * @param {string[]} opts.legacyArgs - original args
219
+ * @param {string} opts.cwd - project dir
220
+ * @param {boolean} opts.raw - raw output mode
221
+ * @param {Function} opts.error - error reporter
222
+ * @param {Function} opts.output - output emitter (core.output)
223
+ */
224
+ function _dispatchNonFamily({ registryCommand, registryArgs, legacyCommand, legacyArgs, cwd, raw, error, output }) {
225
+ void registryCommand;
226
+ void registryArgs;
227
+ void legacyCommand;
228
+ void legacyArgs;
229
+ void cwd;
230
+ void raw;
231
+ void error;
232
+ void output;
233
+ return false;
234
+ }
235
+
236
+ // ─── Arg parsing helpers ──────────────────────────────────────────────────────
237
+
238
+ // ─── CLI Router ───────────────────────────────────────────────────────────────
239
+
240
+ async function main() {
241
+ let args = process.argv.slice(2);
242
+
243
+ // --json-errors / GSD_JSON_ERRORS=1: when active, error() emits structured
244
+ // JSON ({ ok: false, reason: <ERROR_REASON code>, message }) to stderr
245
+ // instead of "Error: <text>". Lets test suites assert on typed reason codes
246
+ // per CONTRIBUTING.md "Prohibited: Raw Text Matching" (#2974).
247
+ //
248
+ // Detect early — before any flag parsing that can fire error() — so even
249
+ // --cwd and workstream-resolution failures emit structured stderr (#3310).
250
+ // The argv splice must happen here too, otherwise the dispatcher below sees
251
+ // "--json-errors" as an unknown command. Default off — human operators keep
252
+ // their plain-text diagnostic.
253
+ const jsonErrorsIdx = args.indexOf('--json-errors');
254
+ if (jsonErrorsIdx !== -1) {
255
+ core.setJsonErrorMode(true);
256
+ args.splice(jsonErrorsIdx, 1);
257
+ } else if (process.env.GSD_JSON_ERRORS === '1') {
258
+ core.setJsonErrorMode(true);
259
+ }
260
+
261
+ // Optional cwd override for sandboxed subagents running outside project root.
262
+ let cwd = process.cwd();
263
+ const cwdEqArg = args.find(arg => arg.startsWith('--cwd='));
264
+ const cwdIdx = args.indexOf('--cwd');
265
+ if (cwdEqArg) {
266
+ const value = cwdEqArg.slice('--cwd='.length).trim();
267
+ if (!value) error('Missing value for --cwd', ERROR_REASON.USAGE);
268
+ args.splice(args.indexOf(cwdEqArg), 1);
269
+ cwd = path.resolve(value);
270
+ } else if (cwdIdx !== -1) {
271
+ const value = args[cwdIdx + 1];
272
+ if (!value || value.startsWith('--')) error('Missing value for --cwd', ERROR_REASON.USAGE);
273
+ args.splice(cwdIdx, 2);
274
+ cwd = path.resolve(value);
275
+ }
276
+
277
+ if (!fs.existsSync(cwd) || !fs.statSync(cwd).isDirectory()) {
278
+ error(`Invalid --cwd: ${cwd}`, ERROR_REASON.USAGE);
279
+ }
280
+
281
+ // Resolve worktree root: in a linked worktree, .planning/ lives in the main worktree.
282
+ // However, in monorepo worktrees where the subdirectory itself owns .planning/,
283
+ // skip worktree resolution — the CWD is already the correct project root.
284
+ const { resolveWorktreeRoot } = require('./lib/core.cjs');
285
+ if (!fs.existsSync(path.join(cwd, '.planning'))) {
286
+ const worktreeRoot = resolveWorktreeRoot(cwd);
287
+ if (worktreeRoot !== cwd) {
288
+ cwd = worktreeRoot;
289
+ }
290
+ }
291
+
292
+ // Optional workstream override for parallel milestone work.
293
+ // Priority: --ws flag > GSD_WORKSTREAM env var > session/shared pointer > null.
294
+ let ws = null;
295
+ let workstreamContext = null;
296
+ try {
297
+ workstreamContext = resolveActiveWorkstream(cwd, args, process.env, {
298
+ getStored: getActiveWorkstream,
299
+ });
300
+ ws = workstreamContext.ws;
301
+ args = workstreamContext.args;
302
+ // Set env var so all modules (planningDir, planningPaths) auto-resolve workstream paths.
303
+ applyResolvedWorkstreamEnv(workstreamContext, process.env);
304
+ } catch (err) {
305
+ error(err.message || String(err));
306
+ }
307
+
308
+ const rawIndex = args.indexOf('--raw');
309
+ const raw = rawIndex !== -1;
310
+ if (rawIndex !== -1) args.splice(rawIndex, 1);
311
+
312
+ // --pick <name>: extract a single field from JSON output (replaces jq dependency).
313
+ // Supports dot-notation (e.g., --pick workflow.research) and bracket notation
314
+ // for arrays (e.g., --pick directories[-1]).
315
+ const pickIdx = args.indexOf('--pick');
316
+ let pickField = null;
317
+ if (pickIdx !== -1) {
318
+ pickField = args[pickIdx + 1];
319
+ if (!pickField || pickField.startsWith('--')) error('Missing value for --pick', ERROR_REASON.USAGE);
320
+ args.splice(pickIdx, 2);
321
+ }
322
+
323
+ // --default <value>: for config-get, return this value instead of erroring
324
+ // when the key is absent. Allows workflows to express optional config reads
325
+ // without defensive `2>/dev/null || true` boilerplate (#1893).
326
+ const defaultIdx = args.indexOf('--default');
327
+ let defaultValue = undefined;
328
+ if (defaultIdx !== -1) {
329
+ defaultValue = args[defaultIdx + 1];
330
+ if (defaultValue === undefined) defaultValue = '';
331
+ args.splice(defaultIdx, 2);
332
+ }
333
+
334
+ let command = args[0];
335
+
336
+ // Accept `query` as a meta-prefix for canonical dotted/spaced commands.
337
+ // Workflows may call `node gsd-tools.cjs query <command>` directly.
338
+ if (command === 'query') {
339
+ args.shift();
340
+ command = args[0];
341
+ }
342
+
343
+ // #3243: accept dotted canonical form (e.g. `state.update`) as well as the
344
+ // spaced form (`state update`). Some workflow callers pass the dotted
345
+ // canonical form directly; this normalization keeps both forms valid.
346
+ //
347
+ // Split on the FIRST dot only — `check.decision-coverage-plan` becomes
348
+ // command='check', args=['check','decision-coverage-plan',...rest].
349
+ // Guard: head and rest must both be non-empty (rejects leading-dot args like
350
+ // ".hidden" and bare-dot ".").
351
+ const originalCommand = command; // preserved for "Unknown command" suggestion
352
+ if (typeof command === 'string' && command.includes('.')) {
353
+ const dotIdx = command.indexOf('.');
354
+ const head = command.slice(0, dotIdx);
355
+ const rest = command.slice(dotIdx + 1);
356
+ if (head && rest) {
357
+ command = head;
358
+ args = [head, rest, ...args.slice(1)];
359
+ }
360
+ }
361
+
362
+ // Top-level usage string — emitted by `gsd-tools` (no args) and by
363
+ // `gsd-tools --help` / any `--help` request below.
364
+ // CR feedback: the command list must enumerate every top-level command
365
+ // supported by the dispatcher so `--help` is actually useful for
366
+ // discovery; previously it was a partial subset that didn't include
367
+ // phase / roadmap / milestone / progress / etc.
368
+ const TOP_LEVEL_USAGE = 'Usage: gsd-tools <command> [args] [--raw] [--pick <field>] [--cwd <path>] [--ws <name>] [--json-errors]\n' +
369
+ 'Commands: agent, agent-skills, audit-open, audit-uat, check, check-commit, commit, commit-to-subrepo, ' +
370
+ 'config-ensure-section, config-get, config-new-project, config-path, config-set, migrate-config, ' +
371
+ 'current-timestamp, detect-custom-files, docs-init, extract-messages, find-phase, ' +
372
+ 'from-gsd2, frontmatter, gap-analysis, generate-claude-md, generate-claude-profile, ' +
373
+ 'generate-dev-preferences, generate-slug, graphify, history-digest, init, intel, ' +
374
+ 'learnings, list-todos, milestone, phase, phase-plan-index, phases, profile-questionnaire, ' +
375
+ 'profile-sample, progress, prompt-budget, requirements, resolve-model, roadmap, scaffold, state, ' +
376
+ 'task, template, validate, verify, verify-path-exists, verify-summary, workstream, worktree\n\n' +
377
+ 'Global flags:\n' +
378
+ ' --raw Emit raw output without post-processing\n' +
379
+ ' --pick <field> Extract a single field from JSON output (dot/bracket notation)\n' +
380
+ ' --cwd <path> Override working directory for project-root resolution\n' +
381
+ ' --ws <name> Override active workstream (or set GSD_WORKSTREAM)\n' +
382
+ ' --json-errors Emit structured JSON error objects on stderr (or set GSD_JSON_ERRORS=1)\n\n' +
383
+ 'For command-specific argument requirements, invoke the command without args ' +
384
+ '(e.g. `gsd-tools phase add`) — the resulting error lists what is required.';
385
+
386
+ if (!command) {
387
+ error(TOP_LEVEL_USAGE);
388
+ }
389
+
390
+ // #3019: a `--help` / `-h` flag in argv must render the top-level usage
391
+ // and exit 0 — not error out with "Unknown flag". The previous shape
392
+ // erred on agent-hallucinated flags, but it also blocked humans from
393
+ // discovering the command surface via subcommand help requests routed
394
+ // through this dispatcher. Rendering top-level usage on --help is strictly
395
+ // better UX than the old short-circuit that printed unrelated usage text.
396
+ const HELP_FLAGS = new Set(['-h', '--help', '-?', '--h', '--usage']);
397
+ if (args.some((a) => HELP_FLAGS.has(a))) {
398
+ process.stdout.write(TOP_LEVEL_USAGE + '\n');
399
+ return;
400
+ }
401
+
402
+ // Reject version flags. AI agents sometimes hallucinate --version on tool
403
+ // invocations; silently ignoring it can cause destructive operations to
404
+ // proceed unchecked. (Help flags are handled above.)
405
+ const NEVER_VALID_FLAGS = new Set(['--version', '-v']);
406
+ for (const arg of args) {
407
+ if (NEVER_VALID_FLAGS.has(arg)) {
408
+ error(`Unknown flag: ${arg}\ngsd-tools does not accept version flags. Run "gsd-tools" with no arguments for usage.`, ERROR_REASON.USAGE);
409
+ }
410
+ }
411
+
412
+ // Multi-repo guard: resolve project root for commands that read/write .planning/.
413
+ // Skip for pure-utility commands that don't touch .planning/ to avoid unnecessary
414
+ // filesystem traversal on every invocation.
415
+ const SKIP_ROOT_RESOLUTION = new Set([
416
+ 'generate-slug', 'current-timestamp', 'verify-path-exists',
417
+ 'verify-summary', 'template', 'frontmatter', 'detect-custom-files',
418
+ 'worktree', 'prompt-budget',
419
+ ]);
420
+ if (!SKIP_ROOT_RESOLUTION.has(command)) {
421
+ cwd = findProjectRoot(cwd);
422
+ }
423
+
424
+ // When --pick is active, capture stdout and extract the requested field.
425
+ if (pickField) {
426
+ const captured = await captureStdoutSyncWrites(async () => {
427
+ await runCommand(command, args, cwd, raw, defaultValue, originalCommand, workstreamContext);
428
+ });
429
+ const resolved = resolveAtFileOutput(captured);
430
+ try {
431
+ const obj = JSON.parse(resolved);
432
+ const value = extractField(obj, pickField);
433
+ const result = value === null || value === undefined ? '' : String(value);
434
+ fs.writeSync(1, result);
435
+ } catch {
436
+ fs.writeSync(1, captured);
437
+ }
438
+ return;
439
+ }
440
+
441
+ // Intercept stdout to transparently resolve @file: references (#1891).
442
+ // core.cjs output() writes @file:<path> when JSON > 50KB. The --pick path
443
+ // already resolves this, but the normal path wrote @file: to stdout, forcing
444
+ // every workflow to have a bash-specific `if [[ "$INIT" == @file:* ]]` check
445
+ // that breaks on PowerShell and other non-bash shells.
446
+ const captured = await captureStdoutSyncWrites(async () => {
447
+ await runCommand(command, args, cwd, raw, defaultValue, originalCommand, workstreamContext);
448
+ });
449
+ fs.writeSync(1, resolveAtFileOutput(captured));
450
+ }
451
+
452
+ function captureStdoutSyncWrites(run) {
453
+ const originalWriteSync = fs.writeSync;
454
+ let captured = '';
455
+
456
+ fs.writeSync = function patchedWriteSync(fd, data, ...rest) {
457
+ if (fd === 1) {
458
+ if (Buffer.isBuffer(data)) {
459
+ captured += data.toString('utf-8');
460
+ return data.length;
461
+ }
462
+ const text = String(data);
463
+ captured += text;
464
+ let encoding = 'utf-8';
465
+ if (typeof rest[1] === 'string') encoding = rest[1];
466
+ return Buffer.byteLength(text, encoding);
467
+ }
468
+ return originalWriteSync.call(fs, fd, data, ...rest);
469
+ };
470
+
471
+ const restore = () => {
472
+ fs.writeSync = originalWriteSync;
473
+ };
474
+
475
+ return Promise.resolve()
476
+ .then(() => run())
477
+ .then(() => {
478
+ restore();
479
+ return captured;
480
+ }, (err) => {
481
+ restore();
482
+ throw err;
483
+ });
484
+ }
485
+
486
+ function resolveAtFileOutput(captured) {
487
+ if (!captured.startsWith('@file:')) return captured;
488
+ return fs.readFileSync(captured.slice(6), 'utf-8');
489
+ }
490
+
491
+ /**
492
+ * Extract a field from an object using dot-notation and bracket syntax.
493
+ * Supports: 'field', 'parent.child', 'arr[-1]', 'arr[0]'
494
+ */
495
+ function extractField(obj, fieldPath) {
496
+ const parts = fieldPath.split('.');
497
+ let current = obj;
498
+ for (const part of parts) {
499
+ if (current === null || current === undefined) return undefined;
500
+ const bracketMatch = part.match(/^(.+?)\[(-?\d+)]$/);
501
+ if (bracketMatch) {
502
+ const key = bracketMatch[1];
503
+ const index = parseInt(bracketMatch[2], 10);
504
+ current = current[key];
505
+ if (!Array.isArray(current)) return undefined;
506
+ current = index < 0 ? current[current.length + index] : current[index];
507
+ } else {
508
+ current = current[part];
509
+ }
510
+ }
511
+ return current;
512
+ }
513
+
514
+ async function runCommand(command, args, cwd, raw, defaultValue, originalCommand, workstreamContext = null) {
515
+ switch (command) {
516
+ case 'agent': {
517
+ routeAgentCommand({ args, raw });
518
+ break;
519
+ }
520
+
521
+ case 'check': {
522
+ routeCheckCommand({ args, cwd, raw });
523
+ break;
524
+ }
525
+
526
+ case 'state': {
527
+ routeStateCommand({
528
+ state,
529
+ args,
530
+ cwd,
531
+ raw,
532
+ error,
533
+ });
534
+ break;
535
+ }
536
+
537
+ case 'resolve-model': {
538
+ commands.cmdResolveModel(cwd, args[1], raw);
539
+ break;
540
+ }
541
+
542
+ case 'resolve-execution': {
543
+ // Deterministic flag parsing: consume --flag <value> pairs first,
544
+ // then the AGENT is the single remaining positional.
545
+ // Supports both orderings: <agent> --flag val AND --flag val <agent>.
546
+ // Also supports --flag=value form (same convention as --cwd= above).
547
+ const execArgs = args.slice(1);
548
+ let effortOverride;
549
+ let fastModeOverride;
550
+ let attempt;
551
+ const positionals = [];
552
+ for (let i = 0; i < execArgs.length; i++) {
553
+ const a = execArgs[i];
554
+ // --effort=<val> form
555
+ if (a.startsWith('--effort=')) {
556
+ effortOverride = a.slice('--effort='.length);
557
+ continue;
558
+ }
559
+ // --fast-mode=<val> form
560
+ if (a.startsWith('--fast-mode=')) {
561
+ const v = a.slice('--fast-mode='.length);
562
+ fastModeOverride = v === 'true' ? true : v === 'false' ? false : undefined;
563
+ continue;
564
+ }
565
+ // --attempt=<val> form
566
+ if (a.startsWith('--attempt=')) {
567
+ const v = a.slice('--attempt='.length);
568
+ const n = parseInt(v, 10);
569
+ if (!Number.isInteger(n) || n < 0) error('--attempt requires a non-negative integer', ERROR_REASON.USAGE);
570
+ attempt = n;
571
+ continue;
572
+ }
573
+ // --effort <val>
574
+ if (a === '--effort') {
575
+ const val = execArgs[i + 1];
576
+ if (val === undefined || val.startsWith('--')) error('Missing value for --effort', ERROR_REASON.USAGE);
577
+ effortOverride = val;
578
+ i++;
579
+ continue;
580
+ }
581
+ // --fast-mode <val>
582
+ if (a === '--fast-mode') {
583
+ const val = execArgs[i + 1];
584
+ if (val === undefined || val.startsWith('--')) error('Missing value for --fast-mode', ERROR_REASON.USAGE);
585
+ fastModeOverride = val === 'true' ? true : val === 'false' ? false : undefined;
586
+ i++;
587
+ continue;
588
+ }
589
+ // --attempt <val>
590
+ if (a === '--attempt') {
591
+ const val = execArgs[i + 1];
592
+ if (val === undefined || val.startsWith('--')) error('Missing value for --attempt', ERROR_REASON.USAGE);
593
+ const n = parseInt(val, 10);
594
+ if (!Number.isInteger(n) || n < 0) error('--attempt requires a non-negative integer', ERROR_REASON.USAGE);
595
+ attempt = n;
596
+ i++;
597
+ continue;
598
+ }
599
+ // --raw is handled by top-level arg processing; skip it here
600
+ if (a === '--raw') continue;
601
+ // Unknown flag
602
+ if (a.startsWith('-')) error(`Unknown flag for resolve-execution: ${a}`, ERROR_REASON.USAGE);
603
+ // Positional
604
+ positionals.push(a);
605
+ }
606
+ if (positionals.length === 0) error('agent-type required', ERROR_REASON.USAGE);
607
+ if (positionals.length > 1) error(`resolve-execution requires exactly one agent-type argument; got: ${positionals.join(', ')}`, ERROR_REASON.USAGE);
608
+ const agentTypeArg = positionals[0];
609
+ commands.cmdResolveExecution(cwd, agentTypeArg, raw, {
610
+ effortOverride,
611
+ fastModeOverride,
612
+ attempt,
613
+ });
614
+ break;
615
+ }
616
+
617
+ case 'find-phase': {
618
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
619
+ // SDK handler: findPhase in sdk/src/query/phase.ts.
620
+ const handled = _dispatchNonFamily({
621
+ registryCommand: 'find-phase',
622
+ registryArgs: args.slice(1),
623
+ legacyCommand: 'find-phase',
624
+ legacyArgs: args.slice(1),
625
+ cwd,
626
+ raw,
627
+ error,
628
+ output: core.output,
629
+ });
630
+ if (!handled) phase.cmdFindPhase(cwd, args[1], raw);
631
+ break;
632
+ }
633
+
634
+ case 'commit': {
635
+ const amend = args.includes('--amend');
636
+ const noVerify = args.includes('--no-verify');
637
+ const filesIndex = args.indexOf('--files');
638
+ // Collect all positional args between command name and first flag,
639
+ // then join them — handles both quoted ("multi word msg") and
640
+ // unquoted (multi word msg) invocations from different shells
641
+ const endIndex = filesIndex !== -1 ? filesIndex : args.length;
642
+ const messageArgs = args.slice(1, endIndex).filter(a => !a.startsWith('--'));
643
+ const message = messageArgs.join(' ') || undefined;
644
+ const files = filesIndex !== -1 ? args.slice(filesIndex + 1).filter(a => !a.startsWith('--')) : [];
645
+ commands.cmdCommit(cwd, message, files, raw, amend, noVerify);
646
+ break;
647
+ }
648
+
649
+ case 'check-commit': {
650
+ commands.cmdCheckCommit(cwd, raw);
651
+ break;
652
+ }
653
+
654
+ case 'commit-to-subrepo': {
655
+ const message = args[1];
656
+ const filesIndex = args.indexOf('--files');
657
+ const files = filesIndex !== -1 ? args.slice(filesIndex + 1).filter(a => !a.startsWith('--')) : [];
658
+ commands.cmdCommitToSubrepo(cwd, message, files, raw);
659
+ break;
660
+ }
661
+
662
+ case 'verify-summary': {
663
+ const summaryPath = args[1];
664
+ const countIndex = args.indexOf('--check-count');
665
+ const checkCount = countIndex !== -1 ? parseInt(args[countIndex + 1], 10) : 2;
666
+ verify.cmdVerifySummary(cwd, summaryPath, checkCount, raw);
667
+ break;
668
+ }
669
+
670
+ case 'template': {
671
+ const subcommand = args[1];
672
+ if (subcommand === 'select') {
673
+ template.cmdTemplateSelect(cwd, args[2], raw);
674
+ } else if (subcommand === 'fill') {
675
+ const templateType = args[2];
676
+ const { phase, plan, name, type, wave, fields: fieldsRaw } = parseNamedArgs(args, ['phase', 'plan', 'name', 'type', 'wave', 'fields']);
677
+ let fields = {};
678
+ if (fieldsRaw) {
679
+ const { safeJsonParse } = require('./lib/security.cjs');
680
+ const result = safeJsonParse(fieldsRaw, { label: '--fields' });
681
+ if (!result.ok) error(result.error);
682
+ fields = result.value;
683
+ }
684
+ template.cmdTemplateFill(cwd, templateType, {
685
+ phase, plan, name, fields,
686
+ type: type || 'execute',
687
+ wave: wave || '1',
688
+ }, raw);
689
+ } else {
690
+ error('Unknown template subcommand. Available: select, fill', ERROR_REASON.SDK_UNKNOWN_COMMAND);
691
+ }
692
+ break;
693
+ }
694
+
695
+ case 'task': {
696
+ routeTaskCommand({ args, cwd, raw });
697
+ break;
698
+ }
699
+
700
+ case 'frontmatter': {
701
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
702
+ // SDK handler: sdk/src/query/frontmatter.ts + frontmatter-mutation.ts.
703
+ // CJS fallback: frontmatter.cjs (cooperating sibling).
704
+ const subcommand = args[1];
705
+ const file = args[2];
706
+ const FRONTMATTER_SDK_MAP = {
707
+ get: 'frontmatter.get',
708
+ set: 'frontmatter.set',
709
+ merge: 'frontmatter.merge',
710
+ validate: 'frontmatter.validate',
711
+ };
712
+ if (subcommand in FRONTMATTER_SDK_MAP) {
713
+ const handled = _dispatchNonFamily({
714
+ registryCommand: FRONTMATTER_SDK_MAP[subcommand],
715
+ registryArgs: args.slice(2),
716
+ legacyCommand: 'frontmatter',
717
+ legacyArgs: args.slice(1),
718
+ cwd,
719
+ raw,
720
+ error,
721
+ output: core.output,
722
+ });
723
+ if (handled) break;
724
+ }
725
+ // CJS fallback (SDK unavailable or unknown subcommand)
726
+ if (subcommand === 'get') {
727
+ frontmatter.cmdFrontmatterGet(cwd, file, parseNamedArgs(args, ['field']).field, raw);
728
+ } else if (subcommand === 'set') {
729
+ const { field, value } = parseNamedArgs(args, ['field', 'value']);
730
+ frontmatter.cmdFrontmatterSet(cwd, file, field, value !== null ? value : undefined, raw);
731
+ } else if (subcommand === 'merge') {
732
+ frontmatter.cmdFrontmatterMerge(cwd, file, parseNamedArgs(args, ['data']).data, raw);
733
+ } else if (subcommand === 'validate') {
734
+ frontmatter.cmdFrontmatterValidate(cwd, file, parseNamedArgs(args, ['schema']).schema, raw);
735
+ } else {
736
+ error('Unknown frontmatter subcommand. Available: get, set, merge, validate', ERROR_REASON.SDK_UNKNOWN_COMMAND);
737
+ }
738
+ break;
739
+ }
740
+
741
+ case 'verify': {
742
+ routeVerifyCommand({
743
+ verify,
744
+ args,
745
+ cwd,
746
+ raw,
747
+ error,
748
+ });
749
+ break;
750
+ }
751
+
752
+ case 'generate-slug': {
753
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
754
+ // SDK handler: generateSlug in sdk/src/query/utils.ts.
755
+ const handled = _dispatchNonFamily({
756
+ registryCommand: 'generate-slug',
757
+ registryArgs: args.slice(1),
758
+ legacyCommand: 'generate-slug',
759
+ legacyArgs: args.slice(1),
760
+ cwd,
761
+ raw,
762
+ error,
763
+ output: core.output,
764
+ });
765
+ if (!handled) commands.cmdGenerateSlug(args[1], raw);
766
+ break;
767
+ }
768
+
769
+ case 'current-timestamp': {
770
+ // Keep this command on the CJS fast path.
771
+ // Rationale: it is a pure local formatter and avoids SDK bridge startup
772
+ // in tight subprocess loops where Windows CI has shown intermittent
773
+ // native crashes (0xC0000005 / 3221225477).
774
+ commands.cmdCurrentTimestamp(args[1] || 'full', raw);
775
+ break;
776
+ }
777
+
778
+ case 'list-todos': {
779
+ commands.cmdListTodos(cwd, args[1], raw);
780
+ break;
781
+ }
782
+
783
+ case 'verify-path-exists': {
784
+ commands.cmdVerifyPathExists(cwd, args[1], raw);
785
+ break;
786
+ }
787
+
788
+ case 'config-ensure-section': {
789
+ // Phase 6 (#3575): dispatch via SDK executeForCjs. The catalog rebinds
790
+ // 'config-ensure-section' to configNewProject in
791
+ // sdk/src/query/command-static-catalog-foundation.ts, restoring the
792
+ // legacy "no-arg full default init" contract on the SDK path
793
+ // (configEnsureSection itself stays available as an unbound single-
794
+ // section helper for future SDK callers).
795
+ const handled = _dispatchNonFamily({
796
+ registryCommand: 'config-ensure-section',
797
+ registryArgs: args.slice(1),
798
+ legacyCommand: 'config-ensure-section',
799
+ legacyArgs: args.slice(1),
800
+ cwd,
801
+ raw,
802
+ error,
803
+ output: core.output,
804
+ });
805
+ if (!handled) config.cmdConfigEnsureSection(cwd, raw);
806
+ break;
807
+ }
808
+
809
+ case 'config-set': {
810
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
811
+ const handled = _dispatchNonFamily({
812
+ registryCommand: 'config-set',
813
+ registryArgs: args.slice(1),
814
+ legacyCommand: 'config-set',
815
+ legacyArgs: args.slice(1),
816
+ cwd,
817
+ raw,
818
+ error,
819
+ output: core.output,
820
+ });
821
+ if (!handled) config.cmdConfigSet(cwd, args[1], args[2], raw);
822
+ break;
823
+ }
824
+
825
+ case "config-set-model-profile": {
826
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
827
+ const handled = _dispatchNonFamily({
828
+ registryCommand: 'config-set-model-profile',
829
+ registryArgs: args.slice(1),
830
+ legacyCommand: 'config-set-model-profile',
831
+ legacyArgs: args.slice(1),
832
+ cwd,
833
+ raw,
834
+ error,
835
+ output: core.output,
836
+ });
837
+ if (!handled) config.cmdConfigSetModelProfile(cwd, args[1], raw);
838
+ break;
839
+ }
840
+
841
+ case 'config-get': {
842
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
843
+ // The SDK handler supports --default via the registry args (args.slice(1)
844
+ // contains the key; defaultValue is handled by the SDK via the --default
845
+ // flag which was already stripped from args and held in defaultValue).
846
+ // Pass the full original args.slice(1) so the SDK sees the key; the
847
+ // defaultValue from the flag is in the global defaultValue variable above.
848
+ // Since the SDK handler reads --default from registryArgs, re-inject it.
849
+ const configGetSdkArgs = defaultValue !== undefined
850
+ ? [args[1], '--default', defaultValue]
851
+ : args.slice(1);
852
+ const handled = _dispatchNonFamily({
853
+ registryCommand: 'config-get',
854
+ registryArgs: configGetSdkArgs,
855
+ legacyCommand: 'config-get',
856
+ legacyArgs: args.slice(1),
857
+ cwd,
858
+ raw,
859
+ error,
860
+ output: core.output,
861
+ });
862
+ if (!handled) config.cmdConfigGet(cwd, args[1], raw, defaultValue);
863
+ break;
864
+ }
865
+
866
+ case 'config-new-project': {
867
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
868
+ const handled = _dispatchNonFamily({
869
+ registryCommand: 'config-new-project',
870
+ registryArgs: args.slice(1),
871
+ legacyCommand: 'config-new-project',
872
+ legacyArgs: args.slice(1),
873
+ cwd,
874
+ raw,
875
+ error,
876
+ output: core.output,
877
+ });
878
+ if (!handled) config.cmdConfigNewProject(cwd, args[1], raw);
879
+ break;
880
+ }
881
+
882
+ case 'config-path': {
883
+ // CJS-native: config-path returns the filesystem path to config.json.
884
+ // The SDK handler (configPath) also exists but requires a projectDir that
885
+ // is already resolved. Both produce identical output; keeping CJS here is
886
+ // simpler and avoids sync-bridge overhead for a trivial path lookup.
887
+ config.cmdConfigPath(cwd, raw, workstreamContext);
888
+ break;
889
+ }
890
+
891
+ case 'migrate-config': {
892
+ // CJS-native: migrate-config wraps the Configuration Module migrateOnDisk()
893
+ // which is async and mutates the filesystem. No SDK counterpart exists in
894
+ // the command registry (it's a one-shot migration utility). Must await.
895
+ await config.cmdMigrateConfig(cwd, raw);
896
+ break;
897
+ }
898
+
899
+ case 'agent-skills': {
900
+ // --json emits typed IR { agent_type, block, skills_count } for test assertions
901
+ // (#455). Default (no flag) outputs raw XML so workflow shell expansions work.
902
+ const jsonIdx = args.indexOf('--json');
903
+ const agentSkillsJsonMode = jsonIdx !== -1;
904
+ if (agentSkillsJsonMode) args.splice(jsonIdx, 1);
905
+ init.cmdAgentSkills(cwd, args[1], raw, agentSkillsJsonMode);
906
+ break;
907
+ }
908
+
909
+ case 'skill-manifest': {
910
+ init.cmdSkillManifest(cwd, args, raw);
911
+ break;
912
+ }
913
+
914
+ case 'history-digest': {
915
+ commands.cmdHistoryDigest(cwd, raw);
916
+ break;
917
+ }
918
+
919
+ case 'phases': {
920
+ routePhasesCommand({
921
+ phase,
922
+ milestone,
923
+ args,
924
+ cwd,
925
+ raw,
926
+ error,
927
+ });
928
+ break;
929
+ }
930
+
931
+ case 'roadmap': {
932
+ routeRoadmapCommand({
933
+ roadmap,
934
+ args,
935
+ cwd,
936
+ raw,
937
+ error,
938
+ });
939
+ break;
940
+ }
941
+
942
+ case 'requirements': {
943
+ const subcommand = args[1];
944
+ if (subcommand === 'mark-complete') {
945
+ milestone.cmdRequirementsMarkComplete(cwd, args.slice(2), raw);
946
+ } else {
947
+ error('Unknown requirements subcommand. Available: mark-complete', ERROR_REASON.SDK_UNKNOWN_COMMAND);
948
+ }
949
+ break;
950
+ }
951
+
952
+ case 'gap-analysis': {
953
+ // Post-planning gap checker (#2493) — unified REQUIREMENTS.md +
954
+ // CONTEXT.md <decisions> coverage report against PLAN.md files.
955
+ gapChecker.cmdGapAnalysis(cwd, args.slice(1), raw);
956
+ break;
957
+ }
958
+
959
+ case 'phase': {
960
+ routePhaseCommand({
961
+ phase,
962
+ args,
963
+ cwd,
964
+ raw,
965
+ error,
966
+ });
967
+ break;
968
+ }
969
+
970
+ case 'milestone': {
971
+ const subcommand = args[1];
972
+ if (subcommand === 'complete') {
973
+ const milestoneName = parseMultiwordArg(args, 'name');
974
+ const archivePhases = args.includes('--archive-phases');
975
+ milestone.cmdMilestoneComplete(cwd, args[2], { name: milestoneName, archivePhases }, raw);
976
+ } else {
977
+ error('Unknown milestone subcommand. Available: complete', ERROR_REASON.SDK_UNKNOWN_COMMAND);
978
+ }
979
+ break;
980
+ }
981
+
982
+ case 'validate': {
983
+ routeValidateCommand({
984
+ verify,
985
+ args,
986
+ cwd,
987
+ raw,
988
+ output: core.output,
989
+ error,
990
+ });
991
+ break;
992
+ }
993
+
994
+ case 'progress': {
995
+ const subcommand = args[1] || 'json';
996
+ commands.cmdProgressRender(cwd, subcommand, raw);
997
+ break;
998
+ }
999
+
1000
+ case 'audit-uat': {
1001
+ const uat = require('./lib/uat.cjs');
1002
+ uat.cmdAuditUat(cwd, raw);
1003
+ break;
1004
+ }
1005
+
1006
+ case 'audit-open': {
1007
+ const { auditOpenArtifacts, formatAuditReport } = require('./lib/audit.cjs');
1008
+ const wantJson = args.includes('--json');
1009
+ const result = auditOpenArtifacts(cwd);
1010
+ if (wantJson) {
1011
+ // core.output JSON-stringifies its first arg; pass the object directly.
1012
+ core.output(result, raw);
1013
+ } else {
1014
+ // Human-readable report must bypass JSON encoding — use the rawValue
1015
+ // form (third arg) which core.output emits verbatim.
1016
+ core.output(null, true, formatAuditReport(result));
1017
+ }
1018
+ break;
1019
+ }
1020
+
1021
+ case 'uat': {
1022
+ const subcommand = args[1];
1023
+ const uat = require('./lib/uat.cjs');
1024
+ if (subcommand === 'render-checkpoint') {
1025
+ const options = parseNamedArgs(args, ['file']);
1026
+ uat.cmdRenderCheckpoint(cwd, options, raw);
1027
+ } else {
1028
+ error('Unknown uat subcommand. Available: render-checkpoint', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1029
+ }
1030
+ break;
1031
+ }
1032
+
1033
+ case 'stats': {
1034
+ const subcommand = args[1] || 'json';
1035
+ commands.cmdStats(cwd, subcommand, raw);
1036
+ break;
1037
+ }
1038
+
1039
+ case 'todo': {
1040
+ const subcommand = args[1];
1041
+ if (subcommand === 'complete') {
1042
+ commands.cmdTodoComplete(cwd, args[2], raw);
1043
+ } else if (subcommand === 'match-phase') {
1044
+ commands.cmdTodoMatchPhase(cwd, args[2], raw);
1045
+ } else {
1046
+ error('Unknown todo subcommand. Available: complete, match-phase', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1047
+ }
1048
+ break;
1049
+ }
1050
+
1051
+ case 'scaffold': {
1052
+ const scaffoldType = args[1];
1053
+ const scaffoldOptions = {
1054
+ phase: parseNamedArgs(args, ['phase']).phase,
1055
+ name: parseMultiwordArg(args, 'name'),
1056
+ };
1057
+ commands.cmdScaffold(cwd, scaffoldType, scaffoldOptions, raw);
1058
+ break;
1059
+ }
1060
+
1061
+ case 'init': {
1062
+ routeInitCommand({
1063
+ init,
1064
+ args,
1065
+ cwd,
1066
+ raw,
1067
+ error,
1068
+ });
1069
+ break;
1070
+ }
1071
+
1072
+ case 'phase-plan-index': {
1073
+ phase.cmdPhasePlanIndex(cwd, args[1], raw);
1074
+ break;
1075
+ }
1076
+
1077
+ case 'state-snapshot': {
1078
+ state.cmdStateSnapshot(cwd, raw);
1079
+ break;
1080
+ }
1081
+
1082
+ case 'summary-extract': {
1083
+ const summaryPath = args[1];
1084
+ const fieldsIndex = args.indexOf('--fields');
1085
+ const fields = fieldsIndex !== -1 ? args[fieldsIndex + 1].split(',') : null;
1086
+ commands.cmdSummaryExtract(cwd, summaryPath, fields, raw);
1087
+ break;
1088
+ }
1089
+
1090
+ case 'websearch': {
1091
+ const query = args[1];
1092
+ const limitIdx = args.indexOf('--limit');
1093
+ const freshnessIdx = args.indexOf('--freshness');
1094
+ await commands.cmdWebsearch(query, {
1095
+ limit: limitIdx !== -1 ? parseInt(args[limitIdx + 1], 10) : 10,
1096
+ freshness: freshnessIdx !== -1 ? args[freshnessIdx + 1] : null,
1097
+ }, raw);
1098
+ break;
1099
+ }
1100
+
1101
+ // ─── Profiling Pipeline ────────────────────────────────────────────────
1102
+
1103
+ case 'scan-sessions': {
1104
+ const pathIdx = args.indexOf('--path');
1105
+ const sessionsPath = pathIdx !== -1 ? args[pathIdx + 1] : null;
1106
+ const verboseFlag = args.includes('--verbose');
1107
+ const jsonFlag = args.includes('--json');
1108
+ await profilePipeline.cmdScanSessions(sessionsPath, { verbose: verboseFlag, json: jsonFlag }, raw);
1109
+ break;
1110
+ }
1111
+
1112
+ case 'extract-messages': {
1113
+ const sessionIdx = args.indexOf('--session');
1114
+ const sessionId = sessionIdx !== -1 ? args[sessionIdx + 1] : null;
1115
+ const limitIdx = args.indexOf('--limit');
1116
+ const limit = limitIdx !== -1 ? parseInt(args[limitIdx + 1], 10) : null;
1117
+ const pathIdx = args.indexOf('--path');
1118
+ const sessionsPath = pathIdx !== -1 ? args[pathIdx + 1] : null;
1119
+ const projectArg = args[1];
1120
+ if (!projectArg || projectArg.startsWith('--')) {
1121
+ error('Usage: gsd-tools extract-messages <project> [--session <id>] [--limit N] [--path <dir>]\nRun scan-sessions first to see available projects.', ERROR_REASON.USAGE);
1122
+ }
1123
+ await profilePipeline.cmdExtractMessages(projectArg, { sessionId, limit }, raw, sessionsPath);
1124
+ break;
1125
+ }
1126
+
1127
+ case 'profile-sample': {
1128
+ const pathIdx = args.indexOf('--path');
1129
+ const sessionsPath = pathIdx !== -1 ? args[pathIdx + 1] : null;
1130
+ const limitIdx = args.indexOf('--limit');
1131
+ const limit = limitIdx !== -1 ? parseInt(args[limitIdx + 1], 10) : 150;
1132
+ const maxPerIdx = args.indexOf('--max-per-project');
1133
+ const maxPerProject = maxPerIdx !== -1 ? parseInt(args[maxPerIdx + 1], 10) : null;
1134
+ const maxCharsIdx = args.indexOf('--max-chars');
1135
+ const maxChars = maxCharsIdx !== -1 ? parseInt(args[maxCharsIdx + 1], 10) : 500;
1136
+ await profilePipeline.cmdProfileSample(sessionsPath, { limit, maxPerProject, maxChars }, raw);
1137
+ break;
1138
+ }
1139
+
1140
+ // ─── Profile Output ──────────────────────────────────────────────────
1141
+
1142
+ case 'write-profile': {
1143
+ const inputIdx = args.indexOf('--input');
1144
+ const inputPath = inputIdx !== -1 ? args[inputIdx + 1] : null;
1145
+ if (!inputPath) error('--input <analysis-json-path> is required', ERROR_REASON.USAGE);
1146
+ const outputIdx = args.indexOf('--output');
1147
+ const outputPath = outputIdx !== -1 ? args[outputIdx + 1] : null;
1148
+ profileOutput.cmdWriteProfile(cwd, { input: inputPath, output: outputPath }, raw);
1149
+ break;
1150
+ }
1151
+
1152
+ case 'profile-questionnaire': {
1153
+ const answersIdx = args.indexOf('--answers');
1154
+ const answers = answersIdx !== -1 ? args[answersIdx + 1] : null;
1155
+ profileOutput.cmdProfileQuestionnaire({ answers }, raw);
1156
+ break;
1157
+ }
1158
+
1159
+ case 'generate-dev-preferences': {
1160
+ const analysisIdx = args.indexOf('--analysis');
1161
+ const analysisPath = analysisIdx !== -1 ? args[analysisIdx + 1] : null;
1162
+ const outputIdx = args.indexOf('--output');
1163
+ const outputPath = outputIdx !== -1 ? args[outputIdx + 1] : null;
1164
+ const stackIdx = args.indexOf('--stack');
1165
+ const stack = stackIdx !== -1 ? args[stackIdx + 1] : null;
1166
+ profileOutput.cmdGenerateDevPreferences(cwd, { analysis: analysisPath, output: outputPath, stack }, raw);
1167
+ break;
1168
+ }
1169
+
1170
+ case 'generate-claude-profile': {
1171
+ const analysisIdx = args.indexOf('--analysis');
1172
+ const analysisPath = analysisIdx !== -1 ? args[analysisIdx + 1] : null;
1173
+ const outputIdx = args.indexOf('--output');
1174
+ const outputPath = outputIdx !== -1 ? args[outputIdx + 1] : null;
1175
+ const globalFlag = args.includes('--global');
1176
+ profileOutput.cmdGenerateClaudeProfile(cwd, { analysis: analysisPath, output: outputPath, global: globalFlag }, raw);
1177
+ break;
1178
+ }
1179
+
1180
+ case 'generate-claude-md': {
1181
+ const outputIdx = args.indexOf('--output');
1182
+ const outputPath = outputIdx !== -1 ? args[outputIdx + 1] : null;
1183
+ const autoFlag = args.includes('--auto');
1184
+ const forceFlag = args.includes('--force');
1185
+ profileOutput.cmdGenerateClaudeMd(cwd, { output: outputPath, auto: autoFlag, force: forceFlag }, raw);
1186
+ break;
1187
+ }
1188
+
1189
+ case 'workstream': {
1190
+ const subcommand = args[1];
1191
+ if (subcommand === 'create') {
1192
+ const migrateNameIdx = args.indexOf('--migrate-name');
1193
+ const noMigrate = args.includes('--no-migrate');
1194
+ workstream.cmdWorkstreamCreate(cwd, args[2], {
1195
+ migrate: !noMigrate,
1196
+ migrateName: migrateNameIdx !== -1 ? args[migrateNameIdx + 1] : null,
1197
+ }, raw);
1198
+ } else if (subcommand === 'list') {
1199
+ workstream.cmdWorkstreamList(cwd, raw);
1200
+ } else if (subcommand === 'status') {
1201
+ workstream.cmdWorkstreamStatus(cwd, args[2], raw);
1202
+ } else if (subcommand === 'complete') {
1203
+ workstream.cmdWorkstreamComplete(cwd, args[2], {}, raw);
1204
+ } else if (subcommand === 'set') {
1205
+ workstream.cmdWorkstreamSet(cwd, args[2], raw);
1206
+ } else if (subcommand === 'get') {
1207
+ workstream.cmdWorkstreamGet(cwd, raw);
1208
+ } else if (subcommand === 'progress') {
1209
+ workstream.cmdWorkstreamProgress(cwd, raw);
1210
+ } else {
1211
+ error('Unknown workstream subcommand. Available: create, list, status, complete, set, get, progress', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1212
+ }
1213
+ break;
1214
+ }
1215
+
1216
+ case 'worktree': {
1217
+ const subcommand = args[1];
1218
+ const worktreeSafety = require('./lib/worktree-safety.cjs');
1219
+ if (subcommand === 'cleanup-wave') {
1220
+ worktreeSafety.cmdWorktreeCleanupWave(cwd, args.slice(2));
1221
+ } else if (subcommand === 'reap-orphans') {
1222
+ worktreeSafety.cmdWorktreeReapOrphans(cwd);
1223
+ } else {
1224
+ error('Unknown worktree subcommand. Available: cleanup-wave, reap-orphans', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1225
+ }
1226
+ break;
1227
+ }
1228
+
1229
+ // ─── Intel ────────────────────────────────────────────────────────────
1230
+
1231
+ case 'intel': {
1232
+ const intel = require('./lib/intel.cjs');
1233
+ const subcommand = args[1];
1234
+ if (subcommand === 'query') {
1235
+ const term = args[2];
1236
+ if (!term) error('Usage: gsd-tools intel query <term>', ERROR_REASON.USAGE);
1237
+ const planningDir = path.join(cwd, '.planning');
1238
+ core.output(intel.intelQuery(term, planningDir), raw);
1239
+ } else if (subcommand === 'status') {
1240
+ const planningDir = path.join(cwd, '.planning');
1241
+ const status = intel.intelStatus(planningDir);
1242
+ if (!raw && status.files) {
1243
+ for (const file of Object.values(status.files)) {
1244
+ if (file.updated_at) {
1245
+ file.updated_at = core.timeAgo(new Date(file.updated_at));
1246
+ }
1247
+ }
1248
+ }
1249
+ core.output(status, raw);
1250
+ } else if (subcommand === 'diff') {
1251
+ const planningDir = path.join(cwd, '.planning');
1252
+ core.output(intel.intelDiff(planningDir), raw);
1253
+ } else if (subcommand === 'snapshot') {
1254
+ const planningDir = path.join(cwd, '.planning');
1255
+ core.output(intel.intelSnapshot(planningDir), raw);
1256
+ } else if (subcommand === 'patch-meta') {
1257
+ const filePath = args[2];
1258
+ if (!filePath) error('Usage: gsd-tools intel patch-meta <file-path>', ERROR_REASON.USAGE);
1259
+ core.output(intel.intelPatchMeta(path.resolve(cwd, filePath)), raw);
1260
+ } else if (subcommand === 'validate') {
1261
+ const planningDir = path.join(cwd, '.planning');
1262
+ core.output(intel.intelValidate(planningDir), raw);
1263
+ } else if (subcommand === 'extract-exports') {
1264
+ const filePath = args[2];
1265
+ if (!filePath) error('Usage: gsd-tools intel extract-exports <file-path>', ERROR_REASON.USAGE);
1266
+ core.output(intel.intelExtractExports(path.resolve(cwd, filePath)), raw);
1267
+ } else if (subcommand === 'update') {
1268
+ const planningDir = path.join(cwd, '.planning');
1269
+ core.output(intel.intelUpdate(planningDir), raw);
1270
+ } else if (subcommand === 'api-surface') {
1271
+ const planningDir = path.join(cwd, '.planning');
1272
+ core.output(intel.intelApiSurface(planningDir), raw);
1273
+ } else {
1274
+ error('Unknown intel subcommand. Available: query, status, update, diff, snapshot, patch-meta, validate, extract-exports, api-surface', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1275
+ }
1276
+ break;
1277
+ }
1278
+
1279
+ // ─── Graphify ──────────────────────────────────────────────────────────
1280
+
1281
+ case 'graphify': {
1282
+ const graphify = require('./lib/graphify.cjs');
1283
+ const subcommand = args[1];
1284
+ if (subcommand === 'query') {
1285
+ const term = args[2];
1286
+ if (!term) error('Usage: gsd-tools graphify query <term>', ERROR_REASON.USAGE);
1287
+ const budgetIdx = args.indexOf('--budget');
1288
+ const budget = budgetIdx !== -1 ? parseInt(args[budgetIdx + 1], 10) : null;
1289
+ core.output(graphify.graphifyQuery(cwd, term, { budget }), raw);
1290
+ } else if (subcommand === 'status') {
1291
+ core.output(graphify.graphifyStatus(cwd), raw);
1292
+ } else if (subcommand === 'diff') {
1293
+ core.output(graphify.graphifyDiff(cwd), raw);
1294
+ } else if (subcommand === 'build') {
1295
+ if (args[2] === 'snapshot') {
1296
+ core.output(graphify.writeSnapshot(cwd), raw);
1297
+ } else {
1298
+ core.output(graphify.graphifyBuild(cwd), raw);
1299
+ }
1300
+ } else {
1301
+ error('Unknown graphify subcommand. Available: build, query, status, diff', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1302
+ }
1303
+ break;
1304
+ }
1305
+
1306
+ // ─── Documentation ────────────────────────────────────────────────────
1307
+
1308
+ case 'docs-init': {
1309
+ // Phase 6 (#3575): dispatch via SDK executeForCjs when available.
1310
+ // SDK handler: docsInit in sdk/src/query/docs-init.ts.
1311
+ const handled = _dispatchNonFamily({
1312
+ registryCommand: 'docs-init',
1313
+ registryArgs: args.slice(1),
1314
+ legacyCommand: 'docs-init',
1315
+ legacyArgs: args.slice(1),
1316
+ cwd,
1317
+ raw,
1318
+ error,
1319
+ output: core.output,
1320
+ });
1321
+ if (!handled) docs.cmdDocsInit(cwd, raw);
1322
+ break;
1323
+ }
1324
+
1325
+ // ─── Learnings ─────────────────────────────────────────────────────────
1326
+
1327
+ case 'learnings': {
1328
+ const subcommand = args[1];
1329
+ if (subcommand === 'list') {
1330
+ learnings.cmdLearningsList(raw);
1331
+ } else if (subcommand === 'query') {
1332
+ const tagIdx = args.indexOf('--tag');
1333
+ const tag = tagIdx !== -1 ? args[tagIdx + 1] : null;
1334
+ if (!tag) error('Usage: gsd-tools learnings query --tag <tag>', ERROR_REASON.USAGE);
1335
+ learnings.cmdLearningsQuery(tag, raw);
1336
+ } else if (subcommand === 'copy') {
1337
+ learnings.cmdLearningsCopy(cwd, raw);
1338
+ } else if (subcommand === 'prune') {
1339
+ const olderIdx = args.indexOf('--older-than');
1340
+ const olderThan = olderIdx !== -1 ? args[olderIdx + 1] : null;
1341
+ if (!olderThan) error('Usage: gsd-tools learnings prune --older-than <duration>', ERROR_REASON.USAGE);
1342
+ learnings.cmdLearningsPrune(olderThan, raw);
1343
+ } else if (subcommand === 'delete') {
1344
+ const id = args[2];
1345
+ if (!id) error('Usage: gsd-tools learnings delete <id>', ERROR_REASON.USAGE);
1346
+ learnings.cmdLearningsDelete(id, raw);
1347
+ } else {
1348
+ error('Unknown learnings subcommand. Available: list, query, copy, prune, delete', ERROR_REASON.SDK_UNKNOWN_COMMAND);
1349
+ }
1350
+ break;
1351
+ }
1352
+
1353
+ // ─── detect-custom-files ───────────────────────────────────────────────
1354
+ // CJS-native: no SDK counterpart exists in the command registry.
1355
+ // detect-custom-files reads a gsd-file-manifest.json against the
1356
+ // live filesystem to identify user-added files. It is installer-specific
1357
+ // logic that has no async query equivalent in the SDK.
1358
+ //
1359
+ // Detect user-added files inside GSD-managed directories that are not
1360
+ // tracked in gsd-file-manifest.json. Used by the update workflow to back
1361
+ // up custom files before the installer wipes those directories.
1362
+ //
1363
+ // This replaces the fragile bash pattern:
1364
+ // MANIFEST_FILES=$(node -e "require('$RUNTIME_DIR/...')" 2>/dev/null)
1365
+ // ${filepath#$RUNTIME_DIR/} # unreliable path stripping
1366
+ // which silently returns CUSTOM_COUNT=0 when $RUNTIME_DIR is unset or
1367
+ // when the stripped path does not match the manifest key format (#1997).
1368
+
1369
+ case 'detect-custom-files': {
1370
+ const configDirIdx = args.indexOf('--config-dir');
1371
+ const configDir = configDirIdx !== -1 ? args[configDirIdx + 1] : null;
1372
+ if (!configDir) {
1373
+ error('Usage: gsd-tools detect-custom-files --config-dir <path>', ERROR_REASON.USAGE);
1374
+ }
1375
+ const resolvedConfigDir = path.resolve(configDir);
1376
+ if (!fs.existsSync(resolvedConfigDir)) {
1377
+ error(`Config directory not found: ${resolvedConfigDir}`, ERROR_REASON.USAGE);
1378
+ }
1379
+
1380
+ const manifestPath = path.join(resolvedConfigDir, 'gsd-file-manifest.json');
1381
+ if (!fs.existsSync(manifestPath)) {
1382
+ // No manifest — cannot determine what is custom. Return empty list
1383
+ // (same behaviour as saveLocalPatches in install.js when no manifest).
1384
+ const out = { custom_files: [], custom_count: 0, manifest_found: false };
1385
+ process.stdout.write(JSON.stringify(out, null, 2));
1386
+ break;
1387
+ }
1388
+
1389
+ let manifest;
1390
+ try {
1391
+ manifest = JSON.parse(await fs.promises.readFile(manifestPath, 'utf8'));
1392
+ } catch {
1393
+ const out = { custom_files: [], custom_count: 0, manifest_found: false, error: 'manifest parse error' };
1394
+ process.stdout.write(JSON.stringify(out, null, 2));
1395
+ break;
1396
+ }
1397
+
1398
+ const manifestKeys = new Set(Object.keys(manifest.files || {}));
1399
+
1400
+ // GSD-managed directories to scan for user-added files.
1401
+ // These are the directories the installer wipes on update.
1402
+ const GSD_MANAGED_DIRS = [
1403
+ 'get-shit-done',
1404
+ 'agents',
1405
+ path.join('commands', 'gsd'),
1406
+ 'hooks',
1407
+ 'skills',
1408
+ ];
1409
+
1410
+ function collectCustomFiles(dir, baseDir, manifestKeys, out) {
1411
+ if (!fs.existsSync(dir)) return;
1412
+ for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
1413
+ const fullPath = path.join(dir, entry.name);
1414
+ if (entry.isDirectory()) {
1415
+ collectCustomFiles(fullPath, baseDir, manifestKeys, out);
1416
+ continue;
1417
+ }
1418
+ // Use forward slashes for cross-platform manifest key compatibility
1419
+ const relPath = path.relative(baseDir, fullPath).replace(/\\/g, '/');
1420
+ if (!manifestKeys.has(relPath)) {
1421
+ out.push(relPath);
1422
+ }
1423
+ }
1424
+ }
1425
+
1426
+ const customFiles = [];
1427
+ for (const managedDir of GSD_MANAGED_DIRS) {
1428
+ const absDir = path.join(resolvedConfigDir, managedDir);
1429
+ if (!fs.existsSync(absDir)) continue;
1430
+ collectCustomFiles(absDir, resolvedConfigDir, manifestKeys, customFiles);
1431
+ }
1432
+
1433
+ const out = {
1434
+ custom_files: customFiles,
1435
+ custom_count: customFiles.length,
1436
+ manifest_found: true,
1437
+ manifest_version: manifest.version || null,
1438
+ };
1439
+ process.stdout.write(JSON.stringify(out, null, 2));
1440
+ break;
1441
+ }
1442
+
1443
+ // ─── GSD-2 Reverse Migration ───────────────────────────────────────────
1444
+
1445
+ case 'from-gsd2': {
1446
+ const gsd2Import = require('./lib/gsd2-import.cjs');
1447
+ gsd2Import.cmdFromGsd2(args.slice(1), cwd, raw);
1448
+ break;
1449
+ }
1450
+
1451
+ // ─── Prompt Budget ────────────────────────────────────────────────────
1452
+ //
1453
+ // Assemble and deterministically trim review prompt sections to fit a
1454
+ // token budget. Used by the /gsd-review workflow before dispatching to
1455
+ // small-context local model servers (Ollama, llama.cpp, LM Studio).
1456
+ //
1457
+ // Required flags:
1458
+ // --budget <N> Token budget (integer > 0)
1459
+ // --instructions-file <path> Review instructions
1460
+ // --roadmap-file <path> Roadmap section
1461
+ // --plan-file <path> Plan file (may be repeated)
1462
+ // --output-prompt <path> Write trimmed prompt here
1463
+ // --output-metadata <path> Write metadata JSON here
1464
+ //
1465
+ // Optional flags:
1466
+ // --safety-margin-pct <N> Default 10
1467
+ // --project-md-head-lines <N> Default 40
1468
+ // --project-file <path>
1469
+ // --context-file <path>
1470
+ // --research-file <path>
1471
+ // --requirements-file <path>
1472
+ //
1473
+ // Exit codes:
1474
+ // 0 success (trim or no-trim)
1475
+ // 1 invocation error (missing required arg, missing file, invalid budget)
1476
+ // 2 hardFailed: prompt cannot fit effective budget after trim policy
1477
+
1478
+ case 'prompt-budget': {
1479
+ const promptBudget = require('./lib/prompt-budget.cjs');
1480
+
1481
+ // ── Collect multi-value --plan-file flags ──────────────────────────
1482
+ const planFiles = [];
1483
+ for (let i = 1; i < args.length; i++) {
1484
+ if (args[i] === '--plan-file' && args[i + 1] && !args[i + 1].startsWith('--')) {
1485
+ planFiles.push(args[i + 1]);
1486
+ i++;
1487
+ }
1488
+ }
1489
+
1490
+ // ── Parse single-value flags ───────────────────────────────────────
1491
+ const flagMap = new Map();
1492
+ for (let i = 1; i < args.length; i++) {
1493
+ const current = args[i];
1494
+ const next = args[i + 1];
1495
+ if (!current.startsWith('--')) continue;
1496
+ if (!next || next.startsWith('--')) {
1497
+ if (!flagMap.has(current)) flagMap.set(current, null);
1498
+ continue;
1499
+ }
1500
+ if (!flagMap.has(current)) flagMap.set(current, next);
1501
+ i++;
1502
+ }
1503
+ const getFlag = (flag) => flagMap.get(flag) ?? null;
1504
+
1505
+ const budgetStr = getFlag('--budget');
1506
+ const instructionsFile = getFlag('--instructions-file');
1507
+ const roadmapFile = getFlag('--roadmap-file');
1508
+ const outputPromptFile = getFlag('--output-prompt');
1509
+ const outputMetadataFile = getFlag('--output-metadata');
1510
+ const safetyMarginStr = getFlag('--safety-margin-pct');
1511
+ const projectMdHeadLinesStr = getFlag('--project-md-head-lines');
1512
+ const projectFile = getFlag('--project-file');
1513
+ const contextFile = getFlag('--context-file');
1514
+ const researchFile = getFlag('--research-file');
1515
+ const requirementsFile = getFlag('--requirements-file');
1516
+
1517
+ // ── Validate required args ─────────────────────────────────────────
1518
+ if (!budgetStr) {
1519
+ process.stderr.write('Error: --budget <N> is required\n');
1520
+ process.exit(1);
1521
+ }
1522
+ const budget = parseInt(budgetStr, 10);
1523
+ if (!Number.isFinite(budget) || budget <= 0) {
1524
+ process.stderr.write('Error: --budget must be a positive integer\n');
1525
+ process.exit(1);
1526
+ }
1527
+ if (!instructionsFile) {
1528
+ process.stderr.write('Error: --instructions-file <path> is required\n');
1529
+ process.exit(1);
1530
+ }
1531
+ if (!roadmapFile) {
1532
+ process.stderr.write('Error: --roadmap-file <path> is required\n');
1533
+ process.exit(1);
1534
+ }
1535
+ if (planFiles.length === 0) {
1536
+ process.stderr.write('Error: at least one --plan-file <path> is required\n');
1537
+ process.exit(1);
1538
+ }
1539
+ if (!outputPromptFile) {
1540
+ process.stderr.write('Error: --output-prompt <path> is required\n');
1541
+ process.exit(1);
1542
+ }
1543
+ if (!outputMetadataFile) {
1544
+ process.stderr.write('Error: --output-metadata <path> is required\n');
1545
+ process.exit(1);
1546
+ }
1547
+
1548
+ // ── Validate and read required files ──────────────────────────────
1549
+ async function readRequired(filePath, flagName) {
1550
+ const resolved = path.resolve(filePath);
1551
+ try {
1552
+ return await fs.promises.readFile(resolved, 'utf8');
1553
+ } catch (err) {
1554
+ if (err && err.code === 'ENOENT') {
1555
+ process.stderr.write(`Error: file not found for ${flagName}: ${resolved}\n`);
1556
+ process.exit(1);
1557
+ }
1558
+ process.stderr.write(`Error: cannot read file for ${flagName}: ${resolved}\n`);
1559
+ process.exit(1);
1560
+ }
1561
+ }
1562
+
1563
+ async function readOptional(filePath) {
1564
+ if (!filePath) return null;
1565
+ const resolved = path.resolve(filePath);
1566
+ try {
1567
+ return await fs.promises.readFile(resolved, 'utf8');
1568
+ } catch (err) {
1569
+ if (err && err.code === 'ENOENT') return null;
1570
+ process.stderr.write(`Error: cannot read optional file: ${resolved}\n`);
1571
+ process.exit(1);
1572
+ }
1573
+ }
1574
+
1575
+ const instructions = await readRequired(instructionsFile, '--instructions-file');
1576
+ const roadmap = await readRequired(roadmapFile, '--roadmap-file');
1577
+ const plans = await Promise.all(planFiles.map(async (p) => {
1578
+ const resolved = path.resolve(p);
1579
+ try {
1580
+ const content = await fs.promises.readFile(resolved, 'utf8');
1581
+ return { file: path.basename(p), content };
1582
+ } catch (err) {
1583
+ if (err && err.code === 'ENOENT') {
1584
+ process.stderr.write(`Error: plan file not found: ${resolved}\n`);
1585
+ process.exit(1);
1586
+ }
1587
+ process.stderr.write(`Error: cannot read plan file: ${resolved}\n`);
1588
+ process.exit(1);
1589
+ }
1590
+ }));
1591
+
1592
+ const projectMd = await readOptional(projectFile);
1593
+ const context = await readOptional(contextFile);
1594
+ const research = await readOptional(researchFile);
1595
+ const requirements = await readOptional(requirementsFile);
1596
+
1597
+ // ── Build options ─────────────────────────────────────────────────
1598
+ const options = {};
1599
+ if (safetyMarginStr !== null) {
1600
+ const pct = parseInt(safetyMarginStr, 10);
1601
+ if (Number.isFinite(pct)) options.safetyMarginPct = pct;
1602
+ }
1603
+ if (projectMdHeadLinesStr !== null) {
1604
+ const lines = parseInt(projectMdHeadLinesStr, 10);
1605
+ if (Number.isFinite(lines)) options.projectMdHeadLines = lines;
1606
+ }
1607
+
1608
+ // ── Call applyBudget ──────────────────────────────────────────────
1609
+ const sections = { instructions, roadmap, plans, projectMd, context, research, requirements };
1610
+ const { prompt, metadata } = promptBudget.applyBudget({ sections, budget, options });
1611
+
1612
+ // ── Write outputs ─────────────────────────────────────────────────
1613
+ await fs.promises.writeFile(path.resolve(outputMetadataFile), JSON.stringify(metadata, null, 2));
1614
+ await fs.promises.writeFile(path.resolve(outputPromptFile), prompt);
1615
+
1616
+ if (metadata.hardFailed) {
1617
+ process.exit(2);
1618
+ }
1619
+ break;
1620
+ }
1621
+
1622
+ case 'update-context': {
1623
+ // #498: resolve the installed GSD version, scope, runtime, and config dir
1624
+ // for /gsd:update. Replaces ~280 lines of inline bash in update.md with a
1625
+ // tested projection. Emits the contract as JSON: { installedVersion,
1626
+ // scope, runtime, gsdDir }. Optional --config-dir / --runtime carry the
1627
+ // workflow's execution_context hints (the one thing only it can know).
1628
+ const { loadUpdateContext } = require('./lib/update-context.cjs');
1629
+ const ucArgs = args.slice(1);
1630
+ let preferredConfigDir = '';
1631
+ let preferredRuntime = '';
1632
+ for (let i = 0; i < ucArgs.length; i++) {
1633
+ const a = ucArgs[i];
1634
+ if (a.startsWith('--config-dir=')) { preferredConfigDir = a.slice('--config-dir='.length); continue; }
1635
+ if (a.startsWith('--runtime=')) { preferredRuntime = a.slice('--runtime='.length); continue; }
1636
+ if (a === '--config-dir') {
1637
+ const v = ucArgs[i + 1];
1638
+ if (v === undefined || v.startsWith('--')) error('Missing value for --config-dir', ERROR_REASON.USAGE);
1639
+ preferredConfigDir = v; i++; continue;
1640
+ }
1641
+ if (a === '--runtime') {
1642
+ const v = ucArgs[i + 1];
1643
+ if (v === undefined || v.startsWith('--')) error('Missing value for --runtime', ERROR_REASON.USAGE);
1644
+ preferredRuntime = v; i++; continue;
1645
+ }
1646
+ if (a === '--json') continue; // JSON is the only output; accepted for symmetry
1647
+ if (a.startsWith('-')) error(`Unknown flag for update-context: ${a}`, ERROR_REASON.USAGE);
1648
+ }
1649
+ const ctx = loadUpdateContext({ preferredConfigDir, preferredRuntime });
1650
+ process.stdout.write(JSON.stringify(ctx) + '\n');
1651
+ break;
1652
+ }
1653
+
1654
+ default: {
1655
+ // #3243: if the caller passed a dotted form (e.g. "foo.bar"), the shim
1656
+ // above split it so `command` here is the head ("foo"). Use
1657
+ // originalCommand to reconstruct the original dotted form and suggest
1658
+ // the spaced equivalent — surfacing a useful diagnostic instead of just
1659
+ // "Unknown command: foo".
1660
+ const wasDotted =
1661
+ typeof originalCommand === 'string' &&
1662
+ originalCommand !== command &&
1663
+ originalCommand.includes('.');
1664
+ let suggestion = '';
1665
+ if (wasDotted) {
1666
+ const dotIdx = originalCommand.indexOf('.');
1667
+ const head = originalCommand.slice(0, dotIdx);
1668
+ const rest = originalCommand.slice(dotIdx + 1);
1669
+ suggestion = ` — did you mean: "${head} ${rest}"?`;
1670
+ }
1671
+ error(`Unknown command: ${command}${suggestion}`, ERROR_REASON.SDK_UNKNOWN_COMMAND);
1672
+ }
1673
+ }
1674
+ }
1675
+
1676
+ main();