@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,697 +0,0 @@
1
- // run-store.js — manifold-owned flow_runs in runtime.sqlite.
2
- //
3
- // This is the sole read/write store for manifold flows. No dual-write;
4
- // registry.sqlite is never touched. All executor transitions go through the
5
- // CAS methods here.
6
- //
7
- // CAS pattern: every active-state write is revision-checked.
8
- // UPDATE ... WHERE flow_id = ? AND revision = ? AND status NOT IN (terminals)
9
- // If rowsAffected === 0 → disambiguate via SELECT revision, status to return the
10
- // right code: not_found | terminal | revision_conflict.
11
-
12
- import path from "node:path";
13
- import os from "node:os";
14
- import { mkdirSync } from "node:fs";
15
- import { eq, and, notInArray, inArray, isNull, isNotNull, ne, desc, sql } from "drizzle-orm";
16
- import { alias } from "drizzle-orm/sqlite-core";
17
- import { getSharedDb, withSharedWrite, resolveStorePaths } from "./db.js";
18
- import { flowRuns, flowNodeInvocations, pipelines } from "./schema.js";
19
- import { migrateDatabase, RUNTIME_MIGRATIONS_FOLDER } from "./migrate.js";
20
- import { enqueue, flush as flushBatchWriter } from "./batch-writer.js";
21
-
22
- function defaultDbPath() {
23
- return resolveStorePaths().runtimeDb;
24
- }
25
-
26
- // Exported so callers deciding "is this flow cancellable / already done"
27
- // (e.g. the CLI's and dashboard's own cancel handlers) check against the
28
- // real terminal set instead of hand-rolling their own allowlist of active
29
- // statuses — a `status !== "running" && status !== "blocked"` check misses
30
- // "queued" (an admission-pool wait, very much non-terminal) and wrongly
31
- // refuses to cancel it.
32
- export const TERMINAL_STATUSES = ["succeeded", "failed", "cancelled", "skipped"];
33
- export const TERMINAL_STATUS_SET = new Set(TERMINAL_STATUSES);
34
- const TERMINAL_SET = TERMINAL_STATUS_SET;
35
-
36
- // ── store factory ────────────────────────────────────────────────────────────
37
-
38
- export function createRunStore(opts = {}) {
39
- const _p = _createRunStoreImpl(opts);
40
- return new Proxy({}, { get(_, k) {
41
- if (k === "then" || typeof k === "symbol") return undefined;
42
- if (k === "ready") return () => _p.then(() => {});
43
- return (...a) => _p.then(s => { if (typeof s[k] !== "function") throw new TypeError(`run-store: ${String(k)} is not a function`); return s[k](...a); });
44
- } });
45
- }
46
-
47
- async function _createRunStoreImpl({ logger, dbPath = defaultDbPath() } = {}) {
48
- mkdirSync(path.dirname(dbPath), { recursive: true });
49
- await migrateDatabase({ dbPath, migrationsFolder: RUNTIME_MIGRATIONS_FOLDER });
50
- const { db, client } = await getSharedDb(dbPath);
51
-
52
- // ── CAS failure classifier ───────────────────────────────────────────────
53
-
54
- async function _casClassify(flowId) {
55
- const rows = await db.select({ revision: flowRuns.revision, status: flowRuns.status })
56
- .from(flowRuns).where(eq(flowRuns.flowId, flowId));
57
- const row = rows[0] ?? null;
58
- if (!row) return { applied: false, code: "not_found" };
59
- if (TERMINAL_SET.has(row.status)) return { applied: false, code: "terminal" };
60
- return { applied: false, code: "revision_conflict" };
61
- }
62
-
63
- // ── public API ───────────────────────────────────────────────────────────
64
-
65
- return {
66
- async insert({ flowId, ownerKey, pipelineId, revision, status,
67
- lastStep, stateJson, createdAt, updatedAt, parentInvocationId }) {
68
- try {
69
- await withSharedWrite(dbPath, async () => {
70
- if (pipelineId != null) {
71
- const now = Date.now();
72
- await db.insert(pipelines).values({
73
- id: pipelineId, name: pipelineId, createdAt: now, updatedAt: now,
74
- }).onConflictDoNothing();
75
- }
76
- await db.insert(flowRuns).values({
77
- flowId,
78
- ownerKey: ownerKey ?? "",
79
- pipelineId: pipelineId ?? null,
80
- revision: revision ?? 0,
81
- status,
82
- lastStep: lastStep ?? null,
83
- stateJson: _str(stateJson) ?? "{}",
84
- createdAt: createdAt ?? Date.now(),
85
- updatedAt: updatedAt ?? Date.now(),
86
- parentInvocationId: parentInvocationId ?? null,
87
- }).onConflictDoNothing();
88
- });
89
- } catch {}
90
- },
91
-
92
- // CAS: advance to next running step (clears wait state).
93
- async casSetRunning(flowId, { expectedRevision, lastStep, stateJson }) {
94
- try {
95
- return await withSharedWrite(dbPath, async () => {
96
- const now = Date.now();
97
- const r = await db.update(flowRuns).set({
98
- status: "running",
99
- revision: sql`${flowRuns.revision} + 1`,
100
- lastStep: lastStep ?? null,
101
- stateJson: _str(stateJson),
102
- waitJson: null,
103
- statusReason: null,
104
- updatedAt: now,
105
- }).where(and(
106
- eq(flowRuns.flowId, flowId),
107
- eq(flowRuns.revision, expectedRevision),
108
- notInArray(flowRuns.status, TERMINAL_STATUSES),
109
- ));
110
- if (r.rowsAffected > 0) return { applied: true };
111
- return await _casClassify(flowId);
112
- });
113
- } catch (err) {
114
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
115
- }
116
- },
117
-
118
- // CAS: block on external signal (human-gate).
119
- async casSetWaiting(flowId, { expectedRevision, lastStep, blockingNodeId, stateJson, waitJson, statusReason }) {
120
- try {
121
- return await withSharedWrite(dbPath, async () => {
122
- const now = Date.now();
123
- const r = await db.update(flowRuns).set({
124
- status: "blocked",
125
- revision: sql`${flowRuns.revision} + 1`,
126
- lastStep: lastStep ?? null,
127
- stateJson: _str(stateJson),
128
- waitJson: _str(waitJson),
129
- statusReason: statusReason ?? null,
130
- updatedAt: now,
131
- }).where(and(
132
- eq(flowRuns.flowId, flowId),
133
- eq(flowRuns.revision, expectedRevision),
134
- notInArray(flowRuns.status, TERMINAL_STATUSES),
135
- ));
136
- if (r.rowsAffected > 0) return { applied: true };
137
- return await _casClassify(flowId);
138
- });
139
- } catch (err) {
140
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
141
- }
142
- },
143
-
144
- // CAS: block on an internal resource (a shared admission-pool slot,
145
- // cross-process lock) — same shape as casSetWaiting, distinct status so
146
- // consumers can tell "waiting on an operator decision" from "waiting
147
- // on a resource that will free itself" without inspecting wait_json.kind.
148
- async casSetQueued(flowId, { expectedRevision, lastStep, blockingNodeId, stateJson, waitJson, statusReason }) {
149
- try {
150
- return await withSharedWrite(dbPath, async () => {
151
- const now = Date.now();
152
- const r = await db.update(flowRuns).set({
153
- status: "queued",
154
- revision: sql`${flowRuns.revision} + 1`,
155
- lastStep: lastStep ?? null,
156
- stateJson: _str(stateJson),
157
- waitJson: _str(waitJson),
158
- statusReason: statusReason ?? null,
159
- updatedAt: now,
160
- }).where(and(
161
- eq(flowRuns.flowId, flowId),
162
- eq(flowRuns.revision, expectedRevision),
163
- notInArray(flowRuns.status, TERMINAL_STATUSES),
164
- ));
165
- if (r.rowsAffected > 0) return { applied: true };
166
- return await _casClassify(flowId);
167
- });
168
- } catch (err) {
169
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
170
- }
171
- },
172
-
173
- // CAS: terminal success.
174
- async casFinish(flowId, { expectedRevision, stateJson }) {
175
- try {
176
- return await withSharedWrite(dbPath, async () => {
177
- const now = Date.now();
178
- const r = await db.update(flowRuns).set({
179
- status: "succeeded",
180
- revision: sql`${flowRuns.revision} + 1`,
181
- stateJson: _str(stateJson),
182
- waitJson: null,
183
- statusReason: null,
184
- updatedAt: now,
185
- endedAt: now,
186
- }).where(and(
187
- eq(flowRuns.flowId, flowId),
188
- eq(flowRuns.revision, expectedRevision),
189
- notInArray(flowRuns.status, TERMINAL_STATUSES),
190
- ));
191
- if (r.rowsAffected > 0) return { applied: true };
192
- return await _casClassify(flowId);
193
- });
194
- } catch (err) {
195
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
196
- }
197
- },
198
-
199
- // CAS: terminal skip (pipeline ran, nothing to output).
200
- async casSkip(flowId, { expectedRevision, stateJson, reason }) {
201
- try {
202
- return await withSharedWrite(dbPath, async () => {
203
- const now = Date.now();
204
- const r = await db.update(flowRuns).set({
205
- status: "skipped",
206
- revision: sql`${flowRuns.revision} + 1`,
207
- stateJson: _str(stateJson),
208
- waitJson: null,
209
- statusReason: reason ?? null,
210
- updatedAt: now,
211
- endedAt: now,
212
- }).where(and(
213
- eq(flowRuns.flowId, flowId),
214
- eq(flowRuns.revision, expectedRevision),
215
- notInArray(flowRuns.status, TERMINAL_STATUSES),
216
- ));
217
- if (r.rowsAffected > 0) return { applied: true };
218
- return await _casClassify(flowId);
219
- });
220
- } catch (err) {
221
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
222
- }
223
- },
224
-
225
- // CAS: terminal failure. current_step stays frozen at the failing node.
226
- async casFail(flowId, { expectedRevision, stateJson, statusReason }) {
227
- try {
228
- return await withSharedWrite(dbPath, async () => {
229
- const now = Date.now();
230
- const r = await db.update(flowRuns).set({
231
- status: "failed",
232
- revision: sql`${flowRuns.revision} + 1`,
233
- stateJson: _str(stateJson),
234
- statusReason: statusReason ?? null,
235
- waitJson: null,
236
- updatedAt: now,
237
- endedAt: now,
238
- }).where(and(
239
- eq(flowRuns.flowId, flowId),
240
- eq(flowRuns.revision, expectedRevision),
241
- notInArray(flowRuns.status, TERMINAL_STATUSES),
242
- ));
243
- if (r.rowsAffected > 0) return { applied: true };
244
- return await _casClassify(flowId);
245
- });
246
- } catch (err) {
247
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
248
- }
249
- },
250
-
251
- // CAS: force a terminal flow back to 'blocked'.
252
- // Returns { applied: false, code: "not_terminal" } when the row exists,
253
- // the revision matches, but its status is not in TERMINAL_STATUSES.
254
- async forceBlock(flowId, { expectedRevision, stateJson, statusReason, ownerKey }) {
255
- try {
256
- return await withSharedWrite(dbPath, async () => {
257
- const now = Date.now();
258
- const r = await db.update(flowRuns).set({
259
- status: "blocked",
260
- revision: sql`${flowRuns.revision} + 1`,
261
- stateJson: _str(stateJson),
262
- waitJson: null,
263
- statusReason: statusReason ?? null,
264
- endedAt: null,
265
- ownerKey: ownerKey ?? "",
266
- updatedAt: now,
267
- }).where(and(
268
- eq(flowRuns.flowId, flowId),
269
- eq(flowRuns.revision, expectedRevision),
270
- inArray(flowRuns.status, TERMINAL_STATUSES),
271
- ));
272
- if (r.rowsAffected > 0) return { applied: true };
273
- const rows = await db.select({ revision: flowRuns.revision, status: flowRuns.status })
274
- .from(flowRuns).where(eq(flowRuns.flowId, flowId));
275
- const row = rows[0] ?? null;
276
- if (!row) return { applied: false, code: "not_found" };
277
- if (Number(row.revision) !== expectedRevision) return { applied: false, code: "revision_conflict" };
278
- return { applied: false, code: "not_terminal" };
279
- });
280
- } catch (err) {
281
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
282
- }
283
- },
284
-
285
- // Non-CAS cancel: terminal guard only. Returns { cancelled: boolean }.
286
- async cancelFlow(flowId, reason = null) {
287
- try {
288
- const now = Date.now();
289
- const r = await withSharedWrite(dbPath, () => db.update(flowRuns).set({
290
- status: "cancelled",
291
- waitJson: null,
292
- statusReason: reason ?? null,
293
- updatedAt: now,
294
- endedAt: now,
295
- }).where(and(
296
- eq(flowRuns.flowId, flowId),
297
- notInArray(flowRuns.status, TERMINAL_STATUSES),
298
- )));
299
- return { cancelled: r.rowsAffected > 0 };
300
- } catch {
301
- return { cancelled: false };
302
- }
303
- },
304
-
305
- // Stamps state_json before the cancel status flip.
306
- async stampStateJson(flowId, stateJson) {
307
- try {
308
- await withSharedWrite(dbPath, () => db.update(flowRuns)
309
- .set({ stateJson: _str(stateJson) })
310
- .where(eq(flowRuns.flowId, flowId)));
311
- } catch {}
312
- },
313
-
314
- // Atomic combination of stampStateJson() + cancelFlow(), for
315
- // state-store.js's cancel(): the state_json stamp and the cancel
316
- // status flip must commit together — as two separate awaited writes a
317
- // crash between them could leave state_json updated but status not
318
- // flipped to 'cancelled', or vice versa. One db.batch() call commits
319
- // both inside a single transaction instead.
320
- // Returns the same { cancelled } shape cancelFlow() alone returns,
321
- // read from that statement's own rowsAffected within the batch —
322
- // db.batch() preserves each query's normal per-statement result shape,
323
- // not just an overall success flag.
324
- async cancelWithStateJson(flowId, stateJson, reason = null) {
325
- try {
326
- const results = await withSharedWrite(dbPath, () => {
327
- const now = Date.now();
328
- const queries = [];
329
- if (stateJson != null) {
330
- queries.push(db.update(flowRuns)
331
- .set({ stateJson: _str(stateJson) })
332
- .where(eq(flowRuns.flowId, flowId)));
333
- }
334
- queries.push(db.update(flowRuns).set({
335
- status: "cancelled",
336
- waitJson: null,
337
- statusReason: reason ?? null,
338
- updatedAt: now,
339
- endedAt: now,
340
- }).where(and(
341
- eq(flowRuns.flowId, flowId),
342
- notInArray(flowRuns.status, TERMINAL_STATUSES),
343
- )));
344
- return db.batch(queries);
345
- });
346
- const cancelResult = results[results.length - 1];
347
- return { cancelled: cancelResult.rowsAffected > 0 };
348
- } catch {
349
- return { cancelled: false };
350
- }
351
- },
352
-
353
- // Stamps cancel_requested_at flag (no revision check — just a signal).
354
- // Deferred via batch-writer: requestCancel() (state-store.js) awaits
355
- // this purely to sequence before returning { applied: true } — it
356
- // never inspects a result, so there's no read-after-write to preserve.
357
- // NOT the same as cancelFlow()/stampStateJson() below — those feed a
358
- // caller-visible { cancelled } result that gates real side effects
359
- // (event emission, resume triggers), so they stay immediate. The
360
- // deferred batch itself is protected at flush time — see batch-writer.js.
361
- stampCancelRequested(flowId) {
362
- const now = Date.now();
363
- enqueue(dbPath, db.update(flowRuns)
364
- .set({ cancelRequestedAt: now, updatedAt: now })
365
- .where(eq(flowRuns.flowId, flowId)));
366
- },
367
-
368
- // Revision-checked state_json update for flows that are in the DB but not
369
- // in the executor's in-process Map (post-restart blocked flows).
370
- async updateStateJsonCAS(flowId, expectedRevision, stateJson) {
371
- try {
372
- const r = await withSharedWrite(dbPath, () => db.update(flowRuns).set({
373
- stateJson: _str(stateJson),
374
- revision: sql`${flowRuns.revision} + 1`,
375
- updatedAt: Date.now(),
376
- }).where(and(
377
- eq(flowRuns.flowId, flowId),
378
- eq(flowRuns.revision, expectedRevision),
379
- )));
380
- if (r.rowsAffected === 0) return { applied: false, code: "revision_conflict" };
381
- return { applied: true, via: "run-store-cas" };
382
- } catch (err) {
383
- return { applied: false, code: "run_store_failed", error: err?.message ?? String(err) };
384
- }
385
- },
386
-
387
- async get(flowId) {
388
- try {
389
- const rows = await db.select().from(flowRuns).where(eq(flowRuns.flowId, flowId));
390
- return mapRow(rows[0] ?? null);
391
- } catch { return null; }
392
- },
393
-
394
- async list() {
395
- try {
396
- const rows = await db.select().from(flowRuns)
397
- .where(isNull(flowRuns.parentInvocationId))
398
- .orderBy(desc(flowRuns.updatedAt));
399
- return rows.map(mapRow);
400
- } catch { return []; }
401
- },
402
-
403
- async listFor(pipelineId) {
404
- try {
405
- const rows = await db.select().from(flowRuns)
406
- .where(eq(flowRuns.pipelineId, pipelineId))
407
- .orderBy(desc(flowRuns.updatedAt));
408
- return rows.map(mapRow);
409
- } catch { return []; }
410
- },
411
-
412
- // The child spawned for one subflow-item invocation, if it exists.
413
- async getByParentInvocationId(invocationId) {
414
- try {
415
- const rows = await db.select().from(flowRuns)
416
- .where(eq(flowRuns.parentInvocationId, invocationId));
417
- return mapRow(rows[0] ?? null);
418
- } catch { return null; }
419
- },
420
-
421
- // Every child spawned for a set of sibling invocations.
422
- async listForParentInvocations(invocationIds) {
423
- if (!Array.isArray(invocationIds) || invocationIds.length === 0) return [];
424
- try {
425
- const rows = await db.select().from(flowRuns)
426
- .where(inArray(flowRuns.parentInvocationId, invocationIds));
427
- return rows.map(mapRow);
428
- } catch { return []; }
429
- },
430
-
431
- // ── Read-only dashboard/route queries ────────────────────────
432
-
433
- // list() with optional includeChildren flag (default: top-level only).
434
- // The no-arg form is identical to the original list() above.
435
- async list({ includeChildren = false } = {}) {
436
- const q = includeChildren
437
- ? db.select().from(flowRuns).orderBy(desc(flowRuns.updatedAt))
438
- : db.select().from(flowRuns)
439
- .where(isNull(flowRuns.parentInvocationId))
440
- .orderBy(desc(flowRuns.updatedAt));
441
- return (await q).map(mapRow);
442
- },
443
-
444
- // Returns all non-terminal flows (ended_at IS NULL) across all pipelines,
445
- // including children. Used by live-bridge's one-shot startup seed —
446
- // only active flows need tracking set entries, so O(active) not O(all).
447
- async listActive() {
448
- return (await db.select().from(flowRuns)
449
- .where(isNull(flowRuns.endedAt))
450
- .orderBy(desc(flowRuns.updatedAt))).map(mapRow);
451
- },
452
-
453
- // { [status]: count } for top-level runs, optionally filtered by pipeline.
454
- // Additive dashboard data: a failed count must degrade to no counts, the
455
- // same contract as get/list — GET /manifold/runs renders the runs list
456
- // whatever this returns, and letting it throw turns an additive card
457
- // into a 500 for the whole response.
458
- async listStatusCounts({ pipelineId } = {}) {
459
- try {
460
- const conditions = [isNull(flowRuns.parentInvocationId)];
461
- if (pipelineId) conditions.push(eq(flowRuns.pipelineId, pipelineId));
462
- const rows = await db.select({
463
- status: flowRuns.status,
464
- n: sql`COUNT(*)`.mapWith(Number),
465
- }).from(flowRuns)
466
- .where(and(...conditions))
467
- .groupBy(flowRuns.status);
468
- const out = {};
469
- for (const { status, n } of rows) out[status ?? "unknown"] = n;
470
- return out;
471
- } catch {
472
- return {};
473
- }
474
- },
475
-
476
- // Aggregate stats for a single pipeline in two queries.
477
- async statsFor(pipelineId, { todayStartMs = 0, sevenDaysMs = 0 } = {}) {
478
- const where = and(eq(flowRuns.pipelineId, pipelineId), isNull(flowRuns.parentInvocationId));
479
- const [row] = await db.select({
480
- totalRuns: sql`COUNT(*)`.mapWith(Number),
481
- lastRunAtMs: sql`MAX(${flowRuns.updatedAt})`.mapWith(Number),
482
- todayTotal: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} THEN 1 ELSE 0 END)`.mapWith(Number),
483
- todaySucceeded: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} = 'succeeded' THEN 1 ELSE 0 END)`.mapWith(Number),
484
- todayFailed: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} = 'failed' THEN 1 ELSE 0 END)`.mapWith(Number),
485
- todayActive: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} NOT IN ('succeeded','failed','skipped','cancelled') THEN 1 ELSE 0 END)`.mapWith(Number),
486
- last7dTotal: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} THEN 1 ELSE 0 END)`.mapWith(Number),
487
- last7dSucceeded: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} AND ${flowRuns.status} = 'succeeded' THEN 1 ELSE 0 END)`.mapWith(Number),
488
- last7dFailed: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} AND ${flowRuns.status} = 'failed' THEN 1 ELSE 0 END)`.mapWith(Number),
489
- }).from(flowRuns).where(where);
490
- const [lastRow] = await db.select({ status: flowRuns.status })
491
- .from(flowRuns).where(where)
492
- .orderBy(desc(flowRuns.updatedAt)).limit(1);
493
- return {
494
- totalRuns: row?.totalRuns ?? 0,
495
- lastRunAtMs: row?.lastRunAtMs ?? null,
496
- lastRunStatus: lastRow?.status ?? null,
497
- todayTotal: row?.todayTotal ?? 0,
498
- todaySucceeded: row?.todaySucceeded ?? 0,
499
- todayFailed: row?.todayFailed ?? 0,
500
- todayActive: row?.todayActive ?? 0,
501
- last7dTotal: row?.last7dTotal ?? 0,
502
- last7dSucceeded: row?.last7dSucceeded ?? 0,
503
- last7dFailed: row?.last7dFailed ?? 0,
504
- };
505
- },
506
-
507
- // Narrow 5-column rows for succeeded peers of a pipeline.
508
- async listPeers({ pipelineId, excludeFlowId } = {}) {
509
- const where = excludeFlowId
510
- ? and(eq(flowRuns.pipelineId, pipelineId), eq(flowRuns.status, "succeeded"), isNull(flowRuns.parentInvocationId), ne(flowRuns.flowId, excludeFlowId))
511
- : and(eq(flowRuns.pipelineId, pipelineId), eq(flowRuns.status, "succeeded"), isNull(flowRuns.parentInvocationId));
512
- return db.select({
513
- flowId: flowRuns.flowId,
514
- createdAt: flowRuns.createdAt,
515
- updatedAt: flowRuns.updatedAt,
516
- endedAt: flowRuns.endedAt,
517
- revision: flowRuns.revision,
518
- }).from(flowRuns).where(where).orderBy(desc(flowRuns.endedAt));
519
- },
520
-
521
- // Queued flows with parent context via LEFT JOIN. Raw client — Drizzle
522
- // JOIN merges both tables' columns and the alias approach is verbose enough
523
- // that the raw SQL is clearer here.
524
- async listQueued() {
525
- try {
526
- const { rows } = await client.execute(`
527
- SELECT
528
- fr.flow_id AS flowId,
529
- fr.last_step AS lastStep, fr.status_reason AS statusReason,
530
- fr.wait_json AS waitJson, fr.created_at AS createdAt,
531
- fr.updated_at AS updatedAt,
532
- COALESCE(pr.pipeline_id, fr.pipeline_id) AS pipelineId,
533
- inv.flow_id AS parentFlowId, inv.node_id AS nodeId,
534
- inv.subflow_key AS subflowKey
535
- FROM flow_runs fr
536
- LEFT JOIN flow_node_invocations inv ON inv.id = fr.parent_invocation_id
537
- LEFT JOIN flow_runs pr ON pr.flow_id = inv.flow_id
538
- WHERE fr.status = 'queued'
539
- ORDER BY fr.created_at
540
- `);
541
- return rows.map(row => {
542
- let waitJson = null;
543
- try { if (row.waitJson) waitJson = JSON.parse(row.waitJson); } catch {}
544
- return {
545
- flowId: row.flowId,
546
- pipelineId: row.pipelineId ?? null,
547
- parentFlowId: row.parentFlowId ?? null,
548
- nodeId: row.nodeId ?? null,
549
- subflowKey: row.subflowKey ?? null,
550
- lastStep: row.lastStep ?? null,
551
- statusReason: row.statusReason,
552
- waitJson,
553
- createdAt: row.createdAt,
554
- updatedAt: row.updatedAt,
555
- };
556
- });
557
- } catch { return []; }
558
- },
559
-
560
- // Cursor-paginated run list for companion views — includes JSON_EXTRACT display fields.
561
- async listRunsPage({ pipelineId, status, limit = 50, beforeMs, beforeId } = {}) {
562
- const conditions = [isNull(flowRuns.parentInvocationId)];
563
- if (pipelineId) conditions.push(eq(flowRuns.pipelineId, pipelineId));
564
- if (status) conditions.push(eq(flowRuns.status, status));
565
- if (beforeMs != null && beforeId != null) {
566
- conditions.push(sql`(${flowRuns.createdAt} < ${beforeMs} OR (${flowRuns.createdAt} = ${beforeMs} AND ${flowRuns.flowId} > ${beforeId}))`);
567
- }
568
- const rows = await db.select({
569
- flowId: flowRuns.flowId,
570
- pipelineId: flowRuns.pipelineId,
571
- status: flowRuns.status,
572
- lastStep: flowRuns.lastStep,
573
- statusReason: flowRuns.statusReason,
574
- createdAtMs: flowRuns.createdAt,
575
- updatedAtMs: flowRuns.updatedAt,
576
- endedAtMs: flowRuns.endedAt,
577
- displayTitle: sql`JSON_EXTRACT(${flowRuns.stateJson}, '$._display.title')`,
578
- displayMetrics: sql`JSON_EXTRACT(${flowRuns.stateJson}, '$._display.metrics')`,
579
- }).from(flowRuns)
580
- .where(and(...conditions))
581
- .orderBy(desc(flowRuns.createdAt), flowRuns.flowId)
582
- .limit(limit);
583
- return rows.map(row => {
584
- let metrics = null;
585
- if (row.displayMetrics != null) {
586
- try { metrics = JSON.parse(row.displayMetrics); } catch {}
587
- }
588
- return { ...row, displayMetrics: metrics };
589
- });
590
- },
591
-
592
- // Map<pipelineId, stats> for ALL pipelines — one GROUP BY aggregate + one
593
- // window-function query for last-run status (raw client: Drizzle has no
594
- // window function support).
595
- async listAllPipelineStats({ todayStartMs = 0, sevenDaysMs = 0 } = {}) {
596
- const agg = await db.select({
597
- pipelineId: flowRuns.pipelineId,
598
- totalRuns: sql`COUNT(*)`.mapWith(Number),
599
- lastRunAtMs: sql`MAX(${flowRuns.updatedAt})`.mapWith(Number),
600
- todayTotal: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} THEN 1 ELSE 0 END)`.mapWith(Number),
601
- todaySucceeded: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} = 'succeeded' THEN 1 ELSE 0 END)`.mapWith(Number),
602
- todayFailed: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} = 'failed' THEN 1 ELSE 0 END)`.mapWith(Number),
603
- todaySkipped: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} IN ('skipped','cancelled') THEN 1 ELSE 0 END)`.mapWith(Number),
604
- todayActive: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${todayStartMs} AND ${flowRuns.status} IN ('running','blocked') THEN 1 ELSE 0 END)`.mapWith(Number),
605
- last7dTotal: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} THEN 1 ELSE 0 END)`.mapWith(Number),
606
- last7dSucceeded: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} AND ${flowRuns.status} = 'succeeded' THEN 1 ELSE 0 END)`.mapWith(Number),
607
- last7dFailed: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} AND ${flowRuns.status} = 'failed' THEN 1 ELSE 0 END)`.mapWith(Number),
608
- last7dSkipped: sql`SUM(CASE WHEN ${flowRuns.createdAt} >= ${sevenDaysMs} AND ${flowRuns.status} IN ('skipped','cancelled') THEN 1 ELSE 0 END)`.mapWith(Number),
609
- }).from(flowRuns)
610
- .where(and(isNull(flowRuns.parentInvocationId), isNotNull(flowRuns.pipelineId)))
611
- .groupBy(flowRuns.pipelineId);
612
-
613
- const { rows: lastStatusRows } = await client.execute(`
614
- SELECT pipeline_id, status FROM (
615
- SELECT pipeline_id, status,
616
- ROW_NUMBER() OVER (PARTITION BY pipeline_id ORDER BY updated_at DESC) AS rn
617
- FROM flow_runs
618
- WHERE parent_invocation_id IS NULL AND pipeline_id IS NOT NULL
619
- ) WHERE rn = 1
620
- `);
621
- const lastStatusMap = new Map(lastStatusRows.map(r => [r.pipeline_id, r.status]));
622
-
623
- const statsMap = new Map();
624
- for (const row of agg) {
625
- if (!row.pipelineId) continue;
626
- statsMap.set(row.pipelineId, {
627
- totalRuns: row.totalRuns,
628
- lastRunAtMs: row.lastRunAtMs || null,
629
- lastRunStatus: lastStatusMap.get(row.pipelineId) ?? null,
630
- todayTotal: row.todayTotal,
631
- todaySucceeded: row.todaySucceeded,
632
- todayFailed: row.todayFailed,
633
- todaySkipped: row.todaySkipped,
634
- todayActive: row.todayActive,
635
- last7dTotal: row.last7dTotal,
636
- last7dSucceeded: row.last7dSucceeded,
637
- last7dFailed: row.last7dFailed,
638
- last7dSkipped: row.last7dSkipped,
639
- });
640
- }
641
- return statsMap;
642
- },
643
-
644
- // Narrow 5-column snapshot for cost analysis.
645
- async listCostSnapshot() {
646
- return db.select({
647
- flowId: flowRuns.flowId,
648
- pipelineId: flowRuns.pipelineId,
649
- status: flowRuns.status,
650
- createdAt: flowRuns.createdAt,
651
- updatedAt: flowRuns.updatedAt,
652
- }).from(flowRuns).where(isNull(flowRuns.parentInvocationId));
653
- },
654
-
655
- // stampCancelRequested() defers through batch-writer — for tests that
656
- // assert read-after-write. Not meant for production use.
657
- async flush() {
658
- await flushBatchWriter(dbPath);
659
- },
660
-
661
- close() { try { client.close(); } catch {} },
662
- _db: client,
663
- };
664
- }
665
-
666
- // ── helpers ───────────────────────────────────────────────────────────────────
667
-
668
- function _str(v) {
669
- if (v == null) return null;
670
- if (typeof v === "string") return v;
671
- return JSON.stringify(v);
672
- }
673
-
674
- // Drizzle returns camelCase keys matching the schema field names.
675
- // JSON columns (stateJson, waitJson) are still stored as text — parse them here.
676
- function mapRow(row) {
677
- if (!row) return null;
678
- let stateJson = null, waitJson = null;
679
- try { if (row.stateJson) stateJson = JSON.parse(row.stateJson); } catch {}
680
- try { if (row.waitJson) waitJson = JSON.parse(row.waitJson); } catch {}
681
- return {
682
- flowId: row.flowId,
683
- ownerKey: row.ownerKey,
684
- pipelineId: row.pipelineId,
685
- revision: Number(row.revision),
686
- status: row.status,
687
- lastStep: row.lastStep ?? null,
688
- statusReason: row.statusReason ?? null,
689
- stateJson,
690
- waitJson,
691
- cancelRequestedAt: row.cancelRequestedAt ?? null,
692
- createdAt: Number(row.createdAt),
693
- updatedAt: Number(row.updatedAt),
694
- endedAt: row.endedAt ?? null,
695
- parentInvocationId: row.parentInvocationId ?? null,
696
- };
697
- }