gsd-pi 2.29.0-dev.2ccf3fb → 2.29.0-dev.4c155ee

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 (116) hide show
  1. package/dist/headless.js +4 -0
  2. package/dist/resources/extensions/gsd/auto-dashboard.ts +31 -0
  3. package/dist/resources/extensions/gsd/auto-dispatch.ts +32 -3
  4. package/dist/resources/extensions/gsd/auto-post-unit.ts +39 -10
  5. package/dist/resources/extensions/gsd/auto-prompts.ts +40 -17
  6. package/dist/resources/extensions/gsd/auto-recovery.ts +2 -1
  7. package/dist/resources/extensions/gsd/auto-start.ts +18 -32
  8. package/dist/resources/extensions/gsd/auto-worktree.ts +21 -182
  9. package/dist/resources/extensions/gsd/auto.ts +2 -9
  10. package/dist/resources/extensions/gsd/captures.ts +4 -10
  11. package/dist/resources/extensions/gsd/commands-handlers.ts +2 -1
  12. package/dist/resources/extensions/gsd/commands.ts +2 -1
  13. package/dist/resources/extensions/gsd/detection.ts +2 -1
  14. package/dist/resources/extensions/gsd/doctor-checks.ts +49 -1
  15. package/dist/resources/extensions/gsd/doctor-types.ts +3 -1
  16. package/dist/resources/extensions/gsd/forensics.ts +2 -2
  17. package/dist/resources/extensions/gsd/git-service.ts +3 -2
  18. package/dist/resources/extensions/gsd/gitignore.ts +9 -63
  19. package/dist/resources/extensions/gsd/gsd-db.ts +1 -165
  20. package/dist/resources/extensions/gsd/guided-flow.ts +8 -5
  21. package/dist/resources/extensions/gsd/index.ts +3 -3
  22. package/dist/resources/extensions/gsd/md-importer.ts +3 -2
  23. package/dist/resources/extensions/gsd/mechanical-completion.ts +430 -0
  24. package/dist/resources/extensions/gsd/migrate/command.ts +3 -2
  25. package/dist/resources/extensions/gsd/migrate/writer.ts +2 -1
  26. package/dist/resources/extensions/gsd/migrate-external.ts +123 -0
  27. package/dist/resources/extensions/gsd/paths.ts +24 -2
  28. package/dist/resources/extensions/gsd/post-unit-hooks.ts +6 -5
  29. package/dist/resources/extensions/gsd/preferences-models.ts +7 -1
  30. package/dist/resources/extensions/gsd/preferences-validation.ts +2 -1
  31. package/dist/resources/extensions/gsd/preferences.ts +10 -5
  32. package/dist/resources/extensions/gsd/prompts/discuss-headless.md +4 -2
  33. package/dist/resources/extensions/gsd/prompts/plan-milestone.md +26 -2
  34. package/dist/resources/extensions/gsd/prompts/plan-slice.md +15 -1
  35. package/dist/resources/extensions/gsd/repo-identity.ts +148 -0
  36. package/dist/resources/extensions/gsd/resource-version.ts +99 -0
  37. package/dist/resources/extensions/gsd/session-forensics.ts +4 -3
  38. package/dist/resources/extensions/gsd/tests/activity-log.test.ts +2 -2
  39. package/dist/resources/extensions/gsd/tests/auto-recovery.test.ts +3 -3
  40. package/dist/resources/extensions/gsd/tests/auto-worktree.test.ts +0 -58
  41. package/dist/resources/extensions/gsd/tests/doctor-runtime.test.ts +3 -4
  42. package/dist/resources/extensions/gsd/tests/feature-branch-lifecycle-integration.test.ts +5 -18
  43. package/dist/resources/extensions/gsd/tests/git-service.test.ts +10 -37
  44. package/dist/resources/extensions/gsd/tests/knowledge.test.ts +4 -4
  45. package/dist/resources/extensions/gsd/tests/mechanical-completion.test.ts +356 -0
  46. package/dist/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +1 -0
  47. package/dist/resources/extensions/gsd/tests/token-profile.test.ts +14 -16
  48. package/dist/resources/extensions/gsd/triage-resolution.ts +2 -1
  49. package/dist/resources/extensions/gsd/types.ts +2 -0
  50. package/dist/resources/extensions/gsd/worktree-command.ts +1 -11
  51. package/dist/resources/extensions/gsd/worktree-manager.ts +3 -2
  52. package/dist/resources/extensions/gsd/worktree.ts +42 -5
  53. package/dist/resources/skills/react-best-practices/SKILL.md +1 -1
  54. package/package.json +1 -1
  55. package/packages/pi-coding-agent/dist/core/lsp/client.d.ts.map +1 -1
  56. package/packages/pi-coding-agent/dist/core/lsp/client.js +3 -0
  57. package/packages/pi-coding-agent/dist/core/lsp/client.js.map +1 -1
  58. package/packages/pi-coding-agent/src/core/lsp/client.ts +3 -0
  59. package/src/resources/extensions/gsd/auto-dashboard.ts +31 -0
  60. package/src/resources/extensions/gsd/auto-dispatch.ts +32 -3
  61. package/src/resources/extensions/gsd/auto-post-unit.ts +39 -10
  62. package/src/resources/extensions/gsd/auto-prompts.ts +40 -17
  63. package/src/resources/extensions/gsd/auto-recovery.ts +2 -1
  64. package/src/resources/extensions/gsd/auto-start.ts +18 -32
  65. package/src/resources/extensions/gsd/auto-worktree.ts +21 -182
  66. package/src/resources/extensions/gsd/auto.ts +2 -9
  67. package/src/resources/extensions/gsd/captures.ts +4 -10
  68. package/src/resources/extensions/gsd/commands-handlers.ts +2 -1
  69. package/src/resources/extensions/gsd/commands.ts +2 -1
  70. package/src/resources/extensions/gsd/detection.ts +2 -1
  71. package/src/resources/extensions/gsd/doctor-checks.ts +49 -1
  72. package/src/resources/extensions/gsd/doctor-types.ts +3 -1
  73. package/src/resources/extensions/gsd/forensics.ts +2 -2
  74. package/src/resources/extensions/gsd/git-service.ts +3 -2
  75. package/src/resources/extensions/gsd/gitignore.ts +9 -63
  76. package/src/resources/extensions/gsd/gsd-db.ts +1 -165
  77. package/src/resources/extensions/gsd/guided-flow.ts +8 -5
  78. package/src/resources/extensions/gsd/index.ts +3 -3
  79. package/src/resources/extensions/gsd/md-importer.ts +3 -2
  80. package/src/resources/extensions/gsd/mechanical-completion.ts +430 -0
  81. package/src/resources/extensions/gsd/migrate/command.ts +3 -2
  82. package/src/resources/extensions/gsd/migrate/writer.ts +2 -1
  83. package/src/resources/extensions/gsd/migrate-external.ts +123 -0
  84. package/src/resources/extensions/gsd/paths.ts +24 -2
  85. package/src/resources/extensions/gsd/post-unit-hooks.ts +6 -5
  86. package/src/resources/extensions/gsd/preferences-models.ts +7 -1
  87. package/src/resources/extensions/gsd/preferences-validation.ts +2 -1
  88. package/src/resources/extensions/gsd/preferences.ts +10 -5
  89. package/src/resources/extensions/gsd/prompts/discuss-headless.md +4 -2
  90. package/src/resources/extensions/gsd/prompts/plan-milestone.md +26 -2
  91. package/src/resources/extensions/gsd/prompts/plan-slice.md +15 -1
  92. package/src/resources/extensions/gsd/repo-identity.ts +148 -0
  93. package/src/resources/extensions/gsd/resource-version.ts +99 -0
  94. package/src/resources/extensions/gsd/session-forensics.ts +4 -3
  95. package/src/resources/extensions/gsd/tests/activity-log.test.ts +2 -2
  96. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +3 -3
  97. package/src/resources/extensions/gsd/tests/auto-worktree.test.ts +0 -58
  98. package/src/resources/extensions/gsd/tests/doctor-runtime.test.ts +3 -4
  99. package/src/resources/extensions/gsd/tests/feature-branch-lifecycle-integration.test.ts +5 -18
  100. package/src/resources/extensions/gsd/tests/git-service.test.ts +10 -37
  101. package/src/resources/extensions/gsd/tests/knowledge.test.ts +4 -4
  102. package/src/resources/extensions/gsd/tests/mechanical-completion.test.ts +356 -0
  103. package/src/resources/extensions/gsd/tests/plan-slice-prompt.test.ts +1 -0
  104. package/src/resources/extensions/gsd/tests/token-profile.test.ts +14 -16
  105. package/src/resources/extensions/gsd/triage-resolution.ts +2 -1
  106. package/src/resources/extensions/gsd/types.ts +2 -0
  107. package/src/resources/extensions/gsd/worktree-command.ts +1 -11
  108. package/src/resources/extensions/gsd/worktree-manager.ts +3 -2
  109. package/src/resources/extensions/gsd/worktree.ts +42 -5
  110. package/src/resources/skills/react-best-practices/SKILL.md +1 -1
  111. package/dist/resources/extensions/gsd/auto-worktree-sync.ts +0 -199
  112. package/dist/resources/extensions/gsd/tests/worktree-db-integration.test.ts +0 -205
  113. package/dist/resources/extensions/gsd/tests/worktree-db.test.ts +0 -442
  114. package/src/resources/extensions/gsd/auto-worktree-sync.ts +0 -199
  115. package/src/resources/extensions/gsd/tests/worktree-db-integration.test.ts +0 -205
  116. package/src/resources/extensions/gsd/tests/worktree-db.test.ts +0 -442
