@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
package/telemetry/cli.js
DELETED
|
@@ -1,331 +0,0 @@
|
|
|
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
|
-
}
|
package/telemetry/grammar.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
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
|
-
|
package/telemetry/http-budget.js
DELETED
|
@@ -1,132 +0,0 @@
|
|
|
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
|
-
}
|