@verax-ai/body 0.7.0 → 0.8.0

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.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The approvals row on disk is not signed. The terminal, the operator door and
3
+ * the ledger API show it, while a running body approves its own copy. Each of
4
+ * them holds the row to the hashes the signed defer names, here, so a row
5
+ * edited on disk is never what an operator is asked to approve.
6
+ */
7
+ import { type ApprovalRow } from "@verax-ai/proxy";
8
+ /** Reads the decision and inputs files once; the returned check is per row. */
9
+ export declare function boundOnDisk(stateDir: string): (row: ApprovalRow) => boolean;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * The approvals row on disk is not signed. The terminal, the operator door and
3
+ * the ledger API show it, while a running body approves its own copy. Each of
4
+ * them holds the row to the hashes the signed defer names, here, so a row
5
+ * edited on disk is never what an operator is asked to approve.
6
+ */
7
+ import { approvalArgsBound, loadDecisionRequestHashesFromDir, loadSignedForwardedHashesFromDir, } from "@verax-ai/proxy";
8
+ /** Reads the decision and inputs files once; the returned check is per row. */
9
+ export function boundOnDisk(stateDir) {
10
+ const requestHashes = loadDecisionRequestHashesFromDir(stateDir);
11
+ const forwardedHashes = loadSignedForwardedHashesFromDir(stateDir);
12
+ return (row) => {
13
+ const signed = requestHashes.get(row.ref);
14
+ return typeof signed === "string" && signed !== "" && approvalArgsBound(row, signed, forwardedHashes.get(row.ref));
15
+ };
16
+ }
@@ -1,3 +1,4 @@
1
+ import { type ApprovalRow } from "@verax-ai/proxy";
1
2
  import { type ToolExec, type WindowsMachineRoots } from "./install.ts";
