gsd-pi 2.81.0-dev.72a81bdf3 → 2.82.0-dev.2841a1e44

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 (243) hide show
  1. package/README.md +49 -30
  2. package/dist/resources/.managed-resources-content-hash +1 -1
  3. package/dist/resources/GSD-WORKFLOW.md +3 -1
  4. package/dist/resources/extensions/browser-tools/tools/screenshot.js +1 -0
  5. package/dist/resources/extensions/browser-tools/tools/zoom.js +1 -0
  6. package/dist/resources/extensions/cmux/index.js +5 -0
  7. package/dist/resources/extensions/gsd/auto/orchestrator.js +113 -6
  8. package/dist/resources/extensions/gsd/auto/phases.js +9 -0
  9. package/dist/resources/extensions/gsd/auto-post-unit.js +169 -124
  10. package/dist/resources/extensions/gsd/auto-prompts.js +13 -5
  11. package/dist/resources/extensions/gsd/auto-verification.js +28 -22
  12. package/dist/resources/extensions/gsd/auto.js +128 -52
  13. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +5 -0
  14. package/dist/resources/extensions/gsd/bootstrap/subagent-input.js +16 -7
  15. package/dist/resources/extensions/gsd/bootstrap/system-context.js +55 -12
  16. package/dist/resources/extensions/gsd/bootstrap/write-gate.js +3 -1
  17. package/dist/resources/extensions/gsd/clean-root-preflight.js +170 -8
  18. package/dist/resources/extensions/gsd/commands/catalog.js +4 -1
  19. package/dist/resources/extensions/gsd/commands/handlers/core.js +22 -1
  20. package/dist/resources/extensions/gsd/commands-bootstrap.js +5 -0
  21. package/dist/resources/extensions/gsd/commands-handlers.js +15 -2
  22. package/dist/resources/extensions/gsd/context-store.js +112 -0
  23. package/dist/resources/extensions/gsd/db-writer.js +150 -84
  24. package/dist/resources/extensions/gsd/docs/preferences-reference.md +1 -1
  25. package/dist/resources/extensions/gsd/doctor-git-checks.js +41 -6
  26. package/dist/resources/extensions/gsd/knowledge-backfill.js +144 -0
  27. package/dist/resources/extensions/gsd/knowledge-capture.js +136 -0
  28. package/dist/resources/extensions/gsd/knowledge-parser.js +154 -0
  29. package/dist/resources/extensions/gsd/knowledge-projection.js +210 -0
  30. package/dist/resources/extensions/gsd/markdown-renderer.js +6 -1
  31. package/dist/resources/extensions/gsd/md-importer.js +1 -1
  32. package/dist/resources/extensions/gsd/memory-backfill.js +73 -17
  33. package/dist/resources/extensions/gsd/memory-consolidation-scanner.js +222 -0
  34. package/dist/resources/extensions/gsd/migrate/command.js +5 -0
  35. package/dist/resources/extensions/gsd/migrate/preview.js +9 -0
  36. package/dist/resources/extensions/gsd/migrate/transformer.js +51 -4
  37. package/dist/resources/extensions/gsd/migrate/writer.js +11 -1
  38. package/dist/resources/extensions/gsd/prompts/system.md +2 -2
  39. package/dist/resources/extensions/gsd/provider-switch-observer.js +146 -0
  40. package/dist/resources/extensions/gsd/templates/knowledge.md +2 -2
  41. package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +119 -0
  42. package/dist/resources/extensions/gsd/unit-context-manifest.js +25 -2
  43. package/dist/resources/extensions/gsd/verification-verdict.js +26 -0
  44. package/dist/resources/extensions/gsd/worktree-lifecycle.js +21 -2
  45. package/dist/resources/extensions/subagent/index.js +448 -78
  46. package/dist/resources/extensions/subagent/launch.js +77 -0
  47. package/dist/resources/extensions/subagent/run-store.js +148 -0
  48. package/dist/resources/extensions/visual-brief/artifact-policy.js +29 -0
  49. package/dist/resources/extensions/visual-brief/extension-manifest.json +8 -0
  50. package/dist/resources/extensions/visual-brief/index.js +5 -0
  51. package/dist/resources/extensions/visual-brief/page-contract.js +122 -0
  52. package/dist/resources/extensions/visual-brief/prompts.js +111 -0
  53. package/dist/tsconfig.extensions.tsbuildinfo +1 -1
  54. package/dist/web/standalone/.next/BUILD_ID +1 -1
  55. package/dist/web/standalone/.next/app-path-routes-manifest.json +15 -15
  56. package/dist/web/standalone/.next/build-manifest.json +3 -3
  57. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  58. package/dist/web/standalone/.next/react-loadable-manifest.json +3 -3
  59. package/dist/web/standalone/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
  60. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  61. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  63. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  65. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  66. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  67. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  68. package/dist/web/standalone/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
  69. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  70. package/dist/web/standalone/.next/server/app/_not-found.rsc +2 -2
  71. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +2 -2
  72. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  73. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +2 -2
  74. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  75. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  76. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +2 -2
  77. package/dist/web/standalone/.next/server/app/index.html +1 -1
  78. package/dist/web/standalone/.next/server/app/index.rsc +2 -2
  79. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  80. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +2 -2
  81. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  82. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +2 -2
  83. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +2 -2
  84. package/dist/web/standalone/.next/server/app/page_client-reference-manifest.js +1 -1
  85. package/dist/web/standalone/.next/server/app-paths-manifest.json +15 -15
  86. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  87. package/dist/web/standalone/.next/server/middleware-react-loadable-manifest.js +1 -1
  88. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  89. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  90. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  91. package/dist/web/standalone/.next/static/chunks/2973.33f26573894b6153.js +2 -0
  92. package/dist/web/standalone/.next/static/chunks/{8359.e059d86b255fce1c.js → 8359.7eb3bb8f8ecf4c01.js} +2 -2
  93. package/dist/web/standalone/.next/static/chunks/{webpack-de742b64187e13fe.js → webpack-6a95bc41e0f7ec89.js} +1 -1
  94. package/dist/web/standalone/.next/static/css/0262768ec1b89d34.css +1 -0
  95. package/package.json +5 -4
  96. package/packages/contracts/dist/rpc.test.js +7 -0
  97. package/packages/contracts/dist/rpc.test.js.map +1 -1
  98. package/packages/contracts/dist/workflow.d.ts +21 -0
  99. package/packages/contracts/dist/workflow.d.ts.map +1 -1
  100. package/packages/contracts/dist/workflow.js +24 -0
  101. package/packages/contracts/dist/workflow.js.map +1 -1
  102. package/packages/contracts/src/rpc.test.ts +8 -0
  103. package/packages/contracts/src/workflow.ts +24 -0
  104. package/packages/daemon/package.json +2 -2
  105. package/packages/mcp-server/README.md +14 -3
  106. package/packages/mcp-server/dist/workflow-tools.d.ts +0 -3
  107. package/packages/mcp-server/dist/workflow-tools.d.ts.map +1 -1
  108. package/packages/mcp-server/dist/workflow-tools.js +80 -0
  109. package/packages/mcp-server/dist/workflow-tools.js.map +1 -1
  110. package/packages/mcp-server/package.json +2 -2
  111. package/packages/mcp-server/src/workflow-tools-parity.test.ts +244 -0
  112. package/packages/mcp-server/src/workflow-tools.test.ts +22 -0
  113. package/packages/mcp-server/src/workflow-tools.ts +168 -0
  114. package/packages/mcp-server/tsconfig.tsbuildinfo +1 -1
  115. package/packages/native/package.json +1 -1
  116. package/packages/pi-agent-core/package.json +1 -1
  117. package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -1
  118. package/packages/pi-ai/dist/index.d.ts +2 -2
  119. package/packages/pi-ai/dist/index.d.ts.map +1 -1
  120. package/packages/pi-ai/dist/index.js +1 -1
  121. package/packages/pi-ai/dist/index.js.map +1 -1
  122. package/packages/pi-ai/dist/providers/transform-messages.d.ts +11 -0
  123. package/packages/pi-ai/dist/providers/transform-messages.d.ts.map +1 -1
  124. package/packages/pi-ai/dist/providers/transform-messages.js +20 -0
  125. package/packages/pi-ai/dist/providers/transform-messages.js.map +1 -1
  126. package/packages/pi-ai/package.json +1 -1
  127. package/packages/pi-ai/src/index.ts +7 -2
  128. package/packages/pi-ai/src/providers/transform-messages.ts +24 -0
  129. package/packages/pi-ai/tsconfig.tsbuildinfo +1 -1
  130. package/packages/pi-coding-agent/dist/core/system-prompt.js +4 -4
  131. package/packages/pi-coding-agent/dist/core/system-prompt.js.map +1 -1
  132. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.d.ts +2 -0
  133. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.d.ts.map +1 -0
  134. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.js +17 -0
  135. package/packages/pi-coding-agent/dist/tests/system-prompt-file-safety.test.js.map +1 -0
  136. package/packages/pi-coding-agent/package.json +1 -1
  137. package/packages/pi-coding-agent/src/core/system-prompt.ts +4 -4
  138. package/packages/pi-coding-agent/src/tests/system-prompt-file-safety.test.ts +22 -0
  139. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -1
  140. package/packages/pi-tui/dist/tui.d.ts.map +1 -1
  141. package/packages/pi-tui/dist/tui.js +5 -0
  142. package/packages/pi-tui/dist/tui.js.map +1 -1
  143. package/packages/pi-tui/package.json +1 -1
  144. package/packages/pi-tui/src/tui.ts +6 -0
  145. package/packages/pi-tui/tsconfig.tsbuildinfo +1 -1
  146. package/packages/rpc-client/package.json +1 -1
  147. package/packages/rpc-client/tsconfig.tsbuildinfo +1 -1
  148. package/pkg/package.json +1 -1
  149. package/src/resources/GSD-WORKFLOW.md +3 -1
  150. package/src/resources/extensions/browser-tools/tools/screenshot.ts +1 -0
  151. package/src/resources/extensions/browser-tools/tools/zoom.ts +1 -0
  152. package/src/resources/extensions/cmux/index.ts +6 -0
  153. package/src/resources/extensions/gsd/auto/contracts.ts +46 -11
  154. package/src/resources/extensions/gsd/auto/orchestrator.ts +118 -6
  155. package/src/resources/extensions/gsd/auto/phases.ts +14 -0
  156. package/src/resources/extensions/gsd/auto-post-unit.ts +194 -137
  157. package/src/resources/extensions/gsd/auto-prompts.ts +13 -5
  158. package/src/resources/extensions/gsd/auto-verification.ts +36 -34
  159. package/src/resources/extensions/gsd/auto.ts +136 -51
  160. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +6 -0
  161. package/src/resources/extensions/gsd/bootstrap/subagent-input.ts +16 -6
  162. package/src/resources/extensions/gsd/bootstrap/system-context.ts +58 -15
  163. package/src/resources/extensions/gsd/bootstrap/write-gate.ts +3 -2
  164. package/src/resources/extensions/gsd/clean-root-preflight.ts +174 -8
  165. package/src/resources/extensions/gsd/commands/catalog.ts +4 -1
  166. package/src/resources/extensions/gsd/commands/handlers/core.ts +25 -1
  167. package/src/resources/extensions/gsd/commands-bootstrap.ts +10 -0
  168. package/src/resources/extensions/gsd/commands-handlers.ts +19 -2
  169. package/src/resources/extensions/gsd/context-store.ts +120 -1
  170. package/src/resources/extensions/gsd/db-writer.ts +167 -84
  171. package/src/resources/extensions/gsd/docs/preferences-reference.md +1 -1
  172. package/src/resources/extensions/gsd/doctor-git-checks.ts +44 -6
  173. package/src/resources/extensions/gsd/doctor-types.ts +2 -0
  174. package/src/resources/extensions/gsd/knowledge-backfill.ts +164 -0
  175. package/src/resources/extensions/gsd/knowledge-capture.ts +160 -0
  176. package/src/resources/extensions/gsd/knowledge-parser.ts +174 -0
  177. package/src/resources/extensions/gsd/knowledge-projection.ts +241 -0
  178. package/src/resources/extensions/gsd/markdown-renderer.ts +6 -1
  179. package/src/resources/extensions/gsd/md-importer.ts +1 -1
  180. package/src/resources/extensions/gsd/memory-backfill.ts +89 -17
  181. package/src/resources/extensions/gsd/memory-consolidation-scanner.ts +277 -0
  182. package/src/resources/extensions/gsd/migrate/command.ts +5 -0
  183. package/src/resources/extensions/gsd/migrate/preview.ts +10 -0
  184. package/src/resources/extensions/gsd/migrate/transformer.ts +58 -4
  185. package/src/resources/extensions/gsd/migrate/writer.ts +14 -1
  186. package/src/resources/extensions/gsd/prompts/system.md +2 -2
  187. package/src/resources/extensions/gsd/provider-switch-observer.ts +185 -0
  188. package/src/resources/extensions/gsd/templates/knowledge.md +2 -2
  189. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +75 -0
  190. package/src/resources/extensions/gsd/tests/auto-orchestrator.test.ts +408 -4
  191. package/src/resources/extensions/gsd/tests/auto-paused-ui-cleanup.test.ts +6 -5
  192. package/src/resources/extensions/gsd/tests/auto-runtime-state.test.ts +4 -4
  193. package/src/resources/extensions/gsd/tests/brief-command.test.ts +89 -0
  194. package/src/resources/extensions/gsd/tests/browser-tools-compatibility-declarations.test.ts +62 -0
  195. package/src/resources/extensions/gsd/tests/clean-root-preflight.test.ts +107 -2
  196. package/src/resources/extensions/gsd/tests/closeout-git-deferral.test.ts +16 -0
  197. package/src/resources/extensions/gsd/tests/context-store-decisions-from-memories.test.ts +312 -0
  198. package/src/resources/extensions/gsd/tests/db-writer.test.ts +13 -8
  199. package/src/resources/extensions/gsd/tests/decisions-projection-from-memories.test.ts +453 -0
  200. package/src/resources/extensions/gsd/tests/decisions-stop-table-writes.test.ts +348 -0
  201. package/src/resources/extensions/gsd/tests/evidence-cross-ref.test.ts +38 -0
  202. package/src/resources/extensions/gsd/tests/freeform-decisions.test.ts +8 -4
  203. package/src/resources/extensions/gsd/tests/gsd-tools.test.ts +11 -7
  204. package/src/resources/extensions/gsd/tests/integration/doctor-git.test.ts +44 -0
  205. package/src/resources/extensions/gsd/tests/integration/integration-lifecycle.test.ts +13 -5
  206. package/src/resources/extensions/gsd/tests/integration/migrate-command.test.ts +48 -3
  207. package/src/resources/extensions/gsd/tests/knowledge-backfill-projection.test.ts +323 -0
  208. package/src/resources/extensions/gsd/tests/knowledge-capture.test.ts +242 -0
  209. package/src/resources/extensions/gsd/tests/knowledge.test.ts +47 -2
  210. package/src/resources/extensions/gsd/tests/load-knowledge-block-rules-only.test.ts +209 -0
  211. package/src/resources/extensions/gsd/tests/memory-consolidation-scanner.test.ts +316 -0
  212. package/src/resources/extensions/gsd/tests/migrate-transformer.test.ts +5 -1
  213. package/src/resources/extensions/gsd/tests/migrate-writer-integration.test.ts +6 -1
  214. package/src/resources/extensions/gsd/tests/plan-milestone-sketch-render.test.ts +157 -0
  215. package/src/resources/extensions/gsd/tests/post-exec-retry-bypass.test.ts +79 -1
  216. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +8 -0
  217. package/src/resources/extensions/gsd/tests/provider-switch-observer.test.ts +252 -0
  218. package/src/resources/extensions/gsd/tests/session-start-footer.test.ts +16 -4
  219. package/src/resources/extensions/gsd/tests/state-corruption-2945.test.ts +6 -0
  220. package/src/resources/extensions/gsd/tests/unit-context-manifest.test.ts +21 -0
  221. package/src/resources/extensions/gsd/tests/verification-verdict.test.ts +78 -0
  222. package/src/resources/extensions/gsd/tests/worktree-lifecycle.test.ts +25 -0
  223. package/src/resources/extensions/gsd/tests/write-gate-planning-unit.test.ts +16 -0
  224. package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +135 -0
  225. package/src/resources/extensions/gsd/unit-context-manifest.ts +35 -2
  226. package/src/resources/extensions/gsd/verification-verdict.ts +47 -0
  227. package/src/resources/extensions/gsd/workflow-logger.ts +4 -0
  228. package/src/resources/extensions/gsd/worktree-lifecycle.ts +20 -2
  229. package/src/resources/extensions/subagent/index.ts +567 -103
  230. package/src/resources/extensions/subagent/launch.ts +131 -0
  231. package/src/resources/extensions/subagent/run-store.ts +218 -0
  232. package/src/resources/extensions/subagent/tests/launch.test.ts +115 -0
  233. package/src/resources/extensions/subagent/tests/run-store.test.ts +111 -0
  234. package/src/resources/extensions/visual-brief/artifact-policy.ts +41 -0
  235. package/src/resources/extensions/visual-brief/extension-manifest.json +8 -0
  236. package/src/resources/extensions/visual-brief/index.ts +8 -0
  237. package/src/resources/extensions/visual-brief/page-contract.ts +134 -0
  238. package/src/resources/extensions/visual-brief/prompts.ts +147 -0
  239. package/src/resources/extensions/visual-brief/tests/visual-brief.test.ts +172 -0
  240. package/dist/web/standalone/.next/static/chunks/2556.0527fea66e123b7f.js +0 -1
  241. package/dist/web/standalone/.next/static/css/54ec2745c1da488b.css +0 -1
  242. /package/dist/web/standalone/.next/static/{rIkMv4YSNlfSeqmGqWVns → Qgr2B_MRhPxC0z8fwv4vT}/_buildManifest.js +0 -0
  243. /package/dist/web/standalone/.next/static/{rIkMv4YSNlfSeqmGqWVns → Qgr2B_MRhPxC0z8fwv4vT}/_ssgManifest.js +0 -0
