gsd-pi 2.82.0-dev.2841a1e44 → 2.82.0-dev.3a3c6509d

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 (377) hide show
  1. package/README.md +3 -3
  2. package/dist/resources/.managed-resources-content-hash +1 -1
  3. package/dist/resources/GSD-WORKFLOW.md +7 -0
  4. package/dist/resources/extensions/claude-code-cli/partial-builder.js +2 -1
  5. package/dist/resources/extensions/claude-code-cli/stream-adapter.js +1 -1
  6. package/dist/resources/extensions/gsd/auto/infra-errors.js +9 -3
  7. package/dist/resources/extensions/gsd/auto/loop.js +5 -5
  8. package/dist/resources/extensions/gsd/auto/orchestrator.js +11 -0
  9. package/dist/resources/extensions/gsd/auto/phases.js +81 -31
  10. package/dist/resources/extensions/gsd/auto/workflow-memory-pressure.js +12 -0
  11. package/dist/resources/extensions/gsd/auto-dashboard.js +66 -1
  12. package/dist/resources/extensions/gsd/auto-direct-dispatch.js +1 -0
  13. package/dist/resources/extensions/gsd/auto-dispatch.js +20 -19
  14. package/dist/resources/extensions/gsd/auto-model-selection.js +2 -0
  15. package/dist/resources/extensions/gsd/auto-post-unit.js +71 -10
  16. package/dist/resources/extensions/gsd/auto-recovery.js +71 -14
  17. package/dist/resources/extensions/gsd/auto-start.js +87 -14
  18. package/dist/resources/extensions/gsd/auto-verification.js +17 -4
  19. package/dist/resources/extensions/gsd/auto-worktree.js +176 -10
  20. package/dist/resources/extensions/gsd/auto.js +37 -5
  21. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +31 -7
  22. package/dist/resources/extensions/gsd/bootstrap/db-tools.js +10 -9
  23. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +4 -2
  24. package/dist/resources/extensions/gsd/bootstrap/subagent-input.js +5 -2
  25. package/dist/resources/extensions/gsd/bootstrap/write-gate.js +14 -2
  26. package/dist/resources/extensions/gsd/commands/handlers/core.js +17 -1
  27. package/dist/resources/extensions/gsd/commands-prefs-wizard.js +7 -2
  28. package/dist/resources/extensions/gsd/crash-recovery.js +43 -5
  29. package/dist/resources/extensions/gsd/db/milestone-leases.js +24 -0
  30. package/dist/resources/extensions/gsd/db/unit-dispatches.js +3 -2
  31. package/dist/resources/extensions/gsd/dispatch-guard.js +2 -2
  32. package/dist/resources/extensions/gsd/doctor-git-checks.js +46 -1
  33. package/dist/resources/extensions/gsd/doctor-runtime-checks.js +28 -11
  34. package/dist/resources/extensions/gsd/doctor.js +2 -28
  35. package/dist/resources/extensions/gsd/export-html.js +27 -425
  36. package/dist/resources/extensions/gsd/forensics.js +3 -3
  37. package/dist/resources/extensions/gsd/git-service.js +45 -3
  38. package/dist/resources/extensions/gsd/gsd-db.js +21 -6
  39. package/dist/resources/extensions/gsd/guided-flow-queue.js +4 -3
  40. package/dist/resources/extensions/gsd/guided-flow.js +101 -116
  41. package/dist/resources/extensions/gsd/guided-unit-context.js +23 -0
  42. package/dist/resources/extensions/gsd/migrate/parsers.js +10 -0
  43. package/dist/resources/extensions/gsd/migration-auto-check.js +12 -17
  44. package/dist/resources/extensions/gsd/milestone-actions.js +11 -4
  45. package/dist/resources/extensions/gsd/native-git-bridge.js +48 -12
  46. package/dist/resources/extensions/gsd/pending-auto-start.js +52 -0
  47. package/dist/resources/extensions/gsd/post-execution-checks.js +73 -2
  48. package/dist/resources/extensions/gsd/pre-execution-checks.js +28 -1
  49. package/dist/resources/extensions/gsd/prompt-loader.js +1 -1
  50. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
  51. package/dist/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  52. package/dist/resources/extensions/gsd/prompts/discuss-headless.md +8 -8
  53. package/dist/resources/extensions/gsd/prompts/discuss.md +9 -9
  54. package/dist/resources/extensions/gsd/prompts/guided-discuss-project.md +4 -4
  55. package/dist/resources/extensions/gsd/prompts/guided-discuss-requirements.md +3 -3
  56. package/dist/resources/extensions/gsd/prompts/plan-slice.md +4 -4
  57. package/dist/resources/extensions/gsd/prompts/queue.md +4 -4
  58. package/dist/resources/extensions/gsd/prompts/refine-slice.md +2 -2
  59. package/dist/resources/extensions/gsd/prompts/rewrite-docs.md +1 -1
  60. package/dist/resources/extensions/gsd/queue-reorder-ui.js +30 -13
  61. package/dist/resources/extensions/gsd/smart-entry-routing.js +36 -0
  62. package/dist/resources/extensions/gsd/state-reconciliation/drift/merge-state.js +6 -1
  63. package/dist/resources/extensions/gsd/state-reconciliation/drift/project-md.js +9 -14
  64. package/dist/resources/extensions/gsd/state-reconciliation/drift/roadmap.js +19 -24
  65. package/dist/resources/extensions/gsd/state.js +1 -1
  66. package/dist/resources/extensions/gsd/status-guards.js +11 -0
  67. package/dist/resources/extensions/gsd/templates/plan.md +8 -5
  68. package/dist/resources/extensions/gsd/templates/task-plan.md +4 -2
  69. package/dist/resources/extensions/gsd/tools/complete-milestone.js +6 -8
  70. package/dist/resources/extensions/gsd/tools/complete-slice.js +6 -8
  71. package/dist/resources/extensions/gsd/tools/plan-milestone.js +7 -1
  72. package/dist/resources/extensions/gsd/tools/plan-slice.js +89 -14
  73. package/dist/resources/extensions/gsd/unit-context-manifest.js +7 -8
  74. package/dist/resources/extensions/gsd/validation.js +23 -1
  75. package/dist/resources/extensions/gsd/verification-gate.js +68 -7
  76. package/dist/resources/extensions/gsd/workflow-mcp.js +17 -1
  77. package/dist/resources/extensions/gsd/workflow-projections.js +6 -8
  78. package/dist/resources/extensions/gsd/worktree-lifecycle.js +33 -8
  79. package/dist/resources/extensions/gsd/worktree-manager.js +1 -1
  80. package/dist/resources/extensions/shared/html-shell.js +388 -0
  81. package/dist/resources/extensions/visual-brief/page-contract.js +2 -0
  82. package/dist/resources/extensions/visual-brief/prompts.js +29 -0
  83. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  84. package/dist/web/standalone/.next/BUILD_ID +1 -1
  85. package/dist/web/standalone/.next/app-path-routes-manifest.json +11 -11
  86. package/dist/web/standalone/.next/build-manifest.json +3 -3
  87. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  88. package/dist/web/standalone/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
  89. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  90. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  91. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  92. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  93. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  94. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  95. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  96. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  97. package/dist/web/standalone/.next/server/app/_not-found/page.js +2 -2
  98. package/dist/web/standalone/.next/server/app/_not-found/page.js.nft.json +1 -1
  99. package/dist/web/standalone/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
  100. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  101. package/dist/web/standalone/.next/server/app/_not-found.rsc +4 -7
  102. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +4 -7
  103. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  104. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +4 -5
  105. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  106. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  107. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +2 -5
  108. package/dist/web/standalone/.next/server/app/api/browse-directories/route.js +1 -1
  109. package/dist/web/standalone/.next/server/app/api/git/route.js +1 -1
  110. package/dist/web/standalone/.next/server/app/index.html +1 -1
  111. package/dist/web/standalone/.next/server/app/index.rsc +4 -7
  112. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  113. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +4 -7
  114. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  115. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +4 -5
  116. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +2 -5
  117. package/dist/web/standalone/.next/server/app/page.js +2 -2
  118. package/dist/web/standalone/.next/server/app/page.js.nft.json +1 -1
  119. package/dist/web/standalone/.next/server/app/page_client-reference-manifest.js +1 -1
  120. package/dist/web/standalone/.next/server/app-paths-manifest.json +11 -11
  121. package/dist/web/standalone/.next/server/chunks/4266.js +2 -0
  122. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  123. package/dist/web/standalone/.next/server/next-font-manifest.js +1 -1
  124. package/dist/web/standalone/.next/server/next-font-manifest.json +1 -1
  125. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  126. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  127. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  128. package/dist/web/standalone/.next/static/chunks/app/layout-8c10ec293ae0f1d5.js +1 -0
  129. package/dist/web/standalone/.next/static/chunks/{webpack-6a95bc41e0f7ec89.js → webpack-9a4db269f9ed63ad.js} +1 -1
  130. package/dist/web/standalone/.next/static/css/746ee28c929d1880.css +1 -0
  131. package/package.json +2 -2
  132. package/packages/mcp-server/src/workflow-tools.test.ts +1 -1
  133. package/packages/native/tsconfig.json +2 -1
  134. package/packages/native/tsconfig.tsbuildinfo +1 -1
  135. package/packages/pi-ai/dist/providers/google-gemini-cli.d.ts.map +1 -1
  136. package/packages/pi-ai/dist/providers/google-gemini-cli.js +5 -0
  137. package/packages/pi-ai/dist/providers/google-gemini-cli.js.map +1 -1
  138. package/packages/pi-ai/dist/providers/google-gemini-cli.test.d.ts +2 -0
  139. package/packages/pi-ai/dist/providers/google-gemini-cli.test.d.ts.map +1 -0
  140. package/packages/pi-ai/dist/providers/google-gemini-cli.test.js +41 -0
  141. package/packages/pi-ai/dist/providers/google-gemini-cli.test.js.map +1 -0
  142. package/packages/pi-ai/dist/providers/openai-codex-responses.d.ts.map +1 -1
  143. package/packages/pi-ai/dist/providers/openai-codex-responses.js +82 -1
  144. package/packages/pi-ai/dist/providers/openai-codex-responses.js.map +1 -1
  145. package/packages/pi-ai/dist/providers/openai-codex-responses.test.d.ts +2 -0
  146. package/packages/pi-ai/dist/providers/openai-codex-responses.test.d.ts.map +1 -0
  147. package/packages/pi-ai/dist/providers/openai-codex-responses.test.js +52 -0
  148. package/packages/pi-ai/dist/providers/openai-codex-responses.test.js.map +1 -0
  149. package/packages/pi-ai/dist/providers/simple-options.d.ts +2 -4
  150. package/packages/pi-ai/dist/providers/simple-options.d.ts.map +1 -1
  151. package/packages/pi-ai/dist/providers/simple-options.js +5 -6
  152. package/packages/pi-ai/dist/providers/simple-options.js.map +1 -1
  153. package/packages/pi-ai/dist/providers/simple-options.test.d.ts +2 -0
  154. package/packages/pi-ai/dist/providers/simple-options.test.d.ts.map +1 -0
  155. package/packages/pi-ai/dist/providers/simple-options.test.js +50 -0
  156. package/packages/pi-ai/dist/providers/simple-options.test.js.map +1 -0
  157. package/packages/pi-ai/src/providers/google-gemini-cli.test.ts +49 -0
  158. package/packages/pi-ai/src/providers/google-gemini-cli.ts +7 -0
  159. package/packages/pi-ai/src/providers/openai-codex-responses.test.ts +63 -0
  160. package/packages/pi-ai/src/providers/openai-codex-responses.ts +91 -1
  161. package/packages/pi-ai/src/providers/simple-options.test.ts +60 -0
  162. package/packages/pi-ai/src/providers/simple-options.ts +5 -6
  163. package/packages/pi-ai/tsconfig.tsbuildinfo +1 -1
  164. package/packages/pi-coding-agent/dist/core/agent-session-thinking-level.test.d.ts +2 -0
  165. package/packages/pi-coding-agent/dist/core/agent-session-thinking-level.test.d.ts.map +1 -0
  166. package/packages/pi-coding-agent/dist/core/agent-session-thinking-level.test.js +66 -0
  167. package/packages/pi-coding-agent/dist/core/agent-session-thinking-level.test.js.map +1 -0
  168. package/packages/pi-coding-agent/dist/core/agent-session.js +1 -1
  169. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  170. package/packages/pi-coding-agent/dist/core/chat-controller-ordering.test.js +44 -3
  171. package/packages/pi-coding-agent/dist/core/chat-controller-ordering.test.js.map +1 -1
  172. package/packages/pi-coding-agent/dist/core/sdk.js +1 -1
  173. package/packages/pi-coding-agent/dist/core/sdk.js.map +1 -1
  174. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.d.ts.map +1 -1
  175. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js +24 -6
  176. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js.map +1 -1
  177. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.d.ts.map +1 -1
  178. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js +71 -97
  179. package/packages/pi-coding-agent/dist/modes/interactive/controllers/chat-controller.js.map +1 -1
  180. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode-ordering.test.js +12 -0
  181. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode-ordering.test.js.map +1 -1
  182. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
  183. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.js +19 -8
  184. package/packages/pi-coding-agent/dist/modes/interactive/interactive-mode.js.map +1 -1
  185. package/packages/pi-coding-agent/src/core/agent-session-thinking-level.test.ts +79 -0
  186. package/packages/pi-coding-agent/src/core/agent-session.ts +1 -1
  187. package/packages/pi-coding-agent/src/core/chat-controller-ordering.test.ts +53 -3
  188. package/packages/pi-coding-agent/src/core/sdk.ts +1 -1
  189. package/packages/pi-coding-agent/src/modes/interactive/components/footer.ts +23 -7
  190. package/packages/pi-coding-agent/src/modes/interactive/controllers/chat-controller.ts +75 -102
  191. package/packages/pi-coding-agent/src/modes/interactive/interactive-mode-ordering.test.ts +14 -0
  192. package/packages/pi-coding-agent/src/modes/interactive/interactive-mode.ts +23 -8
  193. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  194. package/packages/pi-tui/dist/__tests__/terminal.test.d.ts +2 -0
  195. package/packages/pi-tui/dist/__tests__/terminal.test.d.ts.map +1 -0
  196. package/packages/pi-tui/dist/__tests__/terminal.test.js +103 -0
  197. package/packages/pi-tui/dist/__tests__/terminal.test.js.map +1 -0
  198. package/packages/pi-tui/dist/terminal.d.ts +2 -0
  199. package/packages/pi-tui/dist/terminal.d.ts.map +1 -1
  200. package/packages/pi-tui/dist/terminal.js +12 -0
  201. package/packages/pi-tui/dist/terminal.js.map +1 -1
  202. package/packages/pi-tui/src/__tests__/terminal.test.ts +121 -0
  203. package/packages/pi-tui/src/terminal.ts +11 -0
  204. package/packages/pi-tui/tsconfig.tsbuildinfo +1 -1
  205. package/src/resources/GSD-WORKFLOW.md +7 -0
  206. package/src/resources/extensions/claude-code-cli/partial-builder.ts +2 -1
  207. package/src/resources/extensions/claude-code-cli/stream-adapter.ts +1 -1
  208. package/src/resources/extensions/claude-code-cli/tests/partial-builder.test.ts +19 -2
  209. package/src/resources/extensions/claude-code-cli/tests/stream-adapter.test.ts +9 -0
  210. package/src/resources/extensions/gsd/auto/contracts.ts +14 -6
  211. package/src/resources/extensions/gsd/auto/infra-errors.ts +9 -3
  212. package/src/resources/extensions/gsd/auto/loop.ts +8 -5
  213. package/src/resources/extensions/gsd/auto/orchestrator.ts +11 -0
  214. package/src/resources/extensions/gsd/auto/phases.ts +90 -38
  215. package/src/resources/extensions/gsd/auto/workflow-memory-pressure.ts +13 -0
  216. package/src/resources/extensions/gsd/auto-dashboard.ts +72 -1
  217. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +1 -0
  218. package/src/resources/extensions/gsd/auto-dispatch.ts +21 -19
  219. package/src/resources/extensions/gsd/auto-model-selection.ts +2 -1
  220. package/src/resources/extensions/gsd/auto-post-unit.ts +78 -8
  221. package/src/resources/extensions/gsd/auto-recovery.ts +74 -11
  222. package/src/resources/extensions/gsd/auto-start.ts +94 -12
  223. package/src/resources/extensions/gsd/auto-verification.ts +22 -2
  224. package/src/resources/extensions/gsd/auto-worktree.ts +193 -10
  225. package/src/resources/extensions/gsd/auto.ts +40 -5
  226. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +42 -7
  227. package/src/resources/extensions/gsd/bootstrap/db-tools.ts +10 -9
  228. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +4 -2
  229. package/src/resources/extensions/gsd/bootstrap/subagent-input.ts +3 -1
  230. package/src/resources/extensions/gsd/bootstrap/write-gate.ts +17 -2
  231. package/src/resources/extensions/gsd/commands/handlers/core.ts +17 -1
  232. package/src/resources/extensions/gsd/commands-prefs-wizard.ts +8 -3
  233. package/src/resources/extensions/gsd/crash-recovery.ts +44 -4
  234. package/src/resources/extensions/gsd/db/milestone-leases.ts +26 -0
  235. package/src/resources/extensions/gsd/db/unit-dispatches.ts +4 -3
  236. package/src/resources/extensions/gsd/dispatch-guard.ts +2 -2
  237. package/src/resources/extensions/gsd/doctor-git-checks.ts +45 -1
  238. package/src/resources/extensions/gsd/doctor-runtime-checks.ts +25 -13
  239. package/src/resources/extensions/gsd/doctor-types.ts +1 -0
  240. package/src/resources/extensions/gsd/doctor.ts +2 -27
  241. package/src/resources/extensions/gsd/export-html.ts +27 -427
  242. package/src/resources/extensions/gsd/forensics.ts +3 -3
  243. package/src/resources/extensions/gsd/git-service.ts +51 -4
  244. package/src/resources/extensions/gsd/gsd-db.ts +21 -6
  245. package/src/resources/extensions/gsd/guided-flow-queue.ts +4 -3
  246. package/src/resources/extensions/gsd/guided-flow.ts +134 -133
  247. package/src/resources/extensions/gsd/guided-unit-context.ts +30 -0
  248. package/src/resources/extensions/gsd/migrate/parsers.ts +11 -0
  249. package/src/resources/extensions/gsd/migration-auto-check.ts +15 -23
  250. package/src/resources/extensions/gsd/milestone-actions.ts +10 -4
  251. package/src/resources/extensions/gsd/native-git-bridge.ts +54 -12
  252. package/src/resources/extensions/gsd/pending-auto-start.ts +79 -0
  253. package/src/resources/extensions/gsd/post-execution-checks.ts +87 -2
  254. package/src/resources/extensions/gsd/pre-execution-checks.ts +32 -1
  255. package/src/resources/extensions/gsd/prompt-loader.ts +1 -1
  256. package/src/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
  257. package/src/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  258. package/src/resources/extensions/gsd/prompts/discuss-headless.md +8 -8
  259. package/src/resources/extensions/gsd/prompts/discuss.md +9 -9
  260. package/src/resources/extensions/gsd/prompts/guided-discuss-project.md +4 -4
  261. package/src/resources/extensions/gsd/prompts/guided-discuss-requirements.md +3 -3
  262. package/src/resources/extensions/gsd/prompts/plan-slice.md +4 -4
  263. package/src/resources/extensions/gsd/prompts/queue.md +4 -4
  264. package/src/resources/extensions/gsd/prompts/refine-slice.md +2 -2
  265. package/src/resources/extensions/gsd/prompts/rewrite-docs.md +1 -1
  266. package/src/resources/extensions/gsd/queue-reorder-ui.ts +31 -13
  267. package/src/resources/extensions/gsd/smart-entry-routing.ts +77 -0
  268. package/src/resources/extensions/gsd/state-reconciliation/drift/merge-state.ts +8 -1
  269. package/src/resources/extensions/gsd/state-reconciliation/drift/project-md.ts +12 -15
  270. package/src/resources/extensions/gsd/state-reconciliation/drift/roadmap.ts +17 -25
  271. package/src/resources/extensions/gsd/state.ts +1 -1
  272. package/src/resources/extensions/gsd/status-guards.ts +13 -0
  273. package/src/resources/extensions/gsd/templates/plan.md +8 -5
  274. package/src/resources/extensions/gsd/templates/task-plan.md +4 -2
  275. package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +71 -0
  276. package/src/resources/extensions/gsd/tests/auto-deterministic-error-classification-4973.test.ts +116 -0
  277. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +56 -0
  278. package/src/resources/extensions/gsd/tests/auto-orchestrator.test.ts +80 -1
  279. package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +35 -7
  280. package/src/resources/extensions/gsd/tests/auto-phases-lifecycle.test.ts +53 -2
  281. package/src/resources/extensions/gsd/tests/auto-post-unit-step-message.test.ts +12 -1
  282. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +91 -6
  283. package/src/resources/extensions/gsd/tests/auto-start-orphan-bootstrap.test.ts +1 -0
  284. package/src/resources/extensions/gsd/tests/auto-stop-notification.test.ts +20 -0
  285. package/src/resources/extensions/gsd/tests/auto-worktree-registry.test.ts +69 -1
  286. package/src/resources/extensions/gsd/tests/checkout-branch-stash-guard.test.ts +87 -0
  287. package/src/resources/extensions/gsd/tests/clear-stale-autostart.test.ts +11 -2
  288. package/src/resources/extensions/gsd/tests/complete-milestone.test.ts +4 -1
  289. package/src/resources/extensions/gsd/tests/complete-slice.test.ts +5 -9
  290. package/src/resources/extensions/gsd/tests/complete-task.test.ts +3 -1
  291. package/src/resources/extensions/gsd/tests/crash-recovery-via-db.test.ts +86 -2
  292. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +2 -0
  293. package/src/resources/extensions/gsd/tests/db-authority-regression.test.ts +208 -0
  294. package/src/resources/extensions/gsd/tests/deep-project-auto-loop.test.ts +59 -2
  295. package/src/resources/extensions/gsd/tests/dispatch-complete-milestone-guard.test.ts +66 -0
  296. package/src/resources/extensions/gsd/tests/dispatch-guard.test.ts +27 -0
  297. package/src/resources/extensions/gsd/tests/doctor-empty-worktree.test.ts +65 -0
  298. package/src/resources/extensions/gsd/tests/export-html-enhancements.test.ts +8 -0
  299. package/src/resources/extensions/gsd/tests/gsd-db.test.ts +11 -0
  300. package/src/resources/extensions/gsd/tests/guided-discuss-project-prompt-rendering.test.ts +2 -0
  301. package/src/resources/extensions/gsd/tests/guided-dispatch-root.test.ts +106 -0
  302. package/src/resources/extensions/gsd/tests/guided-flow-session-isolation.test.ts +59 -11
  303. package/src/resources/extensions/gsd/tests/guided-flow.test.ts +21 -0
  304. package/src/resources/extensions/gsd/tests/guided-tool-contract.test.ts +65 -0
  305. package/src/resources/extensions/gsd/tests/headless-milestone-parity.test.ts +7 -7
  306. package/src/resources/extensions/gsd/tests/hook-model-resolution.test.ts +5 -0
  307. package/src/resources/extensions/gsd/tests/infra-error.test.ts +2 -2
  308. package/src/resources/extensions/gsd/tests/infra-errors-cooldown.test.ts +9 -0
  309. package/src/resources/extensions/gsd/tests/integration/doctor-runtime.test.ts +20 -0
  310. package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +112 -1
  311. package/src/resources/extensions/gsd/tests/integration/state-machine-runtime-failures.test.ts +6 -1
  312. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +46 -0
  313. package/src/resources/extensions/gsd/tests/merge-db-cycle.test.ts +179 -0
  314. package/src/resources/extensions/gsd/tests/migrate-validator-parsers.test.ts +24 -1
  315. package/src/resources/extensions/gsd/tests/migration-auto-check.test.ts +26 -18
  316. package/src/resources/extensions/gsd/tests/native-git-bridge-exec-fallback.test.ts +63 -2
  317. package/src/resources/extensions/gsd/tests/orphaned-worktree-audit.test.ts +121 -1
  318. package/src/resources/extensions/gsd/tests/park-db-sync.test.ts +55 -1
  319. package/src/resources/extensions/gsd/tests/pending-autostart-scope.test.ts +29 -5
  320. package/src/resources/extensions/gsd/tests/pipeline-variant-dispatch.test.ts +2 -1
  321. package/src/resources/extensions/gsd/tests/plan-milestone.test.ts +26 -0
  322. package/src/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +2 -0
  323. package/src/resources/extensions/gsd/tests/plan-slice.test.ts +225 -1
  324. package/src/resources/extensions/gsd/tests/plan-task.test.ts +17 -0
  325. package/src/resources/extensions/gsd/tests/post-execution-checks.test.ts +86 -0
  326. package/src/resources/extensions/gsd/tests/post-unit-git-failure.test.ts +1 -1
  327. package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +53 -0
  328. package/src/resources/extensions/gsd/tests/prefs-wizard-coverage.test.ts +59 -0
  329. package/src/resources/extensions/gsd/tests/prompt-loader.test.ts +23 -0
  330. package/src/resources/extensions/gsd/tests/provider-errors.test.ts +37 -1
  331. package/src/resources/extensions/gsd/tests/queue-reorder-ui.test.ts +54 -0
  332. package/src/resources/extensions/gsd/tests/remediation-completion-guard.test.ts +89 -2
  333. package/src/resources/extensions/gsd/tests/run-uat-replay-cap.test.ts +2 -3
  334. package/src/resources/extensions/gsd/tests/session-switch-abort-misclassification.test.ts +10 -0
  335. package/src/resources/extensions/gsd/tests/smart-entry-routing.test.ts +113 -0
  336. package/src/resources/extensions/gsd/tests/start-auto-detached.test.ts +53 -2
  337. package/src/resources/extensions/gsd/tests/state-reconciliation-drift.test.ts +119 -23
  338. package/src/resources/extensions/gsd/tests/status-guards.test.ts +13 -1
  339. package/src/resources/extensions/gsd/tests/stuck-state-via-db.test.ts +64 -1
  340. package/src/resources/extensions/gsd/tests/summary-render-parity.test.ts +7 -3
  341. package/src/resources/extensions/gsd/tests/unit-context-manifest.test.ts +82 -7
  342. package/src/resources/extensions/gsd/tests/validate-milestone-stuck-guard.test.ts +29 -2
  343. package/src/resources/extensions/gsd/tests/verification-gate.test.ts +110 -1
  344. package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +19 -1
  345. package/src/resources/extensions/gsd/tests/workflow-memory-pressure.test.ts +21 -1
  346. package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +1 -1
  347. package/src/resources/extensions/gsd/tests/worktree-git-pathspec.test.ts +39 -0
  348. package/src/resources/extensions/gsd/tests/worktree-journal-events.test.ts +64 -12
  349. package/src/resources/extensions/gsd/tests/write-gate-planning-unit.test.ts +38 -0
  350. package/src/resources/extensions/gsd/tools/complete-milestone.ts +8 -10
  351. package/src/resources/extensions/gsd/tools/complete-slice.ts +6 -8
  352. package/src/resources/extensions/gsd/tools/plan-milestone.ts +5 -1
  353. package/src/resources/extensions/gsd/tools/plan-slice.ts +98 -12
  354. package/src/resources/extensions/gsd/types.ts +1 -1
  355. package/src/resources/extensions/gsd/unit-context-manifest.ts +12 -9
  356. package/src/resources/extensions/gsd/validation.ts +23 -1
  357. package/src/resources/extensions/gsd/verification-gate.ts +78 -6
  358. package/src/resources/extensions/gsd/workflow-mcp.ts +18 -1
  359. package/src/resources/extensions/gsd/workflow-projections.ts +6 -8
  360. package/src/resources/extensions/gsd/worktree-lifecycle.ts +41 -8
  361. package/src/resources/extensions/gsd/worktree-manager.ts +1 -1
  362. package/src/resources/extensions/shared/html-shell.ts +412 -0
  363. package/src/resources/extensions/visual-brief/page-contract.ts +2 -0
  364. package/src/resources/extensions/visual-brief/prompts.ts +37 -1
  365. package/src/resources/extensions/visual-brief/tests/visual-brief.test.ts +40 -0
  366. package/dist/web/standalone/.next/server/chunks/5822.js +0 -2
  367. package/dist/web/standalone/.next/static/chunks/app/layout-a16c7a7ecdf0c2cf.js +0 -1
  368. package/dist/web/standalone/.next/static/css/0262768ec1b89d34.css +0 -1
  369. package/dist/web/standalone/.next/static/css/de70bee13400563f.css +0 -1
  370. package/dist/web/standalone/.next/static/media/4cf2300e9c8272f7-s.p.woff2 +0 -0
  371. package/dist/web/standalone/.next/static/media/747892c23ea88013-s.woff2 +0 -0
  372. package/dist/web/standalone/.next/static/media/8d697b304b401681-s.woff2 +0 -0
  373. package/dist/web/standalone/.next/static/media/93f479601ee12b01-s.p.woff2 +0 -0
  374. package/dist/web/standalone/.next/static/media/9610d9e46709d722-s.woff2 +0 -0
  375. package/dist/web/standalone/.next/static/media/ba015fad6dcf6784-s.woff2 +0 -0
  376. /package/dist/web/standalone/.next/static/{Qgr2B_MRhPxC0z8fwv4vT → O6femb9LLl3nlgsDaYwS-}/_buildManifest.js +0 -0
  377. /package/dist/web/standalone/.next/static/{Qgr2B_MRhPxC0z8fwv4vT → O6femb9LLl3nlgsDaYwS-}/_ssgManifest.js +0 -0
