@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,134 @@
1
+ // Pure helpers and shared SQL fragments for telemetry HTTP routes.
2
+ import { displayRootKind } from "./labels.js";
3
+
4
+ const ALL_ROOT_KINDS = [
5
+ "cron-owner", "cron-tick",
6
+ "telegram-dm",
7
+ "operator-trigger", "explicit", "agent_tool", "cli",
8
+ ];
9
+
10
+ export function peersForKind(kind) {
11
+ const label = displayRootKind(kind);
12
+ if (label === "unknown") return [kind];
13
+ return ALL_ROOT_KINDS.filter((k) => displayRootKind(k) === label);
14
+ }
15
+
16
+ // Trigger-edge re-attribution: re-root pipeline cost to its originating session
17
+ // (cron / operator / telegram) instead of graph-flow-owner at query time.
18
+ export const ATTR_JOIN = `
19
+ LEFT JOIN session_lineage sl
20
+ ON cbc.flow_id IS NOT NULL
21
+ AND sl.child_session_key = 'flow:' || cbc.flow_id
22
+ AND sl.source IN ('trigger_hook', 'trigger_backfill')
23
+ AND sl.root_kind != 'unknown'`;
24
+ export const ATTR_ROOT_KIND = `COALESCE(sl.root_kind, cbc.root_kind)`;
25
+ export const ATTR_ROOT_LABEL = `COALESCE(sl.root_label, cbc.root_label)`;
26
+
27
+ export function formatSessionLabel(session, tsFirstMs) {
28
+ if (tsFirstMs != null) {
29
+ const d = new Date(tsFirstMs);
30
+ const mon = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"][d.getMonth()];
31
+ const day = d.getDate();
32
+ const hh = String(d.getHours()).padStart(2, "0");
33
+ const mm = String(d.getMinutes()).padStart(2, "0");
34
+ return `${mon} ${day} ${hh}:${mm}`;
35
+ }
36
+ const tail = (session ?? "").split(":").pop() ?? session;
37
+ return (tail || session || "").slice(0, 24);
38
+ }
39
+
40
+ export function sessionKindForRoot(rootKind, session) {
41
+ if (typeof session === "string") {
42
+ if (session.startsWith("agent:operator:cli:")) return "cli-trigger";
43
+ if (session.startsWith("agent:operator:")) return "operator-trigger";
44
+ }
45
+ switch (rootKind) {
46
+ case "cron-owner":
47
+ return (typeof session === "string" && session.includes(":run:"))
48
+ ? "cron-tick"
49
+ : "cron-job-session";
50
+ case "explicit": return "operator-run";
51
+ case "telegram-dm": return "conversation";
52
+ case "memory-synth-chunk": return "memory-chunk";
53
+ case "bare-subagent": return "subagent-run";
54
+ case "agent-default": return "agent-run";
55
+ default: return "session";
56
+ }
57
+ }
58
+
59
+ export function formatSessionChildLabel(row) {
60
+ const { child_key, kind, root_label, flow_id } = row;
61
+ if (child_key.startsWith("flow:")) return `flow ${flow_id?.slice(0,8) ?? child_key.slice(5,13)}`;
62
+ if (kind === "cron-tick") {
63
+ const parts = child_key.split(":");
64
+ const idx = parts.indexOf("run");
65
+ if (idx >= 0 && parts[idx+1]) return `run ${parts[idx+1].slice(0,8)}`;
66
+ }
67
+ if (kind === "flow-node") {
68
+ const parts = child_key.split(":");
69
+ const agent = parts[1] ?? "?";
70
+ const tail = parts[parts.length - 1] ?? "";
71
+ return `${agent} · ${tail.slice(0, 40)}`;
72
+ }
73
+ const parts = child_key.split(":");
74
+ return `${parts[1] ?? "?"} · ${(parts[parts.length - 1] ?? "").slice(0, 30)}`;
75
+ }
76
+
77
+ // Probabilistic midnight forecast: sample per-hour historical distributions,
78
+ // run numSamples ensemble paths, return column-wise p10/p50/p90 per remaining hour.
79
+ export function bootstrapForecast(byDay, currentHour, todayRows, numSamples = 200) {
80
+ const horizon = 23 - currentHour;
81
+ if (horizon <= 0 || byDay.size === 0) return null;
82
+ const hourDists = [];
83
+ for (let h = currentHour + 1; h <= 23; h++) {
84
+ const vals = [];
85
+ for (const [, hourMap] of byDay) vals.push(hourMap.get(h) ?? 0);
86
+ hourDists.push(vals);
87
+ }
88
+ const base = todayRows.reduce((s, r) => s + r.cost, 0);
89
+ const paths = Array.from({ length: numSamples }, () => {
90
+ let cumul = base;
91
+ return hourDists.map((vals) => {
92
+ cumul += vals[Math.floor(Math.random() * vals.length)];
93
+ return cumul;
94
+ });
95
+ });
96
+ return hourDists.map((_, i) => {
97
+ const col = paths.map((p) => p[i]).sort((a, b) => a - b);
98
+ const at = (q) => col[Math.floor((col.length - 1) * q)];
99
+ return {
100
+ hour: currentHour + 1 + i,
101
+ p10: Number(at(0.10).toFixed(6)),
102
+ p50: Number(at(0.50).toFixed(6)),
103
+ p90: Number(at(0.90).toFixed(6)),
104
+ };
105
+ });
106
+ }
107
+
108
+ // execAll/execGet — @libsql/client's `execute()` returns rows as
109
+ // array-like Proxy objects (indexable by both position and column name).
110
+ // Spreading one directly (`{...row}`) picks up numeric index keys
111
+ // alongside the named ones, so we flatten to a plain object here once,
112
+ // at the query boundary, instead of at every call site.
113
+ export async function execAll(client, sql, args = []) {
114
+ const { rows, columns } = await client.execute({ sql, args });
115
+ return rows.map((r) => {
116
+ const o = {};
117
+ for (let i = 0; i < columns.length; i++) o[columns[i]] = r[i];
118
+ return o;
119
+ });
120
+ }
121
+
122
+ export async function execGet(client, sql, args = []) {
123
+ const rows = await execAll(client, sql, args);
124
+ return rows[0] ?? null;
125
+ }
126
+
127
+ export async function hasFlowRuns(client) {
128
+ try {
129
+ const rows = await execAll(client,
130
+ "SELECT 1 FROM sqlite_master WHERE type='table' AND name='flow_runs' LIMIT 1"
131
+ );
132
+ return rows.length > 0;
133
+ } catch { return false; }
134
+ }
@@ -0,0 +1,349 @@
1
+ // /manifold/telemetry/tree/expand — lazy-load one level of the genealogy cost tree.
2
+ import { displayRootKind, displayRootLabel, stripTeamPrefix } from "./labels.js";
3
+ import {
4
+ peersForKind, ATTR_JOIN, ATTR_ROOT_KIND, ATTR_ROOT_LABEL,
5
+ formatSessionLabel, sessionKindForRoot, formatSessionChildLabel,
6
+ execAll, execGet,
7
+ } from "./http-helpers.js";
8
+
9
+ export function registerTreeRoutes({ api, getStore, logger, json, bad, parseRange }) {
10
+ api.registerHttpRoute({
11
+ method: "GET", path: "/manifold/telemetry/tree/expand", auth: "plugin", match: "exact",
12
+ handler: async (req, res) => {
13
+ const store = getStore();
14
+ if (!store) return bad(res, "lineage store not ready");
15
+ const client = store._client;
16
+ try {
17
+ const url = new URL(req.url, "http://x");
18
+ const parent = url.searchParams.get("parent") || "__root__";
19
+ const { where: rangeWhere, args: rangeArgs } = parseRange(url.searchParams);
20
+ const dayClause = rangeWhere.length ? `AND ${rangeWhere.join(" AND ")}` : "";
21
+
22
+ const ATTRIBUTED = `(
23
+ SELECT cbc.ts, cbc.cost_total, cbc.flow_id, cbc.pipeline_id,
24
+ cbc.node_id, cbc.model, cbc.session_id, cbc.session_key,
25
+ ${ATTR_ROOT_KIND} AS root_kind,
26
+ ${ATTR_ROOT_LABEL} AS root_label,
27
+ COALESCE(sl.root_session_key, cbc.root_session_key) AS root_session_key
28
+ FROM cost_by_call cbc ${ATTR_JOIN}
29
+ )`;
30
+
31
+ let children = [];
32
+
33
+ if (parent === "__root__") {
34
+ const rows = await execAll(client, `
35
+ SELECT root_kind, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
36
+ FROM ${ATTRIBUTED}
37
+ WHERE root_kind IS NOT NULL ${dayClause}
38
+ GROUP BY root_kind ORDER BY cost DESC NULLS LAST
39
+ `, rangeArgs);
40
+ const triggerNodes = rows.map((r) => ({
41
+ key: `trigger:${r.root_kind}`,
42
+ kind: "trigger",
43
+ label: displayRootKind(r.root_kind),
44
+ cost: r.cost ?? 0,
45
+ calls: r.calls,
46
+ hasChildren: true,
47
+ _multiKind: false,
48
+ }));
49
+ // Merge nodes that translate to the same display label. First (highest
50
+ // cost, ORDER BY cost DESC) wins the key. "unknown" merges become
51
+ // non-expandable; named buckets keep hasChildren: true.
52
+ const byTriggerLabel = new Map();
53
+ for (const n of triggerNodes) {
54
+ if (byTriggerLabel.has(n.label)) {
55
+ const m = byTriggerLabel.get(n.label);
56
+ m.calls += n.calls;
57
+ m.cost = Number(((m.cost || 0) + (n.cost || 0)).toFixed(6));
58
+ m._multiKind = true;
59
+ } else {
60
+ byTriggerLabel.set(n.label, { ...n });
61
+ }
62
+ }
63
+ children = [...byTriggerLabel.values()]
64
+ .map(({ _multiKind, ...n }) =>
65
+ (_multiKind && n.label === "unknown") ? { ...n, hasChildren: false } : n)
66
+ .sort((a, b) => (b.cost || 0) - (a.cost || 0));
67
+
68
+ } else if (parent.startsWith("trigger:")) {
69
+ const kind = parent.slice("trigger:".length);
70
+ const kinds = peersForKind(kind);
71
+ const kindPh = kinds.map(() => "?").join(",");
72
+ const rows = await execAll(client, `
73
+ SELECT root_label, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
74
+ FROM ${ATTRIBUTED}
75
+ WHERE root_kind IN (${kindPh}) AND root_label IS NOT NULL ${dayClause}
76
+ GROUP BY root_label ORDER BY cost DESC NULLS LAST
77
+ LIMIT 50
78
+ `, [...kinds, ...rangeArgs]);
79
+ const instanceNodes = rows.map((r) => ({
80
+ key: `instance:${kind}:${r.root_label}`,
81
+ kind: "instance",
82
+ label: displayRootLabel(kind, r.root_label),
83
+ cost: r.cost ?? 0,
84
+ calls: r.calls,
85
+ _multiLabel: false,
86
+ }));
87
+ // Merge instance rows that translate to the same display label
88
+ // (e.g. many distinct hand-typed --session-id values all reading
89
+ // as "Ad-hoc sessions" — see labels.js's UUID_RE) — same pattern
90
+ // the trigger level already uses above. A merged node can't be
91
+ // re-expanded through a single instance:<kind>:<rawLabel> key
92
+ // (there's no reverse mapping back to every raw label it absorbed,
93
+ // unlike peersForKind for trigger kinds), so it's non-expandable.
94
+ const byInstanceLabel = new Map();
95
+ for (const n of instanceNodes) {
96
+ if (byInstanceLabel.has(n.label)) {
97
+ const m = byInstanceLabel.get(n.label);
98
+ m.calls += n.calls;
99
+ m.cost = Number(((m.cost || 0) + (n.cost || 0)).toFixed(6));
100
+ m._multiLabel = true;
101
+ } else {
102
+ byInstanceLabel.set(n.label, { ...n });
103
+ }
104
+ }
105
+ children = [...byInstanceLabel.values()]
106
+ .map(({ _multiLabel, ...n }) => ({ ...n, hasChildren: !_multiLabel }))
107
+ .sort((a, b) => (b.cost || 0) - (a.cost || 0));
108
+
109
+ } else if (parent.startsWith("instance:")) {
110
+ const tail = parent.slice("instance:".length);
111
+ const colonIdx = tail.indexOf(":");
112
+ if (colonIdx < 0) return bad(res, `invalid instance parent: ${parent}`, 400);
113
+ const kind = tail.slice(0, colonIdx);
114
+ const label = tail.slice(colonIdx + 1);
115
+ const kinds = peersForKind(kind);
116
+ const kindPh = kinds.map(() => "?").join(",");
117
+ const rows = await execAll(client, `
118
+ WITH attributed_trigger AS (
119
+ SELECT cbc.ts, cbc.cost_total,
120
+ COALESCE(sl.root_kind, cbc.root_kind) AS root_kind,
121
+ COALESCE(sl.root_label, cbc.root_label) AS root_label,
122
+ COALESCE(sl.parent_session_key, cbc.session_key) AS trigger_session_key
123
+ FROM cost_by_call cbc
124
+ LEFT JOIN session_lineage sl
125
+ ON cbc.flow_id IS NOT NULL
126
+ AND sl.child_session_key = 'flow:' || cbc.flow_id
127
+ AND sl.source IN ('trigger_hook', 'trigger_backfill')
128
+ AND sl.kind = 'flow-triggered'
129
+ )
130
+ SELECT trigger_session_key AS session_key,
131
+ COUNT(*) AS calls,
132
+ ROUND(SUM(cost_total), 6) AS cost,
133
+ MIN(ts) AS ts_first, MAX(ts) AS ts_last
134
+ FROM attributed_trigger
135
+ WHERE root_kind IN (${kindPh}) AND root_label = ? ${dayClause}
136
+ GROUP BY trigger_session_key
137
+ ORDER BY cost DESC NULLS LAST
138
+ LIMIT 100
139
+ `, [...kinds, label, ...rangeArgs]);
140
+ // hasChildren: check whether each session actually has expandable children
141
+ // (triggered flows or direct cost rows) rather than hardcoding true.
142
+ const sessions = rows.map((r) => r.session_key);
143
+ const expandable = new Set();
144
+ if (sessions.length > 0) {
145
+ const ph = sessions.map(() => "?").join(",");
146
+ const triggers = await execAll(client, `
147
+ SELECT DISTINCT parent_session_key FROM session_lineage
148
+ WHERE parent_session_key IN (${ph})
149
+ AND kind = 'flow-triggered'
150
+ AND source IN ('trigger_hook', 'trigger_backfill')
151
+ `, sessions);
152
+ for (const t of triggers) expandable.add(t.parent_session_key);
153
+ const directWhere = ["session_key IN (" + ph + ")", "pipeline_id IS NULL", ...rangeWhere];
154
+ const directs = await execAll(client, `
155
+ SELECT DISTINCT session_key FROM cost_by_call
156
+ WHERE ${directWhere.join(" AND ")}
157
+ `, [...sessions, ...rangeArgs]);
158
+ for (const d of directs) expandable.add(d.session_key);
159
+ }
160
+ children = rows.map((r) => {
161
+ const isFlowKey = r.session_key.startsWith("flow:");
162
+ const flowShort = isFlowKey ? r.session_key.slice("flow:".length, "flow:".length + 8) : null;
163
+ return {
164
+ key: r.session_key,
165
+ kind: isFlowKey ? "flow-triggered" : sessionKindForRoot(kind, r.session_key),
166
+ label: isFlowKey
167
+ ? `flow ${flowShort} · ${formatSessionLabel(null, r.ts_first)}`
168
+ : formatSessionLabel(r.session_key, r.ts_first),
169
+ cost: r.cost ?? 0,
170
+ calls: r.calls,
171
+ hasChildren: expandable.has(r.session_key),
172
+ meta: { ts_first: r.ts_first, ts_last: r.ts_last },
173
+ };
174
+ });
175
+
176
+ } else if (parent.startsWith("flow:")) {
177
+ const flowId = parent.slice("flow:".length);
178
+ const rows = await execAll(client, `
179
+ SELECT node_id, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
180
+ FROM cost_by_call
181
+ WHERE flow_id = ? AND node_id IS NOT NULL ${dayClause}
182
+ GROUP BY node_id ORDER BY cost DESC NULLS LAST
183
+ `, [flowId, ...rangeArgs]);
184
+ children = rows.map((r) => ({
185
+ key: `node|${flowId}|${r.node_id}`,
186
+ kind: "flow-node",
187
+ label: r.node_id,
188
+ cost: r.cost ?? 0,
189
+ calls: r.calls,
190
+ hasChildren: true,
191
+ }));
192
+
193
+ } else if (parent.startsWith("node|")) {
194
+ const parts = parent.split("|");
195
+ if (parts.length !== 3) return bad(res, `invalid node parent: ${parent}`, 400);
196
+ const [, flowId, nodeId] = parts;
197
+ const itemRows = await execAll(client, `
198
+ SELECT item_key, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
199
+ FROM cost_by_call
200
+ WHERE flow_id = ? AND node_id = ? AND item_key IS NOT NULL ${dayClause}
201
+ GROUP BY item_key ORDER BY cost DESC NULLS LAST
202
+ `, [flowId, nodeId, ...rangeArgs]);
203
+ children = itemRows.map((r) => ({
204
+ key: `item|${flowId}|${nodeId}|${r.item_key}`,
205
+ kind: "item",
206
+ label: r.item_key,
207
+ cost: r.cost ?? 0,
208
+ calls: r.calls,
209
+ hasChildren: true,
210
+ }));
211
+ const direct = await execGet(client, `
212
+ SELECT COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
213
+ FROM cost_by_call
214
+ WHERE flow_id = ? AND node_id = ? AND item_key IS NULL ${dayClause}
215
+ `, [flowId, nodeId, ...rangeArgs]);
216
+ if (direct?.calls > 0) {
217
+ children.push({
218
+ key: `direct-item|${flowId}|${nodeId}`,
219
+ kind: "direct-item",
220
+ label: "Direct (no item)",
221
+ cost: direct.cost ?? 0,
222
+ calls: direct.calls,
223
+ hasChildren: true,
224
+ });
225
+ }
226
+
227
+ } else if (parent.startsWith("item|")) {
228
+ const parts = parent.split("|");
229
+ if (parts.length !== 4) return bad(res, `invalid item parent: ${parent}`, 400);
230
+ const [, flowId, nodeId, itemKey] = parts;
231
+ const rows = await execAll(client, `
232
+ SELECT session_key, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
233
+ FROM cost_by_call
234
+ WHERE flow_id = ? AND node_id = ? AND item_key = ? ${dayClause}
235
+ GROUP BY session_key ORDER BY cost DESC NULLS LAST
236
+ `, [flowId, nodeId, itemKey, ...rangeArgs]);
237
+ children = rows.map((r) => ({
238
+ key: r.session_key,
239
+ kind: "subagent",
240
+ label: formatSessionChildLabel({ child_key: r.session_key, kind: "flow-node", flow_id: flowId }),
241
+ cost: r.cost ?? 0,
242
+ calls: r.calls,
243
+ hasChildren: false,
244
+ }));
245
+
246
+ } else if (parent.startsWith("direct-item|")) {
247
+ const parts = parent.split("|");
248
+ if (parts.length !== 3) return bad(res, `invalid direct-item parent: ${parent}`, 400);
249
+ const [, flowId, nodeId] = parts;
250
+ const rows = await execAll(client, `
251
+ SELECT session_key, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
252
+ FROM cost_by_call
253
+ WHERE flow_id = ? AND node_id = ? AND item_key IS NULL ${dayClause}
254
+ GROUP BY session_key ORDER BY cost DESC NULLS LAST
255
+ `, [flowId, nodeId, ...rangeArgs]);
256
+ children = rows.map((r) => ({
257
+ key: r.session_key,
258
+ kind: "subagent",
259
+ label: formatSessionChildLabel({ child_key: r.session_key, kind: "flow-node", flow_id: flowId }),
260
+ cost: r.cost ?? 0,
261
+ calls: r.calls,
262
+ hasChildren: false,
263
+ }));
264
+
265
+ } else if (parent.startsWith("direct-pipeline|")) {
266
+ const parts = parent.split("|");
267
+ if (parts.length !== 3) return bad(res, `invalid direct-pipeline parent: ${parent}`, 400);
268
+ const [, kind, label] = parts;
269
+ const rows = await execAll(client, `
270
+ SELECT session_key, COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost,
271
+ MIN(ts) AS ts_first
272
+ FROM ${ATTRIBUTED}
273
+ WHERE root_kind = ? AND root_label = ? AND pipeline_id IS NULL ${dayClause}
274
+ GROUP BY session_key ORDER BY cost DESC NULLS LAST
275
+ LIMIT 100
276
+ `, [kind, label, ...rangeArgs]);
277
+ children = rows.map((r) => ({
278
+ key: r.session_key,
279
+ kind: sessionKindForRoot(kind, r.session_key),
280
+ label: formatSessionLabel(r.session_key, r.ts_first),
281
+ cost: r.cost ?? 0,
282
+ calls: r.calls,
283
+ hasChildren: false,
284
+ meta: { ts_first: r.ts_first },
285
+ }));
286
+
287
+ } else if (parent.startsWith("agent:")) {
288
+ // Trigger-session: children are flow cards it triggered + a Direct leaf.
289
+ const flowEdges = await execAll(client, `
290
+ SELECT flow_id, child_session_key
291
+ FROM session_lineage
292
+ WHERE parent_session_key = ?
293
+ AND source IN ('trigger_hook', 'trigger_backfill')
294
+ AND kind = 'flow-triggered'
295
+ AND flow_id IS NOT NULL
296
+ `, [parent]);
297
+ children = [];
298
+ if (flowEdges.length > 0) {
299
+ const flowIds = flowEdges.map((e) => e.flow_id);
300
+ const ph = flowIds.map(() => "?").join(",");
301
+ const flowRows = await execAll(client, `
302
+ SELECT flow_id, pipeline_id,
303
+ COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost,
304
+ MIN(ts) AS ts_first, MAX(ts) AS ts_last
305
+ FROM cost_by_call
306
+ WHERE flow_id IN (${ph}) ${dayClause}
307
+ GROUP BY flow_id
308
+ `, [...flowIds, ...rangeArgs]);
309
+ for (const r of flowRows.sort((a, b) => (b.cost ?? 0) - (a.cost ?? 0))) {
310
+ const pipeName = stripTeamPrefix(r.pipeline_id || "flow").replace(/_/g, "-");
311
+ children.push({
312
+ key: `flow:${r.flow_id}`,
313
+ kind: "flow-triggered",
314
+ label: `${pipeName} · ${r.flow_id.slice(0, 8)} · ${formatSessionLabel(null, r.ts_first)}`,
315
+ cost: r.cost ?? 0,
316
+ calls: r.calls,
317
+ hasChildren: true,
318
+ meta: { flow_id: r.flow_id, ts_first: r.ts_first, ts_last: r.ts_last },
319
+ });
320
+ }
321
+ }
322
+ const direct = await execGet(client, `
323
+ SELECT COUNT(*) AS calls, ROUND(SUM(cost_total), 6) AS cost
324
+ FROM cost_by_call
325
+ WHERE session_key = ? AND pipeline_id IS NULL ${dayClause}
326
+ `, [parent, ...rangeArgs]);
327
+ if (direct?.calls > 0) {
328
+ children.push({
329
+ key: `direct-session|${parent}`,
330
+ kind: "direct-pipeline",
331
+ label: "Direct (this session's own calls)",
332
+ cost: direct.cost ?? 0,
333
+ calls: direct.calls,
334
+ hasChildren: false,
335
+ });
336
+ }
337
+
338
+ } else {
339
+ return bad(res, `unknown parent shape: ${parent}`, 400);
340
+ }
341
+
342
+ json(res, { parent, children });
343
+ } catch (err) {
344
+ logger?.warn?.(`lineage[http /tree/expand]: ${err?.message ?? err}`);
345
+ json(res, { error: String(err?.message ?? err) }, 500);
346
+ }
347
+ },
348
+ });
349
+ }
@@ -0,0 +1,44 @@
1
+ // HTTP routes for manifold telemetry. Mounted under /manifold/telemetry/*.
2
+ //
3
+ // All routes are GET/PUT, read-only except budget cap writes, and return JSON.
4
+ // Auth: "plugin" — operator session only, matches dashboard semantics.
5
+ //
6
+ // Routes:
7
+ // /manifold/telemetry/cost/today — running total + top movers
8
+ // /manifold/telemetry/cost/by-root [?since=&until=] — group by root session
9
+ // /manifold/telemetry/cost/by-trigger — group by root_kind (cron / telegram / explicit / ...)
10
+ // /manifold/telemetry/cost/by-cron-job — group by cron job name
11
+ // /manifold/telemetry/cost/by-pipeline — group by pipeline_id
12
+ // /manifold/telemetry/cost/by-flow [?pipeline_id=] — group by flow_id (drill-down inside a pipeline)
13
+ // /manifold/telemetry/cost/by-node [?flow_id=] — group by node_id (drill-down inside a flow)
14
+ // /manifold/telemetry/cost/by-model — group by provider+model
15
+ // /manifold/telemetry/cost/by-day [?days=14] — group by local day for the last N days
16
+ // /manifold/telemetry/cost/top-calls [?limit=20] — most expensive single LLM calls
17
+ // /manifold/telemetry/cost/home — combined Home-tile payload
18
+
19
+ import { registerCostRoutes } from "./http-cost.js";
20
+ import { registerTreeRoutes } from "./http-tree.js";
21
+ import { registerBudgetRoutes } from "./http-budget.js";
22
+
23
+ export function registerHttpRoutes({ api, getStore, logger, getBroadcast }) {
24
+ const json = (res, body, status = 200) => {
25
+ res.statusCode = status;
26
+ res.setHeader("content-type", "application/json");
27
+ res.end(JSON.stringify(body));
28
+ };
29
+ const bad = (res, msg, status = 503) => json(res, { error: msg }, status);
30
+
31
+ const parseRange = (urlParams) => {
32
+ const since = urlParams.get("since");
33
+ const until = urlParams.get("until");
34
+ const args = [];
35
+ const where = [];
36
+ if (since) { const ms = Date.parse(since); if (Number.isFinite(ms)) { where.push("ts >= ?"); args.push(ms); } }
37
+ if (until) { const ms = Date.parse(until); if (Number.isFinite(ms)) { where.push("ts <= ?"); args.push(ms); } }
38
+ return { where, args };
39
+ };
40
+
41
+ registerCostRoutes({ api, getStore, logger, json, bad, parseRange });
42
+ registerTreeRoutes({ api, getStore, logger, json, bad, parseRange });
43
+ registerBudgetRoutes({ api, getStore, logger, json, bad, getBroadcast });
44
+ }