gsd-pi 2.49.0-dev.de3d9f6 → 2.50.0-dev.9476db8

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 (249) hide show
  1. package/dist/headless-ui.js +12 -2
  2. package/dist/headless.js +29 -13
  3. package/dist/resources/extensions/gsd/auto/infra-errors.js +1 -0
  4. package/dist/resources/extensions/gsd/auto/phases.js +11 -11
  5. package/dist/resources/extensions/gsd/auto/resolve.js +2 -2
  6. package/dist/resources/extensions/gsd/auto/run-unit.js +2 -2
  7. package/dist/resources/extensions/gsd/auto/session.js +4 -0
  8. package/dist/resources/extensions/gsd/auto-artifact-paths.js +8 -10
  9. package/dist/resources/extensions/gsd/auto-dashboard.js +6 -3
  10. package/dist/resources/extensions/gsd/auto-dispatch.js +33 -21
  11. package/dist/resources/extensions/gsd/auto-post-unit.js +17 -24
  12. package/dist/resources/extensions/gsd/auto-prompts.js +102 -21
  13. package/dist/resources/extensions/gsd/auto-recovery.js +62 -184
  14. package/dist/resources/extensions/gsd/auto-start.js +4 -31
  15. package/dist/resources/extensions/gsd/auto-timers.js +2 -2
  16. package/dist/resources/extensions/gsd/auto-verification.js +4 -7
  17. package/dist/resources/extensions/gsd/auto-worktree.js +257 -113
  18. package/dist/resources/extensions/gsd/auto.js +7 -5
  19. package/dist/resources/extensions/gsd/bootstrap/db-tools.js +89 -0
  20. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +8 -1
  21. package/dist/resources/extensions/gsd/branch-patterns.js +13 -0
  22. package/dist/resources/extensions/gsd/doctor-checks.js +5 -1234
  23. package/dist/resources/extensions/gsd/doctor-engine-checks.js +168 -0
  24. package/dist/resources/extensions/gsd/doctor-environment.js +28 -7
  25. package/dist/resources/extensions/gsd/doctor-git-checks.js +405 -0
  26. package/dist/resources/extensions/gsd/doctor-global-checks.js +74 -0
  27. package/dist/resources/extensions/gsd/doctor-runtime-checks.js +600 -0
  28. package/dist/resources/extensions/gsd/doctor.js +9 -1
  29. package/dist/resources/extensions/gsd/extension-manifest.json +1 -1
  30. package/dist/resources/extensions/gsd/git-service.js +9 -10
  31. package/dist/resources/extensions/gsd/gsd-db.js +124 -1
  32. package/dist/resources/extensions/gsd/guided-flow-queue.js +10 -11
  33. package/dist/resources/extensions/gsd/markdown-renderer.js +33 -5
  34. package/dist/resources/extensions/gsd/preferences-types.js +2 -1
  35. package/dist/resources/extensions/gsd/preferences-validation.js +39 -0
  36. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +27 -8
  37. package/dist/resources/extensions/gsd/prompts/complete-slice.md +9 -8
  38. package/dist/resources/extensions/gsd/prompts/execute-task.md +16 -13
  39. package/dist/resources/extensions/gsd/prompts/forensics.md +12 -5
  40. package/dist/resources/extensions/gsd/prompts/gate-evaluate.md +32 -0
  41. package/dist/resources/extensions/gsd/prompts/guided-complete-slice.md +1 -1
  42. package/dist/resources/extensions/gsd/prompts/guided-execute-task.md +1 -1
  43. package/dist/resources/extensions/gsd/prompts/guided-plan-milestone.md +1 -1
  44. package/dist/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -1
  45. package/dist/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
  46. package/dist/resources/extensions/gsd/prompts/plan-slice.md +8 -3
  47. package/dist/resources/extensions/gsd/prompts/reassess-roadmap.md +3 -0
  48. package/dist/resources/extensions/gsd/prompts/replan-slice.md +1 -1
  49. package/dist/resources/extensions/gsd/repo-identity.js +29 -0
  50. package/dist/resources/extensions/gsd/roadmap-slices.js +2 -2
  51. package/dist/resources/extensions/gsd/session-forensics.js +6 -11
  52. package/dist/resources/extensions/gsd/session-lock.js +67 -56
  53. package/dist/resources/extensions/gsd/state.js +34 -7
  54. package/dist/resources/extensions/gsd/templates/milestone-summary.md +8 -0
  55. package/dist/resources/extensions/gsd/templates/plan.md +16 -0
  56. package/dist/resources/extensions/gsd/templates/roadmap.md +13 -0
  57. package/dist/resources/extensions/gsd/templates/slice-summary.md +9 -0
  58. package/dist/resources/extensions/gsd/templates/task-plan.md +24 -0
  59. package/dist/resources/extensions/gsd/tools/plan-slice.js +14 -1
  60. package/dist/resources/extensions/gsd/tools/validate-milestone.js +3 -3
  61. package/dist/resources/extensions/gsd/verdict-parser.js +84 -0
  62. package/dist/resources/extensions/gsd/worktree-resolver.js +24 -0
  63. package/dist/resources/extensions/gsd/worktree.js +3 -2
  64. package/dist/resources/extensions/remote-questions/config.js +3 -5
  65. package/dist/resources/extensions/search-the-web/native-search.js +8 -3
  66. package/dist/resources/extensions/search-the-web/tool-search.js +19 -2
  67. package/dist/resources/skills/github-workflows/references/gh/SKILL.md +22 -1
  68. package/dist/web/standalone/.next/BUILD_ID +1 -1
  69. package/dist/web/standalone/.next/app-path-routes-manifest.json +15 -15
  70. package/dist/web/standalone/.next/build-manifest.json +3 -3
  71. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  72. package/dist/web/standalone/.next/react-loadable-manifest.json +1 -1
  73. package/dist/web/standalone/.next/required-server-files.json +1 -1
  74. package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
  75. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  76. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  77. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  78. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  79. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  80. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  81. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  82. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  83. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  84. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  85. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  86. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  87. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  88. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  89. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  90. package/dist/web/standalone/.next/server/app/index.html +1 -1
  91. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  92. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  93. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  94. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  95. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  96. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  97. package/dist/web/standalone/.next/server/app-paths-manifest.json +15 -15
  98. package/dist/web/standalone/.next/server/chunks/229.js +2 -2
  99. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  100. package/dist/web/standalone/.next/server/middleware-react-loadable-manifest.js +1 -1
  101. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  102. package/dist/web/standalone/.next/server/pages/500.html +2 -2
  103. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  104. package/dist/web/standalone/.next/static/chunks/4024.7c75ac378de0f2b5.js +9 -0
  105. package/dist/web/standalone/.next/static/chunks/{webpack-0a4cd455ec4197d2.js → webpack-2473ce2c3879fff4.js} +1 -1
  106. package/dist/web/standalone/server.js +1 -1
  107. package/package.json +1 -1
  108. package/packages/pi-agent-core/dist/agent-loop.d.ts.map +1 -1
  109. package/packages/pi-agent-core/dist/agent-loop.js +4 -1
  110. package/packages/pi-agent-core/dist/agent-loop.js.map +1 -1
  111. package/packages/pi-agent-core/src/agent-loop.ts +4 -1
  112. package/packages/pi-ai/dist/providers/openai-codex-responses.js +39 -10
  113. package/packages/pi-ai/dist/providers/openai-codex-responses.js.map +1 -1
  114. package/packages/pi-ai/src/providers/openai-codex-responses.ts +39 -8
  115. package/packages/pi-coding-agent/dist/core/blob-store.d.ts.map +1 -1
  116. package/packages/pi-coding-agent/dist/core/blob-store.js +8 -3
  117. package/packages/pi-coding-agent/dist/core/blob-store.js.map +1 -1
  118. package/packages/pi-coding-agent/dist/core/discovery-cache.d.ts.map +1 -1
  119. package/packages/pi-coding-agent/dist/core/discovery-cache.js +9 -2
  120. package/packages/pi-coding-agent/dist/core/discovery-cache.js.map +1 -1
  121. package/packages/pi-coding-agent/dist/core/retry-handler.js +1 -1
  122. package/packages/pi-coding-agent/dist/core/retry-handler.js.map +1 -1
  123. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  124. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.js +7 -32
  125. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.js.map +1 -1
  126. package/packages/pi-coding-agent/dist/modes/rpc/jsonl.d.ts.map +1 -1
  127. package/packages/pi-coding-agent/dist/modes/rpc/jsonl.js +5 -0
  128. package/packages/pi-coding-agent/dist/modes/rpc/jsonl.js.map +1 -1
  129. package/packages/pi-coding-agent/dist/modes/rpc/rpc-client.d.ts.map +1 -1
  130. package/packages/pi-coding-agent/dist/modes/rpc/rpc-client.js +0 -1
  131. package/packages/pi-coding-agent/dist/modes/rpc/rpc-client.js.map +1 -1
  132. package/packages/pi-coding-agent/dist/modes/rpc/rpc-mode.js +1 -1
  133. package/packages/pi-coding-agent/dist/modes/rpc/rpc-mode.js.map +1 -1
  134. package/packages/pi-coding-agent/package.json +1 -1
  135. package/packages/pi-coding-agent/src/core/blob-store.ts +6 -3
  136. package/packages/pi-coding-agent/src/core/discovery-cache.ts +9 -2
  137. package/packages/pi-coding-agent/src/core/retry-handler.ts +1 -1
  138. package/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts +7 -32
  139. package/packages/pi-coding-agent/src/modes/rpc/jsonl.ts +6 -0
  140. package/packages/pi-coding-agent/src/modes/rpc/rpc-client.ts +0 -2
  141. package/packages/pi-coding-agent/src/modes/rpc/rpc-mode.ts +2 -2
  142. package/pkg/package.json +1 -1
  143. package/src/resources/extensions/gsd/auto/infra-errors.ts +1 -0
  144. package/src/resources/extensions/gsd/auto/phases.ts +10 -11
  145. package/src/resources/extensions/gsd/auto/resolve.ts +3 -3
  146. package/src/resources/extensions/gsd/auto/run-unit.ts +2 -2
  147. package/src/resources/extensions/gsd/auto/session.ts +5 -0
  148. package/src/resources/extensions/gsd/auto/types.ts +13 -0
  149. package/src/resources/extensions/gsd/auto-artifact-paths.ts +19 -21
  150. package/src/resources/extensions/gsd/auto-dashboard.ts +5 -2
  151. package/src/resources/extensions/gsd/auto-dispatch.ts +39 -21
  152. package/src/resources/extensions/gsd/auto-loop.ts +1 -1
  153. package/src/resources/extensions/gsd/auto-post-unit.ts +18 -28
  154. package/src/resources/extensions/gsd/auto-prompts.ts +113 -19
  155. package/src/resources/extensions/gsd/auto-recovery.ts +65 -199
  156. package/src/resources/extensions/gsd/auto-start.ts +7 -27
  157. package/src/resources/extensions/gsd/auto-timers.ts +2 -2
  158. package/src/resources/extensions/gsd/auto-verification.ts +4 -7
  159. package/src/resources/extensions/gsd/auto-worktree.ts +305 -108
  160. package/src/resources/extensions/gsd/auto.ts +11 -10
  161. package/src/resources/extensions/gsd/bootstrap/db-tools.ts +93 -0
  162. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +8 -0
  163. package/src/resources/extensions/gsd/branch-patterns.ts +16 -0
  164. package/src/resources/extensions/gsd/doctor-checks.ts +5 -1291
  165. package/src/resources/extensions/gsd/doctor-engine-checks.ts +182 -0
  166. package/src/resources/extensions/gsd/doctor-environment.ts +30 -7
  167. package/src/resources/extensions/gsd/doctor-git-checks.ts +415 -0
  168. package/src/resources/extensions/gsd/doctor-global-checks.ts +84 -0
  169. package/src/resources/extensions/gsd/doctor-runtime-checks.ts +626 -0
  170. package/src/resources/extensions/gsd/doctor.ts +9 -1
  171. package/src/resources/extensions/gsd/extension-manifest.json +1 -1
  172. package/src/resources/extensions/gsd/git-service.ts +7 -15
  173. package/src/resources/extensions/gsd/gsd-db.ts +150 -2
  174. package/src/resources/extensions/gsd/guided-flow-queue.ts +11 -12
  175. package/src/resources/extensions/gsd/markdown-renderer.ts +37 -4
  176. package/src/resources/extensions/gsd/preferences-types.ts +5 -1
  177. package/src/resources/extensions/gsd/preferences-validation.ts +37 -0
  178. package/src/resources/extensions/gsd/prompts/complete-milestone.md +27 -8
  179. package/src/resources/extensions/gsd/prompts/complete-slice.md +9 -8
  180. package/src/resources/extensions/gsd/prompts/execute-task.md +16 -13
  181. package/src/resources/extensions/gsd/prompts/forensics.md +12 -5
  182. package/src/resources/extensions/gsd/prompts/gate-evaluate.md +32 -0
  183. package/src/resources/extensions/gsd/prompts/guided-complete-slice.md +1 -1
  184. package/src/resources/extensions/gsd/prompts/guided-execute-task.md +1 -1
  185. package/src/resources/extensions/gsd/prompts/guided-plan-milestone.md +1 -1
  186. package/src/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -1
  187. package/src/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
  188. package/src/resources/extensions/gsd/prompts/plan-slice.md +8 -3
  189. package/src/resources/extensions/gsd/prompts/reassess-roadmap.md +3 -0
  190. package/src/resources/extensions/gsd/prompts/replan-slice.md +1 -1
  191. package/src/resources/extensions/gsd/repo-identity.ts +28 -0
  192. package/src/resources/extensions/gsd/roadmap-slices.ts +2 -2
  193. package/src/resources/extensions/gsd/session-forensics.ts +6 -11
  194. package/src/resources/extensions/gsd/session-lock.ts +92 -64
  195. package/src/resources/extensions/gsd/state.ts +38 -5
  196. package/src/resources/extensions/gsd/templates/milestone-summary.md +8 -0
  197. package/src/resources/extensions/gsd/templates/plan.md +16 -0
  198. package/src/resources/extensions/gsd/templates/roadmap.md +13 -0
  199. package/src/resources/extensions/gsd/templates/slice-summary.md +9 -0
  200. package/src/resources/extensions/gsd/templates/task-plan.md +24 -0
  201. package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +2 -2
  202. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +35 -0
  203. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +1 -81
  204. package/src/resources/extensions/gsd/tests/complete-slice.test.ts +2 -2
  205. package/src/resources/extensions/gsd/tests/complete-task.test.ts +2 -2
  206. package/src/resources/extensions/gsd/tests/completed-units-metrics-sync.test.ts +9 -12
  207. package/src/resources/extensions/gsd/tests/doctor-environment.test.ts +115 -1
  208. package/src/resources/extensions/gsd/tests/doctor-fixlevel.test.ts +65 -1
  209. package/src/resources/extensions/gsd/tests/doctor-git.test.ts +50 -0
  210. package/src/resources/extensions/gsd/tests/gate-dispatch.test.ts +189 -0
  211. package/src/resources/extensions/gsd/tests/gate-storage.test.ts +156 -0
  212. package/src/resources/extensions/gsd/tests/git-service.test.ts +49 -0
  213. package/src/resources/extensions/gsd/tests/gsd-db.test.ts +1 -1
  214. package/src/resources/extensions/gsd/tests/infra-error.test.ts +12 -2
  215. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +39 -0
  216. package/src/resources/extensions/gsd/tests/md-importer.test.ts +1 -1
  217. package/src/resources/extensions/gsd/tests/memory-store.test.ts +2 -2
  218. package/src/resources/extensions/gsd/tests/quality-gates.test.ts +347 -0
  219. package/src/resources/extensions/gsd/tests/queue-completed-milestone-perf.test.ts +155 -0
  220. package/src/resources/extensions/gsd/tests/replan-slice.test.ts +2 -1
  221. package/src/resources/extensions/gsd/tests/repo-identity-worktree.test.ts +32 -0
  222. package/src/resources/extensions/gsd/tests/roadmap-slices.test.ts +26 -0
  223. package/src/resources/extensions/gsd/tests/run-uat.test.ts +20 -16
  224. package/src/resources/extensions/gsd/tests/session-lock-transient-read.test.ts +223 -0
  225. package/src/resources/extensions/gsd/tests/skill-activation.test.ts +44 -4
  226. package/src/resources/extensions/gsd/tests/tool-naming.test.ts +1 -1
  227. package/src/resources/extensions/gsd/tests/validate-milestone.test.ts +2 -1
  228. package/src/resources/extensions/gsd/tests/verification-gate.test.ts +0 -16
  229. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +67 -0
  230. package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +1 -1
  231. package/src/resources/extensions/gsd/tests/worktree-sync-overwrite-loop.test.ts +204 -0
  232. package/src/resources/extensions/gsd/tools/plan-slice.ts +16 -0
  233. package/src/resources/extensions/gsd/tools/validate-milestone.ts +3 -3
  234. package/src/resources/extensions/gsd/types.ts +30 -0
  235. package/src/resources/extensions/gsd/verdict-parser.ts +95 -0
  236. package/src/resources/extensions/gsd/verification-gate.ts +0 -2
  237. package/src/resources/extensions/gsd/worktree-resolver.ts +31 -0
  238. package/src/resources/extensions/gsd/worktree.ts +3 -2
  239. package/src/resources/extensions/remote-questions/config.ts +3 -5
  240. package/src/resources/extensions/search-the-web/native-search.ts +8 -3
  241. package/src/resources/extensions/search-the-web/tool-search.ts +22 -2
  242. package/src/resources/skills/github-workflows/references/gh/SKILL.md +22 -1
  243. package/dist/resources/extensions/gsd/auto-worktree-sync.js +0 -191
  244. package/dist/resources/extensions/gsd/resource-version.js +0 -97
  245. package/dist/web/standalone/.next/static/chunks/4024.11ca5c01938e5948.js +0 -9
  246. package/src/resources/extensions/gsd/auto-worktree-sync.ts +0 -234
  247. package/src/resources/extensions/gsd/resource-version.ts +0 -101
  248. /package/dist/web/standalone/.next/static/{ceckLbAMjhzHaQ3RPtJnT → MkE9kzqUGny3-cSE0GNnm}/_buildManifest.js +0 -0
  249. /package/dist/web/standalone/.next/static/{ceckLbAMjhzHaQ3RPtJnT → MkE9kzqUGny3-cSE0GNnm}/_ssgManifest.js +0 -0
