gsd-pi 2.37.1-dev.d3ace49 → 2.38.0-dev.63ad7e5

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 (150) hide show
  1. package/dist/app-paths.js +1 -1
  2. package/dist/cli.js +9 -0
  3. package/dist/extension-discovery.d.ts +5 -3
  4. package/dist/extension-discovery.js +14 -9
  5. package/dist/extension-registry.js +2 -2
  6. package/dist/remote-questions-config.js +2 -2
  7. package/dist/resources/extensions/browser-tools/package.json +3 -1
  8. package/dist/resources/extensions/cmux/index.js +55 -1
  9. package/dist/resources/extensions/context7/package.json +1 -1
  10. package/dist/resources/extensions/env-utils.js +29 -0
  11. package/dist/resources/extensions/get-secrets-from-user.js +5 -24
  12. package/dist/resources/extensions/google-search/package.json +3 -1
  13. package/dist/resources/extensions/gsd/auto/session.js +6 -23
  14. package/dist/resources/extensions/gsd/auto-dispatch.js +7 -8
  15. package/dist/resources/extensions/gsd/auto-loop.js +68 -97
  16. package/dist/resources/extensions/gsd/auto-post-unit.js +75 -71
  17. package/dist/resources/extensions/gsd/auto-prompts.js +7 -31
  18. package/dist/resources/extensions/gsd/auto-start.js +13 -2
  19. package/dist/resources/extensions/gsd/auto-worktree-sync.js +13 -5
  20. package/dist/resources/extensions/gsd/auto.js +143 -96
  21. package/dist/resources/extensions/gsd/captures.js +9 -1
  22. package/dist/resources/extensions/gsd/commands-extensions.js +3 -2
  23. package/dist/resources/extensions/gsd/commands-handlers.js +16 -3
  24. package/dist/resources/extensions/gsd/commands-prefs-wizard.js +1 -1
  25. package/dist/resources/extensions/gsd/commands.js +22 -2
  26. package/dist/resources/extensions/gsd/context-budget.js +2 -10
  27. package/dist/resources/extensions/gsd/detection.js +1 -2
  28. package/dist/resources/extensions/gsd/docs/preferences-reference.md +0 -2
  29. package/dist/resources/extensions/gsd/doctor-checks.js +82 -0
  30. package/dist/resources/extensions/gsd/doctor-environment.js +78 -0
  31. package/dist/resources/extensions/gsd/doctor-format.js +15 -0
  32. package/dist/resources/extensions/gsd/doctor-providers.js +27 -11
  33. package/dist/resources/extensions/gsd/doctor.js +184 -11
  34. package/dist/resources/extensions/gsd/export.js +1 -1
  35. package/dist/resources/extensions/gsd/files.js +2 -2
  36. package/dist/resources/extensions/gsd/forensics.js +1 -1
  37. package/dist/resources/extensions/gsd/index.js +2 -1
  38. package/dist/resources/extensions/gsd/migrate/parsers.js +1 -1
  39. package/dist/resources/extensions/gsd/package.json +1 -1
  40. package/dist/resources/extensions/gsd/preferences-models.js +0 -12
  41. package/dist/resources/extensions/gsd/preferences-types.js +0 -1
  42. package/dist/resources/extensions/gsd/preferences-validation.js +1 -11
  43. package/dist/resources/extensions/gsd/preferences.js +5 -5
  44. package/dist/resources/extensions/gsd/prompts/discuss.md +11 -14
  45. package/dist/resources/extensions/gsd/prompts/execute-task.md +2 -2
  46. package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
  47. package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
  48. package/dist/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
  49. package/dist/resources/extensions/gsd/prompts/queue.md +4 -8
  50. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +11 -8
  51. package/dist/resources/extensions/gsd/prompts/run-uat.md +25 -10
  52. package/dist/resources/extensions/gsd/prompts/workflow-start.md +2 -2
  53. package/dist/resources/extensions/gsd/repo-identity.js +21 -4
  54. package/dist/resources/extensions/gsd/resource-version.js +2 -1
  55. package/dist/resources/extensions/gsd/state.js +1 -1
  56. package/dist/resources/extensions/gsd/visualizer-data.js +1 -1
  57. package/dist/resources/extensions/gsd/worktree.js +35 -16
  58. package/dist/resources/extensions/remote-questions/status.js +2 -1
  59. package/dist/resources/extensions/remote-questions/store.js +2 -1
  60. package/dist/resources/extensions/search-the-web/provider.js +2 -1
  61. package/dist/resources/extensions/subagent/index.js +12 -3
  62. package/dist/resources/extensions/subagent/isolation.js +2 -1
  63. package/dist/resources/extensions/ttsr/rule-loader.js +2 -1
  64. package/dist/resources/extensions/universal-config/package.json +1 -1
  65. package/dist/welcome-screen.d.ts +12 -0
  66. package/dist/welcome-screen.js +53 -0
  67. package/package.json +1 -1
  68. package/packages/pi-coding-agent/dist/core/package-manager.d.ts.map +1 -1
  69. package/packages/pi-coding-agent/dist/core/package-manager.js +8 -4
  70. package/packages/pi-coding-agent/dist/core/package-manager.js.map +1 -1
  71. package/packages/pi-coding-agent/package.json +1 -1
  72. package/packages/pi-coding-agent/src/core/package-manager.ts +8 -4
  73. package/pkg/package.json +1 -1
  74. package/src/resources/extensions/cmux/index.ts +57 -1
  75. package/src/resources/extensions/env-utils.ts +31 -0
  76. package/src/resources/extensions/get-secrets-from-user.ts +5 -24
  77. package/src/resources/extensions/gsd/auto/session.ts +7 -25
  78. package/src/resources/extensions/gsd/auto-dispatch.ts +6 -8
  79. package/src/resources/extensions/gsd/auto-loop.ts +88 -133
  80. package/src/resources/extensions/gsd/auto-post-unit.ts +52 -42
  81. package/src/resources/extensions/gsd/auto-prompts.ts +7 -33
  82. package/src/resources/extensions/gsd/auto-start.ts +18 -2
  83. package/src/resources/extensions/gsd/auto-worktree-sync.ts +15 -4
  84. package/src/resources/extensions/gsd/auto.ts +139 -101
  85. package/src/resources/extensions/gsd/captures.ts +10 -1
  86. package/src/resources/extensions/gsd/commands-extensions.ts +4 -2
  87. package/src/resources/extensions/gsd/commands-handlers.ts +17 -2
  88. package/src/resources/extensions/gsd/commands-prefs-wizard.ts +1 -1
  89. package/src/resources/extensions/gsd/commands.ts +24 -2
  90. package/src/resources/extensions/gsd/context-budget.ts +2 -12
  91. package/src/resources/extensions/gsd/detection.ts +2 -2
  92. package/src/resources/extensions/gsd/docs/preferences-reference.md +0 -2
  93. package/src/resources/extensions/gsd/doctor-checks.ts +75 -0
  94. package/src/resources/extensions/gsd/doctor-environment.ts +82 -1
  95. package/src/resources/extensions/gsd/doctor-format.ts +20 -0
  96. package/src/resources/extensions/gsd/doctor-providers.ts +26 -9
  97. package/src/resources/extensions/gsd/doctor-types.ts +16 -1
  98. package/src/resources/extensions/gsd/doctor.ts +177 -13
  99. package/src/resources/extensions/gsd/export.ts +1 -1
  100. package/src/resources/extensions/gsd/files.ts +2 -2
  101. package/src/resources/extensions/gsd/forensics.ts +1 -1
  102. package/src/resources/extensions/gsd/index.ts +3 -1
  103. package/src/resources/extensions/gsd/migrate/parsers.ts +1 -1
  104. package/src/resources/extensions/gsd/preferences-models.ts +0 -12
  105. package/src/resources/extensions/gsd/preferences-types.ts +0 -4
  106. package/src/resources/extensions/gsd/preferences-validation.ts +1 -11
  107. package/src/resources/extensions/gsd/preferences.ts +5 -5
  108. package/src/resources/extensions/gsd/prompts/discuss.md +11 -14
  109. package/src/resources/extensions/gsd/prompts/execute-task.md +2 -2
  110. package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +11 -12
  111. package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +8 -10
  112. package/src/resources/extensions/gsd/prompts/guided-resume-task.md +1 -1
  113. package/src/resources/extensions/gsd/prompts/queue.md +4 -8
  114. package/src/resources/extensions/gsd/prompts/reactive-execute.md +11 -8
  115. package/src/resources/extensions/gsd/prompts/run-uat.md +25 -10
  116. package/src/resources/extensions/gsd/prompts/workflow-start.md +2 -2
  117. package/src/resources/extensions/gsd/repo-identity.ts +23 -4
  118. package/src/resources/extensions/gsd/resource-version.ts +3 -1
  119. package/src/resources/extensions/gsd/state.ts +1 -1
  120. package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +21 -18
  121. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +11 -31
  122. package/src/resources/extensions/gsd/tests/cmux.test.ts +93 -0
  123. package/src/resources/extensions/gsd/tests/doctor-enhancements.test.ts +266 -0
  124. package/src/resources/extensions/gsd/tests/doctor-providers.test.ts +86 -3
  125. package/src/resources/extensions/gsd/tests/preferences.test.ts +2 -7
  126. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +59 -0
  127. package/src/resources/extensions/gsd/tests/repo-identity-worktree.test.ts +21 -1
  128. package/src/resources/extensions/gsd/tests/run-uat.test.ts +11 -3
  129. package/src/resources/extensions/gsd/tests/worktree.test.ts +47 -0
  130. package/src/resources/extensions/gsd/types.ts +0 -1
  131. package/src/resources/extensions/gsd/visualizer-data.ts +1 -1
  132. package/src/resources/extensions/gsd/worktree.ts +35 -15
  133. package/src/resources/extensions/remote-questions/status.ts +3 -1
  134. package/src/resources/extensions/remote-questions/store.ts +3 -1
  135. package/src/resources/extensions/search-the-web/provider.ts +2 -1
  136. package/src/resources/extensions/subagent/index.ts +12 -3
  137. package/src/resources/extensions/subagent/isolation.ts +3 -1
  138. package/src/resources/extensions/ttsr/rule-loader.ts +3 -1
  139. package/dist/resources/extensions/gsd/prompt-compressor.js +0 -393
  140. package/dist/resources/extensions/gsd/semantic-chunker.js +0 -254
  141. package/dist/resources/extensions/gsd/summary-distiller.js +0 -212
  142. package/src/resources/extensions/gsd/prompt-compressor.ts +0 -508
  143. package/src/resources/extensions/gsd/semantic-chunker.ts +0 -336
  144. package/src/resources/extensions/gsd/summary-distiller.ts +0 -258
  145. package/src/resources/extensions/gsd/tests/context-compression.test.ts +0 -193
  146. package/src/resources/extensions/gsd/tests/prompt-compressor.test.ts +0 -529
  147. package/src/resources/extensions/gsd/tests/semantic-chunker.test.ts +0 -426
  148. package/src/resources/extensions/gsd/tests/summary-distiller.test.ts +0 -323
  149. package/src/resources/extensions/gsd/tests/token-optimization-benchmark.test.ts +0 -1272
  150. package/src/resources/extensions/gsd/tests/token-optimization-prefs.test.ts +0 -164
