gsd-pi 2.35.0-dev.cd3b7ea → 2.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (164) 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 +17 -3
  15. package/dist/resources/extensions/gsd/auto-model-selection.js +15 -3
  16. package/dist/resources/extensions/gsd/auto-recovery.js +35 -0
  17. package/dist/resources/extensions/gsd/auto-start.js +35 -2
  18. package/dist/resources/extensions/gsd/auto.js +59 -4
  19. package/dist/resources/extensions/gsd/commands-handlers.js +2 -2
  20. package/dist/resources/extensions/gsd/commands-inspect.js +10 -3
  21. package/dist/resources/extensions/gsd/commands-rate.js +31 -0
  22. package/dist/resources/extensions/gsd/commands.js +43 -1
  23. package/dist/resources/extensions/gsd/doctor-environment.js +26 -17
  24. package/dist/resources/extensions/gsd/files.js +11 -2
  25. package/dist/resources/extensions/gsd/gitignore.js +54 -7
  26. package/dist/resources/extensions/gsd/guided-flow.js +8 -2
  27. package/dist/resources/extensions/gsd/health-widget-core.js +96 -0
  28. package/dist/resources/extensions/gsd/health-widget.js +97 -46
  29. package/dist/resources/extensions/gsd/index.js +26 -33
  30. package/dist/resources/extensions/gsd/migrate-external.js +55 -2
  31. package/dist/resources/extensions/gsd/milestone-ids.js +3 -2
  32. package/dist/resources/extensions/gsd/paths.js +74 -7
  33. package/dist/resources/extensions/gsd/post-unit-hooks.js +4 -1
  34. package/dist/resources/extensions/gsd/preferences-validation.js +16 -1
  35. package/dist/resources/extensions/gsd/preferences.js +12 -0
  36. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  37. package/dist/resources/extensions/gsd/prompts/validate-milestone.md +2 -0
  38. package/dist/resources/extensions/gsd/roadmap-mutations.js +55 -0
  39. package/dist/resources/extensions/gsd/session-lock.js +53 -2
  40. package/dist/resources/extensions/gsd/state.js +2 -1
  41. package/dist/resources/extensions/gsd/templates/plan.md +8 -0
  42. package/dist/resources/extensions/gsd/worktree-resolver.js +12 -0
  43. package/dist/resources/extensions/remote-questions/remote-command.js +2 -22
  44. package/dist/resources/extensions/shared/mod.js +1 -1
  45. package/dist/resources/extensions/shared/sanitize.js +30 -0
  46. package/dist/resources/extensions/subagent/index.js +6 -14
  47. package/dist/resources/skills/core-web-vitals/SKILL.md +1 -1
  48. package/dist/resources/skills/create-gsd-extension/workflows/debug-extension.md +1 -1
  49. package/dist/resources/skills/github-workflows/SKILL.md +0 -2
  50. package/dist/resources/skills/web-quality-audit/SKILL.md +0 -2
  51. package/package.json +2 -1
  52. package/packages/pi-agent-core/dist/agent.d.ts +10 -2
  53. package/packages/pi-agent-core/dist/agent.d.ts.map +1 -1
  54. package/packages/pi-agent-core/dist/agent.js +19 -8
  55. package/packages/pi-agent-core/dist/agent.js.map +1 -1
  56. package/packages/pi-agent-core/src/agent.ts +31 -10
  57. package/packages/pi-ai/dist/providers/openai-responses.js +1 -1
  58. package/packages/pi-ai/dist/providers/openai-responses.js.map +1 -1
  59. package/packages/pi-ai/src/providers/openai-responses.ts +1 -1
  60. package/packages/pi-coding-agent/dist/core/agent-session.d.ts.map +1 -1
  61. package/packages/pi-coding-agent/dist/core/agent-session.js +20 -4
  62. package/packages/pi-coding-agent/dist/core/agent-session.js.map +1 -1
  63. package/packages/pi-coding-agent/dist/core/resource-loader.d.ts.map +1 -1
  64. package/packages/pi-coding-agent/dist/core/resource-loader.js +13 -2
  65. package/packages/pi-coding-agent/dist/core/resource-loader.js.map +1 -1
  66. package/packages/pi-coding-agent/package.json +1 -1
  67. package/packages/pi-coding-agent/src/core/agent-session.ts +36 -12
  68. package/packages/pi-coding-agent/src/core/resource-loader.ts +13 -2
  69. package/pkg/package.json +1 -1
  70. package/src/resources/extensions/async-jobs/await-tool.ts +0 -2
  71. package/src/resources/extensions/async-jobs/job-manager.ts +0 -7
  72. package/src/resources/extensions/bg-shell/output-formatter.ts +0 -17
  73. package/src/resources/extensions/bg-shell/process-manager.ts +0 -4
  74. package/src/resources/extensions/bg-shell/types.ts +0 -12
  75. package/src/resources/extensions/context7/index.ts +7 -0
  76. package/src/resources/extensions/get-secrets-from-user.ts +2 -35
  77. package/src/resources/extensions/google-search/index.ts +7 -0
  78. package/src/resources/extensions/gsd/auto-dispatch.ts +49 -1
  79. package/src/resources/extensions/gsd/auto-loop.ts +22 -2
  80. package/src/resources/extensions/gsd/auto-model-selection.ts +23 -2
  81. package/src/resources/extensions/gsd/auto-recovery.ts +39 -0
  82. package/src/resources/extensions/gsd/auto-start.ts +42 -2
  83. package/src/resources/extensions/gsd/auto.ts +61 -3
  84. package/src/resources/extensions/gsd/commands-handlers.ts +2 -2
  85. package/src/resources/extensions/gsd/commands-inspect.ts +10 -3
  86. package/src/resources/extensions/gsd/commands-rate.ts +55 -0
  87. package/src/resources/extensions/gsd/commands.ts +43 -1
  88. package/src/resources/extensions/gsd/doctor-environment.ts +26 -16
  89. package/src/resources/extensions/gsd/files.ts +12 -2
  90. package/src/resources/extensions/gsd/gitignore.ts +54 -7
  91. package/src/resources/extensions/gsd/guided-flow.ts +8 -2
  92. package/src/resources/extensions/gsd/health-widget-core.ts +129 -0
  93. package/src/resources/extensions/gsd/health-widget.ts +103 -59
  94. package/src/resources/extensions/gsd/index.ts +30 -33
  95. package/src/resources/extensions/gsd/migrate-external.ts +47 -2
  96. package/src/resources/extensions/gsd/milestone-ids.ts +3 -2
  97. package/src/resources/extensions/gsd/paths.ts +73 -7
  98. package/src/resources/extensions/gsd/post-unit-hooks.ts +5 -1
  99. package/src/resources/extensions/gsd/preferences-validation.ts +16 -1
  100. package/src/resources/extensions/gsd/preferences.ts +14 -1
  101. package/src/resources/extensions/gsd/prompts/complete-milestone.md +2 -0
  102. package/src/resources/extensions/gsd/prompts/validate-milestone.md +2 -0
  103. package/src/resources/extensions/gsd/roadmap-mutations.ts +66 -0
  104. package/src/resources/extensions/gsd/session-lock.ts +59 -2
  105. package/src/resources/extensions/gsd/state.ts +2 -1
  106. package/src/resources/extensions/gsd/templates/plan.md +8 -0
  107. package/src/resources/extensions/gsd/tests/commands-inspect-open-db.test.ts +46 -0
  108. package/src/resources/extensions/gsd/tests/files-loadfile-eisdir.test.ts +20 -0
  109. package/src/resources/extensions/gsd/tests/gitignore-tracked-gsd.test.ts +214 -0
  110. package/src/resources/extensions/gsd/tests/health-widget.test.ts +158 -0
  111. package/src/resources/extensions/gsd/tests/paths.test.ts +113 -0
  112. package/src/resources/extensions/gsd/tests/preferences.test.ts +12 -2
  113. package/src/resources/extensions/gsd/tests/queue-reorder-e2e.test.ts +26 -0
  114. package/src/resources/extensions/gsd/tests/test-utils.ts +165 -0
  115. package/src/resources/extensions/gsd/tests/validate-directory.test.ts +15 -0
  116. package/src/resources/extensions/gsd/tests/validate-milestone.test.ts +7 -0
  117. package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +32 -0
  118. package/src/resources/extensions/gsd/worktree-resolver.ts +11 -0
  119. package/src/resources/extensions/remote-questions/remote-command.ts +2 -23
  120. package/src/resources/extensions/shared/mod.ts +1 -1
  121. package/src/resources/extensions/shared/sanitize.ts +36 -0
  122. package/src/resources/extensions/subagent/index.ts +6 -12
  123. package/src/resources/skills/core-web-vitals/SKILL.md +1 -1
  124. package/src/resources/skills/create-gsd-extension/workflows/debug-extension.md +1 -1
  125. package/src/resources/skills/github-workflows/SKILL.md +0 -2
  126. package/src/resources/skills/web-quality-audit/SKILL.md +0 -2
  127. package/dist/resources/extensions/shared/wizard-ui.js +0 -478
  128. package/dist/resources/skills/swiftui/SKILL.md +0 -208
  129. package/dist/resources/skills/swiftui/references/animations.md +0 -921
  130. package/dist/resources/skills/swiftui/references/architecture.md +0 -1561
  131. package/dist/resources/skills/swiftui/references/layout-system.md +0 -1186
  132. package/dist/resources/skills/swiftui/references/navigation.md +0 -1492
  133. package/dist/resources/skills/swiftui/references/networking-async.md +0 -214
  134. package/dist/resources/skills/swiftui/references/performance.md +0 -1706
  135. package/dist/resources/skills/swiftui/references/platform-integration.md +0 -204
  136. package/dist/resources/skills/swiftui/references/state-management.md +0 -1443
  137. package/dist/resources/skills/swiftui/references/swiftdata.md +0 -297
  138. package/dist/resources/skills/swiftui/references/testing-debugging.md +0 -247
  139. package/dist/resources/skills/swiftui/references/uikit-appkit-interop.md +0 -218
  140. package/dist/resources/skills/swiftui/workflows/add-feature.md +0 -191
  141. package/dist/resources/skills/swiftui/workflows/build-new-app.md +0 -311
  142. package/dist/resources/skills/swiftui/workflows/debug-swiftui.md +0 -192
  143. package/dist/resources/skills/swiftui/workflows/optimize-performance.md +0 -197
  144. package/dist/resources/skills/swiftui/workflows/ship-app.md +0 -203
  145. package/dist/resources/skills/swiftui/workflows/write-tests.md +0 -235
  146. package/src/resources/extensions/shared/wizard-ui.ts +0 -551
  147. package/src/resources/skills/swiftui/SKILL.md +0 -208
  148. package/src/resources/skills/swiftui/references/animations.md +0 -921
  149. package/src/resources/skills/swiftui/references/architecture.md +0 -1561
  150. package/src/resources/skills/swiftui/references/layout-system.md +0 -1186
  151. package/src/resources/skills/swiftui/references/navigation.md +0 -1492
  152. package/src/resources/skills/swiftui/references/networking-async.md +0 -214
  153. package/src/resources/skills/swiftui/references/performance.md +0 -1706
  154. package/src/resources/skills/swiftui/references/platform-integration.md +0 -204
  155. package/src/resources/skills/swiftui/references/state-management.md +0 -1443
  156. package/src/resources/skills/swiftui/references/swiftdata.md +0 -297
  157. package/src/resources/skills/swiftui/references/testing-debugging.md +0 -247
  158. package/src/resources/skills/swiftui/references/uikit-appkit-interop.md +0 -218
  159. package/src/resources/skills/swiftui/workflows/add-feature.md +0 -191
  160. package/src/resources/skills/swiftui/workflows/build-new-app.md +0 -311
  161. package/src/resources/skills/swiftui/workflows/debug-swiftui.md +0 -192
  162. package/src/resources/skills/swiftui/workflows/optimize-performance.md +0 -197
  163. package/src/resources/skills/swiftui/workflows/ship-app.md +0 -203
  164. package/src/resources/skills/swiftui/workflows/write-tests.md +0 -235