@@ -6,7 +6,8 @@
6
6
  * manages create, enter, detect, and teardown for auto-mode worktrees.
7
7
  */
8
8
  import { existsSync, cpSync, readFileSync, readdirSync, mkdirSync, realpathSync, rmSync, unlinkSync, lstatSync as lstatSyncFn, } from "node:fs";
9
- import { isAbsolute, join } from "node:path";
9
+ import { isAbsolute, join, sep as pathSep } from "node:path";
10
+ import { homedir } from "node:os";
10
11
  import { GSDError, GSD_IO_ERROR, GSD_GIT_ERROR } from "./errors.js";
11
12
  import { reconcileWorktreeDb, isDbAvailable, getMilestone, getMilestoneSlices, } from "./gsd-db.js";
12
13
  import { atomicWriteSync } from "./atomic-write.js";
@@ -20,6 +21,35 @@ import { debugLog } from "./debug-logger.js";
20
21
  import { logWarning } from "./workflow-logger.js";
21
22
  import { loadEffectiveGSDPreferences } from "./preferences.js";
22
23
  import { nativeGetCurrentBranch, nativeDetectMainBranch, nativeWorkingTreeStatus, nativeAddAllWithExclusions, nativeCommit, nativeCheckoutBranch, nativeMergeSquash, nativeConflictFiles, nativeCheckoutTheirs, nativeAddPaths, nativeRmForce, nativeBranchDelete, nativeBranchExists, nativeDiffNumstat, nativeUpdateRef, nativeIsAncestor, } from "./native-git-bridge.js";
