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
@@ -11,57 +11,57 @@
11
11
  */
12
12
  import { deriveState } from "./state.js";
13
13
  import { getManifestStatus } from "./files.js";
14
+ export { inlinePriorMilestoneSummary } from "./files.js";
14
15
  import { collectSecretsFromManifest } from "../get-secrets-from-user.js";
15
16
  import { gsdRoot, resolveMilestoneFile, resolveMilestonePath, resolveDir, milestonesDir, } from "./paths.js";
16
17
  import { invalidateAllCaches } from "./cache.js";
17
18
  import { clearActivityLogState } from "./activity-log.js";
18
- import { synthesizeCrashRecovery, getDeepDiagnostic } from "./session-forensics.js";
19
- import { writeLock, clearLock, readCrashLock, isLockProcessAlive } from "./crash-recovery.js";
19
+ import { synthesizeCrashRecovery, getDeepDiagnostic, } from "./session-forensics.js";
20
+ import { writeLock, clearLock, readCrashLock, isLockProcessAlive, } from "./crash-recovery.js";
20
21
  import { acquireSessionLock, validateSessionLock, releaseSessionLock, updateSessionLock, } from "./session-lock.js";
21
22
  import { clearUnitRuntimeRecord, readUnitRuntimeRecord, writeUnitRuntimeRecord, } from "./unit-runtime.js";
22
- import { resolveAutoSupervisorConfig, loadEffectiveGSDPreferences, getIsolationMode } from "./preferences.js";
23
+ import { resolveAutoSupervisorConfig, loadEffectiveGSDPreferences, getIsolationMode, } from "./preferences.js";
23
24
  import { sendDesktopNotification } from "./notifications.js";
24
25
  import { getBudgetAlertLevel, getNewBudgetAlertLevel, getBudgetEnforcementAction, } from "./auto-budget.js";
25
26
  import { markToolStart as _markToolStart, markToolEnd as _markToolEnd, getOldestInFlightToolAgeMs as _getOldestInFlightToolAgeMs, clearInFlightTools, } from "./auto-tool-tracking.js";
26
27
  import { collectObservabilityWarnings as _collectObservabilityWarnings, buildObservabilityRepairBlock, } from "./auto-observability.js";
27
28
  import { closeoutUnit } from "./auto-unit-closeout.js";
28
29
  import { selectAndApplyModel } from "./auto-model-selection.js";
29
- import { checkResourcesStale, escapeStaleWorktree, } from "./resource-version.js";
30
+ import { syncProjectRootToWorktree, checkResourcesStale, escapeStaleWorktree, } from "./auto-worktree-sync.js";
30
31
  import { resetRoutingHistory, recordOutcome } from "./routing-history.js";
31
32
  import { resetHookState, runPreDispatchHooks, restoreHookState, clearPersistedHookState, } from "./post-unit-hooks.js";
32
33
  import { runGSDDoctor, rebuildState } from "./doctor.js";
33
34
  import { preDispatchHealthGate, resetProactiveHealing, } from "./doctor-proactive.js";
34
35
  import { clearSkillSnapshot } from "./skill-discovery.js";
35
- import { captureAvailableSkills, resetSkillTelemetry } from "./skill-telemetry.js";
36
+ import { captureAvailableSkills, resetSkillTelemetry, } from "./skill-telemetry.js";
36
37
  import { initMetrics, resetMetrics, getLedger, getProjectTotals, formatCost, formatTokenCount, } from "./metrics.js";
37
38
  import { join } from "node:path";
38
- import { parseUnitId } from "./unit-id.js";
39
39
  import { readFileSync, existsSync, mkdirSync } from "node:fs";
40
40
  import { atomicWriteSync } from "./atomic-write.js";
41
41
  import { autoCommitCurrentBranch, captureIntegrationBranch, detectWorktreeName, getCurrentBranch, getMainBranch, setActiveMilestoneId, } from "./worktree.js";
42
- import { createGitService } from "./git-service.js";
42
+ import { GitServiceImpl } from "./git-service.js";
43
43
  import { getPriorSliceCompletionBlocker } from "./dispatch-guard.js";
44
44
  import { createAutoWorktree, enterAutoWorktree, teardownAutoWorktree, isInAutoWorktree, getAutoWorktreePath, mergeMilestoneToMain, autoWorktreeBranch, syncWorktreeStateBack, } from "./auto-worktree.js";
45
45
  import { pruneQueueOrder } from "./queue-order.js";
46
- import { showNextAction } from "../shared/mod.js";
47
- import { debugLog, debugTime, isDebugEnabled, writeDebugSummary } from "./debug-logger.js";
48
- import { verifyExpectedArtifact, completedKeysPath, persistCompletedKey, selfHealRuntimeRecords, reconcileMergeState, } from "./auto-recovery.js";
46
+ import { debugLog, isDebugEnabled, writeDebugSummary } from "./debug-logger.js";
47
+ import { verifyExpectedArtifact, reconcileMergeState, } from "./auto-recovery.js";
49
48
  import { resolveDispatch } from "./auto-dispatch.js";
50
- import { updateProgressWidget as _updateProgressWidget, updateSliceProgressCache, clearSliceProgressCache, describeNextUnit as _describeNextUnit, unitVerb, hideFooter, } from "./auto-dashboard.js";
49
+ import { updateProgressWidget as _updateProgressWidget, updateSliceProgressCache, clearSliceProgressCache, hideFooter, } from "./auto-dashboard.js";
51
50
  import { registerSigtermHandler as _registerSigtermHandler, deregisterSigtermHandler as _deregisterSigtermHandler, } from "./auto-supervisor.js";
52
51
  import { isDbAvailable } from "./gsd-db.js";
53
52
  import { countPendingCaptures } from "./captures.js";
54
53
  // ── Extracted modules ──────────────────────────────────────────────────────
55
54
  import { startUnitSupervision } from "./auto-timers.js";
56
- import { checkIdempotency } from "./auto-idempotency.js";
57
- import { checkStuckAndRecover } from "./auto-stuck-detection.js";
58
55
  import { runPostUnitVerification } from "./auto-verification.js";
59
- import { postUnitPreVerification, postUnitPostVerification } from "./auto-post-unit.js";
56
+ import { postUnitPreVerification, postUnitPostVerification, } from "./auto-post-unit.js";
60
57
  import { bootstrapAutoSession } from "./auto-start.js";
61
- // Resource staleness, stale worktree escape → resource-version.ts
58
+ import { autoLoop, resolveAgentEnd } from "./auto-loop.js";
59
+ import { WorktreeResolver, } from "./worktree-resolver.js";
60
+ import { reorderForCaching } from "./prompt-ordering.js";
61
+ // Worktree sync, resource staleness, stale worktree escape → auto-worktree-sync.ts
62
62
  // ─── Session State ─────────────────────────────────────────────────────────
63
- import { AutoSession, DISPATCH_GAP_TIMEOUT_MS, MAX_SKIP_DEPTH, NEW_SESSION_TIMEOUT_MS, DISPATCH_HANG_TIMEOUT_MS, } from "./auto/session.js";
64
- import { getErrorMessage } from "./error-utils.js";
63
+ import { AutoSession, } from "./auto/session.js";
64
+ export { MAX_UNIT_DISPATCHES, STUB_RECOVERY_THRESHOLD, MAX_LIFETIME_DISPATCHES, NEW_SESSION_TIMEOUT_MS, } from "./auto/session.js";
65
65
  // ── ENCAPSULATION INVARIANT ─────────────────────────────────────────────────
66
66
  // ALL mutable auto-mode state lives in the AutoSession class (auto/session.ts).
67
67
  // This file must NOT declare module-level `let` or `var` variables for state.
@@ -75,6 +75,8 @@ import { getErrorMessage } from "./error-utils.js";
75
75
  // Tests in auto-session-encapsulation.test.ts enforce this invariant.
76
76
  // ─────────────────────────────────────────────────────────────────────────────
77
77
  const s = new AutoSession();
