@igorjpimenta/manifold 0.0.0-stage → 2026.11.22-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (210) hide show
  1. package/.migrations/ops/0000_futuristic_karma.sql +111 -0
  2. package/.migrations/ops/0001_low_maximus.sql +7 -0
  3. package/.migrations/ops/meta/0000_snapshot.json +763 -0
  4. package/.migrations/ops/meta/0001_snapshot.json +173 -0
  5. package/.migrations/ops/meta/_journal.json +20 -0
  6. package/.migrations/runtime/0000_real_boomer.sql +180 -0
  7. package/.migrations/runtime/0001_hot_dakota_north.sql +2 -0
  8. package/.migrations/runtime/0002_breezy_vertigo.sql +8 -0
  9. package/.migrations/runtime/0003_dashing_jack_flag.sql +5 -0
  10. package/.migrations/runtime/0004_panoramic_ego.sql +1 -0
  11. package/.migrations/runtime/0005_tidy_ben_grimm.sql +10 -0
  12. package/.migrations/runtime/0006_wet_nightmare.sql +57 -0
  13. package/.migrations/runtime/0007_ambiguous_chronomancer.sql +8 -0
  14. package/.migrations/runtime/0008_wealthy_thundra.sql +301 -0
  15. package/.migrations/runtime/0009_lying_shocker.sql +96 -0
  16. package/.migrations/runtime/0010_solid_phantom_reporter.sql +160 -0
  17. package/.migrations/runtime/0011_windy_demogoblin.sql +175 -0
  18. package/.migrations/runtime/0012_restore_harness_fks.sql +103 -0
  19. package/.migrations/runtime/meta/0000_snapshot.json +1181 -0
  20. package/.migrations/runtime/meta/0001_snapshot.json +1209 -0
  21. package/.migrations/runtime/meta/0002_snapshot.json +1261 -0
  22. package/.migrations/runtime/meta/0003_snapshot.json +1292 -0
  23. package/.migrations/runtime/meta/0004_snapshot.json +1240 -0
  24. package/.migrations/runtime/meta/0006_snapshot.json +1310 -0
  25. package/.migrations/runtime/meta/0007_snapshot.json +1362 -0
  26. package/.migrations/runtime/meta/0008_snapshot.json +1742 -0
  27. package/.migrations/runtime/meta/0009_snapshot.json +2413 -0
  28. package/.migrations/runtime/meta/0010_snapshot.json +2562 -0
  29. package/.migrations/runtime/meta/0011_snapshot.json +2812 -0
  30. package/.migrations/runtime/meta/0012_snapshot.json +2812 -0
  31. package/.migrations/runtime/meta/_journal.json +97 -0
  32. package/README.md +1583 -2
  33. package/RELEASING.md +89 -0
  34. package/agent/fleet.js +100 -0
  35. package/bin/cli.js +854 -0
  36. package/bin/manifold.js +4 -0
  37. package/boot/flow-revival.js +536 -0
  38. package/budget/index.js +495 -0
  39. package/budget/lib.js +59 -0
  40. package/dashboard/cost.js +528 -0
  41. package/dashboard/dist/assets/index-CUfp63sj.css +1 -0
  42. package/dashboard/dist/assets/index-Qy9JMpma.js +371 -0
  43. package/dashboard/dist/index.html +13 -0
  44. package/dashboard/event-log.js +267 -0
  45. package/dashboard/index.html +12 -0
  46. package/dashboard/package-lock.json +2877 -0
  47. package/dashboard/package.json +37 -0
  48. package/dashboard/pipeline-graph.js +131 -0
  49. package/dashboard/postcss.config.js +6 -0
  50. package/dashboard/search.js +183 -0
  51. package/dashboard/server.js +1015 -0
  52. package/dashboard/src/App.tsx +42 -0
  53. package/dashboard/src/api/client.ts +118 -0
  54. package/dashboard/src/api/types.ts +510 -0
  55. package/dashboard/src/api/ws.tsx +112 -0
  56. package/dashboard/src/components/CancelButton.tsx +63 -0
  57. package/dashboard/src/components/ClickToCopy.tsx +68 -0
  58. package/dashboard/src/components/CopyButton.tsx +61 -0
  59. package/dashboard/src/components/DailyCostTrend.tsx +85 -0
  60. package/dashboard/src/components/DailyTimeTrend.tsx +94 -0
  61. package/dashboard/src/components/Funnel.tsx +149 -0
  62. package/dashboard/src/components/GateForm.tsx +158 -0
  63. package/dashboard/src/components/GraphView.tsx +302 -0
  64. package/dashboard/src/components/GroupPathsSummary.tsx +47 -0
  65. package/dashboard/src/components/Histogram.tsx +292 -0
  66. package/dashboard/src/components/Layout.tsx +28 -0
  67. package/dashboard/src/components/NeedsAttention.tsx +98 -0
  68. package/dashboard/src/components/NodeContextCard.tsx +132 -0
  69. package/dashboard/src/components/PathTrail.tsx +35 -0
  70. package/dashboard/src/components/PipelineCostBreakdown.tsx +277 -0
  71. package/dashboard/src/components/RerunButton.tsx +46 -0
  72. package/dashboard/src/components/RunComparison.tsx +53 -0
  73. package/dashboard/src/components/Sparkline.tsx +94 -0
  74. package/dashboard/src/components/StatStrip.tsx +147 -0
  75. package/dashboard/src/components/StateTree.tsx +84 -0
  76. package/dashboard/src/components/StatusBadge.tsx +18 -0
  77. package/dashboard/src/components/SubflowPathPills.tsx +38 -0
  78. package/dashboard/src/components/SubflowSummary.tsx +24 -0
  79. package/dashboard/src/components/SubflowsCard.tsx +327 -0
  80. package/dashboard/src/components/TodayStrip.tsx +86 -0
  81. package/dashboard/src/components/TranscriptPane.tsx +99 -0
  82. package/dashboard/src/components/TranscriptRow.tsx +69 -0
  83. package/dashboard/src/components/ui/badge.tsx +38 -0
  84. package/dashboard/src/components/ui/button.tsx +28 -0
  85. package/dashboard/src/components/ui/card.tsx +42 -0
  86. package/dashboard/src/components/ui/skeleton.tsx +9 -0
  87. package/dashboard/src/components/ui/table.tsx +62 -0
  88. package/dashboard/src/index.css +26 -0
  89. package/dashboard/src/lib/anomaly.ts +42 -0
  90. package/dashboard/src/lib/format.ts +30 -0
  91. package/dashboard/src/lib/step-history.ts +13 -0
  92. package/dashboard/src/lib/subflow.ts +154 -0
  93. package/dashboard/src/lib/use-click-outside.ts +28 -0
  94. package/dashboard/src/lib/use-ignored-clusters.ts +67 -0
  95. package/dashboard/src/lib/utils.ts +6 -0
  96. package/dashboard/src/main.tsx +10 -0
  97. package/dashboard/src/types/shims.d.ts +4 -0
  98. package/dashboard/src/views/Home.tsx +478 -0
  99. package/dashboard/src/views/PipelineDetail.tsx +1185 -0
  100. package/dashboard/src/views/PipelinesList.tsx +111 -0
  101. package/dashboard/src/views/RunDetail.tsx +248 -0
  102. package/dashboard/src/views/Search.tsx +196 -0
  103. package/dashboard/stats.js +668 -0
  104. package/dashboard/tailwind.config.ts +43 -0
  105. package/dashboard/tree-stats.js +12 -0
  106. package/dashboard/tsconfig.json +25 -0
  107. package/dashboard/views.js +231 -0
  108. package/dashboard/vite.config.ts +34 -0
  109. package/drizzle.config.js +12 -0
  110. package/drizzle.config.ops.js +12 -0
  111. package/elevation.js +116 -0
  112. package/engine/admission-promotion-resume.js +127 -0
  113. package/engine/agent-dispatch.js +64 -0
  114. package/engine/child-resume-trigger.js +315 -0
  115. package/engine/executor-budget.js +87 -0
  116. package/engine/executor-cancel.js +83 -0
  117. package/engine/executor-errors.js +122 -0
  118. package/engine/executor-helpers.js +254 -0
  119. package/engine/executor-nodes.js +292 -0
  120. package/engine/executor-persist.js +212 -0
  121. package/engine/executor-subflow-children.js +275 -0
  122. package/engine/executor.js +1013 -0
  123. package/engine/global-concurrency.js +33 -0
  124. package/engine/loader.js +528 -0
  125. package/engine/needs-resolver.js +229 -0
  126. package/engine/pipelines-watcher.js +133 -0
  127. package/engine/result-replay.js +34 -0
  128. package/engine/revival-messages.js +11 -0
  129. package/engine/spawn-child.js +55 -0
  130. package/engine/wait-for-flow-settlement.js +109 -0
  131. package/events/catalog.js +26 -0
  132. package/events/emit.js +44 -0
  133. package/events/kinds.js +118 -0
  134. package/events/serve-ws.js +74 -0
  135. package/events/stream.js +35 -0
  136. package/events/ws-dialog.js +257 -0
  137. package/events/ws-frame.js +194 -0
  138. package/events/ws-run-events.js +70 -0
  139. package/events.js +32 -0
  140. package/gates/gate-resolver.js +576 -0
  141. package/gates/paused-flows.js +55 -0
  142. package/graphql/columns.js +67 -0
  143. package/graphql/schema.js +333 -0
  144. package/index.js +49 -0
  145. package/mcp/http.js +159 -0
  146. package/mcp/identity.js +94 -0
  147. package/mcp/stdio.js +80 -0
  148. package/mcp/tools.js +245 -0
  149. package/mcp/wire-ops.js +178 -0
  150. package/package.json +49 -4
  151. package/routes/bearer-auth.js +57 -0
  152. package/routes/discuss-audio.js +87 -0
  153. package/routes/discuss-component.js +39 -0
  154. package/routes/discuss-draft.js +159 -0
  155. package/routes/discuss-helpers.js +20 -0
  156. package/routes/discuss.js +848 -0
  157. package/routes/failures.js +104 -0
  158. package/routes/graphql.js +61 -0
  159. package/routes/health.js +54 -0
  160. package/routes/operator-discuss.js +193 -0
  161. package/routes/operator-resume.js +32 -0
  162. package/routes/operator-retry.js +301 -0
  163. package/routes/operator.js +539 -0
  164. package/routes/pipelines.js +118 -0
  165. package/routes/runs.js +1129 -0
  166. package/scripts/ci/import-check.mjs +50 -0
  167. package/scripts/link-core-self.mjs +31 -0
  168. package/scripts/with-guard.sh +24 -0
  169. package/server/app.js +503 -0
  170. package/server/registry.js +53 -0
  171. package/server/serve.js +163 -0
  172. package/state/admission-queue-store.js +325 -0
  173. package/state/attribution-registries.js +102 -0
  174. package/state/batch-writer.js +162 -0
  175. package/state/db-guard.js +63 -0
  176. package/state/db-health.js +27 -0
  177. package/state/db.js +122 -0
  178. package/state/discuss-store.js +299 -0
  179. package/state/failure-store.js +427 -0
  180. package/state/migrate.js +104 -0
  181. package/state/node-cost.js +67 -0
  182. package/state/node-executions.js +775 -0
  183. package/state/node-health-store.js +124 -0
  184. package/state/ops-merge.js +93 -0
  185. package/state/paths.js +146 -0
  186. package/state/pipeline-dirs.js +99 -0
  187. package/state/pipeline-registry.js +449 -0
  188. package/state/run-store.js +697 -0
  189. package/state/schema.js +438 -0
  190. package/state/schema.ops.js +44 -0
  191. package/state/session-index.js +177 -0
  192. package/state/session-keys.js +39 -0
  193. package/state/state-store.js +311 -0
  194. package/state/subflow-state.js +126 -0
  195. package/state/tool-handoff.js +169 -0
  196. package/telemetry/budget-alert.js +353 -0
  197. package/telemetry/cli.js +331 -0
  198. package/telemetry/grammar.js +119 -0
  199. package/telemetry/http-budget.js +132 -0
  200. package/telemetry/http-cost.js +670 -0
  201. package/telemetry/http-helpers.js +134 -0
  202. package/telemetry/http-tree.js +349 -0
  203. package/telemetry/http.js +44 -0
  204. package/telemetry/index.js +307 -0
  205. package/telemetry/ingest.js +134 -0
  206. package/telemetry/labels.js +115 -0
  207. package/telemetry/pricer.js +77 -0
  208. package/telemetry/resolver.js +314 -0
  209. package/telemetry/store.js +147 -0
  210. package/utils/run-patch.js +43 -0
