@igorjpimenta/manifold 0.0.0-bootstrap.1 → 0.0.0-stage

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 (210) hide show
  1. package/README.md +2 -1583
  2. package/package.json +4 -49
  3. package/.migrations/ops/0000_futuristic_karma.sql +0 -111
  4. package/.migrations/ops/0001_low_maximus.sql +0 -7
  5. package/.migrations/ops/meta/0000_snapshot.json +0 -763
  6. package/.migrations/ops/meta/0001_snapshot.json +0 -173
  7. package/.migrations/ops/meta/_journal.json +0 -20
  8. package/.migrations/runtime/0000_real_boomer.sql +0 -180
  9. package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
  10. package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
  11. package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
  12. package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
  13. package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
  14. package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
  15. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
  16. package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
  17. package/.migrations/runtime/0009_lying_shocker.sql +0 -96
  18. package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
  19. package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
  20. package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
  21. package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
  22. package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
  23. package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
  24. package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
  25. package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
  26. package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
  27. package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
  28. package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
  29. package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
  30. package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
  31. package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
  32. package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
  33. package/.migrations/runtime/meta/_journal.json +0 -97
  34. package/RELEASING.md +0 -89
  35. package/agent/fleet.js +0 -100
  36. package/bin/cli.js +0 -854
  37. package/bin/manifold.js +0 -4
  38. package/boot/flow-revival.js +0 -536
  39. package/budget/index.js +0 -495
  40. package/budget/lib.js +0 -59
  41. package/dashboard/cost.js +0 -528
  42. package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
  43. package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
  44. package/dashboard/dist/index.html +0 -13
  45. package/dashboard/event-log.js +0 -267
  46. package/dashboard/index.html +0 -12
  47. package/dashboard/package-lock.json +0 -2877
  48. package/dashboard/package.json +0 -37
  49. package/dashboard/pipeline-graph.js +0 -131
  50. package/dashboard/postcss.config.js +0 -6
  51. package/dashboard/search.js +0 -183
  52. package/dashboard/server.js +0 -1015
  53. package/dashboard/src/App.tsx +0 -42
  54. package/dashboard/src/api/client.ts +0 -118
  55. package/dashboard/src/api/types.ts +0 -510
  56. package/dashboard/src/api/ws.tsx +0 -112
  57. package/dashboard/src/components/CancelButton.tsx +0 -63
  58. package/dashboard/src/components/ClickToCopy.tsx +0 -68
  59. package/dashboard/src/components/CopyButton.tsx +0 -61
  60. package/dashboard/src/components/DailyCostTrend.tsx +0 -85
  61. package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
  62. package/dashboard/src/components/Funnel.tsx +0 -149
  63. package/dashboard/src/components/GateForm.tsx +0 -158
  64. package/dashboard/src/components/GraphView.tsx +0 -302
  65. package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
  66. package/dashboard/src/components/Histogram.tsx +0 -292
  67. package/dashboard/src/components/Layout.tsx +0 -28
  68. package/dashboard/src/components/NeedsAttention.tsx +0 -98
  69. package/dashboard/src/components/NodeContextCard.tsx +0 -132
  70. package/dashboard/src/components/PathTrail.tsx +0 -35
  71. package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
  72. package/dashboard/src/components/RerunButton.tsx +0 -46
  73. package/dashboard/src/components/RunComparison.tsx +0 -53
  74. package/dashboard/src/components/Sparkline.tsx +0 -94
  75. package/dashboard/src/components/StatStrip.tsx +0 -147
  76. package/dashboard/src/components/StateTree.tsx +0 -84
  77. package/dashboard/src/components/StatusBadge.tsx +0 -18
  78. package/dashboard/src/components/SubflowPathPills.tsx +0 -38
  79. package/dashboard/src/components/SubflowSummary.tsx +0 -24
  80. package/dashboard/src/components/SubflowsCard.tsx +0 -327
  81. package/dashboard/src/components/TodayStrip.tsx +0 -86
  82. package/dashboard/src/components/TranscriptPane.tsx +0 -99
  83. package/dashboard/src/components/TranscriptRow.tsx +0 -69
  84. package/dashboard/src/components/ui/badge.tsx +0 -38
  85. package/dashboard/src/components/ui/button.tsx +0 -28
  86. package/dashboard/src/components/ui/card.tsx +0 -42
  87. package/dashboard/src/components/ui/skeleton.tsx +0 -9
  88. package/dashboard/src/components/ui/table.tsx +0 -62
  89. package/dashboard/src/index.css +0 -26
  90. package/dashboard/src/lib/anomaly.ts +0 -42
  91. package/dashboard/src/lib/format.ts +0 -30
  92. package/dashboard/src/lib/step-history.ts +0 -13
  93. package/dashboard/src/lib/subflow.ts +0 -154
  94. package/dashboard/src/lib/use-click-outside.ts +0 -28
  95. package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
  96. package/dashboard/src/lib/utils.ts +0 -6
  97. package/dashboard/src/main.tsx +0 -10
  98. package/dashboard/src/types/shims.d.ts +0 -4
  99. package/dashboard/src/views/Home.tsx +0 -478
  100. package/dashboard/src/views/PipelineDetail.tsx +0 -1185
  101. package/dashboard/src/views/PipelinesList.tsx +0 -111
  102. package/dashboard/src/views/RunDetail.tsx +0 -248
  103. package/dashboard/src/views/Search.tsx +0 -196
  104. package/dashboard/stats.js +0 -668
  105. package/dashboard/tailwind.config.ts +0 -43
  106. package/dashboard/tree-stats.js +0 -12
  107. package/dashboard/tsconfig.json +0 -25
  108. package/dashboard/views.js +0 -231
  109. package/dashboard/vite.config.ts +0 -34
  110. package/drizzle.config.js +0 -12
  111. package/drizzle.config.ops.js +0 -12
  112. package/elevation.js +0 -116
  113. package/engine/admission-promotion-resume.js +0 -127
  114. package/engine/agent-dispatch.js +0 -64
  115. package/engine/child-resume-trigger.js +0 -315
  116. package/engine/executor-budget.js +0 -87
  117. package/engine/executor-cancel.js +0 -83
  118. package/engine/executor-errors.js +0 -122
  119. package/engine/executor-helpers.js +0 -254
  120. package/engine/executor-nodes.js +0 -292
  121. package/engine/executor-persist.js +0 -212
  122. package/engine/executor-subflow-children.js +0 -275
  123. package/engine/executor.js +0 -1013
  124. package/engine/global-concurrency.js +0 -33
  125. package/engine/loader.js +0 -528
  126. package/engine/needs-resolver.js +0 -229
  127. package/engine/pipelines-watcher.js +0 -133
  128. package/engine/result-replay.js +0 -34
  129. package/engine/revival-messages.js +0 -11
  130. package/engine/spawn-child.js +0 -55
  131. package/engine/wait-for-flow-settlement.js +0 -109
  132. package/events/catalog.js +0 -26
  133. package/events/emit.js +0 -44
  134. package/events/kinds.js +0 -118
  135. package/events/serve-ws.js +0 -74
  136. package/events/stream.js +0 -35
  137. package/events/ws-dialog.js +0 -257
  138. package/events/ws-frame.js +0 -194
  139. package/events/ws-run-events.js +0 -70
  140. package/events.js +0 -32
  141. package/gates/gate-resolver.js +0 -576
  142. package/gates/paused-flows.js +0 -55
  143. package/graphql/columns.js +0 -67
  144. package/graphql/schema.js +0 -333
  145. package/index.js +0 -49
  146. package/mcp/http.js +0 -159
  147. package/mcp/identity.js +0 -94
  148. package/mcp/stdio.js +0 -80
  149. package/mcp/tools.js +0 -245
  150. package/mcp/wire-ops.js +0 -178
  151. package/routes/bearer-auth.js +0 -57
  152. package/routes/discuss-audio.js +0 -87
  153. package/routes/discuss-component.js +0 -39
  154. package/routes/discuss-draft.js +0 -159
  155. package/routes/discuss-helpers.js +0 -20
  156. package/routes/discuss.js +0 -848
  157. package/routes/failures.js +0 -104
  158. package/routes/graphql.js +0 -61
  159. package/routes/health.js +0 -54
  160. package/routes/operator-discuss.js +0 -193
  161. package/routes/operator-resume.js +0 -32
  162. package/routes/operator-retry.js +0 -301
  163. package/routes/operator.js +0 -539
  164. package/routes/pipelines.js +0 -118
  165. package/routes/runs.js +0 -1129
  166. package/scripts/ci/import-check.mjs +0 -50
  167. package/scripts/link-core-self.mjs +0 -31
  168. package/scripts/with-guard.sh +0 -24
  169. package/server/app.js +0 -503
  170. package/server/registry.js +0 -53
  171. package/server/serve.js +0 -163
  172. package/state/admission-queue-store.js +0 -325
  173. package/state/attribution-registries.js +0 -102
  174. package/state/batch-writer.js +0 -162
  175. package/state/db-guard.js +0 -63
  176. package/state/db-health.js +0 -27
  177. package/state/db.js +0 -122
  178. package/state/discuss-store.js +0 -299
  179. package/state/failure-store.js +0 -427
  180. package/state/migrate.js +0 -104
  181. package/state/node-cost.js +0 -67
  182. package/state/node-executions.js +0 -775
  183. package/state/node-health-store.js +0 -124
  184. package/state/ops-merge.js +0 -93
  185. package/state/paths.js +0 -146
  186. package/state/pipeline-dirs.js +0 -99
  187. package/state/pipeline-registry.js +0 -449
  188. package/state/run-store.js +0 -697
  189. package/state/schema.js +0 -438
  190. package/state/schema.ops.js +0 -44
  191. package/state/session-index.js +0 -177
  192. package/state/session-keys.js +0 -39
  193. package/state/state-store.js +0 -311
  194. package/state/subflow-state.js +0 -126
  195. package/state/tool-handoff.js +0 -169
  196. package/telemetry/budget-alert.js +0 -353
  197. package/telemetry/cli.js +0 -331
  198. package/telemetry/grammar.js +0 -119
  199. package/telemetry/http-budget.js +0 -132
  200. package/telemetry/http-cost.js +0 -670
  201. package/telemetry/http-helpers.js +0 -134
  202. package/telemetry/http-tree.js +0 -349
  203. package/telemetry/http.js +0 -44
  204. package/telemetry/index.js +0 -307
  205. package/telemetry/ingest.js +0 -134
  206. package/telemetry/labels.js +0 -115
  207. package/telemetry/pricer.js +0 -77
  208. package/telemetry/resolver.js +0 -314
  209. package/telemetry/store.js +0 -147
  210. package/utils/run-patch.js +0 -43