24
+ const gsdHome = process.env.GSD_HOME || join(homedir(), ".gsd");
25
+ // ─── Shared Constants & Helpers ─────────────────────────────────────────────
26
+ /**
27
+ * Root-level .gsd/ state files synced between worktree and project root.
28
+ * Single source of truth — used by syncGsdStateToWorktree, syncWorktreeStateBack,
29
+ * and the dispatch-level sync functions.
30
+ */
31
+ const ROOT_STATE_FILES = [
32
+ "DECISIONS.md",
33
+ "REQUIREMENTS.md",
34
+ "PROJECT.md",
35
+ "KNOWLEDGE.md",
36
+ "OVERRIDES.md",
37
+ "QUEUE.md",
38
+ "completed-units.json",
39
+ "metrics.json",
40
+ ];
41
+ /**
42
+ * Check if two filesystem paths resolve to the same real location.
43
+ * Returns false if either path cannot be resolved (e.g. doesn't exist).
44
+ */
45
+ function isSamePath(a, b) {
46
+ try {
47
+ return realpathSync(a) === realpathSync(b);
48
+ }
49
+ catch {
50
+ return false;
51
+ }
52
+ }
23
53
  // ─── Module State ──────────────────────────────────────────────────────────
24
54
  /** Original project root before chdir into auto-worktree. */
