gsd-pi 2.37.1 → 2.38.0-dev.29edcdc
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/app-paths.js +1 -1
- package/dist/cli.js +9 -0
- package/dist/extension-discovery.d.ts +5 -3
- package/dist/extension-discovery.js +14 -9
- package/dist/extension-registry.js +2 -2
- package/dist/onboarding.js +1 -0
- package/dist/remote-questions-config.js +2 -2
- package/dist/resource-loader.js +34 -1
- package/dist/resources/extensions/browser-tools/package.json +3 -1
- package/dist/resources/extensions/cmux/index.js +55 -1
- package/dist/resources/extensions/context7/package.json +1 -1
- package/dist/resources/extensions/env-utils.js +29 -0
- package/dist/resources/extensions/get-secrets-from-user.js +5 -24
- package/dist/resources/extensions/github-sync/cli.js +284 -0
- package/dist/resources/extensions/github-sync/index.js +73 -0
- package/dist/resources/extensions/github-sync/mapping.js +67 -0
- package/dist/resources/extensions/github-sync/sync.js +424 -0
- package/dist/resources/extensions/github-sync/templates.js +118 -0
- package/dist/resources/extensions/github-sync/types.js +7 -0
- package/dist/resources/extensions/google-search/package.json +3 -1
- package/dist/resources/extensions/gsd/auto/session.js +6 -23
- package/dist/resources/extensions/gsd/auto-dispatch.js +75 -10
- package/dist/resources/extensions/gsd/auto-loop.js +597 -588
- package/dist/resources/extensions/gsd/auto-post-unit.js +111 -68
- package/dist/resources/extensions/gsd/auto-prompts.js +114 -45
- package/dist/resources/extensions/gsd/auto-recovery.js +37 -1
- package/dist/resources/extensions/gsd/auto-start.js +13 -2
- package/dist/resources/extensions/gsd/auto-worktree-sync.js +13 -5
- package/dist/resources/extensions/gsd/auto-worktree.js +3 -3
- package/dist/resources/extensions/gsd/auto.js +143 -96
- package/dist/resources/extensions/gsd/captures.js +9 -1
- package/dist/resources/extensions/gsd/commands-extensions.js +3 -2
- package/dist/resources/extensions/gsd/commands-handlers.js +16 -3
- package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
- package/dist/resources/extensions/gsd/commands.js +24 -3
- package/dist/resources/extensions/gsd/context-budget.js +2 -10
- package/dist/resources/extensions/gsd/detection.js +1 -2
- package/dist/resources/extensions/gsd/docs/preferences-reference.md +0 -2
- package/dist/resources/extensions/gsd/doctor-checks.js +82 -0
- package/dist/resources/extensions/gsd/doctor-environment.js +78 -0
- package/dist/resources/extensions/gsd/doctor-format.js +15 -0
- package/dist/resources/extensions/gsd/doctor-providers.js +62 -12
- package/dist/resources/extensions/gsd/doctor.js +204 -12
- package/dist/resources/extensions/gsd/exit-command.js +2 -1
- package/dist/resources/extensions/gsd/export.js +1 -1
- package/dist/resources/extensions/gsd/files.js +47 -2
- package/dist/resources/extensions/gsd/forensics.js +1 -1
- package/dist/resources/extensions/gsd/git-service.js +15 -12
- package/dist/resources/extensions/gsd/guided-flow.js +82 -32
- package/dist/resources/extensions/gsd/index.js +24 -20
- package/dist/resources/extensions/gsd/migrate/parsers.js +1 -1
- package/dist/resources/extensions/gsd/native-git-bridge.js +37 -0
- package/dist/resources/extensions/gsd/observability-validator.js +24 -0
- package/dist/resources/extensions/gsd/package.json +1 -1
- package/dist/resources/extensions/gsd/preferences-models.js +0 -12
- package/dist/resources/extensions/gsd/preferences-types.js +3 -2
- package/dist/resources/extensions/gsd/preferences-validation.js +101 -11
- package/dist/resources/extensions/gsd/preferences.js +8 -5
- package/dist/resources/extensions/gsd/prompts/discuss.md +11 -14
- package/dist/resources/extensions/gsd/prompts/execute-task.md +2 -2
- package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
- package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
- package/dist/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
- package/dist/resources/extensions/gsd/prompts/plan-slice.md +2 -1
- package/dist/resources/extensions/gsd/prompts/queue.md +4 -8
- package/dist/resources/extensions/gsd/prompts/reactive-execute.md +44 -0
- package/dist/resources/extensions/gsd/prompts/run-uat.md +27 -10
- package/dist/resources/extensions/gsd/prompts/workflow-start.md +2 -2
- package/dist/resources/extensions/gsd/reactive-graph.js +227 -0
- package/dist/resources/extensions/gsd/repo-identity.js +21 -4
- package/dist/resources/extensions/gsd/resource-version.js +2 -1
- package/dist/resources/extensions/gsd/roadmap-mutations.js +24 -0
- package/dist/resources/extensions/gsd/state.js +1 -1
- package/dist/resources/extensions/gsd/templates/task-plan.md +11 -3
- package/dist/resources/extensions/gsd/visualizer-data.js +1 -1
- package/dist/resources/extensions/gsd/worktree.js +35 -16
- package/dist/resources/extensions/mcp-client/index.js +14 -1
- package/dist/resources/extensions/remote-questions/status.js +2 -1
- package/dist/resources/extensions/remote-questions/store.js +2 -1
- package/dist/resources/extensions/search-the-web/provider.js +2 -1
- package/dist/resources/extensions/subagent/index.js +12 -3
- package/dist/resources/extensions/subagent/isolation.js +2 -1
- package/dist/resources/extensions/ttsr/rule-loader.js +2 -1
- package/dist/resources/extensions/universal-config/package.json +1 -1
- package/dist/welcome-screen.d.ts +12 -0
- package/dist/welcome-screen.js +53 -0
- package/package.json +2 -1
- package/packages/pi-ai/dist/env-api-keys.js +13 -0
- package/packages/pi-ai/dist/env-api-keys.js.map +1 -1
- package/packages/pi-ai/dist/models.generated.d.ts +172 -0
- package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
- package/packages/pi-ai/dist/models.generated.js +172 -0
- package/packages/pi-ai/dist/models.generated.js.map +1 -1
- package/packages/pi-ai/dist/providers/anthropic-shared.d.ts +64 -0
- package/packages/pi-ai/dist/providers/anthropic-shared.d.ts.map +1 -0
- package/packages/pi-ai/dist/providers/anthropic-shared.js +668 -0
- package/packages/pi-ai/dist/providers/anthropic-shared.js.map +1 -0
- package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts +5 -0
- package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts.map +1 -0
- package/packages/pi-ai/dist/providers/anthropic-vertex.js +85 -0
- package/packages/pi-ai/dist/providers/anthropic-vertex.js.map +1 -0
- package/packages/pi-ai/dist/providers/anthropic.d.ts +4 -30
- package/packages/pi-ai/dist/providers/anthropic.d.ts.map +1 -1
- package/packages/pi-ai/dist/providers/anthropic.js +47 -764
- package/packages/pi-ai/dist/providers/anthropic.js.map +1 -1
- package/packages/pi-ai/dist/providers/register-builtins.d.ts.map +1 -1
- package/packages/pi-ai/dist/providers/register-builtins.js +6 -0
- package/packages/pi-ai/dist/providers/register-builtins.js.map +1 -1
- package/packages/pi-ai/dist/types.d.ts +2 -2
- package/packages/pi-ai/dist/types.d.ts.map +1 -1
- package/packages/pi-ai/dist/types.js.map +1 -1
- package/packages/pi-ai/dist/utils/oauth/anthropic.js +2 -2
- package/packages/pi-ai/dist/utils/oauth/anthropic.js.map +1 -1
- package/packages/pi-ai/package.json +1 -0
- package/packages/pi-ai/src/env-api-keys.ts +14 -0
- package/packages/pi-ai/src/models.generated.ts +172 -0
- package/packages/pi-ai/src/providers/anthropic-shared.ts +761 -0
- package/packages/pi-ai/src/providers/anthropic-vertex.ts +130 -0
- package/packages/pi-ai/src/providers/anthropic.ts +76 -868
- package/packages/pi-ai/src/providers/register-builtins.ts +7 -0
- package/packages/pi-ai/src/types.ts +2 -0
- package/packages/pi-ai/src/utils/oauth/anthropic.ts +2 -2
- package/packages/pi-coding-agent/dist/core/extensions/loader.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/extensions/loader.js +205 -7
- package/packages/pi-coding-agent/dist/core/extensions/loader.js.map +1 -1
- package/packages/pi-coding-agent/dist/core/model-resolver.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/model-resolver.js +1 -0
- package/packages/pi-coding-agent/dist/core/model-resolver.js.map +1 -1
- package/packages/pi-coding-agent/dist/core/package-manager.d.ts.map +1 -1
- package/packages/pi-coding-agent/dist/core/package-manager.js +8 -4
- package/packages/pi-coding-agent/dist/core/package-manager.js.map +1 -1
- package/packages/pi-coding-agent/package.json +1 -1
- package/packages/pi-coding-agent/src/core/extensions/loader.ts +223 -7
- package/packages/pi-coding-agent/src/core/model-resolver.ts +1 -0
- package/packages/pi-coding-agent/src/core/package-manager.ts +8 -4
- package/pkg/package.json +1 -1
- package/src/resources/extensions/cmux/index.ts +57 -1
- package/src/resources/extensions/env-utils.ts +31 -0
- package/src/resources/extensions/get-secrets-from-user.ts +5 -24
- package/src/resources/extensions/github-sync/cli.ts +364 -0
- package/src/resources/extensions/github-sync/index.ts +93 -0
- package/src/resources/extensions/github-sync/mapping.ts +81 -0
- package/src/resources/extensions/github-sync/sync.ts +556 -0
- package/src/resources/extensions/github-sync/templates.ts +183 -0
- package/src/resources/extensions/github-sync/tests/cli.test.ts +20 -0
- package/src/resources/extensions/github-sync/tests/commit-linking.test.ts +39 -0
- package/src/resources/extensions/github-sync/tests/mapping.test.ts +104 -0
- package/src/resources/extensions/github-sync/tests/templates.test.ts +110 -0
- package/src/resources/extensions/github-sync/types.ts +47 -0
- package/src/resources/extensions/gsd/auto/session.ts +7 -25
- package/src/resources/extensions/gsd/auto-dispatch.ts +100 -9
- package/src/resources/extensions/gsd/auto-loop.ts +484 -546
- package/src/resources/extensions/gsd/auto-post-unit.ts +92 -42
- package/src/resources/extensions/gsd/auto-prompts.ts +150 -48
- package/src/resources/extensions/gsd/auto-recovery.ts +42 -0
- package/src/resources/extensions/gsd/auto-start.ts +18 -2
- package/src/resources/extensions/gsd/auto-worktree-sync.ts +15 -4
- package/src/resources/extensions/gsd/auto-worktree.ts +3 -3
- package/src/resources/extensions/gsd/auto.ts +139 -101
- package/src/resources/extensions/gsd/captures.ts +10 -1
- package/src/resources/extensions/gsd/commands-extensions.ts +4 -2
- package/src/resources/extensions/gsd/commands-handlers.ts +17 -2
- package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
- package/src/resources/extensions/gsd/commands.ts +26 -4
- package/src/resources/extensions/gsd/context-budget.ts +2 -12
- package/src/resources/extensions/gsd/detection.ts +2 -2
- package/src/resources/extensions/gsd/docs/preferences-reference.md +0 -2
- package/src/resources/extensions/gsd/doctor-checks.ts +75 -0
- package/src/resources/extensions/gsd/doctor-environment.ts +82 -1
- package/src/resources/extensions/gsd/doctor-format.ts +20 -0
- package/src/resources/extensions/gsd/doctor-providers.ts +64 -10
- package/src/resources/extensions/gsd/doctor-types.ts +16 -1
- package/src/resources/extensions/gsd/doctor.ts +199 -14
- package/src/resources/extensions/gsd/exit-command.ts +2 -2
- package/src/resources/extensions/gsd/export.ts +1 -1
- package/src/resources/extensions/gsd/files.ts +50 -3
- package/src/resources/extensions/gsd/forensics.ts +1 -1
- package/src/resources/extensions/gsd/git-service.ts +20 -10
- package/src/resources/extensions/gsd/guided-flow.ts +110 -38
- package/src/resources/extensions/gsd/index.ts +24 -17
- package/src/resources/extensions/gsd/migrate/parsers.ts +1 -1
- package/src/resources/extensions/gsd/native-git-bridge.ts +37 -0
- package/src/resources/extensions/gsd/observability-validator.ts +27 -0
- package/src/resources/extensions/gsd/preferences-models.ts +0 -12
- package/src/resources/extensions/gsd/preferences-types.ts +9 -5
- package/src/resources/extensions/gsd/preferences-validation.ts +92 -11
- package/src/resources/extensions/gsd/preferences.ts +8 -5
- package/src/resources/extensions/gsd/prompts/discuss.md +11 -14
- package/src/resources/extensions/gsd/prompts/execute-task.md +2 -2
- package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
- package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
- package/src/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
- package/src/resources/extensions/gsd/prompts/plan-slice.md +2 -1
- package/src/resources/extensions/gsd/prompts/queue.md +4 -8
- package/src/resources/extensions/gsd/prompts/reactive-execute.md +44 -0
- package/src/resources/extensions/gsd/prompts/run-uat.md +27 -10
- package/src/resources/extensions/gsd/prompts/workflow-start.md +2 -2
- package/src/resources/extensions/gsd/reactive-graph.ts +289 -0
- package/src/resources/extensions/gsd/repo-identity.ts +23 -4
- package/src/resources/extensions/gsd/resource-version.ts +3 -1
- package/src/resources/extensions/gsd/roadmap-mutations.ts +29 -0
- package/src/resources/extensions/gsd/state.ts +1 -1
- package/src/resources/extensions/gsd/templates/task-plan.md +11 -3
- package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +21 -18
- package/src/resources/extensions/gsd/tests/auto-loop.test.ts +122 -68
- package/src/resources/extensions/gsd/tests/cmux.test.ts +93 -0
- package/src/resources/extensions/gsd/tests/doctor-enhancements.test.ts +266 -0
- package/src/resources/extensions/gsd/tests/doctor-providers.test.ts +191 -3
- package/src/resources/extensions/gsd/tests/plan-quality-validator.test.ts +111 -0
- package/src/resources/extensions/gsd/tests/preferences.test.ts +2 -7
- package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +59 -0
- package/src/resources/extensions/gsd/tests/reactive-executor.test.ts +511 -0
- package/src/resources/extensions/gsd/tests/reactive-graph.test.ts +299 -0
- package/src/resources/extensions/gsd/tests/repo-identity-worktree.test.ts +21 -1
- package/src/resources/extensions/gsd/tests/run-uat.test.ts +11 -3
- package/src/resources/extensions/gsd/tests/worktree.test.ts +47 -0
- package/src/resources/extensions/gsd/types.ts +43 -1
- package/src/resources/extensions/gsd/visualizer-data.ts +1 -1
- package/src/resources/extensions/gsd/worktree.ts +35 -15
- package/src/resources/extensions/mcp-client/index.ts +17 -1
- package/src/resources/extensions/remote-questions/status.ts +3 -1
- package/src/resources/extensions/remote-questions/store.ts +3 -1
- package/src/resources/extensions/search-the-web/provider.ts +2 -1
- package/src/resources/extensions/subagent/index.ts +12 -3
- package/src/resources/extensions/subagent/isolation.ts +3 -1
- package/src/resources/extensions/ttsr/rule-loader.ts +3 -1
- package/dist/resources/extensions/gsd/prompt-compressor.js +0 -393
- package/dist/resources/extensions/gsd/semantic-chunker.js +0 -254
- package/dist/resources/extensions/gsd/summary-distiller.js +0 -212
- package/src/resources/extensions/gsd/prompt-compressor.ts +0 -508
- package/src/resources/extensions/gsd/semantic-chunker.ts +0 -336
- package/src/resources/extensions/gsd/summary-distiller.ts +0 -258
- package/src/resources/extensions/gsd/tests/context-compression.test.ts +0 -193
- package/src/resources/extensions/gsd/tests/prompt-compressor.test.ts +0 -529
- package/src/resources/extensions/gsd/tests/semantic-chunker.test.ts +0 -426
- package/src/resources/extensions/gsd/tests/summary-distiller.test.ts +0 -323
- package/src/resources/extensions/gsd/tests/token-optimization-benchmark.test.ts +0 -1272
- package/src/resources/extensions/gsd/tests/token-optimization-prefs.test.ts +0 -164
|
@@ -5,12 +5,16 @@
|
|
|
5
5
|
* pattern with a while loop. The agent_end event resolves a promise instead
|
|
6
6
|
* of recursing.
|
|
7
7
|
*
|
|
8
|
-
* MAINTENANCE RULE:
|
|
9
|
-
*
|
|
10
|
-
*
|
|
8
|
+
* MAINTENANCE RULE: Module-level mutable state is limited to `_currentResolve`
|
|
9
|
+
* (per-unit one-shot resolver) and `_sessionSwitchInFlight` (guard for
|
|
10
|
+
* session rotation). No queue — stale agent_end events are dropped.
|
|
11
11
|
*/
|
|
12
|
+
import { importExtensionModule } from "@gsd/pi-coding-agent";
|
|
12
13
|
import { NEW_SESSION_TIMEOUT_MS } from "./auto/session.js";
|
|
13
14
|
import { debugLog } from "./debug-logger.js";
|
|
15
|
+
import { gsdRoot } from "./paths.js";
|
|
16
|
+
import { atomicWriteSync } from "./atomic-write.js";
|
|
17
|
+
import { join } from "node:path";
|
|
14
18
|
/**
|
|
15
19
|
* Maximum total loop iterations before forced stop. Prevents runaway loops
|
|
16
20
|
* when units alternate IDs (bypassing the same-unit stuck detector).
|
|
@@ -18,71 +22,114 @@ import { debugLog } from "./debug-logger.js";
|
|
|
18
22
|
* generous headroom including retries and sidecar work.
|
|
19
23
|
*/
|
|
20
24
|
const MAX_LOOP_ITERATIONS = 500;
|
|
21
|
-
|
|
25
|
+
/** Maximum characters of failure/crash context included in recovery prompts. */
|
|
26
|
+
const MAX_RECOVERY_CHARS = 50_000;
|
|
27
|
+
/** Data-driven budget threshold notifications (descending). The 100% entry
|
|
28
|
+
* triggers special enforcement logic (halt/pause/warn); sub-100 entries fire
|
|
29
|
+
* a simple notification. */
|
|
30
|
+
const BUDGET_THRESHOLDS = [
|
|
31
|
+
{ pct: 100, label: "Budget ceiling reached", notifyLevel: "error", cmuxLevel: "error" },
|
|
32
|
+
{ pct: 90, label: "Budget 90%", notifyLevel: "warning", cmuxLevel: "warning" },
|
|
33
|
+
{ pct: 80, label: "Approaching budget ceiling — 80%", notifyLevel: "warning", cmuxLevel: "warning" },
|
|
34
|
+
{ pct: 75, label: "Budget 75%", notifyLevel: "info", cmuxLevel: "progress" },
|
|
35
|
+
];
|
|
36
|
+
// ─── Per-unit one-shot promise state ────────────────────────────────────────
|
|
22
37
|
//
|
|
23
|
-
//
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*/
|
|
30
|
-
let _activeSession = null;
|
|
38
|
+
// A single module-level resolve function scoped to the current unit execution.
|
|
39
|
+
// No queue — if an agent_end arrives with no pending resolver, it is dropped
|
|
40
|
+
// (logged as warning). This is simpler and safer than the previous session-
|
|
41
|
+
// scoped pendingResolve + pendingAgentEndQueue pattern.
|
|
42
|
+
let _currentResolve = null;
|
|
43
|
+
let _sessionSwitchInFlight = false;
|
|
31
44
|
// ─── resolveAgentEnd ─────────────────────────────────────────────────────────
|
|
32
45
|
/**
|
|
33
46
|
* Called from the agent_end event handler in index.ts to resolve the
|
|
34
47
|
* in-flight unit promise. One-shot: the resolver is nulled before calling
|
|
35
48
|
* to prevent double-resolution from model fallback retries.
|
|
36
49
|
*
|
|
37
|
-
* If no
|
|
38
|
-
* the event is
|
|
50
|
+
* If no resolver exists (event arrived between loop iterations or during
|
|
51
|
+
* session switch), the event is dropped with a debug warning.
|
|
39
52
|
*/
|
|
40
53
|
export function resolveAgentEnd(event) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
debugLog("resolveAgentEnd", {
|
|
44
|
-
status: "no-active-session",
|
|
45
|
-
warning: "agent_end with no active loop session",
|
|
46
|
-
});
|
|
54
|
+
if (_sessionSwitchInFlight) {
|
|
55
|
+
debugLog("resolveAgentEnd", { status: "ignored-during-switch" });
|
|
47
56
|
return;
|
|
48
57
|
}
|
|
49
|
-
if (
|
|
58
|
+
if (_currentResolve) {
|
|
50
59
|
debugLog("resolveAgentEnd", { status: "resolving", hasEvent: true });
|
|
51
|
-
const r =
|
|
52
|
-
|
|
60
|
+
const r = _currentResolve;
|
|
61
|
+
_currentResolve = null;
|
|
53
62
|
r({ status: "completed", event });
|
|
54
63
|
}
|
|
55
64
|
else {
|
|
56
|
-
// Queue the event so the next runUnit picks it up immediately
|
|
57
65
|
debugLog("resolveAgentEnd", {
|
|
58
|
-
status: "
|
|
59
|
-
|
|
60
|
-
warning: "agent_end arrived between loop iterations — queued for next runUnit",
|
|
66
|
+
status: "no-pending-resolve",
|
|
67
|
+
warning: "agent_end with no pending unit",
|
|
61
68
|
});
|
|
62
|
-
s.pendingAgentEndQueue.push(event);
|
|
63
69
|
}
|
|
64
70
|
}
|
|
65
71
|
export function isSessionSwitchInFlight() {
|
|
66
|
-
return
|
|
72
|
+
return _sessionSwitchInFlight;
|
|
67
73
|
}
|
|
68
74
|
// ─── resetPendingResolve (test helper) ───────────────────────────────────────
|
|
69
75
|
/**
|
|
70
|
-
* Reset
|
|
71
|
-
* should never call this.
|
|
76
|
+
* Reset module-level promise state. Only exported for test cleanup —
|
|
77
|
+
* production code should never call this.
|
|
72
78
|
*/
|
|
73
79
|
export function _resetPendingResolve() {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
_activeSession.pendingAgentEndQueue = [];
|
|
77
|
-
}
|
|
78
|
-
_activeSession = null;
|
|
80
|
+
_currentResolve = null;
|
|
81
|
+
_sessionSwitchInFlight = false;
|
|
79
82
|
}
|
|
80
83
|
/**
|
|
81
|
-
*
|
|
82
|
-
*
|
|
84
|
+
* No-op for backward compatibility with tests that previously set the
|
|
85
|
+
* active session. The module no longer holds a session reference.
|
|
83
86
|
*/
|
|
84
|
-
export function _setActiveSession(
|
|
85
|
-
|
|
87
|
+
export function _setActiveSession(_session) {
|
|
88
|
+
// No-op — kept for test backward compatibility
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Analyze a sliding window of recent unit dispatches for stuck patterns.
|
|
92
|
+
* Returns a signal with reason if stuck, null otherwise.
|
|
93
|
+
*
|
|
94
|
+
* Rule 1: Same error string twice in a row → stuck immediately.
|
|
95
|
+
* Rule 2: Same unit key 3+ consecutive times → stuck (preserves prior behavior).
|
|
96
|
+
* Rule 3: Oscillation A→B→A→B in last 4 entries → stuck.
|
|
97
|
+
*/
|
|
98
|
+
export function detectStuck(window) {
|
|
99
|
+
if (window.length < 2)
|
|
100
|
+
return null;
|
|
101
|
+
const last = window[window.length - 1];
|
|
102
|
+
const prev = window[window.length - 2];
|
|
103
|
+
// Rule 1: Same error repeated consecutively
|
|
104
|
+
if (last.error && prev.error && last.error === prev.error) {
|
|
105
|
+
return {
|
|
106
|
+
stuck: true,
|
|
107
|
+
reason: `Same error repeated: ${last.error.slice(0, 200)}`,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
// Rule 2: Same unit 3+ consecutive times
|
|
111
|
+
if (window.length >= 3) {
|
|
112
|
+
const lastThree = window.slice(-3);
|
|
113
|
+
if (lastThree.every((u) => u.key === last.key)) {
|
|
114
|
+
return {
|
|
115
|
+
stuck: true,
|
|
116
|
+
reason: `${last.key} derived 3 consecutive times without progress`,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Rule 3: Oscillation (A→B→A→B in last 4)
|
|
121
|
+
if (window.length >= 4) {
|
|
122
|
+
const w = window.slice(-4);
|
|
123
|
+
if (w[0].key === w[2].key &&
|
|
124
|
+
w[1].key === w[3].key &&
|
|
125
|
+
w[0].key !== w[1].key) {
|
|
126
|
+
return {
|
|
127
|
+
stuck: true,
|
|
128
|
+
reason: `Oscillation detected: ${w[0].key} ↔ ${w[1].key}`,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return null;
|
|
86
133
|
}
|
|
87
134
|
// ─── runUnit ─────────────────────────────────────────────────────────────────
|
|
88
135
|
/**
|
|
@@ -93,41 +140,16 @@ export function _setActiveSession(session) {
|
|
|
93
140
|
* On session creation failure or timeout, returns { status: 'cancelled' }
|
|
94
141
|
* without awaiting the promise.
|
|
95
142
|
*/
|
|
96
|
-
export async function runUnit(ctx, pi, s, unitType, unitId, prompt
|
|
143
|
+
export async function runUnit(ctx, pi, s, unitType, unitId, prompt) {
|
|
97
144
|
debugLog("runUnit", { phase: "start", unitType, unitId });
|
|
98
|
-
// ── Drain queued events from error-recovery retries ──
|
|
99
|
-
// If an agent_end arrived between iterations (e.g. from a model fallback
|
|
100
|
-
// sendMessage retry), consume it immediately instead of creating a new promise.
|
|
101
|
-
// Cap queue to 3 entries to prevent unbounded growth from stale events.
|
|
102
|
-
if (s.pendingAgentEndQueue.length > 3) {
|
|
103
|
-
debugLog("runUnit", {
|
|
104
|
-
phase: "queue-overflow",
|
|
105
|
-
dropped: s.pendingAgentEndQueue.length - 1,
|
|
106
|
-
unitType,
|
|
107
|
-
unitId,
|
|
108
|
-
});
|
|
109
|
-
s.pendingAgentEndQueue = [
|
|
110
|
-
s.pendingAgentEndQueue[s.pendingAgentEndQueue.length - 1],
|
|
111
|
-
];
|
|
112
|
-
}
|
|
113
|
-
if (s.pendingAgentEndQueue.length > 0) {
|
|
114
|
-
const queued = s.pendingAgentEndQueue.shift();
|
|
115
|
-
debugLog("runUnit", {
|
|
116
|
-
phase: "drained-queued-event",
|
|
117
|
-
unitType,
|
|
118
|
-
unitId,
|
|
119
|
-
queueRemaining: s.pendingAgentEndQueue.length,
|
|
120
|
-
});
|
|
121
|
-
return { status: "completed", event: queued };
|
|
122
|
-
}
|
|
123
145
|
// ── Session creation with timeout ──
|
|
124
146
|
debugLog("runUnit", { phase: "session-create", unitType, unitId });
|
|
125
147
|
let sessionResult;
|
|
126
148
|
let sessionTimeoutHandle;
|
|
127
|
-
|
|
149
|
+
_sessionSwitchInFlight = true;
|
|
128
150
|
try {
|
|
129
151
|
const sessionPromise = s.cmdCtx.newSession().finally(() => {
|
|
130
|
-
|
|
152
|
+
_sessionSwitchInFlight = false;
|
|
131
153
|
});
|
|
132
154
|
const timeoutPromise = new Promise((resolve) => {
|
|
133
155
|
sessionTimeoutHandle = setTimeout(() => resolve({ cancelled: true }), NEW_SESSION_TIMEOUT_MS);
|
|
@@ -155,11 +177,12 @@ export async function runUnit(ctx, pi, s, unitType, unitId, prompt, _prefs) {
|
|
|
155
177
|
if (!s.active) {
|
|
156
178
|
return { status: "cancelled" };
|
|
157
179
|
}
|
|
158
|
-
// ── Create the agent_end promise (
|
|
180
|
+
// ── Create the agent_end promise (per-unit one-shot) ──
|
|
159
181
|
// This happens after newSession completes so session-switch agent_end events
|
|
160
182
|
// from the previous session cannot resolve the new unit.
|
|
183
|
+
_sessionSwitchInFlight = false;
|
|
161
184
|
const unitPromise = new Promise((resolve) => {
|
|
162
|
-
|
|
185
|
+
_currentResolve = resolve;
|
|
163
186
|
});
|
|
164
187
|
// Ensure cwd matches basePath before dispatch (#1389).
|
|
165
188
|
// async_bash and background jobs can drift cwd away from the worktree.
|
|
@@ -184,6 +207,60 @@ export async function runUnit(ctx, pi, s, unitType, unitId, prompt, _prefs) {
|
|
|
184
207
|
});
|
|
185
208
|
return result;
|
|
186
209
|
}
|
|
210
|
+
// ─── generateMilestoneReport ──────────────────────────────────────────────────
|
|
211
|
+
/**
|
|
212
|
+
* Generate and write an HTML milestone report snapshot.
|
|
213
|
+
* Extracted from the milestone-transition block in autoLoop.
|
|
214
|
+
*/
|
|
215
|
+
async function generateMilestoneReport(s, ctx, milestoneId) {
|
|
216
|
+
const { loadVisualizerData } = await importExtensionModule(import.meta.url, "./visualizer-data.js");
|
|
217
|
+
const { generateHtmlReport } = await importExtensionModule(import.meta.url, "./export-html.js");
|
|
218
|
+
const { writeReportSnapshot } = await importExtensionModule(import.meta.url, "./reports.js");
|
|
219
|
+
const { basename } = await import("node:path");
|
|
220
|
+
const snapData = await loadVisualizerData(s.basePath);
|
|
221
|
+
const completedMs = snapData.milestones.find((m) => m.id === milestoneId);
|
|
222
|
+
const msTitle = completedMs?.title ?? milestoneId;
|
|
223
|
+
const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
|
|
224
|
+
const projName = basename(s.basePath);
|
|
225
|
+
const doneSlices = snapData.milestones.reduce((acc, m) => acc + m.slices.filter((sl) => sl.done).length, 0);
|
|
226
|
+
const totalSlices = snapData.milestones.reduce((acc, m) => acc + m.slices.length, 0);
|
|
227
|
+
const outPath = writeReportSnapshot({
|
|
228
|
+
basePath: s.basePath,
|
|
229
|
+
html: generateHtmlReport(snapData, {
|
|
230
|
+
projectName: projName,
|
|
231
|
+
projectPath: s.basePath,
|
|
232
|
+
gsdVersion,
|
|
233
|
+
milestoneId,
|
|
234
|
+
indexRelPath: "index.html",
|
|
235
|
+
}),
|
|
236
|
+
milestoneId,
|
|
237
|
+
milestoneTitle: msTitle,
|
|
238
|
+
kind: "milestone",
|
|
239
|
+
projectName: projName,
|
|
240
|
+
projectPath: s.basePath,
|
|
241
|
+
gsdVersion,
|
|
242
|
+
totalCost: snapData.totals?.cost ?? 0,
|
|
243
|
+
totalTokens: snapData.totals?.tokens.total ?? 0,
|
|
244
|
+
totalDuration: snapData.totals?.duration ?? 0,
|
|
245
|
+
doneSlices,
|
|
246
|
+
totalSlices,
|
|
247
|
+
doneMilestones: snapData.milestones.filter((m) => m.status === "complete").length,
|
|
248
|
+
totalMilestones: snapData.milestones.length,
|
|
249
|
+
phase: snapData.phase,
|
|
250
|
+
});
|
|
251
|
+
ctx.ui.notify(`Report saved: .gsd/reports/${basename(outPath)} — open index.html to browse progression.`, "info");
|
|
252
|
+
}
|
|
253
|
+
// ─── closeoutAndStop ──────────────────────────────────────────────────────────
|
|
254
|
+
/**
|
|
255
|
+
* If a unit is in-flight, close it out, then stop auto-mode.
|
|
256
|
+
* Extracted from ~4 identical if-closeout-then-stop sequences in autoLoop.
|
|
257
|
+
*/
|
|
258
|
+
async function closeoutAndStop(ctx, pi, s, deps, reason) {
|
|
259
|
+
if (s.currentUnit) {
|
|
260
|
+
await deps.closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
|
|
261
|
+
}
|
|
262
|
+
await deps.stopAuto(ctx, pi, reason);
|
|
263
|
+
}
|
|
187
264
|
// ─── autoLoop ────────────────────────────────────────────────────────────────
|
|
188
265
|
/**
|
|
189
266
|
* Main auto-mode execution loop. Iterates: derive → dispatch → guards →
|
|
@@ -195,10 +272,11 @@ export async function runUnit(ctx, pi, s, unitType, unitId, prompt, _prefs) {
|
|
|
195
272
|
*/
|
|
196
273
|
export async function autoLoop(ctx, pi, s, deps) {
|
|
197
274
|
debugLog("autoLoop", { phase: "enter" });
|
|
198
|
-
_activeSession = s;
|
|
199
275
|
let iteration = 0;
|
|
200
|
-
|
|
201
|
-
|
|
276
|
+
// ── Sliding-window stuck detection ──
|
|
277
|
+
const recentUnits = [];
|
|
278
|
+
const STUCK_WINDOW_SIZE = 6;
|
|
279
|
+
let stuckRecoveryAttempts = 0;
|
|
202
280
|
let consecutiveErrors = 0;
|
|
203
281
|
while (s.active) {
|
|
204
282
|
iteration++;
|
|
@@ -218,6 +296,18 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
218
296
|
}
|
|
219
297
|
try {
|
|
220
298
|
// ── Blanket try/catch: one bad iteration must not kill the session
|
|
299
|
+
const prefs = deps.loadEffectiveGSDPreferences()?.preferences;
|
|
300
|
+
// ── Check sidecar queue before deriveState ──
|
|
301
|
+
let sidecarItem;
|
|
302
|
+
if (s.sidecarQueue.length > 0) {
|
|
303
|
+
sidecarItem = s.sidecarQueue.shift();
|
|
304
|
+
debugLog("autoLoop", {
|
|
305
|
+
phase: "sidecar-dequeue",
|
|
306
|
+
kind: sidecarItem.kind,
|
|
307
|
+
unitType: sidecarItem.unitType,
|
|
308
|
+
unitId: sidecarItem.unitId,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
221
311
|
const sessionLockBase = deps.lockBase();
|
|
222
312
|
if (sessionLockBase) {
|
|
223
313
|
const lockStatus = deps.validateSessionLock(sessionLockBase);
|
|
@@ -237,419 +327,397 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
237
327
|
break;
|
|
238
328
|
}
|
|
239
329
|
}
|
|
240
|
-
//
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
}
|
|
257
|
-
if (!healthGate.proceed) {
|
|
258
|
-
ctx.ui.notify(healthGate.reason ?? "Pre-dispatch health check failed.", "error");
|
|
259
|
-
await deps.pauseAuto(ctx, pi);
|
|
260
|
-
debugLog("autoLoop", { phase: "exit", reason: "health-gate-failed" });
|
|
330
|
+
// Variables shared between the sidecar and normal paths
|
|
331
|
+
let unitType;
|
|
332
|
+
let unitId;
|
|
333
|
+
let prompt;
|
|
334
|
+
let pauseAfterUatDispatch = false;
|
|
335
|
+
let state;
|
|
336
|
+
let mid;
|
|
337
|
+
let midTitle;
|
|
338
|
+
let observabilityIssues = [];
|
|
339
|
+
if (!sidecarItem) {
|
|
340
|
+
// ── Phase 1: Pre-dispatch ───────────────────────────────────────────
|
|
341
|
+
// Resource version guard
|
|
342
|
+
const staleMsg = deps.checkResourcesStale(s.resourceVersionOnStart);
|
|
343
|
+
if (staleMsg) {
|
|
344
|
+
await deps.stopAuto(ctx, pi, staleMsg);
|
|
345
|
+
debugLog("autoLoop", { phase: "exit", reason: "resources-stale" });
|
|
261
346
|
break;
|
|
262
347
|
}
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
deps.syncProjectRootToWorktree(s.originalBasePath, s.basePath, s.currentMilestoneId);
|
|
272
|
-
}
|
|
273
|
-
// Derive state
|
|
274
|
-
let state = await deps.deriveState(s.basePath);
|
|
275
|
-
deps.syncCmuxSidebar(deps.loadEffectiveGSDPreferences()?.preferences, state);
|
|
276
|
-
let mid = state.activeMilestone?.id;
|
|
277
|
-
let midTitle = state.activeMilestone?.title;
|
|
278
|
-
debugLog("autoLoop", {
|
|
279
|
-
phase: "state-derived",
|
|
280
|
-
iteration,
|
|
281
|
-
mid,
|
|
282
|
-
statePhase: state.phase,
|
|
283
|
-
});
|
|
284
|
-
// ── Milestone transition ────────────────────────────────────────────
|
|
285
|
-
if (mid && s.currentMilestoneId && mid !== s.currentMilestoneId) {
|
|
286
|
-
ctx.ui.notify(`Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}: ${midTitle}.`, "info");
|
|
287
|
-
deps.sendDesktopNotification("GSD", `Milestone ${s.currentMilestoneId} complete!`, "success", "milestone");
|
|
288
|
-
deps.logCmuxEvent(deps.loadEffectiveGSDPreferences()?.preferences, `Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}.`, "success");
|
|
289
|
-
const vizPrefs = deps.loadEffectiveGSDPreferences()?.preferences;
|
|
290
|
-
if (vizPrefs?.auto_visualize) {
|
|
291
|
-
ctx.ui.notify("Run /gsd visualize to see progress overview.", "info");
|
|
292
|
-
}
|
|
293
|
-
if (vizPrefs?.auto_report !== false) {
|
|
294
|
-
try {
|
|
295
|
-
const { loadVisualizerData } = await import("./visualizer-data.js");
|
|
296
|
-
const { generateHtmlReport } = await import("./export-html.js");
|
|
297
|
-
const { writeReportSnapshot } = await import("./reports.js");
|
|
298
|
-
const { basename } = await import("node:path");
|
|
299
|
-
const snapData = await loadVisualizerData(s.basePath);
|
|
300
|
-
const completedMs = snapData.milestones.find((m) => m.id === s.currentMilestoneId);
|
|
301
|
-
const msTitle = completedMs?.title ?? s.currentMilestoneId;
|
|
302
|
-
const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
|
|
303
|
-
const projName = basename(s.basePath);
|
|
304
|
-
const doneSlices = snapData.milestones.reduce((acc, m) => acc +
|
|
305
|
-
m.slices.filter((sl) => sl.done).length, 0);
|
|
306
|
-
const totalSlices = snapData.milestones.reduce((acc, m) => acc + m.slices.length, 0);
|
|
307
|
-
const outPath = writeReportSnapshot({
|
|
308
|
-
basePath: s.basePath,
|
|
309
|
-
html: generateHtmlReport(snapData, {
|
|
310
|
-
projectName: projName,
|
|
311
|
-
projectPath: s.basePath,
|
|
312
|
-
gsdVersion,
|
|
313
|
-
milestoneId: s.currentMilestoneId,
|
|
314
|
-
indexRelPath: "index.html",
|
|
315
|
-
}),
|
|
316
|
-
milestoneId: s.currentMilestoneId,
|
|
317
|
-
milestoneTitle: msTitle,
|
|
318
|
-
kind: "milestone",
|
|
319
|
-
projectName: projName,
|
|
320
|
-
projectPath: s.basePath,
|
|
321
|
-
gsdVersion,
|
|
322
|
-
totalCost: snapData.totals?.cost ?? 0,
|
|
323
|
-
totalTokens: snapData.totals?.tokens.total ?? 0,
|
|
324
|
-
totalDuration: snapData.totals?.duration ?? 0,
|
|
325
|
-
doneSlices,
|
|
326
|
-
totalSlices,
|
|
327
|
-
doneMilestones: snapData.milestones.filter((m) => m.status === "complete").length,
|
|
328
|
-
totalMilestones: snapData.milestones.length,
|
|
329
|
-
phase: snapData.phase,
|
|
330
|
-
});
|
|
331
|
-
ctx.ui.notify(`Report saved: .gsd/reports/${(await import("node:path")).basename(outPath)} — open index.html to browse progression.`, "info");
|
|
348
|
+
deps.invalidateAllCaches();
|
|
349
|
+
s.lastPromptCharCount = undefined;
|
|
350
|
+
s.lastBaselineCharCount = undefined;
|
|
351
|
+
// Pre-dispatch health gate
|
|
352
|
+
try {
|
|
353
|
+
const healthGate = await deps.preDispatchHealthGate(s.basePath);
|
|
354
|
+
if (healthGate.fixesApplied.length > 0) {
|
|
355
|
+
ctx.ui.notify(`Pre-dispatch: ${healthGate.fixesApplied.join(", ")}`, "info");
|
|
332
356
|
}
|
|
333
|
-
|
|
334
|
-
ctx.ui.notify(
|
|
357
|
+
if (!healthGate.proceed) {
|
|
358
|
+
ctx.ui.notify(healthGate.reason ?? "Pre-dispatch health check failed.", "error");
|
|
359
|
+
await deps.pauseAuto(ctx, pi);
|
|
360
|
+
debugLog("autoLoop", { phase: "exit", reason: "health-gate-failed" });
|
|
361
|
+
break;
|
|
335
362
|
}
|
|
336
363
|
}
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
364
|
+
catch {
|
|
365
|
+
// Non-fatal
|
|
366
|
+
}
|
|
367
|
+
// Sync project root artifacts into worktree
|
|
368
|
+
if (s.originalBasePath &&
|
|
369
|
+
s.basePath !== s.originalBasePath &&
|
|
370
|
+
s.currentMilestoneId) {
|
|
371
|
+
deps.syncProjectRootToWorktree(s.originalBasePath, s.basePath, s.currentMilestoneId);
|
|
372
|
+
}
|
|
373
|
+
// Derive state
|
|
346
374
|
state = await deps.deriveState(s.basePath);
|
|
375
|
+
deps.syncCmuxSidebar(prefs, state);
|
|
347
376
|
mid = state.activeMilestone?.id;
|
|
348
377
|
midTitle = state.activeMilestone?.title;
|
|
378
|
+
debugLog("autoLoop", {
|
|
379
|
+
phase: "state-derived",
|
|
380
|
+
iteration,
|
|
381
|
+
mid,
|
|
382
|
+
statePhase: state.phase,
|
|
383
|
+
});
|
|
384
|
+
// ── Milestone transition ────────────────────────────────────────────
|
|
385
|
+
if (mid && s.currentMilestoneId && mid !== s.currentMilestoneId) {
|
|
386
|
+
ctx.ui.notify(`Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}: ${midTitle}.`, "info");
|
|
387
|
+
deps.sendDesktopNotification("GSD", `Milestone ${s.currentMilestoneId} complete!`, "success", "milestone");
|
|
388
|
+
deps.logCmuxEvent(prefs, `Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}.`, "success");
|
|
389
|
+
const vizPrefs = prefs;
|
|
390
|
+
if (vizPrefs?.auto_visualize) {
|
|
391
|
+
ctx.ui.notify("Run /gsd visualize to see progress overview.", "info");
|
|
392
|
+
}
|
|
393
|
+
if (vizPrefs?.auto_report !== false) {
|
|
394
|
+
try {
|
|
395
|
+
await generateMilestoneReport(s, ctx, s.currentMilestoneId);
|
|
396
|
+
}
|
|
397
|
+
catch (err) {
|
|
398
|
+
ctx.ui.notify(`Report generation failed: ${err instanceof Error ? err.message : String(err)}`, "warning");
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
// Reset dispatch counters for new milestone
|
|
402
|
+
s.unitDispatchCount.clear();
|
|
403
|
+
s.unitRecoveryCount.clear();
|
|
404
|
+
s.unitLifetimeDispatches.clear();
|
|
405
|
+
recentUnits.length = 0;
|
|
406
|
+
stuckRecoveryAttempts = 0;
|
|
407
|
+
// Worktree lifecycle on milestone transition — merge current, enter next
|
|
408
|
+
deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
|
|
409
|
+
deps.invalidateAllCaches();
|
|
410
|
+
state = await deps.deriveState(s.basePath);
|
|
411
|
+
mid = state.activeMilestone?.id;
|
|
412
|
+
midTitle = state.activeMilestone?.title;
|
|
413
|
+
if (mid) {
|
|
414
|
+
if (deps.getIsolationMode() !== "none") {
|
|
415
|
+
deps.captureIntegrationBranch(s.basePath, mid, {
|
|
416
|
+
commitDocs: prefs?.git?.commit_docs,
|
|
417
|
+
});
|
|
418
|
+
}
|
|
419
|
+
deps.resolver.enterMilestone(mid, ctx.ui);
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
// mid is undefined — no milestone to capture integration branch for
|
|
423
|
+
}
|
|
424
|
+
const pendingIds = state.registry
|
|
425
|
+
.filter((m) => m.status !== "complete" && m.status !== "parked")
|
|
426
|
+
.map((m) => m.id);
|
|
427
|
+
deps.pruneQueueOrder(s.basePath, pendingIds);
|
|
428
|
+
}
|
|
349
429
|
if (mid) {
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
430
|
+
s.currentMilestoneId = mid;
|
|
431
|
+
deps.setActiveMilestoneId(s.basePath, mid);
|
|
432
|
+
}
|
|
433
|
+
// ── Terminal conditions ──────────────────────────────────────────────
|
|
434
|
+
if (!mid) {
|
|
435
|
+
if (s.currentUnit) {
|
|
436
|
+
await deps.closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
|
|
437
|
+
}
|
|
438
|
+
const incomplete = state.registry.filter((m) => m.status !== "complete" && m.status !== "parked");
|
|
439
|
+
if (incomplete.length === 0 && state.registry.length > 0) {
|
|
440
|
+
// All milestones complete — merge milestone branch before stopping
|
|
441
|
+
if (s.currentMilestoneId) {
|
|
442
|
+
deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
|
|
443
|
+
}
|
|
444
|
+
deps.sendDesktopNotification("GSD", "All milestones complete!", "success", "milestone");
|
|
445
|
+
deps.logCmuxEvent(prefs, "All milestones complete.", "success");
|
|
446
|
+
await deps.stopAuto(ctx, pi, "All milestones complete");
|
|
355
447
|
}
|
|
356
|
-
|
|
448
|
+
else if (incomplete.length === 0 && state.registry.length === 0) {
|
|
449
|
+
// Empty registry — no milestones visible, likely a path resolution bug
|
|
450
|
+
const diag = `basePath=${s.basePath}, phase=${state.phase}`;
|
|
451
|
+
ctx.ui.notify(`No milestones visible in current scope. Possible path resolution issue.\n Diagnostic: ${diag}`, "error");
|
|
452
|
+
await deps.stopAuto(ctx, pi, `No milestones found — check basePath resolution`);
|
|
453
|
+
}
|
|
454
|
+
else if (state.phase === "blocked") {
|
|
455
|
+
const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
|
|
456
|
+
await deps.stopAuto(ctx, pi, blockerMsg);
|
|
457
|
+
ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
|
|
458
|
+
deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
|
|
459
|
+
deps.logCmuxEvent(prefs, blockerMsg, "error");
|
|
460
|
+
}
|
|
461
|
+
else {
|
|
462
|
+
const ids = incomplete.map((m) => m.id).join(", ");
|
|
463
|
+
const diag = `basePath=${s.basePath}, milestones=[${state.registry.map((m) => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
|
|
464
|
+
ctx.ui.notify(`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`, "error");
|
|
465
|
+
await deps.stopAuto(ctx, pi, `No active milestone — ${incomplete.length} incomplete (${ids}), see diagnostic above`);
|
|
466
|
+
}
|
|
467
|
+
debugLog("autoLoop", { phase: "exit", reason: "no-active-milestone" });
|
|
468
|
+
break;
|
|
357
469
|
}
|
|
358
|
-
|
|
359
|
-
|
|
470
|
+
if (!midTitle) {
|
|
471
|
+
midTitle = mid;
|
|
472
|
+
ctx.ui.notify(`Milestone ${mid} has no title in roadmap — using ID as fallback.`, "warning");
|
|
360
473
|
}
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
.
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
474
|
+
// Mid-merge safety check
|
|
475
|
+
if (deps.reconcileMergeState(s.basePath, ctx)) {
|
|
476
|
+
deps.invalidateAllCaches();
|
|
477
|
+
state = await deps.deriveState(s.basePath);
|
|
478
|
+
mid = state.activeMilestone?.id;
|
|
479
|
+
midTitle = state.activeMilestone?.title;
|
|
480
|
+
}
|
|
481
|
+
if (!mid || !midTitle) {
|
|
482
|
+
const noMilestoneReason = !mid
|
|
483
|
+
? "No active milestone after merge reconciliation"
|
|
484
|
+
: `Milestone ${mid} has no title after reconciliation`;
|
|
485
|
+
await closeoutAndStop(ctx, pi, s, deps, noMilestoneReason);
|
|
486
|
+
debugLog("autoLoop", {
|
|
487
|
+
phase: "exit",
|
|
488
|
+
reason: "no-milestone-after-reconciliation",
|
|
489
|
+
});
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
// Terminal: complete
|
|
493
|
+
if (state.phase === "complete") {
|
|
494
|
+
// Milestone merge on complete (before closeout so branch state is clean)
|
|
378
495
|
if (s.currentMilestoneId) {
|
|
379
496
|
deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
|
|
380
497
|
}
|
|
381
|
-
deps.sendDesktopNotification("GSD",
|
|
382
|
-
deps.logCmuxEvent(
|
|
383
|
-
await
|
|
498
|
+
deps.sendDesktopNotification("GSD", `Milestone ${mid} complete!`, "success", "milestone");
|
|
499
|
+
deps.logCmuxEvent(prefs, `Milestone ${mid} complete.`, "success");
|
|
500
|
+
await closeoutAndStop(ctx, pi, s, deps, `Milestone ${mid} complete`);
|
|
501
|
+
debugLog("autoLoop", { phase: "exit", reason: "milestone-complete" });
|
|
502
|
+
break;
|
|
384
503
|
}
|
|
385
|
-
|
|
504
|
+
// Terminal: blocked
|
|
505
|
+
if (state.phase === "blocked") {
|
|
386
506
|
const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
|
|
387
|
-
await
|
|
507
|
+
await closeoutAndStop(ctx, pi, s, deps, blockerMsg);
|
|
388
508
|
ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
|
|
389
509
|
deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
|
|
390
|
-
deps.logCmuxEvent(
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
const ids = incomplete.map((m) => m.id).join(", ");
|
|
394
|
-
const diag = `basePath=${s.basePath}, milestones=[${state.registry.map((m) => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
|
|
395
|
-
ctx.ui.notify(`Unexpected: ${incomplete.length} incomplete milestone(s) (${ids}) but no active milestone.\n Diagnostic: ${diag}`, "error");
|
|
396
|
-
await deps.stopAuto(ctx, pi, `No active milestone — ${incomplete.length} incomplete (${ids}), see diagnostic above`);
|
|
510
|
+
deps.logCmuxEvent(prefs, blockerMsg, "error");
|
|
511
|
+
debugLog("autoLoop", { phase: "exit", reason: "blocked" });
|
|
512
|
+
break;
|
|
397
513
|
}
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
514
|
+
// ── Phase 2: Guards ─────────────────────────────────────────────────
|
|
515
|
+
// Budget ceiling guard
|
|
516
|
+
const budgetCeiling = prefs?.budget_ceiling;
|
|
517
|
+
if (budgetCeiling !== undefined && budgetCeiling > 0) {
|
|
518
|
+
const currentLedger = deps.getLedger();
|
|
519
|
+
const totalCost = currentLedger
|
|
520
|
+
? deps.getProjectTotals(currentLedger.units).cost
|
|
521
|
+
: 0;
|
|
522
|
+
const budgetPct = totalCost / budgetCeiling;
|
|
523
|
+
const budgetAlertLevel = deps.getBudgetAlertLevel(budgetPct);
|
|
524
|
+
const newBudgetAlertLevel = deps.getNewBudgetAlertLevel(s.lastBudgetAlertLevel, budgetPct);
|
|
525
|
+
const enforcement = prefs?.budget_enforcement ?? "pause";
|
|
526
|
+
const budgetEnforcementAction = deps.getBudgetEnforcementAction(enforcement, budgetPct);
|
|
527
|
+
// Data-driven threshold check — loop descending, fire first match
|
|
528
|
+
const threshold = BUDGET_THRESHOLDS.find((t) => newBudgetAlertLevel >= t.pct);
|
|
529
|
+
if (threshold) {
|
|
530
|
+
s.lastBudgetAlertLevel =
|
|
531
|
+
newBudgetAlertLevel;
|
|
532
|
+
if (threshold.pct === 100 && budgetEnforcementAction !== "none") {
|
|
533
|
+
// 100% — special enforcement logic (halt/pause/warn)
|
|
534
|
+
const msg = `Budget ceiling ${deps.formatCost(budgetCeiling)} reached (spent ${deps.formatCost(totalCost)}).`;
|
|
535
|
+
if (budgetEnforcementAction === "halt") {
|
|
536
|
+
deps.sendDesktopNotification("GSD", msg, "error", "budget");
|
|
537
|
+
await deps.stopAuto(ctx, pi, "Budget ceiling reached");
|
|
538
|
+
debugLog("autoLoop", { phase: "exit", reason: "budget-halt" });
|
|
539
|
+
break;
|
|
540
|
+
}
|
|
541
|
+
if (budgetEnforcementAction === "pause") {
|
|
542
|
+
ctx.ui.notify(`${msg} Pausing auto-mode — /gsd auto to override and continue.`, "warning");
|
|
543
|
+
deps.sendDesktopNotification("GSD", msg, "warning", "budget");
|
|
544
|
+
deps.logCmuxEvent(prefs, msg, "warning");
|
|
545
|
+
await deps.pauseAuto(ctx, pi);
|
|
546
|
+
debugLog("autoLoop", { phase: "exit", reason: "budget-pause" });
|
|
547
|
+
break;
|
|
548
|
+
}
|
|
549
|
+
ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
|
|
550
|
+
deps.sendDesktopNotification("GSD", msg, "warning", "budget");
|
|
551
|
+
deps.logCmuxEvent(prefs, msg, "warning");
|
|
552
|
+
}
|
|
553
|
+
else if (threshold.pct < 100) {
|
|
554
|
+
// Sub-100% — simple notification
|
|
555
|
+
const msg = `${threshold.label}: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`;
|
|
556
|
+
ctx.ui.notify(msg, threshold.notifyLevel);
|
|
557
|
+
deps.sendDesktopNotification("GSD", msg, threshold.notifyLevel, "budget");
|
|
558
|
+
deps.logCmuxEvent(prefs, msg, threshold.cmuxLevel);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
else if (budgetAlertLevel === 0) {
|
|
562
|
+
s.lastBudgetAlertLevel = 0;
|
|
563
|
+
}
|
|
430
564
|
}
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
|
|
565
|
+
else {
|
|
566
|
+
s.lastBudgetAlertLevel = 0;
|
|
434
567
|
}
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
}
|
|
446
|
-
const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
|
|
447
|
-
await deps.stopAuto(ctx, pi, blockerMsg);
|
|
448
|
-
ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
|
|
449
|
-
deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
|
|
450
|
-
deps.logCmuxEvent(deps.loadEffectiveGSDPreferences()?.preferences, blockerMsg, "error");
|
|
451
|
-
debugLog("autoLoop", { phase: "exit", reason: "blocked" });
|
|
452
|
-
break;
|
|
453
|
-
}
|
|
454
|
-
// ── Phase 2: Guards ─────────────────────────────────────────────────
|
|
455
|
-
const prefs = deps.loadEffectiveGSDPreferences()?.preferences;
|
|
456
|
-
// Budget ceiling guard
|
|
457
|
-
const budgetCeiling = prefs?.budget_ceiling;
|
|
458
|
-
if (budgetCeiling !== undefined && budgetCeiling > 0) {
|
|
459
|
-
const currentLedger = deps.getLedger();
|
|
460
|
-
const totalCost = currentLedger
|
|
461
|
-
? deps.getProjectTotals(currentLedger.units).cost
|
|
462
|
-
: 0;
|
|
463
|
-
const budgetPct = totalCost / budgetCeiling;
|
|
464
|
-
const budgetAlertLevel = deps.getBudgetAlertLevel(budgetPct);
|
|
465
|
-
const newBudgetAlertLevel = deps.getNewBudgetAlertLevel(s.lastBudgetAlertLevel, budgetPct);
|
|
466
|
-
const enforcement = prefs?.budget_enforcement ?? "pause";
|
|
467
|
-
const budgetEnforcementAction = deps.getBudgetEnforcementAction(enforcement, budgetPct);
|
|
468
|
-
if (newBudgetAlertLevel === 100 && budgetEnforcementAction !== "none") {
|
|
469
|
-
const msg = `Budget ceiling ${deps.formatCost(budgetCeiling)} reached (spent ${deps.formatCost(totalCost)}).`;
|
|
470
|
-
s.lastBudgetAlertLevel =
|
|
471
|
-
newBudgetAlertLevel;
|
|
472
|
-
if (budgetEnforcementAction === "halt") {
|
|
473
|
-
deps.sendDesktopNotification("GSD", msg, "error", "budget");
|
|
474
|
-
await deps.stopAuto(ctx, pi, "Budget ceiling reached");
|
|
475
|
-
debugLog("autoLoop", { phase: "exit", reason: "budget-halt" });
|
|
476
|
-
break;
|
|
477
|
-
}
|
|
478
|
-
if (budgetEnforcementAction === "pause") {
|
|
479
|
-
ctx.ui.notify(`${msg} Pausing auto-mode — /gsd auto to override and continue.`, "warning");
|
|
480
|
-
deps.sendDesktopNotification("GSD", msg, "warning", "budget");
|
|
481
|
-
deps.logCmuxEvent(prefs, msg, "warning");
|
|
568
|
+
// Context window guard
|
|
569
|
+
const contextThreshold = prefs?.context_pause_threshold ?? 0;
|
|
570
|
+
if (contextThreshold > 0 && s.cmdCtx) {
|
|
571
|
+
const contextUsage = s.cmdCtx.getContextUsage();
|
|
572
|
+
if (contextUsage &&
|
|
573
|
+
contextUsage.percent !== null &&
|
|
574
|
+
contextUsage.percent >= contextThreshold) {
|
|
575
|
+
const msg = `Context window at ${contextUsage.percent}% (threshold: ${contextThreshold}%). Pausing to prevent truncated output.`;
|
|
576
|
+
ctx.ui.notify(`${msg} Run /gsd auto to continue (will start fresh session).`, "warning");
|
|
577
|
+
deps.sendDesktopNotification("GSD", `Context ${contextUsage.percent}% — paused`, "warning", "attention");
|
|
482
578
|
await deps.pauseAuto(ctx, pi);
|
|
483
|
-
debugLog("autoLoop", { phase: "exit", reason: "
|
|
579
|
+
debugLog("autoLoop", { phase: "exit", reason: "context-window" });
|
|
484
580
|
break;
|
|
485
581
|
}
|
|
486
|
-
ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
|
|
487
|
-
deps.sendDesktopNotification("GSD", msg, "warning", "budget");
|
|
488
|
-
deps.logCmuxEvent(prefs, msg, "warning");
|
|
489
|
-
}
|
|
490
|
-
else if (newBudgetAlertLevel === 90) {
|
|
491
|
-
s.lastBudgetAlertLevel =
|
|
492
|
-
newBudgetAlertLevel;
|
|
493
|
-
ctx.ui.notify(`Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "warning");
|
|
494
|
-
deps.sendDesktopNotification("GSD", `Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "warning", "budget");
|
|
495
|
-
deps.logCmuxEvent(prefs, `Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "warning");
|
|
496
|
-
}
|
|
497
|
-
else if (newBudgetAlertLevel === 80) {
|
|
498
|
-
s.lastBudgetAlertLevel =
|
|
499
|
-
newBudgetAlertLevel;
|
|
500
|
-
ctx.ui.notify(`Approaching budget ceiling — 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "warning");
|
|
501
|
-
deps.sendDesktopNotification("GSD", `Approaching budget ceiling — 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "warning", "budget");
|
|
502
|
-
deps.logCmuxEvent(prefs, `Budget 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "warning");
|
|
503
|
-
}
|
|
504
|
-
else if (newBudgetAlertLevel === 75) {
|
|
505
|
-
s.lastBudgetAlertLevel =
|
|
506
|
-
newBudgetAlertLevel;
|
|
507
|
-
ctx.ui.notify(`Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "info");
|
|
508
|
-
deps.sendDesktopNotification("GSD", `Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "info", "budget");
|
|
509
|
-
deps.logCmuxEvent(prefs, `Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`, "progress");
|
|
510
|
-
}
|
|
511
|
-
else if (budgetAlertLevel === 0) {
|
|
512
|
-
s.lastBudgetAlertLevel = 0;
|
|
513
|
-
}
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
s.lastBudgetAlertLevel = 0;
|
|
517
|
-
}
|
|
518
|
-
// Context window guard
|
|
519
|
-
const contextThreshold = prefs?.context_pause_threshold ?? 0;
|
|
520
|
-
if (contextThreshold > 0 && s.cmdCtx) {
|
|
521
|
-
const contextUsage = s.cmdCtx.getContextUsage();
|
|
522
|
-
if (contextUsage &&
|
|
523
|
-
contextUsage.percent !== null &&
|
|
524
|
-
contextUsage.percent >= contextThreshold) {
|
|
525
|
-
const msg = `Context window at ${contextUsage.percent}% (threshold: ${contextThreshold}%). Pausing to prevent truncated output.`;
|
|
526
|
-
ctx.ui.notify(`${msg} Run /gsd auto to continue (will start fresh session).`, "warning");
|
|
527
|
-
deps.sendDesktopNotification("GSD", `Context ${contextUsage.percent}% — paused`, "warning", "attention");
|
|
528
|
-
await deps.pauseAuto(ctx, pi);
|
|
529
|
-
debugLog("autoLoop", { phase: "exit", reason: "context-window" });
|
|
530
|
-
break;
|
|
531
582
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
583
|
+
// Secrets re-check gate
|
|
584
|
+
try {
|
|
585
|
+
const manifestStatus = await deps.getManifestStatus(s.basePath, mid, s.originalBasePath);
|
|
586
|
+
if (manifestStatus && manifestStatus.pending.length > 0) {
|
|
587
|
+
const result = await deps.collectSecretsFromManifest(s.basePath, mid, ctx);
|
|
588
|
+
if (result &&
|
|
589
|
+
result.applied &&
|
|
590
|
+
result.skipped &&
|
|
591
|
+
result.existingSkipped) {
|
|
592
|
+
ctx.ui.notify(`Secrets collected: ${result.applied.length} applied, ${result.skipped.length} skipped, ${result.existingSkipped.length} already set.`, "info");
|
|
593
|
+
}
|
|
594
|
+
else {
|
|
595
|
+
ctx.ui.notify("Secrets collection skipped.", "info");
|
|
596
|
+
}
|
|
546
597
|
}
|
|
547
598
|
}
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
ctx.ui.notify(`Secrets collection error: ${err instanceof Error ? err.message : String(err)}. Continuing with next task.`, "warning");
|
|
551
|
-
}
|
|
552
|
-
// ── Phase 3: Dispatch resolution ────────────────────────────────────
|
|
553
|
-
debugLog("autoLoop", { phase: "dispatch-resolve", iteration });
|
|
554
|
-
const dispatchResult = await deps.resolveDispatch({
|
|
555
|
-
basePath: s.basePath,
|
|
556
|
-
mid,
|
|
557
|
-
midTitle: midTitle,
|
|
558
|
-
state,
|
|
559
|
-
prefs,
|
|
560
|
-
});
|
|
561
|
-
if (dispatchResult.action === "stop") {
|
|
562
|
-
if (s.currentUnit) {
|
|
563
|
-
await deps.closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
|
|
599
|
+
catch (err) {
|
|
600
|
+
ctx.ui.notify(`Secrets collection error: ${err instanceof Error ? err.message : String(err)}. Continuing with next task.`, "warning");
|
|
564
601
|
}
|
|
565
|
-
|
|
566
|
-
debugLog("autoLoop", { phase: "
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
let unitType = dispatchResult.unitType;
|
|
575
|
-
let unitId = dispatchResult.unitId;
|
|
576
|
-
let prompt = dispatchResult.prompt;
|
|
577
|
-
const pauseAfterUatDispatch = dispatchResult.pauseAfterDispatch ?? false;
|
|
578
|
-
// ── Same-unit stuck counter with graduated recovery ──
|
|
579
|
-
const derivedKey = `${unitType}/${unitId}`;
|
|
580
|
-
if (derivedKey === lastDerivedUnit && !s.pendingVerificationRetry) {
|
|
581
|
-
sameUnitCount++;
|
|
582
|
-
debugLog("autoLoop", {
|
|
583
|
-
phase: "stuck-check",
|
|
584
|
-
unitType,
|
|
585
|
-
unitId,
|
|
586
|
-
sameUnitCount,
|
|
602
|
+
// ── Phase 3: Dispatch resolution ────────────────────────────────────
|
|
603
|
+
debugLog("autoLoop", { phase: "dispatch-resolve", iteration });
|
|
604
|
+
const dispatchResult = await deps.resolveDispatch({
|
|
605
|
+
basePath: s.basePath,
|
|
606
|
+
mid,
|
|
607
|
+
midTitle: midTitle,
|
|
608
|
+
state,
|
|
609
|
+
prefs,
|
|
610
|
+
session: s,
|
|
587
611
|
});
|
|
588
|
-
if (
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
612
|
+
if (dispatchResult.action === "stop") {
|
|
613
|
+
await closeoutAndStop(ctx, pi, s, deps, dispatchResult.reason);
|
|
614
|
+
debugLog("autoLoop", { phase: "exit", reason: "dispatch-stop" });
|
|
615
|
+
break;
|
|
616
|
+
}
|
|
617
|
+
if (dispatchResult.action !== "dispatch") {
|
|
618
|
+
// Non-dispatch action (e.g. "skip") — re-derive state
|
|
619
|
+
await new Promise((r) => setImmediate(r));
|
|
620
|
+
continue;
|
|
621
|
+
}
|
|
622
|
+
unitType = dispatchResult.unitType;
|
|
623
|
+
unitId = dispatchResult.unitId;
|
|
624
|
+
prompt = dispatchResult.prompt;
|
|
625
|
+
pauseAfterUatDispatch = dispatchResult.pauseAfterDispatch ?? false;
|
|
626
|
+
// ── Sliding-window stuck detection with graduated recovery ──
|
|
627
|
+
const derivedKey = `${unitType}/${unitId}`;
|
|
628
|
+
if (!s.pendingVerificationRetry) {
|
|
629
|
+
recentUnits.push({ key: derivedKey });
|
|
630
|
+
if (recentUnits.length > STUCK_WINDOW_SIZE)
|
|
631
|
+
recentUnits.shift();
|
|
632
|
+
const stuckSignal = detectStuck(recentUnits);
|
|
633
|
+
if (stuckSignal) {
|
|
592
634
|
debugLog("autoLoop", {
|
|
593
|
-
phase: "stuck-
|
|
594
|
-
|
|
595
|
-
|
|
635
|
+
phase: "stuck-check",
|
|
636
|
+
unitType,
|
|
637
|
+
unitId,
|
|
638
|
+
reason: stuckSignal.reason,
|
|
639
|
+
recoveryAttempts: stuckRecoveryAttempts,
|
|
596
640
|
});
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
641
|
+
if (stuckRecoveryAttempts === 0) {
|
|
642
|
+
// Level 1: try verifying the artifact, then cache invalidation + retry
|
|
643
|
+
stuckRecoveryAttempts++;
|
|
644
|
+
const artifactExists = deps.verifyExpectedArtifact(unitType, unitId, s.basePath);
|
|
645
|
+
if (artifactExists) {
|
|
646
|
+
debugLog("autoLoop", {
|
|
647
|
+
phase: "stuck-recovery",
|
|
648
|
+
level: 1,
|
|
649
|
+
action: "artifact-found",
|
|
650
|
+
});
|
|
651
|
+
ctx.ui.notify(`Stuck recovery: artifact for ${unitType} ${unitId} found on disk. Invalidating caches.`, "info");
|
|
652
|
+
deps.invalidateAllCaches();
|
|
653
|
+
continue;
|
|
654
|
+
}
|
|
655
|
+
ctx.ui.notify(`Stuck on ${unitType} ${unitId} (${stuckSignal.reason}). Invalidating caches and retrying.`, "warning");
|
|
656
|
+
deps.invalidateAllCaches();
|
|
657
|
+
}
|
|
658
|
+
else {
|
|
659
|
+
// Level 2: hard stop — genuinely stuck
|
|
660
|
+
debugLog("autoLoop", {
|
|
661
|
+
phase: "stuck-detected",
|
|
662
|
+
unitType,
|
|
663
|
+
unitId,
|
|
664
|
+
reason: stuckSignal.reason,
|
|
665
|
+
});
|
|
666
|
+
await deps.stopAuto(ctx, pi, `Stuck: ${stuckSignal.reason}`);
|
|
667
|
+
ctx.ui.notify(`Stuck on ${unitType} ${unitId} — ${stuckSignal.reason}. The expected artifact was not written.`, "error");
|
|
668
|
+
break;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
// Progress detected — reset recovery counter
|
|
673
|
+
if (stuckRecoveryAttempts > 0) {
|
|
674
|
+
debugLog("autoLoop", {
|
|
675
|
+
phase: "stuck-counter-reset",
|
|
676
|
+
from: recentUnits[recentUnits.length - 2]?.key ?? "",
|
|
677
|
+
to: derivedKey,
|
|
678
|
+
});
|
|
679
|
+
stuckRecoveryAttempts = 0;
|
|
680
|
+
}
|
|
600
681
|
}
|
|
601
|
-
ctx.ui.notify(`Stuck on ${unitType} ${unitId} (attempt ${sameUnitCount}). Invalidating caches and retrying.`, "warning");
|
|
602
|
-
deps.invalidateAllCaches();
|
|
603
682
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
683
|
+
// Pre-dispatch hooks
|
|
684
|
+
const preDispatchResult = deps.runPreDispatchHooks(unitType, unitId, prompt, s.basePath);
|
|
685
|
+
if (preDispatchResult.firedHooks.length > 0) {
|
|
686
|
+
ctx.ui.notify(`Pre-dispatch hook${preDispatchResult.firedHooks.length > 1 ? "s" : ""}: ${preDispatchResult.firedHooks.join(", ")}`, "info");
|
|
687
|
+
}
|
|
688
|
+
if (preDispatchResult.action === "skip") {
|
|
689
|
+
ctx.ui.notify(`Skipping ${unitType} ${unitId} (pre-dispatch hook).`, "info");
|
|
690
|
+
await new Promise((r) => setImmediate(r));
|
|
691
|
+
continue;
|
|
692
|
+
}
|
|
693
|
+
if (preDispatchResult.action === "replace") {
|
|
694
|
+
prompt = preDispatchResult.prompt ?? prompt;
|
|
695
|
+
if (preDispatchResult.unitType)
|
|
696
|
+
unitType = preDispatchResult.unitType;
|
|
697
|
+
}
|
|
698
|
+
else if (preDispatchResult.prompt) {
|
|
699
|
+
prompt = preDispatchResult.prompt;
|
|
700
|
+
}
|
|
701
|
+
const priorSliceBlocker = deps.getPriorSliceCompletionBlocker(s.basePath, deps.getMainBranch(s.basePath), unitType, unitId);
|
|
702
|
+
if (priorSliceBlocker) {
|
|
703
|
+
await deps.stopAuto(ctx, pi, priorSliceBlocker);
|
|
704
|
+
debugLog("autoLoop", { phase: "exit", reason: "prior-slice-blocker" });
|
|
614
705
|
break;
|
|
615
706
|
}
|
|
707
|
+
observabilityIssues = await deps.collectObservabilityWarnings(ctx, s.basePath, unitType, unitId);
|
|
708
|
+
// Derive state for shared use in execution phase
|
|
709
|
+
// (state, mid, midTitle already set above)
|
|
616
710
|
}
|
|
617
711
|
else {
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
sameUnitCount = 0;
|
|
627
|
-
}
|
|
628
|
-
// Pre-dispatch hooks
|
|
629
|
-
const preDispatchResult = deps.runPreDispatchHooks(unitType, unitId, prompt, s.basePath);
|
|
630
|
-
if (preDispatchResult.firedHooks.length > 0) {
|
|
631
|
-
ctx.ui.notify(`Pre-dispatch hook${preDispatchResult.firedHooks.length > 1 ? "s" : ""}: ${preDispatchResult.firedHooks.join(", ")}`, "info");
|
|
632
|
-
}
|
|
633
|
-
if (preDispatchResult.action === "skip") {
|
|
634
|
-
ctx.ui.notify(`Skipping ${unitType} ${unitId} (pre-dispatch hook).`, "info");
|
|
635
|
-
await new Promise((r) => setImmediate(r));
|
|
636
|
-
continue;
|
|
637
|
-
}
|
|
638
|
-
if (preDispatchResult.action === "replace") {
|
|
639
|
-
prompt = preDispatchResult.prompt ?? prompt;
|
|
640
|
-
if (preDispatchResult.unitType)
|
|
641
|
-
unitType = preDispatchResult.unitType;
|
|
642
|
-
}
|
|
643
|
-
else if (preDispatchResult.prompt) {
|
|
644
|
-
prompt = preDispatchResult.prompt;
|
|
645
|
-
}
|
|
646
|
-
const priorSliceBlocker = deps.getPriorSliceCompletionBlocker(s.basePath, deps.getMainBranch(s.basePath), unitType, unitId);
|
|
647
|
-
if (priorSliceBlocker) {
|
|
648
|
-
await deps.stopAuto(ctx, pi, priorSliceBlocker);
|
|
649
|
-
debugLog("autoLoop", { phase: "exit", reason: "prior-slice-blocker" });
|
|
650
|
-
break;
|
|
712
|
+
// ── Sidecar path: use values from the sidecar item directly ──
|
|
713
|
+
unitType = sidecarItem.unitType;
|
|
714
|
+
unitId = sidecarItem.unitId;
|
|
715
|
+
prompt = sidecarItem.prompt;
|
|
716
|
+
// Derive minimal state for progress widget / execution context
|
|
717
|
+
state = await deps.deriveState(s.basePath);
|
|
718
|
+
mid = state.activeMilestone?.id;
|
|
719
|
+
midTitle = state.activeMilestone?.title;
|
|
651
720
|
}
|
|
652
|
-
const observabilityIssues = await deps.collectObservabilityWarnings(ctx, s.basePath, unitType, unitId);
|
|
653
721
|
// ── Phase 4: Unit execution ─────────────────────────────────────────
|
|
654
722
|
debugLog("autoLoop", {
|
|
655
723
|
phase: "unit-execution",
|
|
@@ -662,33 +730,6 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
662
730
|
s.currentUnit.type === unitType &&
|
|
663
731
|
s.currentUnit.id === unitId);
|
|
664
732
|
const previousTier = s.currentUnitRouting?.tier;
|
|
665
|
-
// Closeout previous unit
|
|
666
|
-
if (s.currentUnit) {
|
|
667
|
-
await deps.closeoutUnit(ctx, s.basePath, s.currentUnit.type, s.currentUnit.id, s.currentUnit.startedAt, deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id));
|
|
668
|
-
if (s.currentUnitRouting) {
|
|
669
|
-
const isRetry = s.currentUnit.type === unitType && s.currentUnit.id === unitId;
|
|
670
|
-
deps.recordOutcome(s.currentUnit.type, s.currentUnitRouting.tier, !isRetry);
|
|
671
|
-
}
|
|
672
|
-
const closeoutKey = `${s.currentUnit.type}/${s.currentUnit.id}`;
|
|
673
|
-
const incomingKey = `${unitType}/${unitId}`;
|
|
674
|
-
const isHookUnit = s.currentUnit.type.startsWith("hook/");
|
|
675
|
-
const artifactVerified = isHookUnit ||
|
|
676
|
-
deps.verifyExpectedArtifact(s.currentUnit.type, s.currentUnit.id, s.basePath);
|
|
677
|
-
if (closeoutKey !== incomingKey && artifactVerified) {
|
|
678
|
-
s.completedUnits.push({
|
|
679
|
-
type: s.currentUnit.type,
|
|
680
|
-
id: s.currentUnit.id,
|
|
681
|
-
startedAt: s.currentUnit.startedAt,
|
|
682
|
-
finishedAt: Date.now(),
|
|
683
|
-
});
|
|
684
|
-
if (s.completedUnits.length > 200) {
|
|
685
|
-
s.completedUnits = s.completedUnits.slice(-200);
|
|
686
|
-
}
|
|
687
|
-
deps.clearUnitRuntimeRecord(s.basePath, s.currentUnit.type, s.currentUnit.id);
|
|
688
|
-
s.unitDispatchCount.delete(`${s.currentUnit.type}/${s.currentUnit.id}`);
|
|
689
|
-
s.unitRecoveryCount.delete(`${s.currentUnit.type}/${s.currentUnit.id}`);
|
|
690
|
-
}
|
|
691
|
-
}
|
|
692
733
|
s.currentUnit = { type: unitType, id: unitId, startedAt: Date.now() };
|
|
693
734
|
deps.captureAvailableSkills();
|
|
694
735
|
deps.writeUnitRuntimeRecord(s.basePath, unitType, unitId, s.currentUnit.startedAt, {
|
|
@@ -706,7 +747,6 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
706
747
|
deps.updateProgressWidget(ctx, unitType, unitId, state);
|
|
707
748
|
deps.ensurePreconditions(unitType, unitId, s.basePath, state);
|
|
708
749
|
// Prompt injection
|
|
709
|
-
const MAX_RECOVERY_CHARS = 50_000;
|
|
710
750
|
let finalPrompt = prompt;
|
|
711
751
|
if (s.pendingVerificationRetry) {
|
|
712
752
|
const retryCtx = s.pendingVerificationRetry;
|
|
@@ -744,7 +784,7 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
744
784
|
s.lastBaselineCharCount = undefined;
|
|
745
785
|
if (deps.isDbAvailable()) {
|
|
746
786
|
try {
|
|
747
|
-
const { inlineGsdRootFile } = await import
|
|
787
|
+
const { inlineGsdRootFile } = await importExtensionModule(import.meta.url, "./auto-prompts.js");
|
|
748
788
|
const [decisionsContent, requirementsContent, projectContent] = await Promise.all([
|
|
749
789
|
inlineGsdRootFile(s.basePath, "decisions.md", "Decisions"),
|
|
750
790
|
inlineGsdRootFile(s.basePath, "requirements.md", "Requirements"),
|
|
@@ -767,8 +807,8 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
767
807
|
const msg = reorderErr instanceof Error ? reorderErr.message : String(reorderErr);
|
|
768
808
|
process.stderr.write(`[gsd] prompt reorder failed (non-fatal): ${msg}\n`);
|
|
769
809
|
}
|
|
770
|
-
// Select and apply model (with tier escalation on retry)
|
|
771
|
-
const modelResult = await deps.selectAndApplyModel(ctx, pi, unitType, unitId, s.basePath, prefs, s.verbose, s.autoModeStartModel, { isRetry, previousTier });
|
|
810
|
+
// Select and apply model (with tier escalation on retry — normal units only)
|
|
811
|
+
const modelResult = await deps.selectAndApplyModel(ctx, pi, unitType, unitId, s.basePath, prefs, s.verbose, s.autoModeStartModel, sidecarItem ? undefined : { isRetry, previousTier });
|
|
772
812
|
s.currentUnitRouting =
|
|
773
813
|
modelResult.routing;
|
|
774
814
|
// Start unit supervision
|
|
@@ -794,7 +834,7 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
794
834
|
unitType,
|
|
795
835
|
unitId,
|
|
796
836
|
});
|
|
797
|
-
const unitResult = await runUnit(ctx, pi, s, unitType, unitId, finalPrompt
|
|
837
|
+
const unitResult = await runUnit(ctx, pi, s, unitType, unitId, finalPrompt);
|
|
798
838
|
debugLog("autoLoop", {
|
|
799
839
|
phase: "runUnit-end",
|
|
800
840
|
iteration,
|
|
@@ -802,12 +842,60 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
802
842
|
unitId,
|
|
803
843
|
status: unitResult.status,
|
|
804
844
|
});
|
|
845
|
+
// Tag the most recent window entry with error info for stuck detection
|
|
846
|
+
if (unitResult.status === "error" || unitResult.status === "cancelled") {
|
|
847
|
+
const lastEntry = recentUnits[recentUnits.length - 1];
|
|
848
|
+
if (lastEntry) {
|
|
849
|
+
lastEntry.error = `${unitResult.status}:${unitType}/${unitId}`;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
else if (unitResult.event?.messages?.length) {
|
|
853
|
+
const lastMsg = unitResult.event.messages[unitResult.event.messages.length - 1];
|
|
854
|
+
const msgStr = typeof lastMsg === "string" ? lastMsg : JSON.stringify(lastMsg);
|
|
855
|
+
if (/error|fail|exception/i.test(msgStr)) {
|
|
856
|
+
const lastEntry = recentUnits[recentUnits.length - 1];
|
|
857
|
+
if (lastEntry) {
|
|
858
|
+
lastEntry.error = msgStr.slice(0, 200);
|
|
859
|
+
}
|
|
860
|
+
}
|
|
861
|
+
}
|
|
805
862
|
if (unitResult.status === "cancelled") {
|
|
806
863
|
ctx.ui.notify(`Session creation timed out or was cancelled for ${unitType} ${unitId}. Will retry.`, "warning");
|
|
807
864
|
await deps.stopAuto(ctx, pi, "Session creation failed");
|
|
808
865
|
debugLog("autoLoop", { phase: "exit", reason: "session-failed" });
|
|
809
866
|
break;
|
|
810
867
|
}
|
|
868
|
+
// ── Immediate unit closeout (metrics, activity log, memory) ────────
|
|
869
|
+
// Run right after runUnit() returns so telemetry is never lost to a
|
|
870
|
+
// crash between iterations.
|
|
871
|
+
await deps.closeoutUnit(ctx, s.basePath, unitType, unitId, s.currentUnit.startedAt, deps.buildSnapshotOpts(unitType, unitId));
|
|
872
|
+
if (s.currentUnitRouting) {
|
|
873
|
+
deps.recordOutcome(unitType, s.currentUnitRouting.tier, true);
|
|
874
|
+
}
|
|
875
|
+
const isHookUnit = unitType.startsWith("hook/");
|
|
876
|
+
const artifactVerified = isHookUnit ||
|
|
877
|
+
deps.verifyExpectedArtifact(unitType, unitId, s.basePath);
|
|
878
|
+
if (artifactVerified) {
|
|
879
|
+
s.completedUnits.push({
|
|
880
|
+
type: unitType,
|
|
881
|
+
id: unitId,
|
|
882
|
+
startedAt: s.currentUnit.startedAt,
|
|
883
|
+
finishedAt: Date.now(),
|
|
884
|
+
});
|
|
885
|
+
if (s.completedUnits.length > 200) {
|
|
886
|
+
s.completedUnits = s.completedUnits.slice(-200);
|
|
887
|
+
}
|
|
888
|
+
// Flush completed-units to disk so the record survives crashes
|
|
889
|
+
try {
|
|
890
|
+
const completedKeysPath = join(gsdRoot(s.basePath), "completed-units.json");
|
|
891
|
+
const keys = s.completedUnits.map((u) => `${u.type}/${u.id}`);
|
|
892
|
+
atomicWriteSync(completedKeysPath, JSON.stringify(keys, null, 2));
|
|
893
|
+
}
|
|
894
|
+
catch { /* non-fatal: disk flush failure */ }
|
|
895
|
+
deps.clearUnitRuntimeRecord(s.basePath, unitType, unitId);
|
|
896
|
+
s.unitDispatchCount.delete(`${unitType}/${unitId}`);
|
|
897
|
+
s.unitRecoveryCount.delete(`${unitType}/${unitId}`);
|
|
898
|
+
}
|
|
811
899
|
// ── Phase 5: Finalize ───────────────────────────────────────────────
|
|
812
900
|
debugLog("autoLoop", { phase: "finalize", iteration });
|
|
813
901
|
// Clear unit timeout (unit completed)
|
|
@@ -824,7 +912,13 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
824
912
|
updateProgressWidget: deps.updateProgressWidget,
|
|
825
913
|
};
|
|
826
914
|
// Pre-verification processing (commit, doctor, state rebuild, etc.)
|
|
827
|
-
|
|
915
|
+
// Sidecar items use lightweight pre-verification opts
|
|
916
|
+
const preVerificationOpts = sidecarItem
|
|
917
|
+
? sidecarItem.kind === "hook"
|
|
918
|
+
? { skipSettleDelay: true, skipDoctor: true, skipStateRebuild: true, skipWorktreeSync: true }
|
|
919
|
+
: { skipSettleDelay: true, skipStateRebuild: true }
|
|
920
|
+
: undefined;
|
|
921
|
+
const preResult = await deps.postUnitPreVerification(postUnitCtx, preVerificationOpts);
|
|
828
922
|
if (preResult === "dispatched") {
|
|
829
923
|
debugLog("autoLoop", {
|
|
830
924
|
phase: "exit",
|
|
@@ -838,17 +932,28 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
838
932
|
debugLog("autoLoop", { phase: "exit", reason: "uat-pause" });
|
|
839
933
|
break;
|
|
840
934
|
}
|
|
841
|
-
// Verification gate
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
935
|
+
// Verification gate
|
|
936
|
+
// Hook sidecar items skip verification entirely.
|
|
937
|
+
// Non-hook sidecar items run verification but skip retries (just continue).
|
|
938
|
+
const skipVerification = sidecarItem?.kind === "hook";
|
|
939
|
+
if (!skipVerification) {
|
|
940
|
+
const verificationResult = await deps.runPostUnitVerification({ s, ctx, pi }, deps.pauseAuto);
|
|
941
|
+
if (verificationResult === "pause") {
|
|
942
|
+
debugLog("autoLoop", { phase: "exit", reason: "verification-pause" });
|
|
943
|
+
break;
|
|
944
|
+
}
|
|
945
|
+
if (verificationResult === "retry") {
|
|
946
|
+
if (sidecarItem) {
|
|
947
|
+
// Sidecar verification retries are skipped — just continue
|
|
948
|
+
debugLog("autoLoop", { phase: "sidecar-verification-retry-skipped", iteration });
|
|
949
|
+
}
|
|
950
|
+
else {
|
|
951
|
+
// s.pendingVerificationRetry was set by runPostUnitVerification.
|
|
952
|
+
// Continue the loop — next iteration will inject the retry context into the prompt.
|
|
953
|
+
debugLog("autoLoop", { phase: "verification-retry", iteration });
|
|
954
|
+
continue;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
852
957
|
}
|
|
853
958
|
// Post-verification processing (DB dual-write, hooks, triage, quick-tasks)
|
|
854
959
|
const postResult = await deps.postUnitPostVerification(postUnitCtx);
|
|
@@ -864,102 +969,6 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
864
969
|
debugLog("autoLoop", { phase: "exit", reason: "step-wizard" });
|
|
865
970
|
break;
|
|
866
971
|
}
|
|
867
|
-
// ── Sidecar drain: dispatch enqueued hooks/triage/quick-tasks ──
|
|
868
|
-
let sidecarBroke = false;
|
|
869
|
-
while (s.sidecarQueue.length > 0 && s.active) {
|
|
870
|
-
const item = s.sidecarQueue.shift();
|
|
871
|
-
debugLog("autoLoop", {
|
|
872
|
-
phase: "sidecar-dequeue",
|
|
873
|
-
kind: item.kind,
|
|
874
|
-
unitType: item.unitType,
|
|
875
|
-
unitId: item.unitId,
|
|
876
|
-
});
|
|
877
|
-
// Set up as current unit
|
|
878
|
-
const sidecarStartedAt = Date.now();
|
|
879
|
-
s.currentUnit = {
|
|
880
|
-
type: item.unitType,
|
|
881
|
-
id: item.unitId,
|
|
882
|
-
startedAt: sidecarStartedAt,
|
|
883
|
-
};
|
|
884
|
-
deps.writeUnitRuntimeRecord(s.basePath, item.unitType, item.unitId, sidecarStartedAt, {
|
|
885
|
-
phase: "dispatched",
|
|
886
|
-
wrapupWarningSent: false,
|
|
887
|
-
timeoutAt: null,
|
|
888
|
-
lastProgressAt: sidecarStartedAt,
|
|
889
|
-
progressCount: 0,
|
|
890
|
-
lastProgressKind: "dispatch",
|
|
891
|
-
});
|
|
892
|
-
// Model selection (handles hook model override)
|
|
893
|
-
await deps.selectAndApplyModel(ctx, pi, item.unitType, item.unitId, s.basePath, prefs, s.verbose, s.autoModeStartModel);
|
|
894
|
-
// Supervision
|
|
895
|
-
deps.clearUnitTimeout();
|
|
896
|
-
deps.startUnitSupervision({
|
|
897
|
-
s,
|
|
898
|
-
ctx,
|
|
899
|
-
pi,
|
|
900
|
-
unitType: item.unitType,
|
|
901
|
-
unitId: item.unitId,
|
|
902
|
-
prefs,
|
|
903
|
-
buildSnapshotOpts: () => deps.buildSnapshotOpts(item.unitType, item.unitId),
|
|
904
|
-
buildRecoveryContext: () => ({}),
|
|
905
|
-
pauseAuto: deps.pauseAuto,
|
|
906
|
-
});
|
|
907
|
-
// Write lock
|
|
908
|
-
const sidecarSessionFile = deps.getSessionFile(ctx);
|
|
909
|
-
deps.writeLock(deps.lockBase(), item.unitType, item.unitId, s.completedUnits.length, sidecarSessionFile);
|
|
910
|
-
// Execute via standard runUnit
|
|
911
|
-
const sidecarResult = await runUnit(ctx, pi, s, item.unitType, item.unitId, item.prompt, prefs);
|
|
912
|
-
deps.clearUnitTimeout();
|
|
913
|
-
if (sidecarResult.status === "cancelled") {
|
|
914
|
-
ctx.ui.notify(`Sidecar unit ${item.unitType} ${item.unitId} session cancelled. Stopping.`, "warning");
|
|
915
|
-
await deps.stopAuto(ctx, pi, "Sidecar session creation failed");
|
|
916
|
-
sidecarBroke = true;
|
|
917
|
-
break;
|
|
918
|
-
}
|
|
919
|
-
// Run pre-verification for the sidecar unit
|
|
920
|
-
const sidecarPreResult = await deps.postUnitPreVerification(postUnitCtx);
|
|
921
|
-
if (sidecarPreResult === "dispatched") {
|
|
922
|
-
// Pre-verification caused stop/pause
|
|
923
|
-
debugLog("autoLoop", {
|
|
924
|
-
phase: "exit",
|
|
925
|
-
reason: "sidecar-pre-verification-stop",
|
|
926
|
-
});
|
|
927
|
-
sidecarBroke = true;
|
|
928
|
-
break;
|
|
929
|
-
}
|
|
930
|
-
// Verification gate for non-hook sidecar units (triage, quick-tasks)
|
|
931
|
-
// Hook units are lightweight and don't need verification.
|
|
932
|
-
if (item.kind !== "hook") {
|
|
933
|
-
const sidecarVerification = await deps.runPostUnitVerification({ s, ctx, pi }, deps.pauseAuto);
|
|
934
|
-
if (sidecarVerification === "pause") {
|
|
935
|
-
debugLog("autoLoop", {
|
|
936
|
-
phase: "exit",
|
|
937
|
-
reason: "sidecar-verification-pause",
|
|
938
|
-
});
|
|
939
|
-
sidecarBroke = true;
|
|
940
|
-
break;
|
|
941
|
-
}
|
|
942
|
-
// "retry" for sidecars — skip retry, just continue (sidecar retries are not worth the complexity)
|
|
943
|
-
}
|
|
944
|
-
// Post-verification (may enqueue more sidecar items)
|
|
945
|
-
const sidecarPostResult = await deps.postUnitPostVerification(postUnitCtx);
|
|
946
|
-
if (sidecarPostResult === "stopped") {
|
|
947
|
-
debugLog("autoLoop", { phase: "exit", reason: "sidecar-stopped" });
|
|
948
|
-
sidecarBroke = true;
|
|
949
|
-
break;
|
|
950
|
-
}
|
|
951
|
-
if (sidecarPostResult === "step-wizard") {
|
|
952
|
-
debugLog("autoLoop", {
|
|
953
|
-
phase: "exit",
|
|
954
|
-
reason: "sidecar-step-wizard",
|
|
955
|
-
});
|
|
956
|
-
sidecarBroke = true;
|
|
957
|
-
break;
|
|
958
|
-
}
|
|
959
|
-
// "continue" — loop checks sidecarQueue again
|
|
960
|
-
}
|
|
961
|
-
if (sidecarBroke)
|
|
962
|
-
break;
|
|
963
972
|
consecutiveErrors = 0; // Iteration completed successfully
|
|
964
973
|
debugLog("autoLoop", { phase: "iteration-complete", iteration });
|
|
965
974
|
}
|
|
@@ -990,6 +999,6 @@ export async function autoLoop(ctx, pi, s, deps) {
|
|
|
990
999
|
}
|
|
991
1000
|
}
|
|
992
1001
|
}
|
|
993
|
-
|
|
1002
|
+
_currentResolve = null;
|
|
994
1003
|
debugLog("autoLoop", { phase: "exit", totalIterations: iteration });
|
|
995
1004
|
}
|