@igorjpimenta/manifold 0.0.0-bootstrap.1 → 0.0.0-stage
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1583
- package/package.json +4 -49
- package/.migrations/ops/0000_futuristic_karma.sql +0 -111
- package/.migrations/ops/0001_low_maximus.sql +0 -7
- package/.migrations/ops/meta/0000_snapshot.json +0 -763
- package/.migrations/ops/meta/0001_snapshot.json +0 -173
- package/.migrations/ops/meta/_journal.json +0 -20
- package/.migrations/runtime/0000_real_boomer.sql +0 -180
- package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
- package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
- package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
- package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
- package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
- package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
- package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
- package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
- package/.migrations/runtime/0009_lying_shocker.sql +0 -96
- package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
- package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
- package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
- package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
- package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
- package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
- package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
- package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
- package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
- package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
- package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
- package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
- package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
- package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
- package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
- package/.migrations/runtime/meta/_journal.json +0 -97
- package/RELEASING.md +0 -89
- package/agent/fleet.js +0 -100
- package/bin/cli.js +0 -854
- package/bin/manifold.js +0 -4
- package/boot/flow-revival.js +0 -536
- package/budget/index.js +0 -495
- package/budget/lib.js +0 -59
- package/dashboard/cost.js +0 -528
- package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
- package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
- package/dashboard/dist/index.html +0 -13
- package/dashboard/event-log.js +0 -267
- package/dashboard/index.html +0 -12
- package/dashboard/package-lock.json +0 -2877
- package/dashboard/package.json +0 -37
- package/dashboard/pipeline-graph.js +0 -131
- package/dashboard/postcss.config.js +0 -6
- package/dashboard/search.js +0 -183
- package/dashboard/server.js +0 -1015
- package/dashboard/src/App.tsx +0 -42
- package/dashboard/src/api/client.ts +0 -118
- package/dashboard/src/api/types.ts +0 -510
- package/dashboard/src/api/ws.tsx +0 -112
- package/dashboard/src/components/CancelButton.tsx +0 -63
- package/dashboard/src/components/ClickToCopy.tsx +0 -68
- package/dashboard/src/components/CopyButton.tsx +0 -61
- package/dashboard/src/components/DailyCostTrend.tsx +0 -85
- package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
- package/dashboard/src/components/Funnel.tsx +0 -149
- package/dashboard/src/components/GateForm.tsx +0 -158
- package/dashboard/src/components/GraphView.tsx +0 -302
- package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
- package/dashboard/src/components/Histogram.tsx +0 -292
- package/dashboard/src/components/Layout.tsx +0 -28
- package/dashboard/src/components/NeedsAttention.tsx +0 -98
- package/dashboard/src/components/NodeContextCard.tsx +0 -132
- package/dashboard/src/components/PathTrail.tsx +0 -35
- package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
- package/dashboard/src/components/RerunButton.tsx +0 -46
- package/dashboard/src/components/RunComparison.tsx +0 -53
- package/dashboard/src/components/Sparkline.tsx +0 -94
- package/dashboard/src/components/StatStrip.tsx +0 -147
- package/dashboard/src/components/StateTree.tsx +0 -84
- package/dashboard/src/components/StatusBadge.tsx +0 -18
- package/dashboard/src/components/SubflowPathPills.tsx +0 -38
- package/dashboard/src/components/SubflowSummary.tsx +0 -24
- package/dashboard/src/components/SubflowsCard.tsx +0 -327
- package/dashboard/src/components/TodayStrip.tsx +0 -86
- package/dashboard/src/components/TranscriptPane.tsx +0 -99
- package/dashboard/src/components/TranscriptRow.tsx +0 -69
- package/dashboard/src/components/ui/badge.tsx +0 -38
- package/dashboard/src/components/ui/button.tsx +0 -28
- package/dashboard/src/components/ui/card.tsx +0 -42
- package/dashboard/src/components/ui/skeleton.tsx +0 -9
- package/dashboard/src/components/ui/table.tsx +0 -62
- package/dashboard/src/index.css +0 -26
- package/dashboard/src/lib/anomaly.ts +0 -42
- package/dashboard/src/lib/format.ts +0 -30
- package/dashboard/src/lib/step-history.ts +0 -13
- package/dashboard/src/lib/subflow.ts +0 -154
- package/dashboard/src/lib/use-click-outside.ts +0 -28
- package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
- package/dashboard/src/lib/utils.ts +0 -6
- package/dashboard/src/main.tsx +0 -10
- package/dashboard/src/types/shims.d.ts +0 -4
- package/dashboard/src/views/Home.tsx +0 -478
- package/dashboard/src/views/PipelineDetail.tsx +0 -1185
- package/dashboard/src/views/PipelinesList.tsx +0 -111
- package/dashboard/src/views/RunDetail.tsx +0 -248
- package/dashboard/src/views/Search.tsx +0 -196
- package/dashboard/stats.js +0 -668
- package/dashboard/tailwind.config.ts +0 -43
- package/dashboard/tree-stats.js +0 -12
- package/dashboard/tsconfig.json +0 -25
- package/dashboard/views.js +0 -231
- package/dashboard/vite.config.ts +0 -34
- package/drizzle.config.js +0 -12
- package/drizzle.config.ops.js +0 -12
- package/elevation.js +0 -116
- package/engine/admission-promotion-resume.js +0 -127
- package/engine/agent-dispatch.js +0 -64
- package/engine/child-resume-trigger.js +0 -315
- package/engine/executor-budget.js +0 -87
- package/engine/executor-cancel.js +0 -83
- package/engine/executor-errors.js +0 -122
- package/engine/executor-helpers.js +0 -254
- package/engine/executor-nodes.js +0 -292
- package/engine/executor-persist.js +0 -212
- package/engine/executor-subflow-children.js +0 -275
- package/engine/executor.js +0 -1013
- package/engine/global-concurrency.js +0 -33
- package/engine/loader.js +0 -528
- package/engine/needs-resolver.js +0 -229
- package/engine/pipelines-watcher.js +0 -133
- package/engine/result-replay.js +0 -34
- package/engine/revival-messages.js +0 -11
- package/engine/spawn-child.js +0 -55
- package/engine/wait-for-flow-settlement.js +0 -109
- package/events/catalog.js +0 -26
- package/events/emit.js +0 -44
- package/events/kinds.js +0 -118
- package/events/serve-ws.js +0 -74
- package/events/stream.js +0 -35
- package/events/ws-dialog.js +0 -257
- package/events/ws-frame.js +0 -194
- package/events/ws-run-events.js +0 -70
- package/events.js +0 -32
- package/gates/gate-resolver.js +0 -576
- package/gates/paused-flows.js +0 -55
- package/graphql/columns.js +0 -67
- package/graphql/schema.js +0 -333
- package/index.js +0 -49
- package/mcp/http.js +0 -159
- package/mcp/identity.js +0 -94
- package/mcp/stdio.js +0 -80
- package/mcp/tools.js +0 -245
- package/mcp/wire-ops.js +0 -178
- package/routes/bearer-auth.js +0 -57
- package/routes/discuss-audio.js +0 -87
- package/routes/discuss-component.js +0 -39
- package/routes/discuss-draft.js +0 -159
- package/routes/discuss-helpers.js +0 -20
- package/routes/discuss.js +0 -848
- package/routes/failures.js +0 -104
- package/routes/graphql.js +0 -61
- package/routes/health.js +0 -54
- package/routes/operator-discuss.js +0 -193
- package/routes/operator-resume.js +0 -32
- package/routes/operator-retry.js +0 -301
- package/routes/operator.js +0 -539
- package/routes/pipelines.js +0 -118
- package/routes/runs.js +0 -1129
- package/scripts/ci/import-check.mjs +0 -50
- package/scripts/link-core-self.mjs +0 -31
- package/scripts/with-guard.sh +0 -24
- package/server/app.js +0 -503
- package/server/registry.js +0 -53
- package/server/serve.js +0 -163
- package/state/admission-queue-store.js +0 -325
- package/state/attribution-registries.js +0 -102
- package/state/batch-writer.js +0 -162
- package/state/db-guard.js +0 -63
- package/state/db-health.js +0 -27
- package/state/db.js +0 -122
- package/state/discuss-store.js +0 -299
- package/state/failure-store.js +0 -427
- package/state/migrate.js +0 -104
- package/state/node-cost.js +0 -67
- package/state/node-executions.js +0 -775
- package/state/node-health-store.js +0 -124
- package/state/ops-merge.js +0 -93
- package/state/paths.js +0 -146
- package/state/pipeline-dirs.js +0 -99
- package/state/pipeline-registry.js +0 -449
- package/state/run-store.js +0 -697
- package/state/schema.js +0 -438
- package/state/schema.ops.js +0 -44
- package/state/session-index.js +0 -177
- package/state/session-keys.js +0 -39
- package/state/state-store.js +0 -311
- package/state/subflow-state.js +0 -126
- package/state/tool-handoff.js +0 -169
- package/telemetry/budget-alert.js +0 -353
- package/telemetry/cli.js +0 -331
- package/telemetry/grammar.js +0 -119
- package/telemetry/http-budget.js +0 -132
- package/telemetry/http-cost.js +0 -670
- package/telemetry/http-helpers.js +0 -134
- package/telemetry/http-tree.js +0 -349
- package/telemetry/http.js +0 -44
- package/telemetry/index.js +0 -307
- package/telemetry/ingest.js +0 -134
- package/telemetry/labels.js +0 -115
- package/telemetry/pricer.js +0 -77
- package/telemetry/resolver.js +0 -314
- package/telemetry/store.js +0 -147
- package/utils/run-patch.js +0 -43
|
@@ -1,134 +0,0 @@
|
|
|
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
|
-
}
|
package/telemetry/http-tree.js
DELETED
|
@@ -1,349 +0,0 @@
|
|
|
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
|
-
}
|
package/telemetry/http.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
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
|
-
}
|