gsd-pi 2.74.0-dev.2b524c3 → 2.74.0-dev.b741afb

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 (159) hide show
  1. package/dist/cli.js +85 -0
  2. package/dist/headless-query.js +4 -1
  3. package/dist/help-text.js +23 -0
  4. package/dist/resources/extensions/gsd/auto/detect-stuck.js +11 -4
  5. package/dist/resources/extensions/gsd/auto/phases.js +45 -1
  6. package/dist/resources/extensions/gsd/auto-post-unit.js +52 -56
  7. package/dist/resources/extensions/gsd/auto-prompts.js +12 -0
  8. package/dist/resources/extensions/gsd/auto.js +8 -2
  9. package/dist/resources/extensions/gsd/bootstrap/register-extension.js +21 -8
  10. package/dist/resources/extensions/gsd/commands/catalog.js +26 -1
  11. package/dist/resources/extensions/gsd/commands/handlers/ops.js +20 -0
  12. package/dist/resources/extensions/gsd/commands/handlers/workflow.js +68 -9
  13. package/dist/resources/extensions/gsd/commands-add-tests.js +111 -0
  14. package/dist/resources/extensions/gsd/commands-backlog.js +140 -0
  15. package/dist/resources/extensions/gsd/commands-do.js +79 -0
  16. package/dist/resources/extensions/gsd/commands-maintenance.js +6 -6
  17. package/dist/resources/extensions/gsd/commands-pr-branch.js +180 -0
  18. package/dist/resources/extensions/gsd/commands-session-report.js +82 -0
  19. package/dist/resources/extensions/gsd/commands-ship.js +187 -0
  20. package/dist/resources/extensions/gsd/db-writer.js +3 -5
  21. package/dist/resources/extensions/gsd/graph-context.js +66 -0
  22. package/dist/resources/extensions/gsd/gsd-db.js +321 -0
  23. package/dist/resources/extensions/gsd/index.js +15 -2
  24. package/dist/resources/extensions/gsd/md-importer.js +3 -4
  25. package/dist/resources/extensions/gsd/memory-store.js +19 -51
  26. package/dist/resources/extensions/gsd/milestone-validation-gates.js +13 -12
  27. package/dist/resources/extensions/gsd/native-git-bridge.js +7 -4
  28. package/dist/resources/extensions/gsd/prompts/add-tests.md +35 -0
  29. package/dist/resources/extensions/gsd/state.js +5 -1
  30. package/dist/resources/extensions/gsd/tools/complete-slice.js +15 -0
  31. package/dist/resources/extensions/gsd/tools/workflow-tool-executors.js +3 -14
  32. package/dist/resources/extensions/gsd/triage-resolution.js +2 -5
  33. package/dist/resources/extensions/gsd/workflow-manifest.js +8 -69
  34. package/dist/resources/extensions/gsd/workflow-migration.js +21 -22
  35. package/dist/resources/extensions/gsd/workflow-projections.js +4 -1
  36. package/dist/resources/extensions/gsd/workflow-reconcile.js +14 -11
  37. package/dist/tsconfig.extensions.tsbuildinfo +1 -0
  38. package/dist/web/standalone/.next/BUILD_ID +1 -1
  39. package/dist/web/standalone/.next/app-path-routes-manifest.json +7 -7
  40. package/dist/web/standalone/.next/build-manifest.json +2 -2
  41. package/dist/web/standalone/.next/prerender-manifest.json +3 -3
  42. package/dist/web/standalone/.next/server/app/_global-error.html +1 -1
  43. package/dist/web/standalone/.next/server/app/_global-error.rsc +1 -1
  44. package/dist/web/standalone/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  45. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  46. package/dist/web/standalone/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  47. package/dist/web/standalone/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  48. package/dist/web/standalone/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  49. package/dist/web/standalone/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  50. package/dist/web/standalone/.next/server/app/_not-found.html +1 -1
  51. package/dist/web/standalone/.next/server/app/_not-found.rsc +1 -1
  52. package/dist/web/standalone/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  53. package/dist/web/standalone/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  54. package/dist/web/standalone/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  55. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  56. package/dist/web/standalone/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  57. package/dist/web/standalone/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  58. package/dist/web/standalone/.next/server/app/index.html +1 -1
  59. package/dist/web/standalone/.next/server/app/index.rsc +1 -1
  60. package/dist/web/standalone/.next/server/app/index.segments/__PAGE__.segment.rsc +1 -1
  61. package/dist/web/standalone/.next/server/app/index.segments/_full.segment.rsc +1 -1
  62. package/dist/web/standalone/.next/server/app/index.segments/_head.segment.rsc +1 -1
  63. package/dist/web/standalone/.next/server/app/index.segments/_index.segment.rsc +1 -1
  64. package/dist/web/standalone/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  65. package/dist/web/standalone/.next/server/app-paths-manifest.json +7 -7
  66. package/dist/web/standalone/.next/server/middleware-build-manifest.js +1 -1
  67. package/dist/web/standalone/.next/server/middleware-manifest.json +5 -5
  68. package/dist/web/standalone/.next/server/pages/404.html +1 -1
  69. package/dist/web/standalone/.next/server/pages/500.html +1 -1
  70. package/dist/web/standalone/.next/server/server-reference-manifest.json +1 -1
  71. package/package.json +3 -2
  72. package/packages/daemon/package.json +2 -2
  73. package/packages/mcp-server/dist/index.d.ts +3 -0
  74. package/packages/mcp-server/dist/index.d.ts.map +1 -1
  75. package/packages/mcp-server/dist/index.js +3 -0
  76. package/packages/mcp-server/dist/index.js.map +1 -1
  77. package/packages/mcp-server/dist/readers/graph.d.ts +87 -0
  78. package/packages/mcp-server/dist/readers/graph.d.ts.map +1 -0
  79. package/packages/mcp-server/dist/readers/graph.js +548 -0
  80. package/packages/mcp-server/dist/readers/graph.js.map +1 -0
  81. package/packages/mcp-server/dist/readers/index.d.ts +2 -0
  82. package/packages/mcp-server/dist/readers/index.d.ts.map +1 -1
  83. package/packages/mcp-server/dist/readers/index.js +1 -0
  84. package/packages/mcp-server/dist/readers/index.js.map +1 -1
  85. package/packages/mcp-server/dist/server.d.ts.map +1 -1
  86. package/packages/mcp-server/dist/server.js +65 -0
  87. package/packages/mcp-server/dist/server.js.map +1 -1
  88. package/packages/mcp-server/package.json +2 -2
  89. package/packages/mcp-server/src/index.ts +15 -0
  90. package/packages/mcp-server/src/readers/graph.test.ts +426 -0
  91. package/packages/mcp-server/src/readers/graph.ts +708 -0
  92. package/packages/mcp-server/src/readers/index.ts +12 -0
  93. package/packages/mcp-server/src/server.ts +83 -0
  94. package/packages/mcp-server/tsconfig.json +1 -0
  95. package/packages/mcp-server/tsconfig.tsbuildinfo +1 -0
  96. package/packages/native/package.json +2 -2
  97. package/packages/native/tsconfig.tsbuildinfo +1 -0
  98. package/packages/pi-agent-core/package.json +1 -1
  99. package/packages/pi-agent-core/tsconfig.json +1 -0
  100. package/packages/pi-agent-core/tsconfig.tsbuildinfo +1 -0
  101. package/packages/pi-ai/package.json +1 -1
  102. package/packages/pi-ai/tsconfig.json +1 -0
  103. package/packages/pi-ai/tsconfig.tsbuildinfo +1 -0
  104. package/packages/pi-coding-agent/tsconfig.json +1 -0
  105. package/packages/pi-coding-agent/tsconfig.tsbuildinfo +1 -0
  106. package/packages/pi-tui/package.json +1 -1
  107. package/packages/pi-tui/tsconfig.json +1 -0
  108. package/packages/pi-tui/tsconfig.tsbuildinfo +1 -0
  109. package/packages/rpc-client/package.json +1 -1
  110. package/packages/rpc-client/tsconfig.json +1 -0
  111. package/packages/rpc-client/tsconfig.tsbuildinfo +1 -0
  112. package/src/resources/extensions/gsd/auto/detect-stuck.ts +12 -4
  113. package/src/resources/extensions/gsd/auto/loop-deps.ts +6 -0
  114. package/src/resources/extensions/gsd/auto/phases.ts +68 -1
  115. package/src/resources/extensions/gsd/auto-post-unit.ts +60 -57
  116. package/src/resources/extensions/gsd/auto-prompts.ts +13 -0
  117. package/src/resources/extensions/gsd/auto.ts +7 -0
  118. package/src/resources/extensions/gsd/bootstrap/register-extension.ts +24 -8
  119. package/src/resources/extensions/gsd/commands/catalog.ts +26 -1
  120. package/src/resources/extensions/gsd/commands/handlers/ops.ts +20 -0
  121. package/src/resources/extensions/gsd/commands/handlers/workflow.ts +74 -9
  122. package/src/resources/extensions/gsd/commands-add-tests.ts +137 -0
  123. package/src/resources/extensions/gsd/commands-backlog.ts +182 -0
  124. package/src/resources/extensions/gsd/commands-do.ts +109 -0
  125. package/src/resources/extensions/gsd/commands-maintenance.ts +6 -6
  126. package/src/resources/extensions/gsd/commands-pr-branch.ts +234 -0
  127. package/src/resources/extensions/gsd/commands-session-report.ts +101 -0
  128. package/src/resources/extensions/gsd/commands-ship.ts +219 -0
  129. package/src/resources/extensions/gsd/db-writer.ts +3 -5
  130. package/src/resources/extensions/gsd/graph-context.ts +85 -0
  131. package/src/resources/extensions/gsd/gsd-db.ts +467 -0
  132. package/src/resources/extensions/gsd/index.ts +18 -2
  133. package/src/resources/extensions/gsd/md-importer.ts +3 -5
  134. package/src/resources/extensions/gsd/memory-store.ts +31 -62
  135. package/src/resources/extensions/gsd/milestone-validation-gates.ts +13 -14
  136. package/src/resources/extensions/gsd/native-git-bridge.ts +11 -12
  137. package/src/resources/extensions/gsd/prompts/add-tests.md +35 -0
  138. package/src/resources/extensions/gsd/state.ts +9 -2
  139. package/src/resources/extensions/gsd/tests/commands-backlog.test.ts +158 -0
  140. package/src/resources/extensions/gsd/tests/commands-do.test.ts +127 -0
  141. package/src/resources/extensions/gsd/tests/commands-pr-branch.test.ts +68 -0
  142. package/src/resources/extensions/gsd/tests/commands-session-report.test.ts +82 -0
  143. package/src/resources/extensions/gsd/tests/commands-ship.test.ts +71 -0
  144. package/src/resources/extensions/gsd/tests/commands-workflow-custom.test.ts +14 -0
  145. package/src/resources/extensions/gsd/tests/extension-bootstrap-isolation.test.ts +154 -0
  146. package/src/resources/extensions/gsd/tests/graph-context.test.ts +337 -0
  147. package/src/resources/extensions/gsd/tests/journal-integration.test.ts +68 -1
  148. package/src/resources/extensions/gsd/tests/native-git-bridge-exec-fallback.test.ts +140 -0
  149. package/src/resources/extensions/gsd/tests/single-writer-invariant.test.ts +180 -0
  150. package/src/resources/extensions/gsd/tests/workflow-logger-wiring.test.ts +223 -0
  151. package/src/resources/extensions/gsd/tools/complete-slice.ts +19 -0
  152. package/src/resources/extensions/gsd/tools/workflow-tool-executors.ts +3 -11
  153. package/src/resources/extensions/gsd/triage-resolution.ts +2 -7
  154. package/src/resources/extensions/gsd/workflow-manifest.ts +9 -104
  155. package/src/resources/extensions/gsd/workflow-migration.ts +21 -29
  156. package/src/resources/extensions/gsd/workflow-projections.ts +8 -1
  157. package/src/resources/extensions/gsd/workflow-reconcile.ts +15 -15
  158. /package/dist/web/standalone/.next/static/{YzIEI9sxJy4t5xgClF08g → XnHY5eXUsTCFmNodWHetD}/_buildManifest.js +0 -0
  159. /package/dist/web/standalone/.next/static/{YzIEI9sxJy4t5xgClF08g → XnHY5eXUsTCFmNodWHetD}/_ssgManifest.js +0 -0
