@therocketcode/gsd-core 1.4.0

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