@@ -46,6 +46,7 @@ import {
46
46
  import { classifyMilestoneSummaryContent } from "./milestone-summary-classifier.js";
47
47
  import { validateArtifact } from "./schemas/validate.js";
48
48
  import { getProjectResearchStatus } from "./project-research-policy.js";
49
+ import { isGsdWorktreePath } from "./worktree-root.js";
49
50
 
50
51
  // Re-export so existing consumers of auto-recovery.ts keep working.
51
52
  export { resolveExpectedArtifactPath, diagnoseExpectedArtifact };
@@ -56,6 +57,29 @@ export {
56
57
 
57
58
  // ─── Artifact Resolution & Verification ───────────────────────────────────────
58
59
 
60
+ export function diagnoseWorktreeIntegrityFailure(basePath: string): string | null {
61
+ if (!isGsdWorktreePath(basePath)) return null;
62
+ if (!existsSync(basePath)) {
63
+ return `Worktree integrity failure: ${basePath} does not exist. Repair or recreate the worktree before retrying.`;
64
+ }
65
+
66
+ const gitPath = join(basePath, ".git");
67
+ if (!existsSync(gitPath)) {
68
+ return `Worktree integrity failure: ${basePath} is not a valid git worktree (.git missing). Repair or recreate the worktree before retrying.`;
69
+ }
70
+
71
+ try {
72
+ execFileSync("git", ["rev-parse", "--git-dir"], {
73
+ cwd: basePath,
74
+ stdio: ["ignore", "pipe", "pipe"],
75
+ encoding: "utf-8",
76
+ });
77
+ return null;
78
+ } catch (err) {
79
+ return `Worktree integrity failure: ${basePath} is not a valid git worktree (git rev-parse failed: ${getErrorMessage(err).split("\n")[0]}). Repair or recreate the worktree before retrying.`;
80
+ }
81
+ }
82
+
59
83
  export type ArtifactRecoveryDbRefreshResult =
60
84
  | { ok: true }
61
85
  | { ok: false; fatal: boolean; message: string; reason: string };
@@ -177,9 +201,15 @@ export function hasImplementationArtifacts(basePath: string, milestoneId?: strin
177
201
  // Strategy: check `git diff --name-only` against the merge-base with the
178
202
  // main branch. This captures ALL files changed during the milestone's
179
203
  // lifetime while running on a milestone branch.
180
- const integrationBranch = milestoneId
181
- ? readIntegrationBranch(basePath, milestoneId) ?? detectMainBranch(basePath)
182
- : detectMainBranch(basePath);
204
+ const recordedIntegrationBranch = milestoneId
205
+ ? readIntegrationBranch(basePath, milestoneId)
206
+ : null;
207
+ let integrationBranch: string;
208
+ if (recordedIntegrationBranch?.startsWith("milestone/")) {
209
+ integrationBranch = detectMainBranch(basePath);
210
+ } else {
211
+ integrationBranch = recordedIntegrationBranch ?? detectMainBranch(basePath);
212
+ }
183
213
  const currentBranch = getCurrentBranch(basePath);
184
214
  const branchDiff = getChangedFilesSinceBranch(basePath, integrationBranch);
185
215
  if (!branchDiff.ok) return "unknown";
@@ -532,27 +562,55 @@ function commitMatchesMilestone(basePath: string, message: string, milestoneId:
532
562
  // rather than Mxx/Sxx/Tyy. Bind those commits back to the milestone when
533
563
  // either the commit touched this milestone's artifacts, or — for projects
534
564
  // where .gsd/ is gitignored/external (#5033) — the message explicitly
535
- // names the milestone or local GSD state proves the task belongs here.
565
+ // names the milestone, local GSD state proves the task belongs here, or the
566
+ // commit is implementation-bearing evidence itself (#5100).
536
567
  if (/^GSD-Task:\s*S[^/\s]+\/T\S+/m.test(message)) {
537
568
  if (files.some((file) => isMilestoneArtifactPath(file, milestoneId))) return true;
538
569
  if (commitMessageMentionsMilestone(message, milestoneId)) return true;
539
- if (commitTaskTrailerBelongsToMilestone(basePath, message, milestoneId)) return true;
570
+ const taskTrailerOwnership = getTaskOwnershipStatus(basePath, message, milestoneId);
571
+ if (taskTrailerOwnership === true) return true;
572
+ if (taskTrailerOwnership === false) return false;
573
+ // taskTrailerOwnership === null: unknown ownership. Apply fallback only
574
+ // in this case to avoid cross-milestone attribution.
575
+ if (MILESTONE_ID_RE.test(milestoneId) && classifyImplementationFiles(files) === "present") return true;
540
576
  }
541
577
 
542
578
  return false;
543
579
  }
544
580
 
545
- function commitTaskTrailerBelongsToMilestone(basePath: string, message: string, milestoneId: string): boolean {
581
+ /**
582
+ * Tri-state task ownership probe.
583
+ * true => DB or local files confirm this milestone owns the task.
584
+ * false => DB is available and this milestone is registered, but task is absent.
585
+ * null => ownership unknown (milestone not in DB yet, or no DB + no local files).
586
+ */
587
+ function getTaskOwnershipStatus(
588
+ basePath: string,
589
+ message: string,
590
+ milestoneId: string,
591
+ ): true | false | null {
546
592
  const match = message.match(/^GSD-Task:\s*(S[^/\s]+)\/(T[^\s]+)/m);
547
- if (!match) return false;
593
+ if (!match) return null;
548
594
  const [, sliceId, taskId] = match;
549
595
 
550
- if (getTask(milestoneId, sliceId, taskId)) return true;
596
+ if (isDbAvailable()) {
597
+ if (!getMilestone(milestoneId)) return null;
598
+ return getTask(milestoneId, sliceId, taskId) ? true : false;
599
+ }
551
600
 
601
+ // DB unavailable: fallback to local task-file presence.
552
602
  const tasksDir = resolveTasksDir(basePath, milestoneId, sliceId);
553
- if (!tasksDir) return false;
554
- return existsSync(join(tasksDir, `${taskId}-PLAN.md`))
555
- || existsSync(join(tasksDir, `${taskId}-SUMMARY.md`));
603
+ if (
604
+ tasksDir
605
+ && (
606
+ existsSync(join(tasksDir, `${taskId}-PLAN.md`))
607
+ || existsSync(join(tasksDir, `${taskId}-SUMMARY.md`))
608
+ )
609
+ ) {
610
+ return true;
611
+ }
612
+
613
+ return null;
556
614
  }
557
615
 
558
616
  function commitMessageMentionsMilestone(message: string, milestoneId: string): boolean {
@@ -752,6 +810,11 @@ export function verifyExpectedArtifact(
752
810
  return false;
753
811
  }
754
812
  if (!existsSync(absPath)) {
813
+ const worktreeFailure = diagnoseWorktreeIntegrityFailure(base);
814
+ if (worktreeFailure) {
815
+ logError("recovery", `${worktreeFailure} Unit: ${unitType} ${unitId}.`);
816
+ return false;
817
+ }
755
818
  logWarning("recovery", `verify-fail ${unitType} ${unitId}: existsSync false for ${absPath}`);
756
819
  return false;
757
820
  }
@@ -42,8 +42,8 @@ import {
42
42
  nativeCommit,
43
43
  nativeGetCurrentBranch,
44
44
  nativeDetectMainBranch,
45
- nativeCheckoutBranch,
46
45
  nativeBranchList,
46
+ nativeBranchExists,
47
47
  nativeBranchListMerged,
48
48
  nativeBranchDelete,
49
49
  nativeWorktreeRemove,
@@ -55,7 +55,7 @@ import {
55
55
  detectWorktreeName,
56
56
  setActiveMilestoneId,
57
57
  } from "./worktree.js";
58
- import { getAutoWorktreePath, isInAutoWorktree } from "./auto-worktree.js";
58
+ import { getAutoWorktreePath, isInAutoWorktree, checkoutBranchWithStashGuard } from "./auto-worktree.js";
59
59
  import { readResourceVersion, cleanStaleRuntimeUnits } from "./auto-worktree.js";
60
60
  import { worktreePath as getWorktreeDir, isInsideWorktreesDir } from "./worktree-manager.js";
61
61
  import { emitWorktreeOrphaned } from "./worktree-telemetry.js";
@@ -64,7 +64,7 @@ import { initRoutingHistory } from "./routing-history.js";
64
64
  import { restoreHookState, resetHookState } from "./post-unit-hooks.js";
65
65
  import { resetProactiveHealing, setLevelChangeCallback } from "./doctor-proactive.js";
66
66
  import { snapshotSkills } from "./skill-discovery.js";
67
- import { isDbAvailable, getMilestone, openDatabase, getDbStatus } from "./gsd-db.js";
67
+ import { isDbAvailable, getMilestone, getAllMilestones, openDatabase, getDbStatus } from "./gsd-db.js";
68
68
  import { isClosedStatus } from "./status-guards.js";
69
69
  import { classifyMilestoneSummaryContent } from "./milestone-summary-classifier.js";
70
70
  import { auditOrphanedPreflightStashes } from "./orphan-stash-audit.js";
@@ -101,6 +101,7 @@ import { getSessionModelOverride } from "./session-model-override.js";
101
101
  export interface BootstrapDeps {
102
102
  shouldUseWorktreeIsolation: (basePath?: string) => boolean;
103
103
  registerSigtermHandler: (basePath: string) => void;
104
+ registerAutoWorkerForSession: (basePath: string) => void;
104
105
  lockBase: () => string;
105
106
  buildLifecycle: () => WorktreeLifecycle;
106
107
  }
@@ -202,9 +203,15 @@ export function decideSurvivorAction(
202
203
  export function auditOrphanedMilestoneBranches(
203
204
  basePath: string,
204
205
  isolationMode: "worktree" | "branch" | "none",
206
+ gitDeps: {
207
+ branchList?: typeof nativeBranchList;
208
+ branchExists?: typeof nativeBranchExists;
209
+ } = {},
205
210
  ): { recovered: string[]; warnings: string[] } {
206
211
  const recovered: string[] = [];
207
212
  const warnings: string[] = [];
213
+ const branchList = gitDeps.branchList ?? nativeBranchList;
214
+ const branchExists = gitDeps.branchExists ?? nativeBranchExists;
208
215
 
209
216
  // Skip in none mode — no milestone branches are created
210
217
  if (isolationMode === "none") return { recovered, warnings };
@@ -213,15 +220,16 @@ export function auditOrphanedMilestoneBranches(
213
220
  if (!isDbAvailable()) return { recovered, warnings };
214
221
 
215
222
  let milestoneBranches: string[];
223
+ let milestoneBranchListAvailable = true;
216
224
  try {
217
- milestoneBranches = nativeBranchList(basePath, "milestone/*");
225
+ milestoneBranches = branchList(basePath, "milestone/*");
218
226
  } catch {
219
- // git branch list failed — skip audit
220
- return { recovered, warnings };
227
+ milestoneBranchListAvailable = false;
228
+ // git branch list failed — fall through with an empty branch set so the
229
+ // branch-less orphan pass can still run after per-milestone verification.
230
+ milestoneBranches = [];
221
231
  }
222
232
 
223
- if (milestoneBranches.length === 0) return { recovered, warnings };
224
-
225
233
  // Detect main branch for merge-check
226
234
  let mainBranch: string;
227
235
  try {
@@ -354,6 +362,74 @@ export function auditOrphanedMilestoneBranches(
354
362
  }
355
363
  }
356
364
 
365
+ // Second pass (#5879): catch worktree directories stranded by a previous
366
+ // audit that deleted the milestone/* branch but failed to remove the
367
+ // directory (or the dir was orphaned by a separate path entirely, e.g.
368
+ // postflight-stash-restore-failed during closeout). The branch-keyed loop
369
+ // above is invisible to these cases — `nativeBranchList` returns nothing
370
+ // for the milestone, so the dir-cleanup block at line ~310 is never
371
+ // reached.
372
+ //
373
+ // Keyed on milestones whose DB status is `complete`. We do not iterate
374
+ // over arbitrary directories under .gsd/worktrees/ to avoid touching
375
+ // dirs that belong to an in-progress milestone whose branch was deleted
376
+ // separately — those are handled by the in-progress orphan path above
377
+ // when the branch is present, and by `/gsd doctor` when it is not.
378
+ const seenMilestoneIds = new Set(
379
+ milestoneBranches.map((branch) => branch.replace(/^milestone\//, "")),
380
+ );
381
+ let completedMilestones: readonly { id: string; status: string }[] = [];
382
+ try {
383
+ completedMilestones = getAllMilestones();
384
+ } catch {
385
+ // DB read failure — skip the second pass; the first pass is still useful.
386
+ completedMilestones = [];
387
+ }
388
+ for (const m of completedMilestones) {
389
+ if (m.status !== "complete") continue;
390
+ if (seenMilestoneIds.has(m.id)) continue; // already processed in the branch loop
391
+ if (!milestoneBranchListAvailable) {
392
+ try {
393
+ if (branchExists(basePath, `milestone/${m.id}`)) continue;
394
+ } catch (err) {
395
+ warnings.push(
396
+ `Could not verify whether milestone/${m.id} still exists; skipping branch-less worktree cleanup for safety: ${err instanceof Error ? err.message : String(err)}`,
397
+ );
398
+ continue;
399
+ }
400
+ }
401
+ const wtDir = getWorktreeDir(basePath, m.id);
402
+ if (!existsSync(wtDir)) continue;
403
+ if (!isInsideWorktreesDir(basePath, wtDir)) {
404
+ warnings.push(
405
+ `Orphaned worktree directory for ${m.id} is outside .gsd/worktrees/ — skipping removal for safety.`,
406
+ );
407
+ continue;
408
+ }
409
+ // Try `git worktree remove` first in case the dir is still registered
410
+ // (defensive — usually it is not when we reach this branch-less pass).
411
+ try {
412
+ nativeWorktreeRemove(basePath, wtDir, true);
413
+ } catch (e) {
414
+ logWarning(
415
+ "engine",
416
+ `worktree remove failed (expected for branch-less orphans): ${e instanceof Error ? e.message : String(e)}`,
417
+ );
418
+ }
419
+ if (existsSync(wtDir)) {
420
+ try {
421
+ rmSync(wtDir, { recursive: true, force: true });
422
+ recovered.push(`Removed orphaned worktree directory for ${m.id} (branch already deleted).`);
423
+ } catch (err) {
424
+ warnings.push(
425
+ `Failed to remove orphaned worktree directory for ${m.id}: ${err instanceof Error ? err.message : String(err)}`,
426
+ );
427
+ }
428
+ } else {
429
+ recovered.push(`Removed orphaned worktree directory for ${m.id} (branch already deleted).`);
430
+ }
431
+ }
432
+
357
433
  return { recovered, warnings };
358
434
  }
359
435
 
@@ -533,6 +609,7 @@ export async function bootstrapAutoSession(
533
609
  const {
534
610
  shouldUseWorktreeIsolation,
535
611
  registerSigtermHandler,
612
+ registerAutoWorkerForSession,
536
613
  lockBase,
537
614
  buildLifecycle,
538
615
  } = deps;
@@ -722,6 +799,7 @@ export async function bootstrapAutoSession(
722
799
  // consult DB status and avoid clearing runtime units for milestones that
723
800
  // only have a failure-path SUMMARY on disk (#4663).
724
801
  await openProjectDbIfPresent(base);
802
+ registerAutoWorkerForSession(base);
725
803
 
726
804
  // Clean stale runtime unit files for completed milestones (#887).
727
805
  // DB-authoritative: when DB is available, require DB status to be closed
@@ -818,14 +896,18 @@ export async function bootstrapAutoSession(
818
896
  // worktree cleanup) was never run — the survivor branch must be merged.
819
897
  // Applies to both worktree and branch isolation modes.
820
898
  let hasSurvivorBranch = false;
899
+ let survivorMilestoneId = state.activeMilestone?.id ?? null;
900
+ if (!survivorMilestoneId && state.phase === "complete") {
901
+ survivorMilestoneId = findUnmergedCompletedMilestone(base, getIsolationMode(base));
902
+ }
821
903
  if (
822
- state.activeMilestone &&
904
+ survivorMilestoneId &&
823
905
  (state.phase === "pre-planning" || state.phase === "complete") &&
824
906
  getIsolationMode(base) !== "none" &&
825
907
  !detectWorktreeName(base) &&
826
908
  !base.includes(`${pathSep}.gsd${pathSep}worktrees${pathSep}`)
827
909
  ) {
828
- const milestoneBranch = `milestone/${state.activeMilestone.id}`;
910
+ const milestoneBranch = `milestone/${survivorMilestoneId}`;
829
911
  const { nativeBranchExists } = await import("./native-git-bridge.js");
830
912
  hasSurvivorBranch = nativeBranchExists(base, milestoneBranch);
831
913
  if (hasSurvivorBranch) {
@@ -869,7 +951,7 @@ export async function bootstrapAutoSession(
869
951
  // Re-evaluate via the helper — the discuss branch above may have cleared
870
952
  // hasSurvivorBranch after a successful promotion.
871
953
  if (decideSurvivorAction(hasSurvivorBranch, state.phase) === "finalize") {
872
- const mid = state.activeMilestone!.id;
954
+ const mid = survivorMilestoneId!;
873
955
  // Commit 68ef58a3c made `_mergeBranchMode` throw on wrong-branch
874
956
  // instead of returning false silently. Wrap the call so the throw is
875
957
  // converted into an error notify + clean bootstrap abort, not an
@@ -1087,7 +1169,7 @@ export async function bootstrapAutoSession(
1087
1169
  isRepo,
1088
1170
  );
1089
1171
  if (branchToCheckout) {
1090
- nativeCheckoutBranch(base, branchToCheckout);
1172
+ checkoutBranchWithStashGuard(base, branchToCheckout, "isolation-none-recovery");
1091
1173
  logWarning("bootstrap", `Returned to "${branchToCheckout}" — HEAD was on stale milestone branch "${currentBranch}" (isolation: none does not use milestone branches).`);
1092
1174
  }
1093
1175
  } catch (err) {
@@ -105,11 +105,31 @@ async function runValidateMilestonePostCheck(
105
105
  const { milestone: mid } = parseUnitId(s.currentUnit.id);
106
106
  if (!mid) return "continue";
107
107
 
108
+ const setToolFailureRetry = (message: string): VerificationResult => {
109
+ const retryKey = verificationRetryKey(s.currentUnit!.type, s.currentUnit!.id);
110
+ const attempt = (s.verificationRetryCount.get(retryKey) ?? 0) + 1;
111
+ s.verificationRetryCount.set(retryKey, attempt);
112
+ s.pendingVerificationRetry = {
113
+ unitId: s.currentUnit!.id,
114
+ failureContext: message,
115
+ attempt,
116
+ };
117
+ return "retry";
118
+ };
119
+
108
120
  const validationFile = resolveMilestoneFile(s.basePath, mid, "VALIDATION");
109
- if (!validationFile) return "continue";
121
+ if (!validationFile) {
122
+ return setToolFailureRetry(
123
+ "You must call gsd_validate_milestone to persist the validation results. No VALIDATION.md was created.",
124
+ );
125
+ }
110
126
 
111
127
  const validationContent = await loadFile(validationFile);
112
- if (!validationContent) return "continue";
128
+ if (!validationContent) {
129
+ return setToolFailureRetry(
130
+ "You must call gsd_validate_milestone to persist the validation results. VALIDATION.md exists but is empty.",
131
+ );
132
+ }
113
133
 
114
134
  const verdict = extractVerdict(validationContent);
115
135
  if (verdict !== "needs-remediation") {
@@ -252,7 +252,17 @@ function gitPathspecForWorktreePath(basePath: string, targetPath: string): strin
252
252
  let base = basePath;
253
253
  let target = targetPath;
254
254
  try {
255
- base = realpathSync.native(basePath);
255
+ base = execFileSync("git", ["rev-parse", "--show-toplevel"], {
256
+ cwd: basePath,
257
+ stdio: ["ignore", "pipe", "ignore"],
258
+ encoding: "utf-8",
259
+ }).trim() || basePath;
260
+ } catch {
261
+ /* keep original */
262
+ void base;
263
+ }
264
+ try {
265
+ base = realpathSync.native(base);
256
266
  } catch {
257
267
  /* keep original */
258
268
  void base;
@@ -269,6 +279,10 @@ function gitPathspecForWorktreePath(basePath: string, targetPath: string): strin
269
279
  return rel.replaceAll("\\", "/");
270
280
  }
271
281
 
282
+ export function _gitPathspecForWorktreePath(basePath: string, targetPath: string): string | null {
283
+ return gitPathspecForWorktreePath(basePath, targetPath);
284
+ }
285
+
272
286
  function gitRemoteExists(basePath: string, remote: string): boolean {
273
287
  try {
274
288
  execFileSync("git", ["remote", "get-url", remote], {
@@ -282,6 +296,50 @@ function gitRemoteExists(basePath: string, remote: string): boolean {
282
296
  }
283
297
  }
284
298
 
299
+ function findRegularMergeChangedPaths(basePath: string, milestoneBranch: string, mainBranch: string): Set<string> {
300
+ const changedPaths = new Set<string>();
301
+ let mergeLog = "";
302
+ try {
303
+ mergeLog = execFileSync("git", ["rev-list", "--merges", "--parents", mainBranch], {
304
+ cwd: basePath,
305
+ stdio: ["ignore", "pipe", "pipe"],
306
+ encoding: "utf-8",
307
+ }).trim();
308
+ } catch (err) {
309
+ logWarning("worktree", `regular merge lookup failed: ${err instanceof Error ? err.message : String(err)}`);
310
+ return changedPaths;
311
+ }
312
+
313
+ for (const line of mergeLog.split("\n").filter(Boolean)) {
314
+ const [mergeCommit, firstParent, ...otherParents] = line.split(" ");
315
+ if (!mergeCommit || !firstParent || otherParents.length === 0) continue;
316
+ const mergedMilestone = otherParents.some((parent) => {
317
+ try {
318
+ return nativeIsAncestor(basePath, milestoneBranch, parent);
319
+ } catch {
320
+ return false;
321
+ }
322
+ });
323
+ if (!mergedMilestone) continue;
324
+
325
+ try {
326
+ const output = execFileSync("git", ["diff", "--name-only", firstParent, mergeCommit], {
327
+ cwd: basePath,
328
+ stdio: ["ignore", "pipe", "pipe"],
329
+ encoding: "utf-8",
330
+ }).trim();
331
+ for (const path of output.split("\n").filter(Boolean)) {
332
+ if (!path.startsWith(".gsd/")) changedPaths.add(path);
333
+ }
334
+ } catch (err) {
335
+ logWarning("worktree", `regular merge diff lookup failed: ${err instanceof Error ? err.message : String(err)}`);
336
+ }
337
+ return changedPaths;
338
+ }
339
+
340
+ return changedPaths;
341
+ }
342
+
285
343
  function clearProjectRootStateFiles(basePath: string, milestoneId: string): void {
286
344
  const gsdDir = gsdRoot(basePath);
287
345
  // Phase C pt 2: auto.lock removed from this list — the file is gone
@@ -949,7 +1007,73 @@ export function enterBranchModeForMilestone(
949
1007
  });
950
1008
  }
951
1009
 
952
- nativeCheckoutBranch(basePath, branch);
1010
+ checkoutBranchWithStashGuard(basePath, branch, `enter-branch-mode:${milestoneId}`);
1011
+ }
1012
+
1013
+ export function checkoutBranchWithStashGuard(
1014
+ basePath: string,
1015
+ branch: string,
1016
+ reason: string,
1017
+ ): void {
1018
+ let stashMarker: string | null = null;
1019
+ let stashed = false;
1020
+
1021
+ const status = nativeWorkingTreeStatus(basePath).trim();
1022
+ if (status.length > 0) {
1023
+ stashMarker = `gsd-checkout-stash:${reason}:${process.pid}:${Date.now()}:${process.hrtime.bigint().toString(36)}`;
1024
+ const stashListBefore = execFileSync("git", ["stash", "list"], {
1025
+ cwd: basePath,
1026
+ stdio: ["ignore", "pipe", "pipe"],
1027
+ encoding: "utf-8",
1028
+ });
1029
+ execFileSync(
1030
+ "git",
1031
+ ["stash", "push", "--include-untracked", "-m", `gsd: checkout stash [${stashMarker}]`],
1032
+ {
1033
+ cwd: basePath,
1034
+ stdio: ["ignore", "pipe", "pipe"],
1035
+ encoding: "utf-8",
1036
+ },
1037
+ );
1038
+ const stashListAfter = execFileSync("git", ["stash", "list"], {
1039
+ cwd: basePath,
1040
+ stdio: ["ignore", "pipe", "pipe"],
1041
+ encoding: "utf-8",
1042
+ });
1043
+ stashed = stashListAfter !== stashListBefore;
1044
+ }
1045
+
1046
+ // Checkout and stash-restore are split so we can distinguish two failure
1047
+ // modes: (a) checkout failed → HEAD did not move, restore stash and rethrow;
1048
+ // (b) checkout succeeded but stash pop failed → HEAD moved to `branch` but
1049
+ // the working-tree changes remain in the stash list. We surface a distinct
1050
+ // error in case (b) so callers don't assume the branch switch was rolled back.
1051
+ try {
1052
+ nativeCheckoutBranch(basePath, branch);
1053
+ } catch (checkoutErr) {
1054
+ if (stashed) {
1055
+ try {
1056
+ popStashByRef(basePath, stashMarker);
1057
+ } catch (restoreErr) {
1058
+ logWarning("worktree", `git stash pop failed during checkout restore: ${restoreErr instanceof Error ? restoreErr.message : String(restoreErr)}`);
1059
+ }
1060
+ }
1061
+ throw checkoutErr;
1062
+ }
1063
+
1064
+ if (stashed) {
1065
+ try {
1066
+ popStashByRef(basePath, stashMarker);
1067
+ } catch (popErr) {
1068
+ const msg = popErr instanceof Error ? popErr.message : String(popErr);
1069
+ const wrapped = new Error(
1070
+ `checkout to '${branch}' succeeded but stash restore failed; working tree changes remain in the stash list. Original error: ${msg}`,
1071
+ );
1072
+ const ref = (popErr as { stashRef?: string } | null)?.stashRef;
1073
+ if (ref) (wrapped as { stashRef?: string }).stashRef = ref;
1074
+ throw wrapped;
1075
+ }
1076
+ }
953
1077
  }
954
1078
 
955
1079
  // ─── Public API ────────────────────────────────────────────────────────────
@@ -1739,6 +1863,66 @@ export function mergeMilestoneToMain(
1739
1863
  }
1740
1864
  }
1741
1865
 
1866
+ // Already regular-merged milestones can skip the squash path and proceed to cleanup (#5831).
1867
+ if (nativeIsAncestor(originalBasePath_, milestoneBranch, mainBranch)) {
1868
+ const codeChanges = nativeDiffNumstat(
1869
+ originalBasePath_,
1870
+ mainBranch,
1871
+ milestoneBranch,
1872
+ ).filter((entry) => !entry.path.startsWith(".gsd/"));
1873
+ if (codeChanges.length > 0) {
1874
+ const regularMergeChangedPaths = findRegularMergeChangedPaths(
1875
+ originalBasePath_,
1876
+ milestoneBranch,
1877
+ mainBranch,
1878
+ );
1879
+ const unanchoredCodeChanges = codeChanges.filter((entry) =>
1880
+ regularMergeChangedPaths.has(entry.path)
1881
+ );
1882
+ if (unanchoredCodeChanges.length > 0) {
1883
+ process.chdir(previousCwd);
1884
+ throw new GSDError(
1885
+ GSD_GIT_ERROR,
1886
+ `Milestone branch "${milestoneBranch}" is reachable from "${mainBranch}" ` +
1887
+ `but has ${unanchoredCodeChanges.length} milestone-touched code file(s) not on current "${mainBranch}". ` +
1888
+ `Aborting worktree teardown to prevent data loss.`,
1889
+ );
1890
+ }
1891
+ }
1892
+ debugLog("mergeMilestoneToMain", {
1893
+ action: "skip-squash-already-merged",
1894
+ milestoneId,
1895
+ milestoneBranch,
1896
+ mainBranch,
1897
+ });
1898
+ try {
1899
+ clearProjectRootStateFiles(originalBasePath_, milestoneId);
1900
+ } catch (err) {
1901
+ logWarning("worktree", `clearProjectRootStateFiles failed during already-merged cleanup: ${err instanceof Error ? err.message : String(err)}`);
1902
+ }
1903
+ try {
1904
+ removeWorktree(originalBasePath_, milestoneId, {
1905
+ branch: milestoneBranch,
1906
+ deleteBranch: false,
1907
+ });
1908
+ } catch (err) {
1909
+ logWarning("worktree", `worktree removal failed: ${err instanceof Error ? err.message : String(err)}`);
1910
+ }
1911
+ try {
1912
+ nativeBranchDelete(originalBasePath_, milestoneBranch);
1913
+ } catch (err) {
1914
+ logWarning("worktree", `git branch-delete failed: ${err instanceof Error ? err.message : String(err)}`);
1915
+ }
1916
+ setActiveWorkspace(null);
1917
+ nudgeGitBranchCache(previousCwd);
1918
+ try {
1919
+ process.chdir(originalBasePath_);
1920
+ } catch (err) {
1921
+ logWarning("worktree", `chdir to project root after already-merged cleanup failed: ${err instanceof Error ? err.message : String(err)}`);
1922
+ }
1923
+ return { commitMessage, pushed: false, prCreated: false, codeFilesChanged: true };
1924
+ }
1925
+
1742
1926
  // 7. Shelter queued milestone directories before the squash merge (#2505).
1743
1927
  // The milestone branch may contain copies of queued milestone dirs (via
1744
1928
  // copyPlanningArtifacts), so `git merge --squash` rejects when those same
@@ -1874,14 +2058,6 @@ export function mergeMilestoneToMain(
1874
2058
  logWarning("worktree", `git stash failed: ${err instanceof Error ? err.message : String(err)}`);
1875
2059
  }
1876
2060
 
1877
- if (needsDbCycle && dbPathToReopen) {
1878
- try {
1879
- openDatabase(dbPathToReopen);
1880
- } catch (err) {
1881
- logWarning("worktree", `post-stash db reopen failed: ${err instanceof Error ? err.message : String(err)}`);
1882
- }
1883
- }
1884
-
1885
2061
  // 7b. Clean up stale merge state before attempting squash merge (#2912).
1886
2062
  // A leftover MERGE_HEAD (from a previous failed merge, libgit2 native path,
1887
2063
  // or interrupted operation) causes `git merge --squash` to refuse with
@@ -1891,6 +2067,13 @@ export function mergeMilestoneToMain(
1891
2067
 
1892
2068
  // 8. Squash merge — auto-resolve .gsd/ state file conflicts (#530)
1893
2069
  const mergeResult = nativeMergeSquash(originalBasePath_, milestoneBranch);
2070
+ if (needsDbCycle && dbPathToReopen) {
2071
+ try {
2072
+ openDatabase(dbPathToReopen);
2073
+ } catch (err) {
2074
+ logWarning("worktree", `post-merge db reopen failed: ${err instanceof Error ? err.message : String(err)}`);
2075
+ }
2076
+ }
1894
2077
 
1895
2078
  if (!mergeResult.success) {
1896
2079
  // Dirty working tree — the merge was rejected before it started (e.g.