@opengsd/gsd-pi 1.2.0-dev.4813ead6 → 1.2.0-dev.5457a158

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 (342) hide show
  1. package/dist/cli-style.d.ts +17 -0
  2. package/dist/cli-style.js +28 -0
  3. package/dist/cli.js +1 -1
  4. package/dist/headless-events.d.ts +4 -2
  5. package/dist/headless-events.js +7 -29
  6. package/dist/models-resolver.d.ts +3 -13
  7. package/dist/models-resolver.js +3 -22
  8. package/dist/resource-loader.js +2 -14
  9. package/dist/resources/.managed-resources-content-hash +1 -1
  10. package/dist/resources/extensions/bg-shell/utilities.js +5 -2
  11. package/dist/resources/extensions/claude-code-cli/models.js +9 -0
  12. package/dist/resources/extensions/claude-code-cli/stream-adapter.js +35 -4
  13. package/dist/resources/extensions/gsd/auto/orchestrator.js +33 -4
  14. package/dist/resources/extensions/gsd/auto/phases.js +6 -1
  15. package/dist/resources/extensions/gsd/auto-post-unit.js +19 -8
  16. package/dist/resources/extensions/gsd/auto-prompts.js +3 -0
  17. package/dist/resources/extensions/gsd/auto-start.js +12 -14
  18. package/dist/resources/extensions/gsd/auto-tool-tracking.js +18 -0
  19. package/dist/resources/extensions/gsd/auto-unit-tool-scope.js +7 -16
  20. package/dist/resources/extensions/gsd/auto-worktree-repair.js +10 -2
  21. package/dist/resources/extensions/gsd/auto-worktree.js +35 -352
  22. package/dist/resources/extensions/gsd/auto.js +8 -20
  23. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +3 -2
  24. package/dist/resources/extensions/gsd/bootstrap/dynamic-tools.js +9 -6
  25. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +86 -6
  26. package/dist/resources/extensions/gsd/bootstrap/write-gate.js +30 -4
  27. package/dist/resources/extensions/gsd/branch-patterns.js +2 -0
  28. package/dist/resources/extensions/gsd/captures.js +5 -15
  29. package/dist/resources/extensions/gsd/closeout-recovery.js +3 -2
  30. package/dist/resources/extensions/gsd/commands/catalog.js +6 -62
  31. package/dist/resources/extensions/gsd/crash-recovery.js +4 -12
  32. package/dist/resources/extensions/gsd/db/engine.js +755 -0
  33. package/dist/resources/extensions/gsd/db/queries.js +372 -0
  34. package/dist/resources/extensions/gsd/db/sql-constants.js +11 -0
  35. package/dist/resources/extensions/gsd/db/writers/cascades.js +194 -0
  36. package/dist/resources/extensions/gsd/db/writers/import-restore.js +182 -0
  37. package/dist/resources/extensions/gsd/db/writers/memory.js +149 -0
  38. package/dist/resources/extensions/gsd/db/writers/reconcile.js +458 -0
  39. package/dist/resources/extensions/gsd/db/writers/status.js +70 -0
  40. package/dist/resources/extensions/gsd/doctor-environment.js +5 -11
  41. package/dist/resources/extensions/gsd/doctor-format.js +9 -6
  42. package/dist/resources/extensions/gsd/doctor-git-checks.js +4 -3
  43. package/dist/resources/extensions/gsd/doctor-runtime-checks.js +21 -16
  44. package/dist/resources/extensions/gsd/error-classifier.js +9 -0
  45. package/dist/resources/extensions/gsd/git-service.js +1 -0
  46. package/dist/resources/extensions/gsd/gitignore.js +3 -0
  47. package/dist/resources/extensions/gsd/gsd-db.js +171 -2048
  48. package/dist/resources/extensions/gsd/guidance.js +98 -0
  49. package/dist/resources/extensions/gsd/guided-flow.js +51 -5
  50. package/dist/resources/extensions/gsd/mcp-tool-name.js +5 -13
  51. package/dist/resources/extensions/gsd/memory-consolidation-scanner.js +1 -1
  52. package/dist/resources/extensions/gsd/migrate/safety.js +20 -9
  53. package/dist/resources/extensions/gsd/migration-auto-check.js +24 -3
  54. package/dist/resources/extensions/gsd/model-cost-table.js +1 -0
  55. package/dist/resources/extensions/gsd/model-router.js +3 -0
  56. package/dist/resources/extensions/gsd/notification-store.js +11 -4
  57. package/dist/resources/extensions/gsd/parallel-merge.js +14 -11
  58. package/dist/resources/extensions/gsd/parallel-monitor-overlay.js +11 -7
  59. package/dist/resources/extensions/gsd/paths.js +37 -24
  60. package/dist/resources/extensions/gsd/pre-execution-checks.js +91 -3
  61. package/dist/resources/extensions/gsd/preferences-models.js +12 -46
  62. package/dist/resources/extensions/gsd/preferences.js +14 -0
  63. package/dist/resources/extensions/gsd/prompts/plan-slice.md +1 -1
  64. package/dist/resources/extensions/gsd/prompts/refine-slice.md +1 -1
  65. package/dist/resources/extensions/gsd/prompts/replan-slice.md +1 -1
  66. package/dist/resources/extensions/gsd/provider-error-guidance.js +1 -5
  67. package/dist/resources/extensions/gsd/provider-switch-observer.js +1 -1
  68. package/dist/resources/extensions/gsd/publication.js +87 -0
  69. package/dist/resources/extensions/gsd/recovery-classification.js +41 -87
  70. package/dist/resources/extensions/gsd/safety/evidence-collector.js +37 -4
  71. package/dist/resources/extensions/gsd/safety/evidence-cross-ref.js +7 -2
  72. package/dist/resources/extensions/gsd/safety/file-change-validator.js +10 -0
  73. package/dist/resources/extensions/gsd/state-transition-matrix.js +38 -0
  74. package/dist/resources/extensions/gsd/state.js +1 -20
  75. package/dist/resources/extensions/gsd/status-guards.js +56 -8
  76. package/dist/resources/extensions/gsd/stop-notice.js +57 -0
  77. package/dist/resources/extensions/gsd/tool-surface-readiness.js +56 -0
  78. package/dist/resources/extensions/gsd/tools/complete-slice.js +24 -43
  79. package/dist/resources/extensions/gsd/tools/exec-tool.js +5 -8
  80. package/dist/resources/extensions/gsd/tools/plan-slice.js +12 -6
  81. package/dist/resources/extensions/gsd/tools/reopen-milestone.js +11 -29
  82. package/dist/resources/extensions/gsd/tools/reopen-slice.js +14 -33
  83. package/dist/resources/extensions/gsd/tools/skip-slice.js +18 -36
  84. package/dist/resources/extensions/gsd/undo.js +8 -7
  85. package/dist/resources/extensions/gsd/unit-closeout.js +138 -0
  86. package/dist/resources/extensions/gsd/unit-context-composer.js +9 -1
  87. package/dist/resources/extensions/gsd/unit-context-manifest.js +4 -27
  88. package/dist/resources/extensions/gsd/unit-registry.js +350 -0
  89. package/dist/resources/extensions/gsd/unit-tool-contracts.js +9 -182
  90. package/dist/resources/extensions/gsd/workflow-tool-surface.js +1 -1
  91. package/dist/resources/extensions/gsd/worktree-git-recovery.js +293 -0
  92. package/dist/resources/extensions/gsd/worktree-lifecycle.js +9 -1
  93. package/dist/resources/extensions/gsd/worktree-manager.js +45 -28
  94. package/dist/resources/extensions/gsd/worktree-placement.js +59 -0
  95. package/dist/resources/extensions/gsd/worktree-reentry.js +12 -8
  96. package/dist/resources/extensions/gsd/worktree-root.js +28 -6
  97. package/dist/resources/extensions/gsd/worktree-safety.js +8 -5
  98. package/dist/resources/extensions/gsd/worktree-session-state.js +12 -11
  99. package/dist/resources/skills/gsd-browser/SKILL.md +1 -1
  100. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  101. package/dist/web/standalone/.next/BUILD_ID +1 -1
  102. package/dist/web/standalone/.next/app-path-routes-manifest.json +6 -6
  103. package/dist/web/standalone/.next/build-manifest.json +2 -2
  104. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  105. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  106. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  107. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  108. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  109. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  110. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  111. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  112. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  113. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  114. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  115. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  116. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  117. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  118. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  119. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  120. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  121. package/dist/web/standalone/.next/server/app/api/boot/route.js.nft.json +1 -1
  122. package/dist/web/standalone/.next/server/app/api/bridge-terminal/input/route.js.nft.json +1 -1
  123. package/dist/web/standalone/.next/server/app/api/bridge-terminal/resize/route.js.nft.json +1 -1
  124. package/dist/web/standalone/.next/server/app/api/bridge-terminal/stream/route.js.nft.json +1 -1
  125. package/dist/web/standalone/.next/server/app/api/captures/route.js.nft.json +1 -1
  126. package/dist/web/standalone/.next/server/app/api/cleanup/route.js.nft.json +1 -1
  127. package/dist/web/standalone/.next/server/app/api/doctor/route.js.nft.json +1 -1
  128. package/dist/web/standalone/.next/server/app/api/export-data/route.js.nft.json +1 -1
  129. package/dist/web/standalone/.next/server/app/api/files/route.js.nft.json +1 -1
  130. package/dist/web/standalone/.next/server/app/api/forensics/route.js.nft.json +1 -1
  131. package/dist/web/standalone/.next/server/app/api/git/route.js.nft.json +1 -1
  132. package/dist/web/standalone/.next/server/app/api/history/route.js.nft.json +1 -1
  133. package/dist/web/standalone/.next/server/app/api/hooks/route.js.nft.json +1 -1
  134. package/dist/web/standalone/.next/server/app/api/inspect/route.js.nft.json +1 -1
  135. package/dist/web/standalone/.next/server/app/api/knowledge/route.js.nft.json +1 -1
  136. package/dist/web/standalone/.next/server/app/api/live-state/route.js.nft.json +1 -1
  137. package/dist/web/standalone/.next/server/app/api/mcp-connections/route.js.nft.json +1 -1
  138. package/dist/web/standalone/.next/server/app/api/notifications/route.js.nft.json +1 -1
  139. package/dist/web/standalone/.next/server/app/api/onboarding/route.js.nft.json +1 -1
  140. package/dist/web/standalone/.next/server/app/api/projects/route.js.nft.json +1 -1
  141. package/dist/web/standalone/.next/server/app/api/recovery/route.js.nft.json +1 -1
  142. package/dist/web/standalone/.next/server/app/api/session/browser/route.js.nft.json +1 -1
  143. package/dist/web/standalone/.next/server/app/api/session/command/route.js.nft.json +1 -1
  144. package/dist/web/standalone/.next/server/app/api/session/events/route.js.nft.json +1 -1
  145. package/dist/web/standalone/.next/server/app/api/session/manage/route.js.nft.json +1 -1
  146. package/dist/web/standalone/.next/server/app/api/settings-data/route.js.nft.json +1 -1
  147. package/dist/web/standalone/.next/server/app/api/shutdown/route.js.nft.json +1 -1
  148. package/dist/web/standalone/.next/server/app/api/skill-health/route.js.nft.json +1 -1
  149. package/dist/web/standalone/.next/server/app/api/steer/route.js.nft.json +1 -1
  150. package/dist/web/standalone/.next/server/app/api/switch-root/route.js.nft.json +1 -1
  151. package/dist/web/standalone/.next/server/app/api/terminal/sessions/route.js.nft.json +1 -1
  152. package/dist/web/standalone/.next/server/app/api/terminal/stream/route.js.nft.json +1 -1
  153. package/dist/web/standalone/.next/server/app/api/undo/route.js.nft.json +1 -1
  154. package/dist/web/standalone/.next/server/app/api/visualizer/route.js.nft.json +1 -1
  155. package/dist/web/standalone/.next/server/app/index.html +1 -1
  156. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  157. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  158. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  159. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  160. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  161. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  162. package/dist/web/standalone/.next/server/app-paths-manifest.json +6 -6
  163. package/dist/web/standalone/.next/server/chunks/5124.js +1 -1
  164. package/dist/web/standalone/.next/server/chunks/{5047.js → 5942.js} +2 -2
  165. package/dist/web/standalone/.next/server/chunks/8357.js +1 -1
  166. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  167. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  168. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  169. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  170. package/dist/worktree-cli.js +3 -6
  171. package/dist/worktree-status-banner.js +7 -11
  172. package/package.json +1 -1
  173. package/packages/cloud-mcp-gateway/package.json +2 -2
  174. package/packages/contracts/dist/workflow.d.ts +4 -0
  175. package/packages/contracts/dist/workflow.d.ts.map +1 -1
  176. package/packages/contracts/dist/workflow.js.map +1 -1
  177. package/packages/contracts/package.json +1 -1
  178. package/packages/daemon/package.json +4 -4
  179. package/packages/gsd-agent-core/package.json +5 -5
  180. package/packages/gsd-agent-modes/package.json +7 -7
  181. package/packages/mcp-server/dist/cli.js +6 -3
  182. package/packages/mcp-server/dist/cli.js.map +1 -1
  183. package/packages/mcp-server/dist/workflow-tools.d.ts +8 -0
  184. package/packages/mcp-server/dist/workflow-tools.d.ts.map +1 -1
  185. package/packages/mcp-server/dist/workflow-tools.js +46 -21
  186. package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
  187. package/packages/mcp-server/package.json +3 -3
  188. package/packages/native/package.json +1 -1
  189. package/packages/pi-agent-core/package.json +1 -1
  190. package/packages/pi-ai/dist/image-models.generated.d.ts +0 -30
  191. package/packages/pi-ai/dist/image-models.generated.d.ts.map +1 -1
  192. package/packages/pi-ai/dist/image-models.generated.js +0 -30
  193. package/packages/pi-ai/dist/image-models.generated.js.map +1 -1
  194. package/packages/pi-ai/dist/models.generated.d.ts +361 -255
  195. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  196. package/packages/pi-ai/dist/models.generated.js +311 -256
  197. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  198. package/packages/pi-ai/package.json +1 -1
  199. package/packages/pi-coding-agent/dist/core/capability-patches.d.ts.map +1 -1
  200. package/packages/pi-coding-agent/dist/core/capability-patches.js +3 -1
  201. package/packages/pi-coding-agent/dist/core/capability-patches.js.map +1 -1
  202. package/packages/pi-coding-agent/package.json +7 -7
  203. package/packages/pi-tui/package.json +2 -2
  204. package/packages/rpc-client/package.json +2 -2
  205. package/pkg/package.json +1 -1
  206. package/src/resources/extensions/bg-shell/utilities.ts +5 -2
  207. package/src/resources/extensions/claude-code-cli/models.ts +9 -0
  208. package/src/resources/extensions/claude-code-cli/stream-adapter.ts +37 -2
  209. package/src/resources/extensions/claude-code-cli/tests/stream-adapter.test.ts +28 -0
  210. package/src/resources/extensions/gsd/auto/loop-deps.ts +1 -1
  211. package/src/resources/extensions/gsd/auto/orchestrator.ts +39 -5
  212. package/src/resources/extensions/gsd/auto/phases.ts +10 -1
  213. package/src/resources/extensions/gsd/auto-post-unit.ts +25 -7
  214. package/src/resources/extensions/gsd/auto-prompts.ts +3 -0
  215. package/src/resources/extensions/gsd/auto-start.ts +12 -15
  216. package/src/resources/extensions/gsd/auto-tool-tracking.ts +19 -0
  217. package/src/resources/extensions/gsd/auto-unit-tool-scope.ts +10 -17
  218. package/src/resources/extensions/gsd/auto-worktree-repair.ts +13 -2
  219. package/src/resources/extensions/gsd/auto-worktree.ts +41 -364
  220. package/src/resources/extensions/gsd/auto.ts +20 -24
  221. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +3 -5
  222. package/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts +10 -6
  223. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +87 -6
  224. package/src/resources/extensions/gsd/bootstrap/write-gate.ts +29 -3
  225. package/src/resources/extensions/gsd/branch-patterns.ts +3 -0
  226. package/src/resources/extensions/gsd/captures.ts +5 -16
  227. package/src/resources/extensions/gsd/closeout-recovery.ts +2 -1
  228. package/src/resources/extensions/gsd/commands/catalog.ts +6 -68
  229. package/src/resources/extensions/gsd/crash-recovery.ts +3 -9
  230. package/src/resources/extensions/gsd/db/engine.ts +809 -0
  231. package/src/resources/extensions/gsd/db/queries.ts +453 -0
  232. package/src/resources/extensions/gsd/db/sql-constants.ts +12 -0
  233. package/src/resources/extensions/gsd/db/writers/cascades.ts +237 -0
  234. package/src/resources/extensions/gsd/db/writers/import-restore.ts +310 -0
  235. package/src/resources/extensions/gsd/db/writers/memory.ts +220 -0
  236. package/src/resources/extensions/gsd/db/writers/reconcile.ts +500 -0
  237. package/src/resources/extensions/gsd/db/writers/status.ts +88 -0
  238. package/src/resources/extensions/gsd/doctor-environment.ts +5 -13
  239. package/src/resources/extensions/gsd/doctor-format.ts +12 -7
  240. package/src/resources/extensions/gsd/doctor-git-checks.ts +3 -3
  241. package/src/resources/extensions/gsd/doctor-runtime-checks.ts +22 -17
  242. package/src/resources/extensions/gsd/error-classifier.ts +11 -0
  243. package/src/resources/extensions/gsd/git-service.ts +1 -0
  244. package/src/resources/extensions/gsd/gitignore.ts +3 -0
  245. package/src/resources/extensions/gsd/gsd-db.ts +173 -2373
  246. package/src/resources/extensions/gsd/guidance.ts +139 -0
  247. package/src/resources/extensions/gsd/guided-flow.ts +50 -5
  248. package/src/resources/extensions/gsd/mcp-tool-name.ts +6 -11
  249. package/src/resources/extensions/gsd/memory-consolidation-scanner.ts +1 -1
  250. package/src/resources/extensions/gsd/migrate/safety.ts +18 -7
  251. package/src/resources/extensions/gsd/migration-auto-check.ts +28 -3
  252. package/src/resources/extensions/gsd/model-cost-table.ts +1 -0
  253. package/src/resources/extensions/gsd/model-router.ts +3 -0
  254. package/src/resources/extensions/gsd/notification-store.ts +26 -3
  255. package/src/resources/extensions/gsd/parallel-merge.ts +12 -9
  256. package/src/resources/extensions/gsd/parallel-monitor-overlay.ts +10 -7
  257. package/src/resources/extensions/gsd/paths.ts +42 -22
  258. package/src/resources/extensions/gsd/pre-execution-checks.ts +109 -3
  259. package/src/resources/extensions/gsd/preferences-models.ts +10 -46
  260. package/src/resources/extensions/gsd/preferences.ts +18 -0
  261. package/src/resources/extensions/gsd/prompts/plan-slice.md +1 -1
  262. package/src/resources/extensions/gsd/prompts/refine-slice.md +1 -1
  263. package/src/resources/extensions/gsd/prompts/replan-slice.md +1 -1
  264. package/src/resources/extensions/gsd/provider-error-guidance.ts +4 -9
  265. package/src/resources/extensions/gsd/provider-switch-observer.ts +1 -1
  266. package/src/resources/extensions/gsd/publication.ts +122 -0
  267. package/src/resources/extensions/gsd/recovery-classification.ts +47 -88
  268. package/src/resources/extensions/gsd/safety/evidence-collector.ts +36 -4
  269. package/src/resources/extensions/gsd/safety/evidence-cross-ref.ts +7 -2
  270. package/src/resources/extensions/gsd/safety/file-change-validator.ts +14 -0
  271. package/src/resources/extensions/gsd/state-transition-matrix.ts +42 -0
  272. package/src/resources/extensions/gsd/state.ts +4 -21
  273. package/src/resources/extensions/gsd/status-guards.ts +59 -8
  274. package/src/resources/extensions/gsd/stop-notice.ts +75 -0
  275. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +123 -0
  276. package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +3 -1
  277. package/src/resources/extensions/gsd/tests/auto-post-unit-evidence-crossref-4909.test.ts +46 -0
  278. package/src/resources/extensions/gsd/tests/auto-worktree-registry.test.ts +2 -2
  279. package/src/resources/extensions/gsd/tests/auto-worktree-repair.test.ts +4 -2
  280. package/src/resources/extensions/gsd/tests/checkout-branch-stash-guard.test.ts +66 -1
  281. package/src/resources/extensions/gsd/tests/clear-stale-autostart.test.ts +44 -0
  282. package/src/resources/extensions/gsd/tests/commands-verdict.test.ts +8 -7
  283. package/src/resources/extensions/gsd/tests/evidence-xref-gsd-exec.test.ts +157 -0
  284. package/src/resources/extensions/gsd/tests/file-change-validator.test.ts +33 -1
  285. package/src/resources/extensions/gsd/tests/guidance.test.ts +125 -0
  286. package/src/resources/extensions/gsd/tests/integration/auto-worktree-milestone-merge.test.ts +51 -4
  287. package/src/resources/extensions/gsd/tests/integration/auto-worktree.test.ts +54 -1
  288. package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +3 -2
  289. package/src/resources/extensions/gsd/tests/migration-auto-check.test.ts +85 -1
  290. package/src/resources/extensions/gsd/tests/notification-store.test.ts +32 -0
  291. package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +193 -1
  292. package/src/resources/extensions/gsd/tests/provider-error-guidance.test.ts +3 -3
  293. package/src/resources/extensions/gsd/tests/publication.test.ts +120 -0
  294. package/src/resources/extensions/gsd/tests/recovery-classification-illegal-transition.test.ts +30 -0
  295. package/src/resources/extensions/gsd/tests/register-hooks-depth-verification.test.ts +248 -1
  296. package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +1 -0
  297. package/src/resources/extensions/gsd/tests/safety-harness-false-positives.test.ts +38 -0
  298. package/src/resources/extensions/gsd/tests/session-switch-clears-pending-autostart.test.ts +108 -0
  299. package/src/resources/extensions/gsd/tests/single-writer-invariant.test.ts +43 -6
  300. package/src/resources/extensions/gsd/tests/state-transition-matrix.test.ts +36 -0
  301. package/src/resources/extensions/gsd/tests/status-guards.test.ts +38 -0
  302. package/src/resources/extensions/gsd/tests/stop-notice.test.ts +70 -0
  303. package/src/resources/extensions/gsd/tests/tool-invocation-error-loop-break.test.ts +8 -0
  304. package/src/resources/extensions/gsd/tests/tool-surface-readiness.test.ts +155 -0
  305. package/src/resources/extensions/gsd/tests/unit-closeout.test.ts +209 -0
  306. package/src/resources/extensions/gsd/tests/unit-context-composer.test.ts +23 -2
  307. package/src/resources/extensions/gsd/tests/unit-registry.test.ts +163 -0
  308. package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +2 -2
  309. package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +2 -2
  310. package/src/resources/extensions/gsd/tests/worktree-lifecycle.test.ts +41 -4
  311. package/src/resources/extensions/gsd/tests/worktree-manager.test.ts +22 -1
  312. package/src/resources/extensions/gsd/tests/worktree-placement.test.ts +113 -0
  313. package/src/resources/extensions/gsd/tests/worktree-reentry.test.ts +1 -1
  314. package/src/resources/extensions/gsd/tests/worktree-safety.test.ts +3 -1
  315. package/src/resources/extensions/gsd/tests/worktree-symlink-removal.test.ts +12 -6
  316. package/src/resources/extensions/gsd/tests/worktree-teardown-safety.test.ts +2 -2
  317. package/src/resources/extensions/gsd/tests/write-gate.test.ts +42 -0
  318. package/src/resources/extensions/gsd/tool-surface-readiness.ts +76 -0
  319. package/src/resources/extensions/gsd/tools/complete-slice.ts +23 -58
  320. package/src/resources/extensions/gsd/tools/exec-tool.ts +5 -8
  321. package/src/resources/extensions/gsd/tools/plan-slice.ts +12 -6
  322. package/src/resources/extensions/gsd/tools/reopen-milestone.ts +11 -38
  323. package/src/resources/extensions/gsd/tools/reopen-slice.ts +14 -42
  324. package/src/resources/extensions/gsd/tools/skip-slice.ts +18 -44
  325. package/src/resources/extensions/gsd/undo.ts +9 -8
  326. package/src/resources/extensions/gsd/unit-closeout.ts +201 -0
  327. package/src/resources/extensions/gsd/unit-context-composer.ts +12 -1
  328. package/src/resources/extensions/gsd/unit-context-manifest.ts +4 -28
  329. package/src/resources/extensions/gsd/unit-registry.ts +425 -0
  330. package/src/resources/extensions/gsd/unit-tool-contracts.ts +27 -192
  331. package/src/resources/extensions/gsd/workflow-tool-surface.ts +4 -1
  332. package/src/resources/extensions/gsd/worktree-git-recovery.ts +314 -0
  333. package/src/resources/extensions/gsd/worktree-lifecycle.ts +10 -1
  334. package/src/resources/extensions/gsd/worktree-manager.ts +47 -28
  335. package/src/resources/extensions/gsd/worktree-placement.ts +63 -0
  336. package/src/resources/extensions/gsd/worktree-reentry.ts +10 -7
  337. package/src/resources/extensions/gsd/worktree-root.ts +29 -6
  338. package/src/resources/extensions/gsd/worktree-safety.ts +8 -5
  339. package/src/resources/extensions/gsd/worktree-session-state.ts +11 -11
  340. package/src/resources/skills/gsd-browser/SKILL.md +1 -1
  341. /package/dist/web/standalone/.next/static/{tkLHUSzPA2kMmWz4DmGwI → 2p9Rv9pQflAxCBbGVI2vb}/_buildManifest.js +0 -0
  342. /package/dist/web/standalone/.next/static/{tkLHUSzPA2kMmWz4DmGwI → 2p9Rv9pQflAxCBbGVI2vb}/_ssgManifest.js +0 -0
