gsd-pi 2.45.0-dev.fdcf73c → 2.46.0-dev.5c2b8ed

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 (183) hide show
  1. package/dist/resources/extensions/gsd/auto/phases.js +14 -35
  2. package/dist/resources/extensions/gsd/auto/session.js +0 -11
  3. package/dist/resources/extensions/gsd/auto-artifact-paths.js +112 -0
  4. package/dist/resources/extensions/gsd/auto-post-unit.js +25 -96
  5. package/dist/resources/extensions/gsd/auto-start.js +2 -3
  6. package/dist/resources/extensions/gsd/auto.js +8 -52
  7. package/dist/resources/extensions/gsd/bootstrap/register-hooks.js +18 -0
  8. package/dist/resources/extensions/gsd/commands/context.js +0 -4
  9. package/dist/resources/extensions/gsd/commands/handlers/parallel.js +1 -1
  10. package/dist/resources/extensions/gsd/crash-recovery.js +2 -4
  11. package/dist/resources/extensions/gsd/dashboard-overlay.js +0 -44
  12. package/dist/resources/extensions/gsd/doctor-checks.js +166 -1
  13. package/dist/resources/extensions/gsd/doctor.js +3 -1
  14. package/dist/resources/extensions/gsd/gsd-db.js +11 -2
  15. package/dist/resources/extensions/gsd/guided-flow.js +1 -2
  16. package/dist/resources/extensions/gsd/parallel-merge.js +1 -1
  17. package/dist/resources/extensions/gsd/parallel-orchestrator.js +5 -18
  18. package/dist/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
  19. package/dist/resources/extensions/gsd/prompts/complete-slice.md +10 -23
  20. package/dist/resources/extensions/gsd/prompts/discuss.md +2 -2
  21. package/dist/resources/extensions/gsd/prompts/execute-task.md +5 -15
  22. package/dist/resources/extensions/gsd/prompts/guided-discuss-milestone.md +1 -1
  23. package/dist/resources/extensions/gsd/prompts/guided-discuss-slice.md +1 -1
  24. package/dist/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -1
  25. package/dist/resources/extensions/gsd/prompts/guided-research-slice.md +1 -1
  26. package/dist/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
  27. package/dist/resources/extensions/gsd/prompts/plan-slice.md +4 -2
  28. package/dist/resources/extensions/gsd/prompts/queue.md +2 -2
  29. package/dist/resources/extensions/gsd/prompts/quick-task.md +2 -0
  30. package/dist/resources/extensions/gsd/prompts/reactive-execute.md +1 -1
  31. package/dist/resources/extensions/gsd/prompts/research-slice.md +3 -3
  32. package/dist/resources/extensions/gsd/prompts/rethink.md +7 -2
  33. package/dist/resources/extensions/gsd/prompts/system.md +1 -1
  34. package/dist/resources/extensions/gsd/session-lock.js +1 -3
  35. package/dist/resources/extensions/gsd/state.js +7 -0
  36. package/dist/resources/extensions/gsd/sync-lock.js +89 -0
  37. package/dist/resources/extensions/gsd/tools/complete-milestone.js +58 -12
  38. package/dist/resources/extensions/gsd/tools/complete-slice.js +56 -11
  39. package/dist/resources/extensions/gsd/tools/complete-task.js +50 -2
  40. package/dist/resources/extensions/gsd/tools/plan-milestone.js +37 -1
  41. package/dist/resources/extensions/gsd/tools/plan-slice.js +30 -1
  42. package/dist/resources/extensions/gsd/tools/plan-task.js +27 -1
  43. package/dist/resources/extensions/gsd/tools/reassess-roadmap.js +32 -2
  44. package/dist/resources/extensions/gsd/tools/reopen-slice.js +86 -0
  45. package/dist/resources/extensions/gsd/tools/reopen-task.js +90 -0
  46. package/dist/resources/extensions/gsd/tools/replan-slice.js +32 -2
  47. package/dist/resources/extensions/gsd/unit-ownership.js +85 -0
  48. package/dist/resources/extensions/gsd/workflow-events.js +102 -0
  49. package/dist/resources/extensions/gsd/workflow-logger.js +56 -1
  50. package/dist/resources/extensions/gsd/workflow-manifest.js +244 -0
  51. package/dist/resources/extensions/gsd/workflow-migration.js +280 -0
  52. package/dist/resources/extensions/gsd/workflow-projections.js +373 -0
  53. package/dist/resources/extensions/gsd/workflow-reconcile.js +411 -0
  54. package/dist/resources/extensions/gsd/write-intercept.js +84 -0
  55. package/dist/resources/extensions/remote-questions/config.js +42 -0
  56. package/dist/web/standalone/.next/BUILD_ID +1 -1
  57. package/dist/web/standalone/.next/app-path-routes-manifest.json +19 -19
  58. package/dist/web/standalone/.next/build-manifest.json +2 -2
  59. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  60. package/dist/web/standalone/.next/server/app/_global-error.html +2 -2
  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.html +1 -1
  69. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  70. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  71. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  72. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  73. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  74. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  75. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  76. package/dist/web/standalone/.next/server/app/index.html +1 -1
  77. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  78. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  79. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  80. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  81. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  82. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  83. package/dist/web/standalone/.next/server/app-paths-manifest.json +19 -19
  84. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  85. package/dist/web/standalone/.next/server/pages/500.html +2 -2
  86. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  87. package/package.json +1 -1
  88. package/packages/pi-coding-agent/package.json +1 -1
  89. package/pkg/package.json +1 -1
  90. package/src/resources/extensions/gsd/auto/loop-deps.ts +0 -19
  91. package/src/resources/extensions/gsd/auto/phases.ts +11 -35
  92. package/src/resources/extensions/gsd/auto/session.ts +0 -18
  93. package/src/resources/extensions/gsd/auto-artifact-paths.ts +131 -0
  94. package/src/resources/extensions/gsd/auto-dashboard.ts +0 -1
  95. package/src/resources/extensions/gsd/auto-post-unit.ts +25 -106
  96. package/src/resources/extensions/gsd/auto-start.ts +1 -3
  97. package/src/resources/extensions/gsd/auto.ts +4 -80
  98. package/src/resources/extensions/gsd/bootstrap/register-hooks.ts +22 -0
  99. package/src/resources/extensions/gsd/commands/context.ts +0 -5
  100. package/src/resources/extensions/gsd/commands/handlers/parallel.ts +1 -1
  101. package/src/resources/extensions/gsd/crash-recovery.ts +1 -5
  102. package/src/resources/extensions/gsd/dashboard-overlay.ts +0 -50
  103. package/src/resources/extensions/gsd/doctor-checks.ts +179 -1
  104. package/src/resources/extensions/gsd/doctor-types.ts +7 -1
  105. package/src/resources/extensions/gsd/doctor.ts +4 -1
  106. package/src/resources/extensions/gsd/gsd-db.ts +11 -2
  107. package/src/resources/extensions/gsd/guided-flow.ts +1 -2
  108. package/src/resources/extensions/gsd/parallel-merge.ts +1 -1
  109. package/src/resources/extensions/gsd/parallel-orchestrator.ts +5 -21
  110. package/src/resources/extensions/gsd/prompts/complete-milestone.md +1 -1
  111. package/src/resources/extensions/gsd/prompts/complete-slice.md +10 -23
  112. package/src/resources/extensions/gsd/prompts/discuss.md +2 -2
  113. package/src/resources/extensions/gsd/prompts/execute-task.md +5 -15
  114. package/src/resources/extensions/gsd/prompts/guided-discuss-milestone.md +1 -1
  115. package/src/resources/extensions/gsd/prompts/guided-discuss-slice.md +1 -1
  116. package/src/resources/extensions/gsd/prompts/guided-plan-slice.md +1 -1
  117. package/src/resources/extensions/gsd/prompts/guided-research-slice.md +1 -1
  118. package/src/resources/extensions/gsd/prompts/plan-milestone.md +1 -1
  119. package/src/resources/extensions/gsd/prompts/plan-slice.md +4 -2
  120. package/src/resources/extensions/gsd/prompts/queue.md +2 -2
  121. package/src/resources/extensions/gsd/prompts/quick-task.md +2 -0
  122. package/src/resources/extensions/gsd/prompts/reactive-execute.md +1 -1
  123. package/src/resources/extensions/gsd/prompts/research-slice.md +3 -3
  124. package/src/resources/extensions/gsd/prompts/rethink.md +7 -2
  125. package/src/resources/extensions/gsd/prompts/system.md +1 -1
  126. package/src/resources/extensions/gsd/session-lock.ts +0 -4
  127. package/src/resources/extensions/gsd/state.ts +8 -0
  128. package/src/resources/extensions/gsd/sync-lock.ts +94 -0
  129. package/src/resources/extensions/gsd/tests/auto-lock-creation.test.ts +5 -13
  130. package/src/resources/extensions/gsd/tests/auto-loop.test.ts +6 -10
  131. package/src/resources/extensions/gsd/tests/complete-slice.test.ts +264 -228
  132. package/src/resources/extensions/gsd/tests/complete-task.test.ts +317 -250
  133. package/src/resources/extensions/gsd/tests/crash-recovery.test.ts +2 -8
  134. package/src/resources/extensions/gsd/tests/custom-engine-loop-integration.test.ts +0 -3
  135. package/src/resources/extensions/gsd/tests/gsd-db.test.ts +1 -1
  136. package/src/resources/extensions/gsd/tests/idle-recovery.test.ts +1 -1
  137. package/src/resources/extensions/gsd/tests/integration-proof.test.ts +15 -24
  138. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +0 -3
  139. package/src/resources/extensions/gsd/tests/md-importer.test.ts +1 -1
  140. package/src/resources/extensions/gsd/tests/memory-store.test.ts +2 -2
  141. package/src/resources/extensions/gsd/tests/milestone-transition-state-rebuild.test.ts +8 -9
  142. package/src/resources/extensions/gsd/tests/parallel-budget-atomicity.test.ts +0 -1
  143. package/src/resources/extensions/gsd/tests/parallel-crash-recovery.test.ts +0 -7
  144. package/src/resources/extensions/gsd/tests/parallel-merge.test.ts +7 -8
  145. package/src/resources/extensions/gsd/tests/parallel-orchestration.test.ts +20 -24
  146. package/src/resources/extensions/gsd/tests/parallel-worker-monitoring.test.ts +0 -2
  147. package/src/resources/extensions/gsd/tests/plan-milestone.test.ts +9 -6
  148. package/src/resources/extensions/gsd/tests/post-mutation-hook.test.ts +171 -0
  149. package/src/resources/extensions/gsd/tests/projection-regression.test.ts +174 -0
  150. package/src/resources/extensions/gsd/tests/prompt-contracts.test.ts +15 -14
  151. package/src/resources/extensions/gsd/tests/remote-questions.test.ts +84 -0
  152. package/src/resources/extensions/gsd/tests/reopen-slice.test.ts +155 -0
  153. package/src/resources/extensions/gsd/tests/reopen-task.test.ts +165 -0
  154. package/src/resources/extensions/gsd/tests/session-lock-regression.test.ts +1 -4
  155. package/src/resources/extensions/gsd/tests/stop-auto-remote.test.ts +2 -3
  156. package/src/resources/extensions/gsd/tests/sync-lock.test.ts +122 -0
  157. package/src/resources/extensions/gsd/tests/unit-ownership.test.ts +175 -0
  158. package/src/resources/extensions/gsd/tests/workflow-events.test.ts +205 -0
  159. package/src/resources/extensions/gsd/tests/workflow-manifest.test.ts +186 -0
  160. package/src/resources/extensions/gsd/tests/workflow-projections.test.ts +171 -0
  161. package/src/resources/extensions/gsd/tests/write-intercept.test.ts +76 -0
  162. package/src/resources/extensions/gsd/tools/complete-milestone.ts +70 -13
  163. package/src/resources/extensions/gsd/tools/complete-slice.ts +68 -11
  164. package/src/resources/extensions/gsd/tools/complete-task.ts +63 -1
  165. package/src/resources/extensions/gsd/tools/plan-milestone.ts +45 -0
  166. package/src/resources/extensions/gsd/tools/plan-slice.ts +38 -0
  167. package/src/resources/extensions/gsd/tools/plan-task.ts +35 -1
  168. package/src/resources/extensions/gsd/tools/reassess-roadmap.ts +39 -1
  169. package/src/resources/extensions/gsd/tools/reopen-slice.ts +125 -0
  170. package/src/resources/extensions/gsd/tools/reopen-task.ts +129 -0
  171. package/src/resources/extensions/gsd/tools/replan-slice.ts +38 -1
  172. package/src/resources/extensions/gsd/types.ts +8 -0
  173. package/src/resources/extensions/gsd/unit-ownership.ts +104 -0
  174. package/src/resources/extensions/gsd/workflow-events.ts +154 -0
  175. package/src/resources/extensions/gsd/workflow-logger.ts +51 -1
  176. package/src/resources/extensions/gsd/workflow-manifest.ts +334 -0
  177. package/src/resources/extensions/gsd/workflow-migration.ts +345 -0
  178. package/src/resources/extensions/gsd/workflow-projections.ts +425 -0
  179. package/src/resources/extensions/gsd/workflow-reconcile.ts +503 -0
  180. package/src/resources/extensions/gsd/write-intercept.ts +90 -0
  181. package/src/resources/extensions/remote-questions/config.ts +45 -0
  182. /package/dist/web/standalone/.next/static/{zWYDSwB-terOjfhmWzqk1 → NtM_pwvGTaXTeqiByOv3A}/_buildManifest.js +0 -0
  183. /package/dist/web/standalone/.next/static/{zWYDSwB-terOjfhmWzqk1 → NtM_pwvGTaXTeqiByOv3A}/_ssgManifest.js +0 -0
