gsd-pi 2.35.0-dev.55dcc60 → 2.35.0-dev.6179610

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 (104) hide show
  1. package/README.md +3 -1
  2. package/dist/cli.js +7 -2
  3. package/dist/resource-loader.d.ts +1 -1
  4. package/dist/resource-loader.js +13 -1
  5. package/dist/resources/extensions/async-jobs/await-tool.js +0 -2
  6. package/dist/resources/extensions/async-jobs/job-manager.js +0 -6
  7. package/dist/resources/extensions/bg-shell/output-formatter.js +1 -19
  8. package/dist/resources/extensions/bg-shell/process-manager.js +0 -4
  9. package/dist/resources/extensions/bg-shell/types.js +0 -2
  10. package/dist/resources/extensions/context7/index.js +5 -0
  11. package/dist/resources/extensions/get-secrets-from-user.js +2 -30
  12. package/dist/resources/extensions/google-search/index.js +5 -0
  13. package/dist/resources/extensions/gsd/auto-dispatch.js +43 -1
  14. package/dist/resources/extensions/gsd/auto-loop.js +10 -1
  15. package/dist/resources/extensions/gsd/auto-recovery.js +35 -0
  16. package/dist/resources/extensions/gsd/auto-start.js +35 -2
  17. package/dist/resources/extensions/gsd/auto.js +59 -4
  18. package/dist/resources/extensions/gsd/commands-handlers.js +2 -2
  19. package/dist/resources/extensions/gsd/commands-rate.js +31 -0
  20. package/dist/resources/extensions/gsd/commands.js +6 -0
  21. package/dist/resources/extensions/gsd/doctor-environment.js +26 -17
  22. package/dist/resources/extensions/gsd/files.js +11 -2
  23. package/dist/resources/extensions/gsd/gitignore.js +54 -7
  24. package/dist/resources/extensions/gsd/guided-flow.js +1 -1
  25. package/dist/resources/extensions/gsd/health-widget-core.js +96 -0
  26. package/dist/resources/extensions/gsd/health-widget.js +97 -46
  27. package/dist/resources/extensions/gsd/index.js +26 -33
  28. package/dist/resources/extensions/gsd/migrate-external.js +55 -2
  29. package/dist/resources/extensions/gsd/milestone-ids.js +3 -2
  30. package/dist/resources/extensions/gsd/paths.js +74 -7
  31. package/dist/resources/extensions/gsd/post-unit-hooks.js +4 -1
  32. package/dist/resources/extensions/gsd/preferences-validation.js +16 -1
  33. package/dist/resources/extensions/gsd/preferences.js +12 -0
  34. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  35. package/dist/resources/extensions/gsd/prompts/validate-milestone.md +2 -0
  36. package/dist/resources/extensions/gsd/roadmap-mutations.js +55 -0
  37. package/dist/resources/extensions/gsd/session-lock.js +53 -2
  38. package/dist/resources/extensions/gsd/state.js +2 -1
  39. package/dist/resources/extensions/gsd/templates/plan.md +8 -0
  40. package/dist/resources/extensions/gsd/worktree-resolver.js +12 -0
  41. package/dist/resources/extensions/remote-questions/remote-command.js +2 -22
  42. package/dist/resources/extensions/shared/mod.js +1 -1
  43. package/dist/resources/extensions/shared/sanitize.js +30 -0
  44. package/dist/resources/extensions/subagent/index.js +6 -14
  45. package/package.json +2 -1
  46. package/packages/pi-ai/dist/providers/openai-responses.js +1 -1
  47. package/packages/pi-ai/dist/providers/openai-responses.js.map +1 -1
  48. package/packages/pi-ai/src/providers/openai-responses.ts +1 -1
  49. package/packages/pi-coding-agent/dist/core/resource-loader.d.ts.map +1 -1
  50. package/packages/pi-coding-agent/dist/core/resource-loader.js +13 -2
  51. package/packages/pi-coding-agent/dist/core/resource-loader.js.map +1 -1
  52. package/packages/pi-coding-agent/src/core/resource-loader.ts +13 -2
  53. package/src/resources/extensions/async-jobs/await-tool.ts +0 -2
  54. package/src/resources/extensions/async-jobs/job-manager.ts +0 -7
  55. package/src/resources/extensions/bg-shell/output-formatter.ts +0 -17
  56. package/src/resources/extensions/bg-shell/process-manager.ts +0 -4
  57. package/src/resources/extensions/bg-shell/types.ts +0 -12
  58. package/src/resources/extensions/context7/index.ts +7 -0
  59. package/src/resources/extensions/get-secrets-from-user.ts +2 -35
  60. package/src/resources/extensions/google-search/index.ts +7 -0
  61. package/src/resources/extensions/gsd/auto-dispatch.ts +49 -1
  62. package/src/resources/extensions/gsd/auto-loop.ts +11 -1
  63. package/src/resources/extensions/gsd/auto-recovery.ts +39 -0
  64. package/src/resources/extensions/gsd/auto-start.ts +42 -2
  65. package/src/resources/extensions/gsd/auto.ts +61 -3
  66. package/src/resources/extensions/gsd/commands-handlers.ts +2 -2
  67. package/src/resources/extensions/gsd/commands-rate.ts +55 -0
  68. package/src/resources/extensions/gsd/commands.ts +7 -0
  69. package/src/resources/extensions/gsd/doctor-environment.ts +26 -16
  70. package/src/resources/extensions/gsd/files.ts +12 -2
  71. package/src/resources/extensions/gsd/gitignore.ts +54 -7
  72. package/src/resources/extensions/gsd/guided-flow.ts +1 -1
  73. package/src/resources/extensions/gsd/health-widget-core.ts +129 -0
  74. package/src/resources/extensions/gsd/health-widget.ts +103 -59
  75. package/src/resources/extensions/gsd/index.ts +30 -33
  76. package/src/resources/extensions/gsd/migrate-external.ts +47 -2
  77. package/src/resources/extensions/gsd/milestone-ids.ts +3 -2
  78. package/src/resources/extensions/gsd/paths.ts +73 -7
  79. package/src/resources/extensions/gsd/post-unit-hooks.ts +5 -1
  80. package/src/resources/extensions/gsd/preferences-validation.ts +16 -1
  81. package/src/resources/extensions/gsd/preferences.ts +14 -1
  82. package/src/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  83. package/src/resources/extensions/gsd/prompts/validate-milestone.md +2 -0
  84. package/src/resources/extensions/gsd/roadmap-mutations.ts +66 -0
  85. package/src/resources/extensions/gsd/session-lock.ts +59 -2
  86. package/src/resources/extensions/gsd/state.ts +2 -1
  87. package/src/resources/extensions/gsd/templates/plan.md +8 -0
  88. package/src/resources/extensions/gsd/tests/files-loadfile-eisdir.test.ts +20 -0
  89. package/src/resources/extensions/gsd/tests/gitignore-tracked-gsd.test.ts +214 -0
  90. package/src/resources/extensions/gsd/tests/health-widget.test.ts +158 -0
  91. package/src/resources/extensions/gsd/tests/paths.test.ts +113 -0
  92. package/src/resources/extensions/gsd/tests/preferences.test.ts +12 -2
  93. package/src/resources/extensions/gsd/tests/queue-reorder-e2e.test.ts +26 -0
  94. package/src/resources/extensions/gsd/tests/test-utils.ts +165 -0
  95. package/src/resources/extensions/gsd/tests/validate-directory.test.ts +15 -0
  96. package/src/resources/extensions/gsd/tests/validate-milestone.test.ts +7 -0
  97. package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +32 -0
  98. package/src/resources/extensions/gsd/worktree-resolver.ts +11 -0
  99. package/src/resources/extensions/remote-questions/remote-command.ts +2 -23
  100. package/src/resources/extensions/shared/mod.ts +1 -1
  101. package/src/resources/extensions/shared/sanitize.ts +36 -0
  102. package/src/resources/extensions/subagent/index.ts +6 -12
  103. package/dist/resources/extensions/shared/wizard-ui.js +0 -478
  104. package/src/resources/extensions/shared/wizard-ui.ts +0 -551