@@ -6,25 +6,24 @@
6
6
  * manages create, enter, detect, and teardown for auto-mode worktrees.
7
7
  */
8
8
 
9
- import { existsSync, cpSync, readFileSync, readdirSync, mkdirSync, realpathSync, unlinkSync, statSync } from "node:fs";
9
+ import { existsSync, readFileSync, realpathSync, unlinkSync, statSync } from "node:fs";
10
10
  import { isAbsolute, join, sep } from "node:path";
11
11
  import { GSDError, GSD_IO_ERROR, GSD_GIT_ERROR } from "./errors.js";
12
- import { copyWorktreeDb, reconcileWorktreeDb, isDbAvailable } from "./gsd-db.js";
13
- import { atomicWriteSync } from "./atomic-write.js";
14
12
  import { execSync, execFileSync } from "node:child_process";
15
- import { safeCopy, safeCopyRecursive } from "./safe-fs.js";
16
13
  import {
17
14
  createWorktree,
18
15
  removeWorktree,
19
16
  worktreePath,
20
17
  } from "./worktree-manager.js";
21
18
  import { detectWorktreeName, resolveGitHeadPath, nudgeGitBranchCache } from "./worktree.js";
19
+ import { ensureGsdSymlink } from "./repo-identity.js";
22
20
  import {
23
21
  MergeConflictError,
24
22
  readIntegrationBranch,
25
23
  } from "./git-service.js";
