gsd-pi 2.81.0 → 2.82.0-dev.ed17d078d

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 (503) hide show
  1. package/README.md +60 -30
  2. package/dist/resources/.managed-resources-content-hash +1 -1
  3. package/dist/resources/extensions/browser-tools/tools/screenshot.js +1 -0
  4. package/dist/resources/extensions/browser-tools/tools/zoom.js +1 -0
  5. package/dist/resources/extensions/gsd/auto/loop.js +111 -8
  6. package/dist/resources/extensions/gsd/auto/orchestrator.js +113 -6
  7. package/dist/resources/extensions/gsd/auto/phases.js +199 -97
  8. package/dist/resources/extensions/gsd/auto/run-unit.js +66 -3
  9. package/dist/resources/extensions/gsd/auto/session.js +9 -0
  10. package/dist/resources/extensions/gsd/auto/verification-retry-policy.js +43 -0
  11. package/dist/resources/extensions/gsd/auto-dashboard.js +182 -178
  12. package/dist/resources/extensions/gsd/auto-dispatch.js +14 -11
  13. package/dist/resources/extensions/gsd/auto-post-unit.js +7 -1
  14. package/dist/resources/extensions/gsd/auto-prompts.js +11 -3
  15. package/dist/resources/extensions/gsd/auto-recovery.js +6 -181
  16. package/dist/resources/extensions/gsd/auto-runtime-state.js +5 -0
  17. package/dist/resources/extensions/gsd/auto-start.js +20 -23
  18. package/dist/resources/extensions/gsd/auto-unit-closeout.js +33 -5
  19. package/dist/resources/extensions/gsd/auto-verification.js +12 -6
  20. package/dist/resources/extensions/gsd/auto-worktree.js +8 -0
  21. package/dist/resources/extensions/gsd/auto.js +386 -106
  22. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +13 -6
  23. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +13 -2
  24. package/dist/resources/extensions/gsd/bootstrap/register-shortcuts.js +4 -8
  25. package/dist/resources/extensions/gsd/bootstrap/system-context.js +55 -12
  26. package/dist/resources/extensions/gsd/commands/handlers/core.js +1 -1
  27. package/dist/resources/extensions/gsd/commands/handlers/notifications-handler.js +4 -10
  28. package/dist/resources/extensions/gsd/commands/handlers/parallel.js +9 -0
  29. package/dist/resources/extensions/gsd/commands-handlers.js +15 -2
  30. package/dist/resources/extensions/gsd/context-store.js +112 -0
  31. package/dist/resources/extensions/gsd/db-writer.js +150 -84
  32. package/dist/resources/extensions/gsd/docs/preferences-reference.md +1 -1
  33. package/dist/resources/extensions/gsd/doctor-git-checks.js +41 -6
  34. package/dist/resources/extensions/gsd/git-service.js +2 -1
  35. package/dist/resources/extensions/gsd/gsd-db.js +7 -23
  36. package/dist/resources/extensions/gsd/health-widget-core.js +1 -1
  37. package/dist/resources/extensions/gsd/health-widget.js +4 -10
  38. package/dist/resources/extensions/gsd/knowledge-backfill.js +144 -0
  39. package/dist/resources/extensions/gsd/knowledge-capture.js +136 -0
  40. package/dist/resources/extensions/gsd/knowledge-parser.js +154 -0
  41. package/dist/resources/extensions/gsd/knowledge-projection.js +210 -0
  42. package/dist/resources/extensions/gsd/markdown-renderer.js +6 -96
  43. package/dist/resources/extensions/gsd/md-importer.js +1 -1
  44. package/dist/resources/extensions/gsd/memory-backfill.js +73 -17
  45. package/dist/resources/extensions/gsd/memory-consolidation-scanner.js +222 -0
  46. package/dist/resources/extensions/gsd/migrate/command.js +5 -0
  47. package/dist/resources/extensions/gsd/migrate/preview.js +9 -0
  48. package/dist/resources/extensions/gsd/migrate/transformer.js +51 -4
  49. package/dist/resources/extensions/gsd/migrate/writer.js +11 -1
  50. package/dist/resources/extensions/gsd/native-git-bridge.js +14 -14
  51. package/dist/resources/extensions/gsd/notification-overlay.js +35 -40
  52. package/dist/resources/extensions/gsd/parallel-merge.js +53 -30
  53. package/dist/resources/extensions/gsd/parallel-monitor-overlay.js +25 -33
  54. package/dist/resources/extensions/gsd/prompts/complete-slice.md +14 -12
  55. package/dist/resources/extensions/gsd/prompts/discuss-headless.md +20 -2
  56. package/dist/resources/extensions/gsd/prompts/discuss.md +20 -2
  57. package/dist/resources/extensions/gsd/prompts/system.md +2 -2
  58. package/dist/resources/extensions/gsd/provider-switch-observer.js +146 -0
  59. package/dist/resources/extensions/gsd/recovery-classification.js +15 -1
  60. package/dist/resources/extensions/gsd/session-lock.js +40 -0
  61. package/dist/resources/extensions/gsd/state-reconciliation/drift/completion.js +131 -0
  62. package/dist/resources/extensions/gsd/state-reconciliation/drift/merge-state.js +247 -0
  63. package/dist/resources/extensions/gsd/state-reconciliation/drift/project-md.js +50 -0
  64. package/dist/resources/extensions/gsd/state-reconciliation/drift/roadmap.js +87 -0
  65. package/dist/resources/extensions/gsd/state-reconciliation/drift/sketch-flag.js +50 -0
  66. package/dist/resources/extensions/gsd/state-reconciliation/drift/stale-render.js +124 -0
  67. package/dist/resources/extensions/gsd/state-reconciliation/drift/stale-worker.js +32 -0
  68. package/dist/resources/extensions/gsd/state-reconciliation/errors.js +41 -0
  69. package/dist/resources/extensions/gsd/state-reconciliation/index.js +99 -0
  70. package/dist/resources/extensions/gsd/state-reconciliation/registry.js +24 -0
  71. package/dist/resources/extensions/gsd/state-reconciliation/spawn-gate.js +43 -0
  72. package/dist/resources/extensions/gsd/state-reconciliation/types.js +3 -0
  73. package/dist/resources/extensions/gsd/state-reconciliation.js +5 -26
  74. package/dist/resources/extensions/gsd/templates/knowledge.md +2 -2
  75. package/dist/resources/extensions/gsd/tui/render-kit.js +74 -0
  76. package/dist/resources/extensions/gsd/watch/header-renderer.js +92 -69
  77. package/dist/resources/extensions/gsd/watch/splash-palette.js +10 -0
  78. package/dist/resources/extensions/gsd/workflow-mcp.js +2 -2
  79. package/dist/resources/extensions/gsd/worktree-lifecycle.js +722 -316
  80. package/dist/resources/extensions/gsd/worktree-telemetry.js +3 -1
  81. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  82. package/dist/web/standalone/.next/BUILD_ID +1 -1
  83. package/dist/web/standalone/.next/app-path-routes-manifest.json +6 -6
  84. package/dist/web/standalone/.next/build-manifest.json +3 -3
  85. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  86. package/dist/web/standalone/.next/required-server-files.json +3 -3
  87. package/dist/web/standalone/.next/server/app/_global-error/page.js +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_client-reference-manifest.js +1 -1
  99. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  100. package/dist/web/standalone/.next/server/app/_not-found.rsc +3 -3
  101. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +3 -3
  102. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  103. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +3 -3
  104. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  105. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  106. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  107. package/dist/web/standalone/.next/server/app/api/boot/route.js +1 -1
  108. package/dist/web/standalone/.next/server/app/api/boot/route_client-reference-manifest.js +1 -1
  109. package/dist/web/standalone/.next/server/app/api/bridge-terminal/input/route.js +1 -1
  110. package/dist/web/standalone/.next/server/app/api/bridge-terminal/input/route_client-reference-manifest.js +1 -1
  111. package/dist/web/standalone/.next/server/app/api/bridge-terminal/resize/route.js +1 -1
  112. package/dist/web/standalone/.next/server/app/api/bridge-terminal/resize/route_client-reference-manifest.js +1 -1
  113. package/dist/web/standalone/.next/server/app/api/bridge-terminal/stream/route.js +2 -2
  114. package/dist/web/standalone/.next/server/app/api/bridge-terminal/stream/route_client-reference-manifest.js +1 -1
  115. package/dist/web/standalone/.next/server/app/api/browse-directories/route.js +1 -1
  116. package/dist/web/standalone/.next/server/app/api/browse-directories/route_client-reference-manifest.js +1 -1
  117. package/dist/web/standalone/.next/server/app/api/captures/route.js +1 -1
  118. package/dist/web/standalone/.next/server/app/api/captures/route_client-reference-manifest.js +1 -1
  119. package/dist/web/standalone/.next/server/app/api/cleanup/route.js +1 -1
  120. package/dist/web/standalone/.next/server/app/api/cleanup/route_client-reference-manifest.js +1 -1
  121. package/dist/web/standalone/.next/server/app/api/dev-mode/route.js +1 -1
  122. package/dist/web/standalone/.next/server/app/api/dev-mode/route_client-reference-manifest.js +1 -1
  123. package/dist/web/standalone/.next/server/app/api/doctor/route.js +1 -1
  124. package/dist/web/standalone/.next/server/app/api/doctor/route_client-reference-manifest.js +1 -1
  125. package/dist/web/standalone/.next/server/app/api/experimental/route.js +2 -2
  126. package/dist/web/standalone/.next/server/app/api/experimental/route_client-reference-manifest.js +1 -1
  127. package/dist/web/standalone/.next/server/app/api/export-data/route.js +1 -1
  128. package/dist/web/standalone/.next/server/app/api/export-data/route_client-reference-manifest.js +1 -1
  129. package/dist/web/standalone/.next/server/app/api/files/route.js +1 -1
  130. package/dist/web/standalone/.next/server/app/api/files/route_client-reference-manifest.js +1 -1
  131. package/dist/web/standalone/.next/server/app/api/forensics/route.js +1 -1
  132. package/dist/web/standalone/.next/server/app/api/forensics/route_client-reference-manifest.js +1 -1
  133. package/dist/web/standalone/.next/server/app/api/git/route.js +1 -1
  134. package/dist/web/standalone/.next/server/app/api/git/route_client-reference-manifest.js +1 -1
  135. package/dist/web/standalone/.next/server/app/api/history/route.js +1 -1
  136. package/dist/web/standalone/.next/server/app/api/history/route_client-reference-manifest.js +1 -1
  137. package/dist/web/standalone/.next/server/app/api/hooks/route.js +1 -1
  138. package/dist/web/standalone/.next/server/app/api/hooks/route_client-reference-manifest.js +1 -1
  139. package/dist/web/standalone/.next/server/app/api/inspect/route.js +1 -1
  140. package/dist/web/standalone/.next/server/app/api/inspect/route_client-reference-manifest.js +1 -1
  141. package/dist/web/standalone/.next/server/app/api/knowledge/route.js +1 -1
  142. package/dist/web/standalone/.next/server/app/api/knowledge/route_client-reference-manifest.js +1 -1
  143. package/dist/web/standalone/.next/server/app/api/live-state/route.js +1 -1
  144. package/dist/web/standalone/.next/server/app/api/live-state/route_client-reference-manifest.js +1 -1
  145. package/dist/web/standalone/.next/server/app/api/notifications/route.js +2 -2
  146. package/dist/web/standalone/.next/server/app/api/notifications/route_client-reference-manifest.js +1 -1
  147. package/dist/web/standalone/.next/server/app/api/onboarding/route.js +1 -1
  148. package/dist/web/standalone/.next/server/app/api/onboarding/route_client-reference-manifest.js +1 -1
  149. package/dist/web/standalone/.next/server/app/api/preferences/route.js +1 -1
  150. package/dist/web/standalone/.next/server/app/api/preferences/route_client-reference-manifest.js +1 -1
  151. package/dist/web/standalone/.next/server/app/api/projects/route.js +1 -1
  152. package/dist/web/standalone/.next/server/app/api/projects/route_client-reference-manifest.js +1 -1
  153. package/dist/web/standalone/.next/server/app/api/recovery/route.js +1 -1
  154. package/dist/web/standalone/.next/server/app/api/recovery/route_client-reference-manifest.js +1 -1
  155. package/dist/web/standalone/.next/server/app/api/remote-questions/route.js +2 -2
  156. package/dist/web/standalone/.next/server/app/api/remote-questions/route_client-reference-manifest.js +1 -1
  157. package/dist/web/standalone/.next/server/app/api/session/browser/route.js +1 -1
  158. package/dist/web/standalone/.next/server/app/api/session/browser/route_client-reference-manifest.js +1 -1
  159. package/dist/web/standalone/.next/server/app/api/session/command/route.js +1 -1
  160. package/dist/web/standalone/.next/server/app/api/session/command/route_client-reference-manifest.js +1 -1
  161. package/dist/web/standalone/.next/server/app/api/session/events/route.js +2 -2
  162. package/dist/web/standalone/.next/server/app/api/session/events/route_client-reference-manifest.js +1 -1
  163. package/dist/web/standalone/.next/server/app/api/session/manage/route.js +1 -1
  164. package/dist/web/standalone/.next/server/app/api/session/manage/route_client-reference-manifest.js +1 -1
  165. package/dist/web/standalone/.next/server/app/api/settings-data/route.js +1 -1
  166. package/dist/web/standalone/.next/server/app/api/settings-data/route_client-reference-manifest.js +1 -1
  167. package/dist/web/standalone/.next/server/app/api/shutdown/route.js +1 -1
  168. package/dist/web/standalone/.next/server/app/api/shutdown/route_client-reference-manifest.js +1 -1
  169. package/dist/web/standalone/.next/server/app/api/skill-health/route.js +1 -1
  170. package/dist/web/standalone/.next/server/app/api/skill-health/route_client-reference-manifest.js +1 -1
  171. package/dist/web/standalone/.next/server/app/api/steer/route.js +1 -1
  172. package/dist/web/standalone/.next/server/app/api/steer/route_client-reference-manifest.js +1 -1
  173. package/dist/web/standalone/.next/server/app/api/switch-root/route.js +1 -1
  174. package/dist/web/standalone/.next/server/app/api/switch-root/route_client-reference-manifest.js +1 -1
  175. package/dist/web/standalone/.next/server/app/api/terminal/input/route.js +1 -1
  176. package/dist/web/standalone/.next/server/app/api/terminal/input/route_client-reference-manifest.js +1 -1
  177. package/dist/web/standalone/.next/server/app/api/terminal/resize/route.js +2 -2
  178. package/dist/web/standalone/.next/server/app/api/terminal/resize/route_client-reference-manifest.js +1 -1
  179. package/dist/web/standalone/.next/server/app/api/terminal/sessions/route.js +1 -1
  180. package/dist/web/standalone/.next/server/app/api/terminal/sessions/route_client-reference-manifest.js +1 -1
  181. package/dist/web/standalone/.next/server/app/api/terminal/stream/route.js +2 -2
  182. package/dist/web/standalone/.next/server/app/api/terminal/stream/route_client-reference-manifest.js +1 -1
  183. package/dist/web/standalone/.next/server/app/api/terminal/upload/route.js +1 -1
  184. package/dist/web/standalone/.next/server/app/api/terminal/upload/route_client-reference-manifest.js +1 -1
  185. package/dist/web/standalone/.next/server/app/api/undo/route.js +1 -1
  186. package/dist/web/standalone/.next/server/app/api/undo/route_client-reference-manifest.js +1 -1
  187. package/dist/web/standalone/.next/server/app/api/update/route.js +1 -1
  188. package/dist/web/standalone/.next/server/app/api/update/route_client-reference-manifest.js +1 -1
  189. package/dist/web/standalone/.next/server/app/api/visualizer/route.js +1 -1
  190. package/dist/web/standalone/.next/server/app/api/visualizer/route_client-reference-manifest.js +1 -1
  191. package/dist/web/standalone/.next/server/app/index.html +1 -1
  192. package/dist/web/standalone/.next/server/app/index.rsc +4 -4
  193. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +2 -2
  194. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +4 -4
  195. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  196. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +3 -3
  197. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  198. package/dist/web/standalone/.next/server/app/page.js +2 -2
  199. package/dist/web/standalone/.next/server/app/page_client-reference-manifest.js +1 -1
  200. package/dist/web/standalone/.next/server/app-paths-manifest.json +6 -6
  201. package/dist/web/standalone/.next/server/chunks/63.js +3 -3
  202. package/dist/web/standalone/.next/server/chunks/6897.js +1 -1
  203. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  204. package/dist/web/standalone/.next/server/middleware.js +2 -2
  205. package/dist/web/standalone/.next/server/next-font-manifest.js +1 -1
  206. package/dist/web/standalone/.next/server/next-font-manifest.json +1 -1
  207. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  208. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  209. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  210. package/dist/web/standalone/.next/static/chunks/app/_not-found/{page-2f24283c162b6ab3.js → page-f2a7482d42a5614b.js} +1 -1
  211. package/dist/web/standalone/.next/static/chunks/app/{layout-9ecfd95f343793f0.js → layout-a16c7a7ecdf0c2cf.js} +1 -1
  212. package/dist/web/standalone/.next/static/chunks/app/page-752f1e2ebdaa3e45.js +1 -0
  213. package/dist/web/standalone/.next/static/chunks/main-app-fdab67f7802d7832.js +1 -0
  214. package/dist/web/standalone/.next/static/chunks/next/dist/client/components/builtin/global-error-459824ffb8c323dd.js +1 -0
  215. package/dist/web/standalone/node_modules/node-pty/build/Makefile +2 -2
  216. package/dist/web/standalone/node_modules/node-pty/build/Release/pty.node +0 -0
  217. package/dist/web/standalone/node_modules/node-pty/build/pty.target.mk +14 -14
  218. package/dist/web/standalone/node_modules/node-pty/node-addon-api/node_addon_api.target.mk +14 -14
  219. package/dist/web/standalone/node_modules/node-pty/node-addon-api/node_addon_api_except.target.mk +14 -14
  220. package/dist/web/standalone/node_modules/node-pty/node-addon-api/node_addon_api_maybe.target.mk +14 -14
  221. package/dist/web/standalone/server.js +1 -1
  222. package/dist/welcome-screen.d.ts +0 -7
  223. package/dist/welcome-screen.js +60 -69
  224. package/package.json +3 -2
  225. package/packages/daemon/package.json +2 -2
  226. package/packages/mcp-server/README.md +2 -0
  227. package/packages/mcp-server/package.json +2 -2
  228. package/packages/mcp-server/src/workflow-tools-parity.test.ts +244 -0
  229. package/packages/native/package.json +1 -1
  230. package/packages/pi-agent-core/package.json +1 -1
  231. package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -1
  232. package/packages/pi-ai/dist/index.d.ts +2 -2
  233. package/packages/pi-ai/dist/index.d.ts.map +1 -1
  234. package/packages/pi-ai/dist/index.js +1 -1
  235. package/packages/pi-ai/dist/index.js.map +1 -1
  236. package/packages/pi-ai/dist/providers/transform-messages.d.ts +11 -0
  237. package/packages/pi-ai/dist/providers/transform-messages.d.ts.map +1 -1
  238. package/packages/pi-ai/dist/providers/transform-messages.js +20 -0
  239. package/packages/pi-ai/dist/providers/transform-messages.js.map +1 -1
  240. package/packages/pi-ai/package.json +1 -1
  241. package/packages/pi-ai/src/index.ts +7 -2
  242. package/packages/pi-ai/src/providers/transform-messages.ts +24 -0
  243. package/packages/pi-ai/tsconfig.tsbuildinfo +1 -1
  244. package/packages/pi-coding-agent/dist/core/system-prompt.js +4 -4
  245. package/packages/pi-coding-agent/dist/core/system-prompt.js.map +1 -1
  246. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.d.ts +2 -0
  247. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.d.ts.map +1 -0
  248. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.js +47 -0
  249. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.js.map +1 -0
  250. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-execution.test.js +76 -9
  251. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-execution.test.js.map +1 -1
  252. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.d.ts +2 -0
  253. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.d.ts.map +1 -0
  254. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.js +40 -0
  255. package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.js.map +1 -0
  256. package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.d.ts +0 -1
  257. package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.d.ts.map +1 -1
  258. package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.js +30 -29
  259. package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.js.map +1 -1
  260. package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.test.js +10 -3
  261. package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.test.js.map +1 -1
  262. package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
  263. package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.js +13 -13
  264. package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.js.map +1 -1
  265. package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.d.ts +1 -3
  266. package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
  267. package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.js +58 -3
  268. package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.js.map +1 -1
  269. package/packages/pi-coding-agent/dist/modes/interactive/components/diff.d.ts +2 -2
  270. package/packages/pi-coding-agent/dist/modes/interactive/components/diff.d.ts.map +1 -1
  271. package/packages/pi-coding-agent/dist/modes/interactive/components/diff.js +12 -6
  272. package/packages/pi-coding-agent/dist/modes/interactive/components/diff.js.map +1 -1
  273. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.d.ts.map +1 -1
  274. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js +14 -41
  275. package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js.map +1 -1
  276. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.d.ts +0 -1
  277. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
  278. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.js +86 -82
  279. package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.js.map +1 -1
  280. package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.d.ts +35 -0
  281. package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.d.ts.map +1 -0
  282. package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.js +152 -0
  283. package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.js.map +1 -0
  284. package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.d.ts +16 -0
  285. package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.d.ts.map +1 -0
  286. package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.js +73 -0
  287. package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.js.map +1 -0
  288. package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.d.ts +1 -1
  289. package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.d.ts.map +1 -1
  290. package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.js +12 -8
  291. package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.js.map +1 -1
  292. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.d.ts +2 -0
  293. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.d.ts.map +1 -0
  294. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.js +17 -0
  295. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.js.map +1 -0
  296. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  297. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme.js +105 -1
  298. package/packages/pi-coding-agent/dist/modes/interactive/theme/theme.js.map +1 -1
  299. package/packages/pi-coding-agent/dist/modes/interactive/theme/themes.d.ts.map +1 -1
  300. package/packages/pi-coding-agent/dist/modes/interactive/theme/themes.js +27 -26
  301. package/packages/pi-coding-agent/dist/modes/interactive/theme/themes.js.map +1 -1
  302. package/packages/pi-coding-agent/dist/modes/interactive/tui-mode.test.js +9 -6
  303. package/packages/pi-coding-agent/dist/modes/interactive/tui-mode.test.js.map +1 -1
  304. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.d.ts +2 -0
  305. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.d.ts.map +1 -0
  306. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.js +17 -0
  307. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.js.map +1 -0
  308. package/packages/pi-coding-agent/package.json +1 -1
  309. package/packages/pi-coding-agent/src/core/system-prompt.ts +4 -4
  310. package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/assistant-message-design.test.ts +56 -0
  311. package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/tool-execution.test.ts +113 -9
  312. package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/user-message-design.test.ts +48 -0
  313. package/packages/pi-coding-agent/src/modes/interactive/components/adaptive-layout.test.ts +10 -3
  314. package/packages/pi-coding-agent/src/modes/interactive/components/adaptive-layout.ts +43 -42
  315. package/packages/pi-coding-agent/src/modes/interactive/components/assistant-message.ts +14 -14
  316. package/packages/pi-coding-agent/src/modes/interactive/components/bash-execution.ts +64 -3
  317. package/packages/pi-coding-agent/src/modes/interactive/components/diff.ts +13 -7
  318. package/packages/pi-coding-agent/src/modes/interactive/components/footer.ts +15 -42
  319. package/packages/pi-coding-agent/src/modes/interactive/components/tool-execution.ts +84 -104
  320. package/packages/pi-coding-agent/src/modes/interactive/components/transcript-design.ts +196 -0
  321. package/packages/pi-coding-agent/src/modes/interactive/components/tui-style-kit.ts +94 -0
  322. package/packages/pi-coding-agent/src/modes/interactive/components/user-message.ts +14 -9
  323. package/packages/pi-coding-agent/src/modes/interactive/theme/theme-highlight.test.ts +23 -0
  324. package/packages/pi-coding-agent/src/modes/interactive/theme/theme.ts +106 -1
  325. package/packages/pi-coding-agent/src/modes/interactive/theme/themes.ts +27 -26
  326. package/packages/pi-coding-agent/src/modes/interactive/tui-mode.test.ts +9 -6
  327. package/packages/pi-coding-agent/src/tests/system-prompt-file-safety.test.ts +22 -0
  328. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  329. package/packages/pi-tui/dist/__tests__/overlay-layout.test.js +14 -1
  330. package/packages/pi-tui/dist/__tests__/overlay-layout.test.js.map +1 -1
  331. package/packages/pi-tui/dist/overlay-layout.d.ts.map +1 -1
  332. package/packages/pi-tui/dist/overlay-layout.js +9 -6
  333. package/packages/pi-tui/dist/overlay-layout.js.map +1 -1
  334. package/packages/pi-tui/dist/tui.d.ts.map +1 -1
  335. package/packages/pi-tui/dist/tui.js +5 -0
  336. package/packages/pi-tui/dist/tui.js.map +1 -1
  337. package/packages/pi-tui/package.json +1 -1
  338. package/packages/pi-tui/src/__tests__/overlay-layout.test.ts +20 -1
  339. package/packages/pi-tui/src/overlay-layout.ts +10 -7
  340. package/packages/pi-tui/src/tui.ts +6 -0
  341. package/packages/pi-tui/tsconfig.tsbuildinfo +1 -1
  342. package/packages/rpc-client/package.json +1 -1
  343. package/pkg/dist/modes/interactive/theme/theme-highlight.test.d.ts +2 -0
  344. package/pkg/dist/modes/interactive/theme/theme-highlight.test.d.ts.map +1 -0
  345. package/pkg/dist/modes/interactive/theme/theme-highlight.test.js +17 -0
  346. package/pkg/dist/modes/interactive/theme/theme-highlight.test.js.map +1 -0
  347. package/pkg/dist/modes/interactive/theme/theme.d.ts.map +1 -1
  348. package/pkg/dist/modes/interactive/theme/theme.js +105 -1
  349. package/pkg/dist/modes/interactive/theme/theme.js.map +1 -1
  350. package/pkg/dist/modes/interactive/theme/themes.d.ts.map +1 -1
  351. package/pkg/dist/modes/interactive/theme/themes.js +27 -26
  352. package/pkg/dist/modes/interactive/theme/themes.js.map +1 -1
  353. package/pkg/package.json +1 -1
  354. package/src/resources/extensions/browser-tools/tools/screenshot.ts +1 -0
  355. package/src/resources/extensions/browser-tools/tools/zoom.ts +1 -0
  356. package/src/resources/extensions/gsd/auto/contracts.ts +46 -11
  357. package/src/resources/extensions/gsd/auto/loop-deps.ts +9 -5
  358. package/src/resources/extensions/gsd/auto/loop.ts +113 -9
  359. package/src/resources/extensions/gsd/auto/orchestrator.ts +118 -6
  360. package/src/resources/extensions/gsd/auto/phases.ts +158 -19
  361. package/src/resources/extensions/gsd/auto/run-unit.ts +69 -4
  362. package/src/resources/extensions/gsd/auto/session.ts +10 -0
  363. package/src/resources/extensions/gsd/auto/verification-retry-policy.ts +82 -0
  364. package/src/resources/extensions/gsd/auto-dashboard.ts +230 -183
  365. package/src/resources/extensions/gsd/auto-dispatch.ts +15 -1
  366. package/src/resources/extensions/gsd/auto-post-unit.ts +7 -1
  367. package/src/resources/extensions/gsd/auto-prompts.ts +11 -3
  368. package/src/resources/extensions/gsd/auto-recovery.ts +7 -209
  369. package/src/resources/extensions/gsd/auto-runtime-state.ts +5 -0
  370. package/src/resources/extensions/gsd/auto-start.ts +22 -22
  371. package/src/resources/extensions/gsd/auto-unit-closeout.ts +51 -0
  372. package/src/resources/extensions/gsd/auto-verification.ts +12 -6
  373. package/src/resources/extensions/gsd/auto-worktree.ts +8 -0
  374. package/src/resources/extensions/gsd/auto.ts +424 -106
  375. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +21 -6
  376. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +12 -2
  377. package/src/resources/extensions/gsd/bootstrap/register-shortcuts.ts +5 -8
  378. package/src/resources/extensions/gsd/bootstrap/system-context.ts +58 -15
  379. package/src/resources/extensions/gsd/commands/handlers/core.ts +1 -1
  380. package/src/resources/extensions/gsd/commands/handlers/notifications-handler.ts +4 -10
  381. package/src/resources/extensions/gsd/commands/handlers/parallel.ts +12 -0
  382. package/src/resources/extensions/gsd/commands-handlers.ts +19 -2
  383. package/src/resources/extensions/gsd/context-store.ts +120 -1
  384. package/src/resources/extensions/gsd/db-writer.ts +167 -84
  385. package/src/resources/extensions/gsd/docs/preferences-reference.md +1 -1
  386. package/src/resources/extensions/gsd/doctor-git-checks.ts +44 -6
  387. package/src/resources/extensions/gsd/doctor-types.ts +2 -0
  388. package/src/resources/extensions/gsd/git-service.ts +2 -0
  389. package/src/resources/extensions/gsd/gsd-db.ts +7 -23
  390. package/src/resources/extensions/gsd/health-widget-core.ts +1 -1
  391. package/src/resources/extensions/gsd/health-widget.ts +6 -10
  392. package/src/resources/extensions/gsd/journal.ts +2 -0
  393. package/src/resources/extensions/gsd/knowledge-backfill.ts +164 -0
  394. package/src/resources/extensions/gsd/knowledge-capture.ts +160 -0
  395. package/src/resources/extensions/gsd/knowledge-parser.ts +174 -0
  396. package/src/resources/extensions/gsd/knowledge-projection.ts +241 -0
  397. package/src/resources/extensions/gsd/markdown-renderer.ts +10 -96
  398. package/src/resources/extensions/gsd/md-importer.ts +1 -1
  399. package/src/resources/extensions/gsd/memory-backfill.ts +89 -17
  400. package/src/resources/extensions/gsd/memory-consolidation-scanner.ts +277 -0
  401. package/src/resources/extensions/gsd/migrate/command.ts +5 -0
  402. package/src/resources/extensions/gsd/migrate/preview.ts +10 -0
  403. package/src/resources/extensions/gsd/migrate/transformer.ts +58 -4
  404. package/src/resources/extensions/gsd/migrate/writer.ts +14 -1
  405. package/src/resources/extensions/gsd/native-git-bridge.ts +14 -13
  406. package/src/resources/extensions/gsd/notification-overlay.ts +50 -46
  407. package/src/resources/extensions/gsd/parallel-merge.ts +61 -34
  408. package/src/resources/extensions/gsd/parallel-monitor-overlay.ts +33 -35
  409. package/src/resources/extensions/gsd/prompts/complete-slice.md +14 -12
  410. package/src/resources/extensions/gsd/prompts/discuss-headless.md +20 -2
  411. package/src/resources/extensions/gsd/prompts/discuss.md +20 -2
  412. package/src/resources/extensions/gsd/prompts/system.md +2 -2
  413. package/src/resources/extensions/gsd/provider-switch-observer.ts +185 -0
  414. package/src/resources/extensions/gsd/recovery-classification.ts +18 -1
  415. package/src/resources/extensions/gsd/session-lock.ts +41 -0
  416. package/src/resources/extensions/gsd/state-reconciliation/drift/completion.ts +172 -0
  417. package/src/resources/extensions/gsd/state-reconciliation/drift/merge-state.ts +337 -0
  418. package/src/resources/extensions/gsd/state-reconciliation/drift/project-md.ts +69 -0
  419. package/src/resources/extensions/gsd/state-reconciliation/drift/roadmap.ts +109 -0
  420. package/src/resources/extensions/gsd/state-reconciliation/drift/sketch-flag.ts +68 -0
  421. package/src/resources/extensions/gsd/state-reconciliation/drift/stale-render.ts +185 -0
  422. package/src/resources/extensions/gsd/state-reconciliation/drift/stale-worker.ts +46 -0
  423. package/src/resources/extensions/gsd/state-reconciliation/errors.ts +67 -0
  424. package/src/resources/extensions/gsd/state-reconciliation/index.ts +142 -0
  425. package/src/resources/extensions/gsd/state-reconciliation/registry.ts +27 -0
  426. package/src/resources/extensions/gsd/state-reconciliation/spawn-gate.ts +60 -0
  427. package/src/resources/extensions/gsd/state-reconciliation/types.ts +83 -0
  428. package/src/resources/extensions/gsd/state-reconciliation.ts +21 -53
  429. package/src/resources/extensions/gsd/templates/knowledge.md +2 -2
  430. package/src/resources/extensions/gsd/tests/artifact-retry-cap.test.ts +1 -1
  431. package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +99 -0
  432. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +729 -176
  433. package/src/resources/extensions/gsd/tests/auto-orchestrator.test.ts +408 -4
  434. package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +291 -4
  435. package/src/resources/extensions/gsd/tests/auto-runtime-state.test.ts +20 -5
  436. package/src/resources/extensions/gsd/tests/auto-start-orphan-bootstrap.test.ts +18 -0
  437. package/src/resources/extensions/gsd/tests/auto-unit-closeout.test.ts +68 -0
  438. package/src/resources/extensions/gsd/tests/browser-tools-compatibility-declarations.test.ts +62 -0
  439. package/src/resources/extensions/gsd/tests/context-store-decisions-from-memories.test.ts +312 -0
  440. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +28 -1
  441. package/src/resources/extensions/gsd/tests/db-writer.test.ts +13 -8
  442. package/src/resources/extensions/gsd/tests/decisions-projection-from-memories.test.ts +453 -0
  443. package/src/resources/extensions/gsd/tests/decisions-stop-table-writes.test.ts +348 -0
  444. package/src/resources/extensions/gsd/tests/deep-project-auto-loop.test.ts +20 -2
  445. package/src/resources/extensions/gsd/tests/dispatch-complete-milestone-guard.test.ts +44 -0
  446. package/src/resources/extensions/gsd/tests/freeform-decisions.test.ts +8 -4
  447. package/src/resources/extensions/gsd/tests/gsd-tools.test.ts +11 -7
  448. package/src/resources/extensions/gsd/tests/header-renderer.test.ts +40 -0
  449. package/src/resources/extensions/gsd/tests/headless-milestone-parity.test.ts +10 -0
  450. package/src/resources/extensions/gsd/tests/health-widget.test.ts +14 -4
  451. package/src/resources/extensions/gsd/tests/integration/doctor-git.test.ts +44 -0
  452. package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +26 -0
  453. package/src/resources/extensions/gsd/tests/integration/integration-lifecycle.test.ts +13 -5
  454. package/src/resources/extensions/gsd/tests/integration/integration-proof.test.ts +1 -1
  455. package/src/resources/extensions/gsd/tests/integration/migrate-command.test.ts +48 -3
  456. package/src/resources/extensions/gsd/tests/integration/parallel-merge.test.ts +116 -24
  457. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +0 -1
  458. package/src/resources/extensions/gsd/tests/knowledge-backfill-projection.test.ts +323 -0
  459. package/src/resources/extensions/gsd/tests/knowledge-capture.test.ts +242 -0
  460. package/src/resources/extensions/gsd/tests/knowledge.test.ts +47 -2
  461. package/src/resources/extensions/gsd/tests/load-knowledge-block-rules-only.test.ts +209 -0
  462. package/src/resources/extensions/gsd/tests/markdown-renderer.test.ts +1 -1
  463. package/src/resources/extensions/gsd/tests/memory-consolidation-scanner.test.ts +316 -0
  464. package/src/resources/extensions/gsd/tests/merge-conflict-stops-loop.test.ts +46 -11
  465. package/src/resources/extensions/gsd/tests/migrate-transformer.test.ts +5 -1
  466. package/src/resources/extensions/gsd/tests/migrate-writer-integration.test.ts +6 -1
  467. package/src/resources/extensions/gsd/tests/notification-overlay.test.ts +78 -41
  468. package/src/resources/extensions/gsd/tests/notifications-handler.test.ts +44 -0
  469. package/src/resources/extensions/gsd/tests/originalbase-path-comparison.test.ts +12 -217
  470. package/src/resources/extensions/gsd/tests/parallel-monitor-overlay.test.ts +38 -6
  471. package/src/resources/extensions/gsd/tests/plan-milestone-sketch-render.test.ts +157 -0
  472. package/src/resources/extensions/gsd/tests/post-exec-retry-bypass.test.ts +2 -2
  473. package/src/resources/extensions/gsd/tests/progressive-planning.test.ts +1 -1
  474. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +32 -1
  475. package/src/resources/extensions/gsd/tests/provider-switch-observer.test.ts +252 -0
  476. package/src/resources/extensions/gsd/tests/resume-dispatch-worktree.test.ts +7 -3
  477. package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +6 -3
  478. package/src/resources/extensions/gsd/tests/session-start-footer.test.ts +16 -4
  479. package/src/resources/extensions/gsd/tests/session-switch-abort-misclassification.test.ts +24 -0
  480. package/src/resources/extensions/gsd/tests/state-corruption-2945.test.ts +65 -58
  481. package/src/resources/extensions/gsd/tests/state-reconciliation-drift.test.ts +952 -0
  482. package/src/resources/extensions/gsd/tests/token-tool-gating.test.ts +4 -0
  483. package/src/resources/extensions/gsd/tests/tui-header-lifecycle.test.ts +121 -1
  484. package/src/resources/extensions/gsd/tests/tui-render-kit.test.ts +66 -0
  485. package/src/resources/extensions/gsd/tests/verification-retry-policy.test.ts +83 -0
  486. package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +6 -0
  487. package/src/resources/extensions/gsd/tests/worktree-journal-events.test.ts +158 -58
  488. package/src/resources/extensions/gsd/tests/worktree-lifecycle.test.ts +572 -118
  489. package/src/resources/extensions/gsd/tests/worktree-telemetry.test.ts +59 -2
  490. package/src/resources/extensions/gsd/tests/write-gate-planning-unit.test.ts +18 -0
  491. package/src/resources/extensions/gsd/tui/render-kit.ts +109 -0
  492. package/src/resources/extensions/gsd/watch/header-renderer.ts +121 -79
  493. package/src/resources/extensions/gsd/watch/splash-palette.ts +11 -0
  494. package/src/resources/extensions/gsd/workflow-logger.ts +4 -0
  495. package/src/resources/extensions/gsd/workflow-mcp.ts +2 -2
  496. package/src/resources/extensions/gsd/worktree-lifecycle.ts +1151 -524
  497. package/src/resources/extensions/gsd/worktree-telemetry.ts +7 -2
  498. package/dist/web/standalone/.next/static/chunks/app/page-200592a7f3baf579.js +0 -1
  499. package/dist/web/standalone/.next/static/chunks/main-app-d3d4c336195465f9.js +0 -1
  500. package/dist/web/standalone/.next/static/chunks/next/dist/client/components/builtin/global-error-ab5a8926e07ec673.js +0 -1
  501. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +0 -1544
  502. /package/dist/web/standalone/.next/static/{drLMkgfHQ8lzS229_HWYR → YEvjuT-fsFfYQhDSWtueS}/_buildManifest.js +0 -0
  503. /package/dist/web/standalone/.next/static/{drLMkgfHQ8lzS229_HWYR → YEvjuT-fsFfYQhDSWtueS}/_ssgManifest.js +0 -0