@@ -0,0 +1,495 @@
1
+ // budget — approval queue module absorbed into manifold.
2
+ //
3
+ // Owns gate_state and budget alerts in the store home's ops.sqlite; the
4
+ // approval queue lives in the sibling runtime.sqlite. Registers HTTP
5
+ // routes under /manifold/budget/*. Sets api.runtime.approvalQueue with the
6
+ // async API that executor.js calls per-node (isGateLocked,
7
+ // lookupNodeDecision, enqueueNodeApproval).
8
+
9
+ import { mkdirSync } from "node:fs";
10
+ import { getSharedDb, withSharedWrite, resolveStorePaths } from "../state/db.js";
11
+ import { createEmitter } from "../events/emit.js";
12
+ import { randomUUID } from "node:crypto";
13
+ import path from "node:path";
14
+ import os from "node:os";
15
+ import { deserialize, isGateLocked, resolveNodeDecision, classifyDecisionStatus, todayLocal } from "./lib.js";
16
+ import { migrateDatabase, OPS_MIGRATIONS_FOLDER, RUNTIME_MIGRATIONS_FOLDER } from "../state/migrate.js";
17
+ import { ensureSessionKeys } from "../state/session-keys.js";
18
+ import { ensurePipelineNodes } from "../state/attribution-registries.js";
19
+
20
+ const STORE_DIR = path.join(os.homedir(), ".openclaw");
21
+ const DB_PATH = path.join(STORE_DIR, "ops.sqlite");
22
+
23
+ // queue_items lives in runtime.sqlite beside ops.sqlite. Alternate dbPaths
24
+ // (tests, one-off stores) get their own sibling so two fixtures never share
25
+ // a /tmp/runtime.sqlite.
26
+ function runtimeSibling(dbPath) {
27
+ return path.basename(dbPath) === "ops.sqlite"
28
+ ? path.join(path.dirname(dbPath), "runtime.sqlite")
29
+ : `${dbPath}.runtime.sqlite`;
30
+ }
31
+
32
+ const MOCK_ITEMS = [
33
+ {
34
+ label: "SCORE × 12 listings",
35
+ agent: "scorer",
36
+ agentEmoji: "⚖️",
37
+ trigger: "cron",
38
+ pipeline: "workana-proposal",
39
+ node: "score",
40
+ tokenEstimate: 10800,
41
+ costLow: 0.0018,
42
+ costHigh: 0.0054,
43
+ brief: "Score 12 new Workana listings against PROFILE.md rubric (python, typescript, api, automation categories)",
44
+ modelChain: ["zai/glm-4.5-flash", "openrouter/deepseek/deepseek-v4-flash"],
45
+ },
46
+ {
47
+ label: "DRAFT × 3 listings",
48
+ agent: "content-writer",
49
+ agentEmoji: "✍️",
50
+ trigger: "cron",
51
+ pipeline: "workana-proposal",
52
+ node: "propose.draft",
53
+ tokenEstimate: 33000,
54
+ costLow: 0.067,
55
+ costHigh: 0.2,
56
+ brief: "Draft proposals for: sistema-de-automacao-rpa-python, api-integracao-whatsapp-crm, dashboard-analytics-react-typescript",
57
+ modelChain: ["zai/glm-5.1", "openrouter/deepseek/deepseek-v4-pro"],
58
+ },
59
+ {
60
+ label: "SUBMIT × 1 proposal",
61
+ agent: "proposal-sender",
62
+ agentEmoji: "📤",
63
+ trigger: "cron",
64
+ pipeline: "workana-proposal",
65
+ node: "submit",
66
+ tokenEstimate: 3200,
67
+ costLow: 0.0005,
68
+ costHigh: 0.0016,
69
+ brief: "Submit approved draft for: sistema-de-automacao-rpa-python (score 87, budget R$2.400)",
70
+ modelChain: ["zai/glm-4.5-flash", "openrouter/deepseek/deepseek-v4-flash"],
71
+ },
72
+ ];
73
+
74
+ // getSharedDb() instead of a fresh openDb() call: on the first call ever for
75
+ // this dbPath, this does the real work (open + migrate). On every call
76
+ // after — including a brand-new registerBudgetRoutes closure created by a
77
+ // gateway plugin reload — it returns the SAME already-open connection
78
+ // straight from db.js's process-wide cache, instantly. That shared cached
79
+ // connection is what closes the "budget store not attached yet" race: a
80
+ // fresh connection per reload opens a genuine "not ready" window each time
81
+ // by construction (see ensureClient's own comment below for the hazard).
82
+ // Snapshots are idempotent no-ops once applied, so re-running openStore
83
+ // against an already-migrated cached connection on a later reload costs
84
+ // one quick journal SELECT, not a meaningful delay.
85
+ // Exported so tests can exercise the real getSharedDb-backed path against a
86
+ // tmp dbPath (see test/unit/budget/index.test.js's "real openStore()"
87
+ // suite) instead of only ever testing through a fully-fake openStore DI
88
+ // override that bypasses this function — and by extension getSharedDb —
89
+ // entirely.
90
+ export async function openStore({ dbPath = DB_PATH, runtimeDbPath = runtimeSibling(dbPath) } = {}) {
91
+ mkdirSync(path.dirname(dbPath), { recursive: true });
92
+ await migrateDatabase({ dbPath, migrationsFolder: OPS_MIGRATIONS_FOLDER });
93
+ const { client } = await getSharedDb(dbPath);
94
+ await client.execute("PRAGMA foreign_keys = ON");
95
+ // The singleton row is runtime state, not schema: every gate_state reader
96
+ // assumes it exists, so seed it unconditionally on every open.
97
+ await client.execute(`INSERT OR IGNORE INTO gate_state (id, locked) VALUES ('singleton', 0)`);
98
+ // queue_items lives in runtime.sqlite; gate_state and the alert tables stay
99
+ // in ops. The extra handle rides on the ops client so callers that only
100
+ // inject one fake still work.
101
+ await migrateDatabase({ dbPath: runtimeDbPath, migrationsFolder: RUNTIME_MIGRATIONS_FOLDER });
102
+ const { client: runtimeClient } = await getSharedDb(runtimeDbPath);
103
+ client.runtime = runtimeClient;
104
+ client.runtimePath = runtimeDbPath;
105
+ return client;
106
+ }
107
+
108
+ async function readBody(req) {
109
+ let raw = "";
110
+ for await (const chunk of req) raw += chunk;
111
+ try { return raw ? JSON.parse(raw) : {}; }
112
+ catch { return null; }
113
+ }
114
+
115
+ const _GW_PORT = Number(process.env.OPENCLAW_GATEWAY_PORT) || 18789;
116
+
117
+ async function _loopback(path, body, extraHeaders = {}) {
118
+ const token = process.env.OPENCLAW_GATEWAY_TOKEN ?? "";
119
+ try {
120
+ await fetch(`http://127.0.0.1:${_GW_PORT}${path}`, {
121
+ method: "POST",
122
+ headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, ...extraHeaders },
123
+ body: JSON.stringify(body ?? {}),
124
+ signal: AbortSignal.timeout(8000),
125
+ });
126
+ } catch { /* best-effort fire-and-forget */ }
127
+ }
128
+
129
+ export function registerBudgetRoutes(api, { getBroadcast, openStore: openStoreFn = openStore, onFlowResume } = {}) {
130
+ // Canonical-kind fan-out for the three WS emissions below: canonical
131
+ // wire names, envelope adds kind.
132
+ // onFlowResume({flowId, nodeId, decision}) resumes a flow-bound approval
133
+ // in-process (routes/operator-resume.js, injected by index.js) — no HTTP
134
+ // loopback. Absent (tests), per-node resume is skipped but the decision
135
+ // still commits and broadcasts.
136
+ const { emit } = createEmitter({ getBroadcast, logger: api.logger });
137
+ let runtimeDbPath = runtimeSibling(DB_PATH);
138
+ let client;
139
+ let runtimeClient;
140
+ let clientPromise = null;
141
+
142
+ // `client` must NOT be attached only from registerService's start() — a
143
+ // single external lifecycle hook fired once per registerBudgetRoutes call.
144
+ // When the gateway's plugin set reloads twice in quick succession, the
145
+ // LAST registerBudgetRoutes call's `api.runtime.approvalQueue` closure can
146
+ // end up the one left installed while an EARLIER generation's start() is
147
+ // the one that actually ran — or neither generation's start() runs for the
148
+ // closure left live. That closure's `client` would then stay undefined
149
+ // forever and isGateLocked() would throw on every call in that state,
150
+ // parking the calling node on a wait that nothing ever woke.
151
+ //
152
+ // So: any call into THIS closure's own isGateLocked() attaches the client
153
+ // itself, on demand, the first time it's actually needed, and every caller
154
+ // simply awaits
155
+ // that attachment instead of throwing and needing something else to wake
156
+ // it later. Self-healing regardless of whether this generation's
157
+ // registerService start() ever fires, and getSharedDb (state/db.js) means
158
+ // a LATER generation's ensureClient() resolves near-instantly from the
159
+ // cached connection rather than paying a fresh open+migrate every time.
160
+ function ensureClient() {
161
+ if (client) return Promise.resolve(client);
162
+ if (!clientPromise) {
163
+ clientPromise = openStoreFn()
164
+ .then((c) => {
165
+ client = c;
166
+ runtimeClient = c.runtime ?? c;
167
+ runtimeDbPath = c.runtimePath ?? runtimeDbPath;
168
+ api.logger.info("[manifold/budget] store ready at " + (c.dbPath ?? DB_PATH));
169
+ return c;
170
+ })
171
+ .catch((err) => {
172
+ api.logger.warn(`[manifold/budget] store attach failed: ${err?.message ?? err}`);
173
+ clientPromise = null; // allow a later call to retry instead of wedging on a permanently-rejected promise
174
+ throw err;
175
+ });
176
+ }
177
+ return clientPromise;
178
+ }
179
+
180
+ api.registerService({
181
+ id: "manifold-budget-store",
182
+ start: async () => { await ensureClient(); },
183
+ stop: async () => {
184
+ try { client?.close?.(); } catch { /* ignore */ }
185
+ },
186
+ });
187
+
188
+ // GET /manifold/budget/pending
189
+ api.registerHttpRoute({
190
+ path: "/manifold/budget/pending",
191
+ auth: "plugin",
192
+ match: "exact",
193
+ handler: async (req, res) => {
194
+ if (req.method !== "GET") { res.statusCode = 405; res.end("method not allowed"); return true; }
195
+ const { rows } = await runtimeClient.execute(
196
+ "SELECT * FROM queue_items WHERE status='pending' ORDER BY created_at ASC"
197
+ );
198
+ res.statusCode = 200;
199
+ res.setHeader("Content-Type", "application/json");
200
+ res.end(JSON.stringify({ items: rows.map(deserialize) }));
201
+ return true;
202
+ },
203
+ });
204
+
205
+ // POST /manifold/budget/decide { ids: string[], decision: "approve"|"reject" }
206
+ api.registerHttpRoute({
207
+ path: "/manifold/budget/decide",
208
+ auth: "plugin",
209
+ match: "exact",
210
+ handler: async (req, res) => {
211
+ if (req.method !== "POST") { res.statusCode = 405; res.end("method not allowed"); return true; }
212
+ const body = await readBody(req);
213
+ if (!body) { res.statusCode = 400; res.end("invalid json"); return true; }
214
+ const { ids, decision } = body;
215
+ if (!Array.isArray(ids) || !ids.length || !["approve", "reject"].includes(decision)) {
216
+ res.statusCode = 400;
217
+ res.end(JSON.stringify({ error: "ids (string[]) and decision (approve|reject) required" }));
218
+ return true;
219
+ }
220
+ const now = Date.now();
221
+ let updated = 0;
222
+ const updatedIds = [];
223
+ for (const id of ids) {
224
+ const r = await withSharedWrite(runtimeDbPath, () => runtimeClient.execute({
225
+ sql: "UPDATE queue_items SET status=?, decided_at=?, decision=? WHERE id=? AND status='pending'",
226
+ args: [classifyDecisionStatus(decision), now, decision, id],
227
+ }));
228
+ if (r.rowsAffected) { updated++; updatedIds.push(id); }
229
+ }
230
+ if (updatedIds.length > 0) {
231
+ const ph = updatedIds.map(() => "?").join(",");
232
+ const { rows: decided } = await runtimeClient.execute({
233
+ sql: `SELECT id, pipeline_id, pipeline_args, flow_id, node, enqueuer_session_key FROM queue_items WHERE id IN (${ph})`,
234
+ args: updatedIds,
235
+ });
236
+ for (const row of decided) {
237
+ if (row.flow_id) {
238
+ // In-process resume (routes/operator-resume.js, injected). The
239
+ // start-gate branch below still loops back: elevation needs
240
+ // gateway scope.
241
+ if (onFlowResume) {
242
+ try { await onFlowResume({ flowId: row.flow_id, nodeId: row.node, decision }); } catch {}
243
+ }
244
+ api.logger.info(`[manifold/budget] ${decision} per-node — resuming flow=${String(row.flow_id).slice(0,8)} node=${row.node}`);
245
+ } else if (decision === "approve" && row.pipeline_id) {
246
+ _loopback(
247
+ "/manifold/_elev/start",
248
+ {
249
+ pipelineId: row.pipeline_id,
250
+ args: row.pipeline_args ? JSON.parse(row.pipeline_args) : {},
251
+ via: "approval",
252
+ ...(row.enqueuer_session_key ? { parentSessionKey: row.enqueuer_session_key } : {}),
253
+ },
254
+ { "x-skip-gate": "1" }
255
+ );
256
+ api.logger.info(`[manifold/budget] approve start-gate — firing pipeline=${row.pipeline_id}`);
257
+ }
258
+ }
259
+ }
260
+ api.logger.info(`[manifold/budget] ${decision}: ${updated}/${ids.length}`);
261
+ if (updated > 0) {
262
+ try { await emit("budget.queue.updated", { decision, count: updated }); } catch {}
263
+ }
264
+ res.statusCode = 200;
265
+ res.setHeader("Content-Type", "application/json");
266
+ res.end(JSON.stringify({ ok: true, updated, decision }));
267
+ return true;
268
+ },
269
+ });
270
+
271
+ // GET /manifold/budget/history
272
+ api.registerHttpRoute({
273
+ path: "/manifold/budget/history",
274
+ auth: "plugin",
275
+ match: "exact",
276
+ handler: async (req, res) => {
277
+ if (req.method !== "GET") { res.statusCode = 405; res.end("method not allowed"); return true; }
278
+ const { rows } = await runtimeClient.execute(
279
+ "SELECT * FROM queue_items WHERE status != 'pending' ORDER BY decided_at DESC LIMIT 50"
280
+ );
281
+ res.statusCode = 200;
282
+ res.setHeader("Content-Type", "application/json");
283
+ res.end(JSON.stringify({ items: rows.map(deserialize) }));
284
+ return true;
285
+ },
286
+ });
287
+
288
+ // POST /manifold/budget/mock { count?: number }
289
+ api.registerHttpRoute({
290
+ path: "/manifold/budget/mock",
291
+ auth: "plugin",
292
+ match: "exact",
293
+ handler: async (req, res) => {
294
+ if (req.method !== "POST") { res.statusCode = 405; res.end("method not allowed"); return true; }
295
+ const body = (await readBody(req)) ?? {};
296
+ const count = Math.min(Number(body.count ?? MOCK_ITEMS.length), MOCK_ITEMS.length);
297
+ const now = Date.now();
298
+ const created = [];
299
+ for (let i = 0; i < count; i++) {
300
+ const t = MOCK_ITEMS[i];
301
+ const id = randomUUID();
302
+ await withSharedWrite(runtimeDbPath, () => runtimeClient.execute({
303
+ sql: `INSERT OR REPLACE INTO queue_items
304
+ (id, label, agent, agent_emoji, trigger, cost_low, cost_high, brief, model_chain, pipeline, node, token_estimate, kind, status, created_at)
305
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'node', 'pending', ?)`,
306
+ args: [id, t.label, t.agent, t.agentEmoji, t.trigger,
307
+ t.costLow, t.costHigh, t.brief, JSON.stringify(t.modelChain),
308
+ t.pipeline ?? null, t.node ?? null, t.tokenEstimate ?? null,
309
+ now],
310
+ }));
311
+ created.push(id);
312
+ }
313
+ res.statusCode = 201;
314
+ res.setHeader("Content-Type", "application/json");
315
+ res.end(JSON.stringify({ ok: true, created }));
316
+ return true;
317
+ },
318
+ });
319
+
320
+ // GET /manifold/budget/gate
321
+ api.registerHttpRoute({
322
+ path: "/manifold/budget/gate",
323
+ auth: "plugin",
324
+ match: "exact",
325
+ handler: async (req, res) => {
326
+ if (req.method !== "GET") { res.statusCode = 405; res.end("method not allowed"); return true; }
327
+ const { rows } = await client.execute("SELECT locked, day FROM gate_state WHERE id='singleton'");
328
+ const row = rows[0] ?? null;
329
+ const today = todayLocal();
330
+ if (row?.locked && row?.day && row.day < today) {
331
+ await withSharedWrite(DB_PATH, () => client.execute({ sql: "UPDATE gate_state SET locked=0, day=? WHERE id='singleton'", args: [today] }));
332
+ api.logger.info(`[manifold/budget] gate auto-unlocked on stale day rollover (was ${row.day})`);
333
+ }
334
+ const locked = isGateLocked(row, today);
335
+ res.statusCode = 200;
336
+ res.setHeader("Content-Type", "application/json");
337
+ res.end(JSON.stringify({ locked, day: row?.day ?? null }));
338
+ return true;
339
+ },
340
+ });
341
+
342
+ // POST /manifold/budget/gate/lock
343
+ api.registerHttpRoute({
344
+ path: "/manifold/budget/gate/lock",
345
+ auth: "plugin",
346
+ match: "exact",
347
+ handler: async (req, res) => {
348
+ if (req.method !== "POST") { res.statusCode = 405; res.end("method not allowed"); return true; }
349
+ const today = todayLocal();
350
+ await withSharedWrite(DB_PATH, () => client.execute({ sql: "UPDATE gate_state SET locked=1, day=? WHERE id='singleton'", args: [today] }));
351
+ api.logger.info(`[manifold/budget] gate locked day=${today}`);
352
+ try { await emit("budget.lock.changed", { locked: true, day: today }); } catch {}
353
+ res.statusCode = 200;
354
+ res.setHeader("Content-Type", "application/json");
355
+ res.end(JSON.stringify({ ok: true, locked: true, day: today }));
356
+ return true;
357
+ },
358
+ });
359
+
360
+ // POST /manifold/budget/gate/unlock
361
+ api.registerHttpRoute({
362
+ path: "/manifold/budget/gate/unlock",
363
+ auth: "plugin",
364
+ match: "exact",
365
+ handler: async (req, res) => {
366
+ if (req.method !== "POST") { res.statusCode = 405; res.end("method not allowed"); return true; }
367
+ const today = todayLocal();
368
+ await withSharedWrite(DB_PATH, () => client.execute({ sql: "UPDATE gate_state SET locked=0, day=? WHERE id='singleton'", args: [today] }));
369
+ api.logger.info(`[manifold/budget] gate unlocked day=${today}`);
370
+ try { await emit("budget.lock.changed", { locked: false, day: today }); } catch {}
371
+ res.statusCode = 200;
372
+ res.setHeader("Content-Type", "application/json");
373
+ res.end(JSON.stringify({ ok: true, locked: false, day: today }));
374
+ return true;
375
+ },
376
+ });
377
+
378
+ // POST /manifold/budget/items — programmatic item creation (called by manifold gate)
379
+ api.registerHttpRoute({
380
+ path: "/manifold/budget/items",
381
+ auth: "gateway",
382
+ match: "exact",
383
+ handler: async (req, res) => {
384
+ if (req.method !== "POST") { res.statusCode = 405; res.end("method not allowed"); return true; }
385
+ const body = await readBody(req);
386
+ if (!body) { res.statusCode = 400; res.end("invalid json"); return true; }
387
+ const { label, agent = "manifold", agentEmoji = "🔁", trigger = "manual",
388
+ pipelineId, pipelineArgs, brief, costLow = 0, costHigh = 0,
389
+ modelChain = [], tokenEstimate = null, pipeline = null, node = null,
390
+ enqueuerSessionKey = null } = body;
391
+ if (!label || !pipelineId) {
392
+ res.statusCode = 400;
393
+ res.setHeader("Content-Type", "application/json");
394
+ res.end(JSON.stringify({ ok: false, error: "label and pipelineId required" }));
395
+ return true;
396
+ }
397
+ const id = randomUUID();
398
+ const now = Date.now();
399
+ await withSharedWrite(runtimeDbPath, async () => {
400
+ await ensureSessionKeys(runtimeClient, [enqueuerSessionKey]);
401
+ await ensurePipelineNodes(runtimeClient, [[pipelineId, node ?? pipeline ?? null]]);
402
+ return runtimeClient.execute({
403
+ sql: `INSERT INTO queue_items
404
+ (id, label, agent, agent_emoji, trigger, cost_low, cost_high, brief, model_chain,
405
+ pipeline, node, token_estimate, pipeline_id, flow_id, enqueuer_session_key, kind, status, created_at)
406
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?)`,
407
+ args: [id, label, agent, agentEmoji, trigger, costLow, costHigh, brief ?? null,
408
+ JSON.stringify(modelChain), pipeline ?? pipelineId, node ?? null,
409
+ tokenEstimate ?? null, pipelineId, JSON.stringify(pipelineArgs ?? {}),
410
+ enqueuerSessionKey ?? null, now],
411
+ });
412
+ });
413
+ try {
414
+ await emit("approval.requested", { id });
415
+ } catch {}
416
+ api.logger.info(`[manifold/budget] queued pipeline=${pipelineId} item=${id}`);
417
+ res.statusCode = 201;
418
+ res.setHeader("Content-Type", "application/json");
419
+ res.end(JSON.stringify({ ok: true, itemId: id }));
420
+ return true;
421
+ },
422
+ });
423
+
424
+ // ── Runtime API: async libsql reads + writes ──────────────────────────────
425
+ // executor.js calls these per-node (isGateLocked, lookupNodeDecision,
426
+ // enqueueNodeApproval) without an HTTP hop.
427
+ api.runtime = api.runtime ?? {};
428
+ api.runtime.approvalQueue = {
429
+ async isGateLocked() {
430
+ // Awaits attachment in place instead of throwing when the client
431
+ // isn't attached yet — a cold store on the first call after a reload
432
+ // is no longer a special "transient wait" case the caller has to
433
+ // park on and something else has to remember to wake; it's just a
434
+ // normal await, same as run-store.js's Proxy factory already does
435
+ // for every other store in this codebase. getSharedDb (state/db.js)
436
+ // is what makes this cheap: a later generation's attach resolves
437
+ // near-instantly from the cached connection.
438
+ //
439
+ // A genuine attach FAILURE (not "still attaching" — that case never
440
+ // rejects, it just takes longer) is deliberately NOT caught here: it
441
+ // propagates and fails this node closed, same as any other thrown
442
+ // error reaching maybeBudgetGate. Treating "the store is broken, we
443
+ // can't tell if spend is locked" as "not locked" would let arbitrarily
444
+ // expensive nodes dispatch with zero budget control — the one case
445
+ // this function must never fail open on.
446
+ const c = await ensureClient();
447
+ try {
448
+ return await withSharedWrite(DB_PATH, async () => {
449
+ const { rows } = await c.execute("SELECT locked, day FROM gate_state WHERE id='singleton'");
450
+ const row = rows[0] ?? null;
451
+ const today = todayLocal();
452
+ if (row?.locked && row?.day && row.day < today) {
453
+ await c.execute({ sql: "UPDATE gate_state SET locked=0, day=? WHERE id='singleton'", args: [today] });
454
+ return false;
455
+ }
456
+ return isGateLocked(row, today);
457
+ });
458
+ } catch { return false; }
459
+ },
460
+ async lookupNodeDecision({ flowId, nodeId }) {
461
+ if (!flowId || !nodeId) return null;
462
+ const { rows } = await runtimeClient.execute({
463
+ sql: `SELECT status, decision FROM queue_items WHERE flow_id=? AND node=?
464
+ ORDER BY created_at DESC LIMIT 1`,
465
+ args: [flowId, nodeId],
466
+ });
467
+ return resolveNodeDecision(rows[0] ?? null);
468
+ },
469
+ async enqueueNodeApproval({ flowId, nodeId, pipeline, pipelineId, label, agent, agentEmoji,
470
+ trigger = "cron", brief, costLow = 0, costHigh = 0,
471
+ modelChain = [], tokenEstimate = null, enqueuerSessionKey = null, kind = "node" }) {
472
+ const id = randomUUID();
473
+ await withSharedWrite(runtimeDbPath, async () => {
474
+ await ensureSessionKeys(runtimeClient, [enqueuerSessionKey]);
475
+ await ensurePipelineNodes(runtimeClient, [[pipelineId, nodeId]]);
476
+ return runtimeClient.execute({
477
+ sql: `INSERT INTO queue_items
478
+ (id, label, agent, agent_emoji, trigger, cost_low, cost_high, brief, model_chain,
479
+ pipeline, node, token_estimate, pipeline_id, flow_id, enqueuer_session_key, kind, status, created_at)
480
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'pending', ?)`,
481
+ args: [id, label, agent ?? "manifold", agentEmoji ?? "🔁", trigger,
482
+ costLow, costHigh, brief ?? null, JSON.stringify(modelChain ?? []),
483
+ pipeline ?? pipelineId ?? null, nodeId, tokenEstimate ?? null,
484
+ pipelineId ?? null, flowId, enqueuerSessionKey ?? null, kind, Date.now()],
485
+ });
486
+ });
487
+ try {
488
+ await emit("approval.requested", { id, flowId: flowId ?? null });
489
+ } catch {}
490
+ return id;
491
+ },
492
+ };
493
+
494
+ api.logger.info("[manifold/budget] routes registered at /manifold/budget/*");
495
+ }
package/budget/lib.js ADDED
@@ -0,0 +1,59 @@
1
+ // Pure logic extracted from budget/index.js for unit testing.
2
+ // No openclaw SDK or SQLite imports — safe to require in Node --test.
3
+
4
+ export function deserialize(row) {
5
+ return {
6
+ id: row.id,
7
+ label: row.label,
8
+ agent: row.agent,
9
+ agentEmoji: row.agent_emoji,
10
+ trigger: row.trigger,
11
+ costLow: row.cost_low,
12
+ costHigh: row.cost_high,
13
+ brief: row.brief ?? null,
14
+ modelChain: row.model_chain ? JSON.parse(row.model_chain) : [],
15
+ pipeline: row.pipeline ?? null,
16
+ node: row.node ?? null,
17
+ tokenEstimate: row.token_estimate ?? null,
18
+ status: row.status,
19
+ createdAt: row.created_at,
20
+ expiresAt: row.expires_at ?? null,
21
+ decidedAt: row.decided_at ?? null,
22
+ decision: row.decision ?? null,
23
+ pipelineId: row.pipeline_id ?? null,
24
+ pipelineArgs: row.pipeline_args ? JSON.parse(row.pipeline_args) : null,
25
+ flowId: row.flow_id ?? null,
26
+ kind: row.kind ?? "node",
27
+ };
28
+ }
29
+
30
+ // Canonical day source for the budget gate. Local, not UTC: the gate is a
31
+ // human control, so an operator locking spend at 23:30 means locked through
32
+ // their own midnight.
33
+ //
34
+ // Every gate_state read and write must derive the day from here. A reader on
35
+ // a different clock than the writer fails open, and the two only diverge
36
+ // during part of the day, so the mismatch is invisible to a daytime test run.
37
+ export function todayLocal(now = new Date()) {
38
+ return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}`;
39
+ }
40
+
41
+ export function isGateLocked(row, nowIso) {
42
+ if (!row) return false;
43
+ const today = nowIso ?? todayLocal();
44
+ return Boolean(row.locked && row.day >= today);
45
+ }
46
+
47
+ export function resolveNodeDecision(row) {
48
+ if (!row) return null;
49
+ if (row.status === "pending") return "pending";
50
+ if (row.decision === "approve") return "approve";
51
+ if (row.decision === "reject") return "reject";
52
+ return null;
53
+ }
54
+
55
+ export function classifyDecisionStatus(decision) {
56
+ if (decision === "approve") return "approved";
57
+ if (decision === "reject") return "rejected";
58
+ return null;
59
+ }