26
24
  import { parseRoadmap } from "./files.js";
27
25
  import { loadEffectiveGSDPreferences } from "./preferences.js";
26
+ import { gsdRoot } from "./paths.js";
28
27
  import {
29
28
  nativeGetCurrentBranch,
30
29
  nativeWorkingTreeStatus,
@@ -103,111 +102,6 @@ export function autoWorktreeBranch(milestoneId: string): string {
103
102
  * to prevent split-brain.
104
103
  */
105
104
 
106
- /**
107
- * Forward-merge plan checkbox state from the project root into a freshly
108
- * re-attached worktree (#778).
109
- *
110
- * When auto-mode stops via crash (not graceful stop), the milestone branch
111
- * HEAD may be behind the filesystem state at the project root because
112
- * syncStateToProjectRoot() runs after every task completion but the final
113
- * git commit may not have happened before the crash. On restart the worktree
114
- * is re-attached to the branch HEAD, which has [ ] for the crashed task,
115
- * causing verifyExpectedArtifact() to fail and triggering an infinite
116
- * dispatch/skip loop.
117
- *
118
- * Fix: after re-attaching, read every *.md plan file in the milestone
119
- * directory at the project root and apply any [x] checkbox states that are
120
- * ahead of the worktree version (forward-only: never downgrade [x] → [ ]).
121
- *
122
- * This is safe because syncStateToProjectRoot() is the authoritative source
123
- * of post-task state at the project root — it writes the same [x] the LLM
124
- * produced, then the auto-commit follows. If the commit never happened, the
125
- * filesystem copy is still valid and correct.
126
- */
127
- function reconcilePlanCheckboxes(projectRoot: string, wtPath: string, milestoneId: string): void {
128
- const srcMilestone = join(projectRoot, ".gsd", "milestones", milestoneId);
129
- const dstMilestone = join(wtPath, ".gsd", "milestones", milestoneId);
130
- if (!existsSync(srcMilestone) || !existsSync(dstMilestone)) return;
131
-
132
- // Walk all markdown files in the milestone directory (plans, summaries, etc.)
133
- function walkMd(dir: string): string[] {
134
- const results: string[] = [];
135
- try {
136
- for (const entry of readdirSync(dir, { withFileTypes: true })) {
137
- const full = join(dir, entry.name);
138
- if (entry.isDirectory()) {
139
- results.push(...walkMd(full));
140
- } else if (entry.isFile() && entry.name.endsWith(".md")) {
141
- results.push(full);
142
- }
143
- }
144
- } catch { /* non-fatal */ }
145
- return results;
146
- }
147
-
148
- for (const srcFile of walkMd(srcMilestone)) {
149
- const rel = srcFile.slice(srcMilestone.length);
150
- const dstFile = dstMilestone + rel;
151
- if (!existsSync(dstFile)) continue; // only reconcile existing files
152
-
153
- let srcContent: string;
154
- let dstContent: string;
155
- try {
156
- srcContent = readFileSync(srcFile, "utf-8");
157
- dstContent = readFileSync(dstFile, "utf-8");
158
- } catch { continue; }
159
-
160
- if (srcContent === dstContent) continue;
161
-
162
- // Extract all checked task IDs from the source (project root)
163
- // Pattern: - [x] **T<id>: or - [x] **S<id>: (case-insensitive x)
164
- const checkedRe = /^- \[[xX]\] \*\*([TS]\d+):/gm;
165
- const srcChecked = new Set<string>();
166
- for (const m of srcContent.matchAll(checkedRe)) srcChecked.add(m[1]);
167
-
168
- if (srcChecked.size === 0) continue;
169
-
170
- // Forward-apply: replace [ ] → [x] for any IDs that are checked in src
171
- let updated = dstContent;
172
- let changed = false;
173
- for (const id of srcChecked) {
174
- const escapedId = id.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
175
- const uncheckedRe = new RegExp(`^(- )\\[ \\]( \\*\\*${escapedId}:)`, "gm");
176
- if (uncheckedRe.test(updated)) {
177
- updated = updated.replace(
178
- new RegExp(`^(- )\\[ \\]( \\*\\*${escapedId}:)`, "gm"),
179
- "$1[x]$2",
180
- );
181
- changed = true;
182
- }
183
- }
184
-
185
- if (changed) {
186
- try {
187
- atomicWriteSync(dstFile, updated, "utf-8");
188
- } catch { /* non-fatal */ }
189
- }
190
- }
191
-
192
- // Also forward-merge completed-units.json (set-union)
193
- const srcKeys = join(projectRoot, ".gsd", "completed-units.json");
194
- const dstKeys = join(wtPath, ".gsd", "completed-units.json");
195
- if (existsSync(srcKeys)) {
196
- try {
197
- const src: string[] = JSON.parse(readFileSync(srcKeys, "utf-8"));
198
- let dst: string[] = [];
199
- if (existsSync(dstKeys)) {
200
- try { dst = JSON.parse(readFileSync(dstKeys, "utf-8")); } catch { /* ignore corrupt */ }
201
- }
202
- const merged = [...new Set([...dst, ...src])];
203
- if (merged.length > dst.length) {
204
- mkdirSync(join(wtPath, ".gsd"), { recursive: true });
205
- atomicWriteSync(dstKeys, JSON.stringify(merged), "utf-8");
206
- }
207
- } catch { /* non-fatal */ }
208
- }
209
- }
210
-
211
105
  export function createAutoWorktree(basePath: string, milestoneId: string): string {
212
106
  const branch = autoWorktreeBranch(milestoneId);
213
107
 
@@ -227,32 +121,8 @@ export function createAutoWorktree(basePath: string, milestoneId: string): strin
227
121
  info = createWorktree(basePath, milestoneId, { branch, startPoint: integrationBranch });
228
122
  }
229
123
 
230
- // Copy .gsd/ planning artifacts from the source repo into the new worktree.
231
- // Worktrees are fresh git checkouts — untracked files don't carry over.
232
- // Planning artifacts may be untracked if the project's .gitignore had a
233
- // blanket .gsd/ rule (pre-v2.14.0). Without this copy, auto-mode loops
234
- // on plan-slice because the plan file doesn't exist in the worktree.
235
- //
236
- // IMPORTANT: Skip when re-attaching to an existing branch (#759).
237
- // The branch checkout already has committed artifacts with correct state
238
- // (e.g. [x] for completed slices). Copying from the project root would
239
- // overwrite them with stale data ([ ] checkboxes) because the root is
240
- // not always fully synced.
241
- if (!branchExists) {
242
- copyPlanningArtifacts(basePath, info.path);
243
- } else {
244
- // Re-attaching to an existing branch: forward-merge any plan checkpoint
245
- // state from the project root into the worktree (#778).
246
- //
247
- // If auto-mode stopped via crash, the milestone branch HEAD may lag behind
248
- // the project root filesystem because syncStateToProjectRoot() ran after
249
- // task completion but the auto-commit never fired. On restart the worktree
250
- // is re-created from the branch HEAD (which has [ ] for the crashed task),
251
- // causing verifyExpectedArtifact() to return false → stale-key eviction →
252
- // infinite dispatch/skip loop. Reconciling here ensures the worktree sees
253
- // the same [x] state that syncStateToProjectRoot() wrote to the root.
254
- reconcilePlanCheckboxes(basePath, info.path, milestoneId);
255
- }
124
+ // Ensure worktree shares external state via symlink
125
+ ensureGsdSymlink(info.path);
256
126
 
257
127
  // Run user-configured post-create hook (#597) — e.g. copy .env, symlink assets
258
128
  const hookError = runWorktreePostCreateHook(basePath, info.path);
@@ -279,36 +149,6 @@ export function createAutoWorktree(basePath: string, milestoneId: string): strin
279
149
  return info.path;
280
150
  }
281
151
 
282
- /**
283
- * Copy .gsd/ planning artifacts from source repo to a new worktree.
284
- * Copies milestones/, DECISIONS.md, REQUIREMENTS.md, PROJECT.md, QUEUE.md,
285
- * STATE.md, KNOWLEDGE.md, and OVERRIDES.md.
286
- * Skips runtime files (auto.lock, metrics.json, etc.) and the worktrees/ dir.
287
- * Best-effort — failures are non-fatal since auto-mode can recreate artifacts.
288
- */
289
- function copyPlanningArtifacts(srcBase: string, wtPath: string): void {
290
- const srcGsd = join(srcBase, ".gsd");
291
- const dstGsd = join(wtPath, ".gsd");
292
- if (!existsSync(srcGsd)) return;
293
-
294
- // Copy milestones/ directory (planning files, roadmaps, plans, research)
295
- safeCopyRecursive(join(srcGsd, "milestones"), join(dstGsd, "milestones"), { force: true });
296
-
297
- // Copy top-level planning files
298
- for (const file of ["DECISIONS.md", "REQUIREMENTS.md", "PROJECT.md", "QUEUE.md", "STATE.md", "KNOWLEDGE.md", "OVERRIDES.md"]) {
299
- safeCopy(join(srcGsd, file), join(dstGsd, file), { force: true });
300
- }
301
-
302
- // Copy gsd.db if present in source
303
- const srcDb = join(srcGsd, "gsd.db");
304
- const destDb = join(dstGsd, "gsd.db");
305
- if (existsSync(srcDb)) {
306
- try {
307
- copyWorktreeDb(srcDb, destDb);
308
- } catch { /* non-fatal */ }
309
- }
310
- }
311
-
312
152
  /**
313
153
  * Teardown an auto-worktree: chdir back to original base, then remove
314
154
  * the worktree and its branch.
@@ -346,7 +186,7 @@ export function isInAutoWorktree(basePath: string): boolean {
346
186
  // Primary check: use originalBase if available (fast path)
347
187
  if (originalBase) {
348
188
  const resolvedBase = existsSync(basePath) ? realpathSync(basePath) : basePath;
349
- const wtDir = join(resolvedBase, ".gsd", "worktrees");
189
+ const wtDir = join(gsdRoot(resolvedBase), "worktrees");
350
190
  if (!cwd.startsWith(wtDir)) return false;
351
191
  const branch = nativeGetCurrentBranch(cwd);
352
192
  return branch.startsWith("milestone/");
@@ -364,8 +204,8 @@ export function isInAutoWorktree(basePath: string): boolean {
364
204
  // Worktrees have a .git file with "gitdir: ..." pointing to the main repo
365
205
  const gitContent = readFileSync(worktreeMarker, "utf-8").trim();
366
206
  if (!gitContent.startsWith("gitdir:")) return false;
367
- // Verify cwd path contains .gsd/worktrees/
368
- if (!cwd.includes(`${sep}.gsd${sep}worktrees${sep}`) && !cwd.includes("/.gsd/worktrees/")) return false;
207
+ // Verify we're inside a GSD-managed worktree
208
+ if (!detectWorktreeName(cwd)) return false;
369
209
  const branch = nativeGetCurrentBranch(cwd);
370
210
  return branch.startsWith("milestone/");
371
211
  } catch {
@@ -458,7 +298,7 @@ export function getActiveAutoWorktreeContext(): {
458
298
  if (!originalBase) return null;
459
299
  const cwd = process.cwd();
460
300
  const resolvedBase = existsSync(originalBase) ? realpathSync(originalBase) : originalBase;
461
- const wtDir = join(resolvedBase, ".gsd", "worktrees");
301
+ const wtDir = join(gsdRoot(resolvedBase), "worktrees");
462
302
  if (!cwd.startsWith(wtDir)) return null;
463
303
  const worktreeName = detectWorktreeName(cwd);
464
304
  if (!worktreeName) return null;
@@ -518,15 +358,6 @@ export function mergeMilestoneToMain(
518
358
  // 1. Auto-commit dirty state in worktree before leaving
519
359
  autoCommitDirtyState(worktreeCwd);
520
360
 
521
- // Reconcile worktree DB into main DB before leaving worktree context
522
- if (isDbAvailable()) {
523
- try {
524
- const worktreeDbPath = join(worktreeCwd, ".gsd", "gsd.db");
525
- const mainDbPath = join(originalBasePath_, ".gsd", "gsd.db");
526
- reconcileWorktreeDb(mainDbPath, worktreeDbPath);
527
- } catch { /* non-fatal */ }
528
- }
529
-
530
361
  // 2. Parse roadmap for slice listing
