gsd-pi 2.33.1-dev.ee47f1b → 2.34.0-dev.bbb5216

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 (135) hide show
  1. package/dist/bundled-resource-path.d.ts +8 -0
  2. package/dist/bundled-resource-path.js +14 -0
  3. package/dist/headless-query.js +6 -6
  4. package/dist/resources/extensions/gsd/auto/session.js +27 -32
  5. package/dist/resources/extensions/gsd/auto-dashboard.js +29 -109
  6. package/dist/resources/extensions/gsd/auto-direct-dispatch.js +6 -1
  7. package/dist/resources/extensions/gsd/auto-dispatch.js +52 -81
  8. package/dist/resources/extensions/gsd/auto-loop.js +956 -0
  9. package/dist/resources/extensions/gsd/auto-observability.js +4 -2
  10. package/dist/resources/extensions/gsd/auto-post-unit.js +75 -185
  11. package/dist/resources/extensions/gsd/auto-prompts.js +133 -101
  12. package/dist/resources/extensions/gsd/auto-recovery.js +59 -97
  13. package/dist/resources/extensions/gsd/auto-start.js +330 -309
  14. package/dist/resources/extensions/gsd/auto-supervisor.js +5 -11
  15. package/dist/resources/extensions/gsd/auto-timeout-recovery.js +7 -7
  16. package/dist/resources/extensions/gsd/auto-timers.js +3 -4
  17. package/dist/resources/extensions/gsd/auto-verification.js +35 -73
  18. package/dist/resources/extensions/gsd/auto-worktree-sync.js +167 -0
  19. package/dist/resources/extensions/gsd/auto-worktree.js +291 -126
  20. package/dist/resources/extensions/gsd/auto.js +283 -1013
  21. package/dist/resources/extensions/gsd/captures.js +10 -4
  22. package/dist/resources/extensions/gsd/dispatch-guard.js +7 -8
  23. package/dist/resources/extensions/gsd/docs/preferences-reference.md +25 -18
  24. package/dist/resources/extensions/gsd/doctor-checks.js +3 -4
  25. package/dist/resources/extensions/gsd/git-service.js +1 -1
  26. package/dist/resources/extensions/gsd/gsd-db.js +296 -151
  27. package/dist/resources/extensions/gsd/index.js +92 -228
  28. package/dist/resources/extensions/gsd/post-unit-hooks.js +13 -13
  29. package/dist/resources/extensions/gsd/progress-score.js +61 -156
  30. package/dist/resources/extensions/gsd/quick.js +98 -122
  31. package/dist/resources/extensions/gsd/session-lock.js +13 -0
  32. package/dist/resources/extensions/gsd/templates/preferences.md +1 -0
  33. package/dist/resources/extensions/gsd/undo.js +43 -48
  34. package/dist/resources/extensions/gsd/unit-runtime.js +16 -15
  35. package/dist/resources/extensions/gsd/verification-evidence.js +0 -1
  36. package/dist/resources/extensions/gsd/verification-gate.js +6 -35
  37. package/dist/resources/extensions/gsd/worktree-command.js +30 -24
  38. package/dist/resources/extensions/gsd/worktree-manager.js +2 -3
  39. package/dist/resources/extensions/gsd/worktree-resolver.js +344 -0
  40. package/dist/resources/extensions/gsd/worktree.js +7 -44
  41. package/dist/tool-bootstrap.js +59 -11
  42. package/dist/worktree-cli.js +7 -7
  43. package/package.json +1 -1
  44. package/packages/pi-ai/dist/models.generated.d.ts +3630 -5483
  45. package/packages/pi-ai/dist/models.generated.d.ts.map +1 -1
  46. package/packages/pi-ai/dist/models.generated.js +735 -2588
  47. package/packages/pi-ai/dist/models.generated.js.map +1 -1
  48. package/packages/pi-ai/src/models.generated.ts +1039 -2892
  49. package/packages/pi-coding-agent/package.json +1 -1
  50. package/pkg/package.json +1 -1
  51. package/src/resources/extensions/gsd/auto/session.ts +47 -30
  52. package/src/resources/extensions/gsd/auto-dashboard.ts +28 -131
  53. package/src/resources/extensions/gsd/auto-direct-dispatch.ts +6 -1
  54. package/src/resources/extensions/gsd/auto-dispatch.ts +135 -91
  55. package/src/resources/extensions/gsd/auto-loop.ts +1665 -0
  56. package/src/resources/extensions/gsd/auto-observability.ts +4 -2
  57. package/src/resources/extensions/gsd/auto-post-unit.ts +85 -228
  58. package/src/resources/extensions/gsd/auto-prompts.ts +138 -109
  59. package/src/resources/extensions/gsd/auto-recovery.ts +124 -118
  60. package/src/resources/extensions/gsd/auto-start.ts +440 -354
  61. package/src/resources/extensions/gsd/auto-supervisor.ts +5 -12
  62. package/src/resources/extensions/gsd/auto-timeout-recovery.ts +8 -8
  63. package/src/resources/extensions/gsd/auto-timers.ts +3 -4
  64. package/src/resources/extensions/gsd/auto-verification.ts +76 -90
  65. package/src/resources/extensions/gsd/auto-worktree-sync.ts +204 -0
  66. package/src/resources/extensions/gsd/auto-worktree.ts +389 -141
  67. package/src/resources/extensions/gsd/auto.ts +515 -1199
  68. package/src/resources/extensions/gsd/captures.ts +10 -4
  69. package/src/resources/extensions/gsd/dispatch-guard.ts +13 -9
  70. package/src/resources/extensions/gsd/docs/preferences-reference.md +25 -18
  71. package/src/resources/extensions/gsd/doctor-checks.ts +3 -4
  72. package/src/resources/extensions/gsd/git-service.ts +8 -1
  73. package/src/resources/extensions/gsd/gitignore.ts +4 -2
  74. package/src/resources/extensions/gsd/gsd-db.ts +375 -180
  75. package/src/resources/extensions/gsd/index.ts +104 -263
  76. package/src/resources/extensions/gsd/post-unit-hooks.ts +13 -13
  77. package/src/resources/extensions/gsd/progress-score.ts +65 -200
  78. package/src/resources/extensions/gsd/quick.ts +121 -125
  79. package/src/resources/extensions/gsd/session-lock.ts +11 -0
  80. package/src/resources/extensions/gsd/templates/preferences.md +1 -0
  81. package/src/resources/extensions/gsd/tests/agent-end-retry.test.ts +32 -59
  82. package/src/resources/extensions/gsd/tests/all-milestones-complete-merge.test.ts +75 -27
  83. package/src/resources/extensions/gsd/tests/auto-budget-alerts.test.ts +1 -1
  84. package/src/resources/extensions/gsd/tests/auto-lock-creation.test.ts +37 -0
  85. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +1458 -0
  86. package/src/resources/extensions/gsd/tests/auto-recovery.test.ts +8 -162
  87. package/src/resources/extensions/gsd/tests/auto-secrets-gate.test.ts +2 -108
  88. package/src/resources/extensions/gsd/tests/auto-session-encapsulation.test.ts +1 -3
  89. package/src/resources/extensions/gsd/tests/auto-worktree-milestone-merge.test.ts +0 -3
  90. package/src/resources/extensions/gsd/tests/auto-worktree.test.ts +58 -0
  91. package/src/resources/extensions/gsd/tests/dispatch-guard.test.ts +0 -55
  92. package/src/resources/extensions/gsd/tests/headless-query.test.ts +22 -0
  93. package/src/resources/extensions/gsd/tests/milestone-transition-worktree.test.ts +8 -11
  94. package/src/resources/extensions/gsd/tests/provider-errors.test.ts +4 -6
  95. package/src/resources/extensions/gsd/tests/run-uat.test.ts +3 -3
  96. package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +64 -0
  97. package/src/resources/extensions/gsd/tests/sidecar-queue.test.ts +181 -0
  98. package/src/resources/extensions/gsd/tests/stale-worktree-cwd.test.ts +0 -3
  99. package/src/resources/extensions/gsd/tests/token-profile.test.ts +6 -6
  100. package/src/resources/extensions/gsd/tests/triage-dispatch.test.ts +6 -6
  101. package/src/resources/extensions/gsd/tests/undo.test.ts +6 -0
  102. package/src/resources/extensions/gsd/tests/verification-evidence.test.ts +24 -26
  103. package/src/resources/extensions/gsd/tests/verification-gate.test.ts +7 -201
  104. package/src/resources/extensions/gsd/tests/worktree-db-integration.test.ts +205 -0
  105. package/src/resources/extensions/gsd/tests/worktree-db.test.ts +442 -0
  106. package/src/resources/extensions/gsd/tests/worktree-e2e.test.ts +0 -3
  107. package/src/resources/extensions/gsd/tests/worktree-resolver.test.ts +705 -0
  108. package/src/resources/extensions/gsd/tests/worktree-sync-milestones.test.ts +57 -106
  109. package/src/resources/extensions/gsd/tests/worktree.test.ts +5 -1
  110. package/src/resources/extensions/gsd/tests/write-gate.test.ts +43 -132
  111. package/src/resources/extensions/gsd/types.ts +90 -81
  112. package/src/resources/extensions/gsd/undo.ts +42 -46
  113. package/src/resources/extensions/gsd/unit-runtime.ts +14 -18
  114. package/src/resources/extensions/gsd/verification-evidence.ts +1 -3
  115. package/src/resources/extensions/gsd/verification-gate.ts +6 -39
  116. package/src/resources/extensions/gsd/worktree-command.ts +36 -24
  117. package/src/resources/extensions/gsd/worktree-manager.ts +2 -3
  118. package/src/resources/extensions/gsd/worktree-resolver.ts +485 -0
  119. package/src/resources/extensions/gsd/worktree.ts +7 -44
  120. package/dist/resources/extensions/gsd/auto-constants.js +0 -5
  121. package/dist/resources/extensions/gsd/auto-idempotency.js +0 -106
  122. package/dist/resources/extensions/gsd/auto-stuck-detection.js +0 -165
  123. package/dist/resources/extensions/gsd/mechanical-completion.js +0 -351
  124. package/src/resources/extensions/gsd/auto-constants.ts +0 -6
  125. package/src/resources/extensions/gsd/auto-idempotency.ts +0 -151
  126. package/src/resources/extensions/gsd/auto-stuck-detection.ts +0 -221
  127. package/src/resources/extensions/gsd/mechanical-completion.ts +0 -430
  128. package/src/resources/extensions/gsd/tests/auto-dispatch-loop.test.ts +0 -691
  129. package/src/resources/extensions/gsd/tests/auto-reentrancy-guard.test.ts +0 -127
  130. package/src/resources/extensions/gsd/tests/auto-skip-loop.test.ts +0 -123
  131. package/src/resources/extensions/gsd/tests/dispatch-stall-guard.test.ts +0 -126
  132. package/src/resources/extensions/gsd/tests/loop-regression.test.ts +0 -874
  133. package/src/resources/extensions/gsd/tests/mechanical-completion.test.ts +0 -356
  134. package/src/resources/extensions/gsd/tests/progress-score.test.ts +0 -206
  135. package/src/resources/extensions/gsd/tests/session-lock.test.ts +0 -434
