gsd-pi 2.79.0-dev.5c910bb05 → 2.79.0-dev.9941c9c24

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 (109) hide show
  1. package/dist/resources/.managed-resources-content-hash +1 -1
  2. package/dist/resources/extensions/gsd/auto/contracts.js +1 -0
  3. package/dist/resources/extensions/gsd/auto/orchestrator.js +146 -0
  4. package/dist/resources/extensions/gsd/auto/phases.js +61 -7
  5. package/dist/resources/extensions/gsd/auto/session.js +8 -0
  6. package/dist/resources/extensions/gsd/auto-artifact-paths.js +2 -2
  7. package/dist/resources/extensions/gsd/auto-dispatch.js +2 -0
  8. package/dist/resources/extensions/gsd/auto-recovery.js +63 -55
  9. package/dist/resources/extensions/gsd/auto-runtime-state.js +4 -0
  10. package/dist/resources/extensions/gsd/auto-start.js +3 -2
  11. package/dist/resources/extensions/gsd/auto.js +159 -2
  12. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +9 -1
  13. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +8 -8
  14. package/dist/resources/extensions/gsd/bootstrap/write-gate.js +8 -8
  15. package/dist/resources/extensions/gsd/gsd-db.js +34 -1
  16. package/dist/resources/extensions/gsd/guided-flow.js +40 -0
  17. package/dist/resources/extensions/gsd/paths.js +5 -1
  18. package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +45 -4
  19. package/dist/resources/extensions/gsd/uok/audit.js +23 -9
  20. package/dist/resources/extensions/gsd/uok/contracts.js +69 -1
  21. package/dist/resources/extensions/gsd/uok/dispatch-envelope.js +3 -0
  22. package/dist/resources/extensions/gsd/uok/loop-adapter.js +48 -33
  23. package/dist/resources/extensions/gsd/uok/timeline.js +125 -0
  24. package/dist/resources/extensions/shared/gsd-phase-state.js +45 -3
  25. package/dist/resources/extensions/shared/interview-ui.js +15 -4
  26. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  27. package/dist/web/standalone/.next/BUILD_ID +1 -1
  28. package/dist/web/standalone/.next/app-path-routes-manifest.json +14 -14
  29. package/dist/web/standalone/.next/build-manifest.json +2 -2
  30. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  31. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  32. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  33. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  34. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  35. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  36. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  37. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  38. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  39. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  40. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  41. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  42. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  43. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  44. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  45. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  46. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  47. package/dist/web/standalone/.next/server/app/index.html +1 -1
  48. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  49. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  51. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app-paths-manifest.json +14 -14
  55. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  56. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  57. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  58. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  59. package/package.json +1 -1
  60. package/packages/mcp-server/src/workflow-tools.test.ts +13 -2
  61. package/src/resources/extensions/gsd/auto/contracts.ts +87 -0
  62. package/src/resources/extensions/gsd/auto/loop-deps.ts +10 -3
  63. package/src/resources/extensions/gsd/auto/orchestrator.ts +161 -0
  64. package/src/resources/extensions/gsd/auto/phases.ts +88 -9
  65. package/src/resources/extensions/gsd/auto/session.ts +11 -0
  66. package/src/resources/extensions/gsd/auto-artifact-paths.ts +2 -2
  67. package/src/resources/extensions/gsd/auto-dispatch.ts +1 -0
  68. package/src/resources/extensions/gsd/auto-recovery.ts +59 -53
  69. package/src/resources/extensions/gsd/auto-runtime-state.ts +7 -0
  70. package/src/resources/extensions/gsd/auto-start.ts +3 -2
  71. package/src/resources/extensions/gsd/auto.ts +167 -1
  72. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +14 -1
  73. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +11 -8
  74. package/src/resources/extensions/gsd/bootstrap/tests/write-gate-shouldblock-basepath.test.ts +97 -0
  75. package/src/resources/extensions/gsd/bootstrap/write-gate.ts +8 -4
  76. package/src/resources/extensions/gsd/gsd-db.ts +35 -1
  77. package/src/resources/extensions/gsd/guided-flow.ts +47 -0
  78. package/src/resources/extensions/gsd/interrupted-session.ts +1 -0
  79. package/src/resources/extensions/gsd/paths.ts +6 -1
  80. package/src/resources/extensions/gsd/tests/auto-abort-pause-regression.test.ts +32 -0
  81. package/src/resources/extensions/gsd/tests/auto-orchestrator.test.ts +353 -0
  82. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +108 -1
  83. package/src/resources/extensions/gsd/tests/auto-runtime-state.test.ts +39 -0
  84. package/src/resources/extensions/gsd/tests/auto-session-encapsulation.test.ts +3 -0
  85. package/src/resources/extensions/gsd/tests/check-auto-start-pending-gate.test.ts +203 -0
  86. package/src/resources/extensions/gsd/tests/check-auto-start-ready-guard.test.ts +148 -0
  87. package/src/resources/extensions/gsd/tests/deep-planning-mode-dispatch.test.ts +42 -0
  88. package/src/resources/extensions/gsd/tests/deep-project-auto-loop.test.ts +63 -2
  89. package/src/resources/extensions/gsd/tests/execute-summary-save-empty-project.test.ts +109 -0
  90. package/src/resources/extensions/gsd/tests/gsd-db.test.ts +95 -0
  91. package/src/resources/extensions/gsd/tests/integration/auto-recovery.test.ts +79 -0
  92. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +134 -0
  93. package/src/resources/extensions/gsd/tests/paused-session-via-db.test.ts +2 -0
  94. package/src/resources/extensions/gsd/tests/plan-slice.test.ts +27 -0
  95. package/src/resources/extensions/gsd/tests/uok-contracts.test.ts +109 -1
  96. package/src/resources/extensions/gsd/tests/uok-loop-adapter-writer.test.ts +98 -0
  97. package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +36 -7
  98. package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +47 -4
  99. package/src/resources/extensions/gsd/uok/audit.ts +25 -9
  100. package/src/resources/extensions/gsd/uok/contracts.ts +105 -0
  101. package/src/resources/extensions/gsd/uok/dispatch-envelope.ts +4 -0
  102. package/src/resources/extensions/gsd/uok/loop-adapter.ts +60 -45
  103. package/src/resources/extensions/gsd/uok/timeline.ts +158 -0
  104. package/src/resources/extensions/shared/gsd-phase-state.ts +56 -3
  105. package/src/resources/extensions/shared/interview-ui.ts +18 -5
  106. package/src/resources/extensions/shared/tests/gsd-phase-state.test.ts +43 -1
  107. package/src/resources/extensions/shared/tests/interview-notes-loop.test.ts +41 -0
  108. /package/dist/web/standalone/.next/static/{DSZPSz1kgrF8zPIrV_AMD → zSHLdEn5cpkqivbJZq0Qq}/_buildManifest.js +0 -0
  109. /package/dist/web/standalone/.next/static/{DSZPSz1kgrF8zPIrV_AMD → zSHLdEn5cpkqivbJZq0Qq}/_ssgManifest.js +0 -0
