@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.
Files changed (210) hide show
  1. package/README.md +2 -1583
  2. package/package.json +4 -49
  3. package/.migrations/ops/0000_futuristic_karma.sql +0 -111
  4. package/.migrations/ops/0001_low_maximus.sql +0 -7
  5. package/.migrations/ops/meta/0000_snapshot.json +0 -763
  6. package/.migrations/ops/meta/0001_snapshot.json +0 -173
  7. package/.migrations/ops/meta/_journal.json +0 -20
  8. package/.migrations/runtime/0000_real_boomer.sql +0 -180
  9. package/.migrations/runtime/0001_hot_dakota_north.sql +0 -2
  10. package/.migrations/runtime/0002_breezy_vertigo.sql +0 -8
  11. package/.migrations/runtime/0003_dashing_jack_flag.sql +0 -5
  12. package/.migrations/runtime/0004_panoramic_ego.sql +0 -1
  13. package/.migrations/runtime/0005_tidy_ben_grimm.sql +0 -10
  14. package/.migrations/runtime/0006_wet_nightmare.sql +0 -57
  15. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +0 -8
  16. package/.migrations/runtime/0008_wealthy_thundra.sql +0 -301
  17. package/.migrations/runtime/0009_lying_shocker.sql +0 -96
  18. package/.migrations/runtime/0010_solid_phantom_reporter.sql +0 -160
  19. package/.migrations/runtime/0011_windy_demogoblin.sql +0 -175
  20. package/.migrations/runtime/0012_restore_harness_fks.sql +0 -103
  21. package/.migrations/runtime/meta/0000_snapshot.json +0 -1181
  22. package/.migrations/runtime/meta/0001_snapshot.json +0 -1209
  23. package/.migrations/runtime/meta/0002_snapshot.json +0 -1261
  24. package/.migrations/runtime/meta/0003_snapshot.json +0 -1292
  25. package/.migrations/runtime/meta/0004_snapshot.json +0 -1240
  26. package/.migrations/runtime/meta/0006_snapshot.json +0 -1310
  27. package/.migrations/runtime/meta/0007_snapshot.json +0 -1362
  28. package/.migrations/runtime/meta/0008_snapshot.json +0 -1742
  29. package/.migrations/runtime/meta/0009_snapshot.json +0 -2413
  30. package/.migrations/runtime/meta/0010_snapshot.json +0 -2562
  31. package/.migrations/runtime/meta/0011_snapshot.json +0 -2812
  32. package/.migrations/runtime/meta/0012_snapshot.json +0 -2812
  33. package/.migrations/runtime/meta/_journal.json +0 -97
  34. package/RELEASING.md +0 -89
  35. package/agent/fleet.js +0 -100
  36. package/bin/cli.js +0 -854
  37. package/bin/manifold.js +0 -4
  38. package/boot/flow-revival.js +0 -536
  39. package/budget/index.js +0 -495
  40. package/budget/lib.js +0 -59
  41. package/dashboard/cost.js +0 -528
  42. package/dashboard/dist/assets/index-CUfp63sj.css +0 -1
  43. package/dashboard/dist/assets/index-Qy9JMpma.js +0 -371
  44. package/dashboard/dist/index.html +0 -13
  45. package/dashboard/event-log.js +0 -267
  46. package/dashboard/index.html +0 -12
  47. package/dashboard/package-lock.json +0 -2877
  48. package/dashboard/package.json +0 -37
  49. package/dashboard/pipeline-graph.js +0 -131
  50. package/dashboard/postcss.config.js +0 -6
  51. package/dashboard/search.js +0 -183
  52. package/dashboard/server.js +0 -1015
  53. package/dashboard/src/App.tsx +0 -42
  54. package/dashboard/src/api/client.ts +0 -118
  55. package/dashboard/src/api/types.ts +0 -510
  56. package/dashboard/src/api/ws.tsx +0 -112
  57. package/dashboard/src/components/CancelButton.tsx +0 -63
  58. package/dashboard/src/components/ClickToCopy.tsx +0 -68
  59. package/dashboard/src/components/CopyButton.tsx +0 -61
  60. package/dashboard/src/components/DailyCostTrend.tsx +0 -85
  61. package/dashboard/src/components/DailyTimeTrend.tsx +0 -94
  62. package/dashboard/src/components/Funnel.tsx +0 -149
  63. package/dashboard/src/components/GateForm.tsx +0 -158
  64. package/dashboard/src/components/GraphView.tsx +0 -302
  65. package/dashboard/src/components/GroupPathsSummary.tsx +0 -47
  66. package/dashboard/src/components/Histogram.tsx +0 -292
  67. package/dashboard/src/components/Layout.tsx +0 -28
  68. package/dashboard/src/components/NeedsAttention.tsx +0 -98
  69. package/dashboard/src/components/NodeContextCard.tsx +0 -132
  70. package/dashboard/src/components/PathTrail.tsx +0 -35
  71. package/dashboard/src/components/PipelineCostBreakdown.tsx +0 -277
  72. package/dashboard/src/components/RerunButton.tsx +0 -46
  73. package/dashboard/src/components/RunComparison.tsx +0 -53
  74. package/dashboard/src/components/Sparkline.tsx +0 -94
  75. package/dashboard/src/components/StatStrip.tsx +0 -147
  76. package/dashboard/src/components/StateTree.tsx +0 -84
  77. package/dashboard/src/components/StatusBadge.tsx +0 -18
  78. package/dashboard/src/components/SubflowPathPills.tsx +0 -38
  79. package/dashboard/src/components/SubflowSummary.tsx +0 -24
  80. package/dashboard/src/components/SubflowsCard.tsx +0 -327
  81. package/dashboard/src/components/TodayStrip.tsx +0 -86
  82. package/dashboard/src/components/TranscriptPane.tsx +0 -99
  83. package/dashboard/src/components/TranscriptRow.tsx +0 -69
  84. package/dashboard/src/components/ui/badge.tsx +0 -38
  85. package/dashboard/src/components/ui/button.tsx +0 -28
  86. package/dashboard/src/components/ui/card.tsx +0 -42
  87. package/dashboard/src/components/ui/skeleton.tsx +0 -9
  88. package/dashboard/src/components/ui/table.tsx +0 -62
  89. package/dashboard/src/index.css +0 -26
  90. package/dashboard/src/lib/anomaly.ts +0 -42
  91. package/dashboard/src/lib/format.ts +0 -30
  92. package/dashboard/src/lib/step-history.ts +0 -13
  93. package/dashboard/src/lib/subflow.ts +0 -154
  94. package/dashboard/src/lib/use-click-outside.ts +0 -28
  95. package/dashboard/src/lib/use-ignored-clusters.ts +0 -67
  96. package/dashboard/src/lib/utils.ts +0 -6
  97. package/dashboard/src/main.tsx +0 -10
  98. package/dashboard/src/types/shims.d.ts +0 -4
  99. package/dashboard/src/views/Home.tsx +0 -478
  100. package/dashboard/src/views/PipelineDetail.tsx +0 -1185
  101. package/dashboard/src/views/PipelinesList.tsx +0 -111
  102. package/dashboard/src/views/RunDetail.tsx +0 -248
  103. package/dashboard/src/views/Search.tsx +0 -196
  104. package/dashboard/stats.js +0 -668
  105. package/dashboard/tailwind.config.ts +0 -43
  106. package/dashboard/tree-stats.js +0 -12
  107. package/dashboard/tsconfig.json +0 -25
  108. package/dashboard/views.js +0 -231
  109. package/dashboard/vite.config.ts +0 -34
  110. package/drizzle.config.js +0 -12
  111. package/drizzle.config.ops.js +0 -12
  112. package/elevation.js +0 -116
  113. package/engine/admission-promotion-resume.js +0 -127
  114. package/engine/agent-dispatch.js +0 -64
  115. package/engine/child-resume-trigger.js +0 -315
  116. package/engine/executor-budget.js +0 -87
  117. package/engine/executor-cancel.js +0 -83
  118. package/engine/executor-errors.js +0 -122
  119. package/engine/executor-helpers.js +0 -254
  120. package/engine/executor-nodes.js +0 -292
  121. package/engine/executor-persist.js +0 -212
  122. package/engine/executor-subflow-children.js +0 -275
  123. package/engine/executor.js +0 -1013
  124. package/engine/global-concurrency.js +0 -33
  125. package/engine/loader.js +0 -528
  126. package/engine/needs-resolver.js +0 -229
  127. package/engine/pipelines-watcher.js +0 -133
  128. package/engine/result-replay.js +0 -34
  129. package/engine/revival-messages.js +0 -11
  130. package/engine/spawn-child.js +0 -55
  131. package/engine/wait-for-flow-settlement.js +0 -109
  132. package/events/catalog.js +0 -26
  133. package/events/emit.js +0 -44
  134. package/events/kinds.js +0 -118
  135. package/events/serve-ws.js +0 -74
  136. package/events/stream.js +0 -35
  137. package/events/ws-dialog.js +0 -257
  138. package/events/ws-frame.js +0 -194
  139. package/events/ws-run-events.js +0 -70
  140. package/events.js +0 -32
  141. package/gates/gate-resolver.js +0 -576
  142. package/gates/paused-flows.js +0 -55
  143. package/graphql/columns.js +0 -67
  144. package/graphql/schema.js +0 -333
  145. package/index.js +0 -49
  146. package/mcp/http.js +0 -159
  147. package/mcp/identity.js +0 -94
  148. package/mcp/stdio.js +0 -80
  149. package/mcp/tools.js +0 -245
  150. package/mcp/wire-ops.js +0 -178
  151. package/routes/bearer-auth.js +0 -57
  152. package/routes/discuss-audio.js +0 -87
  153. package/routes/discuss-component.js +0 -39
  154. package/routes/discuss-draft.js +0 -159
  155. package/routes/discuss-helpers.js +0 -20
  156. package/routes/discuss.js +0 -848
  157. package/routes/failures.js +0 -104
  158. package/routes/graphql.js +0 -61
  159. package/routes/health.js +0 -54
  160. package/routes/operator-discuss.js +0 -193
  161. package/routes/operator-resume.js +0 -32
  162. package/routes/operator-retry.js +0 -301
  163. package/routes/operator.js +0 -539
  164. package/routes/pipelines.js +0 -118
  165. package/routes/runs.js +0 -1129
  166. package/scripts/ci/import-check.mjs +0 -50
  167. package/scripts/link-core-self.mjs +0 -31
  168. package/scripts/with-guard.sh +0 -24
  169. package/server/app.js +0 -503
  170. package/server/registry.js +0 -53
  171. package/server/serve.js +0 -163
  172. package/state/admission-queue-store.js +0 -325
  173. package/state/attribution-registries.js +0 -102
  174. package/state/batch-writer.js +0 -162
  175. package/state/db-guard.js +0 -63
  176. package/state/db-health.js +0 -27
  177. package/state/db.js +0 -122
  178. package/state/discuss-store.js +0 -299
  179. package/state/failure-store.js +0 -427
  180. package/state/migrate.js +0 -104
  181. package/state/node-cost.js +0 -67
  182. package/state/node-executions.js +0 -775
  183. package/state/node-health-store.js +0 -124
  184. package/state/ops-merge.js +0 -93
  185. package/state/paths.js +0 -146
  186. package/state/pipeline-dirs.js +0 -99
  187. package/state/pipeline-registry.js +0 -449
  188. package/state/run-store.js +0 -697
  189. package/state/schema.js +0 -438
  190. package/state/schema.ops.js +0 -44
  191. package/state/session-index.js +0 -177
  192. package/state/session-keys.js +0 -39
  193. package/state/state-store.js +0 -311
  194. package/state/subflow-state.js +0 -126
  195. package/state/tool-handoff.js +0 -169
  196. package/telemetry/budget-alert.js +0 -353
  197. package/telemetry/cli.js +0 -331
  198. package/telemetry/grammar.js +0 -119
  199. package/telemetry/http-budget.js +0 -132
  200. package/telemetry/http-cost.js +0 -670
  201. package/telemetry/http-helpers.js +0 -134
  202. package/telemetry/http-tree.js +0 -349
  203. package/telemetry/http.js +0 -44
  204. package/telemetry/index.js +0 -307
  205. package/telemetry/ingest.js +0 -134
  206. package/telemetry/labels.js +0 -115
  207. package/telemetry/pricer.js +0 -77
  208. package/telemetry/resolver.js +0 -314
  209. package/telemetry/store.js +0 -147
  210. package/utils/run-patch.js +0 -43