25
55
  let originalBase = null;
@@ -70,6 +100,187 @@ function clearProjectRootStateFiles(basePath, milestoneId) {
70
100
  }
71
101
  }
72
102
  }
103
+ // ─── Dispatch-Level Sync (project root ↔ worktree) ──────────────────────────
104
+ /**
105
+ * Sync milestone artifacts from project root INTO worktree before deriveState.
106
+ * Covers the case where the LLM wrote artifacts to the main repo filesystem
107
+ * (e.g. via absolute paths) but the worktree has stale data. Also deletes
108
+ * gsd.db in the worktree so it rebuilds from fresh disk state (#853).
109
+ * Non-fatal — sync failure should never block dispatch.
110
+ */
111
+ export function syncProjectRootToWorktree(projectRoot, worktreePath_, milestoneId) {
112
+ if (!worktreePath_ || !projectRoot || worktreePath_ === projectRoot)
113
+ return;
114
+ if (!milestoneId)
115
+ return;
116
+ const prGsd = join(projectRoot, ".gsd");
117
+ const wtGsd = join(worktreePath_, ".gsd");
118
+ // Copy milestone directory from project root to worktree — additive only.
119
+ // force:false prevents cpSync from overwriting existing worktree files.
120
+ // Without this, worktree-authoritative files (e.g. VALIDATION.md written
121
+ // by validate-milestone) get clobbered by stale project root copies,
122
+ // causing an infinite re-validation loop (#1886).
123
+ safeCopyRecursive(join(prGsd, "milestones", milestoneId), join(wtGsd, "milestones", milestoneId), { force: false });
124
+ // Forward-sync completed-units.json from project root to worktree.
125
+ // Project root is authoritative for completion state after crash recovery;
126
+ // without this, the worktree re-dispatches already-completed units (#1886).
127
+ safeCopy(join(prGsd, "completed-units.json"), join(wtGsd, "completed-units.json"), { force: true });
128
+ // Delete worktree gsd.db so it rebuilds from the freshly synced files.
129
+ // Stale DB rows are the root cause of the infinite skip loop (#853).
130
+ try {
131
+ const wtDb = join(wtGsd, "gsd.db");
132
+ if (existsSync(wtDb)) {
133
+ unlinkSync(wtDb);
134
+ }
135
+ }
136
+ catch {
137
+ /* non-fatal */
138
+ }
139
+ }
140
+ /**
141
+ * Sync dispatch-critical .gsd/ state files from worktree to project root.
142
+ * Only runs when inside an auto-worktree (worktreePath differs from projectRoot).
143
+ * Copies: STATE.md + active milestone directory (roadmap, slice plans, task summaries).
144
+ * Non-fatal — sync failure should never block dispatch.
145
+ */
146
+ export function syncStateToProjectRoot(worktreePath_, projectRoot, milestoneId) {
147
+ if (!worktreePath_ || !projectRoot || worktreePath_ === projectRoot)
148
+ return;
149
+ if (!milestoneId)
150
+ return;
151
+ const wtGsd = join(worktreePath_, ".gsd");
152
+ const prGsd = join(projectRoot, ".gsd");
153
+ // 1. STATE.md — the quick-glance status used by initial deriveState()
154
+ safeCopy(join(wtGsd, "STATE.md"), join(prGsd, "STATE.md"), { force: true });
155
+ // 2. Milestone directory — ROADMAP, slice PLANs, task summaries
156
+ // Copy the entire milestone .gsd subtree so deriveState reads current checkboxes
157
+ safeCopyRecursive(join(wtGsd, "milestones", milestoneId), join(prGsd, "milestones", milestoneId), { force: true });
158
+ // 3. metrics.json — session cost/token tracking (#2313).
159
+ // Without this, metrics accumulated in the worktree are invisible from the
160
+ // project root and never appear in the dashboard or skill-health reports.
161
+ safeCopy(join(wtGsd, "metrics.json"), join(prGsd, "metrics.json"), { force: true });
162
+ // 4. Runtime records — unit dispatch state used by selfHealRuntimeRecords().
163
+ // Without this, a crash during a unit leaves the runtime record only in the
164
+ // worktree. If the next session resolves basePath before worktree re-entry,
165
+ // selfHeal can't find or clear the stale record (#769).
166
+ safeCopyRecursive(join(wtGsd, "runtime", "units"), join(prGsd, "runtime", "units"), { force: true });
167
+ }
168
+ // ─── Resource Staleness ───────────────────────────────────────────────────
169
+ /**
170
+ * Read the resource version (semver) from the managed-resources manifest.
171
+ * Uses gsdVersion instead of syncedAt so that launching a second session
172
+ * doesn't falsely trigger staleness (#804).
173
+ */
174
+ export function readResourceVersion() {
175
+ const agentDir = process.env.GSD_CODING_AGENT_DIR || join(gsdHome, "agent");
176
+ const manifestPath = join(agentDir, "managed-resources.json");
177
+ try {
178
+ const manifest = JSON.parse(readFileSync(manifestPath, "utf-8"));
179
+ return typeof manifest?.gsdVersion === "string"
180
+ ? manifest.gsdVersion
181
+ : null;
182
+ }
183
+ catch {
184
+ return null;
185
+ }
186
+ }
187
+ /**
188
+ * Check if managed resources have been updated since session start.
189
+ * Returns a warning message if stale, null otherwise.
190
+ */
191
+ export function checkResourcesStale(versionOnStart) {
192
+ if (versionOnStart === null)
193
+ return null;
194
+ const current = readResourceVersion();
195
+ if (current === null)
196
+ return null;
197
+ if (current !== versionOnStart) {
198
+ return "GSD resources were updated since this session started. Restart gsd to load the new code.";
199
+ }
200
+ return null;
201
+ }
202
+ // ─── Stale Worktree Escape ────────────────────────────────────────────────
203
+ /**
204
+ * Detect and escape a stale worktree cwd (#608).
205
+ *
206
+ * After milestone completion + merge, the worktree directory is removed but
207
+ * the process cwd may still point inside `.gsd/worktrees/<MID>/`.
208
+ * When a new session starts, `process.cwd()` is passed as `base` to startAuto
209
+ * and all subsequent writes land in the wrong directory. This function detects
210
+ * that scenario and chdir back to the project root.
211
+ *
212
+ * Returns the corrected base path.
213
+ */
214
+ export function escapeStaleWorktree(base) {
215
+ // Direct layout: /.gsd/worktrees/
216
+ const directMarker = `${pathSep}.gsd${pathSep}worktrees${pathSep}`;
217
+ let idx = base.indexOf(directMarker);
218
+ if (idx === -1) {
219
+ // Symlink-resolved layout: /.gsd/projects/<hash>/worktrees/
220
+ const symlinkRe = new RegExp(`\\${pathSep}\\.gsd\\${pathSep}projects\\${pathSep}[a-f0-9]+\\${pathSep}worktrees\\${pathSep}`);
221
+ const match = base.match(symlinkRe);
222
+ if (!match || match.index === undefined)
223
+ return base;
224
+ idx = match.index;
225
+ }
226
+ // base is inside .gsd/worktrees/<something> — extract the project root
227
+ const projectRoot = base.slice(0, idx);
228
+ // Guard: If the candidate project root's .gsd IS the user-level ~/.gsd,
229
+ // the string-slice heuristic matched the wrong /.gsd/ boundary. This happens
230
+ // when .gsd is a symlink into ~/.gsd/projects/<hash> and process.cwd()
231
+ // resolved through the symlink. Returning ~ would be catastrophic (#1676).
232
+ const candidateGsd = join(projectRoot, ".gsd").replaceAll("\\", "/");
233
+ const gsdHomePath = gsdHome.replaceAll("\\", "/");
234
+ if (candidateGsd === gsdHomePath || candidateGsd.startsWith(gsdHomePath + "/")) {
235
+ // Don't chdir to home — return base unchanged.
236
+ // resolveProjectRoot() in worktree.ts has the full git-file-based recovery
237
+ // and will be called by the caller (startAuto → projectRoot()).
238
+ return base;
239
+ }
240
+ try {
241
+ process.chdir(projectRoot);
242
+ }
243
+ catch {
244
+ // If chdir fails, return the original — caller will handle errors downstream
245
+ return base;
246
+ }
247
+ return projectRoot;
248
+ }
249
+ /**
250
+ * Clean stale runtime unit files for completed milestones.
251
+ *
252
+ * After restart, stale runtime/units/*.json from prior milestones can
253
+ * cause deriveState to resume the wrong milestone (#887). Removes files
254
+ * for milestones that have a SUMMARY (fully complete).
255
+ */
256
+ export function cleanStaleRuntimeUnits(gsdRootPath, hasMilestoneSummary) {
257
+ const runtimeUnitsDir = join(gsdRootPath, "runtime", "units");
258
+ if (!existsSync(runtimeUnitsDir))
259
+ return 0;
260
+ let cleaned = 0;
261
+ try {
262
+ for (const file of readdirSync(runtimeUnitsDir)) {
263
+ if (!file.endsWith(".json"))
264
+ continue;
265
+ const midMatch = file.match(/(M\d+(?:-[a-z0-9]{6})?)/);
266
+ if (!midMatch)
267
+ continue;
268
+ if (hasMilestoneSummary(midMatch[1])) {
269
+ try {
270
+ unlinkSync(join(runtimeUnitsDir, file));
271
+ cleaned++;
272
+ }
273
+ catch {
274
+ /* non-fatal */
275
+ }
276
+ }
277
+ }
278
+ }
279
+ catch {
280
+ /* non-fatal */
281
+ }
282
+ return cleaned;
283
+ }
73
284
  // ─── Worktree ↔ Main Repo Sync (#1311) ──────────────────────────────────────
