gsd-pi 2.80.0-dev.cf9433f56 → 2.80.0-dev.d4fc28e6b

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (237) hide show
  1. package/dist/cli.js +0 -19
  2. package/dist/resources/.managed-resources-content-hash +1 -1
  3. package/dist/resources/extensions/claude-code-cli/stream-adapter.js +29 -0
  4. package/dist/resources/extensions/gsd/auto/loop.js +71 -8
  5. package/dist/resources/extensions/gsd/auto/phases.js +150 -94
  6. package/dist/resources/extensions/gsd/auto/resolve.js +12 -0
  7. package/dist/resources/extensions/gsd/auto/run-unit.js +10 -30
  8. package/dist/resources/extensions/gsd/auto/session.js +8 -0
  9. package/dist/resources/extensions/gsd/auto/workflow-dispatch-claim.js +33 -1
  10. package/dist/resources/extensions/gsd/auto/workflow-worker-heartbeat.js +9 -1
  11. package/dist/resources/extensions/gsd/auto-direct-dispatch.js +5 -32
  12. package/dist/resources/extensions/gsd/auto-dispatch.js +16 -0
  13. package/dist/resources/extensions/gsd/auto-post-unit.js +17 -4
  14. package/dist/resources/extensions/gsd/auto-prompts.js +90 -15
  15. package/dist/resources/extensions/gsd/auto-start.js +197 -6
  16. package/dist/resources/extensions/gsd/auto-worktree.js +111 -1
  17. package/dist/resources/extensions/gsd/auto.js +18 -22
  18. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +86 -19
  19. package/dist/resources/extensions/gsd/bootstrap/db-tools.js +49 -36
  20. package/dist/resources/extensions/gsd/bootstrap/dynamic-tools.js +15 -5
  21. package/dist/resources/extensions/gsd/bootstrap/exec-tools.js +9 -3
  22. package/dist/resources/extensions/gsd/bootstrap/journal-tools.js +7 -1
  23. package/dist/resources/extensions/gsd/bootstrap/memory-tools.js +9 -3
  24. package/dist/resources/extensions/gsd/bootstrap/query-tools.js +8 -2
  25. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +298 -54
  26. package/dist/resources/extensions/gsd/bootstrap/system-context.js +82 -23
  27. package/dist/resources/extensions/gsd/clean-root-preflight.js +24 -6
  28. package/dist/resources/extensions/gsd/commands-handlers.js +23 -9
  29. package/dist/resources/extensions/gsd/db/unit-dispatches.js +53 -0
  30. package/dist/resources/extensions/gsd/ecosystem/gsd-extension-api.js +2 -0
  31. package/dist/resources/extensions/gsd/guided-flow.js +47 -28
  32. package/dist/resources/extensions/gsd/native-git-bridge.js +32 -8
  33. package/dist/resources/extensions/gsd/orphan-stash-audit.js +101 -0
  34. package/dist/resources/extensions/gsd/parallel-orchestrator.js +13 -3
  35. package/dist/resources/extensions/gsd/pre-execution-checks.js +15 -0
  36. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  37. package/dist/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  38. package/dist/resources/extensions/gsd/prompts/execute-task.md +4 -2
  39. package/dist/resources/extensions/gsd/prompts/plan-slice.md +1 -1
  40. package/dist/resources/extensions/gsd/prompts/replan-slice.md +2 -2
  41. package/dist/resources/extensions/gsd/workflow-protocol.js +131 -0
  42. package/dist/resources/extensions/gsd/worktree-resolver.js +35 -4
  43. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  44. package/dist/web/standalone/.next/BUILD_ID +1 -1
  45. package/dist/web/standalone/.next/app-path-routes-manifest.json +12 -12
  46. package/dist/web/standalone/.next/build-manifest.json +2 -2
  47. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  48. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  49. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  51. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  55. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  56. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  57. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  59. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  61. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  63. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/index.html +1 -1
  65. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  66. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  67. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  68. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  69. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  70. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  71. package/dist/web/standalone/.next/server/app-paths-manifest.json +12 -12
  72. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  73. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  74. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  75. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  76. package/dist/welcome-screen.d.ts +2 -0
  77. package/dist/welcome-screen.js +9 -7
  78. package/package.json +1 -1
  79. package/packages/pi-agent-core/dist/agent-loop.d.ts.map +1 -1
  80. package/packages/pi-agent-core/dist/agent-loop.js +4 -1
  81. package/packages/pi-agent-core/dist/agent-loop.js.map +1 -1
  82. package/packages/pi-agent-core/dist/agent.d.ts +5 -0
  83. package/packages/pi-agent-core/dist/agent.d.ts.map +1 -1
  84. package/packages/pi-agent-core/dist/agent.js +2 -0
  85. package/packages/pi-agent-core/dist/agent.js.map +1 -1
  86. package/packages/pi-agent-core/dist/index.d.ts +1 -0
  87. package/packages/pi-agent-core/dist/index.d.ts.map +1 -1
  88. package/packages/pi-agent-core/dist/index.js +2 -0
  89. package/packages/pi-agent-core/dist/index.js.map +1 -1
  90. package/packages/pi-agent-core/dist/token-audit.d.ts +47 -0
  91. package/packages/pi-agent-core/dist/token-audit.d.ts.map +1 -0
  92. package/packages/pi-agent-core/dist/token-audit.js +221 -0
  93. package/packages/pi-agent-core/dist/token-audit.js.map +1 -0
  94. package/packages/pi-agent-core/dist/types.d.ts +9 -0
  95. package/packages/pi-agent-core/dist/types.d.ts.map +1 -1
  96. package/packages/pi-agent-core/dist/types.js.map +1 -1
  97. package/packages/pi-agent-core/src/agent-loop.test.ts +128 -0
  98. package/packages/pi-agent-core/src/agent-loop.ts +4 -1
  99. package/packages/pi-agent-core/src/agent.ts +8 -0
  100. package/packages/pi-agent-core/src/index.ts +2 -0
  101. package/packages/pi-agent-core/src/token-audit.test.ts +189 -0
  102. package/packages/pi-agent-core/src/token-audit.ts +287 -0
  103. package/packages/pi-agent-core/src/types.ts +14 -0
  104. package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -1
  105. package/packages/pi-coding-agent/dist/core/agent-session-tool-refresh.test.js +18 -0
  106. package/packages/pi-coding-agent/dist/core/agent-session-tool-refresh.test.js.map +1 -1
  107. package/packages/pi-coding-agent/dist/core/agent-session.d.ts +12 -0
  108. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  109. package/packages/pi-coding-agent/dist/core/agent-session.js +36 -7
  110. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  111. package/packages/pi-coding-agent/dist/core/extensions/loader.d.ts.map +1 -1
  112. package/packages/pi-coding-agent/dist/core/extensions/loader.js +8 -0
  113. package/packages/pi-coding-agent/dist/core/extensions/loader.js.map +1 -1
  114. package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts +2 -0
  115. package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts.map +1 -1
  116. package/packages/pi-coding-agent/dist/core/extensions/runner.js +3 -6
  117. package/packages/pi-coding-agent/dist/core/extensions/runner.js.map +1 -1
  118. package/packages/pi-coding-agent/dist/core/extensions/runner.test.js +3 -3
  119. package/packages/pi-coding-agent/dist/core/extensions/runner.test.js.map +1 -1
  120. package/packages/pi-coding-agent/dist/core/extensions/types.d.ts +32 -1
  121. package/packages/pi-coding-agent/dist/core/extensions/types.d.ts.map +1 -1
  122. package/packages/pi-coding-agent/dist/core/extensions/types.js.map +1 -1
  123. package/packages/pi-coding-agent/dist/core/hooks-runner.test.js +2 -0
  124. package/packages/pi-coding-agent/dist/core/hooks-runner.test.js.map +1 -1
  125. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.d.ts +2 -0
  126. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.d.ts.map +1 -0
  127. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.js +46 -0
  128. package/packages/pi-coding-agent/dist/core/sdk-tool-filter.test.js.map +1 -0
  129. package/packages/pi-coding-agent/dist/core/sdk.d.ts +10 -2
  130. package/packages/pi-coding-agent/dist/core/sdk.d.ts.map +1 -1
  131. package/packages/pi-coding-agent/dist/core/sdk.js +74 -2
  132. package/packages/pi-coding-agent/dist/core/sdk.js.map +1 -1
  133. package/packages/pi-coding-agent/dist/core/skill-tool.test.js +22 -0
  134. package/packages/pi-coding-agent/dist/core/skill-tool.test.js.map +1 -1
  135. package/packages/pi-coding-agent/dist/core/system-prompt.d.ts +6 -7
  136. package/packages/pi-coding-agent/dist/core/system-prompt.d.ts.map +1 -1
  137. package/packages/pi-coding-agent/dist/core/system-prompt.js +2 -3
  138. package/packages/pi-coding-agent/dist/core/system-prompt.js.map +1 -1
  139. package/packages/pi-coding-agent/src/core/agent-session-tool-refresh.test.ts +25 -0
  140. package/packages/pi-coding-agent/src/core/agent-session.ts +40 -7
  141. package/packages/pi-coding-agent/src/core/extensions/loader.ts +10 -0
  142. package/packages/pi-coding-agent/src/core/extensions/runner.test.ts +3 -3
  143. package/packages/pi-coding-agent/src/core/extensions/runner.ts +5 -5
  144. package/packages/pi-coding-agent/src/core/extensions/types.ts +35 -1
  145. package/packages/pi-coding-agent/src/core/hooks-runner.test.ts +2 -0
  146. package/packages/pi-coding-agent/src/core/sdk-tool-filter.test.ts +60 -0
  147. package/packages/pi-coding-agent/src/core/sdk.ts +85 -3
  148. package/packages/pi-coding-agent/src/core/skill-tool.test.ts +28 -0
  149. package/packages/pi-coding-agent/src/core/system-prompt.ts +8 -10
  150. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  151. package/src/resources/extensions/claude-code-cli/stream-adapter.ts +30 -0
  152. package/src/resources/extensions/claude-code-cli/tests/stream-adapter.test.ts +26 -0
  153. package/src/resources/extensions/gsd/auto/loop-deps.ts +2 -2
  154. package/src/resources/extensions/gsd/auto/loop.ts +84 -8
  155. package/src/resources/extensions/gsd/auto/phases.ts +218 -154
  156. package/src/resources/extensions/gsd/auto/resolve.ts +19 -0
  157. package/src/resources/extensions/gsd/auto/run-unit.ts +10 -29
  158. package/src/resources/extensions/gsd/auto/session.ts +8 -0
  159. package/src/resources/extensions/gsd/auto/workflow-dispatch-claim.ts +63 -1
  160. package/src/resources/extensions/gsd/auto/workflow-worker-heartbeat.ts +14 -1
  161. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +8 -34
  162. package/src/resources/extensions/gsd/auto-dispatch.ts +16 -0
  163. package/src/resources/extensions/gsd/auto-post-unit.ts +18 -4
  164. package/src/resources/extensions/gsd/auto-prompts.ts +95 -14
  165. package/src/resources/extensions/gsd/auto-start.ts +230 -9
  166. package/src/resources/extensions/gsd/auto-worktree.ts +123 -0
  167. package/src/resources/extensions/gsd/auto.ts +18 -18
  168. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +100 -18
  169. package/src/resources/extensions/gsd/bootstrap/db-tools.ts +50 -36
  170. package/src/resources/extensions/gsd/bootstrap/dynamic-tools.ts +16 -5
  171. package/src/resources/extensions/gsd/bootstrap/exec-tools.ts +10 -3
  172. package/src/resources/extensions/gsd/bootstrap/journal-tools.ts +8 -1
  173. package/src/resources/extensions/gsd/bootstrap/memory-tools.ts +10 -3
  174. package/src/resources/extensions/gsd/bootstrap/query-tools.ts +9 -2
  175. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +347 -54
  176. package/src/resources/extensions/gsd/bootstrap/system-context.ts +90 -22
  177. package/src/resources/extensions/gsd/clean-root-preflight.ts +32 -7
  178. package/src/resources/extensions/gsd/commands-handlers.ts +34 -15
  179. package/src/resources/extensions/gsd/db/unit-dispatches.ts +66 -0
  180. package/src/resources/extensions/gsd/ecosystem/gsd-extension-api.ts +3 -0
  181. package/src/resources/extensions/gsd/guided-flow.ts +52 -35
  182. package/src/resources/extensions/gsd/native-git-bridge.ts +39 -6
  183. package/src/resources/extensions/gsd/orphan-stash-audit.ts +117 -0
  184. package/src/resources/extensions/gsd/parallel-orchestrator.ts +13 -3
  185. package/src/resources/extensions/gsd/pre-execution-checks.ts +16 -0
  186. package/src/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  187. package/src/resources/extensions/gsd/prompts/complete-slice.md +1 -1
  188. package/src/resources/extensions/gsd/prompts/execute-task.md +4 -2
  189. package/src/resources/extensions/gsd/prompts/plan-slice.md +1 -1
  190. package/src/resources/extensions/gsd/prompts/replan-slice.md +2 -2
  191. package/src/resources/extensions/gsd/tests/artifact-retry-cap.test.ts +2 -2
  192. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +361 -10
  193. package/src/resources/extensions/gsd/tests/auto-wrapup-inflight-guard.test.ts +168 -6
  194. package/src/resources/extensions/gsd/tests/clean-root-preflight.test.ts +15 -6
  195. package/src/resources/extensions/gsd/tests/complete-milestone-excerpt.test.ts +31 -0
  196. package/src/resources/extensions/gsd/tests/complete-slice-composer.test.ts +3 -2
  197. package/src/resources/extensions/gsd/tests/context-store.test.ts +7 -1
  198. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +5 -1
  199. package/src/resources/extensions/gsd/tests/execute-task-rendering.test.ts +5 -2
  200. package/src/resources/extensions/gsd/tests/fast-forward-reused-milestone-branch.test.ts +219 -0
  201. package/src/resources/extensions/gsd/tests/finalize-survivor-branch.test.ts +132 -0
  202. package/src/resources/extensions/gsd/tests/isolation-none-branch-guard.test.ts +6 -3
  203. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +5 -1
  204. package/src/resources/extensions/gsd/tests/journal-query-tool.test.ts +32 -0
  205. package/src/resources/extensions/gsd/tests/knowledge.test.ts +47 -0
  206. package/src/resources/extensions/gsd/tests/merge-conflict-stops-loop.test.ts +1 -0
  207. package/src/resources/extensions/gsd/tests/milestone-merge-stash-restore.test.ts +242 -0
  208. package/src/resources/extensions/gsd/tests/native-git-bridge-exec-fallback.test.ts +34 -2
  209. package/src/resources/extensions/gsd/tests/originalbase-path-comparison.test.ts +3 -0
  210. package/src/resources/extensions/gsd/tests/orphan-merge-bootstrap.test.ts +133 -0
  211. package/src/resources/extensions/gsd/tests/orphan-stash-audit.test.ts +201 -0
  212. package/src/resources/extensions/gsd/tests/parallel-orchestrator-fast-forward.test.ts +113 -0
  213. package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +7 -5
  214. package/src/resources/extensions/gsd/tests/prompt-duplication-cuts.test.ts +230 -0
  215. package/src/resources/extensions/gsd/tests/query-tools-db-open.test.ts +3 -3
  216. package/src/resources/extensions/gsd/tests/restore-tools-after-discuss.test.ts +38 -17
  217. package/src/resources/extensions/gsd/tests/select-resumable-milestone.test.ts +96 -0
  218. package/src/resources/extensions/gsd/tests/session-start-footer.test.ts +77 -0
  219. package/src/resources/extensions/gsd/tests/session-switch-abort-misclassification.test.ts +166 -0
  220. package/src/resources/extensions/gsd/tests/state-corruption-2945.test.ts +1 -0
  221. package/src/resources/extensions/gsd/tests/system-context-memory.test.ts +112 -0
  222. package/src/resources/extensions/gsd/tests/system-context-message-routing.test.ts +7 -9
  223. package/src/resources/extensions/gsd/tests/token-tool-gating.test.ts +291 -0
  224. package/src/resources/extensions/gsd/tests/unit-dispatches.test.ts +50 -1
  225. package/src/resources/extensions/gsd/tests/unstructured-continue-context-injection.test.ts +5 -4
  226. package/src/resources/extensions/gsd/tests/workflow-dispatch-claim.test.ts +142 -0
  227. package/src/resources/extensions/gsd/tests/workflow-protocol-excerpt.test.ts +99 -0
  228. package/src/resources/extensions/gsd/tests/workflow-worker-heartbeat.test.ts +32 -1
  229. package/src/resources/extensions/gsd/tests/worktree-journal-events.test.ts +1 -0
  230. package/src/resources/extensions/gsd/tests/worktree-path-injection.test.ts +22 -19
  231. package/src/resources/extensions/gsd/tests/worktree-project-root-degrade.test.ts +66 -0
  232. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +104 -3
  233. package/src/resources/extensions/gsd/workflow-protocol.ts +160 -0
  234. package/src/resources/extensions/gsd/worktree-resolver.ts +49 -4
  235. package/src/resources/extensions/gsd/tests/phases-merge-error-stops-auto.test.ts +0 -97
  236. /package/dist/web/standalone/.next/static/{-5nHJWzSdG-WkPMul_khA → cWaxzf-sdbSSbbwYu8q7a}/_buildManifest.js +0 -0
  237. /package/dist/web/standalone/.next/static/{-5nHJWzSdG-WkPMul_khA → cWaxzf-sdbSSbbwYu8q7a}/_ssgManifest.js +0 -0
