@verax-ai/body 0.6.0 → 0.6.1

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/dist/config.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export declare const EX_CONFIG = 78;
2
+ /** A start error whose message begins with this is one stderr line and exit 78. */
3
+ export declare const POLICY_FILE_ERROR_PREFIX = "VERAX_POLICY_FILE ";
2
4
  export type BodyConfig = {
3
5
  issuer: string;
4
6
  jwksUrl: string;
@@ -67,6 +69,8 @@ export type ConfigResult = {
67
69
  code: typeof EX_CONFIG;
68
70
  };
69
71
  export declare function isLoopbackHost(host: string): boolean;
72
+ /** A directory or an unreadable file. A path that is not there is left to the read. */
73
+ export declare function policyFileRefusal(path: string): string | null;
70
74
  /**
71
75
  * `VERAX_OPERATOR_DOOR` names the origin an operator reaches the door at. It
72
76
  * is HTTPS, or `http://localhost` for a trial on the machine itself, and it
package/dist/config.js CHANGED
@@ -1,5 +1,7 @@
1
- import { readFileSync } from "node:fs";
1
+ import { readFileSync, statSync } from "node:fs";
2
2
  export const EX_CONFIG = 78;
3
+ /** A start error whose message begins with this is one stderr line and exit 78. */
4
+ export const POLICY_FILE_ERROR_PREFIX = "VERAX_POLICY_FILE ";
3
5
  /** Per-tenant `memory.put` cap. The HTTP body cap does not reset this total. */
4
6
  export const DEFAULT_MEMORY_QUOTA_BYTES = 1024 * 1024;
5
7
  /** Per-tenant `message.send` cap. The HTTP body cap does not reset this total. */
@@ -47,6 +49,30 @@ function jwksPinParses(raw) {
47
49
  return null;
48
50
  }
49
51
  }
