@igorjpimenta/manifold 0.0.0-bootstrap.1

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 +1584 -0
  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 +51 -0
  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,307 @@
1
+ // telemetry/index.js — lineage + cost attribution wired into manifold.
2
+ // Absorbed from extensions/lineage/; exposes registerTelemetry(api) instead of
3
+ // definePluginEntry so manifold's own plugin entry calls it inline.
4
+ //
5
+ // Responsibilities (unchanged from lineage):
6
+ // - session_lineage + cost_by_call writes via before_agent_start /
7
+ // subagent_spawned / session_start / before_message_write hooks
8
+ // - /manifold/telemetry/* HTTP read-only endpoints (cost, tree, budget cap)
9
+ // - api.runtime.lineage.recordTrigger / recordSpawn cross-plugin API
10
+ // - `openclaw lineage` CLI subcommands
11
+
12
+ import { openStore, defaultDbPath } from "./store.js";
13
+ import { makeResolver } from "./resolver.js";
14
+ import { registerCliCommands } from "./cli.js";
15
+ import { resolveCost } from "./pricer.js";
16
+ import { checkBudget, initCapStore } from "./budget-alert.js";
17
+ import { registerHttpRoutes } from "./http.js";
18
+ import { applyTelemetryEvent } from "./ingest.js";
19
+ import { authorizeBearerRequest, sendUnauthorized } from "../routes/bearer-auth.js";
20
+ import { createEmitter } from "../events/emit.js";
21
+ // Hoisted to module-level so every call's hook closures share the same
22
+ // store/resolver. The SDK can call register() repeatedly (config reload,
23
+ // hot-reload); only the first service start actually opens the DB.
24
+ let SHARED_STORE = null;
25
+ let SHARED_RESOLVER = null;
26
+
27
+ // Live accessor for manifold modules that need to resolve a session's
28
+ // flow/item lineage without opening their own resolver instance. Returns
29
+ // null until the lineage-service's start() hook has run.
30
+ export function getSharedResolver() {
31
+ return SHARED_RESOLVER;
32
+ }
33
+
34
+ // Calls that arrive before the store is ready (gateway boot revival) are
35
+ // buffered and flushed once the service starts.
36
+ let PENDING_CALLS = [];
37
+
38
+ export function registerTelemetry(api, { getBroadcast, _dbPath } = {}) {
39
+ const dbPath = _dbPath ?? defaultDbPath();
40
+
41
+ const logger = api.logger;
42
+
43
+ // Canonical-kind fan-out for cost.record.added below: canonical wire
44
+ // name, envelope adds kind.
45
+ const { emit } = createEmitter({ getBroadcast, logger });
46
+
47
+ // ── Budget-cap store service ────────────────────────────────────────────────
48
+ // Must load before checkBudget()/the /budget/cap HTTP routes see traffic —
49
+ // otherwise they'd read the DEFAULT_CAP_USD fallback instead of the
50
+ // operator-set value.
51
+
52
+ api.registerService({
53
+ id: "budget-cap-store",
54
+ start: async () => {
55
+ try {
56
+ await initCapStore({ dbPath });
57
+ logger.info(`telemetry: budget-cap store ready`);
58
+ } catch (err) {
59
+ logger.error(`telemetry: budget-cap store init failed: ${err?.message ?? err}`);
60
+ }
61
+ },
62
+ });
63
+
64
+ // ── Store service ──────────────────────────────────────────────────────────
65
+
66
+ api.registerService({
67
+ id: "lineage-service",
68
+ start: async () => {
69
+ if (SHARED_STORE) { logger.info(`telemetry: store already initialized — skipping`); return; }
70
+ try {
71
+ SHARED_STORE = await openStore({ dbPath });
72
+ SHARED_RESOLVER = makeResolver({ store: SHARED_STORE });
73
+ logger.info(`telemetry: store ready at ${dbPath}`);
74
+ if (PENDING_CALLS.length) {
75
+ const pending = PENDING_CALLS.splice(0);
76
+ logger.info(`telemetry: flushing ${pending.length} queued pre-ready call(s)`);
77
+ for (const { type, args } of pending) {
78
+ try {
79
+ if (type === "trigger") await api.runtime.lineage.recordTrigger(args);
80
+ else if (type === "spawn") await api.runtime.lineage.recordSpawn(args);
81
+ } catch (e) {
82
+ logger.warn(`telemetry: pending flush error (${type}): ${e?.message ?? e}`);
83
+ }
84
+ }
85
+ }
86
+ } catch (err) {
87
+ logger.error(`telemetry: service start failed: ${err?.message ?? err}`);
88
+ SHARED_STORE = null; SHARED_RESOLVER = null;
89
+ }
90
+ },
91
+ });
92
+
93
+ // ── Ingest ─────────────────────────────────────────────────────────────────
94
+ // Harnesses observe their own lifecycle; core decides what those
95
+ // observations mean. Adapters post semantic events (lineage.session,
96
+ // lineage.spawn, cost.usage) and ingest.js derives the rows.
97
+ const ingestToken = process.env.MANIFOLD_INGEST_TOKEN || process.env.MANIFOLD_BEARER || "";
98
+
99
+ api.registerHttpRoute({
100
+ path: "/manifold/telemetry/event",
101
+ auth: "plugin",
102
+ match: "exact",
103
+ handler: async (req, res) => {
104
+ if (req.method !== "POST") {
105
+ res.statusCode = 405;
106
+ res.setHeader("Allow", "POST");
107
+ res.end("method not allowed");
108
+ return true;
109
+ }
110
+ const authz = authorizeBearerRequest(req, { logger, secret: ingestToken });
111
+ if (!authz.ok) { sendUnauthorized(res, authz.reason); return true; }
112
+ let size = 0;
113
+ const chunks = [];
114
+ for await (const chunk of req) {
115
+ size += chunk.length;
116
+ if (size > 256 * 1024) { res.statusCode = 413; res.end("body too large"); return true; }
117
+ chunks.push(chunk);
118
+ }
119
+ let event;
120
+ try { event = JSON.parse(Buffer.concat(chunks).toString("utf8") || "{}"); }
121
+ catch { res.statusCode = 400; res.end(JSON.stringify({ ok: false, error: "invalid json body" })); return true; }
122
+ const result = await applyTelemetryEvent(event, {
123
+ getStore: () => SHARED_STORE,
124
+ getResolver: () => SHARED_RESOLVER,
125
+ getBroadcast, logger, emit,
126
+ });
127
+ res.statusCode = result.ok ? 202 : 400;
128
+ res.setHeader("Content-Type", "application/json");
129
+ res.end(JSON.stringify(result));
130
+ return true;
131
+ },
132
+ });
133
+
134
+ // ── HTTP routes ────────────────────────────────────────────────────────────
135
+
136
+ registerHttpRoutes({ api, getStore: () => SHARED_STORE, logger, getBroadcast });
137
+
138
+ // ── Runtime API ────────────────────────────────────────────────────────────
139
+
140
+ api.runtime = api.runtime ?? {};
141
+ api.runtime.lineage = {
142
+ async recordTrigger({ flowId, runId, pipelineId, parentSessionKey, rootKind: rkHint, rootLabel: rlHint }) {
143
+ try {
144
+ const store = SHARED_STORE, resolver = SHARED_RESOLVER;
145
+ if (!store || !resolver) {
146
+ PENDING_CALLS.push({ type: "trigger", args: { flowId, runId, pipelineId, parentSessionKey, rootKind: rkHint, rootLabel: rlHint } });
147
+ return { ok: false, reason: "not_ready", queued: true };
148
+ }
149
+ if (!flowId || !parentSessionKey) return { ok: false, reason: "missing_args" };
150
+
151
+ const WALK_THROUGH = new Set(["agent_tool", "cli"]);
152
+ let rootKey = parentSessionKey, rootKind = rkHint ?? "unknown", rootLabel = rlHint ?? parentSessionKey;
153
+ if (!rkHint || WALK_THROUGH.has(rkHint)) {
154
+ try {
155
+ const chain = await resolver.walk(parentSessionKey);
156
+ if (chain.length) {
157
+ const r = chain[chain.length - 1];
158
+ rootKey = r.key; rootKind = r.kind; rootLabel = r.rootLabel ?? r.key;
159
+ }
160
+ } catch { /* fall through with self-root defaults */ }
161
+ }
162
+
163
+ // An "explicit" root is a hand-typed/ad hoc session purpose string —
164
+ // it has no durable identity worth grouping by, and labels.js collapses
165
+ // it to "Ad-hoc sessions" once it's UUID-suffixed. The pipeline this
166
+ // trigger is dispatching is the far more useful attribution — same
167
+ // convention routes/operator.js already uses for its parent-less
168
+ // synthetic trigger path (rootKind: "explicit", rootLabel: pipelineId).
169
+ //
170
+ // "operator-trigger" hits the identical problem via a different route:
171
+ // routes/operator.js's synthetic `agent:operator:(cli|operator):
172
+ // <flowId>` key is never itself a stored child_session_key (only
173
+ // `flow:<id>` rows get upserted), so a subflow dispatched from an
174
+ // agent session whose chain reaches that key directly — rather than
175
+ // through the seeded flow:<topFlowId> row — gets classified fresh by
176
+ // grammar.js from the key shape alone, landing on resolver.js's
177
+ // generic "Operator run <shortid>" / "Operator runs" fallback (no
178
+ // pipelineId access there). Same fix applies: prefer the pipeline
179
+ // we're actually dispatching over an uninformative fallback label.
180
+ //
181
+ // pipelineId may be a composite child id (engine/loader.js's
182
+ // `<pipeline>::NODE[::NODE...]` shape for a subflow dispatch, e.g.
183
+ // `freelance_freelancer-proposal::PROPOSE::SUBMIT`) — every subflow of
184
+ // one manually-triggered run independently calls recordTrigger with
185
+ // its own composite id, but they must all collapse to the SAME root
186
+ // label so http-tree.js's GROUP BY root_label merges them into one
187
+ // instance instead of fragmenting one run into N rows, one per node.
188
+ if (pipelineId && (rootKind === "explicit" || rootKind === "operator-trigger")) {
189
+ rootLabel = pipelineId.split("::")[0];
190
+ }
191
+
192
+ await store.upsertLineage({
193
+ child_session_key: `flow:${flowId}`,
194
+ parent_session_key: parentSessionKey,
195
+ root_session_key: rootKey,
196
+ kind: "flow-triggered",
197
+ root_kind: rootKind,
198
+ root_label: rootLabel,
199
+ agent_id: null,
200
+ pipeline_id: pipelineId ?? null,
201
+ flow_id: flowId,
202
+ run_id: runId ?? null,
203
+ node_id: null,
204
+ item_key: null,
205
+ first_seen_at: Date.now(),
206
+ source: "trigger_hook",
207
+ });
208
+ return { ok: true };
209
+ } catch (err) {
210
+ logger.warn(`telemetry.recordTrigger: ${err?.message ?? err}`);
211
+ return { ok: false, reason: err?.message ?? String(err) };
212
+ }
213
+ },
214
+
215
+ async recordSpawn({ childSession, flowId, nodeId, itemKey, pipelineId, agentId, kind, source = "spawn_hook" }) {
216
+ try {
217
+ const store = SHARED_STORE, resolver = SHARED_RESOLVER;
218
+ if (!store) {
219
+ PENDING_CALLS.push({ type: "spawn", args: { childSession, flowId, nodeId, itemKey, pipelineId, agentId, kind, source } });
220
+ return { ok: false, reason: "not_ready", queued: true };
221
+ }
222
+ if (!childSession || !flowId) return { ok: false, reason: "missing_args" };
223
+
224
+ // Resolve the real root by walking the flow's own trigger edge
225
+ // (flow:<flowId>) instead of hardcoding "flow-node" — a flow-node
226
+ // subagent's cost should attribute to whatever triggered the flow
227
+ // (cron, operator, etc.), not to a made-up kind of its own. Falls
228
+ // back to `kind ?? "flow-node"` only if the resolver isn't ready or
229
+ // the trigger edge hasn't landed yet (race between flow start and
230
+ // recordTrigger) — the HTTP-layer ATTR_JOIN re-attribution still
231
+ // recovers those.
232
+ const parentKey = `flow:${flowId}`;
233
+ let rootKind = kind ?? "flow-node", rootLabel = null, rootSessionKey = childSession;
234
+ if (resolver) {
235
+ const chain = await resolver.walk(parentKey);
236
+ const rootFrame = chain[chain.length - 1];
237
+ if (rootFrame) {
238
+ rootKind = rootFrame.rootKind ?? rootFrame.kind ?? rootKind;
239
+ rootLabel = rootFrame.rootLabel ?? null;
240
+ rootSessionKey = rootFrame.rootKey ?? rootFrame.key ?? rootSessionKey;
241
+ }
242
+ }
243
+
244
+ await store.upsertLineage({
245
+ child_session_key: childSession,
246
+ parent_session_key: parentKey,
247
+ root_session_key: rootSessionKey,
248
+ kind: kind ?? "flow-node",
249
+ root_kind: rootKind,
250
+ root_label: rootLabel,
251
+ agent_id: agentId ?? null,
252
+ pipeline_id: pipelineId ?? null,
253
+ flow_id: flowId,
254
+ node_id: nodeId ?? null,
255
+ item_key: itemKey ?? null,
256
+ first_seen_at: Date.now(),
257
+ source,
258
+ });
259
+ return { ok: true };
260
+ } catch (err) {
261
+ logger.warn(`telemetry.recordSpawn: ${err?.message ?? err}`);
262
+ return { ok: false, reason: err?.message ?? String(err) };
263
+ }
264
+ },
265
+ };
266
+
267
+ // ── CLI ────────────────────────────────────────────────────────────────────
268
+
269
+ async function ensureStore() {
270
+ if (SHARED_STORE) return SHARED_STORE;
271
+ try {
272
+ SHARED_STORE = await openStore({ dbPath });
273
+ SHARED_RESOLVER = makeResolver({ store: SHARED_STORE });
274
+ } catch (err) {
275
+ logger.error(`telemetry CLI: lazy store init failed: ${err?.message ?? err}`);
276
+ }
277
+ return SHARED_STORE;
278
+ }
279
+ api.registerCli(
280
+ (ctx) => registerCliCommands({
281
+ program: ctx.program,
282
+ logger,
283
+ getStore: () => ensureStore(),
284
+ getResolver: async () => { await ensureStore(); return SHARED_RESOLVER; },
285
+ dbPath,
286
+ }),
287
+ {
288
+ commands: ["lineage"],
289
+ descriptors: [{
290
+ name: "lineage",
291
+ description: "Session lineage + per-call cost attribution (lookup / stats / cost)",
292
+ hasSubcommands: true,
293
+ }],
294
+ },
295
+ );
296
+
297
+ logger.info("telemetry: registered (subagent_spawned + session_start + before_message_write + CLI)");
298
+ }
299
+
300
+ function parseTs(t) {
301
+ if (typeof t === "number") return t;
302
+ if (typeof t === "string") {
303
+ const ms = Date.parse(t);
304
+ return Number.isFinite(ms) ? ms : null;
305
+ }
306
+ return null;
307
+ }
@@ -0,0 +1,134 @@
1
+ // ingest.js — telemetry event generation and reconciliation.
2
+ //
3
+ // Harnesses observe their own lifecycle; core owns what those observations
4
+ // mean. A harness adapter posts semantic events here (lineage.session,
5
+ // lineage.spawn, cost.usage) and this module derives the lineage/cost rows,
6
+ // resolves ancestry and applies the budget check. No harness field names
7
+ // appear below — the adapter's translation layer carries those.
8
+ //
9
+ // Events are best-effort: a malformed event is logged and dropped, never
10
+ // thrown back at the poster.
11
+
12
+ import { resolveCost } from "./pricer.js";
13
+ import { checkBudget } from "./budget-alert.js";
14
+
15
+ const KINDS = new Map([
16
+ ["lineage.session", applyLineageSession],
17
+ ["lineage.spawn", applyLineageSpawn],
18
+ ["cost.usage", applyCostUsage],
19
+ ]);
20
+
21
+ export function ingestKinds() {
22
+ return [...KINDS.keys()];
23
+ }
24
+
25
+ export async function applyTelemetryEvent(event, deps) {
26
+ const apply = KINDS.get(event?.kind);
27
+ if (!apply) return { ok: false, code: "unknown_kind" };
28
+ const { getStore, getResolver, logger = null } = deps;
29
+ try {
30
+ const store = getStore?.();
31
+ const resolver = getResolver?.();
32
+ if (!store || !resolver) return { ok: false, code: "store_not_ready" };
33
+ await apply(event, { ...deps, store, resolver, logger });
34
+ return { ok: true };
35
+ } catch (err) {
36
+ logger?.warn?.(`telemetry[${event?.kind}]: ${err?.message ?? err}`);
37
+ return { ok: false, code: "apply_failed", error: err?.message ?? String(err) };
38
+ }
39
+ }
40
+
41
+ async function applyLineageSession(event, { store, resolver }) {
42
+ const session = event?.session;
43
+ if (!session) return;
44
+ const existing = await store.getLineage(session);
45
+ if (existing && existing.source === "spawn_hook") return;
46
+ const source = event?.source === "session_start" ? "session_start_hook" : "start_hook";
47
+ const row = await resolver.resolve(session, { source });
48
+ if (!row) return;
49
+ row.agent_id = row.agent_id || event?.agentId || null;
50
+ if (event?.parentSession) row.parent_session_key = event.parentSession;
51
+ await store.upsertLineage(row);
52
+ }
53
+
54
+ async function applyLineageSpawn(event, { store, resolver }) {
55
+ const childSession = event?.session;
56
+ if (!childSession) return;
57
+ const parentKey = event?.parentSession || null;
58
+ const row = (await resolver.resolve(childSession, { source: "spawn_hook" })) || {};
59
+ row.child_session_key = childSession;
60
+ row.parent_session_key = parentKey || row.parent_session_key || null;
61
+ row.agent_id = row.agent_id || event?.agentId || null;
62
+
63
+ if (parentKey) {
64
+ const pChain = await resolver.walk(parentKey);
65
+ if (pChain.length) {
66
+ const pRoot = pChain[pChain.length - 1];
67
+ row.root_session_key = pRoot.key;
68
+ row.root_kind = pRoot.kind;
69
+ row.root_label = pRoot.rootLabel ?? pRoot.key;
70
+ }
71
+ }
72
+ row.kind = row.kind || "unknown";
73
+ row.root_kind = row.root_kind || row.kind;
74
+ row.root_session_key = row.root_session_key || childSession;
75
+ row.source = "spawn_hook";
76
+ await store.upsertLineage(row);
77
+ }
78
+
79
+ async function applyCostUsage(event, { store, resolver, getBroadcast, logger, emit }) {
80
+ const usage = event?.usage;
81
+ if (!usage || typeof usage !== "object") return;
82
+ const provider = event?.provider ?? null;
83
+ const model = event?.model ?? null;
84
+ const cost = resolveCost({ provider, model, usage });
85
+ if (!cost || typeof cost.total !== "number") return;
86
+
87
+ const session = event?.session ?? null;
88
+ const sessionId = event?.sessionId ?? session ?? "unknown";
89
+ const msgId = event?.messageId ?? `${sessionId}-${event?.timestamp ?? Date.now()}`;
90
+ const ts = parseTs(event?.timestamp) ?? Date.now();
91
+
92
+ let lineage = session ? await store.getLineage(session) : null;
93
+ if (!lineage && session) {
94
+ const row = await resolver.resolve(session, { source: "lazy_resolve" });
95
+ if (row) { await store.upsertLineage(row); lineage = row; }
96
+ }
97
+
98
+ await store.insertCost({
99
+ session_id: sessionId,
100
+ msg_id: msgId,
101
+ ts,
102
+ provider, model,
103
+ input_tokens: usage.input ?? null,
104
+ output_tokens: usage.output ?? null,
105
+ cache_read_tokens: usage.cacheRead ?? null,
106
+ cache_write_tokens: usage.cacheWrite ?? null,
107
+ total_tokens: usage.totalTokens ?? usage.total ?? null,
108
+ cost_input: cost.input ?? null,
109
+ cost_output: cost.output ?? null,
110
+ cost_cache_read: cost.cacheRead ?? null,
111
+ cost_cache_write: cost.cacheWrite ?? null,
112
+ cost_total: cost.total,
113
+ session_key: session,
114
+ agent_id: lineage?.agent_id ?? event?.agentId ?? null,
115
+ root_session_key: lineage?.root_session_key ?? session,
116
+ root_kind: lineage?.root_kind ?? lineage?.kind ?? null,
117
+ root_label: lineage?.root_label ?? null,
118
+ pipeline_id: lineage?.pipeline_id ?? null,
119
+ flow_id: lineage?.flow_id ?? null,
120
+ node_id: lineage?.node_id ?? null,
121
+ item_key: lineage?.item_key ?? null,
122
+ });
123
+ try { await emit?.("cost.record.added", { costTotal: cost.total, flowId: lineage?.flow_id ?? null }); } catch {}
124
+ checkBudget({ store, logger, justWroteUsd: cost.total, notify: (payload) => getBroadcast?.()?.("plugin.manifold.budget.alert", payload) });
125
+ }
126
+
127
+ function parseTs(value) {
128
+ if (typeof value === "number" && Number.isFinite(value)) return value;
129
+ if (typeof value === "string") {
130
+ const ms = Date.parse(value);
131
+ return Number.isFinite(ms) ? ms : null;
132
+ }
133
+ return null;
134
+ }
@@ -0,0 +1,115 @@
1
+ // labels — translate internal scope names to operator-friendly display
2
+ // strings. Used by HTTP responses so clients don't need their own
3
+ // translation table (single source of truth).
4
+ //
5
+ // Mapping is intentionally lossy in the friendly direction — we hide
6
+ // uuids and internal scope names because the operator doesn't read those.
7
+
8
+ // Matches any label carrying a UUID — the shape of a hand-typed or scripted
9
+ // `--session-id` (test/forge scaffolding, one-off debugging labels), as
10
+ // opposed to a real pipelineId (which never contains one). Detecting by
11
+ // shape instead of enumerating known prefixes ("test-", "verify-",
12
+ // "scorer-creation-", ...) means new ad hoc labels collapse into the same
13
+ // bucket automatically instead of adding fresh noise every time someone
14
+ // types a new one.
15
+ const UUID_RE = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/i;
16
+
17
+ // Display label for cost that exists but belongs to no pipeline — the
18
+ // "Where it's going" breakdown's bucket for every cost row whose lineage
19
+ // walk landed on a root that isn't a flow (cron-owned agent turns,
20
+ // operator ad-hoc sessions, spawned subagents, Telegram DMs).
21
+ //
22
+ // Deliberately a real string rather than SQL NULL: CostBreakdown.Row.label
23
+ // is a non-optional String on clients with strict decoding, and
24
+ // stripTeamPrefix passes non-strings through unchanged, so a null label
25
+ // fails the whole decode and silently blanks the Budget card rather than
26
+ // crashing. Kept in step with UNATTRIBUTED_FILTER below so the projection,
27
+ // the transform, and any future client agree on one value.
28
+ export const UNATTRIBUTED_LABEL = "(unattributed)";
29
+
30
+ // The `filter` payload that round-trips UNATTRIBUTED_LABEL back to the NULL
31
+ // rows it was computed from. Mirrors the "__null__" sentinel /cost/top-calls
32
+ // already accepts (see http-cost.js's top-calls filter loop): a literal
33
+ // `pipeline_id = '(unattributed)'` would match zero rows, because no row
34
+ // stores that string — only the projection synthesizes it.
35
+ export const UNATTRIBUTED_FILTER = { pipeline_id: "__null__" };
36
+
37
+ // Pipeline ids are always shaped `<team>_<name>` (workspace/.pipelines/<team>_<name>.js —
38
+ // the same convention every team's pipelines follow). Stripping the leading
39
+ // segment up to the first underscore is therefore a shape-based rule, not a
40
+ // specific-team rule: it applies identically regardless of which team owns
41
+ // the pipeline, so no team name is ever hardcoded here.
42
+ export function stripTeamPrefix(pipelineId) {
43
+ return typeof pipelineId === "string" ? pipelineId.replace(/^[^_]+_/, "") : pipelineId;
44
+ }
45
+
46
+ export function displayRootLabel(rootKind, rootLabel) {
47
+ if (!rootLabel || rootKind === "unknown") return null;
48
+ switch (rootKind) {
49
+ case "cron-owner":
50
+ // rootLabel here is the cron job's own free-text name (grammar.js's
51
+ // CRON_OWN capture) — an opaque string we don't generate and have no
52
+ // shape contract for, so it's shown as-is rather than guessing at a
53
+ // team-prefix boundary that may not exist or may differ per job.
54
+ return rootLabel;
55
+ case "telegram-dm":
56
+ // rootLabel is the chat id — opaque, so the label is a constant
57
+ // rather than a pass-through.
58
+ return "Chat (Telegram)";
59
+ case "operator-trigger":
60
+ // rootLabel is the triggering pipelineId (routes/operator.js's
61
+ // recordTrigger call) — strip the team prefix for terseness. Legacy
62
+ // rows still literally "Operator runs" have no underscore, so
63
+ // stripTeamPrefix is a no-op on them.
64
+ return stripTeamPrefix(rootLabel);
65
+ case "explicit":
66
+ // The most common explicit purpose is "graph-flow-owner" — used by
67
+ // any flow launched from the CLI or operator-initiated tool call.
68
+ if (rootLabel === "graph-flow-owner") return "Operator runs";
69
+ // Skill-test / probe / forge sessions
70
+ if (rootLabel.startsWith("skill-")) return `Skill test: ${rootLabel.replace(/^skill-/, "")}`;
71
+ // Hand-typed or scripted --session-id values (test/forge scaffolding,
72
+ // one-off debugging) — collapse by shape (see UUID_RE above) rather
73
+ // than passing through raw, uuid-suffixed strings that clutter the
74
+ // instance list. http-tree.js merges same-display-label instance rows,
75
+ // so every ad hoc label lands in one consolidated bucket.
76
+ if (UUID_RE.test(rootLabel)) return "Ad-hoc sessions";
77
+ // Manually-triggered pipeline runs (index.js's recordTrigger overrides
78
+ // rootLabel to the pipelineId when the resolved root is otherwise a bare
79
+ // ad hoc session) — same shape-based stripping as operator-trigger.
80
+ return stripTeamPrefix(rootLabel);
81
+ case "agent-default":
82
+ return `${rootLabel} (idle agent)`;
83
+ case "bare-subagent":
84
+ return "Spawned subagent";
85
+ case "memory-synth-chunk":
86
+ return "Memory synthesis";
87
+ case "flow-triggered":
88
+ return rootLabel || "Pipeline run";
89
+ case "flow-prime":
90
+ return "Pipeline prime";
91
+ case "flow-node":
92
+ case "flow-gate-discuss":
93
+ // These are mid-chain frames; if they appear as root it's because
94
+ // the parent flow was pruned. Surface gently.
95
+ return `Pipeline (parent expired)`;
96
+ case "cron-tick":
97
+ // Mid-chain frame surfacing as root — rare.
98
+ return `Cron run`;
99
+ default:
100
+ return rootLabel;
101
+ }
102
+ }
103
+
104
+ export function displayRootKind(rootKind) {
105
+ switch (rootKind) {
106
+ case "cron-owner":
107
+ case "cron-tick": return "Cron";
108
+ case "telegram-dm": return "Telegram";
109
+ case "operator-trigger":
110
+ case "explicit":
111
+ case "agent_tool":
112
+ case "cli": return "Operator";
113
+ default: return "unknown";
114
+ }
115
+ }
@@ -0,0 +1,77 @@
1
+ // pricer — when the runner fails to stamp usage.cost in a JSONL message
2
+ // (model not registered in config, fallback chain breakage, etc.), compute
3
+ // dollar cost from token counts × per-million rates. Used by the
4
+ // before_message_write hook so hook-written rows converge on one value.
5
+ //
6
+ // Rates sourced from openclaw.json's per-model `cost` blocks where present.
7
+ // Adding a new (provider, model): just append to RATES.
8
+
9
+ const RATES = {
10
+ // openrouter
11
+ "openrouter|deepseek/deepseek-v4-pro": { in: 0.435, out: 0.870, cR: 0.003625, cW: 0 },
12
+ "openrouter|deepseek/deepseek-v4-flash": { in: 0.0983, out: 0.1966, cR: 0.0197, cW: 0 },
13
+ "openrouter|anthropic/claude-sonnet-4.6":{ in: 3.0, out: 15.0, cR: 0.3, cW: 3.75 },
14
+
15
+ // zai (general endpoint)
16
+ "zai|glm-4.5-flash": { in: 0, out: 0, cR: 0, cW: 0 },
17
+
18
+ // zai-coding (GLM Coding Plan dedicated endpoint — required for glm-5.1
19
+ // on this account; the general endpoint rejects it with "insufficient
20
+ // balance", the plan's resource package only being visible from here)
21
+ "zai-coding|glm-5.1": { in: 1.2, out: 4.0, cR: 0.24, cW: 0 },
22
+
23
+ // evolink-anthropic
24
+ "evolink-anthropic|claude-sonnet-4-6": { in: 2.7, out: 13.5, cR: 0.271, cW: 3.375 },
25
+ "evolink-anthropic|deepseek-v4-pro": { in: 1.74, out: 3.48, cR: 0, cW: 0 },
26
+
27
+ // evolink (Google/DeepSeek aggregator)
28
+ "evolink|gemini-2.5-flash-lite": { in: 0.094, out: 0.374, cR: 0.009, cW: 0 },
29
+ "evolink|gemini-3-pro-preview": { in: 1.865, out: 11.182, cR: 0.187, cW: 0 },
30
+ "evolink|deepseek-reasoner": { in: 0.294, out: 0.441, cR: 0.029, cW: 0 },
31
+ };
32
+
33
+ /**
34
+ * Returns { input, output, cacheRead, cacheWrite, total } in dollars,
35
+ * or null if (provider, model) isn't in the rate table.
36
+ */
37
+ export function computeCost(provider, model, usage) {
38
+ if (!provider || !model || !usage) return null;
39
+ const rate = RATES[`${provider}|${model}`];
40
+ if (!rate) return null;
41
+ const ci = ((usage.input ?? 0) / 1_000_000) * rate.in;
42
+ const co = ((usage.output ?? 0) / 1_000_000) * rate.out;
43
+ const cR = ((usage.cacheRead ?? 0) / 1_000_000) * rate.cR;
44
+ const cW = ((usage.cacheWrite ?? 0) / 1_000_000) * rate.cW;
45
+ return { input: ci, output: co, cacheRead: cR, cacheWrite: cW, total: ci + co + cR + cW };
46
+ }
47
+
48
+ /**
49
+ * Take what the source has (usage.cost) and, if it's all zeros despite
50
+ * real tokens, fall back to computed cost. Annotates the returned row
51
+ * with the source so callers can filter.
52
+ */
53
+ export function resolveCost({ provider, model, usage }) {
54
+ const srcCost = usage?.cost;
55
+ const hasTokens =
56
+ (usage?.input ?? 0) > 0 ||
57
+ (usage?.output ?? 0) > 0 ||
58
+ (usage?.cacheRead ?? 0) > 0 ||
59
+ (usage?.cacheWrite ?? 0) > 0;
60
+ const srcTotal = typeof srcCost?.total === "number" ? srcCost.total : 0;
61
+
62
+ if (srcTotal > 0 || !hasTokens) {
63
+ return { source: "runner", ...srcCost, total: srcTotal };
64
+ }
65
+ // Source has zero cost but real tokens — try to compute
66
+ const computed = computeCost(provider, model, usage);
67
+ if (computed) return { source: "computed", ...computed };
68
+ // Unknown rate — record zero but mark unknown
69
+ return { source: "unknown", input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 };
70
+ }
71
+
72
+ export function knownModels() {
73
+ return Object.keys(RATES).map((k) => {
74
+ const [provider, model] = k.split("|");
75
+ return { provider, model };
76
+ });
77
+ }