78
+ /** Throttle STATE.md rebuilds — at most once per 30 seconds */
79
+ const STATE_REBUILD_MIN_INTERVAL_MS = 30_000;
78
80
  export function shouldUseWorktreeIsolation() {
79
81
  const prefs = loadEffectiveGSDPreferences()?.preferences?.git;
80
82
  if (prefs?.isolation === "none")
@@ -83,7 +85,33 @@ export function shouldUseWorktreeIsolation() {
83
85
  return false;
84
86
  return true; // default: worktree
85
87
  }
86
- // All mutable state lives in AutoSession (auto/session.ts) see encapsulation invariant above.
88
+ /** Crash recovery prompt set by startAuto, consumed by the main loop */
89
+ /** Pending verification retry — set when gate fails with retries remaining, consumed by autoLoop */
90
+ /** Verification retry count per unitId — separate from s.unitDispatchCount which tracks artifact-missing retries */
91
+ /** Session file path captured at pause — used to synthesize recovery briefing on resume */
92
+ /** Dashboard tracking */
93
+ /** Track dynamic routing decision for the current unit (for metrics) */
94
+ /** Queue of quick-task captures awaiting dispatch after triage resolution */
95
+ /**
96
+ * Model captured at auto-mode start. Used to prevent model bleed between
97
+ * concurrent GSD instances sharing the same global settings.json (#650).
98
+ * When preferences don't specify a model for a unit type, this ensures
99
+ * the session's original model is re-applied instead of reading from
100
+ * the shared global settings (which another instance may have overwritten).
101
+ */
102
+ /** Track current milestone to detect transitions */
103
+ /** Model the user had selected before auto-mode started */
104
+ /** Progress-aware timeout supervision */
105
+ /** Context-pressure continue-here monitor — fires once when context usage >= 70% */
106
+ /** Prompt character measurement for token savings analysis (R051). */
107
+ /** SIGTERM handler registered while auto-mode is active — cleared on stop/pause. */
108
+ /**
109
+ * Tool calls currently being executed — prevents false idle detection during long-running tools.
110
+ * Maps toolCallId → start timestamp (ms) so the idle watchdog can detect tools that have been
111
+ * running suspiciously long (e.g., a Bash command hung because `&` kept stdout open).
112
+ */
113
+ // Re-export budget utilities for external consumers
114
+ export { getBudgetAlertLevel, getNewBudgetAlertLevel, getBudgetEnforcementAction, } from "./auto-budget.js";
87
115
  /** Wrapper: register SIGTERM handler and store reference. */
88
116
  function registerSigtermHandler(currentBasePath) {
89
117
  s.sigtermHandler = _registerSigtermHandler(currentBasePath, s.sigtermHandler);
@@ -106,12 +134,15 @@ export function getAutoDashboardData() {
106
134
  catch {
107
135
  // Non-fatal — captures module may not be loaded
108
136
  }
109
- return { active: s.active, paused: s.paused,
137
+ return {
138
+ active: s.active,
139
+ paused: s.paused,
110
140
  stepMode: s.stepMode,
111
141
  startTime: s.autoStartTime,
112
- elapsed: (s.active || s.paused) ? Date.now() - s.autoStartTime : 0,
142
+ elapsed: s.active || s.paused ? Date.now() - s.autoStartTime : 0,
113
143
  currentUnit: s.currentUnit ? { ...s.currentUnit } : null,
114
- completedUnits: [...s.completedUnits], basePath: s.basePath,
144
+ completedUnits: [...s.completedUnits],
145
+ basePath: s.basePath,
115
146
  totalCost: totals?.cost ?? 0,
116
147
  totalTokens: totals?.tokens.total ?? 0,
117
148
  pendingCaptureCount,
@@ -146,9 +177,11 @@ export function getOldestInFlightToolAgeMs() {
146
177
  * Return the base path to use for the auto.lock file.
147
178
  * Always uses the original project root (not the worktree) so that
148
179
  * a second terminal can discover and stop a running auto-mode session.
180
+ *
181
+ * Delegates to AutoSession.lockBasePath — the single source of truth.
149
182
  */
150
183
  function lockBase() {
151
- return s.originalBasePath || s.basePath;
184
+ return s.lockBasePath;
152
185
  }
153
186
  /**
154
187
  * Attempt to stop a running auto-mode session from a different process.
@@ -196,17 +229,12 @@ function clearUnitTimeout() {
196
229
  s.continueHereHandle = null;
197
230
  }
198
231
  clearInFlightTools();
199
- clearDispatchGapWatchdog();
200
- }
201
- function clearDispatchGapWatchdog() {
202
- if (s.dispatchGapHandle) {
203
- clearTimeout(s.dispatchGapHandle);
204
- s.dispatchGapHandle = null;
205
- }
206
232
  }
207
233
  /** Build snapshot metric opts, enriching with continueHereFired from the runtime record. */
208
234
  function buildSnapshotOpts(unitType, unitId) {
209
- const runtime = s.currentUnit ? readUnitRuntimeRecord(s.basePath, unitType, unitId) : null;
235
+ const runtime = s.currentUnit
236
+ ? readUnitRuntimeRecord(s.basePath, unitType, unitId)
237
+ : null;
210
238
  return {
211
239
  promptCharCount: s.lastPromptCharCount,
212
240
  baselineCharCount: s.lastBaselineCharCount,
@@ -214,139 +242,38 @@ function buildSnapshotOpts(unitType, unitId) {
214
242
  ...(runtime?.continueHereFired ? { continueHereFired: true } : {}),
215
243
  };
216
244
  }
217
- // ─── Extracted Merge Helper ───────────────────────────────────────────────
218
- /**
219
- * Attempt to merge the current milestone branch to main.
220
- * Handles both worktree and branch isolation modes with a single code path.
221
- * Returns true if merge succeeded, false on error (non-fatal, logged).
222
- *
223
- * Extracted from 4 duplicate merge blocks in dispatchNextUnit to eliminate
224
- * the bug factory where fixing one copy didn't fix the others (#1308).
225
- */
226
- function tryMergeMilestone(ctx, milestoneId, mode) {
227
- const isolationMode = getIsolationMode();
228
- // Worktree merge path
229
- if (isInAutoWorktree(s.basePath) && s.originalBasePath) {
230
- try {
231
- // Sync completion artifacts from worktree → external state before merge (#1412)
232
- try {
233
- const { synced } = syncWorktreeStateBack(s.originalBasePath, s.basePath, milestoneId);
234
- if (synced.length > 0) {
235
- debugLog("worktree-reverse-sync", { milestoneId, synced: synced.length });
236
- }
237
- }
238
- catch (syncErr) {
239
- debugLog("worktree-reverse-sync-failed", { milestoneId, error: getErrorMessage(syncErr) });
240
- }
241
- const roadmapPath = resolveMilestoneFile(s.originalBasePath, milestoneId, "ROADMAP");
242
- if (!roadmapPath) {
243
- teardownAutoWorktree(s.originalBasePath, milestoneId);
244
- ctx.ui.notify(`Exited worktree for ${milestoneId} (no roadmap for merge).`, "info");
245
- return false;
246
- }
247
- const roadmapContent = readFileSync(roadmapPath, "utf-8");
248
- const mergeResult = mergeMilestoneToMain(s.originalBasePath, milestoneId, roadmapContent);
249
- s.basePath = s.originalBasePath;
250
- s.gitService = createGitService(s.basePath);
251
- ctx.ui.notify(`Milestone ${milestoneId} merged to main.${mergeResult.pushed ? " Pushed to remote." : ""}`, "info");
252
- return true;
253
- }
254
- catch (err) {
255
- ctx.ui.notify(`Milestone merge failed: ${err instanceof Error ? err.message : String(err)}`, "warning");
256
- if (s.originalBasePath) {
257
- s.basePath = s.originalBasePath;
258
- try {
259
- process.chdir(s.basePath);
260
- }
261
- catch { /* best-effort */ }
262
- }
263
- return false;
264
- }
265
- }
266
- // Branch-mode merge path
267
- if (isolationMode === "branch") {
268
- try {
269
- const currentBranch = getCurrentBranch(s.basePath);
270
- const milestoneBranch = autoWorktreeBranch(milestoneId);
271
- if (currentBranch === milestoneBranch) {
272
- const roadmapPath = resolveMilestoneFile(s.basePath, milestoneId, "ROADMAP");
273
- if (roadmapPath) {
274
- const roadmapContent = readFileSync(roadmapPath, "utf-8");
275
- const mergeResult = mergeMilestoneToMain(s.basePath, milestoneId, roadmapContent);
276
- s.gitService = createGitService(s.basePath);
277
- ctx.ui.notify(`Milestone ${milestoneId} merged (branch mode).${mergeResult.pushed ? " Pushed to remote." : ""}`, "info");
278
- return true;
279
- }
280
- }
281
- }
282
- catch (err) {
283
- ctx.ui.notify(`Milestone merge failed (branch mode): ${err instanceof Error ? err.message : String(err)}`, "warning");
284
- }
285
- }
286
- return false;
287
- }
288
- /**
289
- * Start a watchdog that fires if no new unit is dispatched within DISPATCH_GAP_TIMEOUT_MS
290
- * after handleAgentEnd completes. This catches the case where the dispatch chain silently
291
- * breaks (e.g., unhandled exception in dispatchNextUnit) and auto-mode is left s.active but idle.
292
- *
293
- * The watchdog is cleared on the next successful unit dispatch (clearUnitTimeout is called
294
- * at the start of handleAgentEnd, which calls clearDispatchGapWatchdog).
295
- */
296
- function startDispatchGapWatchdog(ctx, pi) {
297
- clearDispatchGapWatchdog();
298
- s.dispatchGapHandle = setTimeout(async () => {
299
- s.dispatchGapHandle = null;
300
- if (!s.active || !s.cmdCtx)
301
- return;
302
- if (s.verbose) {
303
- ctx.ui.notify("Dispatch gap detected — re-evaluating state.", "info");
304
- }
305
- try {
306
- await dispatchNextUnit(ctx, pi);
307
- }
308
- catch (retryErr) {
309
- const message = getErrorMessage(retryErr);
310
- await stopAuto(ctx, pi, `Dispatch gap recovery failed: ${message}`);
311
- return;
312
- }
313
- if (s.active && !s.unitTimeoutHandle && !s.wrapupWarningHandle) {
314
- await stopAuto(ctx, pi, "Stalled — no dispatchable unit after retry");
315
- }
316
- }, DISPATCH_GAP_TIMEOUT_MS);
245
+ function handleLostSessionLock(ctx) {
246
+ debugLog("session-lock-lost", { lockBase: lockBase() });
247
+ s.active = false;
248
+ s.paused = false;
249
+ clearUnitTimeout();
250
+ deregisterSigtermHandler();
251
+ ctx?.ui.notify("Session lock lost another GSD process appears to have taken over. Stopping gracefully.", "error");
252
+ ctx?.ui.setStatus("gsd-auto", undefined);
253
+ ctx?.ui.setWidget("gsd-progress", undefined);
254
+ ctx?.ui.setFooter(undefined);
317
255
  }
318
256
  export async function stopAuto(ctx, pi, reason) {
319
257
  if (!s.active && !s.paused)
320
258
  return;
321
259
  const reasonSuffix = reason ? ` — ${reason}` : "";
322
260
  clearUnitTimeout();
323
- if (lockBase()) {
324
- releaseSessionLock(lockBase());
261
+ if (lockBase())
325
262
  clearLock(lockBase());
326
- }
263
+ if (lockBase())
264
+ releaseSessionLock(lockBase());
327
265
  clearSkillSnapshot();
328
266
  resetSkillTelemetry();
329
- s.dispatching = false;
330
- s.skipDepth = 0;
331
267
  // Remove SIGTERM handler registered at auto-mode start
332
268
  deregisterSigtermHandler();
333
269
  // ── Auto-worktree: exit worktree and reset s.basePath on stop ──
334
- if (s.currentMilestoneId && isInAutoWorktree(s.basePath)) {
335
- try {
336
- try {
337
- autoCommitCurrentBranch(s.basePath, "stop", s.currentMilestoneId);
338
- }
339
- catch (e) {
340
- debugLog("stop-auto-commit-failed", { error: getErrorMessage(e) });
341
- }
342
- teardownAutoWorktree(s.originalBasePath, s.currentMilestoneId, { preserveBranch: true });
343
- s.basePath = s.originalBasePath;
344
- s.gitService = createGitService(s.basePath);
345
- ctx?.ui.notify("Exited auto-worktree (branch preserved for resume).", "info");
346
- }
347
- catch (err) {
348
- ctx?.ui.notify(`Auto-worktree teardown failed: ${getErrorMessage(err)}`, "warning");
349
- }
270
+ if (s.currentMilestoneId) {
271
+ const notifyCtx = ctx
272
+ ? { notify: ctx.ui.notify.bind(ctx.ui) }
273
+ : { notify: () => { } };
274
+ buildResolver().exitMilestone(s.currentMilestoneId, notifyCtx, {
275
+ preserveBranch: true,
276
+ });
350
277
  }
351
278
  // ── DB cleanup: close the SQLite connection ──
352
279
  if (isDbAvailable()) {
@@ -355,7 +282,9 @@ export async function stopAuto(ctx, pi, reason) {
355
282
  closeDatabase();
356
283
  }
357
284
  catch (e) {
358
- debugLog("db-close-failed", { error: getErrorMessage(e) });
285
+ debugLog("db-close-failed", {
286
+ error: e instanceof Error ? e.message : String(e),
287
+ });
359
288
  }
360
289
  }
361
290
  if (s.originalBasePath) {
@@ -363,7 +292,9 @@ export async function stopAuto(ctx, pi, reason) {
363
292
  try {
364
293
  process.chdir(s.basePath);
365
294
  }
366
- catch { /* best-effort */ }
295
+ catch {
296
+ /* best-effort */
297
+ }
367
298
  }
368
299
  const ledger = getLedger();
369
300
  if (ledger && ledger.units.length > 0) {
@@ -378,7 +309,9 @@ export async function stopAuto(ctx, pi, reason) {
378
309
  await rebuildState(s.basePath);
379
310
  }
380
311
  catch (e) {
381
- debugLog("stop-rebuild-state-failed", { error: getErrorMessage(e) });
312
+ debugLog("stop-rebuild-state-failed", {
313
+ error: e instanceof Error ? e.message : String(e),
314
+ });
382
315
  }
383
316
  }
384
317
  if (isDebugEnabled()) {
@@ -397,7 +330,6 @@ export async function stopAuto(ctx, pi, reason) {
397
330
  s.stepMode = false;
398
331
  s.unitDispatchCount.clear();
399
332
  s.unitRecoveryCount.clear();
400
- s.unitConsecutiveSkips.clear();
401
333
  clearInFlightTools();
402
334
  s.lastBudgetAlertLevel = 0;
403
335
  s.lastStateRebuildAt = 0;
@@ -411,12 +343,10 @@ export async function stopAuto(ctx, pi, reason) {
411
343
  clearSliceProgressCache();
412
344
  clearActivityLogState();
413
345
  resetProactiveHealing();
414
- s.recentlyEvictedKeys.clear();
415
346
  s.pendingCrashRecovery = null;
416
347
  s.pendingVerificationRetry = null;
417
348
  s.verificationRetryCount.clear();
418
349
  s.pausedSessionFile = null;
419
- s.handlingAgentEnd = false;
420
350
  ctx?.ui.setStatus("gsd-auto", undefined);
421
351
  ctx?.ui.setWidget("gsd-progress", undefined);
422
352
  ctx?.ui.setFooter(undefined);
@@ -439,10 +369,10 @@ export async function pauseAuto(ctx, _pi) {
439
369
  return;
440
370
  clearUnitTimeout();
441
371
  s.pausedSessionFile = ctx?.sessionManager?.getSessionFile() ?? null;
442
- if (lockBase()) {
443
- releaseSessionLock(lockBase());
372
+ if (lockBase())
444
373
  clearLock(lockBase());
445
- }
374
+ if (lockBase())
375
+ releaseSessionLock(lockBase());
446
376
  deregisterSigtermHandler();
447
377
  s.active = false;
448
378
  s.paused = true;
@@ -454,13 +384,143 @@ export async function pauseAuto(ctx, _pi) {
454
384
  const resumeCmd = s.stepMode ? "/gsd next" : "/gsd auto";
455
385
  ctx?.ui.notify(`${s.stepMode ? "Step" : "Auto"}-mode paused (Escape). Type to interact, or ${resumeCmd} to resume.`, "info");
456
386
  }
387
+ /**
388
+ * Build a WorktreeResolverDeps from auto.ts private scope.
389
+ * Shared by buildResolver() and buildLoopDeps().
390
+ */
391
+ function buildResolverDeps() {
392
+ return {
393
+ isInAutoWorktree,
394
+ shouldUseWorktreeIsolation,
395
+ getIsolationMode,
396
+ mergeMilestoneToMain,
397
+ syncWorktreeStateBack,
398
+ teardownAutoWorktree,
399
+ createAutoWorktree,
400
+ enterAutoWorktree,
401
+ getAutoWorktreePath,
402
+ autoCommitCurrentBranch,
403
+ getCurrentBranch,
404
+ autoWorktreeBranch,
405
+ resolveMilestoneFile,
406
+ readFileSync: (path, encoding) => readFileSync(path, encoding),
407
+ GitServiceImpl: GitServiceImpl,
408
+ loadEffectiveGSDPreferences: loadEffectiveGSDPreferences,
409
+ invalidateAllCaches,
410
+ captureIntegrationBranch,
411
+ };
412
+ }
413
+ /**
414
+ * Build a WorktreeResolver wrapping the current session.
415
+ * Cheap to construct — it's just a thin wrapper over `s` + deps.
416
+ * Used by stopAuto(), resume path, and buildLoopDeps().
417
+ */
418
+ function buildResolver() {
419
+ return new WorktreeResolver(s, buildResolverDeps());
420
+ }
421
+ /**
422
+ * Build the LoopDeps object from auto.ts private scope.
423
+ * This bundles all private functions that autoLoop needs without exporting them.
424
+ */
425
+ function buildLoopDeps() {
426
+ return {
427
+ lockBase,
428
+ buildSnapshotOpts,
429
+ stopAuto,
430
+ pauseAuto,
431
+ clearUnitTimeout,
432
+ updateProgressWidget,
433
+ // State and cache
434
+ invalidateAllCaches,
435
+ deriveState,
436
+ loadEffectiveGSDPreferences,
437
+ // Pre-dispatch health gate
438
+ preDispatchHealthGate,
439
+ // Worktree sync
440
+ syncProjectRootToWorktree,
441
+ // Resource version guard
442
+ checkResourcesStale,
443
+ // Session lock
444
+ validateSessionLock,
445
+ updateSessionLock,
446
+ handleLostSessionLock,
447
+ // Milestone transition
448
+ sendDesktopNotification,
449
+ setActiveMilestoneId,
450
+ pruneQueueOrder,
451
+ isInAutoWorktree,
452
+ shouldUseWorktreeIsolation,
453
+ mergeMilestoneToMain,
454
+ teardownAutoWorktree,
455
+ createAutoWorktree,
456
+ captureIntegrationBranch,
457
+ getIsolationMode,
458
+ getCurrentBranch,
459
+ autoWorktreeBranch,
460
+ resolveMilestoneFile,
461
+ reconcileMergeState,
462
+ // Budget/context/secrets
463
+ getLedger,
464
+ getProjectTotals,
465
+ formatCost,
466
+ getBudgetAlertLevel,
467
+ getNewBudgetAlertLevel,
468
+ getBudgetEnforcementAction,
469
+ getManifestStatus,
470
+ collectSecretsFromManifest,
471
+ // Dispatch
472
+ resolveDispatch,
473
+ runPreDispatchHooks,
474
+ getPriorSliceCompletionBlocker,
475
+ getMainBranch,
476
+ collectObservabilityWarnings: _collectObservabilityWarnings,
477
+ buildObservabilityRepairBlock,
478
+ // Unit closeout + runtime records
479
+ closeoutUnit,
480
+ verifyExpectedArtifact,
481
+ clearUnitRuntimeRecord,
482
+ writeUnitRuntimeRecord,
483
+ recordOutcome,
484
+ writeLock,
485
+ captureAvailableSkills,
486
+ ensurePreconditions,
487
+ updateSliceProgressCache,
488
+ // Model selection + supervision
489
+ selectAndApplyModel,
490
+ startUnitSupervision,
491
+ // Prompt helpers
492
+ getDeepDiagnostic,
493
+ isDbAvailable,
494
+ reorderForCaching,
495
+ // Filesystem
496
+ existsSync,
497
+ readFileSync: (path, encoding) => readFileSync(path, encoding),
498
+ atomicWriteSync,
499
+ // Git
500
+ GitServiceImpl: GitServiceImpl,
501
+ // WorktreeResolver
502
+ resolver: buildResolver(),
503
+ // Post-unit processing
504
+ postUnitPreVerification,
505
+ runPostUnitVerification,
506
+ postUnitPostVerification,
507
+ // Session manager
508
+ getSessionFile: (ctx) => {
509
+ try {
510
+ return ctx.sessionManager?.getSessionFile() ?? "";
511
+ }
512
+ catch {
513
+ return "";
514
+ }
515
+ },
516
+ };
517
+ }
457
518
  export async function startAuto(ctx, pi, base, verboseMode, options) {
458
519
  const requestedStepMode = options?.step ?? false;
459
520
  // Escape stale worktree cwd from a previous milestone (#608).
460
521
  base = escapeStaleWorktree(base);
461
522
  // If resuming from paused state, just re-activate and dispatch next unit.
462
523
  if (s.paused) {
463
- // Re-acquire session lock before resuming
464
524
  const resumeLock = acquireSessionLock(base);
465
525
  if (!resumeLock.acquired) {
466
526
  ctx.ui.notify(`Cannot resume: ${resumeLock.reason}`, "error");
@@ -474,31 +534,20 @@ export async function startAuto(ctx, pi, base, verboseMode, options) {
474
534
  s.basePath = base;
475
535
  s.unitDispatchCount.clear();
476
536
  s.unitLifetimeDispatches.clear();
477
- s.unitConsecutiveSkips.clear();
478
537
  if (!getLedger())
479
538
  initMetrics(base);
480
539
  if (s.currentMilestoneId)
481
540
  setActiveMilestoneId(base, s.currentMilestoneId);
482
541
  // ── Auto-worktree: re-enter worktree on resume ──
483
- if (s.currentMilestoneId && shouldUseWorktreeIsolation() && s.originalBasePath && !isInAutoWorktree(s.basePath) && !detectWorktreeName(s.basePath) && !detectWorktreeName(s.originalBasePath)) {
484
- try {
485
- const existingWtPath = getAutoWorktreePath(s.originalBasePath, s.currentMilestoneId);
486
- if (existingWtPath) {
487
- const wtPath = enterAutoWorktree(s.originalBasePath, s.currentMilestoneId);
488
- s.basePath = wtPath;
489
- s.gitService = createGitService(s.basePath);
490
- ctx.ui.notify(`Re-entered auto-worktree at ${wtPath}`, "info");
491
- }
492
- else {
493
- const wtPath = createAutoWorktree(s.originalBasePath, s.currentMilestoneId);
494
- s.basePath = wtPath;
495
- s.gitService = createGitService(s.basePath);
496
- ctx.ui.notify(`Recreated auto-worktree at ${wtPath}`, "info");
497
- }
498
- }
499
- catch (err) {
500
- ctx.ui.notify(`Auto-worktree re-entry failed: ${getErrorMessage(err)}. Continuing at current path.`, "warning");
501
- }
542
+ if (s.currentMilestoneId &&
543
+ shouldUseWorktreeIsolation() &&
544
+ s.originalBasePath &&
545
+ !isInAutoWorktree(s.basePath) &&
546
+ !detectWorktreeName(s.basePath) &&
547
+ !detectWorktreeName(s.originalBasePath)) {
548
+ buildResolver().enterMilestone(s.currentMilestoneId, {
549
+ notify: ctx.ui.notify.bind(ctx.ui),
550
+ });
502
551
  }
503
552
  registerSigtermHandler(lockBase());
504
553
  ctx.ui.setStatus("gsd-auto", s.stepMode ? "next" : "auto");
@@ -509,7 +558,9 @@ export async function startAuto(ctx, pi, base, verboseMode, options) {
509
558
  await rebuildState(s.basePath);
510
559
  }
511
560
  catch (e) {
512
- debugLog("resume-rebuild-state-failed", { error: getErrorMessage(e) });
561
+ debugLog("resume-rebuild-state-failed", {
562
+ error: e instanceof Error ? e.message : String(e),
563
+ });
513
564
  }
514
565
  try {
515
566
  const report = await runGSDDoctor(s.basePath, { fix: true });
@@ -518,9 +569,10 @@ export async function startAuto(ctx, pi, base, verboseMode, options) {
518
569
  }
519
570
  }
520
571
  catch (e) {
521
- debugLog("resume-doctor-failed", { error: getErrorMessage(e) });
572
+ debugLog("resume-doctor-failed", {
573
+ error: e instanceof Error ? e.message : String(e),
574
+ });
522
575
  }
523
- await selfHealRuntimeRecords(s.basePath, ctx, s.completedKeySet);
524
576
  invalidateAllCaches();
525
577
  if (s.pausedSessionFile) {
526
578
  const activityDir = join(gsdRoot(s.basePath), "activity");
@@ -531,33 +583,9 @@ export async function startAuto(ctx, pi, base, verboseMode, options) {
531
583
  }
532
584
  s.pausedSessionFile = null;
533
585
  }
534
- // If resuming from a secrets pause, re-collect before dispatching (#1146)
535
- if (s.pausedForSecrets && s.currentMilestoneId) {
536
- try {
537
- const manifestStatus = await getManifestStatus(s.basePath, s.currentMilestoneId);
538
- if (manifestStatus && manifestStatus.pending.length > 0) {
539
- const result = await collectSecretsFromManifest(s.basePath, s.currentMilestoneId, ctx);
540
- if (result && result.applied.length > 0) {
541
- ctx.ui.notify(`Secrets collected: ${result.applied.length} applied, ${result.skipped.length} skipped, ${result.existingSkipped.length} already set.`, "info");
542
- }
543
- else if (result && result.applied.length === 0 && result.skipped.length > 0) {
544
- // All keys were skipped — still pending, re-pause
545
- s.paused = true;
546
- s.active = false;
547
- ctx.ui.notify(`All env variables were skipped. Auto-mode remains paused.\nCollect them with /gsd secrets, then resume with /gsd auto.`, "warning");
548
- ctx.ui.setStatus("gsd-auto", "paused");
549
- return;
550
- }
551
- }
552
- }
553
- catch (err) {
554
- ctx.ui.notify(`Secrets check error: ${getErrorMessage(err)}. Continuing without secrets.`, "warning");
555
- }
556
- s.pausedForSecrets = false;
557
- }
558
586
  updateSessionLock(lockBase(), "resuming", s.currentMilestoneId ?? "unknown", s.completedUnits.length);
559
587
  writeLock(lockBase(), "resuming", s.currentMilestoneId ?? "unknown", s.completedUnits.length);
560
- await dispatchNextUnit(ctx, pi);
588
+ await autoLoop(ctx, pi, s, buildLoopDeps());
561
589
  return;
562
590
  }
563
591
  // ── Fresh start path — delegated to auto-start.ts ──
@@ -565,186 +593,37 @@ export async function startAuto(ctx, pi, base, verboseMode, options) {
565
593
  shouldUseWorktreeIsolation,
566
594
  registerSigtermHandler,
567
595
  lockBase,
596
+ buildResolver,
568
597
  };
569
598
  const ready = await bootstrapAutoSession(s, ctx, pi, base, verboseMode, requestedStepMode, bootstrapDeps);
570
599
  if (!ready)
571
600
  return;
572
601
  // Dispatch the first unit
573
- await dispatchNextUnit(ctx, pi);
602
+ await autoLoop(ctx, pi, s, buildLoopDeps());
574
603
  }
575
604
  // ─── Agent End Handler ────────────────────────────────────────────────────────
576
- /** Guard against concurrent handleAgentEnd execution. */
577
- export async function handleAgentEnd(ctx, pi) {
578
- if (!s.active || !s.cmdCtx)
579
- return;
580
- if (s.handlingAgentEnd) {
581
- // Another agent_end arrived while we're still processing the previous one.
582
- // This happens when a unit dispatched inside handleAgentEnd (e.g. via hooks,
583
- // triage, or quick-task early-dispatch paths) completes before the outer
584
- // handleAgentEnd returns. Queue a retry so the completed unit's agent_end
585
- // is not silently dropped (#1072).
586
- s.pendingAgentEndRetry = true;
587
- return;
588
- }
589
- s.handlingAgentEnd = true;
590
- try {
591
- // Unit completed — clear its timeout
592
- clearUnitTimeout();
593
- // ── Pre-verification processing (commit, doctor, state rebuild, etc.) ──
594
- const postUnitCtx = {
595
- s,
596
- ctx,
597
- pi,
598
- buildSnapshotOpts,
599
- lockBase,
600
- stopAuto,
601
- pauseAuto,
602
- updateProgressWidget,
603
- };
604
- const preResult = await postUnitPreVerification(postUnitCtx);
605
- if (preResult === "dispatched")
606
- return;
607
- // ── Verification gate: run typecheck/lint/test after execute-task ──
608
- const verificationResult = await runPostUnitVerification({ s, ctx, pi }, dispatchNextUnit, startDispatchGapWatchdog, pauseAuto);
609
- if (verificationResult === "retry" || verificationResult === "pause")
610
- return;
611
- // ── Post-verification processing (DB dual-write, hooks, triage, quick-tasks) ──
612
- const postResult = await postUnitPostVerification(postUnitCtx);
613
- if (postResult === "dispatched" || postResult === "stopped")
614
- return;
615
- if (postResult === "step-wizard") {
616
- await showStepWizard(ctx, pi);
617
- return;
618
- }
619
- // ── Dispatch with hang detection (#1073) ────────────────────────────────
620
- // Start a safety watchdog BEFORE calling dispatchNextUnit. If dispatch
621
- // hangs at any await (newSession, model selection, etc.), the gap watchdog
622
- // inside handleAgentEnd never fires because we never reach the check.
623
- // This pre-dispatch watchdog ensures recovery even when dispatchNextUnit
624
- // itself is permanently blocked.
625
- const dispatchHangGuard = setTimeout(() => {
626
- if (!s.active)
627
- return;
628
- // dispatchNextUnit has been running for too long — it's likely hung.
629
- // Start the gap watchdog which will retry dispatch from scratch.
630
- if (!s.unitTimeoutHandle && !s.wrapupWarningHandle) {
631
- ctx.ui.notify(`Dispatch hang detected (${DISPATCH_HANG_TIMEOUT_MS / 1000}s without completion). Starting recovery watchdog.`, "warning");
632
- startDispatchGapWatchdog(ctx, pi);
633
- }
634
- }, DISPATCH_HANG_TIMEOUT_MS);
635
- try {
636
- await dispatchNextUnit(ctx, pi);
637
- }
638
- catch (dispatchErr) {
639
- const message = getErrorMessage(dispatchErr);
640
- ctx.ui.notify(`Dispatch error after unit completion: ${message}. Retrying in ${DISPATCH_GAP_TIMEOUT_MS / 1000}s.`, "error");
641
- startDispatchGapWatchdog(ctx, pi);
642
- return;
643
- }
644
- finally {
645
- clearTimeout(dispatchHangGuard);
646
- }
647
- if (s.active && !s.unitTimeoutHandle && !s.wrapupWarningHandle) {
648
- startDispatchGapWatchdog(ctx, pi);
649
- }
650
- }
651
- finally {
652
- s.handlingAgentEnd = false;
653
- // If an agent_end event was dropped by the reentrancy guard while we were
654
- // processing, re-enter handleAgentEnd on the next microtask. This prevents
655
- // the summarizing phase stall (#1072) where a unit dispatched inside
656
- // handleAgentEnd (hooks, triage, quick-task) completes before we return,
657
- // and its agent_end is silently dropped — leaving auto-mode active but
658
- // permanently stalled with no unit running and no watchdog set.
659
- if (s.pendingAgentEndRetry) {
660
- s.pendingAgentEndRetry = false;
661
- // Clear gap watchdog from the previous cycle to prevent concurrent
662
- // dispatch when the deferred handleAgentEnd calls dispatchNextUnit (#1272).
663
- clearDispatchGapWatchdog();
664
- setImmediate(() => {
665
- handleAgentEnd(ctx, pi).catch((err) => {
666
- const msg = getErrorMessage(err);
667
- ctx.ui.notify(`Deferred agent_end retry failed: ${msg}`, "error");
668
- pauseAuto(ctx, pi).catch(() => { });
669
- });
670
- });
671
- }
672
- }
673
- }
674
- // ─── Step Mode Wizard ─────────────────────────────────────────────────────
675
605
  /**
676
- * Show the step-mode wizard after a unit completes.
606
+ * Deprecated thin wrapper kept as export for backward compatibility.
607
+ * The actual agent_end processing now happens via resolveAgentEnd() in auto-loop.ts,
608
+ * which is called directly from index.ts. The autoLoop() while loop handles all
609
+ * post-unit processing (verification, hooks, dispatch) that this function used to do.
610
+ *
611
+ * If called by straggler code, it simply resolves the pending promise so the loop
612
+ * can continue.
677
613
  */
678
- async function showStepWizard(ctx, pi) {
679
- if (!s.cmdCtx)
680
- return;
681
- const state = await deriveState(s.basePath);
682
- const mid = state.activeMilestone?.id;
683
- const justFinished = s.currentUnit
684
- ? `${unitVerb(s.currentUnit.type)} ${s.currentUnit.id}`
685
- : "previous unit";
686
- if (!mid || state.phase === "complete") {
687
- const incomplete = (state.registry ?? []).filter(m => m.status !== "complete" && m.status !== "parked");
688
- if (incomplete.length > 0 && state.phase !== "complete" && state.phase !== "blocked" && state.phase !== "pre-planning") {
689
- const ids = incomplete.map(m => m.id).join(", ");
690
- const diag = `basePath=${s.basePath}, milestones=[${state.registry.map(m => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
691
- ctx.ui.notify(`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`, "error");
692
- await stopAuto(ctx, pi, `No active milestone — ${incomplete.length} incomplete (${ids})`);
693
- }
694
- else {
695
- await stopAuto(ctx, pi, state.phase === "complete" ? "All work complete" : "No active milestone");
696
- }
614
+ export async function handleAgentEnd(ctx, pi) {
615
+ if (!s.active || !s.cmdCtx)
697
616
  return;
698
- }
699
- const nextDesc = _describeNextUnit(state);
700
- const choice = await showNextAction(s.cmdCtx, {
701
- title: `GSD — ${justFinished} complete`,
702
- summary: [
703
- `${mid}: ${state.activeMilestone?.title ?? mid}`,
704
- ...(state.activeSlice ? [`${state.activeSlice.id}: ${state.activeSlice.title}`] : []),
705
- ],
706
- actions: [
707
- {
708
- id: "continue",
709
- label: nextDesc.label,
710
- description: nextDesc.description,
711
- recommended: true,
712
- },
713
- {
714
- id: "auto",
715
- label: "Switch to auto",
716
- description: "Continue without pausing between steps.",
717
- },
718
- {
719
- id: "status",
720
- label: "View status",
721
- description: "Open the dashboard.",
722
- },
723
- ],
724
- notYetMessage: "Run /gsd next when ready to continue.",
725
- });
726
- if (choice === "continue") {
727
- await dispatchNextUnit(ctx, pi);
728
- }
729
- else if (choice === "auto") {
730
- s.stepMode = false;
731
- ctx.ui.setStatus("gsd-auto", "auto");
732
- ctx.ui.notify("Switched to auto-mode.", "info");
733
- await dispatchNextUnit(ctx, pi);
734
- }
735
- else if (choice === "status") {
736
- const { fireStatusViaCommand } = await import("./commands.js");
737
- await fireStatusViaCommand(ctx);
738
- await showStepWizard(ctx, pi);
739
- }
740
- else {
741
- await pauseAuto(ctx, pi);
742
- }
617
+ clearUnitTimeout();
618
+ resolveAgentEnd({ messages: [] });
743
619
  }
620
+ // describeNextUnit is imported from auto-dashboard.ts and re-exported
621
+ export { describeNextUnit } from "./auto-dashboard.js";
744
622
  /** Thin wrapper: delegates to auto-dashboard.ts, passing state accessors. */
745
623
  function updateProgressWidget(ctx, unitType, unitId, state) {
746
624
  const badge = s.currentUnitRouting?.tier
747
- ? ({ light: "L", standard: "S", heavy: "H" }[s.currentUnitRouting.tier] ?? undefined)
625
+ ? ({ light: "L", standard: "S", heavy: "H" }[s.currentUnitRouting.tier] ??
626
+ undefined)
748
627
  : undefined;
749
628
  _updateProgressWidget(ctx, unitType, unitId, state, widgetStateAccessors, badge);
750
629
  }
@@ -756,637 +635,21 @@ const widgetStateAccessors = {
756
635
  getBasePath: () => s.basePath,
757
636
  isVerbose: () => s.verbose,
758
637
  };
759
- // ─── Core Loop ────────────────────────────────────────────────────────────────
760
- async function dispatchNextUnit(ctx, pi) {
761
- if (!s.active || !s.cmdCtx) {
762
- debugLog(`dispatchNextUnit early return — active=${s.active}, cmdCtx=${!!s.cmdCtx}`);
763
- if (s.active && !s.cmdCtx) {
764
- ctx.ui.notify("Auto-mode session expired. Run /gsd auto to restart.", "info");
765
- }
766
- return;
767
- }
768
- // ── Session lock validation: detect if another process has taken over ──
769
- if (lockBase() && !validateSessionLock(lockBase())) {
770
- debugLog("dispatchNextUnit session-lock-lost — another process may have taken over");
771
- ctx.ui.notify("Session lock lost — another GSD process appears to have taken over. Stopping gracefully.", "error");
772
- // Don't call stopAuto here to avoid releasing the lock we don't own
773
- s.active = false;
774
- s.paused = false;
775
- clearUnitTimeout();
776
- deregisterSigtermHandler();
777
- ctx.ui.setStatus("gsd-auto", undefined);
778
- ctx.ui.setWidget("gsd-progress", undefined);
779
- ctx.ui.setFooter(undefined);
780
- return;
781
- }
782
- // Reentrancy guard — unconditional to prevent concurrent dispatch from
783
- // gap watchdog or pendingAgentEndRetry during skip chains (#1272).
784
- // Previously the guard was bypassed when skipDepth > 0, but the recursive
785
- // skip chain's inner finally block resets s.dispatching = false before the
786
- // outer call's finally runs, opening a window for concurrent entry.
787
- if (s.dispatching) {
788
- debugLog("dispatchNextUnit reentrancy guard — another dispatch in progress, bailing");
789
- return;
790
- }
791
- s.dispatching = true;
792
- try {
793
- // Recursion depth guard
794
- if (s.skipDepth > MAX_SKIP_DEPTH) {
795
- s.skipDepth = 0;
796
- ctx.ui.notify(`Skipped ${MAX_SKIP_DEPTH}+ completed units. Yielding to UI before continuing.`, "info");
797
- await new Promise(r => setTimeout(r, 200));
798
- }
799
- // Resource version guard
800
- const staleMsg = checkResourcesStale(s.resourceVersionOnStart);
801
- if (staleMsg) {
802
- await stopAuto(ctx, pi, staleMsg);
803
- return;
804
- }
805
- invalidateAllCaches();
806
- s.lastPromptCharCount = undefined;
807
- s.lastBaselineCharCount = undefined;
808
- // ── Pre-dispatch health gate ──
809
- try {
810
- const healthGate = await preDispatchHealthGate(s.basePath);
811
- if (healthGate.fixesApplied.length > 0) {
812
- ctx.ui.notify(`Pre-dispatch: ${healthGate.fixesApplied.join(", ")}`, "info");
813
- }
814
- if (!healthGate.proceed) {
815
- ctx.ui.notify(healthGate.reason ?? "Pre-dispatch health check failed.", "error");
816
- await pauseAuto(ctx, pi);
817
- return;
818
- }
819
- }
820
- catch {
821
- // Non-fatal
822
- }
823
- const stopDeriveTimer = debugTime("derive-state");
824
- let state = await deriveState(s.basePath);
825
- stopDeriveTimer({
826
- phase: state.phase,
827
- milestone: state.activeMilestone?.id,
828
- slice: state.activeSlice?.id,
829
- task: state.activeTask?.id,
830
- });
831
- let mid = state.activeMilestone?.id;
832
- let midTitle = state.activeMilestone?.title;
833
- // Detect milestone transition
834
- if (mid && s.currentMilestoneId && mid !== s.currentMilestoneId) {
835
- ctx.ui.notify(`Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}: ${midTitle}.`, "info");
836
- sendDesktopNotification("GSD", `Milestone ${s.currentMilestoneId} complete!`, "success", "milestone");
837
- const vizPrefs = loadEffectiveGSDPreferences()?.preferences;
838
- if (vizPrefs?.auto_visualize) {
839
- ctx.ui.notify("Run /gsd visualize to see progress overview.", "info");
840
- }
841
- if (vizPrefs?.auto_report !== false) {
842
- try {
843
- const { loadVisualizerData } = await import("./visualizer-data.js");
844
- const { generateHtmlReport } = await import("./export-html.js");
845
- const { writeReportSnapshot, reportsDir } = await import("./reports.js");
846
- const { basename } = await import("node:path");
847
- const snapData = await loadVisualizerData(s.basePath);
848
- const completedMs = snapData.milestones.find(m => m.id === s.currentMilestoneId);
849
- const msTitle = completedMs?.title ?? s.currentMilestoneId;
850
- const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
851
- const projName = basename(s.basePath);
852
- const doneSlices = snapData.milestones.reduce((s, m) => s + m.slices.filter(sl => sl.done).length, 0);
853
- const totalSlices = snapData.milestones.reduce((s, m) => s + m.slices.length, 0);
854
- const outPath = writeReportSnapshot({ basePath: s.basePath,
855
- html: generateHtmlReport(snapData, {
856
- projectName: projName,
857
- projectPath: s.basePath,
858
- gsdVersion,
859
- milestoneId: s.currentMilestoneId,
860
- indexRelPath: "index.html",
861
- }),
862
- milestoneId: s.currentMilestoneId,
863
- milestoneTitle: msTitle,
864
- kind: "milestone",
865
- projectName: projName,
866
- projectPath: s.basePath,
867
- gsdVersion,
868
- totalCost: snapData.totals?.cost ?? 0,
869
- totalTokens: snapData.totals?.tokens.total ?? 0,
870
- totalDuration: snapData.totals?.duration ?? 0,
871
- doneSlices,
872
- totalSlices,
873
- doneMilestones: snapData.milestones.filter(m => m.status === "complete").length,
874
- totalMilestones: snapData.milestones.length,
875
- phase: snapData.phase,
876
- });
877
- ctx.ui.notify(`Report saved: .gsd/reports/${basename(outPath)} — open index.html to browse progression.`, "info");
878
- }
879
- catch (err) {
880
- ctx.ui.notify(`Report generation failed: ${getErrorMessage(err)}`, "warning");
881
- }
882
- }
883
- // Reset stuck detection for new milestone
884
- s.unitDispatchCount.clear();
885
- s.unitRecoveryCount.clear();
886
- s.unitConsecutiveSkips.clear();
887
- s.unitLifetimeDispatches.clear();
888
- try {
889
- const file = completedKeysPath(s.basePath);
890
- if (existsSync(file)) {
891
- atomicWriteSync(file, JSON.stringify([]));
892
- }
893
- s.completedKeySet.clear();
894
- }
895
- catch (e) {
896
- debugLog("completed-keys-reset-failed", { error: getErrorMessage(e) });
897
- }
898
- // ── Worktree lifecycle on milestone transition (#616) ──
899
- if ((isInAutoWorktree(s.basePath) || getIsolationMode() === "branch") && shouldUseWorktreeIsolation()) {
900
- tryMergeMilestone(ctx, s.currentMilestoneId, "transition");
901
- // Reset to project root and re-derive state for the new milestone
902
- if (s.originalBasePath) {
903
- s.basePath = s.originalBasePath;
904
- s.gitService = createGitService(s.basePath);
905
- }
906
- invalidateAllCaches();
907
- state = await deriveState(s.basePath);
908
- mid = state.activeMilestone?.id;
909
- midTitle = state.activeMilestone?.title;
910
- if (mid) {
911
- captureIntegrationBranch(s.basePath, mid);
912
- try {
913
- const wtPath = createAutoWorktree(s.basePath, mid);
914
- s.basePath = wtPath;
915
- s.gitService = createGitService(s.basePath);
916
- ctx.ui.notify(`Created auto-worktree for ${mid} at ${wtPath}`, "info");
917
- }
918
- catch (err) {
919
- ctx.ui.notify(`Auto-worktree creation for ${mid} failed: ${getErrorMessage(err)}. Continuing in project root.`, "warning");
920
- }
921
- }
922
- }
923
- else {
924
- if (getIsolationMode() !== "none") {
925
- captureIntegrationBranch(s.originalBasePath || s.basePath, mid);
926
- }
927
- }
928
- const pendingIds = (state.registry ?? [])
929
- .filter(m => m.status !== "complete")
930
- .map(m => m.id);
931
- pruneQueueOrder(s.basePath, pendingIds);
932
- }
933
- if (mid) {
934
- s.currentMilestoneId = mid;
935
- setActiveMilestoneId(s.basePath, mid);
936
- }
937
- if (!mid) {
938
- if (s.currentUnit) {
939
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
940
- }
941
- const incomplete = (state.registry ?? []).filter(m => m.status !== "complete" && m.status !== "parked");
942
- if (incomplete.length === 0) {
943
- // Genuinely all complete (parked milestones excluded) — merge milestone branch to main before stopping (#962)
944
- if (s.currentMilestoneId) {
945
- tryMergeMilestone(ctx, s.currentMilestoneId, "complete");
946
- }
947
- sendDesktopNotification("GSD", "All milestones complete!", "success", "milestone");
948
- await stopAuto(ctx, pi, "All milestones complete");
949
- }
950
- else if (state.phase === "blocked") {
951
- const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
952
- await stopAuto(ctx, pi, blockerMsg);
953
- ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
954
- sendDesktopNotification("GSD", blockerMsg, "error", "attention");
955
- }
956
- else {
957
- const ids = incomplete.map(m => m.id).join(", ");
958
- const diag = `basePath=${s.basePath}, milestones=[${state.registry.map(m => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
959
- ctx.ui.notify(`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`, "error");
960
- await stopAuto(ctx, pi, `No active milestone — ${incomplete.length} incomplete (${ids}), see diagnostic above`);
961
- }
962
- return;
963
- }
964
- if (!midTitle) {
965
- midTitle = mid;
966
- ctx.ui.notify(`Milestone ${mid} has no title in roadmap — using ID as fallback.`, "warning");
967
- }
968
- // ── Mid-merge safety check ──
969
- if (reconcileMergeState(s.basePath, ctx)) {
970
- invalidateAllCaches();
971
- state = await deriveState(s.basePath);
972
- mid = state.activeMilestone?.id;
973
- midTitle = state.activeMilestone?.title;
974
- }
975
- if (!mid || !midTitle) {
976
- if (s.currentUnit) {
977
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
978
- }
979
- const noMilestoneReason = !mid
980
- ? "No active milestone after merge reconciliation"
981
- : `Milestone ${mid} has no title after reconciliation`;
982
- await stopAuto(ctx, pi, noMilestoneReason);
983
- return;
984
- }
985
- // Determine next unit
986
- let unitType;
987
- let unitId;
988
- let prompt;
989
- if (state.phase === "complete") {
990
- if (s.currentUnit) {
991
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
992
- }
993
- try {
994
- const file = completedKeysPath(s.basePath);
995
- if (existsSync(file)) {
996
- atomicWriteSync(file, JSON.stringify([]));
997
- }
998
- s.completedKeySet.clear();
999
- }
1000
- catch (e) {
1001
- debugLog("completed-keys-reset-failed", { error: getErrorMessage(e) });
1002
- }
1003
- // ── Milestone merge ──
1004
- if (s.currentMilestoneId) {
1005
- tryMergeMilestone(ctx, s.currentMilestoneId, "complete");
1006
- }
1007
- sendDesktopNotification("GSD", `Milestone ${mid} complete!`, "success", "milestone");
1008
- await stopAuto(ctx, pi, `Milestone ${mid} complete`);
1009
- return;
1010
- }
1011
- if (state.phase === "blocked") {
1012
- if (s.currentUnit) {
1013
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1014
- }
1015
- const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
1016
- await stopAuto(ctx, pi, blockerMsg);
1017
- ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
1018
- sendDesktopNotification("GSD", blockerMsg, "error", "attention");
1019
- return;
1020
- }
1021
- // Budget ceiling guard, context window guard, secrets gate, dispatch table
1022
- const prefs = loadEffectiveGSDPreferences()?.preferences;
1023
- const budgetCeiling = prefs?.budget_ceiling;
1024
- if (budgetCeiling !== undefined && budgetCeiling > 0) {
1025
- const currentLedger = getLedger();
1026
- const totalCost = currentLedger ? getProjectTotals(currentLedger.units).cost : 0;
1027
- const budgetPct = totalCost / budgetCeiling;
1028
- const budgetAlertLevel = getBudgetAlertLevel(budgetPct);
1029
- const newBudgetAlertLevel = getNewBudgetAlertLevel(s.lastBudgetAlertLevel, budgetPct);
1030
- const enforcement = prefs?.budget_enforcement ?? "pause";
1031
- const budgetEnforcementAction = getBudgetEnforcementAction(enforcement, budgetPct);
1032
- if (newBudgetAlertLevel === 100 && budgetEnforcementAction !== "none") {
1033
- const msg = `Budget ceiling ${formatCost(budgetCeiling)} reached (spent ${formatCost(totalCost)}).`;
1034
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1035
- if (budgetEnforcementAction === "halt") {
1036
- sendDesktopNotification("GSD", msg, "error", "budget");
1037
- await stopAuto(ctx, pi, "Budget ceiling reached");
1038
- return;
1039
- }
1040
- if (budgetEnforcementAction === "pause") {
1041
- ctx.ui.notify(`${msg} Pausing auto-mode — /gsd auto to override and continue.`, "warning");
1042
- sendDesktopNotification("GSD", msg, "warning", "budget");
1043
- await pauseAuto(ctx, pi);
1044
- return;
1045
- }
1046
- ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
1047
- sendDesktopNotification("GSD", msg, "warning", "budget");
1048
- }
1049
- else if (newBudgetAlertLevel === 90) {
1050
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1051
- ctx.ui.notify(`Budget 90%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning");
1052
- sendDesktopNotification("GSD", `Budget 90%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning", "budget");
1053
- }
1054
- else if (newBudgetAlertLevel === 80) {
1055
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1056
- ctx.ui.notify(`Approaching budget ceiling — 80%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning");
1057
- sendDesktopNotification("GSD", `Approaching budget ceiling — 80%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "warning", "budget");
1058
- }
1059
- else if (newBudgetAlertLevel === 75) {
1060
- s.lastBudgetAlertLevel = newBudgetAlertLevel;
1061
- ctx.ui.notify(`Budget 75%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "info");
1062
- sendDesktopNotification("GSD", `Budget 75%: ${formatCost(totalCost)} / ${formatCost(budgetCeiling)}`, "info", "budget");
1063
- }
1064
- else if (budgetAlertLevel === 0) {
1065
- s.lastBudgetAlertLevel = 0;
1066
- }
1067
- }
1068
- else {
1069
- s.lastBudgetAlertLevel = 0;
1070
- }
1071
- const contextThreshold = prefs?.context_pause_threshold ?? 0;
1072
- if (contextThreshold > 0 && s.cmdCtx) {
1073
- const contextUsage = s.cmdCtx.getContextUsage();
1074
- if (contextUsage && contextUsage.percent !== null && contextUsage.percent >= contextThreshold) {
1075
- const msg = `Context window at ${contextUsage.percent}% (threshold: ${contextThreshold}%). Pausing to prevent truncated output.`;
1076
- ctx.ui.notify(`${msg} Run /gsd auto to continue (will start fresh session).`, "warning");
1077
- sendDesktopNotification("GSD", `Context ${contextUsage.percent}% — paused`, "warning", "attention");
1078
- await pauseAuto(ctx, pi);
1079
- return;
1080
- }
1081
- }
1082
- // Secrets re-check gate
1083
- const runSecretsGate = async () => {
1084
- try {
1085
- const manifestStatus = await getManifestStatus(s.basePath, mid);
1086
- if (manifestStatus && manifestStatus.pending.length > 0) {
1087
- const result = await collectSecretsFromManifest(s.basePath, mid, ctx);
1088
- if (result && result.applied && result.skipped && result.existingSkipped) {
1089
- ctx.ui.notify(`Secrets collected: ${result.applied.length} applied, ${result.skipped.length} skipped, ${result.existingSkipped.length} already set.`, "info");
1090
- }
1091
- else {
1092
- ctx.ui.notify("Secrets collection skipped.", "info");
1093
- }
1094
- }
1095
- }
1096
- catch (err) {
1097
- ctx.ui.notify(`Secrets collection error: ${getErrorMessage(err)}. Continuing with next task.`, "warning");
1098
- }
1099
- };
1100
- await runSecretsGate();
1101
- // ── Interactive discussion gate ──
1102
- // If the active milestone needs discussion (has CONTEXT-DRAFT.md but no roadmap),
1103
- // stop auto-mode and route to the interactive discussion flow. The guided-flow
1104
- // handles needs-discussion correctly — it just needs to be called instead of
1105
- // letting the dispatch table fire "needs-discussion → stop" (#1170).
1106
- if (state.phase === "needs-discussion") {
1107
- if (s.currentUnit) {
1108
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1109
- }
1110
- const cmdCtx = s.cmdCtx;
1111
- const basePath = s.basePath;
1112
- await stopAuto(ctx, pi, `${mid}: ${midTitle} needs discussion before planning.`);
1113
- const { showSmartEntry } = await import("./guided-flow.js");
1114
- await showSmartEntry(cmdCtx, pi, basePath);
1115
- return;
1116
- }
1117
- // ── Dispatch table ──
1118
- const dispatchResult = await resolveDispatch({ basePath: s.basePath, mid, midTitle: midTitle, state, prefs, });
1119
- if (dispatchResult.action === "stop") {
1120
- if (s.currentUnit) {
1121
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1122
- }
1123
- await stopAuto(ctx, pi, dispatchResult.reason);
1124
- return;
1125
- }
1126
- if (dispatchResult.action !== "dispatch") {
1127
- // Defer re-dispatch to next microtask so s.dispatching is released first,
1128
- // preventing reentrancy guard bypass during concurrent entry (#1272).
1129
- setImmediate(() => dispatchNextUnit(ctx, pi).catch(err => {
1130
- ctx.ui.notify(`Deferred dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1131
- pauseAuto(ctx, pi).catch(() => { });
1132
- }));
1133
- return;
1134
- }
1135
- unitType = dispatchResult.unitType;
1136
- unitId = dispatchResult.unitId;
1137
- prompt = dispatchResult.prompt;
1138
- // ── Pre-dispatch hooks ──
1139
- const preDispatchResult = runPreDispatchHooks(unitType, unitId, prompt, s.basePath);
1140
- if (preDispatchResult.firedHooks.length > 0) {
1141
- ctx.ui.notify(`Pre-dispatch hook${preDispatchResult.firedHooks.length > 1 ? "s" : ""}: ${preDispatchResult.firedHooks.join(", ")}`, "info");
1142
- }
1143
- if (preDispatchResult.action === "skip") {
1144
- ctx.ui.notify(`Skipping ${unitType} ${unitId} (pre-dispatch hook).`, "info");
1145
- setImmediate(() => dispatchNextUnit(ctx, pi).catch(err => {
1146
- ctx.ui.notify(`Deferred dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1147
- pauseAuto(ctx, pi).catch(() => { });
1148
- }));
1149
- return;
1150
- }
1151
- if (preDispatchResult.action === "replace") {
1152
- prompt = preDispatchResult.prompt ?? prompt;
1153
- if (preDispatchResult.unitType)
1154
- unitType = preDispatchResult.unitType;
1155
- }
1156
- else if (preDispatchResult.prompt) {
1157
- prompt = preDispatchResult.prompt;
1158
- }
1159
- const priorSliceBlocker = getPriorSliceCompletionBlocker(s.basePath, getMainBranch(s.basePath), unitType, unitId);
1160
- if (priorSliceBlocker) {
1161
- await stopAuto(ctx, pi, priorSliceBlocker);
1162
- return;
1163
- }
1164
- const observabilityIssues = await _collectObservabilityWarnings(ctx, s.basePath, unitType, unitId);
1165
- // ── Idempotency check (delegated to auto-idempotency.ts) ──
1166
- const idempotencyResult = checkIdempotency({
1167
- s,
1168
- unitType,
1169
- unitId,
1170
- basePath: s.basePath,
1171
- notify: (msg, level) => ctx.ui.notify(msg, level),
1172
- });
1173
- if (idempotencyResult.action === "skip") {
1174
- if (idempotencyResult.reason === "completed" || idempotencyResult.reason === "fallback-persisted" || idempotencyResult.reason === "phantom-loop-cleared" || idempotencyResult.reason === "evicted") {
1175
- if (!s.active)
1176
- return;
1177
- s.skipDepth++;
1178
- const skipDelay = idempotencyResult.reason === "phantom-loop-cleared" ? 50 : 150;
1179
- // Defer re-dispatch so s.dispatching is released first (#1272).
1180
- setTimeout(() => {
1181
- dispatchNextUnit(ctx, pi).catch(err => {
1182
- ctx.ui.notify(`Deferred skip-dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1183
- pauseAuto(ctx, pi).catch(() => { });
1184
- }).finally(() => {
1185
- s.skipDepth = Math.max(0, s.skipDepth - 1);
1186
- });
1187
- }, skipDelay);
1188
- return;
1189
- }
1190
- }
1191
- else if (idempotencyResult.action === "stop") {
1192
- await stopAuto(ctx, pi, idempotencyResult.reason);
1193
- ctx.ui.notify(`Hard loop detected: ${unitType} ${unitId} hit lifetime cap during skip cycle.`, "error");
1194
- return;
1195
- }
1196
- // "rerun" and "proceed" fall through to stuck detection
1197
- // ── Stuck detection (delegated to auto-stuck-detection.ts) ──
1198
- const stuckResult = await checkStuckAndRecover({
1199
- s,
1200
- ctx,
1201
- unitType,
1202
- unitId,
1203
- basePath: s.basePath,
1204
- buildSnapshotOpts: () => buildSnapshotOpts(unitType, unitId),
1205
- });
1206
- if (stuckResult.action === "stop") {
1207
- await stopAuto(ctx, pi, stuckResult.reason);
1208
- if (stuckResult.notifyMessage) {
1209
- ctx.ui.notify(stuckResult.notifyMessage, "error");
1210
- }
1211
- return;
1212
- }
1213
- if (stuckResult.action === "recovered" && stuckResult.dispatchAgain) {
1214
- // Defer re-dispatch so s.dispatching is released first (#1272).
1215
- setImmediate(() => dispatchNextUnit(ctx, pi).catch(err => {
1216
- ctx.ui.notify(`Deferred recovery-dispatch failed: ${err instanceof Error ? err.message : String(err)}`, "error");
1217
- pauseAuto(ctx, pi).catch(() => { });
1218
- }));
1219
- return;
1220
- }
1221
- // Snapshot metrics + activity log for the PREVIOUS unit before we reassign.
1222
- if (s.currentUnit) {
1223
- await closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
1224
- if (s.currentUnitRouting) {
1225
- const isRetry = s.currentUnit.type === unitType && s.currentUnit.id === unitId;
1226
- recordOutcome(s.currentUnit.type, s.currentUnitRouting.tier, !isRetry);
1227
- }
1228
- const closeoutKey = `${s.currentUnit.type}/${s.currentUnit.id}`;
1229
- const incomingKey = `${unitType}/${unitId}`;
1230
- const isHookUnit = s.currentUnit.type.startsWith("hook/");
1231
- const artifactVerified = isHookUnit || verifyExpectedArtifact(s.currentUnit.type, s.currentUnit.id, s.basePath);
1232
- if (closeoutKey !== incomingKey && artifactVerified) {
1233
- if (!isHookUnit) {
1234
- persistCompletedKey(s.basePath, closeoutKey);
1235
- s.completedKeySet.add(closeoutKey);
1236
- }
1237
- s.completedUnits.push({
1238
- type: s.currentUnit.type,
1239
- id: s.currentUnit.id,
1240
- startedAt: s.currentUnit.startedAt,
1241
- finishedAt: Date.now(),
1242
- });
1243
- if (s.completedUnits.length > 200) {
1244
- s.completedUnits = s.completedUnits.slice(-200);
1245
- }
1246
- clearUnitRuntimeRecord(s.basePath, s.currentUnit.type, s.currentUnit.id);
1247
- s.unitDispatchCount.delete(`${s.currentUnit.type}/${s.currentUnit.id}`);
1248
- s.unitRecoveryCount.delete(`${s.currentUnit.type}/${s.currentUnit.id}`);
1249
- }
1250
- }
1251
- s.currentUnit = { type: unitType, id: unitId, startedAt: Date.now() };
1252
- captureAvailableSkills();
1253
- writeUnitRuntimeRecord(s.basePath, unitType, unitId, s.currentUnit.startedAt, {
1254
- phase: "dispatched",
1255
- wrapupWarningSent: false,
1256
- timeoutAt: null,
1257
- lastProgressAt: s.currentUnit.startedAt,
1258
- progressCount: 0,
1259
- lastProgressKind: "dispatch",
1260
- });
1261
- // Status bar + progress widget
1262
- ctx.ui.setStatus("gsd-auto", "auto");
1263
- if (mid)
1264
- updateSliceProgressCache(s.basePath, mid, state.activeSlice?.id);
1265
- updateProgressWidget(ctx, unitType, unitId, state);
1266
- ensurePreconditions(unitType, unitId, s.basePath, state);
1267
- // Fresh session — with timeout to prevent permanent hangs (#1073).
1268
- // If newSession() hangs (e.g., session manager deadlock, network issue),
1269
- // without this timeout the entire dispatch chain stalls permanently: no
1270
- // timeouts are set, no gap watchdog fires, and auto-mode is left active
1271
- // but idle until the user Ctrl+C's.
1272
- let result;
1273
- try {
1274
- const sessionPromise = s.cmdCtx.newSession();
1275
- const timeoutPromise = new Promise((resolve) => setTimeout(() => resolve({ cancelled: true }), NEW_SESSION_TIMEOUT_MS));
1276
- result = await Promise.race([sessionPromise, timeoutPromise]);
1277
- }
1278
- catch (sessionErr) {
1279
- const msg = getErrorMessage(sessionErr);
1280
- ctx.ui.notify(`Session creation failed: ${msg}. Retrying via watchdog.`, "error");
1281
- throw new Error(`newSession() failed: ${msg}`);
1282
- }
1283
- if (result.cancelled) {
1284
- ctx.ui.notify(`Session creation timed out or was cancelled for ${unitType} ${unitId}. Will retry.`, "warning");
1285
- await stopAuto(ctx, pi, "Session creation failed");
1286
- return;
1287
- }
1288
- const sessionFile = ctx.sessionManager.getSessionFile();
1289
- updateSessionLock(lockBase(), unitType, unitId, s.completedUnits.length, sessionFile);
1290
- writeLock(lockBase(), unitType, unitId, s.completedUnits.length, sessionFile);
1291
- // Prompt injection
1292
- const MAX_RECOVERY_CHARS = 50_000;
1293
- let finalPrompt = prompt;
1294
- if (s.pendingVerificationRetry) {
1295
- const retryCtx = s.pendingVerificationRetry;
1296
- s.pendingVerificationRetry = null;
1297
- const capped = retryCtx.failureContext.length > MAX_RECOVERY_CHARS
1298
- ? retryCtx.failureContext.slice(0, MAX_RECOVERY_CHARS) + "\n\n[...failure context truncated]"
1299
- : retryCtx.failureContext;
1300
- 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}`;
1301
- }
1302
- if (s.pendingCrashRecovery) {
1303
- const capped = s.pendingCrashRecovery.length > MAX_RECOVERY_CHARS
1304
- ? s.pendingCrashRecovery.slice(0, MAX_RECOVERY_CHARS) + "\n\n[...recovery briefing truncated to prevent memory exhaustion]"
1305
- : s.pendingCrashRecovery;
1306
- finalPrompt = `${capped}\n\n---\n\n${finalPrompt}`;
1307
- s.pendingCrashRecovery = null;
1308
- }
1309
- else if ((s.unitDispatchCount.get(`${unitType}/${unitId}`) ?? 0) > 1) {
1310
- const diagnostic = getDeepDiagnostic(s.basePath);
1311
- if (diagnostic) {
1312
- const cappedDiag = diagnostic.length > MAX_RECOVERY_CHARS
1313
- ? diagnostic.slice(0, MAX_RECOVERY_CHARS) + "\n\n[...diagnostic truncated to prevent memory exhaustion]"
1314
- : diagnostic;
1315
- 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}`;
1316
- }
1317
- }
1318
- const repairBlock = buildObservabilityRepairBlock(observabilityIssues);
1319
- if (repairBlock) {
1320
- finalPrompt = `${finalPrompt}${repairBlock}`;
1321
- }
1322
- // ── Prompt char measurement ──
1323
- s.lastPromptCharCount = finalPrompt.length;
1324
- s.lastBaselineCharCount = undefined;
1325
- if (isDbAvailable()) {
1326
- try {
1327
- const { inlineGsdRootFile } = await import("./auto-prompts.js");
1328
- const [decisionsContent, requirementsContent, projectContent] = await Promise.all([
1329
- inlineGsdRootFile(s.basePath, "decisions.md", "Decisions"),
1330
- inlineGsdRootFile(s.basePath, "requirements.md", "Requirements"),
1331
- inlineGsdRootFile(s.basePath, "project.md", "Project"),
1332
- ]);
1333
- s.lastBaselineCharCount =
1334
- (decisionsContent?.length ?? 0) +
1335
- (requirementsContent?.length ?? 0) +
1336
- (projectContent?.length ?? 0);
1337
- }
1338
- catch {
1339
- // Non-fatal
1340
- }
1341
- }
1342
- // Cache-optimize prompt section ordering
1343
- try {
1344
- const { reorderForCaching } = await import("./prompt-ordering.js");
1345
- finalPrompt = reorderForCaching(finalPrompt);
1346
- }
1347
- catch (reorderErr) {
1348
- const msg = getErrorMessage(reorderErr);
1349
- process.stderr.write(`[gsd] prompt reorder failed (non-fatal): ${msg}\n`);
1350
- }
1351
- // Select and apply model
1352
- const modelResult = await selectAndApplyModel(ctx, pi, unitType, unitId, s.basePath, prefs, s.verbose, s.autoModeStartModel);
1353
- s.currentUnitRouting = modelResult.routing;
1354
- // ── Start unit supervision (delegated to auto-timers.ts) ──
1355
- clearUnitTimeout();
1356
- startUnitSupervision({
1357
- s,
1358
- ctx,
1359
- pi,
1360
- unitType,
1361
- unitId,
1362
- prefs,
1363
- buildSnapshotOpts: () => buildSnapshotOpts(unitType, unitId),
1364
- buildRecoveryContext: () => buildRecoveryContext(),
1365
- pauseAuto,
1366
- });
1367
- // Inject prompt
1368
- if (!s.active)
1369
- return;
1370
- pi.sendMessage({ customType: "gsd-auto", content: finalPrompt, display: s.verbose }, { triggerTurn: true });
1371
- }
1372
- finally {
1373
- s.dispatching = false;
1374
- }
1375
- }
1376
638
  // ─── Preconditions ────────────────────────────────────────────────────────────
1377
639
  /**
1378
640
  * Ensure directories, branches, and other prerequisites exist before
1379
641
  * dispatching a unit. The LLM should never need to mkdir or git checkout.
1380
642
  */
1381
643
  function ensurePreconditions(unitType, unitId, base, state) {
1382
- const { milestone: mid } = parseUnitId(unitId);
644
+ const parts = unitId.split("/");
645
+ const mid = parts[0];
1383
646
  const mDir = resolveMilestonePath(base, mid);
1384
647
  if (!mDir) {
1385
648
  const newDir = join(milestonesDir(base), mid);
1386
649
  mkdirSync(join(newDir, "slices"), { recursive: true });
1387
650
  }
1388
- const sid = parseUnitId(unitId).slice;
1389
- if (sid) {
651
+ if (parts.length >= 2) {
652
+ const sid = parts[1];
1390
653
  const mDirResolved = resolveMilestonePath(base, mid);
1391
654
  if (mDirResolved) {
1392
655
  const slicesDir = join(mDirResolved, "slices");
@@ -1405,9 +668,11 @@ function ensurePreconditions(unitType, unitId, base, state) {
1405
668
  // ─── Diagnostics ──────────────────────────────────────────────────────────────
1406
669
  /** Build recovery context from module state for recoverTimedOutUnit */
1407
670
  function buildRecoveryContext() {
1408
- return { basePath: s.basePath, verbose: s.verbose,
1409
- currentUnitStartedAt: s.currentUnit?.startedAt ?? Date.now(), unitRecoveryCount: s.unitRecoveryCount,
1410
- dispatchNextUnit,
671
+ return {
672
+ basePath: s.basePath,
673
+ verbose: s.verbose,
674
+ currentUnitStartedAt: s.currentUnit?.startedAt ?? Date.now(),
675
+ unitRecoveryCount: s.unitRecoveryCount,
1411
676
  };
1412
677
  }
1413
678
  // Re-export recovery functions for external consumers
@@ -1416,16 +681,6 @@ export { resolveExpectedArtifactPath, verifyExpectedArtifact, writeBlockerPlaceh
1416
681
  * Test-only: expose skip-loop state for unit tests.
1417
682
  * Not part of the public API.
1418
683
  */
1419
- export function _getUnitConsecutiveSkips() { return s.unitConsecutiveSkips; }
1420
- export function _resetUnitConsecutiveSkips() { s.unitConsecutiveSkips.clear(); }
1421
- /**
1422
- * Test-only: expose dispatching / skipDepth state for reentrancy guard tests.
1423
- * Not part of the public API.
1424
- */
1425
- export function _getDispatching() { return s.dispatching; }
1426
- export function _setDispatching(v) { s.dispatching = v; }
1427
- export function _getSkipDepth() { return s.skipDepth; }
1428
- export function _setSkipDepth(v) { s.skipDepth = v; }
1429
684
  /**
1430
685
  * Dispatch a hook unit directly, bypassing normal pre-dispatch hooks.
1431
686
  * Used for manual hook triggers via /gsd run-hook.
@@ -1443,13 +698,21 @@ export async function dispatchHookUnit(ctx, pi, hookName, triggerUnitType, trigg
1443
698
  }
1444
699
  const hookUnitType = `hook/${hookName}`;
1445
700
  const hookStartedAt = Date.now();
1446
- s.currentUnit = { type: triggerUnitType, id: triggerUnitId, startedAt: hookStartedAt };
701
+ s.currentUnit = {
702
+ type: triggerUnitType,
703
+ id: triggerUnitId,
704
+ startedAt: hookStartedAt,
705
+ };
1447
706
  const result = await s.cmdCtx.newSession();
1448
707
  if (result.cancelled) {
1449
708
  await stopAuto(ctx, pi);
1450
709
  return false;
1451
710
  }
1452
- s.currentUnit = { type: hookUnitType, id: triggerUnitId, startedAt: hookStartedAt };
711
+ s.currentUnit = {
712
+ type: hookUnitType,
713
+ id: triggerUnitId,
714
+ startedAt: hookStartedAt,
715
+ };
1453
716
  writeUnitRuntimeRecord(s.basePath, hookUnitType, triggerUnitId, hookStartedAt, {
1454
717
  phase: "dispatched",
1455
718
  wrapupWarningSent: false,
@@ -1460,16 +723,17 @@ export async function dispatchHookUnit(ctx, pi, hookName, triggerUnitType, trigg
1460
723
  });
1461
724
  if (hookModel) {
1462
725
  const availableModels = ctx.modelRegistry.getAvailable();
1463
- const match = availableModels.find(m => m.id === hookModel || `${m.provider}/${m.id}` === hookModel);
726
+ const match = availableModels.find((m) => m.id === hookModel || `${m.provider}/${m.id}` === hookModel);
1464
727
  if (match) {
1465
728
  try {
1466
729
  await pi.setModel(match);
1467
730
  }
1468
- catch { /* non-fatal */ }
731
+ catch {
732
+ /* non-fatal */
733
+ }
1469
734
  }
1470
735
  }
1471
736
  const sessionFile = ctx.sessionManager.getSessionFile();
1472
- updateSessionLock(lockBase(), hookUnitType, triggerUnitId, s.completedUnits.length, sessionFile);
1473
737
  writeLock(lockBase(), hookUnitType, triggerUnitId, s.completedUnits.length, sessionFile);
1474
738
  clearUnitTimeout();
1475
739
  const supervisor = resolveAutoSupervisorConfig();
@@ -1490,6 +754,12 @@ export async function dispatchHookUnit(ctx, pi, hookName, triggerUnitType, trigg
1490
754
  }, hookHardTimeoutMs);
1491
755
  ctx.ui.setStatus("gsd-auto", s.stepMode ? "next" : "auto");
1492
756
  ctx.ui.notify(`Running post-unit hook: ${hookName}`, "info");
757
+ debugLog("dispatchHookUnit", {
758
+ phase: "send-message",
759
+ promptLength: hookPrompt.length,
760
+ });
1493
761
  pi.sendMessage({ customType: "gsd-auto", content: hookPrompt, display: true }, { triggerTurn: true });
1494
762
  return true;
1495
763
  }
764
+ // Direct phase dispatch → auto-direct-dispatch.ts
765
+ export { dispatchDirectPhase } from "./auto-direct-dispatch.js";