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.
- package/dist/bundled-resource-path.d.ts +8 -0
- package/dist/bundled-resource-path.js +14 -0
- package/dist/headless-query.js +6 -6
- package/dist/resources/extensions/gsd/auto/session.js +27 -32
- package/dist/resources/extensions/gsd/auto-dashboard.js +29 -109
- package/dist/resources/extensions/gsd/auto-direct-dispatch.js +6 -1
- package/dist/resources/extensions/gsd/auto-dispatch.js +52 -81
- package/dist/resources/extensions/gsd/auto-loop.js +956 -0
- package/dist/resources/extensions/gsd/auto-observability.js +4 -2
- package/dist/resources/extensions/gsd/auto-post-unit.js +75 -185
- package/dist/resources/extensions/gsd/auto-prompts.js +133 -101
- package/dist/resources/extensions/gsd/auto-recovery.js +59 -97
- package/dist/resources/extensions/gsd/auto-start.js +330 -309
- package/dist/resources/extensions/gsd/auto-supervisor.js +5 -11
- package/dist/resources/extensions/gsd/auto-timeout-recovery.js +7 -7
- package/dist/resources/extensions/gsd/auto-timers.js +3 -4
- package/dist/resources/extensions/gsd/auto-verification.js +35 -73
- package/dist/resources/extensions/gsd/auto-worktree-sync.js +167 -0
- package/dist/resources/extensions/gsd/auto-worktree.js +291 -126
- package/dist/resources/extensions/gsd/auto.js +283 -1013
- package/dist/resources/extensions/gsd/captures.js +10 -4
- package/dist/resources/extensions/gsd/dispatch-guard.js +7 -8
- package/dist/resources/extensions/gsd/docs/preferences-reference.md +25 -18
- package/dist/resources/extensions/gsd/doctor-checks.js +3 -4
- package/dist/resources/extensions/gsd/git-service.js +1 -1
- package/dist/resources/extensions/gsd/gsd-db.js +296 -151
- package/dist/resources/extensions/gsd/index.js +92 -228
- package/dist/resources/extensions/gsd/post-unit-hooks.js +13 -13
- package/dist/resources/extensions/gsd/progress-score.js +61 -156
- package/dist/resources/extensions/gsd/quick.js +98 -122
- package/dist/resources/extensions/gsd/session-lock.js +13 -0
- package/dist/resources/extensions/gsd/templates/preferences.md +1 -0
- package/dist/resources/extensions/gsd/undo.js +43 -48
- package/dist/resources/extensions/gsd/unit-runtime.js +16 -15
- package/dist/resources/extensions/gsd/verification-evidence.js +0 -1
- package/dist/resources/extensions/gsd/verification-gate.js +6 -35
- package/dist/resources/extensions/gsd/worktree-command.js +30 -24
- package/dist/resources/extensions/gsd/worktree-manager.js +2 -3
- package/dist/resources/extensions/gsd/worktree-resolver.js +344 -0
- package/dist/resources/extensions/gsd/worktree.js +7 -44
- package/dist/tool-bootstrap.js +59 -11
- package/dist/worktree-cli.js +7 -7
- package/package.json +1 -1
- package/packages/pi-ai/dist/models.generated.d.ts +3630 -5483
- package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
- package/packages/pi-ai/dist/models.generated.js +735 -2588
- package/packages/pi-ai/dist/models.generated.js.map +1 -1
- package/packages/pi-ai/src/models.generated.ts +1039 -2892
- package/packages/pi-coding-agent/package.json +1 -1
- package/pkg/package.json +1 -1
- package/src/resources/extensions/gsd/auto/session.ts +47 -30
- package/src/resources/extensions/gsd/auto-dashboard.ts +28 -131
- package/src/resources/extensions/gsd/auto-direct-dispatch.ts +6 -1
- package/src/resources/extensions/gsd/auto-dispatch.ts +135 -91
- package/src/resources/extensions/gsd/auto-loop.ts +1665 -0
- package/src/resources/extensions/gsd/auto-observability.ts +4 -2
- package/src/resources/extensions/gsd/auto-post-unit.ts +85 -228
- package/src/resources/extensions/gsd/auto-prompts.ts +138 -109
- package/src/resources/extensions/gsd/auto-recovery.ts +124 -118
- package/src/resources/extensions/gsd/auto-start.ts +440 -354
- package/src/resources/extensions/gsd/auto-supervisor.ts +5 -12
- package/src/resources/extensions/gsd/auto-timeout-recovery.ts +8 -8
- package/src/resources/extensions/gsd/auto-timers.ts +3 -4
- package/src/resources/extensions/gsd/auto-verification.ts +76 -90
- package/src/resources/extensions/gsd/auto-worktree-sync.ts +204 -0
- package/src/resources/extensions/gsd/auto-worktree.ts +389 -141
- package/src/resources/extensions/gsd/auto.ts +515 -1199
- package/src/resources/extensions/gsd/captures.ts +10 -4
- package/src/resources/extensions/gsd/dispatch-guard.ts +13 -9
- package/src/resources/extensions/gsd/docs/preferences-reference.md +25 -18
- package/src/resources/extensions/gsd/doctor-checks.ts +3 -4
- package/src/resources/extensions/gsd/git-service.ts +8 -1
- package/src/resources/extensions/gsd/gitignore.ts +4 -2
- package/src/resources/extensions/gsd/gsd-db.ts +375 -180
- package/src/resources/extensions/gsd/index.ts +104 -263
- package/src/resources/extensions/gsd/post-unit-hooks.ts +13 -13
- package/src/resources/extensions/gsd/progress-score.ts +65 -200
- package/src/resources/extensions/gsd/quick.ts +121 -125
- package/src/resources/extensions/gsd/session-lock.ts +11 -0
- package/src/resources/extensions/gsd/templates/preferences.md +1 -0
- package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +32 -59
- package/src/resources/extensions/gsd/tests/all-milestones-complete-merge.test.ts +75 -27
- package/src/resources/extensions/gsd/tests/auto-budget-alerts.test.ts +1 -1
- package/src/resources/extensions/gsd/tests/auto-lock-creation.test.ts +37 -0
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +1458 -0
- package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +8 -162
- package/src/resources/extensions/gsd/tests/auto-secrets-gate.test.ts +2 -108
- package/src/resources/extensions/gsd/tests/auto-session-encapsulation.test.ts +1 -3
- package/src/resources/extensions/gsd/tests/auto-worktree-milestone-merge.test.ts +0 -3
- package/src/resources/extensions/gsd/tests/auto-worktree.test.ts +58 -0
- package/src/resources/extensions/gsd/tests/dispatch-guard.test.ts +0 -55
- package/src/resources/extensions/gsd/tests/headless-query.test.ts +22 -0
- package/src/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +8 -11
- package/src/resources/extensions/gsd/tests/provider-errors.test.ts +4 -6
- package/src/resources/extensions/gsd/tests/run-uat.test.ts +3 -3
- package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +64 -0
- package/src/resources/extensions/gsd/tests/sidecar-queue.test.ts +181 -0
- package/src/resources/extensions/gsd/tests/stale-worktree-cwd.test.ts +0 -3
- package/src/resources/extensions/gsd/tests/token-profile.test.ts +6 -6
- package/src/resources/extensions/gsd/tests/triage-dispatch.test.ts +6 -6
- package/src/resources/extensions/gsd/tests/undo.test.ts +6 -0
- package/src/resources/extensions/gsd/tests/verification-evidence.test.ts +24 -26
- package/src/resources/extensions/gsd/tests/verification-gate.test.ts +7 -201
- package/src/resources/extensions/gsd/tests/worktree-db-integration.test.ts +205 -0
- package/src/resources/extensions/gsd/tests/worktree-db.test.ts +442 -0
- package/src/resources/extensions/gsd/tests/worktree-e2e.test.ts +0 -3
- package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +705 -0
- package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +57 -106
- package/src/resources/extensions/gsd/tests/worktree.test.ts +5 -1
- package/src/resources/extensions/gsd/tests/write-gate.test.ts +43 -132
- package/src/resources/extensions/gsd/types.ts +90 -81
- package/src/resources/extensions/gsd/undo.ts +42 -46
- package/src/resources/extensions/gsd/unit-runtime.ts +14 -18
- package/src/resources/extensions/gsd/verification-evidence.ts +1 -3
- package/src/resources/extensions/gsd/verification-gate.ts +6 -39
- package/src/resources/extensions/gsd/worktree-command.ts +36 -24
- package/src/resources/extensions/gsd/worktree-manager.ts +2 -3
- package/src/resources/extensions/gsd/worktree-resolver.ts +485 -0
- package/src/resources/extensions/gsd/worktree.ts +7 -44
- package/dist/resources/extensions/gsd/auto-constants.js +0 -5
- package/dist/resources/extensions/gsd/auto-idempotency.js +0 -106
- package/dist/resources/extensions/gsd/auto-stuck-detection.js +0 -165
- package/dist/resources/extensions/gsd/mechanical-completion.js +0 -351
- package/src/resources/extensions/gsd/auto-constants.ts +0 -6
- package/src/resources/extensions/gsd/auto-idempotency.ts +0 -151
- package/src/resources/extensions/gsd/auto-stuck-detection.ts +0 -221
- package/src/resources/extensions/gsd/mechanical-completion.ts +0 -430
- package/src/resources/extensions/gsd/tests/auto-dispatch-loop.test.ts +0 -691
- package/src/resources/extensions/gsd/tests/auto-reentrancy-guard.test.ts +0 -127
- package/src/resources/extensions/gsd/tests/auto-skip-loop.test.ts +0 -123
- package/src/resources/extensions/gsd/tests/dispatch-stall-guard.test.ts +0 -126
- package/src/resources/extensions/gsd/tests/loop-regression.test.ts +0 -874
- package/src/resources/extensions/gsd/tests/mechanical-completion.test.ts +0 -356
- package/src/resources/extensions/gsd/tests/progress-score.test.ts +0 -206
- package/src/resources/extensions/gsd/tests/session-lock.test.ts +0 -434
|
@@ -0,0 +1,1665 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* auto-loop.ts — Linear loop execution backbone for auto-mode.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the recursive dispatchNextUnit → handleAgentEnd → dispatchNextUnit
|
|
5
|
+
* pattern with a while loop. The agent_end event resolves a promise instead
|
|
6
|
+
* of recursing.
|
|
7
|
+
*
|
|
8
|
+
* MAINTENANCE RULE: The only module-level mutable state here is `_activeSession`,
|
|
9
|
+
* used by the agent_end bridge. Promise state itself lives on AutoSession so
|
|
10
|
+
* concurrent auto sessions cannot corrupt each other.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ExtensionAPI, ExtensionContext } from "@gsd/pi-coding-agent";
|
|
14
|
+
|
|
15
|
+
import type { AutoSession } from "./auto/session.js";
|
|
16
|
+
import { NEW_SESSION_TIMEOUT_MS } from "./auto/session.js";
|
|
17
|
+
import type { GSDPreferences } from "./preferences.js";
|
|
18
|
+
import type { GSDState } from "./types.js";
|
|
19
|
+
import type { CloseoutOptions } from "./auto-unit-closeout.js";
|
|
20
|
+
import type { PostUnitContext } from "./auto-post-unit.js";
|
|
21
|
+
import type {
|
|
22
|
+
VerificationContext,
|
|
23
|
+
VerificationResult,
|
|
24
|
+
} from "./auto-verification.js";
|
|
25
|
+
import type { DispatchAction } from "./auto-dispatch.js";
|
|
26
|
+
import type { WorktreeResolver } from "./worktree-resolver.js";
|
|
27
|
+
import { debugLog } from "./debug-logger.js";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Maximum total loop iterations before forced stop. Prevents runaway loops
|
|
31
|
+
* when units alternate IDs (bypassing the same-unit stuck detector).
|
|
32
|
+
* A milestone with 20 slices × 5 tasks × 3 phases ≈ 300 units. 500 gives
|
|
33
|
+
* generous headroom including retries and sidecar work.
|
|
34
|
+
*/
|
|
35
|
+
const MAX_LOOP_ITERATIONS = 500;
|
|
36
|
+
|
|
37
|
+
// ─── Types ───────────────────────────────────────────────────────────────────
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Minimal shape of the event parameter from pi.on("agent_end", ...).
|
|
41
|
+
* The full event has more fields, but the loop only needs messages.
|
|
42
|
+
*/
|
|
43
|
+
export interface AgentEndEvent {
|
|
44
|
+
messages: unknown[];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Result of a single unit execution (one iteration of the loop).
|
|
49
|
+
*/
|
|
50
|
+
export interface UnitResult {
|
|
51
|
+
status: "completed" | "cancelled" | "error";
|
|
52
|
+
event?: AgentEndEvent;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// ─── Session-scoped promise state ───────────────────────────────────────────
|
|
56
|
+
//
|
|
57
|
+
// pendingResolve and pendingAgentEndQueue live on AutoSession (not module-level)
|
|
58
|
+
// so concurrent sessions cannot corrupt each other's promises.
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The singleton session reference used by resolveAgentEnd. Set by autoLoop
|
|
62
|
+
* on entry so that the agent_end handler in index.ts can resolve the correct
|
|
63
|
+
* session's promise without needing a direct reference to `s`.
|
|
64
|
+
*/
|
|
65
|
+
let _activeSession: AutoSession | null = null;
|
|
66
|
+
|
|
67
|
+
// ─── resolveAgentEnd ─────────────────────────────────────────────────────────
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Called from the agent_end event handler in index.ts to resolve the
|
|
71
|
+
* in-flight unit promise. One-shot: the resolver is nulled before calling
|
|
72
|
+
* to prevent double-resolution from model fallback retries.
|
|
73
|
+
*
|
|
74
|
+
* If no pendingResolve exists (event arrived between loop iterations),
|
|
75
|
+
* the event is queued on the session so the next runUnit can drain it.
|
|
76
|
+
*/
|
|
77
|
+
export function resolveAgentEnd(event: AgentEndEvent): void {
|
|
78
|
+
const s = _activeSession;
|
|
79
|
+
if (!s) {
|
|
80
|
+
debugLog("resolveAgentEnd", {
|
|
81
|
+
status: "no-active-session",
|
|
82
|
+
warning: "agent_end with no active loop session",
|
|
83
|
+
});
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (s.pendingResolve) {
|
|
88
|
+
debugLog("resolveAgentEnd", { status: "resolving", hasEvent: true });
|
|
89
|
+
const r = s.pendingResolve;
|
|
90
|
+
s.pendingResolve = null;
|
|
91
|
+
r({ status: "completed", event });
|
|
92
|
+
} else {
|
|
93
|
+
// Queue the event so the next runUnit picks it up immediately
|
|
94
|
+
debugLog("resolveAgentEnd", {
|
|
95
|
+
status: "queued",
|
|
96
|
+
queueLength: s.pendingAgentEndQueue.length + 1,
|
|
97
|
+
warning:
|
|
98
|
+
"agent_end arrived between loop iterations — queued for next runUnit",
|
|
99
|
+
});
|
|
100
|
+
s.pendingAgentEndQueue.push(event);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function isSessionSwitchInFlight(): boolean {
|
|
105
|
+
return _activeSession?.sessionSwitchInFlight ?? false;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// ─── resetPendingResolve (test helper) ───────────────────────────────────────
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Reset session promise state. Only exported for test cleanup — production code
|
|
112
|
+
* should never call this.
|
|
113
|
+
*/
|
|
114
|
+
export function _resetPendingResolve(): void {
|
|
115
|
+
if (_activeSession) {
|
|
116
|
+
_activeSession.pendingResolve = null;
|
|
117
|
+
_activeSession.pendingAgentEndQueue = [];
|
|
118
|
+
}
|
|
119
|
+
_activeSession = null;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Set the active session for resolveAgentEnd. Only exported for test setup —
|
|
124
|
+
* production code sets this via autoLoop entry.
|
|
125
|
+
*/
|
|
126
|
+
export function _setActiveSession(session: AutoSession | null): void {
|
|
127
|
+
_activeSession = session;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// ─── runUnit ─────────────────────────────────────────────────────────────────
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Execute a single unit: create a new session, send the prompt, and await
|
|
134
|
+
* the agent_end promise. Returns a UnitResult describing what happened.
|
|
135
|
+
*
|
|
136
|
+
* The promise is one-shot: resolveAgentEnd() is the only way to resolve it.
|
|
137
|
+
* On session creation failure or timeout, returns { status: 'cancelled' }
|
|
138
|
+
* without awaiting the promise.
|
|
139
|
+
*/
|
|
140
|
+
export async function runUnit(
|
|
141
|
+
ctx: ExtensionContext,
|
|
142
|
+
pi: ExtensionAPI,
|
|
143
|
+
s: AutoSession,
|
|
144
|
+
unitType: string,
|
|
145
|
+
unitId: string,
|
|
146
|
+
prompt: string,
|
|
147
|
+
_prefs: GSDPreferences | undefined,
|
|
148
|
+
): Promise<UnitResult> {
|
|
149
|
+
debugLog("runUnit", { phase: "start", unitType, unitId });
|
|
150
|
+
|
|
151
|
+
// ── Drain queued events from error-recovery retries ──
|
|
152
|
+
// If an agent_end arrived between iterations (e.g. from a model fallback
|
|
153
|
+
// sendMessage retry), consume it immediately instead of creating a new promise.
|
|
154
|
+
// Cap queue to 3 entries to prevent unbounded growth from stale events.
|
|
155
|
+
if (s.pendingAgentEndQueue.length > 3) {
|
|
156
|
+
debugLog("runUnit", {
|
|
157
|
+
phase: "queue-overflow",
|
|
158
|
+
dropped: s.pendingAgentEndQueue.length - 1,
|
|
159
|
+
unitType,
|
|
160
|
+
unitId,
|
|
161
|
+
});
|
|
162
|
+
s.pendingAgentEndQueue = [
|
|
163
|
+
s.pendingAgentEndQueue[s.pendingAgentEndQueue.length - 1]!,
|
|
164
|
+
];
|
|
165
|
+
}
|
|
166
|
+
if (s.pendingAgentEndQueue.length > 0) {
|
|
167
|
+
const queued = s.pendingAgentEndQueue.shift()!;
|
|
168
|
+
debugLog("runUnit", {
|
|
169
|
+
phase: "drained-queued-event",
|
|
170
|
+
unitType,
|
|
171
|
+
unitId,
|
|
172
|
+
queueRemaining: s.pendingAgentEndQueue.length,
|
|
173
|
+
});
|
|
174
|
+
return { status: "completed", event: queued };
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// ── Session creation with timeout ──
|
|
178
|
+
debugLog("runUnit", { phase: "session-create", unitType, unitId });
|
|
179
|
+
|
|
180
|
+
let sessionResult: { cancelled: boolean };
|
|
181
|
+
let sessionTimeoutHandle: ReturnType<typeof setTimeout> | undefined;
|
|
182
|
+
s.sessionSwitchInFlight = true;
|
|
183
|
+
try {
|
|
184
|
+
const sessionPromise = s.cmdCtx!.newSession().finally(() => {
|
|
185
|
+
s.sessionSwitchInFlight = false;
|
|
186
|
+
});
|
|
187
|
+
const timeoutPromise = new Promise<{ cancelled: true }>((resolve) => {
|
|
188
|
+
sessionTimeoutHandle = setTimeout(
|
|
189
|
+
() => resolve({ cancelled: true }),
|
|
190
|
+
NEW_SESSION_TIMEOUT_MS,
|
|
191
|
+
);
|
|
192
|
+
});
|
|
193
|
+
sessionResult = await Promise.race([sessionPromise, timeoutPromise]);
|
|
194
|
+
} catch (sessionErr) {
|
|
195
|
+
if (sessionTimeoutHandle) clearTimeout(sessionTimeoutHandle);
|
|
196
|
+
const msg =
|
|
197
|
+
sessionErr instanceof Error ? sessionErr.message : String(sessionErr);
|
|
198
|
+
debugLog("runUnit", {
|
|
199
|
+
phase: "session-error",
|
|
200
|
+
unitType,
|
|
201
|
+
unitId,
|
|
202
|
+
error: msg,
|
|
203
|
+
});
|
|
204
|
+
return { status: "cancelled" };
|
|
205
|
+
}
|
|
206
|
+
if (sessionTimeoutHandle) clearTimeout(sessionTimeoutHandle);
|
|
207
|
+
|
|
208
|
+
if (sessionResult.cancelled) {
|
|
209
|
+
debugLog("runUnit-session-timeout", { unitType, unitId });
|
|
210
|
+
return { status: "cancelled" };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (!s.active) {
|
|
214
|
+
return { status: "cancelled" };
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ── Create the agent_end promise (session-scoped) ──
|
|
218
|
+
// This happens after newSession completes so session-switch agent_end events
|
|
219
|
+
// from the previous session cannot resolve the new unit.
|
|
220
|
+
const unitPromise = new Promise<UnitResult>((resolve) => {
|
|
221
|
+
s.pendingResolve = resolve;
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
// ── Send the prompt ──
|
|
225
|
+
debugLog("runUnit", { phase: "send-message", unitType, unitId });
|
|
226
|
+
|
|
227
|
+
pi.sendMessage(
|
|
228
|
+
{ customType: "gsd-auto", content: prompt, display: s.verbose },
|
|
229
|
+
{ triggerTurn: true },
|
|
230
|
+
);
|
|
231
|
+
|
|
232
|
+
// ── Await agent_end ──
|
|
233
|
+
debugLog("runUnit", { phase: "awaiting-agent-end", unitType, unitId });
|
|
234
|
+
const result = await unitPromise;
|
|
235
|
+
debugLog("runUnit", {
|
|
236
|
+
phase: "agent-end-received",
|
|
237
|
+
unitType,
|
|
238
|
+
unitId,
|
|
239
|
+
status: result.status,
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// ─── LoopDeps ────────────────────────────────────────────────────────────────
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Dependencies injected by the caller (auto.ts startAuto) so autoLoop
|
|
249
|
+
* can access private functions from auto.ts without exporting them.
|
|
250
|
+
*/
|
|
251
|
+
export interface LoopDeps {
|
|
252
|
+
lockBase: () => string;
|
|
253
|
+
buildSnapshotOpts: (
|
|
254
|
+
unitType: string,
|
|
255
|
+
unitId: string,
|
|
256
|
+
) => CloseoutOptions & Record<string, unknown>;
|
|
257
|
+
stopAuto: (
|
|
258
|
+
ctx?: ExtensionContext,
|
|
259
|
+
pi?: ExtensionAPI,
|
|
260
|
+
reason?: string,
|
|
261
|
+
) => Promise<void>;
|
|
262
|
+
pauseAuto: (ctx?: ExtensionContext, pi?: ExtensionAPI) => Promise<void>;
|
|
263
|
+
clearUnitTimeout: () => void;
|
|
264
|
+
updateProgressWidget: (
|
|
265
|
+
ctx: ExtensionContext,
|
|
266
|
+
unitType: string,
|
|
267
|
+
unitId: string,
|
|
268
|
+
state: GSDState,
|
|
269
|
+
) => void;
|
|
270
|
+
|
|
271
|
+
// State and cache functions
|
|
272
|
+
invalidateAllCaches: () => void;
|
|
273
|
+
deriveState: (basePath: string) => Promise<GSDState>;
|
|
274
|
+
loadEffectiveGSDPreferences: () =>
|
|
275
|
+
| { preferences?: GSDPreferences }
|
|
276
|
+
| undefined;
|
|
277
|
+
|
|
278
|
+
// Pre-dispatch health gate
|
|
279
|
+
preDispatchHealthGate: (
|
|
280
|
+
basePath: string,
|
|
281
|
+
) => Promise<{ proceed: boolean; reason?: string; fixesApplied: string[] }>;
|
|
282
|
+
|
|
283
|
+
// Worktree sync
|
|
284
|
+
syncProjectRootToWorktree: (
|
|
285
|
+
originalBase: string,
|
|
286
|
+
basePath: string,
|
|
287
|
+
milestoneId: string | null,
|
|
288
|
+
) => void;
|
|
289
|
+
|
|
290
|
+
// Resource version guard
|
|
291
|
+
checkResourcesStale: (version: string | null) => string | null;
|
|
292
|
+
|
|
293
|
+
// Session lock
|
|
294
|
+
validateSessionLock: (basePath: string) => boolean;
|
|
295
|
+
updateSessionLock: (
|
|
296
|
+
basePath: string,
|
|
297
|
+
unitType: string,
|
|
298
|
+
unitId: string,
|
|
299
|
+
completedUnits: number,
|
|
300
|
+
sessionFile?: string,
|
|
301
|
+
) => void;
|
|
302
|
+
handleLostSessionLock: (ctx?: ExtensionContext) => void;
|
|
303
|
+
|
|
304
|
+
// Milestone transition functions
|
|
305
|
+
sendDesktopNotification: (
|
|
306
|
+
title: string,
|
|
307
|
+
body: string,
|
|
308
|
+
kind: string,
|
|
309
|
+
category: string,
|
|
310
|
+
) => void;
|
|
311
|
+
setActiveMilestoneId: (basePath: string, mid: string) => void;
|
|
312
|
+
pruneQueueOrder: (basePath: string, pendingIds: string[]) => void;
|
|
313
|
+
isInAutoWorktree: (basePath: string) => boolean;
|
|
314
|
+
shouldUseWorktreeIsolation: () => boolean;
|
|
315
|
+
mergeMilestoneToMain: (
|
|
316
|
+
basePath: string,
|
|
317
|
+
milestoneId: string,
|
|
318
|
+
roadmapContent: string,
|
|
319
|
+
) => { pushed: boolean };
|
|
320
|
+
teardownAutoWorktree: (basePath: string, milestoneId: string) => void;
|
|
321
|
+
createAutoWorktree: (basePath: string, milestoneId: string) => string;
|
|
322
|
+
captureIntegrationBranch: (
|
|
323
|
+
basePath: string,
|
|
324
|
+
mid: string,
|
|
325
|
+
opts?: { commitDocs?: boolean },
|
|
326
|
+
) => void;
|
|
327
|
+
getIsolationMode: () => string;
|
|
328
|
+
getCurrentBranch: (basePath: string) => string;
|
|
329
|
+
autoWorktreeBranch: (milestoneId: string) => string;
|
|
330
|
+
resolveMilestoneFile: (
|
|
331
|
+
basePath: string,
|
|
332
|
+
milestoneId: string,
|
|
333
|
+
fileType: string,
|
|
334
|
+
) => string | null;
|
|
335
|
+
reconcileMergeState: (basePath: string, ctx: ExtensionContext) => boolean;
|
|
336
|
+
|
|
337
|
+
// Budget/context/secrets
|
|
338
|
+
getLedger: () => unknown;
|
|
339
|
+
getProjectTotals: (units: unknown) => { cost: number };
|
|
340
|
+
formatCost: (cost: number) => string;
|
|
341
|
+
getBudgetAlertLevel: (pct: number) => number;
|
|
342
|
+
getNewBudgetAlertLevel: (lastLevel: number, pct: number) => number;
|
|
343
|
+
getBudgetEnforcementAction: (enforcement: string, pct: number) => string;
|
|
344
|
+
getManifestStatus: (
|
|
345
|
+
basePath: string,
|
|
346
|
+
mid: string | undefined,
|
|
347
|
+
) => Promise<{ pending: unknown[] } | null>;
|
|
348
|
+
collectSecretsFromManifest: (
|
|
349
|
+
basePath: string,
|
|
350
|
+
mid: string | undefined,
|
|
351
|
+
ctx: ExtensionContext,
|
|
352
|
+
) => Promise<{
|
|
353
|
+
applied: unknown[];
|
|
354
|
+
skipped: unknown[];
|
|
355
|
+
existingSkipped: unknown[];
|
|
356
|
+
} | null>;
|
|
357
|
+
|
|
358
|
+
// Dispatch
|
|
359
|
+
resolveDispatch: (dctx: {
|
|
360
|
+
basePath: string;
|
|
361
|
+
mid: string;
|
|
362
|
+
midTitle: string;
|
|
363
|
+
state: GSDState;
|
|
364
|
+
prefs: GSDPreferences | undefined;
|
|
365
|
+
}) => Promise<DispatchAction>;
|
|
366
|
+
runPreDispatchHooks: (
|
|
367
|
+
unitType: string,
|
|
368
|
+
unitId: string,
|
|
369
|
+
prompt: string,
|
|
370
|
+
basePath: string,
|
|
371
|
+
) => {
|
|
372
|
+
firedHooks: string[];
|
|
373
|
+
action: string;
|
|
374
|
+
prompt?: string;
|
|
375
|
+
unitType?: string;
|
|
376
|
+
};
|
|
377
|
+
getPriorSliceCompletionBlocker: (
|
|
378
|
+
basePath: string,
|
|
379
|
+
mainBranch: string,
|
|
380
|
+
unitType: string,
|
|
381
|
+
unitId: string,
|
|
382
|
+
) => string | null;
|
|
383
|
+
getMainBranch: (basePath: string) => string;
|
|
384
|
+
collectObservabilityWarnings: (
|
|
385
|
+
ctx: ExtensionContext,
|
|
386
|
+
basePath: string,
|
|
387
|
+
unitType: string,
|
|
388
|
+
unitId: string,
|
|
389
|
+
) => Promise<unknown[]>;
|
|
390
|
+
buildObservabilityRepairBlock: (issues: unknown[]) => string | null;
|
|
391
|
+
|
|
392
|
+
// Unit closeout + runtime records
|
|
393
|
+
closeoutUnit: (
|
|
394
|
+
ctx: ExtensionContext,
|
|
395
|
+
basePath: string,
|
|
396
|
+
unitType: string,
|
|
397
|
+
unitId: string,
|
|
398
|
+
startedAt: number,
|
|
399
|
+
opts?: CloseoutOptions & Record<string, unknown>,
|
|
400
|
+
) => Promise<void>;
|
|
401
|
+
verifyExpectedArtifact: (
|
|
402
|
+
unitType: string,
|
|
403
|
+
unitId: string,
|
|
404
|
+
basePath: string,
|
|
405
|
+
) => boolean;
|
|
406
|
+
clearUnitRuntimeRecord: (
|
|
407
|
+
basePath: string,
|
|
408
|
+
unitType: string,
|
|
409
|
+
unitId: string,
|
|
410
|
+
) => void;
|
|
411
|
+
writeUnitRuntimeRecord: (
|
|
412
|
+
basePath: string,
|
|
413
|
+
unitType: string,
|
|
414
|
+
unitId: string,
|
|
415
|
+
startedAt: number,
|
|
416
|
+
record: Record<string, unknown>,
|
|
417
|
+
) => void;
|
|
418
|
+
recordOutcome: (unitType: string, tier: string, success: boolean) => void;
|
|
419
|
+
writeLock: (
|
|
420
|
+
lockBase: string,
|
|
421
|
+
unitType: string,
|
|
422
|
+
unitId: string,
|
|
423
|
+
completedCount: number,
|
|
424
|
+
sessionFile?: string,
|
|
425
|
+
) => void;
|
|
426
|
+
captureAvailableSkills: () => void;
|
|
427
|
+
ensurePreconditions: (
|
|
428
|
+
unitType: string,
|
|
429
|
+
unitId: string,
|
|
430
|
+
basePath: string,
|
|
431
|
+
state: GSDState,
|
|
432
|
+
) => void;
|
|
433
|
+
updateSliceProgressCache: (
|
|
434
|
+
basePath: string,
|
|
435
|
+
mid: string,
|
|
436
|
+
sliceId?: string,
|
|
437
|
+
) => void;
|
|
438
|
+
|
|
439
|
+
// Model selection + supervision
|
|
440
|
+
selectAndApplyModel: (
|
|
441
|
+
ctx: ExtensionContext,
|
|
442
|
+
pi: ExtensionAPI,
|
|
443
|
+
unitType: string,
|
|
444
|
+
unitId: string,
|
|
445
|
+
basePath: string,
|
|
446
|
+
prefs: GSDPreferences | undefined,
|
|
447
|
+
verbose: boolean,
|
|
448
|
+
startModel: { provider: string; id: string } | null,
|
|
449
|
+
) => Promise<{ routing: { tier: string; modelDowngraded: boolean } | null }>;
|
|
450
|
+
startUnitSupervision: (sctx: {
|
|
451
|
+
s: AutoSession;
|
|
452
|
+
ctx: ExtensionContext;
|
|
453
|
+
pi: ExtensionAPI;
|
|
454
|
+
unitType: string;
|
|
455
|
+
unitId: string;
|
|
456
|
+
prefs: GSDPreferences | undefined;
|
|
457
|
+
buildSnapshotOpts: () => CloseoutOptions & Record<string, unknown>;
|
|
458
|
+
buildRecoveryContext: () => unknown;
|
|
459
|
+
pauseAuto: (ctx?: ExtensionContext, pi?: ExtensionAPI) => Promise<void>;
|
|
460
|
+
}) => void;
|
|
461
|
+
|
|
462
|
+
// Prompt helpers
|
|
463
|
+
getDeepDiagnostic: (basePath: string) => string | null;
|
|
464
|
+
isDbAvailable: () => boolean;
|
|
465
|
+
reorderForCaching: (prompt: string) => string;
|
|
466
|
+
|
|
467
|
+
// Filesystem
|
|
468
|
+
existsSync: (path: string) => boolean;
|
|
469
|
+
readFileSync: (path: string, encoding: string) => string;
|
|
470
|
+
atomicWriteSync: (path: string, content: string) => void;
|
|
471
|
+
|
|
472
|
+
// Git
|
|
473
|
+
GitServiceImpl: new (basePath: string, gitConfig: unknown) => unknown;
|
|
474
|
+
|
|
475
|
+
// WorktreeResolver
|
|
476
|
+
resolver: WorktreeResolver;
|
|
477
|
+
|
|
478
|
+
// Post-unit processing
|
|
479
|
+
postUnitPreVerification: (
|
|
480
|
+
pctx: PostUnitContext,
|
|
481
|
+
) => Promise<"dispatched" | "continue">;
|
|
482
|
+
runPostUnitVerification: (
|
|
483
|
+
vctx: VerificationContext,
|
|
484
|
+
pauseAuto: (ctx?: ExtensionContext, pi?: ExtensionAPI) => Promise<void>,
|
|
485
|
+
) => Promise<VerificationResult>;
|
|
486
|
+
postUnitPostVerification: (
|
|
487
|
+
pctx: PostUnitContext,
|
|
488
|
+
) => Promise<"continue" | "step-wizard" | "stopped">;
|
|
489
|
+
|
|
490
|
+
// Session manager
|
|
491
|
+
getSessionFile: (ctx: ExtensionContext) => string;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
// ─── autoLoop ────────────────────────────────────────────────────────────────
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Main auto-mode execution loop. Iterates: derive → dispatch → guards →
|
|
498
|
+
* runUnit → finalize → repeat. Exits when s.active becomes false or a
|
|
499
|
+
* terminal condition is reached.
|
|
500
|
+
*
|
|
501
|
+
* This is the linear replacement for the recursive
|
|
502
|
+
* dispatchNextUnit → handleAgentEnd → dispatchNextUnit chain.
|
|
503
|
+
*/
|
|
504
|
+
export async function autoLoop(
|
|
505
|
+
ctx: ExtensionContext,
|
|
506
|
+
pi: ExtensionAPI,
|
|
507
|
+
s: AutoSession,
|
|
508
|
+
deps: LoopDeps,
|
|
509
|
+
): Promise<void> {
|
|
510
|
+
debugLog("autoLoop", { phase: "enter" });
|
|
511
|
+
_activeSession = s;
|
|
512
|
+
let iteration = 0;
|
|
513
|
+
let lastDerivedUnit = "";
|
|
514
|
+
let sameUnitCount = 0;
|
|
515
|
+
|
|
516
|
+
let consecutiveErrors = 0;
|
|
517
|
+
|
|
518
|
+
while (s.active) {
|
|
519
|
+
iteration++;
|
|
520
|
+
debugLog("autoLoop", { phase: "loop-top", iteration });
|
|
521
|
+
|
|
522
|
+
if (iteration > MAX_LOOP_ITERATIONS) {
|
|
523
|
+
debugLog("autoLoop", {
|
|
524
|
+
phase: "exit",
|
|
525
|
+
reason: "max-iterations",
|
|
526
|
+
iteration,
|
|
527
|
+
});
|
|
528
|
+
await deps.stopAuto(
|
|
529
|
+
ctx,
|
|
530
|
+
pi,
|
|
531
|
+
`Safety: loop exceeded ${MAX_LOOP_ITERATIONS} iterations — possible runaway`,
|
|
532
|
+
);
|
|
533
|
+
break;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
if (!s.cmdCtx) {
|
|
537
|
+
debugLog("autoLoop", { phase: "exit", reason: "no-cmdCtx" });
|
|
538
|
+
break;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
try {
|
|
542
|
+
// ── Blanket try/catch: one bad iteration must not kill the session
|
|
543
|
+
|
|
544
|
+
if (deps.lockBase() && !deps.validateSessionLock(deps.lockBase())) {
|
|
545
|
+
deps.handleLostSessionLock(ctx);
|
|
546
|
+
debugLog("autoLoop", { phase: "exit", reason: "session-lock-lost" });
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
// ── Phase 1: Pre-dispatch ───────────────────────────────────────────
|
|
551
|
+
|
|
552
|
+
// Resource version guard
|
|
553
|
+
const staleMsg = deps.checkResourcesStale(s.resourceVersionOnStart);
|
|
554
|
+
if (staleMsg) {
|
|
555
|
+
await deps.stopAuto(ctx, pi, staleMsg);
|
|
556
|
+
debugLog("autoLoop", { phase: "exit", reason: "resources-stale" });
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
deps.invalidateAllCaches();
|
|
561
|
+
s.lastPromptCharCount = undefined;
|
|
562
|
+
s.lastBaselineCharCount = undefined;
|
|
563
|
+
|
|
564
|
+
// Pre-dispatch health gate
|
|
565
|
+
try {
|
|
566
|
+
const healthGate = await deps.preDispatchHealthGate(s.basePath);
|
|
567
|
+
if (healthGate.fixesApplied.length > 0) {
|
|
568
|
+
ctx.ui.notify(
|
|
569
|
+
`Pre-dispatch: ${healthGate.fixesApplied.join(", ")}`,
|
|
570
|
+
"info",
|
|
571
|
+
);
|
|
572
|
+
}
|
|
573
|
+
if (!healthGate.proceed) {
|
|
574
|
+
ctx.ui.notify(
|
|
575
|
+
healthGate.reason ?? "Pre-dispatch health check failed.",
|
|
576
|
+
"error",
|
|
577
|
+
);
|
|
578
|
+
await deps.pauseAuto(ctx, pi);
|
|
579
|
+
debugLog("autoLoop", { phase: "exit", reason: "health-gate-failed" });
|
|
580
|
+
break;
|
|
581
|
+
}
|
|
582
|
+
} catch {
|
|
583
|
+
// Non-fatal
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
// Sync project root artifacts into worktree
|
|
587
|
+
if (
|
|
588
|
+
s.originalBasePath &&
|
|
589
|
+
s.basePath !== s.originalBasePath &&
|
|
590
|
+
s.currentMilestoneId
|
|
591
|
+
) {
|
|
592
|
+
deps.syncProjectRootToWorktree(
|
|
593
|
+
s.originalBasePath,
|
|
594
|
+
s.basePath,
|
|
595
|
+
s.currentMilestoneId,
|
|
596
|
+
);
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
// Derive state
|
|
600
|
+
let state = await deps.deriveState(s.basePath);
|
|
601
|
+
let mid = state.activeMilestone?.id;
|
|
602
|
+
let midTitle = state.activeMilestone?.title;
|
|
603
|
+
debugLog("autoLoop", {
|
|
604
|
+
phase: "state-derived",
|
|
605
|
+
iteration,
|
|
606
|
+
mid,
|
|
607
|
+
statePhase: state.phase,
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
// ── Milestone transition ────────────────────────────────────────────
|
|
611
|
+
if (mid && s.currentMilestoneId && mid !== s.currentMilestoneId) {
|
|
612
|
+
ctx.ui.notify(
|
|
613
|
+
`Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}: ${midTitle}.`,
|
|
614
|
+
"info",
|
|
615
|
+
);
|
|
616
|
+
deps.sendDesktopNotification(
|
|
617
|
+
"GSD",
|
|
618
|
+
`Milestone ${s.currentMilestoneId} complete!`,
|
|
619
|
+
"success",
|
|
620
|
+
"milestone",
|
|
621
|
+
);
|
|
622
|
+
|
|
623
|
+
const vizPrefs = deps.loadEffectiveGSDPreferences()?.preferences;
|
|
624
|
+
if (vizPrefs?.auto_visualize) {
|
|
625
|
+
ctx.ui.notify("Run /gsd visualize to see progress overview.", "info");
|
|
626
|
+
}
|
|
627
|
+
if (vizPrefs?.auto_report !== false) {
|
|
628
|
+
try {
|
|
629
|
+
const { loadVisualizerData } = await import("./visualizer-data.js");
|
|
630
|
+
const { generateHtmlReport } = await import("./export-html.js");
|
|
631
|
+
const { writeReportSnapshot } = await import("./reports.js");
|
|
632
|
+
const { basename } = await import("node:path");
|
|
633
|
+
const snapData = await loadVisualizerData(s.basePath);
|
|
634
|
+
const completedMs = snapData.milestones.find(
|
|
635
|
+
(m: { id: string }) => m.id === s.currentMilestoneId,
|
|
636
|
+
);
|
|
637
|
+
const msTitle = completedMs?.title ?? s.currentMilestoneId;
|
|
638
|
+
const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
|
|
639
|
+
const projName = basename(s.basePath);
|
|
640
|
+
const doneSlices = snapData.milestones.reduce(
|
|
641
|
+
(acc: number, m: { slices: { done: boolean }[] }) =>
|
|
642
|
+
acc +
|
|
643
|
+
m.slices.filter((sl: { done: boolean }) => sl.done).length,
|
|
644
|
+
0,
|
|
645
|
+
);
|
|
646
|
+
const totalSlices = snapData.milestones.reduce(
|
|
647
|
+
(acc: number, m: { slices: unknown[] }) => acc + m.slices.length,
|
|
648
|
+
0,
|
|
649
|
+
);
|
|
650
|
+
const outPath = writeReportSnapshot({
|
|
651
|
+
basePath: s.basePath,
|
|
652
|
+
html: generateHtmlReport(snapData, {
|
|
653
|
+
projectName: projName,
|
|
654
|
+
projectPath: s.basePath,
|
|
655
|
+
gsdVersion,
|
|
656
|
+
milestoneId: s.currentMilestoneId,
|
|
657
|
+
indexRelPath: "index.html",
|
|
658
|
+
}),
|
|
659
|
+
milestoneId: s.currentMilestoneId!,
|
|
660
|
+
milestoneTitle: msTitle,
|
|
661
|
+
kind: "milestone",
|
|
662
|
+
projectName: projName,
|
|
663
|
+
projectPath: s.basePath,
|
|
664
|
+
gsdVersion,
|
|
665
|
+
totalCost: snapData.totals?.cost ?? 0,
|
|
666
|
+
totalTokens: snapData.totals?.tokens.total ?? 0,
|
|
667
|
+
totalDuration: snapData.totals?.duration ?? 0,
|
|
668
|
+
doneSlices,
|
|
669
|
+
totalSlices,
|
|
670
|
+
doneMilestones: snapData.milestones.filter(
|
|
671
|
+
(m: { status: string }) => m.status === "complete",
|
|
672
|
+
).length,
|
|
673
|
+
totalMilestones: snapData.milestones.length,
|
|
674
|
+
phase: snapData.phase,
|
|
675
|
+
});
|
|
676
|
+
ctx.ui.notify(
|
|
677
|
+
`Report saved: .gsd/reports/${(await import("node:path")).basename(outPath)} — open index.html to browse progression.`,
|
|
678
|
+
"info",
|
|
679
|
+
);
|
|
680
|
+
} catch (err) {
|
|
681
|
+
ctx.ui.notify(
|
|
682
|
+
`Report generation failed: ${err instanceof Error ? err.message : String(err)}`,
|
|
683
|
+
"warning",
|
|
684
|
+
);
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
// Reset dispatch counters for new milestone
|
|
689
|
+
s.unitDispatchCount.clear();
|
|
690
|
+
s.unitRecoveryCount.clear();
|
|
691
|
+
s.unitLifetimeDispatches.clear();
|
|
692
|
+
lastDerivedUnit = "";
|
|
693
|
+
sameUnitCount = 0;
|
|
694
|
+
|
|
695
|
+
// Worktree lifecycle on milestone transition — merge current, enter next
|
|
696
|
+
deps.resolver.mergeAndExit(s.currentMilestoneId!, ctx.ui);
|
|
697
|
+
deps.invalidateAllCaches();
|
|
698
|
+
|
|
699
|
+
state = await deps.deriveState(s.basePath);
|
|
700
|
+
mid = state.activeMilestone?.id;
|
|
701
|
+
midTitle = state.activeMilestone?.title;
|
|
702
|
+
|
|
703
|
+
if (mid) {
|
|
704
|
+
if (deps.getIsolationMode() !== "none") {
|
|
705
|
+
deps.captureIntegrationBranch(s.basePath, mid, {
|
|
706
|
+
commitDocs:
|
|
707
|
+
deps.loadEffectiveGSDPreferences()?.preferences?.git
|
|
708
|
+
?.commit_docs,
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
deps.resolver.enterMilestone(mid, ctx.ui);
|
|
712
|
+
} else {
|
|
713
|
+
// mid is undefined — no milestone to capture integration branch for
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
const pendingIds = state.registry
|
|
717
|
+
.filter(
|
|
718
|
+
(m: { status: string }) =>
|
|
719
|
+
m.status !== "complete" && m.status !== "parked",
|
|
720
|
+
)
|
|
721
|
+
.map((m: { id: string }) => m.id);
|
|
722
|
+
deps.pruneQueueOrder(s.basePath, pendingIds);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
if (mid) {
|
|
726
|
+
s.currentMilestoneId = mid;
|
|
727
|
+
deps.setActiveMilestoneId(s.basePath, mid);
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
// ── Terminal conditions ──────────────────────────────────────────────
|
|
731
|
+
|
|
732
|
+
if (!mid) {
|
|
733
|
+
if (s.currentUnit) {
|
|
734
|
+
await deps.closeoutUnit(
|
|
735
|
+
ctx,
|
|
736
|
+
s.basePath,
|
|
737
|
+
s.currentUnit.type,
|
|
738
|
+
s.currentUnit.id,
|
|
739
|
+
s.currentUnit.startedAt,
|
|
740
|
+
deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
const incomplete = state.registry.filter(
|
|
745
|
+
(m: { status: string }) =>
|
|
746
|
+
m.status !== "complete" && m.status !== "parked",
|
|
747
|
+
);
|
|
748
|
+
if (incomplete.length === 0) {
|
|
749
|
+
// All milestones complete — merge milestone branch before stopping
|
|
750
|
+
if (s.currentMilestoneId) {
|
|
751
|
+
deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
|
|
752
|
+
}
|
|
753
|
+
deps.sendDesktopNotification(
|
|
754
|
+
"GSD",
|
|
755
|
+
"All milestones complete!",
|
|
756
|
+
"success",
|
|
757
|
+
"milestone",
|
|
758
|
+
);
|
|
759
|
+
await deps.stopAuto(ctx, pi, "All milestones complete");
|
|
760
|
+
} else if (state.phase === "blocked") {
|
|
761
|
+
const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
|
|
762
|
+
await deps.stopAuto(ctx, pi, blockerMsg);
|
|
763
|
+
ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
|
|
764
|
+
deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
|
|
765
|
+
} else {
|
|
766
|
+
const ids = incomplete.map((m: { id: string }) => m.id).join(", ");
|
|
767
|
+
const diag = `basePath=${s.basePath}, milestones=[${state.registry.map((m: { id: string; status: string }) => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
|
|
768
|
+
ctx.ui.notify(
|
|
769
|
+
`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`,
|
|
770
|
+
"error",
|
|
771
|
+
);
|
|
772
|
+
await deps.stopAuto(
|
|
773
|
+
ctx,
|
|
774
|
+
pi,
|
|
775
|
+
`No active milestone — ${incomplete.length} incomplete (${ids}), see diagnostic above`,
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
debugLog("autoLoop", { phase: "exit", reason: "no-active-milestone" });
|
|
779
|
+
break;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
if (!midTitle) {
|
|
783
|
+
midTitle = mid;
|
|
784
|
+
ctx.ui.notify(
|
|
785
|
+
`Milestone ${mid} has no title in roadmap — using ID as fallback.`,
|
|
786
|
+
"warning",
|
|
787
|
+
);
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
// Mid-merge safety check
|
|
791
|
+
if (deps.reconcileMergeState(s.basePath, ctx)) {
|
|
792
|
+
deps.invalidateAllCaches();
|
|
793
|
+
state = await deps.deriveState(s.basePath);
|
|
794
|
+
mid = state.activeMilestone?.id;
|
|
795
|
+
midTitle = state.activeMilestone?.title;
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
if (!mid || !midTitle) {
|
|
799
|
+
if (s.currentUnit) {
|
|
800
|
+
await deps.closeoutUnit(
|
|
801
|
+
ctx,
|
|
802
|
+
s.basePath,
|
|
803
|
+
s.currentUnit.type,
|
|
804
|
+
s.currentUnit.id,
|
|
805
|
+
s.currentUnit.startedAt,
|
|
806
|
+
deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
|
|
807
|
+
);
|
|
808
|
+
}
|
|
809
|
+
const noMilestoneReason = !mid
|
|
810
|
+
? "No active milestone after merge reconciliation"
|
|
811
|
+
: `Milestone ${mid} has no title after reconciliation`;
|
|
812
|
+
await deps.stopAuto(ctx, pi, noMilestoneReason);
|
|
813
|
+
debugLog("autoLoop", {
|
|
814
|
+
phase: "exit",
|
|
815
|
+
reason: "no-milestone-after-reconciliation",
|
|
816
|
+
});
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
// Terminal: complete
|
|
821
|
+
if (state.phase === "complete") {
|
|
822
|
+
if (s.currentUnit) {
|
|
823
|
+
await deps.closeoutUnit(
|
|
824
|
+
ctx,
|
|
825
|
+
s.basePath,
|
|
826
|
+
s.currentUnit.type,
|
|
827
|
+
s.currentUnit.id,
|
|
828
|
+
s.currentUnit.startedAt,
|
|
829
|
+
deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
|
|
830
|
+
);
|
|
831
|
+
}
|
|
832
|
+
// Milestone merge on complete
|
|
833
|
+
if (s.currentMilestoneId) {
|
|
834
|
+
deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
|
|
835
|
+
}
|
|
836
|
+
deps.sendDesktopNotification(
|
|
837
|
+
"GSD",
|
|
838
|
+
`Milestone ${mid} complete!`,
|
|
839
|
+
"success",
|
|
840
|
+
"milestone",
|
|
841
|
+
);
|
|
842
|
+
await deps.stopAuto(ctx, pi, `Milestone ${mid} complete`);
|
|
843
|
+
debugLog("autoLoop", { phase: "exit", reason: "milestone-complete" });
|
|
844
|
+
break;
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
// Terminal: blocked
|
|
848
|
+
if (state.phase === "blocked") {
|
|
849
|
+
if (s.currentUnit) {
|
|
850
|
+
await deps.closeoutUnit(
|
|
851
|
+
ctx,
|
|
852
|
+
s.basePath,
|
|
853
|
+
s.currentUnit.type,
|
|
854
|
+
s.currentUnit.id,
|
|
855
|
+
s.currentUnit.startedAt,
|
|
856
|
+
deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
|
|
857
|
+
);
|
|
858
|
+
}
|
|
859
|
+
const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
|
|
860
|
+
await deps.stopAuto(ctx, pi, blockerMsg);
|
|
861
|
+
ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
|
|
862
|
+
deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
|
|
863
|
+
debugLog("autoLoop", { phase: "exit", reason: "blocked" });
|
|
864
|
+
break;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
// ── Phase 2: Guards ─────────────────────────────────────────────────
|
|
868
|
+
|
|
869
|
+
const prefs = deps.loadEffectiveGSDPreferences()?.preferences;
|
|
870
|
+
|
|
871
|
+
// Budget ceiling guard
|
|
872
|
+
const budgetCeiling = prefs?.budget_ceiling;
|
|
873
|
+
if (budgetCeiling !== undefined && budgetCeiling > 0) {
|
|
874
|
+
const currentLedger = deps.getLedger() as { units: unknown } | null;
|
|
875
|
+
const totalCost = currentLedger
|
|
876
|
+
? deps.getProjectTotals(currentLedger.units).cost
|
|
877
|
+
: 0;
|
|
878
|
+
const budgetPct = totalCost / budgetCeiling;
|
|
879
|
+
const budgetAlertLevel = deps.getBudgetAlertLevel(budgetPct);
|
|
880
|
+
const newBudgetAlertLevel = deps.getNewBudgetAlertLevel(
|
|
881
|
+
s.lastBudgetAlertLevel,
|
|
882
|
+
budgetPct,
|
|
883
|
+
);
|
|
884
|
+
const enforcement = prefs?.budget_enforcement ?? "pause";
|
|
885
|
+
const budgetEnforcementAction = deps.getBudgetEnforcementAction(
|
|
886
|
+
enforcement,
|
|
887
|
+
budgetPct,
|
|
888
|
+
);
|
|
889
|
+
|
|
890
|
+
if (newBudgetAlertLevel === 100 && budgetEnforcementAction !== "none") {
|
|
891
|
+
const msg = `Budget ceiling ${deps.formatCost(budgetCeiling)} reached (spent ${deps.formatCost(totalCost)}).`;
|
|
892
|
+
s.lastBudgetAlertLevel =
|
|
893
|
+
newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
|
|
894
|
+
if (budgetEnforcementAction === "halt") {
|
|
895
|
+
deps.sendDesktopNotification("GSD", msg, "error", "budget");
|
|
896
|
+
await deps.stopAuto(ctx, pi, "Budget ceiling reached");
|
|
897
|
+
debugLog("autoLoop", { phase: "exit", reason: "budget-halt" });
|
|
898
|
+
break;
|
|
899
|
+
}
|
|
900
|
+
if (budgetEnforcementAction === "pause") {
|
|
901
|
+
ctx.ui.notify(
|
|
902
|
+
`${msg} Pausing auto-mode — /gsd auto to override and continue.`,
|
|
903
|
+
"warning",
|
|
904
|
+
);
|
|
905
|
+
deps.sendDesktopNotification("GSD", msg, "warning", "budget");
|
|
906
|
+
await deps.pauseAuto(ctx, pi);
|
|
907
|
+
debugLog("autoLoop", { phase: "exit", reason: "budget-pause" });
|
|
908
|
+
break;
|
|
909
|
+
}
|
|
910
|
+
ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
|
|
911
|
+
deps.sendDesktopNotification("GSD", msg, "warning", "budget");
|
|
912
|
+
} else if (newBudgetAlertLevel === 90) {
|
|
913
|
+
s.lastBudgetAlertLevel =
|
|
914
|
+
newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
|
|
915
|
+
ctx.ui.notify(
|
|
916
|
+
`Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
|
|
917
|
+
"warning",
|
|
918
|
+
);
|
|
919
|
+
deps.sendDesktopNotification(
|
|
920
|
+
"GSD",
|
|
921
|
+
`Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
|
|
922
|
+
"warning",
|
|
923
|
+
"budget",
|
|
924
|
+
);
|
|
925
|
+
} else if (newBudgetAlertLevel === 80) {
|
|
926
|
+
s.lastBudgetAlertLevel =
|
|
927
|
+
newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
|
|
928
|
+
ctx.ui.notify(
|
|
929
|
+
`Approaching budget ceiling — 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
|
|
930
|
+
"warning",
|
|
931
|
+
);
|
|
932
|
+
deps.sendDesktopNotification(
|
|
933
|
+
"GSD",
|
|
934
|
+
`Approaching budget ceiling — 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
|
|
935
|
+
"warning",
|
|
936
|
+
"budget",
|
|
937
|
+
);
|
|
938
|
+
} else if (newBudgetAlertLevel === 75) {
|
|
939
|
+
s.lastBudgetAlertLevel =
|
|
940
|
+
newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
|
|
941
|
+
ctx.ui.notify(
|
|
942
|
+
`Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
|
|
943
|
+
"info",
|
|
944
|
+
);
|
|
945
|
+
deps.sendDesktopNotification(
|
|
946
|
+
"GSD",
|
|
947
|
+
`Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
|
|
948
|
+
"info",
|
|
949
|
+
"budget",
|
|
950
|
+
);
|
|
951
|
+
} else if (budgetAlertLevel === 0) {
|
|
952
|
+
s.lastBudgetAlertLevel = 0;
|
|
953
|
+
}
|
|
954
|
+
} else {
|
|
955
|
+
s.lastBudgetAlertLevel = 0;
|
|
956
|
+
}
|
|
957
|
+
|
|
958
|
+
// Context window guard
|
|
959
|
+
const contextThreshold = prefs?.context_pause_threshold ?? 0;
|
|
960
|
+
if (contextThreshold > 0 && s.cmdCtx) {
|
|
961
|
+
const contextUsage = s.cmdCtx.getContextUsage();
|
|
962
|
+
if (
|
|
963
|
+
contextUsage &&
|
|
964
|
+
contextUsage.percent !== null &&
|
|
965
|
+
contextUsage.percent >= contextThreshold
|
|
966
|
+
) {
|
|
967
|
+
const msg = `Context window at ${contextUsage.percent}% (threshold: ${contextThreshold}%). Pausing to prevent truncated output.`;
|
|
968
|
+
ctx.ui.notify(
|
|
969
|
+
`${msg} Run /gsd auto to continue (will start fresh session).`,
|
|
970
|
+
"warning",
|
|
971
|
+
);
|
|
972
|
+
deps.sendDesktopNotification(
|
|
973
|
+
"GSD",
|
|
974
|
+
`Context ${contextUsage.percent}% — paused`,
|
|
975
|
+
"warning",
|
|
976
|
+
"attention",
|
|
977
|
+
);
|
|
978
|
+
await deps.pauseAuto(ctx, pi);
|
|
979
|
+
debugLog("autoLoop", { phase: "exit", reason: "context-window" });
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
}
|
|
983
|
+
|
|
984
|
+
// Secrets re-check gate
|
|
985
|
+
try {
|
|
986
|
+
const manifestStatus = await deps.getManifestStatus(s.basePath, mid);
|
|
987
|
+
if (manifestStatus && manifestStatus.pending.length > 0) {
|
|
988
|
+
const result = await deps.collectSecretsFromManifest(
|
|
989
|
+
s.basePath,
|
|
990
|
+
mid,
|
|
991
|
+
ctx,
|
|
992
|
+
);
|
|
993
|
+
if (
|
|
994
|
+
result &&
|
|
995
|
+
result.applied &&
|
|
996
|
+
result.skipped &&
|
|
997
|
+
result.existingSkipped
|
|
998
|
+
) {
|
|
999
|
+
ctx.ui.notify(
|
|
1000
|
+
`Secrets collected: ${result.applied.length} applied, ${result.skipped.length} skipped, ${result.existingSkipped.length} already set.`,
|
|
1001
|
+
"info",
|
|
1002
|
+
);
|
|
1003
|
+
} else {
|
|
1004
|
+
ctx.ui.notify("Secrets collection skipped.", "info");
|
|
1005
|
+
}
|
|
1006
|
+
}
|
|
1007
|
+
} catch (err) {
|
|
1008
|
+
ctx.ui.notify(
|
|
1009
|
+
`Secrets collection error: ${err instanceof Error ? err.message : String(err)}. Continuing with next task.`,
|
|
1010
|
+
"warning",
|
|
1011
|
+
);
|
|
1012
|
+
}
|
|
1013
|
+
|
|
1014
|
+
// ── Phase 3: Dispatch resolution ────────────────────────────────────
|
|
1015
|
+
|
|
1016
|
+
debugLog("autoLoop", { phase: "dispatch-resolve", iteration });
|
|
1017
|
+
const dispatchResult = await deps.resolveDispatch({
|
|
1018
|
+
basePath: s.basePath,
|
|
1019
|
+
mid,
|
|
1020
|
+
midTitle: midTitle!,
|
|
1021
|
+
state,
|
|
1022
|
+
prefs,
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
if (dispatchResult.action === "stop") {
|
|
1026
|
+
if (s.currentUnit) {
|
|
1027
|
+
await deps.closeoutUnit(
|
|
1028
|
+
ctx,
|
|
1029
|
+
s.basePath,
|
|
1030
|
+
s.currentUnit.type,
|
|
1031
|
+
s.currentUnit.id,
|
|
1032
|
+
s.currentUnit.startedAt,
|
|
1033
|
+
deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
|
|
1034
|
+
);
|
|
1035
|
+
}
|
|
1036
|
+
await deps.stopAuto(ctx, pi, dispatchResult.reason);
|
|
1037
|
+
debugLog("autoLoop", { phase: "exit", reason: "dispatch-stop" });
|
|
1038
|
+
break;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
if (dispatchResult.action !== "dispatch") {
|
|
1042
|
+
// Non-dispatch action (e.g. "skip") — re-derive state
|
|
1043
|
+
await new Promise((r) => setImmediate(r));
|
|
1044
|
+
continue;
|
|
1045
|
+
}
|
|
1046
|
+
|
|
1047
|
+
let unitType = dispatchResult.unitType;
|
|
1048
|
+
let unitId = dispatchResult.unitId;
|
|
1049
|
+
let prompt = dispatchResult.prompt;
|
|
1050
|
+
const pauseAfterUatDispatch = dispatchResult.pauseAfterDispatch ?? false;
|
|
1051
|
+
|
|
1052
|
+
// ── Same-unit stuck counter with graduated recovery ──
|
|
1053
|
+
const derivedKey = `${unitType}/${unitId}`;
|
|
1054
|
+
if (derivedKey === lastDerivedUnit && !s.pendingVerificationRetry) {
|
|
1055
|
+
sameUnitCount++;
|
|
1056
|
+
debugLog("autoLoop", {
|
|
1057
|
+
phase: "stuck-check",
|
|
1058
|
+
unitType,
|
|
1059
|
+
unitId,
|
|
1060
|
+
sameUnitCount,
|
|
1061
|
+
});
|
|
1062
|
+
|
|
1063
|
+
if (sameUnitCount === 3) {
|
|
1064
|
+
// Level 1: try verifying the artifact — maybe it was written but not detected
|
|
1065
|
+
const artifactExists = deps.verifyExpectedArtifact(
|
|
1066
|
+
unitType,
|
|
1067
|
+
unitId,
|
|
1068
|
+
s.basePath,
|
|
1069
|
+
);
|
|
1070
|
+
if (artifactExists) {
|
|
1071
|
+
debugLog("autoLoop", {
|
|
1072
|
+
phase: "stuck-recovery",
|
|
1073
|
+
level: 1,
|
|
1074
|
+
action: "artifact-found",
|
|
1075
|
+
});
|
|
1076
|
+
ctx.ui.notify(
|
|
1077
|
+
`Stuck recovery: artifact for ${unitType} ${unitId} found on disk. Invalidating caches.`,
|
|
1078
|
+
"info",
|
|
1079
|
+
);
|
|
1080
|
+
deps.invalidateAllCaches();
|
|
1081
|
+
continue;
|
|
1082
|
+
}
|
|
1083
|
+
ctx.ui.notify(
|
|
1084
|
+
`Stuck on ${unitType} ${unitId} (attempt ${sameUnitCount}). Invalidating caches and retrying.`,
|
|
1085
|
+
"warning",
|
|
1086
|
+
);
|
|
1087
|
+
deps.invalidateAllCaches();
|
|
1088
|
+
} else if (sameUnitCount === 5) {
|
|
1089
|
+
// Level 2: hard stop — genuinely stuck
|
|
1090
|
+
debugLog("autoLoop", {
|
|
1091
|
+
phase: "stuck-detected",
|
|
1092
|
+
unitType,
|
|
1093
|
+
unitId,
|
|
1094
|
+
sameUnitCount,
|
|
1095
|
+
});
|
|
1096
|
+
await deps.stopAuto(
|
|
1097
|
+
ctx,
|
|
1098
|
+
pi,
|
|
1099
|
+
`Stuck: ${unitType} ${unitId} derived ${sameUnitCount} consecutive times without progress`,
|
|
1100
|
+
);
|
|
1101
|
+
ctx.ui.notify(
|
|
1102
|
+
`Stuck on ${unitType} ${unitId} — deriveState returns the same unit after ${sameUnitCount} attempts. The expected artifact was not written.`,
|
|
1103
|
+
"error",
|
|
1104
|
+
);
|
|
1105
|
+
break;
|
|
1106
|
+
}
|
|
1107
|
+
} else {
|
|
1108
|
+
if (derivedKey !== lastDerivedUnit) {
|
|
1109
|
+
debugLog("autoLoop", {
|
|
1110
|
+
phase: "stuck-counter-reset",
|
|
1111
|
+
from: lastDerivedUnit,
|
|
1112
|
+
to: derivedKey,
|
|
1113
|
+
});
|
|
1114
|
+
}
|
|
1115
|
+
lastDerivedUnit = derivedKey;
|
|
1116
|
+
sameUnitCount = 0;
|
|
1117
|
+
}
|
|
1118
|
+
|
|
1119
|
+
// Pre-dispatch hooks
|
|
1120
|
+
const preDispatchResult = deps.runPreDispatchHooks(
|
|
1121
|
+
unitType,
|
|
1122
|
+
unitId,
|
|
1123
|
+
prompt,
|
|
1124
|
+
s.basePath,
|
|
1125
|
+
);
|
|
1126
|
+
if (preDispatchResult.firedHooks.length > 0) {
|
|
1127
|
+
ctx.ui.notify(
|
|
1128
|
+
`Pre-dispatch hook${preDispatchResult.firedHooks.length > 1 ? "s" : ""}: ${preDispatchResult.firedHooks.join(", ")}`,
|
|
1129
|
+
"info",
|
|
1130
|
+
);
|
|
1131
|
+
}
|
|
1132
|
+
if (preDispatchResult.action === "skip") {
|
|
1133
|
+
ctx.ui.notify(
|
|
1134
|
+
`Skipping ${unitType} ${unitId} (pre-dispatch hook).`,
|
|
1135
|
+
"info",
|
|
1136
|
+
);
|
|
1137
|
+
await new Promise((r) => setImmediate(r));
|
|
1138
|
+
continue;
|
|
1139
|
+
}
|
|
1140
|
+
if (preDispatchResult.action === "replace") {
|
|
1141
|
+
prompt = preDispatchResult.prompt ?? prompt;
|
|
1142
|
+
if (preDispatchResult.unitType) unitType = preDispatchResult.unitType;
|
|
1143
|
+
} else if (preDispatchResult.prompt) {
|
|
1144
|
+
prompt = preDispatchResult.prompt;
|
|
1145
|
+
}
|
|
1146
|
+
|
|
1147
|
+
const priorSliceBlocker = deps.getPriorSliceCompletionBlocker(
|
|
1148
|
+
s.basePath,
|
|
1149
|
+
deps.getMainBranch(s.basePath),
|
|
1150
|
+
unitType,
|
|
1151
|
+
unitId,
|
|
1152
|
+
);
|
|
1153
|
+
if (priorSliceBlocker) {
|
|
1154
|
+
await deps.stopAuto(ctx, pi, priorSliceBlocker);
|
|
1155
|
+
debugLog("autoLoop", { phase: "exit", reason: "prior-slice-blocker" });
|
|
1156
|
+
break;
|
|
1157
|
+
}
|
|
1158
|
+
|
|
1159
|
+
const observabilityIssues = await deps.collectObservabilityWarnings(
|
|
1160
|
+
ctx,
|
|
1161
|
+
s.basePath,
|
|
1162
|
+
unitType,
|
|
1163
|
+
unitId,
|
|
1164
|
+
);
|
|
1165
|
+
|
|
1166
|
+
// ── Phase 4: Unit execution ─────────────────────────────────────────
|
|
1167
|
+
|
|
1168
|
+
debugLog("autoLoop", {
|
|
1169
|
+
phase: "unit-execution",
|
|
1170
|
+
iteration,
|
|
1171
|
+
unitType,
|
|
1172
|
+
unitId,
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1175
|
+
// Closeout previous unit
|
|
1176
|
+
if (s.currentUnit) {
|
|
1177
|
+
await deps.closeoutUnit(
|
|
1178
|
+
ctx,
|
|
1179
|
+
s.basePath,
|
|
1180
|
+
s.currentUnit.type,
|
|
1181
|
+
s.currentUnit.id,
|
|
1182
|
+
s.currentUnit.startedAt,
|
|
1183
|
+
deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
|
|
1184
|
+
);
|
|
1185
|
+
|
|
1186
|
+
if (s.currentUnitRouting) {
|
|
1187
|
+
const isRetry =
|
|
1188
|
+
s.currentUnit.type === unitType && s.currentUnit.id === unitId;
|
|
1189
|
+
deps.recordOutcome(
|
|
1190
|
+
s.currentUnit.type,
|
|
1191
|
+
s.currentUnitRouting.tier as "light" | "standard" | "heavy",
|
|
1192
|
+
!isRetry,
|
|
1193
|
+
);
|
|
1194
|
+
}
|
|
1195
|
+
|
|
1196
|
+
const closeoutKey = `${s.currentUnit.type}/${s.currentUnit.id}`;
|
|
1197
|
+
const incomingKey = `${unitType}/${unitId}`;
|
|
1198
|
+
const isHookUnit = s.currentUnit.type.startsWith("hook/");
|
|
1199
|
+
const artifactVerified =
|
|
1200
|
+
isHookUnit ||
|
|
1201
|
+
deps.verifyExpectedArtifact(
|
|
1202
|
+
s.currentUnit.type,
|
|
1203
|
+
s.currentUnit.id,
|
|
1204
|
+
s.basePath,
|
|
1205
|
+
);
|
|
1206
|
+
if (closeoutKey !== incomingKey && artifactVerified) {
|
|
1207
|
+
s.completedUnits.push({
|
|
1208
|
+
type: s.currentUnit.type,
|
|
1209
|
+
id: s.currentUnit.id,
|
|
1210
|
+
startedAt: s.currentUnit.startedAt,
|
|
1211
|
+
finishedAt: Date.now(),
|
|
1212
|
+
});
|
|
1213
|
+
if (s.completedUnits.length > 200) {
|
|
1214
|
+
s.completedUnits = s.completedUnits.slice(-200);
|
|
1215
|
+
}
|
|
1216
|
+
deps.clearUnitRuntimeRecord(
|
|
1217
|
+
s.basePath,
|
|
1218
|
+
s.currentUnit.type,
|
|
1219
|
+
s.currentUnit.id,
|
|
1220
|
+
);
|
|
1221
|
+
s.unitDispatchCount.delete(
|
|
1222
|
+
`${s.currentUnit.type}/${s.currentUnit.id}`,
|
|
1223
|
+
);
|
|
1224
|
+
s.unitRecoveryCount.delete(
|
|
1225
|
+
`${s.currentUnit.type}/${s.currentUnit.id}`,
|
|
1226
|
+
);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
s.currentUnit = { type: unitType, id: unitId, startedAt: Date.now() };
|
|
1231
|
+
deps.captureAvailableSkills();
|
|
1232
|
+
deps.writeUnitRuntimeRecord(
|
|
1233
|
+
s.basePath,
|
|
1234
|
+
unitType,
|
|
1235
|
+
unitId,
|
|
1236
|
+
s.currentUnit.startedAt,
|
|
1237
|
+
{
|
|
1238
|
+
phase: "dispatched",
|
|
1239
|
+
wrapupWarningSent: false,
|
|
1240
|
+
timeoutAt: null,
|
|
1241
|
+
lastProgressAt: s.currentUnit.startedAt,
|
|
1242
|
+
progressCount: 0,
|
|
1243
|
+
lastProgressKind: "dispatch",
|
|
1244
|
+
},
|
|
1245
|
+
);
|
|
1246
|
+
|
|
1247
|
+
// Status bar + progress widget
|
|
1248
|
+
ctx.ui.setStatus("gsd-auto", "auto");
|
|
1249
|
+
if (mid)
|
|
1250
|
+
deps.updateSliceProgressCache(s.basePath, mid, state.activeSlice?.id);
|
|
1251
|
+
deps.updateProgressWidget(ctx, unitType, unitId, state);
|
|
1252
|
+
|
|
1253
|
+
deps.ensurePreconditions(unitType, unitId, s.basePath, state);
|
|
1254
|
+
|
|
1255
|
+
// Prompt injection
|
|
1256
|
+
const MAX_RECOVERY_CHARS = 50_000;
|
|
1257
|
+
let finalPrompt = prompt;
|
|
1258
|
+
|
|
1259
|
+
if (s.pendingVerificationRetry) {
|
|
1260
|
+
const retryCtx = s.pendingVerificationRetry;
|
|
1261
|
+
s.pendingVerificationRetry = null;
|
|
1262
|
+
const capped =
|
|
1263
|
+
retryCtx.failureContext.length > MAX_RECOVERY_CHARS
|
|
1264
|
+
? retryCtx.failureContext.slice(0, MAX_RECOVERY_CHARS) +
|
|
1265
|
+
"\n\n[...failure context truncated]"
|
|
1266
|
+
: retryCtx.failureContext;
|
|
1267
|
+
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}`;
|
|
1268
|
+
}
|
|
1269
|
+
|
|
1270
|
+
if (s.pendingCrashRecovery) {
|
|
1271
|
+
const capped =
|
|
1272
|
+
s.pendingCrashRecovery.length > MAX_RECOVERY_CHARS
|
|
1273
|
+
? s.pendingCrashRecovery.slice(0, MAX_RECOVERY_CHARS) +
|
|
1274
|
+
"\n\n[...recovery briefing truncated to prevent memory exhaustion]"
|
|
1275
|
+
: s.pendingCrashRecovery;
|
|
1276
|
+
finalPrompt = `${capped}\n\n---\n\n${finalPrompt}`;
|
|
1277
|
+
s.pendingCrashRecovery = null;
|
|
1278
|
+
} else if ((s.unitDispatchCount.get(`${unitType}/${unitId}`) ?? 0) > 1) {
|
|
1279
|
+
const diagnostic = deps.getDeepDiagnostic(s.basePath);
|
|
1280
|
+
if (diagnostic) {
|
|
1281
|
+
const cappedDiag =
|
|
1282
|
+
diagnostic.length > MAX_RECOVERY_CHARS
|
|
1283
|
+
? diagnostic.slice(0, MAX_RECOVERY_CHARS) +
|
|
1284
|
+
"\n\n[...diagnostic truncated to prevent memory exhaustion]"
|
|
1285
|
+
: diagnostic;
|
|
1286
|
+
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}`;
|
|
1287
|
+
}
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
const repairBlock =
|
|
1291
|
+
deps.buildObservabilityRepairBlock(observabilityIssues);
|
|
1292
|
+
if (repairBlock) {
|
|
1293
|
+
finalPrompt = `${finalPrompt}${repairBlock}`;
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
// Prompt char measurement
|
|
1297
|
+
s.lastPromptCharCount = finalPrompt.length;
|
|
1298
|
+
s.lastBaselineCharCount = undefined;
|
|
1299
|
+
if (deps.isDbAvailable()) {
|
|
1300
|
+
try {
|
|
1301
|
+
const { inlineGsdRootFile } = await import("./auto-prompts.js");
|
|
1302
|
+
const [decisionsContent, requirementsContent, projectContent] =
|
|
1303
|
+
await Promise.all([
|
|
1304
|
+
inlineGsdRootFile(s.basePath, "decisions.md", "Decisions"),
|
|
1305
|
+
inlineGsdRootFile(s.basePath, "requirements.md", "Requirements"),
|
|
1306
|
+
inlineGsdRootFile(s.basePath, "project.md", "Project"),
|
|
1307
|
+
]);
|
|
1308
|
+
s.lastBaselineCharCount =
|
|
1309
|
+
(decisionsContent?.length ?? 0) +
|
|
1310
|
+
(requirementsContent?.length ?? 0) +
|
|
1311
|
+
(projectContent?.length ?? 0);
|
|
1312
|
+
} catch {
|
|
1313
|
+
// Non-fatal
|
|
1314
|
+
}
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
// Cache-optimize prompt section ordering
|
|
1318
|
+
try {
|
|
1319
|
+
finalPrompt = deps.reorderForCaching(finalPrompt);
|
|
1320
|
+
} catch (reorderErr) {
|
|
1321
|
+
const msg =
|
|
1322
|
+
reorderErr instanceof Error ? reorderErr.message : String(reorderErr);
|
|
1323
|
+
process.stderr.write(
|
|
1324
|
+
`[gsd] prompt reorder failed (non-fatal): ${msg}\n`,
|
|
1325
|
+
);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// Select and apply model
|
|
1329
|
+
const modelResult = await deps.selectAndApplyModel(
|
|
1330
|
+
ctx,
|
|
1331
|
+
pi,
|
|
1332
|
+
unitType,
|
|
1333
|
+
unitId,
|
|
1334
|
+
s.basePath,
|
|
1335
|
+
prefs,
|
|
1336
|
+
s.verbose,
|
|
1337
|
+
s.autoModeStartModel,
|
|
1338
|
+
);
|
|
1339
|
+
s.currentUnitRouting =
|
|
1340
|
+
modelResult.routing as AutoSession["currentUnitRouting"];
|
|
1341
|
+
|
|
1342
|
+
// Start unit supervision
|
|
1343
|
+
deps.clearUnitTimeout();
|
|
1344
|
+
deps.startUnitSupervision({
|
|
1345
|
+
s,
|
|
1346
|
+
ctx,
|
|
1347
|
+
pi,
|
|
1348
|
+
unitType,
|
|
1349
|
+
unitId,
|
|
1350
|
+
prefs,
|
|
1351
|
+
buildSnapshotOpts: () => deps.buildSnapshotOpts(unitType, unitId),
|
|
1352
|
+
buildRecoveryContext: () => ({}),
|
|
1353
|
+
pauseAuto: deps.pauseAuto,
|
|
1354
|
+
});
|
|
1355
|
+
|
|
1356
|
+
// Session + send + await
|
|
1357
|
+
const sessionFile = deps.getSessionFile(ctx);
|
|
1358
|
+
deps.updateSessionLock(
|
|
1359
|
+
deps.lockBase(),
|
|
1360
|
+
unitType,
|
|
1361
|
+
unitId,
|
|
1362
|
+
s.completedUnits.length,
|
|
1363
|
+
sessionFile,
|
|
1364
|
+
);
|
|
1365
|
+
deps.writeLock(
|
|
1366
|
+
deps.lockBase(),
|
|
1367
|
+
unitType,
|
|
1368
|
+
unitId,
|
|
1369
|
+
s.completedUnits.length,
|
|
1370
|
+
sessionFile,
|
|
1371
|
+
);
|
|
1372
|
+
|
|
1373
|
+
debugLog("autoLoop", {
|
|
1374
|
+
phase: "runUnit-start",
|
|
1375
|
+
iteration,
|
|
1376
|
+
unitType,
|
|
1377
|
+
unitId,
|
|
1378
|
+
});
|
|
1379
|
+
const unitResult = await runUnit(
|
|
1380
|
+
ctx,
|
|
1381
|
+
pi,
|
|
1382
|
+
s,
|
|
1383
|
+
unitType,
|
|
1384
|
+
unitId,
|
|
1385
|
+
finalPrompt,
|
|
1386
|
+
prefs,
|
|
1387
|
+
);
|
|
1388
|
+
debugLog("autoLoop", {
|
|
1389
|
+
phase: "runUnit-end",
|
|
1390
|
+
iteration,
|
|
1391
|
+
unitType,
|
|
1392
|
+
unitId,
|
|
1393
|
+
status: unitResult.status,
|
|
1394
|
+
});
|
|
1395
|
+
|
|
1396
|
+
if (unitResult.status === "cancelled") {
|
|
1397
|
+
ctx.ui.notify(
|
|
1398
|
+
`Session creation timed out or was cancelled for ${unitType} ${unitId}. Will retry.`,
|
|
1399
|
+
"warning",
|
|
1400
|
+
);
|
|
1401
|
+
await deps.stopAuto(ctx, pi, "Session creation failed");
|
|
1402
|
+
debugLog("autoLoop", { phase: "exit", reason: "session-failed" });
|
|
1403
|
+
break;
|
|
1404
|
+
}
|
|
1405
|
+
|
|
1406
|
+
// ── Phase 5: Finalize ───────────────────────────────────────────────
|
|
1407
|
+
|
|
1408
|
+
debugLog("autoLoop", { phase: "finalize", iteration });
|
|
1409
|
+
|
|
1410
|
+
// Clear unit timeout (unit completed)
|
|
1411
|
+
deps.clearUnitTimeout();
|
|
1412
|
+
|
|
1413
|
+
// Post-unit context for pre/post verification
|
|
1414
|
+
const postUnitCtx: PostUnitContext = {
|
|
1415
|
+
s,
|
|
1416
|
+
ctx,
|
|
1417
|
+
pi,
|
|
1418
|
+
buildSnapshotOpts: deps.buildSnapshotOpts,
|
|
1419
|
+
lockBase: deps.lockBase,
|
|
1420
|
+
stopAuto: deps.stopAuto,
|
|
1421
|
+
pauseAuto: deps.pauseAuto,
|
|
1422
|
+
updateProgressWidget: deps.updateProgressWidget,
|
|
1423
|
+
};
|
|
1424
|
+
|
|
1425
|
+
// Pre-verification processing (commit, doctor, state rebuild, etc.)
|
|
1426
|
+
const preResult = await deps.postUnitPreVerification(postUnitCtx);
|
|
1427
|
+
if (preResult === "dispatched") {
|
|
1428
|
+
debugLog("autoLoop", {
|
|
1429
|
+
phase: "exit",
|
|
1430
|
+
reason: "pre-verification-dispatched",
|
|
1431
|
+
});
|
|
1432
|
+
break;
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
if (pauseAfterUatDispatch) {
|
|
1436
|
+
ctx.ui.notify(
|
|
1437
|
+
"UAT requires human execution. Auto-mode will pause after this unit writes the result file.",
|
|
1438
|
+
"info",
|
|
1439
|
+
);
|
|
1440
|
+
await deps.pauseAuto(ctx, pi);
|
|
1441
|
+
debugLog("autoLoop", { phase: "exit", reason: "uat-pause" });
|
|
1442
|
+
break;
|
|
1443
|
+
}
|
|
1444
|
+
|
|
1445
|
+
// Verification gate — the loop handles retries via s.pendingVerificationRetry
|
|
1446
|
+
const verificationResult = await deps.runPostUnitVerification(
|
|
1447
|
+
{ s, ctx, pi },
|
|
1448
|
+
deps.pauseAuto,
|
|
1449
|
+
);
|
|
1450
|
+
|
|
1451
|
+
if (verificationResult === "pause") {
|
|
1452
|
+
debugLog("autoLoop", { phase: "exit", reason: "verification-pause" });
|
|
1453
|
+
break;
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
if (verificationResult === "retry") {
|
|
1457
|
+
// s.pendingVerificationRetry was set by runPostUnitVerification.
|
|
1458
|
+
// Continue the loop — next iteration will inject the retry context into the prompt.
|
|
1459
|
+
debugLog("autoLoop", { phase: "verification-retry", iteration });
|
|
1460
|
+
continue;
|
|
1461
|
+
}
|
|
1462
|
+
|
|
1463
|
+
// Post-verification processing (DB dual-write, hooks, triage, quick-tasks)
|
|
1464
|
+
const postResult = await deps.postUnitPostVerification(postUnitCtx);
|
|
1465
|
+
|
|
1466
|
+
if (postResult === "stopped") {
|
|
1467
|
+
debugLog("autoLoop", {
|
|
1468
|
+
phase: "exit",
|
|
1469
|
+
reason: "post-verification-stopped",
|
|
1470
|
+
});
|
|
1471
|
+
break;
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
if (postResult === "step-wizard") {
|
|
1475
|
+
// Step mode — exit the loop (caller handles wizard)
|
|
1476
|
+
debugLog("autoLoop", { phase: "exit", reason: "step-wizard" });
|
|
1477
|
+
break;
|
|
1478
|
+
}
|
|
1479
|
+
|
|
1480
|
+
// ── Sidecar drain: dispatch enqueued hooks/triage/quick-tasks ──
|
|
1481
|
+
let sidecarBroke = false;
|
|
1482
|
+
while (s.sidecarQueue.length > 0 && s.active) {
|
|
1483
|
+
const item = s.sidecarQueue.shift()!;
|
|
1484
|
+
debugLog("autoLoop", {
|
|
1485
|
+
phase: "sidecar-dequeue",
|
|
1486
|
+
kind: item.kind,
|
|
1487
|
+
unitType: item.unitType,
|
|
1488
|
+
unitId: item.unitId,
|
|
1489
|
+
});
|
|
1490
|
+
|
|
1491
|
+
// Set up as current unit
|
|
1492
|
+
const sidecarStartedAt = Date.now();
|
|
1493
|
+
s.currentUnit = {
|
|
1494
|
+
type: item.unitType,
|
|
1495
|
+
id: item.unitId,
|
|
1496
|
+
startedAt: sidecarStartedAt,
|
|
1497
|
+
};
|
|
1498
|
+
deps.writeUnitRuntimeRecord(
|
|
1499
|
+
s.basePath,
|
|
1500
|
+
item.unitType,
|
|
1501
|
+
item.unitId,
|
|
1502
|
+
sidecarStartedAt,
|
|
1503
|
+
{
|
|
1504
|
+
phase: "dispatched",
|
|
1505
|
+
wrapupWarningSent: false,
|
|
1506
|
+
timeoutAt: null,
|
|
1507
|
+
lastProgressAt: sidecarStartedAt,
|
|
1508
|
+
progressCount: 0,
|
|
1509
|
+
lastProgressKind: "dispatch",
|
|
1510
|
+
},
|
|
1511
|
+
);
|
|
1512
|
+
|
|
1513
|
+
// Model selection (handles hook model override)
|
|
1514
|
+
await deps.selectAndApplyModel(
|
|
1515
|
+
ctx,
|
|
1516
|
+
pi,
|
|
1517
|
+
item.unitType,
|
|
1518
|
+
item.unitId,
|
|
1519
|
+
s.basePath,
|
|
1520
|
+
prefs,
|
|
1521
|
+
s.verbose,
|
|
1522
|
+
s.autoModeStartModel,
|
|
1523
|
+
);
|
|
1524
|
+
|
|
1525
|
+
// Supervision
|
|
1526
|
+
deps.clearUnitTimeout();
|
|
1527
|
+
deps.startUnitSupervision({
|
|
1528
|
+
s,
|
|
1529
|
+
ctx,
|
|
1530
|
+
pi,
|
|
1531
|
+
unitType: item.unitType,
|
|
1532
|
+
unitId: item.unitId,
|
|
1533
|
+
prefs,
|
|
1534
|
+
buildSnapshotOpts: () =>
|
|
1535
|
+
deps.buildSnapshotOpts(item.unitType, item.unitId),
|
|
1536
|
+
buildRecoveryContext: () => ({}),
|
|
1537
|
+
pauseAuto: deps.pauseAuto,
|
|
1538
|
+
});
|
|
1539
|
+
|
|
1540
|
+
// Write lock
|
|
1541
|
+
const sidecarSessionFile = deps.getSessionFile(ctx);
|
|
1542
|
+
deps.writeLock(
|
|
1543
|
+
deps.lockBase(),
|
|
1544
|
+
item.unitType,
|
|
1545
|
+
item.unitId,
|
|
1546
|
+
s.completedUnits.length,
|
|
1547
|
+
sidecarSessionFile,
|
|
1548
|
+
);
|
|
1549
|
+
|
|
1550
|
+
// Execute via standard runUnit
|
|
1551
|
+
const sidecarResult = await runUnit(
|
|
1552
|
+
ctx,
|
|
1553
|
+
pi,
|
|
1554
|
+
s,
|
|
1555
|
+
item.unitType,
|
|
1556
|
+
item.unitId,
|
|
1557
|
+
item.prompt,
|
|
1558
|
+
prefs,
|
|
1559
|
+
);
|
|
1560
|
+
deps.clearUnitTimeout();
|
|
1561
|
+
|
|
1562
|
+
if (sidecarResult.status === "cancelled") {
|
|
1563
|
+
ctx.ui.notify(
|
|
1564
|
+
`Sidecar unit ${item.unitType} ${item.unitId} session cancelled. Stopping.`,
|
|
1565
|
+
"warning",
|
|
1566
|
+
);
|
|
1567
|
+
await deps.stopAuto(ctx, pi, "Sidecar session creation failed");
|
|
1568
|
+
sidecarBroke = true;
|
|
1569
|
+
break;
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
// Run pre-verification for the sidecar unit
|
|
1573
|
+
const sidecarPreResult =
|
|
1574
|
+
await deps.postUnitPreVerification(postUnitCtx);
|
|
1575
|
+
if (sidecarPreResult === "dispatched") {
|
|
1576
|
+
// Pre-verification caused stop/pause
|
|
1577
|
+
debugLog("autoLoop", {
|
|
1578
|
+
phase: "exit",
|
|
1579
|
+
reason: "sidecar-pre-verification-stop",
|
|
1580
|
+
});
|
|
1581
|
+
sidecarBroke = true;
|
|
1582
|
+
break;
|
|
1583
|
+
}
|
|
1584
|
+
|
|
1585
|
+
// Verification gate for non-hook sidecar units (triage, quick-tasks)
|
|
1586
|
+
// Hook units are lightweight and don't need verification.
|
|
1587
|
+
if (item.kind !== "hook") {
|
|
1588
|
+
const sidecarVerification = await deps.runPostUnitVerification(
|
|
1589
|
+
{ s, ctx, pi },
|
|
1590
|
+
deps.pauseAuto,
|
|
1591
|
+
);
|
|
1592
|
+
if (sidecarVerification === "pause") {
|
|
1593
|
+
debugLog("autoLoop", {
|
|
1594
|
+
phase: "exit",
|
|
1595
|
+
reason: "sidecar-verification-pause",
|
|
1596
|
+
});
|
|
1597
|
+
sidecarBroke = true;
|
|
1598
|
+
break;
|
|
1599
|
+
}
|
|
1600
|
+
// "retry" for sidecars — skip retry, just continue (sidecar retries are not worth the complexity)
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
// Post-verification (may enqueue more sidecar items)
|
|
1604
|
+
const sidecarPostResult =
|
|
1605
|
+
await deps.postUnitPostVerification(postUnitCtx);
|
|
1606
|
+
if (sidecarPostResult === "stopped") {
|
|
1607
|
+
debugLog("autoLoop", { phase: "exit", reason: "sidecar-stopped" });
|
|
1608
|
+
sidecarBroke = true;
|
|
1609
|
+
break;
|
|
1610
|
+
}
|
|
1611
|
+
if (sidecarPostResult === "step-wizard") {
|
|
1612
|
+
debugLog("autoLoop", {
|
|
1613
|
+
phase: "exit",
|
|
1614
|
+
reason: "sidecar-step-wizard",
|
|
1615
|
+
});
|
|
1616
|
+
sidecarBroke = true;
|
|
1617
|
+
break;
|
|
1618
|
+
}
|
|
1619
|
+
// "continue" — loop checks sidecarQueue again
|
|
1620
|
+
}
|
|
1621
|
+
|
|
1622
|
+
if (sidecarBroke) break;
|
|
1623
|
+
|
|
1624
|
+
consecutiveErrors = 0; // Iteration completed successfully
|
|
1625
|
+
debugLog("autoLoop", { phase: "iteration-complete", iteration });
|
|
1626
|
+
} catch (loopErr) {
|
|
1627
|
+
// ── Blanket catch: absorb unexpected exceptions, apply graduated recovery ──
|
|
1628
|
+
consecutiveErrors++;
|
|
1629
|
+
const msg = loopErr instanceof Error ? loopErr.message : String(loopErr);
|
|
1630
|
+
debugLog("autoLoop", {
|
|
1631
|
+
phase: "iteration-error",
|
|
1632
|
+
iteration,
|
|
1633
|
+
consecutiveErrors,
|
|
1634
|
+
error: msg,
|
|
1635
|
+
});
|
|
1636
|
+
|
|
1637
|
+
if (consecutiveErrors >= 3) {
|
|
1638
|
+
// 3+ consecutive: hard stop — something is fundamentally broken
|
|
1639
|
+
ctx.ui.notify(
|
|
1640
|
+
`Auto-mode stopped: ${consecutiveErrors} consecutive iteration failures. Last: ${msg}`,
|
|
1641
|
+
"error",
|
|
1642
|
+
);
|
|
1643
|
+
await deps.stopAuto(
|
|
1644
|
+
ctx,
|
|
1645
|
+
pi,
|
|
1646
|
+
`${consecutiveErrors} consecutive iteration failures`,
|
|
1647
|
+
);
|
|
1648
|
+
break;
|
|
1649
|
+
} else if (consecutiveErrors === 2) {
|
|
1650
|
+
// 2nd consecutive: try invalidating caches + re-deriving state
|
|
1651
|
+
ctx.ui.notify(
|
|
1652
|
+
`Iteration error (attempt ${consecutiveErrors}): ${msg}. Invalidating caches and retrying.`,
|
|
1653
|
+
"warning",
|
|
1654
|
+
);
|
|
1655
|
+
deps.invalidateAllCaches();
|
|
1656
|
+
} else {
|
|
1657
|
+
// 1st error: log and retry — transient failures happen
|
|
1658
|
+
ctx.ui.notify(`Iteration error: ${msg}. Retrying.`, "warning");
|
|
1659
|
+
}
|
|
1660
|
+
}
|
|
1661
|
+
}
|
|
1662
|
+
|
|
1663
|
+
_activeSession = null;
|
|
1664
|
+
debugLog("autoLoop", { phase: "exit", totalIterations: iteration });
|
|
1665
|
+
}
|