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.
Files changed (239) hide show
  1. package/README.md +1 -1
  2. package/dist/app-paths.js +1 -1
  3. package/dist/cli.js +9 -0
  4. package/dist/extension-discovery.d.ts +5 -3
  5. package/dist/extension-discovery.js +14 -9
  6. package/dist/extension-registry.js +2 -2
  7. package/dist/onboarding.js +1 -0
  8. package/dist/remote-questions-config.js +2 -2
  9. package/dist/resource-loader.js +34 -1
  10. package/dist/resources/extensions/browser-tools/package.json +3 -1
  11. package/dist/resources/extensions/cmux/index.js +55 -1
  12. package/dist/resources/extensions/context7/package.json +1 -1
  13. package/dist/resources/extensions/env-utils.js +29 -0
  14. package/dist/resources/extensions/get-secrets-from-user.js +5 -24
  15. package/dist/resources/extensions/github-sync/cli.js +284 -0
  16. package/dist/resources/extensions/github-sync/index.js +73 -0
  17. package/dist/resources/extensions/github-sync/mapping.js +67 -0
  18. package/dist/resources/extensions/github-sync/sync.js +424 -0
  19. package/dist/resources/extensions/github-sync/templates.js +118 -0
  20. package/dist/resources/extensions/github-sync/types.js +7 -0
  21. package/dist/resources/extensions/google-search/package.json +3 -1
  22. package/dist/resources/extensions/gsd/auto/session.js +6 -23
  23. package/dist/resources/extensions/gsd/auto-dispatch.js +75 -10
  24. package/dist/resources/extensions/gsd/auto-loop.js +597 -588
  25. package/dist/resources/extensions/gsd/auto-post-unit.js +111 -68
  26. package/dist/resources/extensions/gsd/auto-prompts.js +114 -45
  27. package/dist/resources/extensions/gsd/auto-recovery.js +37 -1
  28. package/dist/resources/extensions/gsd/auto-start.js +13 -2
  29. package/dist/resources/extensions/gsd/auto-worktree-sync.js +13 -5
  30. package/dist/resources/extensions/gsd/auto-worktree.js +3 -3
  31. package/dist/resources/extensions/gsd/auto.js +143 -96
  32. package/dist/resources/extensions/gsd/captures.js +9 -1
  33. package/dist/resources/extensions/gsd/commands-extensions.js +3 -2
  34. package/dist/resources/extensions/gsd/commands-handlers.js +16 -3
  35. package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
  36. package/dist/resources/extensions/gsd/commands.js +24 -3
  37. package/dist/resources/extensions/gsd/context-budget.js +2 -10
  38. package/dist/resources/extensions/gsd/detection.js +1 -2
  39. package/dist/resources/extensions/gsd/docs/preferences-reference.md +0 -2
  40. package/dist/resources/extensions/gsd/doctor-checks.js +82 -0
  41. package/dist/resources/extensions/gsd/doctor-environment.js +78 -0
  42. package/dist/resources/extensions/gsd/doctor-format.js +15 -0
  43. package/dist/resources/extensions/gsd/doctor-providers.js +62 -12
  44. package/dist/resources/extensions/gsd/doctor.js +204 -12
  45. package/dist/resources/extensions/gsd/exit-command.js +2 -1
  46. package/dist/resources/extensions/gsd/export.js +1 -1
  47. package/dist/resources/extensions/gsd/files.js +47 -2
  48. package/dist/resources/extensions/gsd/forensics.js +1 -1
  49. package/dist/resources/extensions/gsd/git-service.js +15 -12
  50. package/dist/resources/extensions/gsd/guided-flow.js +82 -32
  51. package/dist/resources/extensions/gsd/index.js +24 -20
  52. package/dist/resources/extensions/gsd/migrate/parsers.js +1 -1
  53. package/dist/resources/extensions/gsd/native-git-bridge.js +37 -0
  54. package/dist/resources/extensions/gsd/observability-validator.js +24 -0
  55. package/dist/resources/extensions/gsd/package.json +1 -1
  56. package/dist/resources/extensions/gsd/preferences-models.js +0 -12
  57. package/dist/resources/extensions/gsd/preferences-types.js +3 -2
  58. package/dist/resources/extensions/gsd/preferences-validation.js +101 -11
  59. package/dist/resources/extensions/gsd/preferences.js +8 -5
  60. package/dist/resources/extensions/gsd/prompts/discuss.md +11 -14
  61. package/dist/resources/extensions/gsd/prompts/execute-task.md +2 -2
  62. package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
  63. package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
  64. package/dist/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
  65. package/dist/resources/extensions/gsd/prompts/plan-slice.md +2 -1
  66. package/dist/resources/extensions/gsd/prompts/queue.md +4 -8
  67. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +44 -0
  68. package/dist/resources/extensions/gsd/prompts/run-uat.md +27 -10
  69. package/dist/resources/extensions/gsd/prompts/workflow-start.md +2 -2
  70. package/dist/resources/extensions/gsd/reactive-graph.js +227 -0
  71. package/dist/resources/extensions/gsd/repo-identity.js +21 -4
  72. package/dist/resources/extensions/gsd/resource-version.js +2 -1
  73. package/dist/resources/extensions/gsd/roadmap-mutations.js +24 -0
  74. package/dist/resources/extensions/gsd/state.js +1 -1
  75. package/dist/resources/extensions/gsd/templates/task-plan.md +11 -3
  76. package/dist/resources/extensions/gsd/visualizer-data.js +1 -1
  77. package/dist/resources/extensions/gsd/worktree.js +35 -16
  78. package/dist/resources/extensions/mcp-client/index.js +14 -1
  79. package/dist/resources/extensions/remote-questions/status.js +2 -1
  80. package/dist/resources/extensions/remote-questions/store.js +2 -1
  81. package/dist/resources/extensions/search-the-web/provider.js +2 -1
  82. package/dist/resources/extensions/subagent/index.js +12 -3
  83. package/dist/resources/extensions/subagent/isolation.js +2 -1
  84. package/dist/resources/extensions/ttsr/rule-loader.js +2 -1
  85. package/dist/resources/extensions/universal-config/package.json +1 -1
  86. package/dist/welcome-screen.d.ts +12 -0
  87. package/dist/welcome-screen.js +53 -0
  88. package/package.json +2 -1
  89. package/packages/pi-ai/dist/env-api-keys.js +13 -0
  90. package/packages/pi-ai/dist/env-api-keys.js.map +1 -1
  91. package/packages/pi-ai/dist/models.generated.d.ts +172 -0
  92. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  93. package/packages/pi-ai/dist/models.generated.js +172 -0
  94. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  95. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts +64 -0
  96. package/packages/pi-ai/dist/providers/anthropic-shared.d.ts.map +1 -0
  97. package/packages/pi-ai/dist/providers/anthropic-shared.js +668 -0
  98. package/packages/pi-ai/dist/providers/anthropic-shared.js.map +1 -0
  99. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts +5 -0
  100. package/packages/pi-ai/dist/providers/anthropic-vertex.d.ts.map +1 -0
  101. package/packages/pi-ai/dist/providers/anthropic-vertex.js +85 -0
  102. package/packages/pi-ai/dist/providers/anthropic-vertex.js.map +1 -0
  103. package/packages/pi-ai/dist/providers/anthropic.d.ts +4 -30
  104. package/packages/pi-ai/dist/providers/anthropic.d.ts.map +1 -1
  105. package/packages/pi-ai/dist/providers/anthropic.js +47 -764
  106. package/packages/pi-ai/dist/providers/anthropic.js.map +1 -1
  107. package/packages/pi-ai/dist/providers/register-builtins.d.ts.map +1 -1
  108. package/packages/pi-ai/dist/providers/register-builtins.js +6 -0
  109. package/packages/pi-ai/dist/providers/register-builtins.js.map +1 -1
  110. package/packages/pi-ai/dist/types.d.ts +2 -2
  111. package/packages/pi-ai/dist/types.d.ts.map +1 -1
  112. package/packages/pi-ai/dist/types.js.map +1 -1
  113. package/packages/pi-ai/dist/utils/oauth/anthropic.js +2 -2
  114. package/packages/pi-ai/dist/utils/oauth/anthropic.js.map +1 -1
  115. package/packages/pi-ai/package.json +1 -0
  116. package/packages/pi-ai/src/env-api-keys.ts +14 -0
  117. package/packages/pi-ai/src/models.generated.ts +172 -0
  118. package/packages/pi-ai/src/providers/anthropic-shared.ts +761 -0
  119. package/packages/pi-ai/src/providers/anthropic-vertex.ts +130 -0
  120. package/packages/pi-ai/src/providers/anthropic.ts +76 -868
  121. package/packages/pi-ai/src/providers/register-builtins.ts +7 -0
  122. package/packages/pi-ai/src/types.ts +2 -0
  123. package/packages/pi-ai/src/utils/oauth/anthropic.ts +2 -2
  124. package/packages/pi-coding-agent/dist/core/extensions/loader.d.ts.map +1 -1
  125. package/packages/pi-coding-agent/dist/core/extensions/loader.js +205 -7
  126. package/packages/pi-coding-agent/dist/core/extensions/loader.js.map +1 -1
  127. package/packages/pi-coding-agent/dist/core/model-resolver.d.ts.map +1 -1
  128. package/packages/pi-coding-agent/dist/core/model-resolver.js +1 -0
  129. package/packages/pi-coding-agent/dist/core/model-resolver.js.map +1 -1
  130. package/packages/pi-coding-agent/dist/core/package-manager.d.ts.map +1 -1
  131. package/packages/pi-coding-agent/dist/core/package-manager.js +8 -4
  132. package/packages/pi-coding-agent/dist/core/package-manager.js.map +1 -1
  133. package/packages/pi-coding-agent/package.json +1 -1
  134. package/packages/pi-coding-agent/src/core/extensions/loader.ts +223 -7
  135. package/packages/pi-coding-agent/src/core/model-resolver.ts +1 -0
  136. package/packages/pi-coding-agent/src/core/package-manager.ts +8 -4
  137. package/pkg/package.json +1 -1
  138. package/src/resources/extensions/cmux/index.ts +57 -1
  139. package/src/resources/extensions/env-utils.ts +31 -0
  140. package/src/resources/extensions/get-secrets-from-user.ts +5 -24
  141. package/src/resources/extensions/github-sync/cli.ts +364 -0
  142. package/src/resources/extensions/github-sync/index.ts +93 -0
  143. package/src/resources/extensions/github-sync/mapping.ts +81 -0
  144. package/src/resources/extensions/github-sync/sync.ts +556 -0
  145. package/src/resources/extensions/github-sync/templates.ts +183 -0
  146. package/src/resources/extensions/github-sync/tests/cli.test.ts +20 -0
  147. package/src/resources/extensions/github-sync/tests/commit-linking.test.ts +39 -0
  148. package/src/resources/extensions/github-sync/tests/mapping.test.ts +104 -0
  149. package/src/resources/extensions/github-sync/tests/templates.test.ts +110 -0
  150. package/src/resources/extensions/github-sync/types.ts +47 -0
  151. package/src/resources/extensions/gsd/auto/session.ts +7 -25
  152. package/src/resources/extensions/gsd/auto-dispatch.ts +100 -9
  153. package/src/resources/extensions/gsd/auto-loop.ts +484 -546
  154. package/src/resources/extensions/gsd/auto-post-unit.ts +92 -42
  155. package/src/resources/extensions/gsd/auto-prompts.ts +150 -48
  156. package/src/resources/extensions/gsd/auto-recovery.ts +42 -0
  157. package/src/resources/extensions/gsd/auto-start.ts +18 -2
  158. package/src/resources/extensions/gsd/auto-worktree-sync.ts +15 -4
  159. package/src/resources/extensions/gsd/auto-worktree.ts +3 -3
  160. package/src/resources/extensions/gsd/auto.ts +139 -101
  161. package/src/resources/extensions/gsd/captures.ts +10 -1
  162. package/src/resources/extensions/gsd/commands-extensions.ts +4 -2
  163. package/src/resources/extensions/gsd/commands-handlers.ts +17 -2
  164. package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
  165. package/src/resources/extensions/gsd/commands.ts +26 -4
  166. package/src/resources/extensions/gsd/context-budget.ts +2 -12
  167. package/src/resources/extensions/gsd/detection.ts +2 -2
  168. package/src/resources/extensions/gsd/docs/preferences-reference.md +0 -2
  169. package/src/resources/extensions/gsd/doctor-checks.ts +75 -0
  170. package/src/resources/extensions/gsd/doctor-environment.ts +82 -1
  171. package/src/resources/extensions/gsd/doctor-format.ts +20 -0
  172. package/src/resources/extensions/gsd/doctor-providers.ts +64 -10
  173. package/src/resources/extensions/gsd/doctor-types.ts +16 -1
  174. package/src/resources/extensions/gsd/doctor.ts +199 -14
  175. package/src/resources/extensions/gsd/exit-command.ts +2 -2
  176. package/src/resources/extensions/gsd/export.ts +1 -1
  177. package/src/resources/extensions/gsd/files.ts +50 -3
  178. package/src/resources/extensions/gsd/forensics.ts +1 -1
  179. package/src/resources/extensions/gsd/git-service.ts +20 -10
  180. package/src/resources/extensions/gsd/guided-flow.ts +110 -38
  181. package/src/resources/extensions/gsd/index.ts +24 -17
  182. package/src/resources/extensions/gsd/migrate/parsers.ts +1 -1
  183. package/src/resources/extensions/gsd/native-git-bridge.ts +37 -0
  184. package/src/resources/extensions/gsd/observability-validator.ts +27 -0
  185. package/src/resources/extensions/gsd/preferences-models.ts +0 -12
  186. package/src/resources/extensions/gsd/preferences-types.ts +9 -5
  187. package/src/resources/extensions/gsd/preferences-validation.ts +92 -11
  188. package/src/resources/extensions/gsd/preferences.ts +8 -5
  189. package/src/resources/extensions/gsd/prompts/discuss.md +11 -14
  190. package/src/resources/extensions/gsd/prompts/execute-task.md +2 -2
  191. package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
  192. package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
  193. package/src/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
  194. package/src/resources/extensions/gsd/prompts/plan-slice.md +2 -1
  195. package/src/resources/extensions/gsd/prompts/queue.md +4 -8
  196. package/src/resources/extensions/gsd/prompts/reactive-execute.md +44 -0
  197. package/src/resources/extensions/gsd/prompts/run-uat.md +27 -10
  198. package/src/resources/extensions/gsd/prompts/workflow-start.md +2 -2
  199. package/src/resources/extensions/gsd/reactive-graph.ts +289 -0
  200. package/src/resources/extensions/gsd/repo-identity.ts +23 -4
  201. package/src/resources/extensions/gsd/resource-version.ts +3 -1
  202. package/src/resources/extensions/gsd/roadmap-mutations.ts +29 -0
  203. package/src/resources/extensions/gsd/state.ts +1 -1
  204. package/src/resources/extensions/gsd/templates/task-plan.md +11 -3
  205. package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +21 -18
  206. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +122 -68
  207. package/src/resources/extensions/gsd/tests/cmux.test.ts +93 -0
  208. package/src/resources/extensions/gsd/tests/doctor-enhancements.test.ts +266 -0
  209. package/src/resources/extensions/gsd/tests/doctor-providers.test.ts +191 -3
  210. package/src/resources/extensions/gsd/tests/plan-quality-validator.test.ts +111 -0
  211. package/src/resources/extensions/gsd/tests/preferences.test.ts +2 -7
  212. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +59 -0
  213. package/src/resources/extensions/gsd/tests/reactive-executor.test.ts +511 -0
  214. package/src/resources/extensions/gsd/tests/reactive-graph.test.ts +299 -0
  215. package/src/resources/extensions/gsd/tests/repo-identity-worktree.test.ts +21 -1
  216. package/src/resources/extensions/gsd/tests/run-uat.test.ts +11 -3
  217. package/src/resources/extensions/gsd/tests/worktree.test.ts +47 -0
  218. package/src/resources/extensions/gsd/types.ts +43 -1
  219. package/src/resources/extensions/gsd/visualizer-data.ts +1 -1
  220. package/src/resources/extensions/gsd/worktree.ts +35 -15
  221. package/src/resources/extensions/mcp-client/index.ts +17 -1
  222. package/src/resources/extensions/remote-questions/status.ts +3 -1
  223. package/src/resources/extensions/remote-questions/store.ts +3 -1
  224. package/src/resources/extensions/search-the-web/provider.ts +2 -1
  225. package/src/resources/extensions/subagent/index.ts +12 -3
  226. package/src/resources/extensions/subagent/isolation.ts +3 -1
  227. package/src/resources/extensions/ttsr/rule-loader.ts +3 -1
  228. package/dist/resources/extensions/gsd/prompt-compressor.js +0 -393
  229. package/dist/resources/extensions/gsd/semantic-chunker.js +0 -254
  230. package/dist/resources/extensions/gsd/summary-distiller.js +0 -212
  231. package/src/resources/extensions/gsd/prompt-compressor.ts +0 -508
  232. package/src/resources/extensions/gsd/semantic-chunker.ts +0 -336
  233. package/src/resources/extensions/gsd/summary-distiller.ts +0 -258
  234. package/src/resources/extensions/gsd/tests/context-compression.test.ts +0 -193
  235. package/src/resources/extensions/gsd/tests/prompt-compressor.test.ts +0 -529
  236. package/src/resources/extensions/gsd/tests/semantic-chunker.test.ts +0 -426
  237. package/src/resources/extensions/gsd/tests/summary-distiller.test.ts +0 -323
  238. package/src/resources/extensions/gsd/tests/token-optimization-benchmark.test.ts +0 -1272
  239. package/src/resources/extensions/gsd/tests/token-optimization-prefs.test.ts +0 -164
