gsd-pi 2.80.0-dev.e51d2c88c → 2.80.0-dev.e6c48c3af

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 (83) hide show
  1. package/dist/resources/.managed-resources-content-hash +1 -1
  2. package/dist/resources/extensions/gsd/auto/phases.js +30 -6
  3. package/dist/resources/extensions/gsd/auto/run-unit.js +4 -1
  4. package/dist/resources/extensions/gsd/auto-direct-dispatch.js +1 -1
  5. package/dist/resources/extensions/gsd/auto.js +18 -1
  6. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +54 -4
  7. package/dist/resources/extensions/gsd/clean-root-preflight.js +24 -6
  8. package/dist/resources/extensions/gsd/git-service.js +36 -4
  9. package/dist/resources/extensions/gsd/pre-execution-checks.js +7 -0
  10. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  11. package/dist/resources/extensions/gsd/worktree-resolver.js +33 -17
  12. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  13. package/dist/web/standalone/.next/BUILD_ID +1 -1
  14. package/dist/web/standalone/.next/app-path-routes-manifest.json +11 -11
  15. package/dist/web/standalone/.next/build-manifest.json +2 -2
  16. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  17. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  18. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  19. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  20. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  21. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  22. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  23. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  24. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  25. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  26. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  27. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  28. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  29. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  30. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  31. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  32. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  33. package/dist/web/standalone/.next/server/app/index.html +1 -1
  34. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  35. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  36. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  37. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  38. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  39. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  40. package/dist/web/standalone/.next/server/app-paths-manifest.json +11 -11
  41. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  42. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  43. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  44. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  45. package/package.json +1 -1
  46. package/packages/pi-coding-agent/dist/core/agent-session-tool-refresh.test.js +15 -0
  47. package/packages/pi-coding-agent/dist/core/agent-session-tool-refresh.test.js.map +1 -1
  48. package/packages/pi-coding-agent/dist/core/agent-session.d.ts +2 -0
  49. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  50. package/packages/pi-coding-agent/dist/core/agent-session.js +4 -3
  51. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  52. package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts +2 -0
  53. package/packages/pi-coding-agent/dist/core/extensions/runner.d.ts.map +1 -1
  54. package/packages/pi-coding-agent/dist/core/extensions/runner.js.map +1 -1
  55. package/packages/pi-coding-agent/dist/core/extensions/types.d.ts +5 -0
  56. package/packages/pi-coding-agent/dist/core/extensions/types.d.ts.map +1 -1
  57. package/packages/pi-coding-agent/dist/core/extensions/types.js.map +1 -1
  58. package/packages/pi-coding-agent/src/core/agent-session-tool-refresh.test.ts +18 -0
  59. package/packages/pi-coding-agent/src/core/agent-session.ts +6 -3
  60. package/packages/pi-coding-agent/src/core/extensions/runner.ts +2 -0
  61. package/packages/pi-coding-agent/src/core/extensions/types.ts +5 -0
  62. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  63. package/src/resources/extensions/gsd/auto/loop-deps.ts +2 -2
  64. package/src/resources/extensions/gsd/auto/phases.ts +50 -15
  65. package/src/resources/extensions/gsd/auto/run-unit.ts +4 -1
  66. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +1 -1
  67. package/src/resources/extensions/gsd/auto.ts +18 -1
  68. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +66 -4
  69. package/src/resources/extensions/gsd/clean-root-preflight.ts +32 -7
  70. package/src/resources/extensions/gsd/git-service.ts +46 -8
  71. package/src/resources/extensions/gsd/pre-execution-checks.ts +7 -0
  72. package/src/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  73. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +144 -1
  74. package/src/resources/extensions/gsd/tests/auto-wrapup-inflight-guard.test.ts +166 -4
  75. package/src/resources/extensions/gsd/tests/clean-root-preflight.test.ts +15 -6
  76. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +5 -1
  77. package/src/resources/extensions/gsd/tests/integration/git-service.test.ts +54 -0
  78. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +5 -1
  79. package/src/resources/extensions/gsd/tests/pre-execution-checks.test.ts +38 -0
  80. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +63 -1
  81. package/src/resources/extensions/gsd/worktree-resolver.ts +36 -15
  82. /package/dist/web/standalone/.next/static/{8F5YpnZNBaooIWGF4GBV3 → 4dQ9NTZJ8pEvFwBgDUX93}/_buildManifest.js +0 -0
  83. /package/dist/web/standalone/.next/static/{8F5YpnZNBaooIWGF4GBV3 → 4dQ9NTZJ8pEvFwBgDUX93}/_ssgManifest.js +0 -0
