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
@@ -23,7 +23,7 @@ import { debugLog } from "./debug-logger.js";
23
23
  import { logWarning, logError } from "./workflow-logger.js";
24
24
  import { loadEffectiveGSDPreferences } from "./preferences.js";
25
25
  import { MILESTONE_ID_RE } from "./milestone-ids.js";
26
- import { nativeGetCurrentBranch, nativeDetectMainBranch, nativeWorkingTreeStatus, nativeAddAllWithExclusions, nativeCommit, nativeCheckoutBranch, nativeMergeSquash, nativeConflictFiles, nativeCheckoutTheirs, nativeAddPaths, nativeRmForce, nativeBranchDelete, nativeBranchForceReset, nativeBranchExists, nativeDiffNumstat, nativeUpdateRef, nativeIsAncestor, nativeMergeAbort, } from "./native-git-bridge.js";
26
+ import { nativeGetCurrentBranch, nativeDetectMainBranch, nativeWorkingTreeStatus, nativeAddAllWithExclusions, nativeCommit, nativeCheckoutBranch, nativeMergeSquash, nativeConflictFiles, nativeCheckoutTheirs, nativeAddPaths, nativeRmForce, nativeBranchDelete, nativeBranchForceReset, nativeBranchExists, nativeDiffNumstat, nativeUpdateRef, nativeIsAncestor, nativeMergeAbort, nativeWorktreeList, } from "./native-git-bridge.js";
27
27
  import { gsdHome } from "./gsd-home.js";
28
28
  import { createWorkspace } from "./workspace.js";
29
29
  const PROJECT_PREFERENCES_FILE = "PREFERENCES.md";
