gsd-pi 2.81.0 → 2.82.0
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.
- package/README.md +36 -24
- package/dist/resources/.managed-resources-content-hash +1 -1
- package/dist/resources/extensions/gsd/auto/loop.js +111 -8
- package/dist/resources/extensions/gsd/auto/phases.js +190 -97
- package/dist/resources/extensions/gsd/auto/run-unit.js +66 -3
- package/dist/resources/extensions/gsd/auto/session.js +9 -0
- package/dist/resources/extensions/gsd/auto/verification-retry-policy.js +43 -0
- package/dist/resources/extensions/gsd/auto-dashboard.js +182 -178
- package/dist/resources/extensions/gsd/auto-dispatch.js +14 -11
- package/dist/resources/extensions/gsd/auto-post-unit.js +7 -1
- package/dist/resources/extensions/gsd/auto-recovery.js +6 -181
- package/dist/resources/extensions/gsd/auto-runtime-state.js +5 -0
- package/dist/resources/extensions/gsd/auto-start.js +20 -23
- package/dist/resources/extensions/gsd/auto-unit-closeout.js +33 -5
- package/dist/resources/extensions/gsd/auto-verification.js +12 -6
- package/dist/resources/extensions/gsd/auto-worktree.js +8 -0
- package/dist/resources/extensions/gsd/auto.js +265 -76
- package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +13 -6
- package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +8 -2
- package/dist/resources/extensions/gsd/bootstrap/register-shortcuts.js +4 -8
- package/dist/resources/extensions/gsd/commands/handlers/notifications-handler.js +4 -10
- package/dist/resources/extensions/gsd/commands/handlers/parallel.js +9 -0
- package/dist/resources/extensions/gsd/git-service.js +2 -1
- package/dist/resources/extensions/gsd/gsd-db.js +7 -23
- package/dist/resources/extensions/gsd/health-widget-core.js +1 -1
- package/dist/resources/extensions/gsd/health-widget.js +4 -10
- package/dist/resources/extensions/gsd/markdown-renderer.js +0 -95
- package/dist/resources/extensions/gsd/native-git-bridge.js +14 -14
- package/dist/resources/extensions/gsd/notification-overlay.js +35 -40
- package/dist/resources/extensions/gsd/parallel-merge.js +53 -30
- package/dist/resources/extensions/gsd/parallel-monitor-overlay.js +25 -33
- package/dist/resources/extensions/gsd/prompts/complete-slice.md +14 -12
- package/dist/resources/extensions/gsd/prompts/discuss-headless.md +20 -2
- package/dist/resources/extensions/gsd/prompts/discuss.md +20 -2
- package/dist/resources/extensions/gsd/recovery-classification.js +15 -1
- package/dist/resources/extensions/gsd/session-lock.js +40 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/completion.js +131 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/merge-state.js +247 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/project-md.js +50 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/roadmap.js +87 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/sketch-flag.js +50 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/stale-render.js +124 -0
- package/dist/resources/extensions/gsd/state-reconciliation/drift/stale-worker.js +32 -0
- package/dist/resources/extensions/gsd/state-reconciliation/errors.js +41 -0
- package/dist/resources/extensions/gsd/state-reconciliation/index.js +99 -0
- package/dist/resources/extensions/gsd/state-reconciliation/registry.js +24 -0
- package/dist/resources/extensions/gsd/state-reconciliation/spawn-gate.js +43 -0
- package/dist/resources/extensions/gsd/state-reconciliation/types.js +3 -0
- package/dist/resources/extensions/gsd/state-reconciliation.js +5 -26
- package/dist/resources/extensions/gsd/tui/render-kit.js +74 -0
- package/dist/resources/extensions/gsd/watch/header-renderer.js +92 -69
- package/dist/resources/extensions/gsd/watch/splash-palette.js +10 -0
- package/dist/resources/extensions/gsd/workflow-mcp.js +2 -2
- package/dist/resources/extensions/gsd/worktree-lifecycle.js +722 -316
- package/dist/resources/extensions/gsd/worktree-telemetry.js +3 -1
- package/dist/tsconfig.extensions.tsbuildinfo +1 -1
- package/dist/web/standalone/.next/BUILD_ID +1 -1
- package/dist/web/standalone/.next/app-path-routes-manifest.json +9 -9
- package/dist/web/standalone/.next/build-manifest.json +2 -2
- package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.html +1 -1
- package/dist/web/standalone/.next/server/app/index.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
- package/dist/web/standalone/.next/server/app-paths-manifest.json +9 -9
- package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
- package/dist/web/standalone/.next/server/pages/404.html +1 -1
- package/dist/web/standalone/.next/server/pages/500.html +1 -1
- package/dist/welcome-screen.d.ts +0 -7
- package/dist/welcome-screen.js +60 -69
- package/package.json +1 -1
- package/packages/daemon/package.json +2 -2
- package/packages/mcp-server/package.json +2 -2
- package/packages/native/package.json +1 -1
- package/packages/pi-agent-core/package.json +1 -1
- package/packages/pi-ai/package.json +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.d.ts +2 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.d.ts.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.js +47 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/assistant-message-design.test.js.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-execution.test.js +76 -9
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/tool-execution.test.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.d.ts +2 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.d.ts.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.js +40 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/__tests__/user-message-design.test.js.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.d.ts +0 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.js +30 -29
- package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.test.js +10 -3
- package/packages/pi-coding-agent/dist/modes/interactive/components/adaptive-layout.test.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.js +13 -13
- package/packages/pi-coding-agent/dist/modes/interactive/components/assistant-message.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.d.ts +1 -3
- package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.js +58 -3
- package/packages/pi-coding-agent/dist/modes/interactive/components/bash-execution.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/diff.d.ts +2 -2
- package/packages/pi-coding-agent/dist/modes/interactive/components/diff.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/diff.js +12 -6
- package/packages/pi-coding-agent/dist/modes/interactive/components/diff.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/footer.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js +14 -41
- package/packages/pi-coding-agent/dist/modes/interactive/components/footer.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.d.ts +0 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.js +86 -82
- package/packages/pi-coding-agent/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.d.ts +35 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.d.ts.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.js +152 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/transcript-design.js.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.d.ts +16 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.d.ts.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.js +73 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/tui-style-kit.js.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.d.ts +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.js +12 -8
- package/packages/pi-coding-agent/dist/modes/interactive/components/user-message.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.d.ts +2 -0
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.d.ts.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.js +17 -0
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme-highlight.test.js.map +1 -0
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme.js +105 -1
- package/packages/pi-coding-agent/dist/modes/interactive/theme/theme.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/theme/themes.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/theme/themes.js +27 -26
- package/packages/pi-coding-agent/dist/modes/interactive/theme/themes.js.map +1 -1
- package/packages/pi-coding-agent/dist/modes/interactive/tui-mode.test.js +9 -6
- package/packages/pi-coding-agent/dist/modes/interactive/tui-mode.test.js.map +1 -1
- package/packages/pi-coding-agent/package.json +1 -1
- package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/assistant-message-design.test.ts +56 -0
- package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/tool-execution.test.ts +113 -9
- package/packages/pi-coding-agent/src/modes/interactive/components/__tests__/user-message-design.test.ts +48 -0
- package/packages/pi-coding-agent/src/modes/interactive/components/adaptive-layout.test.ts +10 -3
- package/packages/pi-coding-agent/src/modes/interactive/components/adaptive-layout.ts +43 -42
- package/packages/pi-coding-agent/src/modes/interactive/components/assistant-message.ts +14 -14
- package/packages/pi-coding-agent/src/modes/interactive/components/bash-execution.ts +64 -3
- package/packages/pi-coding-agent/src/modes/interactive/components/diff.ts +13 -7
- package/packages/pi-coding-agent/src/modes/interactive/components/footer.ts +15 -42
- package/packages/pi-coding-agent/src/modes/interactive/components/tool-execution.ts +84 -104
- package/packages/pi-coding-agent/src/modes/interactive/components/transcript-design.ts +196 -0
- package/packages/pi-coding-agent/src/modes/interactive/components/tui-style-kit.ts +94 -0
- package/packages/pi-coding-agent/src/modes/interactive/components/user-message.ts +14 -9
- package/packages/pi-coding-agent/src/modes/interactive/theme/theme-highlight.test.ts +23 -0
- package/packages/pi-coding-agent/src/modes/interactive/theme/theme.ts +106 -1
- package/packages/pi-coding-agent/src/modes/interactive/theme/themes.ts +27 -26
- package/packages/pi-coding-agent/src/modes/interactive/tui-mode.test.ts +9 -6
- package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
- package/packages/pi-tui/dist/__tests__/overlay-layout.test.js +14 -1
- package/packages/pi-tui/dist/__tests__/overlay-layout.test.js.map +1 -1
- package/packages/pi-tui/dist/overlay-layout.d.ts.map +1 -1
- package/packages/pi-tui/dist/overlay-layout.js +9 -6
- package/packages/pi-tui/dist/overlay-layout.js.map +1 -1
- package/packages/pi-tui/package.json +1 -1
- package/packages/pi-tui/src/__tests__/overlay-layout.test.ts +20 -1
- package/packages/pi-tui/src/overlay-layout.ts +10 -7
- package/packages/pi-tui/tsconfig.tsbuildinfo +1 -1
- package/packages/rpc-client/package.json +1 -1
- package/pkg/dist/modes/interactive/theme/theme-highlight.test.d.ts +2 -0
- package/pkg/dist/modes/interactive/theme/theme-highlight.test.d.ts.map +1 -0
- package/pkg/dist/modes/interactive/theme/theme-highlight.test.js +17 -0
- package/pkg/dist/modes/interactive/theme/theme-highlight.test.js.map +1 -0
- package/pkg/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/pkg/dist/modes/interactive/theme/theme.js +105 -1
- package/pkg/dist/modes/interactive/theme/theme.js.map +1 -1
- package/pkg/dist/modes/interactive/theme/themes.d.ts.map +1 -1
- package/pkg/dist/modes/interactive/theme/themes.js +27 -26
- package/pkg/dist/modes/interactive/theme/themes.js.map +1 -1
- package/pkg/package.json +1 -1
- package/src/resources/extensions/gsd/auto/loop-deps.ts +9 -5
- package/src/resources/extensions/gsd/auto/loop.ts +113 -9
- package/src/resources/extensions/gsd/auto/phases.ts +144 -19
- package/src/resources/extensions/gsd/auto/run-unit.ts +69 -4
- package/src/resources/extensions/gsd/auto/session.ts +10 -0
- package/src/resources/extensions/gsd/auto/verification-retry-policy.ts +82 -0
- package/src/resources/extensions/gsd/auto-dashboard.ts +230 -183
- package/src/resources/extensions/gsd/auto-dispatch.ts +15 -1
- package/src/resources/extensions/gsd/auto-post-unit.ts +7 -1
- package/src/resources/extensions/gsd/auto-recovery.ts +7 -209
- package/src/resources/extensions/gsd/auto-runtime-state.ts +5 -0
- package/src/resources/extensions/gsd/auto-start.ts +22 -22
- package/src/resources/extensions/gsd/auto-unit-closeout.ts +51 -0
- package/src/resources/extensions/gsd/auto-verification.ts +12 -6
- package/src/resources/extensions/gsd/auto-worktree.ts +8 -0
- package/src/resources/extensions/gsd/auto.ts +295 -75
- package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +21 -6
- package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +6 -2
- package/src/resources/extensions/gsd/bootstrap/register-shortcuts.ts +5 -8
- package/src/resources/extensions/gsd/commands/handlers/notifications-handler.ts +4 -10
- package/src/resources/extensions/gsd/commands/handlers/parallel.ts +12 -0
- package/src/resources/extensions/gsd/git-service.ts +2 -0
- package/src/resources/extensions/gsd/gsd-db.ts +7 -23
- package/src/resources/extensions/gsd/health-widget-core.ts +1 -1
- package/src/resources/extensions/gsd/health-widget.ts +6 -10
- package/src/resources/extensions/gsd/journal.ts +2 -0
- package/src/resources/extensions/gsd/markdown-renderer.ts +4 -95
- package/src/resources/extensions/gsd/native-git-bridge.ts +14 -13
- package/src/resources/extensions/gsd/notification-overlay.ts +50 -46
- package/src/resources/extensions/gsd/parallel-merge.ts +61 -34
- package/src/resources/extensions/gsd/parallel-monitor-overlay.ts +33 -35
- package/src/resources/extensions/gsd/prompts/complete-slice.md +14 -12
- package/src/resources/extensions/gsd/prompts/discuss-headless.md +20 -2
- package/src/resources/extensions/gsd/prompts/discuss.md +20 -2
- package/src/resources/extensions/gsd/recovery-classification.ts +18 -1
- package/src/resources/extensions/gsd/session-lock.ts +41 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/completion.ts +172 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/merge-state.ts +337 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/project-md.ts +69 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/roadmap.ts +109 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/sketch-flag.ts +68 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/stale-render.ts +185 -0
- package/src/resources/extensions/gsd/state-reconciliation/drift/stale-worker.ts +46 -0
- package/src/resources/extensions/gsd/state-reconciliation/errors.ts +67 -0
- package/src/resources/extensions/gsd/state-reconciliation/index.ts +142 -0
- package/src/resources/extensions/gsd/state-reconciliation/registry.ts +27 -0
- package/src/resources/extensions/gsd/state-reconciliation/spawn-gate.ts +60 -0
- package/src/resources/extensions/gsd/state-reconciliation/types.ts +83 -0
- package/src/resources/extensions/gsd/state-reconciliation.ts +21 -53
- package/src/resources/extensions/gsd/tests/artifact-retry-cap.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/auto-dashboard.test.ts +99 -0
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +654 -176
- package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +291 -4
- package/src/resources/extensions/gsd/tests/auto-runtime-state.test.ts +16 -1
- package/src/resources/extensions/gsd/tests/auto-start-orphan-bootstrap.test.ts +18 -0
- package/src/resources/extensions/gsd/tests/auto-unit-closeout.test.ts +68 -0
- package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +28 -1
- package/src/resources/extensions/gsd/tests/deep-project-auto-loop.test.ts +20 -2
- package/src/resources/extensions/gsd/tests/dispatch-complete-milestone-guard.test.ts +44 -0
- package/src/resources/extensions/gsd/tests/header-renderer.test.ts +40 -0
- package/src/resources/extensions/gsd/tests/headless-milestone-parity.test.ts +10 -0
- package/src/resources/extensions/gsd/tests/health-widget.test.ts +14 -4
- package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +26 -0
- package/src/resources/extensions/gsd/tests/integration/integration-proof.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/integration/parallel-merge.test.ts +116 -24
- package/src/resources/extensions/gsd/tests/journal-integration.test.ts +0 -1
- package/src/resources/extensions/gsd/tests/markdown-renderer.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/merge-conflict-stops-loop.test.ts +46 -11
- package/src/resources/extensions/gsd/tests/notification-overlay.test.ts +78 -41
- package/src/resources/extensions/gsd/tests/notifications-handler.test.ts +44 -0
- package/src/resources/extensions/gsd/tests/originalbase-path-comparison.test.ts +12 -217
- package/src/resources/extensions/gsd/tests/parallel-monitor-overlay.test.ts +38 -6
- package/src/resources/extensions/gsd/tests/post-exec-retry-bypass.test.ts +2 -2
- package/src/resources/extensions/gsd/tests/progressive-planning.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +24 -1
- package/src/resources/extensions/gsd/tests/resume-dispatch-worktree.test.ts +7 -3
- package/src/resources/extensions/gsd/tests/runtime-invariant-modules.test.ts +6 -3
- package/src/resources/extensions/gsd/tests/session-switch-abort-misclassification.test.ts +24 -0
- package/src/resources/extensions/gsd/tests/state-corruption-2945.test.ts +65 -58
- package/src/resources/extensions/gsd/tests/state-reconciliation-drift.test.ts +952 -0
- package/src/resources/extensions/gsd/tests/token-tool-gating.test.ts +4 -0
- package/src/resources/extensions/gsd/tests/tui-header-lifecycle.test.ts +121 -1
- package/src/resources/extensions/gsd/tests/tui-render-kit.test.ts +66 -0
- package/src/resources/extensions/gsd/tests/verification-retry-policy.test.ts +83 -0
- package/src/resources/extensions/gsd/tests/workflow-mcp.test.ts +6 -0
- package/src/resources/extensions/gsd/tests/worktree-journal-events.test.ts +158 -58
- package/src/resources/extensions/gsd/tests/worktree-lifecycle.test.ts +572 -118
- package/src/resources/extensions/gsd/tests/worktree-telemetry.test.ts +59 -2
- package/src/resources/extensions/gsd/tests/write-gate-planning-unit.test.ts +18 -0
- package/src/resources/extensions/gsd/tui/render-kit.ts +109 -0
- package/src/resources/extensions/gsd/watch/header-renderer.ts +121 -79
- package/src/resources/extensions/gsd/watch/splash-palette.ts +11 -0
- package/src/resources/extensions/gsd/workflow-mcp.ts +2 -2
- package/src/resources/extensions/gsd/worktree-lifecycle.ts +1151 -524
- package/src/resources/extensions/gsd/worktree-telemetry.ts +7 -2
- package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +0 -1544
- /package/dist/web/standalone/.next/static/{drLMkgfHQ8lzS229_HWYR → S44UQTFCUdA44dkjfYt6S}/_buildManifest.js +0 -0
- /package/dist/web/standalone/.next/static/{drLMkgfHQ8lzS229_HWYR → S44UQTFCUdA44dkjfYt6S}/_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
|
-
|
|
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
|
-
// ──
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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
|
-
// ──
|
|
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
|
-
*
|
|
109
|
-
*
|
|
110
|
-
*
|
|
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
|
-
|
|
113
|
-
resolveMilestoneFile: (
|
|
127
|
+
mergeMilestoneToMain: (
|
|
114
128
|
basePath: string,
|
|
115
129
|
milestoneId: string,
|
|
116
|
-
|
|
117
|
-
) =>
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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 =
|
|
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
|
|
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
|
-
|
|
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 =
|
|
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
|
|
720
|
+
wtPath = lifecycleEnterAutoWorktree(deps, basePath, milestoneId);
|
|
429
721
|
} else {
|
|
430
|
-
wtPath = deps
|
|
722
|
+
wtPath = lifecycleCreateAutoWorktree(deps, basePath, milestoneId);
|
|
431
723
|
}
|
|
432
724
|
|
|
433
725
|
s.basePath = wtPath;
|
|
434
726
|
rebuildGitService(s, deps);
|
|
435
|
-
|
|
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
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
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
|
-
// ───
|
|
839
|
+
// ─── Session-less merge entry (ADR-016 phase 2 / A1) ─────────────────────
|
|
527
840
|
|
|
528
841
|
/**
|
|
529
|
-
* Worktree
|
|
842
|
+
* Worktree-mode merge body. Session-less — operates on a `MergeContext`.
|
|
530
843
|
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
533
|
-
*
|
|
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
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
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
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
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
|
-
|
|
586
|
-
|
|
587
|
-
|
|
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
|
-
|
|
590
|
-
|
|
591
|
-
|
|
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
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
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
|
-
|
|
615
|
-
} catch
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
//
|
|
628
|
-
|
|
629
|
-
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
-
* -
|
|
746
|
-
*
|
|
747
|
-
*
|
|
748
|
-
*
|
|
749
|
-
* -
|
|
750
|
-
*
|
|
751
|
-
* -
|
|
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
|
|
754
|
-
* (
|
|
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(
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
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
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
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
|
-
|
|
827
|
-
|
|
1549
|
+
isolationDegraded: this.s.isolationDegraded,
|
|
1550
|
+
notify: ctx.notify,
|
|
828
1551
|
});
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
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 (!
|
|
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
|
-
|
|
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 =
|
|
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
|
|
1593
|
+
const resquashResult = resquashMilestoneOnMain(
|
|
859
1594
|
this.s.originalBasePath || this.s.basePath,
|
|
860
1595
|
milestoneId,
|
|
861
1596
|
startSha,
|
|
862
1597
|
);
|
|
863
|
-
if (
|
|
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
|
-
|
|
990
|
-
|
|
991
|
-
|
|
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: "
|
|
1021
|
-
|
|
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
|
-
|
|
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
|
|
1686
|
+
lifecycleEnterBranchMode(this.deps, basePath, milestoneId);
|
|
1214
1687
|
rebuildGitService(this.s, this.deps);
|
|
1215
|
-
|
|
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
|
-
|
|
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. */
|