@opengsd/gsd-core 1.9.0 → 1.10.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 (223) hide show
  1. package/.claude-plugin/marketplace.json +1 -1
  2. package/.claude-plugin/plugin.json +2 -3
  3. package/.opencode/plugins/gsd-core.js +8 -1
  4. package/agents/gsd-code-fixer.md +131 -34
  5. package/agents/gsd-debugger.md +12 -246
  6. package/agents/gsd-executor.md +7 -5
  7. package/agents/gsd-integration-checker.md +3 -0
  8. package/agents/gsd-plan-checker.md +9 -0
  9. package/agents/gsd-planner.md +5 -8
  10. package/agents/gsd-roadmapper.md +21 -3
  11. package/agents/gsd-verifier.md +14 -70
  12. package/bin/install.js +503 -341
  13. package/commands/gsd/mempalace-capture.md +1 -1
  14. package/commands/gsd/new-milestone.md +1 -1
  15. package/commands/gsd/plan-phase.md +1 -1
  16. package/gsd-core/bin/gsd-tools.cjs +607 -63
  17. package/gsd-core/bin/lib/active-workstream-store.cjs +25 -0
  18. package/gsd-core/bin/lib/agent-install-check.cjs +38 -6
  19. package/gsd-core/bin/lib/api-coverage.cjs +120 -0
  20. package/gsd-core/bin/lib/audit.cjs +89 -1
  21. package/gsd-core/bin/lib/broken-windows.cjs +36 -6
  22. package/gsd-core/bin/lib/capability-registry.cjs +96 -110
  23. package/gsd-core/bin/lib/capability-validator.cjs +12 -2
  24. package/gsd-core/bin/lib/check-command-router.cjs +43 -1
  25. package/gsd-core/bin/lib/command-aliases.cjs +72 -0
  26. package/gsd-core/bin/lib/commands.cjs +26 -25
  27. package/gsd-core/bin/lib/commonjs-marker.cjs +136 -0
  28. package/gsd-core/bin/lib/config-loader.cjs +1 -0
  29. package/gsd-core/bin/lib/config.cjs +12 -1
  30. package/gsd-core/bin/lib/context-composer.cjs +278 -0
  31. package/gsd-core/bin/lib/context-predicates.cjs +506 -0
  32. package/gsd-core/bin/lib/core-utils.cjs +91 -12
  33. package/gsd-core/bin/lib/docs.cjs +3 -2
  34. package/gsd-core/bin/lib/external-job.cjs +19 -4
  35. package/gsd-core/bin/lib/frontmatter.cjs +84 -12
  36. package/gsd-core/bin/lib/gate-predicate-evaluator.cjs +57 -6
  37. package/gsd-core/bin/lib/git-base-branch.cjs +58 -15
  38. package/gsd-core/bin/lib/graphify.cjs +142 -27
  39. package/gsd-core/bin/lib/gsd2-import.cjs +27 -4
  40. package/gsd-core/bin/lib/host-integration.cjs +13 -1
  41. package/gsd-core/bin/lib/init-command-router.cjs +83 -8
  42. package/gsd-core/bin/lib/init.cjs +1021 -57
  43. package/gsd-core/bin/lib/install-engine.cjs +64 -10
  44. package/gsd-core/bin/lib/install-profiles.cjs +27 -1
  45. package/gsd-core/bin/lib/installer-migration-authoring.cjs +3 -1
  46. package/gsd-core/bin/lib/installer-migration-report.cjs +4 -0
  47. package/gsd-core/bin/lib/installer-migrations/007-retire-config-root-commonjs-marker.cjs +149 -0
  48. package/gsd-core/bin/lib/installer-migrations/008-cursor-retire-commands-surface.cjs +55 -0
  49. package/gsd-core/bin/lib/installer-migrations/009-pi-retire-reserved-hooks-dir.cjs +199 -0
  50. package/gsd-core/bin/lib/installer-migrations.cjs +87 -1
  51. package/gsd-core/bin/lib/io.cjs +28 -3
  52. package/gsd-core/bin/lib/markdown-sectionizer.cjs +6 -0
  53. package/gsd-core/bin/lib/mcp-catalog.cjs +518 -0
  54. package/gsd-core/bin/lib/mcp-server.cjs +135 -3
  55. package/gsd-core/bin/lib/milestone.cjs +106 -51
  56. package/gsd-core/bin/lib/phase-id.cjs +63 -0
  57. package/gsd-core/bin/lib/phase-locator.cjs +138 -45
  58. package/gsd-core/bin/lib/phase.cjs +260 -25
  59. package/gsd-core/bin/lib/plan-dependency-graph.cjs +232 -0
  60. package/gsd-core/bin/lib/planning-workspace.cjs +4 -0
  61. package/gsd-core/bin/lib/project-root.cjs +48 -0
  62. package/gsd-core/bin/lib/prompt-budget.cjs +128 -165
  63. package/gsd-core/bin/lib/retired-artifact-cleanup.cjs +80 -0
  64. package/gsd-core/bin/lib/review-lane-descriptor.cjs +99 -0
  65. package/gsd-core/bin/lib/review-lane-runner.cjs +30 -6
  66. package/gsd-core/bin/lib/roadmap-command-router.cjs +42 -9
  67. package/gsd-core/bin/lib/roadmap-parser.cjs +100 -18
  68. package/gsd-core/bin/lib/roadmap.cjs +37 -7
  69. package/gsd-core/bin/lib/runtime-artifact-conversion.cjs +195 -62
  70. package/gsd-core/bin/lib/runtime-artifact-layout.cjs +15 -3
  71. package/gsd-core/bin/lib/runtime-homes.cjs +154 -41
  72. package/gsd-core/bin/lib/runtime-hooks-surface.cjs +105 -41
  73. package/gsd-core/bin/lib/section-manifest.cjs +209 -0
  74. package/gsd-core/bin/lib/shell-command-projection.cjs +113 -27
  75. package/gsd-core/bin/lib/smart-entry.cjs +12 -0
  76. package/gsd-core/bin/lib/state-transition.cjs +73 -8
  77. package/gsd-core/bin/lib/state.cjs +151 -62
  78. package/gsd-core/bin/lib/surface.cjs +12 -1
  79. package/gsd-core/bin/lib/uat-predicate.cjs +11 -1
  80. package/gsd-core/bin/lib/uat.cjs +320 -21
  81. package/gsd-core/bin/lib/unusable-input.cjs +9 -0
  82. package/gsd-core/bin/lib/verification.cjs +29 -12
  83. package/gsd-core/bin/lib/verify.cjs +29 -5
  84. package/gsd-core/bin/lib/workflow-fragments.cjs +557 -0
  85. package/gsd-core/bin/lib/workstream-inventory-builder.cjs +181 -18
  86. package/gsd-core/bin/lib/workstream-inventory.cjs +519 -27
  87. package/gsd-core/bin/lib/workstream.cjs +6 -0
  88. package/gsd-core/bin/lib/worktree-base-ref.cjs +50 -6
  89. package/gsd-core/bin/lib/worktree-safety.cjs +276 -118
  90. package/gsd-core/bin/shared/config-schema.manifest.json +2 -0
  91. package/gsd-core/references/artifact-types.md +10 -3
  92. package/gsd-core/references/autonomous-ui-design-contract.md +42 -0
  93. package/gsd-core/references/debugger-techniques.md +255 -0
  94. package/gsd-core/references/research-documentation-lookup.md +5 -3
  95. package/gsd-core/references/specless-probe-fallback.md +7 -6
  96. package/gsd-core/references/verifier-wiring-patterns.md +100 -0
  97. package/gsd-core/references/worktree-branch-check.md +2 -2
  98. package/gsd-core/templates/summary-complex.md +2 -0
  99. package/gsd-core/templates/summary-minimal.md +2 -0
  100. package/gsd-core/templates/summary-standard.md +2 -0
  101. package/gsd-core/templates/summary.md +2 -0
  102. package/gsd-core/workflows/audit-milestone.md +3 -0
  103. package/gsd-core/workflows/autonomous/steps/converge-banner.md +1 -0
  104. package/gsd-core/workflows/autonomous/steps/converge-dispatch-bg.md +11 -0
  105. package/gsd-core/workflows/autonomous/steps/converge-dispatch-inline.md +7 -0
  106. package/gsd-core/workflows/autonomous/steps/converge-fail-fast.md +21 -0
  107. package/gsd-core/workflows/autonomous/steps/converge-loop.md +7 -0
  108. package/gsd-core/workflows/autonomous.md +32 -69
  109. package/gsd-core/workflows/code-review/steps/dispatch-fix.md +39 -0
  110. package/gsd-core/workflows/code-review/steps/structural-pre-pass.md +83 -0
  111. package/gsd-core/workflows/code-review.md +42 -145
  112. package/gsd-core/workflows/complete-milestone/steps/git-tag.md +29 -0
  113. package/gsd-core/workflows/complete-milestone.md +23 -81
  114. package/gsd-core/workflows/debug.md +9 -12
  115. package/gsd-core/workflows/diagnose-issues.md +22 -0
  116. package/gsd-core/workflows/discovery-phase.md +4 -4
  117. package/gsd-core/workflows/discuss-phase-assumptions/steps/auto-advance-dispatch.md +15 -0
  118. package/gsd-core/workflows/discuss-phase-assumptions.md +5 -16
  119. package/gsd-core/workflows/docs-update/steps/dispatch-monorepo-packages.md +51 -0
  120. package/gsd-core/workflows/docs-update.md +8 -51
  121. package/gsd-core/workflows/execute-phase/steps/executor-isolation-dispatch.md +34 -2
  122. package/gsd-core/workflows/execute-phase/steps/gap-closure-artifacts.md +50 -0
  123. package/gsd-core/workflows/execute-phase/steps/partial-wave.md +31 -0
  124. package/gsd-core/workflows/execute-phase/steps/per-plan-worktree-gate.md +19 -0
  125. package/gsd-core/workflows/execute-phase/steps/regression-gate-run.md +42 -0
  126. package/gsd-core/workflows/execute-phase/steps/regression-gate.md +43 -37
  127. package/gsd-core/workflows/execute-phase.md +65 -137
  128. package/gsd-core/workflows/execute-plan.md +1 -1
  129. package/gsd-core/workflows/help/modes/full.md +6 -1
  130. package/gsd-core/workflows/ingest-docs.md +2 -1
  131. package/gsd-core/workflows/new-milestone/steps/project-md-milestone-write.md +16 -0
  132. package/gsd-core/workflows/new-milestone/steps/reset-phase-safety.md +19 -0
  133. package/gsd-core/workflows/new-milestone.md +21 -38
  134. package/gsd-core/workflows/new-project/steps/auto-mode-config.md +176 -0
  135. package/gsd-core/workflows/new-project/steps/auto-mode-detection.md +32 -0
  136. package/gsd-core/workflows/new-project/steps/codebase-map-offer.md +18 -0
  137. package/gsd-core/workflows/new-project.md +13 -226
  138. package/gsd-core/workflows/plan-phase/steps/adr-ingest-express-path.md +15 -0
  139. package/gsd-core/workflows/plan-phase/steps/chunked-planning-mode.md +110 -0
  140. package/gsd-core/workflows/plan-phase/steps/prd-express-gate.md +8 -0
  141. package/gsd-core/workflows/plan-phase/steps/research-only-early-exit.md +17 -0
  142. package/gsd-core/workflows/plan-phase/steps/research-only-modifiers.md +16 -0
  143. package/gsd-core/workflows/plan-phase/steps/reviews-prerequisite.md +17 -0
  144. package/gsd-core/workflows/plan-phase/steps/stall-detection-helpers.md +149 -0
  145. package/gsd-core/workflows/plan-phase.md +49 -193
  146. package/gsd-core/workflows/progress/steps/forensic-audit.md +125 -0
  147. package/gsd-core/workflows/progress/steps/mvp-display.md +18 -0
  148. package/gsd-core/workflows/progress.md +11 -153
  149. package/gsd-core/workflows/quick/steps/discussion-phase.md +124 -0
  150. package/gsd-core/workflows/quick/steps/plan-checker-loop.md +111 -0
  151. package/gsd-core/workflows/quick/steps/quick-verification.md +46 -0
  152. package/gsd-core/workflows/quick/steps/research-phase.md +72 -0
  153. package/gsd-core/workflows/quick/steps/worktree-pre-dispatch-commit.md +37 -0
  154. package/gsd-core/workflows/quick.md +20 -390
  155. package/gsd-core/workflows/resume-project.md +3 -0
  156. package/gsd-core/workflows/review/steps/reviewer-instances-note-1.md +4 -0
  157. package/gsd-core/workflows/review/steps/reviewer-instances-note-2.md +3 -0
  158. package/gsd-core/workflows/review.md +15 -8
  159. package/gsd-core/workflows/section-manifest.json +219 -0
  160. package/gsd-core/workflows/sketch.md +1 -1
  161. package/gsd-core/workflows/spec-phase.md +17 -14
  162. package/gsd-core/workflows/spike-wrap-up.md +20 -5
  163. package/gsd-core/workflows/spike.md +50 -16
  164. package/gsd-core/workflows/sync-skills.md +49 -11
  165. package/gsd-core/workflows/transition/steps/workstream-collision-check.md +17 -0
  166. package/gsd-core/workflows/transition.md +8 -21
  167. package/gsd-core/workflows/ui-phase.md +8 -7
  168. package/gsd-core/workflows/update/steps/channel-banner.md +7 -0
  169. package/gsd-core/workflows/update.md +18 -7
  170. package/gsd-core/workflows/verify-phase.md +4 -7
  171. package/gsd-core/workflows/verify-work/steps/automated-ui-verification.md +36 -0
  172. package/gsd-core/workflows/verify-work/steps/mvp-uat-framing.md +21 -0
  173. package/gsd-core/workflows/verify-work.md +8 -58
  174. package/hooks/dist/gsd-agent-isolation-guard.js +428 -0
  175. package/hooks/dist/gsd-check-update-worker.js +14 -5
  176. package/hooks/dist/gsd-cursor-subagent-start.js +532 -26
  177. package/hooks/dist/gsd-read-injection-scanner.js +7 -0
  178. package/hooks/dist/gsd-statusline.js +72 -6
  179. package/hooks/dist/gsd-worktree-path-guard.js +2 -1
  180. package/hooks/dist/gsd-write-guard.js +359 -0
  181. package/hooks/dist/lib/isolation-sentinel.js +268 -0
  182. package/hooks/dist/managed-hooks-registry.cjs +2 -0
  183. package/hooks/gsd-agent-isolation-guard.js +428 -0
  184. package/hooks/gsd-check-update-worker.js +14 -5
  185. package/hooks/gsd-cursor-subagent-start.js +532 -26
  186. package/hooks/gsd-read-injection-scanner.js +7 -0
  187. package/hooks/gsd-statusline.js +72 -6
  188. package/hooks/gsd-worktree-path-guard.js +2 -1
  189. package/hooks/gsd-write-guard.js +359 -0
  190. package/hooks/hooks.json +12 -0
  191. package/hooks/lib/isolation-sentinel.js +268 -0
  192. package/hooks/managed-hooks-registry.cjs +2 -0
  193. package/package.json +14 -5
  194. package/pi/gsd.cjs +57 -12
  195. package/scripts/build-hooks.js +9 -0
  196. package/scripts/changeset/lint.cjs +9 -2
  197. package/scripts/changeset/serialize.cjs +5 -1
  198. package/scripts/gen-capability-matrix.cjs +1 -1
  199. package/scripts/gen-context-index.cjs +448 -0
  200. package/scripts/gen-inventory-manifest.cjs +101 -1
  201. package/scripts/gen-prompt-budget-parity-corpus.cjs +645 -0
  202. package/scripts/gen-registry.cjs +39 -15
  203. package/scripts/gen-section-manifest.cjs +638 -0
  204. package/scripts/generate-package-identity.cjs +4 -2
  205. package/scripts/lint-allow-test-rule-refs.allowlist.json +17 -31
  206. package/scripts/lint-compiled-artifact-sync.cjs +6 -1
  207. package/scripts/lint-docs-command-form.cjs +195 -0
  208. package/scripts/lint-docs-required.cjs +9 -1
  209. package/scripts/lint-emitted-drift-ack.cjs +215 -20
  210. package/scripts/lint-example-parser-parity.cjs +395 -0
  211. package/scripts/lint-test-file-count.allowlist.json +27 -1
  212. package/scripts/mutation-matrix.cjs +13 -0
  213. package/scripts/prompt-injection-scan.sh +27 -6
  214. package/scripts/registry-schema.cjs +323 -94
  215. package/scripts/run-tests.cjs +3 -2
  216. package/scripts/validate-registry.cjs +10 -6
  217. package/skills/gsd-autonomous/SKILL.md +1 -1
  218. package/skills/gsd-execute-phase/SKILL.md +1 -1
  219. package/skills/gsd-mempalace-capture/SKILL.md +1 -1
  220. package/skills/gsd-new-milestone/SKILL.md +1 -1
  221. package/skills/gsd-plan-phase/SKILL.md +2 -2
  222. package/vscode/package.json +1 -1
  223. package/scripts/gen-emitted-baseline.cjs +0 -145