@@ -5,20 +5,20 @@
5
5
  * pattern with a while loop. The agent_end event resolves a promise instead
6
6
  * of recursing.
7
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.
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
12
 
13
- import type { ExtensionAPI, ExtensionContext } from "@gsd/pi-coding-agent";
13
+ import { importExtensionModule, type ExtensionAPI, type ExtensionContext } from "@gsd/pi-coding-agent";
14
14
 
15
- import type { AutoSession } from "./auto/session.js";
15
+ import type { AutoSession, SidecarItem } from "./auto/session.js";
16
16
  import { NEW_SESSION_TIMEOUT_MS } from "./auto/session.js";
17
17
  import type { GSDPreferences } from "./preferences.js";
18
18
  import type { SessionLockStatus } from "./session-lock.js";
19
19
  import type { GSDState } from "./types.js";
20
20
  import type { CloseoutOptions } from "./auto-unit-closeout.js";
21
- import type { PostUnitContext } from "./auto-post-unit.js";
21
+ import type { PostUnitContext, PreVerificationOpts } from "./auto-post-unit.js";
22
22
  import type {
23
23
  VerificationContext,
24
24
  VerificationResult,
@@ -26,6 +26,9 @@ import type {
26
26
  import type { DispatchAction } from "./auto-dispatch.js";
27
27
  import type { WorktreeResolver } from "./worktree-resolver.js";
28
28
  import { debugLog } from "./debug-logger.js";
29
+ import { gsdRoot } from "./paths.js";
30
+ import { atomicWriteSync } from "./atomic-write.js";
31
+ import { join } from "node:path";
29
32
  import type { CmuxLogLevel } from "../cmux/index.js";
30
33
 
31
34
  /**
@@ -35,6 +38,23 @@ import type { CmuxLogLevel } from "../cmux/index.js";
35
38
  * generous headroom including retries and sidecar work.
36
39
  */
37
40
  const MAX_LOOP_ITERATIONS = 500;
41
+ /** Maximum characters of failure/crash context included in recovery prompts. */
42
+ const MAX_RECOVERY_CHARS = 50_000;
43
+
44
+ /** Data-driven budget threshold notifications (descending). The 100% entry
45
+ * triggers special enforcement logic (halt/pause/warn); sub-100 entries fire
46
+ * a simple notification. */
47
+ const BUDGET_THRESHOLDS: Array<{
48
+ pct: number;
49
+ label: string;
50
+ notifyLevel: "info" | "warning" | "error";
51
+ cmuxLevel: "progress" | "warning" | "error";
52
+ }> = [
53
+ { pct: 100, label: "Budget ceiling reached", notifyLevel: "error", cmuxLevel: "error" },
54
+ { pct: 90, label: "Budget 90%", notifyLevel: "warning", cmuxLevel: "warning" },
55
+ { pct: 80, label: "Approaching budget ceiling — 80%", notifyLevel: "warning", cmuxLevel: "warning" },
56
+ { pct: 75, label: "Budget 75%", notifyLevel: "info", cmuxLevel: "progress" },
57
+ ];
38
58
 
39
59
  // ─── Types ───────────────────────────────────────────────────────────────────
40
60
 
@@ -54,17 +74,15 @@ export interface UnitResult {
54
74
  event?: AgentEndEvent;
55
75
  }
56
76
 
57
- // ─── Session-scoped promise state ───────────────────────────────────────────
77
+ // ─── Per-unit one-shot promise state ────────────────────────────────────────
58
78
  //
59
- // pendingResolve and pendingAgentEndQueue live on AutoSession (not module-level)
60
- // so concurrent sessions cannot corrupt each other's promises.
79
+ // A single module-level resolve function scoped to the current unit execution.
80
+ // No queue if an agent_end arrives with no pending resolver, it is dropped
81
+ // (logged as warning). This is simpler and safer than the previous session-
82
+ // scoped pendingResolve + pendingAgentEndQueue pattern.
61
83
 
62
- /**
63
- * The singleton session reference used by resolveAgentEnd. Set by autoLoop
64
- * on entry so that the agent_end handler in index.ts can resolve the correct
65
- * session's promise without needing a direct reference to `s`.
66
- */
67
- let _activeSession: AutoSession | null = null;
84
+ let _currentResolve: ((result: UnitResult) => void) | null = null;
85
+ let _sessionSwitchInFlight = false;
68
86
 
69
87
  // ─── resolveAgentEnd ─────────────────────────────────────────────────────────
70
88
 
@@ -73,60 +91,105 @@ let _activeSession: AutoSession | null = null;
73
91
  * in-flight unit promise. One-shot: the resolver is nulled before calling
74
92
  * to prevent double-resolution from model fallback retries.
75
93
  *
76
- * If no pendingResolve exists (event arrived between loop iterations),
77
- * the event is queued on the session so the next runUnit can drain it.
94
+ * If no resolver exists (event arrived between loop iterations or during
95
+ * session switch), the event is dropped with a debug warning.
78
96
  */
79
97
  export function resolveAgentEnd(event: AgentEndEvent): void {
80
- const s = _activeSession;
81
- if (!s) {
82
- debugLog("resolveAgentEnd", {
83
- status: "no-active-session",
84
- warning: "agent_end with no active loop session",
85
- });
98
+ if (_sessionSwitchInFlight) {
99
+ debugLog("resolveAgentEnd", { status: "ignored-during-switch" });
86
100
  return;
87
101
  }
88
-
89
- if (s.pendingResolve) {
102
+ if (_currentResolve) {
90
103
  debugLog("resolveAgentEnd", { status: "resolving", hasEvent: true });
91
- const r = s.pendingResolve;
92
- s.pendingResolve = null;
104
+ const r = _currentResolve;
105
+ _currentResolve = null;
93
106
  r({ status: "completed", event });
94
107
  } else {
95
- // Queue the event so the next runUnit picks it up immediately
96
108
  debugLog("resolveAgentEnd", {
97
- status: "queued",
98
- queueLength: s.pendingAgentEndQueue.length + 1,
99
- warning:
100
- "agent_end arrived between loop iterations — queued for next runUnit",
109
+ status: "no-pending-resolve",
110
+ warning: "agent_end with no pending unit",
101
111
  });
102
- s.pendingAgentEndQueue.push(event);
103
112
  }
104
113
  }
105
114
 
106
115
  export function isSessionSwitchInFlight(): boolean {
107
- return _activeSession?.sessionSwitchInFlight ?? false;
116
+ return _sessionSwitchInFlight;
108
117
  }
109
118
 
110
119
  // ─── resetPendingResolve (test helper) ───────────────────────────────────────
111
120
 
112
121
  /**
113
- * Reset session promise state. Only exported for test cleanup — production code
114
- * should never call this.
122
+ * Reset module-level promise state. Only exported for test cleanup —
123
+ * production code should never call this.
115
124
  */
116
125
  export function _resetPendingResolve(): void {
117
- if (_activeSession) {
118
- _activeSession.pendingResolve = null;
119
- _activeSession.pendingAgentEndQueue = [];
120
- }
121
- _activeSession = null;
126
+ _currentResolve = null;
127
+ _sessionSwitchInFlight = false;
122
128
  }
123
129
 
124
130
  /**
125
- * Set the active session for resolveAgentEnd. Only exported for test setup —
126
- * production code sets this via autoLoop entry.
131
+ * No-op for backward compatibility with tests that previously set the
132
+ * active session. The module no longer holds a session reference.
127
133
  */
128
- export function _setActiveSession(session: AutoSession | null): void {
129
- _activeSession = session;
134
+ export function _setActiveSession(_session: AutoSession | null): void {
135
+ // No-op — kept for test backward compatibility
136
+ }
137
+
138
+ // ─── detectStuck ─────────────────────────────────────────────────────────────
139
+
140
+ type WindowEntry = { key: string; error?: string };
141
+
142
+ /**
143
+ * Analyze a sliding window of recent unit dispatches for stuck patterns.
144
+ * Returns a signal with reason if stuck, null otherwise.
145
+ *
146
+ * Rule 1: Same error string twice in a row → stuck immediately.
147
+ * Rule 2: Same unit key 3+ consecutive times → stuck (preserves prior behavior).
148
+ * Rule 3: Oscillation A→B→A→B in last 4 entries → stuck.
149
+ */
150
+ export function detectStuck(
151
+ window: readonly WindowEntry[],
152
+ ): { stuck: true; reason: string } | null {
153
+ if (window.length < 2) return null;
154
+
155
+ const last = window[window.length - 1];
156
+ const prev = window[window.length - 2];
157
+
158
+ // Rule 1: Same error repeated consecutively
159
+ if (last.error && prev.error && last.error === prev.error) {
160
+ return {
161
+ stuck: true,
162
+ reason: `Same error repeated: ${last.error.slice(0, 200)}`,
163
+ };
164
+ }
165
+
166
+ // Rule 2: Same unit 3+ consecutive times
167
+ if (window.length >= 3) {
168
+ const lastThree = window.slice(-3);
169
+ if (lastThree.every((u) => u.key === last.key)) {
170
+ return {
171
+ stuck: true,
172
+ reason: `${last.key} derived 3 consecutive times without progress`,
173
+ };
174
+ }
175
+ }
176
+
177
+ // Rule 3: Oscillation (A→B→A→B in last 4)
178
+ if (window.length >= 4) {
179
+ const w = window.slice(-4);
180
+ if (
181
+ w[0].key === w[2].key &&
182
+ w[1].key === w[3].key &&
183
+ w[0].key !== w[1].key
184
+ ) {
185
+ return {
186
+ stuck: true,
187
+ reason: `Oscillation detected: ${w[0].key} ↔ ${w[1].key}`,
188
+ };
189
+ }
190
+ }
191
+
192
+ return null;
130
193
  }
131
194
 
132
195
  // ─── runUnit ─────────────────────────────────────────────────────────────────
@@ -146,45 +209,18 @@ export async function runUnit(
146
209
  unitType: string,
147
210
  unitId: string,
148
211
  prompt: string,
149
- _prefs: GSDPreferences | undefined,
150
212
  ): Promise<UnitResult> {
151
213
  debugLog("runUnit", { phase: "start", unitType, unitId });
152
214
 
153
- // ── Drain queued events from error-recovery retries ──
154
- // If an agent_end arrived between iterations (e.g. from a model fallback
155
- // sendMessage retry), consume it immediately instead of creating a new promise.
156
- // Cap queue to 3 entries to prevent unbounded growth from stale events.
157
- if (s.pendingAgentEndQueue.length > 3) {
158
- debugLog("runUnit", {
159
- phase: "queue-overflow",
160
- dropped: s.pendingAgentEndQueue.length - 1,
161
- unitType,
162
- unitId,
163
- });
164
- s.pendingAgentEndQueue = [
165
- s.pendingAgentEndQueue[s.pendingAgentEndQueue.length - 1]!,
166
- ];
167
- }
168
- if (s.pendingAgentEndQueue.length > 0) {
169
- const queued = s.pendingAgentEndQueue.shift()!;
170
- debugLog("runUnit", {
171
- phase: "drained-queued-event",
172
- unitType,
173
- unitId,
174
- queueRemaining: s.pendingAgentEndQueue.length,
175
- });
176
- return { status: "completed", event: queued };
177
- }
178
-
179
215
  // ── Session creation with timeout ──
180
216
  debugLog("runUnit", { phase: "session-create", unitType, unitId });
181
217
 
182
218
  let sessionResult: { cancelled: boolean };
183
219
  let sessionTimeoutHandle: ReturnType<typeof setTimeout> | undefined;
184
- s.sessionSwitchInFlight = true;
220
+ _sessionSwitchInFlight = true;
185
221
  try {
186
222
  const sessionPromise = s.cmdCtx!.newSession().finally(() => {
187
- s.sessionSwitchInFlight = false;
223
+ _sessionSwitchInFlight = false;
188
224
  });
189
225
  const timeoutPromise = new Promise<{ cancelled: true }>((resolve) => {
190
226
  sessionTimeoutHandle = setTimeout(
@@ -216,11 +252,12 @@ export async function runUnit(
216
252
  return { status: "cancelled" };
217
253
  }
218
254
 
219
- // ── Create the agent_end promise (session-scoped) ──
255
+ // ── Create the agent_end promise (per-unit one-shot) ──
220
256
  // This happens after newSession completes so session-switch agent_end events
221
257
  // from the previous session cannot resolve the new unit.
258
+ _sessionSwitchInFlight = false;
222
259
  const unitPromise = new Promise<UnitResult>((resolve) => {
223
- s.pendingResolve = resolve;
260
+ _currentResolve = resolve;
224
261
  });
225
262
 
226
263
  // Ensure cwd matches basePath before dispatch (#1389).
@@ -383,6 +420,7 @@ export interface LoopDeps {
383
420
  midTitle: string;
384
421
  state: GSDState;
385
422
  prefs: GSDPreferences | undefined;
423
+ session?: AutoSession;
386
424
  }) => Promise<DispatchAction>;
387
425
  runPreDispatchHooks: (
388
426
  unitType: string,
@@ -500,6 +538,7 @@ export interface LoopDeps {
500
538
  // Post-unit processing
501
539
  postUnitPreVerification: (
502
540
  pctx: PostUnitContext,
541
+ opts?: PreVerificationOpts,
503
542
  ) => Promise<"dispatched" | "continue">;
504
543
  runPostUnitVerification: (
505
544
  vctx: VerificationContext,
@@ -513,6 +552,96 @@ export interface LoopDeps {
513
552
  getSessionFile: (ctx: ExtensionContext) => string;
514
553
  }
515
554
 
555
+ // ─── generateMilestoneReport ──────────────────────────────────────────────────
556
+
557
+ /**
558
+ * Generate and write an HTML milestone report snapshot.
559
+ * Extracted from the milestone-transition block in autoLoop.
560
+ */
561
+ async function generateMilestoneReport(
562
+ s: AutoSession,
563
+ ctx: ExtensionContext,
564
+ milestoneId: string,
565
+ ): Promise<void> {
566
+ const { loadVisualizerData } = await importExtensionModule<typeof import("./visualizer-data.js")>(import.meta.url, "./visualizer-data.js");
567
+ const { generateHtmlReport } = await importExtensionModule<typeof import("./export-html.js")>(import.meta.url, "./export-html.js");
568
+ const { writeReportSnapshot } = await importExtensionModule<typeof import("./reports.js")>(import.meta.url, "./reports.js");
569
+ const { basename } = await import("node:path");
570
+
571
+ const snapData = await loadVisualizerData(s.basePath);
572
+ const completedMs = snapData.milestones.find(
573
+ (m: { id: string }) => m.id === milestoneId,
574
+ );
575
+ const msTitle = completedMs?.title ?? milestoneId;
576
+ const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
577
+ const projName = basename(s.basePath);
578
+ const doneSlices = snapData.milestones.reduce(
579
+ (acc: number, m: { slices: { done: boolean }[] }) =>
580
+ acc + m.slices.filter((sl: { done: boolean }) => sl.done).length,
581
+ 0,
582
+ );
583
+ const totalSlices = snapData.milestones.reduce(
584
+ (acc: number, m: { slices: unknown[] }) => acc + m.slices.length,
585
+ 0,
586
+ );
587
+ const outPath = writeReportSnapshot({
588
+ basePath: s.basePath,
589
+ html: generateHtmlReport(snapData, {
590
+ projectName: projName,
591
+ projectPath: s.basePath,
592
+ gsdVersion,
593
+ milestoneId,
594
+ indexRelPath: "index.html",
595
+ }),
596
+ milestoneId,
597
+ milestoneTitle: msTitle,
598
+ kind: "milestone",
599
+ projectName: projName,
600
+ projectPath: s.basePath,
601
+ gsdVersion,
602
+ totalCost: snapData.totals?.cost ?? 0,
603
+ totalTokens: snapData.totals?.tokens.total ?? 0,
604
+ totalDuration: snapData.totals?.duration ?? 0,
605
+ doneSlices,
606
+ totalSlices,
607
+ doneMilestones: snapData.milestones.filter(
608
+ (m: { status: string }) => m.status === "complete",
609
+ ).length,
610
+ totalMilestones: snapData.milestones.length,
611
+ phase: snapData.phase,
612
+ });
613
+ ctx.ui.notify(
614
+ `Report saved: .gsd/reports/${basename(outPath)} — open index.html to browse progression.`,
615
+ "info",
616
+ );
617
+ }
618
+
619
+ // ─── closeoutAndStop ──────────────────────────────────────────────────────────
620
+
621
+ /**
622
+ * If a unit is in-flight, close it out, then stop auto-mode.
623
+ * Extracted from ~4 identical if-closeout-then-stop sequences in autoLoop.
624
+ */
625
+ async function closeoutAndStop(
626
+ ctx: ExtensionContext,
627
+ pi: ExtensionAPI,
628
+ s: AutoSession,
629
+ deps: LoopDeps,
630
+ reason: string,
631
+ ): Promise<void> {
632
+ if (s.currentUnit) {
633
+ await deps.closeoutUnit(
634
+ ctx,
635
+ s.basePath,
636
+ s.currentUnit.type,
637
+ s.currentUnit.id,
638
+ s.currentUnit.startedAt,
639
+ deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
640
+ );
641
+ }
642
+ await deps.stopAuto(ctx, pi, reason);
643
+ }
644
+
516
645
  // ─── autoLoop ────────────────────────────────────────────────────────────────
517
646
 
518
647
  /**
@@ -530,10 +659,11 @@ export async function autoLoop(
530
659
  deps: LoopDeps,
531
660
  ): Promise<void> {
532
661
  debugLog("autoLoop", { phase: "enter" });
533
- _activeSession = s;
534
662
  let iteration = 0;
535
- let lastDerivedUnit = "";
536
- let sameUnitCount = 0;
663
+ // ── Sliding-window stuck detection ──
664
+ const recentUnits: Array<{ key: string; error?: string }> = [];
665
+ const STUCK_WINDOW_SIZE = 6;
666
+ let stuckRecoveryAttempts = 0;
537
667
 
538
668
  let consecutiveErrors = 0;
539
669
 
@@ -562,6 +692,19 @@ export async function autoLoop(
562
692
 
563
693
  try {
564
694
  // ── Blanket try/catch: one bad iteration must not kill the session
695
+ const prefs = deps.loadEffectiveGSDPreferences()?.preferences;
696
+
697
+ // ── Check sidecar queue before deriveState ──
698
+ let sidecarItem: SidecarItem | undefined;
699
+ if (s.sidecarQueue.length > 0) {
700
+ sidecarItem = s.sidecarQueue.shift()!;
701
+ debugLog("autoLoop", {
702
+ phase: "sidecar-dequeue",
703
+ kind: sidecarItem.kind,
704
+ unitType: sidecarItem.unitType,
705
+ unitId: sidecarItem.unitId,
706
+ });
707
+ }
565
708
 
566
709
  const sessionLockBase = deps.lockBase();
567
710
  if (sessionLockBase) {
@@ -583,6 +726,17 @@ export async function autoLoop(
583
726
  }
584
727
  }
585
728
 
729
+ // Variables shared between the sidecar and normal paths
730
+ let unitType: string;
731
+ let unitId: string;
732
+ let prompt: string;
733
+ let pauseAfterUatDispatch = false;
734
+ let state: GSDState;
735
+ let mid: string | undefined;
736
+ let midTitle: string | undefined;
737
+ let observabilityIssues: unknown[] = [];
738
+
739
+ if (!sidecarItem) {
586
740
  // ── Phase 1: Pre-dispatch ───────────────────────────────────────────
587
741
 
588
742
  // Resource version guard
@@ -633,10 +787,10 @@ export async function autoLoop(
633
787
  }
634
788
 
635
789
  // Derive state
636
- let state = await deps.deriveState(s.basePath);
637
- deps.syncCmuxSidebar(deps.loadEffectiveGSDPreferences()?.preferences, state);
638
- let mid = state.activeMilestone?.id;
639
- let midTitle = state.activeMilestone?.title;
790
+ state = await deps.deriveState(s.basePath);
791
+ deps.syncCmuxSidebar(prefs, state);
792
+ mid = state.activeMilestone?.id;
793
+ midTitle = state.activeMilestone?.title;
640
794
  debugLog("autoLoop", {
641
795
  phase: "state-derived",
642
796
  iteration,
@@ -657,68 +811,18 @@ export async function autoLoop(
657
811
  "milestone",
658
812
  );
659
813
  deps.logCmuxEvent(
660
- deps.loadEffectiveGSDPreferences()?.preferences,
814
+ prefs,
661
815
  `Milestone ${s.currentMilestoneId} complete. Advancing to ${mid}.`,
662
816
  "success",
663
817
  );
664
818
 
665
- const vizPrefs = deps.loadEffectiveGSDPreferences()?.preferences;
819
+ const vizPrefs = prefs;
666
820
  if (vizPrefs?.auto_visualize) {
667
821
  ctx.ui.notify("Run /gsd visualize to see progress overview.", "info");
668
822
  }
669
823
  if (vizPrefs?.auto_report !== false) {
670
824
  try {
671
- const { loadVisualizerData } = await import("./visualizer-data.js");
672
- const { generateHtmlReport } = await import("./export-html.js");
673
- const { writeReportSnapshot } = await import("./reports.js");
674
- const { basename } = await import("node:path");
675
- const snapData = await loadVisualizerData(s.basePath);
676
- const completedMs = snapData.milestones.find(
677
- (m: { id: string }) => m.id === s.currentMilestoneId,
678
- );
679
- const msTitle = completedMs?.title ?? s.currentMilestoneId;
680
- const gsdVersion = process.env.GSD_VERSION ?? "0.0.0";
681
- const projName = basename(s.basePath);
682
- const doneSlices = snapData.milestones.reduce(
683
- (acc: number, m: { slices: { done: boolean }[] }) =>
684
- acc +
685
- m.slices.filter((sl: { done: boolean }) => sl.done).length,
686
- 0,
687
- );
688
- const totalSlices = snapData.milestones.reduce(
689
- (acc: number, m: { slices: unknown[] }) => acc + m.slices.length,
690
- 0,
691
- );
692
- const outPath = writeReportSnapshot({
693
- basePath: s.basePath,
694
- html: generateHtmlReport(snapData, {
695
- projectName: projName,
696
- projectPath: s.basePath,
697
- gsdVersion,
698
- milestoneId: s.currentMilestoneId,
699
- indexRelPath: "index.html",
700
- }),
701
- milestoneId: s.currentMilestoneId!,
702
- milestoneTitle: msTitle,
703
- kind: "milestone",
704
- projectName: projName,
705
- projectPath: s.basePath,
706
- gsdVersion,
707
- totalCost: snapData.totals?.cost ?? 0,
708
- totalTokens: snapData.totals?.tokens.total ?? 0,
709
- totalDuration: snapData.totals?.duration ?? 0,
710
- doneSlices,
711
- totalSlices,
712
- doneMilestones: snapData.milestones.filter(
713
- (m: { status: string }) => m.status === "complete",
714
- ).length,
715
- totalMilestones: snapData.milestones.length,
716
- phase: snapData.phase,
717
- });
718
- ctx.ui.notify(
719
- `Report saved: .gsd/reports/${(await import("node:path")).basename(outPath)} — open index.html to browse progression.`,
720
- "info",
721
- );
825
+ await generateMilestoneReport(s, ctx, s.currentMilestoneId!);
722
826
  } catch (err) {
723
827
  ctx.ui.notify(
724
828
  `Report generation failed: ${err instanceof Error ? err.message : String(err)}`,
@@ -731,8 +835,8 @@ export async function autoLoop(
731
835
  s.unitDispatchCount.clear();
732
836
  s.unitRecoveryCount.clear();
733
837
  s.unitLifetimeDispatches.clear();
734
- lastDerivedUnit = "";
735
- sameUnitCount = 0;
838
+ recentUnits.length = 0;
839
+ stuckRecoveryAttempts = 0;
736
840
 
737
841
  // Worktree lifecycle on milestone transition — merge current, enter next
738
842
  deps.resolver.mergeAndExit(s.currentMilestoneId!, ctx.ui);
@@ -745,9 +849,7 @@ export async function autoLoop(
745
849
  if (mid) {
746
850
  if (deps.getIsolationMode() !== "none") {
747
851
  deps.captureIntegrationBranch(s.basePath, mid, {
748
- commitDocs:
749
- deps.loadEffectiveGSDPreferences()?.preferences?.git
750
- ?.commit_docs,
852
+ commitDocs: prefs?.git?.commit_docs,
751
853
  });
752
854
  }
753
855
  deps.resolver.enterMilestone(mid, ctx.ui);
@@ -787,7 +889,7 @@ export async function autoLoop(
787
889
  (m: { status: string }) =>
788
890
  m.status !== "complete" && m.status !== "parked",
789
891
  );
790
- if (incomplete.length === 0) {
892
+ if (incomplete.length === 0 && state.registry.length > 0) {
791
893
  // All milestones complete — merge milestone branch before stopping
792
894
  if (s.currentMilestoneId) {
793
895
  deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
@@ -799,17 +901,29 @@ export async function autoLoop(
799
901
  "milestone",
800
902
  );
801
903
  deps.logCmuxEvent(
802
- deps.loadEffectiveGSDPreferences()?.preferences,
904
+ prefs,
803
905
  "All milestones complete.",
804
906
  "success",
805
907
  );
806
908
  await deps.stopAuto(ctx, pi, "All milestones complete");
909
+ } else if (incomplete.length === 0 && state.registry.length === 0) {
910
+ // Empty registry — no milestones visible, likely a path resolution bug
911
+ const diag = `basePath=${s.basePath}, phase=${state.phase}`;
912
+ ctx.ui.notify(
913
+ `No milestones visible in current scope. Possible path resolution issue.\n Diagnostic: ${diag}`,
914
+ "error",
915
+ );
916
+ await deps.stopAuto(
917
+ ctx,
918
+ pi,
919
+ `No milestones found — check basePath resolution`,
920
+ );
807
921
  } else if (state.phase === "blocked") {
808
922
  const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
809
923
  await deps.stopAuto(ctx, pi, blockerMsg);
810
924
  ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
811
925
  deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
812
- deps.logCmuxEvent(deps.loadEffectiveGSDPreferences()?.preferences, blockerMsg, "error");
926
+ deps.logCmuxEvent(prefs, blockerMsg, "error");
813
927
  } else {
814
928
  const ids = incomplete.map((m: { id: string }) => m.id).join(", ");
815
929
  const diag = `basePath=${s.basePath}, milestones=[${state.registry.map((m: { id: string; status: string }) => `${m.id}:${m.status}`).join(", ")}], phase=${state.phase}`;
@@ -844,20 +958,10 @@ export async function autoLoop(
844
958
  }
845
959
 
846
960
  if (!mid || !midTitle) {
847
- if (s.currentUnit) {
848
- await deps.closeoutUnit(
849
- ctx,
850
- s.basePath,
851
- s.currentUnit.type,
852
- s.currentUnit.id,
853
- s.currentUnit.startedAt,
854
- deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
855
- );
856
- }
857
961
  const noMilestoneReason = !mid
858
962
  ? "No active milestone after merge reconciliation"
859
963
  : `Milestone ${mid} has no title after reconciliation`;
860
- await deps.stopAuto(ctx, pi, noMilestoneReason);
964
+ await closeoutAndStop(ctx, pi, s, deps, noMilestoneReason);
861
965
  debugLog("autoLoop", {
862
966
  phase: "exit",
863
967
  reason: "no-milestone-after-reconciliation",
@@ -867,17 +971,7 @@ export async function autoLoop(
867
971
 
868
972
  // Terminal: complete
869
973
  if (state.phase === "complete") {
870
- if (s.currentUnit) {
871
- await deps.closeoutUnit(
872
- ctx,
873
- s.basePath,
874
- s.currentUnit.type,
875
- s.currentUnit.id,
876
- s.currentUnit.startedAt,
877
- deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
878
- );
879
- }
880
- // Milestone merge on complete
974
+ // Milestone merge on complete (before closeout so branch state is clean)
881
975
  if (s.currentMilestoneId) {
882
976
  deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
883
977
  }
@@ -888,40 +982,28 @@ export async function autoLoop(
888
982
  "milestone",
889
983
  );
890
984
  deps.logCmuxEvent(
891
- deps.loadEffectiveGSDPreferences()?.preferences,
985
+ prefs,
892
986
  `Milestone ${mid} complete.`,
893
987
  "success",
894
988
  );
895
- await deps.stopAuto(ctx, pi, `Milestone ${mid} complete`);
989
+ await closeoutAndStop(ctx, pi, s, deps, `Milestone ${mid} complete`);
896
990
  debugLog("autoLoop", { phase: "exit", reason: "milestone-complete" });
897
991
  break;
898
992
  }
899
993
 
900
994
  // Terminal: blocked
901
995
  if (state.phase === "blocked") {
902
- if (s.currentUnit) {
903
- await deps.closeoutUnit(
904
- ctx,
905
- s.basePath,
906
- s.currentUnit.type,
907
- s.currentUnit.id,
908
- s.currentUnit.startedAt,
909
- deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
910
- );
911
- }
912
996
  const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
913
- await deps.stopAuto(ctx, pi, blockerMsg);
997
+ await closeoutAndStop(ctx, pi, s, deps, blockerMsg);
914
998
  ctx.ui.notify(`${blockerMsg}. Fix and run /gsd auto.`, "warning");
915
999
  deps.sendDesktopNotification("GSD", blockerMsg, "error", "attention");
916
- deps.logCmuxEvent(deps.loadEffectiveGSDPreferences()?.preferences, blockerMsg, "error");
1000
+ deps.logCmuxEvent(prefs, blockerMsg, "error");
917
1001
  debugLog("autoLoop", { phase: "exit", reason: "blocked" });
918
1002
  break;
919
1003
  }
920
1004
 
921
1005
  // ── Phase 2: Guards ─────────────────────────────────────────────────
922
1006
 
923
- const prefs = deps.loadEffectiveGSDPreferences()?.preferences;
924
-
925
1007
  // Budget ceiling guard
926
1008
  const budgetCeiling = prefs?.budget_ceiling;
927
1009
  if (budgetCeiling !== undefined && budgetCeiling > 0) {
@@ -941,84 +1023,49 @@ export async function autoLoop(
941
1023
  budgetPct,
942
1024
  );
943
1025
 
944
- if (newBudgetAlertLevel === 100 && budgetEnforcementAction !== "none") {
945
- const msg = `Budget ceiling ${deps.formatCost(budgetCeiling)} reached (spent ${deps.formatCost(totalCost)}).`;
1026
+ // Data-driven threshold check loop descending, fire first match
1027
+ const threshold = BUDGET_THRESHOLDS.find(
1028
+ (t) => newBudgetAlertLevel >= t.pct,
1029
+ );
1030
+ if (threshold) {
946
1031
  s.lastBudgetAlertLevel =
947
1032
  newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
948
- if (budgetEnforcementAction === "halt") {
949
- deps.sendDesktopNotification("GSD", msg, "error", "budget");
950
- await deps.stopAuto(ctx, pi, "Budget ceiling reached");
951
- debugLog("autoLoop", { phase: "exit", reason: "budget-halt" });
952
- break;
953
- }
954
- if (budgetEnforcementAction === "pause") {
955
- ctx.ui.notify(
956
- `${msg} Pausing auto-mode — /gsd auto to override and continue.`,
957
- "warning",
958
- );
1033
+
1034
+ if (threshold.pct === 100 && budgetEnforcementAction !== "none") {
1035
+ // 100% special enforcement logic (halt/pause/warn)
1036
+ const msg = `Budget ceiling ${deps.formatCost(budgetCeiling)} reached (spent ${deps.formatCost(totalCost)}).`;
1037
+ if (budgetEnforcementAction === "halt") {
1038
+ deps.sendDesktopNotification("GSD", msg, "error", "budget");
1039
+ await deps.stopAuto(ctx, pi, "Budget ceiling reached");
1040
+ debugLog("autoLoop", { phase: "exit", reason: "budget-halt" });
1041
+ break;
1042
+ }
1043
+ if (budgetEnforcementAction === "pause") {
1044
+ ctx.ui.notify(
1045
+ `${msg} Pausing auto-mode — /gsd auto to override and continue.`,
1046
+ "warning",
1047
+ );
1048
+ deps.sendDesktopNotification("GSD", msg, "warning", "budget");
1049
+ deps.logCmuxEvent(prefs, msg, "warning");
1050
+ await deps.pauseAuto(ctx, pi);
1051
+ debugLog("autoLoop", { phase: "exit", reason: "budget-pause" });
1052
+ break;
1053
+ }
1054
+ ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
959
1055
  deps.sendDesktopNotification("GSD", msg, "warning", "budget");
960
1056
  deps.logCmuxEvent(prefs, msg, "warning");
961
- await deps.pauseAuto(ctx, pi);
962
- debugLog("autoLoop", { phase: "exit", reason: "budget-pause" });
963
- break;
1057
+ } else if (threshold.pct < 100) {
1058
+ // Sub-100% simple notification
1059
+ const msg = `${threshold.label}: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`;
1060
+ ctx.ui.notify(msg, threshold.notifyLevel);
1061
+ deps.sendDesktopNotification(
1062
+ "GSD",
1063
+ msg,
1064
+ threshold.notifyLevel,
1065
+ "budget",
1066
+ );
1067
+ deps.logCmuxEvent(prefs, msg, threshold.cmuxLevel);
964
1068
  }
965
- ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
966
- deps.sendDesktopNotification("GSD", msg, "warning", "budget");
967
- deps.logCmuxEvent(prefs, msg, "warning");
968
- } else if (newBudgetAlertLevel === 90) {
969
- s.lastBudgetAlertLevel =
970
- newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
971
- ctx.ui.notify(
972
- `Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
973
- "warning",
974
- );
975
- deps.sendDesktopNotification(
976
- "GSD",
977
- `Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
978
- "warning",
979
- "budget",
980
- );
981
- deps.logCmuxEvent(
982
- prefs,
983
- `Budget 90%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
984
- "warning",
985
- );
986
- } else if (newBudgetAlertLevel === 80) {
987
- s.lastBudgetAlertLevel =
988
- newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
989
- ctx.ui.notify(
990
- `Approaching budget ceiling — 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
991
- "warning",
992
- );
993
- deps.sendDesktopNotification(
994
- "GSD",
995
- `Approaching budget ceiling — 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
996
- "warning",
997
- "budget",
998
- );
999
- deps.logCmuxEvent(
1000
- prefs,
1001
- `Budget 80%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
1002
- "warning",
1003
- );
1004
- } else if (newBudgetAlertLevel === 75) {
1005
- s.lastBudgetAlertLevel =
1006
- newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
1007
- ctx.ui.notify(
1008
- `Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
1009
- "info",
1010
- );
1011
- deps.sendDesktopNotification(
1012
- "GSD",
1013
- `Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
1014
- "info",
1015
- "budget",
1016
- );
1017
- deps.logCmuxEvent(
1018
- prefs,
1019
- `Budget 75%: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`,
1020
- "progress",
1021
- );
1022
1069
  } else if (budgetAlertLevel === 0) {
1023
1070
  s.lastBudgetAlertLevel = 0;
1024
1071
  }
@@ -1091,20 +1138,11 @@ export async function autoLoop(
1091
1138
  midTitle: midTitle!,
1092
1139
  state,
1093
1140
  prefs,
1141
+ session: s,
1094
1142
  });
1095
1143
 
1096
1144
  if (dispatchResult.action === "stop") {
1097
- if (s.currentUnit) {
1098
- await deps.closeoutUnit(
1099
- ctx,
1100
- s.basePath,
1101
- s.currentUnit.type,
1102
- s.currentUnit.id,
1103
- s.currentUnit.startedAt,
1104
- deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
1105
- );
1106
- }
1107
- await deps.stopAuto(ctx, pi, dispatchResult.reason);
1145
+ await closeoutAndStop(ctx, pi, s, deps, dispatchResult.reason);
1108
1146
  debugLog("autoLoop", { phase: "exit", reason: "dispatch-stop" });
1109
1147
  break;
1110
1148
  }
@@ -1115,76 +1153,84 @@ export async function autoLoop(
1115
1153
  continue;
1116
1154
  }
1117
1155
 
1118
- let unitType = dispatchResult.unitType;
1119
- let unitId = dispatchResult.unitId;
1120
- let prompt = dispatchResult.prompt;
1121
- const pauseAfterUatDispatch = dispatchResult.pauseAfterDispatch ?? false;
1156
+ unitType = dispatchResult.unitType;
1157
+ unitId = dispatchResult.unitId;
1158
+ prompt = dispatchResult.prompt;
1159
+ pauseAfterUatDispatch = dispatchResult.pauseAfterDispatch ?? false;
1122
1160
 
1123
- // ── Same-unit stuck counter with graduated recovery ──
1161
+ // ── Sliding-window stuck detection with graduated recovery ──
1124
1162
  const derivedKey = `${unitType}/${unitId}`;
1125
- if (derivedKey === lastDerivedUnit && !s.pendingVerificationRetry) {
1126
- sameUnitCount++;
1127
- debugLog("autoLoop", {
1128
- phase: "stuck-check",
1129
- unitType,
1130
- unitId,
1131
- sameUnitCount,
1132
- });
1133
1163
 
1134
- if (sameUnitCount === 3) {
1135
- // Level 1: try verifying the artifact — maybe it was written but not detected
1136
- const artifactExists = deps.verifyExpectedArtifact(
1164
+ if (!s.pendingVerificationRetry) {
1165
+ recentUnits.push({ key: derivedKey });
1166
+ if (recentUnits.length > STUCK_WINDOW_SIZE) recentUnits.shift();
1167
+
1168
+ const stuckSignal = detectStuck(recentUnits);
1169
+ if (stuckSignal) {
1170
+ debugLog("autoLoop", {
1171
+ phase: "stuck-check",
1137
1172
  unitType,
1138
1173
  unitId,
1139
- s.basePath,
1140
- );
1141
- if (artifactExists) {
1174
+ reason: stuckSignal.reason,
1175
+ recoveryAttempts: stuckRecoveryAttempts,
1176
+ });
1177
+
1178
+ if (stuckRecoveryAttempts === 0) {
1179
+ // Level 1: try verifying the artifact, then cache invalidation + retry
1180
+ stuckRecoveryAttempts++;
1181
+ const artifactExists = deps.verifyExpectedArtifact(
1182
+ unitType,
1183
+ unitId,
1184
+ s.basePath,
1185
+ );
1186
+ if (artifactExists) {
1187
+ debugLog("autoLoop", {
1188
+ phase: "stuck-recovery",
1189
+ level: 1,
1190
+ action: "artifact-found",
1191
+ });
1192
+ ctx.ui.notify(
1193
+ `Stuck recovery: artifact for ${unitType} ${unitId} found on disk. Invalidating caches.`,
1194
+ "info",
1195
+ );
1196
+ deps.invalidateAllCaches();
1197
+ continue;
1198
+ }
1199
+ ctx.ui.notify(
1200
+ `Stuck on ${unitType} ${unitId} (${stuckSignal.reason}). Invalidating caches and retrying.`,
1201
+ "warning",
1202
+ );
1203
+ deps.invalidateAllCaches();
1204
+ } else {
1205
+ // Level 2: hard stop — genuinely stuck
1142
1206
  debugLog("autoLoop", {
1143
- phase: "stuck-recovery",
1144
- level: 1,
1145
- action: "artifact-found",
1207
+ phase: "stuck-detected",
1208
+ unitType,
1209
+ unitId,
1210
+ reason: stuckSignal.reason,
1146
1211
  });
1212
+ await deps.stopAuto(
1213
+ ctx,
1214
+ pi,
1215
+ `Stuck: ${stuckSignal.reason}`,
1216
+ );
1147
1217
  ctx.ui.notify(
1148
- `Stuck recovery: artifact for ${unitType} ${unitId} found on disk. Invalidating caches.`,
1149
- "info",
1218
+ `Stuck on ${unitType} ${unitId} ${stuckSignal.reason}. The expected artifact was not written.`,
1219
+ "error",
1150
1220
  );
1151
- deps.invalidateAllCaches();
1152
- continue;
1221
+ break;
1222
+ }
1223
+ } else {
1224
+ // Progress detected — reset recovery counter
1225
+ if (stuckRecoveryAttempts > 0) {
1226
+ debugLog("autoLoop", {
1227
+ phase: "stuck-counter-reset",
1228
+ from: recentUnits[recentUnits.length - 2]?.key ?? "",
1229
+ to: derivedKey,
1230
+ });
1231
+ stuckRecoveryAttempts = 0;
1153
1232
  }
1154
- ctx.ui.notify(
1155
- `Stuck on ${unitType} ${unitId} (attempt ${sameUnitCount}). Invalidating caches and retrying.`,
1156
- "warning",
1157
- );
1158
- deps.invalidateAllCaches();
1159
- } else if (sameUnitCount === 5) {
1160
- // Level 2: hard stop — genuinely stuck
1161
- debugLog("autoLoop", {
1162
- phase: "stuck-detected",
1163
- unitType,
1164
- unitId,
1165
- sameUnitCount,
1166
- });
1167
- await deps.stopAuto(
1168
- ctx,
1169
- pi,
1170
- `Stuck: ${unitType} ${unitId} derived ${sameUnitCount} consecutive times without progress`,
1171
- );
1172
- ctx.ui.notify(
1173
- `Stuck on ${unitType} ${unitId} — deriveState returns the same unit after ${sameUnitCount} attempts. The expected artifact was not written.`,
1174
- "error",
1175
- );
1176
- break;
1177
- }
1178
- } else {
1179
- if (derivedKey !== lastDerivedUnit) {
1180
- debugLog("autoLoop", {
1181
- phase: "stuck-counter-reset",
1182
- from: lastDerivedUnit,
1183
- to: derivedKey,
1184
- });
1185
1233
  }
1186
- lastDerivedUnit = derivedKey;
1187
- sameUnitCount = 0;
1188
1234
  }
1189
1235
 
1190
1236
  // Pre-dispatch hooks
@@ -1227,13 +1273,27 @@ export async function autoLoop(
1227
1273
  break;
1228
1274
  }
1229
1275
 
1230
- const observabilityIssues = await deps.collectObservabilityWarnings(
1276
+ observabilityIssues = await deps.collectObservabilityWarnings(
1231
1277
  ctx,
1232
1278
  s.basePath,
1233
1279
  unitType,
1234
1280
  unitId,
1235
1281
  );
1236
1282
 
1283
+ // Derive state for shared use in execution phase
1284
+ // (state, mid, midTitle already set above)
1285
+
1286
+ } else {
1287
+ // ── Sidecar path: use values from the sidecar item directly ──
1288
+ unitType = sidecarItem.unitType;
1289
+ unitId = sidecarItem.unitId;
1290
+ prompt = sidecarItem.prompt;
1291
+ // Derive minimal state for progress widget / execution context
1292
+ state = await deps.deriveState(s.basePath);
1293
+ mid = state.activeMilestone?.id;
1294
+ midTitle = state.activeMilestone?.title;
1295
+ }
1296
+
1237
1297
  // ── Phase 4: Unit execution ─────────────────────────────────────────
1238
1298
 
1239
1299
  debugLog("autoLoop", {
@@ -1251,61 +1311,6 @@ export async function autoLoop(
1251
1311
  );
1252
1312
  const previousTier = s.currentUnitRouting?.tier;
1253
1313
 
1254
- // Closeout previous unit
1255
- if (s.currentUnit) {
1256
- await deps.closeoutUnit(
1257
- ctx,
1258
- s.basePath,
1259
- s.currentUnit.type,
1260
- s.currentUnit.id,
1261
- s.currentUnit.startedAt,
1262
- deps.buildSnapshotOpts(s.currentUnit.type, s.currentUnit.id),
1263
- );
1264
-
1265
- if (s.currentUnitRouting) {
1266
- const isRetry =
1267
- s.currentUnit.type === unitType && s.currentUnit.id === unitId;
1268
- deps.recordOutcome(
1269
- s.currentUnit.type,
1270
- s.currentUnitRouting.tier as "light" | "standard" | "heavy",
1271
- !isRetry,
1272
- );
1273
- }
1274
-
1275
- const closeoutKey = `${s.currentUnit.type}/${s.currentUnit.id}`;
1276
- const incomingKey = `${unitType}/${unitId}`;
1277
- const isHookUnit = s.currentUnit.type.startsWith("hook/");
1278
- const artifactVerified =
1279
- isHookUnit ||
1280
- deps.verifyExpectedArtifact(
1281
- s.currentUnit.type,
1282
- s.currentUnit.id,
1283
- s.basePath,
1284
- );
1285
- if (closeoutKey !== incomingKey && artifactVerified) {
1286
- s.completedUnits.push({
1287
- type: s.currentUnit.type,
1288
- id: s.currentUnit.id,
1289
- startedAt: s.currentUnit.startedAt,
1290
- finishedAt: Date.now(),
1291
- });
1292
- if (s.completedUnits.length > 200) {
1293
- s.completedUnits = s.completedUnits.slice(-200);
1294
- }
1295
- deps.clearUnitRuntimeRecord(
1296
- s.basePath,
1297
- s.currentUnit.type,
1298
- s.currentUnit.id,
1299
- );
1300
- s.unitDispatchCount.delete(
1301
- `${s.currentUnit.type}/${s.currentUnit.id}`,
1302
- );
1303
- s.unitRecoveryCount.delete(
1304
- `${s.currentUnit.type}/${s.currentUnit.id}`,
1305
- );
1306
- }
1307
- }
1308
-
1309
1314
  s.currentUnit = { type: unitType, id: unitId, startedAt: Date.now() };
1310
1315
  deps.captureAvailableSkills();
1311
1316
  deps.writeUnitRuntimeRecord(
@@ -1332,7 +1337,6 @@ export async function autoLoop(
1332
1337
  deps.ensurePreconditions(unitType, unitId, s.basePath, state);
1333
1338
 
1334
1339
  // Prompt injection
1335
- const MAX_RECOVERY_CHARS = 50_000;
1336
1340
  let finalPrompt = prompt;
1337
1341
 
1338
1342
  if (s.pendingVerificationRetry) {
@@ -1377,7 +1381,7 @@ export async function autoLoop(
1377
1381
  s.lastBaselineCharCount = undefined;
1378
1382
  if (deps.isDbAvailable()) {
1379
1383
  try {
1380
- const { inlineGsdRootFile } = await import("./auto-prompts.js");
1384
+ const { inlineGsdRootFile } = await importExtensionModule<typeof import("./auto-prompts.js")>(import.meta.url, "./auto-prompts.js");
1381
1385
  const [decisionsContent, requirementsContent, projectContent] =
1382
1386
  await Promise.all([
1383
1387
  inlineGsdRootFile(s.basePath, "decisions.md", "Decisions"),
@@ -1404,7 +1408,7 @@ export async function autoLoop(
1404
1408
  );
1405
1409
  }
1406
1410
 
1407
- // Select and apply model (with tier escalation on retry)
1411
+ // Select and apply model (with tier escalation on retry — normal units only)
1408
1412
  const modelResult = await deps.selectAndApplyModel(
1409
1413
  ctx,
1410
1414
  pi,
@@ -1414,7 +1418,7 @@ export async function autoLoop(
1414
1418
  prefs,
1415
1419
  s.verbose,
1416
1420
  s.autoModeStartModel,
1417
- { isRetry, previousTier },
1421
+ sidecarItem ? undefined : { isRetry, previousTier },
1418
1422
  );
1419
1423
  s.currentUnitRouting =
1420
1424
  modelResult.routing as AutoSession["currentUnitRouting"];
@@ -1463,7 +1467,6 @@ export async function autoLoop(
1463
1467
  unitType,
1464
1468
  unitId,
1465
1469
  finalPrompt,
1466
- prefs,
1467
1470
  );
1468
1471
  debugLog("autoLoop", {
1469
1472
  phase: "runUnit-end",
@@ -1473,6 +1476,23 @@ export async function autoLoop(
1473
1476
  status: unitResult.status,
1474
1477
  });
1475
1478
 
1479
+ // Tag the most recent window entry with error info for stuck detection
1480
+ if (unitResult.status === "error" || unitResult.status === "cancelled") {
1481
+ const lastEntry = recentUnits[recentUnits.length - 1];
1482
+ if (lastEntry) {
1483
+ lastEntry.error = `${unitResult.status}:${unitType}/${unitId}`;
1484
+ }
1485
+ } else if (unitResult.event?.messages?.length) {
1486
+ const lastMsg = unitResult.event.messages[unitResult.event.messages.length - 1];
1487
+ const msgStr = typeof lastMsg === "string" ? lastMsg : JSON.stringify(lastMsg);
1488
+ if (/error|fail|exception/i.test(msgStr)) {
1489
+ const lastEntry = recentUnits[recentUnits.length - 1];
1490
+ if (lastEntry) {
1491
+ lastEntry.error = msgStr.slice(0, 200);
1492
+ }
1493
+ }
1494
+ }
1495
+
1476
1496
  if (unitResult.status === "cancelled") {
1477
1497
  ctx.ui.notify(
1478
1498
  `Session creation timed out or was cancelled for ${unitType} ${unitId}. Will retry.`,
@@ -1483,6 +1503,52 @@ export async function autoLoop(
1483
1503
  break;
1484
1504
  }
1485
1505
 
1506
+ // ── Immediate unit closeout (metrics, activity log, memory) ────────
1507
+ // Run right after runUnit() returns so telemetry is never lost to a
1508
+ // crash between iterations.
1509
+ await deps.closeoutUnit(
1510
+ ctx,
1511
+ s.basePath,
1512
+ unitType,
1513
+ unitId,
1514
+ s.currentUnit.startedAt,
1515
+ deps.buildSnapshotOpts(unitType, unitId),
1516
+ );
1517
+
1518
+ if (s.currentUnitRouting) {
1519
+ deps.recordOutcome(
1520
+ unitType,
1521
+ s.currentUnitRouting.tier as "light" | "standard" | "heavy",
1522
+ true, // success assumed; dispatch will re-dispatch if artifact missing
1523
+ );
1524
+ }
1525
+
1526
+ const isHookUnit = unitType.startsWith("hook/");
1527
+ const artifactVerified =
1528
+ isHookUnit ||
1529
+ deps.verifyExpectedArtifact(unitType, unitId, s.basePath);
1530
+ if (artifactVerified) {
1531
+ s.completedUnits.push({
1532
+ type: unitType,
1533
+ id: unitId,
1534
+ startedAt: s.currentUnit.startedAt,
1535
+ finishedAt: Date.now(),
1536
+ });
1537
+ if (s.completedUnits.length > 200) {
1538
+ s.completedUnits = s.completedUnits.slice(-200);
1539
+ }
1540
+ // Flush completed-units to disk so the record survives crashes
1541
+ try {
1542
+ const completedKeysPath = join(gsdRoot(s.basePath), "completed-units.json");
1543
+ const keys = s.completedUnits.map((u) => `${u.type}/${u.id}`);
1544
+ atomicWriteSync(completedKeysPath, JSON.stringify(keys, null, 2));
1545
+ } catch { /* non-fatal: disk flush failure */ }
1546
+
1547
+ deps.clearUnitRuntimeRecord(s.basePath, unitType, unitId);
1548
+ s.unitDispatchCount.delete(`${unitType}/${unitId}`);
1549
+ s.unitRecoveryCount.delete(`${unitType}/${unitId}`);
1550
+ }
1551
+
1486
1552
  // ── Phase 5: Finalize ───────────────────────────────────────────────
1487
1553
 
1488
1554
  debugLog("autoLoop", { phase: "finalize", iteration });
@@ -1503,7 +1569,13 @@ export async function autoLoop(
1503
1569
  };
1504
1570
 
1505
1571
  // Pre-verification processing (commit, doctor, state rebuild, etc.)
1506
- const preResult = await deps.postUnitPreVerification(postUnitCtx);
1572
+ // Sidecar items use lightweight pre-verification opts
1573
+ const preVerificationOpts: PreVerificationOpts | undefined = sidecarItem
1574
+ ? sidecarItem.kind === "hook"
1575
+ ? { skipSettleDelay: true, skipDoctor: true, skipStateRebuild: true, skipWorktreeSync: true }
1576
+ : { skipSettleDelay: true, skipStateRebuild: true }
1577
+ : undefined;
1578
+ const preResult = await deps.postUnitPreVerification(postUnitCtx, preVerificationOpts);
1507
1579
  if (preResult === "dispatched") {
1508
1580
  debugLog("autoLoop", {
1509
1581
  phase: "exit",
@@ -1522,22 +1594,32 @@ export async function autoLoop(
1522
1594
  break;
1523
1595
  }
1524
1596
 
1525
- // Verification gate — the loop handles retries via s.pendingVerificationRetry
1526
- const verificationResult = await deps.runPostUnitVerification(
1527
- { s, ctx, pi },
1528
- deps.pauseAuto,
1529
- );
1597
+ // Verification gate
1598
+ // Hook sidecar items skip verification entirely.
1599
+ // Non-hook sidecar items run verification but skip retries (just continue).
1600
+ const skipVerification = sidecarItem?.kind === "hook";
1601
+ if (!skipVerification) {
1602
+ const verificationResult = await deps.runPostUnitVerification(
1603
+ { s, ctx, pi },
1604
+ deps.pauseAuto,
1605
+ );
1530
1606
 
1531
- if (verificationResult === "pause") {
1532
- debugLog("autoLoop", { phase: "exit", reason: "verification-pause" });
1533
- break;
1534
- }
1607
+ if (verificationResult === "pause") {
1608
+ debugLog("autoLoop", { phase: "exit", reason: "verification-pause" });
1609
+ break;
1610
+ }
1535
1611
 
1536
- if (verificationResult === "retry") {
1537
- // s.pendingVerificationRetry was set by runPostUnitVerification.
1538
- // Continue the loop next iteration will inject the retry context into the prompt.
1539
- debugLog("autoLoop", { phase: "verification-retry", iteration });
1540
- continue;
1612
+ if (verificationResult === "retry") {
1613
+ if (sidecarItem) {
1614
+ // Sidecar verification retries are skipped just continue
1615
+ debugLog("autoLoop", { phase: "sidecar-verification-retry-skipped", iteration });
1616
+ } else {
1617
+ // s.pendingVerificationRetry was set by runPostUnitVerification.
1618
+ // Continue the loop — next iteration will inject the retry context into the prompt.
1619
+ debugLog("autoLoop", { phase: "verification-retry", iteration });
1620
+ continue;
1621
+ }
1622
+ }
1541
1623
  }
1542
1624
 
1543
1625
  // Post-verification processing (DB dual-write, hooks, triage, quick-tasks)
@@ -1557,150 +1639,6 @@ export async function autoLoop(
1557
1639
  break;
1558
1640
  }
1559
1641
 
1560
- // ── Sidecar drain: dispatch enqueued hooks/triage/quick-tasks ──
1561
- let sidecarBroke = false;
1562
- while (s.sidecarQueue.length > 0 && s.active) {
1563
- const item = s.sidecarQueue.shift()!;
1564
- debugLog("autoLoop", {
1565
- phase: "sidecar-dequeue",
1566
- kind: item.kind,
1567
- unitType: item.unitType,
1568
- unitId: item.unitId,
1569
- });
1570
-
1571
- // Set up as current unit
1572
- const sidecarStartedAt = Date.now();
1573
- s.currentUnit = {
1574
- type: item.unitType,
1575
- id: item.unitId,
1576
- startedAt: sidecarStartedAt,
1577
- };
1578
- deps.writeUnitRuntimeRecord(
1579
- s.basePath,
1580
- item.unitType,
1581
- item.unitId,
1582
- sidecarStartedAt,
1583
- {
1584
- phase: "dispatched",
1585
- wrapupWarningSent: false,
1586
- timeoutAt: null,
1587
- lastProgressAt: sidecarStartedAt,
1588
- progressCount: 0,
1589
- lastProgressKind: "dispatch",
1590
- },
1591
- );
1592
-
1593
- // Model selection (handles hook model override)
1594
- await deps.selectAndApplyModel(
1595
- ctx,
1596
- pi,
1597
- item.unitType,
1598
- item.unitId,
1599
- s.basePath,
1600
- prefs,
1601
- s.verbose,
1602
- s.autoModeStartModel,
1603
- );
1604
-
1605
- // Supervision
1606
- deps.clearUnitTimeout();
1607
- deps.startUnitSupervision({
1608
- s,
1609
- ctx,
1610
- pi,
1611
- unitType: item.unitType,
1612
- unitId: item.unitId,
1613
- prefs,
1614
- buildSnapshotOpts: () =>
1615
- deps.buildSnapshotOpts(item.unitType, item.unitId),
1616
- buildRecoveryContext: () => ({}),
1617
- pauseAuto: deps.pauseAuto,
1618
- });
1619
-
1620
- // Write lock
1621
- const sidecarSessionFile = deps.getSessionFile(ctx);
1622
- deps.writeLock(
1623
- deps.lockBase(),
1624
- item.unitType,
1625
- item.unitId,
1626
- s.completedUnits.length,
1627
- sidecarSessionFile,
1628
- );
1629
-
1630
- // Execute via standard runUnit
1631
- const sidecarResult = await runUnit(
1632
- ctx,
1633
- pi,
1634
- s,
1635
- item.unitType,
1636
- item.unitId,
1637
- item.prompt,
1638
- prefs,
1639
- );
1640
- deps.clearUnitTimeout();
1641
-
1642
- if (sidecarResult.status === "cancelled") {
1643
- ctx.ui.notify(
1644
- `Sidecar unit ${item.unitType} ${item.unitId} session cancelled. Stopping.`,
1645
- "warning",
1646
- );
1647
- await deps.stopAuto(ctx, pi, "Sidecar session creation failed");
1648
- sidecarBroke = true;
1649
- break;
1650
- }
1651
-
1652
- // Run pre-verification for the sidecar unit
1653
- const sidecarPreResult =
1654
- await deps.postUnitPreVerification(postUnitCtx);
1655
- if (sidecarPreResult === "dispatched") {
1656
- // Pre-verification caused stop/pause
1657
- debugLog("autoLoop", {
1658
- phase: "exit",
1659
- reason: "sidecar-pre-verification-stop",
1660
- });
1661
- sidecarBroke = true;
1662
- break;
1663
- }
1664
-
1665
- // Verification gate for non-hook sidecar units (triage, quick-tasks)
1666
- // Hook units are lightweight and don't need verification.
1667
- if (item.kind !== "hook") {
1668
- const sidecarVerification = await deps.runPostUnitVerification(
1669
- { s, ctx, pi },
1670
- deps.pauseAuto,
1671
- );
1672
- if (sidecarVerification === "pause") {
1673
- debugLog("autoLoop", {
1674
- phase: "exit",
1675
- reason: "sidecar-verification-pause",
1676
- });
1677
- sidecarBroke = true;
1678
- break;
1679
- }
1680
- // "retry" for sidecars — skip retry, just continue (sidecar retries are not worth the complexity)
1681
- }
1682
-
1683
- // Post-verification (may enqueue more sidecar items)
1684
- const sidecarPostResult =
1685
- await deps.postUnitPostVerification(postUnitCtx);
1686
- if (sidecarPostResult === "stopped") {
1687
- debugLog("autoLoop", { phase: "exit", reason: "sidecar-stopped" });
1688
- sidecarBroke = true;
1689
- break;
1690
- }
1691
- if (sidecarPostResult === "step-wizard") {
1692
- debugLog("autoLoop", {
1693
- phase: "exit",
1694
- reason: "sidecar-step-wizard",
1695
- });
1696
- sidecarBroke = true;
1697
- break;
1698
- }
1699
- // "continue" — loop checks sidecarQueue again
1700
- }
1701
-
1702
- if (sidecarBroke) break;
1703
-
1704
1642
  consecutiveErrors = 0; // Iteration completed successfully
1705
1643
  debugLog("autoLoop", { phase: "iteration-complete", iteration });
1706
1644
  } catch (loopErr) {
@@ -1740,6 +1678,6 @@ export async function autoLoop(
1740
1678
  }
1741
1679
  }
1742
1680
 
1743
- _activeSession = null;
1681
+ _currentResolve = null;
1744
1682
  debugLog("autoLoop", { phase: "exit", totalIterations: iteration });
1745
1683
  }