@@ -0,0 +1,314 @@
1
+ // Project/App: gsd-pi
2
+ // File Purpose: Git checkout/stash/merge-state recovery primitives for worktree operations.
3
+
4
+ /**
5
+ * Worktree Git Recovery — the recurring-bug hot spot, in one place.
6
+ *
7
+ * Owns the verbs that recover a repository from interrupted or conflicting
8
+ * git operations during worktree transitions:
9
+ *
10
+ * - `checkoutBranchWithStashGuard` — branch switch with stash protection,
11
+ * including the stash-pop EEXIST collision recovery for untracked files
12
+ * (force-checkout + targeted stash drop; #645 broadened it beyond `.gsd/`,
13
+ * guarded by "no non-.gsd unmerged entries remain").
14
+ * - `removeMergeStateFiles` — clears SQUASH_MSG / MERGE_HEAD / etc. left by
15
+ * a failed merge so subsequent merges don't fail on stale state.
16
+ * - `cleanupConflictState` — merge-abort + index reset + state-file cleanup
17
+ * after a conflicted (including squash) merge.
18
+ * - stash helpers (`popStashByRef`, `stashRefFromError`,
19
+ * `stashAlreadyExistsFilesFromError`, `gsdJsonlFilesWithConflictMarkers`)
20
+ * used by the merge pipeline in auto-worktree.ts.
21
+ *
22
+ * Extracted from auto-worktree.ts so recovery fixes land here instead of as
23
+ * embedded special cases in a 2,600-line orchestration module, and so the
24
+ * rules can be tested against scripted git states.
25
+ *
26
+ * The State Reconciliation drift repair (`state-reconciliation/drift/
27
+ * merge-state.ts`) keeps its own merge-state primitive by design — drift
28
+ * repairs own their raw primitives (see CONTEXT.md, Drift repair).
29
+ */
30
+
31
+ import { execFileSync } from "node:child_process";
32
+ import { existsSync, readFileSync, unlinkSync } from "node:fs";
33
+ import { isAbsolute, join, resolve } from "node:path";
34
+
35
+ import { debugLog } from "./debug-logger.js";
36
+ import { logError, logWarning } from "./workflow-logger.js";
37
+ import {
38
+ nativeAddPaths,
39
+ nativeCheckoutBranch,
40
+ nativeConflictFiles,
41
+ nativeLsFiles,
42
+ nativeMergeAbort,
43
+ nativeWorkingTreeStatus,
44
+ } from "./native-git-bridge.js";
45
+ import { resolveGitDir } from "./worktree-manager.js";
46
+
47
+ /**
48
+ * Pop the stash entry created with `stashMarker` in its subject, resolving it
49
+ * to a concrete `stash@{n}` ref first so a concurrent stash push cannot make
50
+ * `git stash pop` grab the wrong entry.
51
+ *
52
+ * If `stashMarker` is null or no longer present in the stash list (e.g. a
53
+ * concurrent process popped/dropped it), leaves the stash list untouched and
54
+ * returns null.
55
+ *
56
+ * Throws on pop failure so callers can handle conflict cases the same way
57
+ * they would with the prior `git stash pop` form. When throwing after a
58
+ * targeted pop attempt, the error is annotated with the targeted stash ref.
59
+ *
60
+ * (Issue #4980 HIGH-6)
61
+ */
62
+ export function popStashByRef(basePath: string, stashMarker: string | null): string | null {
63
+ let popArg: string | null = null;
64
+ if (stashMarker) {
65
+ try {
66
+ const list = execFileSync("git", ["stash", "list", "--format=%gd%x00%s"], {
67
+ cwd: basePath,
68
+ stdio: ["ignore", "pipe", "pipe"],
69
+ encoding: "utf-8",
70
+ }).trim().split("\n").filter(Boolean);
71
+ for (const entry of list) {
72
+ const [ref, subject] = entry.split("\0");
73
+ if (ref && subject?.includes(stashMarker)) {
74
+ popArg = ref;
75
+ break;
76
+ }
77
+ }
78
+ } catch (err) {
79
+ logWarning("worktree", `stash list lookup failed; leaving stash untouched: ${err instanceof Error ? err.message : String(err)}`);
80
+ }
81
+ }
82
+ if (!popArg) {
83
+ logWarning("worktree", "recorded stash entry could not be resolved; skipping automatic pop");
84
+ return null;
85
+ }
86
+ try {
87
+ execFileSync("git", ["stash", "pop", popArg], {
88
+ cwd: basePath,
89
+ stdio: ["ignore", "pipe", "pipe"],
90
+ encoding: "utf-8",
91
+ });
92
+ } catch (err) {
93
+ if (err && typeof err === "object") {
94
+ (err as { stashRef?: string }).stashRef = popArg;
95
+ }
96
+ throw err;
97
+ }
98
+ return popArg;
99
+ }
100
+
101
+ /**
102
+ * Extract a stash ref annotation injected by popStashByRef() when git stash
103
+ * pop fails and we need to conditionally drop the exact stash entry later.
104
+ */
105
+ export function stashRefFromError(err: unknown): string | null {
106
+ if (!err || typeof err !== "object") return null;
107
+ const stashRef = (err as { stashRef?: unknown }).stashRef;
108
+ return typeof stashRef === "string" && stashRef.length > 0 ? stashRef : null;
109
+ }
110
+
111
+ export function stashAlreadyExistsFilesFromError(err: unknown): string[] {
112
+ if (!err || typeof err !== "object") return [];
113
+ const stderr = (err as { stderr?: unknown }).stderr;
114
+ const stderrText = typeof stderr === "string"
115
+ ? stderr
116
+ : stderr instanceof Uint8Array
117
+ ? Buffer.from(stderr).toString("utf-8")
118
+ : "";
119
+ const message = err instanceof Error ? err.message : String(err);
120
+ const text = `${stderrText}\n${message}`;
121
+ const files = new Set<string>();
122
+ for (const line of text.split("\n")) {
123
+ const m = line.match(/^(.*?)\s+already exists, no checkout\s*$/i);
124
+ if (!m) continue;
125
+ const filePath = m[1]?.trim();
126
+ if (filePath) files.add(filePath);
127
+ }
128
+ return [...files];
129
+ }
130
+
131
+ /**
132
+ * Detect whether an on-disk file still contains unresolved merge conflict
133
+ * markers from a failed stash-pop or merge attempt.
134
+ *
135
+ * Returns false when the file cannot be read.
136
+ */
137
+ export function hasConflictMarkers(filePath: string): boolean {
138
+ try {
139
+ const content = readFileSync(filePath, "utf-8");
140
+ return content.includes("<<<<<<<") && content.includes("=======") && content.includes(">>>>>>>");
141
+ } catch {
142
+ return false;
143
+ }
144
+ }
145
+
146
+ export function gsdJsonlFilesWithConflictMarkers(basePath: string): string[] {
147
+ return nativeLsFiles(basePath, ".gsd/*.jsonl").filter((f) =>
148
+ hasConflictMarkers(join(basePath, f)),
149
+ );
150
+ }
151
+
152
+ export function removeMergeStateFiles(basePath: string, contextLabel: string): void {
153
+ try {
154
+ for (const f of ["SQUASH_MSG", "MERGE_MSG", "MERGE_MODE", "MERGE_HEAD", "AUTO_MERGE"]) {
155
+ const rawPath = execFileSync("git", ["rev-parse", "--git-path", f], {
156
+ cwd: basePath,
157
+ stdio: ["ignore", "pipe", "pipe"],
158
+ encoding: "utf-8",
159
+ }).trim();
160
+ const p = rawPath.length > 0
161
+ ? (isAbsolute(rawPath) ? rawPath : resolve(basePath, rawPath))
162
+ : join(resolveGitDir(basePath), f);
163
+ if (existsSync(p)) unlinkSync(p);
164
+ }
165
+ } catch (err) {
166
+ logError("worktree", `${contextLabel} merge state cleanup failed: ${err instanceof Error ? err.message : String(err)}`);
167
+ }
168
+ }
169
+
170
+ export function cleanupConflictState(basePath: string): void {
171
+ // Merge conflicts can leave unmerged index entries; merge-abort alone is not
172
+ // enough for squash merges (MERGE_HEAD is never written). Reset the merge
173
+ // index, then remove merge message files that native/libgit2 paths may have
174
+ // created.
175
+ try {
176
+ nativeMergeAbort(basePath);
177
+ } catch (err) {
178
+ // MERGE_HEAD absent (squash merge path) — abort is a no-op, which is fine.
179
+ debugLog("conflict-cleanup:merge-abort-skipped", {
180
+ error: err instanceof Error ? err.message : String(err),
181
+ });
182
+ }
183
+ try {
184
+ execFileSync("git", ["reset", "--merge"], {
185
+ cwd: basePath,
186
+ stdio: ["ignore", "pipe", "pipe"],
187
+ encoding: "utf-8",
188
+ });
189
+ } catch (err) {
190
+ logError("worktree", `git reset --merge failed after merge conflict: ${err instanceof Error ? err.message : String(err)}`);
191
+ }
192
+ removeMergeStateFiles(basePath, "conflict");
193
+ }
194
+
195
+ export function checkoutBranchWithStashGuard(
196
+ basePath: string,
197
+ branch: string,
198
+ reason: string,
199
+ ): void {
200
+ let stashMarker: string | null = null;
201
+ let stashed = false;
202
+
203
+ const status = nativeWorkingTreeStatus(basePath).trim();
204
+ if (status.length > 0) {
205
+ stashMarker = `gsd-checkout-stash:${reason}:${process.pid}:${Date.now()}:${process.hrtime.bigint().toString(36)}`;
206
+ const stashListBefore = execFileSync("git", ["stash", "list"], {
207
+ cwd: basePath,
208
+ stdio: ["ignore", "pipe", "pipe"],
209
+ encoding: "utf-8",
210
+ });
211
+ execFileSync(
212
+ "git",
213
+ ["stash", "push", "--include-untracked", "-m", `gsd: checkout stash [${stashMarker}]`],
214
+ {
215
+ cwd: basePath,
216
+ stdio: ["ignore", "pipe", "pipe"],
217
+ encoding: "utf-8",
218
+ },
219
+ );
220
+ const stashListAfter = execFileSync("git", ["stash", "list"], {
221
+ cwd: basePath,
222
+ stdio: ["ignore", "pipe", "pipe"],
223
+ encoding: "utf-8",
224
+ });
225
+ stashed = stashListAfter !== stashListBefore;
226
+ }
227
+
228
+ // Checkout and stash-restore are split so we can distinguish two failure
229
+ // modes: (a) checkout failed → HEAD did not move, restore stash and rethrow;
230
+ // (b) checkout succeeded but stash pop failed → HEAD moved to `branch` but
231
+ // the working-tree changes remain in the stash list. We surface a distinct
232
+ // error in case (b) so callers don't assume the branch switch was rolled back.
233
+ try {
234
+ nativeCheckoutBranch(basePath, branch);
235
+ } catch (checkoutErr) {
236
+ if (stashed) {
237
+ try {
238
+ popStashByRef(basePath, stashMarker);
239
+ } catch (restoreErr) {
240
+ logWarning("worktree", `git stash pop failed during checkout restore: ${restoreErr instanceof Error ? restoreErr.message : String(restoreErr)}`);
241
+ }
242
+ }
243
+ throw checkoutErr;
244
+ }
245
+
246
+ if (stashed) {
247
+ try {
248
+ popStashByRef(basePath, stashMarker);
249
+ } catch (popErr) {
250
+ const msg = popErr instanceof Error ? popErr.message : String(popErr);
251
+ const stderr = popErr && typeof popErr === "object"
252
+ ? (popErr as { stderr?: unknown }).stderr
253
+ : undefined;
254
+ const stderrText = typeof stderr === "string"
255
+ ? stderr
256
+ : stderr instanceof Uint8Array
257
+ ? Buffer.from(stderr).toString("utf-8")
258
+ : "";
259
+ const stashPopMessage = `${stderrText}\n${msg}`.trim();
260
+ const alreadyExists = stashAlreadyExistsFilesFromError(popErr);
261
+ const isUntrackedRestoreFailure = stashPopMessage.includes("could not restore untracked files from stash");
262
+ const stashRefForDrop = stashRefFromError(popErr);
263
+ const allConflictFiles = nativeConflictFiles(basePath);
264
+ const nonGsdUnmerged = allConflictFiles.filter((f) => !f.startsWith(".gsd/"));
265
+ const gsdUnmerged = allConflictFiles.filter((f) => f.startsWith(".gsd/"));
266
+ const gsdContentConflicts = isUntrackedRestoreFailure
267
+ ? gsdJsonlFilesWithConflictMarkers(basePath)
268
+ : [];
269
+ // Resolve ALL untracked-collision files by accepting HEAD — files in
270
+ // alreadyExists were untracked on the source branch by definition of the
271
+ // "already exists, no checkout" failure, so target HEAD is authoritative.
272
+ // gsdUnmerged: .gsd/ index conflicts left by the partial stash pop are
273
+ // also resolved via HEAD — .gsd/ runtime state is always authoritative
274
+ // on the target branch, so accepting HEAD is safe here too.
275
+ const resolvable = [...new Set([...alreadyExists, ...gsdContentConflicts, ...gsdUnmerged])];
276
+
277
+ if (
278
+ isUntrackedRestoreFailure &&
279
+ resolvable.length > 0 &&
280
+ nonGsdUnmerged.length === 0
281
+ ) {
282
+ for (const f of resolvable) {
283
+ execFileSync("git", ["checkout", "HEAD", "--", f], {
284
+ cwd: basePath,
285
+ stdio: ["ignore", "pipe", "pipe"],
286
+ encoding: "utf-8",
287
+ });
288
+ nativeAddPaths(basePath, [f]);
289
+ }
290
+
291
+ if (stashRefForDrop) {
292
+ try {
293
+ execFileSync("git", ["stash", "drop", stashRefForDrop], {
294
+ cwd: basePath,
295
+ stdio: ["ignore", "pipe", "pipe"],
296
+ encoding: "utf-8",
297
+ });
298
+ } catch (err) { /* stash may already be consumed */
299
+ logWarning("worktree", `git stash drop failed: ${err instanceof Error ? err.message : String(err)}`);
300
+ }
301
+ } else {
302
+ logWarning("worktree", "recorded stash entry could not be resolved; skipping automatic drop");
303
+ }
304
+ return;
305
+ }
306
+
307
+ const wrapped = new Error(
308
+ `checkout to '${branch}' succeeded but stash restore failed; working tree changes remain in the stash list. Original error: ${msg}`,
309
+ );
310
+ if (stashRefForDrop) (wrapped as { stashRef?: string }).stashRef = stashRefForDrop;
311
+ throw wrapped;
312
+ }
313
+ }
314
+ }
@@ -675,7 +675,16 @@ export function _enterMilestoneCore(
675
675
  // Handles the case where originalBasePath is falsy and basePath is itself
676
676
  // a worktree path — prevents double-nested worktree paths (#3729).
677
677
  const basePath = resolveWorktreeProjectRoot(s.basePath, s.originalBasePath);
678
- const mode = opts.modeOverride ?? getIsolationMode(basePath);
678
+ // A stranded-recovery session that adopted the milestone branch in the
679
+ // project root must keep re-entering in that mode: the root checkout holds
680
+ // the branch, so creating the canonical worktree would fail with "already
681
+ // in use by another worktree". The override clears when the recovered
682
+ // milestone merges (_mergeAndExit), restoring configured isolation for
683
+ // subsequent milestones.
684
+ const mode =
685
+ opts.modeOverride ??
686
+ s.strandedRecoveryIsolationMode ??
687
+ getIsolationMode(basePath);
679
688
 
680
689
  if (s.isolationDegraded) {
681
690
  if (mode === "worktree") {
@@ -4,7 +4,8 @@
4
4
  /**
5
5
  * GSD Worktree Manager
6
6
  *
7
- * Creates and manages git worktrees under .gsd/worktrees/<name>/.
7
+ * Creates and manages git worktrees under .gsd-worktrees/<name>/ (canonical;
8
+ * legacy .gsd/worktrees/<name>/ stays recognized — see worktree-placement.ts).
8
9
  * Each worktree gets its own branch (worktree/<name>) and a full
9
10
  * working copy of the project, enabling parallel work streams.
10
11
  *
@@ -12,7 +13,7 @@
12
13
  * the main branch, then dispatches an LLM-guided merge flow.
13
14
  *
14
15
  * Flow:
15
- * 1. create() — git worktree add .gsd/worktrees/<name> -b worktree/<name>
16
+ * 1. create() — git worktree add .gsd-worktrees/<name> -b worktree/<name>
16
17
  * 2. user works in the worktree (new plans, milestones, etc.)
17
18
  * 3. merge() — LLM-guided reconciliation of .gsd/ artifacts back to main
18
19
  * 4. remove() — git worktree remove + branch cleanup
@@ -48,6 +49,7 @@ import {
48
49
  normalizeWorktreePathForCompare,
49
50
  resolveWorktreeProjectRoot,
50
51
  } from "./worktree-root.js";
52
+ import { canonicalWorktreesDir, worktreePathFor, worktreesDirs } from "./worktree-placement.js";
51
53
 
52
54
  // ─── Types ─────────────────────────────────────────────────────────────────
53
55
 
@@ -138,12 +140,20 @@ export function resolveGitDir(basePath: string): string {
138
140
  return gitPath;
139
141
  }
140
142
 
143
+ /** Canonical container for new worktrees. For scans that must also see legacy
144
+ * worktrees, use allWorktreesDirs(). */
141
145
  export function worktreesDir(basePath: string): string {
142
- return join(resolveWorktreeProjectRoot(basePath), ".gsd", "worktrees");
146
+ return canonicalWorktreesDir(resolveWorktreeProjectRoot(basePath));
143
147
  }
144
148
 
149
+ /** Every container a GSD worktree may live in (canonical + legacy), canonical first. */
150
+ export function allWorktreesDirs(basePath: string): string[] {
151
+ return worktreesDirs(resolveWorktreeProjectRoot(basePath));
152
+ }
153
+
154
+ /** Path for worktree `name` — an existing legacy worktree keeps its location. */
145
155
  export function worktreePath(basePath: string, name: string): string {
146
- return join(worktreesDir(basePath), name);
156
+ return worktreePathFor(resolveWorktreeProjectRoot(basePath), name);
147
157
  }
148
158
 
149
159
  export function worktreeBranchName(name: string): string {
@@ -151,20 +161,22 @@ export function worktreeBranchName(name: string): string {
151
161
  }
152
162
 
153
163
  /**
154
- * Validate that a path is inside the .gsd/worktrees/ directory.
155
- * Resolves symlinks and normalizes ".." traversals before comparison
156
- * so that a symlink-resolved or crafted path cannot escape containment.
164
+ * Validate that a path is inside a GSD worktrees container (canonical
165
+ * .gsd-worktrees/ or legacy .gsd/worktrees/). Resolves symlinks and
166
+ * normalizes ".." traversals before comparison so that a symlink-resolved
167
+ * or crafted path cannot escape containment.
157
168
  *
158
169
  * Used as a safety gate before any destructive operation (rmSync,
159
170
  * nativeWorktreeRemove --force) to prevent #2365-style data loss.
160
171
  */
161
172
  export function isInsideWorktreesDir(basePath: string, targetPath: string): boolean {
162
- const wtDirPath = worktreesDir(basePath);
163
- const wtDir = existsSync(wtDirPath) ? realpathSync(wtDirPath) : resolve(wtDirPath);
164
173
  const resolved = existsSync(targetPath) ? realpathSync(targetPath) : resolve(targetPath);
165
- // The resolved path must start with the worktrees dir followed by a separator,
166
- // not merely be a prefix match (e.g. ".gsd/worktrees-extra" must not match).
167
- return resolved === wtDir || resolved.startsWith(wtDir + sep);
174
+ return allWorktreesDirs(basePath).some((wtDirPath) => {
175
+ const wtDir = existsSync(wtDirPath) ? realpathSync(wtDirPath) : resolve(wtDirPath);
176
+ // The resolved path must start with the worktrees dir followed by a separator,
177
+ // not merely be a prefix match (e.g. ".gsd/worktrees-extra" must not match).
178
+ return resolved === wtDir || resolved.startsWith(wtDir + sep);
179
+ });
168
180
  }
169
181
 
170
182
  function isRegisteredGitWorktreeAtPath(basePath: string, wtPath: string): boolean {
@@ -277,12 +289,12 @@ export function buildManualValidationGuidance(
277
289
  ): string | null {
278
290
  if (!milestoneId) return null;
279
291
  const validationRoot = resolveCanonicalMilestoneRoot(basePath, milestoneId);
280
- const inWorktree = validationRoot.includes(`${sep}.gsd${sep}worktrees${sep}`);
292
+ const inWorktree = isGsdWorktreePath(validationRoot);
281
293
  const lines: string[] = [`Validate the work here: ${validationRoot}`];
282
294
  if (inWorktree) {
283
295
  lines.push(
284
- "This milestone runs in a git worktree, so the code lives under the hidden " +
285
- `\`.gsd/worktrees/\` path. Open it with: cd "${validationRoot}"`,
296
+ "This milestone runs in a git worktree, so the code lives under the " +
297
+ `GSD worktrees directory. Open it with: cd "${validationRoot}"`,
286
298
  );
287
299
  }
288
300
  if (opts.uatPath) {
@@ -307,24 +319,33 @@ export function createWorktree(basePath: string, name: string, opts: { branch?:
307
319
  throw new GSDError(GSD_PARSE_ERROR, `Invalid worktree name "${name}". Use only letters, numbers, hyphens, and underscores.`);
308
320
  }
309
321
 
310
- const wtPath = worktreePath(basePath, name);
322
+ const existingPath = worktreePath(basePath, name);
311
323
  const branch = opts.branch ?? worktreeBranchName(name);
312
324
 
313
- if (existsSync(wtPath)) {
325
+ if (existsSync(existingPath)) {
314
326
  // A valid git worktree is registered in `git worktree list` and has a .git
315
327
  // *file* with a gitdir: pointer. Leftover directories (no .git, a standalone
316
328
  // .git directory from accidental `git init`, or an orphan pointer) block
317
329
  // creation unless removed.
318
- if (isRegisteredGitWorktreeAtPath(basePath, wtPath)) {
319
- throw new GSDError(GSD_STALE_STATE, `Worktree "${name}" already exists at ${wtPath}`);
330
+ if (isRegisteredGitWorktreeAtPath(basePath, existingPath)) {
331
+ throw new GSDError(GSD_STALE_STATE, `Worktree "${name}" already exists at ${existingPath}`);
320
332
  }
321
- removeStaleWorktreeDirectory(wtPath, name);
333
+ removeStaleWorktreeDirectory(existingPath, name);
322
334
  }
323
335
 
324
- // Ensure the .gsd/worktrees/ directory exists
336
+ // New worktrees always land in the canonical container, even when a stale
337
+ // legacy directory was just cleaned up.
325
338
  const wtDir = worktreesDir(basePath);
339
+ const wtPath = join(wtDir, name);
326
340
  mkdirSync(wtDir, { recursive: true });
327
341
 
342
+ // When existingPath resolved to a legacy location, the canonical target may
343
+ // still hold a stale directory from a prior aborted creation (no .git marker).
344
+ // Remove it so git worktree add does not fail with "path already exists".
345
+ if (existingPath !== wtPath && existsSync(wtPath) && !isRegisteredGitWorktreeAtPath(basePath, wtPath)) {
346
+ removeStaleWorktreeDirectory(wtPath, name);
347
+ }
348
+
328
349
  // Prune any stale worktree entries from a previous removal
329
350
  nativeWorktreePrune(basePath);
330
351
 
@@ -405,7 +426,8 @@ export function createWorktree(basePath: string, name: string, opts: { branch?:
405
426
 
406
427
  /**
407
428
  * List all GSD-managed worktrees.
408
- * Uses native worktree list and filters to those under .gsd/worktrees/.
429
+ * Uses native worktree list and filters to those under a GSD worktrees
430
+ * container (canonical .gsd-worktrees/ or legacy .gsd/worktrees/).
409
431
  */
410
432
  export function listWorktrees(basePath: string): WorktreeInfo[] {
411
433
  basePath = normalizeBasePathForWorktreeOps(basePath);
@@ -416,12 +438,8 @@ export function listWorktrees(basePath: string): WorktreeInfo[] {
416
438
  }
417
439
  const seenRoots = new Set<string>();
418
440
  const worktreeRoots = baseVariants
419
- .map(baseVariant => {
420
- const path = join(baseVariant, ".gsd", "worktrees");
421
- return {
422
- normalized: normalizePathForComparison(path),
423
- };
424
- })
441
+ .flatMap(baseVariant => worktreesDirs(baseVariant))
442
+ .map(path => ({ normalized: normalizePathForComparison(path) }))
425
443
  .filter(root => {
426
444
  if (seenRoots.has(root.normalized)) return false;
427
445
  seenRoots.add(root.normalized);
@@ -794,6 +812,7 @@ export function removeWorktree(
794
812
  * This module uses a split representation (paths/exact/prefixes) for efficient matching.
795
813
  */
796
814
  const SKIP_PATHS = [
815
+ ".gsd-worktrees/",
797
816
  ".gsd/worktrees/",
798
817
  ".gsd/runtime/",
799
818
  ".gsd/activity/",
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Worktree Placement module — owns WHERE GSD worktrees physically live.
3
+ *
4
+ * Canonical placement: `<projectRoot>/.gsd-worktrees/<name>` — a real
5
+ * directory that never crosses the `.gsd` symlink managed by repo-identity.
6
+ * Under the external-state layout (`.gsd → ~/.gsd/projects/<hash>/`), the
7
+ * legacy `.gsd/worktrees/` location materialised worktrees inside the user's
8
+ * home directory behind an opaque hash path; the canonical sibling keeps the
9
+ * working copy at the project root regardless of where `.gsd` state lives.
10
+ *
11
+ * Legacy placement (`<projectRoot>/.gsd/worktrees/<name>`, possibly resolving
12
+ * through the symlink to `~/.gsd/projects/<hash>/worktrees/<name>`) stays
13
+ * recognized so in-flight milestones keep working across upgrades:
14
+ * - creation always uses the canonical location;
15
+ * - resolution prefers an existing legacy worktree for the same name;
16
+ * - containment checks accept membership in either location.
17
+ *
18
+ * Path → project identification lives in worktree-root.ts; this module owns
19
+ * only the forward direction (project + name → physical path).
20
+ */
21
+
22
+ import { existsSync } from "node:fs";
23
+ import { join } from "node:path";
24
+
25
+ /** Directory name of the canonical worktrees container at the project root. */
26
+ export const CANONICAL_WORKTREES_DIRNAME = ".gsd-worktrees";
27
+
28
+ /** Canonical container for newly created worktrees. Never crosses the `.gsd` symlink. */
29
+ export function canonicalWorktreesDir(projectRoot: string): string {
30
+ return join(projectRoot, CANONICAL_WORKTREES_DIRNAME);
31
+ }
32
+
33
+ /** Legacy container (`.gsd/worktrees/`) — may resolve through the external-state symlink. */
34
+ export function legacyWorktreesDir(projectRoot: string): string {
35
+ return join(projectRoot, ".gsd", "worktrees");
36
+ }
37
+
38
+ /**
39
+ * All containers a GSD worktree may live in, canonical first.
40
+ * Use for listing, scanning, and containment checks.
41
+ */
42
+ export function worktreesDirs(projectRoot: string): string[] {
43
+ return [canonicalWorktreesDir(projectRoot), legacyWorktreesDir(projectRoot)];
44
+ }
45
+
46
+ /**
47
+ * Physical path for the worktree `name` under `projectRoot`.
48
+ *
49
+ * Canonical wins only when it has a `.git` marker (i.e. is a live worktree).
50
+ * A bare directory with no `.git` — a stale leftover from an aborted create
51
+ * or a partial cleanup — does not shadow an existing legacy worktree, so the
52
+ * legacy path is tried next. The legacy check remains a plain existsSync;
53
+ * callers that need a *registered* worktree validate the `.git` marker
54
+ * themselves (resolveCanonicalMilestoneRoot, getAutoWorktreePath).
55
+ * Falls back to canonical for new-worktree creation when neither path exists.
56
+ */
57
+ export function worktreePathFor(projectRoot: string, name: string): string {
58
+ const canonical = join(canonicalWorktreesDir(projectRoot), name);
59
+ if (existsSync(canonical) && existsSync(join(canonical, ".git"))) return canonical;
60
+ const legacy = join(legacyWorktreesDir(projectRoot), name);
61
+ if (existsSync(legacy)) return legacy;
62
+ return canonical;
63
+ }
@@ -10,7 +10,7 @@ import { readdirSync } from "node:fs";
10
10
 
11
11
  import { enterAutoWorktree, getAutoWorktreePath } from "./auto-worktree.js";
12
12
  import { getIsolationMode } from "./preferences.js";
13
- import { worktreesDir } from "./worktree-manager.js";
13
+ import { allWorktreesDirs } from "./worktree-manager.js";
14
14
  import { isGsdWorktreePath, resolveWorktreeProjectRoot } from "./worktree-root.js";
15
15
 
16
16
  interface LiveWorktree {
@@ -19,15 +19,18 @@ interface LiveWorktree {
19
19
  }
20
20
 
21
21
  /**
22
- * Enumerate the live (valid git) auto-worktrees under <projectRoot>/.gsd/worktrees/.
22
+ * Enumerate the live (valid git) auto-worktrees in the project's worktree
23
+ * containers (canonical .gsd-worktrees/ and legacy .gsd/worktrees/).
23
24
  * Reuses getAutoWorktreePath's validation so stray directories are ignored.
24
25
  */
25
26
  function liveMilestoneWorktrees(projectRoot: string): LiveWorktree[] {
26
- let names: string[];
27
- try {
28
- names = readdirSync(worktreesDir(projectRoot));
29
- } catch {
30
- return [];
27
+ const names = new Set<string>();
28
+ for (const dir of allWorktreesDirs(projectRoot)) {
29
+ try {
30
+ for (const name of readdirSync(dir)) names.add(name);
31
+ } catch {
32
+ // container absent — skip
33
+ }
31
34
  }
32
35
  const live: LiveWorktree[] = [];
33
36
  for (const id of names) {
@@ -20,10 +20,17 @@ export function normalizeWorktreePathForCompare(path: string): string {
20
20
  }
21
21
 
22
22
  /**
23
- * Find the GSD worktree segment in both direct project layout and the
24
- * symlink-resolved external-state layout used by ~/.gsd/projects/<hash>.
23
+ * Find the GSD worktree segment in the canonical layout (.gsd-worktrees/),
24
+ * the legacy direct layout (.gsd/worktrees/), and the symlink-resolved
25
+ * external-state layout used by ~/.gsd/projects/<hash>.
25
26
  */
26
27
  export function findWorktreeSegment(normalizedPath: string): WorktreeSegment | null {
28
+ const canonicalMarker = "/.gsd-worktrees/";
29
+ const canonicalIdx = normalizedPath.indexOf(canonicalMarker);
30
+ if (canonicalIdx !== -1) {
31
+ return { gsdIdx: canonicalIdx, afterWorktrees: canonicalIdx + canonicalMarker.length };
32
+ }
33
+
27
34
  const directMarker = "/.gsd/worktrees/";
28
35
  const directIdx = normalizedPath.indexOf(directMarker);
29
36
  if (directIdx !== -1) {
@@ -62,6 +69,18 @@ export function isGsdWorktreePath(path: string): boolean {
62
69
  return findWorktreeSegment(path.replaceAll("\\", "/")) !== null;
63
70
  }
64
71
 
72
+ /**
73
+ * Project-root prefix of a GSD worktree path, or null when the path is not
74
+ * inside a recognized worktree layout. Pure string split — no env handling,
75
+ * HOME guard, or filesystem fallbacks (resolveWorktreeProjectRoot adds
76
+ * those). Separator normalization is 1:1 on characters, so the prefix is
77
+ * sliced from the ORIGINAL string and keeps its separators.
78
+ */
79
+ export function projectRootFromWorktreePath(path: string): string | null {
80
+ const segment = findWorktreeSegment(path.replaceAll("\\", "/"));
81
+ return segment ? path.slice(0, segment.gsdIdx) : null;
82
+ }
83
+
65
84
  /**
66
85
  * When a milestone worktree lives under the external-state layout
67
86
  * (`<gsdHome>/projects/<hash>/worktrees/<MID>/`, or the `GSD_STATE_DIR`
@@ -72,7 +91,8 @@ export function isGsdWorktreePath(path: string): boolean {
72
91
  export function resolveExternalStateProjectGsdFromWorktreePath(projectPath: string): string | null {
73
92
  const normalized = resolve(projectPath).replaceAll("\\", "/");
74
93
 
75
- // Direct layout — parent state is resolved via repoIdentity(git root).
94
+ // Canonical/direct layouts — parent state is resolved via repoIdentity(git root).
95
+ if (normalized.includes("/.gsd-worktrees/")) return null;
76
96
  if (normalized.includes("/.gsd/worktrees/") && !normalized.includes("/.gsd/projects/")) {
77
97
  return null;
78
98
  }
@@ -139,12 +159,15 @@ function resolveProjectRootFromPath(path: string): string {
139
159
  return resolveNearestBootstrappedGsdRoot(path) ?? resolveGitWorkingTreeRoot(path) ?? path;
140
160
  }
141
161
 
162
+ // Slice at the first `.gsd` boundary, but never past the worktree segment:
163
+ // in the canonical layout (`<root>/.gsd-worktrees/<MID>/.gsd/...`) the first
164
+ // `/.gsd/` occurrence is the worktree-local projection dir, not the project
165
+ // root's state dir.
142
166
  const sepChar = path.includes("\\") ? "\\" : "/";
143
167
  const gsdMarker = `${sepChar}.gsd${sepChar}`;
144
168
  const markerIdx = path.indexOf(gsdMarker);
145
- const candidate = markerIdx !== -1
146
- ? path.slice(0, markerIdx)
147
- : path.slice(0, segment.gsdIdx);
169
+ const sliceIdx = markerIdx !== -1 ? Math.min(markerIdx, segment.gsdIdx) : segment.gsdIdx;
170
+ const candidate = path.slice(0, sliceIdx);
148
171
 
149
172
  const gsdHomeNorm = normalizeWorktreePathForCompare(gsdHome());
150
173
  const candidateGsdPath = normalizeWorktreePathForCompare(join(candidate, ".gsd"));