@@ -65,6 +65,7 @@ import { snapshotSkills } from "./skill-discovery.js";
65
65
  import { isDbAvailable, getMilestone, openDatabase, getDbStatus } from "./gsd-db.js";
66
66
  import { isClosedStatus } from "./status-guards.js";
67
67
  import { classifyMilestoneSummaryContent } from "./milestone-summary-classifier.js";
68
+ import { auditOrphanedPreflightStashes } from "./orphan-stash-audit.js";
68
69
 
69
70
  import {
70
71
  debugLog,
@@ -332,6 +333,165 @@ export function auditOrphanedMilestoneBranches(
332
333
  return { recovered, warnings };
333
334
  }
334
335
 
336
+ /**
337
+ * Pure decision function for picking which orphan milestone the auto-loop
338
+ * should resume the merge transition for. Extracted so it can be unit-tested
339
+ * without spinning up a git repo or a SQLite DB.
340
+ *
341
+ * Returns the lexicographically-greatest milestone id (e.g. "M002" beats
342
+ * "M001") whose branch is unmerged AND has commits ahead of main AND whose
343
+ * status is `complete`. Lex-ordering matches the project's M00x convention,
344
+ * which is the most-recently-completed milestone in practice.
345
+ * `isComplete` errors propagate; `commitsAhead` errors are treated as 0.
346
+ */
347
+ export function _selectResumableMilestone(
348
+ branchNames: readonly string[],
349
+ mergedBranches: ReadonlySet<string>,
350
+ isComplete: (milestoneId: string) => boolean,
351
+ commitsAhead: (branch: string) => number,
352
+ ): string | null {
353
+ const candidates: string[] = [];
354
+ for (const branch of branchNames) {
355
+ if (!branch.startsWith("milestone/")) continue;
356
+ const milestoneId = branch.slice("milestone/".length);
357
+ if (mergedBranches.has(branch)) continue;
358
+ if (!isComplete(milestoneId)) continue;
359
+ let ahead = 0;
360
+ try {
361
+ ahead = commitsAhead(branch);
362
+ } catch {
363
+ continue;
364
+ }
365
+ if (ahead <= 0) continue;
366
+ candidates.push(milestoneId);
367
+ }
368
+ if (candidates.length === 0) return null;
369
+ candidates.sort();
370
+ return candidates[candidates.length - 1];
371
+ }
372
+
373
+ /**
374
+ * Find the most-recent completed milestone whose branch still has unmerged
375
+ * commits ahead of the integration branch. Used by `bootstrapAutoSession`
376
+ * to seed `s.currentMilestoneId` so the auto-loop's transition guard at
377
+ * `phases.ts:730` fires on the first iteration after a process restart —
378
+ * without this, the in-memory-only `s.currentMilestoneId` is `null` after
379
+ * restart, the guard short-circuits, and the orphaned milestone branch
380
+ * never gets merged into main (#5538-followup).
381
+ *
382
+ * Returns null when isolation is `none`, the DB is unavailable, or no
383
+ * orphan candidate exists. All git failures degrade silently — startup
384
+ * must never block on this defensive lookup.
385
+ */
386
+ export function findUnmergedCompletedMilestone(
387
+ basePath: string,
388
+ isolationMode: "worktree" | "branch" | "none",
389
+ ): string | null {
390
+ if (isolationMode === "none") return null;
391
+ if (!isDbAvailable()) return null;
392
+
393
+ let milestoneBranches: string[];
394
+ try {
395
+ milestoneBranches = nativeBranchList(basePath, "milestone/*");
396
+ } catch {
397
+ return null;
398
+ }
399
+ if (milestoneBranches.length === 0) return null;
400
+
401
+ let mainBranch: string;
402
+ try {
403
+ mainBranch = nativeDetectMainBranch(basePath);
404
+ } catch {
405
+ mainBranch = "main";
406
+ }
407
+
408
+ let mergedBranches: Set<string>;
409
+ try {
410
+ mergedBranches = new Set(
411
+ nativeBranchListMerged(basePath, mainBranch, "milestone/*"),
412
+ );
413
+ } catch {
414
+ mergedBranches = new Set();
415
+ }
416
+
417
+ return _selectResumableMilestone(
418
+ milestoneBranches,
419
+ mergedBranches,
420
+ (milestoneId) => {
421
+ const row = getMilestone(milestoneId);
422
+ return !!row && row.status === "complete";
423
+ },
424
+ (branch) => nativeCommitCountBetween(basePath, mainBranch, branch),
425
+ );
426
+ }
427
+
428
+ /**
429
+ * Run `mergeAndExit` for a milestone whose worktree/branch finalization
430
+ * never completed in a prior session — the active-milestone in phase
431
+ * `complete` with a survivor `milestone/<id>` branch still around.
432
+ *
433
+ * Wraps the call in try/catch so a thrown error from `_mergeBranchMode`
434
+ * (made fail-loud in commit 68ef58a3c) is converted into a user-facing
435
+ * error notify instead of an unhandled exception that propagates through
436
+ * `bootstrapAutoSession` to the slash-command caller's `.catch` block.
437
+ *
438
+ * Returns `{ merged: true }` on success; `{ merged: false, error }` on
439
+ * throw — caller decides whether to abort bootstrap.
440
+ */
441
+ export function _finalizeSurvivorBranch(
442
+ resolver: WorktreeResolver,
443
+ milestoneId: string,
444
+ ui: { notify: (msg: string, level?: "info" | "warning" | "error" | "success") => void },
445
+ ): { merged: boolean; error?: unknown } {
446
+ ui.notify(
447
+ `Milestone ${milestoneId} is complete but branch/worktree was not finalized. Running merge now.`,
448
+ "info",
449
+ );
450
+ try {
451
+ resolver.mergeAndExit(milestoneId, { notify: ui.notify.bind(ui) });
452
+ return { merged: true };
453
+ } catch (err) {
454
+ const msg = err instanceof Error ? err.message : String(err);
455
+ ui.notify(
456
+ `Survivor-branch finalization for ${milestoneId} failed: ${msg}. Resolve manually and re-run /gsd auto.`,
457
+ "error",
458
+ );
459
+ return { merged: false, error: err };
460
+ }
461
+ }
462
+
463
+ /**
464
+ * Merge a milestone whose DB row is `complete` but whose branch is still
465
+ * unmerged into the integration branch. Called from `bootstrapAutoSession`
466
+ * for orphans surfaced by `findUnmergedCompletedMilestone`.
467
+ *
468
+ * Notifies the user before and after, swallowing errors so a transient git
469
+ * failure never blocks bootstrap. Returns `{ merged: true }` when the
470
+ * underlying `mergeAndExit` completes; `{ merged: false, error }` on throw.
471
+ *
472
+ * Extracted to keep `bootstrapAutoSession` testable: the merge call and the
473
+ * notify shape are exercised against a mock resolver in
474
+ * `tests/orphan-merge-bootstrap.test.ts`.
475
+ */
476
+ export function _mergeOrphanCompletedMilestone(
477
+ resolver: WorktreeResolver,
478
+ orphanId: string,
479
+ ui: { notify: (msg: string, level?: "info" | "warning" | "error" | "success") => void },
480
+ ): { merged: boolean; error?: unknown } {
481
+ ui.notify(`Detected unmerged completed milestone ${orphanId}. Merging now.`, "info");
482
+ try {
483
+ resolver.mergeAndExit(orphanId, { notify: ui.notify.bind(ui) });
484
+ return { merged: true };
485
+ } catch (err) {
486
+ const msg = err instanceof Error ? err.message : String(err);
487
+ ui.notify(
488
+ `Could not merge orphan milestone ${orphanId}: ${msg}. Resolve manually and re-run /gsd auto.`,
489
+ "warning",
490
+ );
491
+ return { merged: false, error: err };
492
+ }
493
+ }
494
+
335
495
  export async function bootstrapAutoSession(
336
496
  s: AutoSession,
337
497
  ctx: ExtensionCommandContext,
@@ -577,6 +737,39 @@ export async function bootstrapAutoSession(
577
737
  logWarning("bootstrap", `orphaned milestone branch audit failed: ${err instanceof Error ? err.message : String(err)}`);
578
738
  }
579
739
 
740
+ // ── Orphaned preflight-stash audit (#5538-followup) ──
741
+ // Reapplies pre-merge stashes whose milestone is now complete but whose
742
+ // postflight pop was skipped by an interrupted merge in a prior session.
743
+ // Uses `git stash apply` (not pop) so the entry remains as a backup.
744
+ try {
745
+ if (isDbAvailable()) {
746
+ const stashAudit = auditOrphanedPreflightStashes(base, (milestoneId) => {
747
+ const row = getMilestone(milestoneId);
748
+ return !!row && isClosedStatus(row.status);
749
+ });
750
+ for (const entry of stashAudit.applied) {
751
+ ctx.ui.notify(
752
+ `Orphan audit: applied preflight stash ${entry.stashRef} for completed milestone ${entry.milestoneId}. The stash entry is preserved as a backup.`,
753
+ "info",
754
+ );
755
+ }
756
+ for (const msg of stashAudit.warnings) {
757
+ ctx.ui.notify(`Orphan audit: ${msg}`, "warning");
758
+ }
759
+ if (stashAudit.applied.length > 0) {
760
+ debugLog("orphan-stash-audit", {
761
+ applied: stashAudit.applied,
762
+ warnings: stashAudit.warnings,
763
+ });
764
+ }
765
+ }
766
+ } catch (err) {
767
+ logWarning(
768
+ "bootstrap",
769
+ `orphaned preflight-stash audit failed: ${err instanceof Error ? err.message : String(err)}`,
770
+ );
771
+ }
772
+
580
773
  let state = await deriveState(base);
581
774
 
582
775
  // Stale worktree state recovery (#654)
@@ -649,20 +842,48 @@ export async function bootstrapAutoSession(
649
842
  // hasSurvivorBranch after a successful promotion.
650
843
  if (decideSurvivorAction(hasSurvivorBranch, state.phase) === "finalize") {
651
844
  const mid = state.activeMilestone!.id;
652
- ctx.ui.notify(
653
- `Milestone ${mid} is complete but branch/worktree was not finalized. Running merge now.`,
654
- "info",
655
- );
656
- const resolver = buildResolver();
657
- resolver.mergeAndExit(mid, {
658
- notify: ctx.ui.notify.bind(ctx.ui),
659
- });
845
+ // Commit 68ef58a3c made `_mergeBranchMode` throw on wrong-branch
846
+ // instead of returning false silently. Wrap the call so the throw is
847
+ // converted into an error notify + clean bootstrap abort, not an
848
+ // unhandled exception propagating to the slash-command caller (#5549
849
+ // post-merge audit, R2).
850
+ const finalize = _finalizeSurvivorBranch(buildResolver(), mid, ctx.ui);
851
+ if (!finalize.merged) {
852
+ return releaseLockAndReturn();
853
+ }
660
854
  invalidateAllCaches();
661
855
  state = await deriveState(base);
662
856
  // Clear survivor flag — finalization is done
663
857
  hasSurvivorBranch = false;
664
858
  }
665
859
 
860
+ // ── Orphan-completed-milestone merge (#5538-followup) ──
861
+ // A process killed between `complete-milestone` (DB flip + SUMMARY write)
862
+ // and the loop's transition-guard merge strands the milestone branch
863
+ // forever: `s.currentMilestoneId` is in-memory only, so on the next
864
+ // bootstrap the guard at phases.ts:730 sees `mid === s.currentMilestoneId`
865
+ // and short-circuits.
866
+ //
867
+ // The earlier attempt at this fix seeded `s.currentMilestoneId` to the
868
+ // orphan id pre-state-derivation, but the unconditional assignment at
869
+ // line 948 (`s.currentMilestoneId = state.activeMilestone?.id ?? null`)
870
+ // immediately overwrote the seed. Active-merge is the more durable fix:
871
+ // call `mergeAndExit` directly during bootstrap, then re-derive state so
872
+ // the loop's normal flow continues without an in-memory hint.
873
+ //
874
+ // Mirrors the survivor-finalize block above. Failures degrade to a
875
+ // warning notify so a transient git error doesn't block bootstrap.
876
+ {
877
+ const orphan = findUnmergedCompletedMilestone(base, getIsolationMode(base));
878
+ if (orphan && orphan !== state.activeMilestone?.id) {
879
+ const result = _mergeOrphanCompletedMilestone(buildResolver(), orphan, ctx.ui);
880
+ if (result.merged) {
881
+ invalidateAllCaches();
882
+ state = await deriveState(base);
883
+ }
884
+ }
885
+ }
886
+
666
887
  const effectivePrefs = loadEffectiveGSDPreferences(base)?.preferences;
667
888
  const { shouldRunDeepProjectSetup } = await import("./auto-dispatch.js");
668
889
  const deepProjectStagePending = shouldRunDeepProjectSetup(
@@ -790,7 +1011,7 @@ export async function bootstrapAutoSession(
790
1011
  s.resourceVersionOnStart = readResourceVersion();
791
1012
  s.pendingQuickTasks = [];
792
1013
  s.currentUnit = null;
793
- s.currentMilestoneId = deepProjectStagePending ? null : state.activeMilestone?.id ?? null;
1014
+ s.currentMilestoneId ??= deepProjectStagePending ? null : state.activeMilestone?.id ?? null;
794
1015
  s.originalModelId = startModelSnapshot?.id ?? ctx.model?.id ?? null;
795
1016
  s.originalModelProvider = startModelSnapshot?.provider ?? ctx.model?.provider ?? null;
796
1017
  s.originalThinkingLevel = startThinkingSnapshot ?? null;
@@ -78,6 +78,7 @@ import {
78
78
  nativeUpdateRef,
79
79
  nativeIsAncestor,
80
80
  nativeMergeAbort,
81
+ nativeWorktreeList,
81
82
  } from "./native-git-bridge.js";
82
83
  import { gsdHome } from "./gsd-home.js";
83
84
  import { type MilestoneScope, type GsdWorkspace, createWorkspace } from "./workspace.js";
@@ -1180,6 +1181,122 @@ export function enterBranchModeForMilestone(
1180
1181
  * (both formerly here) became dead.
1181
1182
  */
1182
1183
 
1184
+ /**
1185
+ * True when `branch` is checked out in any worktree listed by
1186
+ * `git worktree list --porcelain`. Used to gate ref updates that would
1187
+ * otherwise leave a concurrent worktree's HEAD inconsistent with its
1188
+ * index/working tree (Codex peer-review of #5538-followup).
1189
+ *
1190
+ * Best-effort: a `nativeWorktreeList` failure returns true so we err on
1191
+ * the side of NOT moving the ref. Better to skip a fast-forward than to
1192
+ * silently corrupt another worktree.
1193
+ */
1194
+ export function _isBranchCheckedOutElsewhere(
1195
+ basePath: string,
1196
+ branch: string,
1197
+ ): boolean {
1198
+ try {
1199
+ const entries = nativeWorktreeList(basePath);
1200
+ return entries.some((entry) => entry.branch === branch);
1201
+ } catch {
1202
+ return true;
1203
+ }
1204
+ }
1205
+
1206
+ /**
1207
+ * Resolve the integration branch using the same 3-tier fallback as the
1208
+ * fresh-create path: META.json → git.main_branch preference → detected
1209
+ * main branch. Returns null when no usable target exists.
1210
+ */
1211
+ function _resolveIntegrationBranchForReuse(
1212
+ basePath: string,
1213
+ milestoneId: string,
1214
+ ): string | null {
1215
+ const fromMeta = readIntegrationBranch(basePath, milestoneId);
1216
+ if (fromMeta) return fromMeta;
1217
+
1218
+ const gitPrefs = loadEffectiveGSDPreferences()?.preferences?.git;
1219
+ const fromPref = gitPrefs?.main_branch &&
1220
+ typeof gitPrefs.main_branch === "string" &&
1221
+ gitPrefs.main_branch.length > 0 &&
1222
+ nativeBranchExists(basePath, gitPrefs.main_branch)
1223
+ ? gitPrefs.main_branch
1224
+ : null;
1225
+ if (fromPref) return fromPref;
1226
+
1227
+ try {
1228
+ return nativeDetectMainBranch(basePath);
1229
+ } catch {
1230
+ return null;
1231
+ }
1232
+ }
1233
+
1234
+ /**
1235
+ * When reusing an existing milestone branch, fast-forward it onto the
1236
+ * integration branch when that's safe (branch is a strict ancestor of
1237
+ * integration — no commits would be lost). Skips when the branch has its
1238
+ * own commits ahead of integration, when the integration branch can't be
1239
+ * resolved, or when any git operation fails — the merge gate at milestone
1240
+ * completion will surface real divergence as a conflict.
1241
+ *
1242
+ * The previous behavior re-attached the worktree to whatever stale tip
1243
+ * the branch held, which caused new milestone work to fork from a base
1244
+ * missing prior milestones' merges (#5538-followup).
1245
+ */
1246
+ export function fastForwardReusedMilestoneBranchIfSafe(
1247
+ basePath: string,
1248
+ milestoneId: string,
1249
+ branch: string,
1250
+ ): void {
1251
+ try {
1252
+ const integrationBranch = _resolveIntegrationBranchForReuse(basePath, milestoneId);
1253
+ if (!integrationBranch || integrationBranch === branch) return;
1254
+ if (!nativeBranchExists(basePath, integrationBranch)) return;
1255
+
1256
+ // Pure fast-forward only: branch must be a strict ancestor of integration.
1257
+ // If the branch has its own commits ahead, leave it alone.
1258
+ if (!nativeIsAncestor(basePath, branch, integrationBranch)) {
1259
+ debugLog("createAutoWorktree", {
1260
+ phase: "skip-ff-branch-not-ancestor",
1261
+ milestoneId,
1262
+ branch,
1263
+ integration: integrationBranch,
1264
+ });
1265
+ return;
1266
+ }
1267
+
1268
+ // Codex peer-review: `nativeUpdateRef` succeeds even when the branch is
1269
+ // currently checked out in another worktree, leaving that worktree's HEAD
1270
+ // inconsistent with its index/work tree. Skip the fast-forward if any
1271
+ // listed worktree has this branch checked out — the merge gate at
1272
+ // milestone-completion will surface stale-base divergence as a conflict
1273
+ // instead of silently corrupting the other worktree's state.
1274
+ if (_isBranchCheckedOutElsewhere(basePath, branch)) {
1275
+ debugLog("createAutoWorktree", {
1276
+ phase: "skip-ff-branch-checked-out-elsewhere",
1277
+ milestoneId,
1278
+ branch,
1279
+ });
1280
+ return;
1281
+ }
1282
+
1283
+ nativeUpdateRef(basePath, `refs/heads/${branch}`, integrationBranch);
1284
+ debugLog("createAutoWorktree", {
1285
+ phase: "fast-forward-reused-branch",
1286
+ milestoneId,
1287
+ branch,
1288
+ integration: integrationBranch,
1289
+ });
1290
+ } catch (err) {
1291
+ debugLog("createAutoWorktree", {
1292
+ phase: "fast-forward-reused-branch-failed",
1293
+ milestoneId,
1294
+ branch,
1295
+ error: err instanceof Error ? err.message : String(err),
1296
+ });
1297
+ }
1298
+ }
1299
+
1183
1300
  export function createAutoWorktree(
1184
1301
  basePath: string,
1185
1302
  milestoneId: string,
@@ -1206,6 +1323,12 @@ export function createAutoWorktree(
1206
1323
 
1207
1324
  let info: { name: string; path: string; branch: string; exists: boolean };
1208
1325
  if (branchExists) {
1326
+ // #5538-followup: fast-forward the reused branch onto the integration
1327
+ // branch when safe so the next milestone forks from up-to-date code.
1328
+ // Without this, a milestone that was created before another milestone
1329
+ // merged into main would carry a stale base into its worktree.
1330
+ fastForwardReusedMilestoneBranchIfSafe(basePath, milestoneId, branch);
1331
+
1209
1332
  // Re-attach worktree to the existing milestone branch (preserving commits)
1210
1333
  info = createWorktree(basePath, milestoneId, {
1211
1334
  branch,
@@ -151,6 +151,7 @@ import {
151
151
  resolveProjectRoot,
152
152
  } from "./worktree.js";
153
153
  import { GitServiceImpl } from "./git-service.js";
154
+ import { nativeCheckoutBranch } from "./native-git-bridge.js";
154
155
  import { getPriorSliceCompletionBlocker } from "./dispatch-guard.js";
155
156
  import {
156
157
  createAutoWorktree,
@@ -1173,6 +1174,21 @@ export async function stopAuto(
1173
1174
  debugLog("stop-cleanup-basepath", { error: e instanceof Error ? e.message : String(e) });
1174
1175
  }
1175
1176
 
1177
+ // Re-root the active command session/tool runtime after worktree teardown.
1178
+ // mergeAndExit restores process.cwd(), but AgentSession has already captured
1179
+ // its own cwd for tools and system prompt; refresh it before returning to the
1180
+ // user so follow-up commands do not target a removed milestone worktree.
1181
+ if (s.originalBasePath && ctx && s.cmdCtx) {
1182
+ try {
1183
+ const result = await s.cmdCtx.newSession({ workspaceRoot: s.basePath });
1184
+ if (result.cancelled) {
1185
+ logWarning("engine", "post-stop session re-root was cancelled", { file: "auto.ts", basePath: s.basePath });
1186
+ }
1187
+ } catch (err) {
1188
+ logWarning("engine", `post-stop session re-root failed: ${err instanceof Error ? err.message : String(err)}`, { file: "auto.ts", basePath: s.basePath });
1189
+ }
1190
+ }
1191
+
1176
1192
  // ── Step 8: Ledger notification ──
1177
1193
  try {
1178
1194
  const ledger = getLedger();
@@ -1436,6 +1452,7 @@ function buildResolverDeps(): WorktreeResolverDeps {
1436
1452
  getAutoWorktreePath,
1437
1453
  autoCommitCurrentBranch,
1438
1454
  getCurrentBranch,
1455
+ checkoutBranch: nativeCheckoutBranch,
1439
1456
  autoWorktreeBranch,
1440
1457
  resolveMilestoneFile,
1441
1458
  readFileSync: (path: string, encoding: string) =>
@@ -2301,24 +2318,7 @@ export async function dispatchHookUnit(
2301
2318
  startedAt: hookStartedAt,
2302
2319
  };
2303
2320
 
2304
- // Ensure cwd matches basePath BEFORE newSession() captures it (#1389).
2305
- // newSession() snapshots process.cwd() during construction; chdir-ing
2306
- // afterward leaves the session rooted to whatever cwd was when the call
2307
- // was made. Must be synchronous — no awaits between chdir and newSession.
2308
- try { if (process.cwd() !== s.basePath) process.chdir(s.basePath); } catch (err) {
2309
- const msg = `Failed to chdir before hook newSession (basePath: ${s.basePath}): ${err instanceof Error ? err.message : String(err)}`;
2310
- logWarning("engine", msg, { file: "auto.ts", basePath: s.basePath, error: err instanceof Error ? err.message : String(err) });
2311
- ctx.ui.notify(`${msg}. Cancelling hook dispatch to avoid running in the wrong directory.`, "error");
2312
- if (wasActive) {
2313
- s.basePath = previousBasePath;
2314
- s.currentUnit = previousCurrentUnit;
2315
- } else {
2316
- s.reset();
2317
- }
2318
- return false;
2319
- }
2320
-
2321
- const result = await s.cmdCtx!.newSession();
2321
+ const result = await s.cmdCtx!.newSession({ workspaceRoot: s.basePath });
2322
2322
  if (result.cancelled) {
2323
2323
  await stopAuto(ctx, pi);
2324
2324
  return false;
@@ -2,6 +2,7 @@
2
2
 
3
3
  import type { ExtensionAPI, ExtensionContext } from "@gsd/pi-coding-agent";
4
4
 
5
+ import type { ErrorContext } from "../auto/types.js";
5
6
  import { logWarning } from "../workflow-logger.js";
6
7
  import {
7
8
  checkDeepProjectSetupAfterTurn,
@@ -14,7 +15,12 @@ import { clearPathCache } from "../paths.js";
14
15
  import { getAutoDashboardData, getAutoModeStartModel, isAutoActive, pauseAuto, setCurrentDispatchedModelId } from "../auto.js";
15
16
  import { getNextFallbackModel, resolveModelWithFallbacksForUnit } from "../preferences.js";
16
17
  import { pauseAutoForProviderError } from "../provider-error-pause.js";
17
- import { isSessionSwitchInFlight, resolveAgentEnd, resolveAgentEndCancelled } from "../auto/resolve.js";
18
+ import {
19
+ isSessionSwitchAbortGraceActive,
20
+ isSessionSwitchInFlight,
21
+ resolveAgentEnd,
22
+ resolveAgentEndCancelled,
23
+ } from "../auto/resolve.js";
18
24
  import { resolveModelId } from "../auto-model-selection.js";
19
25
  import { resolveProjectRoot } from "../worktree.js";
20
26
  import { clearDiscussionFlowState } from "./write-gate.js";
@@ -76,6 +82,91 @@ export function isUserInitiatedAbortMessage(message: string | undefined | null):
76
82
  return /\b(?:claude code process aborted by user|request aborted by user|process aborted by user)\b/i.test(message);
77
83
  }
78
84
 
85
+ function isBareClaudeCodeSessionSwitchAbortMarker(message: string | undefined | null): boolean {
86
+ if (!message) return false;
87
+ const normalized = message.trim().replace(/\s+/g, " ").toLowerCase();
88
+ return normalized === "claude code process aborted by user"
89
+ || normalized === "request aborted by user"
90
+ || normalized === "process aborted by user"
91
+ || normalized === "claude code stream aborted by caller";
92
+ }
93
+
94
+ function readAssistantTextContent(content: unknown): string {
95
+ if (!Array.isArray(content)) return "";
96
+ return content
97
+ .map((block) => {
98
+ if (!block || typeof block !== "object") return "";
99
+ const text = (block as { text?: unknown }).text;
100
+ return typeof text === "string" ? text : "";
101
+ })
102
+ .filter(Boolean)
103
+ .join("\n");
104
+ }
105
+
106
+ export function isClaudeCodeSessionSwitchAbortMessage(lastMsg: unknown): boolean {
107
+ if (!lastMsg || typeof lastMsg !== "object") return false;
108
+ const m = lastMsg as { stopReason?: unknown; errorMessage?: unknown; content?: unknown };
109
+ const carriers = [
110
+ m.errorMessage ? String(m.errorMessage) : "",
111
+ readAssistantTextContent(m.content),
112
+ ].filter((value) => value.trim().length > 0);
113
+
114
+ if ((m.stopReason === "error" || m.stopReason === "aborted") && carriers.length > 0) {
115
+ return carriers.every(isBareClaudeCodeSessionSwitchAbortMarker);
116
+ }
117
+
118
+ return false;
119
+ }
120
+
121
+ /**
122
+ * Resolve an agent_end event observed while a session switch is in flight.
123
+ *
124
+ * #5538-followup: When `newSession()` aborts an in-flight stream as part of a
125
+ * session transition (run-unit.ts:63 → _settleCurrentTurnForSessionTransition
126
+ * → agent.abort()), the SDK emits "Claude Code process aborted by user" or
127
+ * "Request aborted by user" against the previous unit's turn. The previous
128
+ * code path treated that as a user cancellation and propagated it to the next
129
+ * unit via the pending-switch-cancellation queue, killing auto-mode with
130
+ * "Auto-mode stopped — Unit aborted: Claude Code process aborted by user"
131
+ * even though no user input occurred.
132
+ *
133
+ * Claude Code abort markers are intentionally ignored when the abort fires
134
+ * while the session-switch is in flight: the abort is the expected side-effect
135
+ * of the transition, not a user signal. Other branches (genuine `stopReason
136
+ * === "aborted"` with diagnostic content/errorMessage) preserve the prior
137
+ * behavior.
138
+ */
139
+ export function _handleSessionSwitchAgentEnd(
140
+ lastMsg: unknown,
141
+ resolveCancelled: (ctx: ErrorContext) => boolean,
142
+ ): void {
143
+ if (!lastMsg || typeof lastMsg !== "object") return;
144
+ const m = lastMsg as { stopReason?: unknown; errorMessage?: unknown; content?: unknown };
145
+
146
+ if (isClaudeCodeSessionSwitchAbortMessage(m)) {
147
+ // Internal abort from in-flight session transition — drop on the floor.
148
+ return;
149
+ }
150
+
151
+ if (m.stopReason === "error") {
152
+ const rawErrorMsg = m.errorMessage ? String(m.errorMessage) : "";
153
+ if (isBareClaudeCodeSessionSwitchAbortMarker(rawErrorMsg)) {
154
+ // Internal abort from in-flight session transition — drop on the floor.
155
+ return;
156
+ }
157
+ return;
158
+ }
159
+
160
+ if (m.stopReason === "aborted") {
161
+ const content = m.content;
162
+ const hasEmptyContent = Array.isArray(content) && content.length === 0;
163
+ const hasErrorMessage = !!m.errorMessage;
164
+ if (!hasEmptyContent || hasErrorMessage) {
165
+ resolveCancelled(_buildAbortedPauseContext(m as { errorMessage?: unknown }));
166
+ }
167
+ }
168
+ }
169
+
79
170
  async function pauseTransientWithBackoff(
80
171
  cls: ErrorClass,
81
172
  pi: ExtensionAPI,
@@ -156,23 +247,14 @@ export async function handleAgentEnd(
156
247
 
157
248
  const lastMsg = event.messages[event.messages.length - 1];
158
249
  if (isSessionSwitchInFlight()) {
159
- if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "error") {
160
- const rawErrorMsg = ("errorMessage" in lastMsg && lastMsg.errorMessage) ? String(lastMsg.errorMessage) : "";
161
- if (isUserInitiatedAbortMessage(rawErrorMsg)) {
162
- resolveAgentEndCancelled({
163
- message: rawErrorMsg,
164
- category: "aborted",
165
- isTransient: false,
166
- });
167
- }
168
- } else if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "aborted") {
169
- const content = "content" in lastMsg ? lastMsg.content : undefined;
170
- const hasEmptyContent = Array.isArray(content) && content.length === 0;
171
- const hasErrorMessage = "errorMessage" in lastMsg && !!lastMsg.errorMessage;
172
- if (!hasEmptyContent || hasErrorMessage) {
173
- resolveAgentEndCancelled(_buildAbortedPauseContext(lastMsg as { errorMessage?: unknown }));
174
- }
175
- }
250
+ _handleSessionSwitchAgentEnd(lastMsg, resolveAgentEndCancelled);
251
+ return;
252
+ }
253
+
254
+ if (isSessionSwitchAbortGraceActive() && isClaudeCodeSessionSwitchAbortMessage(lastMsg)) {
255
+ // Claude Code can report the abort from `newSession()` a few hundred ms
256
+ // after the guard drops. That event belongs to the old turn; do not let it
257
+ // cancel the freshly-dispatched unit.
176
258
  return;
177
259
  }
178
260