@@ -10,7 +10,8 @@
10
10
  */
11
11
 
12
12
  import { readdirSync, existsSync, realpathSync, Dirent } from "node:fs";
13
- import { join } from "node:path";
13
+ import { join, dirname, normalize } from "node:path";
14
+ import { spawnSync } from "node:child_process";
14
15
  import { nativeScanGsdTree, type GsdTreeEntry } from "./native-parser-bridge.js";
15
16
  import { DIR_CACHE_MAX } from "./constants.js";
16
17
 
@@ -277,15 +278,80 @@ const LEGACY_GSD_ROOT_FILES: Record<GSDRootFileKey, string> = {
277
278
  KNOWLEDGE: "knowledge.md",
278
279
  };
279
280
 
281
+ // ─── GSD Root Discovery ───────────────────────────────────────────────────────
282
+
283
+ const gsdRootCache = new Map<string, string>();
284
+
285
+ /** Exported for tests only — do not call in production code. */
286
+ export function _clearGsdRootCache(): void {
287
+ gsdRootCache.clear();
288
+ }
289
+
290
+ /**
291
+ * Resolve the `.gsd` directory for a given project base path.
292
+ *
293
+ * Probe order:
294
+ * 1. basePath/.gsd — fast path (common case)
295
+ * 2. git rev-parse root — handles cwd-is-a-subdirectory
296
+ * 3. Walk up from basePath — handles moved .gsd in an ancestor (bounded by git root)
297
+ * 4. basePath/.gsd — creation fallback (init scenario)
298
+ *
299
+ * Result is cached per basePath for the process lifetime.
300
+ */
280
301
  export function gsdRoot(basePath: string): string {
281
- const local = join(basePath, ".gsd");
282
- try {
283
- const resolved = realpathSync(local);
284
- if (resolved !== local) return resolved; // symlink resolved
285
- } catch { /* doesn't exist yet — fall through */ }
286
- return local; // backwards compat: unmigrated projects
302
+ const cached = gsdRootCache.get(basePath);
303
+ if (cached) return cached;
304
+
305
+ const result = probeGsdRoot(basePath);
306
+ gsdRootCache.set(basePath, result);
307
+ return result;
287
308
  }
