gsd-pi 2.80.0-dev.cf9433f56 → 2.80.0-dev.d4fc28e6b

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 (237) hide show
  1. package/dist/cli.js +0 -19
  2. package/dist/resources/.managed-resources-content-hash +1 -1
  3. package/dist/resources/extensions/claude-code-cli/stream-adapter.js +29 -0
  4. package/dist/resources/extensions/gsd/auto/loop.js +71 -8
  5. package/dist/resources/extensions/gsd/auto/phases.js +150 -94
  6. package/dist/resources/extensions/gsd/auto/resolve.js +12 -0
  7. package/dist/resources/extensions/gsd/auto/run-unit.js +10 -30
  8. package/dist/resources/extensions/gsd/auto/session.js +8 -0
  9. package/dist/resources/extensions/gsd/auto/workflow-dispatch-claim.js +33 -1
  10. package/dist/resources/extensions/gsd/auto/workflow-worker-heartbeat.js +9 -1
  11. package/dist/resources/extensions/gsd/auto-direct-dispatch.js +5 -32
  12. package/dist/resources/extensions/gsd/auto-dispatch.js +16 -0
  13. package/dist/resources/extensions/gsd/auto-post-unit.js +17 -4
  14. package/dist/resources/extensions/gsd/auto-prompts.js +90 -15
  15. package/dist/resources/extensions/gsd/auto-start.js +197 -6
  16. package/dist/resources/extensions/gsd/auto-worktree.js +111 -1
  17. package/dist/resources/extensions/gsd/auto.js +18 -22
  18. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +86 -19
  19. package/dist/resources/extensions/gsd/bootstrap/db-tools.js +49 -36
  20. package/dist/resources/extensions/gsd/bootstrap/dynamic-tools.js +15 -5
  21. package/dist/resources/extensions/gsd/bootstrap/exec-tools.js +9 -3
  22. package/dist/resources/extensions/gsd/bootstrap/journal-tools.js +7 -1
  23. package/dist/resources/extensions/gsd/bootstrap/memory-tools.js +9 -3
  24. package/dist/resources/extensions/gsd/bootstrap/query-tools.js +8 -2
  25. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +298 -54
  26. package/dist/resources/extensions/gsd/bootstrap/system-context.js +82 -23
  27. package/dist/resources/extensions/gsd/clean-root-preflight.js +24 -6
  28. package/dist/resources/extensions/gsd/commands-handlers.js +23 -9
  29. package/dist/resources/extensions/gsd/db/unit-dispatches.js +53 -0
  30. package/dist/resources/extensions/gsd/ecosystem/gsd-extension-api.js +2 -0
  31. package/dist/resources/extensions/gsd/guided-flow.js +47 -28
  32. package/dist/resources/extensions/gsd/native-git-bridge.js +32 -8
  33. package/dist/resources/extensions/gsd/orphan-stash-audit.js +101 -0
  34. package/dist/resources/extensions/gsd/parallel-orchestrator.js +13 -3
  35. package/dist/resources/extensions/gsd/pre-execution-checks.js +15 -0
  36. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  37. package/dist/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  38. package/dist/resources/extensions/gsd/prompts/execute-task.md +4 -2
  39. package/dist/resources/extensions/gsd/prompts/plan-slice.md +1 -1
  40. package/dist/resources/extensions/gsd/prompts/replan-slice.md +2 -2
  41. package/dist/resources/extensions/gsd/workflow-protocol.js +131 -0
  42. package/dist/resources/extensions/gsd/worktree-resolver.js +35 -4
  43. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  44. package/dist/web/standalone/.next/BUILD_ID +1 -1
  45. package/dist/web/standalone/.next/app-path-routes-manifest.json +12 -12
  46. package/dist/web/standalone/.next/build-manifest.json +2 -2
  47. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  48. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  49. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  51. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  55. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  56. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  57. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  59. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  61. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  63. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/index.html +1 -1
  65. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  66. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  67. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  68. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  69. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  70. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  71. package/dist/web/standalone/.next/server/app-paths-manifest.json +12 -12
  72. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  73. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  74. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  75. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  76. package/dist/welcome-screen.d.ts +2 -0
  77. package/dist/welcome-screen.js +9 -7
  78. package/package.json +1 -1
  79. package/packages/pi-agent-core/dist/agent-loop.d.ts.map +1 -1
  80. package/packages/pi-agent-core/dist/agent-loop.js +4 -1
  81. package/packages/pi-agent-core/dist/agent-loop.js.map +1 -1
  82. package/packages/pi-agent-core/dist/agent.d.ts +5 -0
  83. package/packages/pi-agent-core/dist/agent.d.ts.map +1 -1
  84. package/packages/pi-agent-core/dist/agent.js +2 -0
  85. package/packages/pi-agent-core/dist/agent.js.map +1 -1
  86. package/packages/pi-agent-core/dist/index.d.ts +1 -0
  87. package/packages/pi-agent-core/dist/index.d.ts.map +1 -1
  88. package/packages/pi-agent-core/dist/index.js +2 -0
  89. package/packages/pi-agent-core/dist/index.js.map +1 -1
  90. package/packages/pi-agent-core/dist/token-audit.d.ts +47 -0
  91. package/packages/pi-agent-core/dist/token-audit.d.ts.map +1 -0
  92. package/packages/pi-agent-core/dist/token-audit.js +221 -0
  93. package/packages/pi-agent-core/dist/token-audit.js.map +1 -0
  94. package/packages/pi-agent-core/dist/types.d.ts +9 -0
  95. package/packages/pi-agent-core/dist/types.d.ts.map +1 -1
  96. package/packages/pi-agent-core/dist/types.js.map +1 -1
  97. package/packages/pi-agent-core/src/agent-loop.test.ts +128 -0
  98. package/packages/pi-agent-core/src/agent-loop.ts +4 -1
  99. package/packages/pi-agent-core/src/agent.ts +8 -0
  100. package/packages/pi-agent-core/src/index.ts +2 -0
  101. package/packages/pi-agent-core/src/token-audit.test.ts +189 -0
  102. package/packages/pi-agent-core/src/token-audit.ts +287 -0
  103. package/packages/pi-agent-core/src/types.ts +14 -0
  104. package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -1
  105. package/packages/pi-coding-agent/dist/core/agent-session-tool-refresh.test.js +18 -0
  106. package/packages/pi-coding-agent/dist/core/agent-session-tool-refresh.test.js.map +1 -1
  107. package/packages/pi-coding-agent/dist/core/agent-session.d.ts +12 -0
  108. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  109. package/packages/pi-coding-agent/dist/core/agent-session.js +36 -7
  110. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  111. package/packages/pi-coding-agent/dist/core/extensions/loader.d.ts.map +1 -1
  112. package/packages/pi-coding-agent/dist/core/extensions/loader.js +8 -0
  113. package/packages/pi-coding-agent/dist/core/extensions/loader.js.map +1 -1
  114. package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts +2 -0
  115. package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts.map +1 -1
  116. package/packages/pi-coding-agent/dist/core/extensions/runner.js +3 -6
  117. package/packages/pi-coding-agent/dist/core/extensions/runner.js.map +1 -1
  118. package/packages/pi-coding-agent/dist/core/extensions/runner.test.js +3 -3
  119. package/packages/pi-coding-agent/dist/core/extensions/runner.test.js.map +1 -1
  120. package/packages/pi-coding-agent/dist/core/extensions/types.d.ts +32 -1
  121. package/packages/pi-coding-agent/dist/core/extensions/types.d.ts.map +1 -1
  122. package/packages/pi-coding-agent/dist/core/extensions/types.js.map +1 -1
  123. package/packages/pi-coding-agent/dist/core/hooks-runner.test.js +2 -0
  124. package/packages/pi-coding-agent/dist/core/hooks-runner.test.js.map +1 -1
  125. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.d.ts +2 -0
  126. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.d.ts.map +1 -0
  127. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.js +46 -0
  128. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.js.map +1 -0
  129. package/packages/pi-coding-agent/dist/core/sdk.d.ts +10 -2
  130. package/packages/pi-coding-agent/dist/core/sdk.d.ts.map +1 -1
  131. package/packages/pi-coding-agent/dist/core/sdk.js +74 -2
  132. package/packages/pi-coding-agent/dist/core/sdk.js.map +1 -1
  133. package/packages/pi-coding-agent/dist/core/skill-tool.test.js +22 -0
  134. package/packages/pi-coding-agent/dist/core/skill-tool.test.js.map +1 -1
  135. package/packages/pi-coding-agent/dist/core/system-prompt.d.ts +6 -7
  136. package/packages/pi-coding-agent/dist/core/system-prompt.d.ts.map +1 -1
  137. package/packages/pi-coding-agent/dist/core/system-prompt.js +2 -3
  138. package/packages/pi-coding-agent/dist/core/system-prompt.js.map +1 -1
  139. package/packages/pi-coding-agent/src/core/agent-session-tool-refresh.test.ts +25 -0
  140. package/packages/pi-coding-agent/src/core/agent-session.ts +40 -7
  141. package/packages/pi-coding-agent/src/core/extensions/loader.ts +10 -0
  142. package/packages/pi-coding-agent/src/core/extensions/runner.test.ts +3 -3
  143. package/packages/pi-coding-agent/src/core/extensions/runner.ts +5 -5
  144. package/packages/pi-coding-agent/src/core/extensions/types.ts +35 -1
  145. package/packages/pi-coding-agent/src/core/hooks-runner.test.ts +2 -0
  146. package/packages/pi-coding-agent/src/core/sdk-tool-filter.test.ts +60 -0
  147. package/packages/pi-coding-agent/src/core/sdk.ts +85 -3
  148. package/packages/pi-coding-agent/src/core/skill-tool.test.ts +28 -0
  149. package/packages/pi-coding-agent/src/core/system-prompt.ts +8 -10
  150. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  151. package/src/resources/extensions/claude-code-cli/stream-adapter.ts +30 -0
  152. package/src/resources/extensions/claude-code-cli/tests/stream-adapter.test.ts +26 -0
  153. package/src/resources/extensions/gsd/auto/loop-deps.ts +2 -2
  154. package/src/resources/extensions/gsd/auto/loop.ts +84 -8
  155. package/src/resources/extensions/gsd/auto/phases.ts +218 -154
  156. package/src/resources/extensions/gsd/auto/resolve.ts +19 -0
  157. package/src/resources/extensions/gsd/auto/run-unit.ts +10 -29
  158. package/src/resources/extensions/gsd/auto/session.ts +8 -0
  159. package/src/resources/extensions/gsd/auto/workflow-dispatch-claim.ts +63 -1
  160. package/src/resources/extensions/gsd/auto/workflow-worker-heartbeat.ts +14 -1
  161. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +8 -34
  162. package/src/resources/extensions/gsd/auto-dispatch.ts +16 -0
  163. package/src/resources/extensions/gsd/auto-post-unit.ts +18 -4
  164. package/src/resources/extensions/gsd/auto-prompts.ts +95 -14
  165. package/src/resources/extensions/gsd/auto-start.ts +230 -9
  166. package/src/resources/extensions/gsd/auto-worktree.ts +123 -0
  167. package/src/resources/extensions/gsd/auto.ts +18 -18
  168. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +100 -18
  169. package/src/resources/extensions/gsd/bootstrap/db-tools.ts +50 -36
  170. package/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts +16 -5
  171. package/src/resources/extensions/gsd/bootstrap/exec-tools.ts +10 -3
  172. package/src/resources/extensions/gsd/bootstrap/journal-tools.ts +8 -1
  173. package/src/resources/extensions/gsd/bootstrap/memory-tools.ts +10 -3
  174. package/src/resources/extensions/gsd/bootstrap/query-tools.ts +9 -2
  175. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +347 -54
  176. package/src/resources/extensions/gsd/bootstrap/system-context.ts +90 -22
  177. package/src/resources/extensions/gsd/clean-root-preflight.ts +32 -7
  178. package/src/resources/extensions/gsd/commands-handlers.ts +34 -15
  179. package/src/resources/extensions/gsd/db/unit-dispatches.ts +66 -0
  180. package/src/resources/extensions/gsd/ecosystem/gsd-extension-api.ts +3 -0
  181. package/src/resources/extensions/gsd/guided-flow.ts +52 -35
  182. package/src/resources/extensions/gsd/native-git-bridge.ts +39 -6
  183. package/src/resources/extensions/gsd/orphan-stash-audit.ts +117 -0
  184. package/src/resources/extensions/gsd/parallel-orchestrator.ts +13 -3
  185. package/src/resources/extensions/gsd/pre-execution-checks.ts +16 -0
  186. package/src/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  187. package/src/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  188. package/src/resources/extensions/gsd/prompts/execute-task.md +4 -2
  189. package/src/resources/extensions/gsd/prompts/plan-slice.md +1 -1
  190. package/src/resources/extensions/gsd/prompts/replan-slice.md +2 -2
  191. package/src/resources/extensions/gsd/tests/artifact-retry-cap.test.ts +2 -2
  192. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +361 -10
  193. package/src/resources/extensions/gsd/tests/auto-wrapup-inflight-guard.test.ts +168 -6
  194. package/src/resources/extensions/gsd/tests/clean-root-preflight.test.ts +15 -6
  195. package/src/resources/extensions/gsd/tests/complete-milestone-excerpt.test.ts +31 -0
  196. package/src/resources/extensions/gsd/tests/complete-slice-composer.test.ts +3 -2
  197. package/src/resources/extensions/gsd/tests/context-store.test.ts +7 -1
  198. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +5 -1
  199. package/src/resources/extensions/gsd/tests/execute-task-rendering.test.ts +5 -2
  200. package/src/resources/extensions/gsd/tests/fast-forward-reused-milestone-branch.test.ts +219 -0
  201. package/src/resources/extensions/gsd/tests/finalize-survivor-branch.test.ts +132 -0
  202. package/src/resources/extensions/gsd/tests/isolation-none-branch-guard.test.ts +6 -3
  203. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +5 -1
  204. package/src/resources/extensions/gsd/tests/journal-query-tool.test.ts +32 -0
  205. package/src/resources/extensions/gsd/tests/knowledge.test.ts +47 -0
  206. package/src/resources/extensions/gsd/tests/merge-conflict-stops-loop.test.ts +1 -0
  207. package/src/resources/extensions/gsd/tests/milestone-merge-stash-restore.test.ts +242 -0
  208. package/src/resources/extensions/gsd/tests/native-git-bridge-exec-fallback.test.ts +34 -2
  209. package/src/resources/extensions/gsd/tests/originalbase-path-comparison.test.ts +3 -0
  210. package/src/resources/extensions/gsd/tests/orphan-merge-bootstrap.test.ts +133 -0
  211. package/src/resources/extensions/gsd/tests/orphan-stash-audit.test.ts +201 -0
  212. package/src/resources/extensions/gsd/tests/parallel-orchestrator-fast-forward.test.ts +113 -0
  213. package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +7 -5
  214. package/src/resources/extensions/gsd/tests/prompt-duplication-cuts.test.ts +230 -0
  215. package/src/resources/extensions/gsd/tests/query-tools-db-open.test.ts +3 -3
  216. package/src/resources/extensions/gsd/tests/restore-tools-after-discuss.test.ts +38 -17
  217. package/src/resources/extensions/gsd/tests/select-resumable-milestone.test.ts +96 -0
  218. package/src/resources/extensions/gsd/tests/session-start-footer.test.ts +77 -0
  219. package/src/resources/extensions/gsd/tests/session-switch-abort-misclassification.test.ts +166 -0
  220. package/src/resources/extensions/gsd/tests/state-corruption-2945.test.ts +1 -0
  221. package/src/resources/extensions/gsd/tests/system-context-memory.test.ts +112 -0
  222. package/src/resources/extensions/gsd/tests/system-context-message-routing.test.ts +7 -9
  223. package/src/resources/extensions/gsd/tests/token-tool-gating.test.ts +291 -0
  224. package/src/resources/extensions/gsd/tests/unit-dispatches.test.ts +50 -1
  225. package/src/resources/extensions/gsd/tests/unstructured-continue-context-injection.test.ts +5 -4
  226. package/src/resources/extensions/gsd/tests/workflow-dispatch-claim.test.ts +142 -0
  227. package/src/resources/extensions/gsd/tests/workflow-protocol-excerpt.test.ts +99 -0
  228. package/src/resources/extensions/gsd/tests/workflow-worker-heartbeat.test.ts +32 -1
  229. package/src/resources/extensions/gsd/tests/worktree-journal-events.test.ts +1 -0
  230. package/src/resources/extensions/gsd/tests/worktree-path-injection.test.ts +22 -19
  231. package/src/resources/extensions/gsd/tests/worktree-project-root-degrade.test.ts +66 -0
  232. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +104 -3
  233. package/src/resources/extensions/gsd/workflow-protocol.ts +160 -0
  234. package/src/resources/extensions/gsd/worktree-resolver.ts +49 -4
  235. package/src/resources/extensions/gsd/tests/phases-merge-error-stops-auto.test.ts +0 -97
  236. /package/dist/web/standalone/.next/static/{-5nHJWzSdG-WkPMul_khA → cWaxzf-sdbSSbbwYu8q7a}/_buildManifest.js +0 -0
  237. /package/dist/web/standalone/.next/static/{-5nHJWzSdG-WkPMul_khA → cWaxzf-sdbSSbbwYu8q7a}/_ssgManifest.js +0 -0