@@ -66,32 +66,24 @@ import { toPosixPath } from "../shared/mod.js";
66
66
  import { isParallelActive, shutdownParallel } from "./parallel-orchestrator.js";
67
67
  import { DEFAULT_BASH_TIMEOUT_SECS } from "./constants.js";
68
68
 
69
- // ── Agent Instructions ────────────────────────────────────────────────────
70
- // Lightweight "always follow" files injected into every GSD agent session.
71
- // Global: ~/.gsd/agent-instructions.md Project: .gsd/agent-instructions.md
72
- // Both are loaded and concatenated (global first, project appends).
73
-
74
- function loadAgentInstructions(): string | null {
75
- const parts: string[] = [];
76
-
77
- const globalPath = join(homedir(), ".gsd", "agent-instructions.md");
78
- if (existsSync(globalPath)) {
79
- try {
80
- const content = readFileSync(globalPath, "utf-8").trim();
81
- if (content) parts.push(content);
82
- } catch { /* non-fatal skip unreadable file */ }
83
- }
84
-
85
- const projectPath = join(process.cwd(), ".gsd", "agent-instructions.md");
86
- if (existsSync(projectPath)) {
87
- try {
88
- const content = readFileSync(projectPath, "utf-8").trim();
89
- if (content) parts.push(content);
90
- } catch { /* non-fatal — skip unreadable file */ }
69
+ // ── Agent Instructions (DEPRECATED) ──────────────────────────────────────
70
+ // agent-instructions.md is deprecated. Use AGENTS.md or CLAUDE.md instead.
71
+ // Pi core natively supports AGENTS.md (with CLAUDE.md fallback) per directory.
72
+
73
+ function warnDeprecatedAgentInstructions(): void {
74
+ const paths = [
75
+ join(homedir(), ".gsd", "agent-instructions.md"),
76
+ join(process.cwd(), ".gsd", "agent-instructions.md"),
77
+ ];
78
+ for (const p of paths) {
79
+ if (existsSync(p)) {
80
+ console.warn(
81
+ `[GSD] DEPRECATED: ${p} is no longer loaded. ` +
82
+ `Migrate your instructions to AGENTS.md (or CLAUDE.md) in the same directory. ` +
83
+ `See https://github.com/gsd-build/GSD-2/issues/1492`,
84
+ );
85
+ }
91
86
  }
92
-
93
- if (parts.length === 0) return null;
94
- return parts.join("\n\n");
95
87
  }
96
88
 
97
89
  // ── Depth verification state ──────────────────────────────────────────────
@@ -175,7 +167,16 @@ export default function (pi: ExtensionAPI) {
175
167
  // Pipe closed — nothing we can write; just exit cleanly
176
168
  process.exit(0);
177
169
  }
178
- // Re-throw anything that isn't EPIPE so real crashes still surface
170
+ if ((err as NodeJS.ErrnoException).code === "ENOENT" &&
171
+ (err as any).syscall?.startsWith("spawn")) {
172
+ // spawn ENOENT — command not found (e.g., npx on Windows).
173
+ // This surfaces as an uncaught exception from child_process but
174
+ // is not a fatal process error. Log and continue instead of
175
+ // crashing auto-mode (#1384).
176
+ process.stderr.write(`[gsd] spawn ENOENT: ${(err as any).path ?? "unknown"} — command not found\n`);
177
+ return;
178
+ }
179
+ // Re-throw anything that isn't EPIPE/ENOENT so real crashes still surface
179
180
  throw err;
180
181
  };
181
182
  process.on("uncaughtException", _gsdEpipeGuard);
@@ -673,12 +674,8 @@ export default function (pi: ExtensionAPI) {
673
674
  }
674
675
  }
