gsd-pi 2.33.1-dev.ee47f1b → 2.34.0-dev.bbb5216

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 (135) hide show
  1. package/dist/bundled-resource-path.d.ts +8 -0
  2. package/dist/bundled-resource-path.js +14 -0
  3. package/dist/headless-query.js +6 -6
  4. package/dist/resources/extensions/gsd/auto/session.js +27 -32
  5. package/dist/resources/extensions/gsd/auto-dashboard.js +29 -109
  6. package/dist/resources/extensions/gsd/auto-direct-dispatch.js +6 -1
  7. package/dist/resources/extensions/gsd/auto-dispatch.js +52 -81
  8. package/dist/resources/extensions/gsd/auto-loop.js +956 -0
  9. package/dist/resources/extensions/gsd/auto-observability.js +4 -2
  10. package/dist/resources/extensions/gsd/auto-post-unit.js +75 -185
  11. package/dist/resources/extensions/gsd/auto-prompts.js +133 -101
  12. package/dist/resources/extensions/gsd/auto-recovery.js +59 -97
  13. package/dist/resources/extensions/gsd/auto-start.js +330 -309
  14. package/dist/resources/extensions/gsd/auto-supervisor.js +5 -11
  15. package/dist/resources/extensions/gsd/auto-timeout-recovery.js +7 -7
  16. package/dist/resources/extensions/gsd/auto-timers.js +3 -4
  17. package/dist/resources/extensions/gsd/auto-verification.js +35 -73
  18. package/dist/resources/extensions/gsd/auto-worktree-sync.js +167 -0
  19. package/dist/resources/extensions/gsd/auto-worktree.js +291 -126
  20. package/dist/resources/extensions/gsd/auto.js +283 -1013
  21. package/dist/resources/extensions/gsd/captures.js +10 -4
  22. package/dist/resources/extensions/gsd/dispatch-guard.js +7 -8
  23. package/dist/resources/extensions/gsd/docs/preferences-reference.md +25 -18
  24. package/dist/resources/extensions/gsd/doctor-checks.js +3 -4
  25. package/dist/resources/extensions/gsd/git-service.js +1 -1
  26. package/dist/resources/extensions/gsd/gsd-db.js +296 -151
  27. package/dist/resources/extensions/gsd/index.js +92 -228
  28. package/dist/resources/extensions/gsd/post-unit-hooks.js +13 -13
  29. package/dist/resources/extensions/gsd/progress-score.js +61 -156
  30. package/dist/resources/extensions/gsd/quick.js +98 -122
  31. package/dist/resources/extensions/gsd/session-lock.js +13 -0
  32. package/dist/resources/extensions/gsd/templates/preferences.md +1 -0
  33. package/dist/resources/extensions/gsd/undo.js +43 -48
  34. package/dist/resources/extensions/gsd/unit-runtime.js +16 -15
  35. package/dist/resources/extensions/gsd/verification-evidence.js +0 -1
  36. package/dist/resources/extensions/gsd/verification-gate.js +6 -35
  37. package/dist/resources/extensions/gsd/worktree-command.js +30 -24
  38. package/dist/resources/extensions/gsd/worktree-manager.js +2 -3
  39. package/dist/resources/extensions/gsd/worktree-resolver.js +344 -0
  40. package/dist/resources/extensions/gsd/worktree.js +7 -44
  41. package/dist/tool-bootstrap.js +59 -11
  42. package/dist/worktree-cli.js +7 -7
  43. package/package.json +1 -1
  44. package/packages/pi-ai/dist/models.generated.d.ts +3630 -5483
  45. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  46. package/packages/pi-ai/dist/models.generated.js +735 -2588
  47. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  48. package/packages/pi-ai/src/models.generated.ts +1039 -2892
  49. package/packages/pi-coding-agent/package.json +1 -1
  50. package/pkg/package.json +1 -1
  51. package/src/resources/extensions/gsd/auto/session.ts +47 -30
  52. package/src/resources/extensions/gsd/auto-dashboard.ts +28 -131
  53. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +6 -1
  54. package/src/resources/extensions/gsd/auto-dispatch.ts +135 -91
  55. package/src/resources/extensions/gsd/auto-loop.ts +1665 -0
  56. package/src/resources/extensions/gsd/auto-observability.ts +4 -2
  57. package/src/resources/extensions/gsd/auto-post-unit.ts +85 -228
  58. package/src/resources/extensions/gsd/auto-prompts.ts +138 -109
  59. package/src/resources/extensions/gsd/auto-recovery.ts +124 -118
  60. package/src/resources/extensions/gsd/auto-start.ts +440 -354
  61. package/src/resources/extensions/gsd/auto-supervisor.ts +5 -12
  62. package/src/resources/extensions/gsd/auto-timeout-recovery.ts +8 -8
  63. package/src/resources/extensions/gsd/auto-timers.ts +3 -4
  64. package/src/resources/extensions/gsd/auto-verification.ts +76 -90
  65. package/src/resources/extensions/gsd/auto-worktree-sync.ts +204 -0
  66. package/src/resources/extensions/gsd/auto-worktree.ts +389 -141
  67. package/src/resources/extensions/gsd/auto.ts +515 -1199
  68. package/src/resources/extensions/gsd/captures.ts +10 -4
  69. package/src/resources/extensions/gsd/dispatch-guard.ts +13 -9
  70. package/src/resources/extensions/gsd/docs/preferences-reference.md +25 -18
  71. package/src/resources/extensions/gsd/doctor-checks.ts +3 -4
  72. package/src/resources/extensions/gsd/git-service.ts +8 -1
  73. package/src/resources/extensions/gsd/gitignore.ts +4 -2
  74. package/src/resources/extensions/gsd/gsd-db.ts +375 -180
  75. package/src/resources/extensions/gsd/index.ts +104 -263
  76. package/src/resources/extensions/gsd/post-unit-hooks.ts +13 -13
  77. package/src/resources/extensions/gsd/progress-score.ts +65 -200
  78. package/src/resources/extensions/gsd/quick.ts +121 -125
  79. package/src/resources/extensions/gsd/session-lock.ts +11 -0
  80. package/src/resources/extensions/gsd/templates/preferences.md +1 -0
  81. package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +32 -59
  82. package/src/resources/extensions/gsd/tests/all-milestones-complete-merge.test.ts +75 -27
  83. package/src/resources/extensions/gsd/tests/auto-budget-alerts.test.ts +1 -1
  84. package/src/resources/extensions/gsd/tests/auto-lock-creation.test.ts +37 -0
  85. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +1458 -0
  86. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +8 -162
  87. package/src/resources/extensions/gsd/tests/auto-secrets-gate.test.ts +2 -108
  88. package/src/resources/extensions/gsd/tests/auto-session-encapsulation.test.ts +1 -3
  89. package/src/resources/extensions/gsd/tests/auto-worktree-milestone-merge.test.ts +0 -3
  90. package/src/resources/extensions/gsd/tests/auto-worktree.test.ts +58 -0
  91. package/src/resources/extensions/gsd/tests/dispatch-guard.test.ts +0 -55
  92. package/src/resources/extensions/gsd/tests/headless-query.test.ts +22 -0
  93. package/src/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +8 -11
  94. package/src/resources/extensions/gsd/tests/provider-errors.test.ts +4 -6
  95. package/src/resources/extensions/gsd/tests/run-uat.test.ts +3 -3
  96. package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +64 -0
  97. package/src/resources/extensions/gsd/tests/sidecar-queue.test.ts +181 -0
  98. package/src/resources/extensions/gsd/tests/stale-worktree-cwd.test.ts +0 -3
  99. package/src/resources/extensions/gsd/tests/token-profile.test.ts +6 -6
  100. package/src/resources/extensions/gsd/tests/triage-dispatch.test.ts +6 -6
  101. package/src/resources/extensions/gsd/tests/undo.test.ts +6 -0
  102. package/src/resources/extensions/gsd/tests/verification-evidence.test.ts +24 -26
  103. package/src/resources/extensions/gsd/tests/verification-gate.test.ts +7 -201
  104. package/src/resources/extensions/gsd/tests/worktree-db-integration.test.ts +205 -0
  105. package/src/resources/extensions/gsd/tests/worktree-db.test.ts +442 -0
  106. package/src/resources/extensions/gsd/tests/worktree-e2e.test.ts +0 -3
  107. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +705 -0
  108. package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +57 -106
  109. package/src/resources/extensions/gsd/tests/worktree.test.ts +5 -1
  110. package/src/resources/extensions/gsd/tests/write-gate.test.ts +43 -132
  111. package/src/resources/extensions/gsd/types.ts +90 -81
  112. package/src/resources/extensions/gsd/undo.ts +42 -46
  113. package/src/resources/extensions/gsd/unit-runtime.ts +14 -18
  114. package/src/resources/extensions/gsd/verification-evidence.ts +1 -3
  115. package/src/resources/extensions/gsd/verification-gate.ts +6 -39
  116. package/src/resources/extensions/gsd/worktree-command.ts +36 -24
  117. package/src/resources/extensions/gsd/worktree-manager.ts +2 -3
  118. package/src/resources/extensions/gsd/worktree-resolver.ts +485 -0
  119. package/src/resources/extensions/gsd/worktree.ts +7 -44
  120. package/dist/resources/extensions/gsd/auto-constants.js +0 -5
  121. package/dist/resources/extensions/gsd/auto-idempotency.js +0 -106
  122. package/dist/resources/extensions/gsd/auto-stuck-detection.js +0 -165
  123. package/dist/resources/extensions/gsd/mechanical-completion.js +0 -351
  124. package/src/resources/extensions/gsd/auto-constants.ts +0 -6
  125. package/src/resources/extensions/gsd/auto-idempotency.ts +0 -151
  126. package/src/resources/extensions/gsd/auto-stuck-detection.ts +0 -221
  127. package/src/resources/extensions/gsd/mechanical-completion.ts +0 -430
  128. package/src/resources/extensions/gsd/tests/auto-dispatch-loop.test.ts +0 -691
  129. package/src/resources/extensions/gsd/tests/auto-reentrancy-guard.test.ts +0 -127
  130. package/src/resources/extensions/gsd/tests/auto-skip-loop.test.ts +0 -123
  131. package/src/resources/extensions/gsd/tests/dispatch-stall-guard.test.ts +0 -126
  132. package/src/resources/extensions/gsd/tests/loop-regression.test.ts +0 -874
  133. package/src/resources/extensions/gsd/tests/mechanical-completion.test.ts +0 -356
  134. package/src/resources/extensions/gsd/tests/progress-score.test.ts +0 -206
  135. package/src/resources/extensions/gsd/tests/session-lock.test.ts +0 -434
