@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.
Files changed (210) hide show
  1. package/.migrations/ops/0000_futuristic_karma.sql +111 -0
  2. package/.migrations/ops/0001_low_maximus.sql +7 -0
  3. package/.migrations/ops/meta/0000_snapshot.json +763 -0
  4. package/.migrations/ops/meta/0001_snapshot.json +173 -0
  5. package/.migrations/ops/meta/_journal.json +20 -0
  6. package/.migrations/runtime/0000_real_boomer.sql +180 -0
  7. package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
  8. package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
  9. package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
  10. package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
  11. package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
  12. package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
  13. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
  14. package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
  15. package/.migrations/runtime/0009_lying_shocker.sql +96 -0
  16. package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
  17. package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
  18. package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
  19. package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
  20. package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
  21. package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
  22. package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
  23. package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
  24. package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
  25. package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
  26. package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
  27. package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
  28. package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
  29. package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
  30. package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
  31. package/.migrations/runtime/meta/_journal.json +97 -0
  32. package/README.md +1583 -2
  33. package/RELEASING.md +89 -0
  34. package/agent/fleet.js +100 -0
  35. package/bin/cli.js +854 -0
  36. package/bin/manifold.js +4 -0
  37. package/boot/flow-revival.js +536 -0
  38. package/budget/index.js +495 -0
  39. package/budget/lib.js +59 -0
  40. package/dashboard/cost.js +528 -0
  41. package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
  42. package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
  43. package/dashboard/dist/index.html +13 -0
  44. package/dashboard/event-log.js +267 -0
  45. package/dashboard/index.html +12 -0
  46. package/dashboard/package-lock.json +2877 -0
  47. package/dashboard/package.json +37 -0
  48. package/dashboard/pipeline-graph.js +131 -0
  49. package/dashboard/postcss.config.js +6 -0
  50. package/dashboard/search.js +183 -0
  51. package/dashboard/server.js +1015 -0
  52. package/dashboard/src/App.tsx +42 -0
  53. package/dashboard/src/api/client.ts +118 -0
  54. package/dashboard/src/api/types.ts +510 -0
  55. package/dashboard/src/api/ws.tsx +112 -0
  56. package/dashboard/src/components/CancelButton.tsx +63 -0
  57. package/dashboard/src/components/ClickToCopy.tsx +68 -0
  58. package/dashboard/src/components/CopyButton.tsx +61 -0
  59. package/dashboard/src/components/DailyCostTrend.tsx +85 -0
  60. package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
  61. package/dashboard/src/components/Funnel.tsx +149 -0
  62. package/dashboard/src/components/GateForm.tsx +158 -0
  63. package/dashboard/src/components/GraphView.tsx +302 -0
  64. package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
  65. package/dashboard/src/components/Histogram.tsx +292 -0
  66. package/dashboard/src/components/Layout.tsx +28 -0
  67. package/dashboard/src/components/NeedsAttention.tsx +98 -0
  68. package/dashboard/src/components/NodeContextCard.tsx +132 -0
  69. package/dashboard/src/components/PathTrail.tsx +35 -0
  70. package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
  71. package/dashboard/src/components/RerunButton.tsx +46 -0
  72. package/dashboard/src/components/RunComparison.tsx +53 -0
  73. package/dashboard/src/components/Sparkline.tsx +94 -0
  74. package/dashboard/src/components/StatStrip.tsx +147 -0
  75. package/dashboard/src/components/StateTree.tsx +84 -0
  76. package/dashboard/src/components/StatusBadge.tsx +18 -0
  77. package/dashboard/src/components/SubflowPathPills.tsx +38 -0
  78. package/dashboard/src/components/SubflowSummary.tsx +24 -0
  79. package/dashboard/src/components/SubflowsCard.tsx +327 -0
  80. package/dashboard/src/components/TodayStrip.tsx +86 -0
  81. package/dashboard/src/components/TranscriptPane.tsx +99 -0
  82. package/dashboard/src/components/TranscriptRow.tsx +69 -0
  83. package/dashboard/src/components/ui/badge.tsx +38 -0
  84. package/dashboard/src/components/ui/button.tsx +28 -0
  85. package/dashboard/src/components/ui/card.tsx +42 -0
  86. package/dashboard/src/components/ui/skeleton.tsx +9 -0
  87. package/dashboard/src/components/ui/table.tsx +62 -0
  88. package/dashboard/src/index.css +26 -0
  89. package/dashboard/src/lib/anomaly.ts +42 -0
  90. package/dashboard/src/lib/format.ts +30 -0
  91. package/dashboard/src/lib/step-history.ts +13 -0
  92. package/dashboard/src/lib/subflow.ts +154 -0
  93. package/dashboard/src/lib/use-click-outside.ts +28 -0
  94. package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
  95. package/dashboard/src/lib/utils.ts +6 -0
  96. package/dashboard/src/main.tsx +10 -0
  97. package/dashboard/src/types/shims.d.ts +4 -0
  98. package/dashboard/src/views/Home.tsx +478 -0
  99. package/dashboard/src/views/PipelineDetail.tsx +1185 -0
  100. package/dashboard/src/views/PipelinesList.tsx +111 -0
  101. package/dashboard/src/views/RunDetail.tsx +248 -0
  102. package/dashboard/src/views/Search.tsx +196 -0
  103. package/dashboard/stats.js +668 -0
  104. package/dashboard/tailwind.config.ts +43 -0
  105. package/dashboard/tree-stats.js +12 -0
  106. package/dashboard/tsconfig.json +25 -0
  107. package/dashboard/views.js +231 -0
  108. package/dashboard/vite.config.ts +34 -0
  109. package/drizzle.config.js +12 -0
  110. package/drizzle.config.ops.js +12 -0
  111. package/elevation.js +116 -0
  112. package/engine/admission-promotion-resume.js +127 -0
  113. package/engine/agent-dispatch.js +64 -0
  114. package/engine/child-resume-trigger.js +315 -0
  115. package/engine/executor-budget.js +87 -0
  116. package/engine/executor-cancel.js +83 -0
  117. package/engine/executor-errors.js +122 -0
  118. package/engine/executor-helpers.js +254 -0
  119. package/engine/executor-nodes.js +292 -0
  120. package/engine/executor-persist.js +212 -0
  121. package/engine/executor-subflow-children.js +275 -0
  122. package/engine/executor.js +1013 -0
  123. package/engine/global-concurrency.js +33 -0
  124. package/engine/loader.js +528 -0
  125. package/engine/needs-resolver.js +229 -0
  126. package/engine/pipelines-watcher.js +133 -0
  127. package/engine/result-replay.js +34 -0
  128. package/engine/revival-messages.js +11 -0
  129. package/engine/spawn-child.js +55 -0
  130. package/engine/wait-for-flow-settlement.js +109 -0
  131. package/events/catalog.js +26 -0
  132. package/events/emit.js +44 -0
  133. package/events/kinds.js +118 -0
  134. package/events/serve-ws.js +74 -0
  135. package/events/stream.js +35 -0
  136. package/events/ws-dialog.js +257 -0
  137. package/events/ws-frame.js +194 -0
  138. package/events/ws-run-events.js +70 -0
  139. package/events.js +32 -0
  140. package/gates/gate-resolver.js +576 -0
  141. package/gates/paused-flows.js +55 -0
  142. package/graphql/columns.js +67 -0
  143. package/graphql/schema.js +333 -0
  144. package/index.js +49 -0
  145. package/mcp/http.js +159 -0
  146. package/mcp/identity.js +94 -0
  147. package/mcp/stdio.js +80 -0
  148. package/mcp/tools.js +245 -0
  149. package/mcp/wire-ops.js +178 -0
  150. package/package.json +49 -4
  151. package/routes/bearer-auth.js +57 -0
  152. package/routes/discuss-audio.js +87 -0
  153. package/routes/discuss-component.js +39 -0
  154. package/routes/discuss-draft.js +159 -0
  155. package/routes/discuss-helpers.js +20 -0
  156. package/routes/discuss.js +848 -0
  157. package/routes/failures.js +104 -0
  158. package/routes/graphql.js +61 -0
  159. package/routes/health.js +54 -0
  160. package/routes/operator-discuss.js +193 -0
  161. package/routes/operator-resume.js +32 -0
  162. package/routes/operator-retry.js +301 -0
  163. package/routes/operator.js +539 -0
  164. package/routes/pipelines.js +118 -0
  165. package/routes/runs.js +1129 -0
  166. package/scripts/ci/import-check.mjs +50 -0
  167. package/scripts/link-core-self.mjs +31 -0
  168. package/scripts/with-guard.sh +24 -0
  169. package/server/app.js +503 -0
  170. package/server/registry.js +53 -0
  171. package/server/serve.js +163 -0
  172. package/state/admission-queue-store.js +325 -0
  173. package/state/attribution-registries.js +102 -0
  174. package/state/batch-writer.js +162 -0
  175. package/state/db-guard.js +63 -0
  176. package/state/db-health.js +27 -0
  177. package/state/db.js +122 -0
  178. package/state/discuss-store.js +299 -0
  179. package/state/failure-store.js +427 -0
  180. package/state/migrate.js +104 -0
  181. package/state/node-cost.js +67 -0
  182. package/state/node-executions.js +775 -0
  183. package/state/node-health-store.js +124 -0
  184. package/state/ops-merge.js +93 -0
  185. package/state/paths.js +146 -0
  186. package/state/pipeline-dirs.js +99 -0
  187. package/state/pipeline-registry.js +449 -0
  188. package/state/run-store.js +697 -0
  189. package/state/schema.js +438 -0
  190. package/state/schema.ops.js +44 -0
  191. package/state/session-index.js +177 -0
  192. package/state/session-keys.js +39 -0
  193. package/state/state-store.js +311 -0
  194. package/state/subflow-state.js +126 -0
  195. package/state/tool-handoff.js +169 -0
  196. package/telemetry/budget-alert.js +353 -0
  197. package/telemetry/cli.js +331 -0
  198. package/telemetry/grammar.js +119 -0
  199. package/telemetry/http-budget.js +132 -0
  200. package/telemetry/http-cost.js +670 -0
  201. package/telemetry/http-helpers.js +134 -0
  202. package/telemetry/http-tree.js +349 -0
  203. package/telemetry/http.js +44 -0
  204. package/telemetry/index.js +307 -0
  205. package/telemetry/ingest.js +134 -0
  206. package/telemetry/labels.js +115 -0
  207. package/telemetry/pricer.js +77 -0
  208. package/telemetry/resolver.js +314 -0
  209. package/telemetry/store.js +147 -0
  210. package/utils/run-patch.js +43 -0
@@ -0,0 +1,314 @@
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
+ }
@@ -0,0 +1,147 @@
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
+
@@ -0,0 +1,43 @@
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
+ }