@@ -17,7 +17,7 @@
17
17
  * a circular reference. Both classes share the body until the Resolver retires.
18
18
  */
19
19
 
20
- import { existsSync, unlinkSync } from "node:fs";
20
+ import { existsSync, readFileSync, unlinkSync } from "node:fs";
21
21
  import { randomUUID } from "node:crypto";
22
22
  import { join } from "node:path";
23
23
 
@@ -35,14 +35,46 @@ import {
35
35
  releaseMilestoneLease,
36
36
  } from "./db/milestone-leases.js";
37
37
  import { MergeConflictError } from "./git-service.js";
38
+ import type { GitPreferences } from "./git-service.js";
38
39
  import {
39
40
  getCollapseCadence,
40
41
  getMilestoneResquash,
41
42
  resquashMilestoneOnMain,
42
43
  } from "./slice-cadence.js";
43
- import { loadEffectiveGSDPreferences } from "./preferences.js";
44
+ // ADR-016 phase 2 / C3 (#5626): cache + preferences + path helpers inlined
45
+ // as direct imports. They are leaf-level functions that do not vary across
46
+ // callers — production wiring previously injected them via deps; the seam
47
+ // added type churn without enabling test variation.
48
+ import { loadEffectiveGSDPreferences, getIsolationMode } from "./preferences.js";
49
+ import { invalidateAllCaches } from "./cache.js";
50
+ import { resolveMilestoneFile } from "./paths.js";
44
51
  import type { WorktreeStateProjection } from "./worktree-state-projection.js";