@@ -3,8 +3,14 @@
3
3
 
4
4
  import { describe, test } from "node:test";
5
5
  import assert from "node:assert/strict";
6
- import { readFileSync } from "node:fs";
6
+ import { mkdtempSync, mkdirSync, readFileSync, realpathSync, rmSync } from "node:fs";
7
7
  import { join } from "node:path";
8
+ import { tmpdir } from "node:os";
9
+
10
+ import { autoSession } from "../auto-runtime-state.ts";
11
+ import { dispatchHookUnit } from "../auto.ts";
12
+ import { registerHooks } from "../bootstrap/register-hooks.ts";
13
+ import { clearDiscussionFlowState, getPendingGate } from "../bootstrap/write-gate.ts";
8
14
 
9
15
  const autoTimersPath = join(import.meta.dirname, "..", "auto-timers.ts");
10
16
  const autoTimersSrc = readFileSync(autoTimersPath, "utf-8");
@@ -18,6 +24,37 @@ const runUnitSrc = readFileSync(runUnitPath, "utf-8");
18
24
  const registerHooksPath = join(import.meta.dirname, "..", "bootstrap", "register-hooks.ts");
19
25
  const registerHooksSrc = readFileSync(registerHooksPath, "utf-8");
20
26
 
27
+ function makeHookHarness() {
28
+ const handlers = new Map<string, Array<(event: any, ctx: any) => Promise<any>>>();
29
+ const pi = {
30
+ on(name: string, handler: (event: any, ctx: any) => Promise<any>) {
31
+ const current = handlers.get(name) ?? [];
32
+ current.push(handler);
33
+ handlers.set(name, current);
34
+ },
35
+ };
36
+ const ctx = {
37
+ ui: {
38
+ notify: () => {},
39
+ setStatus: () => {},
40
+ setWidget: () => {},
41
+ },
42
+ modelRegistry: {
43
+ setDisabledModelProviders: () => {},
44
+ },
45
+ setCompactionThresholdOverride: () => {},
46
+ };
47
+ async function emit(name: string, event: any): Promise<any> {
48
+ for (const handler of handlers.get(name) ?? []) {
49
+ const result = await handler(event, ctx);
50
+ if (result?.block) return result;
51
+ }
52
+ return undefined;
53
+ }
54
+ registerHooks(pi as any, []);
55
+ return { emit };
56
+ }
57
+
21
58
  describe("#3512: gsd-auto-wrapup must not interrupt in-flight tool calls", () => {
22
59
  test("soft timeout wrapup gates triggerTurn on getInFlightToolCount() === 0", () => {
23
60
  // The soft timeout sendMessage must NOT use a hardcoded `triggerTurn: true`.
@@ -73,6 +110,61 @@ describe("#3512: gsd-auto-wrapup must not interrupt in-flight tool calls", () =>
73
110
  });
74
111
  });
75
112
 
113
+ describe("hook dispatch session cwd", () => {
114
+ test("dispatchHookUnit passes basePath explicitly to newSession", async (t) => {
115
+ const originalCwd = process.cwd();
116
+ const basePath = mkdtempSync(join(tmpdir(), "gsd-hook-cwd-"));
117
+ mkdirSync(join(basePath, ".gsd"), { recursive: true });
118
+ autoSession.reset();
119
+ t.after(() => {
120
+ try {
121
+ process.chdir(originalCwd);
122
+ } catch {
123
+ // best effort cleanup after cwd-sensitive dispatch tests
124
+ }
125
+ autoSession.reset();
126
+ rmSync(basePath, { recursive: true, force: true });
127
+ });
128
+
129
+ let newSessionOptions: unknown;
130
+ const ctx = {
131
+ ui: {
132
+ notify: () => {},
133
+ setStatus: () => {},
134
+ setWidget: () => {},
135
+ },
136
+ modelRegistry: {
137
+ getAvailable: () => [],
138
+ },
139
+ sessionManager: {
140
+ getSessionFile: () => join(basePath, "session.jsonl"),
141
+ },
142
+ newSession: async (options?: unknown) => {
143
+ newSessionOptions = options;
144
+ return { cancelled: false };
145
+ },
146
+ };
147
+ const pi = {
148
+ sendMessage: () => {},
149
+ setModel: async () => true,
150
+ };
151
+
152
+ const dispatched = await dispatchHookUnit(
153
+ ctx as any,
154
+ pi as any,
155
+ "review",
156
+ "execute-task",
157
+ "M001/S01/T01",
158
+ "review the completed unit",
159
+ undefined,
160
+ basePath,
161
+ );
162
+
163
+ assert.equal(dispatched, true);
164
+ assert.deepEqual(newSessionOptions, { cwd: basePath });
165
+ });
166
+ });
167
+
76
168
  describe("#4276: pending/skipped tools stay visible to auto-mode hooks", () => {
77
169
  test("tool_call handler marks GSD tools in-flight before execution_start", () => {
78
170
  const startMarker = 'pi.on("tool_call", async (event, ctx) => {';
@@ -193,7 +285,7 @@ describe("#4365: tool_execution_start hook must pass toolName to markToolStart",
193
285
  });
194
286
 
195
287
  describe("deep setup approval questions pause immediately", () => {
196
- test("register-hooks sets pending gate during message_update without aborting the stream", () => {
288
+ test("register-hooks defers the pending gate during message_update without aborting the stream", () => {
197
289
  const startMarker = 'pi.on("message_update"';
198
290
  const endMarker = 'pi.on("session_shutdown"';
199
291
  const messageUpdateSection = registerHooksSrc.slice(
@@ -210,8 +302,8 @@ describe("deep setup approval questions pause immediately", () => {
210
302
  "message_update must detect approval/question boundaries",
211
303
  );
212
304
  assert.ok(
213
- messageUpdateSection.includes("approvalGateIdForUnit") && messageUpdateSection.includes("setPendingGate"),
214
- "plain-text approval questions must set the durable write gate",
305
+ messageUpdateSection.includes("approvalGateIdForUnit") && messageUpdateSection.includes("deferApprovalGate"),
306
+ "plain-text approval questions must defer the durable write gate until same-turn draft persistence can finish",
215
307
  );
216
308
  assert.ok(
217
309
  messageUpdateSection.includes("getDiscussionMilestoneIdFor") && messageUpdateSection.includes('"discuss-milestone"'),
@@ -222,4 +314,74 @@ describe("deep setup approval questions pause immediately", () => {
222
314
  "message_update must NOT abort the stream — aborting eats the model's question text on external CLI providers; the pending gate set above blocks subsequent tool calls instead",
223
315
  );
224
316
  });
317
+
318
+ test("plain-text approval boundary defers durable gate until same-turn CONTEXT-DRAFT can save", async () => {
319
+ const base = realpathSync(mkdtempSync(join(tmpdir(), "gsd-deferred-approval-")));
320
+ const previousCwd = process.cwd();
321
+ try {
322
+ mkdirSync(join(base, ".gsd", "milestones", "M003"), { recursive: true });
323
+ process.chdir(base);
324
+ clearDiscussionFlowState(base);
325
+ autoSession.reset();
326
+ autoSession.basePath = base;
327
+ autoSession.currentUnit = {
328
+ type: "discuss-milestone",
329
+ id: "M003",
330
+ startedAt: Date.now(),
331
+ };
332
+
333
+ const { emit } = makeHookHarness();
334
+ await emit("message_update", {
335
+ message: {
336
+ role: "assistant",
337
+ content: [{ type: "text", text: "Did I capture that correctly? If not, tell me what I missed." }],
338
+ },
339
+ });
340
+
341
+ assert.equal(
342
+ getPendingGate(base),
343
+ null,
344
+ "approval text should not install the durable pending gate until the assistant turn ends",
345
+ );
346
+
347
+ const draftResult = await emit("tool_call", {
348
+ toolCallId: "draft-save",
349
+ toolName: "gsd_summary_save",
350
+ input: {
351
+ milestone_id: "M003",
352
+ artifact_type: "CONTEXT-DRAFT",
353
+ content: "# M003 Draft\n",
354
+ },
355
+ });
356
+ assert.equal(
357
+ draftResult?.block,
358
+ undefined,
359
+ "same-turn CONTEXT-DRAFT persistence should remain allowed after the approval text streams",
360
+ );
361
+
362
+ const finalContextResult = await emit("tool_call", {
363
+ toolCallId: "final-context",
364
+ toolName: "gsd_summary_save",
365
+ input: {
366
+ milestone_id: "M003",
367
+ artifact_type: "CONTEXT",
368
+ content: "# M003 Context\n",
369
+ },
370
+ });
371
+ assert.equal(finalContextResult?.block, true, "final CONTEXT must still wait for approval");
372
+ assert.match(finalContextResult.reason, /Approval question "depth_verification_M003_confirm"/);
373
+
374
+ await emit("agent_end", { messages: [] });
375
+ assert.equal(
376
+ getPendingGate(base),
377
+ "depth_verification_M003_confirm",
378
+ "agent_end should activate the durable pending gate for the next turn",
379
+ );
380
+ } finally {
381
+ process.chdir(previousCwd);
382
+ autoSession.reset();
383
+ clearDiscussionFlowState(base);
384
+ rmSync(base, { recursive: true, force: true });
385
+ }
386
+ });
225
387
  });
@@ -131,9 +131,11 @@ test("postflightPopStash — restores stashed changes and emits info notificatio
131
131
  run('git commit -m "simulate merge"', repo);
132
132
 
133
133
  const postNotifications: Array<{ msg: string; level: string }> = [];
134
- postflightPopStash(repo, "M004", preflight.stashMarker, (msg, level) => {
134
+ const postflight = postflightPopStash(repo, "M004", preflight.stashMarker, (msg, level) => {
135
135
  postNotifications.push({ msg, level });
136
136
  });
137
+ assert.equal(postflight.restored, true, "postflight must report successful restore");
138
+ assert.equal(postflight.needsManualRecovery, false, "successful restore must not need manual recovery");
137
139
 
138
140
  // The stashed README.md change must be restored
139
141
  const content = readFileSync(join(repo, "README.md"), "utf-8");
@@ -171,7 +173,8 @@ test("preflight + merge + postflight round-trip preserves uncommitted changes",
171
173
  run('git commit -m "feat: add feature"', repo);
172
174
 
173
175
  // Postflight: pop stash
174
- postflightPopStash(repo, "M005", preflight.stashMarker, () => {});
176
+ const postflight = postflightPopStash(repo, "M005", preflight.stashMarker, () => {});
177
+ assert.equal(postflight.needsManualRecovery, false, "clean restore must not stop auto-mode");
175
178
 
176
179
  // README.md must still have our local content
177
180
  const restored = readFileSync(join(repo, "README.md"), "utf-8");
@@ -197,9 +200,12 @@ test("postflightPopStash conflict warning names the exact stash ref", () => {
197
200
  run('git commit -m "simulate conflicting merge"', repo);
198
201
 
199
202
  const notifications: Array<{ msg: string; level: string }> = [];
200
- postflightPopStash(repo, "M005C", preflight.stashMarker, (msg, level) => {
203
+ const postflight = postflightPopStash(repo, "M005C", preflight.stashMarker, (msg, level) => {
201
204
  notifications.push({ msg, level });
202
205
  });
206
+ assert.equal(postflight.restored, false, "conflicted restore must report restored=false");
207
+ assert.equal(postflight.needsManualRecovery, true, "conflicted restore must require manual recovery");
208
+ assert.match(postflight.message, /failed after merge of milestone M005C/);
203
209
 
204
210
  const warning = notifications.find((n) => n.level === "warning")?.msg ?? "";
205
211
  assert.match(warning, /git stash pop stash@\{\d+\}/);
@@ -219,7 +225,8 @@ test("postflightPopStash restores the matching GSD stash, not stash@{0}", () =>
219
225
  writeFileSync(join(repo, "other.txt"), "other stash\n");
220
226
  run('git stash push --include-untracked -m "unrelated newer stash"', repo);
221
227
 
222
- postflightPopStash(repo, "M006", preflight.stashMarker, () => {});
228
+ const postflight = postflightPopStash(repo, "M006", preflight.stashMarker, () => {});
229
+ assert.equal(postflight.needsManualRecovery, false, "targeted restore must not need manual recovery");
223
230
 
224
231
  const content = readFileSync(join(repo, "README.md"), "utf-8");
225
232
  assert.equal(content.replace(/\r\n/g, "\n"), "# target stash\n");
@@ -242,7 +249,8 @@ test("postflightPopStash restores the exact preflight marker when another same-m
242
249
  writeFileSync(join(repo, "same-milestone.txt"), "newer same milestone stash\n");
243
250
  run('git stash push --include-untracked -m "gsd-preflight-stash [gsd-preflight-stash:M007:other]"', repo);
244
251
 
245
- postflightPopStash(repo, "M007", preflight.stashMarker, () => {});
252
+ const postflight = postflightPopStash(repo, "M007", preflight.stashMarker, () => {});
253
+ assert.equal(postflight.needsManualRecovery, false, "exact marker restore must not need manual recovery");
246
254
 
247
255
  const content = readFileSync(join(repo, "README.md"), "utf-8");
248
256
  assert.equal(content.replace(/\r\n/g, "\n"), "# target stash\n");
@@ -260,7 +268,8 @@ test("postflightPopStash falls back to milestone marker prefix when exact marker
260
268
  writeFileSync(join(repo, "README.md"), "# fallback stash\n");
261
269
  run('git stash push --include-untracked -m "gsd-preflight-stash [gsd-preflight-stash:M008:fallback]"', repo);
262
270
 
263
- postflightPopStash(repo, "M008", undefined, () => {});
271
+ const postflight = postflightPopStash(repo, "M008", undefined, () => {});
272
+ assert.equal(postflight.needsManualRecovery, false, "fallback marker restore must not need manual recovery");
264
273
 
265
274
  const content = readFileSync(join(repo, "README.md"), "utf-8");
266
275
  assert.equal(content.replace(/\r\n/g, "\n"), "# fallback stash\n");
@@ -192,7 +192,11 @@ function makeMockDeps(overrides?: Partial<LoopDeps>): LoopDeps & { callLog: stri
192
192
  resolveMilestoneFile: () => null,
193
193
  reconcileMergeState: () => "clean",
194
194
  preflightCleanRoot: () => ({ stashPushed: false, summary: "" }),
195
- postflightPopStash: () => {},
195
+ postflightPopStash: () => ({
196
+ restored: true,
197
+ needsManualRecovery: false,
198
+ message: "restored",
199
+ }),
196
200
  getLedger: () => null,
197
201
  getProjectTotals: () => ({ cost: 0 }),
198
202
  formatCost: (c: number) => `$${c.toFixed(2)}`,
@@ -542,6 +542,60 @@ describe('git-service', async () => {
542
542
  rmSync(repo, { recursive: true, force: true });
543
543
  });
544
544
 
545
+ // Regression: #5500. The LLM occasionally hallucinates files in
546
+ // task.keyFiles that were never written. Pre-existing scoped-stage code
547
+ // ran `git add -- <every keyFile>` and failed the entire commit on the
548
+ // first missing path. Verify that valid paths still commit and missing
549
+ // ones are dropped silently.
550
+ test('GitServiceImpl: scoped staging drops missing keyFiles and commits the rest', () => {
551
+ const repo = initTempRepo();
552
+ const svc = new GitServiceImpl(repo);
553
+
554
+ createFile(repo, "src/index.ts", "export const ok = true;");
555
+ // Note: src/commands/list.ts is intentionally NOT created — the LLM
556
+ // claimed it wrote this file but didn't.
557
+
558
+ const msg = svc.autoCommit("execute-task", "M001/S01/T02", [], {
559
+ taskId: "S01/T02",
560
+ taskTitle: "wire up command list",
561
+ oneLiner: "Added list command stub",
562
+ keyFiles: ["src/index.ts", "src/commands/list.ts"],
563
+ });
564
+ assert.ok(msg !== null, "autoCommit succeeds when at least one keyFile exists");
565
+
566
+ const committed = run("git show --name-only --format= HEAD", repo);
567
+ assert.ok(committed.includes("src/index.ts"), "existing key file is committed");
568
+ assert.ok(!committed.includes("src/commands/list.ts"), "missing key file is silently dropped");
569
+
570
+ rmSync(repo, { recursive: true, force: true });
571
+ });
572
+
573
+ // Regression: #5500. When ALL keyFiles are bogus, scopedStageTaskFiles
574
+ // must return false so autoCommit falls back to smartStage. The commit
575
+ // still goes out (using `git add -A` semantics) instead of failing.
576
+ test('GitServiceImpl: all missing keyFiles falls back to smartStage', () => {
577
+ const repo = initTempRepo();
578
+ const svc = new GitServiceImpl(repo);
579
+
580
+ createFile(repo, "src/actually-changed.ts", "export const real = true;");
581
+
582
+ const msg = svc.autoCommit("execute-task", "M001/S01/T03", [], {
583
+ taskId: "S01/T03",
584
+ taskTitle: "fix path handling",
585
+ oneLiner: "Hardened path resolution",
586
+ keyFiles: ["src/wrong/path-1.ts", "src/wrong/path-2.ts"],
587
+ });
588
+ assert.ok(msg !== null, "autoCommit falls back to smartStage when all keyFiles are missing");
589
+
590
+ const committed = run("git show --name-only --format= HEAD", repo);
591
+ assert.ok(
592
+ committed.includes("src/actually-changed.ts"),
593
+ "smartStage fallback stages real dirty files when scoped staging finds nothing",
594
+ );
595
+
596
+ rmSync(repo, { recursive: true, force: true });
597
+ });
598
+
545
599
  // ─── GitServiceImpl: empty-after-staging guard ─────────────────────────
546
600
 
547
601
  test('GitServiceImpl: empty-after-staging guard', () => {
@@ -85,7 +85,11 @@ function makeMockDeps(
85
85
  resolveMilestoneFile: () => null,
86
86
  reconcileMergeState: () => "clean",
87
87
  preflightCleanRoot: () => ({ stashPushed: false, summary: "" }),
88
- postflightPopStash: () => {},
88
+ postflightPopStash: () => ({
89
+ restored: true,
90
+ needsManualRecovery: false,
91
+ message: "restored",
92
+ }),
89
93
  getLedger: () => ({ units: [] }),
90
94
  getProjectTotals: () => ({ cost: 0 }),
91
95
  formatCost: (c: number) => `$${c.toFixed(2)}`,
@@ -1,3 +1,6 @@
1
+ // Project/App: GSD-2
2
+ // File Purpose: Unit tests for pre-execution validation checks.
3
+
1
4
  /**
2
5
  * pre-execution-checks.test.ts — Unit tests for pre-execution validation checks.
3
6
  *
@@ -1542,6 +1545,27 @@ describe("checkFilePathConsistency directory inputs (#4446)", () => {
1542
1545
  assert.equal(results[0].blocking, true);
1543
1546
  });
1544
1547
 
1548
+ test("runtime directory annotation is skipped as a pre-execution file dependency", (t) => {
1549
+ const tempDir = join(tmpdir(), `pre-exec-dir-runtime-${Date.now()}`);
1550
+ mkdirSync(tempDir, { recursive: true });
1551
+ t.after(() => rmSync(tempDir, { recursive: true, force: true }));
1552
+
1553
+ const tasks = [
1554
+ createTask({
1555
+ id: "T02",
1556
+ inputs: ["entries/ directory (runtime)"],
1557
+ expected_output: ["src/commands/delete.ts", "src/index.ts"],
1558
+ }),
1559
+ ];
1560
+
1561
+ const results = checkFilePathConsistency(tasks, tempDir);
1562
+ assert.deepEqual(
1563
+ results,
1564
+ [],
1565
+ "Runtime-only directory inputs are created during command execution, not required before the task starts",
1566
+ );
1567
+ });
1568
+
1545
1569
  test("tilde-prefixed input is matched against $HOME, not the project basePath", (t) => {
1546
1570
  const fakeHome = join(tmpdir(), `pre-exec-tilde-home-${Date.now()}`);
1547
1571
  const projectDir = join(tmpdir(), `pre-exec-tilde-proj-${Date.now()}`);
@@ -1597,6 +1621,20 @@ describe("checkTaskOrdering directory inputs (#4446)", () => {
1597
1621
  "Directory reference should not be treated as reading a file created later",
1598
1622
  );
1599
1623
  });
1624
+
1625
+ test("runtime directory annotation does not produce an ordering violation", () => {
1626
+ const tasks = [
1627
+ createTask({
1628
+ id: "T02",
1629
+ sequence: 0,
1630
+ inputs: ["entries/ directory (runtime)"],
1631
+ expected_output: [],
1632
+ }),
1633
+ ];
1634
+
1635
+ const results = checkTaskOrdering(tasks, "/tmp");
1636
+ assert.deepEqual(results, []);
1637
+ });
1600
1638
  });
1601
1639
 
1602
1640
  // ─── Regression Tests: checkTaskOrdering false positive for pre-execution refs (#4071) ──
@@ -1,3 +1,5 @@
1
+ // Project/App: GSD-2
2
+ // File Purpose: WorktreeResolver unit and regression tests.
1
3
  import test from "node:test";
2
4
  import assert from "node:assert/strict";
3
5
  import { mkdtempSync, rmSync, mkdirSync, realpathSync } from "node:fs";
@@ -9,6 +11,17 @@ import {
9
11
  type NotifyCtx,
10
12
  } from "../worktree-resolver.js";
11
13
  import { AutoSession } from "../auto/session.js";
14
+ import {
15
+ closeDatabase,
16
+ insertMilestone,
17
+ openDatabase,
18
+ } from "../gsd-db.js";
19
+ import { registerAutoWorker } from "../db/auto-workers.js";
20
+ import {
21
+ claimMilestoneLease,
22
+ getMilestoneLease,
23
+ releaseMilestoneLease,
24
+ } from "../db/milestone-leases.js";
12
25
 
13
26
  // ─── Helpers ─────────────────────────────────────────────────────────────────
14
27
 
@@ -19,11 +32,12 @@ interface CallLog {
19
32
  }
20
33
 
21
34
  function makeSession(
22
- overrides?: Partial<{ basePath: string; originalBasePath: string }>,
35
+ overrides?: Partial<AutoSession>,
23
36
  ): AutoSession {
24
37
  const s = new AutoSession();
25
38
  s.basePath = overrides?.basePath ?? "/project";
26
39
  s.originalBasePath = overrides?.originalBasePath ?? "/project";
40
+ Object.assign(s, overrides);
27
41
  return s;
28
42
  }
29
43
 
@@ -182,6 +196,17 @@ function findCalls(calls: CallLog[], fn: string): CallLog[] {
182
196
  return calls.filter((c) => c.fn === fn);
183
197
  }
184
198
 
199
+ function makeDbBase(): string {
200
+ const base = mkdtempSync(join(tmpdir(), "gsd-worktree-resolver-"));
201
+ mkdirSync(join(base, ".gsd"), { recursive: true });
202
+ return base;
203
+ }
204
+
205
+ function cleanupDbBase(base: string): void {
206
+ try { closeDatabase(); } catch { /* noop */ }
207
+ try { rmSync(base, { recursive: true, force: true }); } catch { /* noop */ }
208
+ }
209
+
185
210
  // ─── Getter Tests ────────────────────────────────────────────────────────────
186
211
 
187
212
  test("workPath returns s.basePath", () => {
@@ -371,6 +396,43 @@ test("enterMilestone does not create double-nested worktree when originalBasePat
371
396
  );
372
397
  });
373
398
 
399
+ test("enterMilestone reacquires a released same-milestone lease before worktree entry", (t) => {
400
+ const base = makeDbBase();
401
+ t.after(() => cleanupDbBase(base));
402
+ openDatabase(join(base, ".gsd", "gsd.db"));
403
+ insertMilestone({ id: "M001", title: "Test milestone", status: "active" });
404
+
405
+ const workerId = registerAutoWorker({ projectRootRealpath: base });
406
+ const originalClaim = claimMilestoneLease(workerId, "M001");
407
+ assert.equal(originalClaim.ok, true);
408
+ if (!originalClaim.ok) throw new Error("expected test lease claim");
409
+ assert.equal(releaseMilestoneLease(workerId, "M001", originalClaim.token), true);
410
+
411
+ const s = makeSession({
412
+ basePath: base,
413
+ originalBasePath: base,
414
+ workerId,
415
+ currentMilestoneId: "M001",
416
+ milestoneLeaseToken: originalClaim.token,
417
+ });
418
+ const deps = makeDeps({
419
+ createAutoWorktree: (basePath: string, milestoneId: string) => join(basePath, ".gsd", "worktrees", milestoneId),
420
+ });
421
+ const ctx = makeNotifyCtx();
422
+ const resolver = new WorktreeResolver(s, deps);
423
+
424
+ resolver.enterMilestone("M001", ctx);
425
+
426
+ const row = getMilestoneLease("M001");
427
+ assert.ok(row);
428
+ assert.equal(row.worker_id, workerId);
429
+ assert.equal(row.status, "held");
430
+ assert.equal(row.fencing_token, originalClaim.token + 1);
431
+ assert.equal(s.milestoneLeaseToken, originalClaim.token + 1);
432
+ assert.equal(s.basePath, join(base, ".gsd", "worktrees", "M001"));
433
+ assert.equal(ctx.messages.some((m) => m.level === "error"), false);
434
+ });
435
+
374
436
  // ─── enterMilestone Tests (branch mode) ──────────────────────────────────────
375
437
 
376
438
  test("enterMilestone in branch mode calls enterBranchModeForMilestone and rebuilds GitService", () => {
@@ -25,7 +25,7 @@ import { emitWorktreeCreated, emitWorktreeMerged } from "./worktree-telemetry.js
25
25
  import { getCollapseCadence, getMilestoneResquash, resquashMilestoneOnMain } from "./slice-cadence.js";
26
26
  import { loadEffectiveGSDPreferences } from "./preferences.js";
27
27
  import { resolveWorktreeProjectRoot, normalizeWorktreePathForCompare } from "./worktree-root.js";
28
- import { claimMilestoneLease, releaseMilestoneLease } from "./db/milestone-leases.js";
28
+ import { claimMilestoneLease, refreshMilestoneLease, releaseMilestoneLease } from "./db/milestone-leases.js";
29
29
 
30
30
  // ─── Path Comparison Helper ────────────────────────────────────────────────
31
31
  /**
@@ -207,23 +207,44 @@ export class WorktreeResolver {
207
207
  // milestone (re-entry within the same session).
208
208
  if (this.s.workerId) {
209
209
  if (this.s.currentMilestoneId === milestoneId && this.s.milestoneLeaseToken !== null) {
210
- // Already held — no-op, the heartbeat in loop.ts refreshes TTL.
211
- } else {
212
- // If we held a different milestone, release it first so other
213
- // workers don't have to wait for TTL.
214
- if (this.s.currentMilestoneId && this.s.currentMilestoneId !== milestoneId && this.s.milestoneLeaseToken !== null) {
215
- try {
216
- releaseMilestoneLease(this.s.workerId, this.s.currentMilestoneId, this.s.milestoneLeaseToken);
217
- } catch (err) {
218
- debugLog("WorktreeResolver", {
219
- action: "enterMilestone",
220
- milestoneId,
221
- releasePriorLeaseError: err instanceof Error ? err.message : String(err),
222
- });
223
- }
210
+ const refreshed = refreshMilestoneLease(
211
+ this.s.workerId,
212
+ milestoneId,
213
+ this.s.milestoneLeaseToken,
214
+ );
215
+ if (refreshed) {
216
+ debugLog("WorktreeResolver", {
217
+ action: "enterMilestone",
218
+ milestoneId,
219
+ leaseRefreshed: true,
220
+ fencingToken: this.s.milestoneLeaseToken,
221
+ });
222
+ } else {
223
+ debugLog("WorktreeResolver", {
224
+ action: "enterMilestone",
225
+ milestoneId,
226
+ staleLeaseToken: this.s.milestoneLeaseToken,
227
+ });
224
228
  this.s.milestoneLeaseToken = null;
225
229
  }
230
+ }
231
+
232
+ // If we held a different milestone, release it first so other
233
+ // workers don't have to wait for TTL.
234
+ if (this.s.currentMilestoneId && this.s.currentMilestoneId !== milestoneId && this.s.milestoneLeaseToken !== null) {
235
+ try {
236
+ releaseMilestoneLease(this.s.workerId, this.s.currentMilestoneId, this.s.milestoneLeaseToken);
237
+ } catch (err) {
238
+ debugLog("WorktreeResolver", {
239
+ action: "enterMilestone",
240
+ milestoneId,
241
+ releasePriorLeaseError: err instanceof Error ? err.message : String(err),
242
+ });
243
+ }
244
+ this.s.milestoneLeaseToken = null;
245
+ }
226
246
 
247
+ if (this.s.milestoneLeaseToken === null) {
227
248
  try {
228
249
  const claim = claimMilestoneLease(this.s.workerId, milestoneId);
229
250
  if (claim.ok) {