@@ -0,0 +1,334 @@
1
+ import {
2
+ _getAdapter,
3
+ transaction,
4
+ type MilestoneRow,
5
+ type SliceRow,
6
+ type TaskRow,
7
+ } from "./gsd-db.js";
8
+ import type { Decision } from "./types.js";
9
+ import { atomicWriteSync } from "./atomic-write.js";
10
+ import { readFileSync, existsSync, mkdirSync } from "node:fs";
11
+ import { join } from "node:path";
12
+
13
+ // ─── Manifest Types ──────────────────────────────────────────────────────
14
+
15
+ export interface VerificationEvidenceRow {
16
+ id: number;
17
+ task_id: string;
18
+ slice_id: string;
19
+ milestone_id: string;
20
+ command: string;
21
+ exit_code: number | null;
22
+ verdict: string;
23
+ duration_ms: number | null;
24
+ created_at: string;
25
+ }
26
+
27
+ export interface StateManifest {
28
+ version: 1;
29
+ exported_at: string; // ISO 8601
30
+ milestones: MilestoneRow[];
31
+ slices: SliceRow[];
32
+ tasks: TaskRow[];
33
+ decisions: Decision[];
34
+ verification_evidence: VerificationEvidenceRow[];
35
+ }
36
+
37
+ // ─── helpers ─────────────────────────────────────────────────────────────
38
+
39
+ function requireDb() {
40
+ const db = _getAdapter();
41
+ if (!db) throw new Error("workflow-manifest: No database open");
42
+ return db;
43
+ }
44
+
45
+ // ─── snapshotState ───────────────────────────────────────────────────────
46
+
47
+ /**
48
+ * Capture complete DB state as a StateManifest.
49
+ * Reads all rows from milestones, slices, tasks, decisions, verification_evidence.
50
+ *
51
+ * Note: rows returned from raw queries are plain objects with TEXT columns for
52
+ * JSON arrays. We parse them into typed Row objects using the same logic as
53
+ * gsd-db helper functions.
54
+ */
55
+ export function snapshotState(): StateManifest {
56
+ const db = requireDb();
57
+
58
+ // Wrap all reads in a deferred transaction so the snapshot is consistent
59
+ // (all SELECTs see the same DB state even if a concurrent write lands between them).
60
+ db.exec("BEGIN DEFERRED");
61
+
62
+ try {
63
+ const rawMilestones = db.prepare("SELECT * FROM milestones ORDER BY id").all() as Record<string, unknown>[];
64
+ const milestones: MilestoneRow[] = rawMilestones.map((r) => ({
65
+ id: r["id"] as string,
66
+ title: r["title"] as string,
67
+ status: r["status"] as string,
68
+ depends_on: JSON.parse((r["depends_on"] as string) || "[]"),
69
+ created_at: r["created_at"] as string,
70
+ completed_at: (r["completed_at"] as string) ?? null,
71
+ vision: (r["vision"] as string) ?? "",
72
+ success_criteria: JSON.parse((r["success_criteria"] as string) || "[]"),
73
+ key_risks: JSON.parse((r["key_risks"] as string) || "[]"),
74
+ proof_strategy: JSON.parse((r["proof_strategy"] as string) || "[]"),
75
+ verification_contract: (r["verification_contract"] as string) ?? "",
76
+ verification_integration: (r["verification_integration"] as string) ?? "",
77
+ verification_operational: (r["verification_operational"] as string) ?? "",
78
+ verification_uat: (r["verification_uat"] as string) ?? "",
79
+ definition_of_done: JSON.parse((r["definition_of_done"] as string) || "[]"),
80
+ requirement_coverage: (r["requirement_coverage"] as string) ?? "",
81
+ boundary_map_markdown: (r["boundary_map_markdown"] as string) ?? "",
82
+ }));
83
+
84
+ const rawSlices = db.prepare("SELECT * FROM slices ORDER BY milestone_id, sequence, id").all() as Record<string, unknown>[];
85
+ const slices: SliceRow[] = rawSlices.map((r) => ({
86
+ milestone_id: r["milestone_id"] as string,
87
+ id: r["id"] as string,
88
+ title: r["title"] as string,
89
+ status: r["status"] as string,
90
+ risk: r["risk"] as string,
91
+ depends: JSON.parse((r["depends"] as string) || "[]"),
92
+ demo: (r["demo"] as string) ?? "",
93
+ created_at: r["created_at"] as string,
94
+ completed_at: (r["completed_at"] as string) ?? null,
95
+ full_summary_md: (r["full_summary_md"] as string) ?? "",
96
+ full_uat_md: (r["full_uat_md"] as string) ?? "",
97
+ goal: (r["goal"] as string) ?? "",
98
+ success_criteria: (r["success_criteria"] as string) ?? "",
99
+ proof_level: (r["proof_level"] as string) ?? "",
100
+ integration_closure: (r["integration_closure"] as string) ?? "",
101
+ observability_impact: (r["observability_impact"] as string) ?? "",
102
+ sequence: (r["sequence"] as number) ?? 0,
103
+ replan_triggered_at: (r["replan_triggered_at"] as string) ?? null,
104
+ }));
105
+
106
+ const rawTasks = db.prepare("SELECT * FROM tasks ORDER BY milestone_id, slice_id, sequence, id").all() as Record<string, unknown>[];
107
+ const tasks: TaskRow[] = rawTasks.map((r) => ({
108
+ milestone_id: r["milestone_id"] as string,
109
+ slice_id: r["slice_id"] as string,
110
+ id: r["id"] as string,
111
+ title: r["title"] as string,
112
+ status: r["status"] as string,
113
+ one_liner: (r["one_liner"] as string) ?? "",
114
+ narrative: (r["narrative"] as string) ?? "",
115
+ verification_result: (r["verification_result"] as string) ?? "",
116
+ duration: (r["duration"] as string) ?? "",
117
+ completed_at: (r["completed_at"] as string) ?? null,
118
+ blocker_discovered: (r["blocker_discovered"] as number) === 1,
119
+ deviations: (r["deviations"] as string) ?? "",
120
+ known_issues: (r["known_issues"] as string) ?? "",
121
+ key_files: JSON.parse((r["key_files"] as string) || "[]"),
122
+ key_decisions: JSON.parse((r["key_decisions"] as string) || "[]"),
123
+ full_summary_md: (r["full_summary_md"] as string) ?? "",
124
+ description: (r["description"] as string) ?? "",
125
+ estimate: (r["estimate"] as string) ?? "",
126
+ files: JSON.parse((r["files"] as string) || "[]"),
127
+ verify: (r["verify"] as string) ?? "",
128
+ inputs: JSON.parse((r["inputs"] as string) || "[]"),
129
+ expected_output: JSON.parse((r["expected_output"] as string) || "[]"),
130
+ observability_impact: (r["observability_impact"] as string) ?? "",
131
+ full_plan_md: (r["full_plan_md"] as string) ?? "",
132
+ sequence: (r["sequence"] as number) ?? 0,
133
+ }));
134
+
135
+ const rawDecisions = db.prepare("SELECT * FROM decisions ORDER BY seq").all() as Record<string, unknown>[];
136
+ const decisions: Decision[] = rawDecisions.map((r) => ({
137
+ seq: r["seq"] as number,
138
+ id: r["id"] as string,
139
+ when_context: (r["when_context"] as string) ?? "",
140
+ scope: (r["scope"] as string) ?? "",
141
+ decision: (r["decision"] as string) ?? "",
142
+ choice: (r["choice"] as string) ?? "",
143
+ rationale: (r["rationale"] as string) ?? "",
144
+ revisable: (r["revisable"] as string) ?? "",
145
+ made_by: (r["made_by"] as string as Decision["made_by"]) ?? "agent",
146
+ superseded_by: (r["superseded_by"] as string) ?? null,
147
+ }));
148
+
149
+ const rawEvidence = db.prepare("SELECT * FROM verification_evidence ORDER BY id").all() as Record<string, unknown>[];
150
+ const verification_evidence: VerificationEvidenceRow[] = rawEvidence.map((r) => ({
151
+ id: r["id"] as number,
152
+ task_id: r["task_id"] as string,
153
+ slice_id: r["slice_id"] as string,
154
+ milestone_id: r["milestone_id"] as string,
155
+ command: r["command"] as string,
156
+ exit_code: (r["exit_code"] as number) ?? null,
157
+ verdict: (r["verdict"] as string) ?? "",
158
+ duration_ms: (r["duration_ms"] as number) ?? null,
159
+ created_at: r["created_at"] as string,
160
+ }));
161
+
162
+ const result: StateManifest = {
163
+ version: 1,
164
+ exported_at: new Date().toISOString(),
165
+ milestones,
166
+ slices,
167
+ tasks,
168
+ decisions,
169
+ verification_evidence,
170
+ };
171
+
172
+ db.exec("COMMIT");
173
+ return result;
174
+ } catch (err) {
175
+ try { db.exec("ROLLBACK"); } catch { /* ignore rollback failure */ }
176
+ throw err;
177
+ }
178
+ }
179
+
180
+ // ─── restore ─────────────────────────────────────────────────────────────
181
+
182
+ /**
183
+ * Atomically replace all workflow state from a manifest.
184
+ * Runs inside a transaction — if any insert fails, no tables are modified.
185
+ * Only touches engine tables + decisions. Does NOT modify artifacts or memories.
186
+ */
187
+ function restore(manifest: StateManifest): void {
188
+ const db = requireDb();
189
+
190
+ transaction(() => {
191
+ // Clear engine tables (order matters for foreign-key-like consistency)
192
+ db.exec("DELETE FROM verification_evidence");
193
+ db.exec("DELETE FROM tasks");
194
+ db.exec("DELETE FROM slices");
195
+ db.exec("DELETE FROM milestones");
196
+ db.exec("DELETE FROM decisions WHERE 1=1");
197
+
198
+ // Restore milestones
199
+ const msStmt = db.prepare(
200
+ `INSERT INTO milestones (id, title, status, depends_on, created_at, completed_at,
201
+ vision, success_criteria, key_risks, proof_strategy,
202
+ verification_contract, verification_integration, verification_operational, verification_uat,
203
+ definition_of_done, requirement_coverage, boundary_map_markdown)
204
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
205
+ );
206
+ for (const m of manifest.milestones) {
207
+ msStmt.run(
208
+ m.id, m.title, m.status,
209
+ JSON.stringify(m.depends_on), m.created_at, m.completed_at,
210
+ m.vision, JSON.stringify(m.success_criteria), JSON.stringify(m.key_risks),
211
+ JSON.stringify(m.proof_strategy),
212
+ m.verification_contract, m.verification_integration, m.verification_operational, m.verification_uat,
213
+ JSON.stringify(m.definition_of_done), m.requirement_coverage, m.boundary_map_markdown,
214
+ );
215
+ }
216
+
217
+ // Restore slices
218
+ const slStmt = db.prepare(
219
+ `INSERT INTO slices (milestone_id, id, title, status, risk, depends, demo,
220
+ created_at, completed_at, full_summary_md, full_uat_md,
221
+ goal, success_criteria, proof_level, integration_closure, observability_impact,
222
+ sequence, replan_triggered_at)
223
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
224
+ );
225
+ for (const s of manifest.slices) {
226
+ slStmt.run(
227
+ s.milestone_id, s.id, s.title, s.status, s.risk,
228
+ JSON.stringify(s.depends), s.demo,
229
+ s.created_at, s.completed_at, s.full_summary_md, s.full_uat_md,
230
+ s.goal, s.success_criteria, s.proof_level, s.integration_closure, s.observability_impact,
231
+ s.sequence, s.replan_triggered_at,
232
+ );
233
+ }
234
+
235
+ // Restore tasks
236
+ const tkStmt = db.prepare(
237
+ `INSERT INTO tasks (milestone_id, slice_id, id, title, status,
238
+ one_liner, narrative, verification_result, duration, completed_at,
239
+ blocker_discovered, deviations, known_issues, key_files, key_decisions,
240
+ full_summary_md, description, estimate, files, verify,
241
+ inputs, expected_output, observability_impact, sequence)
242
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
243
+ );
244
+ for (const t of manifest.tasks) {
245
+ tkStmt.run(
246
+ t.milestone_id, t.slice_id, t.id, t.title, t.status,
247
+ t.one_liner, t.narrative, t.verification_result, t.duration, t.completed_at,
248
+ t.blocker_discovered ? 1 : 0, t.deviations, t.known_issues,
249
+ JSON.stringify(t.key_files), JSON.stringify(t.key_decisions),
250
+ t.full_summary_md, t.description, t.estimate, JSON.stringify(t.files), t.verify,
251
+ JSON.stringify(t.inputs), JSON.stringify(t.expected_output),
252
+ t.observability_impact, t.sequence,
253
+ );
254
+ }
255
+
256
+ // Restore decisions
257
+ const dcStmt = db.prepare(
258
+ `INSERT INTO decisions (seq, id, when_context, scope, decision, choice, rationale, revisable, made_by, superseded_by)
259
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
260
+ );
261
+ for (const d of manifest.decisions) {
262
+ dcStmt.run(d.seq, d.id, d.when_context, d.scope, d.decision, d.choice, d.rationale, d.revisable, d.made_by, d.superseded_by);
263
+ }
264
+
265
+ // Restore verification evidence
266
+ const evStmt = db.prepare(
267
+ `INSERT INTO verification_evidence (task_id, slice_id, milestone_id, command, exit_code, verdict, duration_ms, created_at)
268
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
269
+ );
270
+ for (const e of manifest.verification_evidence) {
271
+ evStmt.run(e.task_id, e.slice_id, e.milestone_id, e.command, e.exit_code, e.verdict, e.duration_ms, e.created_at);
272
+ }
273
+ });
274
+ }
275
+
276
+ // ─── writeManifest ───────────────────────────────────────────────────────
277
+
278
+ /**
279
+ * Write current DB state to .gsd/state-manifest.json via atomicWriteSync.
280
+ * Uses JSON.stringify with 2-space indent for git three-way merge friendliness.
281
+ */
282
+ export function writeManifest(basePath: string): void {
283
+ const manifest = snapshotState();
284
+ const json = JSON.stringify(manifest, null, 2);
285
+ const dir = join(basePath, ".gsd");
286
+ mkdirSync(dir, { recursive: true });
287
+ atomicWriteSync(join(dir, "state-manifest.json"), json);
288
+ }
289
+
290
+ // ─── readManifest ────────────────────────────────────────────────────────
291
+
292
+ /**
293
+ * Read state-manifest.json and return parsed manifest, or null if not found.
294
+ */
295
+ export function readManifest(basePath: string): StateManifest | null {
296
+ const manifestPath = join(basePath, ".gsd", "state-manifest.json");
297
+
298
+ if (!existsSync(manifestPath)) {
299
+ return null;
300
+ }
301
+
302
+ const raw = readFileSync(manifestPath, "utf-8");
303
+ const parsed = JSON.parse(raw) as StateManifest;
304
+
305
+ if (parsed.version !== 1) {
306
+ throw new Error(`Unsupported manifest version: ${parsed.version}`);
307
+ }
308
+
309
+ // Validate required fields to avoid cryptic errors during restore
310
+ if (!Array.isArray(parsed.milestones) || !Array.isArray(parsed.slices) ||
311
+ !Array.isArray(parsed.tasks) || !Array.isArray(parsed.decisions) ||
312
+ !Array.isArray(parsed.verification_evidence)) {
313
+ throw new Error("Malformed manifest: missing or invalid required arrays");
314
+ }
315
+
316
+ return parsed;
317
+ }
318
+
319
+ // ─── bootstrapFromManifest ──────────────────────────────────────────────
320
+
321
+ /**
322
+ * Read state-manifest.json and restore DB state from it.
323
+ * Returns true if bootstrap succeeded, false if manifest file doesn't exist.
324
+ */
325
+ export function bootstrapFromManifest(basePath: string): boolean {
326
+ const manifest = readManifest(basePath);
327
+
328
+ if (!manifest) {
329
+ return false;
330
+ }
331
+
332
+ restore(manifest);
333
+ return true;
334
+ }
@@ -0,0 +1,345 @@
1
+ // GSD Extension — Legacy Markdown to Engine Migration
2
+ // Converts legacy markdown-only projects to engine state by parsing
3
+ // existing ROADMAP.md, *-PLAN.md, and *-SUMMARY.md files.
4
+ // Populates data into the already-existing v10 schema tables.
5
+
6
+ import { existsSync, readdirSync, readFileSync } from "node:fs";
7
+ import { join } from "node:path";
8
+ import { _getAdapter, transaction } from "./gsd-db.js";
9
+ import { parseRoadmap, parsePlan } from "./parsers-legacy.js";
10
+
11
+ // ─── needsAutoMigration ───────────────────────────────────────────────────
12
+
13
+ /**
14
+ * Returns true when engine tables are empty AND a .gsd/milestones/ directory
15
+ * with markdown files exists — signals that this is a legacy project that needs
16
+ * one-time migration from markdown to engine state.
17
+ */
18
+ export function needsAutoMigration(basePath: string): boolean {
19
+ const db = _getAdapter();
20
+ if (!db) return false;
21
+
22
+ // If milestones table already has rows, migration already done
23
+ try {
24
+ const row = db.prepare("SELECT COUNT(*) as cnt FROM milestones").get();
25
+ if (row && (row["cnt"] as number) > 0) return false;
26
+ } catch {
27
+ // Table might not exist yet — that's fine, we can still migrate
28
+ return false;
29
+ }
30
+
31
+ // Check if .gsd/milestones/ directory exists
32
+ const milestonesDir = join(basePath, ".gsd", "milestones");
33
+ if (!existsSync(milestonesDir)) return false;
34
+
35
+ return true;
36
+ }
37
+
38
+ // ─── migrateFromMarkdown ──────────────────────────────────────────────────
39
+
40
+ /**
41
+ * Migrate legacy markdown-only .gsd/ projects to engine DB state.
42
+ * Reads .gsd/milestones/<ID>/ directories and parses ROADMAP.md, *-PLAN.md
43
+ * files. All inserts are wrapped in a transaction.
44
+ *
45
+ * This function only INSERTs data into the already-existing v10 schema tables
46
+ * (milestones, slices, tasks). It does NOT create tables or run migrations.
47
+ *
48
+ * Handles all directory shapes:
49
+ * - No DB: caller is responsible for openDatabase + initSchema before calling
50
+ * - Stale DB (empty tables): inserts succeed normally
51
+ * - No markdown at all: returns early with stderr message
52
+ * - Orphaned summary files: logs warning, skips without crash
53
+ */
54
+ export function migrateFromMarkdown(basePath: string): void {
55
+ const db = _getAdapter();
56
+ if (!db) {
57
+ process.stderr.write("workflow-migration: no database connection, cannot migrate\n");
58
+ return;
59
+ }
60
+
61
+ const milestonesDir = join(basePath, ".gsd", "milestones");
62
+ if (!existsSync(milestonesDir)) {
63
+ process.stderr.write("workflow-migration: no .gsd/milestones/ directory found, nothing to migrate\n");
64
+ return;
65
+ }
66
+
67
+ // Discover milestone directories (any directory at the top level of milestones/)
68
+ let milestoneDirs: string[];
69
+ try {
70
+ milestoneDirs = readdirSync(milestonesDir, { withFileTypes: true })
71
+ .filter(e => e.isDirectory())
72
+ .map(e => e.name);
73
+ } catch {
74
+ process.stderr.write("workflow-migration: failed to read milestones directory\n");
75
+ return;
76
+ }
77
+
78
+ if (milestoneDirs.length === 0) {
79
+ process.stderr.write("workflow-migration: no milestone directories found in .gsd/milestones/\n");
80
+ return;
81
+ }
82
+
83
+ // Collect all data before the transaction
84
+ const migratedMilestoneIds: string[] = [];
85
+
86
+ interface MilestoneInsert {
87
+ id: string;
88
+ title: string;
89
+ status: string;
90
+ }
91
+
92
+ interface SliceInsert {
93
+ id: string;
94
+ milestoneId: string;
95
+ title: string;
96
+ status: string;
97
+ risk: string;
98
+ sequence: number;
99
+ forceDone: boolean;
100
+ }
101
+
102
+ interface TaskInsert {
103
+ id: string;
104
+ sliceId: string;
105
+ milestoneId: string;
106
+ title: string;
107
+ status: string;
108
+ sequence: number;
109
+ }
110
+
111
+ const milestoneInserts: MilestoneInsert[] = [];
112
+ const sliceInserts: SliceInsert[] = [];
113
+ const taskInserts: TaskInsert[] = [];
114
+
115
+ for (const mId of milestoneDirs) {
116
+ const mDir = join(milestonesDir, mId);
117
+
118
+ // Determine milestone status: done if a milestone-level SUMMARY.md exists
119
+ const milestoneSummaryPath = join(mDir, "SUMMARY.md");
120
+ const milestoneDone = existsSync(milestoneSummaryPath);
121
+ const milestoneStatus = milestoneDone ? "done" : "active";
122
+
123
+ // Parse ROADMAP.md for slices list
124
+ const roadmapPath = join(mDir, "ROADMAP.md");
125
+ let roadmapSlices: Array<{ id: string; title: string; done: boolean; risk: string }> = [];
126
+
127
+ if (existsSync(roadmapPath)) {
128
+ try {
129
+ const roadmapContent = readFileSync(roadmapPath, "utf-8");
130
+ const roadmap = parseRoadmap(roadmapContent);
131
+
132
+ // Extract milestone title from roadmap
133
+ const mTitle = roadmap.title || mId;
134
+
135
+ milestoneInserts.push({ id: mId, title: mTitle, status: milestoneStatus });
136
+
137
+ roadmapSlices = roadmap.slices.map(s => ({
138
+ id: s.id,
139
+ title: s.title,
140
+ done: s.done,
141
+ risk: s.risk || "low",
142
+ }));
143
+ } catch (err) {
144
+ process.stderr.write(`workflow-migration: failed to parse ROADMAP.md for ${mId}: ${(err as Error).message}\n`);
145
+ // Still add milestone with ID as title
146
+ milestoneInserts.push({ id: mId, title: mId, status: milestoneStatus });
147
+ }
148
+ } else {
149
+ // No ROADMAP.md — add milestone entry anyway using directory name
150
+ milestoneInserts.push({ id: mId, title: mId, status: milestoneStatus });
151
+ }
152
+
153
+ migratedMilestoneIds.push(mId);
154
+
155
+ // Collect slices from ROADMAP + their tasks from PLAN files
156
+ const knownSliceIds = new Set(roadmapSlices.map(s => s.id));
157
+
158
+ for (let sIdx = 0; sIdx < roadmapSlices.length; sIdx++) {
159
+ const slice = roadmapSlices[sIdx];
160
+ // Per Pitfall #5: if milestone is done, force all child slices to done
161
+ const sliceStatus = milestoneDone ? "done" : (slice.done ? "done" : "pending");
162
+
163
+ sliceInserts.push({
164
+ id: slice.id,
165
+ milestoneId: mId,
166
+ title: slice.title,
167
+ status: sliceStatus,
168
+ risk: slice.risk,
169
+ sequence: sIdx,
170
+ forceDone: milestoneDone,
171
+ });
172
+
173
+ // Read *-PLAN.md for this slice
174
+ const planPath = join(mDir, `${slice.id}-PLAN.md`);
175
+ if (existsSync(planPath)) {
176
+ try {
177
+ const planContent = readFileSync(planPath, "utf-8");
178
+ const plan = parsePlan(planContent);
179
+
180
+ for (let tIdx = 0; tIdx < plan.tasks.length; tIdx++) {
181
+ const task = plan.tasks[tIdx];
182
+ // Per Pitfall #5: if milestone is done, force all tasks to done
183
+ const taskStatus = milestoneDone ? "done" : (task.done ? "done" : "pending");
184
+ taskInserts.push({
185
+ id: task.id,
186
+ sliceId: slice.id,
187
+ milestoneId: mId,
188
+ title: task.title,
189
+ status: taskStatus,
190
+ sequence: tIdx,
191
+ });
192
+ }
193
+ } catch (err) {
194
+ process.stderr.write(`workflow-migration: failed to parse ${slice.id}-PLAN.md for ${mId}: ${(err as Error).message}\n`);
195
+ }
196
+ }
197
+ }
198
+
199
+ // Check for orphaned summary files (summary for a slice not in ROADMAP)
200
+ try {
201
+ const files = readdirSync(mDir);
202
+ const summaryFiles = files.filter(f => f.endsWith("-SUMMARY.md") && f !== "SUMMARY.md");
203
+ for (const summaryFile of summaryFiles) {
204
+ const sliceId = summaryFile.replace("-SUMMARY.md", "");
205
+ if (!knownSliceIds.has(sliceId)) {
206
+ process.stderr.write(`workflow-migration: orphaned summary file ${summaryFile} in ${mId} (slice not found in ROADMAP.md), skipping\n`);
207
+ }
208
+ }
209
+ } catch {
210
+ // Non-fatal
211
+ }
212
+ }
213
+
214
+ // Execute all inserts atomically
215
+ const now = new Date().toISOString();
216
+ if (migratedMilestoneIds.length === 0) {
217
+ process.stderr.write("workflow-migration: no milestones collected, nothing to insert\n");
218
+ return;
219
+ }
220
+
221
+ const placeholders = migratedMilestoneIds.map(() => "?").join(",");
222
+ transaction(() => {
223
+ // Clear existing data to handle stale DB shape (DELETE ... IN (...))
224
+ db.prepare(`DELETE FROM tasks WHERE milestone_id IN (${placeholders})`).run(...migratedMilestoneIds);
225
+ db.prepare(`DELETE FROM slices WHERE milestone_id IN (${placeholders})`).run(...migratedMilestoneIds);
226
+ db.prepare(`DELETE FROM milestones WHERE id IN (${placeholders})`).run(...migratedMilestoneIds);
227
+
228
+ // Insert milestones
229
+ const insertMilestone = db.prepare("INSERT INTO milestones (id, title, status, created_at) VALUES (?, ?, ?, ?)");
230
+ for (const m of milestoneInserts) {
231
+ insertMilestone.run(m.id, m.title, m.status, now);
232
+ }
233
+
234
+ // Insert slices (using v10 column names: depends, sequence)
235
+ const insertSlice = db.prepare(
236
+ "INSERT INTO slices (id, milestone_id, title, status, risk, depends, sequence, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
237
+ );
238
+ for (const s of sliceInserts) {
239
+ insertSlice.run(s.id, s.milestoneId, s.title, s.status, s.risk, "[]", s.sequence, now);
240
+ }
241
+
242
+ // Insert tasks (using v10 column names: sequence, blocker_discovered, full_summary_md)
243
+ const insertTask = db.prepare(
244
+ "INSERT INTO tasks (id, slice_id, milestone_id, title, description, status, estimate, files, sequence) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
245
+ );
246
+ for (const t of taskInserts) {
247
+ insertTask.run(t.id, t.sliceId, t.milestoneId, t.title, "", t.status, "", "[]", t.sequence);
248
+ }
249
+ });
250
+ }
251
+
252
+ // ─── validateMigration ────────────────────────────────────────────────────
253
+
254
+ /**
255
+ * D-14: Validate that engine state matches what markdown parsers report.
256
+ * Compares milestone count, slice count, task count, and status distributions.
257
+ * Logs each discrepancy to stderr but does NOT throw.
258
+ * Returns array of discrepancy strings (empty = clean migration).
259
+ */
260
+ export function validateMigration(basePath: string): { discrepancies: string[] } {
261
+ const db = _getAdapter();
262
+ if (!db) {
263
+ return { discrepancies: ["No database connection for validation"] };
264
+ }
265
+
266
+ const discrepancies: string[] = [];
267
+
268
+ // Get engine counts
269
+ const engMilestones = db.prepare("SELECT COUNT(*) as cnt FROM milestones").get();
270
+ const engSlices = db.prepare("SELECT COUNT(*) as cnt FROM slices").get();
271
+ const engTasks = db.prepare("SELECT COUNT(*) as cnt FROM tasks").get();
272
+
273
+ const engineMilestoneCount = engMilestones ? (engMilestones["cnt"] as number) : 0;
274
+ const engineSliceCount = engSlices ? (engSlices["cnt"] as number) : 0;
275
+ const engineTaskCount = engTasks ? (engTasks["cnt"] as number) : 0;
276
+
277
+ // Count from markdown
278
+ const milestonesDir = join(basePath, ".gsd", "milestones");
279
+ if (!existsSync(milestonesDir)) {
280
+ return { discrepancies };
281
+ }
282
+
283
+ let mdMilestoneCount = 0;
284
+ let mdSliceCount = 0;
285
+ let mdTaskCount = 0;
286
+
287
+ try {
288
+ const milestoneDirs = readdirSync(milestonesDir, { withFileTypes: true })
289
+ .filter(e => e.isDirectory())
290
+ .map(e => e.name);
291
+
292
+ mdMilestoneCount = milestoneDirs.length;
293
+
294
+ for (const mId of milestoneDirs) {
295
+ const mDir = join(milestonesDir, mId);
296
+ const roadmapPath = join(mDir, "ROADMAP.md");
297
+
298
+ if (existsSync(roadmapPath)) {
299
+ try {
300
+ const content = readFileSync(roadmapPath, "utf-8");
301
+ const roadmap = parseRoadmap(content);
302
+ mdSliceCount += roadmap.slices.length;
303
+
304
+ for (const slice of roadmap.slices) {
305
+ const planPath = join(mDir, `${slice.id}-PLAN.md`);
306
+ if (existsSync(planPath)) {
307
+ try {
308
+ const planContent = readFileSync(planPath, "utf-8");
309
+ const plan = parsePlan(planContent);
310
+ mdTaskCount += plan.tasks.length;
311
+ } catch {
312
+ // Skip unreadable plan
313
+ }
314
+ }
315
+ }
316
+ } catch {
317
+ // Skip unreadable roadmap
318
+ }
319
+ }
320
+ }
321
+ } catch {
322
+ return { discrepancies: ["Failed to read markdown for validation"] };
323
+ }
324
+
325
+ // Compare counts
326
+ if (engineMilestoneCount !== mdMilestoneCount) {
327
+ const msg = `Milestone count mismatch: engine=${engineMilestoneCount}, markdown=${mdMilestoneCount}`;
328
+ discrepancies.push(msg);
329
+ process.stderr.write(`workflow-migration: ${msg}\n`);
330
+ }
331
+
332
+ if (engineSliceCount !== mdSliceCount) {
333
+ const msg = `Slice count mismatch: engine=${engineSliceCount}, markdown=${mdSliceCount}`;
334
+ discrepancies.push(msg);
335
+ process.stderr.write(`workflow-migration: ${msg}\n`);
336
+ }
337
+
338
+ if (engineTaskCount !== mdTaskCount) {
339
+ const msg = `Task count mismatch: engine=${engineTaskCount}, markdown=${mdTaskCount}`;
340
+ discrepancies.push(msg);
341
+ process.stderr.write(`workflow-migration: ${msg}\n`);
342
+ }
343
+
344
+ return { discrepancies };
345
+ }