@@ -147,6 +147,7 @@ import {
147
147
  MergeConflictError,
148
148
  parseSliceBranch,
149
149
  setActiveMilestoneId,
150
+ resolveProjectRoot,
150
151
  } from "./worktree.js";
151
152
  import { GitServiceImpl } from "./git-service.js";
152
153
  import { getPriorSliceCompletionBlocker } from "./dispatch-guard.js";
@@ -231,6 +232,8 @@ import type { ErrorContext } from "./auto/types.js";
231
232
  import { runAutoLoopWithUok } from "./uok/kernel.js";
232
233
  import { resolveUokFlags } from "./uok/flags.js";
233
234
  import { validateDirectory } from "./validate-directory.js";
235
+ import { createAutoOrchestrator } from "./auto/orchestrator.js";
236
+ import type { AutoOrchestrationModule, AutoOrchestratorDeps } from "./auto/contracts.js";
234
237
  // Slice-level parallelism (#2340)
235
238
  import { getEligibleSlices } from "./slice-parallel-eligibility.js";
236
239
  import { startSliceParallel } from "./slice-parallel-orchestrator.js";
@@ -1208,6 +1211,12 @@ export async function stopAuto(
1208
1211
  // changes the user made between sessions (#4959 / CodeRabbit).
1209
1212
  if (pi) clearToolBaseline(pi);
1210
1213
 
1214
+ try {
1215
+ await s.orchestration?.stop(reason ?? "stop");
1216
+ } catch (err) {
1217
+ debugLog("stop-orchestration-stop", { error: err instanceof Error ? err.message : String(err) });
1218
+ }
1219
+
1211
1220
  // Reset all session state in one call
1212
1221
  s.reset();
1213
1222
  }
@@ -1264,6 +1273,7 @@ export async function pauseAuto(
1264
1273
  activeRunDir: s.activeRunDir,
1265
1274
  autoStartTime: s.autoStartTime,
1266
1275
  milestoneLock: s.sessionMilestoneLock ?? undefined,
1276
+ pauseReason: _errorContext?.message,
1267
1277
  };
1268
1278
  setRuntimeKv("global", "", PAUSED_SESSION_KV_KEY, pausedMeta);