@@ -0,0 +1,428 @@
1
+ #!/usr/bin/env node
2
+ // gsd-hook-version: {{GSD_VERSION}}
3
+ // GSD Agent Isolation Dispatch Guard — PreToolUse hook (#3045)
4
+ //
5
+ // Problem: `gsd-core/workflows/execute-phase/steps/executor-isolation-dispatch.md`
6
+ // resolves the project's dispatch isolation correctly
7
+ // (`gsd_run query dispatch-isolation --raw`), but DELIVERY of that value into
8
+ // the model-authored `Agent(subagent_type="gsd-executor", ...)` call is a
9
+ // prose instruction ("substitute $HARNESS_FLAG's value ... on Claude Code it
10
+ // is literally isolation=\"worktree\""). Nothing verifies the model actually
11
+ // copied it. When it is omitted, the executor runs and commits directly in
12
+ // the user's PRIMARY checkout instead of an isolated worktree, with no
13
+ // consent and no warning.
14
+ //
15
+ // A prose backstop cannot fix a prose defect — it is the same class of
16
+ // artifact the model may equally skip. This hook enforces the invariant at
17
+ // the tooling layer instead: HARD-BLOCKING.
18
+ //
19
+ // Applicability (must positively determine all three to act — otherwise
20
+ // inert):
21
+ // 1. this is a GSD project (`.planning/config.json` exists under cwd),
22
+ // 2. the project's resolved dispatch isolation is `harness-worktree`,
23
+ // 3. the dispatch target is an executor (`subagent_type === "gsd-executor"`;
24
+ // no other executor-shaped subagent type exists in agents/ today).
25
+ //
26
+ // Fail-closed exception (#3050 lesson: a guard that cannot verify must not
27
+ // answer "safe"): if the project IS a GSD project but the hook cannot read
28
+ // or resolve its dispatch-isolation configuration, it DENIES rather than
29
+ // defaulting to the "safe-looking" none/allow value that
30
+ // `gsd-core/bin/gsd-tools.cjs`'s own `routeDispatchIsolation` degrades to on
31
+ // error. That existing query is fail-OPEN by design (sequential execution
32
+ // is always safe for the SCHEDULER); this guard's job is the opposite
33
+ // invariant (never dispatch unisolated when isolation was promised), so it
34
+ // cannot reuse that fail-open default and instead resolves isolation
35
+ // itself, distinguishing "resolved cleanly" from "could not resolve".
36
+ //
37
+ // Isolation resolution (#3045 BLOCKER fix, see hooks/lib/isolation-sentinel.js):
38
+ // prefers the workflow's own PERSISTED per-dispatch decision (a sentinel
39
+ // `record-dispatch-isolation` writes after `executor-isolation-dispatch.md`
40
+ // resolves ISOLATION in shell, applying workflow.use_worktrees, the #2474
41
+ // per-plan submodule degrade, and the #683/#3060 base-check auto-degrade)
42
+ // over re-deriving a host CAPABILITY from the registry. A fresh sentinel is
43
+ // authoritative — `none`/`orchestrator-worktree` ALLOW immediately
44
+ // (sequential/orchestrator-managed dispatch is legitimate, not a bug); an
45
+ // absent/stale sentinel falls back to a conservative registry+config check
46
+ // (GSD_RUNTIME env > .planning/config.json `runtime` — no confident signal
47
+ // degrades to inert rather than guessing 'claude', see resolveRegistryIsolation)
48
+ // gated additionally by `workflow.use_worktrees` — read directly, in-process,
49
+ // no subprocess spawn.
50
+ //
51
+ // Triggers on: Agent/Task tool calls with subagent_type === "gsd-executor"
52
+ // (both names accepted — #3045 MAJOR 1: only Agent was previously matched,
53
+ // silently inert on any host/version whose subagent tool is named Task).
54
+ // Action: BLOCK (exit 2) when isolation should be enforced and is not
55
+ // No-op: any tool other than Agent/Task, non-executor targets, GSD projects
56
+ // whose resolved isolation is not harness-worktree, non-GSD projects,
57
+ // malformed payloads, or a dispatch that already carries the correct
58
+ // isolation parameter.
59
+
60
+ 'use strict';
61
+
62
+ const fs = require('fs');
63
+ const path = require('path');
64
+ const os = require('os');
65
+ const { readSentinel, VALID_ISOLATION, extractDispatchIdentifiers, sentinelAppliesToDispatch } = require('./lib/isolation-sentinel.js');
66
+
67
+ // No other executor-shaped subagent_type exists in agents/ today
68
+ // (verified: only agents/gsd-executor.md). A Set, not a bare string compare,
69
+ // so a future sibling executor role can be added here without touching the
70
+ // matching logic below.
71
+ const EXECUTOR_SUBAGENT_TYPES = new Set(['gsd-executor']);
72
+
73
+ /**
74
+ * Parse a registry `harnessIsolationFlag` of the shape `key="value"` (the
75
+ * only shape an `Agent()` tool_input kwarg can express) into its parameter
76
+ * name and expected value. Bare CLI-flag shapes (e.g. a hypothetical
77
+ * `--worktree`) have no tool_input kwarg equivalent and are not checkable
78
+ * here — this hook is scoped to the Claude Code `Agent` tool's keyword-arg
79
+ * dispatch surface.
80
+ */
81
+ function parseHarnessFlag(flag) {
82
+ if (typeof flag !== 'string') return null;
83
+ const m = /^([A-Za-z_][\w-]*)="([^"]*)"$/.exec(flag);
84
+ if (!m) return null;
85
+ return { param: m[1], value: m[2] };
86
+ }
87
+
88
+ /**
89
+ * Resolve this project's declared `runtime` identity WITHOUT defaulting to
90
+ * 'claude' when no explicit signal exists (#3045 MAJOR 2).
91
+ *
92
+ * `gsd-core/templates/config.json` — the actual scaffold used to write every
93
+ * new project's config.json — ships with NO `runtime` key, so "no signal"
94
+ * is the COMMON case, not a corner case. Previously this resolution silently
95
+ * defaulted to 'claude' in that case, which meant every non-Claude runtime
96
+ * that also installs this hook (any `hostIntegration.hooksSurface ===
97
+ * 'settings-json'` runtime, not only Claude) had Claude's
98
+ * `harnessIsolationFlag` ("isolation=\"worktree\"") demanded on its Agent()
99
+ * -equivalent dispatch — a kwarg that runtime's own tool never accepts.
100
+ *
101
+ * Returns `{ runtimeId, confident }`. `confident` is true only when an
102
+ * explicit signal exists (GSD_RUNTIME env override, a `runtime` key literally
103
+ * present in config.json, or a `runtime` persisted to `~/.gsd/defaults.json`
104
+ * by the installer — see below); false means "cannot determine" and callers
105
+ * must NOT silently substitute 'claude' — see resolveRegistryIsolation.
106
+ *
107
+ * #3045 BLOCKER 2 fix: precedence is GSD_RUNTIME env > config.json `runtime`
108
+ * key > `~/.gsd/defaults.json` `runtime`. The first two are unchanged; the
109
+ * third is NEW — `bin/install.js`'s `writeNonClaudeDefaults` already persists
110
+ * the installed runtime to `~/.gsd/defaults.json` for every non-Claude
111
+ * runtime install (`defaults.runtime = runtime`, #2395), so this is real,
112
+ * already-shipping data, not a new write. Before this fix, "no signal" was
113
+ * the COMMON case for any project whose config.json was scaffolded from
114
+ * `gsd-core/templates/config.json` (which ships with NO `runtime` key) and
115
+ * whose session had no `GSD_RUNTIME` override — i.e. nearly every non-Claude
116
+ * install, since Claude installs never reach `writeNonClaudeDefaults` at all
117
+ * (`nativeModelAliases` short-circuits it) and therefore correctly still rely
118
+ * on config.json/env. Reading the installer's own persisted signal makes
119
+ * "confident" the common case instead.
120
+ */
121
+ function resolveRuntimeIdentity(cwd, configPath, resolveRuntimeNameFromCandidates) {
122
+ const envRuntime = resolveRuntimeNameFromCandidates(process.env.GSD_RUNTIME);
123
+ if (envRuntime) return { runtimeId: envRuntime, confident: true };
124
+
125
+ // A throw here (corrupt JSON, EISDIR, permission error) propagates to the
126
+ // caller's catch as a resolution failure — this function only decides
127
+ // "confident vs not", never resolution failure.
128
+ const raw = fs.readFileSync(configPath, 'utf-8');
129
+ const parsed = JSON.parse(raw);
130
+ if (parsed && typeof parsed === 'object' && 'runtime' in parsed) {
131
+ const configRuntime = resolveRuntimeNameFromCandidates(parsed.runtime);
132
+ if (configRuntime) return { runtimeId: configRuntime, confident: true };
133
+ }
134
+
135
+ // #3045 BLOCKER 2: fall back to the installer-persisted default. Read
136
+ // defensively — an absent/corrupt/non-object defaults.json is "no signal",
137
+ // never a resolution failure (this function only ever throws for the
138
+ // config.json read above, which the caller's catch already handles).
139
+ try {
140
+ const defaultsPath = path.join(os.homedir(), '.gsd', 'defaults.json');
141
+ const defaultsRaw = fs.readFileSync(defaultsPath, 'utf-8');
142
+ const defaultsParsed = JSON.parse(defaultsRaw);
143
+ if (defaultsParsed && typeof defaultsParsed === 'object' && 'runtime' in defaultsParsed) {
144
+ const defaultsRuntime = resolveRuntimeNameFromCandidates(defaultsParsed.runtime);
145
+ if (defaultsRuntime) return { runtimeId: defaultsRuntime, confident: true };
146
+ }
147
+ } catch {
148
+ // Absent or unreadable ~/.gsd/defaults.json — no signal, fall through.
149
+ }
150
+
151
+ return { runtimeId: null, confident: false };
152
+ }
153
+
154
+ /**
155
+ * Resolve the registry-declared `harnessIsolationFlag` descriptor for
156
+ * `runtimeId` — pure host-CAPABILITY lookup, used both when the sentinel
157
+ * confirms harness-worktree but omitted the flag, and by the conservative
158
+ * fallback path. Returns `null` when the host declares no usable flag.
159
+ */
160
+ function resolveHarnessFlag(runtimeId, runtimes) {
161
+ const runtimeEntry = runtimes != null ? runtimes[runtimeId] : null;
162
+ const declaredFlag = runtimeEntry?.runtime?.harnessIsolationFlag ?? null;
163
+ return (typeof declaredFlag === 'string' && declaredFlag.length > 0) ? declaredFlag : null;
164
+ }
165
+
166
+ /**
167
+ * Conservative fallback resolution used when the #3045 sentinel is absent or
168
+ * stale: re-derive isolation from the registry CAPABILITY, gated by
169
+ * `workflow.use_worktrees` (config-schema key confirmed present in
170
+ * gsd-core/bin/shared/config-schema.manifest.json's validKeys, so it survives
171
+ * loadConfig's whitelist; read directly from the raw config.json here — same
172
+ * side-effect-free approach cmdConfigGet itself uses, not through loadConfig).
173
+ *
174
+ * MAJOR 2: when the runtime cannot be confidently determined (no GSD_RUNTIME
175
+ * override, no `runtime` key in config.json — the common case, since the
176
+ * project scaffold ships without one), this resolves to 'none' (inert)
177
+ * rather than guessing 'claude' and demanding Claude's flag on a host that
178
+ * may not even be Claude. Denying every dispatch on an undeterminable
179
+ * runtime would repeat the exact false-positive class the #3045 BLOCKER
180
+ * itself was — this is the fallback path only (the sentinel-fresh path above
181
+ * already carries the workflow's own confirmed decision + flag, so this
182
+ * degrades coverage only for dispatches that happen outside a GSD workflow
183
+ * run, e.g. a manual Agent() call before any sentinel has been written).
184
+ */
185
+ function resolveRegistryIsolation(cwd, configPath) {
186
+ const { resolveRuntimeNameFromCandidates } = require('../gsd-core/bin/lib/runtime-name-policy.cjs');
187
+ const { runtimes } = require('../gsd-core/bin/lib/capability-registry.cjs');
188
+
189
+ const { runtimeId, confident } = resolveRuntimeIdentity(cwd, configPath, resolveRuntimeNameFromCandidates);
190
+ if (!confident) {
191
+ return { isolation: 'none', harnessFlag: null };
192
+ }
193
+
194
+ const runtimeEntry = runtimes != null ? runtimes[runtimeId] : null;
195
+ const declared = runtimeEntry?.runtime?.hostIntegration?.dispatch?.isolation ?? null;
196
+ let isolation = (typeof declared === 'string' && VALID_ISOLATION.has(declared)) ? declared : 'none';
197
+
198
+ let harnessFlag = null;
199
+ if (isolation === 'harness-worktree') {
200
+ harnessFlag = resolveHarnessFlag(runtimeId, runtimes);
201
+ if (harnessFlag === null) {
202
+ // A host claiming harness isolation with no declared flag gives this
203
+ // guard nothing to check for — degrade to 'none' rather than block on
204
+ // an unspecifiable requirement.
205
+ isolation = 'none';
206
+ }
207
+ }
208
+
209
+ if (isolation === 'harness-worktree') {
210
+ let useWorktrees = true;
211
+ try {
212
+ const raw = fs.readFileSync(configPath, 'utf-8');
213
+ const parsedCfg = JSON.parse(raw);
214
+ if (parsedCfg && typeof parsedCfg === 'object' && parsedCfg.workflow &&
215
+ typeof parsedCfg.workflow === 'object' && parsedCfg.workflow.use_worktrees === false) {
216
+ useWorktrees = false;
217
+ }
218
+ } catch {
219
+ // Unreadable config already propagated to the outer caller's catch
220
+ // before this point in practice (resolveRuntimeIdentity reads it
221
+ // first); tolerate defensively and keep the conservative (enforce)
222
+ // default rather than silently disabling the guard.
223
+ }
224
+ if (!useWorktrees) isolation = 'none';
225
+ }
226
+
227
+ return { isolation, harnessFlag };
228
+ }
229
+
230
+ /**
231
+ * Resolve this project's dispatch isolation mode, distinguishing three
232
+ * outcomes:
233
+ * - { gsdProject: false } — not a GSD project, inert
234
+ * - { gsdProject: true, error: <Error> } — cannot verify, DENY
235
+ * - { gsdProject: true, isolation, harnessFlag } — resolved cleanly
236
+ *
237
+ * `.planning/config.json` EXISTING (regardless of whether it can be read) is
238
+ * the GSD-project signal, mirroring gsd-workflow-guard.js /
239
+ * gsd-context-monitor.js. Any failure reading or parsing it, or requiring the
240
+ * sibling registry/policy modules, after that point means "GSD project
241
+ * present, isolation mode unknown" — folded into the DENY path rather than
242
+ * silently defaulting to a mode that happens to look safe.
243
+ *
244
+ * #3045 BLOCKER fix: prefer the workflow's own PERSISTED per-dispatch
245
+ * decision (the sentinel `record-dispatch-isolation` writes) over
246
+ * re-deriving a host CAPABILITY from the registry. The registry's
247
+ * harness-worktree entry means "this host CAN isolate", not "this dispatch
248
+ * SHOULD be isolated" — sequential ISOLATION=none legitimately happens even
249
+ * on a harness-worktree-capable host (project opt-out, per-plan submodule
250
+ * intersection, #683/#3060 base-check auto-degrade). A fresh sentinel is
251
+ * authoritative; an absent/stale one falls back to the conservative
252
+ * registry+config check via resolveRegistryIsolation.
253
+ *
254
+ * `clock` is injectable for the sentinel-staleness check (repo clock-seam
255
+ * convention); defaults to the real `Date`.
256
+ *
257
+ * `dispatchIds` (optional, `{plan, phase}`) is the identifiers extracted from
258
+ * THIS dispatch's own prompt/description text (#3045 SECURITY F2 —
259
+ * `extractDispatchIdentifiers` in `hooks/lib/isolation-sentinel.js`). When
260
+ * supplied and a fresh sentinel disagrees with it on a shared identifier, the
261
+ * sentinel is treated as NOT APPLICABLE to this dispatch (falls through to
262
+ * the conservative registry+config fallback below) rather than trusted —
263
+ * "a mismatch is 'no applicable sentinel', not an allow" (#3045 review).
264
+ */
265
+ function resolveIsolationState(cwd, { clock = Date, dispatchIds = null } = {}) {
266
+ const configPath = path.join(cwd, '.planning', 'config.json');
267
+ let projectExists;
268
+ try {
269
+ fs.accessSync(configPath, fs.constants.F_OK);
270
+ projectExists = true;
271
+ } catch {
272
+ projectExists = false;
273
+ }
274
+ if (!projectExists) {
275
+ return { gsdProject: false, isolation: null, harnessFlag: null, error: null };
276
+ }
277
+
278
+ const sentinel = readSentinel(cwd, { clock });
279
+ if (sentinel.present && !sentinel.stale && sentinelAppliesToDispatch(sentinel, dispatchIds)) {
280
+ if (sentinel.isolation !== 'harness-worktree') {
281
+ return { gsdProject: true, isolation: sentinel.isolation, harnessFlag: null, error: null };
282
+ }
283
+ if (sentinel.harnessFlag) {
284
+ return { gsdProject: true, isolation: 'harness-worktree', harnessFlag: sentinel.harnessFlag, error: null };
285
+ }
286
+ // #3045 BLOCKER 2 fix: the sentinel already PROVED this dispatch requires
287
+ // isolation (it resolved harness-worktree) but carries no usable flag —
288
+ // this must DENY, not degrade to the registry's "not confident -> none"
289
+ // fallback. That degrade exists for the case where NOTHING has resolved
290
+ // isolation yet (the conservative fallback path below); reusing it here
291
+ // was the BLOCKER: a fresh sentinel asserting harness-worktree with no
292
+ // flag fell through to a registry lookup that, on the common "runtime not
293
+ // confidently determinable" case, silently returned isolation:'none' and
294
+ // ALLOWED the dispatch to run unisolated in the primary checkout — the
295
+ // exact failure this guard exists to prevent, and on the default-install
296
+ // path (no `runtime` key in gsd-core/templates/config.json), not a corner
297
+ // case. With the #3045 CORE REDESIGN, `dispatch-isolation` always resolves
298
+ // and records `harnessFlag` together with `isolation` in one atomic write
299
+ // whenever isolation is 'harness-worktree' (routeDispatchIsolation
300
+ // degrades to 'none' itself when no flag is declared) — so a fresh
301
+ // sentinel with isolation:'harness-worktree' and no flag should not occur
302
+ // in practice. This branch is defense-in-depth for a sentinel written by
303
+ // an older gsd-tools.cjs, a hand-crafted/corrupted-in-a-*valid*-way
304
+ // sentinel, or any other path that reaches this state; it MUST deny.
305
+ return {
306
+ gsdProject: true,
307
+ isolation: null,
308
+ harnessFlag: null,
309
+ error: new Error(
310
+ 'dispatch-isolation sentinel resolved "harness-worktree" but recorded no harness_flag — ' +
311
+ 'cannot verify what parameter the dispatch must carry.'
312
+ ),
313
+ };
314
+ }
315
+
316
+ // Sentinel absent, stale, or does not apply to this dispatch (F2 mismatch):
317
+ // conservative fallback (#3045 finding — must still cover fail-closed case
318
+ // (a): a project that opted out of worktrees entirely via
319
+ // workflow.use_worktrees).
320
+ try {
321
+ const { isolation, harnessFlag } = resolveRegistryIsolation(cwd, configPath);
322
+ return { gsdProject: true, isolation, harnessFlag, error: null };
323
+ } catch (err) {
324
+ return { gsdProject: true, isolation: null, harnessFlag: null, error: err };
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Process one PreToolUse payload (already JSON-parsed) and return the
330
+ * decision without touching stdin/stdout/process.exit — the exported,
331
+ * directly-testable core of this hook's logic (#3045 MAJOR: "the clock seam
332
+ * is dead code" fix). The stdin-driven script below is now a thin adapter
333
+ * over this function so tests can `require()` it and inject a `clock`
334
+ * (`{now(): number}`) directly per the repo's clock-seam convention, instead
335
+ * of racing real `Date.now()` across a spawned subprocess boundary.
336
+ *
337
+ * Returns `{ action: 'allow' } | { action: 'block', reason: string }`.
338
+ */
339
+ function evaluateDispatch(data, { clock = Date } = {}) {
340
+ if (!data || typeof data !== 'object') return { action: 'allow' };
341
+ // #3045 MAJOR 1: accept both subagent-dispatch tool names. hooks.json's
342
+ // matcher is "Agent|Task" (mirroring the repo's own PostToolUse
343
+ // precedent) so this must match both, or it is silently inert on any
344
+ // host/version whose subagent tool is named "Task".
345
+ if (data.tool_name !== 'Agent' && data.tool_name !== 'Task') return { action: 'allow' };
346
+
347
+ const toolInput = (data.tool_input && typeof data.tool_input === 'object') ? data.tool_input : {};
348
+ const subagentType = toolInput.subagent_type;
349
+ if (typeof subagentType !== 'string' || !EXECUTOR_SUBAGENT_TYPES.has(subagentType)) {
350
+ return { action: 'allow' };
351
+ }
352
+
353
+ const cwd = data.cwd || process.cwd();
354
+ // #3045 SECURITY F2: best-effort plan/phase extraction from this
355
+ // dispatch's own description text, so a fresh sentinel that disagrees
356
+ // with THIS dispatch is treated as inapplicable rather than trusted.
357
+ const dispatchIds = extractDispatchIdentifiers(toolInput.description);
358
+ const state = resolveIsolationState(cwd, { clock, dispatchIds });
359
+
360
+ if (!state.gsdProject) return { action: 'allow' };
361
+
362
+ if (state.error) {
363
+ const reason =
364
+ `Agent isolation guard: could not read or resolve this project's dispatch-isolation ` +
365
+ `configuration ('.planning/config.json' under '${cwd}'). Refusing to dispatch ` +
366
+ `subagent_type="${subagentType}" without being able to verify whether isolation is ` +
367
+ `required — a guard that cannot verify must not answer "safe" (#3050). Retry once the ` +
368
+ `project configuration is readable.`;
369
+ return { action: 'block', reason };
370
+ }
371
+
372
+ if (state.isolation !== 'harness-worktree') return { action: 'allow' };
373
+
374
+ const parsed = parseHarnessFlag(state.harnessFlag);
375
+ if (!parsed) return { action: 'allow' };
376
+
377
+ if (toolInput[parsed.param] === parsed.value) return { action: 'allow' };
378
+
379
+ const reason =
380
+ `Agent isolation guard: this project's dispatch isolation resolves to "harness-worktree", ` +
381
+ `but the Agent() dispatch for subagent_type="${subagentType}" is missing ` +
382
+ `${parsed.param}="${parsed.value}". Add ${parsed.param}="${parsed.value}" to the Agent() ` +
383
+ `call so the executor runs in an isolated worktree instead of the primary checkout ` +
384
+ `(gsd-core/workflows/execute-phase/steps/executor-isolation-dispatch.md).`;
385
+ return { action: 'block', reason };
386
+ }
387
+
388
+ /* istanbul ignore next -- stdin adapter, exercised via spawnSync in tests */
389
+ function main() {
390
+ let input = '';
391
+ const stdinTimeout = setTimeout(() => process.exit(0), 3000);
392
+ process.stdin.setEncoding('utf8');
393
+ process.stdin.on('data', (chunk) => { input += chunk; });
394
+ process.stdin.on('end', () => {
395
+ clearTimeout(stdinTimeout);
396
+ try {
397
+ const data = JSON.parse(input);
398
+ const decision = evaluateDispatch(data);
399
+ if (decision.action === 'block') {
400
+ const out = { decision: 'block', reason: decision.reason };
401
+ process.stdout.write(JSON.stringify(out));
402
+ // Kimi feeds stderr (not stdout) back to the model on exit 2.
403
+ process.stderr.write(decision.reason);
404
+ process.exit(2);
405
+ }
406
+ process.exit(0);
407
+ } catch {
408
+ // Silent fail — never block valid tool calls due to hook errors
409
+ // (malformed payload, etc.). This is distinct from resolveIsolationState's
410
+ // internal error handling, which DOES deny — this outer catch only
411
+ // covers payload parsing before applicability could even be determined.
412
+ process.exit(0);
413
+ }
414
+ });
415
+ }
416
+
417
+ if (require.main === module) {
418
+ main();
419
+ }
420
+
421
+ module.exports = {
422
+ evaluateDispatch,
423
+ resolveIsolationState,
424
+ resolveRuntimeIdentity,
425
+ resolveHarnessFlag,
426
+ resolveRegistryIsolation,
427
+ parseHarnessFlag,
428
+ };
@@ -15,9 +15,12 @@ const { isSemverNewer } = require('../gsd-core/bin/lib/semver-compare.cjs');
15
15
  // Latest-version lookup is delegated to the single deterministic adapter
16
16
  // (#498). checkLatestVersion() owns the npm-view call, the timeout/semver
17
17
  // policy, and the package name — sourced from the baked Package Identity seam.
18
- // The previous `require('../package.json').name` (#378) resolved to undefined
19
- // in the installed tree (only a {"type":"commonjs"} marker ships), so the
20
- // background check never reported updates.
18
+ // The previous `require('../package.json').name` (#378) never yielded a name in
19
+ // the installed tree — at the time it resolved to the synthetic
20
+ // {"type":"commonjs"} marker GSD wrote at the config root, which has no `.name`,
21
+ // so the background check never reported updates. Since #2544 GSD writes no
22
+ // marker there at all, so that require would now fail to resolve outright.
23
+ // Either way the name must come from the baked seam, never a walk-up.
21
24
  const { checkLatestVersion } = require('../gsd-core/bin/check-latest-version.cjs');
22
25
  const { PACKAGE_NAME } = require('../gsd-core/bin/lib/package-identity.cjs');
23
26
  // Authoritative list of managed hooks — shared with tests to retire source-grep
@@ -53,14 +56,20 @@ try {
53
56
  } catch (e) {}
54
57
 
55
58
  // Check for stale hooks — compare hook version headers against installed VERSION
56
- // Hooks are installed at configDir/hooks/ (e.g. ~/.claude/hooks/) (#1421)
59
+ // Since #3023 the bundle directory name is resolved from __dirname (this
60
+ // worker is staged INSIDE the bundle), not assumed to be configDir/hooks —
61
+ // the directory name is runtime-descriptor-driven (e.g. `gsd-hooks/` for pi).
57
62
  // Only check hooks that GSD currently ships — orphaned files from removed features
58
63
  // (e.g., gsd-intel-*.js) must be ignored to avoid permanent stale warnings (#1750)
59
64
  // MANAGED_HOOKS is imported from ./managed-hooks-registry.cjs above.
60
65
 
61
66
  let staleHooks = [];
62
67
  if (configDir) {
63
- const hooksDir = path.join(configDir, 'hooks');
68
+ // #3023: the bundle's directory name is runtime-descriptor-driven (pi stages
69
+ // it as `gsd-hooks/`), so deriving it as `<configDir>/hooks` silently scanned
70
+ // nothing there. This worker is staged INSIDE the bundle, so __dirname is the
71
+ // bundle directory by construction — name-agnostic and one fewer assumption.
72
+ const hooksDir = __dirname;
64
73
  try {
65
74
  if (fs.existsSync(hooksDir)) {
66
75
  const hookFiles = fs.readdirSync(hooksDir).filter(f => MANAGED_HOOKS.includes(f));