@@ -0,0 +1,442 @@
1
+ import { createTestContext } from './test-helpers.ts';
2
+ import * as fs from 'node:fs';
3
+ import * as path from 'node:path';
4
+ import * as os from 'node:os';
5
+ import {
6
+ openDatabase,
7
+ closeDatabase,
8
+ isDbAvailable,
9
+ insertDecision,
10
+ insertRequirement,
11
+ insertArtifact,
12
+ getDecisionById,
13
+ getRequirementById,
14
+ _getAdapter,
15
+ copyWorktreeDb,
16
+ reconcileWorktreeDb,
17
+ } from '../gsd-db.ts';
18
+
19
+ const { assertEq, assertTrue, report } = createTestContext();
20
+
21
+ // ═══════════════════════════════════════════════════════════════════════════
22
+ // Helpers
23
+ // ═══════════════════════════════════════════════════════════════════════════
24
+
25
+ function tempDir(): string {
26
+ return fs.mkdtempSync(path.join(os.tmpdir(), 'gsd-wt-test-'));
27
+ }
28
+
29
+ function cleanup(...dirs: string[]): void {
30
+ closeDatabase();
31
+ for (const dir of dirs) {
32
+ try {
33
+ fs.rmSync(dir, { recursive: true, force: true });
34
+ } catch {
35
+ // best effort
36
+ }
37
+ }
38
+ }
39
+
40
+ function seedMainDb(dbPath: string): void {
41
+ openDatabase(dbPath);
42
+ insertDecision({
43
+ id: 'D001',
44
+ when_context: '2025-01-01',
45
+ scope: 'M001/S01',
46
+ decision: 'Use SQLite',
47
+ choice: 'node:sqlite',
48
+ rationale: 'Built-in',
49
+ revisable: 'yes',
50
+ superseded_by: null,
51
+ });
52
+ insertRequirement({
53
+ id: 'R001',
54
+ class: 'functional',
55
+ status: 'active',
56
+ description: 'Must store decisions',
57
+ why: 'Core feature',
58
+ source: 'design',
59
+ primary_owner: 'S01',
60
+ supporting_slices: '',
61
+ validation: 'test',
62
+ notes: '',
63
+ full_content: 'Full requirement text',
64
+ superseded_by: null,
65
+ });
66
+ insertArtifact({
67
+ path: 'docs/arch.md',
68
+ artifact_type: 'plan',
69
+ milestone_id: 'M001',
70
+ slice_id: null,
71
+ task_id: null,
72
+ full_content: 'Architecture document',
73
+ });
74
+ }
75
+
76
+ // ═══════════════════════════════════════════════════════════════════════════
77
+ // copyWorktreeDb tests
78
+ // ═══════════════════════════════════════════════════════════════════════════
79
+
80
+ console.log('\n=== worktree-db: copyWorktreeDb ===');
81
+
82
+ // Test: copies DB file and data is queryable
83
+ {
84
+ const srcDir = tempDir();
85
+ const destDir = tempDir();
86
+ const srcDb = path.join(srcDir, 'gsd.db');
87
+ const destDb = path.join(destDir, 'nested', 'gsd.db');
88
+
89
+ seedMainDb(srcDb);
90
+ closeDatabase();
91
+
92
+ const result = copyWorktreeDb(srcDb, destDb);
93
+ assertTrue(result === true, 'copyWorktreeDb returns true on success');
94
+ assertTrue(fs.existsSync(destDb), 'dest DB file exists after copy');
95
+
96
+ // Open the copy and verify data is queryable
97
+ openDatabase(destDb);
98
+ const d = getDecisionById('D001');
99
+ assertTrue(d !== null, 'decision queryable in copied DB');
100
+ assertEq(d?.choice, 'node:sqlite', 'decision data preserved in copy');
101
+
102
+ const r = getRequirementById('R001');
103
+ assertTrue(r !== null, 'requirement queryable in copied DB');
104
+ assertEq(r?.description, 'Must store decisions', 'requirement data preserved in copy');
105
+
106
+ cleanup(srcDir, destDir);
107
+ }
108
+
109
+ // Test: skips -wal and -shm files
110
+ {
111
+ const srcDir = tempDir();
112
+ const destDir = tempDir();
113
+ const srcDb = path.join(srcDir, 'gsd.db');
114
+ const destDb = path.join(destDir, 'gsd.db');
115
+
116
+ seedMainDb(srcDb);
117
+ closeDatabase();
118
+
119
+ // Create fake WAL/SHM files
120
+ fs.writeFileSync(srcDb + '-wal', 'fake wal data');
121
+ fs.writeFileSync(srcDb + '-shm', 'fake shm data');
122
+
123
+ copyWorktreeDb(srcDb, destDb);
124
+
125
+ assertTrue(fs.existsSync(destDb), 'DB file copied');
126
+ assertTrue(!fs.existsSync(destDb + '-wal'), 'WAL file NOT copied');
127
+ assertTrue(!fs.existsSync(destDb + '-shm'), 'SHM file NOT copied');
128
+
129
+ cleanup(srcDir, destDir);
130
+ }
131
+
132
+ // Test: returns false when source doesn't exist (no throw)
133
+ {
134
+ const destDir = tempDir();
135
+ const result = copyWorktreeDb('/nonexistent/path/gsd.db', path.join(destDir, 'gsd.db'));
136
+ assertEq(result, false, 'returns false for missing source');
137
+ cleanup(destDir);
138
+ }
139
+
140
+ // Test: creates dest directory if needed
141
+ {
142
+ const srcDir = tempDir();
143
+ const destDir = tempDir();
144
+ const srcDb = path.join(srcDir, 'gsd.db');
145
+ const deepDest = path.join(destDir, 'a', 'b', 'c', 'gsd.db');
146
+
147
+ seedMainDb(srcDb);
148
+ closeDatabase();
149
+
150
+ const result = copyWorktreeDb(srcDb, deepDest);
151
+ assertTrue(result === true, 'copyWorktreeDb succeeds with nested dest');
152
+ assertTrue(fs.existsSync(deepDest), 'DB file created at deeply nested path');
153
+
154
+ cleanup(srcDir, destDir);
155
+ }
156
+
157
+ // ═══════════════════════════════════════════════════════════════════════════
158
+ // reconcileWorktreeDb tests
159
+ // ═══════════════════════════════════════════════════════════════════════════
160
+
161
+ console.log('\n=== worktree-db: reconcileWorktreeDb ===');
162
+
163
+ // Test: merges new decisions from worktree into main
164
+ {
165
+ const mainDir = tempDir();
166
+ const wtDir = tempDir();
167
+ const mainDb = path.join(mainDir, 'gsd.db');
168
+ const wtDb = path.join(wtDir, 'gsd.db');
169
+
170
+ // Seed main with D001
171
+ seedMainDb(mainDb);
172
+ closeDatabase();
173
+
174
+ // Copy to worktree, add D002 in worktree
175
+ copyWorktreeDb(mainDb, wtDb);
176
+ openDatabase(wtDb);
177
+ insertDecision({
178
+ id: 'D002',
179
+ when_context: '2025-02-01',
180
+ scope: 'M001/S02',
181
+ decision: 'Use WAL mode',
182
+ choice: 'WAL',
183
+ rationale: 'Performance',
184
+ revisable: 'yes',
185
+ superseded_by: null,
186
+ });
187
+ closeDatabase();
188
+
189
+ // Re-open main and reconcile
190
+ openDatabase(mainDb);
191
+ const result = reconcileWorktreeDb(mainDb, wtDb);
192
+
193
+ assertTrue(result.decisions > 0, 'decisions merged count > 0');
194
+ const d2 = getDecisionById('D002');
195
+ assertTrue(d2 !== null, 'D002 from worktree now in main');
196
+ assertEq(d2?.choice, 'WAL', 'D002 data correct after merge');
197
+
198
+ cleanup(mainDir, wtDir);
199
+ }
200
+
201
+ // Test: merges new requirements from worktree into main
202
+ {
203
+ const mainDir = tempDir();
204
+ const wtDir = tempDir();
205
+ const mainDb = path.join(mainDir, 'gsd.db');
206
+ const wtDb = path.join(wtDir, 'gsd.db');
207
+
208
+ seedMainDb(mainDb);
209
+ closeDatabase();
210
+ copyWorktreeDb(mainDb, wtDb);
211
+
212
+ openDatabase(wtDb);
213
+ insertRequirement({
214
+ id: 'R002',
215
+ class: 'non-functional',
216
+ status: 'active',
217
+ description: 'Must be fast',
218
+ why: 'UX',
219
+ source: 'design',
220
+ primary_owner: 'S02',
221
+ supporting_slices: '',
222
+ validation: 'benchmark',
223
+ notes: '',
224
+ full_content: 'Performance requirement',
225
+ superseded_by: null,
226
+ });
227
+ closeDatabase();
228
+
229
+ openDatabase(mainDb);
230
+ const result = reconcileWorktreeDb(mainDb, wtDb);
231
+
232
+ assertTrue(result.requirements > 0, 'requirements merged count > 0');
233
+ const r2 = getRequirementById('R002');
234
+ assertTrue(r2 !== null, 'R002 from worktree now in main');
235
+ assertEq(r2?.description, 'Must be fast', 'R002 data correct after merge');
236
+
237
+ cleanup(mainDir, wtDir);
238
+ }
239
+
240
+ // Test: merges new artifacts from worktree into main
241
+ {
242
+ const mainDir = tempDir();
243
+ const wtDir = tempDir();
244
+ const mainDb = path.join(mainDir, 'gsd.db');
245
+ const wtDb = path.join(wtDir, 'gsd.db');
246
+
247
+ seedMainDb(mainDb);
248
+ closeDatabase();
249
+ copyWorktreeDb(mainDb, wtDb);
250
+
251
+ openDatabase(wtDb);
252
+ insertArtifact({
253
+ path: 'docs/api.md',
254
+ artifact_type: 'reference',
255
+ milestone_id: 'M001',
256
+ slice_id: 'S01',
257
+ task_id: 'T01',
258
+ full_content: 'API documentation',
259
+ });
260
+ closeDatabase();
261
+
262
+ openDatabase(mainDb);
263
+ const result = reconcileWorktreeDb(mainDb, wtDb);
264
+
265
+ assertTrue(result.artifacts > 0, 'artifacts merged count > 0');
266
+ const adapter = _getAdapter()!;
267
+ const row = adapter.prepare('SELECT * FROM artifacts WHERE path = ?').get('docs/api.md');
268
+ assertTrue(row !== null, 'artifact from worktree now in main');
269
+ assertEq(row?.['artifact_type'], 'reference', 'artifact data correct after merge');
270
+
271
+ cleanup(mainDir, wtDir);
272
+ }
273
+
274
+ // Test: detects conflicts (same PK, different content in both DBs)
275
+ {
276
+ const mainDir = tempDir();
277
+ const wtDir = tempDir();
278
+ const mainDb = path.join(mainDir, 'gsd.db');
279
+ const wtDb = path.join(wtDir, 'gsd.db');
280
+
281
+ // Seed main with D001
282
+ seedMainDb(mainDb);
283
+ closeDatabase();
284
+ copyWorktreeDb(mainDb, wtDb);
285
+
286
+ // Modify D001 in main
287
+ openDatabase(mainDb);
288
+ const mainAdapter = _getAdapter()!;
289
+ mainAdapter.prepare(
290
+ `UPDATE decisions SET choice = 'better-sqlite3' WHERE id = 'D001'`,
291
+ ).run();
292
+ closeDatabase();
293
+
294
+ // Modify D001 in worktree differently
295
+ openDatabase(wtDb);
296
+ const wtAdapter = _getAdapter()!;
297
+ wtAdapter.prepare(
298
+ `UPDATE decisions SET choice = 'sql.js' WHERE id = 'D001'`,
299
+ ).run();
300
+ closeDatabase();
301
+
302
+ // Reconcile
303
+ openDatabase(mainDb);
304
+ const result = reconcileWorktreeDb(mainDb, wtDb);
305
+
306
+ assertTrue(result.conflicts.length > 0, 'conflicts detected');
307
+ assertTrue(
308
+ result.conflicts.some(c => c.includes('D001')),
309
+ 'conflict mentions D001',
310
+ );
311
+
312
+ // Worktree-wins: D001 should now have worktree's value
313
+ const d1 = getDecisionById('D001');
314
+ assertEq(d1?.choice, 'sql.js', 'worktree wins on conflict (INSERT OR REPLACE)');
315
+
316
+ cleanup(mainDir, wtDir);
317
+ }
318
+
319
+ // Test: handles missing worktree DB gracefully
320
+ {
321
+ const mainDir = tempDir();
322
+ const mainDb = path.join(mainDir, 'gsd.db');
323
+
324
+ seedMainDb(mainDb);
325
+
326
+ const result = reconcileWorktreeDb(mainDb, '/nonexistent/worktree.db');
327
+ assertEq(result.decisions, 0, 'no decisions merged for missing worktree DB');
328
+ assertEq(result.requirements, 0, 'no requirements merged for missing worktree DB');
329
+ assertEq(result.artifacts, 0, 'no artifacts merged for missing worktree DB');
330
+ assertEq(result.conflicts.length, 0, 'no conflicts for missing worktree DB');
331
+
332
+ cleanup(mainDir);
333
+ }
334
+
335
+ // Test: path with spaces works
336
+ {
337
+ const baseDir = tempDir();
338
+ const mainDir = path.join(baseDir, 'main dir');
339
+ const wtDir = path.join(baseDir, 'worktree dir');
340
+ fs.mkdirSync(mainDir, { recursive: true });
341
+ fs.mkdirSync(wtDir, { recursive: true });
342
+
343
+ const mainDb = path.join(mainDir, 'gsd.db');
344
+ const wtDb = path.join(wtDir, 'gsd.db');
345
+
346
+ seedMainDb(mainDb);
347
+ closeDatabase();
348
+ copyWorktreeDb(mainDb, wtDb);
349
+
350
+ // Add a decision in worktree
351
+ openDatabase(wtDb);
352
+ insertDecision({
353
+ id: 'D003',
354
+ when_context: '2025-03-01',
355
+ scope: 'M001/S03',
356
+ decision: 'Path spaces test',
357
+ choice: 'yes',
358
+ rationale: 'Robustness',
359
+ revisable: 'no',
360
+ superseded_by: null,
361
+ });
362
+ closeDatabase();
363
+
364
+ openDatabase(mainDb);
365
+ const result = reconcileWorktreeDb(mainDb, wtDb);
366
+ assertTrue(result.decisions > 0, 'reconciliation works with spaces in path');
367
+ const d3 = getDecisionById('D003');
368
+ assertTrue(d3 !== null, 'D003 merged from worktree with spaces in path');
369
+
370
+ cleanup(baseDir);
371
+ }
372
+
373
+ // Test: main DB is usable after reconciliation (DETACH cleanup verified)
374
+ {
375
+ const mainDir = tempDir();
376
+ const wtDir = tempDir();
377
+ const mainDb = path.join(mainDir, 'gsd.db');
378
+ const wtDb = path.join(wtDir, 'gsd.db');
379
+
380
+ seedMainDb(mainDb);
381
+ closeDatabase();
382
+ copyWorktreeDb(mainDb, wtDb);
383
+
384
+ openDatabase(mainDb);
385
+ reconcileWorktreeDb(mainDb, wtDb);
386
+
387
+ // Verify main DB is still fully usable after DETACH
388
+ assertTrue(isDbAvailable(), 'DB still available after reconciliation');
389
+
390
+ insertDecision({
391
+ id: 'D099',
392
+ when_context: '2025-12-01',
393
+ scope: 'test',
394
+ decision: 'Post-reconcile insert',
395
+ choice: 'works',
396
+ rationale: 'Verify DETACH cleanup',
397
+ revisable: 'no',
398
+ superseded_by: null,
399
+ });
400
+
401
+ const d99 = getDecisionById('D099');
402
+ assertTrue(d99 !== null, 'can insert and query after reconciliation');
403
+ assertEq(d99?.choice, 'works', 'post-reconcile data correct');
404
+
405
+ // Verify no "wt" database still attached
406
+ const adapter = _getAdapter()!;
407
+ let wtAccessible = false;
408
+ try {
409
+ adapter.prepare('SELECT count(*) FROM wt.decisions').get();
410
+ wtAccessible = true;
411
+ } catch {
412
+ // Expected — wt should be detached
413
+ }
414
+ assertTrue(!wtAccessible, 'wt database is detached after reconciliation');
415
+
416
+ cleanup(mainDir, wtDir);
417
+ }
418
+
419
+ // Test: reconcile with empty worktree DB (no new rows, no conflicts)
420
+ {
421
+ const mainDir = tempDir();
422
+ const wtDir = tempDir();
423
+ const mainDb = path.join(mainDir, 'gsd.db');
424
+ const wtDb = path.join(wtDir, 'gsd.db');
425
+
426
+ seedMainDb(mainDb);
427
+ closeDatabase();
428
+ copyWorktreeDb(mainDb, wtDb);
429
+
430
+ // Don't modify the worktree DB at all — reconcile the identical copy
431
+ openDatabase(mainDb);
432
+ const result = reconcileWorktreeDb(mainDb, wtDb);
433
+
434
+ // Should still report counts for the existing rows (INSERT OR REPLACE touches them)
435
+ assertTrue(result.conflicts.length === 0, 'no conflicts when DBs are identical');
436
+ assertTrue(isDbAvailable(), 'DB usable after no-change reconciliation');
437
+
438
+ cleanup(mainDir, wtDir);
439
+ }
440
+
441
+ // ─── Final Report ──────────────────────────────────────────────────────────
442
+ report();
@@ -38,9 +38,6 @@ function createTempRepo(): string {
38
38
  run("git config user.email test@test.com", dir);
39
39
  run("git config user.name Test", dir);
40
40
  writeFileSync(join(dir, "README.md"), "# test\n");
41
- // Mirror production: .gsd/worktrees/ is gitignored so autoCommitDirtyState
42
- // doesn't pick up the worktrees directory as dirty state (#1127 fix).
43
- writeFileSync(join(dir, ".gitignore"), ".gsd/worktrees/\n");
44
41
  mkdirSync(join(dir, ".gsd"), { recursive: true });
45
42
  writeFileSync(join(dir, ".gsd", "STATE.md"), "# State\n");
46
43
  run("git add .", dir);