1269
1279
  } catch (err) {
@@ -1306,6 +1316,12 @@ export async function pauseAuto(
1306
1316
  resolveAgentEnd({ messages: [] });
1307
1317
  _resetPendingResolve();
1308
1318
 
1319
+ try {
1320
+ await s.orchestration?.stop("pause");
1321
+ } catch (err) {
1322
+ debugLog("pause-orchestration-stop", { error: err instanceof Error ? err.message : String(err) });
1323
+ }
1324
+
1309
1325
  s.active = false;
1310
1326
  s.paused = true;
1311
1327
  deactivateGSD();
@@ -1363,6 +1379,141 @@ function buildResolver(): WorktreeResolver {
1363
1379
  return new WorktreeResolver(s, buildResolverDeps());
1364
1380
  }
1365
1381
 
1382
+ /**
1383
+ * Thin entry glue for the new Auto Orchestration module.
1384
+ *
1385
+ * This intentionally wires only dispatch + error notification today, with
1386
+ * no behavior changes to the existing auto loop. It provides a concrete seam
1387
+ * the next refactor steps can adopt incrementally.
1388
+ */
1389
+ export function createWiredAutoOrchestrationModule(
1390
+ ctx: ExtensionContext,
1391
+ _pi: ExtensionAPI,
1392
+ dispatchBasePath: string,
1393
+ runtimeBasePath = resolveProjectRoot(dispatchBasePath),
1394
+ ): AutoOrchestrationModule {
1395
+ const flowId = `auto-orchestrator-${Date.now()}`;
1396
+ let seq = 0;
1397
+
1398
+ const deps: AutoOrchestratorDeps = {
1399
+ dispatch: {
1400
+ async decideNextUnit() {
1401
+ const state = await deriveState(dispatchBasePath);
1402
+ const active = state.activeMilestone;
1403
+ if (!active) return null;
1404
+
1405
+ const prefs = loadEffectiveGSDPreferences(dispatchBasePath)?.preferences;
1406
+ const action = await resolveDispatch({
1407
+ basePath: dispatchBasePath,
1408
+ mid: active.id,
1409
+ midTitle: active.title,
1410
+ state,
1411
+ prefs,
1412
+ });
1413
+
1414
+ if (action.action !== "dispatch") return null;
1415
+ return {
1416
+ unitType: action.unitType,
1417
+ unitId: action.unitId,
1418
+ reason: action.matchedRule ?? "dispatch",
1419
+ preconditions: [],
1420
+ };
1421
+ },
1422
+ },
1423
+ recovery: {
1424
+ async classifyAndRecover(input) {
1425
+ const reason = input.error instanceof Error ? input.error.message : String(input.error ?? "unknown auto error");
1426
+ return { action: "escalate" as const, reason };
1427
+ },
1428
+ },
1429
+ worktree: {
1430
+ async prepareForUnit() {},
1431
+ async syncAfterUnit() {},
1432
+ async cleanupOnStop() {},
1433
+ },
1434
+ health: {
1435
+ async preAdvanceGate() {
1436
+ const gate = await preDispatchHealthGate(dispatchBasePath);
1437
+ return {
1438
+ allow: gate.proceed,
1439
+ reason: gate.reason,
1440
+ };
1441
+ },
1442
+ async postAdvanceRecord(result) {
1443
+ if (result.kind === "error") {
1444
+ recordHealthSnapshot(1, 0, 0, [{
1445
+ code: "orchestration-error",
1446
+ message: result.reason ?? "orchestration error",
1447
+ severity: "error",
1448
+ unitId: "orchestration",
1449
+ }], [], "orchestration");
1450
+ } else if (result.kind === "blocked") {
1451
+ recordHealthSnapshot(0, 1, 0, [{
1452
+ code: "orchestration-blocked",
1453
+ message: result.reason ?? "orchestration blocked",
1454
+ severity: "warning",
1455
+ unitId: "orchestration",
1456
+ }], [], "orchestration");
1457
+ }
1458
+ },
1459
+ },
1460
+ runtime: {
1461
+ async ensureLockOwnership() {
1462
+ const status = getSessionLockStatus(runtimeBasePath);
1463
+ if (!status.valid || status.failureReason === "pid-mismatch") {
1464
+ throw new Error("session lock held by another process");
1465
+ }
1466
+ },
1467
+ async journalTransition(event) {
1468
+ const eventType = event.name === "start"
1469
+ ? "iteration-start"
1470
+ : event.name === "resume"
1471
+ ? "iteration-start"
1472
+ : event.name === "advance"
1473
+ ? "dispatch-match"
1474
+ : event.name === "advance-blocked"
1475
+ ? "guard-block"
1476
+ : event.name === "advance-stopped"
1477
+ ? "dispatch-stop"
1478
+ : event.name === "advance-error"
1479
+ ? "iteration-end"
1480
+ : event.name === "advance-paused" || event.name === "advance-retry"
1481
+ ? "guard-block"
1482
+ : event.name === "stop"
1483
+ ? "terminal"
1484
+ : "iteration-end";
1485
+
1486
+ _emitJournalEvent(runtimeBasePath, {
1487
+ ts: new Date().toISOString(),
1488
+ flowId,
1489
+ seq: ++seq,
1490
+ eventType,
1491
+ data: {
1492
+ source: "auto-orchestrator",
1493
+ name: event.name,
1494
+ reason: event.reason,
1495
+ unitType: event.unitType,
1496
+ unitId: event.unitId,
1497
+ },
1498
+ });
1499
+ },
1500
+ },
1501
+ notifications: {
1502
+ async notifyLifecycle(event) {
1503
+ if (event.name === "error") {
1504
+ ctx.ui.notify(event.detail ?? "auto orchestration error", "error");
1505
+ }
1506
+ },
1507
+ },
1508
+ };
1509
+
1510
+ return createAutoOrchestrator(deps);
1511
+ }
1512
+
1513
+ function ensureOrchestrationModule(ctx: ExtensionContext, pi: ExtensionAPI, basePath: string): void {
1514
+ s.orchestration = createWiredAutoOrchestrationModule(ctx, pi, basePath, lockBase());
1515
+ }
1516
+
1366
1517
  /**
1367
1518
  * Build the LoopDeps object from auto.ts private scope.
1368
1519
  * This bundles all private functions that autoLoop needs without exporting them.
@@ -1785,6 +1936,7 @@ export async function startAuto(
1785
1936
  rebuildScope(s.basePath, s.currentMilestoneId);
1786
1937
  }
1787
1938
 
1939
+ ensureOrchestrationModule(ctx, pi, s.basePath || base);
1788
1940
  registerSigtermHandler(lockBase());
1789
1941
 
1790
1942
  ctx.ui.setStatus("gsd-auto", s.stepMode ? "next" : "auto");
@@ -1868,6 +2020,11 @@ export async function startAuto(
1868
2020
  }
1869
2021
  pi.events.emit(CMUX_CHANNELS.LOG, { preferences: loadEffectiveGSDPreferences(s.basePath || undefined)?.preferences, message: s.stepMode ? "Step-mode resumed." : "Auto-mode resumed.", level: "progress" });
1870
2022
 
2023
+ try {
2024
+ await s.orchestration?.resume();
2025
+ } catch (err) {
2026
+ debugLog("resume-orchestration-resume", { error: err instanceof Error ? err.message : String(err) });
2027
+ }
1871
2028
  startAutoCommandPolling(s.basePath);
1872
2029
  await runAutoLoopWithUok({
1873
2030
  ctx,
@@ -1904,7 +2061,7 @@ export async function startAuto(
1904
2061
  // Build scope after bootstrap has populated s.basePath / s.originalBasePath /
1905
2062
  // s.currentMilestoneId (including worktree setup inside bootstrapAutoSession).
1906
2063
  rebuildScope(s.basePath, s.currentMilestoneId);
1907
-
2064
+ ensureOrchestrationModule(ctx, pi, s.basePath || base);
1908
2065
  captureProjectRootEnv(s.originalBasePath || s.basePath);
1909
2066
  registerAutoWorkerForSession(s);
1910
2067
  try {
@@ -1915,6 +2072,12 @@ export async function startAuto(
1915
2072
  }
1916
2073
  pi.events.emit(CMUX_CHANNELS.LOG, { preferences: loadEffectiveGSDPreferences(s.basePath || undefined)?.preferences, message: requestedStepMode ? "Step-mode started." : "Auto-mode started.", level: "progress" });
1917
2074
 
2075
+ try {
2076
+ await s.orchestration?.start({ basePath: s.basePath, trigger: "auto-loop" });
2077
+ } catch (err) {
2078
+ debugLog("start-orchestration-start", { error: err instanceof Error ? err.message : String(err) });
2079
+ }
2080
+
1918
2081
  startAutoCommandPolling(s.basePath);
1919
2082
 
1920
2083
  // Dispatch the first unit
@@ -2037,6 +2200,9 @@ export async function dispatchHookUnit(
2037
2200
  }
2038
2201
 
2039
2202
  s.basePath = targetBasePath;
2203
+ if (!s.orchestration) {
2204
+ ensureOrchestrationModule(ctx, pi, s.basePath);
2205
+ }
2040
2206
 
2041
2207
  const hookUnitType = `hook/${hookName}`;
2042
2208
  const hookStartedAt = Date.now();
@@ -56,6 +56,19 @@ function resolveAgentEndBasePath(): string | undefined {
56
56
  }
57
57
  }
58
58
 
59
+ export function _buildAbortedPauseContext(lastMsg: { errorMessage?: unknown }): {
60
+ message: string;
61
+ category: "aborted";
62
+ isTransient: true;
63
+ } {
64
+ const hasErrorMessage = Object.prototype.hasOwnProperty.call(lastMsg, "errorMessage") && !!lastMsg.errorMessage;
65
+ return {
66
+ message: hasErrorMessage ? String(lastMsg.errorMessage) : "Operation aborted",
67
+ category: "aborted",
68
+ isTransient: true,
69
+ };
70
+ }
71
+
59
72
  async function pauseTransientWithBackoff(
60
73
  cls: ErrorClass,
61
74
  pi: ExtensionAPI,
@@ -159,7 +172,7 @@ export async function handleAgentEnd(
159
172
  return;
160
173
  }
161
174
 
162
- await pauseAuto(ctx, pi);
175
+ await pauseAuto(ctx, pi, _buildAbortedPauseContext(lastMsg as { errorMessage?: unknown }));
163
176
  return;
164
177
  }
165
178
  if (lastMsg && "stopReason" in lastMsg && lastMsg.stopReason === "error") {
@@ -188,7 +188,7 @@ export function registerHooks(
188
188
  await getEcosystemReadyPromise();
189
189
 
190
190
  const beforeAgentBasePath = process.cwd();
191
- const pendingApprovalGate = getPendingGate();
191
+ const pendingApprovalGate = getPendingGate(beforeAgentBasePath);
192
192
  if (pendingApprovalGate && isExplicitApprovalResponse(event.prompt, pendingApprovalGate)) {
193
193
  markApprovalGateVerified(pendingApprovalGate, beforeAgentBasePath);
194
194
  const milestoneId = extractDepthVerificationMilestoneId(pendingApprovalGate);
@@ -401,20 +401,22 @@ export function registerHooks(
401
401
  // ── Discussion gate enforcement: block tool calls while gate is pending ──
402
402
  // If ask_user_questions was called with a gate ID but hasn't been confirmed,
403
403
  // block all non-read-only tool calls to prevent the model from skipping gates.
404
- if (getPendingGate()) {
404
+ if (getPendingGate(discussionBasePath)) {
405
405
  const milestoneId = await getDiscussionMilestoneIdFor(discussionBasePath);
406
406
  if (isToolCallEventType("bash", event)) {
407
407
  const bashGuard = shouldBlockPendingGateBash(
408
408
  event.input.command,
409
409
  milestoneId,
410
- isQueuePhaseActive(),
410
+ isQueuePhaseActive(discussionBasePath),
411
+ discussionBasePath,
411
412
  );
412
413
  if (bashGuard.block) return bashGuard;
413
414
  } else {
414
415
  const gateGuard = shouldBlockPendingGate(
415
416
  toolName,
416
417
  milestoneId,
417
- isQueuePhaseActive(),
418
+ isQueuePhaseActive(discussionBasePath),
419
+ discussionBasePath,
418
420
  );
419
421
  if (gateGuard.block) return gateGuard;
420
422
  }
@@ -424,7 +426,7 @@ export function registerHooks(
424
426
  // When /gsd queue is active, the agent should only create milestones,
425
427
  // not execute work. Block write/edit to non-.gsd/ paths and bash commands
426
428
  // that would modify files.
427
- if (isQueuePhaseActive()) {
429
+ if (isQueuePhaseActive(discussionBasePath)) {
428
430
  let queueInput = "";
429
431
  if (isToolCallEventType("write", event)) {
430
432
  queueInput = event.input.path;
@@ -498,7 +500,8 @@ export function registerHooks(
498
500
  event.toolName,
499
501
  event.input.path,
500
502
  await getDiscussionMilestoneIdFor(discussionBasePath),
501
- isQueuePhaseActive(),
503
+ isQueuePhaseActive(discussionBasePath),
504
+ discussionBasePath,
502
505
  );
503
506
  if (result.block) return result;
504
507
  });
@@ -558,7 +561,7 @@ export function registerHooks(
558
561
  if (toolName !== "ask_user_questions") return;
559
562
  const basePath = process.cwd();
560
563
  const milestoneId = await getDiscussionMilestoneIdFor(basePath);
561
- const queueActive = isQueuePhaseActive();
564
+ const queueActive = isQueuePhaseActive(basePath);
562
565
 
563
566
  const details = event.details as any;
564
567
 
@@ -568,7 +571,7 @@ export function registerHooks(
568
571
  // If the user responded at all (even "needs adjustment"), clear the pending gate
569
572
  // because the user engaged — the prompt handles the re-ask-after-adjustment flow.
570
573
  const questions: any[] = (event.input as any)?.questions ?? [];
571
- const currentPendingGate = getPendingGate();
574
+ const currentPendingGate = getPendingGate(basePath);
572
575
  if (currentPendingGate) {
573
576
  if (details?.cancelled || !details?.response) {
574
577
  // Gate stays pending. Direct the agent to the most reliable recovery
@@ -0,0 +1,97 @@
1
+ // GSD-2 write-gate bootstrap — regression tests for basePath threading on
2
+ // shouldBlockContextWrite / shouldBlockPendingGate (R1).
3
+ //
4
+ // The underlying bug: readers defaulted to process.cwd() and so missed the
5
+ // per-basePath state Map entry written by markDepthVerified(..., baseDirA)
6
+ // when cwd had drifted to baseDirB. With basePath threaded explicitly to
7
+ // the readers, the depth-gate sees the verified state regardless of cwd.
8
+
9
+ import { test, describe, before, after } from "node:test";
10
+ import assert from "node:assert/strict";
11
+ import { mkdtempSync, rmSync } from "node:fs";
12
+ import { tmpdir } from "node:os";
13
+ import { join } from "node:path";
14
+
15
+ import {
16
+ markDepthVerified,
17
+ setPendingGate,
18
+ shouldBlockContextWrite,
19
+ shouldBlockPendingGate,
20
+ clearDiscussionFlowState,
21
+ } from "../write-gate.js";
22
+
23
+ function makeTempDir(): string {
24
+ return mkdtempSync(join(tmpdir(), "wg-shouldblock-basepath-"));
25
+ }
26
+
27
+ let originalCwd: string;
28
+ before(() => {
29
+ originalCwd = process.cwd();
30
+ });
31
+ after(() => {
32
+ if (process.cwd() !== originalCwd) {
33
+ process.chdir(originalCwd);
34
+ }
35
+ });
36
+
37
+ describe("write-gate shouldBlock readers respect explicit basePath", () => {
38
+ let baseDirA: string;
39
+ let baseDirB: string;
40
+ let prevPersist: string | undefined;
41
+
42
+ before(() => {
43
+ baseDirA = makeTempDir();
44
+ baseDirB = makeTempDir();
45
+ prevPersist = process.env.GSD_PERSIST_WRITE_GATE_STATE;
46
+ process.env.GSD_PERSIST_WRITE_GATE_STATE = "1";
47
+ });
48
+
49
+ after(() => {
50
+ process.chdir(originalCwd);
51
+ if (prevPersist === undefined) {
52
+ delete process.env.GSD_PERSIST_WRITE_GATE_STATE;
53
+ } else {
54
+ process.env.GSD_PERSIST_WRITE_GATE_STATE = prevPersist;
55
+ }
56
+ rmSync(baseDirA, { recursive: true, force: true });
57
+ rmSync(baseDirB, { recursive: true, force: true });
58
+ });
59
+
60
+ test("shouldBlockContextWrite with explicit basePath sees verified state after cwd drift", () => {
61
+ clearDiscussionFlowState(baseDirA);
62
+ clearDiscussionFlowState(baseDirB);
63
+
64
+ markDepthVerified("M001", baseDirA);
65
+ process.chdir(baseDirB);
66
+
67
+ const contextPath = join(baseDirA, ".gsd", "milestones", "M001", "M001-CONTEXT.md");
68
+ const result = shouldBlockContextWrite("write", contextPath, "M001", undefined, baseDirA);
69
+
70
+ assert.equal(result.block, false, "explicit basePath should resolve to baseDirA's verified state");
71
+ });
72
+
73
+ test("shouldBlockContextWrite without basePath defaults to cwd and misses verified state (bug repro)", () => {
74
+ clearDiscussionFlowState(baseDirA);
75
+ clearDiscussionFlowState(baseDirB);
76
+
77
+ markDepthVerified("M001", baseDirA);
78
+ process.chdir(baseDirB);
79
+
80
+ const contextPath = join(baseDirA, ".gsd", "milestones", "M001", "M001-CONTEXT.md");
81
+ const result = shouldBlockContextWrite("write", contextPath, "M001");
82
+
83
+ assert.equal(result.block, true, "default-to-cwd path resolves to baseDirB and misses baseDirA state");
84
+ });
85
+
86
+ test("shouldBlockPendingGate with explicit basePath sees pending gate after cwd drift", () => {
87
+ clearDiscussionFlowState(baseDirA);
88
+ clearDiscussionFlowState(baseDirB);
89
+
90
+ setPendingGate("depth_verification_M001_confirm", baseDirA);
91
+ process.chdir(baseDirB);
92
+
93
+ const result = shouldBlockPendingGate("write", "M001", false, baseDirA);
94
+
95
+ assert.equal(result.block, true, "explicit basePath should resolve to baseDirA's pending gate state");
96
+ });
97
+ });
@@ -351,8 +351,9 @@ export function shouldBlockPendingGate(
351
351
  toolName: string,
352
352
  milestoneId: string | null,
353
353
  queuePhaseActive?: boolean,
354
+ basePath: string = process.cwd(),
354
355
  ): { block: boolean; reason?: string } {
355
- return shouldBlockPendingGateInSnapshot(currentWriteGateSnapshot(), toolName, milestoneId, queuePhaseActive);
356
+ return shouldBlockPendingGateInSnapshot(currentWriteGateSnapshot(basePath), toolName, milestoneId, queuePhaseActive);
356
357
  }
357
358
 
358
359
  export function shouldBlockPendingGateInSnapshot(
@@ -386,8 +387,9 @@ export function shouldBlockPendingGateBash(
386
387
  command: string,
387
388
  milestoneId: string | null,
388
389
  queuePhaseActive?: boolean,
390
+ basePath: string = process.cwd(),
389
391
  ): { block: boolean; reason?: string } {
390
- return shouldBlockPendingGateBashInSnapshot(currentWriteGateSnapshot(), command, milestoneId, queuePhaseActive);
392
+ return shouldBlockPendingGateBashInSnapshot(currentWriteGateSnapshot(basePath), command, milestoneId, queuePhaseActive);
391
393
  }
392
394
 
393
395
  export function shouldBlockPendingGateBashInSnapshot(
@@ -444,6 +446,7 @@ export function shouldBlockContextWrite(
444
446
  inputPath: string,
445
447
  milestoneId: string | null,
446
448
  _queuePhaseActive?: boolean,
449
+ basePath: string = process.cwd(),
447
450
  ): { block: boolean; reason?: string } {
448
451
  if (toolName !== "write") return { block: false };
449
452
  if (!MILESTONE_CONTEXT_RE.test(inputPath)) return { block: false };
@@ -460,7 +463,7 @@ export function shouldBlockContextWrite(
460
463
  };
461
464
  }
462
465
 
463
- if (isMilestoneDepthVerified(targetMilestoneId)) return { block: false };
466
+ if (isMilestoneDepthVerified(targetMilestoneId, basePath)) return { block: false };
464
467
 
465
468
  return {
466
469
  block: true,
@@ -483,8 +486,9 @@ export function shouldBlockContextArtifactSave(
483
486
  artifactType: string,
484
487
  milestoneId: string | null,
485
488
  sliceId?: string | null,
489
+ basePath: string = process.cwd(),
486
490
  ): { block: boolean; reason?: string } {
487
- return shouldBlockContextArtifactSaveInSnapshot(currentWriteGateSnapshot(), artifactType, milestoneId, sliceId);
491
+ return shouldBlockContextArtifactSaveInSnapshot(currentWriteGateSnapshot(basePath), artifactType, milestoneId, sliceId);
488
492
  }
489
493
 
490
494
  export function shouldBlockContextArtifactSaveInSnapshot(
@@ -753,6 +753,11 @@ function columnExists(db: DbAdapter, table: string, column: string): boolean {
753
753
  return rows.some((row) => row["name"] === column);
754
754
  }
755
755
 
756
+ function formatFtsUnavailableError(err: unknown): string {
757
+ const message = err instanceof Error ? err.message : String(err);
758
+ return message.replace(/\bmoduel\s*:\s*/gi, "module: ");
759
+ }
760
+
756
761
  /**
757
762
  * Create the FTS5 virtual table for memories plus the triggers that keep it
758
763
  * in sync with the base table. FTS5 may be unavailable on stripped-down
@@ -787,7 +792,7 @@ export function tryCreateMemoriesFts(db: DbAdapter): boolean {
787
792
  `);
788
793
  return true;
789
794
  } catch (err) {
790
- logWarning("db", `FTS5 unavailable — memory queries will use LIKE fallback: ${(err as Error).message}`);
795
+ logWarning("db", `FTS5 unavailable — memory queries will use LIKE fallback: ${formatFtsUnavailableError(err)}`);
791
796
  return false;
792
797
  }
793
798
  }
@@ -1740,6 +1745,35 @@ export function closeDatabase(): void {
1740
1745
  _lastDbPhase = null;
1741
1746
  }
1742
1747
 
1748
+ /**
1749
+ * Re-open the active database connection from disk.
1750
+ *
1751
+ * Auto-mode can observe artifacts written by a workflow server running in a
1752
+ * different process before its long-lived singleton has re-synchronized. The
1753
+ * recovery path uses this to force the next state derivation to read from the
1754
+ * current on-disk database instead of continuing with a possibly stale handle.
1755
+ */
1756
+ export function refreshOpenDatabaseFromDisk(): boolean {
1757
+ if (!currentDb || !currentPath) return false;
1758
+ if (currentPath === ":memory:") return false;
1759
+
1760
+ const dbPath = currentPath;
1761
+ const identityKey = _currentIdentityKey;
1762
+
1763
+ try {
1764
+ closeDatabase();
1765
+ const opened = openDatabase(dbPath);
1766
+ if (opened && identityKey && currentDb) {
1767
+ _dbCache.set(identityKey, { dbPath, db: currentDb });
1768
+ _currentIdentityKey = identityKey;
1769
+ }
1770
+ return opened;
1771
+ } catch (e) {
1772
+ logWarning("db", `database refresh failed: ${(e as Error).message}`);
1773
+ return false;
1774
+ }
1775
+ }
1776
+
1743
1777
  /** Run a full VACUUM — call sparingly (e.g. after milestone completion). */