@@ -1018,6 +1018,111 @@ export function enterBranchModeForMilestone(basePath, milestoneId) {
1018
1018
  * for both reads and writes. copyPlanningArtifacts and reconcilePlanCheckboxes
1019
1019
  * (both formerly here) became dead.
1020
1020
  */
1021
+ /**
1022
+ * True when `branch` is checked out in any worktree listed by
1023
+ * `git worktree list --porcelain`. Used to gate ref updates that would
1024
+ * otherwise leave a concurrent worktree's HEAD inconsistent with its
1025
+ * index/working tree (Codex peer-review of #5538-followup).
1026
+ *
1027
+ * Best-effort: a `nativeWorktreeList` failure returns true so we err on
1028
+ * the side of NOT moving the ref. Better to skip a fast-forward than to
1029
+ * silently corrupt another worktree.
1030
+ */
1031
+ export function _isBranchCheckedOutElsewhere(basePath, branch) {
1032
+ try {
1033
+ const entries = nativeWorktreeList(basePath);
1034
+ return entries.some((entry) => entry.branch === branch);
1035
+ }
1036
+ catch {
1037
+ return true;
1038
+ }
1039
+ }
1040
+ /**
1041
+ * Resolve the integration branch using the same 3-tier fallback as the
1042
+ * fresh-create path: META.json → git.main_branch preference → detected
1043
+ * main branch. Returns null when no usable target exists.
1044
+ */
1045
+ function _resolveIntegrationBranchForReuse(basePath, milestoneId) {
1046
+ const fromMeta = readIntegrationBranch(basePath, milestoneId);
1047
+ if (fromMeta)
1048
+ return fromMeta;
1049
+ const gitPrefs = loadEffectiveGSDPreferences()?.preferences?.git;
1050
+ const fromPref = gitPrefs?.main_branch &&
1051
+ typeof gitPrefs.main_branch === "string" &&
1052
+ gitPrefs.main_branch.length > 0 &&
1053
+ nativeBranchExists(basePath, gitPrefs.main_branch)
1054
+ ? gitPrefs.main_branch
1055
+ : null;
1056
+ if (fromPref)
1057
+ return fromPref;
1058
+ try {
1059
+ return nativeDetectMainBranch(basePath);
1060
+ }
1061
+ catch {
1062
+ return null;
1063
+ }
1064
+ }
1065
+ /**
1066
+ * When reusing an existing milestone branch, fast-forward it onto the
1067
+ * integration branch when that's safe (branch is a strict ancestor of
1068
+ * integration — no commits would be lost). Skips when the branch has its
1069
+ * own commits ahead of integration, when the integration branch can't be
1070
+ * resolved, or when any git operation fails — the merge gate at milestone
1071
+ * completion will surface real divergence as a conflict.
1072
+ *
1073
+ * The previous behavior re-attached the worktree to whatever stale tip
1074
+ * the branch held, which caused new milestone work to fork from a base
1075
+ * missing prior milestones' merges (#5538-followup).
1076
+ */
1077
+ export function fastForwardReusedMilestoneBranchIfSafe(basePath, milestoneId, branch) {
1078
+ try {
1079
+ const integrationBranch = _resolveIntegrationBranchForReuse(basePath, milestoneId);
1080
+ if (!integrationBranch || integrationBranch === branch)
1081
+ return;
1082
+ if (!nativeBranchExists(basePath, integrationBranch))
1083
+ return;
1084
+ // Pure fast-forward only: branch must be a strict ancestor of integration.
1085
+ // If the branch has its own commits ahead, leave it alone.
1086
+ if (!nativeIsAncestor(basePath, branch, integrationBranch)) {
1087
+ debugLog("createAutoWorktree", {
1088
+ phase: "skip-ff-branch-not-ancestor",
1089
+ milestoneId,
1090
+ branch,
1091
+ integration: integrationBranch,
1092
+ });
1093
+ return;
1094
+ }
1095
+ // Codex peer-review: `nativeUpdateRef` succeeds even when the branch is
1096
+ // currently checked out in another worktree, leaving that worktree's HEAD
1097
+ // inconsistent with its index/work tree. Skip the fast-forward if any
1098
+ // listed worktree has this branch checked out — the merge gate at
1099
+ // milestone-completion will surface stale-base divergence as a conflict
1100
+ // instead of silently corrupting the other worktree's state.
1101
+ if (_isBranchCheckedOutElsewhere(basePath, branch)) {
1102
+ debugLog("createAutoWorktree", {
1103
+ phase: "skip-ff-branch-checked-out-elsewhere",
1104
+ milestoneId,
1105
+ branch,
1106
+ });
1107
+ return;
1108
+ }
1109
+ nativeUpdateRef(basePath, `refs/heads/${branch}`, integrationBranch);
1110
+ debugLog("createAutoWorktree", {
1111
+ phase: "fast-forward-reused-branch",
1112
+ milestoneId,
1113
+ branch,
1114
+ integration: integrationBranch,
1115
+ });
1116
+ }
1117
+ catch (err) {
1118
+ debugLog("createAutoWorktree", {
1119
+ phase: "fast-forward-reused-branch-failed",
1120
+ milestoneId,
1121
+ branch,
1122
+ error: err instanceof Error ? err.message : String(err),
1123
+ });
1124
+ }
1125
+ }
1021
1126
  export function createAutoWorktree(basePath, milestoneId) {
1022
1127
  basePath = resolveWorktreeProjectRoot(basePath);
1023
1128
  // Check if repo has commits — git worktree requires a valid HEAD
@@ -1035,6 +1140,11 @@ export function createAutoWorktree(basePath, milestoneId) {
1035
1140
  const branchExists = nativeBranchExists(basePath, branch);
1036
1141
  let info;
1037
1142
  if (branchExists) {
1143
+ // #5538-followup: fast-forward the reused branch onto the integration
1144
+ // branch when safe so the next milestone forks from up-to-date code.
1145
+ // Without this, a milestone that was created before another milestone
1146
+ // merged into main would carry a stale base into its worktree.
1147
+ fastForwardReusedMilestoneBranchIfSafe(basePath, milestoneId, branch);
1038
1148
  // Re-attach worktree to the existing milestone branch (preserving commits)
1039
1149
  info = createWorktree(basePath, milestoneId, {
1040
1150
  branch,
@@ -45,6 +45,7 @@ import { readFileSync, existsSync, mkdirSync } from "node:fs";
45
45
  import { atomicWriteSync } from "./atomic-write.js";
46
46
  import { autoCommitCurrentBranch, captureIntegrationBranch, detectWorktreeName, getCurrentBranch, getMainBranch, setActiveMilestoneId, resolveProjectRoot, } from "./worktree.js";
47
47
  import { GitServiceImpl } from "./git-service.js";
48
+ import { nativeCheckoutBranch } from "./native-git-bridge.js";
48
49
  import { getPriorSliceCompletionBlocker } from "./dispatch-guard.js";
49
50
  import { createAutoWorktree, enterAutoWorktree, enterBranchModeForMilestone, teardownAutoWorktree, isInAutoWorktree, getAutoWorktreePath, mergeMilestoneToMain, autoWorktreeBranch, syncWorktreeStateBack, syncProjectRootToWorktree, checkResourcesStale, escapeStaleWorktree, } from "./auto-worktree.js";
50
51
  import { pruneQueueOrder } from "./queue-order.js";
@@ -854,6 +855,21 @@ export async function stopAuto(ctx, pi, reason) {
854
855
  catch (e) {
855
856
  debugLog("stop-cleanup-basepath", { error: e instanceof Error ? e.message : String(e) });
856
857
  }
858
+ // Re-root the active command session/tool runtime after worktree teardown.
859
+ // mergeAndExit restores process.cwd(), but AgentSession has already captured
860
+ // its own cwd for tools and system prompt; refresh it before returning to the
861
+ // user so follow-up commands do not target a removed milestone worktree.
862
+ if (s.originalBasePath && ctx && s.cmdCtx) {
863
+ try {
864
+ const result = await s.cmdCtx.newSession({ workspaceRoot: s.basePath });
865
+ if (result.cancelled) {
866
+ logWarning("engine", "post-stop session re-root was cancelled", { file: "auto.ts", basePath: s.basePath });
867
+ }
868
+ }
869
+ catch (err) {
870
+ logWarning("engine", `post-stop session re-root failed: ${err instanceof Error ? err.message : String(err)}`, { file: "auto.ts", basePath: s.basePath });
871
+ }
872
+ }
857
873
  // ── Step 8: Ledger notification ──
858
874
  try {
859
875
  const ledger = getLedger();
@@ -1102,6 +1118,7 @@ function buildResolverDeps() {
1102
1118
  getAutoWorktreePath,
1103
1119
  autoCommitCurrentBranch,
1104
1120
  getCurrentBranch,
1121
+ checkoutBranch: nativeCheckoutBranch,
1105
1122
  autoWorktreeBranch,
1106
1123
  resolveMilestoneFile,
1107
1124
  readFileSync: (path, encoding) => readFileSync(path, encoding),
@@ -1812,28 +1829,7 @@ export async function dispatchHookUnit(ctx, pi, hookName, triggerUnitType, trigg
1812
1829
  id: triggerUnitId,
1813
1830
  startedAt: hookStartedAt,
1814
1831
  };
1815
- // Ensure cwd matches basePath BEFORE newSession() captures it (#1389).
1816
- // newSession() snapshots process.cwd() during construction; chdir-ing
1817
- // afterward leaves the session rooted to whatever cwd was when the call
1818
- // was made. Must be synchronous — no awaits between chdir and newSession.
1819
- try {
1820
- if (process.cwd() !== s.basePath)
1821
- process.chdir(s.basePath);
1822
- }
1823
- catch (err) {
1824
- const msg = `Failed to chdir before hook newSession (basePath: ${s.basePath}): ${err instanceof Error ? err.message : String(err)}`;
1825
- logWarning("engine", msg, { file: "auto.ts", basePath: s.basePath, error: err instanceof Error ? err.message : String(err) });
1826
- ctx.ui.notify(`${msg}. Cancelling hook dispatch to avoid running in the wrong directory.`, "error");
1827
- if (wasActive) {
1828
- s.basePath = previousBasePath;
1829
- s.currentUnit = previousCurrentUnit;
1830
- }
1831
- else {
1832
- s.reset();
1833
- }
1834
- return false;
1835
- }
1836
- const result = await s.cmdCtx.newSession();
1832
+ const result = await s.cmdCtx.newSession({ workspaceRoot: s.basePath });
1837
1833
  if (result.cancelled) {
1838
1834
  await stopAuto(ctx, pi);
1839
1835
  return false;
@@ -5,7 +5,7 @@ import { clearPathCache } from "../paths.js";
5
5
  import { getAutoDashboardData, getAutoModeStartModel, isAutoActive, pauseAuto, setCurrentDispatchedModelId } from "../auto.js";
6
6
  import { getNextFallbackModel, resolveModelWithFallbacksForUnit } from "../preferences.js";
7
7
  import { pauseAutoForProviderError } from "../provider-error-pause.js";
8
- import { isSessionSwitchInFlight, resolveAgentEnd, resolveAgentEndCancelled } from "../auto/resolve.js";
8
+ import { isSessionSwitchAbortGraceActive, isSessionSwitchInFlight, resolveAgentEnd, resolveAgentEndCancelled, } from "../auto/resolve.js";
9
9
  import { resolveModelId } from "../auto-model-selection.js";
10
10
  import { resolveProjectRoot } from "../worktree.js";
11
11
  import { clearDiscussionFlowState } from "./write-gate.js";
@@ -53,6 +53,84 @@ export function isUserInitiatedAbortMessage(message) {
53
53
  return false;
54
54
  return /\b(?:claude code process aborted by user|request aborted by user|process aborted by user)\b/i.test(message);
55
55
  }
56
+ function isBareClaudeCodeSessionSwitchAbortMarker(message) {
57
+ if (!message)
58
+ return false;
59
+ const normalized = message.trim().replace(/\s+/g, " ").toLowerCase();
60
+ return normalized === "claude code process aborted by user"
61
+ || normalized === "request aborted by user"
62
+ || normalized === "process aborted by user"
63
+ || normalized === "claude code stream aborted by caller";
64
+ }
65
+ function readAssistantTextContent(content) {
66
+ if (!Array.isArray(content))
67
+ return "";
68
+ return content
69
+ .map((block) => {
70
+ if (!block || typeof block !== "object")
71
+ return "";
72
+ const text = block.text;
73
+ return typeof text === "string" ? text : "";
74
+ })
75
+ .filter(Boolean)
76
+ .join("\n");
77
+ }
78
+ export function isClaudeCodeSessionSwitchAbortMessage(lastMsg) {
79
+ if (!lastMsg || typeof lastMsg !== "object")
80
+ return false;
81
+ const m = lastMsg;
82
+ const carriers = [
83
+ m.errorMessage ? String(m.errorMessage) : "",
84
+ readAssistantTextContent(m.content),
85
+ ].filter((value) => value.trim().length > 0);
86
+ if ((m.stopReason === "error" || m.stopReason === "aborted") && carriers.length > 0) {
87
+ return carriers.every(isBareClaudeCodeSessionSwitchAbortMarker);
88
+ }
89
+ return false;
90
+ }
91
+ /**
92
+ * Resolve an agent_end event observed while a session switch is in flight.
93
+ *
94
+ * #5538-followup: When `newSession()` aborts an in-flight stream as part of a
95
+ * session transition (run-unit.ts:63 → _settleCurrentTurnForSessionTransition
96
+ * → agent.abort()), the SDK emits "Claude Code process aborted by user" or
97
+ * "Request aborted by user" against the previous unit's turn. The previous
98
+ * code path treated that as a user cancellation and propagated it to the next
99
+ * unit via the pending-switch-cancellation queue, killing auto-mode with
100
+ * "Auto-mode stopped — Unit aborted: Claude Code process aborted by user"
101
+ * even though no user input occurred.
102
+ *
103
+ * Claude Code abort markers are intentionally ignored when the abort fires
104
+ * while the session-switch is in flight: the abort is the expected side-effect
105
+ * of the transition, not a user signal. Other branches (genuine `stopReason
106
+ * === "aborted"` with diagnostic content/errorMessage) preserve the prior
107
+ * behavior.
108
+ */
109
+ export function _handleSessionSwitchAgentEnd(lastMsg, resolveCancelled) {
110
+ if (!lastMsg || typeof lastMsg !== "object")
111
+ return;
112
+ const m = lastMsg;
113
+ if (isClaudeCodeSessionSwitchAbortMessage(m)) {
114
+ // Internal abort from in-flight session transition — drop on the floor.
115
+ return;
116
+ }
117
+ if (m.stopReason === "error") {
118
+ const rawErrorMsg = m.errorMessage ? String(m.errorMessage) : "";
119
+ if (isBareClaudeCodeSessionSwitchAbortMarker(rawErrorMsg)) {
120
+ // Internal abort from in-flight session transition — drop on the floor.
121
+ return;
122
+ }
123
+ return;
124
+ }
125
+ if (m.stopReason === "aborted") {
126
+ const content = m.content;
127
+ const hasEmptyContent = Array.isArray(content) && content.length === 0;
128
+ const hasErrorMessage = !!m.errorMessage;
129
+ if (!hasEmptyContent || hasErrorMessage) {
130
+ resolveCancelled(_buildAbortedPauseContext(m));
131
+ }
132
+ }
133
+ }
56
134
  async function pauseTransientWithBackoff(cls, pi, ctx, errorDetail, isRateLimit) {
57
135
  retryState.consecutiveTransientCount += 1;
58
136
  const baseRetryAfterMs = "retryAfterMs" in cls ? cls.retryAfterMs : 15_000;
@@ -120,24 +198,13 @@ export async function handleAgentEnd(pi, event, ctx) {
120
198
  return;
121
199
  const lastMsg = event.messages[event.messages.length - 1];
122
200
  if (isSessionSwitchInFlight()) {
123
- if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "error") {
124
- const rawErrorMsg = ("errorMessage" in lastMsg && lastMsg.errorMessage) ? String(lastMsg.errorMessage) : "";
125
- if (isUserInitiatedAbortMessage(rawErrorMsg)) {
126
- resolveAgentEndCancelled({
127
- message: rawErrorMsg,
128
- category: "aborted",
129
- isTransient: false,
130
- });
131
- }
132
- }
133
- else if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "aborted") {
134
- const content = "content" in lastMsg ? lastMsg.content : undefined;
135
- const hasEmptyContent = Array.isArray(content) && content.length === 0;
136
- const hasErrorMessage = "errorMessage" in lastMsg && !!lastMsg.errorMessage;
137
- if (!hasEmptyContent || hasErrorMessage) {
138
- resolveAgentEndCancelled(_buildAbortedPauseContext(lastMsg));
139
- }
140
- }
201
+ _handleSessionSwitchAgentEnd(lastMsg, resolveAgentEndCancelled);
202
+ return;
203
+ }
204
+ if (isSessionSwitchAbortGraceActive() && isClaudeCodeSessionSwitchAbortMessage(lastMsg)) {
205
+ // Claude Code can report the abort from `newSession()` a few hundred ms
206
+ // after the guard drops. That event belongs to the old turn; do not let it
207
+ // cancel the freshly-dispatched unit.
141
208
  return;
142
209
  }
143
210
  if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "aborted") {
@@ -11,6 +11,12 @@ import { incrementLegacyTelemetry } from "../legacy-telemetry.js";
11
11
  async function loadWorkflowExecutors() {
12
12
  return import("../tools/workflow-tool-executors.js");
13
13
  }
14
+ function toolWorkspaceRoot(ctx) {
15
+ if (ctx && typeof ctx === "object" && typeof ctx.cwd === "string") {
16
+ return ctx.cwd;
17
+ }
18
+ return process.cwd();
19
+ }
14
20
  /**
15
21
  * Register an alias tool that shares the same execute function as its canonical counterpart.
16
22
  * The alias description and promptGuidelines direct the LLM to prefer the canonical name.
@@ -31,8 +37,8 @@ function registerAlias(pi, toolDef, aliasName, canonicalName) {
31
37
  execute,
32
38
  });
33
39
  }
34
- function requirementRootWriteGuard(operation) {
35
- const guard = shouldBlockRootArtifactSaveInSnapshot(loadWriteGateSnapshot(process.cwd()), "REQUIREMENTS");
40
+ function requirementRootWriteGuard(operation, basePath) {
41
+ const guard = shouldBlockRootArtifactSaveInSnapshot(loadWriteGateSnapshot(basePath), "REQUIREMENTS");
36
42
  if (!guard.block)
37
43
  return null;
38
44
  return {
@@ -56,7 +62,8 @@ function readDetails(result) {
56
62
  export function registerDbTools(pi) {
57
63
  // ─── gsd_decision_save (formerly gsd_save_decision) ─────────────────────
58
64
  const decisionSaveExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
59
- const dbAvailable = await ensureDbOpen();
65
+ const basePath = toolWorkspaceRoot(_ctx);
66
+ const dbAvailable = await ensureDbOpen(basePath);
60
67
  if (!dbAvailable) {
61
68
  return {
62
69
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot save decision." }],
@@ -73,7 +80,7 @@ export function registerDbTools(pi) {
73
80
  revisable: params.revisable,
74
81
  when_context: params.when_context,
75
82
  made_by: params.made_by,
76
- }, process.cwd());
83
+ }, basePath);
77
84
  return {
78
85
  content: [{ type: "text", text: `Saved decision ${id}` }],
79
86
  details: { operation: "save_decision", id },
@@ -140,10 +147,11 @@ export function registerDbTools(pi) {
140
147
  registerAlias(pi, decisionSaveTool, "gsd_save_decision", "gsd_decision_save");
141
148
  // ─── gsd_requirement_update (formerly gsd_update_requirement) ───────────
142
149
  const requirementUpdateExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
143
- const gateBlock = requirementRootWriteGuard("update_requirement");
150
+ const basePath = toolWorkspaceRoot(_ctx);
151
+ const gateBlock = requirementRootWriteGuard("update_requirement", basePath);
144
152
  if (gateBlock)
145
153
  return gateBlock;
146
- const dbAvailable = await ensureDbOpen();
154
+ const dbAvailable = await ensureDbOpen(basePath);
147
155
  if (!dbAvailable) {
148
156
  return {
149
157
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot update requirement." }],
@@ -165,7 +173,7 @@ export function registerDbTools(pi) {
165
173
  updates.primary_owner = params.primary_owner;
166
174
  if (params.supporting_slices !== undefined)
167
175
  updates.supporting_slices = params.supporting_slices;
168
- await updateRequirementInDb(params.id, updates, process.cwd());
176
+ await updateRequirementInDb(params.id, updates, basePath);
169
177
  return {
170
178
  content: [{ type: "text", text: `Updated requirement ${params.id}` }],
171
179
  details: { operation: "update_requirement", id: params.id },
@@ -225,10 +233,11 @@ export function registerDbTools(pi) {
225
233
  registerAlias(pi, requirementUpdateTool, "gsd_update_requirement", "gsd_requirement_update");
226
234
  // ─── gsd_requirement_save ─────────────────────────────────────────────
227
235
  const requirementSaveExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
228
- const gateBlock = requirementRootWriteGuard("save_requirement");
236
+ const basePath = toolWorkspaceRoot(_ctx);
237
+ const gateBlock = requirementRootWriteGuard("save_requirement", basePath);
229
238
  if (gateBlock)
230
239
  return gateBlock;
231
- const dbAvailable = await ensureDbOpen();
240
+ const dbAvailable = await ensureDbOpen(basePath);
232
241
  if (!dbAvailable) {
233
242
  return {
234
243
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot save requirement." }],
@@ -247,7 +256,7 @@ export function registerDbTools(pi) {
247
256
  supporting_slices: params.supporting_slices,
248
257
  validation: params.validation,
249
258
  notes: params.notes,
250
- }, process.cwd());
259
+ }, basePath);
251
260
  return {
252
261
  content: [{ type: "text", text: `Saved requirement ${result.id}` }],
253
262
  details: { operation: "save_requirement", id: result.id },
@@ -324,7 +333,7 @@ export function registerDbTools(pi) {
324
333
  // ─── gsd_summary_save (formerly gsd_save_summary) ──────────────────────
325
334
  const summarySaveExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
326
335
  const { executeSummarySave } = await loadWorkflowExecutors();
327
- return executeSummarySave(params, process.cwd());
336
+ return executeSummarySave(params, toolWorkspaceRoot(_ctx));
328
337
  };
329
338
  const summarySaveTool = {
330
339
  name: "gsd_summary_save",
@@ -373,23 +382,23 @@ export function registerDbTools(pi) {
373
382
  // ─── gsd_milestone_generate_id (formerly gsd_generate_milestone_id) ────
374
383
  const milestoneGenerateIdExecute = async (_toolCallId, _params, _signal, _onUpdate, _ctx) => {
375
384
  try {
385
+ const basePath = toolWorkspaceRoot(_ctx);
376
386
  // Claim a reserved ID if the guided-flow already previewed one to the user.
377
387
  // This guarantees the ID shown in the UI matches the one materialised on disk.
378
388
  const { claimReservedId, findMilestoneIds, getReservedMilestoneIds, nextMilestoneId } = await import("../guided-flow.js");
379
389
  const reserved = claimReservedId();
380
390
  if (reserved) {
381
- await ensureMilestoneDbRow(reserved);
391
+ await ensureMilestoneDbRow(reserved, basePath);
382
392
  return {
383
393
  content: [{ type: "text", text: reserved }],
384
394
  details: { operation: "generate_milestone_id", id: reserved, source: "reserved" },
385
395
  };
386
396
  }
387
- const basePath = process.cwd();
388
397
  const existingIds = findMilestoneIds(basePath);
389
- const uniqueEnabled = !!loadEffectiveGSDPreferences()?.preferences?.unique_milestone_ids;
398
+ const uniqueEnabled = !!loadEffectiveGSDPreferences(basePath)?.preferences?.unique_milestone_ids;
390
399
  const allIds = [...new Set([...existingIds, ...getReservedMilestoneIds()])];
391
400
  const newId = nextMilestoneId(allIds, uniqueEnabled);
392
- await ensureMilestoneDbRow(newId);
401
+ await ensureMilestoneDbRow(newId, basePath);
393
402
  return {
394
403
  content: [{ type: "text", text: newId }],
395
404
  details: { operation: "generate_milestone_id", id: newId, existingCount: existingIds.length, uniqueEnabled },
@@ -409,8 +418,8 @@ export function registerDbTools(pi) {
409
418
  * later writes the full row. Silently skips if the DB isn't available yet
410
419
  * (pre-migration).
411
420
  */
412
- async function ensureMilestoneDbRow(milestoneId) {
413
- const dbAvailable = await ensureDbOpen();
421
+ async function ensureMilestoneDbRow(milestoneId, basePath) {
422
+ const dbAvailable = await ensureDbOpen(basePath);
414
423
  if (!dbAvailable)
415
424
  return;
416
425
  try {
@@ -455,7 +464,7 @@ export function registerDbTools(pi) {
455
464
  // ─── gsd_plan_milestone (gsd_milestone_plan alias) ─────────────────────
456
465
  const planMilestoneExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
457
466
  const { executePlanMilestone } = await loadWorkflowExecutors();
458
- return executePlanMilestone(params, process.cwd());
467
+ return executePlanMilestone(params, toolWorkspaceRoot(_ctx));
459
468
  };
460
469
  const planMilestoneTool = {
461
470
  name: "gsd_plan_milestone",
@@ -520,7 +529,7 @@ export function registerDbTools(pi) {
520
529
  // ─── gsd_plan_slice (gsd_slice_plan alias) ─────────────────────────────
521
530
  const planSliceExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
522
531
  const { executePlanSlice } = await loadWorkflowExecutors();
523
- return executePlanSlice(params, process.cwd());
532
+ return executePlanSlice(params, toolWorkspaceRoot(_ctx));
524
533
  };
525
534
  const planSliceTool = {
526
535
  name: "gsd_plan_slice",
@@ -564,7 +573,8 @@ export function registerDbTools(pi) {
564
573
  registerAlias(pi, planSliceTool, "gsd_slice_plan", "gsd_plan_slice");
565
574
  // ─── gsd_plan_task (gsd_task_plan alias) ───────────────────────────────
566
575
  const planTaskExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
567
- const dbAvailable = await ensureDbOpen();
576
+ const basePath = toolWorkspaceRoot(_ctx);
577
+ const dbAvailable = await ensureDbOpen(basePath);
568
578
  if (!dbAvailable) {
569
579
  return {
570
580
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot plan task." }],
@@ -573,7 +583,7 @@ export function registerDbTools(pi) {
573
583
  }
574
584
  try {
575
585
  const { handlePlanTask } = await import("../tools/plan-task.js");
576
- const result = await handlePlanTask(params, process.cwd());
586
+ const result = await handlePlanTask(params, basePath);
577
587
  if ("error" in result) {
578
588
  return {
579
589
  content: [{ type: "text", text: `Error planning task: ${result.error}` }],
@@ -634,7 +644,7 @@ export function registerDbTools(pi) {
634
644
  // ─── gsd_task_complete (gsd_complete_task alias) ────────────────────────
635
645
  const taskCompleteExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
636
646
  const { executeTaskComplete } = await loadWorkflowExecutors();
637
- return executeTaskComplete(params, process.cwd());
647
+ return executeTaskComplete(params, toolWorkspaceRoot(_ctx));
638
648
  };
639
649
  const taskCompleteTool = {
640
650
  name: "gsd_task_complete",
@@ -697,7 +707,7 @@ export function registerDbTools(pi) {
697
707
  // ─── gsd_slice_complete (gsd_complete_slice alias) ─────────────────────
698
708
  const sliceCompleteExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
699
709
  const { executeSliceComplete } = await loadWorkflowExecutors();
700
- return executeSliceComplete(params, process.cwd());
710
+ return executeSliceComplete(params, toolWorkspaceRoot(_ctx));
701
711
  };
702
712
  const sliceCompleteTool = {
703
713
  name: "gsd_slice_complete",
@@ -777,7 +787,8 @@ export function registerDbTools(pi) {
777
787
  registerAlias(pi, sliceCompleteTool, "gsd_complete_slice", "gsd_slice_complete");
778
788
  // ─── gsd_skip_slice (#3477 / #3487) ───────────────────────────────────
779
789
  const skipSliceExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
780
- const dbAvailable = await ensureDbOpen();
790
+ const basePath = toolWorkspaceRoot(_ctx);
791
+ const dbAvailable = await ensureDbOpen(basePath);
781
792
  if (!dbAvailable) {
782
793
  return {
783
794
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot skip slice." }],
@@ -806,7 +817,6 @@ export function registerDbTools(pi) {
806
817
  // Rebuild STATE.md so it reflects the skip immediately (#3477).
807
818
  // Without this, /gsd auto reads stale STATE.md and resumes the skipped slice.
808
819
  try {
809
- const basePath = process.cwd();
810
820
  const { rebuildState } = await import("../doctor.js");
811
821
  await rebuildState(basePath);
812
822
  }
@@ -862,7 +872,7 @@ export function registerDbTools(pi) {
862
872
  // ─── gsd_complete_milestone ────────────────────────────────────────────
863
873
  const milestoneCompleteExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
864
874
  const { executeCompleteMilestone } = await loadWorkflowExecutors();
865
- return executeCompleteMilestone(params, process.cwd());
875
+ return executeCompleteMilestone(params, toolWorkspaceRoot(_ctx));
866
876
  };
867
877
  const milestoneCompleteTool = {
868
878
  name: "gsd_complete_milestone",
@@ -903,7 +913,7 @@ export function registerDbTools(pi) {
903
913
  // ─── gsd_validate_milestone (gsd_milestone_validate alias) ─────────────
904
914
  const milestoneValidateExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
905
915
  const { executeValidateMilestone } = await loadWorkflowExecutors();
906
- return executeValidateMilestone(params, process.cwd());
916
+ return executeValidateMilestone(params, toolWorkspaceRoot(_ctx));
907
917
  };
908
918
  const milestoneValidateTool = {
909
919
  name: "gsd_validate_milestone",
@@ -936,7 +946,7 @@ export function registerDbTools(pi) {
936
946
  // ─── gsd_replan_slice (gsd_slice_replan alias) ─────────────────────────
937
947
  const replanSliceExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
938
948
  const { executeReplanSlice } = await loadWorkflowExecutors();
939
- return executeReplanSlice(params, process.cwd());
949
+ return executeReplanSlice(params, toolWorkspaceRoot(_ctx));
940
950
  };
941
951
  const replanSliceTool = {
942
952
  name: "gsd_replan_slice",
@@ -979,7 +989,7 @@ export function registerDbTools(pi) {
979
989
  // ─── gsd_reassess_roadmap (gsd_roadmap_reassess alias) ─────────────────
980
990
  const reassessRoadmapExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
981
991
  const { executeReassessRoadmap } = await loadWorkflowExecutors();
982
- return executeReassessRoadmap(params, process.cwd());
992
+ return executeReassessRoadmap(params, toolWorkspaceRoot(_ctx));
983
993
  };
984
994
  const reassessRoadmapTool = {
985
995
  name: "gsd_reassess_roadmap",
@@ -1027,7 +1037,8 @@ export function registerDbTools(pi) {
1027
1037
  // ─── gsd_task_reopen (gsd_reopen_task alias) ───────────────────────────
1028
1038
  // Single-writer v3, Stream 3: reversibility tools for closed units.
1029
1039
  const reopenTaskExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
1030
- const dbAvailable = await ensureDbOpen();
1040
+ const basePath = toolWorkspaceRoot(_ctx);
1041
+ const dbAvailable = await ensureDbOpen(basePath);
1031
1042
  if (!dbAvailable) {
1032
1043
  return {
1033
1044
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot reopen task." }],
@@ -1036,7 +1047,7 @@ export function registerDbTools(pi) {
1036
1047
  }
1037
1048
  try {
1038
1049
  const { handleReopenTask } = await import("../tools/reopen-task.js");
1039
- const result = await handleReopenTask(params, process.cwd());
1050
+ const result = await handleReopenTask(params, basePath);
1040
1051
  if ("error" in result) {
1041
1052
  return {
1042
1053
  content: [{ type: "text", text: `Error reopening task: ${result.error}` }],
@@ -1089,7 +1100,8 @@ export function registerDbTools(pi) {
1089
1100
  registerAlias(pi, reopenTaskTool, "gsd_reopen_task", "gsd_task_reopen");
1090
1101
  // ─── gsd_slice_reopen (gsd_reopen_slice alias) ─────────────────────────
1091
1102
  const reopenSliceExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
1092
- const dbAvailable = await ensureDbOpen();
1103
+ const basePath = toolWorkspaceRoot(_ctx);
1104
+ const dbAvailable = await ensureDbOpen(basePath);
1093
1105
  if (!dbAvailable) {
1094
1106
  return {
1095
1107
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot reopen slice." }],
@@ -1098,7 +1110,7 @@ export function registerDbTools(pi) {
1098
1110
  }
1099
1111
  try {
1100
1112
  const { handleReopenSlice } = await import("../tools/reopen-slice.js");
1101
- const result = await handleReopenSlice(params, process.cwd());
1113
+ const result = await handleReopenSlice(params, basePath);
1102
1114
  if ("error" in result) {
1103
1115
  return {
1104
1116
  content: [{ type: "text", text: `Error reopening slice: ${result.error}` }],
@@ -1151,7 +1163,8 @@ export function registerDbTools(pi) {
1151
1163
  registerAlias(pi, reopenSliceTool, "gsd_reopen_slice", "gsd_slice_reopen");
1152
1164
  // ─── gsd_milestone_reopen (gsd_reopen_milestone alias) ─────────────────
1153
1165
  const reopenMilestoneExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
1154
- const dbAvailable = await ensureDbOpen();
1166
+ const basePath = toolWorkspaceRoot(_ctx);
1167
+ const dbAvailable = await ensureDbOpen(basePath);
1155
1168
  if (!dbAvailable) {
1156
1169
  return {
1157
1170
  content: [{ type: "text", text: "Error: GSD database is not available. Cannot reopen milestone." }],
@@ -1160,7 +1173,7 @@ export function registerDbTools(pi) {
1160
1173
  }
1161
1174
  try {
1162
1175
  const { handleReopenMilestone } = await import("../tools/reopen-milestone.js");
1163
- const result = await handleReopenMilestone(params, process.cwd());
1176
+ const result = await handleReopenMilestone(params, basePath);
1164
1177
  if ("error" in result) {
1165
1178
  return {
1166
1179
  content: [{ type: "text", text: `Error reopening milestone: ${result.error}` }],
@@ -1212,7 +1225,7 @@ export function registerDbTools(pi) {
1212
1225
  // ─── gsd_save_gate_result ──────────────────────────────────────────────
1213
1226
  const saveGateResultExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
1214
1227
  const { executeSaveGateResult } = await loadWorkflowExecutors();
1215
- return executeSaveGateResult(params, process.cwd());
1228
+ return executeSaveGateResult(params, toolWorkspaceRoot(_ctx));
1216
1229
  };
1217
1230
  const saveGateResultTool = {
1218
1231
  name: "gsd_save_gate_result",