288
309
 
310
+ function probeGsdRoot(rawBasePath: string): string {
311
+ // 1. Fast path — check the input path directly
312
+ const local = join(rawBasePath, ".gsd");
313
+ if (existsSync(local)) return local;
314
+
315
+ // Resolve symlinks so path comparisons work correctly across platforms
316
+ // (e.g. macOS /var → /private/var). Use rawBasePath as fallback if not resolvable.
317
+ let basePath: string;
318
+ try { basePath = realpathSync.native(rawBasePath); } catch { basePath = rawBasePath; }
319
+
320
+ // 2. Git root anchor — used as both probe target and walk-up boundary
321
+ // Only walk if we're inside a git project — prevents escaping into
322
+ // unrelated filesystem territory when running outside any repo.
323
+ let gitRoot: string | null = null;
324
+ try {
325
+ const out = spawnSync("git", ["rev-parse", "--show-toplevel"], {
326
+ cwd: basePath,
327
+ encoding: "utf-8",
328
+ });
329
+ if (out.status === 0) {
330
+ const r = out.stdout.trim();
331
+ if (r) gitRoot = normalize(r);
332
+ }
333
+ } catch { /* git not available */ }
334
+
335
+ if (gitRoot) {
336
+ const candidate = join(gitRoot, ".gsd");
337
+ if (existsSync(candidate)) return candidate;
338
+ }
339
+
340
+ // 3. Walk up from basePath to the git root (only if we are in a subdirectory)
341
+ if (gitRoot && basePath !== gitRoot) {
342
+ let cur = dirname(basePath);
343
+ while (cur !== basePath) {
344
+ const candidate = join(cur, ".gsd");
345
+ if (existsSync(candidate)) return candidate;
346
+ if (cur === gitRoot) break;
347
+ basePath = cur;
348
+ cur = dirname(cur);
349
+ }
350
+ }
351
+
352
+ // 4. Fallback for init/creation
353
+ return local;
354
+ }
289
355
  export function milestonesDir(basePath: string): string {
290
356
  return join(gsdRoot(basePath), "milestones");
291
357
  }
