gsd-pi 2.47.0-dev.04be8c9 → 2.47.0-dev.8cfe772

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 (58) hide show
  1. package/dist/resources/extensions/gsd/auto-start.js +8 -1
  2. package/dist/resources/extensions/gsd/guided-flow.js +78 -2
  3. package/dist/resources/extensions/gsd/session-forensics.js +10 -1
  4. package/dist/web/standalone/.next/BUILD_ID +1 -1
  5. package/dist/web/standalone/.next/app-path-routes-manifest.json +14 -14
  6. package/dist/web/standalone/.next/build-manifest.json +2 -2
  7. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  8. package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
  9. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  10. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  11. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  12. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  13. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  14. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  15. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  16. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  17. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  18. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  19. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  20. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  21. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  22. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  23. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  24. package/dist/web/standalone/.next/server/app/index.html +1 -1
  25. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  26. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  27. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  28. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  29. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  30. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  31. package/dist/web/standalone/.next/server/app-paths-manifest.json +14 -14
  32. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  33. package/dist/web/standalone/.next/server/pages/500.html +2 -2
  34. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  35. package/package.json +1 -1
  36. package/packages/pi-agent-core/dist/agent-loop.js +3 -2
  37. package/packages/pi-agent-core/dist/agent-loop.js.map +1 -1
  38. package/packages/pi-agent-core/src/agent-loop.ts +3 -2
  39. package/packages/pi-coding-agent/dist/core/model-registry-auth-mode.test.js +43 -0
  40. package/packages/pi-coding-agent/dist/core/model-registry-auth-mode.test.js.map +1 -1
  41. package/packages/pi-coding-agent/dist/core/model-registry.d.ts.map +1 -1
  42. package/packages/pi-coding-agent/dist/core/model-registry.js +26 -3
  43. package/packages/pi-coding-agent/dist/core/model-registry.js.map +1 -1
  44. package/packages/pi-coding-agent/src/core/model-registry-auth-mode.test.ts +70 -0
  45. package/packages/pi-coding-agent/src/core/model-registry.ts +29 -2
  46. package/packages/pi-tui/dist/components/box.d.ts +1 -0
  47. package/packages/pi-tui/dist/components/box.d.ts.map +1 -1
  48. package/packages/pi-tui/dist/components/box.js +10 -0
  49. package/packages/pi-tui/dist/components/box.js.map +1 -1
  50. package/packages/pi-tui/src/components/box.ts +10 -0
  51. package/src/resources/extensions/gsd/auto-start.ts +7 -1
  52. package/src/resources/extensions/gsd/guided-flow.ts +96 -2
  53. package/src/resources/extensions/gsd/session-forensics.ts +11 -1
  54. package/src/resources/extensions/gsd/tests/discuss-queued-milestones.test.ts +241 -0
  55. package/src/resources/extensions/gsd/tests/forensics-error-filter.test.ts +121 -0
  56. package/src/resources/extensions/gsd/tests/preflight-context-draft-filter.test.ts +115 -0
  57. /package/dist/web/standalone/.next/static/{GR9tXQAPXXBL4AUugDPlJ → DyrX2zX_4v7KZDbUNxE2q}/_buildManifest.js +0 -0
  58. /package/dist/web/standalone/.next/static/{GR9tXQAPXXBL4AUugDPlJ → DyrX2zX_4v7KZDbUNxE2q}/_ssgManifest.js +0 -0
@@ -511,9 +511,14 @@ export async function showDiscuss(
511
511
 
512
512
  const state = await deriveState(basePath);
513
513
 
514
- // Guard: no active milestone
514
+ // No active milestone — check for pending milestones to discuss instead
515
515
  if (!state.activeMilestone) {
516
- ctx.ui.notify("No active milestone. Run /gsd to create one first.", "warning");
516
+ const pendingMilestones = state.registry.filter(m => m.status === "pending");
517
+ if (pendingMilestones.length === 0) {
518
+ ctx.ui.notify("No active milestone. Run /gsd to create one first.", "warning");
519
+ return;
520
+ }
521
+ await showDiscussQueuedMilestone(ctx, pi, basePath, pendingMilestones);
517
522
  return;
518
523
  }
519
524
 
@@ -648,6 +653,17 @@ export async function showDiscuss(
648
653
  };
649
654
  });