@@ -18,20 +18,33 @@ import type {
18
18
 
19
19
  import { deriveState } from "./state.js";
20
20
  import type { GSDState } from "./types.js";
21
- import { loadFile, getManifestStatus, resolveAllOverrides, parsePlan, parseSummary } from "./files.js";
22
- import { loadPrompt } from "./prompt-loader.js";
23
- import { runVerificationGate, formatFailureContext, captureRuntimeErrors, runDependencyAudit } from "./verification-gate.js";
24
- import { writeVerificationJSON } from "./verification-evidence.js";
21
+ import { getManifestStatus } from "./files.js";
22
+ export { inlinePriorMilestoneSummary } from "./files.js";
25
23
  import { collectSecretsFromManifest } from "../get-secrets-from-user.js";
26
24
  import {
27
- gsdRoot, resolveMilestoneFile, resolveSliceFile, resolveSlicePath,
28
- resolveMilestonePath, resolveDir, resolveTasksDir, resolveTaskFile,
29
- milestonesDir, buildTaskFileName,
25
+ gsdRoot,
26
+ resolveMilestoneFile,
27
+ resolveSliceFile,
28
+ resolveSlicePath,
29
+ resolveMilestonePath,
30
+ resolveDir,
31
+ resolveTasksDir,
32
+ resolveTaskFile,
33
+ milestonesDir,
34
+ buildTaskFileName,
30
35
  } from "./paths.js";
31
36
  import { invalidateAllCaches } from "./cache.js";
32
- import { saveActivityLog, clearActivityLogState } from "./activity-log.js";
33
- import { synthesizeCrashRecovery, getDeepDiagnostic } from "./session-forensics.js";
34
- import { writeLock, clearLock, readCrashLock, formatCrashInfo, isLockProcessAlive } from "./crash-recovery.js";
37
+ import { clearActivityLogState } from "./activity-log.js";
38
+ import {
39
+ synthesizeCrashRecovery,
40
+ getDeepDiagnostic,
41
+ } from "./session-forensics.js";
42
+ import {
43
+ writeLock,
44
+ clearLock,
45
+ readCrashLock,
46
+ isLockProcessAlive,
47
+ } from "./crash-recovery.js";
35
48
  import {
36
49
  acquireSessionLock,
37
50
  validateSessionLock,
@@ -44,7 +57,11 @@ import {
44
57
  readUnitRuntimeRecord,
45
58
  writeUnitRuntimeRecord,
46
59
  } from "./unit-runtime.js";
47
- import { resolveAutoSupervisorConfig, loadEffectiveGSDPreferences, resolveSkillDiscoveryMode, getIsolationMode } from "./preferences.js";
60
+ import {
61
+ resolveAutoSupervisorConfig,
62
+ loadEffectiveGSDPreferences,
63
+ getIsolationMode,
64
+ } from "./preferences.js";
48
65
  import { sendDesktopNotification } from "./notifications.js";
49
66
  import type { GSDPreferences } from "./preferences.js";
50
67
  import {
@@ -69,11 +86,13 @@ import { closeoutUnit } from "./auto-unit-closeout.js";
69
86
  import { recoverTimedOutUnit } from "./auto-timeout-recovery.js";
70
87
  import { selectAndApplyModel } from "./auto-model-selection.js";
71
88
  import {
89
+ syncProjectRootToWorktree,
90
+ syncStateToProjectRoot,
72
91
  readResourceVersion,
73
92
  checkResourcesStale,
74
93
  escapeStaleWorktree,
75
- } from "./resource-version.js";
76
- import { initRoutingHistory, resetRoutingHistory, recordOutcome } from "./routing-history.js";
94
+ } from "./auto-worktree-sync.js";
95
+ import { resetRoutingHistory, recordOutcome } from "./routing-history.js";
77
96
  import {
78
97
  checkPostUnitHooks,
79
98
  getActiveHook,
@@ -85,8 +104,7 @@ import {
85
104
  restoreHookState,
86
105
  clearPersistedHookState,
87
106
  } from "./post-unit-hooks.js";
88
- import { ensureGitignore, untrackRuntimeFiles } from "./gitignore.js";
89
- import { runGSDDoctor, rebuildState, summarizeDoctorIssues } from "./doctor.js";
107
+ import { runGSDDoctor, rebuildState } from "./doctor.js";
90
108
  import {
91
109
  preDispatchHealthGate,
92
110
  recordHealthSnapshot,
@@ -95,20 +113,22 @@ import {
95
113
  formatHealthSummary,
96
114
  getConsecutiveErrorUnits,
97
115
  } from "./doctor-proactive.js";
98
- import { snapshotSkills, clearSkillSnapshot } from "./skill-discovery.js";
99
- import { captureAvailableSkills, getAndClearSkills, resetSkillTelemetry } from "./skill-telemetry.js";
116
+ import { clearSkillSnapshot } from "./skill-discovery.js";
117
+ import {
118
+ captureAvailableSkills,
119
+ resetSkillTelemetry,
120
+ } from "./skill-telemetry.js";
100
121
  import {
101
- initMetrics, resetMetrics, getLedger,
102
- getProjectTotals, formatCost, formatTokenCount,
122
+ initMetrics,
123
+ resetMetrics,
124
+ getLedger,
125
+ getProjectTotals,
126
+ formatCost,
127
+ formatTokenCount,
103
128
  } from "./metrics.js";
104
- import { computeBudgets, resolveExecutorContextWindow } from "./context-budget.js";
105
- import { GSDError, GSD_ARTIFACT_MISSING } from "./errors.js";
106
129
  import { join } from "node:path";
107
- import { sep as pathSep } from "node:path";
108
- import { parseUnitId } from "./unit-id.js";
109
- import { readdirSync, readFileSync, existsSync, mkdirSync, writeFileSync, unlinkSync, statSync } from "node:fs";
130
+ import { readFileSync, existsSync, mkdirSync } from "node:fs";
110
131
  import { atomicWriteSync } from "./atomic-write.js";
111
- import { nativeIsRepo, nativeInit, nativeAddAll, nativeCommit } from "./native-git-bridge.js";
112
132
  import {
113
133
  autoCommitCurrentBranch,
114
134
  captureIntegrationBranch,
@@ -119,9 +139,8 @@ import {
119
139
  parseSliceBranch,
120
140
  setActiveMilestoneId,
121
141
  } from "./worktree.js";
122
- import { createGitService, type TaskCommitContext } from "./git-service.js";
142
+ import { GitServiceImpl } from "./git-service.js";
123
143
  import { getPriorSliceCompletionBlocker } from "./dispatch-guard.js";
124
- import { formatGitError } from "./git-self-heal.js";
125
144
  import {
126
145
  createAutoWorktree,
127
146
  enterAutoWorktree,
@@ -134,24 +153,18 @@ import {
134
153
  syncWorktreeStateBack,
135
154
  } from "./auto-worktree.js";
136
155
  import { pruneQueueOrder } from "./queue-order.js";
137
- import { consumeSignal } from "./session-status-io.js";
138
- import { showNextAction } from "../shared/mod.js";
139
- import { debugLog, debugTime, debugCount, debugPeak, enableDebug, isDebugEnabled, writeDebugSummary, getDebugLogPath } from "./debug-logger.js";
156
+
157
+ import { debugLog, isDebugEnabled, writeDebugSummary } from "./debug-logger.js";
140
158
  import {
141
159
  resolveExpectedArtifactPath,
142
160
  verifyExpectedArtifact,
143
161
  writeBlockerPlaceholder,
144
162
  diagnoseExpectedArtifact,
145
163
  skipExecuteTask,
146
- completedKeysPath,
147
- persistCompletedKey,
148
- removePersistedKey,
149
- loadPersistedKeys,
150
- selfHealRuntimeRecords,
151
164
  buildLoopRemediationSteps,
152
165
  reconcileMergeState,
153
166
  } from "./auto-recovery.js";
154
- import { resolveDispatch, resetRewriteCircuitBreaker } from "./auto-dispatch.js";
167
+ import { resolveDispatch } from "./auto-dispatch.js";
155
168
  import {
156
169
  type AutoDashboardData,
157
170
  updateProgressWidget as _updateProgressWidget,
@@ -170,28 +183,52 @@ import {
170
183
  detectWorkingTreeActivity,
171
184
  } from "./auto-supervisor.js";
172
185
  import { isDbAvailable } from "./gsd-db.js";
173
- import { hasPendingCaptures, loadPendingCaptures, countPendingCaptures } from "./captures.js";
186
+ import { countPendingCaptures } from "./captures.js";
174
187
 
175
188
  // ── Extracted modules ──────────────────────────────────────────────────────
176
- import { startUnitSupervision, type SupervisionContext } from "./auto-timers.js";
177
- import { checkIdempotency, type IdempotencyContext } from "./auto-idempotency.js";
178
- import { checkStuckAndRecover, type StuckContext } from "./auto-stuck-detection.js";
179
- import { runPostUnitVerification, type VerificationContext } from "./auto-verification.js";
180
- import { postUnitPreVerification, postUnitPostVerification, type PostUnitContext } from "./auto-post-unit.js";
189
+ import { startUnitSupervision } from "./auto-timers.js";
190
+ import { runPostUnitVerification } from "./auto-verification.js";
191
+ import {
192
+ postUnitPreVerification,
193
+ postUnitPostVerification,
194
+ } from "./auto-post-unit.js";
181
195
  import { bootstrapAutoSession, type BootstrapDeps } from "./auto-start.js";
196
+ import { autoLoop, resolveAgentEnd, type LoopDeps } from "./auto-loop.js";
197
+ import {
198
+ WorktreeResolver,
199
+ type WorktreeResolverDeps,
200
+ } from "./worktree-resolver.js";
201
+ import { reorderForCaching } from "./prompt-ordering.js";
182
202
 
183
- // Resource staleness, stale worktree escape → resource-version.ts
203
+ // Worktree sync, resource staleness, stale worktree escape → auto-worktree-sync.ts
184
204
 
185
205
  // ─── Session State ─────────────────────────────────────────────────────────
186
206
 
187
207
  import {
188
208
  AutoSession,
189
- MAX_UNIT_DISPATCHES, STUB_RECOVERY_THRESHOLD, MAX_LIFETIME_DISPATCHES,
190
- MAX_CONSECUTIVE_SKIPS, DISPATCH_GAP_TIMEOUT_MS, MAX_SKIP_DEPTH,
191
- NEW_SESSION_TIMEOUT_MS, DISPATCH_HANG_TIMEOUT_MS,
209
+ MAX_UNIT_DISPATCHES,
210
+ STUB_RECOVERY_THRESHOLD,
211
+ MAX_LIFETIME_DISPATCHES,
212
+ NEW_SESSION_TIMEOUT_MS,
213
+ } from "./auto/session.js";
214
+ import type {
215
+ CompletedUnit,
216
+ CurrentUnit,
217
+ UnitRouting,
218
+ StartModel,
219
+ } from "./auto/session.js";
220
+ export {
221
+ MAX_UNIT_DISPATCHES,
222
+ STUB_RECOVERY_THRESHOLD,
223
+ MAX_LIFETIME_DISPATCHES,
224
+ NEW_SESSION_TIMEOUT_MS,
225
+ } from "./auto/session.js";
226
+ export type {
227
+ CompletedUnit,
228
+ CurrentUnit,
229
+ UnitRouting,
230
+ StartModel,
192
231
  } from "./auto/session.js";
193
- import type { CompletedUnit, CurrentUnit, UnitRouting, StartModel, PendingVerificationRetry } from "./auto/session.js";
194
- import { getErrorMessage } from "./error-utils.js";
195
232
 
196
233
  // ── ENCAPSULATION INVARIANT ─────────────────────────────────────────────────
197
234
  // ALL mutable auto-mode state lives in the AutoSession class (auto/session.ts).
@@ -207,7 +244,8 @@ import { getErrorMessage } from "./error-utils.js";
207
244
  // ─────────────────────────────────────────────────────────────────────────────
208
245
  const s = new AutoSession();
209
246
 
210
- import { STATE_REBUILD_MIN_INTERVAL_MS } from "./auto-constants.js";
247
+ /** Throttle STATE.md rebuilds at most once per 30 seconds */
248
+ const STATE_REBUILD_MIN_INTERVAL_MS = 30_000;
211
249
 
212
250
  export function shouldUseWorktreeIsolation(): boolean {
213
251
  const prefs = loadEffectiveGSDPreferences()?.preferences?.git;
@@ -216,7 +254,52 @@ export function shouldUseWorktreeIsolation(): boolean {
216
254
  return true; // default: worktree
217
255
  }
218
256
 
219
- // All mutable state lives in AutoSession (auto/session.ts) see encapsulation invariant above.
257
+ /** Crash recovery prompt set by startAuto, consumed by the main loop */
258
+
259
+ /** Pending verification retry — set when gate fails with retries remaining, consumed by autoLoop */
260
+
261
+ /** Verification retry count per unitId — separate from s.unitDispatchCount which tracks artifact-missing retries */
262
+
263
+ /** Session file path captured at pause — used to synthesize recovery briefing on resume */
264
+
265
+ /** Dashboard tracking */
266
+
267
+ /** Track dynamic routing decision for the current unit (for metrics) */
268
+
269
+ /** Queue of quick-task captures awaiting dispatch after triage resolution */
270
+
271
+ /**
272
+ * Model captured at auto-mode start. Used to prevent model bleed between
273
+ * concurrent GSD instances sharing the same global settings.json (#650).
274
+ * When preferences don't specify a model for a unit type, this ensures
275
+ * the session's original model is re-applied instead of reading from
276
+ * the shared global settings (which another instance may have overwritten).
277
+ */
278
+
279
+ /** Track current milestone to detect transitions */
280
+
281
+ /** Model the user had selected before auto-mode started */
282
+
283
+ /** Progress-aware timeout supervision */
284
+
285
+ /** Context-pressure continue-here monitor — fires once when context usage >= 70% */
286
+
287
+ /** Prompt character measurement for token savings analysis (R051). */
288
+
289
+ /** SIGTERM handler registered while auto-mode is active — cleared on stop/pause. */
290
+
291
+ /**
292
+ * Tool calls currently being executed — prevents false idle detection during long-running tools.
293
+ * Maps toolCallId → start timestamp (ms) so the idle watchdog can detect tools that have been
294
+ * running suspiciously long (e.g., a Bash command hung because `&` kept stdout open).
295
+ */
296
+ // Re-export budget utilities for external consumers
297
+ export {
298
+ getBudgetAlertLevel,
299
+ getNewBudgetAlertLevel,
300
+ getBudgetEnforcementAction,
301
+ } from "./auto-budget.js";
302
+
220
303
  /** Wrapper: register SIGTERM handler and store reference. */
221
304
  function registerSigtermHandler(currentBasePath: string): void {
222
305
  s.sigtermHandler = _registerSigtermHandler(currentBasePath, s.sigtermHandler);
@@ -228,6 +311,8 @@ function deregisterSigtermHandler(): void {
228
311
  s.sigtermHandler = null;
229
312
  }
230
313
 
314
+ export { type AutoDashboardData } from "./auto-dashboard.js";
315
+
231
316
  export function getAutoDashboardData(): AutoDashboardData {
232
317
  const ledger = getLedger();
233
318
  const totals = ledger ? getProjectTotals(ledger.units) : null;
@@ -240,12 +325,15 @@ export function getAutoDashboardData(): AutoDashboardData {
240
325
  } catch {
241
326
  // Non-fatal — captures module may not be loaded
242
327
  }
243
- return { active: s.active, paused: s.paused,
328
+ return {
329
+ active: s.active,
330
+ paused: s.paused,
244
331
  stepMode: s.stepMode,
245
332
  startTime: s.autoStartTime,
246
- elapsed: (s.active || s.paused) ? Date.now() - s.autoStartTime : 0,
333
+ elapsed: s.active || s.paused ? Date.now() - s.autoStartTime : 0,
247
334
  currentUnit: s.currentUnit ? { ...s.currentUnit } : null,
248
- completedUnits: [...s.completedUnits], basePath: s.basePath,
335
+ completedUnits: [...s.completedUnits],
336
+ basePath: s.basePath,
249
337
  totalCost: totals?.cost ?? 0,
250
338
  totalTokens: totals?.tokens.total ?? 0,
251
339
  pendingCaptureCount,
@@ -267,7 +355,10 @@ export function isAutoPaused(): boolean {
267
355
  * Used by error-recovery to fall back to the session's own model
268
356
  * instead of reading (potentially stale) preferences from disk (#1065).
269
357
  */
270
- export function getAutoModeStartModel(): { provider: string; id: string } | null {
358
+ export function getAutoModeStartModel(): {
359
+ provider: string;
360
+ id: string;
361
+ } | null {
271
362
  return s.autoModeStartModel;
272
363
  }
273
364
 
@@ -288,9 +379,11 @@ export function getOldestInFlightToolAgeMs(): number {
288
379
  * Return the base path to use for the auto.lock file.
289
380
  * Always uses the original project root (not the worktree) so that
290
381
  * a second terminal can discover and stop a running auto-mode session.
382
+ *
383
+ * Delegates to AutoSession.lockBasePath — the single source of truth.
291
384
  */
292
385
  function lockBase(): string {
293
- return s.originalBasePath || s.basePath;
386
+ return s.lockBasePath;
294
387
  }
295
388
 
296
389
  /**
@@ -300,7 +393,11 @@ function lockBase(): string {
300
393
  *
301
394
  * Returns true if a remote session was found and signaled, false otherwise.
302
395
  */
303
- export function stopAutoRemote(projectRoot: string): { found: boolean; pid?: number; error?: string } {
396
+ export function stopAutoRemote(projectRoot: string): {
397
+ found: boolean;
398
+ pid?: number;
399
+ error?: string;
400
+ } {
304
401
  const lock = readCrashLock(projectRoot);
305
402
  if (!lock) return { found: false };
306
403
 
@@ -341,19 +438,20 @@ function clearUnitTimeout(): void {
341
438
  s.continueHereHandle = null;
342
439
  }
343
440
  clearInFlightTools();
344
- clearDispatchGapWatchdog();
345
- }
346
-
347
- function clearDispatchGapWatchdog(): void {
348
- if (s.dispatchGapHandle) {
349
- clearTimeout(s.dispatchGapHandle);
350
- s.dispatchGapHandle = null;
351
- }
352
441
  }
353
442
 
354
443
  /** Build snapshot metric opts, enriching with continueHereFired from the runtime record. */
355
- function buildSnapshotOpts(unitType: string, unitId: string): { continueHereFired?: boolean; promptCharCount?: number; baselineCharCount?: number } & Record<string, unknown> {
356
- const runtime = s.currentUnit ? readUnitRuntimeRecord(s.basePath, unitType, unitId) : null;
444
+ function buildSnapshotOpts(
445
+ unitType: string,
446
+ unitId: string,
447
+ ): {
448
+ continueHereFired?: boolean;
449
+ promptCharCount?: number;
450
+ baselineCharCount?: number;
451
+ } & Record<string, unknown> {
452
+ const runtime = s.currentUnit
453
+ ? readUnitRuntimeRecord(s.basePath, unitType, unitId)
454
+ : null;
357
455
  return {
358
456
  promptCharCount: s.lastPromptCharCount,
359
457
  baselineCharCount: s.lastBaselineCharCount,
@@ -362,154 +460,45 @@ function buildSnapshotOpts(unitType: string, unitId: string): { continueHereFire
362
460
  };
363
461
  }
364
462
 
365
- // ─── Extracted Merge Helper ───────────────────────────────────────────────
366
-
367
- /**
368
- * Attempt to merge the current milestone branch to main.
369
- * Handles both worktree and branch isolation modes with a single code path.
370
- * Returns true if merge succeeded, false on error (non-fatal, logged).
371
- *
372
- * Extracted from 4 duplicate merge blocks in dispatchNextUnit to eliminate
373
- * the bug factory where fixing one copy didn't fix the others (#1308).
374
- */
375
- function tryMergeMilestone(ctx: ExtensionContext, milestoneId: string, mode: "transition" | "complete"): boolean {
376
- const isolationMode = getIsolationMode();
377
-
378
- // Worktree merge path
379
- if (isInAutoWorktree(s.basePath) && s.originalBasePath) {
380
- try {
381
- // Sync completion artifacts from worktree → external state before merge (#1412)
382
- try {
383
- const { synced } = syncWorktreeStateBack(s.originalBasePath, s.basePath, milestoneId);
384
- if (synced.length > 0) {
385
- debugLog("worktree-reverse-sync", { milestoneId, synced: synced.length });
386
- }
387
- } catch (syncErr) {
388
- debugLog("worktree-reverse-sync-failed", { milestoneId, error: getErrorMessage(syncErr) });
389
- }
390
-
391
- const roadmapPath = resolveMilestoneFile(s.originalBasePath, milestoneId, "ROADMAP");
392
- if (!roadmapPath) {
393
- teardownAutoWorktree(s.originalBasePath, milestoneId);
394
- ctx.ui.notify(`Exited worktree for ${milestoneId} (no roadmap for merge).`, "info");
395
- return false;
396
- }
397
- const roadmapContent = readFileSync(roadmapPath, "utf-8");
398
- const mergeResult = mergeMilestoneToMain(s.originalBasePath, milestoneId, roadmapContent);
399
- s.basePath = s.originalBasePath;
400
- s.gitService = createGitService(s.basePath);
401
- ctx.ui.notify(
402
- `Milestone ${milestoneId} merged to main.${mergeResult.pushed ? " Pushed to remote." : ""}`,
403
- "info",
404
- );
405
- return true;
406
- } catch (err) {
407
- ctx.ui.notify(
408
- `Milestone merge failed: ${err instanceof Error ? err.message : String(err)}`,
409
- "warning",
410
- );
411
- if (s.originalBasePath) {
412
- s.basePath = s.originalBasePath;
413
- try { process.chdir(s.basePath); } catch { /* best-effort */ }
414
- }
415
- return false;
416
- }
417
- }
418
-
419
- // Branch-mode merge path
420
- if (isolationMode === "branch") {
421
- try {
422
- const currentBranch = getCurrentBranch(s.basePath);
423
- const milestoneBranch = autoWorktreeBranch(milestoneId);
424
- if (currentBranch === milestoneBranch) {
425
- const roadmapPath = resolveMilestoneFile(s.basePath, milestoneId, "ROADMAP");
426
- if (roadmapPath) {
427
- const roadmapContent = readFileSync(roadmapPath, "utf-8");
428
- const mergeResult = mergeMilestoneToMain(s.basePath, milestoneId, roadmapContent);
429
- s.gitService = createGitService(s.basePath);
430
- ctx.ui.notify(
431
- `Milestone ${milestoneId} merged (branch mode).${mergeResult.pushed ? " Pushed to remote." : ""}`,
432
- "info",
433
- );
434
- return true;
435
- }
436
- }
437
- } catch (err) {
438
- ctx.ui.notify(
439
- `Milestone merge failed (branch mode): ${err instanceof Error ? err.message : String(err)}`,
440
- "warning",
441
- );
442
- }
443
- }
444
-
445
- return false;
446
- }
447
-
448
- /**
449
- * Start a watchdog that fires if no new unit is dispatched within DISPATCH_GAP_TIMEOUT_MS
450
- * after handleAgentEnd completes. This catches the case where the dispatch chain silently
451
- * breaks (e.g., unhandled exception in dispatchNextUnit) and auto-mode is left s.active but idle.
452
- *
453
- * The watchdog is cleared on the next successful unit dispatch (clearUnitTimeout is called
454
- * at the start of handleAgentEnd, which calls clearDispatchGapWatchdog).
455
- */
456
- function startDispatchGapWatchdog(ctx: ExtensionContext, pi: ExtensionAPI): void {
457
- clearDispatchGapWatchdog();
458
- s.dispatchGapHandle = setTimeout(async () => {
459
- s.dispatchGapHandle = null;
460
- if (!s.active || !s.cmdCtx) return;
461
-
462
- if (s.verbose) {
463
- ctx.ui.notify(
464
- "Dispatch gap detected — re-evaluating state.",
465
- "info",
466
- );
467
- }
468
-
469
- try {
470
- await dispatchNextUnit(ctx, pi);
471
- } catch (retryErr) {
472
- const message = getErrorMessage(retryErr);
473
- await stopAuto(ctx, pi, `Dispatch gap recovery failed: ${message}`);
474
- return;
475
- }
476
-
477
- if (s.active && !s.unitTimeoutHandle && !s.wrapupWarningHandle) {
478
- await stopAuto(ctx, pi, "Stalled — no dispatchable unit after retry");
479
- }
480
- }, DISPATCH_GAP_TIMEOUT_MS);
463
+ function handleLostSessionLock(ctx?: ExtensionContext): void {
464
+ debugLog("session-lock-lost", { lockBase: lockBase() });
465
+ s.active = false;
466
+ s.paused = false;
467
+ clearUnitTimeout();
468
+ deregisterSigtermHandler();
469
+ ctx?.ui.notify(
470
+ "Session lock lost another GSD process appears to have taken over. Stopping gracefully.",
471
+ "error",
472
+ );
473
+ ctx?.ui.setStatus("gsd-auto", undefined);
474
+ ctx?.ui.setWidget("gsd-progress", undefined);
475
+ ctx?.ui.setFooter(undefined);
481
476
  }
482
477
 
483
- export async function stopAuto(ctx?: ExtensionContext, pi?: ExtensionAPI, reason?: string): Promise<void> {
478
+ export async function stopAuto(
479
+ ctx?: ExtensionContext,
480
+ pi?: ExtensionAPI,
481
+ reason?: string,
482
+ ): Promise<void> {
484
483
  if (!s.active && !s.paused) return;
485
484
  const reasonSuffix = reason ? ` — ${reason}` : "";
486
485
  clearUnitTimeout();
487
- if (lockBase()) {
488
- releaseSessionLock(lockBase());
489
- clearLock(lockBase());
490
- }
486
+ if (lockBase()) clearLock(lockBase());
487
+ if (lockBase()) releaseSessionLock(lockBase());
491
488
  clearSkillSnapshot();
492
489
  resetSkillTelemetry();
493
- s.dispatching = false;
494
- s.skipDepth = 0;
495
490
 
496
491
  // Remove SIGTERM handler registered at auto-mode start
497
492
  deregisterSigtermHandler();
498
493
 
499
494
  // ── Auto-worktree: exit worktree and reset s.basePath on stop ──
500
- if (s.currentMilestoneId && isInAutoWorktree(s.basePath)) {
501
- try {
502
- try { autoCommitCurrentBranch(s.basePath, "stop", s.currentMilestoneId); } catch (e) { debugLog("stop-auto-commit-failed", { error: getErrorMessage(e) }); }
503
- teardownAutoWorktree(s.originalBasePath, s.currentMilestoneId, { preserveBranch: true });
504
- s.basePath = s.originalBasePath;
505
- s.gitService = createGitService(s.basePath);
506
- ctx?.ui.notify("Exited auto-worktree (branch preserved for resume).", "info");
507
- } catch (err) {
508
- ctx?.ui.notify(
509
- `Auto-worktree teardown failed: ${getErrorMessage(err)}`,
510
- "warning",
511
- );
512
- }
495
+ if (s.currentMilestoneId) {
496
+ const notifyCtx = ctx
497
+ ? { notify: ctx.ui.notify.bind(ctx.ui) }
498
+ : { notify: () => {} };
499
+ buildResolver().exitMilestone(s.currentMilestoneId, notifyCtx, {
500
+ preserveBranch: true,
501
+ });
513
502
  }
514
503
 
515
504
  // ── DB cleanup: close the SQLite connection ──
@@ -517,12 +506,20 @@ export async function stopAuto(ctx?: ExtensionContext, pi?: ExtensionAPI, reason
517
506
  try {
518
507
  const { closeDatabase } = await import("./gsd-db.js");
519
508
  closeDatabase();
520
- } catch (e) { debugLog("db-close-failed", { error: getErrorMessage(e) }); }
509
+ } catch (e) {
510
+ debugLog("db-close-failed", {
511
+ error: e instanceof Error ? e.message : String(e),
512
+ });
513
+ }
521
514
  }
522
515
 
523
516
  if (s.originalBasePath) {
524
517
  s.basePath = s.originalBasePath;
525
- try { process.chdir(s.basePath); } catch { /* best-effort */ }
518
+ try {
519
+ process.chdir(s.basePath);
520
+ } catch {
521
+ /* best-effort */
522
+ }
526
523
  }
527
524
 
528
525
  const ledger = getLedger();
@@ -537,7 +534,13 @@ export async function stopAuto(ctx?: ExtensionContext, pi?: ExtensionAPI, reason
537
534
  }
538
535
 
539
536
  if (s.basePath) {
540
- try { await rebuildState(s.basePath); } catch (e) { debugLog("stop-rebuild-state-failed", { error: getErrorMessage(e) }); }
537
+ try {
538
+ await rebuildState(s.basePath);
539
+ } catch (e) {
540
+ debugLog("stop-rebuild-state-failed", {
541
+ error: e instanceof Error ? e.message : String(e),
542
+ });
543
+ }
541
544
  }
542
545
 
543
546
  if (isDebugEnabled()) {
@@ -556,7 +559,6 @@ export async function stopAuto(ctx?: ExtensionContext, pi?: ExtensionAPI, reason
556
559
  s.stepMode = false;
557
560
  s.unitDispatchCount.clear();
558
561
  s.unitRecoveryCount.clear();
559
- s.unitConsecutiveSkips.clear();
560
562
  clearInFlightTools();
561
563
  s.lastBudgetAlertLevel = 0;
562
564
  s.lastStateRebuildAt = 0;
@@ -570,18 +572,19 @@ export async function stopAuto(ctx?: ExtensionContext, pi?: ExtensionAPI, reason
570
572
  clearSliceProgressCache();
571
573
  clearActivityLogState();
572
574
  resetProactiveHealing();
573
- s.recentlyEvictedKeys.clear();
574
575
  s.pendingCrashRecovery = null;
575
576
  s.pendingVerificationRetry = null;
576
577
  s.verificationRetryCount.clear();
577
578
  s.pausedSessionFile = null;
578
- s.handlingAgentEnd = false;
579
579
  ctx?.ui.setStatus("gsd-auto", undefined);
580
580
  ctx?.ui.setWidget("gsd-progress", undefined);
581
581
  ctx?.ui.setFooter(undefined);
582
582
 
583
583
  if (pi && ctx && s.originalModelId && s.originalModelProvider) {
584
- const original = ctx.modelRegistry.find(s.originalModelProvider, s.originalModelId);
584
+ const original = ctx.modelRegistry.find(
585
+ s.originalModelProvider,
586
+ s.originalModelId,
587
+ );
585
588
  if (original) await pi.setModel(original);
586
589
  s.originalModelId = null;
587
590
  s.originalModelProvider = null;
@@ -595,16 +598,17 @@ export async function stopAuto(ctx?: ExtensionContext, pi?: ExtensionAPI, reason
595
598
  * The user can interact with the agent, then `/gsd auto` resumes
596
599
  * from disk state. Called when the user presses Escape during auto-mode.
597
600
  */
598
- export async function pauseAuto(ctx?: ExtensionContext, _pi?: ExtensionAPI): Promise<void> {
601
+ export async function pauseAuto(
602
+ ctx?: ExtensionContext,
603
+ _pi?: ExtensionAPI,
604
+ ): Promise<void> {
599
605
  if (!s.active) return;
600
606
  clearUnitTimeout();
601
607
 
602
608
  s.pausedSessionFile = ctx?.sessionManager?.getSessionFile() ?? null;
603
609
 
604
- if (lockBase()) {
605
- releaseSessionLock(lockBase());
606
- clearLock(lockBase());
607
- }
610
+ if (lockBase()) clearLock(lockBase());
611
+ if (lockBase()) releaseSessionLock(lockBase());
608
612
 
609
613
  deregisterSigtermHandler();
610
614
 
@@ -622,6 +626,158 @@ export async function pauseAuto(ctx?: ExtensionContext, _pi?: ExtensionAPI): Pro
622
626
  );
623
627
  }
624
628
 
629
+ /**
630
+ * Build a WorktreeResolverDeps from auto.ts private scope.
631
+ * Shared by buildResolver() and buildLoopDeps().
632
+ */
633
+ function buildResolverDeps(): WorktreeResolverDeps {
634
+ return {
635
+ isInAutoWorktree,
636
+ shouldUseWorktreeIsolation,
637
+ getIsolationMode,
638
+ mergeMilestoneToMain,
639
+ syncWorktreeStateBack,
640
+ teardownAutoWorktree,
641
+ createAutoWorktree,
642
+ enterAutoWorktree,
643
+ getAutoWorktreePath,
644
+ autoCommitCurrentBranch,
645
+ getCurrentBranch,
646
+ autoWorktreeBranch,
647
+ resolveMilestoneFile,
648
+ readFileSync: (path: string, encoding: string) =>
649
+ readFileSync(path, encoding as BufferEncoding),
650
+ GitServiceImpl:
651
+ GitServiceImpl as unknown as WorktreeResolverDeps["GitServiceImpl"],
652
+ loadEffectiveGSDPreferences:
653
+ loadEffectiveGSDPreferences as unknown as WorktreeResolverDeps["loadEffectiveGSDPreferences"],
654
+ invalidateAllCaches,
655
+ captureIntegrationBranch,
656
+ };
657
+ }
658
+
659
+ /**
660
+ * Build a WorktreeResolver wrapping the current session.
661
+ * Cheap to construct — it's just a thin wrapper over `s` + deps.
662
+ * Used by stopAuto(), resume path, and buildLoopDeps().
663
+ */
664
+ function buildResolver(): WorktreeResolver {
665
+ return new WorktreeResolver(s, buildResolverDeps());
666
+ }
667
+
668
+ /**
669
+ * Build the LoopDeps object from auto.ts private scope.
670
+ * This bundles all private functions that autoLoop needs without exporting them.
671
+ */
672
+ function buildLoopDeps(): LoopDeps {
673
+ return {
674
+ lockBase,
675
+ buildSnapshotOpts,
676
+ stopAuto,
677
+ pauseAuto,
678
+ clearUnitTimeout,
679
+ updateProgressWidget,
680
+
681
+ // State and cache
682
+ invalidateAllCaches,
683
+ deriveState,
684
+ loadEffectiveGSDPreferences,
685
+
686
+ // Pre-dispatch health gate
687
+ preDispatchHealthGate,
688
+
689
+ // Worktree sync
690
+ syncProjectRootToWorktree,
691
+
692
+ // Resource version guard
693
+ checkResourcesStale,
694
+
695
+ // Session lock
696
+ validateSessionLock,
697
+ updateSessionLock,
698
+ handleLostSessionLock,
699
+
700
+ // Milestone transition
701
+ sendDesktopNotification,
702
+ setActiveMilestoneId,
703
+ pruneQueueOrder,
704
+ isInAutoWorktree,
705
+ shouldUseWorktreeIsolation,
706
+ mergeMilestoneToMain,
707
+ teardownAutoWorktree,
708
+ createAutoWorktree,
709
+ captureIntegrationBranch,
710
+ getIsolationMode,
711
+ getCurrentBranch,
712
+ autoWorktreeBranch,
713
+ resolveMilestoneFile,
714
+ reconcileMergeState,
715
+
716
+ // Budget/context/secrets
717
+ getLedger,
718
+ getProjectTotals,
719
+ formatCost,
720
+ getBudgetAlertLevel,
721
+ getNewBudgetAlertLevel,
722
+ getBudgetEnforcementAction,
723
+ getManifestStatus,
724
+ collectSecretsFromManifest,
725
+
726
+ // Dispatch
727
+ resolveDispatch,
728
+ runPreDispatchHooks,
729
+ getPriorSliceCompletionBlocker,
730
+ getMainBranch,
731
+ collectObservabilityWarnings: _collectObservabilityWarnings,
732
+ buildObservabilityRepairBlock,
733
+
734
+ // Unit closeout + runtime records
735
+ closeoutUnit,
736
+ verifyExpectedArtifact,
737
+ clearUnitRuntimeRecord,
738
+ writeUnitRuntimeRecord,
739
+ recordOutcome,
740
+ writeLock,
741
+ captureAvailableSkills,
742
+ ensurePreconditions,
743
+ updateSliceProgressCache,
744
+
745
+ // Model selection + supervision
746
+ selectAndApplyModel,
747
+ startUnitSupervision,
748
+
749
+ // Prompt helpers
750
+ getDeepDiagnostic,
751
+ isDbAvailable,
752
+ reorderForCaching,
753
+
754
+ // Filesystem
755
+ existsSync,
756
+ readFileSync: (path: string, encoding: string) =>
757
+ readFileSync(path, encoding as BufferEncoding),
758
+ atomicWriteSync,
759
+
760
+ // Git
761
+ GitServiceImpl: GitServiceImpl as unknown as LoopDeps["GitServiceImpl"],
762
+
763
+ // WorktreeResolver
764
+ resolver: buildResolver(),
765
+
766
+ // Post-unit processing
767
+ postUnitPreVerification,
768
+ runPostUnitVerification,
769
+ postUnitPostVerification,
770
+
771
+ // Session manager
772
+ getSessionFile: (ctx: ExtensionContext) => {
773
+ try {
774
+ return ctx.sessionManager?.getSessionFile() ?? "";
775
+ } catch {
776
+ return "";
777
+ }
778
+ },
779
+ } as unknown as LoopDeps;
780
+ }
625
781
 
626
782
  export async function startAuto(
627
783
  ctx: ExtensionCommandContext,
@@ -637,13 +793,9 @@ export async function startAuto(
637
793
 
638
794
  // If resuming from paused state, just re-activate and dispatch next unit.
639
795
  if (s.paused) {
640
- // Re-acquire session lock before resuming
641
796
  const resumeLock = acquireSessionLock(base);
642
797
  if (!resumeLock.acquired) {
643
- ctx.ui.notify(
644
- `Cannot resume: ${resumeLock.reason}`,
645
- "error",
646
- );
798
+ ctx.ui.notify(`Cannot resume: ${resumeLock.reason}`, "error");
647
799
  return;
648
800
  }
649
801
 
@@ -655,47 +807,52 @@ export async function startAuto(
655
807
  s.basePath = base;
656
808
  s.unitDispatchCount.clear();
657
809
  s.unitLifetimeDispatches.clear();
658
- s.unitConsecutiveSkips.clear();
659
810
  if (!getLedger()) initMetrics(base);
660
811
  if (s.currentMilestoneId) setActiveMilestoneId(base, s.currentMilestoneId);
661
812
 
662
813
  // ── Auto-worktree: re-enter worktree on resume ──
663
- if (s.currentMilestoneId && shouldUseWorktreeIsolation() && s.originalBasePath && !isInAutoWorktree(s.basePath) && !detectWorktreeName(s.basePath) && !detectWorktreeName(s.originalBasePath)) {
664
- try {
665
- const existingWtPath = getAutoWorktreePath(s.originalBasePath, s.currentMilestoneId);
666
- if (existingWtPath) {
667
- const wtPath = enterAutoWorktree(s.originalBasePath, s.currentMilestoneId);
668
- s.basePath = wtPath;
669
- s.gitService = createGitService(s.basePath);
670
- ctx.ui.notify(`Re-entered auto-worktree at ${wtPath}`, "info");
671
- } else {
672
- const wtPath = createAutoWorktree(s.originalBasePath, s.currentMilestoneId);
673
- s.basePath = wtPath;
674
- s.gitService = createGitService(s.basePath);
675
- ctx.ui.notify(`Recreated auto-worktree at ${wtPath}`, "info");
676
- }
677
- } catch (err) {
678
- ctx.ui.notify(
679
- `Auto-worktree re-entry failed: ${getErrorMessage(err)}. Continuing at current path.`,
680
- "warning",
681
- );
682
- }
814
+ if (
815
+ s.currentMilestoneId &&
816
+ shouldUseWorktreeIsolation() &&
817
+ s.originalBasePath &&
818
+ !isInAutoWorktree(s.basePath) &&
819
+ !detectWorktreeName(s.basePath) &&
820
+ !detectWorktreeName(s.originalBasePath)
821
+ ) {
822
+ buildResolver().enterMilestone(s.currentMilestoneId, {
823
+ notify: ctx.ui.notify.bind(ctx.ui),
824
+ });
683
825
  }
684
826
 
685
827
  registerSigtermHandler(lockBase());
686
828
 
687
829
  ctx.ui.setStatus("gsd-auto", s.stepMode ? "next" : "auto");
688
830
  ctx.ui.setFooter(hideFooter);
689
- ctx.ui.notify(s.stepMode ? "Step-mode resumed." : "Auto-mode resumed.", "info");
831
+ ctx.ui.notify(
832
+ s.stepMode ? "Step-mode resumed." : "Auto-mode resumed.",
833
+ "info",
834
+ );
690
835
  restoreHookState(s.basePath);
691
- try { await rebuildState(s.basePath); } catch (e) { debugLog("resume-rebuild-state-failed", { error: getErrorMessage(e) }); }
836
+ try {
837
+ await rebuildState(s.basePath);
838
+ } catch (e) {
839
+ debugLog("resume-rebuild-state-failed", {
840
+ error: e instanceof Error ? e.message : String(e),
841
+ });
842
+ }
692
843
  try {
693
844
  const report = await runGSDDoctor(s.basePath, { fix: true });
694
845
  if (report.fixesApplied.length > 0) {
695
- ctx.ui.notify(`Resume: applied ${report.fixesApplied.length} fix(es) to state.`, "info");
846
+ ctx.ui.notify(
847
+ `Resume: applied ${report.fixesApplied.length} fix(es) to state.`,
848
+ "info",
849
+ );
696
850
  }
697
- } catch (e) { debugLog("resume-doctor-failed", { error: getErrorMessage(e) }); }
698
- await selfHealRuntimeRecords(s.basePath, ctx, s.completedKeySet);
851
+ } catch (e) {
852
+ debugLog("resume-doctor-failed", {
853
+ error: e instanceof Error ? e.message : String(e),
854
+ });
855
+ }
699
856
  invalidateAllCaches();
700
857
 
701
858
  if (s.pausedSessionFile) {
@@ -703,7 +860,8 @@ export async function startAuto(
703
860
  const recovery = synthesizeCrashRecovery(
704
861
  s.basePath,
705
862
  s.currentUnit?.type ?? "unknown",
706
- s.currentUnit?.id ?? "unknown", s.pausedSessionFile ?? undefined,
863
+ s.currentUnit?.id ?? "unknown",
864
+ s.pausedSessionFile ?? undefined,
707
865
  activityDir,
708
866
  );
709
867
  if (recovery && recovery.trace.toolCallCount > 0) {
@@ -716,42 +874,20 @@ export async function startAuto(
716
874
  s.pausedSessionFile = null;
717
875
  }
718
876
 
719
- // If resuming from a secrets pause, re-collect before dispatching (#1146)
720
- if (s.pausedForSecrets && s.currentMilestoneId) {
721
- try {
722
- const manifestStatus = await getManifestStatus(s.basePath, s.currentMilestoneId);
723
- if (manifestStatus && manifestStatus.pending.length > 0) {
724
- const result = await collectSecretsFromManifest(s.basePath, s.currentMilestoneId, ctx);
725
- if (result && result.applied.length > 0) {
726
- ctx.ui.notify(
727
- `Secrets collected: ${result.applied.length} applied, ${result.skipped.length} skipped, ${result.existingSkipped.length} already set.`,
728
- "info",
729
- );
730
- } else if (result && result.applied.length === 0 && result.skipped.length > 0) {
731
- // All keys were skipped — still pending, re-pause
732
- s.paused = true;
733
- s.active = false;
734
- ctx.ui.notify(
735
- `All env variables were skipped. Auto-mode remains paused.\nCollect them with /gsd secrets, then resume with /gsd auto.`,
736
- "warning",
737
- );
738
- ctx.ui.setStatus("gsd-auto", "paused");
739
- return;
740
- }
741
- }
742
- } catch (err) {
743
- ctx.ui.notify(
744
- `Secrets check error: ${getErrorMessage(err)}. Continuing without secrets.`,
745
- "warning",
746
- );
747
- }
748
- s.pausedForSecrets = false;
749
- }
750
-
751
- updateSessionLock(lockBase(), "resuming", s.currentMilestoneId ?? "unknown", s.completedUnits.length);
752
- writeLock(lockBase(), "resuming", s.currentMilestoneId ?? "unknown", s.completedUnits.length);
877
+ updateSessionLock(
878
+ lockBase(),
879
+ "resuming",
880
+ s.currentMilestoneId ?? "unknown",
881
+ s.completedUnits.length,
882
+ );
883
+ writeLock(
884
+ lockBase(),
885
+ "resuming",
886
+ s.currentMilestoneId ?? "unknown",
887
+ s.completedUnits.length,
888
+ );
753
889
 
754
- await dispatchNextUnit(ctx, pi);
890
+ await autoLoop(ctx, pi, s, buildLoopDeps());
755
891
  return;
756
892
  }
757
893
 
@@ -760,210 +896,45 @@ export async function startAuto(
760
896
  shouldUseWorktreeIsolation,
761
897
  registerSigtermHandler,
762
898
  lockBase,
899
+ buildResolver,
763
900
  };
764
901
 
765
- const ready = await bootstrapAutoSession(s, ctx, pi, base, verboseMode, requestedStepMode, bootstrapDeps);
766
- if (!ready) return;
767
-
768
- // Dispatch the first unit
769
- await dispatchNextUnit(ctx, pi);
770
- }
771
-
772
- // ─── Agent End Handler ────────────────────────────────────────────────────────
773
-
774
- /** Guard against concurrent handleAgentEnd execution. */
775
-
776
- export async function handleAgentEnd(
777
- ctx: ExtensionContext,
778
- pi: ExtensionAPI,
779
- ): Promise<void> {
780
- if (!s.active || !s.cmdCtx) return;
781
- if (s.handlingAgentEnd) {
782
- // Another agent_end arrived while we're still processing the previous one.
783
- // This happens when a unit dispatched inside handleAgentEnd (e.g. via hooks,
784
- // triage, or quick-task early-dispatch paths) completes before the outer
785
- // handleAgentEnd returns. Queue a retry so the completed unit's agent_end
786
- // is not silently dropped (#1072).
787
- s.pendingAgentEndRetry = true;
788
- return;
789
- }
790
- s.handlingAgentEnd = true;
791
-
792
- try {
793
-
794
- // Unit completed — clear its timeout
795
- clearUnitTimeout();
796
-
797
- // ── Pre-verification processing (commit, doctor, state rebuild, etc.) ──
798
- const postUnitCtx: PostUnitContext = {
902
+ const ready = await bootstrapAutoSession(
799
903
  s,
800
904
  ctx,
801
905
  pi,
802
- buildSnapshotOpts,
803
- lockBase,
804
- stopAuto,
805
- pauseAuto,
806
- updateProgressWidget,
807
- };
808
-
809
- const preResult = await postUnitPreVerification(postUnitCtx);
810
- if (preResult === "dispatched") return;
811
-
812
- // ── Verification gate: run typecheck/lint/test after execute-task ──
813
- const verificationResult = await runPostUnitVerification(
814
- { s, ctx, pi },
815
- dispatchNextUnit,
816
- startDispatchGapWatchdog,
817
- pauseAuto,
906
+ base,
907
+ verboseMode,
908
+ requestedStepMode,
909
+ bootstrapDeps,
818
910
  );
819
- if (verificationResult === "retry" || verificationResult === "pause") return;
820
-
821
- // ── Post-verification processing (DB dual-write, hooks, triage, quick-tasks) ──
822
- const postResult = await postUnitPostVerification(postUnitCtx);
823
- if (postResult === "dispatched" || postResult === "stopped") return;
824
- if (postResult === "step-wizard") {
825
- await showStepWizard(ctx, pi);
826
- return;
827
- }
828
-
829
- // ── Dispatch with hang detection (#1073) ────────────────────────────────
830
- // Start a safety watchdog BEFORE calling dispatchNextUnit. If dispatch
831
- // hangs at any await (newSession, model selection, etc.), the gap watchdog
832
- // inside handleAgentEnd never fires because we never reach the check.
833
- // This pre-dispatch watchdog ensures recovery even when dispatchNextUnit
834
- // itself is permanently blocked.
835
- const dispatchHangGuard = setTimeout(() => {
836
- if (!s.active) return;
837
- // dispatchNextUnit has been running for too long — it's likely hung.
838
- // Start the gap watchdog which will retry dispatch from scratch.
839
- if (!s.unitTimeoutHandle && !s.wrapupWarningHandle) {
840
- ctx.ui.notify(
841
- `Dispatch hang detected (${DISPATCH_HANG_TIMEOUT_MS / 1000}s without completion). Starting recovery watchdog.`,
842
- "warning",
843
- );
844
- startDispatchGapWatchdog(ctx, pi);
845
- }
846
- }, DISPATCH_HANG_TIMEOUT_MS);
847
-
848
- try {
849
- await dispatchNextUnit(ctx, pi);
850
- } catch (dispatchErr) {
851
- const message = getErrorMessage(dispatchErr);
852
- ctx.ui.notify(
853
- `Dispatch error after unit completion: ${message}. Retrying in ${DISPATCH_GAP_TIMEOUT_MS / 1000}s.`,
854
- "error",
855
- );
856
- startDispatchGapWatchdog(ctx, pi);
857
- return;
858
- } finally {
859
- clearTimeout(dispatchHangGuard);
860
- }
861
-
862
- if (s.active && !s.unitTimeoutHandle && !s.wrapupWarningHandle) {
863
- startDispatchGapWatchdog(ctx, pi);
864
- }
911
+ if (!ready) return;
865
912
 
866
- } finally {
867
- s.handlingAgentEnd = false;
868
-
869
- // If an agent_end event was dropped by the reentrancy guard while we were
870
- // processing, re-enter handleAgentEnd on the next microtask. This prevents
871
- // the summarizing phase stall (#1072) where a unit dispatched inside
872
- // handleAgentEnd (hooks, triage, quick-task) completes before we return,
873
- // and its agent_end is silently dropped — leaving auto-mode active but
874
- // permanently stalled with no unit running and no watchdog set.
875
- if (s.pendingAgentEndRetry) {
876
- s.pendingAgentEndRetry = false;
877
- // Clear gap watchdog from the previous cycle to prevent concurrent
878
- // dispatch when the deferred handleAgentEnd calls dispatchNextUnit (#1272).
879
- clearDispatchGapWatchdog();
880
- setImmediate(() => {
881
- handleAgentEnd(ctx, pi).catch((err) => {
882
- const msg = getErrorMessage(err);
883
- ctx.ui.notify(`Deferred agent_end retry failed: ${msg}`, "error");
884
- pauseAuto(ctx, pi).catch(() => {});
885
- });
886
- });
887
- }
888
- }
913
+ // Dispatch the first unit
914
+ await autoLoop(ctx, pi, s, buildLoopDeps());
889
915
  }
890
916
 
891
- // ─── Step Mode Wizard ─────────────────────────────────────────────────────
917
+ // ─── Agent End Handler ────────────────────────────────────────────────────────
892
918
 
893
919
  /**
894
- * Show the step-mode wizard after a unit completes.
920
+ * Deprecated thin wrapper kept as export for backward compatibility.
921
+ * The actual agent_end processing now happens via resolveAgentEnd() in auto-loop.ts,
922
+ * which is called directly from index.ts. The autoLoop() while loop handles all
923
+ * post-unit processing (verification, hooks, dispatch) that this function used to do.
924
+ *
925
+ * If called by straggler code, it simply resolves the pending promise so the loop
926
+ * can continue.
895
927
  */
896
- async function showStepWizard(
928
+ export async function handleAgentEnd(
897
929
  ctx: ExtensionContext,
898
930
  pi: ExtensionAPI,
899
931
  ): Promise<void> {
900
- if (!s.cmdCtx) return;
901
-
902
- const state = await deriveState(s.basePath);
903
- const mid = state.activeMilestone?.id;
904
-
905
- const justFinished = s.currentUnit
906
- ? `${unitVerb(s.currentUnit.type)} ${s.currentUnit.id}`
907
- : "previous unit";
908
-
909
- if (!mid || state.phase === "complete") {
910
- const incomplete = (state.registry ?? []).filter(m => m.status !== "complete" && m.status !== "parked");
911
- if (incomplete.length > 0 && state.phase !== "complete" && state.phase !== "blocked" && state.phase !== "pre-planning") {
912
- const ids = incomplete.map(m => m.id).join(", ");
913
- const diag = `basePath=${s.basePath}, milestones=[${state.registry.map(m => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
914
- ctx.ui.notify(`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`, "error");
915
- await stopAuto(ctx, pi, `No active milestone — ${incomplete.length} incomplete (${ids})`);
916
- } else {
917
- await stopAuto(ctx, pi, state.phase === "complete" ? "All work complete" : "No active milestone");
918
- }
919
- return;
920
- }
921
-
922
- const nextDesc = _describeNextUnit(state);
923
-
924
- const choice = await showNextAction(s.cmdCtx, {
925
- title: `GSD — ${justFinished} complete`,
926
- summary: [
927
- `${mid}: ${state.activeMilestone?.title ?? mid}`,
928
- ...(state.activeSlice ? [`${state.activeSlice.id}: ${state.activeSlice.title}`] : []),
929
- ],
930
- actions: [
931
- {
932
- id: "continue",
933
- label: nextDesc.label,
934
- description: nextDesc.description,
935
- recommended: true,
936
- },
937
- {
938
- id: "auto",
939
- label: "Switch to auto",
940
- description: "Continue without pausing between steps.",
941
- },
942
- {
943
- id: "status",
944
- label: "View status",
945
- description: "Open the dashboard.",
946
- },
947
- ],
948
- notYetMessage: "Run /gsd next when ready to continue.",
949
- });
950
-
951
- if (choice === "continue") {
952
- await dispatchNextUnit(ctx, pi);
953
- } else if (choice === "auto") {
954
- s.stepMode = false;
955
- ctx.ui.setStatus("gsd-auto", "auto");
956
- ctx.ui.notify("Switched to auto-mode.", "info");
957
- await dispatchNextUnit(ctx, pi);
958
- } else if (choice === "status") {
959
- const { fireStatusViaCommand } = await import("./commands.js");
960
- await fireStatusViaCommand(ctx as ExtensionCommandContext);
961
- await showStepWizard(ctx, pi);
962
- } else {
963
- await pauseAuto(ctx, pi);
964
- }
932
+ if (!s.active || !s.cmdCtx) return;
933
+ clearUnitTimeout();
934
+ resolveAgentEnd({ messages: [] });
965
935
  }
966
-
936
+ // describeNextUnit is imported from auto-dashboard.ts and re-exported
937
+ export { describeNextUnit } from "./auto-dashboard.js";
967
938
 
968
939
  /** Thin wrapper: delegates to auto-dashboard.ts, passing state accessors. */
969
940
  function updateProgressWidget(
@@ -973,9 +944,17 @@ function updateProgressWidget(
973
944
  state: GSDState,
974
945
  ): void {
975
946
  const badge = s.currentUnitRouting?.tier
976
- ? ({ light: "L", standard: "S", heavy: "H" }[s.currentUnitRouting.tier] ?? undefined)
947
+ ? ({ light: "L", standard: "S", heavy: "H" }[s.currentUnitRouting.tier] ??
948
+ undefined)
977
949
  : undefined;
978
- _updateProgressWidget(ctx, unitType, unitId, state, widgetStateAccessors, badge);
950
+ _updateProgressWidget(
951
+ ctx,
952
+ unitType,
953
+ unitId,
954
+ state,
955
+ widgetStateAccessors,
956
+ badge,
957
+ );
979
958
  }
980
959
 
981
960
  /** State accessors for the widget — closures over module globals. */
@@ -987,695 +966,6 @@ const widgetStateAccessors: WidgetStateAccessors = {
987
966
  isVerbose: () => s.verbose,
988
967
  };
989
968
 
990
- // ─── Core Loop ────────────────────────────────────────────────────────────────
991
-
992
- async function dispatchNextUnit(
993
- ctx: ExtensionContext,
994
- pi: ExtensionAPI,
995
- ): Promise<void> {
996
- if (!s.active || !s.cmdCtx) {
997
- debugLog(`dispatchNextUnit early return — active=${s.active}, cmdCtx=${!!s.cmdCtx}`);
998
- if (s.active && !s.cmdCtx) {
999
- ctx.ui.notify("Auto-mode session expired. Run /gsd auto to restart.", "info");
1000
- }
1001
- return;
1002
- }
1003
-
1004
- // ── Session lock validation: detect if another process has taken over ──
1005
- if (lockBase() && !validateSessionLock(lockBase())) {
1006
- debugLog("dispatchNextUnit session-lock-lost — another process may have taken over");
1007
- ctx.ui.notify(
1008
- "Session lock lost — another GSD process appears to have taken over. Stopping gracefully.",
1009
- "error",
1010
- );
1011
- // Don't call stopAuto here to avoid releasing the lock we don't own
1012
- s.active = false;
1013
- s.paused = false;
1014
- clearUnitTimeout();
1015
- deregisterSigtermHandler();
1016
- ctx.ui.setStatus("gsd-auto", undefined);
1017
- ctx.ui.setWidget("gsd-progress", undefined);
1018
- ctx.ui.setFooter(undefined);
1019
- return;
1020
- }
1021
-
1022
- // Reentrancy guard — unconditional to prevent concurrent dispatch from
1023
- // gap watchdog or pendingAgentEndRetry during skip chains (#1272).
1024
- // Previously the guard was bypassed when skipDepth > 0, but the recursive
1025
- // skip chain's inner finally block resets s.dispatching = false before the
1026
- // outer call's finally runs, opening a window for concurrent entry.
1027
- if (s.dispatching) {
1028
- debugLog("dispatchNextUnit reentrancy guard — another dispatch in progress, bailing");
1029
- return;
1030
- }
1031
- s.dispatching = true;
1032
- try {
1033
- // Recursion depth guard
1034
- if (s.skipDepth > MAX_SKIP_DEPTH) {
1035
- s.skipDepth = 0;
1036
- ctx.ui.notify(`Skipped ${MAX_SKIP_DEPTH}+ completed units. Yielding to UI before continuing.`, "info");
1037
- await new Promise(r => setTimeout(r, 200));
1038
- }
1039
-
1040
- // Resource version guard
1041
- const staleMsg = checkResourcesStale(s.resourceVersionOnStart);
1042
- if (staleMsg) {
1043
- await stopAuto(ctx, pi, staleMsg);
1044
- return;
1045
- }
1046
-
1047
- invalidateAllCaches();
1048
- s.lastPromptCharCount = undefined;
1049
- s.lastBaselineCharCount = undefined;
1050
-
1051
- // ── Pre-dispatch health gate ──
1052
- try {
1053
- const healthGate = await preDispatchHealthGate(s.basePath);
1054
- if (healthGate.fixesApplied.length > 0) {
1055
- ctx.ui.notify(`Pre-dispatch: ${healthGate.fixesApplied.join(", ")}`, "info");
1056
- }
1057
- if (!healthGate.proceed) {
1058
- ctx.ui.notify(healthGate.reason ?? "Pre-dispatch health check failed.", "error");
1059
- await pauseAuto(ctx, pi);
1060
- return;
1061
- }
1062
- } catch {
1063
- // Non-fatal
1064
- }
1065
-
1066
- const stopDeriveTimer = debugTime("derive-state");
1067
- let state = await deriveState(s.basePath);
1068
- stopDeriveTimer({
1069
- phase: state.phase,
1070
- milestone: state.activeMilestone?.id,
1071
- slice: state.activeSlice?.id,
1072
- task: state.activeTask?.id,
1073
- });
1074
- let mid = state.activeMilestone?.id;
1075
- let midTitle = state.activeMilestone?.title;
1076
-
1077
- // Detect milestone transition
1078
- if (mid && s.currentMilestoneId && mid !== s.currentMilestoneId) {
1079
- ctx.ui.notify(
1080
- `Milestone ${ s.currentMilestoneId } complete. Advancing to ${mid}: ${midTitle}.`,
1081
- "info",
1082
- );
1083
- sendDesktopNotification("GSD", `Milestone ${s.currentMilestoneId} complete!`, "success", "milestone");
1084
- const vizPrefs = loadEffectiveGSDPreferences()?.preferences;
1085
- if (vizPrefs?.auto_visualize) {
1086
- ctx.ui.notify("Run /gsd visualize to see progress overview.", "info");
1087
- }
1088
- if (vizPrefs?.auto_report !== false) {
1089
- try {
1090
- const { loadVisualizerData } = await import("./visualizer-data.js");
1091
- const { generateHtmlReport } = await import("./export-html.js");
1092
- const { writeReportSnapshot, reportsDir } = await import("./reports.js");
1093
- const { basename } = await import("node:path");
1094
- const snapData = await loadVisualizerData(s.basePath);
1095
- const completedMs = snapData.milestones.find(m => m.id === s.currentMilestoneId);
1096
- const msTitle = completedMs?.title ?? s.currentMilestoneId;
1097
- const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
1098
- const projName = basename(s.basePath);
1099
- const doneSlices = snapData.milestones.reduce((s, m) => s + m.slices.filter(sl => sl.done).length, 0);
1100
- const totalSlices = snapData.milestones.reduce((s, m) => s + m.slices.length, 0);
1101
- const outPath = writeReportSnapshot({ basePath: s.basePath,
1102
- html: generateHtmlReport(snapData, {
1103
- projectName: projName,
1104
- projectPath: s.basePath,
1105
- gsdVersion,
1106
- milestoneId: s.currentMilestoneId,
1107
- indexRelPath: "index.html",
1108
- }),
1109
- milestoneId: s.currentMilestoneId,
1110
- milestoneTitle: msTitle,
1111
- kind: "milestone",
1112
- projectName: projName,
1113
- projectPath: s.basePath,
1114
- gsdVersion,
1115
- totalCost: snapData.totals?.cost ?? 0,
1116
- totalTokens: snapData.totals?.tokens.total ?? 0,
1117
- totalDuration: snapData.totals?.duration ?? 0,
1118
- doneSlices,
1119
- totalSlices,
1120
- doneMilestones: snapData.milestones.filter(m => m.status === "complete").length,
1121
- totalMilestones: snapData.milestones.length,
1122
- phase: snapData.phase,
1123
- });
1124
- ctx.ui.notify(
1125
- `Report saved: .gsd/reports/${basename(outPath)} — open index.html to browse progression.`,
1126
- "info",
1127
- );
1128
- } catch (err) {
1129
- ctx.ui.notify(
1130
- `Report generation failed: ${getErrorMessage(err)}`,
1131
- "warning",
1132
- );
1133
- }
1134
- }
1135
- // Reset stuck detection for new milestone
1136
- s.unitDispatchCount.clear();
1137
- s.unitRecoveryCount.clear();
1138
- s.unitConsecutiveSkips.clear();
1139
- s.unitLifetimeDispatches.clear();
1140
- try {
1141
- const file = completedKeysPath(s.basePath);
1142
- if (existsSync(file)) {
1143
- atomicWriteSync(file, JSON.stringify([]));
1144
- }
1145
- s.completedKeySet.clear();
1146
- } catch (e) { debugLog("completed-keys-reset-failed", { error: getErrorMessage(e) }); }
1147
-
1148
- // ── Worktree lifecycle on milestone transition (#616) ──
1149
- if ((isInAutoWorktree(s.basePath) || getIsolationMode() === "branch") && shouldUseWorktreeIsolation()) {
1150
- tryMergeMilestone(ctx, s.currentMilestoneId, "transition");
1151
-
1152
- // Reset to project root and re-derive state for the new milestone
1153
- if (s.originalBasePath) {
1154
- s.basePath = s.originalBasePath;
1155
- s.gitService = createGitService(s.basePath);
1156
- }
1157
- invalidateAllCaches();
1158
-
1159
- state = await deriveState(s.basePath);
1160
- mid = state.activeMilestone?.id;
1161
- midTitle = state.activeMilestone?.title;
1162
-
1163
- if (mid) {
1164
- captureIntegrationBranch(s.basePath, mid);
1165
- try {
1166
- const wtPath = createAutoWorktree(s.basePath, mid);
1167
- s.basePath = wtPath;
1168
- s.gitService = createGitService(s.basePath);
1169
- ctx.ui.notify(`Created auto-worktree for ${mid} at ${wtPath}`, "info");
1170
- } catch (err) {
1171
- ctx.ui.notify(
1172
- `Auto-worktree creation for ${mid} failed: ${getErrorMessage(err)}. Continuing in project root.`,
1173
- "warning",
1174
- );
1175
- }
1176
- }
1177
- } else {
1178
- if (getIsolationMode() !== "none") {
1179
- captureIntegrationBranch(s.originalBasePath || s.basePath, mid);
1180
- }
1181
- }
1182
-
1183
- const pendingIds = (state.registry ?? [])
1184
- .filter(m => m.status !== "complete")
1185
- .map(m => m.id);
1186
- pruneQueueOrder(s.basePath, pendingIds);
1187
- }
1188
- if (mid) {
1189
- s.currentMilestoneId = mid;
1190
- setActiveMilestoneId(s.basePath, mid);
1191
- }
1192
-
1193
- if (!mid) {
1194
- if (s.currentUnit) {
1195
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1196
- }
1197
-
1198
- const incomplete = (state.registry ?? []).filter(m => m.status !== "complete" && m.status !== "parked");
1199
- if (incomplete.length === 0) {
1200
- // Genuinely all complete (parked milestones excluded) — merge milestone branch to main before stopping (#962)
1201
- if (s.currentMilestoneId) {
1202
- tryMergeMilestone(ctx, s.currentMilestoneId, "complete");
1203
- }
1204
- sendDesktopNotification("GSD", "All milestones complete!", "success", "milestone");
1205
- await stopAuto(ctx, pi, "All milestones complete");
1206
- } else if (state.phase === "blocked") {
1207
- const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
1208
- await stopAuto(ctx, pi, blockerMsg);
1209
- ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
1210
- sendDesktopNotification("GSD", blockerMsg, "error", "attention");
1211
- } else {
1212
- const ids = incomplete.map(m => m.id).join(", ");
1213
- const diag = `basePath=${s.basePath}, milestones=[${state.registry.map(m => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
1214
- ctx.ui.notify(`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`, "error");
1215
- await stopAuto(ctx, pi, `No active milestone — ${incomplete.length} incomplete (${ids}), see diagnostic above`);
1216
- }
1217
- return;
1218
- }
1219
-
1220
- if (!midTitle) {
1221
- midTitle = mid;
1222
- ctx.ui.notify(`Milestone ${mid} has no title in roadmap — using ID as fallback.`, "warning");
1223
- }
1224
-
1225
- // ── Mid-merge safety check ──
1226
- if (reconcileMergeState(s.basePath, ctx)) {
1227
- invalidateAllCaches();
1228
- state = await deriveState(s.basePath);
1229
- mid = state.activeMilestone?.id;
1230
- midTitle = state.activeMilestone?.title;
1231
- }
1232
-
1233
- if (!mid || !midTitle) {
1234
- if (s.currentUnit) {
1235
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1236
- }
1237
- const noMilestoneReason = !mid
1238
- ? "No active milestone after merge reconciliation"
1239
- : `Milestone ${mid} has no title after reconciliation`;
1240
- await stopAuto(ctx, pi, noMilestoneReason);
1241
- return;
1242
- }
1243
-
1244
- // Determine next unit
1245
- let unitType: string;
1246
- let unitId: string;
1247
- let prompt: string;
1248
-
1249
- if (state.phase === "complete") {
1250
- if (s.currentUnit) {
1251
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1252
- }
1253
- try {
1254
- const file = completedKeysPath(s.basePath);
1255
- if (existsSync(file)) {
1256
- atomicWriteSync(file, JSON.stringify([]));
1257
- }
1258
- s.completedKeySet.clear();
1259
- } catch (e) { debugLog("completed-keys-reset-failed", { error: getErrorMessage(e) }); }
1260
- // ── Milestone merge ──
1261
- if (s.currentMilestoneId) {
1262
- tryMergeMilestone(ctx, s.currentMilestoneId, "complete");
1263
- }
1264
- sendDesktopNotification("GSD", `Milestone ${mid} complete!`, "success", "milestone");
1265
- await stopAuto(ctx, pi, `Milestone ${mid} complete`);
1266
- return;
1267
- }
1268
-
1269
- if (state.phase === "blocked") {
1270
- if (s.currentUnit) {
1271
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1272
- }
1273
- const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
1274
- await stopAuto(ctx, pi, blockerMsg);
1275
- ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
1276
- sendDesktopNotification("GSD", blockerMsg, "error", "attention");
1277
- return;
1278
- }
1279
-
1280
- // Budget ceiling guard, context window guard, secrets gate, dispatch table
1281
- const prefs = loadEffectiveGSDPreferences()?.preferences;
1282
-
1283
- const budgetCeiling = prefs?.budget_ceiling;
1284
- if (budgetCeiling !== undefined && budgetCeiling > 0) {
1285
- const currentLedger = getLedger();
1286
- const totalCost = currentLedger ? getProjectTotals(currentLedger.units).cost : 0;
1287
- const budgetPct = totalCost / budgetCeiling;
1288
- const budgetAlertLevel = getBudgetAlertLevel(budgetPct);
1289
- const newBudgetAlertLevel = getNewBudgetAlertLevel(s.lastBudgetAlertLevel, budgetPct);
1290
- const enforcement = prefs?.budget_enforcement ?? "pause";
1291
-
1292
- const budgetEnforcementAction = getBudgetEnforcementAction(enforcement, budgetPct);
1293
-
1294
- if (newBudgetAlertLevel === 100 && budgetEnforcementAction !== "none") {
1295
- const msg = `Budget ceiling ${formatCost(budgetCeiling)} reached (spent ${formatCost(totalCost)}).`;
1296
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1297
- if (budgetEnforcementAction === "halt") {
1298
- sendDesktopNotification("GSD", msg, "error", "budget");
1299
- await stopAuto(ctx, pi, "Budget ceiling reached");
1300
- return;
1301
- }
1302
- if (budgetEnforcementAction === "pause") {
1303
- ctx.ui.notify(`${msg} Pausing auto-mode — /gsd auto to override and continue.`, "warning");
1304
- sendDesktopNotification("GSD", msg, "warning", "budget");
1305
- await pauseAuto(ctx, pi);
1306
- return;
1307
- }
1308
- ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
1309
- sendDesktopNotification("GSD", msg, "warning", "budget");
1310
- } else if (newBudgetAlertLevel === 90) {
1311
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1312
- ctx.ui.notify(`Budget 90%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning");
1313
- sendDesktopNotification("GSD", `Budget 90%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning", "budget");
1314
- } else if (newBudgetAlertLevel === 80) {
1315
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1316
- ctx.ui.notify(`Approaching budget ceiling — 80%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning");
1317
- sendDesktopNotification("GSD", `Approaching budget ceiling — 80%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning", "budget");
1318
- } else if (newBudgetAlertLevel === 75) {
1319
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1320
- ctx.ui.notify(`Budget 75%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "info");
1321
- sendDesktopNotification("GSD", `Budget 75%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "info", "budget");
1322
- } else if (budgetAlertLevel === 0) {
1323
- s.lastBudgetAlertLevel = 0;
1324
- }
1325
- } else {
1326
- s.lastBudgetAlertLevel = 0;
1327
- }
1328
-
1329
- const contextThreshold = prefs?.context_pause_threshold ?? 0;
1330
- if (contextThreshold > 0 && s.cmdCtx) {
1331
- const contextUsage = s.cmdCtx.getContextUsage();
1332
- if (contextUsage && contextUsage.percent !== null && contextUsage.percent >= contextThreshold) {
1333
- const msg = `Context window at ${contextUsage.percent}% (threshold: ${contextThreshold}%). Pausing to prevent truncated output.`;
1334
- ctx.ui.notify(`${msg} Run /gsd auto to continue (will start fresh session).`, "warning");
1335
- sendDesktopNotification("GSD", `Context ${contextUsage.percent}% — paused`, "warning", "attention");
1336
- await pauseAuto(ctx, pi);
1337
- return;
1338
- }
1339
- }
1340
-
1341
- // Secrets re-check gate
1342
- const runSecretsGate = async () => {
1343
- try {
1344
- const manifestStatus = await getManifestStatus(s.basePath, mid);
1345
- if (manifestStatus && manifestStatus.pending.length > 0) {
1346
- const result = await collectSecretsFromManifest(s.basePath, mid, ctx);
1347
- if (result && result.applied && result.skipped && result.existingSkipped) {
1348
- ctx.ui.notify(
1349
- `Secrets collected: ${result.applied.length} applied, ${result.skipped.length} skipped, ${result.existingSkipped.length} already set.`,
1350
- "info",
1351
- );
1352
- } else {
1353
- ctx.ui.notify("Secrets collection skipped.", "info");
1354
- }
1355
- }
1356
- } catch (err) {
1357
- ctx.ui.notify(
1358
- `Secrets collection error: ${getErrorMessage(err)}. Continuing with next task.`,
1359
- "warning",
1360
- );
1361
- }
1362
- };
1363
-
1364
- await runSecretsGate();
1365
-
1366
- // ── Interactive discussion gate ──
1367
- // If the active milestone needs discussion (has CONTEXT-DRAFT.md but no roadmap),
1368
- // stop auto-mode and route to the interactive discussion flow. The guided-flow
1369
- // handles needs-discussion correctly — it just needs to be called instead of
1370
- // letting the dispatch table fire "needs-discussion → stop" (#1170).
1371
- if (state.phase === "needs-discussion") {
1372
- if (s.currentUnit) {
1373
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1374
- }
1375
- const cmdCtx = s.cmdCtx!;
1376
- const basePath = s.basePath;
1377
- await stopAuto(ctx, pi, `${mid}: ${midTitle} needs discussion before planning.`);
1378
- const { showSmartEntry } = await import("./guided-flow.js");
1379
- await showSmartEntry(cmdCtx, pi, basePath);
1380
- return;
1381
- }
1382
-
1383
- // ── Dispatch table ──
1384
- const dispatchResult = await resolveDispatch({ basePath: s.basePath, mid, midTitle: midTitle!, state, prefs,
1385
- });
1386
-
1387
- if (dispatchResult.action === "stop") {
1388
- if (s.currentUnit) {
1389
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1390
- }
1391
- await stopAuto(ctx, pi, dispatchResult.reason);
1392
- return;
1393
- }
1394
-
1395
- if (dispatchResult.action !== "dispatch") {
1396
- // Defer re-dispatch to next microtask so s.dispatching is released first,
1397
- // preventing reentrancy guard bypass during concurrent entry (#1272).
1398
- setImmediate(() => dispatchNextUnit(ctx, pi).catch(err => {
1399
- ctx.ui.notify(`Deferred dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1400
- pauseAuto(ctx, pi).catch(() => {});
1401
- }));
1402
- return;
1403
- }
1404
-
1405
- unitType = dispatchResult.unitType;
1406
- unitId = dispatchResult.unitId;
1407
- prompt = dispatchResult.prompt;
1408
-
1409
- // ── Pre-dispatch hooks ──
1410
- const preDispatchResult = runPreDispatchHooks(unitType, unitId, prompt, s.basePath);
1411
- if (preDispatchResult.firedHooks.length > 0) {
1412
- ctx.ui.notify(
1413
- `Pre-dispatch hook${preDispatchResult.firedHooks.length > 1 ? "s" : ""}: ${preDispatchResult.firedHooks.join(", ")}`,
1414
- "info",
1415
- );
1416
- }
1417
- if (preDispatchResult.action === "skip") {
1418
- ctx.ui.notify(`Skipping ${unitType} ${unitId} (pre-dispatch hook).`, "info");
1419
- setImmediate(() => dispatchNextUnit(ctx, pi).catch(err => {
1420
- ctx.ui.notify(`Deferred dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1421
- pauseAuto(ctx, pi).catch(() => {});
1422
- }));
1423
- return;
1424
- }
1425
- if (preDispatchResult.action === "replace") {
1426
- prompt = preDispatchResult.prompt ?? prompt;
1427
- if (preDispatchResult.unitType) unitType = preDispatchResult.unitType;
1428
- } else if (preDispatchResult.prompt) {
1429
- prompt = preDispatchResult.prompt;
1430
- }
1431
-
1432
- const priorSliceBlocker = getPriorSliceCompletionBlocker(s.basePath, getMainBranch(s.basePath), unitType, unitId);
1433
- if (priorSliceBlocker) {
1434
- await stopAuto(ctx, pi, priorSliceBlocker);
1435
- return;
1436
- }
1437
-
1438
- const observabilityIssues = await _collectObservabilityWarnings(ctx, s.basePath, unitType, unitId);
1439
-
1440
- // ── Idempotency check (delegated to auto-idempotency.ts) ──
1441
- const idempotencyResult = checkIdempotency({
1442
- s,
1443
- unitType,
1444
- unitId,
1445
- basePath: s.basePath,
1446
- notify: (msg, level) => ctx.ui.notify(msg, level),
1447
- });
1448
-
1449
- if (idempotencyResult.action === "skip") {
1450
- if (idempotencyResult.reason === "completed" || idempotencyResult.reason === "fallback-persisted" || idempotencyResult.reason === "phantom-loop-cleared" || idempotencyResult.reason === "evicted") {
1451
- if (!s.active) return;
1452
- s.skipDepth++;
1453
- const skipDelay = idempotencyResult.reason === "phantom-loop-cleared" ? 50 : 150;
1454
- // Defer re-dispatch so s.dispatching is released first (#1272).
1455
- setTimeout(() => {
1456
- dispatchNextUnit(ctx, pi).catch(err => {
1457
- ctx.ui.notify(`Deferred skip-dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1458
- pauseAuto(ctx, pi).catch(() => {});
1459
- }).finally(() => {
1460
- s.skipDepth = Math.max(0, s.skipDepth - 1);
1461
- });
1462
- }, skipDelay);
1463
- return;
1464
- }
1465
- } else if (idempotencyResult.action === "stop") {
1466
- await stopAuto(ctx, pi, idempotencyResult.reason);
1467
- ctx.ui.notify(
1468
- `Hard loop detected: ${unitType} ${unitId} hit lifetime cap during skip cycle.`,
1469
- "error",
1470
- );
1471
- return;
1472
- }
1473
- // "rerun" and "proceed" fall through to stuck detection
1474
-
1475
- // ── Stuck detection (delegated to auto-stuck-detection.ts) ──
1476
- const stuckResult = await checkStuckAndRecover({
1477
- s,
1478
- ctx,
1479
- unitType,
1480
- unitId,
1481
- basePath: s.basePath,
1482
- buildSnapshotOpts: () => buildSnapshotOpts(unitType, unitId),
1483
- });
1484
-
1485
- if (stuckResult.action === "stop") {
1486
- await stopAuto(ctx, pi, stuckResult.reason);
1487
- if (stuckResult.notifyMessage) {
1488
- ctx.ui.notify(stuckResult.notifyMessage, "error");
1489
- }
1490
- return;
1491
- }
1492
- if (stuckResult.action === "recovered" && stuckResult.dispatchAgain) {
1493
- // Defer re-dispatch so s.dispatching is released first (#1272).
1494
- setImmediate(() => dispatchNextUnit(ctx, pi).catch(err => {
1495
- ctx.ui.notify(`Deferred recovery-dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1496
- pauseAuto(ctx, pi).catch(() => {});
1497
- }));
1498
- return;
1499
- }
1500
-
1501
- // Snapshot metrics + activity log for the PREVIOUS unit before we reassign.
1502
- if (s.currentUnit) {
1503
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1504
-
1505
- if (s.currentUnitRouting) {
1506
- const isRetry = s.currentUnit.type === unitType && s.currentUnit.id === unitId;
1507
- recordOutcome(
1508
- s.currentUnit.type,
1509
- s.currentUnitRouting.tier as "light" | "standard" | "heavy",
1510
- !isRetry,
1511
- );
1512
- }
1513
-
1514
- const closeoutKey = `${s.currentUnit.type}/${s.currentUnit.id}`;
1515
- const incomingKey = `${unitType}/${unitId}`;
1516
- const isHookUnit = s.currentUnit.type.startsWith("hook/");
1517
- const artifactVerified = isHookUnit || verifyExpectedArtifact(s.currentUnit.type, s.currentUnit.id, s.basePath);
1518
- if (closeoutKey !== incomingKey && artifactVerified) {
1519
- if (!isHookUnit) {
1520
- persistCompletedKey(s.basePath, closeoutKey);
1521
- s.completedKeySet.add(closeoutKey);
1522
- }
1523
-
1524
- s.completedUnits.push({
1525
- type: s.currentUnit.type,
1526
- id: s.currentUnit.id,
1527
- startedAt: s.currentUnit.startedAt,
1528
- finishedAt: Date.now(),
1529
- });
1530
- if (s.completedUnits.length > 200) {
1531
- s.completedUnits = s.completedUnits.slice(-200);
1532
- }
1533
- clearUnitRuntimeRecord(s.basePath, s.currentUnit.type, s.currentUnit.id);
1534
- s.unitDispatchCount.delete(`${s.currentUnit.type}/${s.currentUnit.id}`);
1535
- s.unitRecoveryCount.delete(`${s.currentUnit.type}/${s.currentUnit.id}`);
1536
- }
1537
- }
1538
- s.currentUnit = { type: unitType, id: unitId, startedAt: Date.now() };
1539
- captureAvailableSkills();
1540
- writeUnitRuntimeRecord(s.basePath, unitType, unitId, s.currentUnit.startedAt, {
1541
- phase: "dispatched",
1542
- wrapupWarningSent: false,
1543
- timeoutAt: null,
1544
- lastProgressAt: s.currentUnit.startedAt,
1545
- progressCount: 0,
1546
- lastProgressKind: "dispatch",
1547
- });
1548
-
1549
- // Status bar + progress widget
1550
- ctx.ui.setStatus("gsd-auto", "auto");
1551
- if (mid) updateSliceProgressCache(s.basePath, mid, state.activeSlice?.id);
1552
- updateProgressWidget(ctx, unitType, unitId, state);
1553
-
1554
- ensurePreconditions(unitType, unitId, s.basePath, state);
1555
-
1556
- // Fresh session — with timeout to prevent permanent hangs (#1073).
1557
- // If newSession() hangs (e.g., session manager deadlock, network issue),
1558
- // without this timeout the entire dispatch chain stalls permanently: no
1559
- // timeouts are set, no gap watchdog fires, and auto-mode is left active
1560
- // but idle until the user Ctrl+C's.
1561
- let result: { cancelled: boolean };
1562
- try {
1563
- const sessionPromise = s.cmdCtx!.newSession();
1564
- const timeoutPromise = new Promise<{ cancelled: true }>((resolve) =>
1565
- setTimeout(() => resolve({ cancelled: true }), NEW_SESSION_TIMEOUT_MS),
1566
- );
1567
- result = await Promise.race([sessionPromise, timeoutPromise]);
1568
- } catch (sessionErr) {
1569
- const msg = getErrorMessage(sessionErr);
1570
- ctx.ui.notify(`Session creation failed: ${msg}. Retrying via watchdog.`, "error");
1571
- throw new Error(`newSession() failed: ${msg}`);
1572
- }
1573
- if (result.cancelled) {
1574
- ctx.ui.notify(
1575
- `Session creation timed out or was cancelled for ${unitType} ${unitId}. Will retry.`,
1576
- "warning",
1577
- );
1578
- await stopAuto(ctx, pi, "Session creation failed");
1579
- return;
1580
- }
1581
-
1582
- const sessionFile = ctx.sessionManager.getSessionFile();
1583
- updateSessionLock(lockBase(), unitType, unitId, s.completedUnits.length, sessionFile);
1584
- writeLock(lockBase(), unitType, unitId, s.completedUnits.length, sessionFile);
1585
-
1586
- // Prompt injection
1587
- const MAX_RECOVERY_CHARS = 50_000;
1588
- let finalPrompt = prompt;
1589
-
1590
- if (s.pendingVerificationRetry) {
1591
- const retryCtx = s.pendingVerificationRetry;
1592
- s.pendingVerificationRetry = null;
1593
- const capped = retryCtx.failureContext.length > MAX_RECOVERY_CHARS
1594
- ? retryCtx.failureContext.slice(0, MAX_RECOVERY_CHARS) + "\n\n[...failure context truncated]"
1595
- : retryCtx.failureContext;
1596
- finalPrompt = `**VERIFICATION FAILED — AUTO-FIX ATTEMPT ${retryCtx.attempt}**\n\nThe verification gate ran after your previous attempt and found failures. Fix these issues before completing the task.\n\n${capped}\n\n---\n\n${finalPrompt}`;
1597
- }
1598
-
1599
- if (s.pendingCrashRecovery) {
1600
- const capped = s.pendingCrashRecovery.length > MAX_RECOVERY_CHARS
1601
- ? s.pendingCrashRecovery.slice(0, MAX_RECOVERY_CHARS) + "\n\n[...recovery briefing truncated to prevent memory exhaustion]"
1602
- : s.pendingCrashRecovery;
1603
- finalPrompt = `${capped}\n\n---\n\n${finalPrompt}`;
1604
- s.pendingCrashRecovery = null;
1605
- } else if ((s.unitDispatchCount.get(`${unitType}/${unitId}`) ?? 0) > 1) {
1606
- const diagnostic = getDeepDiagnostic(s.basePath);
1607
- if (diagnostic) {
1608
- const cappedDiag = diagnostic.length > MAX_RECOVERY_CHARS
1609
- ? diagnostic.slice(0, MAX_RECOVERY_CHARS) + "\n\n[...diagnostic truncated to prevent memory exhaustion]"
1610
- : diagnostic;
1611
- finalPrompt = `**RETRY — your previous attempt did not produce the required artifact.**\n\nDiagnostic from previous attempt:\n${cappedDiag}\n\nFix whatever went wrong and make sure you write the required file this time.\n\n---\n\n${finalPrompt}`;
1612
- }
1613
- }
1614
-
1615
- const repairBlock = buildObservabilityRepairBlock(observabilityIssues);
1616
- if (repairBlock) {
1617
- finalPrompt = `${finalPrompt}${repairBlock}`;
1618
- }
1619
-
1620
- // ── Prompt char measurement ──
1621
- s.lastPromptCharCount = finalPrompt.length;
1622
- s.lastBaselineCharCount = undefined;
1623
- if (isDbAvailable()) {
1624
- try {
1625
- const { inlineGsdRootFile } = await import("./auto-prompts.js");
1626
- const [decisionsContent, requirementsContent, projectContent] = await Promise.all([
1627
- inlineGsdRootFile(s.basePath, "decisions.md", "Decisions"),
1628
- inlineGsdRootFile(s.basePath, "requirements.md", "Requirements"),
1629
- inlineGsdRootFile(s.basePath, "project.md", "Project"),
1630
- ]);
1631
- s.lastBaselineCharCount =
1632
- (decisionsContent?.length ?? 0) +
1633
- (requirementsContent?.length ?? 0) +
1634
- (projectContent?.length ?? 0);
1635
- } catch {
1636
- // Non-fatal
1637
- }
1638
- }
1639
-
1640
- // Cache-optimize prompt section ordering
1641
- try {
1642
- const { reorderForCaching } = await import("./prompt-ordering.js");
1643
- finalPrompt = reorderForCaching(finalPrompt);
1644
- } catch (reorderErr) {
1645
- const msg = getErrorMessage(reorderErr);
1646
- process.stderr.write(`[gsd] prompt reorder failed (non-fatal): ${msg}\n`);
1647
- }
1648
-
1649
- // Select and apply model
1650
- const modelResult = await selectAndApplyModel(ctx, pi, unitType, unitId, s.basePath, prefs, s.verbose, s.autoModeStartModel);
1651
- s.currentUnitRouting = modelResult.routing;
1652
-
1653
- // ── Start unit supervision (delegated to auto-timers.ts) ──
1654
- clearUnitTimeout();
1655
- startUnitSupervision({
1656
- s,
1657
- ctx,
1658
- pi,
1659
- unitType,
1660
- unitId,
1661
- prefs,
1662
- buildSnapshotOpts: () => buildSnapshotOpts(unitType, unitId),
1663
- buildRecoveryContext: () => buildRecoveryContext(),
1664
- pauseAuto,
1665
- });
1666
-
1667
- // Inject prompt
1668
- if (!s.active) return;
1669
- pi.sendMessage(
1670
- { customType: "gsd-auto", content: finalPrompt, display: s.verbose },
1671
- { triggerTurn: true },
1672
- );
1673
-
1674
- } finally {
1675
- s.dispatching = false;
1676
- }
1677
- }
1678
-
1679
969
  // ─── Preconditions ────────────────────────────────────────────────────────────
1680
970
 
1681
971
  /**
@@ -1683,9 +973,13 @@ async function dispatchNextUnit(
1683
973
  * dispatching a unit. The LLM should never need to mkdir or git checkout.
1684
974
  */
1685
975
  function ensurePreconditions(
1686
- unitType: string, unitId: string, base: string, state: GSDState,
976
+ unitType: string,
977
+ unitId: string,
978
+ base: string,
979
+ state: GSDState,
1687
980
  ): void {
1688
- const { milestone: mid } = parseUnitId(unitId);
981
+ const parts = unitId.split("/");
982
+ const mid = parts[0]!;
1689
983
 
1690
984
  const mDir = resolveMilestonePath(base, mid);
1691
985
  if (!mDir) {
@@ -1693,8 +987,8 @@ function ensurePreconditions(
1693
987
  mkdirSync(join(newDir, "slices"), { recursive: true });
1694
988
  }
1695
989
 
1696
- const sid = parseUnitId(unitId).slice;
1697
- if (sid) {
990
+ if (parts.length >= 2) {
991
+ const sid = parts[1]!;
1698
992
 
1699
993
  const mDirResolved = resolveMilestonePath(base, mid);
1700
994
  if (mDirResolved) {
@@ -1710,16 +1004,17 @@ function ensurePreconditions(
1710
1004
  }
1711
1005
  }
1712
1006
  }
1713
-
1714
1007
  }
1715
1008
 
1716
1009
  // ─── Diagnostics ──────────────────────────────────────────────────────────────
1717
1010
 
1718
1011
  /** Build recovery context from module state for recoverTimedOutUnit */
1719
1012
  function buildRecoveryContext(): import("./auto-timeout-recovery.js").RecoveryContext {
1720
- return { basePath: s.basePath, verbose: s.verbose,
1721
- currentUnitStartedAt: s.currentUnit?.startedAt ?? Date.now(), unitRecoveryCount: s.unitRecoveryCount,
1722
- dispatchNextUnit,
1013
+ return {
1014
+ basePath: s.basePath,
1015
+ verbose: s.verbose,
1016
+ currentUnitStartedAt: s.currentUnit?.startedAt ?? Date.now(),
1017
+ unitRecoveryCount: s.unitRecoveryCount,
1723
1018
  };
1724
1019
  }
1725
1020
 
@@ -1736,17 +1031,6 @@ export {
1736
1031
  * Test-only: expose skip-loop state for unit tests.
1737
1032
  * Not part of the public API.
1738
1033
  */
1739
- export function _getUnitConsecutiveSkips(): Map<string, number> { return s.unitConsecutiveSkips; }
1740
- export function _resetUnitConsecutiveSkips(): void { s.unitConsecutiveSkips.clear(); }
1741
-
1742
- /**
1743
- * Test-only: expose dispatching / skipDepth state for reentrancy guard tests.
1744
- * Not part of the public API.
1745
- */
1746
- export function _getDispatching(): boolean { return s.dispatching; }
1747
- export function _setDispatching(v: boolean): void { s.dispatching = v; }
1748
- export function _getSkipDepth(): number { return s.skipDepth; }
1749
- export function _setSkipDepth(v: number): void { s.skipDepth = v; }
1750
1034
 
1751
1035
  /**
1752
1036
  * Dispatch a hook unit directly, bypassing normal pre-dispatch hooks.
@@ -1776,7 +1060,11 @@ export async function dispatchHookUnit(
1776
1060
  const hookUnitType = `hook/${hookName}`;
1777
1061
  const hookStartedAt = Date.now();
1778
1062
 
1779
- s.currentUnit = { type: triggerUnitType, id: triggerUnitId, startedAt: hookStartedAt };
1063
+ s.currentUnit = {
1064
+ type: triggerUnitType,
1065
+ id: triggerUnitId,
1066
+ startedAt: hookStartedAt,
1067
+ };
1780
1068
 
1781
1069
  const result = await s.cmdCtx!.newSession();
1782
1070
  if (result.cancelled) {
@@ -1784,32 +1072,49 @@ export async function dispatchHookUnit(
1784
1072
  return false;
1785
1073
  }
1786
1074
 
1787
- s.currentUnit = { type: hookUnitType, id: triggerUnitId, startedAt: hookStartedAt };
1075
+ s.currentUnit = {
1076
+ type: hookUnitType,
1077
+ id: triggerUnitId,
1078
+ startedAt: hookStartedAt,
1079
+ };
1788
1080
 
1789
- writeUnitRuntimeRecord(s.basePath, hookUnitType, triggerUnitId, hookStartedAt, {
1790
- phase: "dispatched",
1791
- wrapupWarningSent: false,
1792
- timeoutAt: null,
1793
- lastProgressAt: hookStartedAt,
1794
- progressCount: 0,
1795
- lastProgressKind: "dispatch",
1796
- });
1081
+ writeUnitRuntimeRecord(
1082
+ s.basePath,
1083
+ hookUnitType,
1084
+ triggerUnitId,
1085
+ hookStartedAt,
1086
+ {
1087
+ phase: "dispatched",
1088
+ wrapupWarningSent: false,
1089
+ timeoutAt: null,
1090
+ lastProgressAt: hookStartedAt,
1091
+ progressCount: 0,
1092
+ lastProgressKind: "dispatch",
1093
+ },
1094
+ );
1797
1095
 
1798
1096
  if (hookModel) {
1799
1097
  const availableModels = ctx.modelRegistry.getAvailable();
1800
- const match = availableModels.find(m =>
1801
- m.id === hookModel || `${m.provider}/${m.id}` === hookModel,
1098
+ const match = availableModels.find(
1099
+ (m) => m.id === hookModel || `${m.provider}/${m.id}` === hookModel,
1802
1100
  );
1803
1101
  if (match) {
1804
1102
  try {
1805
1103
  await pi.setModel(match);
1806
- } catch { /* non-fatal */ }
1104
+ } catch {
1105
+ /* non-fatal */
1106
+ }
1807
1107
  }
1808
1108
  }
1809
1109
 
1810
1110
  const sessionFile = ctx.sessionManager.getSessionFile();
1811
- updateSessionLock(lockBase(), hookUnitType, triggerUnitId, s.completedUnits.length, sessionFile);
1812
- writeLock(lockBase(), hookUnitType, triggerUnitId, s.completedUnits.length, sessionFile);
1111
+ writeLock(
1112
+ lockBase(),
1113
+ hookUnitType,
1114
+ triggerUnitId,
1115
+ s.completedUnits.length,
1116
+ sessionFile,
1117
+ );
1813
1118
 
1814
1119
  clearUnitTimeout();
1815
1120
  const supervisor = resolveAutoSupervisorConfig();
@@ -1818,10 +1123,16 @@ export async function dispatchHookUnit(
1818
1123
  s.unitTimeoutHandle = null;
1819
1124
  if (!s.active) return;
1820
1125
  if (s.currentUnit) {
1821
- writeUnitRuntimeRecord(s.basePath, hookUnitType, triggerUnitId, hookStartedAt, {
1822
- phase: "timeout",
1823
- timeoutAt: Date.now(),
1824
- });
1126
+ writeUnitRuntimeRecord(
1127
+ s.basePath,
1128
+ hookUnitType,
1129
+ triggerUnitId,
1130
+ hookStartedAt,
1131
+ {
1132
+ phase: "timeout",
1133
+ timeoutAt: Date.now(),
1134
+ },
1135
+ );
1825
1136
  }
1826
1137
  ctx.ui.notify(
1827
1138
  `Hook ${hookName} exceeded ${supervisor.hard_timeout_minutes ?? 30}min timeout. Pausing auto-mode.`,
@@ -1834,6 +1145,10 @@ export async function dispatchHookUnit(
1834
1145
  ctx.ui.setStatus("gsd-auto", s.stepMode ? "next" : "auto");
1835
1146
  ctx.ui.notify(`Running post-unit hook: ${hookName}`, "info");
1836
1147
 
1148
+ debugLog("dispatchHookUnit", {
1149
+ phase: "send-message",
1150
+ promptLength: hookPrompt.length,
1151
+ });
1837
1152
  pi.sendMessage(
1838
1153
  { customType: "gsd-auto", content: hookPrompt, display: true },
1839
1154
  { triggerTurn: true },
@@ -1842,4 +1157,5 @@ export async function dispatchHookUnit(
1842
1157
  return true;
1843
1158
  }
1844
1159
 
1845
-
1160
+ // Direct phase dispatch → auto-direct-dispatch.ts
1161
+ export { dispatchDirectPhase } from "./auto-direct-dispatch.js";