gsd-pi 2.79.0 → 2.80.0

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 (151) hide show
  1. package/README.md +94 -47
  2. package/dist/resources/.managed-resources-content-hash +1 -1
  3. package/dist/resources/extensions/gsd/auto/contracts.js +1 -0
  4. package/dist/resources/extensions/gsd/auto/orchestrator.js +146 -0
  5. package/dist/resources/extensions/gsd/auto/phases.js +61 -7
  6. package/dist/resources/extensions/gsd/auto/session.js +8 -0
  7. package/dist/resources/extensions/gsd/auto-artifact-paths.js +2 -2
  8. package/dist/resources/extensions/gsd/auto-dispatch.js +2 -0
  9. package/dist/resources/extensions/gsd/auto-prompts.js +52 -29
  10. package/dist/resources/extensions/gsd/auto-recovery.js +63 -55
  11. package/dist/resources/extensions/gsd/auto-runtime-state.js +4 -0
  12. package/dist/resources/extensions/gsd/auto-start.js +3 -2
  13. package/dist/resources/extensions/gsd/auto.js +159 -2
  14. package/dist/resources/extensions/gsd/bootstrap/agent-end-recovery.js +9 -1
  15. package/dist/resources/extensions/gsd/bootstrap/exec-tools.js +2 -2
  16. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +41 -45
  17. package/dist/resources/extensions/gsd/bootstrap/write-gate.js +8 -8
  18. package/dist/resources/extensions/gsd/commands/context.js +1 -1
  19. package/dist/resources/extensions/gsd/gsd-db.js +34 -1
  20. package/dist/resources/extensions/gsd/guided-flow.js +40 -0
  21. package/dist/resources/extensions/gsd/paths.js +5 -1
  22. package/dist/resources/extensions/gsd/post-execution-checks.js +25 -6
  23. package/dist/resources/extensions/gsd/preferences-types.js +20 -2
  24. package/dist/resources/extensions/gsd/preferences-validation.js +3 -3
  25. package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +82 -2
  26. package/dist/resources/extensions/gsd/unit-context-composer.js +32 -0
  27. package/dist/resources/extensions/gsd/unit-context-manifest.js +21 -0
  28. package/dist/resources/extensions/gsd/uok/audit.js +23 -9
  29. package/dist/resources/extensions/gsd/uok/contracts.js +69 -1
  30. package/dist/resources/extensions/gsd/uok/dispatch-envelope.js +3 -0
  31. package/dist/resources/extensions/gsd/uok/loop-adapter.js +48 -33
  32. package/dist/resources/extensions/gsd/uok/timeline.js +125 -0
  33. package/dist/resources/extensions/shared/gsd-phase-state.js +45 -3
  34. package/dist/resources/extensions/shared/interview-ui.js +15 -4
  35. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  36. package/dist/web/standalone/.next/BUILD_ID +1 -1
  37. package/dist/web/standalone/.next/app-path-routes-manifest.json +9 -9
  38. package/dist/web/standalone/.next/build-manifest.json +2 -2
  39. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  40. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  41. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  42. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  43. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  44. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  45. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  46. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  47. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  48. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  49. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  51. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  55. package/dist/web/standalone/.next/server/app/index.html +1 -1
  56. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  57. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  59. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  61. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app-paths-manifest.json +9 -9
  63. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  64. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  65. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  66. package/package.json +1 -1
  67. package/packages/daemon/package.json +2 -2
  68. package/packages/mcp-server/dist/workflow-tools.d.ts +1 -1
  69. package/packages/mcp-server/dist/workflow-tools.d.ts.map +1 -1
  70. package/packages/mcp-server/dist/workflow-tools.js +53 -0
  71. package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
  72. package/packages/mcp-server/package.json +2 -2
  73. package/packages/mcp-server/src/workflow-tools.test.ts +129 -2
  74. package/packages/mcp-server/src/workflow-tools.ts +81 -0
  75. package/packages/mcp-server/tsconfig.tsbuildinfo +1 -1
  76. package/packages/native/package.json +1 -1
  77. package/packages/pi-agent-core/package.json +1 -1
  78. package/packages/pi-ai/package.json +1 -1
  79. package/packages/pi-coding-agent/package.json +1 -1
  80. package/packages/pi-tui/package.json +1 -1
  81. package/packages/rpc-client/package.json +1 -1
  82. package/pkg/package.json +1 -1
  83. package/src/resources/extensions/gsd/auto/contracts.ts +87 -0
  84. package/src/resources/extensions/gsd/auto/loop-deps.ts +10 -3
  85. package/src/resources/extensions/gsd/auto/orchestrator.ts +161 -0
  86. package/src/resources/extensions/gsd/auto/phases.ts +88 -9
  87. package/src/resources/extensions/gsd/auto/session.ts +11 -0
  88. package/src/resources/extensions/gsd/auto-artifact-paths.ts +2 -2
  89. package/src/resources/extensions/gsd/auto-dispatch.ts +1 -0
  90. package/src/resources/extensions/gsd/auto-prompts.ts +106 -28
  91. package/src/resources/extensions/gsd/auto-recovery.ts +59 -53
  92. package/src/resources/extensions/gsd/auto-runtime-state.ts +7 -0
  93. package/src/resources/extensions/gsd/auto-start.ts +3 -2
  94. package/src/resources/extensions/gsd/auto.ts +167 -1
  95. package/src/resources/extensions/gsd/bootstrap/agent-end-recovery.ts +14 -1
  96. package/src/resources/extensions/gsd/bootstrap/exec-tools.ts +2 -2
  97. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +49 -46
  98. package/src/resources/extensions/gsd/bootstrap/tests/write-gate-shouldblock-basepath.test.ts +97 -0
  99. package/src/resources/extensions/gsd/bootstrap/write-gate.ts +8 -4
  100. package/src/resources/extensions/gsd/commands/context.ts +1 -1
  101. package/src/resources/extensions/gsd/gsd-db.ts +35 -1
  102. package/src/resources/extensions/gsd/guided-flow.ts +47 -0
  103. package/src/resources/extensions/gsd/interrupted-session.ts +1 -0
  104. package/src/resources/extensions/gsd/paths.ts +6 -1
  105. package/src/resources/extensions/gsd/post-execution-checks.ts +31 -6
  106. package/src/resources/extensions/gsd/preferences-types.ts +23 -4
  107. package/src/resources/extensions/gsd/preferences-validation.ts +3 -3
  108. package/src/resources/extensions/gsd/tests/auto-abort-pause-regression.test.ts +32 -0
  109. package/src/resources/extensions/gsd/tests/auto-orchestrator.test.ts +353 -0
  110. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +108 -1
  111. package/src/resources/extensions/gsd/tests/auto-runtime-state.test.ts +39 -0
  112. package/src/resources/extensions/gsd/tests/auto-session-encapsulation.test.ts +3 -0
  113. package/src/resources/extensions/gsd/tests/bootstrap-derive-state-db-open.test.ts +2 -2
  114. package/src/resources/extensions/gsd/tests/check-auto-start-pending-gate.test.ts +203 -0
  115. package/src/resources/extensions/gsd/tests/check-auto-start-ready-guard.test.ts +148 -0
  116. package/src/resources/extensions/gsd/tests/current-directory-root-homedir-fallback.test.ts +63 -0
  117. package/src/resources/extensions/gsd/tests/deep-planning-mode-dispatch.test.ts +42 -0
  118. package/src/resources/extensions/gsd/tests/deep-project-auto-loop.test.ts +63 -2
  119. package/src/resources/extensions/gsd/tests/execute-summary-save-empty-project.test.ts +109 -0
  120. package/src/resources/extensions/gsd/tests/gsd-db.test.ts +95 -0
  121. package/src/resources/extensions/gsd/tests/guided-flow-prompt-consolidation.test.ts +14 -0
  122. package/src/resources/extensions/gsd/tests/integration/auto-recovery.test.ts +79 -0
  123. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +134 -0
  124. package/src/resources/extensions/gsd/tests/parallel-skill-prompt-integration.test.ts +8 -0
  125. package/src/resources/extensions/gsd/tests/paused-session-via-db.test.ts +2 -0
  126. package/src/resources/extensions/gsd/tests/plan-slice.test.ts +27 -0
  127. package/src/resources/extensions/gsd/tests/post-execution-checks.test.ts +46 -0
  128. package/src/resources/extensions/gsd/tests/pre-exec-gate-loop.test.ts +3 -0
  129. package/src/resources/extensions/gsd/tests/register-hooks-compaction-checkpoint.test.ts +85 -0
  130. package/src/resources/extensions/gsd/tests/run-uat-composer.test.ts +2 -0
  131. package/src/resources/extensions/gsd/tests/subagent-model-dispatch.test.ts +59 -0
  132. package/src/resources/extensions/gsd/tests/unit-context-composer.test.ts +38 -0
  133. package/src/resources/extensions/gsd/tests/unit-context-manifest.test.ts +32 -0
  134. package/src/resources/extensions/gsd/tests/uok-contracts.test.ts +109 -1
  135. package/src/resources/extensions/gsd/tests/uok-loop-adapter-writer.test.ts +98 -0
  136. package/src/resources/extensions/gsd/tests/workflow-tool-executors.test.ts +132 -3
  137. package/src/resources/extensions/gsd/tests/worktree-path-injection.test.ts +3 -0
  138. package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +84 -1
  139. package/src/resources/extensions/gsd/unit-context-composer.ts +49 -0
  140. package/src/resources/extensions/gsd/unit-context-manifest.ts +34 -0
  141. package/src/resources/extensions/gsd/uok/audit.ts +25 -9
  142. package/src/resources/extensions/gsd/uok/contracts.ts +105 -0
  143. package/src/resources/extensions/gsd/uok/dispatch-envelope.ts +4 -0
  144. package/src/resources/extensions/gsd/uok/loop-adapter.ts +60 -45
  145. package/src/resources/extensions/gsd/uok/timeline.ts +158 -0
  146. package/src/resources/extensions/shared/gsd-phase-state.ts +56 -3
  147. package/src/resources/extensions/shared/interview-ui.ts +18 -5
  148. package/src/resources/extensions/shared/tests/gsd-phase-state.test.ts +43 -1
  149. package/src/resources/extensions/shared/tests/interview-notes-loop.test.ts +41 -0
  150. /package/dist/web/standalone/.next/static/{J-CU-p_sp45CJHT3R9TJS → V-3Ehy4B24f9FCGiLPWIM}/_buildManifest.js +0 -0
  151. /package/dist/web/standalone/.next/static/{J-CU-p_sp45CJHT3R9TJS → V-3Ehy4B24f9FCGiLPWIM}/_ssgManifest.js +0 -0