74
285
  /**
75
286
  * Sync .gsd/ state from the main repo into the worktree.
@@ -90,29 +301,12 @@ export function syncGsdStateToWorktree(mainBasePath, worktreePath_) {
90
301
  const wtGsd = gsdRoot(worktreePath_);
91
302
  const synced = [];
92
303
  // If both resolve to the same directory (symlink), no sync needed
93
- try {
94
- const mainResolved = realpathSync(mainGsd);
95
- const wtResolved = realpathSync(wtGsd);
96
- if (mainResolved === wtResolved)
97
- return { synced };
98
- }
99
- catch {
100
- // Can't resolve — proceed with sync as a safety measure
101
- }
304
+ if (isSamePath(mainGsd, wtGsd))
305
+ return { synced };
102
306
  if (!existsSync(mainGsd) || !existsSync(wtGsd))
103
307
  return { synced };
104
308
  // Sync root-level .gsd/ files (DECISIONS, REQUIREMENTS, PROJECT, KNOWLEDGE, etc.)
105
- const rootFiles = [
106
- "DECISIONS.md",
107
- "REQUIREMENTS.md",
108
- "PROJECT.md",
109
- "KNOWLEDGE.md",
110
- "OVERRIDES.md",
111
- "QUEUE.md",
112
- "completed-units.json",
113
- "metrics.json",
114
- ];
115
- for (const f of rootFiles) {
309
+ for (const f of ROOT_STATE_FILES) {
116
310
  const src = join(mainGsd, f);
117
311
  const dst = join(wtGsd, f);
118
312
  if (existsSync(src) && !existsSync(dst)) {
@@ -241,15 +435,8 @@ export function syncWorktreeStateBack(mainBasePath, worktreePath, milestoneId) {
241
435
  const wtGsd = gsdRoot(worktreePath);
242
436
  const synced = [];
243
437
  // If both resolve to the same directory (symlink), no sync needed
244
- try {
245
- const mainResolved = realpathSync(mainGsd);
246
- const wtResolved = realpathSync(wtGsd);
247
- if (mainResolved === wtResolved)
248
- return { synced };
249
- }
250
- catch {
251
- // Can't resolve — proceed with sync
252
- }
438
+ if (isSamePath(mainGsd, wtGsd))
439
+ return { synced };
253
440
  if (!existsSync(wtGsd) || !existsSync(mainGsd))
254
441
  return { synced };
255
442
  // ── 0. Pre-upgrade worktree DB reconciliation ────────────────────────
@@ -274,17 +461,7 @@ export function syncWorktreeStateBack(mainBasePath, worktreePath, milestoneId) {
274
461
  // Also includes QUEUE.md, completed-units.json, and metrics.json which are
275
462
  // written during milestone closeout and lost on teardown without explicit sync
276
463
  // (#1787, #2313).
277
- const rootFiles = [
278
- "DECISIONS.md",
279
- "REQUIREMENTS.md",
280
- "PROJECT.md",
281
- "KNOWLEDGE.md",
282
- "OVERRIDES.md",
283
- "QUEUE.md",
284
- "completed-units.json",
285
- "metrics.json",
286
- ];
287
- for (const f of rootFiles) {
464
+ for (const f of ROOT_STATE_FILES) {
288
465
  const src = join(wtGsd, f);
289
466
  const dst = join(mainGsd, f);
290
467
  if (existsSync(src)) {
@@ -321,92 +498,59 @@ export function syncWorktreeStateBack(mainBasePath, worktreePath, milestoneId) {
321
498
  * Sync a single milestone directory from worktree to main.
322
499
  * Copies milestone-level .md files, slice-level files, and task summaries.
323
500
  */
