@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,314 +0,0 @@
1
- // resolver — given a session, return its parent + root + breadcrumbs.
2
- //
3
- // Three resolution layers tried in order:
4
- // 1. lineage table (fastest; populated by hooks)
5
- // 2. session grammar (parses scope kind out of the session string)
6
- // 3. external joins (flow_runs sqlite, cron jobs.json)
7
- //
8
- // `resolve(key)` returns a fully-populated row ready for `store.upsertLineage`,
9
- // even for sessions never seen before. `walk(key)` returns the full chain
10
- // from leaf to root.
11
-
12
- import path from "node:path";
13
- import { getSharedDb } from "../state/db.js";
14
- import os from "node:os";
15
- import fs from "node:fs";
16
- import { classify, agentIdOf } from "./grammar.js";
17
- import { execAll } from "./http-helpers.js";
18
-
19
- // runtime.sqlite, not flows/registry.sqlite — manifold no longer writes
20
- // flow_runs rows to the latter; runtime.sqlite is the live store (same
21
- // table shape) everywhere else in manifold.
22
- const FLOWS_DB_PATH = path.join(os.homedir(), ".openclaw", "runtime.sqlite");
23
- const CRON_JSON_PATH = path.join(os.homedir(), ".openclaw", "cron", "jobs.json");
24
-
25
- // ─── External joins (lazy-loaded, cached per resolver instance) ──────────────
26
-
27
- export function makeResolver({ store, flowsDbPath = FLOWS_DB_PATH, cronJsonPath = CRON_JSON_PATH } = {}) {
28
- let flowsByPrefix = null; // flow8 → [{ flow_id, owner_key, pipeline_id, status, created_at }]
29
- let cronJobs = null; // cronId → { name, sessionKey, agentId, ... }
30
- let lastFlowsLoadTs = 0;
31
- let lastCronLoadTs = 0;
32
- const FLOWS_TTL_MS = 30_000; // poll fresh data every 30s; flow_runs grows quickly
33
- const CRON_TTL_MS = 60_000;
34
-
35
- async function loadFlows() {
36
- if (flowsByPrefix && Date.now() - lastFlowsLoadTs < FLOWS_TTL_MS) return;
37
- if (!fs.existsSync(flowsDbPath)) { flowsByPrefix = new Map(); return; }
38
- const { client } = await getSharedDb(flowsDbPath, { readonly: true });
39
- // flow_runs carries pipeline_id as a direct column (the SELECT below
40
- // depends on that shape); there is no controller_id column. Failures
41
- // here are swallowed by the try/catch in every hook that calls
42
- // resolver.walk/resolve, so a schema surprise shows up as missing
43
- // lineage rather than a throw.
44
- const rows = await execAll(client,
45
- `SELECT flow_id, owner_key, pipeline_id, status, created_at FROM flow_runs`
46
- );
47
- const map = new Map();
48
- for (const r of rows) {
49
- const k = String(r.flow_id).slice(0, 8);
50
- if (!map.has(k)) map.set(k, []);
51
- map.get(k).push(r);
52
- }
53
- flowsByPrefix = map;
54
- lastFlowsLoadTs = Date.now();
55
- }
56
-
57
- function loadCron() {
58
- if (cronJobs && Date.now() - lastCronLoadTs < CRON_TTL_MS) return;
59
- cronJobs = new Map();
60
- if (!fs.existsSync(cronJsonPath)) return;
61
- try {
62
- const doc = JSON.parse(fs.readFileSync(cronJsonPath, "utf8"));
63
- for (const j of (doc?.jobs || [])) if (j?.id) cronJobs.set(j.id, j);
64
- lastCronLoadTs = Date.now();
65
- } catch { /* corrupted/missing — treat as empty */ }
66
- }
67
-
68
- /** Look up flow record by 8-char prefix. May return null if pruned. */
69
- async function flowFor(flow8) {
70
- await loadFlows();
71
- const bucket = flowsByPrefix.get(flow8) || [];
72
- // If multiple matches (very rare hash collision), prefer most-recent
73
- return bucket.length ? bucket.reduce((a, b) => (a.created_at >= b.created_at ? a : b)) : null;
74
- }
75
-
76
- /** Look up cron job by id. May return null if cron was deleted. */
77
- function cronFor(cronId) { loadCron(); return cronJobs.get(cronId) || null; }
78
-
79
- // ─── Single-step resolution: given a key, who is its parent? ───────────────
80
-
81
- async function classifyWithExternal(session, opts = {}) {
82
- // `flow:<uuid>` is a synthetic key minted by THIS resolver (below) as the
83
- // parent node for all flow subagents. The grammar only knows `agent:*`
84
- // keys so classify() returns "unknown" for it. Short-circuit here: read
85
- // the trigger-edge row from session_lineage (written by recordTrigger).
86
- // If the row is there, return its root directly and stop walking. If not
87
- // yet written, emit a "flow-triggered" placeholder so the walk stops
88
- // gracefully instead of returning "unknown".
89
- if (typeof session === "string" && session.startsWith("flow:") && store) {
90
- const flowId = session.slice("flow:".length);
91
- const edge = await store.getLineage(session);
92
- if (edge) {
93
- return {
94
- kind: edge.kind ?? "flow-triggered",
95
- agentId: null,
96
- flowId,
97
- pipelineId: edge.pipeline_id ?? opts.pipelineId ?? null,
98
- nodeId: null,
99
- itemKey: null,
100
- parentKey: null, // treat the trigger-edge as root; stop walking
101
- rootKey: edge.root_session_key ?? null,
102
- rootKind: edge.root_kind ?? null,
103
- rootLabel: edge.root_label ?? null,
104
- };
105
- }
106
- // Trigger edge not yet written (race between flow start and recordTrigger).
107
- // Use the pipeline name from flow_runs as a best-effort label.
108
- const fr = await flowFor(flowId.slice(0, 8));
109
- return {
110
- kind: "flow-triggered",
111
- agentId: null,
112
- flowId,
113
- pipelineId: fr?.pipeline_id ?? opts.pipelineId ?? null,
114
- nodeId: null,
115
- itemKey: null,
116
- parentKey: null,
117
- rootKey: null,
118
- rootKind: null,
119
- rootLabel: fr?.pipeline_id ?? null,
120
- };
121
- }
122
-
123
- const g = classify(session);
124
- // Forward every scope-specific field from the grammar (cronName,
125
- // telegramChatId, explicitPurpose, memorySynthRun, etc.) so downstream
126
- // rootLabelOf can produce a human label without re-parsing.
127
- const out = {
128
- ...g,
129
- pipelineId: opts.pipelineId ?? null,
130
- flowId: opts.flowId ?? null,
131
- nodeId: g.nodeId ?? null,
132
- itemKey: g.itemKey ?? null,
133
- parentKey: null,
134
- };
135
- delete out.parentHint;
136
-
137
- if (!g.parentHint) return out; // already-root scopes
138
-
139
- if (g.parentHint.kind === "flow") {
140
- const fr = await flowFor(g.parentHint.flowShort);
141
- if (fr) {
142
- // Link the child to the flow's TRIGGER EDGE (synthetic `flow:<id>`
143
- // row in session_lineage), not the flow's owner_key. The trigger
144
- // edge carries the genuine root (cron-tick / operator-trigger /
145
- // etc.); owner_key is always graph-flow-owner and would mis-
146
- // attribute every discuss session and gate-spawned subagent to
147
- // the "Operator runs" instance.
148
- out.parentKey = `flow:${fr.flow_id}`;
149
- out.flowId = fr.flow_id;
150
- out.pipelineId = fr.pipeline_id;
151
- } else if (store) {
152
- // Flow pruned from flow_runs — probe session_lineage for the trigger
153
- // edge directly. The edge key is 'flow:<full-uuid>' but we only have
154
- // the 8-char prefix, so use a LIKE prefix match.
155
- const edge = await execAll(store._client,
156
- `SELECT child_session_key FROM session_lineage
157
- WHERE child_session_key LIKE ?
158
- AND source IN ('trigger_hook', 'trigger_backfill')
159
- LIMIT 1`,
160
- [`flow:${g.parentHint.flowShort}%`]
161
- ).then((rows) => rows[0] ?? null);
162
- if (edge) {
163
- const fullFlowId = edge.child_session_key.slice(5); // strip 'flow:'
164
- out.parentKey = edge.child_session_key;
165
- out.flowId = fullFlowId;
166
- }
167
- }
168
- return out;
169
- }
170
- if (g.parentHint.kind === "cron-id") {
171
- const job = cronFor(g.parentHint.cronId);
172
- out.parentKey = job?.sessionKey || null;
173
- return out;
174
- }
175
- if (g.parentHint.kind === "memory-synth") {
176
- // memory-synth chunks belong to a synth-run; treat the synth-run as root.
177
- // There's no on-disk session for the run itself; the chunk IS the leaf.
178
- return out;
179
- }
180
- return out;
181
- }
182
-
183
- /**
184
- * Walk up from `session` to the root. Returns an array of frames
185
- * (leaf-to-root). Stops on cycle or depth limit. Uses the lineage table
186
- * as a short-circuit cache.
187
- */
188
- async function walk(session, { maxDepth = 16, useCache = true } = {}) {
189
- const chain = [];
190
- const seen = new Set();
191
- let cur = session;
192
- while (cur && chain.length < maxDepth) {
193
- if (seen.has(cur)) { chain.push({ key: cur, kind: "cycle" }); break; }
194
- seen.add(cur);
195
-
196
- // Try cache first
197
- if (useCache && store) {
198
- const cached = await store.getLineage(cur);
199
- if (cached) {
200
- // Stale-miss detection: if root_kind was cached as "unknown" and the
201
- // root_session_key is a synthetic flow:* key, the trigger edge hadn't
202
- // landed yet when this row was first written. Don't propagate the stale
203
- // root — leave rootKey/rootKind/rootLabel undefined so the annotation
204
- // re-derives them from the live walk that follows.
205
- const staleFlowRoot = cached.root_kind === "unknown" &&
206
- typeof cached.root_session_key === "string" &&
207
- cached.root_session_key.startsWith("flow:");
208
- chain.push({
209
- key: cur,
210
- kind: cached.kind,
211
- agentId: cached.agent_id,
212
- pipelineId: cached.pipeline_id,
213
- flowId: cached.flow_id,
214
- nodeId: cached.node_id,
215
- itemKey: cached.item_key,
216
- rootKey: staleFlowRoot ? undefined : cached.root_session_key,
217
- rootKind: staleFlowRoot ? undefined : cached.root_kind,
218
- rootLabel: staleFlowRoot ? undefined : cached.root_label,
219
- });
220
- if (!cached.parent_session_key || cached.parent_session_key === cur) break;
221
- cur = cached.parent_session_key;
222
- continue;
223
- }
224
- }
225
-
226
- // Live resolve — push the whole classifier output so rootLabelOf has
227
- // access to scope-specific fields (cronName, telegramChatId, etc.).
228
- const c = await classifyWithExternal(cur);
229
- chain.push({ ...c, key: cur });
230
- if (!c.parentKey || c.parentKey === cur) break;
231
- cur = c.parentKey;
232
- }
233
- // Annotate root onto every frame
234
- const last = chain[chain.length - 1];
235
- if (last) {
236
- const rootKey = last.rootKey ?? last.key;
237
- const rootKind = last.rootKind ?? last.kind;
238
- const rootLabel = last.rootLabel ?? rootLabelOf(last);
239
- for (const f of chain) {
240
- f.rootKey = f.rootKey ?? rootKey;
241
- f.rootKind = f.rootKind ?? rootKind;
242
- f.rootLabel = f.rootLabel ?? rootLabel;
243
- }
244
- }
245
- return chain;
246
- }
247
-
248
- /**
249
- * Compute the storeable row for a single session (full lineage stamp).
250
- * Used by the hooks.
251
- */
252
- async function resolve(session, opts = {}) {
253
- const chain = await walk(session, opts);
254
- if (!chain.length) return null;
255
- const leaf = chain[0], root = chain[chain.length - 1];
256
- // Inherit pipeline/flow/node/item from the nearest ancestor that
257
- // carries them. The leaf's own key may be a bare-subagent UUID
258
- // (proposal-sender, content-writer side-spawns) or any session
259
- // whose shape doesn't encode flow context — but its parent
260
- // (a discuss-gate session, a flow-node subagent, the flow trigger
261
- // edge itself) almost always does. Without this propagation the
262
- // cost row written for each LLM call stays NULL on flow_id and
263
- // node_id, and the cost tree buckets it under "Direct (no
264
- // pipeline)" instead of under its real flow + node + item.
265
- let pipelineId = leaf.pipelineId, flowId = leaf.flowId;
266
- let nodeId = leaf.nodeId, itemKey = leaf.itemKey;
267
- for (const frame of chain) {
268
- pipelineId = pipelineId || frame.pipelineId;
269
- flowId = flowId || frame.flowId;
270
- nodeId = nodeId || frame.nodeId;
271
- itemKey = itemKey || frame.itemKey;
272
- if (pipelineId && flowId && nodeId && itemKey) break;
273
- }
274
- return {
275
- child_session_key: leaf.key,
276
- parent_session_key: chain[1]?.key ?? null,
277
- root_session_key: root.key,
278
- kind: leaf.kind,
279
- root_kind: root.kind,
280
- root_label: root.rootLabel ?? rootLabelOf(root),
281
- agent_id: leaf.agentId,
282
- pipeline_id: pipelineId,
283
- flow_id: flowId,
284
- node_id: nodeId,
285
- item_key: itemKey,
286
- first_seen_at: opts.firstSeenAt ?? Date.now(),
287
- source: opts.source ?? "lazy_resolve",
288
- };
289
- }
290
-
291
- return { resolve, walk, classifyWithExternal, flowFor, cronFor };
292
- }
293
-
294
- /** Human-readable label for a root session (cron name, telegram chat id, etc.). */
295
- export function rootLabelOf(frame) {
296
- if (!frame) return null;
297
- switch (frame.kind) {
298
- case "cron-owner": return frame.cronName || frame.key;
299
- case "telegram-dm": return frame.telegramChatId || frame.key;
300
- case "explicit": return frame.explicitPurpose || frame.key;
301
- // The operator-trigger session key (agent:operator:(cli|operator):<flowId-uuid>)
302
- // carries no pipelineId — routes/operator.js's recordTrigger call stamps
303
- // the real pipelineId onto the flow/node cost rows directly (see
304
- // labels.js's displayRootLabel case "operator-trigger"), so this fallback
305
- // only fires for cost attributed to the bare operator session itself.
306
- // Distinguish by flow at least, rather than collapsing every run together.
307
- case "operator-trigger": return frame.triggerFlowId ? `Operator run ${frame.triggerFlowId.slice(0, 8)}` : "Operator runs";
308
- case "agent-default": return `agent:${frame.agentId}`;
309
- case "memory-synth-chunk": return `memory-synth:${frame.memorySynthRun || ""}`;
310
- case "flow-prime": return `flow-prime:${frame.primeRef || ""}`;
311
- case "bare-subagent": return `bare-subagent:${frame.bareUuid?.slice(0, 8) || ""}`;
312
- default: return frame.key;
313
- }
314
- }
@@ -1,147 +0,0 @@
1
- // SQLite store for lineage + cost_by_call. WAL mode so the hook write path
2
- // doesn't block on dashboard reads. Schema migrations are append-only — each
3
- // MIGRATIONS entry is one applied step; never edit a past entry in place.
4
-
5
- import path from "node:path";
6
- import { getSharedDb, withSharedWrite, resolveStorePaths } from "../state/db.js";
7
- import fs from "node:fs";
8
- import { migrateDatabase, RUNTIME_MIGRATIONS_FOLDER } from "../state/migrate.js";
9
- import { enqueueRaw, flush as flushBatchWriter } from "../state/batch-writer.js";
10
- import { ensureSessionKeys, ENSURE_SESSION_SQL } from "../state/session-keys.js";
11
- import { ensureFlowItems, ensureTurnRuns, ensurePipelineNodes } from "../state/attribution-registries.js";
12
-
13
- // Lineage/cost now live in runtime.sqlite so flow_id and pipeline_id can
14
- // carry FKs; resolve per call so MANIFOLD_DATA_DIR set after import wins.
15
- export function defaultDbPath() {
16
- return resolveStorePaths().runtimeDb;
17
- }
18
-
19
- const UPSERT_LINEAGE_SQL = `
20
- INSERT INTO session_lineage
21
- (child_session_key, parent_session_key, root_session_key, kind, root_kind, root_label,
22
- agent_id, pipeline_id, flow_id, run_id, node_id, item_key, first_seen_at, source)
23
- VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?)
24
- ON CONFLICT(child_session_key) DO UPDATE SET
25
- parent_session_key = COALESCE(excluded.parent_session_key, session_lineage.parent_session_key),
26
- root_session_key = excluded.root_session_key,
27
- kind = excluded.kind,
28
- root_kind = excluded.root_kind,
29
- root_label = excluded.root_label,
30
- agent_id = COALESCE(excluded.agent_id, session_lineage.agent_id),
31
- pipeline_id = COALESCE(excluded.pipeline_id, session_lineage.pipeline_id),
32
- flow_id = COALESCE(excluded.flow_id, session_lineage.flow_id),
33
- run_id = COALESCE(excluded.run_id, session_lineage.run_id),
34
- node_id = COALESCE(excluded.node_id, session_lineage.node_id),
35
- item_key = COALESCE(excluded.item_key, session_lineage.item_key),
36
- source = excluded.source
37
- WHERE session_lineage.source = 'backfill' OR excluded.source != 'backfill'
38
- `;
39
-
40
- const GET_LINEAGE_SQL = `SELECT * FROM session_lineage WHERE child_session_key = ?`;
41
-
42
- const INSERT_COST_SQL = `
43
- INSERT OR REPLACE INTO cost_by_call
44
- (session_id, msg_id, ts, provider, model,
45
- input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, total_tokens,
46
- cost_input, cost_output, cost_cache_read, cost_cache_write, cost_total,
47
- session_key, agent_id,
48
- root_session_key, root_kind, root_label,
49
- pipeline_id, flow_id, node_id, item_key)
50
- VALUES (?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?, ?,?,?, ?,?,?,?)
51
- `;
52
-
53
- export async function openStore({ dbPath = defaultDbPath() } = {}) {
54
- fs.mkdirSync(path.dirname(dbPath), { recursive: true });
55
- await migrateDatabase({ dbPath, migrationsFolder: RUNTIME_MIGRATIONS_FOLDER });
56
- const { client } = await getSharedDb(dbPath);
57
- return makeStore(client, dbPath);
58
- }
59
-
60
- function makeStore(client, dbPath) {
61
- return {
62
- _client: client,
63
- async upsertLineage(row) {
64
- try {
65
- await withSharedWrite(dbPath, async () => {
66
- await ensureSessionKeys(client, [row.child_session_key, row.parent_session_key, row.root_session_key]);
67
- await ensureFlowItems(client, [[row.flow_id, row.item_key]]);
68
- await ensureTurnRuns(client, [row.run_id]);
69
- await ensurePipelineNodes(client, [[row.pipeline_id, row.node_id]]);
70
- return client.execute({
71
- sql: UPSERT_LINEAGE_SQL,
72
- args: [
73
- row.child_session_key,
74
- row.parent_session_key ?? null,
75
- row.root_session_key,
76
- row.kind,
77
- row.root_kind,
78
- row.root_label ?? null,
79
- row.agent_id ?? null,
80
- row.pipeline_id ?? null,
81
- row.flow_id ?? null,
82
- row.run_id ?? null,
83
- row.node_id ?? null,
84
- row.item_key ?? null,
85
- row.first_seen_at ?? Date.now(),
86
- row.source,
87
- ],
88
- });
89
- });
90
- } catch { /* best-effort */ }
91
- },
92
- async getLineage(childKey) {
93
- try {
94
- const { rows } = await client.execute({ sql: GET_LINEAGE_SQL, args: [childKey] });
95
- return rows[0] ?? null;
96
- } catch { return null; }
97
- },
98
- // Deferred via batch-writer: one call per LLM call with cost data
99
- // (telemetry/index.js's before_message_write hook), likely the
100
- // highest-volume write in runtime.sqlite. Already best-effort (errors
101
- // swallowed, no caller inspects a return value or re-reads the row in
102
- // the same hook invocation — unlike upsertLineage() above, which stays
103
- // immediate because a closely-timed child session's getLineage() can
104
- // depend on a parent's just-written row).
105
- insertCost(row) {
106
- const now = Date.now();
107
- for (const k of [row.session_key, row.root_session_key]) {
108
- if (typeof k === "string" && k.length > 0) enqueueRaw(dbPath, ENSURE_SESSION_SQL, [k, now]);
109
- }
110
- if (row.flow_id && row.item_key) enqueueRaw(dbPath, "INSERT OR IGNORE INTO flow_items (flow_id, item_key, first_seen_at) VALUES (?, ?, ?)", [row.flow_id, row.item_key, now]);
111
- if (row.pipeline_id && row.node_id) enqueueRaw(dbPath, "INSERT OR IGNORE INTO pipeline_nodes (pipeline_id, node_name, first_seen_at) VALUES (?, ?, ?)", [row.pipeline_id, row.node_id, now]);
112
- enqueueRaw(dbPath, INSERT_COST_SQL, [
113
- row.session_id,
114
- row.msg_id,
115
- row.ts,
116
- row.provider ?? null,
117
- row.model ?? null,
118
- row.input_tokens ?? null,
119
- row.output_tokens ?? null,
120
- row.cache_read_tokens ?? null,
121
- row.cache_write_tokens ?? null,
122
- row.total_tokens ?? null,
123
- row.cost_input ?? null,
124
- row.cost_output ?? null,
125
- row.cost_cache_read ?? null,
126
- row.cost_cache_write ?? null,
127
- row.cost_total,
128
- row.session_key ?? null,
129
- row.agent_id ?? null,
130
- row.root_session_key ?? null,
131
- row.root_kind ?? null,
132
- row.root_label ?? null,
133
- row.pipeline_id ?? null,
134
- row.flow_id ?? null,
135
- row.node_id ?? null,
136
- row.item_key ?? null,
137
- ]);
138
- },
139
- // insertCost() defers through batch-writer — for tests that assert
140
- // read-after-write. Not meant for production use.
141
- async flush() {
142
- await flushBatchWriter(dbPath);
143
- },
144
- close() { try { client.close(); } catch { /* ignore */ } },
145
- };
146
- }
147
-
@@ -1,43 +0,0 @@
1
- // run-patch.js — pure RunSummary-shaped patch builders for the flow-state
2
- // WS events (canonical run.* payloads, enveloped by events/emit.js).
3
- //
4
- // Each function takes already-fetched data (no async DB calls inside) so the
5
- // caller can batch DB work and reuse results across multiple items in one
6
- // event tick.
7
- export function prettifyPipelineId(pipelineId) {
8
- if (typeof pipelineId !== "string" || !pipelineId) return "Pipeline";
9
- return pipelineId.replace(/^([a-z]+)_(.+)$/, "$1: $2").replace(/-/g, " ");
10
- }
11
-
12
- export function pipelineMetaFor(pipelineId, { pipelineLookup } = {}) {
13
- const declared = (pipelineLookup && pipelineLookup(pipelineId)) || {};
14
- return {
15
- id: pipelineId,
16
- displayName: declared.displayName ?? prettifyPipelineId(pipelineId),
17
- emoji: declared.emoji ?? "📊",
18
- sfSymbol: declared.sfSymbol ?? "tray.fill",
19
- };
20
- }
21
-
22
- // Builds a RunSummary-shaped patch from a flow row (flowId, stateJson,
23
- // waitJson, lastStep, status, createdAt/updatedAt/endedAt).
24
- export function buildRunPatch(flow, now, { pipelineLookup } = {}) {
25
- const state = flow.stateJson ?? {};
26
- const display = state._display ?? {};
27
- const pipelineId = state.pipelineId ?? null;
28
- const pipelineMeta = pipelineId ? pipelineMetaFor(pipelineId, { pipelineLookup }) : null;
29
- return {
30
- flowId: flow.flowId,
31
- pipelineId,
32
- status: flow.status,
33
- updatedAtMs: flow.updatedAt ?? now,
34
- endedAtMs: flow.endedAt ?? null,
35
- createdAtMs: flow.createdAt ?? null,
36
- statusReason: flow.waitJson?.summary ?? null,
37
- displayTitle: display.title ?? null,
38
- displayMetrics: Array.isArray(display.metrics) ? display.metrics : null,
39
- currentNodeId: flow.lastStep ?? null,
40
- pipelineDisplayName: pipelineMeta?.displayName ?? null,
41
- pipelineEmoji: pipelineMeta?.emoji ?? null,
42
- };
43
- }