1744
1778
  export function vacuumDatabase(): void {
1745
1779
  if (!currentDb) return;
@@ -70,6 +70,7 @@ import {
70
70
  } from "./preparation.js";
71
71
  import { verifyExpectedArtifact } from "./auto-recovery.js";
72
72
  import { createWorkspace, scopeMilestone, type MilestoneScope } from "./workspace.js";
73
+ import { getPendingGate, extractDepthVerificationMilestoneId } from "./bootstrap/write-gate.js";
73
74
 
74
75
  // ─── Re-exports (preserve public API for existing importers) ────────────────
75
76
  export {
@@ -410,6 +411,15 @@ export async function checkDeepProjectSetupAfterTurn(
410
411
  }
411
412
  }
412
413
 
414
+ // R2: a depth-verification gate is still pending — the LLM emitted the
415
+ // confirmation question (via ask_user_questions or plain chat) but the user
416
+ // has not approved yet. Returning false keeps the entry in the
417
+ // pendingDeepProjectSetupMap so the next user message can resume.
418
+ const pendingGateId = getPendingGate(entry.basePath);
419
+ if (pendingGateId) {
420
+ return false;
421
+ }
422
+
413
423
  return dispatchNextDeepProjectSetupStage(entry);
414
424
  }
415
425
 