@@ -1,3 +1,5 @@
1
+ // Project/App: GSD-2
2
+ // File Purpose: System prompt and hidden context bootstrap for GSD sessions.
1
3
  import { existsSync, readFileSync, unlinkSync } from "node:fs";
2
4
  import { join } from "node:path";
3
5
 
@@ -21,6 +23,11 @@ import { toPosixPath } from "../../shared/mod.js";
21
23
  import { autoEnableCmuxPreferences } from "../commands-cmux.js";
22
24
  import { gsdHome } from "../gsd-home.js";
23
25
 
26
+ const DEFAULT_CONTEXT_MESSAGE_MAX_CHARS = 4_000;
27
+ const DEFAULT_KNOWLEDGE_MAX_CHARS = 12_000;
28
+ const DEFAULT_CODEBASE_MAX_CHARS = 8_000;
29
+ const MIN_CONTEXT_MESSAGE_MAX_CHARS = 1_000;
30
+ const MIN_KNOWLEDGE_MAX_CHARS = 1_000;
24
31
 
25
32
  /**
26
33
  * Bundled skill triggers — resolved dynamically at runtime instead of
@@ -158,8 +165,6 @@ export async function buildBeforeAgentStartResult(
158
165
  logWarning("bootstrap", `decisions backfill failed: ${(e as Error).message}`);
159
166
  }
160
167
 
161
- const memoryBlock = await loadMemoryBlock(event.prompt ?? "");
162
-
163
168
  let newSkillsBlock = "";
164
169
  if (hasSkillSnapshot()) {
165
170
  const newSkills = detectNewSkills();
@@ -190,11 +195,10 @@ export async function buildBeforeAgentStartResult(
190
195
  if (rawContent) {
191
196
  // Cap injection size to ~2 000 tokens to avoid bloating every request.
192
197
  // Full map is always available at .gsd/CODEBASE.md.
193
- const MAX_CODEBASE_CHARS = 8_000;
194
198
  const generatedMatch = rawContent.match(/Generated: (\S+)/);
195
199
  const generatedAt = generatedMatch?.[1] ?? "unknown";
196
- const content = rawContent.length > MAX_CODEBASE_CHARS
197
- ? rawContent.slice(0, MAX_CODEBASE_CHARS) + "\n\n*(truncated — see .gsd/CODEBASE.md for full map)*"
200
+ const content = rawContent.length > DEFAULT_CODEBASE_MAX_CHARS
201
+ ? rawContent.slice(0, DEFAULT_CODEBASE_MAX_CHARS) + "\n\n*(truncated — see .gsd/CODEBASE.md for full map)*"
198
202
  : rawContent;
199
203
  codebaseBlock = `\n\n[PROJECT CODEBASE — File structure and descriptions (generated ${generatedAt}, auto-refreshed when GSD detects tracked file changes; use /gsd codebase stats for status)]\n\n${content}`;
200
204
  }
@@ -206,6 +210,9 @@ export async function buildBeforeAgentStartResult(
206
210
  warnDeprecatedAgentInstructions();
207
211
 
208
212
  const injection = await buildGuidedExecuteContextInjection(event.prompt, process.cwd());
213
+ const memoryBlock = await loadMemoryBlock(event.prompt ?? "", {
214
+ includePromptRelevant: !(injection && isLowEntropyResumePrompt(event.prompt ?? "")),
215
+ });
209
216
 
210
217
  // Re-inject forensics context on follow-up turns (#2941)
211
218
  const forensicsInjection = !injection ? buildForensicsContextInjection(process.cwd(), event.prompt) : null;
@@ -218,12 +225,9 @@ export async function buildBeforeAgentStartResult(
218
225
  : "";
219
226
 
220
227
  // memoryBlock is FTS-queried against the user prompt and changes per call.
221
- // Removing it from `fullSystem` keeps the system-prompt cache breakpoint
222
- // stable across calls the only scoped goal of this fix. The pi-ai
223
- // Anthropic adapter additionally cache-marks the last user turn, so the
224
- // memoryBlock injected via the context message may itself be cached up to
225
- // that boundary; that's orthogonal and unchanged from prior behavior. The
226
- // load-bearing win here is preserving the system+tools cache hit. (#5019)
228
+ // Keeping it out of `fullSystem` preserves provider prompt-cache stability
229
+ // for the static system/tool prefix. The dynamic memory block rides the
230
+ // volatile context message instead. (#5019)
227
231
  const fullSystem = `${event.systemPrompt}\n\n[SYSTEM CONTEXT — GSD]\n\n${systemContent}${preferenceBlock}${knowledgeBlock}${codebaseBlock}${newSkillsBlock}${worktreeBlock}${subagentModelBlock}`;
228
232
 
229
233
  stopContextTimer({
@@ -255,21 +259,55 @@ export function buildContextMessage(opts: {
255
259
  injection: string | null;
256
260
  forensicsInjection: string | null;
257
261
  }): { customType: string; content: string; display: false } | null {
258
- const memoryContent = opts.memoryBlock.trim();
262
+ const contextCharLimit = getContextMessageCharLimit();
263
+ const memoryContent = markMemoryContextSupplied(opts.memoryBlock.trim());
259
264
  if (opts.injection) {
260
- const content = memoryContent ? `${memoryContent}\n\n${opts.injection}` : opts.injection;
265
+ const content = limitContextMessageContent(
266
+ memoryContent ? `${memoryContent}\n\n${opts.injection}` : opts.injection,
267
+ contextCharLimit,
268
+ );
261
269
  return { customType: "gsd-guided-context", content, display: false as const };
262
270
  }
263
271
  if (opts.forensicsInjection) {
264
- const content = memoryContent ? `${memoryContent}\n\n${opts.forensicsInjection}` : opts.forensicsInjection;
272
+ const content = limitContextMessageContent(
273
+ memoryContent ? `${memoryContent}\n\n${opts.forensicsInjection}` : opts.forensicsInjection,
274
+ contextCharLimit,
275
+ );
265
276
  return { customType: "gsd-forensics", content, display: false as const };
266
277
  }
267
278
  if (memoryContent) {
268
- return { customType: "gsd-memory", content: memoryContent, display: false as const };
279
+ return {
280
+ customType: "gsd-memory",
281
+ content: limitContextMessageContent(memoryContent, contextCharLimit),
282
+ display: false as const,
283
+ };
269
284
  }
270
285
  return null;
271
286
  }
272
287
 
288
+ function getContextMessageCharLimit(): number | null {
289
+ const raw = process.env.PI_GSD_CONTEXT_MAX_CHARS;
290
+ if (!raw) return DEFAULT_CONTEXT_MESSAGE_MAX_CHARS;
291
+ if (raw === "0") return null;
292
+ const parsed = Number(raw);
293
+ if (!Number.isFinite(parsed) || parsed < MIN_CONTEXT_MESSAGE_MAX_CHARS) {
294
+ return DEFAULT_CONTEXT_MESSAGE_MAX_CHARS;
295
+ }
296
+ return Math.floor(parsed);
297
+ }
298
+
299
+ function limitContextMessageContent(content: string, limit: number | null): string {
300
+ if (!limit || content.length <= limit) return content;
301
+ const suffix = "\n\n[GSD Context Truncated]\nFull context is available from the referenced .gsd files and tools; read on demand only if this excerpt lacks required evidence.";
302
+ const headBudget = Math.max(0, limit - suffix.length);
303
+ return `${content.slice(0, headBudget).trimEnd()}${suffix}`;
304
+ }
305
+
306
+ function markMemoryContextSupplied(memoryContent: string): string {
307
+ if (!memoryContent) return "";
308
+ return `[GSD Context Metadata]\n- Memory supplied: yes\n\n${memoryContent}`;
309
+ }
310
+
273
311
  /**
274
312
  * ADR-013 step 4 — auto-injection parity for the memories table.
275
313
  *
@@ -288,7 +326,10 @@ export function buildContextMessage(opts: {
288
326
  * with a token-budget cap. Failures degrade gracefully — the function never
289
327
  * throws and returns "" so the system prompt construction continues.
290
328
  */