650
655
 
656
+ // Offer access to queued milestones when any exist
657
+ const pendingMilestones = state.registry.filter(m => m.status === "pending");
658
+ if (pendingMilestones.length > 0) {
659
+ actions.push({
660
+ id: "discuss_queued_milestone",
661
+ label: "Discuss a queued milestone",
662
+ description: `Refine context for ${pendingMilestones.length} queued milestone(s). Does not affect current execution.`,
663
+ recommended: false,
664
+ });
665
+ }
666
+
651
667
  const choice = await showNextAction(ctx, {
652
668
  title: "GSD — Discuss a slice",
653
669
  summary: [
@@ -660,6 +676,11 @@ export async function showDiscuss(
660
676
 
661
677
  if (choice === "not_yet") return;
662
678
 
679
+ if (choice === "discuss_queued_milestone") {
680
+ await showDiscussQueuedMilestone(ctx, pi, basePath, pendingMilestones);
681
+ return;
682
+ }
683
+
663
684
  const chosen = pendingSlices.find(s => s.id === choice);
664
685
  if (!chosen) return;
665
686
 
@@ -689,6 +710,79 @@ export async function showDiscuss(
689
710
  }
690
711
  }
691
712
 
713
+ // ─── Queued Milestone Discussion ─────────────────────────────────────────────
714
+
715
+ /**
716
+ * Show a picker of queued (pending) milestones and dispatch a discuss flow for
717
+ * the chosen one. Discussing a queued milestone does NOT activate it — it only
718
+ * refines the CONTEXT.md artifact so it is better prepared when auto-mode
719
+ * eventually reaches it.
720
+ */
721
+ async function showDiscussQueuedMilestone(
722
+ ctx: ExtensionCommandContext,
723
+ pi: ExtensionAPI,
724
+ basePath: string,
725
+ pendingMilestones: Array<{ id: string; title: string; status: string }>,
726
+ ): Promise<void> {
727
+ const actions = pendingMilestones.map((m, i) => {
728
+ const hasContext = !!resolveMilestoneFile(basePath, m.id, "CONTEXT");
729
+ const hasDraft = !hasContext && !!resolveMilestoneFile(basePath, m.id, "CONTEXT-DRAFT");
730
+ const contextStatus = hasContext ? "context ✓" : hasDraft ? "draft context" : "no context yet";
731
+ return {
732
+ id: m.id,
733
+ label: `${m.id}: ${m.title}`,
734
+ description: `[queued] · ${contextStatus}`,
735
+ recommended: i === 0,
736
+ };
737
+ });
738
+
739
+ const choice = await showNextAction(ctx, {
740
+ title: "GSD — Discuss a queued milestone",
741
+ summary: [
742
+ "Select a queued milestone to discuss.",
743
+ "Discussing will update its context file. It will not be activated.",
744
+ ],
745
+ actions,
746
+ notYetMessage: "Run /gsd discuss when ready.",
747
+ });
748
+
749
+ if (choice === "not_yet") return;
750
+
751
+ const chosen = pendingMilestones.find(m => m.id === choice);
752
+ if (!chosen) return;
753
+
754
+ await dispatchDiscussForMilestone(ctx, pi, basePath, chosen.id, chosen.title);
755
+ }
756
+
757
+ /**
758
+ * Dispatch the guided-discuss-milestone prompt for a milestone without
759
+ * setting pendingAutoStart — so discussing a queued milestone does not
760
+ * implicitly activate it when the session ends.
761
+ */
762
+ async function dispatchDiscussForMilestone(
763
+ ctx: ExtensionCommandContext,
764
+ pi: ExtensionAPI,
765
+ basePath: string,
766
+ mid: string,
767
+ milestoneTitle: string,
768
+ ): Promise<void> {
769
+ const draftFile = resolveMilestoneFile(basePath, mid, "CONTEXT-DRAFT");
770
+ const draftContent = draftFile ? await loadFile(draftFile) : null;
771
+ const discussMilestoneTemplates = inlineTemplate("context", "Context");
772
+ const structuredQuestionsAvailable = pi.getActiveTools().includes("ask_user_questions") ? "true" : "false";
773
+ const basePrompt = loadPrompt("guided-discuss-milestone", {
774
+ milestoneId: mid,
775
+ milestoneTitle,
776
+ inlinedTemplates: discussMilestoneTemplates,
777
+ structuredQuestionsAvailable,
778
+ commitInstruction: buildDocsCommitInstruction(`docs(${mid}): milestone context from discuss`),
779
+ });
780
+ const prompt = draftContent
781
+ ? `${basePrompt}\n\n## Prior Discussion (Draft Seed)\n\n${draftContent}`
782
+ : basePrompt;
783
+ await dispatchWorkflow(pi, prompt, "gsd-discuss", ctx, "plan-milestone");
784
+ }
785
+
692
786
  // ─── Smart Entry Point ────────────────────────────────────────────────────────
693
787
 
694
788
  /**
@@ -172,7 +172,17 @@ export function extractTrace(entries: unknown[]): ExecutionTrace {
172
172
  }
173
173
 
174
174
  if (isError && resultText) {
175
- errors.push(resultText.slice(0, 300));
175
+ // Filter out benign "errors" that are normal during code exploration:
176
+ // - grep/rg/find returning exit code 1 (no matches) is expected POSIX behavior
177
+ // - User interrupts (Escape/skip) are intentional, not failures
178
+ const trimmed = resultText.trim();
179
+ const isBenignNoMatch = pending?.name === "bash" &&
180
+ /^\(no output\)\s*\n\s*Command exited with code 1$/m.test(trimmed);
181
+ const isUserSkip = /^Skipped due to queued user message/i.test(trimmed);
182
+
183
+ if (!isBenignNoMatch && !isUserSkip) {
184
+ errors.push(resultText.slice(0, 300));
185
+ }
176
186
  }
177
187
  }
178
188
  }
@@ -0,0 +1,241 @@
1
+ /**
2
+ * discuss-queued-milestones.test.ts — Tests for #2307.
3
+ *
4
+ * /gsd discuss was previously gated on state.activeMilestone, which prevented
5
+ * users from discussing queued (pending) milestones during roadmap grooming.
6
+ *
7
+ * These tests verify:
8
+ * 1. deriveState correctly identifies pending milestones (the set the picker
9
+ * will show when no active milestone is present)
10
+ * 2. resolveMilestoneFile correctly resolves context artifacts for pending
11
+ * milestones so the picker can report their discussion state
12
+ * 3. The guided-flow.ts source code no longer hard-exits when no active
13
+ * milestone exists but pending milestones are present
14
+ * 4. The helper functions for queued discuss exist in the source
15
+ */
16
+
17
+ import { describe, test, afterEach } from "node:test";
18
+ import assert from "node:assert/strict";
19
+ import { mkdtempSync, mkdirSync, rmSync, writeFileSync, readFileSync } from "node:fs";
20
+ import { join } from "node:path";
21
+ import { tmpdir } from "node:os";
22
+ import { fileURLToPath } from "node:url";
23
+ import { dirname } from "node:path";
24
+
25
+ import { deriveState } from "../state.ts";
26
+ import { invalidateAllCaches } from "../cache.ts";
27
+ import { resolveMilestoneFile } from "../paths.ts";
28
+
29
+ // ─── Fixture Helpers ──────────────────────────────────────────────────────────
30
+
31
+ function createBase(): string {
32
+ const base = mkdtempSync(join(tmpdir(), "gsd-discuss-queued-"));
33
+ mkdirSync(join(base, ".gsd", "milestones"), { recursive: true });
34
+ return base;
35
+ }
36
+
37
+ function cleanup(base: string): void {
38
+ rmSync(base, { recursive: true, force: true });
39
+ }
40
+
41
+ function writeMilestoneDir(base: string, mid: string): void {
42
+ mkdirSync(join(base, ".gsd", "milestones", mid), { recursive: true });
43
+ }
44
+
45
+ function writeContext(base: string, mid: string, content: string): void {
46
+ writeMilestoneDir(base, mid);
47
+ writeFileSync(join(base, ".gsd", "milestones", mid, `${mid}-CONTEXT.md`), content);
48
+ }
49
+
50
+ function writeContextDraft(base: string, mid: string, content: string): void {
51
+ writeMilestoneDir(base, mid);
52
+ writeFileSync(join(base, ".gsd", "milestones", mid, `${mid}-CONTEXT-DRAFT.md`), content);
53
+ }
54
+
55
+ function writeRoadmap(base: string, mid: string, content: string): void {
56
+ writeMilestoneDir(base, mid);
57
+ writeFileSync(join(base, ".gsd", "milestones", mid, `${mid}-ROADMAP.md`), content);
58
+ }
59
+
60
+ function readGuidedFlowSource(): string {
61
+ const thisFile = fileURLToPath(import.meta.url);
62
+ const thisDir = dirname(thisFile);
63
+ return readFileSync(join(thisDir, "..", "guided-flow.ts"), "utf-8");
64
+ }
65
+
66
+ // ─── Tests ────────────────────────────────────────────────────────────────────
67
+
68
+ describe("discuss-queued-milestones (#2307)", () => {
69
+
70
+ test("1. pending milestones appear in registry when active milestone exists", async () => {
71
+ const base = createBase();
72
+ try {
73
+ // M001: active — has context + roadmap with a slice
74
+ writeContext(base, "M001", "# M001: Active\nContext here.");
75
+ writeRoadmap(base, "M001",
76
+ "# M001: Active\n\n## Slices\n- [ ] **S01: Do work** `risk:low` `depends:[]`\n > After this: works\n");
77
+
78
+ // M002: pending — context only, no roadmap
79
+ writeContext(base, "M002", "# M002: Queued\nFuture work.");
80
+
81
+ // M003: pending — draft context only
82
+ writeContextDraft(base, "M003", "# M003: Draft\nSeed material.");
83
+
84
+ invalidateAllCaches();
85
+ const state = await deriveState(base);
86
+
87
+ assert.ok(!!state.activeMilestone, "M001 should be the active milestone");
88
+ assert.strictEqual(state.activeMilestone?.id, "M001");
89
+
90
+ const pendingIds = state.registry
91
+ .filter(m => m.status === "pending")
92
+ .map(m => m.id);
93
+
94
+ assert.ok(pendingIds.includes("M002"), "M002 should be pending");
95
+ assert.ok(pendingIds.includes("M003"), "M003 should be pending");
96
+ } finally {
97
+ cleanup(base);
98
+ }
99
+ });
100
+
101
+ test("2. first context-only milestone is active, subsequent ones are pending", async () => {
102
+ const base = createBase();
103
+ try {
104
+ // M001: first milestone with context but no roadmap — deriveState marks it active
105
+ writeContext(base, "M001", "# M001: First\nContext here.");
106
+ // M002: will be pending since M001 is active
107
+ writeContext(base, "M002", "# M002: Second\nMore future work.");
108
+
109
+ invalidateAllCaches();
110
+ const state = await deriveState(base);
111
+
112
+ // deriveState makes the first unfinished milestone "active" even without a roadmap
113
+ assert.ok(!!state.activeMilestone, "first milestone should be active");
114
+ assert.strictEqual(state.activeMilestone?.id, "M001", "M001 is the active milestone");
115
+
116
+ const pendingIds = state.registry
117
+ .filter(m => m.status === "pending")
118
+ .map(m => m.id);
119
+
120
+ assert.ok(pendingIds.includes("M002"),
121
+ "M002 should be pending — it comes after the active M001");
122
+ } finally {
123
+ cleanup(base);
124
+ }
125
+ });
126
+
127
+ test("3. resolveMilestoneFile finds CONTEXT.md for pending milestone", (t) => {
128
+ const base = createBase();
129
+ try {
130
+ writeContext(base, "M002", "# M002: Queued\nContent.");
131
+
132
+ const contextFile = resolveMilestoneFile(base, "M002", "CONTEXT");
133
+ assert.ok(contextFile !== null, "resolveMilestoneFile should find CONTEXT.md for M002");
134
+ assert.ok(contextFile!.endsWith("M002-CONTEXT.md"),
135
+ "resolved path should point to M002-CONTEXT.md");
136
+ } finally {
137
+ cleanup(base);
138
+ }
139
+ });
140
+
141
+ test("4. resolveMilestoneFile finds CONTEXT-DRAFT.md for pending milestone", (t) => {
142
+ const base = createBase();
143
+ try {
144
+ writeContextDraft(base, "M003", "# M003: Draft\nSeed content.");
145
+
146
+ const draftFile = resolveMilestoneFile(base, "M003", "CONTEXT-DRAFT");
147
+ assert.ok(draftFile !== null, "resolveMilestoneFile should find CONTEXT-DRAFT.md for M003");
148
+ assert.ok(draftFile!.endsWith("M003-CONTEXT-DRAFT.md"),
149
+ "resolved path should point to M003-CONTEXT-DRAFT.md");
150
+ } finally {
151
+ cleanup(base);
152
+ }
153
+ });
154
+
155
+ test("5. resolveMilestoneFile returns null when pending milestone has no context", (t) => {
156
+ const base = createBase();
157
+ try {
158
+ writeMilestoneDir(base, "M004");
159
+
160
+ const contextFile = resolveMilestoneFile(base, "M004", "CONTEXT");
161
+ assert.strictEqual(contextFile, null,
162
+ "resolveMilestoneFile should return null when no CONTEXT.md exists");
163
+
164
+ const draftFile = resolveMilestoneFile(base, "M004", "CONTEXT-DRAFT");
165
+ assert.strictEqual(draftFile, null,
166
+ "resolveMilestoneFile should return null when no CONTEXT-DRAFT.md exists");
167
+ } finally {
168
+ cleanup(base);
169
+ }
170
+ });
171
+
172
+ test("6. guided-flow no longer hard-exits when no active milestone but pending exist", () => {
173
+ const source = readGuidedFlowSource();
174
+
175
+ // The old guard was a simple early-exit:
176
+ // if (!state.activeMilestone) {
177
+ // ctx.ui.notify("No active milestone. Run /gsd to create one first.", "warning");
178
+ // return;
179
+ // }
180
+ //
181
+ // The new guard should check for pending milestones and route instead.
182
+ const oldGuardPattern = /if\s*\(!state\.activeMilestone\)\s*\{\s*ctx\.ui\.notify\("No active milestone/;
183
+ assert.ok(
184
+ !oldGuardPattern.test(source),
185
+ "guided-flow must not unconditionally exit when activeMilestone is null",
186
+ );
187
+ });
188
+
189
+ test("7. showDiscussQueuedMilestone helper exists in guided-flow", () => {
190
+ const source = readGuidedFlowSource();
191
+ assert.ok(
192
+ source.includes("showDiscussQueuedMilestone"),
193
+ "guided-flow must export showDiscussQueuedMilestone helper",
194
+ );
195
+ });
196
+
197
+ test("8. dispatchDiscussForMilestone helper exists in guided-flow", () => {
198
+ const source = readGuidedFlowSource();
199
+ assert.ok(
200
+ source.includes("dispatchDiscussForMilestone"),
201
+ "guided-flow must export dispatchDiscussForMilestone helper",
202
+ );
203
+ });
204
+
205
+ test("9. dispatchDiscussForMilestone does not set pendingAutoStart", () => {
206
+ const source = readGuidedFlowSource();
207
+
208
+ // Extract the dispatchDiscussForMilestone function body
209
+ const fnMatch = source.match(
210
+ /async function dispatchDiscussForMilestone\s*\([^)]*\)[^{]*\{([\s\S]*?)\n\}/,
211
+ );
212
+ assert.ok(!!fnMatch, "dispatchDiscussForMilestone function body must be present");
213
+
214
+ if (fnMatch) {
215
+ assert.ok(
216
+ !fnMatch[1].includes("pendingAutoStart"),
217
+ "dispatchDiscussForMilestone must NOT set pendingAutoStart — discussing a queued milestone must not activate it",
218
+ );
219
+ }
220
+ });
221
+
222
+ test("10. slice picker includes queued milestone option when pending milestones exist", () => {
223
+ const source = readGuidedFlowSource();
224
+ assert.ok(
225
+ source.includes("discuss_queued_milestone"),
226
+ "slice picker must include a 'discuss_queued_milestone' action id for queued milestones",
227
+ );
228
+ assert.ok(
229
+ source.includes("Discuss a queued milestone"),
230
+ "slice picker must label the queued milestone action clearly",
231
+ );
232
+ });
233
+
234
+ test("11. queued milestone picker labels entries with [queued]", () => {
235
+ const source = readGuidedFlowSource();
236
+ assert.ok(
237
+ source.includes("[queued]"),
238
+ "queued milestone picker must label entries with [queued] to distinguish from active",
239
+ );
240
+ });
241
+ });
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Regression test for #2539: extractTrace should not count benign bash
3
+ * exit-code-1 (grep no-match) or user skips as errors.
4
+ */
5
+ import { describe, test } from "node:test";
6
+ import assert from "node:assert/strict";
7
+
8
+ import { extractTrace } from "../session-forensics.ts";
9
+
10
+ /**
11
+ * Build a minimal JSONL entry pair: assistant tool_use → toolResult.
12
+ * This is the shape extractTrace() expects from session activity files.
13
+ */
14
+ function makeToolPair(
15
+ toolName: string,
16
+ input: Record<string, unknown>,
17
+ resultText: string,
18
+ isError: boolean,
19
+ ): unknown[] {
20
+ const toolCallId = `toolu_${Math.random().toString(36).slice(2, 10)}`;
21
+ return [
22
+ {
23
+ type: "message",
24
+ message: {
25
+ role: "assistant",
26
+ content: [
27
+ {
28
+ type: "toolCall",
29
+ id: toolCallId,
30
+ name: toolName,
31
+ arguments: input,
32
+ },
33
+ ],
34
+ },
35
+ },
36
+ {
37
+ type: "message",
38
+ message: {
39
+ role: "toolResult",
40
+ toolCallId,
41
+ toolName,
42
+ isError,
43
+ content: [{ type: "text", text: resultText }],
44
+ },
45
+ },
46
+ ];
47
+ }
48
+
49
+ describe("extractTrace error filtering (#2539)", () => {
50
+ test("grep exit-code-1 (no matches) is not counted as an error", () => {
51
+ const entries = makeToolPair(
52
+ "bash",
53
+ { command: "grep -rn 'nonexistent' src/" },
54
+ "(no output)\nCommand exited with code 1",
55
+ true,
56
+ );
57
+ const trace = extractTrace(entries);
58
+ assert.equal(trace.errors.length, 0, "grep no-match should not be an error");
59
+ });
60
+
61
+ test("user skip is not counted as an error", () => {
62
+ const entries = makeToolPair(
63
+ "bash",
64
+ { command: "npm run test" },
65
+ "Skipped due to queued user message",
66
+ true,
67
+ );
68
+ const trace = extractTrace(entries);
69
+ assert.equal(trace.errors.length, 0, "user skip should not be an error");
70
+ });
71
+
72
+ test("real bash error is still counted", () => {
73
+ const entries = makeToolPair(
74
+ "bash",
75
+ { command: "cat /nonexistent" },
76
+ "cat: /nonexistent: No such file or directory\nCommand exited with code 1",
77
+ true,
78
+ );
79
+ const trace = extractTrace(entries);
80
+ assert.equal(trace.errors.length, 1, "real error should still be counted");
81
+ assert.match(trace.errors[0], /No such file or directory/);
82
+ });
83
+
84
+ test("non-bash tool error is still counted", () => {
85
+ const entries = makeToolPair(
86
+ "edit",
87
+ { path: "foo.ts", oldText: "x", newText: "y" },
88
+ "oldText not found in file",
89
+ true,
90
+ );
91
+ const trace = extractTrace(entries);
92
+ assert.equal(trace.errors.length, 1, "non-bash tool errors should still be counted");
93
+ });
94
+
95
+ test("mixed entries: only real errors are counted", () => {
96
+ const entries = [
97
+ // benign grep no-match
98
+ ...makeToolPair("bash", { command: "grep -rn 'pattern' src/" }, "(no output)\nCommand exited with code 1", true),
99
+ // user skip
100
+ ...makeToolPair("bash", { command: "npm test" }, "Skipped due to queued user message", true),
101
+ // real error
102
+ ...makeToolPair("bash", { command: "node broken.js" }, "SyntaxError: Unexpected token\nCommand exited with code 1", true),
103
+ // successful command (not an error)
104
+ ...makeToolPair("bash", { command: "echo hello" }, "hello", false),
105
+ ];
106
+ const trace = extractTrace(entries);
107
+ assert.equal(trace.errors.length, 1, "only the real error should be counted");
108
+ assert.match(trace.errors[0], /SyntaxError/);
109
+ });
110
+
111
+ test("exit code 1 with actual output is still an error", () => {
112
+ const entries = makeToolPair(
113
+ "bash",
114
+ { command: "npm run lint" },
115
+ "src/foo.ts:10:5 - error TS2304: Cannot find name 'x'\nCommand exited with code 1",
116
+ true,
117
+ );
118
+ const trace = extractTrace(entries);
119
+ assert.equal(trace.errors.length, 1, "lint error with output should be counted");
120
+ });
121
+ });
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Regression test for #2473: Pre-flight CONTEXT-DRAFT warning should skip
3
+ * completed and parked milestones.
4
+ *
5
+ * The pre-flight loop in auto-start.ts warns about CONTEXT-DRAFT.md files
6
+ * so the user knows which milestones will pause for discussion. But completed
7
+ * milestones with leftover CONTEXT-DRAFT.md files are not actionable — the
8
+ * warning is noise.
9
+ *
10
+ * This test exercises the filtering logic directly: given a set of milestones
11
+ * with CONTEXT-DRAFT files, only active/pending ones should produce warnings.
12
+ */
13
+ import { describe, test, beforeEach, afterEach } from "node:test";
14
+ import assert from "node:assert/strict";
15
+ import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs";
16
+ import { join } from "node:path";
17
+ import { tmpdir } from "node:os";
18
+
19
+ import {
20
+ openDatabase,
21
+ closeDatabase,
22
+ isDbAvailable,
23
+ insertMilestone,
24
+ getMilestone,
25
+ } from "../gsd-db.ts";
26
+ import { resolveMilestoneFile } from "../paths.ts";
27
+
28
+ describe("pre-flight CONTEXT-DRAFT filter (#2473)", () => {
29
+ let tmpBase: string;
30
+ let gsd: string;
31
+
32
+ beforeEach(() => {
33
+ tmpBase = mkdtempSync(join(tmpdir(), "gsd-preflight-draft-"));
34
+ gsd = join(tmpBase, ".gsd");
35
+
36
+ // Create milestone directories with CONTEXT-DRAFT files
37
+ for (const id of ["M001", "M002", "M003"]) {
38
+ const msDir = join(gsd, "milestones", id);
39
+ mkdirSync(msDir, { recursive: true });
40
+ writeFileSync(join(msDir, `${id}-CONTEXT-DRAFT.md`), `# ${id}: Draft\n`);
41
+ }
42
+
43
+ // Open DB and insert milestones with different statuses
44
+ const dbPath = join(gsd, "gsd.db");
45
+ openDatabase(dbPath);
46
+ insertMilestone({ id: "M001", title: "Complete milestone", status: "complete" });
47
+ insertMilestone({ id: "M002", title: "Active milestone", status: "active" });
48
+ insertMilestone({ id: "M003", title: "Parked milestone", status: "parked" });
49
+ });
50
+
51
+ afterEach(() => {
52
+ closeDatabase();
53
+ rmSync(tmpBase, { recursive: true, force: true });
54
+ });
55
+
56
+ test("completed milestone is skipped — no warning emitted", () => {
57
+ assert.ok(isDbAvailable(), "DB should be available");
58
+ const ms = getMilestone("M001");
59
+ assert.equal(ms?.status, "complete");
60
+ });
61
+
62
+ test("parked milestone is skipped — no warning emitted", () => {
63
+ const ms = getMilestone("M003");
64
+ assert.equal(ms?.status, "parked");
65
+ });
66
+
67
+ test("active milestone with CONTEXT-DRAFT produces warning", () => {
68
+ const ms = getMilestone("M002");
69
+ assert.equal(ms?.status, "active");
70
+
71
+ const draft = resolveMilestoneFile(tmpBase, "M002", "CONTEXT-DRAFT");
72
+ assert.ok(draft, "CONTEXT-DRAFT file should be found for active milestone");
73
+ });
74
+
75
+ test("full pre-flight filter produces warnings only for active milestones", () => {
76
+ const milestoneIds = ["M001", "M002", "M003"];
77
+ const issues: string[] = [];
78
+
79
+ for (const id of milestoneIds) {
80
+ // Replicate the fixed pre-flight logic from auto-start.ts
81
+ if (isDbAvailable()) {
82
+ const ms = getMilestone(id);
83
+ if (ms?.status === "complete" || ms?.status === "parked") continue;
84
+ }
85
+ const draft = resolveMilestoneFile(tmpBase, id, "CONTEXT-DRAFT");
86
+ if (draft) {
87
+ issues.push(`${id}: has CONTEXT-DRAFT.md (will pause for discussion)`);
88
+ }
89
+ }
90
+
91
+ assert.equal(issues.length, 1, "only one warning should be emitted");
92
+ assert.match(issues[0], /M002/, "warning should be for the active milestone only");
93
+ });
94
+
95
+ test("when DB is unavailable, all milestones with CONTEXT-DRAFT produce warnings (safe fallback)", () => {
96
+ closeDatabase();
97
+ assert.ok(!isDbAvailable(), "DB should be unavailable after close");
98
+
99
+ const milestoneIds = ["M001", "M002", "M003"];
100
+ const issues: string[] = [];
101
+
102
+ for (const id of milestoneIds) {
103
+ if (isDbAvailable()) {
104
+ const ms = getMilestone(id);
105
+ if (ms?.status === "complete" || ms?.status === "parked") continue;
106
+ }
107
+ const draft = resolveMilestoneFile(tmpBase, id, "CONTEXT-DRAFT");
108
+ if (draft) {
109
+ issues.push(`${id}: has CONTEXT-DRAFT.md (will pause for discussion)`);
110
+ }
111
+ }
112
+
113
+ assert.equal(issues.length, 3, "all milestones should warn when DB is unavailable");
114
+ });
115
+ });