45
52
  import { createWorkspace, scopeMilestone } from "./workspace.js";
53
+ // ADR-016 phase 2 / C1 (#5624): file-system + git-CLI leaf primitives
54
+ // inlined as direct imports rather than injected through `WorktreeLifecycleDeps`.
55
+ // These four symbols (`readFileSync` from node:fs, `getCurrentBranch` and
56
+ // `autoCommitCurrentBranch` from `./worktree.js`, `nativeCheckoutBranch` from
57
+ // `./native-git-bridge.js`) are leaf-level primitives — no environment varies
58
+ // across callers — so the dependency-injection seam they used to inhabit was
59
+ // adding type churn without enabling any test variation.
60
+ import {
61
+ autoCommitCurrentBranch,
62
+ getCurrentBranch,
63
+ } from "./worktree.js";
64
+ import { nativeCheckoutBranch } from "./native-git-bridge.js";
65
+ // ADR-016 phase 2 / C2 (#5625): worktree-manager helpers inlined from
66
+ // `./auto-worktree.js`. These seven functions are not real seams — Lifecycle
67
+ // is the only Module that calls them, and they live alongside the Module's
68
+ // other primitives in `auto-worktree.ts`.
69
+ import {
70
+ autoWorktreeBranch,
71
+ createAutoWorktree,
72
+ enterAutoWorktree,
73
+ enterBranchModeForMilestone,
74
+ getAutoWorktreePath,
75
+ isInAutoWorktree,
76
+ teardownAutoWorktree,
77
+ } from "./auto-worktree.js";
46
78
 
47
79
  // ─── Types ───────────────────────────────────────────────────────────────
48
80
 
