@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,331 @@
1
+ // Mirrors the http-cost.js endpoints that back the dashboard views — a query
2
+ // change here must land there too, or CLI and dashboard diverge.
3
+
4
+ import { execAll, execGet } from "./http-helpers.js";
5
+
6
+ export function registerCliCommands({ program, logger, getStore, getResolver, dbPath }) {
7
+ const cmd = program.command("lineage").description("Session lineage + cost attribution");
8
+
9
+ cmd.command("lookup <sessionOrId>")
10
+ .description("Walk lineage for a session (or sessionId — looked up via cost_by_call)")
11
+ .action(async (target) => {
12
+ const store = await getStore(); const resolver = await getResolver();
13
+ if (!store || !resolver) { console.error("lineage service not ready"); process.exitCode = 1; return; }
14
+ const key = target.startsWith("agent:") ? target : await sessionIdToSession(store._client, target);
15
+ if (!key) { console.error(`could not resolve ${target} to a session`); process.exitCode = 1; return; }
16
+ const chain = await resolver.walk(key);
17
+ for (let i = 0; i < chain.length; i++) {
18
+ const f = chain[i];
19
+ const indent = " ".repeat(i);
20
+ console.log(`${indent}[${f.kind}] ${f.key}`);
21
+ const extras = [];
22
+ if (f.pipelineId) extras.push(`pipeline=${f.pipelineId}`);
23
+ if (f.flowId) extras.push(`flow=${f.flowId.slice(0,8)}`);
24
+ if (f.nodeId) extras.push(`node=${f.nodeId}`);
25
+ if (f.itemKey) extras.push(`item=${f.itemKey.slice(0,40)}`);
26
+ if (extras.length) console.log(`${indent} ${extras.join(" ")}`);
27
+ }
28
+ const root = chain[chain.length - 1];
29
+ if (root) console.log(`\nROOT: ${root.rootLabel ?? root.key} (kind=${root.kind})`);
30
+ });
31
+
32
+ cmd.command("tree <sessionOrId>")
33
+ .description("Genealogy tree — descendants of a session with per-node $cost. By default starts at the root of the chain; pass --from-here to root at the given node instead.")
34
+ .option("--from-here", "Root the tree at the given key, not at its chain's root")
35
+ .option("--depth <n>", "Max descendant depth (default 12)", "12")
36
+ .option("--show-zero", "Include zero-cost subtrees (default: collapse them)")
37
+ .option("--with-calls", "Drop individual LLM calls as leaves under each session (model, tokens, $)")
38
+ .requiredOption("--kind <kind>", "Input shape (REQUIRED): session-key | session-id | msg | flow | pipeline | cron | run")
39
+ .action(async (target, opts) => {
40
+ const store = await getStore(); const resolver = await getResolver();
41
+ if (!store || !resolver) { console.error("lineage service not ready"); process.exitCode = 1; return; }
42
+ const client = store._client;
43
+ // Resolve the target. Accept any of:
44
+ // • session (starts with agent: / flow:)
45
+ // • flowId (UUID) — looked up via session_lineage.flow_id
46
+ // • cost_by_call.msg_id — for "I have this LLM call's id from a log
47
+ // line, show me the whole tree it lived in"
48
+ // • cost_by_call.session_id (rare — usually identical to a session)
49
+ // • sessionId via cost_by_call.session_id (hook-written rows)
50
+ let key = await resolveTreeTarget(target, client, opts.kind);
51
+ if (!key) {
52
+ console.error(`could not resolve ${target} via --kind ${opts.kind}`);
53
+ process.exitCode = 1; return;
54
+ }
55
+ // Root at the chain root unless --from-here
56
+ if (!opts.fromHere) {
57
+ const row = await execGet(client, `SELECT root_session_key FROM session_lineage WHERE child_session_key = ?`, [key]);
58
+ if (row?.root_session_key) key = row.root_session_key;
59
+ }
60
+ const maxDepth = Math.max(1, Number(opts.depth) || 12);
61
+ const showZero = Boolean(opts.showZero);
62
+ const withCalls = Boolean(opts.withCalls);
63
+ // Pull the parent_session_key-linked descendant subtree in one recursive
64
+ // CTE. The recursion also follows the flow_id side-link: when a row in
65
+ // the frontier is a synthetic `flow:<id>` trigger edge, every session
66
+ // sharing that flow_id (the actual subagent fan-out under manifold-
67
+ // owner) is pulled in as a virtual child. Without this the tree from
68
+ // a cron only reaches the trigger edge and stops — the scorer/content-
69
+ // writer subagents would be missing because their parent_session_key
70
+ // points sideways at graph-flow-owner, not into the cron's chain.
71
+ const rows = await execAll(client, `
72
+ WITH RECURSIVE descendants(child_session_key, depth) AS (
73
+ SELECT child_session_key, 0 FROM session_lineage WHERE child_session_key = ?
74
+ UNION ALL
75
+ SELECT sl.child_session_key, d.depth + 1
76
+ FROM session_lineage sl
77
+ JOIN descendants d
78
+ ON sl.parent_session_key = d.child_session_key
79
+ OR (substr(d.child_session_key, 1, 5) = 'flow:'
80
+ AND sl.flow_id IS NOT NULL
81
+ AND sl.flow_id = substr(d.child_session_key, 6)
82
+ AND sl.child_session_key != d.child_session_key)
83
+ WHERE d.depth < ?
84
+ )
85
+ SELECT sl.child_session_key, sl.parent_session_key, sl.kind, sl.root_label,
86
+ sl.flow_id, sl.first_seen_at,
87
+ COALESCE((SELECT SUM(cost_total) FROM cost_by_call
88
+ WHERE session_key = sl.child_session_key), 0) AS cost
89
+ FROM session_lineage sl
90
+ JOIN descendants d ON sl.child_session_key = d.child_session_key
91
+ GROUP BY sl.child_session_key
92
+ ORDER BY sl.first_seen_at ASC
93
+ `, [key, maxDepth]);
94
+ if (!rows.length) { console.log(`(no lineage row for ${key})`); return; }
95
+ // Build parent → children map. For sessions whose parent_session_key
96
+ // points outside the visible subtree (e.g. flow children pointing to
97
+ // graph-flow-owner), reparent them under the flow:<id> trigger edge.
98
+ const childrenOf = new Map();
99
+ const byKey = new Map();
100
+ for (const r of rows) byKey.set(r.child_session_key, r);
101
+ const flowEdgesByFlowId = new Map();
102
+ for (const r of rows) {
103
+ if (r.child_session_key.startsWith("flow:")) flowEdgesByFlowId.set(r.child_session_key.slice(5), r.child_session_key);
104
+ }
105
+ for (const r of rows) {
106
+ if (r.child_session_key === key) continue;
107
+ let pk = r.parent_session_key;
108
+ // Reparent: if the recorded parent isn't in our subtree but this
109
+ // session belongs to a flow whose trigger edge IS, hang it off the
110
+ // trigger edge.
111
+ if ((!pk || !byKey.has(pk)) && r.flow_id && flowEdgesByFlowId.has(r.flow_id)) {
112
+ pk = flowEdgesByFlowId.get(r.flow_id);
113
+ }
114
+ if (!pk) continue;
115
+ if (!childrenOf.has(pk)) childrenOf.set(pk, []);
116
+ childrenOf.get(pk).push(r);
117
+ }
118
+ // Subtree cost rollup (memoized so we can collapse zero subtrees).
119
+ const subtreeCostMemo = new Map();
120
+ function subtreeCost(k) {
121
+ if (subtreeCostMemo.has(k)) return subtreeCostMemo.get(k);
122
+ const self = byKey.get(k)?.cost ?? 0;
123
+ const kids = childrenOf.get(k) ?? [];
124
+ const total = kids.reduce((a, c) => a + subtreeCost(c.child_session_key), self);
125
+ subtreeCostMemo.set(k, total);
126
+ return total;
127
+ }
128
+ const total = subtreeCost(key);
129
+ function fmt$(v) { return v >= 0.01 ? `$${v.toFixed(2)}` : v > 0 ? `$${v.toFixed(4)}` : "$0"; }
130
+ // Optional per-session LLM-call leaves — model + tokens + $ per row.
131
+ // Fetched up front (one IN-list query) so the recursive render below
132
+ // stays fully synchronous instead of threading async through it.
133
+ const callsBySession = new Map();
134
+ if (withCalls) {
135
+ const ph = rows.map(() => "?").join(",");
136
+ const callRows = await execAll(client, `
137
+ SELECT session_key, ts, model, provider, input_tokens, output_tokens,
138
+ cache_read_tokens, cache_write_tokens, cost_total
139
+ FROM cost_by_call WHERE session_key IN (${ph}) ORDER BY ts ASC
140
+ `, rows.map((r) => r.child_session_key));
141
+ for (const c of callRows) {
142
+ if (!callsBySession.has(c.session_key)) callsBySession.set(c.session_key, []);
143
+ callsBySession.get(c.session_key).push(c);
144
+ }
145
+ }
146
+ function fmtTok(n) {
147
+ if (n == null) return "?";
148
+ if (n >= 1000) return `${(n / 1000).toFixed(n >= 10000 ? 0 : 1)}k`;
149
+ return String(n);
150
+ }
151
+ function renderCalls(session, prefix) {
152
+ if (!withCalls) return;
153
+ const calls = callsBySession.get(session) ?? [];
154
+ calls.forEach((c, i) => {
155
+ const isLast = i === calls.length - 1;
156
+ const t = new Date(c.ts).toISOString().slice(11, 19);
157
+ const model = (c.model ?? "?").split("/").pop().slice(0, 24);
158
+ const tokParts = [];
159
+ if (c.input_tokens != null) tokParts.push(`${fmtTok(c.input_tokens)} in`);
160
+ if (c.output_tokens != null) tokParts.push(`${fmtTok(c.output_tokens)} out`);
161
+ if (c.cache_read_tokens) tokParts.push(`${fmtTok(c.cache_read_tokens)} cache-r`);
162
+ const toks = tokParts.length ? ` (${tokParts.join(", ")})` : "";
163
+ console.log(`${prefix}${isLast ? "└── " : "├── "}[call] ${t} ${model.padEnd(24)} ${fmt$(c.cost_total ?? 0)}${toks}`);
164
+ });
165
+ }
166
+ function renderNode(node, prefix, isLast) {
167
+ const tree$ = subtreeCost(node.child_session_key);
168
+ if (!showZero && tree$ === 0 && (childrenOf.get(node.child_session_key)?.length ?? 0) === 0) {
169
+ // Leaf with no cost — render compactly
170
+ }
171
+ const self$ = node.cost;
172
+ const costStr =
173
+ self$ > 0 && tree$ > self$ ? ` ${fmt$(self$)} (subtree ${fmt$(tree$)})`
174
+ : self$ > 0 ? ` ${fmt$(self$)}`
175
+ : tree$ > 0 ? ` (subtree ${fmt$(tree$)})`
176
+ : "";
177
+ const tailMaxLen = 80;
178
+ const tail = node.child_session_key.length > tailMaxLen
179
+ ? node.child_session_key.slice(0, tailMaxLen - 1) + "…"
180
+ : node.child_session_key;
181
+ console.log(`${prefix}${isLast ? "└── " : "├── "}[${node.kind}] ${tail}${costStr}`);
182
+ const kids = (childrenOf.get(node.child_session_key) ?? [])
183
+ .filter((c) => showZero || subtreeCost(c.child_session_key) > 0 || (childrenOf.get(c.child_session_key)?.length ?? 0) === 0);
184
+ const newPrefix = prefix + (isLast ? " " : "│ ");
185
+ kids.forEach((c, i) => renderNode(c, newPrefix, i === kids.length - 1));
186
+ // Drop LLM call leaves after children (calls are tied to THIS session,
187
+ // not to a child session). Calls render under a different connector
188
+ // since they're leaves, not subtree continuations.
189
+ if (withCalls && self$ > 0) renderCalls(node.child_session_key, newPrefix);
190
+ }
191
+ const root = byKey.get(key);
192
+ console.log(`[${root.kind}] ${root.root_label ?? root.child_session_key} ${fmt$(total)} total`);
193
+ const kids = (childrenOf.get(key) ?? [])
194
+ .filter((c) => showZero || subtreeCost(c.child_session_key) > 0 || (childrenOf.get(c.child_session_key)?.length ?? 0) === 0);
195
+ kids.forEach((c, i) => renderNode(c, "", i === kids.length - 1));
196
+ // Root-level LLM calls (rare — usually the root is an aggregator, not a
197
+ // call site — but cron-owner / explicit roots that directly issue LLMs
198
+ // get their calls rendered too).
199
+ if (withCalls && (root.cost ?? 0) > 0) renderCalls(root.child_session_key, "");
200
+ console.log(`\n${rows.length} session(s) in subtree, ${fmt$(total)} total cost`);
201
+ });
202
+
203
+ cmd.command("stats")
204
+ .description("Summary counts from the lineage + cost tables")
205
+ .action(async () => {
206
+ const store = await getStore();
207
+ if (!store) { console.error("lineage service not ready"); process.exitCode = 1; return; }
208
+ const client = store._client;
209
+ const totalSess = await execGet(client, `SELECT COUNT(*) AS n FROM session_lineage`);
210
+ const bySource = await execAll(client, `SELECT source, COUNT(*) AS n FROM session_lineage GROUP BY source ORDER BY n DESC`);
211
+ const byRootKind = await execAll(client, `SELECT root_kind, COUNT(*) AS n FROM session_lineage GROUP BY root_kind ORDER BY n DESC`);
212
+ const costRows = await execGet(client, `SELECT COUNT(*) AS n, COALESCE(SUM(cost_total),0) AS total FROM cost_by_call`);
213
+ const byRootCost = await execAll(client, `SELECT root_kind, COUNT(*) AS calls, ROUND(SUM(cost_total), 4) AS cost FROM cost_by_call GROUP BY root_kind ORDER BY cost DESC LIMIT 10`);
214
+ console.log(`DB: ${dbPath}`);
215
+ console.log(`Sessions in lineage table: ${totalSess.n}`);
216
+ console.log(`By source:`);
217
+ for (const r of bySource) console.log(` ${r.source.padEnd(22)} ${r.n}`);
218
+ console.log(`By root kind:`);
219
+ for (const r of byRootKind) console.log(` ${r.root_kind.padEnd(22)} ${r.n}`);
220
+ console.log(`\nCost rows: ${costRows.n} total: $${costRows.total.toFixed(4)}`);
221
+ console.log(`By root kind:`);
222
+ for (const r of byRootCost) console.log(` ${r.root_kind.padEnd(22)} calls=${String(r.calls).padStart(6)} cost=$${(r.cost ?? 0).toFixed(4)}`);
223
+ });
224
+
225
+ cmd.command("cost")
226
+ .description("Cost rollup — by-root by default, or pass --by=<dim>")
227
+ .option("--by <dim>", "Dimension: root | pipeline | flow | node | model", "root")
228
+ .option("--since <iso>", "ISO timestamp lower bound")
229
+ .option("--until <iso>", "ISO timestamp upper bound")
230
+ .option("--limit <n>", "Top N", "20")
231
+ .option("--flow-id <id>", "Filter to a specific flow_id")
232
+ .option("--pipeline-id <id>", "Filter to a specific pipeline_id")
233
+ .option("--node-id <id>", "Filter to a specific node_id")
234
+ .option("--root-kind <kind>", "Filter by root_kind (e.g. cron-owner, pipeline-trigger)")
235
+ .option("--root-label <label>", "Filter by root_label")
236
+ .option("--model <model>", "Filter by model")
237
+ .option("--provider <provider>","Filter by provider")
238
+ .action(async (opts) => {
239
+ const store = await getStore();
240
+ if (!store) { console.error("lineage service not ready"); process.exitCode = 1; return; }
241
+ const client = store._client;
242
+ const where = []; const args = [];
243
+ if (opts.since) { where.push("ts >= ?"); args.push(Date.parse(opts.since)); }
244
+ if (opts.until) { where.push("ts <= ?"); args.push(Date.parse(opts.until)); }
245
+ // Column filters — mirror the HTTP makeGroupHandler pass-through
246
+ const colFilters = [
247
+ ["flow_id", opts.flowId],
248
+ ["pipeline_id", opts.pipelineId],
249
+ ["node_id", opts.nodeId],
250
+ ["root_kind", opts.rootKind],
251
+ ["root_label", opts.rootLabel],
252
+ ["model", opts.model],
253
+ ["provider", opts.provider],
254
+ ];
255
+ for (const [col, val] of colFilters) {
256
+ if (val != null && val !== "") { where.push(`${col} = ?`); args.push(val); }
257
+ }
258
+ const w = where.length ? `WHERE ${where.join(" AND ")}` : "";
259
+ const dims = {
260
+ root: `root_label, root_kind, NULL AS extra`,
261
+ pipeline: `pipeline_id, 'pipeline' AS root_kind, NULL AS extra`,
262
+ flow: `flow_id, 'flow' AS root_kind, pipeline_id AS extra`,
263
+ node: `node_id, 'node' AS root_kind, flow_id AS extra`,
264
+ model: `model, 'model' AS root_kind, provider AS extra`,
265
+ };
266
+ const proj = dims[opts.by] || dims.root;
267
+ const sql = `SELECT ${proj}, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost FROM cost_by_call ${w} GROUP BY 1, 2 ORDER BY cost DESC LIMIT ${Number(opts.limit) || 20}`;
268
+ const rows = await execAll(client, sql, args);
269
+ console.log(`By ${opts.by} | rows=${rows.length}`);
270
+ for (const r of rows) {
271
+ const k = r[Object.keys(r)[0]] ?? "(null)";
272
+ const kind = r.root_kind;
273
+ const extra = r.extra ? ` (${r.extra.slice ? r.extra.slice(0, 30) : r.extra})` : "";
274
+ console.log(` ${String(k).padEnd(40)} ${String(kind).padEnd(12)} calls=${String(r.calls).padStart(6)} cost=$${(r.cost ?? 0).toFixed(4)}${extra}`);
275
+ }
276
+ });
277
+ }
278
+
279
+ // Look up a harness sessionId via the hook-written cost_by_call rows, which
280
+ // carry both the harness session_id and the manifold session. Best-effort.
281
+ async function sessionIdToSession(client, sessionId) {
282
+ const hit = await execGet(client,
283
+ `SELECT session_key FROM cost_by_call WHERE session_id = ? AND session_key IS NOT NULL ORDER BY ts DESC LIMIT 1`,
284
+ [sessionId]);
285
+ return hit?.session_key ?? null;
286
+ }
287
+
288
+ // Resolve `lineage tree <target>` into a session via an explicit --kind.
289
+ // No shape-sniffing — the caller has to declare what they're handing in.
290
+ // Predictable, no ambiguous matches across the table.
291
+ async function resolveTreeTarget(target, client, kind) {
292
+ if (!target || typeof target !== "string") return null;
293
+ const t = target.trim();
294
+ const lookups = {
295
+ "session-key": async () => {
296
+ if (!t.startsWith("agent:") && !t.startsWith("flow:")) return null;
297
+ const hit = await execGet(client, `SELECT 1 FROM session_lineage WHERE child_session_key = ? LIMIT 1`, [t]);
298
+ return hit ? t : null;
299
+ },
300
+ "session-id": () => sessionIdToSession(client, t),
301
+ "msg": async () => {
302
+ const r = await execGet(client, `SELECT session_key FROM cost_by_call WHERE msg_id = ? AND session_key IS NOT NULL LIMIT 1`, [t]);
303
+ return r?.session_key ?? null;
304
+ },
305
+ "flow": async () => {
306
+ const edge = await execGet(client, `SELECT 1 FROM session_lineage WHERE child_session_key = ? LIMIT 1`, [`flow:${t}`]);
307
+ if (edge) return `flow:${t}`;
308
+ const any = await execGet(client, `SELECT child_session_key FROM session_lineage WHERE flow_id = ? LIMIT 1`, [t]);
309
+ return any?.child_session_key ?? null;
310
+ },
311
+ "pipeline": async () => {
312
+ // Most recent flow of this pipeline; user wants the latest tree.
313
+ const any = await execGet(client, `SELECT child_session_key FROM session_lineage WHERE pipeline_id = ? AND child_session_key LIKE 'flow:%' ORDER BY first_seen_at DESC LIMIT 1`, [t]);
314
+ return any?.child_session_key ?? null;
315
+ },
316
+ "cron": async () => {
317
+ // Cron-owner row keyed by root_label (the cron name, e.g. freelance-workana-proposal).
318
+ const r = await execGet(client, `SELECT child_session_key FROM session_lineage WHERE root_kind = 'cron-owner' AND root_label = ? LIMIT 1`, [t]);
319
+ return r?.child_session_key ?? null;
320
+ },
321
+ "run": async () => {
322
+ // manifold runId (session_lineage.run_id column). Returns the flow
323
+ // trigger edge that owns this runId.
324
+ const r = await execGet(client, `SELECT child_session_key FROM session_lineage WHERE run_id = ? AND child_session_key LIKE 'flow:%' LIMIT 1`, [t]);
325
+ return r?.child_session_key ?? null;
326
+ },
327
+ };
328
+ const fn = lookups[kind];
329
+ if (!fn) throw new Error(`unknown --kind ${kind}; supported: ${Object.keys(lookups).join(", ")}`);
330
+ return await fn();
331
+ }
@@ -0,0 +1,119 @@
1
+ // session scope grammar — every session shape we know about,
2
+ // with a single `classify` entry point that returns the parent breadcrumb (or
3
+ // null for roots). The breadcrumb might be:
4
+ // { parentKind: "flow", flow8: "..." } → resolver joins to flow_runs
5
+ // { parentKind: "session-key", key: "..." } → resolver walks up
6
+ // { parentKind: "cron-id", cronId: "..." } → resolver joins to cron jobs
7
+ // { parentKind: "memory-synth", runId: "..." } → root (no on-disk session)
8
+ // null → this is a root, no parent
9
+ //
10
+ // Adding a new scope: just add a regex + a CLASSIFIERS entry. The rest of the
11
+ // pipeline (resolver, store, hooks) is grammar-agnostic.
12
+
13
+ const REGEX = {
14
+ CRON_RUN: /^agent:([^:]+):cron:([0-9a-f-]{36}):run:([0-9a-f-]+)$/,
15
+ CRON_OWN: /^agent:([^:]+):cron-(.+)$/,
16
+ BARE_SUB: /^agent:([^:]+):subagent:([0-9a-f-]{36})$/,
17
+ TG_DM: /^agent:([^:]+):telegram:direct:(.+)$/,
18
+ EXPLICIT: /^agent:([^:]+):explicit:(.+)$/,
19
+ MEMSYNTH: /^agent:([^:]+):memory-synth:([^:]+):c-(\d+)$/,
20
+ AGENT_DEF: /^agent:([^:]+):main$/,
21
+ // Synthetic operator-trigger keys minted by the `_elev/start` HTTP route
22
+ // when a pipeline is started via CLI or another harness. Format:
23
+ // agent:operator:(cli|operator):<flowId-uuid>
24
+ // Treated as root (no parent). The `unknown` source variant is NOT included
25
+ // here because those historic flows were actually cron-triggered and need
26
+ // separate SQL attribution, not the operator label.
27
+ OPERATOR_TRIGGER: /^agent:operator:(cli|operator):([0-9a-f-]{36})$/,
28
+ // Catch-all for legacy / manual sessions whose key is `agent:<id>:<tail>`
29
+ // with no recognized scope prefix — e.g. hand-typed --session-id values
30
+ // that don't follow a known scope. Treated as kind=explicit (same semantics
31
+ // as the prefixed form). Tried LAST so specific rules win.
32
+ EXPLICIT_BARE: /^agent:([^:]+):([^:]+)$/,
33
+ };
34
+
35
+ /** Returns { kind, agentId, pipelineId?, flowShort?, nodeId?, itemKey?, parentHint } */
36
+ export function classify(session) {
37
+ if (typeof session !== "string" || !session) {
38
+ return { kind: "unknown", agentId: null, parentHint: null };
39
+ }
40
+
41
+ let m;
42
+
43
+ if ((m = REGEX.CRON_RUN.exec(session))) {
44
+ const [, agentId, cronId, runSid] = m;
45
+ return {
46
+ kind: "cron-tick", agentId, cronId, runSid,
47
+ parentHint: { kind: "cron-id", cronId, agentId },
48
+ };
49
+ }
50
+ if ((m = REGEX.CRON_OWN.exec(session))) {
51
+ const [, agentId, cronName] = m;
52
+ return {
53
+ kind: "cron-owner", agentId, cronName,
54
+ parentHint: null, // ROOT
55
+ };
56
+ }
57
+ if ((m = REGEX.MEMSYNTH.exec(session))) {
58
+ const [, agentId, runTs, chunk] = m;
59
+ return {
60
+ kind: "memory-synth-chunk", agentId, memorySynthRun: runTs, chunk,
61
+ parentHint: { kind: "memory-synth", runId: runTs, agentId },
62
+ };
63
+ }
64
+ if ((m = REGEX.BARE_SUB.exec(session))) {
65
+ const [, agentId, uuid] = m;
66
+ return {
67
+ kind: "bare-subagent", agentId, bareUuid: uuid,
68
+ parentHint: null, // Parent must come from spawn hook
69
+ };
70
+ }
71
+ if ((m = REGEX.TG_DM.exec(session))) {
72
+ const [, agentId, chatId] = m;
73
+ return {
74
+ kind: "telegram-dm", agentId, telegramChatId: chatId,
75
+ parentHint: null, // ROOT
76
+ };
77
+ }
78
+ if ((m = REGEX.OPERATOR_TRIGGER.exec(session))) {
79
+ const [, source, triggerFlowId] = m;
80
+ return {
81
+ kind: "operator-trigger", agentId: "operator", operatorSource: source, triggerFlowId,
82
+ parentHint: null, // ROOT
83
+ };
84
+ }
85
+ if ((m = REGEX.EXPLICIT.exec(session))) {
86
+ const [, agentId, purpose] = m;
87
+ return {
88
+ kind: "explicit", agentId, explicitPurpose: purpose,
89
+ parentHint: null, // ROOT
90
+ };
91
+ }
92
+ if ((m = REGEX.AGENT_DEF.exec(session))) {
93
+ const [, agentId] = m;
94
+ return {
95
+ kind: "agent-default", agentId,
96
+ parentHint: null, // ROOT
97
+ };
98
+ }
99
+ // Fallback: bare-shape explicit (no scope prefix). Specific rules above
100
+ // (cron-owner, subagent, telegram-dm, etc.) get first crack, so this only
101
+ // matches truly unprefixed `agent:<id>:<tail>` keys.
102
+ if ((m = REGEX.EXPLICIT_BARE.exec(session))) {
103
+ const [, agentId, purpose] = m;
104
+ return {
105
+ kind: "explicit", agentId, explicitPurpose: purpose,
106
+ parentHint: null, // ROOT
107
+ };
108
+ }
109
+ return { kind: "unknown", agentId: agentIdOf(session), parentHint: null };
110
+ }
111
+
112
+ export function agentIdOf(session) {
113
+ if (typeof session !== "string") return null;
114
+ const i1 = session.indexOf(":");
115
+ const i2 = session.indexOf(":", i1 + 1);
116
+ if (i1 !== 5 || i2 === -1) return null; // must start "agent:"
117
+ return session.slice(i1 + 1, i2) || null;
118
+ }
119
+
@@ -0,0 +1,132 @@
1
+ // /manifold/telemetry/budget/* routes — cap read/write + alert test.
2
+ import {
3
+ currentCapUSD, setCapAndPersist,
4
+ currentLockThreshold, setLockThresholdAndPersist,
5
+ testFireAlert, getCapHistory, getAlertHistory,
6
+ } from "./budget-alert.js";
7
+ import { createEmitter } from "../events/emit.js";
8
+
9
+ export function registerBudgetRoutes({ api, getStore, logger, json, bad, getBroadcast }) {
10
+ // Canonical-kind fan-out for the two cap emissions below: canonical
11
+ // wire name, envelope adds kind.
12
+ const { emit } = createEmitter({ getBroadcast, logger });
13
+ // ─── /budget/cap — read / update the operator-set daily cap ───────────────
14
+ api.registerHttpRoute({
15
+ path: "/manifold/telemetry/budget/cap", auth: "plugin", match: "exact",
16
+ handler: async (req, res) => {
17
+ if (req.method === "GET") {
18
+ return json(res, { capUSD: currentCapUSD() });
19
+ }
20
+ if (req.method === "PUT") {
21
+ try {
22
+ let raw = "";
23
+ for await (const chunk of req) raw += chunk;
24
+ const body = raw ? JSON.parse(raw) : {};
25
+ const capUSD = Number(body.capUSD);
26
+ if (!capUSD || capUSD <= 0 || !Number.isFinite(capUSD)) {
27
+ return json(res, { error: "capUSD must be a positive number" }, 400);
28
+ }
29
+ await setCapAndPersist(capUSD);
30
+ // Companion views fetch cap/threshold via two independent
31
+ // requests (home tile + hourly chart) — without this broadcast the
32
+ // chart's cap line goes stale until the next unrelated reload.
33
+ try { await emit("budget.cap.changed", { capUSD }); } catch {}
34
+ return json(res, { ok: true, capUSD });
35
+ } catch (err) {
36
+ return json(res, { error: String(err?.message ?? err) }, 400);
37
+ }
38
+ }
39
+ res.statusCode = 405; res.end("method not allowed");
40
+ },
41
+ });
42
+
43
+ // ─── /budget/gate-threshold — read / update the auto-lock threshold ───────
44
+ api.registerHttpRoute({
45
+ path: "/manifold/telemetry/budget/gate-threshold", auth: "plugin", match: "exact",
46
+ handler: async (req, res) => {
47
+ if (req.method === "GET") {
48
+ return json(res, { lockThreshold: currentLockThreshold() });
49
+ }
50
+ if (req.method === "PUT") {
51
+ try {
52
+ let raw = "";
53
+ for await (const chunk of req) raw += chunk;
54
+ const body = raw ? JSON.parse(raw) : {};
55
+ const lockThreshold = Number(body.lockThreshold);
56
+ if (!lockThreshold || lockThreshold <= 0 || lockThreshold > 1 || !Number.isFinite(lockThreshold)) {
57
+ return json(res, { error: "lockThreshold must be a number between 0 and 1" }, 400);
58
+ }
59
+ await setLockThresholdAndPersist(lockThreshold);
60
+ try { await emit("budget.cap.changed", { lockThreshold }); } catch {}
61
+ return json(res, { ok: true, lockThreshold });
62
+ } catch (err) {
63
+ return json(res, { error: String(err?.message ?? err) }, 400);
64
+ }
65
+ }
66
+ res.statusCode = 405; res.end("method not allowed");
67
+ },
68
+ });
69
+
70
+ // ─── /budget/cap-history — SCD2 cap/threshold versions (oldest first) ──────
71
+ api.registerHttpRoute({
72
+ method: "GET", path: "/manifold/telemetry/budget/cap-history", auth: "plugin", match: "exact",
73
+ handler: async (req, res) => {
74
+ try {
75
+ const rows = await getCapHistory();
76
+ const versions = (rows ?? []).map((r) => ({
77
+ capUSD: Number(r.capUSD),
78
+ lockThreshold: Number(r.lockThreshold),
79
+ registeredAt: Number(r.registeredAt),
80
+ retiredAt: r.retiredAt == null ? null : Number(r.retiredAt),
81
+ }));
82
+ return json(res, { versions });
83
+ } catch (err) {
84
+ logger?.warn?.(`lineage[http /budget/cap-history]: ${err?.message ?? err}`);
85
+ return json(res, { error: String(err?.message ?? err) }, 500);
86
+ }
87
+ },
88
+ });
89
+
90
+ // ─── /budget/alert-history — threshold-crossing events, newest first ──────
91
+ // Powers the cap-hit chart's tap-to-detail (which threshold, what time) —
92
+ // distinct from the debounce file, which only suppresses same-day re-fires;
93
+ // every threshold fire is durably logged here with its timestamp.
94
+ api.registerHttpRoute({
95
+ method: "GET", path: "/manifold/telemetry/budget/alert-history", auth: "plugin", match: "exact",
96
+ handler: async (req, res) => {
97
+ try {
98
+ const url = new URL(req.url, "http://x");
99
+ const limit = Math.min(Number(url.searchParams.get("limit")) || 60, 200);
100
+ const rows = await getAlertHistory({ limit });
101
+ const events = (rows ?? []).map((r) => ({
102
+ threshold: Number(r.threshold),
103
+ totalUSD: Number(r.totalUSD),
104
+ capUSD: Number(r.capUSD),
105
+ firedAt: Number(r.firedAt),
106
+ }));
107
+ return json(res, { events });
108
+ } catch (err) {
109
+ logger?.warn?.(`lineage[http /budget/alert-history]: ${err?.message ?? err}`);
110
+ return json(res, { error: String(err?.message ?? err) }, 500);
111
+ }
112
+ },
113
+ });
114
+
115
+ // ─── /budget/test-alert — fire a test push without mutating persisted state
116
+ api.registerHttpRoute({
117
+ method: "POST", path: "/manifold/telemetry/budget/test-alert", auth: "plugin", match: "exact",
118
+ handler: async (req, res) => {
119
+ try {
120
+ const store = getStore();
121
+ if (!store) return bad(res, "lineage store not ready");
122
+ const url = new URL(req.url, "http://x");
123
+ const threshold = Number(url.searchParams.get("threshold") || 0.5);
124
+ await testFireAlert({ store, logger, threshold, notify: (payload) => getBroadcast?.()?.("plugin.manifold.budget.alert", payload) });
125
+ json(res, { ok: true, threshold });
126
+ } catch (err) {
127
+ logger?.warn?.(`lineage[http /budget/test-alert]: ${err?.message ?? err}`);
128
+ json(res, { error: String(err?.message ?? err) }, 500);
129
+ }
130
+ },
131
+ });
132
+ }