291
- export async function loadMemoryBlock(userPrompt: string): Promise<string> {
329
+ export async function loadMemoryBlock(
330
+ userPrompt: string,
331
+ opts: { includePromptRelevant?: boolean } = {},
332
+ ): Promise<string> {
292
333
  try {
293
334
  const { formatMemoriesForPrompt, getActiveMemoriesRanked, queryMemoriesRanked } = await import("../memory-store.js");
294
335
 
@@ -310,7 +351,7 @@ export async function loadMemoryBlock(userPrompt: string): Promise<string> {
310
351
 
311
352
  let relevant: typeof allRanked = [];
312
353
  const trimmed = userPrompt.trim();
313
- if (trimmed) {
354
+ if (trimmed && opts.includePromptRelevant !== false) {
314
355
  const hits = queryMemoriesRanked({ query: trimmed, k: QUERY_K });
315
356
  relevant = hits.map((h) => h.memory).filter((m) => !criticalIds.has(m.id));
316
357
  }
@@ -362,14 +403,37 @@ export function loadKnowledgeBlock(gsdHomeDir: string, cwd: string): { block: st
362
403
  }
363
404
 
364
405
  const parts: string[] = [];
365
- if (globalKnowledge) parts.push(`## Global Knowledge\n\n${globalKnowledge}`);
366
- if (projectKnowledge) parts.push(`## Project Knowledge\n\n${projectKnowledge}`);
406
+ if (globalKnowledge) {
407
+ parts.push(`## Global Knowledge\nSource: \`${globalKnowledgePath}\`\n\n${globalKnowledge}`);
408
+ }
409
+ if (projectKnowledge) {
410
+ parts.push(`## Project Knowledge\nSource: \`${knowledgePath}\`\n\n${projectKnowledge}`);
411
+ }
412
+ const body = limitKnowledgeBlock(parts.join("\n\n"), getKnowledgeCharLimit());
367
413
  return {
368
- block: `\n\n[KNOWLEDGE — Rules, patterns, and lessons learned]\n\n${parts.join("\n\n")}`,
414
+ block: `\n\n[KNOWLEDGE — Rules, patterns, and lessons learned]\n\n${body}`,
369
415
  globalSizeKb,
370
416
  };
371
417
  }
372
418
 
419
+ function getKnowledgeCharLimit(): number | null {
420
+ const raw = process.env.PI_GSD_KNOWLEDGE_MAX_CHARS;
421
+ if (!raw) return DEFAULT_KNOWLEDGE_MAX_CHARS;
422
+ if (raw === "0") return null;
423
+ const parsed = Number(raw);
424
+ if (!Number.isFinite(parsed) || parsed < MIN_KNOWLEDGE_MAX_CHARS) {
425
+ return DEFAULT_KNOWLEDGE_MAX_CHARS;
426
+ }
427
+ return Math.floor(parsed);
428
+ }
429
+
430
+ function limitKnowledgeBlock(content: string, limit: number | null): string {
431
+ if (!limit || content.length <= limit) return content;
432
+ const suffix = "\n\n[Knowledge Truncated]\nFull KNOWLEDGE.md content remains available at the source path(s) above; read on demand only if this excerpt lacks a required rule.";
433
+ const headBudget = Math.max(0, limit - suffix.length);
434
+ return `${content.slice(0, headBudget).trimEnd()}${suffix}`;
435
+ }
436
+
373
437
  function buildWorktreeContextBlock(): string {
374
438
  const worktreeName = getActiveWorktreeName();
375
439
  const worktreeMainCwd = getWorktreeOriginalCwd();
@@ -423,6 +487,11 @@ function buildWorktreeContextBlock(): string {
423
487
  */
424
488
  const RESUME_INTENT_PATTERNS = /^(continue|resume|ok|go|go ahead|proceed|keep going|carry on|next|yes|yeah|yep|sure|do it|let's go|pick up where you left off)$/;
425
489
 
490
+ export function isLowEntropyResumePrompt(prompt: string): boolean {
491
+ const trimmed = prompt.trim().toLowerCase().replace(/[.!?,]+$/g, "");
492
+ return RESUME_INTENT_PATTERNS.test(trimmed);
493
+ }
494
+
426
495
  async function buildGuidedExecuteContextInjection(prompt: string, basePath: string): Promise<string | null> {
427
496
  const ensureStateDbOpen = async () => {
428
497
  const { ensureDbOpen } = await import("./dynamic-tools.js");
@@ -452,8 +521,7 @@ async function buildGuidedExecuteContextInjection(prompt: string, basePath: stri
452
521
  // control/help/diagnostic prompts with unrelated execution context.
453
522
  // Phase-gated: only fire during "executing" to avoid misrouting during
454
523
  // replanning, gate evaluation, or other non-execution phases.
455
- const trimmed = prompt.trim().toLowerCase().replace(/[.!?,]+$/g, "");
456
- if (RESUME_INTENT_PATTERNS.test(trimmed)) {
524
+ if (isLowEntropyResumePrompt(prompt)) {
457
525
  await ensureStateDbOpen();
458
526
  const state = await deriveState(basePath);
459
527
  if (state.phase === "executing" && state.activeTask && state.activeMilestone && state.activeSlice) {
@@ -3,13 +3,13 @@
3
3
  *
4
4
  * #2909: Adds a fast-path git status check before milestone completion merges.
5
5
  * When the working tree is dirty the user is warned and changes are auto-stashed
6
- * so the merge can proceed cleanly. After the merge completes, postflightPopStash
7
- * restores the stashed changes.
6
+ * so the merge can proceed cleanly. After the merge completes, postflightPopStash
7
+ * restores the stashed changes and reports whether manual recovery is needed.
8
8
  *
9
9
  * Design constraints (from Trek-e approval):
10
10
  * - Warn the user before stashing (no silent surprises)
11
11
  * - git stash push / git stash pop only — no custom stash management layer
12
- * - Stash/pop errors are logged but MUST NOT block the merge
12
+ * - Stash/pop errors are logged but MUST NOT block the merge itself
13
13
  * - Fast-path status check — clean trees pay no extra cost
14
14
  */
15
15
 
@@ -27,6 +27,13 @@ export interface PreflightResult {
27
27
  summary: string;
28
28
  }
29
29
 
30
+ export interface PostflightResult {
31
+ restored: boolean;
32
+ needsManualRecovery: boolean;
33
+ message: string;
34
+ stashRef?: string;
35
+ }
36
+
30
37
  function findPreflightStashRef(basePath: string, milestoneId: string, stashMarker?: string): string | null {
31
38
  const markerPrefix = `gsd-preflight-stash:${milestoneId}:`;
32
39
  let fallbackRef: string | null = null;
@@ -112,14 +119,15 @@ export function preflightCleanRoot(
112
119
  *
113
120
  * Only called when preflightCleanRoot returned stashPushed=true.
114
121
  * Any pop error (e.g. conflict) is logged and notified but does NOT throw —
115
- * the merge already completed successfully.
122
+ * the merge already completed successfully. Callers must treat
123
+ * needsManualRecovery=true as a dirty workspace stop, not a clean completion.
116
124
  */
117
125
  export function postflightPopStash(
118
126
  basePath: string,
119
127
  milestoneId: string,
120
128
  stashMarker: string | undefined,
121
129
  notify: (message: string, level: "info" | "warning" | "error") => void,
122
- ): void {
130
+ ): PostflightResult {
123
131
  let stashRef: string | null = null;
124
132
  try {
125
133
  stashRef = findPreflightStashRef(basePath, milestoneId, stashMarker);
@@ -127,7 +135,11 @@ export function postflightPopStash(
127
135
  const msg = `No matching GSD preflight stash found for milestone ${milestoneId}; leaving stash list untouched.`;
128
136
  logWarning("preflight", msg);
129
137
  notify(msg, "warning");
130
- return;
138
+ return {
139
+ restored: false,
140
+ needsManualRecovery: true,
141
+ message: msg,
142
+ };
131
143
  }
132
144
  execFileSync("git", ["stash", "pop", stashRef], {
133
145
  cwd: basePath,
@@ -135,7 +147,14 @@ export function postflightPopStash(
135
147
  encoding: "utf-8",
136
148
  env: GIT_NO_PROMPT_ENV,
137
149
  });
138
- notify(`Restored stashed changes after milestone ${milestoneId} merge.`, "info");
150
+ const msg = `Restored stashed changes after milestone ${milestoneId} merge.`;
151
+ notify(msg, "info");
152
+ return {
153
+ restored: true,
154
+ needsManualRecovery: false,
155
+ message: msg,
156
+ stashRef,
157
+ };
139
158
  } catch (err) {
140
159
  // Pop conflicts mean the merged code collides with the stashed changes.
141
160
  // Log a warning — the user needs to resolve manually, but the merge succeeded.
@@ -145,5 +164,11 @@ export function postflightPopStash(
145
164
  const msg = `git stash pop ${stashRef ?? ""}`.trim() + ` failed after merge of milestone ${milestoneId}: ${err instanceof Error ? err.message : String(err)}. ${restoreHint}`;
146
165
  logWarning("preflight", msg);
147
166
  notify(msg, "warning");
167
+ return {
168
+ restored: false,
169
+ needsManualRecovery: true,
170
+ message: msg,
171
+ ...(stashRef ? { stashRef } : {}),
172
+ };
148
173
  }
149
174
  }
@@ -26,6 +26,15 @@ import { isAutoActive, checkRemoteAutoSession } from "./auto.js";
26
26
  import { getAutoWorktreePath } from "./auto-worktree.js";
27
27
  import { currentDirectoryRoot, projectRoot } from "./commands/context.js";
28
28
  import { loadPrompt } from "./prompt-loader.js";
29
+ import {
30
+ buildDoctorHealIssuePayload,
31
+ buildDoctorHealSummary,
32
+ buildWorkflowDispatchContent,
33
+ } from "./workflow-protocol.js";
34
+ import {
35
+ restoreGsdWorkflowTools,
36
+ scopeGsdWorkflowToolsForDispatch,
37
+ } from "./bootstrap/register-hooks.js";
29
38
 
30
39
  const UPDATE_REGISTRY_URL = "https://registry.npmjs.org/gsd-pi/latest";
31
40
  const UPDATE_FETCH_TIMEOUT_MS = 5000;
@@ -72,18 +81,23 @@ export function dispatchDoctorHeal(pi: ExtensionAPI, scope: string | undefined,
72
81
  const workflowPath = process.env.GSD_WORKFLOW_PATH ?? join(gsdHome(), "agent", "GSD-WORKFLOW.md");
73
82
  const workflow = readFileSync(workflowPath, "utf-8");
74
83
  const prompt = loadPrompt("doctor-heal", {
75
- doctorSummary: reportText,
76
- structuredIssues,
84
+ doctorSummary: buildDoctorHealSummary(reportText),
85
+ structuredIssues: buildDoctorHealIssuePayload(structuredIssues),
77
86
  scopeLabel: scope ?? "active milestone / blocking scope",
78
87
  doctorCommandSuffix: scope ? ` ${scope}` : "",
79
88
  });
80
89
 
81
- const content = `Read the following GSD workflow protocol and execute exactly.\n\n${workflow}\n\n## Your Task\n\n${prompt}`;
90
+ const content = buildWorkflowDispatchContent({ workflow, workflowPath, task: prompt });
91
+ const savedTools = scopeGsdWorkflowToolsForDispatch(pi);
82
92
 
83
- pi.sendMessage(
84
- { customType: "gsd-doctor-heal", content, display: false },
85
- { triggerTurn: true },
86
- );
93
+ try {
94
+ pi.sendMessage(
95
+ { customType: "gsd-doctor-heal", content, display: false },
96
+ { triggerTurn: true },
97
+ );
98
+ } finally {
99
+ restoreGsdWorkflowTools(pi, savedTools);
100
+ }
87
101
  }
88
102
 
89
103
  /** Parse doctor command args into structured flags and positionals (pure, no I/O). */
@@ -258,15 +272,20 @@ export async function handleTriage(ctx: ExtensionCommandContext, pi: ExtensionAP
258
272
 
259
273
  const workflowPath = process.env.GSD_WORKFLOW_PATH ?? join(gsdHome(), "agent", "GSD-WORKFLOW.md");
260
274
  const workflow = readFileSync(workflowPath, "utf-8");
275
+ const savedTools = scopeGsdWorkflowToolsForDispatch(pi);
261
276
 
262
- pi.sendMessage(
263
- {
264
- customType: "gsd-triage",
265
- content: `Read the following GSD workflow protocol and execute exactly.\n\n${workflow}\n\n## Your Task\n\n${prompt}`,
266
- display: false,
267
- },
268
- { triggerTurn: true },
269
- );
277
+ try {
278
+ pi.sendMessage(
279
+ {
280
+ customType: "gsd-triage",
281
+ content: buildWorkflowDispatchContent({ workflow, workflowPath, task: prompt }),
282
+ display: false,
283
+ },
284
+ { triggerTurn: true },
285
+ );
286
+ } finally {
287
+ restoreGsdWorkflowTools(pi, savedTools);
288
+ }
270
289
  }
271
290
 
272
291
  export async function handleSteer(change: string, ctx: ExtensionCommandContext, pi: ExtensionAPI): Promise<void> {
@@ -98,6 +98,70 @@ function isAlreadyActiveConstraintError(err: unknown): boolean {
98
98
  return /\bUNIQUE\b|\bconstraint failed\b/i.test(msg);
99
99
  }
100
100
 
101
+ function settleStaleActiveDispatchForUnit(input: RecordClaimInput, now: string): void {
102
+ const db = _getAdapter()!;
103
+ const active = db.prepare(
104
+ `SELECT id, status, worker_id, milestone_lease_token
105
+ FROM unit_dispatches
106
+ WHERE unit_id = :unit_id
107
+ AND status IN ('claimed','running')
108
+ ORDER BY id DESC
109
+ LIMIT 1`,
110
+ ).get({ ":unit_id": input.unitId }) as
111
+ | { id: number; status: DispatchStatus; worker_id: string; milestone_lease_token: number }
112
+ | undefined;
113
+
114
+ if (!active) return;
115
+ if (
116
+ active.worker_id === input.workerId &&
117
+ active.milestone_lease_token === input.milestoneLeaseToken
118
+ ) {
119
+ return;
120
+ }
121
+
122
+ const reason = "stale-dispatch-lease-takeover";
123
+ const result = db.prepare(
124
+ `UPDATE unit_dispatches
125
+ SET status = 'canceled',
126
+ ended_at = :ended_at,
127
+ exit_reason = :reason
128
+ WHERE id = :id
129
+ AND status IN ('claimed','running')
130
+ AND (worker_id != :worker_id OR milestone_lease_token != :token)`,
131
+ ).run({
132
+ ":id": active.id,
133
+ ":ended_at": now,
134
+ ":reason": reason,
135
+ ":worker_id": input.workerId,
136
+ ":token": input.milestoneLeaseToken,
137
+ });
138
+
139
+ const changes =
140
+ typeof (result as { changes?: unknown }).changes === "number"
141
+ ? (result as { changes: number }).changes
142
+ : 0;
143
+ if (changes < 1) return;
144
+
145
+ insertAuditEvent({
146
+ eventId: randomUUID(),
147
+ traceId: input.traceId,
148
+ turnId: input.turnId ?? undefined,
149
+ category: "orchestration",
150
+ type: "dispatch-stale-canceled",
151
+ ts: now,
152
+ payload: {
153
+ dispatchId: active.id,
154
+ unitId: input.unitId,
155
+ priorStatus: active.status,
156
+ priorWorkerId: active.worker_id,
157
+ priorMilestoneLeaseToken: active.milestone_lease_token,
158
+ takeoverWorkerId: input.workerId,
159
+ takeoverMilestoneLeaseToken: input.milestoneLeaseToken,
160
+ reason,
161
+ },
162
+ });
163
+ }
164
+
101
165
  /**
102
166
  * Insert a new dispatch row in `claimed` state. Atomic guard against
103
167
  * double-claim (B2): the partial unique index
@@ -135,6 +199,8 @@ export function recordDispatchClaim(input: RecordClaimInput): RecordClaimResult
135
199
  };
136
200
  }
137
201
 
202
+ settleStaleActiveDispatchForUnit(input, now);
203
+
138
204
  try {
139
205
  const result = db.prepare(
140
206
  `INSERT INTO unit_dispatches (
@@ -204,6 +204,9 @@ export function createGSDExtensionAPI(
204
204
  getAllTools: () => pi.getAllTools(),
205
205
  setActiveTools: (...args: Parameters<ExtensionAPI["setActiveTools"]>) =>
206
206
  pi.setActiveTools(...args),
207
+ getVisibleSkills: () => pi.getVisibleSkills(),
208
+ setVisibleSkills: (...args: Parameters<ExtensionAPI["setVisibleSkills"]>) =>
209
+ pi.setVisibleSkills(...args),
207
210
  getCommands: () => pi.getCommands(),
208
211
 
209
212
  // ── Model & thinking ───────────────────────────────────────────────
@@ -86,6 +86,8 @@ export {
86
86
  import { logWarning } from "./workflow-logger.js";
87
87
  import { deleteRuntimeKv } from "./db/runtime-kv.js";
88
88
  import { PAUSED_SESSION_KV_KEY } from "./interrupted-session.js";
89
+ import { buildWorkflowDispatchContent } from "./workflow-protocol.js";
90
+ import { isFullGsdToolSurfaceRequested, restoreGsdWorkflowTools, scopeGsdWorkflowToolsForDispatch } from "./bootstrap/register-hooks.js";
89
91
 
90
92
  type AutoStartOptions = Parameters<typeof startAutoDetached>[4];
91
93
  type AutoStartLauncher = typeof startAutoDetached;
@@ -1062,46 +1064,61 @@ async function dispatchWorkflow(
1062
1064
  }
1063
1065
  }
1064
1066
 
1065
- // Scope tools for discuss flows (#2949).
1067
+ // Scope tools for guided workflow turns (#2949, token-consumption savings).
1066
1068
  // Providers with grammar-based constrained decoding (xAI/Grok) return
1067
1069
  // "Grammar is too complex" when the combined tool schema is too large.
1068
- // Discuss flows only need a small subset of GSD tools — strip the heavy
1069
- // planning/execution/completion tools to keep the grammar within limits.
1070
- let savedTools: string[] | null = null;
1071
- if (unitType?.startsWith("discuss-")) {
1072
- const currentTools = pi.getActiveTools();
1073
- savedTools = currentTools;
1074
- // Keep all non-GSD tools (builtins, other extensions) and only the
1075
- // GSD tools on the discuss allowlist.
1076
- const scopedTools = currentTools.filter(
1077
- (t) => !t.startsWith("gsd_") || DISCUSS_TOOLS_ALLOWLIST.includes(t),
1078
- );
1079
- pi.setActiveTools(scopedTools);
1080
- debugLog("discuss-tool-scoping", {
1081
- unitType,
1082
- before: currentTools.length,
1083
- after: scopedTools.length,
1084
- removed: currentTools.length - scopedTools.length,
1085
- });
1086
- }
1070
+ // Guided workflow turns only need the active unit's tool surface; strip
1071
+ // unrelated GSD tools and broad non-GSD tools for this queued turn, then
1072
+ // restore so the narrowed surface does not leak into future dispatches.
1073
+ let savedTools: ReturnType<typeof scopeGsdWorkflowToolsForDispatch> = null;
1087
1074
 
1088
- const workflowPath = process.env.GSD_WORKFLOW_PATH ?? join(gsdHome(), "agent", "GSD-WORKFLOW.md");
1089
- const workflow = readFileSync(workflowPath, "utf-8");
1075
+ try {
1076
+ const currentTools = pi.getActiveTools();
1077
+ savedTools = {
1078
+ tools: currentTools,
1079
+ visibleSkills: typeof pi.getVisibleSkills === "function" ? pi.getVisibleSkills() : undefined,
1080
+ restoreVisibleSkills: typeof pi.setVisibleSkills === "function",
1081
+ };
1082
+ if (unitType?.startsWith("discuss-") && !isFullGsdToolSurfaceRequested()) {
1083
+ // Keep all non-GSD tools (builtins, other extensions) and only the
1084
+ // GSD tools on the discuss allowlist.
1085
+ const scopedTools = currentTools.filter(
1086
+ (t) => !t.startsWith("gsd_") || DISCUSS_TOOLS_ALLOWLIST.includes(t),
1087
+ );
1088
+ pi.setActiveTools(scopedTools);
1089
+ const scopedState = scopeGsdWorkflowToolsForDispatch(pi, unitType);
1090
+ savedTools = {
1091
+ tools: currentTools,
1092
+ visibleSkills: scopedState?.visibleSkills ?? savedTools.visibleSkills,
1093
+ restoreVisibleSkills: scopedState?.restoreVisibleSkills ?? savedTools.restoreVisibleSkills,
1094
+ };
1095
+ debugLog("discuss-tool-scoping", {
1096
+ unitType,
1097
+ before: currentTools.length,
1098
+ after: pi.getActiveTools().length,
1099
+ removed: currentTools.length - pi.getActiveTools().length,
1100
+ });
1101
+ } else {
1102
+ savedTools = scopeGsdWorkflowToolsForDispatch(pi, unitType) ?? savedTools;
1103
+ }
1090
1104
 
1091
- pi.sendMessage(
1092
- {
1093
- customType,
1094
- content: `Read the following GSD workflow protocol and execute exactly.\n\n${workflow}\n\n## Your Task\n\n${note}`,
1095
- display: false,
1096
- },
1097
- { triggerTurn: true },
1098
- );
1105
+ const workflowPath = process.env.GSD_WORKFLOW_PATH ?? join(gsdHome(), "agent", "GSD-WORKFLOW.md");
1106
+ const workflow = readFileSync(workflowPath, "utf-8");
1099
1107
 
1100
- // Restore full tool set after the message is queued. The LLM turn has
1101
- // already captured the scoped set — restoring prevents the narrowed
1102
- // tools from leaking into subsequent dispatches (#3628).
1103
- if (savedTools) {
1104
- pi.setActiveTools(savedTools);
1108
+ pi.sendMessage(
1109
+ {
1110
+ customType,
1111
+ content: buildWorkflowDispatchContent({ workflow, workflowPath, task: note }),
1112
+ display: false,
1113
+ },
1114
+ { triggerTurn: true },
1115
+ );
1116
+ } finally {
1117
+ // Restore full tool set after the message is queued. The LLM turn has
1118
+ // already captured the scoped set — restoring prevents the narrowed
1119
+ // tools from leaking into subsequent dispatches (#3628). The finally
1120
+ // block ensures restoration even if sendMessage throws.
1121
+ restoreGsdWorkflowTools(pi, savedTools);
1105
1122
  }
1106
1123
  }
1107
1124
 
@@ -1183,6 +1183,33 @@ export function nativeRmForce(basePath: string, paths: string[]): void {
1183
1183
  }
1184
1184
  }
1185
1185
 
1186
+ function runGitWorktreeAdd(
1187
+ basePath: string,
1188
+ wtPath: string,
1189
+ branch: string,
1190
+ createBranch?: boolean,
1191
+ startPoint?: string,
1192
+ ): void {
1193
+ if (createBranch) {
1194
+ const branchRef = gitExec(basePath, ["show-ref", "--verify", `refs/heads/${branch}`], true);
1195
+ if (branchRef) {
1196
+ gitExec(basePath, ["worktree", "add", wtPath, branch]);
1197
+ return;
1198
+ }
1199
+ gitExec(basePath, ["worktree", "add", "-b", branch, wtPath, startPoint ?? "HEAD"]);
1200
+ } else {
1201
+ gitExec(basePath, ["worktree", "add", wtPath, branch]);
1202
+ }
1203
+ }
1204
+
1205
+ export function assertWorktreeMaterialized(wtPath: string): void {
1206
+ if (existsSync(join(wtPath, ".git"))) return;
1207
+ throw new GSDError(
1208
+ GSD_GIT_ERROR,
1209
+ `git worktree add did not materialize a valid worktree at ${wtPath}: missing .git file`,
1210
+ );
1211
+ }
1212
+
1186
1213
  /**
1187
1214
  * Add a new git worktree.
1188
1215
  * Native: libgit2 worktree API.
@@ -1198,14 +1225,20 @@ export function nativeWorktreeAdd(
1198
1225
  const native = loadNative();
1199
1226
  if (native) {
1200
1227
  native.gitWorktreeAdd(basePath, wtPath, branch, createBranch, startPoint);
1201
- return;
1228
+ try {
1229
+ assertWorktreeMaterialized(wtPath);
1230
+ return;
1231
+ } catch {
1232
+ rmSync(wtPath, { recursive: true, force: true });
1233
+ gitExec(basePath, ["worktree", "prune"], true);
1234
+ runGitWorktreeAdd(basePath, wtPath, branch, createBranch, startPoint);
1235
+ assertWorktreeMaterialized(wtPath);
1236
+ return;
1237
+ }
1202
1238
  }
1203
1239
 
1204
- if (createBranch) {
1205
- gitExec(basePath, ["worktree", "add", "-b", branch, wtPath, startPoint ?? "HEAD"]);
1206
- } else {
1207
- gitExec(basePath, ["worktree", "add", wtPath, branch]);
1208
- }
1240
+ runGitWorktreeAdd(basePath, wtPath, branch, createBranch, startPoint);
1241
+ assertWorktreeMaterialized(wtPath);
1209
1242
  }
1210
1243
 
1211
1244
  /**