@@ -7,7 +7,7 @@ import { randomUUID } from "node:crypto";
7
7
 
8
8
  import { verifyExpectedArtifact, hasImplementationArtifacts, resolveExpectedArtifactPath, diagnoseExpectedArtifact, buildLoopRemediationSteps, writeBlockerPlaceholder } from "../auto-recovery.ts";
9
9
  import { resolveMilestoneFile } from "../paths.ts";
10
- import { openDatabase, closeDatabase, insertMilestone, insertSlice, insertGateRow } from "../gsd-db.ts";
10
+ import { openDatabase, closeDatabase, insertMilestone, insertSlice, insertGateRow, insertTask } from "../gsd-db.ts";
11
11
  import { clearParseCache } from "../files.ts";
12
12
  import { parseRoadmap } from "../parsers-legacy.ts";
13
13
  import { invalidateAllCaches } from "../cache.ts";
@@ -90,6 +90,46 @@ test("resolveExpectedArtifactPath returns correct path for plan-slice", () => {
90
90
  }
91
91
  });
92
92
 
93
+ test("plan-slice artifact resolution handles lowercase unit IDs against uppercase paths", () => {
94
+ const base = makeTmpBase();
95
+ try {
96
+ const sliceDir = join(base, ".gsd", "milestones", "M001", "slices", "S01");
97
+ const tasksDir = join(sliceDir, "tasks");
98
+ writeFileSync(join(sliceDir, "S01-PLAN.md"), [
99
+ "# S01: Test Slice",
100
+ "",
101
+ "## Tasks",
102
+ "",
103
+ "- [ ] **T01: Implement feature** `est:1h`",
104
+ ].join("\n"));
105
+ writeFileSync(join(tasksDir, "T01-PLAN.md"), "# T01 Plan");
106
+
107
+ const artifactPath = resolveExpectedArtifactPath("plan-slice", "m001/s01", base);
108
+ assert.ok(
109
+ artifactPath?.endsWith(".gsd/milestones/M001/slices/S01/S01-PLAN.md"),
110
+ "lowercase unit IDs should resolve to the existing uppercase artifact path",
111
+ );
112
+
113
+ const diagnostic = diagnoseExpectedArtifact("plan-slice", "m001/s01", base);
114
+ assert.ok(
115
+ diagnostic?.includes(".gsd/milestones/M001/slices/S01/S01-PLAN.md"),
116
+ "diagnostic should report the existing uppercase artifact path",
117
+ );
118
+ assert.ok(
119
+ diagnostic?.includes("task plans"),
120
+ "diagnostic should mention task plans because slice plan alone is insufficient",
121
+ );
122
+
123
+ assert.equal(
124
+ verifyExpectedArtifact("plan-slice", "m001/s01", base),
125
+ true,
126
+ "verification should pass when the uppercase slice plan and task plans exist",
127
+ );
128
+ } finally {
129
+ cleanup(base);
130
+ }
131
+ });
132
+
93
133
  test("resolveExpectedArtifactPath returns null for unknown type", () => {
94
134
  const base = makeTmpBase();
95
135
  try {
@@ -764,6 +804,73 @@ test("hasImplementationArtifacts finds implementation commits when .gsd/ is giti
764
804
  }
765
805
  });
766
806
 
807
+ test("hasImplementationArtifacts binds GSD-Task trailer to milestone via DB state when .gsd/ is gitignored", () => {
808
+ const base = makeGitBase();
809
+ try {
810
+ writeFileSync(join(base, ".git", "info", "exclude"), ".gsd/\n");
811
+ mkdirSync(join(base, ".gsd"), { recursive: true });
812
+ openDatabase(join(base, ".gsd", "gsd.db"));
813
+ insertMilestone({ id: "M001", title: "Milestone One", status: "active" });
814
+ insertSlice({
815
+ id: "S01",
816
+ milestoneId: "M001",
817
+ title: "Slice One",
818
+ status: "complete",
819
+ risk: "low",
820
+ depends: [],
821
+ });
822
+ insertTask({
823
+ id: "T01",
824
+ sliceId: "S01",
825
+ milestoneId: "M001",
826
+ title: "Task One",
827
+ status: "complete",
828
+ });
829
+
830
+ mkdirSync(join(base, "src"), { recursive: true });
831
+ writeFileSync(join(base, "src", "feature.ts"), "export function feature() {}\n");
832
+ execFileSync("git", ["add", "."], { cwd: base, stdio: "ignore" });
833
+ execFileSync(
834
+ "git",
835
+ ["commit", "-m", "feat: add feature\n\nGSD-Task: S01/T01"],
836
+ { cwd: base, stdio: "ignore" },
837
+ );
838
+
839
+ const result = hasImplementationArtifacts(base, "M001");
840
+ assert.equal(
841
+ result,
842
+ "present",
843
+ "DB task ownership should bind S01/T01 implementation commits to M001 without explicit M001 text",
844
+ );
845
+ } finally {
846
+ cleanup(base);
847
+ }
848
+ });
849
+
850
+ test("hasImplementationArtifacts does not bind GSD-Task trailer without milestone ownership evidence", () => {
851
+ const base = makeGitBase();
852
+ try {
853
+ writeFileSync(join(base, ".git", "info", "exclude"), ".gsd/\n");
854
+ mkdirSync(join(base, "src"), { recursive: true });
855
+ writeFileSync(join(base, "src", "feature.ts"), "export function feature() {}\n");
856
+ execFileSync("git", ["add", "."], { cwd: base, stdio: "ignore" });
857
+ execFileSync(
858
+ "git",
859
+ ["commit", "-m", "feat: add feature\n\nGSD-Task: S01/T01"],
860
+ { cwd: base, stdio: "ignore" },
861
+ );
862
+
863
+ const result = hasImplementationArtifacts(base, "M001");
864
+ assert.equal(
865
+ result,
866
+ "absent",
867
+ "S01/T01 shape alone must not bind an implementation commit to M001",
868
+ );
869
+ } finally {
870
+ cleanup(base);
871
+ }
872
+ });
873
+
767
874
  test("hasImplementationArtifacts ignores malformed milestone IDs in commit-message fallback", () => {
768
875
  const base = makeGitBase();
769
876
  try {
@@ -0,0 +1,39 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+
4
+ import { autoSession, getAutoRuntimeSnapshot } from "../auto-runtime-state.ts";
5
+
6
+ test("getAutoRuntimeSnapshot includes orchestration phase when available", () => {
7
+ autoSession.reset();
8
+ autoSession.active = true;
9
+ autoSession.basePath = "/tmp/project";
10
+ autoSession.orchestration = {
11
+ async start() { return { kind: "advanced" as const }; },
12
+ async advance() { return { kind: "advanced" as const }; },
13
+ async resume() { return { kind: "advanced" as const }; },
14
+ async stop() { return { kind: "stopped" as const }; },
15
+ getStatus() {
16
+ return { phase: "running" as const, transitionCount: 3, lastTransitionAt: 123 };
17
+ },
18
+ };
19
+
20
+ const snap = getAutoRuntimeSnapshot();
21
+
22
+ assert.equal(snap.active, true);
23
+ assert.equal(snap.basePath, "/tmp/project");
24
+ assert.equal(snap.orchestrationPhase, "running");
25
+ assert.equal(snap.orchestrationTransitionCount, 3);
26
+ assert.equal(snap.orchestrationLastTransitionAt, 123);
27
+
28
+ autoSession.reset();
29
+ });
30
+
31
+ test("getAutoRuntimeSnapshot omits orchestration phase when seam not wired", () => {
32
+ autoSession.reset();
33
+
34
+ const snap = getAutoRuntimeSnapshot();
35
+
36
+ assert.equal(snap.orchestrationPhase, undefined);
37
+ assert.equal(snap.orchestrationTransitionCount, undefined);
38
+ assert.equal(snap.orchestrationLastTransitionAt, undefined);
39
+ });
@@ -201,6 +201,9 @@ test("AutoSession.toJSON() includes key diagnostic properties", () => {
201
201
  "basePath",
202
202
  "currentMilestoneId",
203
203
  "currentUnit",
204
+ "orchestrationPhase",
205
+ "orchestrationTransitionCount",
206
+ "orchestrationLastTransitionAt",
204
207
  ];
205
208
 
206
209
  const missing = requiredDiagnostics.filter(prop => !toJSONBody.includes(prop));
@@ -31,9 +31,9 @@ describe("bootstrap deriveState DB guards (#3844)", () => {
31
31
  const compactIdx = registerHooksSrc.indexOf('pi.on("session_before_compact"');
32
32
  assert.ok(compactIdx > -1, "register-hooks should define session_before_compact");
33
33
  const compactSection = extractSourceRegion(registerHooksSrc, 'pi.on("session_before_compact"');
34
- const ensureIdx = compactSection.indexOf("ensureDbOpen()");
34
+ const ensureIdx = compactSection.indexOf("ensureDbOpen(basePath)");
35
35
  const deriveIdx = compactSection.indexOf("deriveGsdState(basePath)");
36
- assert.ok(ensureIdx > -1, "session_before_compact should call ensureDbOpen()");
36
+ assert.ok(ensureIdx > -1, "session_before_compact should call ensureDbOpen(basePath)");
37
37
  assert.ok(deriveIdx > -1, "session_before_compact should derive state");
38
38
  assert.ok(ensureIdx < deriveIdx, "session_before_compact should open DB before deriveState");
39
39
  });
@@ -0,0 +1,203 @@
1
+ // GSD-2 + Regression tests for checkAutoStartAfterDiscuss Gate 1a (R2)
2
+ //
3
+ // When a depth-verification gate is still pending (the LLM emitted the
4
+ // confirmation question via ask_user_questions or plain chat but the user has
5
+ // not answered), checkAutoStartAfterDiscuss must NOT advance — even if
6
+ // CONTEXT.md and STATE.md are present on disk. Otherwise the LLM can render
7
+ // the question and the "Milestone M001 ready" phrase in the same turn and
8
+ // race past the gate.
9
+
10
+ import { describe, test, beforeEach, afterEach } from "node:test";
11
+ import assert from "node:assert/strict";
12
+ import { mkdtempSync, mkdirSync, rmSync, writeFileSync, realpathSync } from "node:fs";
13
+ import { join } from "node:path";
14
+ import { tmpdir } from "node:os";
15
+
16
+ import {
17
+ checkAutoStartAfterDiscuss,
18
+ setPendingAutoStart,
19
+ clearPendingAutoStart,
20
+ } from "../guided-flow.ts";
21
+ import { drainLogs } from "../workflow-logger.ts";
22
+ import {
23
+ openDatabase,
24
+ closeDatabase,
25
+ insertMilestone,
26
+ } from "../gsd-db.ts";
27
+ import {
28
+ setPendingGate,
29
+ clearPendingGate,
30
+ clearDiscussionFlowState,
31
+ } from "../bootstrap/write-gate.ts";
32
+
33
+ interface MockCapture {
34
+ notifies: Array<{ msg: string; level: string }>;
35
+ messages: Array<{ payload: any; options: any }>;
36
+ }
37
+
38
+ function mkCapture(): MockCapture {
39
+ return { notifies: [], messages: [] };
40
+ }
41
+
42
+ function mkCtx(cap: MockCapture): any {
43
+ return {
44
+ ui: {
45
+ notify: (msg: string, level: string) => {
46
+ cap.notifies.push({ msg, level });
47
+ },
48
+ },
49
+ };
50
+ }
51
+
52
+ function mkPi(cap: MockCapture): any {
53
+ return {
54
+ sendMessage: (payload: any, options: any) => {
55
+ cap.messages.push({ payload, options });
56
+ },
57
+ setActiveTools: () => undefined,
58
+ getActiveTools: () => [],
59
+ };
60
+ }
61
+
62
+ function mkBase(): string {
63
+ // realpathSync to normalize the macOS /var → /private/var symlink so the
64
+ // basePath we pass to setPendingGate matches what the workspace's
65
+ // realpath-normalized projectRoot will resolve to.
66
+ const base = realpathSync(mkdtempSync(join(tmpdir(), "gsd-gate1a-pending-")));
67
+ mkdirSync(join(base, ".gsd", "milestones", "M001"), { recursive: true });
68
+ // CONTEXT.md (Gate 1) and STATE.md (Gate 2) both present so the only
69
+ // possible blocker in these tests is the new Gate 1a.
70
+ writeFileSync(
71
+ join(base, ".gsd", "milestones", "M001", "M001-CONTEXT.md"),
72
+ "# M001: Pending Gate Test\n\nContext.\n",
73
+ );
74
+ writeFileSync(
75
+ join(base, ".gsd", "STATE.md"),
76
+ "# State\n\nactive: M001\n",
77
+ );
78
+ return base;
79
+ }
80
+
81
+ describe("checkAutoStartAfterDiscuss Gate 1a (pending depth-verification gate)", () => {
82
+ let base: string;
83
+ let cap: MockCapture;
84
+
85
+ beforeEach(() => {
86
+ clearPendingAutoStart();
87
+ drainLogs();
88
+ });
89
+
90
+ afterEach(() => {
91
+ closeDatabase();
92
+ clearPendingAutoStart();
93
+ if (base) {
94
+ try { clearDiscussionFlowState(base); } catch { /* */ }
95
+ try { clearPendingGate(base); } catch { /* */ }
96
+ rmSync(base, { recursive: true, force: true });
97
+ }
98
+ });
99
+
100
+ test("returns false while a depth_verification gate is pending for the same milestone", () => {
101
+ base = mkBase();
102
+ openDatabase(":memory:");
103
+ // DB row present + active so Gate 1b is not the blocker
104
+ insertMilestone({ id: "M001", title: "Pending Gate Test", status: "active" });
105
+
106
+ cap = mkCapture();
107
+ setPendingAutoStart(base, {
108
+ basePath: base,
109
+ milestoneId: "M001",
110
+ ctx: mkCtx(cap),
111
+ pi: mkPi(cap),
112
+ });
113
+
114
+ // The depth-verification gate for THIS milestone is still pending.
115
+ setPendingGate("depth_verification_M001_confirm", base);
116
+
117
+ const result = checkAutoStartAfterDiscuss();
118
+ assert.equal(result, false, "must not advance while the milestone gate is pending");
119
+ // Must not have announced "ready" or kicked auto.
120
+ const readyNotify = cap.notifies.find((n) => /ready\.?$/i.test(n.msg) && n.level === "success");
121
+ assert.equal(readyNotify, undefined, "must not announce 'ready' while gate pending");
122
+ });
123
+
124
+ test("returns false while a depth_verification_project_confirm gate is pending (deep setup)", () => {
125
+ base = mkBase();
126
+ openDatabase(":memory:");
127
+ insertMilestone({ id: "M001", title: "Pending Gate Test", status: "active" });
128
+
129
+ cap = mkCapture();
130
+ setPendingAutoStart(base, {
131
+ basePath: base,
132
+ milestoneId: "M001",
133
+ ctx: mkCtx(cap),
134
+ pi: mkPi(cap),
135
+ });
136
+
137
+ // A project-level depth-verification gate (no milestone id encoded) is pending —
138
+ // deep-setup interview has not been confirmed yet.
139
+ setPendingGate("depth_verification_project_confirm", base);
140
+
141
+ const result = checkAutoStartAfterDiscuss();
142
+ assert.equal(result, false, "must not advance while a project-level gate is pending");
143
+ });
144
+
145
+ test("returns false while a depth_verification_requirements_confirm gate is pending", () => {
146
+ base = mkBase();
147
+ openDatabase(":memory:");
148
+ insertMilestone({ id: "M001", title: "Pending Gate Test", status: "active" });
149
+
150
+ cap = mkCapture();
151
+ setPendingAutoStart(base, {
152
+ basePath: base,
153
+ milestoneId: "M001",
154
+ ctx: mkCtx(cap),
155
+ pi: mkPi(cap),
156
+ });
157
+
158
+ setPendingGate("depth_verification_requirements_confirm", base);
159
+
160
+ const result = checkAutoStartAfterDiscuss();
161
+ assert.equal(result, false, "must not advance while the requirements gate is pending");
162
+ });
163
+
164
+ test("Gate 1a does NOT trip when the pending gate is for a DIFFERENT milestone", () => {
165
+ base = mkBase();
166
+ openDatabase(":memory:");
167
+ // status: "queued" so that Gate 1b downstream of Gate 1a fires its
168
+ // recovery notify ("context file exists but milestone is still queued") —
169
+ // observing that notify proves we advanced past Gate 1a. If Gate 1a
170
+ // wrongly tripped on the M999 gate it would `return false` immediately
171
+ // and Gate 1b would never run, so the notify would be absent.
172
+ insertMilestone({ id: "M001", title: "Pending Gate Test", status: "queued" });
173
+
174
+ cap = mkCapture();
175
+ setPendingAutoStart(base, {
176
+ basePath: base,
177
+ milestoneId: "M001",
178
+ ctx: mkCtx(cap),
179
+ pi: mkPi(cap),
180
+ });
181
+
182
+ setPendingGate("depth_verification_M999_confirm", base);
183
+
184
+ const result = checkAutoStartAfterDiscuss();
185
+ assert.equal(result, false, "Gate 1b returns false (expected) — but only if Gate 1a let us through");
186
+
187
+ // Positive proof we passed Gate 1a: Gate 1b emitted its recovery notify
188
+ // about M001 (not M999 — the pending-gate milestone is irrelevant here).
189
+ const gate1bNotify = cap.notifies.find(n =>
190
+ n.level === "warning" && /M001.*context file exists but milestone is still queued/i.test(n.msg)
191
+ );
192
+ assert.ok(
193
+ gate1bNotify,
194
+ `expected Gate 1b warning notify about M001; got: ${JSON.stringify(cap.notifies)}`,
195
+ );
196
+
197
+ // Negative proof: no Gate 1a notification path exists in source today, but
198
+ // also assert no notify mentions M999 (the pending-gate milestone) — that
199
+ // would suggest Gate 1a is leaking the wrong milestone into messaging.
200
+ const m999Notify = cap.notifies.find(n => /M999/i.test(n.msg));
201
+ assert.equal(m999Notify, undefined, "no notify should reference M999 (the pending-gate milestone)");
202
+ });
203
+ });
@@ -0,0 +1,148 @@
1
+ // GSD-2 + Regression tests for checkAutoStartAfterDiscuss "ready" notify guard (R3b)
2
+ //
3
+ // Belt-and-suspenders: even when CONTEXT.md and STATE.md exist on disk, the
4
+ // "Milestone X ready." success notify must not fire when the milestone DB row
5
+ // is absent. Otherwise the user sees "ready" and then /gsd reports
6
+ // "No Active Milestone" because the milestone was never registered.
7
+
8
+ import { describe, test, beforeEach, afterEach } from "node:test";
9
+ import assert from "node:assert/strict";
10
+ import { mkdtempSync, mkdirSync, rmSync, writeFileSync, realpathSync } from "node:fs";
11
+ import { join } from "node:path";
12
+ import { tmpdir } from "node:os";
13
+
14
+ import {
15
+ checkAutoStartAfterDiscuss,
16
+ setPendingAutoStart,
17
+ clearPendingAutoStart,
18
+ } from "../guided-flow.ts";
19
+ import { drainLogs } from "../workflow-logger.ts";
20
+ import {
21
+ openDatabase,
22
+ closeDatabase,
23
+ insertMilestone,
24
+ } from "../gsd-db.ts";
25
+ import {
26
+ clearDiscussionFlowState,
27
+ clearPendingGate,
28
+ } from "../bootstrap/write-gate.ts";
29
+
30
+ interface MockCapture {
31
+ notifies: Array<{ msg: string; level: string }>;
32
+ messages: Array<{ payload: any; options: any }>;
33
+ }
34
+
35
+ function mkCapture(): MockCapture {
36
+ return { notifies: [], messages: [] };
37
+ }
38
+
39
+ function mkCtx(cap: MockCapture): any {
40
+ return {
41
+ ui: {
42
+ notify: (msg: string, level: string) => {
43
+ cap.notifies.push({ msg, level });
44
+ },
45
+ },
46
+ };
47
+ }
48
+
49
+ function mkPi(cap: MockCapture): any {
50
+ return {
51
+ sendMessage: (payload: any, options: any) => {
52
+ cap.messages.push({ payload, options });
53
+ },
54
+ setActiveTools: () => undefined,
55
+ getActiveTools: () => [],
56
+ };
57
+ }
58
+
59
+ function mkBase(): string {
60
+ // realpathSync to normalize the macOS /var → /private/var symlink so the
61
+ // basePath we pass matches what the workspace projectRoot resolves to.
62
+ const base = realpathSync(mkdtempSync(join(tmpdir(), "gsd-ready-guard-")));
63
+ mkdirSync(join(base, ".gsd", "milestones", "M001"), { recursive: true });
64
+ writeFileSync(
65
+ join(base, ".gsd", "milestones", "M001", "M001-CONTEXT.md"),
66
+ "# M001: Ready Guard Test\n\nContext.\n",
67
+ );
68
+ writeFileSync(
69
+ join(base, ".gsd", "STATE.md"),
70
+ "# State\n\nactive: M001\n",
71
+ );
72
+ return base;
73
+ }
74
+
75
+ describe("checkAutoStartAfterDiscuss ready-notify DB guard (R3b)", () => {
76
+ let base: string;
77
+ let cap: MockCapture;
78
+
79
+ beforeEach(() => {
80
+ clearPendingAutoStart();
81
+ drainLogs();
82
+ });
83
+
84
+ afterEach(() => {
85
+ closeDatabase();
86
+ clearPendingAutoStart();
87
+ if (base) {
88
+ try { clearDiscussionFlowState(base); } catch { /* */ }
89
+ try { clearPendingGate(base); } catch { /* */ }
90
+ rmSync(base, { recursive: true, force: true });
91
+ }
92
+ });
93
+
94
+ test("does not announce 'ready' when the milestone DB row is absent", () => {
95
+ base = mkBase();
96
+ // Open a fresh in-memory DB but DO NOT insertMilestone for M001.
97
+ openDatabase(":memory:");
98
+
99
+ cap = mkCapture();
100
+ setPendingAutoStart(base, {
101
+ basePath: base,
102
+ milestoneId: "M001",
103
+ ctx: mkCtx(cap),
104
+ pi: mkPi(cap),
105
+ });
106
+
107
+ const result = checkAutoStartAfterDiscuss();
108
+ assert.equal(result, false, "must return false when DB row missing");
109
+
110
+ // No success "ready" notify
111
+ const successReady = cap.notifies.find(
112
+ (n) => n.level === "success" && /ready\.?$/i.test(n.msg),
113
+ );
114
+ assert.equal(successReady, undefined, "must not announce 'ready' when DB row missing");
115
+
116
+ // An error notify must explain the missing DB row
117
+ const errorNotify = cap.notifies.find((n) => n.level === "error");
118
+ assert.ok(errorNotify, "must emit an error notify when the DB row is missing");
119
+ assert.match(
120
+ errorNotify!.msg,
121
+ /no DB row exists/i,
122
+ "error notify must mention the missing DB row",
123
+ );
124
+ assert.match(errorNotify!.msg, /M001/, "error notify must mention the milestone id");
125
+ });
126
+
127
+ test("announces 'ready' when DB row exists", () => {
128
+ base = mkBase();
129
+ openDatabase(":memory:");
130
+ insertMilestone({ id: "M001", title: "Ready Guard Test", status: "active" });
131
+
132
+ cap = mkCapture();
133
+ setPendingAutoStart(base, {
134
+ basePath: base,
135
+ milestoneId: "M001",
136
+ ctx: mkCtx(cap),
137
+ pi: mkPi(cap),
138
+ });
139
+
140
+ const result = checkAutoStartAfterDiscuss();
141
+ assert.equal(result, true, "must return true on the happy path");
142
+
143
+ const successReady = cap.notifies.find(
144
+ (n) => n.level === "success" && /Milestone\s+M001\s+ready/i.test(n.msg),
145
+ );
146
+ assert.ok(successReady, "must announce 'Milestone M001 ready.' on success");
147
+ });
148
+ });
@@ -0,0 +1,63 @@
1
+ /**
2
+ * Regression test — currentDirectoryRoot() uses os.homedir() as fallback
3
+ * when process.cwd() throws (e.g. worktree teardown deletes the cwd).
4
+ *
5
+ * Before the fix, the catch block used `process.env.HOME ?? "/"`. On
6
+ * Windows, HOME is typically unset so this resolved to "/", an invalid
7
+ * path. After the fix, os.homedir() is used — it checks USERPROFILE,
8
+ * HOMEDRIVE+HOMEPATH, etc., returning a valid path on all platforms.
9
+ *
10
+ * The test monkey-patches process.cwd() to throw ENOENT, simulating a
11
+ * deleted cwd. currentDirectoryRoot() must NOT propagate the raw error;
12
+ * instead it falls back to homedir(), which validateDirectory correctly
13
+ * rejects as "blocked", yielding GSDNoProjectError — the same controlled
14
+ * error path handlers already know how to catch.
15
+ *
16
+ * The error message is also asserted to match validateDirectory(homedir()),
17
+ * confirming the fallback resolved to homedir() specifically (not "/" or
18
+ * any other path).
19
+ */
20
+ import { describe, test, beforeEach, afterEach } from "node:test";
21
+ import assert from "node:assert/strict";
22
+ import { homedir } from "node:os";
23
+
24
+ import { currentDirectoryRoot, GSDNoProjectError } from "../commands/context.ts";
25
+ import { validateDirectory } from "../validate-directory.ts";
26
+
27
+ describe("currentDirectoryRoot() homedir() fallback on deleted cwd", () => {
28
+ const originalCwd = process.cwd.bind(process);
29
+
30
+ beforeEach(() => {
31
+ process.cwd = () => {
32
+ const err = new Error("ENOENT: no such file or directory, uv_cwd");
33
+ (err as NodeJS.ErrnoException).code = "ENOENT";
34
+ throw err;
35
+ };
36
+ });
37
+
38
+ afterEach(() => {
39
+ process.cwd = originalCwd;
40
+ });
41
+
42
+ test("does not propagate ENOENT — throws GSDNoProjectError via homedir() fallback", () => {
43
+ const expected = validateDirectory(homedir());
44
+ assert.equal(expected.severity, "blocked", "homedir() itself should be blocked");
45
+
46
+ assert.throws(
47
+ () => currentDirectoryRoot(),
48
+ (err: unknown) => {
49
+ assert.ok(
50
+ err instanceof GSDNoProjectError,
51
+ `expected GSDNoProjectError, got: ${err}`,
52
+ );
53
+ assert.equal(
54
+ (err as Error).message,
55
+ expected.reason ?? "GSD must be run inside a project directory.",
56
+ "error message must match validateDirectory(homedir()), confirming homedir() was the fallback",
57
+ );
58
+ return true;
59
+ },
60
+ "should throw GSDNoProjectError (homedir fallback validated), not raw ENOENT",
61
+ );
62
+ });
63
+ });
@@ -14,6 +14,7 @@ import {
14
14
  DISPATCH_RULES,
15
15
  getDeepStageGate,
16
16
  hasPendingDeepStage,
17
+ resolveDispatch,
17
18
  setResearchProjectPromptBuilderForTest,
18
19
  type DispatchContext,
19
20
  } from "../auto-dispatch.ts";
@@ -248,6 +249,11 @@ function writeCapturedDeepPrefs(base: string): void {
248
249
  );
249
250
  }
250
251
 
252
+ function writeSkippedProjectResearchDecision(base: string): void {
253
+ mkdirSync(join(base, ".gsd", "runtime"), { recursive: true });
254
+ writeFileSync(join(base, ".gsd", "runtime", "research-decision.json"), JSON.stringify({ decision: "skip" }));
255
+ }
256
+
251
257
  function makeCtx(
252
258
  basePath: string,
253
259
  prefs: GSDPreferences | undefined,
@@ -785,6 +791,42 @@ test("Deep mode: research-project DOES dispatch when only 3 of 4 research files
785
791
  assert.ok(result && result.action === "dispatch", "any missing dimension must trigger re-run");
786
792
  });
787
793
 
794
+ test("Deep mode: queued milestone without CONTEXT.md routes to milestone research after project setup", async (t) => {
795
+ const base = makeIsolatedBaseWithCleanup(t);
796
+
797
+ writeCapturedDeepPrefs(base);
798
+ writeValidProject(base);
799
+ writeValidRequirements(base);
800
+ writeSkippedProjectResearchDecision(base);
801
+
802
+ const prefs = { planning_depth: "deep" } as GSDPreferences;
803
+ const result = await resolveDispatch(makeCtx(base, prefs));
804
+
805
+ assert.equal(result.action, "dispatch");
806
+ if (result.action === "dispatch") {
807
+ assert.equal(result.unitType, "research-milestone");
808
+ assert.equal(result.unitId, "M001");
809
+ }
810
+ });
811
+
812
+ test("Deep mode: queued milestone without CONTEXT.md can route directly to milestone planning", async (t) => {
813
+ const base = makeIsolatedBaseWithCleanup(t);
814
+
815
+ writeCapturedDeepPrefs(base);
816
+ writeValidProject(base);
817
+ writeValidRequirements(base);
818
+ writeSkippedProjectResearchDecision(base);
819
+
820
+ const prefs = { planning_depth: "deep", phases: { skip_research: true } } as GSDPreferences;
821
+ const result = await resolveDispatch(makeCtx(base, prefs));
822
+
823
+ assert.equal(result.action, "dispatch");
824
+ if (result.action === "dispatch") {
825
+ assert.equal(result.unitType, "plan-milestone");
826
+ assert.equal(result.unitId, "M001");
827
+ }
828
+ });
829
+
788
830
  // ─── centralized deep-stage gate ─────────────────────────────────────────
789
831
 
790
832
  test("Deep mode gate reports the earliest missing section", (t) => {