52
+ /** A directory or an unreadable file. A path that is not there is left to the read. */
53
+ export function policyFileRefusal(path) {
54
+ let st;
55
+ try {
56
+ st = statSync(path);
57
+ }
58
+ catch (err) {
59
+ const code = err.code;
60
+ if (code === "ENOENT")
61
+ return null;
62
+ return `${POLICY_FILE_ERROR_PREFIX}is missing or unreadable: ${path}`;
63
+ }
64
+ if (st.isDirectory())
65
+ return `${POLICY_FILE_ERROR_PREFIX}is a directory: ${path}`;
66
+ if (!st.isFile())
67
+ return `${POLICY_FILE_ERROR_PREFIX}is missing or unreadable: ${path}`;
68
+ try {
69
+ readFileSync(path);
70
+ }
71
+ catch {
72
+ return `${POLICY_FILE_ERROR_PREFIX}is missing or unreadable: ${path}`;
73
+ }
74
+ return null;
75
+ }
50
76
  function jwksFileParses(path) {
51
77
  try {
52
78
  const parsed = JSON.parse(readFileSync(path, "utf8"));
@@ -150,6 +176,10 @@ export function loadConfig(env) {
150
176
  if (policyFile === "") {
151
177
  return { ok: false, code: EX_CONFIG, reason: "missing VERAX_POLICY_FILE" };
152
178
  }
179
+ const policyRefusal = policyFileRefusal(policyFile);
180
+ if (policyRefusal) {
181
+ return { ok: false, code: EX_CONFIG, reason: policyRefusal };
182
+ }
153
183
  const door = readOperatorDoor(env);
154
184
  if (!door.ok) {
155
185
  return { ok: false, code: EX_CONFIG, reason: door.reason };
package/dist/main.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { setTimeout } from "node:timers";
2
2
  import { terminalText } from "@verax-ai/proxy";
3
3
  import { JwksFileError } from "./auth.js";
4
- import { EX_CONFIG, loadConfig, overlayInventoryArg } from "./config.js";
4
+ import { EX_CONFIG, POLICY_FILE_ERROR_PREFIX, loadConfig, overlayInventoryArg } from "./config.js";
5
5
  import { watchDesktopParent } from "./desktop-parent.js";
6
6
  import { KeyAlgorithmError, KeysPartialError } from "./keys.js";
7
7
  import { listen, operatorDoorOf } from "./server.js";
@@ -69,6 +69,11 @@ export async function main(env = process.env, argv = process.argv) {
69
69
  endProcess(EX_CONFIG);
70
70
  return;
71
71
  }
72
+ if (err instanceof Error && err.message.startsWith(POLICY_FILE_ERROR_PREFIX)) {
73
+ process.stderr.write(`${terminalText(err.message)}\n`);
74
+ endProcess(EX_CONFIG);
75
+ return;
76
+ }
72
77
  throw err;
73
78
  }
74
79
  }
@@ -1,7 +1,7 @@
1
1
  import { lstatSync } from "node:fs";
2
2
  import { resolve } from "node:path";
3
3
  import { credentialsPath, credentialsReadable, readCredentials, readCredentialsAt, readRemoved, removeCredential, removedPath, } from "./operator-credentials.js";
4
- import { beginPairing } from "./operator-pairing.js";
4
+ import { beginPairing, livePairing } from "./operator-pairing.js";
5
5
  /**
6
6
  * As whom the pairing code is written. An installed body runs as its own
7
7
  * account and owns its state directory, and root asking for a code is the
@@ -111,7 +111,10 @@ export async function runOperator(argv, sys = realSys) {
111
111
  }
112
112
  }
113
113
  if (verb === "enroll") {
114
+ const replaced = livePairing(writer.stateDir);
114
115
  const { code } = beginPairing(writer.stateDir);
116
+ if (replaced)
117
+ process.stderr.write("the earlier pairing code no longer works\n");
115
118
  process.stdout.write(`${code}\n`);
116
119
  return 0;
117
120
  }
@@ -17,6 +17,8 @@ export declare function hashPairingCode(code: string): string;
17
17
  export declare function mintPairingCode(): string;
18
18
  export declare function writePairing(stateDir: string, record: PairingRecord): void;
19
19
  export declare function readPairing(stateDir: string): PairingRecord | null;
20
+ /** A pairing the door would still accept. Enrolling again replaces that code. */
21
+ export declare function livePairing(stateDir: string, nowMs?: number): boolean;
20
22
  export declare function beginPairing(stateDir: string, nowMs?: number): {
21
23
  code: string;
22
24
  };
@@ -32,6 +32,17 @@ export function readPairing(stateDir) {
32
32
  return null;
33
33
  }
34
34
  }
35
+ /** A pairing the door would still accept. Enrolling again replaces that code. */
36
+ export function livePairing(stateDir, nowMs = Date.now()) {
37
+ const row = readPairing(stateDir);
38
+ if (!row)
39
+ return false;
40
+ if (row.attempts >= PAIRING_MAX_ATTEMPTS)
41
+ return false;
42
+ if (nowMs > row.expiresAtMs)
43
+ return false;
44
+ return true;
45
+ }
35
46
  export function beginPairing(stateDir, nowMs = Date.now()) {
36
47
  const code = mintPairingCode();
37
48
  writePairing(stateDir, {
@@ -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, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile, } from "@verax-ai/proxy";
3
+ import { loadApprovalsFromDir, loadDecisionRefsFromDir, loadDecisionRequestHashesFromDir, 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.
@@ -220,12 +220,14 @@ export function runReconcile(argv, writeErr = (s) => process.stderr.write(s)) {
220
220
  const channel = card ?? parseChannelJsonl(raw);
221
221
  const effects = loadEffectsFromDir(parsed.stateDir);
222
222
  const decisionRefs = loadDecisionRefsFromDir(parsed.stateDir);
223
+ const decisionRequestHashes = loadDecisionRequestHashesFromDir(parsed.stateDir);
223
224
  const loaded = parsed.channel === "card" ? loadDescriptorsFromDir(parsed.stateDir) : undefined;
224
225
  const descriptorsByPayee = loaded?.descriptorsByPayee;
225
226
  const report = reconcile(channel, effects, {
226
227
  toleranceMs: parsed.toleranceMs,
227
228
  window: parsed.window,
228
229
  decisionRefs,
230
+ decisionRequestHashes,
229
231
  approvals: parsed.channel === "card" ? loadApprovalsFromDir(parsed.stateDir) : undefined,
230
232
  skipped: card?.skipped,
231
233
  ...(descriptorsByPayee && Object.keys(descriptorsByPayee).length > 0
package/dist/server.js CHANGED
@@ -10,7 +10,7 @@ import { isLoopbackHost } from "./config.js";
10
10
  import { contentLengthOverLimit, MAX_BODY_BYTES, readJsonBody, send } from "./http-io.js";
11
11
  import { openOperatorDoor } from "./operator-door.js";
12
12
  import { handleApprove, handleApproveChallenge, handleControl } from "./operator-handlers.js";
13
- import { explain, LedgerDenyUnrecorded, loadApprovalsFromDir, terminalText } from "@verax-ai/proxy";
13
+ import { explain, LedgerDenyUnrecorded, loadApprovalsFromDir, loadDecisionRequestHashesFromDir, sha256Canonical, terminalText, } from "@verax-ai/proxy";
14
14
  import { createVerifier, readBearer, resourceMetadataUrl, wwwAuthenticate } from "./auth.js";
15
15
  import { bumpMetric, bumpUnauthenticated } from "./metrics.js";
16
16
  import { isRevokedJti } from "./revoke.js";
@@ -181,6 +181,30 @@ export const TOOL_META = [
181
181
  // authorised until that default. Headers are bounded tighter than the body.
182
182
  const REQUEST_TIMEOUT_MS = 30_000;
183
183
  const HEADERS_TIMEOUT_MS = 20_000;
184
+ /**
185
+ * Amount, currency and payee on a ledger approval are the arguments only when
186
+ * those arguments still hash to the signed request on the defer. Otherwise
187
+ * the copies are left off and the row is marked unbound.
188
+ */
189
+ function approvalsForLedger(stateDir) {
190
+ const requestHashes = loadDecisionRequestHashesFromDir(stateDir);
191
+ return loadApprovalsFromDir(stateDir).map((row) => {
192
+ const signed = requestHashes.get(row.ref);
193
+ const rowHash = sha256Canonical({ name: row.subject, arguments: row.args });
194
+ const bound = typeof signed === "string" && signed !== "" && rowHash === signed;
195
+ if (!bound) {
196
+ const { amount: _amount, currency: _currency, payee: _payee, ...rest } = row;
197
+ return { ...rest, bound: false };
198
+ }
199
+ return {
200
+ ...row,
201
+ bound: true,
202
+ amount: row.args.amountMinor,
203
+ currency: row.args.currency,
204
+ payee: row.args.payee,
205
+ };
206
+ });
207
+ }
184
208
  // Unbounded GET /api/ledger stringified ~690 MB at 200k rows and threw Invalid string length (HTTP 500).
185
209
  const DEFAULT_LEDGER_LIMIT = 1000;
186
210
  const MAX_LEDGER_LIMIT = 5000;
@@ -689,7 +713,7 @@ export async function listen(config) {
689
713
  policy: { hash: services.policyHash, document: services.policyDocument },
690
714
  policies: readPolicySnapshots(config.stateDir, hashes),
691
715
  inputs: await matchingInputs(config.stateDir, decisions),
692
- approvals: loadApprovalsFromDir(config.stateDir),
716
+ approvals: approvalsForLedger(config.stateDir),
693
717
  more,
694
718
  piecesTouched,
695
719
  limit,
package/dist/wiring.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createProxy, FileLedger, loadPolicy, tenantKey, } from "@verax-ai/proxy";
2
2
  import { readFileSync } from "node:fs";
3
+ import { POLICY_FILE_ERROR_PREFIX, policyFileRefusal } from "./config.js";
3
4
  import { persistPolicySnapshot } from "./policy-store.js";
4
5
  import { inputsPrincipal } from "./inputs-read.js";
5
6
  import { memoryBelongsToOtherTenant, memoryExistsForTenant, memoryGet, memoryPut, readMemoryMeta, } from "./tools/memory.js";
@@ -36,7 +37,19 @@ export function createBodyServices(opts) {
36
37
  }
37
38
  // The policy is read before the ledger opens: opening takes the ledger lock,
38
39
  // and a start the policy stops must not leave that lock for the next one.
39
- const policyText = readFileSync(opts.policyFile, "utf8");
40
+ const policyRefusal = policyFileRefusal(opts.policyFile);
41
+ if (policyRefusal)
42
+ throw new Error(policyRefusal);
43
+ let policyText;
44
+ try {
45
+ policyText = readFileSync(opts.policyFile, "utf8");
46
+ }
47
+ catch (err) {
48
+ const code = err.code;
49
+ if (code === "EISDIR")
50
+ throw new Error(`${POLICY_FILE_ERROR_PREFIX}is a directory: ${opts.policyFile}`);
51
+ throw new Error(`${POLICY_FILE_ERROR_PREFIX}is missing or unreadable: ${opts.policyFile}`);
52
+ }
40
53
  const policyDocument = JSON.parse(policyText);
41
54
  const policy = loadPolicy(policyText);
42
55
  const ledger = new FileLedger(opts.stateDir);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@verax-ai/body",
3
- "version": "0.6.0",
3
+ "version": "0.6.1",
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.6.0",
30
- "@verax-ai/proxy": "^0.6.0",
29
+ "@verax-ai/inventory": "^0.6.1",
30
+ "@verax-ai/proxy": "^0.6.1",
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.",