@@ -1,353 +0,0 @@
1
- // Daily-budget guard. Called from the before_message_write hook after each
2
- // cost insert. When today's total exceeds CAP_USD, writes the
3
- // budget_alert_events row and emits a manifold.budget.alert event for a
4
- // consumer to deliver (once per day per threshold — debounced via
5
- // lastAlertedLocalDay). Delivery is not manifold's job.
6
-
7
- import fs from "node:fs";
8
- import path from "node:path";
9
- import os from "node:os";
10
- import { randomUUID } from "node:crypto";
11
- import { displayRootLabel } from "./labels.js";
12
- import { execGet } from "./http-helpers.js";
13
- import { getSharedDb, withSharedWrite } from "../state/db.js";
14
- import { migrateDatabase, OPS_MIGRATIONS_FOLDER } from "../state/migrate.js";
15
-
16
- const DEFAULT_CAP_USD = 3.0;
17
- const DEFAULT_LOCK_THRESHOLD = 0.8; // fraction of cap; operator-configurable via dashboard
18
-
19
- // Thresholds — fraction of cap. Each fires at most once per local day.
20
- // 50% lets the operator intervene before damage is done; 80% is the
21
- // "approaching" warning; 100% means we've crossed the line.
22
- const THRESHOLDS = [0.1, 0.5, 0.8, 1.0];
23
-
24
- // Persisted across gateway restarts so we don't re-alert on every reload of
25
- // the same already-over-budget day. The once-per-day-per-threshold debounce
26
- // is the only silencing mechanism — no operator-facing snooze (removed:
27
- // redundant with the debounce, no clear product job).
28
- const STATE_FILE = path.join(os.homedir(), ".openclaw", "lineage", ".alerted-thresholds.json");
29
- // Same physical file budget/index.js's gate_state lives in — each module
30
- // owns its own tables there.
31
- const DB_PATH = path.join(os.homedir(), ".openclaw", "ops.sqlite");
32
-
33
- let state = readState(); // { day: "YYYY-MM-DD", fired: [0.5, 0.8, 1.0] }
34
- let lastObservedTotal = 0;
35
-
36
- // In-memory cache — read on the hot checkBudget path with zero I/O.
37
- // Populated by initCapStore() at service start; updated optimistically
38
- // (before the DB write lands) by the setters below.
39
- let configuredCapUSD = DEFAULT_CAP_USD;
40
- let configuredLockThreshold = DEFAULT_LOCK_THRESHOLD;
41
-
42
- let _dbPath = DB_PATH;
43
- let _client = null;
44
- async function _getClient() {
45
- if (!_client) {
46
- await migrateDatabase({ dbPath: _dbPath, migrationsFolder: OPS_MIGRATIONS_FOLDER });
47
- const { client } = await getSharedDb(_dbPath);
48
- _client = client;
49
- }
50
- return _client;
51
- }
52
-
53
- /**
54
- * Loads the active cap_versions row into the in-memory cache, seeding one
55
- * row from the hardcoded defaults on a bare table. Must complete before
56
- * checkBudget()/the HTTP cap routes see traffic; registered as its own
57
- * api.registerService start hook.
58
- *
59
- * dbPath override is test-only (isolated per-test files) — production
60
- * always uses the ~/.openclaw default.
61
- */
62
- export async function initCapStore({ dbPath = DB_PATH } = {}) {
63
- _dbPath = dbPath;
64
- _client = null; // re-resolve against the (possibly new) path
65
- const client = await _getClient();
66
- const { rows } = await client.execute(
67
- `SELECT cap_usd AS capUSD, lock_threshold AS lockThreshold FROM cap_versions WHERE retired_at IS NULL`
68
- );
69
- if (rows.length > 0) {
70
- configuredCapUSD = rows[0].capUSD;
71
- configuredLockThreshold = rows[0].lockThreshold;
72
- return;
73
- }
74
- await client.execute({
75
- sql: `INSERT INTO cap_versions (id, cap_usd, lock_threshold, registered_at, retired_at) VALUES (?, ?, ?, ?, NULL)`,
76
- args: [randomUUID(), DEFAULT_CAP_USD, DEFAULT_LOCK_THRESHOLD, Date.now()],
77
- });
78
- configuredCapUSD = DEFAULT_CAP_USD;
79
- configuredLockThreshold = DEFAULT_LOCK_THRESHOLD;
80
- }
81
-
82
- /**
83
- * SCD2 write — retires the current active row and inserts a new one in a
84
- * single atomic batch, same pattern as pipeline_versions. A no-op when the
85
- * patch doesn't actually change anything, so re-PUTting the same value
86
- * doesn't churn out a spurious history row.
87
- */
88
- async function _writeCapVersion(patch) {
89
- const client = await _getClient();
90
- const { rows } = await client.execute(
91
- `SELECT id, cap_usd AS capUSD, lock_threshold AS lockThreshold FROM cap_versions WHERE retired_at IS NULL`
92
- );
93
- const active = rows[0] ?? null;
94
- const nextCap = patch.capUSD ?? active?.capUSD ?? configuredCapUSD;
95
- const nextThreshold = patch.lockThreshold ?? active?.lockThreshold ?? configuredLockThreshold;
96
- if (active && active.capUSD === nextCap && active.lockThreshold === nextThreshold) return;
97
- const now = Date.now();
98
- const queries = [];
99
- if (active) queries.push({ sql: `UPDATE cap_versions SET retired_at = ? WHERE id = ?`, args: [now, active.id] });
100
- queries.push({
101
- sql: `INSERT INTO cap_versions (id, cap_usd, lock_threshold, registered_at, retired_at) VALUES (?, ?, ?, ?, NULL)`,
102
- args: [randomUUID(), nextCap, nextThreshold, now],
103
- });
104
- await withSharedWrite(_dbPath, () => client.batch(queries));
105
- }
106
-
107
- /** Set cap, persist as a new cap_versions row. Only ever called from the
108
- * (already-async) PUT /budget/cap route handler — awaited there so the
109
- * response only confirms ok:true once the write actually landed, same
110
- * durable-before-response guarantee the old synchronous fs.writeFileSync
111
- * gave for free. */
112
- export async function setCapAndPersist(usd) {
113
- if (typeof usd !== "number" || usd <= 0 || !Number.isFinite(usd)) return;
114
- configuredCapUSD = usd;
115
- await _writeCapVersion({ capUSD: usd });
116
- }
117
-
118
- export function currentCapUSD() { return configuredCapUSD; }
119
-
120
- /** Fraction of daily cap at which the gate auto-locks (0 < x ≤ 1). */
121
- export function currentLockThreshold() { return configuredLockThreshold; }
122
-
123
- /** Set gate-lock threshold, persist as a new cap_versions row. Same
124
- * await-before-respond contract as setCapAndPersist above. */
125
- export async function setLockThresholdAndPersist(fraction) {
126
- if (typeof fraction !== "number" || fraction <= 0 || fraction > 1 || !Number.isFinite(fraction)) return;
127
- configuredLockThreshold = fraction;
128
- await _writeCapVersion({ lockThreshold: fraction });
129
- }
130
-
131
- /** Full cap/threshold history, oldest first — e.g. for rendering the actual
132
- * cap in place at each point on a spend-over-time chart, rather than
133
- * assuming today's cap applied retroactively. */
134
- export async function getCapHistory() {
135
- const client = await _getClient();
136
- const { rows } = await client.execute(
137
- `SELECT cap_usd AS capUSD, lock_threshold AS lockThreshold, registered_at AS registeredAt, retired_at AS retiredAt
138
- FROM cap_versions ORDER BY registered_at ASC`
139
- );
140
- return rows;
141
- }
142
-
143
- /** Records one threshold crossing. Fire-and-forget from checkBudget — a
144
- * failed write here should never block the alert push itself. */
145
- async function _recordAlertEvent({ threshold, total, cap }) {
146
- const client = await _getClient();
147
- await withSharedWrite(_dbPath, () => client.execute({
148
- sql: `INSERT INTO budget_alert_events (id, threshold, total_usd, cap_usd, fired_at) VALUES (?, ?, ?, ?, ?)`,
149
- args: [randomUUID(), threshold, total, cap, Date.now()],
150
- }));
151
- }
152
-
153
- /** Threshold-crossing history, newest first — powers a cap-hit chart's
154
- * tap-to-detail (which threshold, what time), not just "was that day bad". */
155
- export async function getAlertHistory({ limit = 60 } = {}) {
156
- const client = await _getClient();
157
- const { rows } = await client.execute({
158
- sql: `SELECT threshold, total_usd AS totalUSD, cap_usd AS capUSD, fired_at AS firedAt
159
- FROM budget_alert_events ORDER BY fired_at DESC LIMIT ?`,
160
- args: [limit],
161
- });
162
- return rows;
163
- }
164
-
165
- function readState() {
166
- try {
167
- const raw = fs.readFileSync(STATE_FILE, "utf8");
168
- const j = JSON.parse(raw);
169
- if (j && typeof j === "object" && typeof j.day === "string" && Array.isArray(j.fired)) {
170
- return { day: j.day, fired: j.fired.filter((x) => typeof x === "number") };
171
- }
172
- } catch { /* fall through */ }
173
- return { day: null, fired: [] };
174
- }
175
-
176
- function writeState(next) {
177
- try {
178
- fs.writeFileSync(STATE_FILE, JSON.stringify(next), "utf8");
179
- } catch { /* best-effort */ }
180
- }
181
-
182
- const _GW_PORT = Number(process.env.OPENCLAW_GATEWAY_PORT) || 18789;
183
-
184
- async function _lockGate(logger) {
185
- const token = process.env.OPENCLAW_GATEWAY_TOKEN ?? "";
186
- try {
187
- await fetch(`http://127.0.0.1:${_GW_PORT}/manifold/budget/gate/lock`, {
188
- method: "POST",
189
- headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
190
- body: "{}",
191
- signal: AbortSignal.timeout(3000),
192
- });
193
- } catch (err) {
194
- logger?.warn?.(`lineage[budget-alert] gate lock failed: ${err?.message ?? err}`);
195
- }
196
- }
197
-
198
- async function _unlockGate(logger) {
199
- const token = process.env.OPENCLAW_GATEWAY_TOKEN ?? "";
200
- try {
201
- await fetch(`http://127.0.0.1:${_GW_PORT}/manifold/budget/gate/unlock`, {
202
- method: "POST",
203
- headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
204
- body: "{}",
205
- signal: AbortSignal.timeout(3000),
206
- });
207
- } catch (err) {
208
- logger?.warn?.(`lineage[budget-alert] gate unlock failed: ${err?.message ?? err}`);
209
- }
210
- }
211
-
212
- function localDayKey(d = new Date()) {
213
- // Local-time YYYY-MM-DD (the spend rolls over at local midnight, not UTC).
214
- const y = d.getFullYear();
215
- const m = String(d.getMonth() + 1).padStart(2, "0");
216
- const day = String(d.getDate()).padStart(2, "0");
217
- return `${y}-${m}-${day}`;
218
- }
219
-
220
- /**
221
- * Returns today's total spend in USD by summing cost_by_call rows whose
222
- * ts falls within the local-time day boundaries. Uses sqlite's localtime
223
- * modifier so the boundary is consistent regardless of timezone.
224
- */
225
- async function todaysTotalUSD(store) {
226
- const row = await execGet(store._client, `
227
- SELECT COALESCE(SUM(cost_total), 0) AS total
228
- FROM cost_by_call
229
- WHERE date(ts/1000, 'unixepoch', 'localtime') = date('now', 'localtime')
230
- `);
231
- return Number(row?.total ?? 0);
232
- }
233
-
234
- /**
235
- * Should be called after every successful cost insert. Cheap — one indexed
236
- * SELECT and an in-memory threshold check. No-op when already alerted for
237
- * every threshold today.
238
- *
239
- * Behavior:
240
- * - On a NEW local day with prior history, silently mark already-crossed
241
- * thresholds as fired (so a mid-day gateway restart doesn't retroactively
242
- * re-fire pushes for thresholds the operator already saw).
243
- * - For each threshold (50% / 80% / 100%), fire the alert at most once
244
- * per local day. Crossing two thresholds in one cost insert (rare) fires
245
- * both notifications.
246
- */
247
- export async function checkBudget({ store, logger, justWroteUsd = 0, notify }) {
248
- try {
249
- const today = localDayKey();
250
-
251
- // New-day rollover: reset fired list. To avoid spamming pushes after a
252
- // mid-day restart of an already-over-budget day, silently mark any
253
- // already-crossed thresholds as fired without dispatching.
254
- if (state.day !== today) {
255
- state = { day: today, fired: [] };
256
- const totalNow = await todaysTotalUSD(store);
257
- const fracNow = configuredCapUSD > 0 ? totalNow / configuredCapUSD : 0;
258
- for (const t of THRESHOLDS) if (fracNow >= t) state.fired.push(t);
259
- writeState(state);
260
- lastObservedTotal = totalNow;
261
- // New day: unlock the gate so pipelines run freely again
262
- _unlockGate(logger).catch(() => {});
263
- // Don't fire on the first call of a new day even if we crossed —
264
- // alerts only fire on transitions observed during this process.
265
- return;
266
- }
267
-
268
- if (state.fired.length === THRESHOLDS.length) return; // all fired today
269
-
270
- const total = await todaysTotalUSD(store);
271
- lastObservedTotal = total;
272
- const frac = configuredCapUSD > 0 ? total / configuredCapUSD : 0;
273
-
274
- for (const t of THRESHOLDS) {
275
- if (frac < t) continue;
276
- if (state.fired.includes(t)) continue;
277
- state.fired.push(t);
278
- writeState(state);
279
- _recordAlertEvent({ threshold: t, total, cap: configuredCapUSD })
280
- .catch((err) => logger?.warn?.(`lineage[budget-alert] record(${t}) error: ${err?.message ?? err}`));
281
- // Fire-and-forget so the hook isn't blocked on push round-trip.
282
- fireAlert({ threshold: t, total, cap: configuredCapUSD, store, logger, notify })
283
- .catch((err) => logger?.warn?.(`lineage[budget-alert] fire(${t}) error: ${err?.message ?? err}`));
284
- }
285
- } catch (err) {
286
- logger?.warn?.(`lineage[budget-alert]: ${err?.message ?? err}`);
287
- }
288
- }
289
-
290
- // ─── alert composition + dispatch ──────────────────────────────────────────
291
-
292
- async function fireAlert({ threshold, total, cap, store, logger, notify }) {
293
- const text = await composeText({ threshold, total, cap, store });
294
- const title = composeTitle({ threshold, total, cap });
295
- if (threshold >= configuredLockThreshold) {
296
- _lockGate(logger).catch(() => {});
297
- logger?.info?.(`lineage[budget-alert] approval gate locked (threshold=${threshold} >= lock=${configuredLockThreshold})`);
298
- }
299
- // The budget_alert_events row is written unconditionally, so the threshold
300
- // crossing is never lost even when no consumer is attached. Delivery
301
- // belongs to whoever subscribes (app bridge, notifier); manifold only
302
- // states the event.
303
- if (typeof notify === "function") {
304
- try {
305
- notify({ title, body: text.body, threshold, total, cap });
306
- logger?.info?.(`lineage[budget-alert] emitted manifold.budget.alert threshold=${threshold}`);
307
- return;
308
- } catch (err) {
309
- logger?.warn?.(`lineage[budget-alert] budget.alert emit failed: ${err?.message ?? err}`);
310
- }
311
- }
312
- logger?.warn?.(`lineage[budget-alert] no consumer attached — threshold=${threshold} alert not delivered`);
313
- }
314
-
315
- function composeTitle({ threshold }) {
316
- if (threshold >= 1.0) return "🚨 LLM budget exceeded";
317
- if (threshold >= 0.8) return "⚠️ LLM budget at 80%";
318
- if (threshold >= 0.5) return "📊 LLM budget at 50%";
319
- return "LLM budget alert";
320
- }
321
-
322
- async function composeText({ threshold, total, cap, store }) {
323
- const pct = Math.round((total / Math.max(cap, 0.0001)) * 100);
324
- const top = await topDriverToday(store);
325
- let body = `$${total.toFixed(2)} of $${cap.toFixed(2)} today (${pct}%)`;
326
- if (top) body += `\nTop: ${top.label} $${top.cost.toFixed(2)}`;
327
- return { body };
328
- }
329
-
330
- /** Highest-spending root in today's data — used to point the operator at
331
- * the driver without having to open the app first. Best-effort. */
332
- async function topDriverToday(store) {
333
- try {
334
- const row = await execGet(store._client, `
335
- SELECT root_label, root_kind, SUM(cost_total) AS cost
336
- FROM cost_by_call
337
- WHERE date(ts/1000, 'unixepoch', 'localtime') = date('now', 'localtime')
338
- AND root_label IS NOT NULL
339
- GROUP BY root_label, root_kind ORDER BY cost DESC LIMIT 1
340
- `);
341
- if (!row || !row.cost) return null;
342
- return { label: displayRootLabel(row.root_kind, row.root_label), cost: row.cost };
343
- } catch { return null; }
344
- }
345
-
346
- /** Fire a one-off alert through the normal event path, ignoring the
347
- * once-per-day debounce. For operator validation only — does not mutate the
348
- * persistent threshold-fired state. */
349
- export async function testFireAlert({ store, logger, threshold = 0.5, notify }) {
350
- const total = await todaysTotalUSD(store);
351
- await fireAlert({ threshold, total, cap: configuredCapUSD, store, logger, notify });
352
- }
353
-