@@ -494,6 +504,25 @@ export function checkAutoStartAfterDiscuss(): boolean {
494
504
  const roadmapFile = existsSync(roadmapFilePath) ? roadmapFilePath : null;
495
505
  if (!contextFile && !roadmapFile) return false; // neither artifact yet — keep waiting
496
506
 
507
+ // Gate 1a: a depth-verification gate is still pending for THIS milestone — the
508
+ // LLM emitted the confirmation question (via ask_user_questions or plain chat)
509
+ // but the user has not answered yet. Advancing now would skip the gate and
510
+ // race ahead with unverified context.
511
+ const basePathForGate = entry.scope.workspace.projectRoot;
512
+ const pendingGateId = getPendingGate(basePathForGate);
513
+ if (pendingGateId) {
514
+ const pendingMilestoneId = extractDepthVerificationMilestoneId(pendingGateId);
515
+ // Block advancement if the gate is for THIS milestone, OR if it's a
516
+ // project/requirements gate (no milestone id encoded) for the deep setup flow.
517
+ const isProjectGate =
518
+ pendingGateId === "depth_verification_project_confirm" ||
519
+ pendingGateId === "depth_verification_requirements_confirm" ||
520
+ pendingGateId === "depth_verification_research_decision_confirm";
521
+ if (pendingMilestoneId === milestoneId || isProjectGate) {
522
+ return false;
523
+ }
524
+ }
525
+
497
526
  // Gate 1b: Discriminate plan-blocked from discuss-incomplete when the DB row is queued.
498
527
  // If the DB is available and the row is still "queued" but CONTEXT.md already exists on
499
528
  // disk, the discuss phase completed but gsd_plan_milestone was hard-blocked by the
@@ -628,6 +657,24 @@ export function checkAutoStartAfterDiscuss(): boolean {
628
657
  try { unlinkSync(manifestPath); } catch (e) { logWarning("guided", `manifest unlink failed: ${(e as Error).message}`); }
629
658
  }
630
659
 
660
+ // R3b: belt-and-suspenders for silent registration failure. The discuss flow
661
+ // finished and STATE.md exists, but the milestone may never have landed in
662
+ // the DB. Without this guard, the user sees "Milestone M001 ready." and then
663
+ // /gsd reports "No Active Milestone".
664
+ if (isDbAvailable()) {
665
+ const milestoneRow = getMilestone(milestoneId);
666
+ if (!milestoneRow) {
667
+ ctx.ui.notify(
668
+ `Milestone ${milestoneId}: discuss artifacts on disk but no DB row exists. ` +
669
+ `PROJECT.md may have failed to register milestones. ` +
670
+ `Re-save PROJECT.md with canonical "- [ ] M001: Title — One-liner" lines, ` +
671
+ `then re-run /gsd to recover.`,
672
+ "error",
673
+ );
674
+ return false;
675
+ }
676
+ }
677
+
631
678
  pendingAutoStartMap.delete(basePath);
632
679
  ctx.ui.notify(`Milestone ${milestoneId} ready.`, "success");
633
680
  startAutoDetached(ctx, pi, basePath, false, { step });
@@ -37,6 +37,7 @@ export interface PausedSessionMetadata {
37
37
  activeRunDir?: string | null;
38
38
  autoStartTime?: number;
39
39
  milestoneLock?: string | null;
40
+ pauseReason?: string;
40
41
  }
41
42
 
42
43
  export interface InterruptedSessionAssessment {
@@ -188,9 +188,14 @@ export function resolveDir(parentDir: string, idPrefix: string): string | null {
188
188
  // Exact match first (current convention: bare ID)
189
189
  const exact = entries.find(e => e.isDirectory() && e.name === idPrefix);
190
190
  if (exact) return exact.name;
191
+ const idLower = idPrefix.toLowerCase();
192
+ const exactCaseInsensitive = entries.find(
193
+ e => e.isDirectory() && e.name.toLowerCase() === idLower
194
+ );
195
+ if (exactCaseInsensitive) return exactCaseInsensitive.name;
191
196
  // Prefix match for legacy descriptor dirs: M001-SOMETHING
192
197
  const prefixed = entries.find(
193
- e => e.isDirectory() && e.name.startsWith(idPrefix + "-")
198
+ e => e.isDirectory() && e.name.toLowerCase().startsWith(idLower + "-")
194
199
  );
195
200
  return prefixed ? prefixed.name : null;
196
201
  } catch {
@@ -0,0 +1,32 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { _buildAbortedPauseContext } from "../bootstrap/agent-end-recovery.js";
5
+ import { _buildCancelledUnitStopReason } from "../auto/phases.js";
6
+
7
+ test("aborted agent_end maps errorMessage into structured aborted pause context", () => {
8
+ const withMessage = _buildAbortedPauseContext({ errorMessage: "provider aborted request" });
9
+ assert.deepEqual(withMessage, {
10
+ message: "provider aborted request",
11
+ category: "aborted",
12
+ isTransient: true,
13
+ });
14
+
15
+ const withoutMessage = _buildAbortedPauseContext({});
16
+ assert.deepEqual(withoutMessage, {
17
+ message: "Operation aborted",
18
+ category: "aborted",
19
+ isTransient: true,
20
+ });
21
+ });
22
+
23
+ test("cancelled non-session failures are labeled as unit aborts (not session-creation failures)", () => {
24
+ const cancelled = _buildCancelledUnitStopReason("execute-task", "M001-S001-T001", {
25
+ category: "aborted",
26
+ message: "tool invocation cancelled",
27
+ });
28
+
29
+ assert.match(cancelled.notifyMessage, /aborted after dispatch/);
30
+ assert.equal(cancelled.stopReason, "Unit aborted: tool invocation cancelled");
31
+ assert.equal(cancelled.loopReason, "unit-aborted");
32
+ });