501
+ /** Copy matching files from srcDir to dstDir (non-fatal per file). */
502
+ function syncDirFiles(srcDir, dstDir, filter, synced, prefix) {
503
+ try {
504
+ for (const entry of readdirSync(srcDir, { withFileTypes: true })) {
505
+ if (!entry.isFile() || !filter(entry.name))
506
+ continue;
507
+ try {
508
+ cpSync(join(srcDir, entry.name), join(dstDir, entry.name), { force: true });
509
+ synced.push(`${prefix}${entry.name}`);
510
+ }
511
+ catch {
512
+ /* non-fatal */
513
+ }
514
+ }
515
+ }
516
+ catch {
517
+ /* non-fatal — srcDir may not be readable */
518
+ }
519
+ }
324
520
  function syncMilestoneDir(wtGsd, mainGsd, mid, synced) {
325
521
  const wtMilestoneDir = join(wtGsd, "milestones", mid);
326
522
  const mainMilestoneDir = join(mainGsd, "milestones", mid);
327
523
  if (!existsSync(wtMilestoneDir))
328
524
  return;
329
525
  mkdirSync(mainMilestoneDir, { recursive: true });
526
+ const isMd = (name) => name.endsWith(".md");
330
527
  // Sync milestone-level files (SUMMARY, VALIDATION, ROADMAP, CONTEXT)
528
+ syncDirFiles(wtMilestoneDir, mainMilestoneDir, isMd, synced, `milestones/${mid}/`);
529
+ // Sync slice-level files (summaries, UATs) and task summaries (#1678)
530
+ const wtSlicesDir = join(wtMilestoneDir, "slices");
531
+ const mainSlicesDir = join(mainMilestoneDir, "slices");
532
+ if (!existsSync(wtSlicesDir))
533
+ return;
331
534
  try {
332
- for (const entry of readdirSync(wtMilestoneDir, { withFileTypes: true })) {
333
- if (entry.isFile() && entry.name.endsWith(".md")) {
334
- const src = join(wtMilestoneDir, entry.name);
335
- const dst = join(mainMilestoneDir, entry.name);
336
- try {
337
- cpSync(src, dst, { force: true });
338
- synced.push(`milestones/${mid}/${entry.name}`);
339
- }
340
- catch {
341
- /* non-fatal */
342
- }
535
+ for (const sliceEntry of readdirSync(wtSlicesDir, { withFileTypes: true })) {
536
+ if (!sliceEntry.isDirectory())
537
+ continue;
538
+ const sid = sliceEntry.name;
539
+ const wtSliceDir = join(wtSlicesDir, sid);
540
+ const mainSliceDir = join(mainSlicesDir, sid);
541
+ mkdirSync(mainSliceDir, { recursive: true });
542
+ syncDirFiles(wtSliceDir, mainSliceDir, isMd, synced, `milestones/${mid}/slices/${sid}/`);
543
+ const wtTasksDir = join(wtSliceDir, "tasks");
544
+ const mainTasksDir = join(mainSliceDir, "tasks");
545
+ if (existsSync(wtTasksDir)) {
546
+ mkdirSync(mainTasksDir, { recursive: true });
547
+ syncDirFiles(wtTasksDir, mainTasksDir, isMd, synced, `milestones/${mid}/slices/${sid}/tasks/`);
343
548
  }
344
549
  }
345
550
  }
346
551
  catch {
347
552
  /* non-fatal */
348
553
  }
349
- // Sync slice-level files (summaries, UATs)
350
- const wtSlicesDir = join(wtMilestoneDir, "slices");
351
- const mainSlicesDir = join(mainMilestoneDir, "slices");
352
- if (existsSync(wtSlicesDir)) {
353
- try {
354
- for (const sliceEntry of readdirSync(wtSlicesDir, {
355
- withFileTypes: true,
356
- })) {
357
- if (!sliceEntry.isDirectory())
358
- continue;
359
- const sid = sliceEntry.name;
360
- const wtSliceDir = join(wtSlicesDir, sid);
361
- const mainSliceDir = join(mainSlicesDir, sid);
362
- mkdirSync(mainSliceDir, { recursive: true });
363
- for (const fileEntry of readdirSync(wtSliceDir, {
364
- withFileTypes: true,
365
- })) {
366
- if (fileEntry.isFile() && fileEntry.name.endsWith(".md")) {
367
- const src = join(wtSliceDir, fileEntry.name);
368
- const dst = join(mainSliceDir, fileEntry.name);
369
- try {
370
- cpSync(src, dst, { force: true });
371
- synced.push(`milestones/${mid}/slices/${sid}/${fileEntry.name}`);
372
- }
373
- catch {
374
- /* non-fatal */
375
- }
376
- }
377
- else if (fileEntry.isDirectory() && fileEntry.name === "tasks") {
378
- // Recurse into tasks/ subdirectory to sync task summaries (#1678).
379
- // Without this, T01-SUMMARY.md etc. are silently dropped on
380
- // worktree teardown because the loop only processes isFile() entries.
381
- const wtTasksDir = join(wtSliceDir, "tasks");
382
- const mainTasksDir = join(mainSliceDir, "tasks");
383
- mkdirSync(mainTasksDir, { recursive: true });
384
- try {
385
- for (const taskEntry of readdirSync(wtTasksDir, { withFileTypes: true })) {
386
- if (taskEntry.isFile() && taskEntry.name.endsWith(".md")) {
387
- const taskSrc = join(wtTasksDir, taskEntry.name);
388
- const taskDst = join(mainTasksDir, taskEntry.name);
389
- try {
390
- cpSync(taskSrc, taskDst, { force: true });
391
- synced.push(`milestones/${mid}/slices/${sid}/tasks/${taskEntry.name}`);
392
- }
393
- catch {
394
- /* non-fatal */
395
- }
396
- }
397
- }
398
- }
399
- catch {
400
- /* non-fatal: tasks dir read failure */
401
- }
402
- }
403
- }
404
- }
405
- }
406
- catch {
407
- /* non-fatal */
408
- }
409
- }
410
554
  }
411
555
  // ─── Worktree Post-Create Hook (#597) ────────────────────────────────────────
412
556
  /**
@@ -16,7 +16,7 @@ import { collectSecretsFromManifest } from "../get-secrets-from-user.js";
16
16
  import { gsdRoot, resolveMilestoneFile, resolveMilestonePath, resolveDir, milestonesDir, } from "./paths.js";
17
17
  import { invalidateAllCaches } from "./cache.js";
18
18
  import { clearActivityLogState } from "./activity-log.js";
19
- import { synthesizeCrashRecovery, getDeepDiagnostic, } from "./session-forensics.js";
19
+ import { synthesizeCrashRecovery, getDeepDiagnostic, readActiveMilestoneId, } from "./session-forensics.js";
20
20
  import { writeLock, clearLock, readCrashLock, isLockProcessAlive, } from "./crash-recovery.js";
21
21
  import { acquireSessionLock, getSessionLockStatus, releaseSessionLock, updateSessionLock, } from "./session-lock.js";
22
22
  import { resolveAutoSupervisorConfig, loadEffectiveGSDPreferences, getIsolationMode, } from "./preferences.js";
@@ -25,7 +25,6 @@ import { getBudgetAlertLevel, getNewBudgetAlertLevel, getBudgetEnforcementAction
25
25
  import { markToolStart as _markToolStart, markToolEnd as _markToolEnd, getOldestInFlightToolAgeMs as _getOldestInFlightToolAgeMs, clearInFlightTools, } from "./auto-tool-tracking.js";
26
26
  import { closeoutUnit } from "./auto-unit-closeout.js";
27
27
  import { selectAndApplyModel, resolveModelId } from "./auto-model-selection.js";
28
- import { syncProjectRootToWorktree, checkResourcesStale, escapeStaleWorktree, } from "./auto-worktree-sync.js";
29
28
  import { resetRoutingHistory, recordOutcome } from "./routing-history.js";
30
29
  import { resetHookState, runPreDispatchHooks, restoreHookState, clearPersistedHookState, } from "./post-unit-hooks.js";
31
30
  import { runGSDDoctor, rebuildState } from "./doctor.js";
@@ -39,7 +38,7 @@ import { atomicWriteSync } from "./atomic-write.js";
39
38
  import { autoCommitCurrentBranch, captureIntegrationBranch, detectWorktreeName, getCurrentBranch, getMainBranch, setActiveMilestoneId, } from "./worktree.js";
40
39
  import { GitServiceImpl } from "./git-service.js";
41
40
  import { getPriorSliceCompletionBlocker } from "./dispatch-guard.js";
42
- import { createAutoWorktree, enterAutoWorktree, teardownAutoWorktree, isInAutoWorktree, getAutoWorktreePath, mergeMilestoneToMain, autoWorktreeBranch, syncWorktreeStateBack, } from "./auto-worktree.js";
41
+ import { createAutoWorktree, enterAutoWorktree, teardownAutoWorktree, isInAutoWorktree, getAutoWorktreePath, mergeMilestoneToMain, autoWorktreeBranch, syncWorktreeStateBack, syncProjectRootToWorktree, checkResourcesStale, escapeStaleWorktree, } from "./auto-worktree.js";
43
42
  import { pruneQueueOrder } from "./queue-order.js";
44
43
  import { debugLog, isDebugEnabled, writeDebugSummary } from "./debug-logger.js";
45
44
  import { reconcileMergeState, } from "./auto-recovery.js";
@@ -59,7 +58,6 @@ import { bootstrapAutoSession } from "./auto-start.js";
59
58
  import { autoLoop, resolveAgentEnd, resolveAgentEndCancelled, _resetPendingResolve, isSessionSwitchInFlight } from "./auto-loop.js";
60
59
  import { WorktreeResolver, } from "./worktree-resolver.js";
61
60
  import { reorderForCaching } from "./prompt-ordering.js";
62
- // Worktree sync, resource staleness, stale worktree escape → auto-worktree-sync.ts
63
61
  // ─── Session State ─────────────────────────────────────────────────────────
64
62
  import { AutoSession, } from "./auto/session.js";
65
63
  export { MAX_UNIT_DISPATCHES, STUB_RECOVERY_THRESHOLD, MAX_LIFETIME_DISPATCHES, NEW_SESSION_TIMEOUT_MS, } from "./auto/session.js";
@@ -706,7 +704,11 @@ function buildLoopDeps() {
706
704
  resolveModelId,
707
705
  startUnitSupervision,
708
706
  // Prompt helpers
709
- getDeepDiagnostic,
707
+ getDeepDiagnostic: (basePath) => {
708
+ const mid = readActiveMilestoneId(basePath);
709
+ const wtPath = mid ? getAutoWorktreePath(basePath, mid) : undefined;
710
+ return getDeepDiagnostic(basePath, wtPath ?? undefined);
711
+ },
710
712
  isDbAvailable,
711
713
  reorderForCaching,
712
714
  // Filesystem
@@ -1042,4 +1042,93 @@ export function registerDbTools(pi) {
1042
1042
  };
1043
1043
  pi.registerTool(reassessRoadmapTool);
1044
1044
  registerAlias(pi, reassessRoadmapTool, "gsd_roadmap_reassess", "gsd_reassess_roadmap");
1045
+ // ─── gsd_save_gate_result ──────────────────────────────────────────────
1046
+ const saveGateResultExecute = async (_toolCallId, params, _signal, _onUpdate, _ctx) => {
1047
+ const dbAvailable = await ensureDbOpen();
1048
+ if (!dbAvailable) {
1049
+ return {
1050
+ content: [{ type: "text", text: "Error: GSD database is not available." }],
1051
+ details: { operation: "save_gate_result", error: "db_unavailable" },
1052
+ };
1053
+ }
1054
+ const validGates = ["Q3", "Q4", "Q5", "Q6", "Q7", "Q8"];
1055
+ if (!validGates.includes(params.gateId)) {
1056
+ return {
1057
+ content: [{ type: "text", text: `Error: Invalid gateId "${params.gateId}". Must be one of: ${validGates.join(", ")}` }],
1058
+ details: { operation: "save_gate_result", error: "invalid_gate_id" },
1059
+ };
1060
+ }
1061
+ const validVerdicts = ["pass", "flag", "omitted"];
1062
+ if (!validVerdicts.includes(params.verdict)) {
1063
+ return {
1064
+ content: [{ type: "text", text: `Error: Invalid verdict "${params.verdict}". Must be one of: ${validVerdicts.join(", ")}` }],
1065
+ details: { operation: "save_gate_result", error: "invalid_verdict" },
1066
+ };
1067
+ }
1068
+ try {
1069
+ const { saveGateResult } = await import("../gsd-db.js");
1070
+ const { invalidateStateCache } = await import("../state.js");
1071
+ saveGateResult({
1072
+ milestoneId: params.milestoneId,
1073
+ sliceId: params.sliceId,
1074
+ gateId: params.gateId,
1075
+ taskId: params.taskId ?? "",
1076
+ verdict: params.verdict,
1077
+ rationale: params.rationale,
1078
+ findings: params.findings ?? "",
1079
+ });
1080
+ invalidateStateCache();
1081
+ return {
1082
+ content: [{ type: "text", text: `Gate ${params.gateId} result saved: verdict=${params.verdict}` }],
1083
+ details: { operation: "save_gate_result", gateId: params.gateId, verdict: params.verdict },
1084
+ };
1085
+ }
1086
+ catch (err) {
1087
+ const msg = err instanceof Error ? err.message : String(err);
1088
+ logError("tool", `gsd_save_gate_result failed: ${msg}`, { tool: "gsd_save_gate_result", error: String(err) });
1089
+ return {
1090
+ content: [{ type: "text", text: `Error saving gate result: ${msg}` }],
1091
+ details: { operation: "save_gate_result", error: msg },
1092
+ };
1093
+ }
1094
+ };
1095
+ const saveGateResultTool = {
1096
+ name: "gsd_save_gate_result",
1097
+ label: "Save Gate Result",
1098
+ description: "Save the result of a quality gate evaluation (Q3-Q8) to the GSD database. " +
1099
+ "Called by gate evaluation sub-agents after analyzing a specific quality question.",
1100
+ promptSnippet: "Save quality gate evaluation result (verdict, rationale, findings)",
1101
+ promptGuidelines: [
1102
+ "Use gsd_save_gate_result after evaluating a quality gate question.",
1103
+ "gateId must be one of: Q3, Q4, Q5, Q6, Q7, Q8.",
1104
+ "verdict must be: pass (no concerns), flag (concerns found), or omitted (not applicable).",
1105
+ "rationale should be a one-sentence justification for the verdict.",
1106
+ "findings should contain detailed markdown analysis (or empty string if omitted).",
1107
+ ],
1108
+ parameters: Type.Object({
1109
+ milestoneId: Type.String({ description: "Milestone ID (e.g. M001)" }),
1110
+ sliceId: Type.String({ description: "Slice ID (e.g. S01)" }),
1111
+ gateId: Type.String({ description: "Gate ID: Q3, Q4, Q5, Q6, Q7, or Q8" }),
1112
+ taskId: Type.Optional(Type.String({ description: "Task ID for task-scoped gates (Q5/Q6/Q7)" })),
1113
+ verdict: Type.String({ description: "pass, flag, or omitted" }),
1114
+ rationale: Type.String({ description: "One-sentence justification" }),
1115
+ findings: Type.Optional(Type.String({ description: "Detailed markdown findings" })),
1116
+ }),
1117
+ execute: saveGateResultExecute,
1118
+ renderCall(args, theme) {
1119
+ let text = theme.fg("toolTitle", theme.bold("save_gate_result "));
1120
+ text += theme.fg("accent", args.gateId ?? "");
1121
+ text += theme.fg("dim", ` → ${args.verdict ?? ""}`);
1122
+ return new Text(text, 0, 0);
1123
+ },
1124
+ renderResult(result, _options, theme) {
1125
+ const d = result.details;
1126
+ if (result.isError || d?.error) {
1127
+ return new Text(theme.fg("error", `Error: ${d?.error ?? "unknown"}`), 0, 0);
1128
+ }
1129
+ const color = d?.verdict === "flag" ? "warning" : "success";
1130
+ return new Text(theme.fg(color, `${d?.gateId}: ${d?.verdict}`), 0, 0);
1131
+ },
1132
+ };
1133
+ pi.registerTool(saveGateResultTool);
1045
1134
  }
@@ -3,7 +3,7 @@ import { isToolCallEventType } from "@gsd/pi-coding-agent";
3
3
  import { buildMilestoneFileName, resolveMilestonePath, resolveSliceFile, resolveSlicePath } from "../paths.js";
4
4
  import { buildBeforeAgentStartResult } from "./system-context.js";
5
5
  import { handleAgentEnd } from "./agent-end-recovery.js";
6
- import { isDepthVerified, isQueuePhaseActive, markDepthVerified, resetWriteGateState, shouldBlockContextWrite } from "./write-gate.js";
6
+ import { clearDiscussionFlowState, isDepthVerified, isQueuePhaseActive, markDepthVerified, resetWriteGateState, shouldBlockContextWrite } from "./write-gate.js";
7
7
  import { isBlockedStateFile, isBashWriteToStateFile, BLOCKED_WRITE_ERROR } from "../write-intercept.js";
8
8
  import { getDiscussionMilestoneId } from "../guided-flow.js";
9
9
  import { loadToolApiKeys } from "../commands-config.js";
@@ -63,6 +63,13 @@ export function registerHooks(pi) {
63
63
  // ignore
64
64
  }
65
65
  });
66
+ pi.on("session_switch", async (_event, ctx) => {
67
+ resetWriteGateState();
68
+ resetToolCallLoopGuard();
69
+ clearDiscussionFlowState();
70
+ await syncServiceTierStatus(ctx);
71
+ loadToolApiKeys();
72
+ });
66
73
  pi.on("before_agent_start", async (event, ctx) => {
67
74
  return buildBeforeAgentStartResult(event, ctx);
68
75
  });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * GSD branch naming patterns — single source of truth.
3
+ *
4
+ * gsd/<worktree>/<milestone>/<slice> → SLICE_BRANCH_RE
5
+ * gsd/quick/<id>-<slug> → QUICK_BRANCH_RE
6
+ * gsd/<workflow>/<...> → WORKFLOW_BRANCH_RE (non-milestone gsd/ branches)
7
+ */
8
+ /** Matches gsd/ slice branches: gsd/[worktree/]M001[-hash]/S01 */
9
+ export const SLICE_BRANCH_RE = /^gsd\/(?:([a-zA-Z0-9_-]+)\/)?(M\d+(?:-[a-z0-9]{6})?)\/(S\d+)$/;
10
+ /** Matches gsd/quick/ task branches */
11
+ export const QUICK_BRANCH_RE = /^gsd\/quick\//;
12
+ /** Matches gsd/ workflow branches (non-milestone, e.g. gsd/workflow-name/...) */
13
+ export const WORKFLOW_BRANCH_RE = /^gsd\/(?!M\d)[\w-]+\//;