@@ -149,11 +149,15 @@ function dequeueNextHook(basePath: string): HookDispatchResult | null {
149
149
 
150
150
  // Build the prompt with variable substitution
151
151
  const [mid, sid, tid] = triggerUnitId.split("/");
152
- const prompt = config.prompt
152
+ let prompt = config.prompt
153
153
  .replace(/\{milestoneId\}/g, mid ?? "")
154
154
  .replace(/\{sliceId\}/g, sid ?? "")
155
155
  .replace(/\{taskId\}/g, tid ?? "");
156
156
 
157
+ // Inject browser safety instruction for hooks that may use browser tools (#1345).
158
+ // Vite HMR and other persistent connections prevent networkidle from resolving.
159
+ prompt += "\n\n**Browser tool safety:** Do NOT use `browser_wait_for` with `condition: \"network_idle\"` — it hangs indefinitely when dev servers keep persistent connections (Vite HMR, WebSocket). Use `selector_visible`, `text_visible`, or `delay` instead.";
160
+
157
161
  return {
158
162
  hookName: config.name,
159
163
  prompt,
@@ -33,9 +33,24 @@ export function validatePreferences(preferences: GSDPreferences): {
33
33
  const validated: GSDPreferences = {};
34
34
 
35
35
  // ─── Unknown Key Detection ──────────────────────────────────────────
36
+ // Common key migration hints for pi-level settings that don't map to GSD prefs
37
+ const KEY_MIGRATION_HINTS: Record<string, string> = {
38
+ taskIsolation: 'use "git.isolation" instead (values: worktree, branch, none)',
39
+ task_isolation: 'use "git.isolation" instead (values: worktree, branch, none)',
40
+ isolation: 'use "git.isolation" instead (values: worktree, branch, none)',
41
+ manage_gitignore: 'use "git.manage_gitignore" instead',
42
+ auto_push: 'use "git.auto_push" instead',
43
+ main_branch: 'use "git.main_branch" instead',
44
+ };
45
+
36
46
  for (const key of Object.keys(preferences)) {
37
47
  if (!KNOWN_PREFERENCE_KEYS.has(key)) {
38
- warnings.push(`unknown preference key "${key}" — ignored`);
48
+ const hint = KEY_MIGRATION_HINTS[key];
49
+ if (hint) {
50
+ warnings.push(`unknown preference key "${key}" — ${hint}`);
51
+ } else {
52
+ warnings.push(`unknown preference key "${key}" — ignored`);
53
+ }
39
54
  }
40
55
  }
41
56
 
@@ -15,9 +15,10 @@ import { homedir } from "node:os";
15
15
  import { join } from "node:path";
16
16
  import { gsdRoot } from "./paths.js";
17
17
  import { parse as parseYaml } from "yaml";
18
- import type { PostUnitHookConfig, PreDispatchHookConfig } from "./types.js";
18
+ import type { PostUnitHookConfig, PreDispatchHookConfig, TokenProfile } from "./types.js";
19
19
  import type { DynamicRoutingConfig } from "./model-router.js";
20
20
  import { normalizeStringArray } from "../shared/mod.js";
21
+ import { resolveProfileDefaults as _resolveProfileDefaults } from "./preferences-models.js";
21
22
 
22
23
  import {
23
24
  MODE_DEFAULTS,
@@ -141,6 +142,18 @@ export function loadEffectiveGSDPreferences(): LoadedGSDPreferences | null {
141
142
  };
142
143
  }
143
144
 
145
+ // Apply token-profile defaults as the lowest-priority layer so that
146
+ // `token_profile: budget` sets models and phase-skips automatically.
147
+ // Explicit user preferences always override profile defaults.
148
+ const profile = result.preferences.token_profile as TokenProfile | undefined;
149
+ if (profile) {
150
+ const profileDefaults = _resolveProfileDefaults(profile);
151
+ result = {
152
+ ...result,
153
+ preferences: mergePreferences(profileDefaults as GSDPreferences, result.preferences),
154
+ };
155
+ }
156
+
144
157
  // Apply mode defaults as the lowest-priority layer
145
158
  if (result.preferences.mode) {
146
159
  result = {
@@ -28,6 +28,8 @@ Then:
28
28
 
29
29
  **Important:** Do NOT skip the success criteria and definition of done verification (steps 3-4). The milestone summary must reflect actual verified outcomes, not assumed success. If any criterion was not met, document it clearly in the summary and do not mark the milestone as passing verification.
30
30
 
31
+ **File system safety:** When scanning milestone directories for evidence, use `ls` or `find` to list directory contents first — never pass a directory path (e.g. `tasks/`, `slices/`) directly to the `read` tool. The `read` tool only accepts file paths, not directories.
32
+
31
33
  **You MUST write `{{milestoneSummaryPath}}` AND update PROJECT.md before finishing.**
32
34
 
33
35
  When done, say: "Milestone {{milestoneId}} complete."
@@ -67,4 +67,6 @@ If verdict is `needs-remediation`:
67
67
 
68
68
  **You MUST write `{{validationPath}}` before finishing.**
69
69
 
70
+ **File system safety:** When scanning milestone directories for evidence, use `ls` or `find` to list directory contents first — never pass a directory path (e.g. `tasks/`, `slices/`) directly to the `read` tool. The `read` tool only accepts file paths, not directories.
71
+
70
72
  When done, say: "Milestone {{milestoneId}} validation complete — verdict: <verdict>."
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Roadmap Mutations — shared utilities for modifying roadmap checkbox state.
3
+ *
4
+ * Extracts the duplicated "flip slice checkbox" pattern that existed in
5
+ * doctor.ts, mechanical-completion.ts, and auto-recovery.ts.
6
+ */
7
+
8
+ import { readFileSync } from "node:fs";
9
+ import { atomicWriteSync } from "./atomic-write.js";
10
+ import { resolveMilestoneFile } from "./paths.js";
11
+ import { clearParseCache } from "./files.js";
12
+
13
+ /**
14
+ * Mark a slice as done ([x]) in the milestone roadmap.
15
+ * Idempotent — no-op if already checked or if the slice isn't found.
16
+ *
17
+ * @returns true if the roadmap was modified, false if no change was needed
18
+ */
19
+ export function markSliceDoneInRoadmap(basePath: string, mid: string, sid: string): boolean {
20
+ const roadmapFile = resolveMilestoneFile(basePath, mid, "ROADMAP");
21
+ if (!roadmapFile) return false;
22
+
23
+ let content: string;
24
+ try {
25
+ content = readFileSync(roadmapFile, "utf-8");
26
+ } catch {
27
+ return false;
28
+ }
29
+
30
+ const updated = content.replace(
31
+ new RegExp(`^(\\s*-\\s+)\\[ \\]\\s+\\*\\*${sid}:`, "m"),
32
+ `$1[x] **${sid}:`,
33
+ );
34
+
35
+ if (updated === content) return false;
36
+
37
+ atomicWriteSync(roadmapFile, updated);
38
+ clearParseCache();
39
+ return true;
40
+ }
41
+
42
+ /**
43
+ * Mark a task as done ([x]) in the slice plan.
44
+ * Idempotent — no-op if already checked or if the task isn't found.
45
+ *
46
+ * @returns true if the plan was modified, false if no change was needed
47
+ */
48
+ export function markTaskDoneInPlan(basePath: string, planPath: string, tid: string): boolean {
49
+ let content: string;
50
+ try {
51
+ content = readFileSync(planPath, "utf-8");
52
+ } catch {
53
+ return false;
54
+ }
55
+
56
+ const updated = content.replace(
57
+ new RegExp(`^(\\s*-\\s+)\\[ \\]\\s+\\*\\*${tid}:`, "m"),
58
+ `$1[x] **${tid}:`,
59
+ );
60
+
61
+ if (updated === content) return false;
62
+
63
+ atomicWriteSync(planPath, updated);
64
+ clearParseCache();
65
+ return true;
66
+ }
@@ -57,9 +57,19 @@ let _lockCompromised: boolean = false;
57
57
  /** Whether we've already registered a process.on('exit') handler. */
58
58
  let _exitHandlerRegistered: boolean = false;
59
59
 
60
+ /** Snapshotted lock file path — captured at acquireSessionLock time to avoid
61
+ * gsdRoot() resolving differently in worktree vs project root contexts (#1363). */
62
+ let _snapshotLockPath: string | null = null;
63
+
64
+ /** Timestamp when the session lock was acquired — used to detect false-positive
65
+ * onCompromised events from event loop stalls within the stale window (#1362). */
66
+ let _lockAcquiredAt: number = 0;
67
+
60
68
  const LOCK_FILE = "auto.lock";
61
69
 
62
70
  function lockPath(basePath: string): string {
71
+ // If we have a snapshotted path from acquisition, use it for consistency
72
+ if (_snapshotLockPath) return _snapshotLockPath;
63
73
  return join(gsdRoot(basePath), LOCK_FILE);
64
74
  }
65
75
 
@@ -198,8 +208,19 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
198
208
  onCompromised: () => {
199
209
  // proper-lockfile detected mtime drift (system sleep, event loop stall, etc.).
200
210
  // Default handler throws inside setTimeout — an uncaught exception that crashes
201
- // or corrupts process state. Instead, set a flag so validateSessionLock() can
202
- // detect the compromise gracefully on the next dispatch cycle.
211
+ // or corrupts process state.
212
+ //
213
+ // False-positive suppression (#1362): If we're still within the stale window
214
+ // (30 min since acquisition), the mtime mismatch is from an event loop stall
215
+ // during a long LLM call — not a real takeover. Log and continue.
216
+ const elapsed = Date.now() - _lockAcquiredAt;
217
+ if (elapsed < 1_800_000) {
218
+ process.stderr.write(
219
+ `[gsd] Lock heartbeat mismatch after ${Math.round(elapsed / 1000)}s — event loop stall, continuing.\n`,
220
+ );
221
+ return; // Suppress false positive
222
+ }
223
+ // Past the stale window — this is a real compromise
203
224
  _lockCompromised = true;
204
225
  _releaseFunction = null;
205
226
  },
@@ -209,6 +230,8 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
209
230
  _lockedPath = basePath;
210
231
  _lockPid = process.pid;
211
232
  _lockCompromised = false;
233
+ _lockAcquiredAt = Date.now();
234
+ _snapshotLockPath = lp; // Snapshot the resolved path for consistent access (#1363)
212
235
 
213
236
  // Safety net: clean up lock dir on process exit if _releaseFunction
214
237
  // wasn't called (e.g., normal exit after clean completion) (#1245).
@@ -237,6 +260,16 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
237
260
  stale: 1_800_000, // 30 minutes — match primary lock settings
238
261
  update: 10_000,
239
262
  onCompromised: () => {
263
+ // Same false-positive suppression as the primary lock (#1512).
264
+ // Without this, the retry path fires _lockCompromised unconditionally
265
+ // on benign mtime drift (laptop sleep, heavy LLM event loop stalls).
266
+ const elapsed = Date.now() - _lockAcquiredAt;
267
+ if (elapsed < 1_800_000) {
268
+ process.stderr.write(
269
+ `[gsd] Lock heartbeat mismatch after ${Math.round(elapsed / 1000)}s — event loop stall, continuing.\n`,
270
+ );
271
+ return;
272
+ }
240
273
  _lockCompromised = true;
241
274
  _releaseFunction = null;
242
275
  },
@@ -245,6 +278,8 @@ export function acquireSessionLock(basePath: string): SessionLockResult {
245
278
  _lockedPath = basePath;
246
279
  _lockPid = process.pid;
247
280
  _lockCompromised = false;
281
+ _lockAcquiredAt = Date.now();
282
+ _snapshotLockPath = lp; // Snapshot for retry path too (#1363)
248
283
 
249
284
  // Safety net — uses centralized handler to avoid double-registration
250
285
  ensureExitHandler(gsdDir);
@@ -336,6 +371,26 @@ export function updateSessionLock(
336
371
  export function validateSessionLock(basePath: string): boolean {
337
372
  // Lock was compromised by proper-lockfile (mtime drift from sleep, stall, etc.)
338
373
  if (_lockCompromised) {
374
+ // Recovery gate (#1512): Before declaring the lock lost, check if the lock
375
+ // file still contains our PID. If it does, no other process took over — the
376
+ // onCompromised fired from benign mtime drift (laptop sleep, event loop stall
377
+ // beyond the stale window). Attempt re-acquisition instead of giving up.
378
+ const lp = lockPath(basePath);
379
+ const existing = readExistingLockData(lp);
380
+ if (existing && existing.pid === process.pid) {
381
+ // Lock file still ours — try to re-acquire the OS lock
382
+ try {
383
+ const result = acquireSessionLock(basePath);
384
+ if (result.acquired) {
385
+ process.stderr.write(
386
+ `[gsd] Lock recovered after onCompromised — lock file PID matched, re-acquired.\n`,
387
+ );
388
+ return true;
389
+ }
390
+ } catch {
391
+ // Re-acquisition failed — fall through to return false
392
+ }
393
+ }
339
394
  return false;
340
395
  }
341
396
 
@@ -394,6 +449,8 @@ export function releaseSessionLock(basePath: string): void {
394
449
  _lockedPath = null;
395
450
  _lockPid = 0;
396
451
  _lockCompromised = false;
452
+ _lockAcquiredAt = 0;
453
+ _snapshotLockPath = null;
397
454
  }
398
455
 
399
456
  /**
@@ -64,11 +64,12 @@ export function isValidationTerminal(validationContent: string): boolean {
64
64
  if (!match) return false;
65
65
  const verdict = match[1].match(/verdict:\s*(\S+)/);
66
66
  if (!verdict) return false;
67
+ const v = verdict[1] === 'passed' ? 'pass' : verdict[1];
67
68
  // 'pass' and 'needs-attention' are always terminal.
68
69
  // 'needs-remediation' is treated as terminal to prevent infinite loops
69
70
  // when no remediation slices exist in the roadmap (#832). The validation
70
71
  // report is preserved on disk for manual review.
71
- return verdict[1] === 'pass' || verdict[1] === 'needs-attention' || verdict[1] === 'needs-remediation';
72
+ return v === 'pass' || v === 'needs-attention' || v === 'needs-remediation';
72
73
  }
73
74
 
74
75
  // ─── State Derivation ──────────────────────────────────────────────────────
@@ -113,6 +113,14 @@
113
113
  - Tasks execute sequentially in order (T01, T02, T03, ...)
114
114
  - est: is informational (e.g. 30m, 1h, 2h) and optional
115
115
 
116
+ Verify field rules:
117
+ - MUST be a mechanically executable command: `npm test`, `grep -q "pattern" file`, `test -f path`
118
+ - For content/document tasks: verify file existence, section count, YAML validity, or word count
119
+ NOT exact phrasing, specific formulas, or "zero TBD" aspirational criteria
120
+ - If no command can verify the output, write: "Manual review — file exists and is non-empty"
121
+ - BAD: "Sections 3.1 and 3.2 exist with exact formulas. Zero TBD/TODO."
122
+ - GOOD: `grep -c "^## " doc.md` returns >= 4 (4+ sections), `! grep -q "TBD\|TODO" doc.md`
123
+
116
124
  Integration closure rule:
117
125
  - At least one slice in any multi-boundary milestone should perform real composition/wiring, not just contract hardening
118
126
  - For the final assembly slice, verification must exercise the real entrypoint or runtime path
@@ -0,0 +1,20 @@
1
+ import test from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import os from "node:os";
4
+ import path from "node:path";
5
+ import fs from "node:fs";
6
+
7
+ import { loadFile } from "../files.ts";
8
+
9
+ test("loadFile returns null for directory paths instead of throwing EISDIR", async () => {
10
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "gsd-loadfile-eisdir-"));
11
+ const dirPath = path.join(tmp, "tasks");
12
+ fs.mkdirSync(dirPath);
13
+
14
+ try {
15
+ const result = await loadFile(dirPath);
16
+ assert.equal(result, null);
17
+ } finally {
18
+ fs.rmSync(tmp, { recursive: true, force: true });
19
+ }
20
+ });
@@ -0,0 +1,214 @@
1
+ /**
2
+ * gitignore-tracked-gsd.test.ts — Regression tests for #1364.
3
+ *
4
+ * Verifies that ensureGitignore() does NOT add ".gsd" to .gitignore
5
+ * when .gsd/ contains git-tracked files, and that migrateToExternalState()
6
+ * aborts migration for tracked .gsd/ directories.
7
+ *
8
+ * Uses real temporary git repos — no mocks.
9
+ */
10
+
11
+ import test from "node:test";
12
+ import assert from "node:assert/strict";
13
+ import { execFileSync } from "node:child_process";
14
+ import {
15
+ existsSync,
16
+ mkdirSync,
17
+ mkdtempSync,
18
+ readFileSync,
19
+ rmSync,
20
+ writeFileSync,
21
+ } from "node:fs";
22
+ import { join } from "node:path";
23
+ import { tmpdir } from "node:os";
24
+
25
+ import { ensureGitignore, hasGitTrackedGsdFiles } from "../gitignore.ts";
26
+ import { migrateToExternalState } from "../migrate-external.ts";
27
+
28
+ // ─── Helpers ─────────────────────────────────────────────────────────
29
+
30
+ function git(dir: string, ...args: string[]): string {
31
+ return execFileSync("git", args, { cwd: dir, stdio: "pipe", encoding: "utf-8" }).trim();
32
+ }
33
+
34
+ function makeTempRepo(): string {
35
+ const dir = mkdtempSync(join(tmpdir(), "gsd-gitignore-test-"));
36
+ git(dir, "init");
37
+ git(dir, "config", "user.email", "test@test.com");
38
+ git(dir, "config", "user.name", "Test");
39
+ writeFileSync(join(dir, "README.md"), "# init\n");
40
+ git(dir, "add", "-A");
41
+ git(dir, "commit", "-m", "init");
42
+ git(dir, "branch", "-M", "main");
43
+ return dir;
44
+ }
45
+
46
+ function cleanup(dir: string): void {
47
+ try {
48
+ rmSync(dir, { recursive: true, force: true });
49
+ } catch {
50
+ // ignore
51
+ }
52
+ }
53
+
54
+ // ─── hasGitTrackedGsdFiles ───────────────────────────────────────────
55
+
56
+ test("hasGitTrackedGsdFiles returns false when .gsd/ does not exist", () => {
57
+ const dir = makeTempRepo();
58
+ try {
59
+ assert.equal(hasGitTrackedGsdFiles(dir), false);
60
+ } finally {
61
+ cleanup(dir);
62
+ }
63
+ });
64
+
65
+ test("hasGitTrackedGsdFiles returns true when .gsd/ has tracked files", () => {
66
+ const dir = makeTempRepo();
67
+ try {
68
+ mkdirSync(join(dir, ".gsd", "milestones"), { recursive: true });
69
+ writeFileSync(join(dir, ".gsd", "PROJECT.md"), "# Test Project\n");
70
+ git(dir, "add", ".gsd/PROJECT.md");
71
+ git(dir, "commit", "-m", "add gsd");
72
+ assert.equal(hasGitTrackedGsdFiles(dir), true);
73
+ } finally {
74
+ cleanup(dir);
75
+ }
76
+ });
77
+
78
+ test("hasGitTrackedGsdFiles returns false when .gsd/ exists but is untracked", () => {
79
+ const dir = makeTempRepo();
80
+ try {
81
+ mkdirSync(join(dir, ".gsd"), { recursive: true });
82
+ writeFileSync(join(dir, ".gsd", "STATE.md"), "state\n");
83
+ // Not git-added — should return false
84
+ assert.equal(hasGitTrackedGsdFiles(dir), false);
85
+ } finally {
86
+ cleanup(dir);
87
+ }
88
+ });
89
+
90
+ // ─── ensureGitignore — tracked .gsd/ protection ─────────────────────
91
+
92
+ test("ensureGitignore does NOT add .gsd when .gsd/ has tracked files (#1364)", () => {
93
+ const dir = makeTempRepo();
94
+ try {
95
+ // Set up .gsd/ with tracked files
96
+ mkdirSync(join(dir, ".gsd", "milestones"), { recursive: true });
97
+ writeFileSync(join(dir, ".gsd", "PROJECT.md"), "# Test Project\n");
98
+ writeFileSync(join(dir, ".gsd", "DECISIONS.md"), "# Decisions\n");
99
+ git(dir, "add", ".gsd/");
100
+ git(dir, "commit", "-m", "track gsd state");
101
+
102
+ // Run ensureGitignore
103
+ ensureGitignore(dir);
104
+
105
+ // Verify .gsd is NOT in .gitignore
106
+ const gitignore = readFileSync(join(dir, ".gitignore"), "utf-8");
107
+ const lines = gitignore.split("\n").map((l) => l.trim());
108
+ assert.ok(
109
+ !lines.includes(".gsd"),
110
+ `Expected .gsd NOT to appear in .gitignore, but it does:\n${gitignore}`,
111
+ );
112
+
113
+ // Other baseline patterns should still be present
114
+ assert.ok(lines.includes(".DS_Store"), "Expected .DS_Store in .gitignore");
115
+ assert.ok(lines.includes("node_modules/"), "Expected node_modules/ in .gitignore");
116
+ } finally {
117
+ cleanup(dir);
118
+ }
119
+ });
120
+
121
+ test("ensureGitignore adds .gsd when .gsd/ has NO tracked files", () => {
122
+ const dir = makeTempRepo();
123
+ try {
124
+ // Run ensureGitignore (no .gsd/ at all)
125
+ ensureGitignore(dir);
126
+
127
+ // Verify .gsd IS in .gitignore
128
+ const gitignore = readFileSync(join(dir, ".gitignore"), "utf-8");
129
+ const lines = gitignore.split("\n").map((l) => l.trim());
130
+ assert.ok(
131
+ lines.includes(".gsd"),
132
+ `Expected .gsd in .gitignore, but it's missing:\n${gitignore}`,
133
+ );
134
+ } finally {
135
+ cleanup(dir);
136
+ }
137
+ });
138
+
139
+ test("ensureGitignore respects manageGitignore: false", () => {
140
+ const dir = makeTempRepo();
141
+ try {
142
+ const result = ensureGitignore(dir, { manageGitignore: false });
143
+ assert.equal(result, false);
144
+ assert.ok(!existsSync(join(dir, ".gitignore")), "Should not create .gitignore");
145
+ } finally {
146
+ cleanup(dir);
147
+ }
148
+ });
149
+
150
+ // ─── ensureGitignore — verify no tracked files become invisible ─────
151
+
152
+ test("ensureGitignore with tracked .gsd/ does not cause git to see files as deleted", () => {
153
+ const dir = makeTempRepo();
154
+ try {
155
+ // Create tracked .gsd/ files
156
+ mkdirSync(join(dir, ".gsd", "milestones", "M001"), { recursive: true });
157
+ writeFileSync(join(dir, ".gsd", "PROJECT.md"), "# Project\n");
158
+ writeFileSync(
159
+ join(dir, ".gsd", "milestones", "M001", "M001-CONTEXT.md"),
160
+ "# M001\n",
161
+ );
162
+ git(dir, "add", ".gsd/");
163
+ git(dir, "commit", "-m", "track gsd state");
164
+
165
+ // Run ensureGitignore
166
+ ensureGitignore(dir);
167
+
168
+ // git status should show NO deleted files under .gsd/
169
+ const status = git(dir, "status", "--porcelain", ".gsd/");
170
+
171
+ // Filter for deletions (lines starting with " D" or "D ")
172
+ const deletions = status
173
+ .split("\n")
174
+ .filter((l) => l.match(/^\s*D\s/) || l.match(/^D\s/));
175
+
176
+ assert.equal(
177
+ deletions.length,
178
+ 0,
179
+ `Expected no deleted .gsd/ files, but found:\n${deletions.join("\n")}`,
180
+ );
181
+ } finally {
182
+ cleanup(dir);
183
+ }
184
+ });
185
+
186
+ // ─── migrateToExternalState — tracked .gsd/ protection ──────────────
187
+
188
+ test("migrateToExternalState aborts when .gsd/ has tracked files (#1364)", () => {
189
+ const dir = makeTempRepo();
190
+ try {
191
+ // Create tracked .gsd/ files
192
+ mkdirSync(join(dir, ".gsd", "milestones"), { recursive: true });
193
+ writeFileSync(join(dir, ".gsd", "PROJECT.md"), "# Project\n");
194
+ git(dir, "add", ".gsd/");
195
+ git(dir, "commit", "-m", "track gsd state");
196
+
197
+ // Attempt migration — should abort without moving anything
198
+ const result = migrateToExternalState(dir);
199
+
200
+ assert.equal(result.migrated, false, "Should NOT migrate tracked .gsd/");
201
+ assert.equal(result.error, undefined, "Should not report an error — just skip");
202
+
203
+ // .gsd/ should still be a real directory, not a symlink
204
+ assert.ok(existsSync(join(dir, ".gsd", "PROJECT.md")), ".gsd/PROJECT.md should still exist");
205
+
206
+ // No .gsd.migrating should exist
207
+ assert.ok(
208
+ !existsSync(join(dir, ".gsd.migrating")),
209
+ ".gsd.migrating should not exist",
210
+ );
211
+ } finally {
212
+ cleanup(dir);
213
+ }
214
+ });