675
676
 
676
- // Load agent instructions (global + project)
677
- let agentInstructionsBlock = "";
678
- const agentInstructions = loadAgentInstructions();
679
- if (agentInstructions) {
680
- agentInstructionsBlock = `\n\n## Agent Instructions\n\nThe following instructions were provided by the user and must be followed in every session:\n\n${agentInstructions}`;
681
- }
677
+ // Warn if deprecated agent-instructions.md files are still present
678
+ warnDeprecatedAgentInstructions();
682
679
 
683
680
  const injection = await buildGuidedExecuteContextInjection(event.prompt, process.cwd());
684
681
 
@@ -723,7 +720,7 @@ export default function (pi: ExtensionAPI) {
723
720
  ].join("\n");
724
721
  }
725
722
 
726
- const fullSystem = `${event.systemPrompt}\n\n[SYSTEM CONTEXT — GSD]\n\n${systemContent}${preferenceBlock}${agentInstructionsBlock}${knowledgeBlock}${memoryBlock}${newSkillsBlock}${worktreeBlock}`;
723
+ const fullSystem = `${event.systemPrompt}\n\n[SYSTEM CONTEXT — GSD]\n\n${systemContent}${preferenceBlock}${knowledgeBlock}${memoryBlock}${newSkillsBlock}${worktreeBlock}`;
727
724
  stopContextTimer({
728
725
  systemPromptSize: fullSystem.length,
729
726
  injectionSize: injection?.length ?? 0,
@@ -6,10 +6,11 @@
6
6
  * symlink replaces the original directory so all paths remain valid.
7
7
  */
8
8
 
9
- import { existsSync, lstatSync, mkdirSync, readdirSync, renameSync, cpSync, rmSync, symlinkSync } from "node:fs";
9
+ import { existsSync, lstatSync, mkdirSync, readdirSync, realpathSync, renameSync, cpSync, rmSync, symlinkSync } from "node:fs";
10
10
  import { join } from "node:path";
11
11
  import { externalGsdRoot } from "./repo-identity.js";
12
12
  import { getErrorMessage } from "./error-utils.js";
13
+ import { hasGitTrackedGsdFiles } from "./gitignore.js";
13
14
 
14
15
  export interface MigrationResult {
15
16
  migrated: boolean;
@@ -51,6 +52,28 @@ export function migrateToExternalState(basePath: string): MigrationResult {
51
52
  return { migrated: false, error: `Cannot stat .gsd: ${getErrorMessage(err)}` };
52
53
  }
53
54
 
55
+ // Skip if .gsd/ contains git-tracked files — the project intentionally
56
+ // keeps .gsd/ in version control and migration would destroy that.
57
+ if (hasGitTrackedGsdFiles(basePath)) {
58
+ return { migrated: false };
59
+ }
60
+
61
+ // Skip if .gsd/worktrees/ has active worktree directories (#1337).
62
+ // On Windows, active git worktrees hold OS-level directory handles that
63
+ // prevent rename/delete. Attempting migration causes EBUSY and data loss.
64
+ const worktreesDir = join(localGsd, "worktrees");
65
+ if (existsSync(worktreesDir)) {
66
+ try {
67
+ const entries = readdirSync(worktreesDir, { withFileTypes: true });
68
+ if (entries.some(e => e.isDirectory())) {
69
+ return { migrated: false };
70
+ }
71
+ } catch {
72
+ // Can't read worktrees dir — skip migration to be safe
73
+ return { migrated: false };
74
+ }
75
+ }
76
+
54
77
  const externalPath = externalGsdRoot(basePath);
55
78
  const migratingPath = join(basePath, ".gsd.migrating");
56
79
 
@@ -99,7 +122,29 @@ export function migrateToExternalState(basePath: string): MigrationResult {
99
122
  // Create symlink .gsd -> external path
100
123
  symlinkSync(externalPath, localGsd, "junction");
101
124
 
102
- // Remove .gsd.migrating
125
+ // Verify the symlink resolves correctly before removing the backup (#1377).
126
+ // On Windows, junction creation can silently succeed but resolve to the wrong
127
+ // target, or the external dir may not be accessible. If verification fails,
128
+ // restore from the backup.
129
+ try {
130
+ const resolved = realpathSync(localGsd);
131
+ const resolvedExternal = realpathSync(externalPath);
132
+ if (resolved !== resolvedExternal) {
133
+ // Symlink points to wrong target — restore backup
134
+ try { rmSync(localGsd, { force: true }); } catch { /* may not exist */ }
135
+ renameSync(migratingPath, localGsd);
136
+ return { migrated: false, error: `Migration verification failed: symlink resolves to ${resolved}, expected ${resolvedExternal}` };
137
+ }
138
+ // Verify we can read through the symlink
139
+ readdirSync(localGsd);
140
+ } catch (verifyErr) {
141
+ // Symlink broken or unreadable — restore backup
142
+ try { rmSync(localGsd, { force: true }); } catch { /* may not exist */ }
143
+ try { renameSync(migratingPath, localGsd); } catch { /* best-effort restore */ }
144
+ return { migrated: false, error: `Migration verification failed: ${getErrorMessage(verifyErr)}` };
145
+ }
146
+
147
+ // Remove .gsd.migrating only after symlink is verified
103
148
  rmSync(migratingPath, { recursive: true, force: true });
104
149
 
105
150
  return { migrated: true };
@@ -80,8 +80,9 @@ export function findMilestoneIds(basePath: string): string[] {
80
80
  .filter((d) => d.isDirectory())
81
81
  .map((d) => {
82
82
  const match = d.name.match(/^(M\d+(?:-[a-z0-9]{6})?)/);
83
- return match ? match[1] : d.name;
84
- });
83
+ return match ? match[1] : null;
84
+ })
85
+ .filter((id): id is string => id !== null);
85
86
 
86
87
  // Apply custom queue order if available, else fall back to numeric sort
87
88
  const customOrder = loadQueueOrder(basePath);
@@ -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,46 @@
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 { handleInspect } from "../commands-inspect.ts";
8
+ import { closeDatabase, openDatabase } from "../gsd-db.ts";
9
+
10
+ test("/gsd inspect opens existing database when it was not yet opened in session", async () => {
11
+ closeDatabase();
12
+
13
+ const tmp = fs.mkdtempSync(path.join(os.tmpdir(), "gsd-inspect-db-"));
14
+ const prevCwd = process.cwd();
15
+
16
+ try {
17
+ const gsdDir = path.join(tmp, ".gsd");
18
+ fs.mkdirSync(gsdDir, { recursive: true });
19
+ const dbPath = path.join(gsdDir, "gsd.db");
20
+
21
+ assert.equal(openDatabase(dbPath), true);
22
+ closeDatabase();
23
+
24
+ process.chdir(tmp);
25
+
26
+ const notifications: Array<{ message: string; level: string }> = [];
27
+ const ctx = {
28
+ ui: {
29
+ notify(message: string, level: string) {
30
+ notifications.push({ message, level });
31
+ },
32
+ },
33
+ } as any;
34
+
35
+ await handleInspect(ctx);
36
+
37
+ assert.equal(notifications.length, 1);
38
+ assert.equal(notifications[0].level, "info");
39
+ assert.match(notifications[0].message, /=== GSD Database Inspect ===/);
40
+ assert.doesNotMatch(notifications[0].message, /No GSD database available/);
41
+ } finally {
42
+ process.chdir(prevCwd);
43
+ closeDatabase();
44
+ fs.rmSync(tmp, { recursive: true, force: true });
45
+ }
46
+ });
@@ -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
+ });