2
3
  export declare function resolveApproveRef(pending: {
3
4
  ref: string;
@@ -10,6 +11,12 @@ export declare function resolveApproveRef(pending: {
10
11
  reason: "unknown-ref" | "ambiguous-ref";
11
12
  candidates: string[];
12
13
  };
14
+ /**
15
+ * What the terminal prints for a held row before it asks, or null when a
16
+ * spend's amount cannot be read as an integer. `runApprove` shows it only for
17
+ * a row the signed defer names.
18
+ */
19
+ export declare function heldPrompt(row: ApprovalRow): string | null;
13
20
  export type ApproveIo = {
14
21
  isTTY: boolean;
15
22
  ask: (prompt: string) => Promise<string>;
@@ -2,7 +2,8 @@ import { existsSync, readFileSync } from "node:fs";
2
2
  import { userInfo } from "node:os";
3
3
  import { join } from "node:path";
4
4
  import { createInterface } from "node:readline";
5
- import { approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueApprovalCommand, FileLedger, loadApprovalsFromDir, loadPolicy, DISPLAY_ESCAPE_CLASS, } from "@verax-ai/proxy";
5
+ import { approvalDispatchArgs, approvePending, approvalsLogFor, createApprovalBudgetGuard, enqueueApprovalCommand, FileLedger, loadApprovalsFromDir, loadPolicy, DISPLAY_ESCAPE_CLASS, } from "@verax-ai/proxy";
6
+ import { boundOnDisk } from "./approval-bound.js";
6
7
  import { EX_CONFIG } from "./config.js";
7
8
  import { heldSpend } from "./held-spend.js";
8
9
  import { cliCodeCheckPassedAlready, defaultElevated, directoryAccess, elevatedCommandCodeRefusal, elevatedStateDirRefusal, readWindowsMachineRoots, stateDirFor, SystemToolError, unreadableSentence, windowsProgramDataRefusal } from "./install.js";
@@ -99,10 +100,26 @@ function spendPrompt(row, spend, amount) {
99
100
  }
100
101
  function argumentPrompt(row) {
101
102
  const lines = [quotedField("tool", row.subject)];
102
- for (const key of Object.keys(row.args).sort())
103
- lines.push(quotedField(key, row.args[key]));
103
+ const args = approvalDispatchArgs(row);
104
+ for (const key of Object.keys(args).sort())
105
+ lines.push(quotedField(key, args[key]));
104
106
  return lines.join("");
105
107
  }
108
+ const TYPE_AMOUNT = "Type the amount in minor units:\n";
109
+ const TYPE_YES = "Type yes:\n";
110
+ /**
111
+ * What the terminal prints for a held row before it asks, or null when a
112
+ * spend's amount cannot be read as an integer. `runApprove` shows it only for
113
+ * a row the signed defer names.
114
+ */
115
+ export function heldPrompt(row) {
116
+ const spend = heldSpend(row);
117
+ if (!spend)
118
+ return `${argumentPrompt(row)}${TYPE_YES}`;
119
+ if (spend.amountMinor === null)
120
+ return null;
121
+ return `${spendPrompt(row, spend, spend.amountMinor)}${TYPE_AMOUNT}`;
122
+ }
106
123
  function readTypedAmount() {
107
124
  const rl = createInterface({ input: process.stdin, output: process.stdout });
108
125
  return new Promise((resolve) => {
@@ -217,25 +234,28 @@ export async function runApprove(argv, writeErr = (s) => process.stderr.write(s)
217
234
  writeErr("approve-unknown-ref\n");
218
235
  return 78;
219
236
  }
220
- const spend = heldSpend(waiting);
221
- if (spend) {
222
- const expected = spend.amountMinor;
223
- if (expected === null) {
224
- writeErr("approve-amount-unreadable\n");
225
- return 1;
226
- }
227
- writeOut(spendPrompt(waiting, spend, expected));
228
- writeOut("Type the amount in minor units:\n");
229
- const typed = (await terminal.ask("Type the amount in minor units:\n")).trim();
237
+ // The prompt shows the row on disk; a running body approves its own copy.
238
+ // Only a row the signed defer names is shown.
239
+ if (!boundOnDisk(stateDir)(waiting)) {
240
+ writeErr("approve-row-unbound\n");
241
+ return 1;
242
+ }
243
+ const prompt = heldPrompt(waiting);
244
+ if (prompt === null) {
245
+ writeErr("approve-amount-unreadable\n");
246
+ return 1;
247
+ }
248
+ writeOut(prompt);
249
+ const expected = heldSpend(waiting)?.amountMinor;
250
+ if (typeof expected === "number") {
251
+ const typed = (await terminal.ask(TYPE_AMOUNT)).trim();
230
252
  if (typed !== String(expected)) {
231
253
  writeErr("approve-amount-mismatch\n");
232
254
  return 1;
233
255
  }
234
256
  }
235
257
  else {
236
- writeOut(argumentPrompt(waiting));
237
- writeOut("Type yes:\n");
238
- const typed = (await terminal.ask("Type yes:\n")).trim();
258
+ const typed = (await terminal.ask(TYPE_YES)).trim();
239
259
  if (typed !== "yes") {
240
260
  writeErr("approve-confirm-mismatch\n");
241
261
  return 1;
@@ -229,12 +229,18 @@ function asTextResult(raw) {
229
229
  const content = [];
230
230
  const items = Array.isArray(rec.content) ? rec.content : [];
231
231
  for (const item of items) {
232
- if (item !== null &&
233
- typeof item === "object" &&
234
- item.type === "text" &&
235
- typeof item.text === "string") {
236
- content.push({ type: "text", text: item.text });
237
- continue;
232
+ if (item !== null && typeof item === "object") {
233
+ const block = item;
234
+ if (block.type === "text" && typeof block.text === "string") {
235
+ content.push({ type: "text", text: block.text });
236
+ continue;
237
+ }
238
+ if ((block.type === "image" || block.type === "audio") &&
239
+ typeof block.data === "string" &&
240
+ typeof block.mimeType === "string") {
241
+ content.push({ type: block.type, data: block.data, mimeType: block.mimeType });
242
+ continue;
243
+ }
238
244
  }
239
245
  content.push({ type: "text", text: JSON.stringify(item) });
240
246
  }
@@ -763,5 +769,5 @@ export async function openDownstream(spec, opts) {
763
769
  };
764
770
  }
765
771
  function textOf(result) {
766
- return result.content.map((c) => c.text).join("");
772
+ return result.content.map((c) => (c.type === "text" ? c.text : "")).join("");
767
773
  }
@@ -3,7 +3,7 @@
3
3
  * one. The terminal and the operator door read the same four fields from
4
4
  * here, so neither can show a payee or an amount the other would not.
5
5
  */
6
- import type { ApprovalRow } from "@verax-ai/proxy";
6
+ import { type ApprovalRow } from "@verax-ai/proxy";
7
7
  export type HeldSpend = {
8
8
  payee: string;
9
9
  amountMinor: number | null;
@@ -1,20 +1,25 @@
1
+ /**
2
+ * What a held spend is shown as, wherever an operator is asked to approve
3
+ * one. The terminal and the operator door read the same four fields from
4
+ * here, so neither can show a payee or an amount the other would not.
5
+ */
6
+ import { approvalDispatchArgs } from "@verax-ai/proxy";
1
7
  function integerMinor(value) {
2
8
  return typeof value === "number" && Number.isInteger(value) ? value : null;
3
9
  }
4
10
  /** An integer minor-unit amount, or null when the stored amount cannot be read as one. */
5
11
  export function minorUnits(row) {
6
- if (typeof row.amount === "number")
7
- return integerMinor(row.amount);
8
- return integerMinor(row.args.amountMinor);
12
+ return integerMinor(approvalDispatchArgs(row).amountMinor);
9
13
  }
10
14
  /** Null for a held call that is not a spend. */
11
15
  export function heldSpend(row) {
12
16
  if (row.subject !== "spend")
13
17
  return null;
18
+ const args = approvalDispatchArgs(row);
14
19
  return {
15
- payee: typeof row.payee === "string" ? row.payee : String(row.args.payee ?? ""),
20
+ payee: typeof args.payee === "string" ? args.payee : String(args.payee ?? ""),
16
21
  amountMinor: minorUnits(row),
17
- currency: typeof row.currency === "string" ? row.currency : String(row.args.currency ?? ""),
18
- reference: typeof row.args.reference === "string" ? row.args.reference : "",
22
+ currency: typeof args.currency === "string" ? args.currency : String(args.currency ?? ""),
23
+ reference: typeof args.reference === "string" ? args.reference : "",
19
24
  };
20
25
  }
@@ -20,7 +20,8 @@
20
20
  import { createHash, createHmac, randomBytes, timingSafeEqual } from "node:crypto";
21
21
  import { rmSync } from "node:fs";
22
22
  import { createServer } from "node:http";
23
- import { loadApprovalsFromDir, terminalText } from "@verax-ai/proxy";
23
+ import { approvalDispatchArgs, loadApprovalsFromDir, terminalText } from "@verax-ai/proxy";
24
+ import { boundOnDisk } from "./approval-bound.js";
24
25
  import { asAssertion } from "./approval-signature.js";
25
26
  import { readBearer } from "./auth.js";
26
27
  import { heldSpend } from "./held-spend.js";
@@ -410,15 +411,18 @@ export async function openOperatorDoor(opts) {
410
411
  }
411
412
  if (url.pathname === "/api/waiting") {
412
413
  const at = now();
414
+ // A row edited on disk is not listed: what the phone shows is what
415
+ // the signed defer names, and that is what an approval sends.
416
+ const isBound = boundOnDisk(stateDir);
413
417
  const waiting = loadApprovalsFromDir(stateDir)
414
- .filter((row) => row.status === "pending" && row.expiresAtMs > at)
418
+ .filter((row) => row.status === "pending" && row.expiresAtMs > at && isBound(row))
415
419
  .map((row) => {
416
420
  const spend = heldSpend(row);
417
421
  return {
418
422
  ref: row.ref,
419
423
  requestHash: row.requestHash,
420
424
  tool: row.subject,
421
- args: row.args,
425
+ args: approvalDispatchArgs(row),
422
426
  agent: row.brain,
423
427
  rule: row.ruleText,
424
428
  ...(row.createdAtMs !== undefined ? { createdAtMs: row.createdAtMs } : {}),
@@ -1,5 +1,6 @@
1
1
  import { decisionRecordHash } from "@cedulon/core";
2
2
  import { approvalsLogFor, approvePending, createApprovalBudgetGuard, loadApprovalsFromDir, terminalText } from "@verax-ai/proxy";
3
+ import { boundOnDisk } from "./approval-bound.js";
3
4
  import { approvalChallenge, gateApprovalSignature } from "./approval-signature.js";
4
5
  import { gateControlSignature, nextControlChallenge } from "./control-signature.js";
5
6
  import { haltBody, readHalt, resumeBody } from "./halt.js";
@@ -170,6 +171,12 @@ export async function handleApprove(ctx) {
170
171
  send(res, 409, { error: "stale", requestHash: waiting.requestHash });
171
172
  return;
172
173
  }
174
+ // The row on disk is what the phone showed; the body approves its own copy.
175
+ // A row the signed defer does not name is not approved.
176
+ if (!boundOnDisk(stateDir)(waiting)) {
177
+ send(res, 409, { error: "row-unbound" });
178
+ return;
179
+ }
173
180
  const deferRecord = await signedDefer(services.ledger, ref);
174
181
  if (!deferRecord) {
175
182
  send(res, 404, { error: "unknown-ref" });
@@ -1,6 +1,6 @@
1
1
  import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
- import { loadApprovalsFromDir, loadDecisionRefsFromDir, loadDecisionRequestHashesFromDir, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile, } from "@verax-ai/proxy";
3
+ import { loadApprovalsFromDir, loadDecisionRefsFromDir, loadDecisionRequestHashesFromDir, loadSignedForwardedHashesFromDir, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile, } from "@verax-ai/proxy";
4
4
  const SNAPSHOT_NAME = /^[0-9a-f]{64}\.json$/;
5
5
  // The descriptors come from the parsed policy, never from the stored document: a snapshot is
6
6
  // read with unknown keys ignored, and a block the hash does not cover must not decide a match.
@@ -221,6 +221,7 @@ export function runReconcile(argv, writeErr = (s) => process.stderr.write(s)) {
221
221
  const effects = loadEffectsFromDir(parsed.stateDir);
222
222
  const decisionRefs = loadDecisionRefsFromDir(parsed.stateDir);
223
223
  const decisionRequestHashes = loadDecisionRequestHashesFromDir(parsed.stateDir);
224
+ const decisionForwardedHashes = loadSignedForwardedHashesFromDir(parsed.stateDir);
224
225
  const loaded = parsed.channel === "card" ? loadDescriptorsFromDir(parsed.stateDir) : undefined;
225
226
  const descriptorsByPayee = loaded?.descriptorsByPayee;
226
227
  const report = reconcile(channel, effects, {
@@ -228,6 +229,7 @@ export function runReconcile(argv, writeErr = (s) => process.stderr.write(s)) {
228
229
  window: parsed.window,
229
230
  decisionRefs,
230
231
  decisionRequestHashes,
232
+ decisionForwardedHashes,
231
233
  approvals: parsed.channel === "card" ? loadApprovalsFromDir(parsed.stateDir) : undefined,
232
234
  skipped: card?.skipped,
233
235
  ...(descriptorsByPayee && Object.keys(descriptorsByPayee).length > 0
package/dist/server.js CHANGED
@@ -11,7 +11,8 @@ import { isLoopbackHost } from "./config.js";
11
11
  import { contentLengthOverLimit, MAX_BODY_BYTES, readJsonBody, send } from "./http-io.js";
12
12
  import { openOperatorDoor } from "./operator-door.js";
13
13
  import { handleApprove, handleApproveChallenge, handleControl } from "./operator-handlers.js";
14
- import { explain, LedgerDenyUnrecorded, loadApprovalsFromDir, loadDecisionRequestHashesFromDir, sha256Canonical, terminalText, } from "@verax-ai/proxy";
14
+ import { boundOnDisk } from "./approval-bound.js";
15
+ import { approvalDispatchArgs, explain, LedgerDenyUnrecorded, loadApprovalsFromDir, terminalText, } from "@verax-ai/proxy";
15
16
  import { createVerifier, readBearer, resourceMetadataUrl, wwwAuthenticate } from "./auth.js";
16
17
  import { bumpMetric, bumpUnauthenticated } from "./metrics.js";
17
18
  import { isRevokedJti } from "./revoke.js";
@@ -190,21 +191,21 @@ const HEADERS_TIMEOUT_MS = 20_000;
190
191
  * the copies are left off and the row is marked unbound.
191
192
  */
192
193
  function approvalsForLedger(stateDir) {
193
- const requestHashes = loadDecisionRequestHashesFromDir(stateDir);
194
+ const isBound = boundOnDisk(stateDir);
194
195
  return loadApprovalsFromDir(stateDir).map((row) => {
195
- const signed = requestHashes.get(row.ref);
196
- const rowHash = sha256Canonical({ name: row.subject, arguments: row.args });
197
- const bound = typeof signed === "string" && signed !== "" && rowHash === signed;
196
+ const bound = isBound(row);
197
+ const shown = approvalDispatchArgs(row);
198
198
  if (!bound) {
199
199
  const { amount: _amount, currency: _currency, payee: _payee, ...rest } = row;
200
- return { ...rest, bound: false };
200
+ return { ...rest, bound: false, args: shown };
201
201
  }
202
202
  return {
203
203
  ...row,
204
204
  bound: true,
205
- amount: row.args.amountMinor,
206
- currency: row.args.currency,
207
- payee: row.args.payee,
205
+ args: shown,
206
+ amount: shown.amountMinor,
207
+ currency: shown.currency,
208
+ payee: shown.payee,
208
209
  };
209
210
  });
210
211
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verax-ai/body",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "license": "Apache-2.0",
5
5
  "type": "module",
6
6
  "engines": {
@@ -26,8 +26,8 @@
26
26
  "@cedulon/cose": "0.13.1",
27
27
  "@cedulon/effect-extract": "0.13.1",
28
28
  "@modelcontextprotocol/sdk": "^1.30.0",
29
- "@verax-ai/inventory": "^0.7.0",
30
- "@verax-ai/proxy": "^0.7.0",
29
+ "@verax-ai/inventory": "^0.8.0",
30
+ "@verax-ai/proxy": "^0.8.0",
31
31
  "jose": "^6.2.12"
32
32
  },
33
33
  "description": "VERAX body: an MCP server that decides, records and proves what an agent did.",