@@ -289,10 +289,17 @@ export class CmuxClient {
289
289
  }
290
290
 
291
291
  async createSplit(direction: "right" | "down" | "left" | "up"): Promise<string | null> {
292
+ return this.createSplitFrom(this.config.surfaceId, direction);
293
+ }
294
+
295
+ async createSplitFrom(
296
+ sourceSurfaceId: string | undefined,
297
+ direction: "right" | "down" | "left" | "up",
298
+ ): Promise<string | null> {
292
299
  if (!this.config.splits) return null;
293
300
  const before = new Set(await this.listSurfaceIds());
294
301
  const args = ["new-split", direction];
295
- const scopedArgs = this.appendSurface(this.appendWorkspace(args), this.config.surfaceId);
302
+ const scopedArgs = this.appendSurface(this.appendWorkspace(args), sourceSurfaceId);
296
303
  await this.runAsync(scopedArgs);
297
304
  const after = await this.listSurfaceIds();
298
305
  for (const id of after) {
@@ -301,6 +308,55 @@ export class CmuxClient {
301
308
  return null;
302
309
  }
303
310
 
311
+ /**
312
+ * Create a grid of surfaces for parallel agent execution.
313
+ *
314
+ * Layout strategy (gsd stays in the original surface):
315
+ * 1 agent: [gsd | A]
316
+ * 2 agents: [gsd | A]
317
+ * [ | B]
318
+ * 3 agents: [gsd | A]
319
+ * [ C | B]
320
+ * 4 agents: [gsd | A]
321
+ * [ C | B] (D splits from B downward)
322
+ * [ | D]
323
+ *
324
+ * Returns surface IDs in order, or empty array on failure.
325
+ */
326
+ async createGridLayout(count: number): Promise<string[]> {
327
+ if (!this.config.splits || count <= 0) return [];
328
+ const surfaces: string[] = [];
329
+
330
+ // First split: create right column from the gsd surface
331
+ const rightCol = await this.createSplitFrom(this.config.surfaceId, "right");
332
+ if (!rightCol) return [];
333
+ surfaces.push(rightCol);
334
+ if (count === 1) return surfaces;
335
+
336
+ // Second split: split right column down → bottom-right
337
+ const bottomRight = await this.createSplitFrom(rightCol, "down");
338
+ if (!bottomRight) return surfaces;
339
+ surfaces.push(bottomRight);
340
+ if (count === 2) return surfaces;
341
+
342
+ // Third split: split gsd surface down → bottom-left
343
+ const bottomLeft = await this.createSplitFrom(this.config.surfaceId, "down");
344
+ if (!bottomLeft) return surfaces;
345
+ surfaces.push(bottomLeft);
346
+ if (count === 3) return surfaces;
347
+
348
+ // Fourth+: split subsequent surfaces down from the last created
349
+ let lastSurface = bottomRight;
350
+ for (let i = 3; i < count; i++) {
351
+ const next = await this.createSplitFrom(lastSurface, "down");
352
+ if (!next) break;
353
+ surfaces.push(next);
354
+ lastSurface = next;
355
+ }
356
+
357
+ return surfaces;
358
+ }
359
+
304
360
  async sendSurface(surfaceId: string, text: string): Promise<boolean> {
305
361
  const payload = text.endsWith("\n") ? text : `${text}\n`;
306
362
  const stdout = await this.runAsync(["send-surface", "--surface", surfaceId, payload]);
@@ -0,0 +1,31 @@
1
+ // GSD Extension — Environment variable utilities
2
+ // Copyright (c) 2026 Jeremy McSpadden <jeremy@fluxlabs.net>
3
+ //
4
+ // Pure utility for checking existing env keys in .env files and process.env.
5
+ // Extracted from get-secrets-from-user.ts to avoid pulling in @gsd/pi-tui
6
+ // when only env-checking is needed (e.g. from files.ts during report generation).
7
+
8
+ import { readFile } from "node:fs/promises";
9
+
10
+ /**
11
+ * Check which keys already exist in a .env file or process.env.
12
+ * Returns the subset of `keys` that are already set.
13
+ */
14
+ export async function checkExistingEnvKeys(keys: string[], envFilePath: string): Promise<string[]> {
15
+ let fileContent = "";
16
+ try {
17
+ fileContent = await readFile(envFilePath, "utf8");
18
+ } catch {
19
+ // ENOENT or other read error — proceed with empty content
20
+ }
21
+
22
+ const existing: string[] = [];
23
+ for (const key of keys) {
24
+ const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
25
+ const regex = new RegExp(`^${escaped}\\s*=`, "m");
26
+ if (regex.test(fileContent) || key in process.env) {
27
+ existing.push(key);
28
+ }
29
+ }
30
+ return existing;
31
+ }
@@ -67,30 +67,11 @@ async function writeEnvKey(filePath: string, key: string, value: string): Promis
67
67
 
68
68
  // ─── Exported utilities ───────────────────────────────────────────────────────
69
69
 
70
- /**
71
- * Check which keys already exist in the .env file or process.env.
72
- * Returns the subset of `keys` that are already set.
73
- * Handles ENOENT gracefully (still checks process.env).
74
- * Empty-string values count as existing.
75
- */
76
- export async function checkExistingEnvKeys(keys: string[], envFilePath: string): Promise<string[]> {
77
- let fileContent = "";
78
- try {
79
- fileContent = await readFile(envFilePath, "utf8");
80
- } catch {
81
- // ENOENT or other read error — proceed with empty content
82
- }
83
-
84
- const existing: string[] = [];
85
- for (const key of keys) {
86
- const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
87
- const regex = new RegExp(`^${escaped}\\s*=`, "m");
88
- if (regex.test(fileContent) || key in process.env) {
89
- existing.push(key);
90
- }
91
- }
92
- return existing;
93
- }
70
+ // Re-export from env-utils.ts so existing consumers still work.
71
+ // The implementation lives in env-utils.ts to avoid pulling @gsd/pi-tui
72
+ // into modules that only need env-checking (e.g. files.ts during reports).
73
+ import { checkExistingEnvKeys } from "./env-utils.js";
74
+ export { checkExistingEnvKeys };
94
75
 
95
76
  /**
96
77
  * Detect the write destination based on project files in basePath.
@@ -124,6 +124,9 @@ export class AutoSession {
124
124
  // ── Sidecar queue ─────────────────────────────────────────────────────
125
125
  sidecarQueue: SidecarItem[] = [];
126
126
 
127
+ // ── Dispatch circuit breakers ──────────────────────────────────────
128
+ rewriteAttemptCount = 0;
129
+
127
130
  // ── Metrics ──────────────────────────────────────────────────────────────
128
131
  autoStartTime = 0;
129
132
  lastPromptCharCount: number | undefined;
@@ -134,27 +137,8 @@ export class AutoSession {
134
137
  sigtermHandler: (() => void) | null = null;
135
138
 
136
139
  // ── Loop promise state ──────────────────────────────────────────────────
137
- /**
138
- * True only while runUnit is rotating into a fresh session. agent_end events
139
- * emitted from the previous session's abort during this window must be
140
- * ignored; they do not belong to the new unit.
141
- */
142
- sessionSwitchInFlight = false;
143
-
144
- /**
145
- * One-shot resolver for the current unit's agent_end promise.
146
- * Non-null only while a unit is in-flight (between sendMessage and agent_end).
147
- * Scoped to the session to prevent concurrent session corruption.
148
- */
149
- pendingResolve: ((result: { status: "completed" | "cancelled" | "error"; event?: { messages: unknown[] } }) => void) | null = null;
150
-
151
- /**
152
- * Queue for agent_end events that arrive when no pendingResolve exists.
153
- * This happens when error-recovery sendMessage retries produce agent_end
154
- * events between loop iterations. The next runUnit drains this queue
155
- * instead of waiting for a new event.
156
- */
157
- pendingAgentEndQueue: Array<{ messages: unknown[] }> = [];
140
+ // Per-unit resolve function and session-switch guard live at module level
141
+ // in auto-loop.ts (_currentResolve, _sessionSwitchInFlight).
158
142
 
159
143
  // ── Methods ──────────────────────────────────────────────────────────────
160
144
 
@@ -228,14 +212,12 @@ export class AutoSession {
228
212
  this.lastBaselineCharCount = undefined;
229
213
  this.pendingQuickTasks = [];
230
214
  this.sidecarQueue = [];
215
+ this.rewriteAttemptCount = 0;
231
216
 
232
217
  // Signal handler
233
218
  this.sigtermHandler = null;
234
219
 
235
- // Loop promise state
236
- this.sessionSwitchInFlight = false;
237
- this.pendingResolve = null;
238
- this.pendingAgentEndQueue = [];
220
+ // Loop promise state lives in auto-loop.ts module scope
239
221
  }
240
222
 
241
223
  toJSON(): Record<string, unknown> {
@@ -62,6 +62,7 @@ export interface DispatchContext {
62
62
  midTitle: string;
63
63
  state: GSDState;
64
64
  prefs: GSDPreferences | undefined;
65
+ session?: import("./auto/session.js").AutoSession;
65
66
  }
66
67
 
67
68
  interface DispatchRule {
@@ -82,26 +83,23 @@ function missingSliceStop(mid: string, phase: string): DispatchAction {
82
83
  // ─── Rewrite Circuit Breaker ──────────────────────────────────────────────
83
84
 
84
85
  const MAX_REWRITE_ATTEMPTS = 3;
85
- let rewriteAttemptCount = 0;
86
- export function resetRewriteCircuitBreaker(): void {
87
- rewriteAttemptCount = 0;
88
- }
89
86
 
90
87
  // ─── Rules ────────────────────────────────────────────────────────────────
91
88
 
92
89
  const DISPATCH_RULES: DispatchRule[] = [
93
90
  {
94
91
  name: "rewrite-docs (override gate)",
95
- match: async ({ mid, midTitle, state, basePath }) => {
92
+ match: async ({ mid, midTitle, state, basePath, session }) => {
96
93
  const pendingOverrides = await loadActiveOverrides(basePath);
97
94
  if (pendingOverrides.length === 0) return null;
98
- if (rewriteAttemptCount >= MAX_REWRITE_ATTEMPTS) {
95
+ const count = session?.rewriteAttemptCount ?? 0;
96
+ if (count >= MAX_REWRITE_ATTEMPTS) {
99
97
  const { resolveAllOverrides } = await import("./files.js");
100
98
  await resolveAllOverrides(basePath);
101
- rewriteAttemptCount = 0;
99
+ if (session) session.rewriteAttemptCount = 0;
102
100
  return null;
103
101
  }
104
- rewriteAttemptCount++;
102
+ if (session) session.rewriteAttemptCount++;
105
103
  const unitId = state.activeSlice ? `${mid}/${state.activeSlice.id}` : mid;
106
104
  return {
107
105
  action: "dispatch",
@@ -5,9 +5,9 @@
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
13
  import type { ExtensionAPI, ExtensionContext } from "@gsd/pi-coding-agent";
@@ -18,7 +18,7 @@ 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,
@@ -36,6 +36,19 @@ import type { CmuxLogLevel } from "../cmux/index.js";
36
36
  */
37
37
  const MAX_LOOP_ITERATIONS = 500;
38
38
 
39
+ /** Data-driven budget threshold notifications (75/80/90%). The 100% case is
40
+ * handled inline because it requires break/pause/stop control flow. */
41
+ const BUDGET_THRESHOLDS: Array<{
42
+ pct: number;
43
+ label: string;
44
+ notifyLevel: "info" | "warning";
45
+ cmuxLevel: "progress" | "warning";
46
+ }> = [
47
+ { pct: 90, label: "Budget 90%", notifyLevel: "warning", cmuxLevel: "warning" },
48
+ { pct: 80, label: "Approaching budget ceiling — 80%", notifyLevel: "warning", cmuxLevel: "warning" },
49
+ { pct: 75, label: "Budget 75%", notifyLevel: "info", cmuxLevel: "progress" },
50
+ ];
51
+
39
52
  // ─── Types ───────────────────────────────────────────────────────────────────
40
53
 
41
54
  /**
@@ -54,17 +67,15 @@ export interface UnitResult {
54
67
  event?: AgentEndEvent;
55
68
  }
56
69
 
57
- // ─── Session-scoped promise state ───────────────────────────────────────────
70
+ // ─── Per-unit one-shot promise state ────────────────────────────────────────
58
71
  //
59
- // pendingResolve and pendingAgentEndQueue live on AutoSession (not module-level)
60
- // so concurrent sessions cannot corrupt each other's promises.
72
+ // A single module-level resolve function scoped to the current unit execution.
73
+ // No queue if an agent_end arrives with no pending resolver, it is dropped
74
+ // (logged as warning). This is simpler and safer than the previous session-
75
+ // scoped pendingResolve + pendingAgentEndQueue pattern.
61
76
 
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;
77
+ let _currentResolve: ((result: UnitResult) => void) | null = null;
78
+ let _sessionSwitchInFlight = false;
68
79
 
69
80
  // ─── resolveAgentEnd ─────────────────────────────────────────────────────────
70
81
 
@@ -73,60 +84,48 @@ let _activeSession: AutoSession | null = null;
73
84
  * in-flight unit promise. One-shot: the resolver is nulled before calling
74
85
  * to prevent double-resolution from model fallback retries.
75
86
  *
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.
87
+ * If no resolver exists (event arrived between loop iterations or during
88
+ * session switch), the event is dropped with a debug warning.
78
89
  */
79
90
  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
- });
91
+ if (_sessionSwitchInFlight) {
92
+ debugLog("resolveAgentEnd", { status: "ignored-during-switch" });
86
93
  return;
87
94
  }
88
-
89
- if (s.pendingResolve) {
95
+ if (_currentResolve) {
90
96
  debugLog("resolveAgentEnd", { status: "resolving", hasEvent: true });
91
- const r = s.pendingResolve;
92
- s.pendingResolve = null;
97
+ const r = _currentResolve;
98
+ _currentResolve = null;
93
99
  r({ status: "completed", event });
94
100
  } else {
95
- // Queue the event so the next runUnit picks it up immediately
96
101
  debugLog("resolveAgentEnd", {
97
- status: "queued",
98
- queueLength: s.pendingAgentEndQueue.length + 1,
99
- warning:
100
- "agent_end arrived between loop iterations — queued for next runUnit",
102
+ status: "no-pending-resolve",
103
+ warning: "agent_end with no pending unit",
101
104
  });
102
- s.pendingAgentEndQueue.push(event);
103
105
  }
104
106
  }
105
107
 
106
108
  export function isSessionSwitchInFlight(): boolean {
107
- return _activeSession?.sessionSwitchInFlight ?? false;
109
+ return _sessionSwitchInFlight;
108
110
  }
109
111
 
110
112
  // ─── resetPendingResolve (test helper) ───────────────────────────────────────
111
113
 
112
114
  /**
113
- * Reset session promise state. Only exported for test cleanup — production code
114
- * should never call this.
115
+ * Reset module-level promise state. Only exported for test cleanup —
116
+ * production code should never call this.
115
117
  */
116
118
  export function _resetPendingResolve(): void {
117
- if (_activeSession) {
118
- _activeSession.pendingResolve = null;
119
- _activeSession.pendingAgentEndQueue = [];
120
- }
121
- _activeSession = null;
119
+ _currentResolve = null;
120
+ _sessionSwitchInFlight = false;
122
121
  }
123
122
 
124
123
  /**
125
- * Set the active session for resolveAgentEnd. Only exported for test setup —
126
- * production code sets this via autoLoop entry.
124
+ * No-op for backward compatibility with tests that previously set the
125
+ * active session. The module no longer holds a session reference.
127
126
  */
128
- export function _setActiveSession(session: AutoSession | null): void {
129
- _activeSession = session;
127
+ export function _setActiveSession(_session: AutoSession | null): void {
128
+ // No-op — kept for test backward compatibility
130
129
  }
131
130
 
132
131
  // ─── runUnit ─────────────────────────────────────────────────────────────────
@@ -150,41 +149,15 @@ export async function runUnit(
150
149
  ): Promise<UnitResult> {
151
150
  debugLog("runUnit", { phase: "start", unitType, unitId });
152
151
 
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
152
  // ── Session creation with timeout ──
180
153
  debugLog("runUnit", { phase: "session-create", unitType, unitId });
181
154
 
182
155
  let sessionResult: { cancelled: boolean };
183
156
  let sessionTimeoutHandle: ReturnType<typeof setTimeout> | undefined;
184
- s.sessionSwitchInFlight = true;
157
+ _sessionSwitchInFlight = true;
185
158
  try {
186
159
  const sessionPromise = s.cmdCtx!.newSession().finally(() => {
187
- s.sessionSwitchInFlight = false;
160
+ _sessionSwitchInFlight = false;
188
161
  });
189
162
  const timeoutPromise = new Promise<{ cancelled: true }>((resolve) => {
190
163
  sessionTimeoutHandle = setTimeout(
@@ -216,11 +189,12 @@ export async function runUnit(
216
189
  return { status: "cancelled" };
217
190
  }
218
191
 
219
- // ── Create the agent_end promise (session-scoped) ──
192
+ // ── Create the agent_end promise (per-unit one-shot) ──
220
193
  // This happens after newSession completes so session-switch agent_end events
221
194
  // from the previous session cannot resolve the new unit.
195
+ _sessionSwitchInFlight = false;
222
196
  const unitPromise = new Promise<UnitResult>((resolve) => {
223
- s.pendingResolve = resolve;
197
+ _currentResolve = resolve;
224
198
  });
225
199
 
226
200
  // Ensure cwd matches basePath before dispatch (#1389).
@@ -383,6 +357,7 @@ export interface LoopDeps {
383
357
  midTitle: string;
384
358
  state: GSDState;
385
359
  prefs: GSDPreferences | undefined;
360
+ session?: AutoSession;
386
361
  }) => Promise<DispatchAction>;
387
362
  runPreDispatchHooks: (
388
363
  unitType: string,
@@ -500,6 +475,7 @@ export interface LoopDeps {
500
475
  // Post-unit processing
501
476
  postUnitPreVerification: (
502
477
  pctx: PostUnitContext,
478
+ opts?: PreVerificationOpts,
503
479
  ) => Promise<"dispatched" | "continue">;
504
480
  runPostUnitVerification: (
505
481
  vctx: VerificationContext,
@@ -530,7 +506,6 @@ export async function autoLoop(
530
506
  deps: LoopDeps,
531
507
  ): Promise<void> {
532
508
  debugLog("autoLoop", { phase: "enter" });
533
- _activeSession = s;
534
509
  let iteration = 0;
535
510
  let lastDerivedUnit = "";
536
511
  let sameUnitCount = 0;
@@ -787,7 +762,7 @@ export async function autoLoop(
787
762
  (m: { status: string }) =>
788
763
  m.status !== "complete" && m.status !== "parked",
789
764
  );
790
- if (incomplete.length === 0) {
765
+ if (incomplete.length === 0 && state.registry.length > 0) {
791
766
  // All milestones complete — merge milestone branch before stopping
792
767
  if (s.currentMilestoneId) {
793
768
  deps.resolver.mergeAndExit(s.currentMilestoneId, ctx.ui);
@@ -804,6 +779,18 @@ export async function autoLoop(
804
779
  "success",
805
780
  );
806
781
  await deps.stopAuto(ctx, pi, "All milestones complete");
782
+ } else if (incomplete.length === 0 && state.registry.length === 0) {
783
+ // Empty registry — no milestones visible, likely a path resolution bug
784
+ const diag = `basePath=${s.basePath}, phase=${state.phase}`;
785
+ ctx.ui.notify(
786
+ `No milestones visible in current scope. Possible path resolution issue.\n Diagnostic: ${diag}`,
787
+ "error",
788
+ );
789
+ await deps.stopAuto(
790
+ ctx,
791
+ pi,
792
+ `No milestones found — check basePath resolution`,
793
+ );
807
794
  } else if (state.phase === "blocked") {
808
795
  const blockerMsg = `Blocked: ${state.blockers.join(", ")}`;
809
796
  await deps.stopAuto(ctx, pi, blockerMsg);
@@ -965,62 +952,26 @@ export async function autoLoop(
965
952
  ctx.ui.notify(`${msg} Continuing (enforcement: warn).`, "warning");
966
953
  deps.sendDesktopNotification("GSD", msg, "warning", "budget");
967
954
  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",
955
+ } else {
956
+ // Data-driven 75/80/90% threshold notifications
957
+ const threshold = BUDGET_THRESHOLDS.find(
958
+ (t) => newBudgetAlertLevel === t.pct,
1021
959
  );
1022
- } else if (budgetAlertLevel === 0) {
1023
- s.lastBudgetAlertLevel = 0;
960
+ if (threshold) {
961
+ s.lastBudgetAlertLevel =
962
+ newBudgetAlertLevel as AutoSession["lastBudgetAlertLevel"];
963
+ const msg = `${threshold.label}: ${deps.formatCost(totalCost)} / ${deps.formatCost(budgetCeiling)}`;
964
+ ctx.ui.notify(msg, threshold.notifyLevel);
965
+ deps.sendDesktopNotification(
966
+ "GSD",
967
+ msg,
968
+ threshold.notifyLevel,
969
+ "budget",
970
+ );
971
+ deps.logCmuxEvent(prefs, msg, threshold.cmuxLevel);
972
+ } else if (budgetAlertLevel === 0) {
973
+ s.lastBudgetAlertLevel = 0;
974
+ }
1024
975
  }
1025
976
  } else {
1026
977
  s.lastBudgetAlertLevel = 0;
@@ -1091,6 +1042,7 @@ export async function autoLoop(
1091
1042
  midTitle: midTitle!,
1092
1043
  state,
1093
1044
  prefs,
1045
+ session: s,
1094
1046
  });
1095
1047
 
1096
1048
  if (dispatchResult.action === "stop") {
@@ -1649,9 +1601,12 @@ export async function autoLoop(
1649
1601
  break;
1650
1602
  }
1651
1603
 
1652
- // Run pre-verification for the sidecar unit
1604
+ // Run pre-verification for the sidecar unit (lightweight path)
1605
+ const sidecarPreOpts: PreVerificationOpts = item.kind === "hook"
1606
+ ? { skipSettleDelay: true, skipDoctor: true, skipStateRebuild: true, skipWorktreeSync: true }
1607
+ : { skipSettleDelay: true, skipStateRebuild: true };
1653
1608
  const sidecarPreResult =
1654
- await deps.postUnitPreVerification(postUnitCtx);
1609
+ await deps.postUnitPreVerification(postUnitCtx, sidecarPreOpts);
1655
1610
  if (sidecarPreResult === "dispatched") {
1656
1611
  // Pre-verification caused stop/pause
1657
1612
  debugLog("autoLoop", {
@@ -1740,6 +1695,6 @@ export async function autoLoop(
1740
1695
  }
1741
1696
  }
1742
1697
 
1743
- _activeSession = null;
1698
+ _currentResolve = null;
1744
1699
  debugLog("autoLoop", { phase: "exit", totalIterations: iteration });
1745
1700
  }