531
362
  const roadmap = parseRoadmap(roadmapContent);
532
363
  const completedSlices = roadmap.slices.filter(s => s.done);
@@ -535,11 +366,19 @@ export function mergeMilestoneToMain(
535
366
  const previousCwd = process.cwd();
536
367
  process.chdir(originalBasePath_);
537
368
 
538
- // 3a. Auto-commit any dirty state in the project root that syncStateToProjectRoot
539
- // wrote during execution. Without this, the squash merge can fail with
540
- // "Your local changes to the following files would be overwritten by merge" (#1127).
369
+ // 3a. Auto-commit any dirty state in the project root. Without this, the
370
+ // squash merge can fail with "Your local changes would be overwritten" (#1127).
541
371
  autoCommitDirtyState(originalBasePath_);
542
372
 
373
+ // 3b. Remove untracked .gsd/ files that syncStateToProjectRoot copied.
374
+ // autoCommitDirtyState stages and commits everything (git add -A), but if
375
+ // the project root branch has no .gsd/ tracking (e.g., .gsd/ is gitignored),
376
+ // these files remain untracked and cause "untracked working tree files would
377
+ // be overwritten by merge" during squash-merge (#1237).
378
+ try {
379
+ execFileSync("git", ["clean", "-fd", ".gsd/"], { cwd: originalBasePath_, stdio: "pipe" });
380
+ } catch { /* non-fatal — clean failure shouldn't block merge attempt */ }
381
+
543
382
  // 4. Resolve integration branch — prefer milestone metadata, fall back to preferences / "main"
544
383
  const prefs = loadEffectiveGSDPreferences()?.preferences?.git ?? {};
545
384
  const integrationBranch = readIntegrationBranch(originalBasePath_, milestoneId);
@@ -556,7 +395,7 @@ export function mergeMilestoneToMain(
556
395
  // "Your local changes would be overwritten" (#827).
557
396
  const gsdStateFiles = ["STATE.md", "completed-units.json", "auto.lock"];
558
397
  for (const f of gsdStateFiles) {
559
- const p = join(originalBasePath_, ".gsd", f);
398
+ const p = join(gsdRoot(originalBasePath_), f);
560
399
  try { unlinkSync(p); } catch { /* non-fatal — file may not exist */ }
561
400
  }
562
401
  nativeCheckoutBranch(originalBasePath_, mainBranch);
@@ -69,12 +69,10 @@ import { closeoutUnit } from "./auto-unit-closeout.js";
69
69
  import { recoverTimedOutUnit } from "./auto-timeout-recovery.js";
70
70
  import { selectAndApplyModel } from "./auto-model-selection.js";
71
71
  import {
72
- syncProjectRootToWorktree,
73
- syncStateToProjectRoot,
74
72
  readResourceVersion,
75
73
  checkResourcesStale,
76
74
  escapeStaleWorktree,
77
- } from "./auto-worktree-sync.js";
75
+ } from "./resource-version.js";
78
76
  import { initRoutingHistory, resetRoutingHistory, recordOutcome } from "./routing-history.js";
79
77
  import {
80
78
  checkPostUnitHooks,
@@ -180,7 +178,7 @@ import { runPostUnitVerification, type VerificationContext } from "./auto-verifi
180
178
  import { postUnitPreVerification, postUnitPostVerification, type PostUnitContext } from "./auto-post-unit.js";
181
179
  import { bootstrapAutoSession, type BootstrapDeps } from "./auto-start.js";
182
180
 
183
- // Worktree sync, resource staleness, stale worktree escape → auto-worktree-sync.ts
181
+ // Resource staleness, stale worktree escape → resource-version.ts
184
182
 
185
183
  // ─── Session State ─────────────────────────────────────────────────────────
186
184
 
@@ -1018,11 +1016,6 @@ async function dispatchNextUnit(
1018
1016
  // Non-fatal
1019
1017
  }
1020
1018
 
1021
- // ── Sync project root artifacts into worktree ──
1022
- if (s.originalBasePath && s.basePath !== s.originalBasePath && s.currentMilestoneId) {
1023
- syncProjectRootToWorktree(s.originalBasePath, s.basePath, s.currentMilestoneId);
1024
- }
1025
-
1026
1019
  const stopDeriveTimer = debugTime("derive-state");
1027
1020
  let state = await deriveState(s.basePath);
1028
1021
  stopDeriveTimer({
@@ -9,9 +9,10 @@
9
9
  */
10
10
 
11
11
  import { existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs";
12
- import { join, resolve, sep } from "node:path";
12
+ import { join, resolve } from "node:path";
13
13
  import { randomUUID } from "node:crypto";
14
14
  import { gsdRoot } from "./paths.js";
15
+ import { resolveProjectRoot } from "./worktree.js";
15
16
 
16
17
  // ─── Types ────────────────────────────────────────────────────────────────────
17
18
 
@@ -58,15 +59,8 @@ const VALID_CLASSIFICATIONS: readonly string[] = [
58
59
  * directory that contains `.gsd/worktrees/` — that's the project root.
59
60
  */
60
61
  export function resolveCapturesPath(basePath: string): string {
61
- const resolved = resolve(basePath);
62
- const worktreeMarker = `${sep}.gsd${sep}worktrees${sep}`;
63
- const idx = resolved.indexOf(worktreeMarker);
64
- if (idx !== -1) {
65
- // basePath is inside a worktree — resolve to project root
66
- const projectRoot = resolved.slice(0, idx);
67
- return join(projectRoot, ".gsd", CAPTURES_FILENAME);
68
- }
69
- return join(gsdRoot(basePath), CAPTURES_FILENAME);
62
+ const projectRoot = resolveProjectRoot(resolve(basePath));
63
+ return join(gsdRoot(projectRoot), CAPTURES_FILENAME);
70
64
  }
71
65
 
72
66
  // ─── File I/O ─────────────────────────────────────────────────────────────────
@@ -9,6 +9,7 @@ import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent
9
9
  import { existsSync, readFileSync, mkdirSync } from "node:fs";
10
10
  import { join } from "node:path";
11
11
  import { deriveState } from "./state.js";
12
+ import { gsdRoot } from "./paths.js";
12
13
  import { appendCapture, hasPendingCaptures, loadPendingCaptures } from "./captures.js";
13
14
  import { appendOverride, appendKnowledge } from "./files.js";
14
15
  import {
@@ -136,7 +137,7 @@ export async function handleCapture(args: string, ctx: ExtensionCommandContext):
136
137
  const basePath = process.cwd();
137
138
 
138
139
  // Ensure .gsd/ exists — capture should work even without a milestone
139
- const gsdDir = join(basePath, ".gsd");
140
+ const gsdDir = gsdRoot(basePath);
140
141
  if (!existsSync(gsdDir)) {
141
142
  mkdirSync(gsdDir, { recursive: true });
142
143
  }
@@ -8,6 +8,7 @@ import type { ExtensionAPI, ExtensionCommandContext } from "@gsd/pi-coding-agent
8
8
  import type { GSDState } from "./types.js";
9
9
  import { existsSync, readFileSync, unlinkSync } from "node:fs";
10
10
  import { join } from "node:path";
11
+ import { gsdRoot } from "./paths.js";
11
12
  import { enableDebug } from "./debug-logger.js";
12
13
  import { deriveState } from "./state.js";
13
14
  import { GSDDashboardOverlay } from "./dashboard-overlay.js";
@@ -698,7 +699,7 @@ export function registerGSDCommand(pi: ExtensionAPI): void {
698
699
 
699
700
  if (trimmed === "new-milestone") {
700
701
  const basePath = projectRoot();
701
- const headlessContextPath = join(basePath, ".gsd", "runtime", "headless-context.md");
702
+ const headlessContextPath = join(gsdRoot(basePath), "runtime", "headless-context.md");
702
703
  if (existsSync(headlessContextPath)) {
703
704
  const seedContext = readFileSync(headlessContextPath, "utf-8");
704
705
  try { unlinkSync(headlessContextPath); } catch { /* non-fatal */ }
@@ -9,6 +9,7 @@
9
9
  import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
10
10
  import { join } from "node:path";
11
11
  import { homedir } from "node:os";
12
+ import { gsdRoot } from "./paths.js";
12
13
 
13
14
  // ─── Types ──────────────────────────────────────────────────────────────────────
14
15
 
@@ -214,7 +215,7 @@ export function detectV1Planning(basePath: string): V1Detection | null {
214
215
  // ─── V2 GSD Detection ──────────────────────────────────────────────────────────
215
216
 
216
217
  function detectV2Gsd(basePath: string): V2Detection | null {
217
- const gsdPath = join(basePath, ".gsd");
218
+ const gsdPath = gsdRoot(basePath);
218
219
 
219
220
  if (!existsSync(gsdPath)) return null;
220
221
 
@@ -1,4 +1,4 @@
1
- import { existsSync, readdirSync, readFileSync, statSync } from "node:fs";
1
+ import { existsSync, lstatSync, readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
2
2
  import { join, sep } from "node:path";
3
3
 
4
4
  import type { DoctorIssue, DoctorIssueCode } from "./doctor-types.js";
@@ -13,6 +13,7 @@ import { nativeIsRepo, nativeWorktreeRemove, nativeBranchList, nativeBranchDelet
13
13
  import { readCrashLock, isLockProcessAlive, clearLock } from "./crash-recovery.js";
14
14
  import { ensureGitignore } from "./gitignore.js";
15
15
  import { readAllSessionStatuses, isSessionStale, removeSessionStatus } from "./session-status-io.js";
16
+ import { recoverFailedMigration } from "./migrate-external.js";
16
17
 
17
18
  export async function checkGitHealth(
18
19
  basePath: string,
@@ -508,6 +509,53 @@ export async function checkRuntimeHealth(
508
509
  } catch {
509
510
  // Non-fatal — gitignore check failed
510
511
  }
512
+
513
+ // ── External state symlink health ──────────────────────────────────────
514
+ try {
515
+ const localGsd = join(basePath, ".gsd");
516
+ if (existsSync(localGsd)) {
517
+ const stat = lstatSync(localGsd);
518
+
519
+ // Check for .gsd.migrating (failed migration)
520
+ const migratingPath = join(basePath, ".gsd.migrating");
521
+ if (existsSync(migratingPath)) {
522
+ issues.push({
523
+ severity: "error",
524
+ code: "failed_migration",
525
+ scope: "project",
526
+ unitId: "project",
527
+ message: "Found .gsd.migrating — a previous external state migration failed. State may be incomplete.",
528
+ file: ".gsd.migrating",
529
+ fixable: true,
530
+ });
531
+
532
+ if (shouldFix("failed_migration")) {
533
+ if (recoverFailedMigration(basePath)) {
534
+ fixesApplied.push("recovered failed migration (.gsd.migrating → .gsd)");
535
+ }
536
+ }
537
+ }
538
+
539
+ // Check symlink target exists
540
+ if (stat.isSymbolicLink()) {
541
+ try {
542
+ realpathSync(localGsd);
543
+ } catch {
544
+ issues.push({
545
+ severity: "error",
546
+ code: "broken_symlink",
547
+ scope: "project",
548
+ unitId: "project",
549
+ message: ".gsd symlink target does not exist. External state directory may have been deleted.",
550
+ file: ".gsd",
551
+ fixable: false,
552
+ });
553
+ }
554
+ }
555
+ }
556
+ } catch {
557
+ // Non-fatal — external state check failed
558
+ }
511
559
  }
512
560
 
513
561
  /**
@@ -30,7 +30,9 @@ export type DoctorIssueCode =
30
30
  | "state_file_stale"
31
31
  | "state_file_missing"
32
32
  | "gitignore_missing_patterns"
33
- | "unresolvable_dependency";
33
+ | "unresolvable_dependency"
34
+ | "failed_migration"
35
+ | "broken_symlink";
34
36
 
35
37
  /**
36
38
  * Issue codes that represent expected completion-transition states.
@@ -268,7 +268,7 @@ function resolveActivityDirs(basePath: string, activeMilestone?: string | null):
268
268
  if (activeMilestone) {
269
269
  const wtPath = getAutoWorktreePath(basePath, activeMilestone);
270
270
  if (wtPath) {
271
- const wtActivityDir = join(wtPath, ".gsd", "activity");
271
+ const wtActivityDir = join(gsdRoot(wtPath), "activity");
272
272
  if (existsSync(wtActivityDir)) {
273
273
  dirs.push(wtActivityDir);
274
274
  }
@@ -285,7 +285,7 @@ function resolveActivityDirs(basePath: string, activeMilestone?: string | null):
285
285
  // ─── Completed Keys Loader ────────────────────────────────────────────────────
286
286
 
287
287
  function loadCompletedKeys(basePath: string): string[] {
288
- const file = join(basePath, ".gsd", "completed-units.json");
288
+ const file = join(gsdRoot(basePath), "completed-units.json");
289
289
  try {
290
290
  if (existsSync(file)) {
291
291
  return JSON.parse(readFileSync(file, "utf-8"));
@@ -11,6 +11,7 @@
11
11
  import { execFileSync, execSync } from "node:child_process";
12
12
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
13
13
  import { join } from "node:path";
14
+ import { gsdRoot } from "./paths.js";
14
15
  import { GIT_NO_PROMPT_ENV } from "./git-constants.js";
15
16
 
16
17
  import {
@@ -193,7 +194,7 @@ export const RUNTIME_EXCLUSION_PATHS: readonly string[] = [
193
194
  * Format: .gsd/milestones/<MID>/<MID>-META.json
194
195
  */
195
196
  function milestoneMetaPath(basePath: string, milestoneId: string): string {
196
- return join(basePath, ".gsd", "milestones", milestoneId, `${milestoneId}-META.json`);
197
+ return join(gsdRoot(basePath), "milestones", milestoneId, `${milestoneId}-META.json`);
197
198
  }
198
199
 
199
200
  /**
@@ -237,7 +238,7 @@ export function writeIntegrationBranch(basePath: string, milestoneId: string, br
237
238
  if (existingBranch === branch) return;
238
239
 
239
240
  const metaFile = milestoneMetaPath(basePath, milestoneId);
240
- mkdirSync(join(basePath, ".gsd", "milestones", milestoneId), { recursive: true });
241
+ mkdirSync(join(gsdRoot(basePath), "milestones", milestoneId), { recursive: true });
241
242
 
242
243
  // Merge with existing metadata if present
243
244
  let existing: Record<string, unknown> = {};
@@ -9,10 +9,12 @@
9
9
  import { join } from "node:path";
10
10
  import { existsSync, readFileSync, writeFileSync } from "node:fs";
11
11
  import { nativeRmCached } from "./native-git-bridge.js";
12
+ import { gsdRoot } from "./paths.js";
12
13
 
13
14
  /**
14
- * Patterns that are always correct regardless of project type.
15
- * No one ever wants these tracked.
15
+ * GSD runtime patterns for git index cleanup.
16
+ * With external state (symlink), these are a no-op in most cases,
17
+ * but retained for backwards compatibility during migration.
16
18
  */
17
19
  const GSD_RUNTIME_PATTERNS = [
18
20
  ".gsd/activity/",
@@ -31,8 +33,8 @@ const GSD_RUNTIME_PATTERNS = [
31
33
  ] as const;
32
34
 
33
35
  const BASELINE_PATTERNS = [
34
- // ── GSD runtime (not source artifacts — planning files are tracked) ──
35
- ...GSD_RUNTIME_PATTERNS,
36
+ // ── GSD state directory (symlink to external storage) ──
37
+ ".gsd",
36
38
 
37
39
  // ── OS junk ──
38
40
  ".DS_Store",
@@ -90,41 +92,12 @@ export function ensureGitignore(basePath: string, options?: { commitDocs?: boole
90
92
  if (options?.manageGitignore === false) return false;
91
93
 
92
94
  const gitignorePath = join(basePath, ".gitignore");
93
- const commitDocs = options?.commitDocs !== false; // default true
94
95
 
95
96
  let existing = "";
96
97
  if (existsSync(gitignorePath)) {
97
98
  existing = readFileSync(gitignorePath, "utf-8");
98
99
  }
99
100
 
100
- // When commit_docs is false, ensure blanket ".gsd/" is in .gitignore
101
- // and skip the self-heal that would remove it.
102
- if (!commitDocs) {
103
- return ensureBlanketGsdIgnore(gitignorePath, existing);
104
- }
105
-
106
- // Self-heal: remove blanket ".gsd/" lines from pre-v2.14.0 projects.
107
- // The blanket ignore prevented planning artifacts (.gsd/milestones/) from
108
- // being tracked in git, causing artifacts to vanish in worktrees and
109
- // triggering loop detection failures. Replace with explicit runtime-only
110
- // ignores so planning files are tracked naturally.
111
- let modified = false;
112
- const lines = existing.split("\n");
113
- const filteredLines = lines.filter(line => {
114
- const trimmed = line.trim();
115
- // Remove standalone ".gsd/" lines (blanket ignore) but keep specific
116
- // .gsd/ subpath patterns like ".gsd/activity/" or ".gsd/auto.lock"
117
- if (trimmed === ".gsd/" || trimmed === ".gsd") {
118
- modified = true;
119
- return false;
120
- }
121
- return true;
122
- });
123
- if (modified) {
124
- existing = filteredLines.join("\n");
125
- writeFileSync(gitignorePath, existing, "utf-8");
126
- }
127
-
128
101
  // Parse existing lines (trimmed, ignoring comments and blanks)
129
102
  const existingLines = new Set(
130
103
  existing
@@ -136,7 +109,7 @@ export function ensureGitignore(basePath: string, options?: { commitDocs?: boole
136
109
  // Find patterns not yet present
137
110
  const missing = BASELINE_PATTERNS.filter((p) => !existingLines.has(p));
138
111
 
139
- if (missing.length === 0) return modified;
112
+ if (missing.length === 0) return false;
140
113
 
141
114
  // Build the block to append
142
115
  const block = [
@@ -184,8 +157,8 @@ export function untrackRuntimeFiles(basePath: string): void {
184
157
  * creating a duplicate when an uppercase file already exists.
185
158
  */
186
159
  export function ensurePreferences(basePath: string): boolean {
187
- const preferencesPath = join(basePath, ".gsd", "preferences.md");
188
- const legacyPath = join(basePath, ".gsd", "PREFERENCES.md");
160
+ const preferencesPath = join(gsdRoot(basePath), "preferences.md");
161
+ const legacyPath = join(gsdRoot(basePath), "PREFERENCES.md");
189
162
 
190
163
  if (existsSync(preferencesPath) || existsSync(legacyPath)) {
191
164
  return false;
@@ -240,31 +213,4 @@ custom_instructions:
240
213
  return true;
241
214
  }
242
215
 
243
- /**
244
- * When commit_docs is false, ensure `.gsd/` is in .gitignore as a blanket
245
- * pattern. This keeps all GSD artifacts local-only.
246
- * Returns true if the file was modified, false if already complete.
247
- */
248
- function ensureBlanketGsdIgnore(gitignorePath: string, existing: string): boolean {
249
- const existingLines = new Set(
250
- existing
251
- .split("\n")
252
- .map((l) => l.trim())
253
- .filter((l) => l && !l.startsWith("#")),
254
- );
255
-
256
- // Already has blanket .gsd/ ignore
257
- if (existingLines.has(".gsd/") || existingLines.has(".gsd")) return false;
258
-
259
- const block = [
260
- "",
261
- "# ── GSD (local-only, commit_docs: false) ──",
262
- ".gsd/",
263
- "",
264
- ].join("\n");
265
-
266
- const prefix = existing && !existing.endsWith("\n") ? "\n" : "";
267
- writeFileSync(gitignorePath, existing + prefix + block, "utf-8");
268
- return true;
269
- }
270
216