@@ -346,8 +346,7 @@ export async function saveRequirementToDb(
346
346
  } catch (diskErr) {
347
347
  logError('manifest', 'disk write failed, rolling back DB row', { fn: 'saveRequirementToDb', error: String((diskErr as Error).message) });
348
348
  try {
349
- const rollbackAdapter = db._getAdapter();
350
- rollbackAdapter?.prepare('DELETE FROM requirements WHERE id = :id').run({ ':id': id });
349
+ db.deleteRequirementById(id);
351
350
  } catch (rollbackErr) {
352
351
  logError('manifest', 'SPLIT BRAIN: disk write failed AND DB rollback failed — DB has orphaned row', { fn: 'saveRequirementToDb', id, error: String((rollbackErr as Error).message) });
353
352
  }
@@ -471,7 +470,7 @@ export async function saveDecisionToDb(
471
470
  } catch (diskErr) {
472
471
  logError('manifest', 'disk write failed, rolling back DB row', { fn: 'saveDecisionToDb', error: String((diskErr as Error).message) });
473
472
  try {
474
- adapter?.prepare('DELETE FROM decisions WHERE id = :id').run({ ':id': id });
473
+ db.deleteDecisionById(id);
475
474
  } catch (rollbackErr) {
476
475
  logError('manifest', 'SPLIT BRAIN: disk write failed AND DB rollback failed — DB has orphaned row', { fn: 'saveDecisionToDb', id, error: String((rollbackErr as Error).message) });
477
476
  }
@@ -714,8 +713,7 @@ export async function saveArtifactToDb(
714
713
  await saveFile(fullPath, opts.content);
715
714
  } catch (diskErr) {
716
715
  logError('manifest', 'disk write failed, rolling back DB row', { fn: 'saveArtifactToDb', error: String((diskErr as Error).message) });
717
- const rollbackAdapter = db._getAdapter();
718
- rollbackAdapter?.prepare('DELETE FROM artifacts WHERE path = :path').run({ ':path': opts.path });
716
+ db.deleteArtifactByPath(opts.path);
719
717
  throw diskErr;
720
718
  }
721
719
  }
@@ -0,0 +1,85 @@
1
+ /**
2
+ * Graph-aware context injection for dispatch prompt builders.
3
+ *
4
+ * Reads the pre-built graph.json and returns a formatted context block
5
+ * for injection into prompts. Gracefully returns null when no graph exists
6
+ * or the query yields no results — callers must handle null.
7
+ */
8
+
9
+ import { logWarning } from "./workflow-logger.js";
10
+ import type { GraphQueryResult, GraphStatusResult } from "@gsd-build/mcp-server";
11
+
12
+ export interface GraphSubgraphOptions {
13
+ /** Budget in tokens passed to graphQuery (1 node ≈ 20 tokens, 1 edge ≈ 10 tokens) */
14
+ budget: number;
15
+ }
16
+
17
+ /**
18
+ * Query the knowledge graph for nodes related to the given term and format
19
+ * the result as an inlined context block.
20
+ *
21
+ * Returns null when:
22
+ * - @gsd-build/mcp-server fails to import
23
+ * - graph.json does not exist (graphQuery already handles this gracefully)
24
+ * - query returns zero nodes
25
+ *
26
+ * Annotates the block header when the graph is stale (> 24 hours old).
27
+ */
28
+ export async function inlineGraphSubgraph(
29
+ projectDir: string,
30
+ term: string,
31
+ opts: GraphSubgraphOptions,
32
+ ): Promise<string | null> {
33
+ if (!term || !term.trim()) return null;
34
+
35
+ try {
36
+ const { graphQuery, graphStatus } = await import("@gsd-build/mcp-server") as {
37
+ graphQuery: (projectDir: string, term: string, budget?: number) => Promise<GraphQueryResult>;
38
+ graphStatus: (projectDir: string) => Promise<GraphStatusResult>;
39
+ };
40
+
41
+ const result = await graphQuery(projectDir, term, opts.budget);
42
+ if (result.nodes.length === 0) return null;
43
+
44
+ // Check staleness for annotation
45
+ let staleAnnotation = "";
46
+ try {
47
+ const status = await graphStatus(projectDir);
48
+ if (status.exists && status.stale && status.ageHours !== undefined) {
49
+ const hours = Math.round(status.ageHours);
50
+ staleAnnotation = `\n> ⚠ Graph last built ${hours}h ago — context may be outdated`;
51
+ }
52
+ } catch {
53
+ // Non-fatal — skip annotation on error
54
+ }
55
+
56
+ // Format nodes as a compact list
57
+ const nodeLines = result.nodes.map((n) => {
58
+ const desc = n.description ? ` — ${n.description}` : "";
59
+ return `- **${n.label}** (\`${n.type}\`, ${n.confidence})${desc}`;
60
+ });
61
+
62
+ // Format edges as relations (only if present)
63
+ const edgeLines = result.edges.length > 0
64
+ ? result.edges.map((e) => `- \`${e.from}\` →[${e.type}]→ \`${e.to}\``)
65
+ : [];
66
+
67
+ const sections: string[] = [
68
+ `### Knowledge Graph Context (term: "${term}")`,
69
+ `Source: \`.gsd/graphs/graph.json\``,
70
+ staleAnnotation,
71
+ "",
72
+ `**Nodes (${result.nodes.length}):**`,
73
+ ...nodeLines,
74
+ ];
75
+
76
+ if (edgeLines.length > 0) {
77
+ sections.push("", `**Relations (${result.edges.length}):**`, ...edgeLines);
78
+ }
79
+
80
+ return sections.filter((l) => l !== undefined).join("\n");
81
+ } catch (err) {
82
+ logWarning("prompt", `inlineGraphSubgraph failed (non-fatal): ${err instanceof Error ? err.message : String(err)}`);
83
+ return null;
84
+ }
85
+ }
@@ -4,6 +4,21 @@
4
4
  //
5
5
  // Exposes a unified sync API for decisions and requirements storage.
6
6
  // Schema is initialized on first open with WAL mode for file-backed DBs.
7
+ //
8
+ // ─── Single-writer invariant ─────────────────────────────────────────────
9
+ // This file is the ONLY place in the codebase that issues write SQL
10
+ // (INSERT / UPDATE / DELETE / REPLACE / BEGIN-COMMIT transactions) against
11
+ // the engine database at `.gsd/gsd.db`. All other modules must call the
12
+ // typed wrappers exported here. The structural test
13
+ // `tests/single-writer-invariant.test.ts` fails CI if a new bypass appears.
14
+ //
15
+ // `_getAdapter()` is retained for read-only SELECTs in query modules
16
+ // (context-store, memory-store queries, doctor checks, projections).
17
+ // Do NOT use it for writes — add a wrapper here instead.
18
+ //
19
+ // The separate `.gsd/unit-claims.db` managed by `unit-ownership.ts` is an
20
+ // intentionally independent store for cross-worktree claim races and is
21
+ // excluded from this invariant.
7
22
 
8
23
  import { createRequire } from "node:module";
9
24
  import { existsSync, copyFileSync, mkdirSync, realpathSync } from "node:fs";
@@ -12,6 +27,10 @@ import type { Decision, Requirement, GateRow, GateId, GateScope, GateStatus, Gat
12
27
  import { GSDError, GSD_STALE_STATE } from "./errors.js";
13
28
  import { getGateIdsForTurn, type OwnerTurn } from "./gate-registry.js";
14
29
  import { logError, logWarning } from "./workflow-logger.js";
30
+ // Type-only import to avoid a circular runtime dep. The runtime side of
31
+ // workflow-manifest.ts depends on this file, but the StateManifest type is
32
+ // pure structure with no runtime coupling.
33
+ import type { StateManifest } from "./workflow-manifest.js";
15
34
 
16
35
  const _require = createRequire(import.meta.url);
17
36
 
@@ -922,6 +941,48 @@ export function transaction<T>(fn: () => T): T {
922
941
  }
923
942
  }
924
943
 
944
+ /**
945
+ * Wrap a block of reads in a DEFERRED transaction so that all SELECTs observe
946
+ * a consistent snapshot of the DB even if a concurrent writer commits between
947
+ * them. Use this for multi-query read flows (e.g. tool executors that query
948
+ * milestone + slices + counts and want one snapshot). Re-entrant — if already
949
+ * inside a transaction, runs fn() without starting a nested one.
950
+ */
951
+ export function readTransaction<T>(fn: () => T): T {
952
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
953
+
954
+ if (_txDepth > 0) {
955
+ _txDepth++;
956
+ try {
957
+ return fn();
958
+ } finally {
959
+ _txDepth--;
960
+ }
961
+ }
962
+
963
+ _txDepth++;
964
+ currentDb.exec("BEGIN DEFERRED");
965
+ try {
966
+ const result = fn();
967
+ currentDb.exec("COMMIT");
968
+ return result;
969
+ } catch (err) {
970
+ try {
971
+ currentDb.exec("ROLLBACK");
972
+ } catch (rollbackErr) {
973
+ // A failed ROLLBACK after a failed read is a split-brain signal —
974
+ // the transaction is in an indeterminate state. Surface it via the
975
+ // logger instead of swallowing it.
976
+ logError("db", "snapshotState ROLLBACK failed", {
977
+ error: (rollbackErr as Error).message,
978
+ });
979
+ }
980
+ throw err;
981
+ } finally {
982
+ _txDepth--;
983
+ }
984
+ }
985
+
925
986
  export function insertDecision(d: Omit<Decision, "seq">): void {
926
987
  if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
927
988
  currentDb.prepare(
@@ -2451,3 +2512,409 @@ export function getPendingGateCountForTurn(
2451
2512
  ): number {
2452
2513
  return getPendingGatesForTurn(milestoneId, sliceId, turn).length;
2453
2514
  }
2515
+
2516
+ // ─── Single-writer bypass wrappers ───────────────────────────────────────
2517
+ // These wrappers exist so modules outside this file never need to call
2518
+ // `_getAdapter()` for writes. Each one is a byte-equivalent replacement for
2519
+ // a raw prepare/run previously issued from another module. Keep them
2520
+ // minimal and direct — they exist to hold SQL text in one place, not to
2521
+ // add new behavior.
2522
+
2523
+ /** Delete a decision row by id. Used by db-writer.ts rollback on disk-write failure. */
2524
+ export function deleteDecisionById(id: string): void {
2525
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2526
+ currentDb.prepare("DELETE FROM decisions WHERE id = :id").run({ ":id": id });
2527
+ }
2528
+
2529
+ /** Delete a requirement row by id. Used by db-writer.ts rollback on disk-write failure. */
2530
+ export function deleteRequirementById(id: string): void {
2531
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2532
+ currentDb.prepare("DELETE FROM requirements WHERE id = :id").run({ ":id": id });
2533
+ }
2534
+
2535
+ /** Delete an artifact row by path. Used by db-writer.ts rollback on disk-write failure. */
2536
+ export function deleteArtifactByPath(path: string): void {
2537
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2538
+ currentDb.prepare("DELETE FROM artifacts WHERE path = :path").run({ ":path": path });
2539
+ }
2540
+
2541
+ /**
2542
+ * Drop all rows from tasks/slices/milestones in dependency order inside a
2543
+ * transaction. Used by `gsd recover` to rebuild engine state from markdown.
2544
+ */
2545
+ export function clearEngineHierarchy(): void {
2546
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2547
+ transaction(() => {
2548
+ currentDb!.exec("DELETE FROM tasks");
2549
+ currentDb!.exec("DELETE FROM slices");
2550
+ currentDb!.exec("DELETE FROM milestones");
2551
+ });
2552
+ }
2553
+
2554
+ /**
2555
+ * INSERT OR IGNORE a slice during event replay (workflow-reconcile.ts).
2556
+ * Strict insert-or-ignore semantics are required here to avoid the
2557
+ * `insertSlice` ON CONFLICT path that could downgrade an already-completed
2558
+ * slice back to 'pending'.
2559
+ */
2560
+ export function insertOrIgnoreSlice(args: {
2561
+ milestoneId: string;
2562
+ sliceId: string;
2563
+ title: string;
2564
+ createdAt: string;
2565
+ }): void {
2566
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2567
+ currentDb.prepare(
2568
+ `INSERT OR IGNORE INTO slices (milestone_id, id, title, status, created_at)
2569
+ VALUES (:mid, :sid, :title, 'pending', :ts)`,
2570
+ ).run({
2571
+ ":mid": args.milestoneId,
2572
+ ":sid": args.sliceId,
2573
+ ":title": args.title,
2574
+ ":ts": args.createdAt,
2575
+ });
2576
+ }
2577
+
2578
+ /**
2579
+ * INSERT OR IGNORE a task during event replay (workflow-reconcile.ts).
2580
+ * Same rationale as `insertOrIgnoreSlice`.
2581
+ */
2582
+ export function insertOrIgnoreTask(args: {
2583
+ milestoneId: string;
2584
+ sliceId: string;
2585
+ taskId: string;
2586
+ title: string;
2587
+ createdAt: string;
2588
+ }): void {
2589
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2590
+ currentDb.prepare(
2591
+ `INSERT OR IGNORE INTO tasks (milestone_id, slice_id, id, title, status, created_at)
2592
+ VALUES (:mid, :sid, :tid, :title, 'pending', :ts)`,
2593
+ ).run({
2594
+ ":mid": args.milestoneId,
2595
+ ":sid": args.sliceId,
2596
+ ":tid": args.taskId,
2597
+ ":title": args.title,
2598
+ ":ts": args.createdAt,
2599
+ });
2600
+ }
2601
+
2602
+ /**
2603
+ * Stamp the `replan_triggered_at` column on a slice. Used by triage-resolution
2604
+ * when a user capture requests a replan so the dispatcher can detect the
2605
+ * trigger via DB in addition to the on-disk REPLAN-TRIGGER.md marker.
2606
+ */
2607
+ export function setSliceReplanTriggeredAt(milestoneId: string, sliceId: string, ts: string): void {
2608
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2609
+ currentDb.prepare(
2610
+ "UPDATE slices SET replan_triggered_at = :ts WHERE milestone_id = :mid AND id = :sid",
2611
+ ).run({ ":ts": ts, ":mid": milestoneId, ":sid": sliceId });
2612
+ }
2613
+
2614
+ /**
2615
+ * INSERT OR REPLACE a quality_gates row. Used by milestone-validation-gates.ts
2616
+ * to persist milestone-level (MV*) gate outcomes after validate-milestone runs.
2617
+ */
2618
+ export function upsertQualityGate(g: {
2619
+ milestoneId: string;
2620
+ sliceId: string;
2621
+ gateId: string;
2622
+ scope: string;
2623
+ taskId: string;
2624
+ status: string;
2625
+ verdict: string;
2626
+ rationale: string;
2627
+ findings: string;
2628
+ evaluatedAt: string;
2629
+ }): void {
2630
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2631
+ currentDb.prepare(
2632
+ `INSERT OR REPLACE INTO quality_gates
2633
+ (milestone_id, slice_id, gate_id, scope, task_id, status, verdict, rationale, findings, evaluated_at)
2634
+ VALUES (:mid, :sid, :gid, :scope, :tid, :status, :verdict, :rationale, :findings, :evaluated_at)`,
2635
+ ).run({
2636
+ ":mid": g.milestoneId,
2637
+ ":sid": g.sliceId,
2638
+ ":gid": g.gateId,
2639
+ ":scope": g.scope,
2640
+ ":tid": g.taskId,
2641
+ ":status": g.status,
2642
+ ":verdict": g.verdict,
2643
+ ":rationale": g.rationale,
2644
+ ":findings": g.findings,
2645
+ ":evaluated_at": g.evaluatedAt,
2646
+ });
2647
+ }
2648
+
2649
+ /**
2650
+ * Atomically replace all workflow state from a manifest. Lifted verbatim from
2651
+ * workflow-manifest.ts so the single-writer invariant holds. Only touches
2652
+ * engine tables + decisions. Does NOT modify artifacts or memories.
2653
+ */
2654
+ export function restoreManifest(manifest: StateManifest): void {
2655
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2656
+ const db = currentDb;
2657
+
2658
+ transaction(() => {
2659
+ // Clear engine tables (order matters for foreign-key-like consistency)
2660
+ db.exec("DELETE FROM verification_evidence");
2661
+ db.exec("DELETE FROM tasks");
2662
+ db.exec("DELETE FROM slices");
2663
+ db.exec("DELETE FROM milestones");
2664
+ db.exec("DELETE FROM decisions WHERE 1=1");
2665
+
2666
+ // Restore milestones
2667
+ const msStmt = db.prepare(
2668
+ `INSERT INTO milestones (id, title, status, depends_on, created_at, completed_at,
2669
+ vision, success_criteria, key_risks, proof_strategy,
2670
+ verification_contract, verification_integration, verification_operational, verification_uat,
2671
+ definition_of_done, requirement_coverage, boundary_map_markdown)
2672
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2673
+ );
2674
+ for (const m of manifest.milestones) {
2675
+ msStmt.run(
2676
+ m.id, m.title, m.status,
2677
+ JSON.stringify(m.depends_on), m.created_at, m.completed_at,
2678
+ m.vision, JSON.stringify(m.success_criteria), JSON.stringify(m.key_risks),
2679
+ JSON.stringify(m.proof_strategy),
2680
+ m.verification_contract, m.verification_integration, m.verification_operational, m.verification_uat,
2681
+ JSON.stringify(m.definition_of_done), m.requirement_coverage, m.boundary_map_markdown,
2682
+ );
2683
+ }
2684
+
2685
+ // Restore slices
2686
+ const slStmt = db.prepare(
2687
+ `INSERT INTO slices (milestone_id, id, title, status, risk, depends, demo,
2688
+ created_at, completed_at, full_summary_md, full_uat_md,
2689
+ goal, success_criteria, proof_level, integration_closure, observability_impact,
2690
+ sequence, replan_triggered_at)
2691
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2692
+ );
2693
+ for (const s of manifest.slices) {
2694
+ slStmt.run(
2695
+ s.milestone_id, s.id, s.title, s.status, s.risk,
2696
+ JSON.stringify(s.depends), s.demo,
2697
+ s.created_at, s.completed_at, s.full_summary_md, s.full_uat_md,
2698
+ s.goal, s.success_criteria, s.proof_level, s.integration_closure, s.observability_impact,
2699
+ s.sequence, s.replan_triggered_at,
2700
+ );
2701
+ }
2702
+
2703
+ // Restore tasks
2704
+ const tkStmt = db.prepare(
2705
+ `INSERT INTO tasks (milestone_id, slice_id, id, title, status,
2706
+ one_liner, narrative, verification_result, duration, completed_at,
2707
+ blocker_discovered, deviations, known_issues, key_files, key_decisions,
2708
+ full_summary_md, description, estimate, files, verify,
2709
+ inputs, expected_output, observability_impact, sequence)
2710
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2711
+ );
2712
+ for (const t of manifest.tasks) {
2713
+ tkStmt.run(
2714
+ t.milestone_id, t.slice_id, t.id, t.title, t.status,
2715
+ t.one_liner, t.narrative, t.verification_result, t.duration, t.completed_at,
2716
+ t.blocker_discovered ? 1 : 0, t.deviations, t.known_issues,
2717
+ JSON.stringify(t.key_files), JSON.stringify(t.key_decisions),
2718
+ t.full_summary_md, t.description, t.estimate, JSON.stringify(t.files), t.verify,
2719
+ JSON.stringify(t.inputs), JSON.stringify(t.expected_output),
2720
+ t.observability_impact, t.sequence,
2721
+ );
2722
+ }
2723
+
2724
+ // Restore decisions
2725
+ const dcStmt = db.prepare(
2726
+ `INSERT INTO decisions (seq, id, when_context, scope, decision, choice, rationale, revisable, made_by, superseded_by)
2727
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
2728
+ );
2729
+ for (const d of manifest.decisions) {
2730
+ 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);
2731
+ }
2732
+
2733
+ // Restore verification evidence
2734
+ const evStmt = db.prepare(
2735
+ `INSERT INTO verification_evidence (task_id, slice_id, milestone_id, command, exit_code, verdict, duration_ms, created_at)
2736
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
2737
+ );
2738
+ for (const e of manifest.verification_evidence) {
2739
+ evStmt.run(e.task_id, e.slice_id, e.milestone_id, e.command, e.exit_code, e.verdict, e.duration_ms, e.created_at);
2740
+ }
2741
+ });
2742
+ }
2743
+
2744
+ // ─── Legacy markdown → DB bulk migration ─────────────────────────────────
2745
+
2746
+ export interface LegacyMilestoneInsert {
2747
+ id: string;
2748
+ title: string;
2749
+ status: string;
2750
+ }
2751
+
2752
+ export interface LegacySliceInsert {
2753
+ id: string;
2754
+ milestoneId: string;
2755
+ title: string;
2756
+ status: string;
2757
+ risk: string;
2758
+ sequence: number;
2759
+ }
2760
+
2761
+ export interface LegacyTaskInsert {
2762
+ id: string;
2763
+ sliceId: string;
2764
+ milestoneId: string;
2765
+ title: string;
2766
+ status: string;
2767
+ sequence: number;
2768
+ }
2769
+
2770
+ /**
2771
+ * Bulk delete + insert a legacy milestone hierarchy for markdown → DB migration.
2772
+ * Used by workflow-migration.ts to populate engine tables from parsed ROADMAP/PLAN
2773
+ * files. All operations run inside a single transaction.
2774
+ */
2775
+ export function bulkInsertLegacyHierarchy(payload: {
2776
+ milestones: LegacyMilestoneInsert[];
2777
+ slices: LegacySliceInsert[];
2778
+ tasks: LegacyTaskInsert[];
2779
+ clearMilestoneIds: string[];
2780
+ createdAt: string;
2781
+ }): void {
2782
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2783
+ const db = currentDb;
2784
+ const { milestones, slices, tasks, clearMilestoneIds, createdAt } = payload;
2785
+
2786
+ if (clearMilestoneIds.length === 0) return;
2787
+ const placeholders = clearMilestoneIds.map(() => "?").join(",");
2788
+
2789
+ transaction(() => {
2790
+ db.prepare(`DELETE FROM tasks WHERE milestone_id IN (${placeholders})`).run(...clearMilestoneIds);
2791
+ db.prepare(`DELETE FROM slices WHERE milestone_id IN (${placeholders})`).run(...clearMilestoneIds);
2792
+ db.prepare(`DELETE FROM milestones WHERE id IN (${placeholders})`).run(...clearMilestoneIds);
2793
+
2794
+ const insertMilestone = db.prepare(
2795
+ "INSERT INTO milestones (id, title, status, created_at) VALUES (?, ?, ?, ?)",
2796
+ );
2797
+ for (const m of milestones) {
2798
+ insertMilestone.run(m.id, m.title, m.status, createdAt);
2799
+ }
2800
+
2801
+ const insertSliceStmt = db.prepare(
2802
+ "INSERT INTO slices (id, milestone_id, title, status, risk, depends, sequence, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
2803
+ );
2804
+ for (const s of slices) {
2805
+ insertSliceStmt.run(s.id, s.milestoneId, s.title, s.status, s.risk, "[]", s.sequence, createdAt);
2806
+ }
2807
+
2808
+ const insertTaskStmt = db.prepare(
2809
+ "INSERT INTO tasks (id, slice_id, milestone_id, title, description, status, estimate, files, sequence) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
2810
+ );
2811
+ for (const t of tasks) {
2812
+ insertTaskStmt.run(t.id, t.sliceId, t.milestoneId, t.title, "", t.status, "", "[]", t.sequence);
2813
+ }
2814
+ });
2815
+ }
2816
+
2817
+ // ─── Memory store writers ────────────────────────────────────────────────
2818
+ // All memory writes go through gsd-db.ts so the single-writer invariant
2819
+ // holds. These are direct pass-throughs to the SQL previously in
2820
+ // memory-store.ts — same bindings, same behavior.
2821
+
2822
+ export function insertMemoryRow(args: {
2823
+ id: string;
2824
+ category: string;
2825
+ content: string;
2826
+ confidence: number;
2827
+ sourceUnitType: string | null;
2828
+ sourceUnitId: string | null;
2829
+ createdAt: string;
2830
+ updatedAt: string;
2831
+ }): void {
2832
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2833
+ currentDb.prepare(
2834
+ `INSERT INTO memories (id, category, content, confidence, source_unit_type, source_unit_id, created_at, updated_at)
2835
+ VALUES (:id, :category, :content, :confidence, :source_unit_type, :source_unit_id, :created_at, :updated_at)`,
2836
+ ).run({
2837
+ ":id": args.id,
2838
+ ":category": args.category,
2839
+ ":content": args.content,
2840
+ ":confidence": args.confidence,
2841
+ ":source_unit_type": args.sourceUnitType,
2842
+ ":source_unit_id": args.sourceUnitId,
2843
+ ":created_at": args.createdAt,
2844
+ ":updated_at": args.updatedAt,
2845
+ });
2846
+ }
2847
+
2848
+ export function rewriteMemoryId(placeholderId: string, realId: string): void {
2849
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2850
+ currentDb.prepare("UPDATE memories SET id = :real_id WHERE id = :placeholder").run({
2851
+ ":real_id": realId,
2852
+ ":placeholder": placeholderId,
2853
+ });
2854
+ }
2855
+
2856
+ export function updateMemoryContentRow(
2857
+ id: string,
2858
+ content: string,
2859
+ confidence: number | undefined,
2860
+ updatedAt: string,
2861
+ ): void {
2862
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2863
+ if (confidence != null) {
2864
+ currentDb.prepare(
2865
+ "UPDATE memories SET content = :content, confidence = :confidence, updated_at = :updated_at WHERE id = :id",
2866
+ ).run({ ":content": content, ":confidence": confidence, ":updated_at": updatedAt, ":id": id });
2867
+ } else {
2868
+ currentDb.prepare(
2869
+ "UPDATE memories SET content = :content, updated_at = :updated_at WHERE id = :id",
2870
+ ).run({ ":content": content, ":updated_at": updatedAt, ":id": id });
2871
+ }
2872
+ }
2873
+
2874
+ export function incrementMemoryHitCount(id: string, updatedAt: string): void {
2875
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2876
+ currentDb.prepare(
2877
+ "UPDATE memories SET hit_count = hit_count + 1, updated_at = :updated_at WHERE id = :id",
2878
+ ).run({ ":updated_at": updatedAt, ":id": id });
2879
+ }
2880
+
2881
+ export function supersedeMemoryRow(oldId: string, newId: string, updatedAt: string): void {
2882
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2883
+ currentDb.prepare(
2884
+ "UPDATE memories SET superseded_by = :new_id, updated_at = :updated_at WHERE id = :old_id",
2885
+ ).run({ ":new_id": newId, ":updated_at": updatedAt, ":old_id": oldId });
2886
+ }
2887
+
2888
+ export function markMemoryUnitProcessed(
2889
+ unitKey: string,
2890
+ activityFile: string,
2891
+ processedAt: string,
2892
+ ): void {
2893
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2894
+ currentDb.prepare(
2895
+ `INSERT OR IGNORE INTO memory_processed_units (unit_key, activity_file, processed_at)
2896
+ VALUES (:key, :file, :at)`,
2897
+ ).run({ ":key": unitKey, ":file": activityFile, ":at": processedAt });
2898
+ }
2899
+
2900
+ export function decayMemoriesBefore(cutoffTs: string, now: string): void {
2901
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2902
+ currentDb.prepare(
2903
+ `UPDATE memories
2904
+ SET confidence = MAX(0.1, confidence - 0.1), updated_at = :now
2905
+ WHERE superseded_by IS NULL AND updated_at < :cutoff AND confidence > 0.1`,
2906
+ ).run({ ":now": now, ":cutoff": cutoffTs });
2907
+ }
2908
+
2909
+ export function supersedeLowestRankedMemories(limit: number, now: string): void {
2910
+ if (!currentDb) throw new GSDError(GSD_STALE_STATE, "gsd-db: No database open");
2911
+ currentDb.prepare(
2912
+ `UPDATE memories SET superseded_by = 'CAP_EXCEEDED', updated_at = :now
2913
+ WHERE id IN (
2914
+ SELECT id FROM memories
2915
+ WHERE superseded_by IS NULL
2916
+ ORDER BY (confidence * (1.0 + hit_count * 0.1)) ASC
2917
+ LIMIT :limit
2918
+ )`,
2919
+ ).run({ ":now": now, ":limit": limit });
2920
+ }
@@ -16,6 +16,22 @@ export {
16
16
  } from "./bootstrap/write-gate.js";
17
17
 
18
18
  export default async function registerExtension(pi: ExtensionAPI) {
19
- const { registerGsdExtension } = await import("./bootstrap/register-extension.js");
20
- registerGsdExtension(pi);
19
+ // Always register the core /gsd command first, in isolation.
20
+ // This ensures /gsd is available even if the full bootstrap (shortcuts,
21
+ // tools, hooks) fails — e.g. due to a Windows-specific import error.
22
+ const { registerGSDCommand } = await import("./commands/index.js");
23
+ registerGSDCommand(pi);
24
+
25
+ // Full setup (shortcuts, tools, hooks) in a separate try/catch so that
26
+ // any platform-specific load failure doesn't take out the core command.
27
+ try {
28
+ const { registerGsdExtension } = await import("./bootstrap/register-extension.js");
29
+ registerGsdExtension(pi);
30
+ } catch (err) {
31
+ const { logWarning } = await import("./workflow-logger.js");
32
+ logWarning(
33
+ "bootstrap",
34
+ `Extension setup partially failed — /gsd commands are available but shortcuts/tools may be missing: ${err instanceof Error ? err.message : String(err)}`,
35
+ );
36
+ }
21
37
  }
@@ -16,6 +16,7 @@ import {
16
16
  insertTask,
17
17
  openDatabase,
18
18
  transaction,
19
+ updateSliceStatus,
19
20
  _getAdapter,
20
21
  } from './gsd-db.js';
21
22
  import {
@@ -672,11 +673,8 @@ export function migrateHierarchyToDb(basePath: string): {
672
673
  return t.done && existsSync(summaryFile);
673
674
  });
674
675
  if (allTasksDone && hasSliceSummary) {
675
- const adapter = _getAdapter();
676
- if (adapter) {
677
- adapter.prepare(
678
- `UPDATE slices SET status = 'complete' WHERE id = :sid AND milestone_id = :mid`,
679
- ).run({ ':sid': sliceEntry.id, ':mid': milestoneId });
676
+ if (_getAdapter()) {
677
+ updateSliceStatus(milestoneId, sliceEntry.id, 'complete');
680
678
  process.stderr.write(
681
679
  `gsd-migrate: ${milestoneId}/${sliceEntry.id} all tasks + slice summary complete — upgrading slice to complete\n`,
682
680
  );