@igorjpimenta/manifold 0.0.0-stage → 2026.11.22-beta.3
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.
- package/.migrations/ops/0000_futuristic_karma.sql +111 -0
- package/.migrations/ops/0001_low_maximus.sql +7 -0
- package/.migrations/ops/meta/0000_snapshot.json +763 -0
- package/.migrations/ops/meta/0001_snapshot.json +173 -0
- package/.migrations/ops/meta/_journal.json +20 -0
- package/.migrations/runtime/0000_real_boomer.sql +180 -0
- package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
- package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
- package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
- package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
- package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
- package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
- package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
- package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
- package/.migrations/runtime/0009_lying_shocker.sql +96 -0
- package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
- package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
- package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
- package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
- package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
- package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
- package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
- package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
- package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
- package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
- package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
- package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
- package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
- package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
- package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
- package/.migrations/runtime/meta/_journal.json +97 -0
- package/README.md +1583 -2
- package/RELEASING.md +89 -0
- package/agent/fleet.js +100 -0
- package/bin/cli.js +854 -0
- package/bin/manifold.js +4 -0
- package/boot/flow-revival.js +536 -0
- package/budget/index.js +495 -0
- package/budget/lib.js +59 -0
- package/dashboard/cost.js +528 -0
- package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
- package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
- package/dashboard/dist/index.html +13 -0
- package/dashboard/event-log.js +267 -0
- package/dashboard/index.html +12 -0
- package/dashboard/package-lock.json +2877 -0
- package/dashboard/package.json +37 -0
- package/dashboard/pipeline-graph.js +131 -0
- package/dashboard/postcss.config.js +6 -0
- package/dashboard/search.js +183 -0
- package/dashboard/server.js +1015 -0
- package/dashboard/src/App.tsx +42 -0
- package/dashboard/src/api/client.ts +118 -0
- package/dashboard/src/api/types.ts +510 -0
- package/dashboard/src/api/ws.tsx +112 -0
- package/dashboard/src/components/CancelButton.tsx +63 -0
- package/dashboard/src/components/ClickToCopy.tsx +68 -0
- package/dashboard/src/components/CopyButton.tsx +61 -0
- package/dashboard/src/components/DailyCostTrend.tsx +85 -0
- package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
- package/dashboard/src/components/Funnel.tsx +149 -0
- package/dashboard/src/components/GateForm.tsx +158 -0
- package/dashboard/src/components/GraphView.tsx +302 -0
- package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
- package/dashboard/src/components/Histogram.tsx +292 -0
- package/dashboard/src/components/Layout.tsx +28 -0
- package/dashboard/src/components/NeedsAttention.tsx +98 -0
- package/dashboard/src/components/NodeContextCard.tsx +132 -0
- package/dashboard/src/components/PathTrail.tsx +35 -0
- package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
- package/dashboard/src/components/RerunButton.tsx +46 -0
- package/dashboard/src/components/RunComparison.tsx +53 -0
- package/dashboard/src/components/Sparkline.tsx +94 -0
- package/dashboard/src/components/StatStrip.tsx +147 -0
- package/dashboard/src/components/StateTree.tsx +84 -0
- package/dashboard/src/components/StatusBadge.tsx +18 -0
- package/dashboard/src/components/SubflowPathPills.tsx +38 -0
- package/dashboard/src/components/SubflowSummary.tsx +24 -0
- package/dashboard/src/components/SubflowsCard.tsx +327 -0
- package/dashboard/src/components/TodayStrip.tsx +86 -0
- package/dashboard/src/components/TranscriptPane.tsx +99 -0
- package/dashboard/src/components/TranscriptRow.tsx +69 -0
- package/dashboard/src/components/ui/badge.tsx +38 -0
- package/dashboard/src/components/ui/button.tsx +28 -0
- package/dashboard/src/components/ui/card.tsx +42 -0
- package/dashboard/src/components/ui/skeleton.tsx +9 -0
- package/dashboard/src/components/ui/table.tsx +62 -0
- package/dashboard/src/index.css +26 -0
- package/dashboard/src/lib/anomaly.ts +42 -0
- package/dashboard/src/lib/format.ts +30 -0
- package/dashboard/src/lib/step-history.ts +13 -0
- package/dashboard/src/lib/subflow.ts +154 -0
- package/dashboard/src/lib/use-click-outside.ts +28 -0
- package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
- package/dashboard/src/lib/utils.ts +6 -0
- package/dashboard/src/main.tsx +10 -0
- package/dashboard/src/types/shims.d.ts +4 -0
- package/dashboard/src/views/Home.tsx +478 -0
- package/dashboard/src/views/PipelineDetail.tsx +1185 -0
- package/dashboard/src/views/PipelinesList.tsx +111 -0
- package/dashboard/src/views/RunDetail.tsx +248 -0
- package/dashboard/src/views/Search.tsx +196 -0
- package/dashboard/stats.js +668 -0
- package/dashboard/tailwind.config.ts +43 -0
- package/dashboard/tree-stats.js +12 -0
- package/dashboard/tsconfig.json +25 -0
- package/dashboard/views.js +231 -0
- package/dashboard/vite.config.ts +34 -0
- package/drizzle.config.js +12 -0
- package/drizzle.config.ops.js +12 -0
- package/elevation.js +116 -0
- package/engine/admission-promotion-resume.js +127 -0
- package/engine/agent-dispatch.js +64 -0
- package/engine/child-resume-trigger.js +315 -0
- package/engine/executor-budget.js +87 -0
- package/engine/executor-cancel.js +83 -0
- package/engine/executor-errors.js +122 -0
- package/engine/executor-helpers.js +254 -0
- package/engine/executor-nodes.js +292 -0
- package/engine/executor-persist.js +212 -0
- package/engine/executor-subflow-children.js +275 -0
- package/engine/executor.js +1013 -0
- package/engine/global-concurrency.js +33 -0
- package/engine/loader.js +528 -0
- package/engine/needs-resolver.js +229 -0
- package/engine/pipelines-watcher.js +133 -0
- package/engine/result-replay.js +34 -0
- package/engine/revival-messages.js +11 -0
- package/engine/spawn-child.js +55 -0
- package/engine/wait-for-flow-settlement.js +109 -0
- package/events/catalog.js +26 -0
- package/events/emit.js +44 -0
- package/events/kinds.js +118 -0
- package/events/serve-ws.js +74 -0
- package/events/stream.js +35 -0
- package/events/ws-dialog.js +257 -0
- package/events/ws-frame.js +194 -0
- package/events/ws-run-events.js +70 -0
- package/events.js +32 -0
- package/gates/gate-resolver.js +576 -0
- package/gates/paused-flows.js +55 -0
- package/graphql/columns.js +67 -0
- package/graphql/schema.js +333 -0
- package/index.js +49 -0
- package/mcp/http.js +159 -0
- package/mcp/identity.js +94 -0
- package/mcp/stdio.js +80 -0
- package/mcp/tools.js +245 -0
- package/mcp/wire-ops.js +178 -0
- package/package.json +49 -4
- package/routes/bearer-auth.js +57 -0
- package/routes/discuss-audio.js +87 -0
- package/routes/discuss-component.js +39 -0
- package/routes/discuss-draft.js +159 -0
- package/routes/discuss-helpers.js +20 -0
- package/routes/discuss.js +848 -0
- package/routes/failures.js +104 -0
- package/routes/graphql.js +61 -0
- package/routes/health.js +54 -0
- package/routes/operator-discuss.js +193 -0
- package/routes/operator-resume.js +32 -0
- package/routes/operator-retry.js +301 -0
- package/routes/operator.js +539 -0
- package/routes/pipelines.js +118 -0
- package/routes/runs.js +1129 -0
- package/scripts/ci/import-check.mjs +50 -0
- package/scripts/link-core-self.mjs +31 -0
- package/scripts/with-guard.sh +24 -0
- package/server/app.js +503 -0
- package/server/registry.js +53 -0
- package/server/serve.js +163 -0
- package/state/admission-queue-store.js +325 -0
- package/state/attribution-registries.js +102 -0
- package/state/batch-writer.js +162 -0
- package/state/db-guard.js +63 -0
- package/state/db-health.js +27 -0
- package/state/db.js +122 -0
- package/state/discuss-store.js +299 -0
- package/state/failure-store.js +427 -0
- package/state/migrate.js +104 -0
- package/state/node-cost.js +67 -0
- package/state/node-executions.js +775 -0
- package/state/node-health-store.js +124 -0
- package/state/ops-merge.js +93 -0
- package/state/paths.js +146 -0
- package/state/pipeline-dirs.js +99 -0
- package/state/pipeline-registry.js +449 -0
- package/state/run-store.js +697 -0
- package/state/schema.js +438 -0
- package/state/schema.ops.js +44 -0
- package/state/session-index.js +177 -0
- package/state/session-keys.js +39 -0
- package/state/state-store.js +311 -0
- package/state/subflow-state.js +126 -0
- package/state/tool-handoff.js +169 -0
- package/telemetry/budget-alert.js +353 -0
- package/telemetry/cli.js +331 -0
- package/telemetry/grammar.js +119 -0
- package/telemetry/http-budget.js +132 -0
- package/telemetry/http-cost.js +670 -0
- package/telemetry/http-helpers.js +134 -0
- package/telemetry/http-tree.js +349 -0
- package/telemetry/http.js +44 -0
- package/telemetry/index.js +307 -0
- package/telemetry/ingest.js +134 -0
- package/telemetry/labels.js +115 -0
- package/telemetry/pricer.js +77 -0
- package/telemetry/resolver.js +314 -0
- package/telemetry/store.js +147 -0
- package/utils/run-patch.js +43 -0
|
@@ -0,0 +1,775 @@
|
|
|
1
|
+
// node-executions.js — per-node invocation + execution-attempt records
|
|
2
|
+
// (flow_node_invocations, flow_node_executions).
|
|
3
|
+
//
|
|
4
|
+
// Lazy singleton: first caller opens the DB and creates the schema; subsequent
|
|
5
|
+
// calls return the cached store. Writes are fire-and-forget (errors swallowed)
|
|
6
|
+
// so a timing/error-recording failure never disrupts flow execution.
|
|
7
|
+
//
|
|
8
|
+
// Two tables, two granularities:
|
|
9
|
+
// flow_node_invocations — one row per LOGICAL node dispatch (root-level
|
|
10
|
+
// node-per-flow-visit, or subflow-item), created exactly once and never
|
|
11
|
+
// duplicated by retries. flow_runs.parent_invocation_id points here.
|
|
12
|
+
// flow_node_executions — one row per EXECUTION ATTEMPT of an invocation
|
|
13
|
+
// (one per retry), referencing its invocation via invocation_id.
|
|
14
|
+
// The split matters because retries legitimately write multiple execution
|
|
15
|
+
// rows sharing the same (flow_id, node_id, group_id, branch_key) tuple, so
|
|
16
|
+
// that tuple could never safely be used as a stable identity/anchor on its
|
|
17
|
+
// own.
|
|
18
|
+
|
|
19
|
+
import { randomUUID } from "node:crypto";
|
|
20
|
+
import { mkdirSync } from "node:fs";
|
|
21
|
+
import path from "node:path";
|
|
22
|
+
import os from "node:os";
|
|
23
|
+
import { eq, and, inArray, isNull, isNotNull, desc, asc } from "drizzle-orm";
|
|
24
|
+
import { getSharedDb, withSharedWrite, resolveStorePaths } from "./db.js";
|
|
25
|
+
import { flowNodeInvocations, flowNodeExecutions } from "./schema.js";
|
|
26
|
+
import { migrateDatabase, RUNTIME_MIGRATIONS_FOLDER } from "./migrate.js";
|
|
27
|
+
import { enqueue, flush as flushBatchWriter } from "./batch-writer.js";
|
|
28
|
+
|
|
29
|
+
function defaultDbPath() {
|
|
30
|
+
return resolveStorePaths().runtimeDb;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// No size cap on resultJson: no cap width is both safe for real payloads
|
|
34
|
+
// and small enough to matter, so writeResult() always stores the full
|
|
35
|
+
// result. resultTooLargeBytes and the read-only handling in
|
|
36
|
+
// result-replay.js/needs-resolver.js stay in place for rows whose capped
|
|
37
|
+
// result cannot be recovered.
|
|
38
|
+
|
|
39
|
+
const VALID_EXECUTION_KINDS = new Set(["fresh", "retry", "revival", "recheck"]);
|
|
40
|
+
|
|
41
|
+
// Normalizes flow_branches.errors' mixed shape (bare strings vs
|
|
42
|
+
// {node, itemId, message, at} objects) — only the last entry is taken.
|
|
43
|
+
// Belongs to the flow_branches error backfill, not to the runtime write
|
|
44
|
+
// path.
|
|
45
|
+
export function lastErrorMessage(errorsJson) {
|
|
46
|
+
let arr;
|
|
47
|
+
try { arr = JSON.parse(errorsJson); } catch { return null; }
|
|
48
|
+
if (!Array.isArray(arr) || arr.length === 0) return null;
|
|
49
|
+
const last = arr[arr.length - 1];
|
|
50
|
+
if (typeof last === "string") return last;
|
|
51
|
+
if (last && typeof last === "object") return last.message ?? JSON.stringify(last);
|
|
52
|
+
return String(last);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let _singleton = null;
|
|
56
|
+
|
|
57
|
+
function _makeStoreProxy(_p) {
|
|
58
|
+
return new Proxy({}, { get(_, k) {
|
|
59
|
+
if (k === "then" || typeof k === "symbol") return undefined;
|
|
60
|
+
if (k === "ready") return () => _p.then(() => {});
|
|
61
|
+
if (k === "_db") return new Proxy({}, { get(__, method) {
|
|
62
|
+
if (method === "then" || typeof method === "symbol") return undefined;
|
|
63
|
+
return (...a) => _p.then(s => s._db[method](...a));
|
|
64
|
+
} });
|
|
65
|
+
return (...a) => _p.then(s => { if (typeof s[k] !== "function") throw new TypeError(`node-executions: ${String(k)} is not a function`); return s[k](...a); });
|
|
66
|
+
} });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function getNodeExecutionsStore(dbPath = defaultDbPath()) {
|
|
70
|
+
if (!_singleton) {
|
|
71
|
+
_singleton = _makeStoreProxy(_createStore(dbPath));
|
|
72
|
+
}
|
|
73
|
+
return _singleton;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Exported for tests: creates an independent store (bypasses the singleton).
|
|
77
|
+
export function createNodeExecutionsStore(dbPath) {
|
|
78
|
+
return _makeStoreProxy(_createStore(dbPath));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function _createStore(dbPath) {
|
|
82
|
+
try {
|
|
83
|
+
mkdirSync(path.dirname(dbPath), { recursive: true });
|
|
84
|
+
await migrateDatabase({ dbPath, migrationsFolder: RUNTIME_MIGRATIONS_FOLDER });
|
|
85
|
+
const { db, client } = await getSharedDb(dbPath);
|
|
86
|
+
|
|
87
|
+
// ── helpers ────────────────────────────────────────────────────────────
|
|
88
|
+
|
|
89
|
+
async function _findLatestInvocation(flowId, nodeId, subflowKey) {
|
|
90
|
+
const nullGuard = subflowKey == null
|
|
91
|
+
? isNull(flowNodeInvocations.subflowKey)
|
|
92
|
+
: eq(flowNodeInvocations.subflowKey, subflowKey);
|
|
93
|
+
const rows = await db.select({
|
|
94
|
+
id: flowNodeInvocations.id,
|
|
95
|
+
visitSeq: flowNodeInvocations.visitSeq,
|
|
96
|
+
invokedAt: flowNodeInvocations.invokedAt,
|
|
97
|
+
})
|
|
98
|
+
.from(flowNodeInvocations)
|
|
99
|
+
.where(and(eq(flowNodeInvocations.flowId, flowId), eq(flowNodeInvocations.nodeId, nodeId), nullGuard))
|
|
100
|
+
.orderBy(desc(flowNodeInvocations.visitSeq))
|
|
101
|
+
.limit(1);
|
|
102
|
+
return rows[0] ?? null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// Find-or-create the invocation for a logical node dispatch.
|
|
106
|
+
// forceNew=true (kind="fresh") creates a new visit_seq row;
|
|
107
|
+
// forceNew=false reuses the latest. Race-safe via INSERT OR IGNORE +
|
|
108
|
+
// re-select.
|
|
109
|
+
//
|
|
110
|
+
// `guard`, if supplied, is an async () => boolean evaluated INSIDE the
|
|
111
|
+
// same withSharedWrite(dbPath, ...) turn as the insert — that queue
|
|
112
|
+
// already serializes every write against this dbPath to one at a time
|
|
113
|
+
// (state/db.js), so running the check there closes the gap a caller-side
|
|
114
|
+
// check can't: two concurrent findOrCreateInvocation calls for
|
|
115
|
+
// DIFFERENT flowIds but the SAME cross-flow identity (e.g. the same
|
|
116
|
+
// listing's SCORE/PROPOSE dispatch, decided by two parent flow-start
|
|
117
|
+
// runs that both read "nothing in flight yet" before either committed)
|
|
118
|
+
// are forced through this one queue, so the second caller's guard always
|
|
119
|
+
// observes the first caller's already-committed row. A false result
|
|
120
|
+
// skips the insert entirely — findOrCreateInvocation returns null rather
|
|
121
|
+
// than creating a duplicate identity for a different flowId. guard has
|
|
122
|
+
// no opinion on same-flow identity (that's the latest/visitSeq check
|
|
123
|
+
// above, unchanged) — it only ever runs for a genuinely new insert.
|
|
124
|
+
async function findOrCreateInvocation({ flowId, pipelineId = null, nodeId, subflowKey = null, forceNew = false, guard }) {
|
|
125
|
+
const latest = await _findLatestInvocation(flowId, nodeId, subflowKey);
|
|
126
|
+
if (!forceNew && latest) return latest.id;
|
|
127
|
+
const nextVisitSeq = (Number(latest?.visitSeq ?? 0)) + 1;
|
|
128
|
+
const id = randomUUID();
|
|
129
|
+
const r = await withSharedWrite(dbPath, async () => {
|
|
130
|
+
if (guard && !(await guard())) return { rowsAffected: 0, guardRejected: true };
|
|
131
|
+
return db.insert(flowNodeInvocations).values({
|
|
132
|
+
id, flowId, pipelineId, nodeId, subflowKey: subflowKey ?? null, visitSeq: nextVisitSeq, invokedAt: Date.now(),
|
|
133
|
+
}).onConflictDoNothing();
|
|
134
|
+
});
|
|
135
|
+
if (r.guardRejected) return null;
|
|
136
|
+
if (r.rowsAffected > 0) return id;
|
|
137
|
+
// Lost the race — re-select.
|
|
138
|
+
const row = await _findLatestInvocation(flowId, nodeId, subflowKey);
|
|
139
|
+
if (row) return row.id;
|
|
140
|
+
throw new Error(`node-executions: findOrCreateInvocation lost the race and found nothing on re-select (${flowId}/${nodeId})`);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ── store API ──────────────────────────────────────────────────────────
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
_db: client,
|
|
147
|
+
|
|
148
|
+
// write() defers its INSERT through batch-writer (see there) instead
|
|
149
|
+
// of awaiting it immediately — production callers don't need this
|
|
150
|
+
// (they never awaited write()'s DB completion either), but a test
|
|
151
|
+
// asserting read-after-write needs an explicit drain point. Not meant
|
|
152
|
+
// for production use.
|
|
153
|
+
async flush() {
|
|
154
|
+
await flushBatchWriter(dbPath);
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
findOrCreateInvocation,
|
|
158
|
+
|
|
159
|
+
// Public wrapper around the internal _findLatestInvocation helper —
|
|
160
|
+
// used by engine/child-resume-trigger.js's repair-propagation guard
|
|
161
|
+
// to confirm a settling child's spawning invocation is still the
|
|
162
|
+
// CURRENT invocation of its node/subflowKey lineage before
|
|
163
|
+
// resurrecting a terminal 'failed' parent — a stale invocation
|
|
164
|
+
// (superseded by a fresh top-level retry or a loop-back re-entry)
|
|
165
|
+
// must not repair a flow that's since moved on independently.
|
|
166
|
+
// Returns `{id, visitSeq}` or null.
|
|
167
|
+
async getLatestInvocationForNode(flowId, nodeId, subflowKey) {
|
|
168
|
+
return _findLatestInvocation(flowId, nodeId, subflowKey);
|
|
169
|
+
},
|
|
170
|
+
|
|
171
|
+
// A companion live-bridge snapshot() uses this to backfill blockedAtMs
|
|
172
|
+
// for gate items opened before requestedAt existed on wait_json
|
|
173
|
+
// items — invokedAt is the exact moment the gate NODE itself was
|
|
174
|
+
// dispatched (written eagerly at invocation start, independent of
|
|
175
|
+
// the node resolving/blocking), a much tighter bound than
|
|
176
|
+
// flow.createdAt, which reflects the whole flow's creation and can
|
|
177
|
+
// be earlier than the gate node's actual dispatch by however long
|
|
178
|
+
// earlier steps in the same flow took to run.
|
|
179
|
+
async getLatestInvocationInvokedAt(flowId, nodeId, subflowKey) {
|
|
180
|
+
const row = await _findLatestInvocation(flowId, nodeId, subflowKey);
|
|
181
|
+
return row?.invokedAt != null ? Number(row.invokedAt) : null;
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
async getInvocation(id) {
|
|
185
|
+
try {
|
|
186
|
+
const rows = await db.select().from(flowNodeInvocations)
|
|
187
|
+
.where(eq(flowNodeInvocations.id, id));
|
|
188
|
+
const row = rows[0] ?? null;
|
|
189
|
+
if (!row) return null;
|
|
190
|
+
return {
|
|
191
|
+
id: row.id,
|
|
192
|
+
flowId: row.flowId,
|
|
193
|
+
pipelineId: row.pipelineId,
|
|
194
|
+
nodeId: row.nodeId,
|
|
195
|
+
subflowKey: row.subflowKey,
|
|
196
|
+
visitSeq: Number(row.visitSeq),
|
|
197
|
+
invokedAt: Number(row.invokedAt),
|
|
198
|
+
resultWrittenAt: row.resultWrittenAt != null ? Number(row.resultWrittenAt) : null,
|
|
199
|
+
};
|
|
200
|
+
} catch { return null; }
|
|
201
|
+
},
|
|
202
|
+
|
|
203
|
+
async listInvocationIdsForNode(flowId, nodeId) {
|
|
204
|
+
try {
|
|
205
|
+
const rows = await db.select({ id: flowNodeInvocations.id }).from(flowNodeInvocations)
|
|
206
|
+
.where(and(
|
|
207
|
+
eq(flowNodeInvocations.flowId, flowId),
|
|
208
|
+
eq(flowNodeInvocations.nodeId, nodeId),
|
|
209
|
+
isNotNull(flowNodeInvocations.subflowKey),
|
|
210
|
+
));
|
|
211
|
+
return rows.map((r) => r.id);
|
|
212
|
+
} catch { return []; }
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
async listInvocationIdsForNodes(flowId, nodeIds) {
|
|
216
|
+
if (!nodeIds?.length) return new Map();
|
|
217
|
+
try {
|
|
218
|
+
const rows = await db.select({ id: flowNodeInvocations.id, nodeId: flowNodeInvocations.nodeId })
|
|
219
|
+
.from(flowNodeInvocations)
|
|
220
|
+
.where(and(
|
|
221
|
+
eq(flowNodeInvocations.flowId, flowId),
|
|
222
|
+
inArray(flowNodeInvocations.nodeId, nodeIds),
|
|
223
|
+
isNotNull(flowNodeInvocations.subflowKey),
|
|
224
|
+
));
|
|
225
|
+
const out = new Map();
|
|
226
|
+
for (const row of rows) {
|
|
227
|
+
if (!out.has(row.nodeId)) out.set(row.nodeId, []);
|
|
228
|
+
out.get(row.nodeId).push(row.id);
|
|
229
|
+
}
|
|
230
|
+
return out;
|
|
231
|
+
} catch { return new Map(); }
|
|
232
|
+
},
|
|
233
|
+
|
|
234
|
+
async listSubflowNodeIdsForFlow(flowId) {
|
|
235
|
+
try {
|
|
236
|
+
const rows = await db.selectDistinct({ nodeId: flowNodeInvocations.nodeId }).from(flowNodeInvocations)
|
|
237
|
+
.where(and(eq(flowNodeInvocations.flowId, flowId), isNotNull(flowNodeInvocations.subflowKey)));
|
|
238
|
+
return rows.map((r) => r.nodeId);
|
|
239
|
+
} catch { return []; }
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
// Create the invocation row before execution starts. Returns
|
|
243
|
+
// { invocationId, isNew } on success, null on DB error (fire-safe).
|
|
244
|
+
// Callers must thread both values through to write() so the post-
|
|
245
|
+
// execution completion row links to the same invocation and the
|
|
246
|
+
// fresh/re-entry invariant check can be satisfied without a second
|
|
247
|
+
// _findLatestInvocation lookup.
|
|
248
|
+
async enter({ flowId, pipelineId = null, nodeId, subflowKey = null, kind }) {
|
|
249
|
+
if (!VALID_EXECUTION_KINDS.has(kind)) {
|
|
250
|
+
throw new Error(`node-executions: enter() requires a valid kind (fresh|retry|revival|recheck), got ${JSON.stringify(kind)}`);
|
|
251
|
+
}
|
|
252
|
+
const forceNew = kind === "fresh";
|
|
253
|
+
try {
|
|
254
|
+
const priorLatest = await _findLatestInvocation(flowId, nodeId, subflowKey);
|
|
255
|
+
const invocationId = await findOrCreateInvocation({ flowId, pipelineId, nodeId, subflowKey, forceNew });
|
|
256
|
+
const isNew = !priorLatest || (forceNew && invocationId !== priorLatest.id);
|
|
257
|
+
return { invocationId, isNew };
|
|
258
|
+
} catch {
|
|
259
|
+
return null;
|
|
260
|
+
}
|
|
261
|
+
},
|
|
262
|
+
|
|
263
|
+
// The node_id of the most recently created invocation for this flow.
|
|
264
|
+
// After enter() is called before execution, this correctly identifies the
|
|
265
|
+
// in-flight node even during execution, not just after completion.
|
|
266
|
+
// Returns null if no invocations exist yet.
|
|
267
|
+
//
|
|
268
|
+
// subflow_key IS NULL scopes this to the flow's own top-level dispatch
|
|
269
|
+
// sequence — enter() (this store's own write path, called from
|
|
270
|
+
// executor.js's runFlow loop) always passes subflowKey: null, so this
|
|
271
|
+
// is the same slice of rows that path ever creates. Per-item child-
|
|
272
|
+
// spawn bookkeeping rows (subflow_key set, written by
|
|
273
|
+
// resolveOrCreateChild/spawn-child.js, never by enter()) are a
|
|
274
|
+
// separate concern and are excluded here on purpose — an unscoped
|
|
275
|
+
// query would occasionally return one of those instead of the node's
|
|
276
|
+
// own identity row, which happens to carry the same node_id today (so
|
|
277
|
+
// callers reading only node_id were never actually wrong) but isn't a
|
|
278
|
+
// designed guarantee. This makes it correct by construction instead.
|
|
279
|
+
async findCurrentStep(flowId) {
|
|
280
|
+
try {
|
|
281
|
+
// rowid DESC breaks ties on invoked_at (Date.now(), millisecond
|
|
282
|
+
// resolution — two rapid successive dispatches for the same flow
|
|
283
|
+
// can genuinely land in the same millisecond). rowid is SQLite's
|
|
284
|
+
// own implicit, strictly-insertion-order column on this table
|
|
285
|
+
// (never declared WITHOUT ROWID) — the only reliable tiebreaker,
|
|
286
|
+
// since `id` is a random UUID with no correlation to insert order.
|
|
287
|
+
// Found via a real, reproducible test flake (not hypothetical):
|
|
288
|
+
// ORDER BY invoked_at DESC LIMIT 1 alone returned either of two
|
|
289
|
+
// same-millisecond rows nondeterministically.
|
|
290
|
+
const { rows } = await client.execute({
|
|
291
|
+
sql: `SELECT node_id FROM flow_node_invocations WHERE flow_id = ? AND subflow_key IS NULL ORDER BY invoked_at DESC, rowid DESC LIMIT 1`,
|
|
292
|
+
args: [flowId],
|
|
293
|
+
});
|
|
294
|
+
return rows[0]?.node_id ?? null;
|
|
295
|
+
} catch { return null; }
|
|
296
|
+
},
|
|
297
|
+
|
|
298
|
+
// `kind` is required and self-validating. Invariant violations throw
|
|
299
|
+
// OUTSIDE any try/catch — must fail loud in tests/dev. Genuine DB
|
|
300
|
+
// failures keep the fire-and-forget contract: swallowed, not thrown.
|
|
301
|
+
// When invocationId + isNewInvocation are provided (from a prior enter()
|
|
302
|
+
// call), the internal findOrCreateInvocation is skipped and those values
|
|
303
|
+
// are trusted directly — saving one DB round-trip per dispatch.
|
|
304
|
+
async write({ flowId, pipelineId = null, nodeId, subflowKey = null, enteredAt, exitedAt, kind, invocationId: providedId = null, isNewInvocation = null }) {
|
|
305
|
+
if (!VALID_EXECUTION_KINDS.has(kind)) {
|
|
306
|
+
throw new Error(`node-executions: write() requires a valid kind (fresh|retry|revival|recheck), got ${JSON.stringify(kind)}`);
|
|
307
|
+
}
|
|
308
|
+
const forceNew = kind === "fresh";
|
|
309
|
+
let wasCreated, invocationId;
|
|
310
|
+
try {
|
|
311
|
+
if (providedId != null && isNewInvocation != null) {
|
|
312
|
+
invocationId = providedId;
|
|
313
|
+
wasCreated = isNewInvocation;
|
|
314
|
+
} else {
|
|
315
|
+
const priorLatest = await _findLatestInvocation(flowId, nodeId, subflowKey);
|
|
316
|
+
invocationId = await findOrCreateInvocation({ flowId, pipelineId, nodeId, subflowKey, forceNew });
|
|
317
|
+
wasCreated = !priorLatest || (forceNew && invocationId !== priorLatest.id);
|
|
318
|
+
}
|
|
319
|
+
} catch {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (kind === "fresh" && !wasCreated) {
|
|
323
|
+
throw new Error(`node-executions: kind="fresh" but invocation ${invocationId} already existed (flow=${flowId} node=${nodeId})`);
|
|
324
|
+
}
|
|
325
|
+
if (kind !== "fresh" && wasCreated) {
|
|
326
|
+
throw new Error(`node-executions: kind="${kind}" but this is the first execution for a brand-new invocation (flow=${flowId} node=${nodeId}) — should be "fresh"`);
|
|
327
|
+
}
|
|
328
|
+
// Routed through batch-writer instead of an immediate awaited
|
|
329
|
+
// INSERT: this is the highest-volume write in the codebase (one per
|
|
330
|
+
// node execution attempt), already fire-and-forget by contract (see
|
|
331
|
+
// this file's header) and every real call site (executor.js) fires
|
|
332
|
+
// it without awaiting the result — safe to defer and coalesce with
|
|
333
|
+
// other pending writes into one db.batch() round-trip instead of
|
|
334
|
+
// one write-lock acquisition per call. executionId is generated
|
|
335
|
+
// synchronously up front (a random UUID, not derived from the row
|
|
336
|
+
// actually landing), so the caller-facing return value doesn't
|
|
337
|
+
// depend on the deferred write having completed yet.
|
|
338
|
+
const executionId = randomUUID();
|
|
339
|
+
enqueue(dbPath, db.insert(flowNodeExecutions).values({
|
|
340
|
+
id: executionId, invocationId, enteredAt, exitedAt, kind,
|
|
341
|
+
}));
|
|
342
|
+
return { invocationId, executionId };
|
|
343
|
+
},
|
|
344
|
+
|
|
345
|
+
// Writes an invocation's own resultJson once it completes, no size
|
|
346
|
+
// cap (see this file's own header comment). resultTooLargeBytes is
|
|
347
|
+
// always written null here — a non-null value marks a row whose
|
|
348
|
+
// result was never stored.
|
|
349
|
+
//
|
|
350
|
+
// Two things this deliberately isn't: (1) it doesn't throw on
|
|
351
|
+
// ordinary failure — same fire-and-forget contract as write() above,
|
|
352
|
+
// a result-recording failure must never disrupt flow execution; (2)
|
|
353
|
+
// it isn't a blind UPDATE. `flow_node_invocations` has no CAS/
|
|
354
|
+
// revision of its own (unlike flow_runs), so an unconditional write
|
|
355
|
+
// here would silently accept last-write-wins on a genuine double-
|
|
356
|
+
// completion race with zero signal — exactly the detection
|
|
357
|
+
// flow_runs.revision's own conflict handling already provides at the
|
|
358
|
+
// flow level. This does the per-row equivalent: read the invocation's
|
|
359
|
+
// current resultWrittenAt, then write conditioned on that same value
|
|
360
|
+
// still being current. A 0-row UPDATE means something else won this
|
|
361
|
+
// invocation's write between the read and the write — the caller
|
|
362
|
+
// gets { ok: false, reason: "race" } back and is expected to log it
|
|
363
|
+
// loudly (this module has no logger of its own — see file header;
|
|
364
|
+
// executor.js does).
|
|
365
|
+
async writeResult(invocationId, result) {
|
|
366
|
+
if (!invocationId) return { ok: false, reason: "no-invocation-id" };
|
|
367
|
+
let serialized;
|
|
368
|
+
try {
|
|
369
|
+
serialized = JSON.stringify(result ?? null);
|
|
370
|
+
} catch {
|
|
371
|
+
return { ok: false, reason: "unserializable" };
|
|
372
|
+
}
|
|
373
|
+
const byteLength = Buffer.byteLength(serialized, "utf8");
|
|
374
|
+
const now = Date.now();
|
|
375
|
+
try {
|
|
376
|
+
// Read-then-conditional-write, retried as one unit on SQLITE_BUSY:
|
|
377
|
+
// a retry re-reads resultWrittenAt fresh and re-attempts the
|
|
378
|
+
// conditional update against that current value, rather than
|
|
379
|
+
// blindly retrying just the UPDATE against a now-possibly-stale
|
|
380
|
+
// `expected` captured before the retry.
|
|
381
|
+
return await withSharedWrite(dbPath, async () => {
|
|
382
|
+
const rows = await db.select({ resultWrittenAt: flowNodeInvocations.resultWrittenAt })
|
|
383
|
+
.from(flowNodeInvocations)
|
|
384
|
+
.where(eq(flowNodeInvocations.id, invocationId));
|
|
385
|
+
if (rows.length === 0) return { ok: false, reason: "no-such-invocation" };
|
|
386
|
+
const expected = rows[0].resultWrittenAt ?? null;
|
|
387
|
+
const whereExpected = expected === null
|
|
388
|
+
? and(eq(flowNodeInvocations.id, invocationId), isNull(flowNodeInvocations.resultWrittenAt))
|
|
389
|
+
: and(eq(flowNodeInvocations.id, invocationId), eq(flowNodeInvocations.resultWrittenAt, expected));
|
|
390
|
+
const r = await db.update(flowNodeInvocations)
|
|
391
|
+
.set({ resultJson: serialized, resultWrittenAt: now, resultTooLargeBytes: null })
|
|
392
|
+
.where(whereExpected);
|
|
393
|
+
if (r.rowsAffected === 0) return { ok: false, reason: "race" };
|
|
394
|
+
return { ok: true, bytes: byteLength };
|
|
395
|
+
});
|
|
396
|
+
} catch {
|
|
397
|
+
return { ok: false, reason: "db-error" };
|
|
398
|
+
}
|
|
399
|
+
},
|
|
400
|
+
|
|
401
|
+
// Single execution row by id — used by failure-store.js's read-time
|
|
402
|
+
// resolved-status derivation to check whether a failure's linked
|
|
403
|
+
// execution recorded a genuine thrown error (error IS NOT NULL,
|
|
404
|
+
// eligible for same-flow auto-resolve) vs a soft state-marker with
|
|
405
|
+
// no throw at all (error IS NULL, never eligible for same-flow
|
|
406
|
+
// auto-resolve — only cross-flow supersession applies to those).
|
|
407
|
+
async getExecution(id) {
|
|
408
|
+
try {
|
|
409
|
+
const rows = await db.select().from(flowNodeExecutions).where(eq(flowNodeExecutions.id, id));
|
|
410
|
+
const row = rows[0] ?? null;
|
|
411
|
+
if (!row) return null;
|
|
412
|
+
return { id: row.id, invocationId: row.invocationId, enteredAt: Number(row.enteredAt), exitedAt: Number(row.exitedAt), kind: row.kind };
|
|
413
|
+
} catch { return null; }
|
|
414
|
+
},
|
|
415
|
+
|
|
416
|
+
// Latest execution outcome for a (pipelineId, nodeId, subflowKey)
|
|
417
|
+
// tuple ACROSS ANY FLOW — not scoped to one flow_id. Used by
|
|
418
|
+
// failure-store.js's cross-flow "auto:branch-success" derivation:
|
|
419
|
+
// when a later flow supersedes an earlier one's failure for the
|
|
420
|
+
// same item (e.g. a re-shortlisted listing that succeeds on a
|
|
421
|
+
// fresh flow), this is how the earlier failure is found to now be
|
|
422
|
+
// resolved. Returns identity + timing only, not error content
|
|
423
|
+
// (this table doesn't know success vs failure at
|
|
424
|
+
// all; the caller checks failure_events for that execution_id
|
|
425
|
+
// instead: no row there means it succeeded).
|
|
426
|
+
async getLatestExecutionOutcomeForItem({ pipelineId, nodeId, subflowKey }) {
|
|
427
|
+
try {
|
|
428
|
+
const { rows } = await client.execute({
|
|
429
|
+
sql: `SELECT e.id AS execution_id, e.exited_at
|
|
430
|
+
FROM flow_node_executions e JOIN flow_node_invocations i ON e.invocation_id = i.id
|
|
431
|
+
WHERE i.pipeline_id = ? AND i.node_id = ? AND i.subflow_key IS ?
|
|
432
|
+
ORDER BY e.exited_at DESC LIMIT 1`,
|
|
433
|
+
args: [pipelineId, nodeId, subflowKey ?? null],
|
|
434
|
+
});
|
|
435
|
+
const row = rows[0] ?? null;
|
|
436
|
+
return row ? { executionId: row.execution_id, exitedAt: Number(row.exited_at) } : null;
|
|
437
|
+
} catch { return null; }
|
|
438
|
+
},
|
|
439
|
+
|
|
440
|
+
async getLatestExecutionForNode({ flowId, nodeId, subflowKey = null }) {
|
|
441
|
+
try {
|
|
442
|
+
const { rows } = await client.execute({
|
|
443
|
+
sql: `SELECT e.id AS execution_id, e.invocation_id, e.exited_at
|
|
444
|
+
FROM flow_node_executions e JOIN flow_node_invocations i ON e.invocation_id = i.id
|
|
445
|
+
WHERE i.flow_id = ? AND i.node_id = ? AND i.subflow_key IS ?
|
|
446
|
+
ORDER BY e.entered_at DESC LIMIT 1`,
|
|
447
|
+
args: [flowId, nodeId, subflowKey ?? null],
|
|
448
|
+
});
|
|
449
|
+
const row = rows[0] ?? null;
|
|
450
|
+
return row
|
|
451
|
+
? { invocationId: row.invocation_id, executionId: row.execution_id, exitedAt: Number(row.exited_at) }
|
|
452
|
+
: null;
|
|
453
|
+
} catch { return null; }
|
|
454
|
+
},
|
|
455
|
+
|
|
456
|
+
async queryByPipeline({ pipelineId, sinceMs = null, untilMs = null } = {}) {
|
|
457
|
+
const conditions = ["i.pipeline_id = ?"];
|
|
458
|
+
const args = [pipelineId];
|
|
459
|
+
if (sinceMs != null) { conditions.push("e.exited_at >= ?"); args.push(sinceMs); }
|
|
460
|
+
if (untilMs != null) { conditions.push("e.exited_at <= ?"); args.push(untilMs); }
|
|
461
|
+
const { rows } = await client.execute({
|
|
462
|
+
sql: `SELECT i.node_id, e.entered_at, e.exited_at
|
|
463
|
+
FROM flow_node_executions e JOIN flow_node_invocations i ON e.invocation_id = i.id
|
|
464
|
+
WHERE ${conditions.join(" AND ")}`,
|
|
465
|
+
args,
|
|
466
|
+
});
|
|
467
|
+
return rows;
|
|
468
|
+
},
|
|
469
|
+
|
|
470
|
+
// queryErrorsByFlow/queryErrorsByFlowIds live in
|
|
471
|
+
// state/failure-store.js — this table stores no error content, so
|
|
472
|
+
// it has nothing to query for "which nodes errored." failure_events
|
|
473
|
+
// is the only source; see failureStore.queryErrorsByFlow(Ids).
|
|
474
|
+
|
|
475
|
+
async summarizeChildrenByFlowIds(flowIds) {
|
|
476
|
+
if (!Array.isArray(flowIds) || flowIds.length === 0) return new Map();
|
|
477
|
+
try {
|
|
478
|
+
const placeholders = flowIds.map(() => "?").join(", ");
|
|
479
|
+
const { rows } = await client.execute({
|
|
480
|
+
sql: `SELECT i.flow_id, fr.status, COUNT(*) AS cnt
|
|
481
|
+
FROM flow_node_invocations i
|
|
482
|
+
JOIN flow_runs fr ON fr.parent_invocation_id = i.id
|
|
483
|
+
WHERE i.flow_id IN (${placeholders})
|
|
484
|
+
GROUP BY i.flow_id, fr.status`,
|
|
485
|
+
args: flowIds,
|
|
486
|
+
});
|
|
487
|
+
const result = new Map();
|
|
488
|
+
for (const { flow_id, status, cnt } of rows) {
|
|
489
|
+
if (!result.has(flow_id)) result.set(flow_id, { total: 0, pending: 0, failed: 0, skipped: 0, succeeded: 0 });
|
|
490
|
+
const c = result.get(flow_id);
|
|
491
|
+
const count = Number(cnt);
|
|
492
|
+
c.total += count;
|
|
493
|
+
if (status === "running" || status === "blocked" || status === "queued") c.pending += count;
|
|
494
|
+
else if (status === "failed") c.failed += count;
|
|
495
|
+
else if (status === "skipped") c.skipped += count;
|
|
496
|
+
else if (status === "succeeded") c.succeeded += count;
|
|
497
|
+
}
|
|
498
|
+
return result;
|
|
499
|
+
} catch { return new Map(); }
|
|
500
|
+
},
|
|
501
|
+
|
|
502
|
+
async listInvocationsForFlow(flowId) {
|
|
503
|
+
const invocations = await db.select({
|
|
504
|
+
id: flowNodeInvocations.id,
|
|
505
|
+
nodeId: flowNodeInvocations.nodeId,
|
|
506
|
+
subflowKey: flowNodeInvocations.subflowKey,
|
|
507
|
+
visitSeq: flowNodeInvocations.visitSeq,
|
|
508
|
+
invokedAt: flowNodeInvocations.invokedAt,
|
|
509
|
+
}).from(flowNodeInvocations)
|
|
510
|
+
.where(and(eq(flowNodeInvocations.flowId, flowId), isNull(flowNodeInvocations.subflowKey)))
|
|
511
|
+
.orderBy(asc(flowNodeInvocations.invokedAt));
|
|
512
|
+
if (invocations.length === 0) return [];
|
|
513
|
+
const executions = await db.select().from(flowNodeExecutions)
|
|
514
|
+
.where(inArray(flowNodeExecutions.invocationId, invocations.map((i) => i.id)))
|
|
515
|
+
.orderBy(asc(flowNodeExecutions.enteredAt));
|
|
516
|
+
const byInvocation = new Map();
|
|
517
|
+
for (const e of executions) {
|
|
518
|
+
if (!byInvocation.has(e.invocationId)) byInvocation.set(e.invocationId, []);
|
|
519
|
+
byInvocation.get(e.invocationId).push(e);
|
|
520
|
+
}
|
|
521
|
+
// No error field here (Phase 1.5 — this table doesn't know success
|
|
522
|
+
// vs failure at all anymore). latestExecutionId is exposed so
|
|
523
|
+
// callers can cross-reference failureStore.queryErrorsByFlow(Ids)
|
|
524
|
+
// themselves to determine whether the MOST RECENT attempt of this
|
|
525
|
+
// invocation failed — see routes/runs.js's buildTreeNodeRows.
|
|
526
|
+
return invocations.map((inv) => {
|
|
527
|
+
const execs = byInvocation.get(inv.id) ?? [];
|
|
528
|
+
const latest = execs.length > 0 ? execs[execs.length - 1] : null;
|
|
529
|
+
return {
|
|
530
|
+
invocationId: inv.id,
|
|
531
|
+
nodeId: inv.nodeId,
|
|
532
|
+
subflowKey: inv.subflowKey,
|
|
533
|
+
visitSeq: Number(inv.visitSeq),
|
|
534
|
+
invokedAt: Number(inv.invokedAt),
|
|
535
|
+
executionCount: execs.length,
|
|
536
|
+
enteredAt: latest ? Number(latest.enteredAt) : null,
|
|
537
|
+
exitedAt: latest ? Number(latest.exitedAt) : null,
|
|
538
|
+
latestExecutionId: latest?.id ?? null,
|
|
539
|
+
kind: latest?.kind ?? null,
|
|
540
|
+
// `kind` above is the LATEST execution only — a subflow node
|
|
541
|
+
// re-entered via "recheck" once its children finish always
|
|
542
|
+
// masks an earlier "retry" among its own executions. Derived
|
|
543
|
+
// from the same `execs` array already loaded above — no extra
|
|
544
|
+
// query.
|
|
545
|
+
everRetriedOrRevived: execs.some((e) => e.kind === "retry" || e.kind === "revival"),
|
|
546
|
+
};
|
|
547
|
+
});
|
|
548
|
+
},
|
|
549
|
+
|
|
550
|
+
// Option B, Phase B1 — one resultJson row per top-level node, ready
|
|
551
|
+
// for assembleState
|
|
552
|
+
// (engine/executor-helpers.js) to replay into a working state
|
|
553
|
+
// object. Two things this does that listInvocationsForFlow (above)
|
|
554
|
+
// deliberately doesn't:
|
|
555
|
+
// - Reduces to exactly one row per node_id — the chronologically
|
|
556
|
+
// LATEST occurrence (a genuine loop-back creates a new row with
|
|
557
|
+
// a higher visit_seq; a retry reuses the same row in place). The
|
|
558
|
+
// dedup preserves that latest occurrence's own position in
|
|
559
|
+
// invocation order, not its first-seen position — so if node A
|
|
560
|
+
// re-fires (loop-back) AFTER node B already ran, A's replay
|
|
561
|
+
// position correctly moves to after B's, matching what actually
|
|
562
|
+
// happened chronologically. (Retries reuse one row and so never
|
|
563
|
+
// move position at all — no ordering question there.)
|
|
564
|
+
// - Orders by invoked_at then rowid, the same tiebreak
|
|
565
|
+
// findCurrentStep uses — invoked_at alone (Date.now(), millisecond
|
|
566
|
+
// resolution) can't distinguish two dispatches landing in the
|
|
567
|
+
// same millisecond; rowid (SQLite's own strictly-insertion-order
|
|
568
|
+
// column, unlike the random-UUID id) breaks the tie deterministically.
|
|
569
|
+
async listResultsForFlow(flowId) {
|
|
570
|
+
try {
|
|
571
|
+
// result_written_at IS NOT NULL is load-bearing, not an
|
|
572
|
+
// optimization: without it, a node that re-enters itself (a
|
|
573
|
+
// loop-back, or any revisit) creates a NEW in-flight invocation
|
|
574
|
+
// whose invoked_at is later than its own prior completed
|
|
575
|
+
// invocation — the per-node dedup below keeps only the latest
|
|
576
|
+
// row BY INVOCATION, so it would keep that in-flight,
|
|
577
|
+
// not-yet-written row and silently drop the earlier COMPLETED
|
|
578
|
+
// one entirely, even though the caller (assembleState(), read
|
|
579
|
+
// from INSIDE that very re-entry's own dispatch) needs exactly
|
|
580
|
+
// that prior value to compute its own next result. The failure
|
|
581
|
+
// mode is silent and livelocks: a LOOP node's edge condition
|
|
582
|
+
// never advances and the node re-dispatches forever.
|
|
583
|
+
const { rows } = await client.execute({
|
|
584
|
+
sql: `SELECT node_id, visit_seq, result_json, result_written_at, result_too_large_bytes
|
|
585
|
+
FROM flow_node_invocations
|
|
586
|
+
WHERE flow_id = ? AND subflow_key IS NULL AND result_written_at IS NOT NULL
|
|
587
|
+
ORDER BY invoked_at ASC, rowid ASC`,
|
|
588
|
+
args: [flowId],
|
|
589
|
+
});
|
|
590
|
+
const byNode = new Map();
|
|
591
|
+
for (const r of rows) {
|
|
592
|
+
byNode.delete(r.node_id);
|
|
593
|
+
byNode.set(r.node_id, r);
|
|
594
|
+
}
|
|
595
|
+
return [...byNode.values()].map((r) => ({
|
|
596
|
+
nodeId: r.node_id,
|
|
597
|
+
visitSeq: Number(r.visit_seq),
|
|
598
|
+
resultWrittenAt: r.result_written_at != null ? Number(r.result_written_at) : null,
|
|
599
|
+
resultTooLargeBytes: r.result_too_large_bytes != null ? Number(r.result_too_large_bytes) : null,
|
|
600
|
+
resultJson: r.result_json,
|
|
601
|
+
}));
|
|
602
|
+
} catch { return []; }
|
|
603
|
+
},
|
|
604
|
+
|
|
605
|
+
// Single-node-scoped sibling of listResultsForFlow — Option B's
|
|
606
|
+
// "needs:" accessor (engine/needs-resolver.js) only ever wants one
|
|
607
|
+
// named node's latest result from a (possibly ancestor) flow, not
|
|
608
|
+
// that flow's entire result set, so this skips fetching/deduping
|
|
609
|
+
// every other node's rows just to throw them away. Same latest-
|
|
610
|
+
// visit_seq convention (ORDER BY invoked_at DESC, rowid DESC LIMIT 1)
|
|
611
|
+
// as findCurrentStep/listResultsForFlow. Returns null — not a
|
|
612
|
+
// resultWrittenAt:null row — when there's no invocation at all for
|
|
613
|
+
// this (flowId, nodeId), so a caller can tell "never dispatched"
|
|
614
|
+
// apart from "dispatched, not yet complete" without a second query.
|
|
615
|
+
async getResultForNode(flowId, nodeId) {
|
|
616
|
+
try {
|
|
617
|
+
const { rows } = await client.execute({
|
|
618
|
+
sql: `SELECT node_id, visit_seq, result_json, result_written_at, result_too_large_bytes
|
|
619
|
+
FROM flow_node_invocations
|
|
620
|
+
WHERE flow_id = ? AND node_id = ? AND subflow_key IS NULL
|
|
621
|
+
ORDER BY invoked_at DESC, rowid DESC
|
|
622
|
+
LIMIT 1`,
|
|
623
|
+
args: [flowId, nodeId],
|
|
624
|
+
});
|
|
625
|
+
if (rows.length === 0) return null;
|
|
626
|
+
const r = rows[0];
|
|
627
|
+
return {
|
|
628
|
+
nodeId: r.node_id,
|
|
629
|
+
visitSeq: Number(r.visit_seq),
|
|
630
|
+
resultWrittenAt: r.result_written_at != null ? Number(r.result_written_at) : null,
|
|
631
|
+
resultTooLargeBytes: r.result_too_large_bytes != null ? Number(r.result_too_large_bytes) : null,
|
|
632
|
+
resultJson: r.result_json,
|
|
633
|
+
};
|
|
634
|
+
} catch { return null; }
|
|
635
|
+
},
|
|
636
|
+
|
|
637
|
+
// gate_records — see the gateRecords def in state/schema.js for why
|
|
638
|
+
// this needs its own identity-keyed table rather than riding along
|
|
639
|
+
// in resultJson.
|
|
640
|
+
// Point upsert: a gate node only ever touches its own (flowId, nodeId)
|
|
641
|
+
// row, so there's no merge/replay involved and no staleness possible.
|
|
642
|
+
async writeGateRecord(flowId, nodeId, record) {
|
|
643
|
+
try {
|
|
644
|
+
const recordJson = JSON.stringify(record ?? null);
|
|
645
|
+
await withSharedWrite(dbPath, () => client.execute({
|
|
646
|
+
sql: `INSERT INTO gate_records (flow_id, node_id, record_json, updated_at)
|
|
647
|
+
VALUES (?, ?, ?, ?)
|
|
648
|
+
ON CONFLICT(flow_id, node_id) DO UPDATE SET
|
|
649
|
+
record_json = excluded.record_json,
|
|
650
|
+
updated_at = excluded.updated_at`,
|
|
651
|
+
args: [flowId, nodeId, recordJson, Date.now()],
|
|
652
|
+
}));
|
|
653
|
+
return { ok: true };
|
|
654
|
+
} catch (err) {
|
|
655
|
+
return { ok: false, reason: err?.message ?? String(err) };
|
|
656
|
+
}
|
|
657
|
+
},
|
|
658
|
+
|
|
659
|
+
// Point lookup — always the real, current value for this one gate,
|
|
660
|
+
// never a stale snapshot. Returns null if this gate has never
|
|
661
|
+
// written a record (not the same as "record exists but is empty").
|
|
662
|
+
async getGateRecord(flowId, nodeId) {
|
|
663
|
+
try {
|
|
664
|
+
const { rows } = await client.execute({
|
|
665
|
+
sql: `SELECT record_json FROM gate_records WHERE flow_id = ? AND node_id = ?`,
|
|
666
|
+
args: [flowId, nodeId],
|
|
667
|
+
});
|
|
668
|
+
if (rows.length === 0) return null;
|
|
669
|
+
try {
|
|
670
|
+
return JSON.parse(rows[0].record_json ?? "null");
|
|
671
|
+
} catch {
|
|
672
|
+
return null;
|
|
673
|
+
}
|
|
674
|
+
} catch { return null; }
|
|
675
|
+
},
|
|
676
|
+
|
|
677
|
+
// Batch sibling of getGateRecord — "every gate this flow has ever
|
|
678
|
+
// opened," for a consumer that needs to display the whole flow's
|
|
679
|
+
// gate state at once (e.g. routes/runs.js's run-detail response,
|
|
680
|
+
// once B2.5 cuts it over) rather than one specific node. Each row
|
|
681
|
+
// is independently correct (no shared key, no merge), so building
|
|
682
|
+
// the {[nodeId]: record} dict here is just a fan-in of already-
|
|
683
|
+
// correct point values — nothing about batching reopens the
|
|
684
|
+
// staleness risk assembleState()'s own gates merge has.
|
|
685
|
+
async listGateRecordsForFlow(flowId) {
|
|
686
|
+
try {
|
|
687
|
+
const { rows } = await client.execute({
|
|
688
|
+
sql: `SELECT node_id, record_json FROM gate_records WHERE flow_id = ?`,
|
|
689
|
+
args: [flowId],
|
|
690
|
+
});
|
|
691
|
+
const out = {};
|
|
692
|
+
for (const r of rows) {
|
|
693
|
+
try {
|
|
694
|
+
out[r.node_id] = JSON.parse(r.record_json ?? "null");
|
|
695
|
+
} catch { /* skip a corrupted row rather than fail the whole batch */ }
|
|
696
|
+
}
|
|
697
|
+
return out;
|
|
698
|
+
} catch { return {}; }
|
|
699
|
+
},
|
|
700
|
+
|
|
701
|
+
// No error field here either — same Phase 1.5 reasoning. Callers
|
|
702
|
+
// needing per-attempt failure content cross-reference executionId
|
|
703
|
+
// against failureStore.queryErrorsByFlow(Ids) themselves (see
|
|
704
|
+
// routes/runs.js's sendInvocationDetail).
|
|
705
|
+
async listExecutionsForInvocation(invocationId) {
|
|
706
|
+
const rows = await db.select().from(flowNodeExecutions)
|
|
707
|
+
.where(eq(flowNodeExecutions.invocationId, invocationId))
|
|
708
|
+
.orderBy(asc(flowNodeExecutions.enteredAt));
|
|
709
|
+
return rows.map((e) => ({
|
|
710
|
+
executionId: e.id,
|
|
711
|
+
enteredAt: Number(e.enteredAt),
|
|
712
|
+
exitedAt: Number(e.exitedAt),
|
|
713
|
+
kind: e.kind,
|
|
714
|
+
}));
|
|
715
|
+
},
|
|
716
|
+
|
|
717
|
+
async hasInvocations(flowIds) {
|
|
718
|
+
if (!Array.isArray(flowIds) || flowIds.length === 0) return new Set();
|
|
719
|
+
try {
|
|
720
|
+
const rows = await db.selectDistinct({ flowId: flowNodeInvocations.flowId }).from(flowNodeInvocations)
|
|
721
|
+
.where(inArray(flowNodeInvocations.flowId, flowIds));
|
|
722
|
+
return new Set(rows.map((r) => r.flowId));
|
|
723
|
+
} catch { return new Set(); }
|
|
724
|
+
},
|
|
725
|
+
|
|
726
|
+
// Batched version of "which top-level nodes did this flow visit" —
|
|
727
|
+
// backing the dashboard auto-funnel's per-node visit check without
|
|
728
|
+
// one HTTP/DB round-trip per run. subflow_key IS NULL for the same
|
|
729
|
+
// reason listInvocationsForFlow/findCurrentStep filter on it —
|
|
730
|
+
// only the flow's own top-level dispatch sequence, not per-item
|
|
731
|
+
// child-spawn bookkeeping rows.
|
|
732
|
+
async listVisitedNodeIdsByFlow(flowIds) {
|
|
733
|
+
if (!Array.isArray(flowIds) || flowIds.length === 0) return new Map();
|
|
734
|
+
try {
|
|
735
|
+
const rows = await db.selectDistinct({
|
|
736
|
+
flowId: flowNodeInvocations.flowId,
|
|
737
|
+
nodeId: flowNodeInvocations.nodeId,
|
|
738
|
+
}).from(flowNodeInvocations)
|
|
739
|
+
.where(and(inArray(flowNodeInvocations.flowId, flowIds), isNull(flowNodeInvocations.subflowKey)));
|
|
740
|
+
const result = new Map();
|
|
741
|
+
for (const { flowId, nodeId } of rows) {
|
|
742
|
+
if (!result.has(flowId)) result.set(flowId, new Set());
|
|
743
|
+
result.get(flowId).add(nodeId);
|
|
744
|
+
}
|
|
745
|
+
return result;
|
|
746
|
+
} catch { return new Map(); }
|
|
747
|
+
},
|
|
748
|
+
|
|
749
|
+
close() { try { client.close(); } catch { /* ignore */ } },
|
|
750
|
+
};
|
|
751
|
+
} catch (err) {
|
|
752
|
+
// If the DB can't open, return a no-op store so callers don't need try/catch.
|
|
753
|
+
const noop = () => {};
|
|
754
|
+
return {
|
|
755
|
+
_db: null, flush: async () => {}, findOrCreateInvocation: () => { throw new Error("node-executions store unavailable"); },
|
|
756
|
+
getInvocation: async () => null, listInvocationIdsForNode: async () => [], listSubflowNodeIdsForFlow: async () => [],
|
|
757
|
+
write: noop, queryByPipeline: async () => [],
|
|
758
|
+
writeResult: async () => ({ ok: false, reason: "db-error" }),
|
|
759
|
+
listResultsForFlow: async () => [],
|
|
760
|
+
getResultForNode: async () => null,
|
|
761
|
+
writeGateRecord: async () => ({ ok: false, reason: "db-error" }),
|
|
762
|
+
getGateRecord: async () => null,
|
|
763
|
+
listGateRecordsForFlow: async () => ({}),
|
|
764
|
+
getLatestExecutionForNode: async () => null,
|
|
765
|
+
getLatestInvocationForNode: async () => null,
|
|
766
|
+
getLatestInvocationInvokedAt: async () => null,
|
|
767
|
+
getExecution: async () => null, getLatestExecutionOutcomeForItem: async () => null,
|
|
768
|
+
summarizeChildrenByFlowIds: async () => new Map(),
|
|
769
|
+
listInvocationsForFlow: async () => [], listExecutionsForInvocation: async () => [],
|
|
770
|
+
hasInvocations: async () => new Set(),
|
|
771
|
+
listVisitedNodeIdsByFlow: async () => new Map(),
|
|
772
|
+
close: noop,
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
}
|