@@ -64,19 +96,16 @@ export interface NotifyCtx {
64
96
  * recursion retires; shrinking it now would force a parallel migration.
65
97
  */
66
98
  export interface WorktreeLifecycleDeps {
67
- // ── Entry / branch-mode setup ────────────────────────────────────────
68
- enterAutoWorktree: (basePath: string, milestoneId: string) => string;
69
- createAutoWorktree: (basePath: string, milestoneId: string) => string;
70
- enterBranchModeForMilestone: (basePath: string, milestoneId: string) => void;
71
- getAutoWorktreePath: (basePath: string, milestoneId: string) => string | null;
72
- getIsolationMode: (basePath?: string) => "worktree" | "branch" | "none";
73
-
74
- // ── Cache + git service rebuild ──────────────────────────────────────
75
- invalidateAllCaches: () => void;
76
- GitServiceImpl: new (basePath: string, gitConfig: unknown) => unknown;
77
- loadEffectiveGSDPreferences: () =>
78
- | { preferences?: { git?: Record<string, unknown> } }
79
- | undefined;
99
+ // ── Git service factory (ADR-016 phase 2 / C4) ───────────────────────
100
+ /**
101
+ * Build a fresh `GitService` instance bound to `basePath`.
102
+ *
103
+ * Hides the constructor shape (new GitServiceImpl(basePath, gitConfig))
104
+ * and the gitConfig load from Lifecycle. The factory takes only a
105
+ * `basePath` and is responsible for loading any config it needs.
106
+ * Tests substitute fakes by passing a function that returns a stub.
107
+ */
108
+ gitServiceFactory: (basePath: string) => AutoSession["gitService"];
80
109
 
81
110
  // ── State Projection Module (ADR-016 one-way edge) ───────────────────
82
111
  /**
@@ -85,44 +114,60 @@ export interface WorktreeLifecycleDeps {
85
114
  */
86
115
  worktreeProjection: WorktreeStateProjection;
87
116
 
88
- // ── Exit / merge / teardown ──────────────────────────────────────────
89
- isInAutoWorktree: (basePath: string) => boolean;
90
- autoCommitCurrentBranch: (
91
- basePath: string,
92
- reason: string,
93
- milestoneId: string,
94
- ) => void;
95
- autoWorktreeBranch: (milestoneId: string) => string;
96
- teardownAutoWorktree: (
97
- basePath: string,
98
- milestoneId: string,
99
- opts?: { preserveBranch?: boolean },
100
- ) => void;
101
- mergeMilestoneToMain: (
102
- basePath: string,
103
- milestoneId: string,
104
- roadmapContent: string,
105
- ) => { pushed: boolean; codeFilesChanged: boolean };
106
- getCurrentBranch: (basePath: string) => string;
117
+ // ── Merge primitive ──────────────────────────────────────────────────
107
118
  /**
108
- * Force-checkout the named branch in `basePath`. Required by the branch-mode
109
- * merge path when HEAD has been moved off the milestone branch — silently
110
- * skipping the merge would strand the milestone's commits.
119
+ * Inner squash-merge primitive (`auto-worktree.ts:mergeMilestoneToMain`).
120
+ *
121
+ * **Module-internal seam do not construct your own.** Only the wiring
122
+ * factory `auto.ts:buildWorktreeLifecycleDeps()` is permitted to populate
123
+ * this field. The primitive is `@internal`; production callers reach the
124
+ * merge body through `WorktreeLifecycle.exitMilestone({ merge: true })`,
125
+ * never by calling this dep directly.
111
126
  */
112
- checkoutBranch: (basePath: string, branch: string) => void;
113
- resolveMilestoneFile: (
127
+ mergeMilestoneToMain: (
114
128
  basePath: string,
115
129
  milestoneId: string,
116
- fileType: string,
117
- ) => string | null;
118
- /**
119
- * Roadmap file reader. Injected so unit tests can substitute fixture
120
- * content without touching the filesystem; production wiring passes
121
- * `node:fs.readFileSync`.
122
- */
123
- readFileSync: (path: string, encoding: string) => string;
130
+ roadmapContent: string,
131
+ ) => {
132
+ pushed: boolean;
133
+ codeFilesChanged: boolean;
134
+ commitMessage?: string;
135
+ };
136
+
137
+ // ADR-016 phase 2 / C1 + C2 + C3 + C4 inlined the following fields as
138
+ // direct imports — leaf primitives that did not vary across callers:
139
+ // C1 (#5624): readFileSync, getCurrentBranch, checkoutBranch,
140
+ // autoCommitCurrentBranch
141
+ // C2 (#5625): enterAutoWorktree, createAutoWorktree,
142
+ // enterBranchModeForMilestone, getAutoWorktreePath,
143
+ // teardownAutoWorktree, isInAutoWorktree, autoWorktreeBranch
144
+ // C3 (#5626): invalidateAllCaches, loadEffectiveGSDPreferences,
145
+ // getIsolationMode, resolveMilestoneFile
146
+ // C4 (#5627): GitServiceImpl constructor → gitServiceFactory above
147
+ //
148
+ // ADR-016 phase 3 (#5693) deleted the @deprecated optional fields that
149
+ // remained on this Interface for legacy test fixtures. Tests that need to
150
+ // substitute primitive implementations cast their deps to
151
+ // `WorktreeLifecycleTestOverrides` (exported below) — the test seam now
152
+ // lives outside the public Interface.
153
+ //
154
+ // Final dep bag: 3 fields. The ADR's envisioned shape was ≤6.
124
155
  }
125
156
 
157
+ /**
158
+ * Test-only override shim. Production callers do not use this type — it
159
+ * exists so legacy test fixtures can substitute the primitive implementations
160
+ * that were inlined into Lifecycle in ADR-016 phase 2 (C1-C4). Pass an object
161
+ * typed `WorktreeLifecycleDeps & WorktreeLifecycleTestOverrides` to the
162
+ * `WorktreeLifecycle` constructor; Lifecycle reads the overrides through the
163
+ * structural-typing escape hatch in `primitiveOverrides()`.
164
+ *
165
+ * The fields here intentionally duplicate the C1-C4-inlined primitive
166
+ * signatures. Adding new fields is fine when a test needs to vary a primitive
167
+ * that has no other seam.
168
+ */
169
+ export type WorktreeLifecycleTestOverrides = WorktreeLifecyclePrimitiveOverrides;
170
+
126
171
  /**
127
172
  * Internal sentinel — thrown by `_mergeBranchMode` when it has already
128
173
  * emitted a user-visible error. The outer `mergeAndExit` catches the type
@@ -166,6 +211,49 @@ export type ExitResult =
166
211
  | { ok: true; merged: boolean; codeFilesChanged: boolean }
167
212
  | { ok: false; reason: "merge-conflict" | "teardown-failed"; cause?: unknown };
168
213
 
214
+ /**
215
+ * Session-less merge entry context. Per ADR-016 phase 2 / A1 (#5616), the
216
+ * merge body is structurally session-less — it reads project root, worktree
217
+ * path, and milestoneId. Single-loop callers (`_mergeAndExit`) build a
218
+ * MergeContext from `this.s`. Parallel callers (`parallel-merge.ts`) build
219
+ * one directly without an `AutoSession`.
220
+ */
221
+ export interface MergeContext {
222
+ /** Project root — merge target (where `git merge --squash` lands). */
223
+ originalBasePath: string;
224
+ /**
225
+ * Current worktree path or project root when in branch mode. Used as the
226
+ * cwd anchor for `mergeMilestoneToMain` and the source for
227
+ * `Projection.finalizeProjectionForMerge`.
228
+ */
229
+ worktreeBasePath: string;
230
+ milestoneId: string;
231
+ /**
232
+ * When true, `mergeMilestoneStandalone` returns `{ merged: false,
233
+ * mode: "skipped" }` immediately (mirrors the single-loop guard). Default
234
+ * `false` for parallel callers, which never run with degraded isolation.
235
+ */
236
+ isolationDegraded?: boolean;
237
+ notify: NotifyCtx["notify"];
238
+ }
239
+
240
+ /**
241
+ * Result of `mergeMilestoneStandalone`. `mode` lets callers decide which
242
+ * session-bound side effects to run (worktree-mode → `restoreToProjectRoot`,
243
+ * branch-mode → `rebuildGitService`, skipped → none).
244
+ */
245
+ export interface MergeStandaloneResult {
246
+ merged: boolean;
247
+ mode: "worktree" | "branch" | "skipped";
248
+ codeFilesChanged: boolean;
249
+ pushed: boolean;
250
+ /**
251
+ * Commit message produced by the squash merge, if available. Forwarded
252
+ * from `mergeMilestoneToMain`. Only populated when `merged === true`.
253
+ */
254
+ commitMessage?: string;
255
+ }
256
+
169
257
  // ─── Validation ──────────────────────────────────────────────────────────
170
258
 
171
259
  function isValidMilestoneId(milestoneId: string): boolean {
@@ -178,6 +266,206 @@ function invalidMilestoneIdError(milestoneId: string): Error {
178
266
  );
179
267
  }
180
268
 
269
+ type WorktreeLifecyclePrimitiveOverrides = {
270
+ readFileSync?: (path: string, encoding: BufferEncoding) => string;
271
+ getCurrentBranch?: (basePath: string) => string;
272
+ checkoutBranch?: (basePath: string, branch: string) => void;
273
+ autoCommitCurrentBranch?: (
274
+ basePath: string,
275
+ unitType: string,
276
+ unitId: string,
277
+ taskContext?: unknown,
278
+ ) => string | null;
279
+ getAutoWorktreePath?: (
280
+ basePath: string,
281
+ milestoneId: string,
282
+ ) => string | null;
283
+ // ADR-016 phase 2 / C2-inlined worktree-manager primitives. Tests still
284
+ // stub these via the structural-typing escape hatch on `WorktreeLifecycleDeps`,
285
+ // so the call sites below check for an override first and fall back to the
286
+ // imported direct primitive.
287
+ isInAutoWorktree?: (basePath: string) => boolean;
288
+ autoWorktreeBranch?: (milestoneId: string) => string;
289
+ teardownAutoWorktree?: (
290
+ basePath: string,
291
+ milestoneId: string,
292
+ opts?: { preserveBranch?: boolean },
293
+ ) => void;
294
+ createAutoWorktree?: (basePath: string, milestoneId: string) => string;
295
+ enterAutoWorktree?: (basePath: string, milestoneId: string) => string;
296
+ enterBranchModeForMilestone?: (basePath: string, milestoneId: string) => void;
297
+ // ADR-016 phase 2 / C3-inlined cache + preferences + path helpers.
298
+ getIsolationMode?: (basePath?: string) => "worktree" | "branch" | "none";
299
+ invalidateAllCaches?: () => void;
300
+ resolveMilestoneFile?: (
301
+ basePath: string,
302
+ milestoneId: string,
303
+ fileType: string,
304
+ ) => string | null;
305
+ loadEffectiveGSDPreferences?: (basePath?: string) =>
306
+ | { preferences?: { git?: Record<string, unknown> } }
307
+ | null
308
+ | undefined;
309
+ };
310
+
311
+ function primitiveOverrides(
312
+ deps: WorktreeLifecycleDeps,
313
+ ): WorktreeLifecyclePrimitiveOverrides {
314
+ return deps as WorktreeLifecycleDeps & WorktreeLifecyclePrimitiveOverrides;
315
+ }
316
+
317
+ function readLifecycleFile(
318
+ deps: WorktreeLifecycleDeps,
319
+ path: string,
320
+ ): string {
321
+ return primitiveOverrides(deps).readFileSync?.(path, "utf-8") ??
322
+ readFileSync(path, "utf-8");
323
+ }
324
+
325
+ function currentLifecycleBranch(
326
+ deps: WorktreeLifecycleDeps,
327
+ basePath: string,
328
+ ): string {
329
+ return primitiveOverrides(deps).getCurrentBranch?.(basePath) ??
330
+ getCurrentBranch(basePath);
331
+ }
332
+
333
+ function checkoutLifecycleBranch(
334
+ deps: WorktreeLifecycleDeps,
335
+ basePath: string,
336
+ branch: string,
337
+ ): void {
338
+ const checkoutBranch = primitiveOverrides(deps).checkoutBranch;
339
+ if (checkoutBranch) {
340
+ checkoutBranch(basePath, branch);
341
+ return;
342
+ }
343
+ nativeCheckoutBranch(basePath, branch);
344
+ }
345
+
346
+ function autoCommitLifecycleBranch(
347
+ deps: WorktreeLifecycleDeps,
348
+ basePath: string,
349
+ unitType: string,
350
+ unitId: string,
351
+ ): string | null {
352
+ return primitiveOverrides(deps).autoCommitCurrentBranch?.(
353
+ basePath,
354
+ unitType,
355
+ unitId,
356
+ ) ?? autoCommitCurrentBranch(basePath, unitType, unitId);
357
+ }
358
+
359
+ // ADR-016 phase 2 / C2-inlined worktree-manager primitives — helpers that
360
+ // honour the structural-typing override pattern so legacy test fixtures keep
361
+ // working without rewriting them onto real-git fixtures.
362
+ function lifecycleIsInAutoWorktree(
363
+ deps: WorktreeLifecycleDeps,
364
+ basePath: string,
365
+ ): boolean {
366
+ return primitiveOverrides(deps).isInAutoWorktree?.(basePath) ??
367
+ isInAutoWorktree(basePath);
368
+ }
369
+
370
+ function lifecycleAutoWorktreeBranch(
371
+ deps: WorktreeLifecycleDeps,
372
+ milestoneId: string,
373
+ ): string {
374
+ return primitiveOverrides(deps).autoWorktreeBranch?.(milestoneId) ??
375
+ autoWorktreeBranch(milestoneId);
376
+ }
377
+
378
+ function lifecycleTeardownAutoWorktree(
379
+ deps: WorktreeLifecycleDeps,
380
+ basePath: string,
381
+ milestoneId: string,
382
+ opts?: { preserveBranch?: boolean },
383
+ ): void {
384
+ const override = primitiveOverrides(deps).teardownAutoWorktree;
385
+ if (override) {
386
+ override(basePath, milestoneId, opts);
387
+ return;
388
+ }
389
+ teardownAutoWorktree(basePath, milestoneId, opts);
390
+ }
391
+
392
+ function lifecycleCreateAutoWorktree(
393
+ deps: WorktreeLifecycleDeps,
394
+ basePath: string,
395
+ milestoneId: string,
396
+ ): string {
397
+ return primitiveOverrides(deps).createAutoWorktree?.(basePath, milestoneId) ??
398
+ createAutoWorktree(basePath, milestoneId);
399
+ }
400
+
401
+ function lifecycleEnterAutoWorktree(
402
+ deps: WorktreeLifecycleDeps,
403
+ basePath: string,
404
+ milestoneId: string,
405
+ ): string {
406
+ return primitiveOverrides(deps).enterAutoWorktree?.(basePath, milestoneId) ??
407
+ enterAutoWorktree(basePath, milestoneId);
408
+ }
409
+
410
+ function lifecycleEnterBranchMode(
411
+ deps: WorktreeLifecycleDeps,
412
+ basePath: string,
413
+ milestoneId: string,
414
+ ): void {
415
+ const override = primitiveOverrides(deps).enterBranchModeForMilestone;
416
+ if (override) {
417
+ override(basePath, milestoneId);
418
+ return;
419
+ }
420
+ enterBranchModeForMilestone(basePath, milestoneId);
421
+ }
422
+
423
+ // ADR-016 phase 2 / C3-inlined cache + preferences + path helpers.
424
+ function lifecycleGetIsolationMode(
425
+ deps: WorktreeLifecycleDeps,
426
+ basePath?: string,
427
+ ): "worktree" | "branch" | "none" {
428
+ return primitiveOverrides(deps).getIsolationMode?.(basePath) ??
429
+ getIsolationMode(basePath);
430
+ }
431
+
432
+ function lifecycleInvalidateAllCaches(deps: WorktreeLifecycleDeps): void {
433
+ const override = primitiveOverrides(deps).invalidateAllCaches;
434
+ if (override) {
435
+ override();
436
+ return;
437
+ }
438
+ invalidateAllCaches();
439
+ }
440
+
441
+ function lifecycleResolveMilestoneFile(
442
+ deps: WorktreeLifecycleDeps,
443
+ basePath: string,
444
+ milestoneId: string,
445
+ fileType: string,
446
+ ): string | null {
447
+ return primitiveOverrides(deps).resolveMilestoneFile?.(
448
+ basePath,
449
+ milestoneId,
450
+ fileType,
451
+ ) ?? resolveMilestoneFile(basePath, milestoneId, fileType);
452
+ }
453
+
454
+ function lifecycleLoadPreferences(
455
+ deps: WorktreeLifecycleDeps,
456
+ basePath?: string,
457
+ ):
458
+ | { preferences?: { git?: Record<string, unknown> } }
459
+ | null
460
+ | undefined {
461
+ const override = primitiveOverrides(deps).loadEffectiveGSDPreferences;
462
+ if (override) return override(basePath);
463
+ return loadEffectiveGSDPreferences(basePath) as
464
+ | { preferences?: { git?: Record<string, unknown> } }
465
+ | null
466
+ | undefined;
467
+ }
468
+
181
469
  /**
182
470
  * Throwing variant used by the merge/exit paths that surface failures via
183
471
  * the typed `ExitResult` (callers wrap the throw → cause). The enter path
@@ -336,7 +624,7 @@ export function _enterMilestoneCore(
336
624
  // Handles the case where originalBasePath is falsy and basePath is itself
337
625
  // a worktree path — prevents double-nested worktree paths (#3729).
338
626
  const basePath = resolveWorktreeProjectRoot(s.basePath, s.originalBasePath);
339
- const mode = deps.getIsolationMode(basePath);
627
+ const mode = getIsolationMode(basePath);
340
628
 
341
629
  if (mode === "none") {
342
630
  debugLog("WorktreeLifecycle", {
@@ -380,12 +668,12 @@ export function _enterMilestoneCore(
380
668
  // ── Branch mode: create/checkout milestone branch, stay in project root ──
381
669
  if (mode === "branch") {
382
670
  try {
383
- deps.enterBranchModeForMilestone(basePath, milestoneId);
671
+ lifecycleEnterBranchMode(deps, basePath, milestoneId);
384
672
  // basePath does not change — no worktree, no chdir.
385
673
  // Rebuild GitService so the new HEAD is reflected, then flush any
386
674
  // path-keyed caches that may have been populated before the checkout.
387
675
  rebuildGitService(s, deps);
388
- deps.invalidateAllCaches();
676
+ invalidateAllCaches();
389
677
  debugLog("WorktreeLifecycle", {
390
678
  action: "enterMilestone",
391
679
  milestoneId,
@@ -421,18 +709,22 @@ export function _enterMilestoneCore(
421
709
 
422
710
  // ── Worktree mode ────────────────────────────────────────────────────────
423
711
  try {
424
- const existingPath = deps.getAutoWorktreePath(basePath, milestoneId);
712
+ const existingPath =
713
+ (primitiveOverrides(deps).getAutoWorktreePath ?? getAutoWorktreePath)(
714
+ basePath,
715
+ milestoneId,
716
+ );
425
717
  let wtPath: string;
426
718
 
427
719
  if (existingPath) {
428
- wtPath = deps.enterAutoWorktree(basePath, milestoneId);
720
+ wtPath = lifecycleEnterAutoWorktree(deps, basePath, milestoneId);
429
721
  } else {
430
- wtPath = deps.createAutoWorktree(basePath, milestoneId);
722
+ wtPath = lifecycleCreateAutoWorktree(deps, basePath, milestoneId);
431
723
  }
432
724
 
433
725
  s.basePath = wtPath;
434
726
  rebuildGitService(s, deps);
435
- deps.invalidateAllCaches();
727
+ invalidateAllCaches();
436
728
 
437
729
  // Per ADR-016: Lifecycle calls Projection on entry, before any Unit
438
730
  // dispatches. Build a temporary scope from the new basePath; callers may
@@ -511,122 +803,607 @@ export function _enterMilestoneCore(
511
803
  }
512
804
  }
513
805
 
806
+ /**
807
+ * Resolve the basePath to adopt on resume from a paused session.
808
+ *
809
+ * Returns `persistedWorktreePath` when the path is non-null and exists on
810
+ * disk; otherwise falls back to `base`. Used by
811
+ * `WorktreeLifecycle.resumeFromPausedSession` (#5621). Exported as a pure
812
+ * function so unit tests can exercise the path-resolution logic without
813
+ * constructing a `WorktreeLifecycle` instance.
814
+ *
815
+ * The optional `pathExists` parameter exists only for tests that need to
816
+ * substitute a stub for `existsSync`.
817
+ */
818
+ export function resolvePausedResumeBasePath(
819
+ base: string,
820
+ persistedWorktreePath: string | null | undefined,
821
+ pathExists: (p: string) => boolean = existsSync,
822
+ ): string {
823
+ return persistedWorktreePath && pathExists(persistedWorktreePath)
824
+ ? persistedWorktreePath
825
+ : base;
826
+ }
827
+
514
828
  function rebuildGitService(
515
829
  s: AutoSession,
516
830
  deps: WorktreeLifecycleDeps,
517
831
  ): void {
518
- const gitConfig =
519
- deps.loadEffectiveGSDPreferences()?.preferences?.git ?? {};
520
- s.gitService = new deps.GitServiceImpl(
521
- s.basePath,
522
- gitConfig,
523
- ) as AutoSession["gitService"];
832
+ // ADR-016 phase 2 / C4 (#5627): the gitConfig load and constructor
833
+ // construction live behind `gitServiceFactory`. Lifecycle no longer
834
+ // sees the constructor shape, the gitConfig type, or the unknown→
835
+ // GitService cast.
836
+ s.gitService = deps.gitServiceFactory(s.basePath);
524
837
  }
525
838
 
526
- // ─── Module class ────────────────────────────────────────────────────────
839
+ // ─── Session-less merge entry (ADR-016 phase 2 / A1) ─────────────────────
527
840
 
528
841
  /**
529
- * Worktree Lifecycle module instance.
842
+ * Worktree-mode merge body. Session-less — operates on a `MergeContext`.
530
843
  *
531
- * Constructed once per auto-mode session. Holds the session reference so
532
- * verbs can mutate `s.basePath` and related coordination state directly
533
- * without round-tripping through callers.
844
+ * On error: emits the "worktree-merge-failed" journal event, notifies the
845
+ * user, cleans up stale `SQUASH_MSG` / `MERGE_HEAD` / `MERGE_MSG` files
846
+ * (#1389), and chdirs back to project root before rethrowing. Session-side
847
+ * cleanup (`restoreToProjectRoot`, `gitService` rebuild) is the caller's
848
+ * responsibility.
534
849
  */
535
- export class WorktreeLifecycle {
536
- private readonly s: AutoSession;
537
- private readonly deps: WorktreeLifecycleDeps;
538
-
539
- constructor(s: AutoSession, deps: WorktreeLifecycleDeps) {
540
- this.s = s;
541
- this.deps = deps;
850
+ function _mergeWorktreeModeImpl(
851
+ deps: WorktreeLifecycleDeps,
852
+ mctx: MergeContext,
853
+ ): MergeStandaloneResult {
854
+ const { originalBasePath, worktreeBasePath, milestoneId, notify } = mctx;
855
+ if (!originalBasePath) {
856
+ debugLog("WorktreeLifecycle", {
857
+ action: "mergeAndExit",
858
+ milestoneId,
859
+ mode: "worktree",
860
+ skipped: true,
861
+ reason: "missing-original-base",
862
+ });
863
+ return {
864
+ merged: false,
865
+ mode: "worktree",
866
+ codeFilesChanged: false,
867
+ pushed: false,
868
+ };
542
869
  }
543
870
 
544
- /**
545
- * Enter or create the auto-worktree for `milestoneId`. Idempotent if
546
- * already in this milestone (lease refreshed; basePath unchanged).
547
- *
548
- * Returns a typed `EnterResult` describing the outcome. Callers may
549
- * ignore the result if they read `s.basePath` directly afterwards
550
- * (legacy behaviour); new callers should branch on the result.
551
- */
552
- enterMilestone(milestoneId: string, ctx: NotifyCtx): EnterResult {
553
- return _enterMilestoneCore(this.s, this.deps, milestoneId, ctx);
554
- }
871
+ try {
872
+ // ADR-016: final projection before teardown. Replaces the legacy
873
+ // syncWorktreeStateBack(originalBase, basePath, milestoneId) call.
874
+ const finalScope = scopeMilestone(
875
+ createWorkspace(worktreeBasePath),
876
+ milestoneId,
877
+ );
878
+ const { synced } = deps.worktreeProjection.finalizeProjectionForMerge(
879
+ finalScope,
880
+ );
881
+ if (synced.length > 0) {
882
+ debugLog("WorktreeLifecycle", {
883
+ action: "mergeAndExit",
884
+ milestoneId,
885
+ phase: "reverse-sync",
886
+ synced: synced.length,
887
+ });
888
+ }
555
889
 
556
- /**
557
- * Exit the current worktree. With `opts.merge === true`, runs the full
558
- * merge-and-teardown path (worktree-mode or branch-mode auto-detected).
559
- * With `opts.merge === false`, runs auto-commit and teardown without
560
- * merging to main.
561
- *
562
- * Returns a typed `ExitResult`. `MergeConflictError` is surfaced as
563
- * `{ ok: false, reason: "merge-conflict", cause }` instead of thrown,
564
- * giving callers a typed branch for the expected failure path.
565
- * Unexpected failures (filesystem, git permissions, etc.) are wrapped
566
- * as `{ ok: false, reason: "teardown-failed", cause }` so callers always
567
- * receive a discriminated union — no exceptions for any expected outcome.
568
- */
569
- exitMilestone(
570
- milestoneId: string,
571
- opts: { merge: boolean; preserveBranch?: boolean },
572
- ctx: NotifyCtx,
573
- ): ExitResult {
574
- if (opts.merge) {
575
- try {
576
- const merged = this._mergeAndExit(milestoneId, ctx);
577
- return { ok: true, merged, codeFilesChanged: false };
578
- } catch (err) {
579
- if (err instanceof MergeConflictError) {
580
- return { ok: false, reason: "merge-conflict", cause: err };
581
- }
582
- return { ok: false, reason: "teardown-failed", cause: err };
890
+ // Resolve roadmap — try project root first, then worktree path as
891
+ // fallback. The worktree may hold the only copy when state-back
892
+ // projection silently dropped it or .gsd/ is not symlinked. Without
893
+ // the fallback, a missing roadmap triggers bare teardown which
894
+ // deletes the branch and orphans all milestone commits (#1573).
895
+ let roadmapPath = resolveMilestoneFile(
896
+ originalBasePath,
897
+ milestoneId,
898
+ "ROADMAP",
899
+ );
900
+ if (
901
+ !roadmapPath &&
902
+ !isSamePathPhysical(worktreeBasePath, originalBasePath)
903
+ ) {
904
+ roadmapPath = resolveMilestoneFile(
905
+ worktreeBasePath,
906
+ milestoneId,
907
+ "ROADMAP",
908
+ );
909
+ if (roadmapPath) {
910
+ debugLog("WorktreeLifecycle", {
911
+ action: "mergeAndExit",
912
+ milestoneId,
913
+ phase: "roadmap-fallback",
914
+ note: "resolved from worktree path",
915
+ });
583
916
  }
584
917
  }
585
- try {
586
- this._exitWithoutMerge(milestoneId, ctx, {
587
- preserveBranch: opts.preserveBranch,
918
+
919
+ if (!roadmapPath) {
920
+ // No roadmap at either location — teardown but PRESERVE the branch
921
+ // so commits are not orphaned (#1573).
922
+ lifecycleTeardownAutoWorktree(deps, originalBasePath, milestoneId, {
923
+ preserveBranch: true,
588
924
  });
589
- return { ok: true, merged: false, codeFilesChanged: false };
590
- } catch (err) {
591
- return { ok: false, reason: "teardown-failed", cause: err };
925
+ notify(
926
+ `Exited worktree for ${milestoneId} (no roadmap found — branch preserved for manual merge).`,
927
+ "warning",
928
+ );
929
+ return {
930
+ merged: false,
931
+ mode: "worktree",
932
+ codeFilesChanged: false,
933
+ pushed: false,
934
+ };
592
935
  }
593
- }
594
936
 
595
- /**
596
- * Milestone transition: merge the current milestone, then enter the next
597
- * one. Pattern used when the loop detects that the active milestone has
598
- * changed (current completed, next is now active). Caller is responsible
599
- * for re-deriving state between the merge and the enter.
600
- */
601
- mergeAndEnterNext(
602
- currentMilestoneId: string,
603
- nextMilestoneId: string,
604
- ctx: NotifyCtx,
605
- ): void {
606
- debugLog("WorktreeLifecycle", {
607
- action: "mergeAndEnterNext",
608
- currentMilestoneId,
609
- nextMilestoneId,
610
- });
611
- let merged = false;
612
- let mergeThrew = false;
937
+ const roadmapContent = readLifecycleFile(deps, roadmapPath);
938
+ const mergeResult = deps.mergeMilestoneToMain(
939
+ originalBasePath,
940
+ milestoneId,
941
+ roadmapContent,
942
+ );
943
+
944
+ // #2945 Bug 3: mergeMilestoneToMain performs best-effort worktree
945
+ // cleanup internally (step 12), but it can silently fail on Windows
946
+ // or when the worktree directory is locked. Perform a secondary
947
+ // teardown here to ensure the worktree is properly cleaned up.
948
+ // Idempotent — if already removed, teardownAutoWorktree no-ops.
613
949
  try {
614
- merged = this._mergeAndExit(currentMilestoneId, ctx);
615
- } catch (err) {
616
- if (err instanceof UserNotifiedError) throw err;
617
- mergeThrew = true;
618
- // _mergeAndExit emits a warning and restores state on failure during
619
- // merge/cleanup. If it throws before recovery runs (e.g. validation,
620
- // emitJournalEvent), basePath isn't restored — re-throw so we don't
621
- // enter the next milestone with the current one unmerged.
622
- const projectRoot = resolveWorktreeProjectRoot(
623
- this.s.basePath,
624
- this.s.originalBasePath,
625
- );
626
- if (this.s.basePath !== projectRoot) throw err;
627
- // Otherwise: merge attempted, failed cleanly with state restored.
628
- // The loop intentionally continues to the next milestone — the
629
- // failed milestone's branch is preserved for manual recovery.
950
+ lifecycleTeardownAutoWorktree(deps, originalBasePath, milestoneId);
951
+ } catch {
952
+ // Best-effort primary cleanup in mergeMilestoneToMain may have
953
+ // already removed the worktree.
954
+ }
955
+
956
+ if (mergeResult.codeFilesChanged) {
957
+ notify(
958
+ `Milestone ${milestoneId} merged to main.${mergeResult.pushed ? " Pushed to remote." : ""}`,
959
+ "info",
960
+ );
961
+ } else {
962
+ // #1906 milestone produced only .gsd/ metadata. Surface
963
+ // clearly so the user knows the milestone is not truly complete.
964
+ notify(
965
+ `WARNING: Milestone ${milestoneId} merged to main but contained NO code changes — only .gsd/ metadata files. ` +
966
+ `The milestone summary may describe planned work that was never implemented. ` +
967
+ `Review the milestone output and re-run if code is missing.`,
968
+ "warning",
969
+ );
970
+ }
971
+
972
+ return {
973
+ merged: true,
974
+ mode: "worktree",
975
+ codeFilesChanged: mergeResult.codeFilesChanged,
976
+ pushed: mergeResult.pushed,
977
+ commitMessage: mergeResult.commitMessage,
978
+ };
979
+ } catch (err) {
980
+ const msg = err instanceof Error ? err.message : String(err);
981
+ debugLog("WorktreeLifecycle", {
982
+ action: "mergeAndExit",
983
+ milestoneId,
984
+ result: "error",
985
+ error: msg,
986
+ fallback: "chdir-to-project-root",
987
+ });
988
+ emitJournalEvent(originalBasePath || worktreeBasePath, {
989
+ ts: new Date().toISOString(),
990
+ flowId: randomUUID(),
991
+ seq: 0,
992
+ eventType: "worktree-merge-failed",
993
+ data: { milestoneId, error: msg },
994
+ });
995
+ // Surface a clear, actionable error. Worktree and milestone branch
996
+ // are intentionally preserved — nothing has been deleted. User can
997
+ // retry /gsd dispatch complete-milestone or merge manually once the
998
+ // underlying issue is fixed (#1668, #1891).
999
+ notify(
1000
+ `Milestone merge failed: ${msg}. Your worktree and milestone branch are preserved — retry with \`/gsd dispatch complete-milestone\` or merge manually.`,
1001
+ "warning",
1002
+ );
1003
+
1004
+ // Clean up stale merge state left by failed squash-merge (#1389)
1005
+ try {
1006
+ const gitDir = join(originalBasePath || worktreeBasePath, ".git");
1007
+ for (const f of ["SQUASH_MSG", "MERGE_HEAD", "MERGE_MSG"]) {
1008
+ const p = join(gitDir, f);
1009
+ if (existsSync(p)) unlinkSync(p);
1010
+ }
1011
+ } catch {
1012
+ /* best-effort */
1013
+ }
1014
+
1015
+ // Error recovery: chdir back to project root only when no real worktree
1016
+ // path is available. Session-side cleanup (restoreToProjectRoot,
1017
+ // gitService rebuild) is the caller's responsibility.
1018
+ if (originalBasePath && !worktreeBasePath) {
1019
+ try {
1020
+ process.chdir(originalBasePath);
1021
+ } catch {
1022
+ /* best-effort */
1023
+ }
1024
+ }
1025
+
1026
+ // Re-throw: MergeConflictError stops the auto loop (#2330);
1027
+ // non-conflict errors must also propagate so broken states are
1028
+ // diagnosable (#4380).
1029
+ throw err;
1030
+ }
1031
+ }
1032
+
1033
+ /**
1034
+ * Branch-mode merge body. Session-less.
1035
+ *
1036
+ * Session-side `gitService` rebuild after HEAD changes is the caller's
1037
+ * responsibility. The branch-mode `UserNotifiedError` sentinel still flows
1038
+ * through unchanged so the outer caller can suppress duplicate toasts.
1039
+ */
1040
+ function _mergeBranchModeImpl(
1041
+ deps: WorktreeLifecycleDeps,
1042
+ mctx: MergeContext,
1043
+ ): MergeStandaloneResult {
1044
+ const { worktreeBasePath, milestoneId, notify } = mctx;
1045
+ try {
1046
+ const currentBranch = currentLifecycleBranch(deps, worktreeBasePath);
1047
+ const milestoneBranch = lifecycleAutoWorktreeBranch(deps, milestoneId);
1048
+
1049
+ if (currentBranch !== milestoneBranch) {
1050
+ // #5538-followup: previous behaviour was to silently `return false`
1051
+ // when HEAD wasn't on the milestone branch — that let the loop
1052
+ // advance with the milestone's commits stranded on the branch.
1053
+ // Attempt recovery by force-checking-out the milestone branch; if
1054
+ // that fails, throw so the caller pauses auto-mode and the user
1055
+ // sees the failure instead of a silent merge skip.
1056
+ debugLog("WorktreeLifecycle", {
1057
+ action: "mergeAndExit",
1058
+ milestoneId,
1059
+ mode: "branch",
1060
+ recovery: "checkout-milestone-branch",
1061
+ currentBranch,
1062
+ milestoneBranch,
1063
+ });
1064
+ try {
1065
+ checkoutLifecycleBranch(deps, worktreeBasePath, milestoneBranch);
1066
+ } catch (checkoutErr) {
1067
+ const checkoutMsg =
1068
+ checkoutErr instanceof Error
1069
+ ? checkoutErr.message
1070
+ : String(checkoutErr);
1071
+ notify(
1072
+ `Cannot merge milestone ${milestoneId}: working tree is on ${currentBranch} and checkout to ${milestoneBranch} failed (${checkoutMsg}). Resolve manually and run /gsd auto to resume.`,
1073
+ "error",
1074
+ );
1075
+ throw new UserNotifiedError(checkoutMsg, checkoutErr);
1076
+ }
1077
+
1078
+ const reverify = currentLifecycleBranch(deps, worktreeBasePath);
1079
+ if (reverify !== milestoneBranch) {
1080
+ const reverifyMsg = `branch checkout to ${milestoneBranch} reported success but current branch is ${reverify}`;
1081
+ notify(
1082
+ `Cannot merge milestone ${milestoneId}: ${reverifyMsg}. Resolve manually and run /gsd auto to resume.`,
1083
+ "error",
1084
+ );
1085
+ throw new UserNotifiedError(reverifyMsg);
1086
+ }
1087
+ }
1088
+
1089
+ const roadmapPath = resolveMilestoneFile(
1090
+ worktreeBasePath,
1091
+ milestoneId,
1092
+ "ROADMAP",
1093
+ );
1094
+ if (!roadmapPath) {
1095
+ debugLog("WorktreeLifecycle", {
1096
+ action: "mergeAndExit",
1097
+ milestoneId,
1098
+ mode: "branch",
1099
+ skipped: true,
1100
+ reason: "no-roadmap",
1101
+ });
1102
+ return {
1103
+ merged: false,
1104
+ mode: "branch",
1105
+ codeFilesChanged: false,
1106
+ pushed: false,
1107
+ };
1108
+ }
1109
+
1110
+ const roadmapContent = readLifecycleFile(deps, roadmapPath);
1111
+ const mergeResult = deps.mergeMilestoneToMain(
1112
+ worktreeBasePath,
1113
+ milestoneId,
1114
+ roadmapContent,
1115
+ );
1116
+
1117
+ if (mergeResult.codeFilesChanged) {
1118
+ notify(
1119
+ `Milestone ${milestoneId} merged (branch mode).${mergeResult.pushed ? " Pushed to remote." : ""}`,
1120
+ "info",
1121
+ );
1122
+ } else {
1123
+ notify(
1124
+ `WARNING: Milestone ${milestoneId} merged (branch mode) but contained NO code changes — only .gsd/ metadata. ` +
1125
+ `Review the milestone output and re-run if code is missing.`,
1126
+ "warning",
1127
+ );
1128
+ }
1129
+ debugLog("WorktreeLifecycle", {
1130
+ action: "mergeAndExit",
1131
+ milestoneId,
1132
+ mode: "branch",
1133
+ result: "success",
1134
+ });
1135
+ return {
1136
+ merged: true,
1137
+ mode: "branch",
1138
+ codeFilesChanged: mergeResult.codeFilesChanged,
1139
+ pushed: mergeResult.pushed,
1140
+ commitMessage: mergeResult.commitMessage,
1141
+ };
1142
+ } catch (err) {
1143
+ const msg = err instanceof Error ? err.message : String(err);
1144
+ debugLog("WorktreeLifecycle", {
1145
+ action: "mergeAndExit",
1146
+ milestoneId,
1147
+ mode: "branch",
1148
+ result: "error",
1149
+ error: msg,
1150
+ });
1151
+ if (!(err instanceof UserNotifiedError)) {
1152
+ notify(`Milestone merge failed (branch mode): ${msg}`, "warning");
1153
+ }
1154
+ // Re-throw all errors so callers can apply their own recovery (#4380).
1155
+ throw err;
1156
+ }
1157
+ }
1158
+
1159
+ /**
1160
+ * Session-less merge entry (ADR-016 phase 2 / A1, issue #5618).
1161
+ *
1162
+ * Runs the worktree-mode or branch-mode merge body without touching session
1163
+ * state. Used directly by `parallel-merge.ts` and indirectly (via
1164
+ * `_mergeAndExit`) by the single-loop path. Caller is responsible for any
1165
+ * session-side cleanup based on the returned `mode`.
1166
+ *
1167
+ * **CWD anchor**: anchors `process.cwd()` at `originalBasePath` before
1168
+ * non-worktree merge paths to mirror the single-loop guard against ENOENT
1169
+ * after teardown (de73fb43d). Worktree-mode merge paths keep the real
1170
+ * worktree as cwd because `mergeMilestoneToMain()` infers source worktree
1171
+ * state from `process.cwd()`. Best-effort; silent on failure.
1172
+ *
1173
+ * **Failure handling**: `MergeConflictError` and other unrecoverable errors
1174
+ * propagate to the caller. The caller is responsible for any state restore
1175
+ * (single-loop callers re-`chdir` and `restoreToProjectRoot`; parallel
1176
+ * callers surface to the user as a `MergeResult` with `success: false`).
1177
+ */
1178
+ export function mergeMilestoneStandalone(
1179
+ deps: WorktreeLifecycleDeps,
1180
+ mctx: MergeContext,
1181
+ ): MergeStandaloneResult {
1182
+ const { originalBasePath, worktreeBasePath, milestoneId, notify } = mctx;
1183
+ validateMilestoneId(milestoneId);
1184
+
1185
+ if (mctx.isolationDegraded) {
1186
+ if (originalBasePath) {
1187
+ try {
1188
+ process.chdir(originalBasePath);
1189
+ } catch (err) {
1190
+ debugLog("WorktreeLifecycle", {
1191
+ action: "mergeAndExit",
1192
+ phase: "pre-merge-chdir-failed",
1193
+ milestoneId,
1194
+ originalBasePath,
1195
+ error: err instanceof Error ? err.message : String(err),
1196
+ });
1197
+ }
1198
+ }
1199
+ debugLog("WorktreeLifecycle", {
1200
+ action: "mergeAndExit",
1201
+ milestoneId,
1202
+ skipped: true,
1203
+ reason: "isolation-degraded",
1204
+ });
1205
+ notify(
1206
+ `Skipping worktree merge for ${milestoneId} — isolation was degraded (worktree creation failed earlier). Work is on the current branch.`,
1207
+ "info",
1208
+ );
1209
+ return {
1210
+ merged: false,
1211
+ mode: "skipped",
1212
+ codeFilesChanged: false,
1213
+ pushed: false,
1214
+ };
1215
+ }
1216
+
1217
+ const mode = getIsolationMode(originalBasePath || worktreeBasePath);
1218
+ debugLog("WorktreeLifecycle", {
1219
+ action: "mergeAndExit",
1220
+ milestoneId,
1221
+ mode,
1222
+ basePath: worktreeBasePath,
1223
+ });
1224
+ emitJournalEvent(originalBasePath || worktreeBasePath, {
1225
+ ts: new Date().toISOString(),
1226
+ flowId: randomUUID(),
1227
+ seq: 0,
1228
+ eventType: "worktree-merge-start",
1229
+ data: { milestoneId, mode },
1230
+ });
1231
+
1232
+ // #2625: If we are physically inside an auto-worktree, we MUST merge
1233
+ // regardless of the current isolation config. This prevents data loss
1234
+ // when the default isolation mode changes between versions.
1235
+ const inWorktree =
1236
+ lifecycleIsInAutoWorktree(deps, worktreeBasePath) && Boolean(originalBasePath);
1237
+
1238
+ if (mode === "none" && !inWorktree) {
1239
+ debugLog("WorktreeLifecycle", {
1240
+ action: "mergeAndExit",
1241
+ milestoneId,
1242
+ skipped: true,
1243
+ reason: "mode-none",
1244
+ });
1245
+ // Anchor cwd at project root before the early return so subsequent
1246
+ // process.cwd() calls after the skip don't ENOENT if we were inside a
1247
+ // worktree directory that gets torn down later. Best-effort.
1248
+ if (originalBasePath) {
1249
+ try {
1250
+ process.chdir(originalBasePath);
1251
+ } catch {
1252
+ /* best-effort */
1253
+ }
1254
+ }
1255
+ return {
1256
+ merged: false,
1257
+ mode: "skipped",
1258
+ codeFilesChanged: false,
1259
+ pushed: false,
1260
+ };
1261
+ }
1262
+
1263
+ // Set cwd to the correct anchor before dispatching to mode implementations.
1264
+ // Worktree mode / in-worktree override must run from the live worktree so
1265
+ // mergeMilestoneToMain can find worktree-local state; branch mode runs from
1266
+ // the original project root. Best-effort for synthetic test paths.
1267
+ const targetCwd = mode === "worktree" || inWorktree
1268
+ ? worktreeBasePath
1269
+ : originalBasePath;
1270
+ if (targetCwd) {
1271
+ try {
1272
+ process.chdir(targetCwd);
1273
+ } catch (err) {
1274
+ debugLog("WorktreeLifecycle", {
1275
+ action: "mergeAndExit",
1276
+ phase: "pre-merge-chdir-failed",
1277
+ milestoneId,
1278
+ targetCwd,
1279
+ error: err instanceof Error ? err.message : String(err),
1280
+ });
1281
+ }
1282
+ }
1283
+
1284
+ if (mode === "worktree" || inWorktree) {
1285
+ return _mergeWorktreeModeImpl(deps, mctx);
1286
+ }
1287
+ if (mode === "branch") {
1288
+ return _mergeBranchModeImpl(deps, mctx);
1289
+ }
1290
+ // Defensive fallback — should not reach here given the mode-none guard above.
1291
+ return {
1292
+ merged: false,
1293
+ mode: "skipped",
1294
+ codeFilesChanged: false,
1295
+ pushed: false,
1296
+ };
1297
+ }
1298
+
1299
+ // ─── Module class ────────────────────────────────────────────────────────
1300
+
1301
+ /**
1302
+ * Worktree Lifecycle module instance.
1303
+ *
1304
+ * Constructed once per auto-mode session. Holds the session reference so
1305
+ * verbs can mutate `s.basePath` and related coordination state directly
1306
+ * without round-tripping through callers.
1307
+ */
1308
+ export class WorktreeLifecycle {
1309
+ private readonly s: AutoSession;
1310
+ private readonly deps: WorktreeLifecycleDeps;
1311
+
1312
+ constructor(s: AutoSession, deps: WorktreeLifecycleDeps) {
1313
+ this.s = s;
1314
+ this.deps = deps;
1315
+ }
1316
+
1317
+ /**
1318
+ * Enter or create the auto-worktree for `milestoneId`. Idempotent if
1319
+ * already in this milestone (lease refreshed; basePath unchanged).
1320
+ *
1321
+ * Returns a typed `EnterResult` describing the outcome. Callers may
1322
+ * ignore the result if they read `s.basePath` directly afterwards
1323
+ * (legacy behaviour); new callers should branch on the result.
1324
+ */
1325
+ enterMilestone(milestoneId: string, ctx: NotifyCtx): EnterResult {
1326
+ return _enterMilestoneCore(this.s, this.deps, milestoneId, ctx);
1327
+ }
1328
+
1329
+ /**
1330
+ * Exit the current worktree. With `opts.merge === true`, runs the full
1331
+ * merge-and-teardown path (worktree-mode or branch-mode auto-detected).
1332
+ * With `opts.merge === false`, runs auto-commit and teardown without
1333
+ * merging to main.
1334
+ *
1335
+ * Returns a typed `ExitResult`. `MergeConflictError` is surfaced as
1336
+ * `{ ok: false, reason: "merge-conflict", cause }` instead of thrown,
1337
+ * giving callers a typed branch for the expected failure path.
1338
+ * Unexpected failures (filesystem, git permissions, etc.) are wrapped
1339
+ * as `{ ok: false, reason: "teardown-failed", cause }` so callers always
1340
+ * receive a discriminated union — no exceptions for any expected outcome.
1341
+ */
1342
+ exitMilestone(
1343
+ milestoneId: string,
1344
+ opts: { merge: boolean; preserveBranch?: boolean },
1345
+ ctx: NotifyCtx,
1346
+ ): ExitResult {
1347
+ if (opts.merge) {
1348
+ try {
1349
+ const merged = this._mergeAndExit(milestoneId, ctx);
1350
+ return {
1351
+ ok: true,
1352
+ merged: merged.merged,
1353
+ codeFilesChanged: merged.codeFilesChanged,
1354
+ };
1355
+ } catch (err) {
1356
+ if (err instanceof MergeConflictError) {
1357
+ return { ok: false, reason: "merge-conflict", cause: err };
1358
+ }
1359
+ return { ok: false, reason: "teardown-failed", cause: err };
1360
+ }
1361
+ }
1362
+ try {
1363
+ this._exitWithoutMerge(milestoneId, ctx, {
1364
+ preserveBranch: opts.preserveBranch,
1365
+ });
1366
+ return { ok: true, merged: false, codeFilesChanged: false };
1367
+ } catch (err) {
1368
+ return { ok: false, reason: "teardown-failed", cause: err };
1369
+ }
1370
+ }
1371
+
1372
+ /**
1373
+ * Milestone transition: merge the current milestone, then enter the next
1374
+ * one. Pattern used when the loop detects that the active milestone has
1375
+ * changed (current completed, next is now active). Caller is responsible
1376
+ * for re-deriving state between the merge and the enter.
1377
+ */
1378
+ mergeAndEnterNext(
1379
+ currentMilestoneId: string,
1380
+ nextMilestoneId: string,
1381
+ ctx: NotifyCtx,
1382
+ ): void {
1383
+ debugLog("WorktreeLifecycle", {
1384
+ action: "mergeAndEnterNext",
1385
+ currentMilestoneId,
1386
+ nextMilestoneId,
1387
+ });
1388
+ let merged = false;
1389
+ let mergeThrew = false;
1390
+ try {
1391
+ merged = this._mergeAndExit(currentMilestoneId, ctx).merged;
1392
+ } catch (err) {
1393
+ if (err instanceof UserNotifiedError) throw err;
1394
+ mergeThrew = true;
1395
+ // _mergeAndExit emits a warning and restores state on failure during
1396
+ // merge/cleanup. If it throws before recovery runs (e.g. validation,
1397
+ // emitJournalEvent), basePath isn't restored — re-throw so we don't
1398
+ // enter the next milestone with the current one unmerged.
1399
+ const projectRoot = resolveWorktreeProjectRoot(
1400
+ this.s.basePath,
1401
+ this.s.originalBasePath,
1402
+ );
1403
+ if (this.s.basePath !== projectRoot) throw err;
1404
+ // Otherwise: merge attempted, failed cleanly with state restored.
1405
+ // The loop intentionally continues to the next milestone — the
1406
+ // failed milestone's branch is preserved for manual recovery.
630
1407
  }
631
1408
  if (!merged && !mergeThrew && !this.s.isolationDegraded) {
632
1409
  // _mergeAndExit returned without attempting a merge (no roadmap
@@ -654,7 +1431,7 @@ export class WorktreeLifecycle {
654
1431
  opts: { preserveBranch?: boolean },
655
1432
  ): void {
656
1433
  validateMilestoneId(milestoneId);
657
- if (!this.deps.isInAutoWorktree(this.s.basePath)) {
1434
+ if (!lifecycleIsInAutoWorktree(this.deps, this.s.basePath)) {
658
1435
  debugLog("WorktreeLifecycle", {
659
1436
  action: "exitMilestone",
660
1437
  milestoneId,
@@ -671,7 +1448,7 @@ export class WorktreeLifecycle {
671
1448
  });
672
1449
 
673
1450
  try {
674
- this.deps.autoCommitCurrentBranch(this.s.basePath, "stop", milestoneId);
1451
+ autoCommitLifecycleBranch(this.deps, this.s.basePath, "stop", milestoneId);
675
1452
  } catch (err) {
676
1453
  debugLog("WorktreeLifecycle", {
677
1454
  action: "exitMilestone",
@@ -680,7 +1457,7 @@ export class WorktreeLifecycle {
680
1457
  error: err instanceof Error ? err.message : String(err),
681
1458
  });
682
1459
  ctx.notify(
683
- `Auto-commit before exiting ${milestoneId} failed: ${err instanceof Error ? err.message : String(err)}. Branch ${this.deps.autoWorktreeBranch(milestoneId)} is preserved for recovery.`,
1460
+ `Auto-commit before exiting ${milestoneId} failed: ${err instanceof Error ? err.message : String(err)}. Branch ${lifecycleAutoWorktreeBranch(this.deps, milestoneId)} is preserved for recovery.`,
684
1461
  "warning",
685
1462
  );
686
1463
  }
@@ -697,7 +1474,7 @@ export class WorktreeLifecycle {
697
1474
  error: err instanceof Error ? err.message : String(err),
698
1475
  });
699
1476
  ctx.notify(
700
- `Could not leave milestone worktree before cleanup: ${err instanceof Error ? err.message : String(err)}. Branch ${this.deps.autoWorktreeBranch(milestoneId)} is preserved for recovery.`,
1477
+ `Could not leave milestone worktree before cleanup: ${err instanceof Error ? err.message : String(err)}. Branch ${lifecycleAutoWorktreeBranch(this.deps, milestoneId)} is preserved for recovery.`,
701
1478
  "warning",
702
1479
  );
703
1480
  }
@@ -705,7 +1482,7 @@ export class WorktreeLifecycle {
705
1482
 
706
1483
  let teardownFailed = false;
707
1484
  try {
708
- this.deps.teardownAutoWorktree(this.s.originalBasePath, milestoneId, {
1485
+ lifecycleTeardownAutoWorktree(this.deps, this.s.originalBasePath, milestoneId, {
709
1486
  preserveBranch: opts.preserveBranch ?? false,
710
1487
  });
711
1488
  } catch (err) {
@@ -717,7 +1494,7 @@ export class WorktreeLifecycle {
717
1494
  error: err instanceof Error ? err.message : String(err),
718
1495
  });
719
1496
  ctx.notify(
720
- `Worktree cleanup failed for ${milestoneId}: ${err instanceof Error ? err.message : String(err)}. Branch ${this.deps.autoWorktreeBranch(milestoneId)} is preserved for recovery.`,
1497
+ `Worktree cleanup failed for ${milestoneId}: ${err instanceof Error ? err.message : String(err)}. Branch ${lifecycleAutoWorktreeBranch(this.deps, milestoneId)} is preserved for recovery.`,
721
1498
  "warning",
722
1499
  );
723
1500
  }
@@ -742,103 +1519,60 @@ export class WorktreeLifecycle {
742
1519
  /**
743
1520
  * Merge the completed milestone branch back to main and exit the worktree.
744
1521
  *
745
- * - **worktree mode**: reads the roadmap, runs squash merge, projects
746
- * final state back via Projection.finalizeProjectionForMerge, tears
747
- * down the worktree, restores `s.basePath`. Falls back to bare
748
- * teardown (preserving the branch) if no roadmap exists.
749
- * - **branch mode**: validates HEAD is on the milestone branch (recovers
750
- * via checkout if not), merges, rebuilds GitService.
751
- * - **none**: no-op unless physically inside an auto-worktree (#2625).
1522
+ * Session-bound wrapper around `mergeMilestoneStandalone`. Builds a
1523
+ * `MergeContext` from `this.s`, layers session-side bookkeeping on top of
1524
+ * the result:
1525
+ *
1526
+ * - resquash-on-merge using `s.milestoneStartShas`
1527
+ * - merge-completion telemetry (duration)
1528
+ * - mode-specific session restore: worktree-mode `restoreToProjectRoot`,
1529
+ * branch-mode → `gitService` rebuild
752
1530
  *
753
- * Returns true when an actual squash-merge ran. Throws MergeConflictError
754
- * (and other non-recoverable errors) for callers to handle.
1531
+ * Returns the session-less merge result. Errors propagate after
1532
+ * `restoreToProjectRoot()` runs so callers always receive a consistent
1533
+ * session.
755
1534
  */
756
- private _mergeAndExit(milestoneId: string, ctx: NotifyCtx): boolean {
757
- validateMilestoneId(milestoneId);
758
-
759
- // Anchor cwd at the project root before any merge work. Some merge
760
- // paths (mergeMilestoneToMain, slice-cadence) chdir explicitly; others
761
- // (branch-mode, isolation-degraded skip) do not. If the worktree dir
762
- // is later torn down while cwd still points into it, every subsequent
763
- // process.cwd() throws ENOENT — which after de73fb43d surfaces as a
764
- // session-failed cancel and (in headless mode) terminates the whole
765
- // gsd process. Best-effort: silent on failure so synthetic test paths
766
- // still pass.
767
- if (this.s.originalBasePath) {
768
- try {
769
- process.chdir(this.s.originalBasePath);
770
- } catch (err) {
771
- debugLog("WorktreeLifecycle", {
772
- action: "mergeAndExit",
773
- phase: "pre-merge-chdir-failed",
774
- milestoneId,
775
- originalBasePath: this.s.originalBasePath,
776
- error: err instanceof Error ? err.message : String(err),
777
- });
778
- }
779
- }
780
-
1535
+ private _mergeAndExit(
1536
+ milestoneId: string,
1537
+ ctx: NotifyCtx,
1538
+ ): MergeStandaloneResult {
781
1539
  // #4764 — telemetry: record start timestamp so we can emit merge duration.
782
1540
  const mergeStartedAt = new Date().toISOString();
783
1541
  const mergeStartMs = Date.now();
784
1542
 
785
- if (this.s.isolationDegraded) {
786
- debugLog("WorktreeLifecycle", {
787
- action: "mergeAndExit",
788
- milestoneId,
789
- skipped: true,
790
- reason: "isolation-degraded",
791
- });
792
- ctx.notify(
793
- `Skipping worktree merge for ${milestoneId} — isolation was degraded (worktree creation failed earlier). Work is on the current branch.`,
794
- "info",
795
- );
796
- return false;
797
- }
798
-
799
- const mode = this.deps.getIsolationMode(
800
- this.s.originalBasePath || this.s.basePath,
801
- );
802
- debugLog("WorktreeLifecycle", {
803
- action: "mergeAndExit",
804
- milestoneId,
805
- mode,
806
- basePath: this.s.basePath,
807
- });
808
- emitJournalEvent(this.s.originalBasePath || this.s.basePath, {
809
- ts: new Date().toISOString(),
810
- flowId: randomUUID(),
811
- seq: 0,
812
- eventType: "worktree-merge-start",
813
- data: { milestoneId, mode },
814
- });
815
-
816
- // #2625: If we are physically inside an auto-worktree, we MUST merge
817
- // regardless of the current isolation config. This prevents data loss
818
- // when the default isolation mode changes between versions.
819
- const inWorktree =
820
- this.deps.isInAutoWorktree(this.s.basePath) && this.s.originalBasePath;
821
-
822
- if (mode === "none" && !inWorktree) {
823
- debugLog("WorktreeLifecycle", {
824
- action: "mergeAndExit",
1543
+ let result: MergeStandaloneResult;
1544
+ try {
1545
+ result = mergeMilestoneStandalone(this.deps, {
1546
+ originalBasePath: this.s.originalBasePath,
1547
+ worktreeBasePath: this.s.basePath,
825
1548
  milestoneId,
826
- skipped: true,
827
- reason: "mode-none",
1549
+ isolationDegraded: this.s.isolationDegraded,
1550
+ notify: ctx.notify,
828
1551
  });
829
- return false;
830
- }
831
-
832
- let actuallyMerged = false;
833
- if (mode === "worktree" || inWorktree) {
834
- actuallyMerged = this._mergeWorktreeMode(milestoneId, ctx);
835
- } else if (mode === "branch") {
836
- actuallyMerged = this._mergeBranchMode(milestoneId, ctx);
1552
+ } catch (err) {
1553
+ // Standalone has already done its session-less cleanup
1554
+ // (chdir, SQUASH_MSG cleanup, journal event). Layer session-side
1555
+ // restore on top so callers get a consistent session.
1556
+ this.restoreToProjectRoot();
1557
+ throw err;
837
1558
  }
838
1559
 
839
- if (!actuallyMerged) {
1560
+ if (!result.merged) {
1561
+ // Skip / no-roadmap / mode-none paths. milestoneStartShas housekeeping
1562
+ // is unconditional; mode-specific session restore happens for
1563
+ // worktree-mode (preserve-branch path tore down the worktree, so
1564
+ // basePath must restore) and not for branch-mode (no basePath change).
840
1565
  this.s.milestoneStartShas.delete(milestoneId);
841
- return false;
1566
+ if (result.mode === "worktree") {
1567
+ this.restoreToProjectRoot();
1568
+ debugLog("WorktreeLifecycle", {
1569
+ action: "mergeAndExit",
1570
+ milestoneId,
1571
+ result: "done",
1572
+ basePath: this.s.basePath,
1573
+ });
1574
+ }
1575
+ return result;
842
1576
  }
843
1577
 
844
1578
  // #4765 — when collapse_cadence=slice AND milestone_resquash=true, the
@@ -848,19 +1582,20 @@ export class WorktreeLifecycle {
848
1582
  try {
849
1583
  const startSha = this.s.milestoneStartShas.get(milestoneId);
850
1584
  if (startSha) {
851
- const prefs = loadEffectiveGSDPreferences(
1585
+ const prefs = lifecycleLoadPreferences(
1586
+ this.deps,
852
1587
  this.s.originalBasePath || this.s.basePath,
853
1588
  )?.preferences;
854
1589
  if (
855
1590
  getCollapseCadence(prefs) === "slice" &&
856
1591
  getMilestoneResquash(prefs)
857
1592
  ) {
858
- const result = resquashMilestoneOnMain(
1593
+ const resquashResult = resquashMilestoneOnMain(
859
1594
  this.s.originalBasePath || this.s.basePath,
860
1595
  milestoneId,
861
1596
  startSha,
862
1597
  );
863
- if (result.resquashed) {
1598
+ if (resquashResult.resquashed) {
864
1599
  ctx.notify(
865
1600
  `slice-cadence: re-squashed slice commits for ${milestoneId} into a single milestone commit.`,
866
1601
  "info",
@@ -900,291 +1635,29 @@ export class WorktreeLifecycle {
900
1635
  : String(telemetryErr),
901
1636
  });
902
1637
  }
903
- return true;
904
- }
905
-
906
- /** Worktree-mode merge body. Returns true when an actual squash-merge ran. */
907
- private _mergeWorktreeMode(milestoneId: string, ctx: NotifyCtx): boolean {
908
- const originalBase = this.s.originalBasePath;
909
- if (!originalBase) {
910
- debugLog("WorktreeLifecycle", {
911
- action: "mergeAndExit",
912
- milestoneId,
913
- mode: "worktree",
914
- skipped: true,
915
- reason: "missing-original-base",
916
- });
917
- return false;
918
- }
919
-
920
- let merged = false;
921
- try {
922
- // ADR-016: final projection before teardown. Replaces the legacy
923
- // syncWorktreeStateBack(originalBase, basePath, milestoneId) call.
924
- const finalScope = scopeMilestone(
925
- createWorkspace(this.s.basePath),
926
- milestoneId,
927
- );
928
- const { synced } =
929
- this.deps.worktreeProjection.finalizeProjectionForMerge(finalScope);
930
- if (synced.length > 0) {
931
- debugLog("WorktreeLifecycle", {
932
- action: "mergeAndExit",
933
- milestoneId,
934
- phase: "reverse-sync",
935
- synced: synced.length,
936
- });
937
- }
938
-
939
- // Resolve roadmap — try project root first, then worktree path as
940
- // fallback. The worktree may hold the only copy when state-back
941
- // projection silently dropped it or .gsd/ is not symlinked. Without
942
- // the fallback, a missing roadmap triggers bare teardown which
943
- // deletes the branch and orphans all milestone commits (#1573).
944
- let roadmapPath = this.deps.resolveMilestoneFile(
945
- originalBase,
946
- milestoneId,
947
- "ROADMAP",
948
- );
949
- if (
950
- !roadmapPath &&
951
- !isSamePathPhysical(this.s.basePath, originalBase)
952
- ) {
953
- roadmapPath = this.deps.resolveMilestoneFile(
954
- this.s.basePath,
955
- milestoneId,
956
- "ROADMAP",
957
- );
958
- if (roadmapPath) {
959
- debugLog("WorktreeLifecycle", {
960
- action: "mergeAndExit",
961
- milestoneId,
962
- phase: "roadmap-fallback",
963
- note: "resolved from worktree path",
964
- });
965
- }
966
- }
967
-
968
- if (roadmapPath) {
969
- const roadmapContent = this.deps.readFileSync(roadmapPath, "utf-8");
970
- const mergeResult = this.deps.mergeMilestoneToMain(
971
- originalBase,
972
- milestoneId,
973
- roadmapContent,
974
- );
975
- merged = true;
976
-
977
- // #2945 Bug 3: mergeMilestoneToMain performs best-effort worktree
978
- // cleanup internally (step 12), but it can silently fail on Windows
979
- // or when the worktree directory is locked. Perform a secondary
980
- // teardown here to ensure the worktree is properly cleaned up.
981
- // Idempotent — if already removed, teardownAutoWorktree no-ops.
982
- try {
983
- this.deps.teardownAutoWorktree(originalBase, milestoneId);
984
- } catch {
985
- // Best-effort — primary cleanup in mergeMilestoneToMain may have
986
- // already removed the worktree.
987
- }
988
1638
 
989
- if (mergeResult.codeFilesChanged) {
990
- ctx.notify(
991
- `Milestone ${milestoneId} merged to main.${mergeResult.pushed ? " Pushed to remote." : ""}`,
992
- "info",
993
- );
994
- } else {
995
- // #1906 — milestone produced only .gsd/ metadata. Surface
996
- // clearly so the user knows the milestone is not truly complete.
997
- ctx.notify(
998
- `WARNING: Milestone ${milestoneId} merged to main but contained NO code changes — only .gsd/ metadata files. ` +
999
- `The milestone summary may describe planned work that was never implemented. ` +
1000
- `Review the milestone output and re-run if code is missing.`,
1001
- "warning",
1002
- );
1003
- }
1004
- } else {
1005
- // No roadmap at either location — teardown but PRESERVE the branch
1006
- // so commits are not orphaned (#1573).
1007
- this.deps.teardownAutoWorktree(originalBase, milestoneId, {
1008
- preserveBranch: true,
1009
- });
1010
- ctx.notify(
1011
- `Exited worktree for ${milestoneId} (no roadmap found — branch preserved for manual merge).`,
1012
- "warning",
1013
- );
1014
- }
1015
- } catch (err) {
1016
- const msg = err instanceof Error ? err.message : String(err);
1639
+ // Mode-specific session restore.
1640
+ if (result.mode === "worktree") {
1641
+ this.restoreToProjectRoot();
1017
1642
  debugLog("WorktreeLifecycle", {
1018
1643
  action: "mergeAndExit",
1019
1644
  milestoneId,
1020
- result: "error",
1021
- error: msg,
1022
- fallback: "chdir-to-project-root",
1023
- });
1024
- emitJournalEvent(this.s.originalBasePath || this.s.basePath, {
1025
- ts: new Date().toISOString(),
1026
- flowId: randomUUID(),
1027
- seq: 0,
1028
- eventType: "worktree-merge-failed",
1029
- data: { milestoneId, error: msg },
1645
+ result: "done",
1646
+ basePath: this.s.basePath,
1030
1647
  });
1031
- // Surface a clear, actionable error. Worktree and milestone branch
1032
- // are intentionally preserved — nothing has been deleted. User can
1033
- // retry /gsd dispatch complete-milestone or merge manually once the
1034
- // underlying issue is fixed (#1668, #1891).
1035
- ctx.notify(
1036
- `Milestone merge failed: ${msg}. Your worktree and milestone branch are preserved — retry with \`/gsd dispatch complete-milestone\` or merge manually.`,
1037
- "warning",
1038
- );
1039
-
1040
- // Clean up stale merge state left by failed squash-merge (#1389)
1041
- try {
1042
- const gitDir = join(originalBase || this.s.basePath, ".git");
1043
- for (const f of ["SQUASH_MSG", "MERGE_HEAD", "MERGE_MSG"]) {
1044
- const p = join(gitDir, f);
1045
- if (existsSync(p)) unlinkSync(p);
1046
- }
1047
- } catch {
1048
- /* best-effort */
1049
- }
1050
-
1051
- // Error recovery: always restore to project root
1052
- if (originalBase) {
1053
- try {
1054
- process.chdir(originalBase);
1055
- } catch {
1056
- /* best-effort */
1057
- }
1058
- }
1059
-
1060
- // Restore state before re-throwing so callers always get a
1061
- // consistent session (#4380).
1062
- this.restoreToProjectRoot();
1063
- // Re-throw: MergeConflictError stops the auto loop (#2330);
1064
- // non-conflict errors must also propagate so broken states are
1065
- // diagnosable (#4380).
1066
- throw err;
1067
- }
1068
-
1069
- // Always restore basePath and rebuild — whether merge succeeded or failed
1070
- this.restoreToProjectRoot();
1071
- debugLog("WorktreeLifecycle", {
1072
- action: "mergeAndExit",
1073
- milestoneId,
1074
- result: "done",
1075
- basePath: this.s.basePath,
1076
- });
1077
- return merged;
1078
- }
1079
-
1080
- /** Branch-mode merge body. Returns true when a merge actually ran. */
1081
- private _mergeBranchMode(milestoneId: string, ctx: NotifyCtx): boolean {
1082
- try {
1083
- const currentBranch = this.deps.getCurrentBranch(this.s.basePath);
1084
- const milestoneBranch = this.deps.autoWorktreeBranch(milestoneId);
1085
-
1086
- if (currentBranch !== milestoneBranch) {
1087
- // #5538-followup: previous behaviour was to silently `return false`
1088
- // when HEAD wasn't on the milestone branch — that let the loop
1089
- // advance with the milestone's commits stranded on the branch.
1090
- // Attempt recovery by force-checking-out the milestone branch; if
1091
- // that fails, throw so the caller pauses auto-mode and the user
1092
- // sees the failure instead of a silent merge skip.
1093
- debugLog("WorktreeLifecycle", {
1094
- action: "mergeAndExit",
1095
- milestoneId,
1096
- mode: "branch",
1097
- recovery: "checkout-milestone-branch",
1098
- currentBranch,
1099
- milestoneBranch,
1100
- });
1101
- try {
1102
- this.deps.checkoutBranch(this.s.basePath, milestoneBranch);
1103
- } catch (checkoutErr) {
1104
- const checkoutMsg =
1105
- checkoutErr instanceof Error
1106
- ? checkoutErr.message
1107
- : String(checkoutErr);
1108
- ctx.notify(
1109
- `Cannot merge milestone ${milestoneId}: working tree is on ${currentBranch} and checkout to ${milestoneBranch} failed (${checkoutMsg}). Resolve manually and run /gsd auto to resume.`,
1110
- "error",
1111
- );
1112
- throw new UserNotifiedError(checkoutMsg, checkoutErr);
1113
- }
1114
-
1115
- const reverify = this.deps.getCurrentBranch(this.s.basePath);
1116
- if (reverify !== milestoneBranch) {
1117
- const reverifyMsg = `branch checkout to ${milestoneBranch} reported success but current branch is ${reverify}`;
1118
- ctx.notify(
1119
- `Cannot merge milestone ${milestoneId}: ${reverifyMsg}. Resolve manually and run /gsd auto to resume.`,
1120
- "error",
1121
- );
1122
- throw new UserNotifiedError(reverifyMsg);
1123
- }
1124
- }
1125
-
1126
- const roadmapPath = this.deps.resolveMilestoneFile(
1127
- this.s.basePath,
1128
- milestoneId,
1129
- "ROADMAP",
1130
- );
1131
- if (!roadmapPath) {
1132
- debugLog("WorktreeLifecycle", {
1133
- action: "mergeAndExit",
1134
- milestoneId,
1135
- mode: "branch",
1136
- skipped: true,
1137
- reason: "no-roadmap",
1138
- });
1139
- return false;
1140
- }
1141
-
1142
- const roadmapContent = this.deps.readFileSync(roadmapPath, "utf-8");
1143
- const mergeResult = this.deps.mergeMilestoneToMain(
1144
- this.s.basePath,
1145
- milestoneId,
1146
- roadmapContent,
1147
- );
1148
-
1648
+ } else if (result.mode === "branch") {
1149
1649
  // Rebuild GitService after merge (branch HEAD changed)
1150
1650
  rebuildGitService(this.s, this.deps);
1151
-
1152
- if (mergeResult.codeFilesChanged) {
1153
- ctx.notify(
1154
- `Milestone ${milestoneId} merged (branch mode).${mergeResult.pushed ? " Pushed to remote." : ""}`,
1155
- "info",
1156
- );
1157
- } else {
1158
- ctx.notify(
1159
- `WARNING: Milestone ${milestoneId} merged (branch mode) but contained NO code changes — only .gsd/ metadata. ` +
1160
- `Review the milestone output and re-run if code is missing.`,
1161
- "warning",
1162
- );
1163
- }
1164
- debugLog("WorktreeLifecycle", {
1165
- action: "mergeAndExit",
1166
- milestoneId,
1167
- mode: "branch",
1168
- result: "success",
1169
- });
1170
- return true;
1171
- } catch (err) {
1172
- const msg = err instanceof Error ? err.message : String(err);
1173
- debugLog("WorktreeLifecycle", {
1174
- action: "mergeAndExit",
1175
- milestoneId,
1176
- mode: "branch",
1177
- result: "error",
1178
- error: msg,
1179
- });
1180
- if (!(err instanceof UserNotifiedError)) {
1181
- ctx.notify(`Milestone merge failed (branch mode): ${msg}`, "warning");
1182
- }
1183
- // Re-throw all errors so callers can apply their own recovery (#4380).
1184
- throw err;
1185
1651
  }
1652
+ return result;
1186
1653
  }
1187
1654
 
1655
+ // ── Removed: _mergeWorktreeMode / _mergeBranchMode bodies ────────────
1656
+ // The merge bodies moved to file-scope `_mergeWorktreeModeImpl` and
1657
+ // `_mergeBranchModeImpl`, callable from the session-less
1658
+ // `mergeMilestoneStandalone` entry. The previous private methods are
1659
+ // gone; `_mergeAndExit` above is the only session-bound caller.
1660
+
1188
1661
  /**
1189
1662
  * Fall back to branch-mode for `milestoneId` after a failed worktree
1190
1663
  * creation, marking the session's isolation as degraded.
@@ -1210,9 +1683,9 @@ export class WorktreeLifecycle {
1210
1683
  this.s.originalBasePath,
1211
1684
  );
1212
1685
  try {
1213
- this.deps.enterBranchModeForMilestone(basePath, milestoneId);
1686
+ lifecycleEnterBranchMode(this.deps, basePath, milestoneId);
1214
1687
  rebuildGitService(this.s, this.deps);
1215
- this.deps.invalidateAllCaches();
1688
+ invalidateAllCaches();
1216
1689
  this.s.isolationDegraded = true;
1217
1690
  ctx.notify(
1218
1691
  `Switched to branch milestone/${milestoneId} (isolation degraded).`,
@@ -1240,7 +1713,161 @@ export class WorktreeLifecycle {
1240
1713
  if (!this.s.originalBasePath) return;
1241
1714
  this.s.basePath = this.s.originalBasePath;
1242
1715
  rebuildGitService(this.s, this.deps);
1243
- this.deps.invalidateAllCaches();
1716
+ invalidateAllCaches();
1717
+ }
1718
+
1719
+ /**
1720
+ * Adopt a session root (ADR-016 phase 2 / B2, issue #5620).
1721
+ *
1722
+ * Sole owner of `s.basePath` mutation for bootstrap-class transitions:
1723
+ * initial session start, paused-resume entry (before persisted-state
1724
+ * consultation), and hook-trigger session activation. Defensive about
1725
+ * `s.originalBasePath`:
1726
+ *
1727
+ * - When `originalBase` is explicit: overwrite.
1728
+ * - Otherwise, set `s.originalBasePath` only if it is currently empty —
1729
+ * resume paths that already restored `s.originalBasePath` from paused
1730
+ * metadata keep their value.
1731
+ *
1732
+ * Does NOT chdir; callers that need cwd alignment with the new basePath
1733
+ * are responsible for it. Does NOT rebuild `s.gitService` — callers that
1734
+ * mutate `s.basePath` to a non-project-root path (e.g. a worktree on a
1735
+ * subsequent milestone enter) go through `enterMilestone`, which handles
1736
+ * the rebuild.
1737
+ */
1738
+ adoptSessionRoot(base: string, originalBase?: string): void {
1739
+ this.s.basePath = base;
1740
+ if (originalBase !== undefined) {
1741
+ this.s.originalBasePath = originalBase;
1742
+ } else if (!this.s.originalBasePath) {
1743
+ this.s.originalBasePath = base;
1744
+ }
1745
+ }
1746
+
1747
+ /**
1748
+ * Resume from a paused session (ADR-016 phase 2 / B3, issue #5621).
1749
+ *
1750
+ * Adopts `persistedWorktreePath` as `s.basePath` when the path is
1751
+ * non-null and exists on disk; otherwise falls back to `base`. Mirrors
1752
+ * the resume guard at `auto.ts:2164` — a stale or removed worktree
1753
+ * directory must not strand the resumed session in an invalid root.
1754
+ *
1755
+ * Folds in the body of the legacy `_resolvePausedResumeBasePathForTest`
1756
+ * helper (see `resolvePausedResumeBasePath` below). After this verb
1757
+ * lands the helper is deleted from `auto.ts` per the slice-7 closure
1758
+ * decision to retire `_*ForTest` suffixes from production paths.
1759
+ *
1760
+ * Like `adoptSessionRoot`, this is a pure session-state mutation — no
1761
+ * chdir, no git service rebuild, no cache invalidation.
1762
+ */
1763
+ resumeFromPausedSession(
1764
+ base: string,
1765
+ persistedWorktreePath: string | null,
1766
+ ): void {
1767
+ this.s.basePath = resolvePausedResumeBasePath(base, persistedWorktreePath);
1768
+ }
1769
+
1770
+ /**
1771
+ * Adopt an orphan worktree for a bootstrap-time merge (ADR-016 phase 2 / B4,
1772
+ * issue #5622).
1773
+ *
1774
+ * Owns the swap-run-revert protocol that bootstrap previously open-coded:
1775
+ *
1776
+ * 1. Snapshot prior `s.basePath` and `s.originalBasePath`.
1777
+ * 2. Resolve `getAutoWorktreePath(base, milestoneId) ?? base` before
1778
+ * mutating session state, then set `s.originalBasePath = base` and
1779
+ * `s.basePath` to the resolved path.
1780
+ * 3. Invoke the caller-supplied `run` callback under the swap.
1781
+ * 4. On `!result.merged`: revert to `base` and `chdir(base)` so the
1782
+ * caller can return early without leaving the session in a half-
1783
+ * swapped state.
1784
+ * 5. On `result.merged && !s.active`: revert to the snapshotted prior
1785
+ * paths (the orphan merge succeeded but bootstrap chose not to keep
1786
+ * the session active).
1787
+ * 6. On `result.merged && s.active`: leave the swap in place — the
1788
+ * loop will continue from the worktree path.
1789
+ *
1790
+ * The callback shape forces every caller through the same revert
1791
+ * protocol; an open-coded swap that forgets to revert on failure was the
1792
+ * original bug pattern this verb is designed to prevent.
1793
+ */
1794
+ adoptOrphanWorktree<T extends { merged: boolean }>(
1795
+ milestoneId: string,
1796
+ base: string,
1797
+ run: () => T,
1798
+ ): T {
1799
+ validateMilestoneId(milestoneId);
1800
+
1801
+ const priorBasePath = this.s.basePath;
1802
+ const priorOriginalBasePath = this.s.originalBasePath;
1803
+ const restorePriorPaths = (phase: string): void => {
1804
+ this.s.basePath = priorBasePath || base;
1805
+ this.s.originalBasePath = priorOriginalBasePath || base;
1806
+ try {
1807
+ process.chdir(this.s.originalBasePath || base);
1808
+ } catch (err) {
1809
+ debugLog("WorktreeLifecycle", {
1810
+ action: "adoptOrphanWorktree",
1811
+ phase,
1812
+ base: this.s.originalBasePath || base,
1813
+ error: err instanceof Error ? err.message : String(err),
1814
+ });
1815
+ }
1816
+ };
1817
+
1818
+ let adoptedBasePath: string;
1819
+ try {
1820
+ const wtPathFn =
1821
+ primitiveOverrides(this.deps).getAutoWorktreePath ?? getAutoWorktreePath;
1822
+ adoptedBasePath = wtPathFn(base, milestoneId) ?? base;
1823
+ } catch (err) {
1824
+ restorePriorPaths("rollback-resolve-worktree-failed");
1825
+ throw err;
1826
+ }
1827
+
1828
+ // Swap into the orphan worktree.
1829
+ this.s.originalBasePath = base;
1830
+ this.s.basePath = adoptedBasePath;
1831
+
1832
+ let result: T;
1833
+ try {
1834
+ result = run();
1835
+ } catch (err) {
1836
+ restorePriorPaths("rollback-run-failed");
1837
+ throw err;
1838
+ }
1839
+
1840
+ if (!result.merged) {
1841
+ // Failed orphan merge — revert to project root so the caller can
1842
+ // safely return early without leaving the session in an invalid
1843
+ // basePath. Mirror the chdir that bootstrap performed inline.
1844
+ this.s.basePath = base;
1845
+ this.s.originalBasePath = base;
1846
+ try {
1847
+ process.chdir(base);
1848
+ } catch (err) {
1849
+ debugLog("WorktreeLifecycle", {
1850
+ action: "adoptOrphanWorktree",
1851
+ phase: "revert-chdir-failed",
1852
+ base,
1853
+ error: err instanceof Error ? err.message : String(err),
1854
+ });
1855
+ }
1856
+ return result;
1857
+ }
1858
+
1859
+ if (!this.s.active) {
1860
+ // Merge succeeded but the session was not (re)activated — restore
1861
+ // the snapshotted paths so the calling context resumes where it
1862
+ // was, with the orphan branch now merged on main.
1863
+ this.s.basePath = priorBasePath || base;
1864
+ this.s.originalBasePath = priorOriginalBasePath || base;
1865
+ }
1866
+ // else: merged && active — leave the swap; the loop continues from
1867
+ // the worktree path. Subsequent milestone enters mutate `s.basePath`
1868
+ // through their own Lifecycle verbs.
1869
+
1870
+ return result;
1244
1871
  }
1245
1872
 
1246
1873
  /** True if `milestoneId` is the session's currently-active milestone. */