@@ -1,162 +0,0 @@
1
- // batch-writer.js — decouples "write happened" from "write persisted" for
2
- // fire-and-forget SQLite writes, and coalesces many of them into one
3
- // db.batch() round-trip (one transaction, one write-lock acquisition)
4
- // instead of one round-trip per write.
5
- //
6
- // Why: state/*.js stores issue one INSERT/UPDATE per event (one per node
7
- // dispatch, one per LLM call, ...) as its own immediate, individually-
8
- // awaited statement. Under concurrent pipeline load that's N separate write-
9
- // lock acquisitions competing against the same file's busy_timeout (db.js).
10
- // Every call site this is wired into was ALREADY fire-and-forget by its own
11
- // contract (errors swallowed, no caller inspects a return value or re-reads
12
- // the row in the same call) — see each store's own "fire-and-forget" header
13
- // comments. This module doesn't change that contract; it just changes N
14
- // individual transactions into ceil(N / maxBatchSize) batched ones.
15
- //
16
- // Only enqueue a write here if it's genuinely fire-and-forget: nothing that
17
- // reads the row back, and no caller inspecting a return value. Anything
18
- // with a compare-and-swap / read-after-write dependency (run-store's
19
- // casSet*, node-executions.enter()/writeResult(), failure-store
20
- // .upsertFailureRow()) must NOT be routed through this — deferring those
21
- // breaks the correctness they depend on.
22
- //
23
- // Ordering: one FIFO queue per dbPath. db.batch() executes its array in
24
- // order, inside one transaction, so same-row writes queued in the same
25
- // flush window still apply last-enqueued-wins, same as today's sequential
26
- // awaits would.
27
-
28
- import { getSharedDb, withSharedWrite } from "./db.js";
29
-
30
- const DEFAULT_MAX_BATCH_SIZE = 50;
31
- const DEFAULT_FLUSH_INTERVAL_MS = 200;
32
-
33
- const _queues = new Map(); // dbPath -> { items: Array<query>, timer, maxBatchSize, flushIntervalMs, logger, flushing: Promise|null }
34
-
35
- function _queueFor(dbPath, opts) {
36
- let q = _queues.get(dbPath);
37
- if (!q) {
38
- q = {
39
- items: [],
40
- timer: null,
41
- maxBatchSize: opts?.maxBatchSize ?? DEFAULT_MAX_BATCH_SIZE,
42
- flushIntervalMs: opts?.flushIntervalMs ?? DEFAULT_FLUSH_INTERVAL_MS,
43
- logger: opts?.logger ?? null,
44
- flushing: null,
45
- };
46
- _queues.set(dbPath, q);
47
- } else if (opts) {
48
- // Later enqueue() calls can supply a logger/tuning even if an earlier
49
- // call created the queue first — last one wins, matches how most of
50
- // this codebase's optional-opts-on-a-shared-resource pattern behaves
51
- // elsewhere (e.g. getSharedDb's cache doesn't re-validate opts either).
52
- if (opts.logger) q.logger = opts.logger;
53
- }
54
- return q;
55
- }
56
-
57
- async function _flush(dbPath) {
58
- const q = _queues.get(dbPath);
59
- if (!q || q.items.length === 0) return;
60
- if (q.timer) { clearTimeout(q.timer); q.timer = null; }
61
-
62
- const batch = q.items;
63
- q.items = [];
64
-
65
- const run = (async () => {
66
- try {
67
- const { db, client } = await getSharedDb(dbPath);
68
- // Two item shapes share one queue/timer per dbPath: un-awaited
69
- // Drizzle query builders (db.insert(table).values(...), the majority
70
- // — node-executions.js etc.) and raw {__raw, sql, args} statements
71
- // (enqueueRaw(), for stores like telemetry/store.js that never touch
72
- // Drizzle at all). They can't be mixed in one batch() call — Drizzle's
73
- // db.batch() only accepts its own BatchItem type, and the raw
74
- // client's batch() only accepts {sql, args}-shaped statements — so a
75
- // flush that happens to contain both partitions and sends two
76
- // round-trips instead of one. In practice a given dbPath's queue is
77
- // always homogeneous (each store only ever produces one item shape),
78
- // so this split costs nothing in the common case.
79
- const drizzleItems = batch.filter((item) => !(item && item.__raw));
80
- const rawItems = batch.filter((item) => item && item.__raw);
81
- // Each batch() call is already its own transaction — serialized and
82
- // retried on SQLITE_BUSY independently via withSharedWrite (state/
83
- // db.js), so a retry of one can never re-run the other's already-
84
- // committed statements, and this flush now serializes against every
85
- // other writer sharing this connection (run-store.js's CAS writes,
86
- // admission-queue-store.js's transactions), not just other flushes.
87
- if (drizzleItems.length > 0) await withSharedWrite(dbPath, () => db.batch(drizzleItems));
88
- if (rawItems.length > 0) await withSharedWrite(dbPath, () => client.batch(rawItems.map((item) => ({ sql: item.sql, args: item.args }))));
89
- } catch (err) {
90
- // Matches the swallow-on-error contract every batchable call site
91
- // already has (writes here were already fire-and-forget with errors
92
- // swallowed before batching existed) — a batch failure is logged, not
93
- // thrown, and the caller that originally enqueued each item never
94
- // sees it, same as before.
95
- q.logger?.warn?.(`batch-writer: flush failed for ${dbPath} (${batch.length} item(s) dropped): ${err?.message ?? err}`);
96
- }
97
- })();
98
-
99
- q.flushing = run;
100
- await run;
101
- if (q.flushing === run) q.flushing = null;
102
- }
103
-
104
- // enqueue(dbPath, query, opts?) — query is an UN-AWAITED Drizzle query
105
- // builder (e.g. db.insert(table).values({...})). Do not await the query
106
- // before passing it in — awaiting it executes it immediately instead of
107
- // deferring. For raw client.execute()-shaped statements, use enqueueRaw()
108
- // below instead — the two aren't interchangeable at flush time.
109
- //
110
- // opts: { maxBatchSize?, flushIntervalMs?, logger? } — only consulted the
111
- // first time a dbPath's queue is created; later callers share the same
112
- // queue/timer.
113
- export function enqueue(dbPath, query, opts) {
114
- const q = _queueFor(dbPath, opts);
115
- q.items.push(query);
116
-
117
- if (q.items.length >= q.maxBatchSize) {
118
- // Size-triggered: flush now rather than waiting for the timer. Fire-
119
- // and-forget from the caller's perspective — enqueue() itself stays
120
- // synchronous so call sites don't need to await it (matching the
121
- // un-awaited flow_node_executions.write() call sites this was built for).
122
- void _flush(dbPath);
123
- return;
124
- }
125
-
126
- if (!q.timer) {
127
- q.timer = setTimeout(() => { void _flush(dbPath); }, q.flushIntervalMs);
128
- q.timer.unref?.();
129
- }
130
- }
131
-
132
- // enqueueRaw(dbPath, sql, args, opts?) — same contract as enqueue(), for
133
- // stores that issue raw client.execute({sql, args}) calls instead of going
134
- // through Drizzle (e.g. telemetry/store.js). Flushed via the raw client's
135
- // own batch() method, not Drizzle's — see _flush()'s partitioning comment.
136
- export function enqueueRaw(dbPath, sql, args, opts) {
137
- enqueue(dbPath, { __raw: true, sql, args }, opts);
138
- }
139
-
140
- // flush(dbPath) — force-drain one dbPath's queue now. Awaits any flush
141
- // already in flight first, so a caller that enqueues then immediately
142
- // flushes doesn't race an in-progress batch. For tests (assert-after-write)
143
- // and the shutdown hook (drain before the gateway exits).
144
- export async function flush(dbPath) {
145
- const q = _queues.get(dbPath);
146
- if (q?.flushing) await q.flushing;
147
- await _flush(dbPath);
148
- }
149
-
150
- // flushAll() — drain every dbPath's queue. Used by the shutdown service hook.
151
- export async function flushAll() {
152
- await Promise.all([..._queues.keys()].map((dbPath) => flush(dbPath)));
153
- }
154
-
155
- // Exposed for tests only — clears in-memory queue state without flushing,
156
- // so test suites using isolated per-test dbPaths don't leak queued items
157
- // (or timers) into a later test that happens to reuse a temp path.
158
- export function _resetForTests(dbPath) {
159
- const q = _queues.get(dbPath);
160
- if (q?.timer) clearTimeout(q.timer);
161
- _queues.delete(dbPath);
162
- }
package/state/db-guard.js DELETED
@@ -1,63 +0,0 @@
1
- // db-guard.js — fail-closed corruption handling.
2
- //
3
- // Transient storage hiccups (BUSY/locked/timeout) keep existing retry and
4
- // degrade paths. Corruption-class failures (CORRUPT/IOERR/NOTADB/MALFORMED)
5
- // are different: running blind mints wrong gate decisions, drops pushes,
6
- // and loses failure records. So they count, and a short burst trips the
7
- // breaker: corruption alert on disk, loud log, process exit. Launchd
8
- // respawns into the boot probe, which refuses until the files are
9
- // healthy — a down gateway with an alert beats a blind one taking
10
- // operator decisions.
11
- //
12
- // The counter is process-wide across all callers; a single blip never
13
- // trips it. Tunables live here (not config) because flapping the
14
- // breaker open from config would defeat it.
15
- import fs from "node:fs";
16
- import path from "node:path";
17
- import os from "node:os";
18
-
19
- export const CORRUPT_CLASS = /(CORRUPT|IOERR|NOTADB|MALFORMED|database disk image is malformed)/i;
20
- export const TRIP_THRESHOLD = 3;
21
- export const TRIP_WINDOW_MS = 60_000;
22
-
23
- let failures = [];
24
- let exitFn = (code) => process.exit(code);
25
-
26
- export function __setExitFn(fn) { exitFn = fn; }
27
- export function __resetGuard() { failures = []; }
28
-
29
- export function isCorruptClass(err) {
30
- const hay = `${err?.code ?? ""} ${err?.message ?? err ?? ""}`;
31
- return CORRUPT_CLASS.test(hay);
32
- }
33
-
34
- function writeAlert(detail) {
35
- const dir = path.join(os.homedir(), ".openclaw", "logs");
36
- try {
37
- fs.mkdirSync(dir, { recursive: true });
38
- fs.writeFileSync(
39
- path.join(dir, "SQLITE_CORRUPTION_ALERT"),
40
- JSON.stringify({ at: new Date().toISOString(), ...detail }) + "\n",
41
- );
42
- } catch {}
43
- }
44
-
45
- export { writeAlert as writeCorruptionAlert };
46
-
47
- // Returns true when this failure tripped the breaker (caller has
48
- // already been told to stop via exit). Never throws.
49
- export function noteDbFailure(err, { dbPath = "unknown", logger = null } = {}) {
50
- if (!isCorruptClass(err)) return false;
51
- const now = Date.now();
52
- failures = failures.filter((t) => now - t < TRIP_WINDOW_MS);
53
- failures.push(now);
54
- logger?.warn?.(`db-guard: corrupt-class failure ${failures.length}/${TRIP_THRESHOLD} on ${dbPath}: ${err?.message ?? err}`);
55
- if (failures.length < TRIP_THRESHOLD) return false;
56
- const detail = { dbPath, failuresInWindow: failures.length, error: String(err?.message ?? err) };
57
- logger?.error?.(`db-guard: TRIPPING — ${failures.length} corrupt-class failures in 60s on ${dbPath}; writing alert and exiting`);
58
- writeAlert(detail);
59
- try {
60
- exitFn(1);
61
- } catch {}
62
- return true;
63
- }
@@ -1,27 +0,0 @@
1
- // db-health.js — synchronous per-file reachability probes.
2
- //
3
- // Used by the boot gate (refuse to finish loading on an unreadable
4
- // primary) and the health endpoint (honest per-file status instead of a
5
- // blanket ok). Synchronous better-sqlite3 on purpose: register() must
6
- // stay synchronous, and a probe is one indexed read, not a workload.
7
- // A missing file reports missing WITHOUT creating it (better-sqlite3
8
- // creates on open — a probe must never materialize databases).
9
- import { existsSync } from "node:fs";
10
- import Database from "better-sqlite3";
11
-
12
- export function checkDb(dbPath) {
13
- const started = Date.now();
14
- if (!existsSync(dbPath)) {
15
- return { ok: false, latencyMs: Date.now() - started, error: `missing: ${dbPath}` };
16
- }
17
- let db = null;
18
- try {
19
- db = new Database(dbPath, { readonly: true });
20
- db.prepare("SELECT 1").get();
21
- return { ok: true, latencyMs: Date.now() - started };
22
- } catch (err) {
23
- return { ok: false, latencyMs: Date.now() - started, error: err?.message ?? String(err), code: err?.code };
24
- } finally {
25
- try { db?.close(); } catch {}
26
- }
27
- }
package/state/db.js DELETED
@@ -1,122 +0,0 @@
1
- import { createClient } from '@libsql/client';
2
- import { drizzle } from 'drizzle-orm/libsql';
3
- import * as schema from './schema.js';
4
- export { resolveStorePaths } from './paths.js';
5
-
6
- // Returns { db, client } where:
7
- // db — Drizzle ORM instance; use for typed queries
8
- // client — raw @libsql/client handle; use for raw SQL the ORM can't express
9
- //
10
- // readonly opens skip WAL/autocheckpoint/busy_timeout — those are writer concerns.
11
- // WAL mode on the write connection already allows concurrent reads without extra config.
12
- //
13
- // `readonly` is a PRAGMA choice, not a read-only open: libsql's local client
14
- // rejects `?mode=ro` (URL_PARAM_NOT_SUPPORTED), so every handle here can write.
15
- // A closed handle therefore checkpoints and unlinks the -wal/-shm, which breaks
16
- // any other connection (in this process or another) still holding them. Use
17
- // getSharedDb() for anything opened while the gateway runs; close() only at
18
- // process shutdown or on a per-test file that nobody else shares.
19
- export async function openDb(dbPath, { readonly = false } = {}) {
20
- const client = createClient({ url: `file:${dbPath}` });
21
- await client.execute("PRAGMA foreign_keys = ON");
22
- if (!readonly) {
23
- await client.execute("PRAGMA busy_timeout = 30000");
24
- await client.execute("PRAGMA journal_mode = WAL");
25
- await client.execute("PRAGMA wal_autocheckpoint = 200");
26
- }
27
- const db = drizzle(client, { schema });
28
- return { db, client };
29
- }
30
-
31
- // Process-wide, path-keyed connection cache: every state/*.js store module
32
- // shares one @libsql/client connection per file rather than each opening its
33
- // own, since separate connections to the same runtime.sqlite all separately
34
- // compete for that one file's write lock (busy_timeout=30000 above) instead
35
- // of sharing a single connection the way SQLite's own single-writer model
36
- // expects. Keyed by `${dbPath}::${readonly}` (not bare dbPath) so a
37
- // readonly-scoped caller and
38
- // a writer-scoped caller against the same file get distinct connections —
39
- // readonly opens skip the writer PRAGMAs above and shouldn't be conflated
40
- // with the writer's.
41
- const _sharedDbCache = new Map(); // key -> Promise<{ db, client }>
42
-
43
- // Self-healing: several stores' close()/test-cleanup paths call client.close()
44
- // directly on what is now a SHARED connection (e.g. run-store.test.js's
45
- // "opening same dbPath twice" idempotency check, session-index.test.js's
46
- // "persists across re-open of same DB file") — a real pattern, not just a
47
- // test artifact: a store can legitimately be closed and later reopened
48
- // against the same path. Wrapping close() to evict the cache entry first
49
- // means the next getSharedDb() call for that key opens a genuinely fresh
50
- // connection instead of handing back a dead one that throws CLIENT_CLOSED
51
- // on first use. The tradeoff: closing a shared connection closes it for
52
- // every other store currently sharing it too — acceptable here because nothing
53
- // in production calls close() on a live store mid-process (it's a boot-time-
54
- // open, run-until-exit lifecycle); close() is exercised by test cleanup and
55
- // isolated per-test dbPaths, which don't share cache keys with each other.
56
- export function getSharedDb(dbPath, { readonly = false } = {}) {
57
- const key = `${dbPath}::${readonly}`;
58
- let entry = _sharedDbCache.get(key);
59
- if (!entry) {
60
- entry = openDb(dbPath, { readonly }).then(({ db, client }) => {
61
- const originalClose = client.close.bind(client);
62
- client.close = (...args) => {
63
- _sharedDbCache.delete(key);
64
- return originalClose(...args);
65
- };
66
- return { db, client };
67
- });
68
- _sharedDbCache.set(key, entry);
69
- }
70
- return entry;
71
- }
72
-
73
- export function isSqliteBusy(err) {
74
- return String(err?.code ?? err?.message ?? "").includes("SQLITE_BUSY");
75
- }
76
-
77
- // Per-dbPath in-process write queue — the same serialize()/withWriteTx()
78
- // idiom admission-queue-store.js uses, generalized here so every store
79
- // sharing a connection via getSharedDb() gets it for free instead of each
80
- // one reimplementing its own copy. (Its own test suite drives 30 genuinely
81
- // concurrent admit() callers through it.)
82
- //
83
- // Keyed by bare dbPath, not the readonly-qualified getSharedDb() cache key
84
- // — SQLITE_BUSY contention is a property of the underlying FILE, not of
85
- // which connection variant issued the call, so a write on the writer
86
- // connection and one on a readonly connection to the same file still need
87
- // to serialize against each other here.
88
- const _writeQueues = new Map(); // dbPath -> Promise (queue tail)
89
-
90
- async function _withRetry(fn, maxAttempts) {
91
- for (let attempt = 0; ; attempt++) {
92
- try {
93
- return await fn();
94
- } catch (err) {
95
- if (attempt < maxAttempts - 1 && isSqliteBusy(err)) {
96
- await new Promise((r) => setTimeout(r, 2 + Math.floor(Math.random() * 8)));
97
- continue;
98
- }
99
- throw err;
100
- }
101
- }
102
- }
103
-
104
- // Runs `fn()` serialized against every other withSharedWrite() call for the
105
- // same dbPath, retrying the whole call on SQLITE_BUSY. Never awaits the
106
- // previous queue occupant's *result* — `.then(attempt, attempt)` runs the
107
- // next attempt whether the prior one resolved or rejected, so one caller's
108
- // failure can never poison the queue for callers after it.
109
- //
110
- // `fn` must be safe to re-run from scratch on a BUSY retry: either a single
111
- // statement (atomic on its own — SQLite's own per-statement guarantee) or a
112
- // caller-owned client.transaction() block (nothing durable commits until
113
- // tx.commit(), so a retry before that point has zero partial effect). Do
114
- // NOT pass multiple independent statements with no enclosing transaction —
115
- // a BUSY on the second would silently re-run the first too.
116
- export function withSharedWrite(dbPath, fn, { maxAttempts = 200 } = {}) {
117
- const prevTail = _writeQueues.get(dbPath) ?? Promise.resolve();
118
- const attempt = () => _withRetry(fn, maxAttempts);
119
- const result = prevTail.then(attempt, attempt);
120
- _writeQueues.set(dbPath, result.then(() => {}, () => {}));
121
- return result;
122
- }
@@ -1,299 +0,0 @@
1
- // discuss-store.js — authoritative server-side state for every discuss
2
- // conversation. Wraps a SQLite db so the discuss view can be rebuilt
3
- // from a single GET after any reconnect, restart, or missed SSE event.
4
- //
5
- // Each (flowId, itemId) pair gets one row that tracks:
6
- // messages_json — full ordered message history (operator + agent)
7
- // turn_status — idle | running | error (drives the "thinking" row)
8
- // active_turn_id — in-flight turnId; lets clients reconcile live events
9
- // pending_json — the proposal waiting on the operator: { text,
10
- // fields, draftId, updatedAt }
11
- // components_json — tool-seeded components keyed by kind; one entry
12
- // per kind, last-write-wins. Cleared on gate resolve.
13
- //
14
- // Session/component events update clients in real-time.
15
- // This store makes correctness independent of event delivery: clients reload
16
- // full state on open/reconnect/foreground.
17
-
18
- import { randomUUID } from "node:crypto";
19
- import { eq, and, inArray, isNotNull } from "drizzle-orm";
20
- import { getSharedDb, withSharedWrite, resolveStorePaths } from "./db.js";
21
- import { discussState } from "./schema.js";
22
- import path from "node:path";
23
- import os from "node:os";
24
- import { mkdirSync } from "node:fs";
25
- import { migrateDatabase, RUNTIME_MIGRATIONS_FOLDER } from "./migrate.js";
26
-
27
- function defaultDbPath() {
28
- return resolveStorePaths().runtimeDb;
29
- }
30
-
31
- function tryParse(s, fallback) {
32
- if (s == null) return fallback;
33
- try { return JSON.parse(s); } catch { return fallback; }
34
- }
35
-
36
- export function createDiscussStore(opts = {}) {
37
- const _p = _createDiscussStoreImpl(opts);
38
- return new Proxy({}, { get(_, k) {
39
- if (k === "then" || typeof k === "symbol") return undefined;
40
- if (k === "ready") return () => _p.then(() => {});
41
- return (...a) => _p.then(s => { if (typeof s[k] !== "function") throw new TypeError(`discuss-store: ${String(k)} is not a function`); return s[k](...a); });
42
- } });
43
- }
44
-
45
- async function _createDiscussStoreImpl({ dbPath = defaultDbPath(), logger } = {}) {
46
- mkdirSync(path.dirname(dbPath), { recursive: true });
47
- await migrateDatabase({ dbPath, migrationsFolder: RUNTIME_MIGRATIONS_FOLDER });
48
- const { db, client } = await getSharedDb(dbPath);
49
-
50
- const _where = (flowId, itemId) => and(eq(discussState.flowId, flowId), eq(discussState.itemId, itemId));
51
-
52
- async function ensureRow(flowId, itemId) {
53
- await withSharedWrite(dbPath, () =>
54
- db.insert(discussState).values({ flowId, itemId, updatedAt: Date.now() }).onConflictDoNothing()
55
- );
56
- }
57
-
58
- async function getRow(flowId, itemId) {
59
- const rows = await db.select().from(discussState).where(_where(flowId, itemId));
60
- return rows[0] ?? null;
61
- }
62
-
63
- // ── Read ────────────────────────────────────────────────────────────────
64
-
65
- // Returns { messages, turnStatus, activeTurnId, pending } or null if
66
- // no row exists yet for this (flowId, itemId).
67
- async function getState(flowId, itemId) {
68
- const row = await getRow(flowId, itemId);
69
- if (!row) return null;
70
- return {
71
- messages: tryParse(row.messagesJson, []),
72
- turnStatus: row.turnStatus ?? "idle",
73
- activeTurnId: row.activeTurnId ?? null,
74
- pending: normalizePending(tryParse(row.pendingJson, null), row.updatedAt),
75
- components: tryParse(row.componentsJson, []),
76
- updatedAt: row.updatedAt,
77
- };
78
- }
79
-
80
- // ── Messages ────────────────────────────────────────────────────────────
81
-
82
- // Append one message to the conversation. `msg` shape:
83
- // { id?, turnId?, role, text?, audioKey?, proposedDraft?,
84
- // proposedMeta?, error?, ts? }
85
- // role must be "operator" or "agent".
86
- async function appendMessage(flowId, itemId, msg) {
87
- await ensureRow(flowId, itemId);
88
- const row = await getRow(flowId, itemId);
89
- const messages = tryParse(row?.messagesJson, []);
90
- messages.push({
91
- id: msg.id ?? randomUUID(),
92
- turnId: msg.turnId ?? null,
93
- role: msg.role,
94
- text: msg.text ?? null,
95
- audioKey: msg.audioKey ?? null,
96
- proposedDraft: msg.proposedDraft ?? null,
97
- proposedMeta: msg.proposedMeta ?? null,
98
- error: msg.error ?? null,
99
- ts: msg.ts ?? Date.now(),
100
- });
101
- await withSharedWrite(dbPath, () =>
102
- db.update(discussState)
103
- .set({ messagesJson: JSON.stringify(messages), updatedAt: Date.now() })
104
- .where(_where(flowId, itemId))
105
- );
106
- }
107
-
108
- // Stamp an error string onto the last operator message for `turnId`
109
- // (or the last operator message if turnId is null). Used when the
110
- // agent turn fails before producing a reply.
111
- async function stampMessageError(flowId, itemId, turnId, errorText) {
112
- await ensureRow(flowId, itemId);
113
- const row = await getRow(flowId, itemId);
114
- const messages = tryParse(row?.messagesJson, []);
115
- let idx = -1;
116
- for (let i = messages.length - 1; i >= 0; i--) {
117
- if (messages[i].role === "operator" && (!turnId || messages[i].turnId === turnId)) {
118
- idx = i; break;
119
- }
120
- }
121
- if (idx !== -1) {
122
- messages[idx] = { ...messages[idx], error: errorText };
123
- await withSharedWrite(dbPath, () =>
124
- db.update(discussState)
125
- .set({ messagesJson: JSON.stringify(messages), updatedAt: Date.now() })
126
- .where(_where(flowId, itemId))
127
- );
128
- }
129
- }
130
-
131
- // ── Turn status ─────────────────────────────────────────────────────────
132
-
133
- // status: "idle" | "running" | "error"
134
- // activeTurnId: string when running, null when idle/error
135
- async function setTurnStatus(flowId, itemId, status, activeTurnId = null) {
136
- await ensureRow(flowId, itemId);
137
- await withSharedWrite(dbPath, () =>
138
- db.update(discussState)
139
- .set({ turnStatus: status, activeTurnId: activeTurnId ?? null, updatedAt: Date.now() })
140
- .where(_where(flowId, itemId))
141
- );
142
- }
143
-
144
- // ── Pending proposal ────────────────────────────────────────────────────
145
- //
146
- // One blob per (flowId, itemId) in pending_json:
147
- // { text?, fields?, draftId?, updatedAt }
148
- //
149
- // updatedAt is the proposal's own timestamp — the row's updated_at also
150
- // moves on every message append, so it cannot stand in for it.
151
-
152
- // Normalizes the stored blob into the shape callers read. Blobs written
153
- // before the blob carried its own timestamp fall back to the row's last
154
- // activity, which is the earliest moment the proposal can have existed.
155
- function normalizePending(blob, rowUpdatedAt) {
156
- if (!blob || typeof blob !== "object" || Array.isArray(blob)) return null;
157
- const out = {
158
- draftId: blob.draftId ?? null,
159
- updatedAt: typeof blob.updatedAt === "number" ? blob.updatedAt : Number(rowUpdatedAt),
160
- };
161
- if (typeof blob.text === "string") out.text = blob.text;
162
- if (blob.fields && typeof blob.fields === "object" && !Array.isArray(blob.fields)) out.fields = blob.fields;
163
- return out;
164
- }
165
-
166
- // The proposal waiting on the operator for this thread, or null.
167
- async function readPending(flowId, itemId) {
168
- if (!flowId || !itemId) return null;
169
- const row = await getRow(flowId, itemId);
170
- if (!row) return null;
171
- return normalizePending(tryParse(row.pendingJson, null), row.updatedAt);
172
- }
173
-
174
- // Write or overwrite the pending draft text. Merges with any existing
175
- // fields so a later text write doesn't wipe earlier field writes, and
176
- // keeps the prior draft slot when the caller names none — the slot picks
177
- // which draft apply-draft commits on multi-draft gates.
178
- async function setPending(flowId, itemId, text, draftId = null) {
179
- await ensureRow(flowId, itemId);
180
- const row = await getRow(flowId, itemId);
181
- const existing = tryParse(row?.pendingJson, {}) ?? {};
182
- const next = {
183
- ...existing,
184
- text,
185
- draftId: draftId ?? existing.draftId ?? null,
186
- updatedAt: Date.now(),
187
- };
188
- await withSharedWrite(dbPath, () =>
189
- db.update(discussState)
190
- .set({ pendingJson: JSON.stringify(next), updatedAt: Date.now() })
191
- .where(_where(flowId, itemId))
192
- );
193
- }
194
-
195
- // Shallow-merge `fields` into the pending proposal's fields object.
196
- // Lets repeated field writes accumulate without resetting earlier ones.
197
- async function mergePendingFields(flowId, itemId, fields) {
198
- await ensureRow(flowId, itemId);
199
- const row = await getRow(flowId, itemId);
200
- const existing = tryParse(row?.pendingJson, {}) ?? {};
201
- const next = {
202
- ...existing,
203
- fields: { ...(existing.fields ?? {}), ...fields },
204
- updatedAt: Date.now(),
205
- };
206
- await withSharedWrite(dbPath, () =>
207
- db.update(discussState)
208
- .set({ pendingJson: JSON.stringify(next), updatedAt: Date.now() })
209
- .where(_where(flowId, itemId))
210
- );
211
- }
212
-
213
- // Clear pending proposal — called after the agent reply lands and
214
- // the proposal has been migrated onto the agent message.
215
- async function clearPending(flowId, itemId) {
216
- await withSharedWrite(dbPath, () =>
217
- db.update(discussState)
218
- .set({ pendingJson: null, updatedAt: Date.now() })
219
- .where(_where(flowId, itemId))
220
- );
221
- }
222
-
223
- // ── Components ──────────────────────────────────────────────────────────
224
-
225
- // Upsert a component by kind. Later calls with the same kind replace
226
- // earlier ones — one component per kind per (flowId, itemId).
227
- async function setComponent(flowId, itemId, component) {
228
- if (!component?.kind) return;
229
- await ensureRow(flowId, itemId);
230
- const row = await getRow(flowId, itemId);
231
- const current = tryParse(row?.componentsJson, []);
232
- const next = [
233
- ...current.filter((c) => c.kind !== component.kind),
234
- { ...component, ts: component.ts ?? Date.now() },
235
- ];
236
- await withSharedWrite(dbPath, () =>
237
- db.update(discussState)
238
- .set({ componentsJson: JSON.stringify(next), updatedAt: Date.now() })
239
- .where(_where(flowId, itemId))
240
- );
241
- }
242
-
243
- // Return all seeded components for this (flowId, itemId).
244
- async function getComponents(flowId, itemId) {
245
- const row = await getRow(flowId, itemId);
246
- return tryParse(row?.componentsJson, []);
247
- }
248
-
249
- // One-shot bulk fetch: for a list of flowIds returns a map of
250
- // flowId → { pendingItemIds: string[] } for any flow that has at
251
- // least one discuss row with a non-null pending_json. The item_id
252
- // values map 1-to-1 with subflow keys so the client can mark
253
- // individual subflow peek rows without a second round-trip.
254
- async function getBulkSummaries(flowIds) {
255
- if (!flowIds || flowIds.length === 0) return {};
256
- const rows = await db.select({ flowId: discussState.flowId, itemId: discussState.itemId })
257
- .from(discussState)
258
- .where(and(inArray(discussState.flowId, flowIds), isNotNull(discussState.pendingJson)));
259
- const result = {};
260
- for (const row of rows) {
261
- if (!result[row.flowId]) result[row.flowId] = { pendingItemIds: [] };
262
- result[row.flowId].pendingItemIds.push(row.itemId);
263
- }
264
- return result;
265
- }
266
-
267
- // Every discuss thread for one flow, oldest activity first.
268
- // Transcript listing builds its discuss entries from these rows so both
269
- // directions of every dialog-door turn come from this store, keyed by
270
- // (flowId, itemId) — never from harness session files.
271
- async function listForFlow(flowId) {
272
- if (!flowId) return [];
273
- const rows = await db.select().from(discussState)
274
- .where(eq(discussState.flowId, flowId));
275
- const entries = rows.map((row) => ({
276
- itemId: row.itemId,
277
- messages: tryParse(row.messagesJson, []),
278
- turnStatus: row.turnStatus ?? "idle",
279
- updatedAt: Number(row.updatedAt),
280
- }));
281
- return entries.sort((a, b) => a.updatedAt - b.updatedAt);
282
- }
283
-
284
- return {
285
- getState,
286
- getBulkSummaries,
287
- listForFlow,
288
- appendMessage,
289
- stampMessageError,
290
- setTurnStatus,
291
- setPending,
292
- readPending,
293
- mergePendingFields,
294
- clearPending,
295
- setComponent,
296
- getComponents,
297
- close() { try { client.close(); } catch { /* ignore */ } },
298
- };
299
- }