@@ -235,14 +235,63 @@ export async function nextDecisionId(): Promise<string> {
235
235
  }
236
236
  }
237
237
 
238
- /** Synchronous variant for use inside db.transaction(). */
239
- function nextDecisionIdSync(adapter: ReturnType<typeof import('./gsd-db.js')._getAdapter>): string {
238
+ /**
239
+ * ADR-013 Stage 3: compute the next `D###` identifier across both the legacy
240
+ * `decisions` table AND the `memories.structured_fields.sourceDecisionId`
241
+ * surface. Returns the max numeric suffix from either side + 1, three-digit
242
+ * padded.
243
+ *
244
+ * Used by `saveDecisionToDb` once writes to the `decisions` table stop —
245
+ * new decisions live only in memories, but historical IDs sit in both
246
+ * places during the cutover bake. The cross-surface max keeps IDs
247
+ * monotonic and avoids collisions on the next save.
248
+ */
249
+ function nextDecisionIdAcrossSurfaces(
250
+ adapter: ReturnType<typeof import('./gsd-db.js')._getAdapter>,
251
+ ): string {
240
252
  if (!adapter) return 'D001';
241
- const row = adapter
242
- .prepare('SELECT MAX(CAST(SUBSTR(id, 2) AS INTEGER)) as max_num FROM decisions')
243
- .get();
244
- const maxNum = row ? (row['max_num'] as number | null) : null;
245
- if (maxNum == null || isNaN(maxNum)) return 'D001';
253
+
254
+ let maxNum = 0;
255
+
256
+ // Legacy table best-effort.
257
+ try {
258
+ const row = adapter
259
+ .prepare('SELECT MAX(CAST(SUBSTR(id, 2) AS INTEGER)) as max_num FROM decisions')
260
+ .get();
261
+ const candidate = row ? (row['max_num'] as number | null) : null;
262
+ if (typeof candidate === 'number' && Number.isFinite(candidate)) {
263
+ maxNum = Math.max(maxNum, candidate);
264
+ }
265
+ } catch {
266
+ // fall through to memory-only
267
+ }
268
+
269
+ // Memory surface: scan structuredFields.sourceDecisionId for D### values.
270
+ // SQLite LIKE on the JSON-stringified field is sufficient — rows tagged
271
+ // with sourceDecisionId are bounded by the decisions count.
272
+ try {
273
+ const rows = adapter
274
+ .prepare(
275
+ "SELECT structured_fields FROM memories WHERE structured_fields LIKE '%\"sourceDecisionId\":\"D%'",
276
+ )
277
+ .all() as Array<{ structured_fields: string | null }>;
278
+ for (const row of rows) {
279
+ if (!row.structured_fields) continue;
280
+ let sf: Record<string, unknown>;
281
+ try {
282
+ sf = JSON.parse(row.structured_fields) as Record<string, unknown>;
283
+ } catch {
284
+ continue;
285
+ }
286
+ const sourceId = sf['sourceDecisionId'];
287
+ if (typeof sourceId !== 'string' || !sourceId.startsWith('D')) continue;
288
+ const num = parseInt(sourceId.slice(1), 10);
289
+ if (Number.isFinite(num) && num > maxNum) maxNum = num;
290
+ }
291
+ } catch {
292
+ // best-effort
293
+ }
294
+
246
295
  const next = maxNum + 1;
247
296
  return `D${String(next).padStart(3, '0')}`;
248
297
  }
@@ -416,6 +465,16 @@ export interface SaveDecisionFields {
416
465
  source?: string;
417
466
  }
418
467
 
468
+ type NormalizedSaveDecisionFields = Omit<
469
+ SaveDecisionFields,
470
+ 'when_context' | 'revisable' | 'made_by' | 'source'
471
+ > & {
472
+ when_context: string;
473
+ revisable: string;
474
+ made_by: NonNullable<SaveDecisionFields['made_by']>;
475
+ source: string;
476
+ };
477
+
419
478
  /**
420
479
  * Save a new decision to DB and regenerate DECISIONS.md.
421
480
  * Auto-assigns the next ID via nextDecisionId().
@@ -445,44 +504,59 @@ export async function saveDecisionToDb(
445
504
 
446
505
  try {
447
506
  const db = await import('./gsd-db.js');
448
-
449
507
  const adapter = db._getAdapter();
508
+ const normalized: NormalizedSaveDecisionFields = {
509
+ ...fields,
510
+ when_context: fields.when_context ?? '',
511
+ revisable: fields.revisable ?? 'Yes',
512
+ made_by: fields.made_by ?? 'agent',
513
+ source: fields.source ?? 'discussion',
514
+ };
450
515
 
451
- const id = db.transaction(() => {
452
- const nextId = nextDecisionIdSync(adapter);
453
- db.upsertDecision({
454
- id: nextId,
455
- when_context: fields.when_context ?? '',
456
- scope: fields.scope,
457
- decision: fields.decision,
458
- choice: fields.choice,
459
- rationale: fields.rationale,
460
- revisable: fields.revisable ?? 'Yes',
461
- made_by: fields.made_by ?? 'agent',
462
- source: fields.source ?? 'discussion',
463
- superseded_by: null,
516
+ // ADR-013 Stage 3 (destructive): writes to the `decisions` table stop
517
+ // here. New decisions live only in the `memories` table; the projection
518
+ // regen below sources from memories (Stage 2a). The decisions table
519
+ // remains for backwards-compat reads (queryDecisions, md-importer,
520
+ // commands-inspect, workflow-manifest) until #5756 drops it.
521
+ //
522
+ // Reversal: a code revert of this change restores the upsertDecision
523
+ // call. Memory rows written between merge and revert stay durable; the
524
+ // legacy table simply doesn't grow during the cutover window.
525
+ const id = nextDecisionIdAcrossSurfaces(adapter);
526
+
527
+ // The mirror-to-memories write is what persists the new decision. Must
528
+ // run before the projection regen — the regen sources from memories
529
+ // (Stage 2a) and would otherwise miss the just-saved decision. Pass
530
+ // the normalized field set so defaults (revisable, made_by, source)
531
+ // are recorded on the memory row.
532
+ await mirrorDecisionToMemory(id, normalized);
533
+
534
+ // Fetch all decisions (including superseded for the full register).
535
+ // ADR-013 Stage 2a: source from the `memories` table. The Phase 5
536
+ // dual-write keeps memories in sync with each decision save; the backfill
537
+ // (memory-backfill.ts) absorbs the historical chain and drift-heals
538
+ // superseded_by on every session start.
539
+ const { getAllDecisionsFromMemories } = await import('./context-store.js');
540
+ let allDecisions: Decision[] = getAllDecisionsFromMemories();
541
+ if (!allDecisions.some(d => d.id === id)) {
542
+ logWarning('projection', 'just-saved decision missing from memories after mirror; injecting fallback for projection', {
543
+ fn: 'saveDecisionToDb',
544
+ decisionId: id,
464
545
  });
465
-
466
-
467
- return nextId;
468
- });
469
-
470
- // Fetch all decisions (including superseded for the full register)
471
- let allDecisions: Decision[] = [];
472
- if (adapter) {
473
- const rows = adapter.prepare('SELECT * FROM decisions ORDER BY seq').all();
474
- allDecisions = rows.map(row => ({
475
- seq: row['seq'] as number,
476
- id: row['id'] as string,
477
- when_context: row['when_context'] as string,
478
- scope: row['scope'] as string,
479
- decision: row['decision'] as string,
480
- choice: row['choice'] as string,
481
- rationale: row['rationale'] as string,
482
- revisable: row['revisable'] as string,
483
- made_by: (row['made_by'] as string as import('./types.js').DecisionMadeBy) ?? 'agent',
484
- superseded_by: (row['superseded_by'] as string) ?? null,
485
- }));
546
+ const nextSeq = allDecisions.reduce((max, d) => Math.max(max, d.seq ?? 0), 0) + 1;
547
+ const fallback: Decision = {
548
+ seq: nextSeq,
549
+ id,
550
+ when_context: normalized.when_context,
551
+ scope: normalized.scope,
552
+ decision: normalized.decision,
553
+ choice: normalized.choice,
554
+ rationale: normalized.rationale,
555
+ revisable: normalized.revisable,
556
+ made_by: normalized.made_by,
557
+ superseded_by: null,
558
+ };
559
+ allDecisions = [...allDecisions, fallback];
486
560
  }
487
561
 
488
562
  const filePath = resolveGsdRootFile(basePath, 'DECISIONS');
@@ -538,48 +612,6 @@ export async function saveDecisionToDb(
538
612
  clearPathCache();
539
613
  clearParseCache();
540
614
 
541
- // ADR-013 dual-write: keep the memory store in sync with every decision
542
- // persisted via the legacy gsd_save_decision path. Without this, prompts
543
- // that still call gsd_save_decision (discuss.md, plan-milestone.md,
544
- // plan-slice.md, et al.) would create decisions rows invisible to
545
- // memory_query and loadMemoryBlock.
546
- // Best-effort — never throw, never roll back the decision on failure.
547
- try {
548
- const { createMemory } = await import('./memory-store.js');
549
- const decisionText = (fields.decision ?? '').trim();
550
- const choiceText = (fields.choice ?? '').trim();
551
- const rationaleText = (fields.rationale ?? '').trim();
552
- const contentParts: string[] = [];
553
- if (decisionText) contentParts.push(decisionText);
554
- if (choiceText) contentParts.push(`Chose: ${choiceText}.`);
555
- if (rationaleText) contentParts.push(`Rationale: ${rationaleText}.`);
556
- const content = contentParts.join(' ').slice(0, 600);
557
- if (content) {
558
- createMemory({
559
- category: 'architecture',
560
- content,
561
- scope: fields.scope || 'project',
562
- confidence: 0.85,
563
- structuredFields: {
564
- sourceDecisionId: id,
565
- when_context: fields.when_context ?? '',
566
- scope: fields.scope,
567
- decision: fields.decision,
568
- choice: fields.choice,
569
- rationale: fields.rationale,
570
- made_by: fields.made_by ?? 'agent',
571
- revisable: fields.revisable ?? '',
572
- },
573
- });
574
- }
575
- } catch (mirrorErr) {
576
- logError('manifest', 'memory-store mirror write failed (non-fatal)', {
577
- fn: 'saveDecisionToDb',
578
- decisionId: id,
579
- error: String((mirrorErr as Error).message),
580
- });
581
- }
582
-
583
615
  return { id };
584
616
  } catch (err) {
585
617
  logError('manifest', 'saveDecisionToDb failed', { fn: 'saveDecisionToDb', error: String((err as Error).message) });
@@ -589,6 +621,57 @@ export async function saveDecisionToDb(
589
621
  }
590
622
  }
591
623
 
624
+ /**
625
+ * ADR-013 dual-write — mirror a freshly-saved decision into the `memories`
626
+ * table so the memory store remains the single source of truth for the
627
+ * DECISIONS.md projection (Stage 2a) and for prompt-inline reads (Stage 1).
628
+ *
629
+ * Best-effort mirror: logs failures without throwing to avoid blocking saves.
630
+ * Caller invokes this AFTER the decisions-table write completes and
631
+ * BEFORE the projection regen — the regen sources from memories and would
632
+ * otherwise miss the just-saved decision.
633
+ */
634
+ async function mirrorDecisionToMemory(
635
+ id: string,
636
+ normalizedFields: NormalizedSaveDecisionFields,
637
+ ): Promise<boolean> {
638
+ try {
639
+ const { createMemory } = await import('./memory-store.js');
640
+ const { synthesizeDecisionMemoryContent } = await import('./memory-backfill.js');
641
+ const content = synthesizeDecisionMemoryContent(normalizedFields);
642
+ if (!content) return false;
643
+
644
+ createMemory({
645
+ category: 'architecture',
646
+ content,
647
+ scope: normalizedFields.scope || 'project',
648
+ confidence: 0.85,
649
+ structuredFields: {
650
+ sourceDecisionId: id,
651
+ when_context: normalizedFields.when_context,
652
+ scope: normalizedFields.scope,
653
+ decision: normalizedFields.decision,
654
+ choice: normalizedFields.choice,
655
+ rationale: normalizedFields.rationale,
656
+ made_by: normalizedFields.made_by,
657
+ revisable: normalizedFields.revisable,
658
+ // New decisions are always written as active; md-importer can later
659
+ // set superseded_by on the source decision row, and the backfill's
660
+ // drift auto-heal pass propagates that update to this memory.
661
+ superseded_by: null,
662
+ },
663
+ });
664
+ return true;
665
+ } catch (mirrorErr) {
666
+ logError('manifest', 'memory-store mirror write failed', {
667
+ fn: 'saveDecisionToDb',
668
+ decisionId: id,
669
+ error: String((mirrorErr as Error).message),
670
+ });
671
+ return false;
672
+ }
673
+ }
674
+
592
675
  /**
593
676
  * Extract a milestone/slice reference from a deferral decision.
594
677
  *
@@ -100,7 +100,7 @@ Setting `prefer_skills: []` does **not** disable skill discovery — it just mea
100
100
 
101
101
  - `skill_rules`: situational rules with a human-readable `when` trigger and one or more of `use`, `prefer`, or `avoid`.
102
102
 
103
- - `custom_instructions`: extra durable instructions related to skill use. For operational project knowledge (recurring rules, gotchas, patterns), use `.gsd/KNOWLEDGE.md` instead it's injected into every agent prompt automatically and agents can append to it during execution.
103
+ - `custom_instructions`: extra durable instructions related to skill use. For operational project knowledge, use `.gsd/KNOWLEDGE.md` instead. Rules are file-canonical; patterns and lessons are persisted to the `memories` table and projected back into `KNOWLEDGE.md` on the next session start.
104
104
 
105
105
  - `language`: preferred response language for all GSD interactions. Accepts any language name or code — `"Chinese"`, `"zh"`, `"German"`, `"de"`, `"日本語"`, etc. When set, GSD injects "Always respond in \<language\>" into every agent's system prompt, including after `/clear`. Quickest way to set it: `/gsd language <name>`. To clear: `/gsd language off`.
106
106
 
@@ -1,3 +1,5 @@
1
+ // GSD-2 doctor git health checks
2
+ import { spawnSync } from "node:child_process";
1
3
  import { existsSync, readdirSync, realpathSync, rmSync, statSync } from "node:fs";
2
4
  import { join, sep } from "node:path";
3
5
 
@@ -52,6 +54,26 @@ function isSameOrNestedPath(candidate: string, container: string): boolean {
52
54
  normalizedCandidate.startsWith(`${normalizedContainer}/`);
53
55
  }
54
56
 
57
+ function getSnapshotDiffCheckFailure(basePath: string): string | null {
58
+ const failures: string[] = [];
59
+
60
+ for (const args of [["--cached"], []]) {
61
+ const result = spawnSync("git", ["diff", "--check", ...args], {
62
+ cwd: basePath,
63
+ encoding: "utf-8",
64
+ });
65
+ if (result.status === 0) continue;
66
+
67
+ const output = [result.stdout, result.stderr, result.error?.message]
68
+ .filter(Boolean)
69
+ .join("\n")
70
+ .trim();
71
+ failures.push(output || `git diff --check ${args.join(" ")} failed`);
72
+ }
73
+
74
+ return failures.length > 0 ? failures.join("\n") : null;
75
+ }
76
+
55
77
  async function isCompletedMilestoneTerminal(basePath: string, milestoneId: string): Promise<boolean> {
56
78
  const summaryPath = resolveMilestoneFile(basePath, milestoneId, "SUMMARY");
57
79
  if (!summaryPath) return false;
@@ -411,15 +433,31 @@ export async function checkGitHealth(
411
433
  fixable: true,
412
434
  });
413
435
 
436
+ const diffCheckFailure = getSnapshotDiffCheckFailure(basePath);
437
+ if (diffCheckFailure) {
438
+ issues.push({
439
+ severity: "error",
440
+ code: "conflict_markers_in_tracked_files",
441
+ scope: "project",
442
+ unitId: "project",
443
+ message: `Cannot create gsd snapshot: tracked changes contain conflict markers or whitespace errors. Resolve conflicts manually before auto-mode can proceed.\n${diffCheckFailure}`,
444
+ fixable: false,
445
+ });
446
+ }
447
+
414
448
  if (shouldFix("stale_uncommitted_changes")) {
415
449
  try {
416
- nativeAddTracked(basePath);
417
- const commitMsg = `gsd snapshot: uncommitted changes after ${mins}m inactivity`;
418
- const result = nativeCommit(basePath, commitMsg);
419
- if (result) {
420
- fixesApplied.push(`created gsd snapshot after ${mins}m of uncommitted changes`);
450
+ if (diffCheckFailure) {
451
+ fixesApplied.push("gsd snapshot skipped - conflict markers detected in tracked files");
421
452
  } else {
422
- fixesApplied.push("gsd snapshot skipped — nothing to commit after staging tracked files");
453
+ nativeAddTracked(basePath);
454
+ const commitMsg = `gsd snapshot: uncommitted changes after ${mins}m inactivity`;
455
+ const result = nativeCommit(basePath, commitMsg);
456
+ if (result) {
457
+ fixesApplied.push(`created gsd snapshot after ${mins}m of uncommitted changes`);
458
+ } else {
459
+ fixesApplied.push("gsd snapshot skipped — nothing to commit after staging tracked files");
460
+ }
423
461
  }
424
462
  } catch {
425
463
  fixesApplied.push("failed to create gsd snapshot commit");
@@ -1,3 +1,4 @@
1
+ // GSD-2 doctor report types
1
2
  export type DoctorSeverity = "info" | "warning" | "error";
2
3
  export type DoctorIssueCode =
3
4
  | "invalid_preferences"
@@ -65,6 +66,7 @@ export type DoctorIssueCode =
65
66
  | "worktree_unpushed"
66
67
  // Stale commit safety check
67
68
  | "stale_uncommitted_changes"
69
+ | "conflict_markers_in_tracked_files"
68
70
  // Snapshot ref bloat
69
71
  | "snapshot_ref_bloat"
70
72
  // Runtime data integrity
@@ -0,0 +1,164 @@
1
+ // GSD2 — KNOWLEDGE.md -> memories backfill (ADR-013 Stage 2b).
2
+ //
3
+ // Idempotent migration of `.gsd/KNOWLEDGE.md` Patterns and Lessons rows into
4
+ // the `memories` table. Patterns become memories with `category: "pattern"`;
5
+ // Lessons become memories with `category: "gotcha"` (mirroring the ADR-013
6
+ // line 38 contract). Rules (K###) are NOT migrated — they remain manually
7
+ // maintained in `KNOWLEDGE.md` per ADR-013 line 39.
8
+ //
9
+ // Idempotency is enforced by tagging each backfilled memory with
10
+ // `structured_fields.sourceKnowledgeId = "<P|L>NNN"`. The
11
+ // memory-consolidation-scanner (PR #5765) checks for the same marker.
12
+ //
13
+ // Triggered opportunistically by `buildBeforeAgentStartResult` so the cost
14
+ // only ever fires once per project. Costs O(N) inserts on first run where N
15
+ // is the row count; subsequent runs are an O(N) lookup that finds existing
16
+ // markers and exits.
17
+
18
+ import { _getAdapter, isDbAvailable } from "./gsd-db.js";
19
+ import { createMemory } from "./memory-store.js";
20
+ import { parseKnowledgeRows, readKnowledgeMd, type KnowledgeRow } from "./knowledge-parser.js";
21
+ import { logWarning } from "./workflow-logger.js";
22
+
23
+ interface SynthesizedRow {
24
+ table: "patterns" | "lessons";
25
+ id: string;
26
+ category: "pattern" | "gotcha";
27
+ content: string;
28
+ scope: string;
29
+ structuredFields: Record<string, unknown>;
30
+ }
31
+
32
+ /**
33
+ * Backfill KNOWLEDGE.md Patterns + Lessons rows into the memories table.
34
+ *
35
+ * - Idempotent (per-row): each migrated memory carries
36
+ * `structured_fields.sourceKnowledgeId = "<P|L>NNN"`. Rows whose ID is
37
+ * already present in the memory store are skipped.
38
+ * - Best-effort: never throws. Logs and returns 0 on failure so a broken
39
+ * backfill cannot block agent startup.
40
+ * - Rules (K###) are intentionally skipped — they remain manually maintained
41
+ * in `KNOWLEDGE.md` per ADR-013.
42
+ *
43
+ * Returns the number of memories written (0 when there's nothing to migrate
44
+ * or when the file is absent).
45
+ */
46
+ export function backfillKnowledgeToMemories(basePath: string): number {
47
+ if (!isDbAvailable()) return 0;
48
+ const adapter = _getAdapter();
49
+ if (!adapter) return 0;
50
+
51
+ try {
52
+ const content = readKnowledgeMd(basePath);
53
+ if (!content.trim()) return 0;
54
+
55
+ const rows = parseKnowledgeRows(content);
56
+ if (rows.length === 0) return 0;
57
+
58
+ const checkExisting = adapter.prepare(
59
+ "SELECT 1 FROM memories WHERE structured_fields LIKE :pattern LIMIT 1",
60
+ );
61
+
62
+ let written = 0;
63
+ for (const row of rows) {
64
+ const synth = synthesize(row);
65
+ if (!synth) continue;
66
+
67
+ // Pattern is anchored on both sides of the value to avoid prefix
68
+ // collisions (e.g. P1 vs P10).
69
+ const matchPattern = `%"sourceKnowledgeId":"${synth.id}"%`;
70
+ if (checkExisting.get({ ":pattern": matchPattern })) continue;
71
+
72
+ const id = createMemory({
73
+ category: synth.category,
74
+ content: synth.content,
75
+ scope: synth.scope,
76
+ confidence: 0.85,
77
+ structuredFields: synth.structuredFields,
78
+ });
79
+ if (id) written += 1;
80
+ }
81
+
82
+ return written;
83
+ } catch (e) {
84
+ logWarning(
85
+ "memory-backfill",
86
+ `KNOWLEDGE.md -> memories backfill failed: ${(e as Error).message}`,
87
+ );
88
+ return 0;
89
+ }
90
+ }
91
+
92
+ /**
93
+ * Convert a parsed KNOWLEDGE.md row into the memory payload we insert.
94
+ * Returns `null` for Rules (K###) which are not migrated, or for rows whose
95
+ * primary content cell is empty (defensive against malformed manual edits).
96
+ */
97
+ function synthesize(row: KnowledgeRow): SynthesizedRow | null {
98
+ if (row.table === "rules") return null;
99
+
100
+ if (row.table === "patterns") {
101
+ // Cells: [P###, Pattern, Where, Notes]
102
+ const [, pattern, where, notes] = row.cells;
103
+ const cleaned = (pattern ?? "").trim();
104
+ if (!cleaned) return null;
105
+
106
+ const contentParts: string[] = [cleaned];
107
+ if (where && where.trim() && where.trim() !== "—") {
108
+ contentParts.push(`Where: ${where.trim()}.`);
109
+ }
110
+ if (notes && notes.trim() && notes.trim() !== "—") {
111
+ contentParts.push(`Notes: ${notes.trim()}.`);
112
+ }
113
+
114
+ return {
115
+ table: "patterns",
116
+ id: row.id,
117
+ category: "pattern",
118
+ content: trim(contentParts.join(" "), 600),
119
+ scope: (where ?? "").trim() || "project",
120
+ structuredFields: {
121
+ sourceKnowledgeId: row.id,
122
+ sourceKnowledgeTable: "patterns",
123
+ pattern: cleaned,
124
+ where: (where ?? "").trim(),
125
+ notes: (notes ?? "").trim(),
126
+ },
127
+ };
128
+ }
129
+
130
+ // table === "lessons"
131
+ // Cells: [L###, What Happened, Root Cause, Fix, Scope]
132
+ const [, whatHappened, rootCause, fix, scope] = row.cells;
133
+ const cleanedWhat = (whatHappened ?? "").trim();
134
+ if (!cleanedWhat) return null;
135
+
136
+ const contentParts: string[] = [cleanedWhat];
137
+ if (rootCause && rootCause.trim() && rootCause.trim() !== "—") {
138
+ contentParts.push(`Root cause: ${rootCause.trim()}.`);
139
+ }
140
+ if (fix && fix.trim() && fix.trim() !== "—") {
141
+ contentParts.push(`Fix: ${fix.trim()}.`);
142
+ }
143
+
144
+ return {
145
+ table: "lessons",
146
+ id: row.id,
147
+ category: "gotcha",
148
+ content: trim(contentParts.join(" "), 600),
149
+ scope: (scope ?? "").trim() || "project",
150
+ structuredFields: {
151
+ sourceKnowledgeId: row.id,
152
+ sourceKnowledgeTable: "lessons",
153
+ whatHappened: cleanedWhat,
154
+ rootCause: (rootCause ?? "").trim(),
155
+ fix: (fix ?? "").trim(),
156
+ scopeText: (scope ?? "").trim(),
157
+ },
158
+ };
159
+ }
160
+
161
+ function trim(value: string, max: number): string {
162
+ const cleaned = value.replace(/\s+/g, " ").trim();
163
+ return cleaned.length > max ? cleaned.slice(0, max - 1) + "…" : cleaned;
164
+ }