@verax-ai/body 0.3.0 → 0.4.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/keys.d.ts CHANGED
@@ -6,6 +6,12 @@ export declare class KeysPartialError extends Error {
6
6
  readonly code = 78;
7
7
  constructor();
8
8
  }
9
+ export declare class KeyAlgorithmError extends Error {
10
+ readonly code = 78;
11
+ constructor(which: string);
12
+ }
13
+ /** A public key this process will verify with. Anything but Ed25519 stops start. */
14
+ export declare function assertEd25519PublicKey(pem: string, which: string): void;
9
15
  export declare function loadOrCreateSigners(stateDir: string): {
10
16
  recordSigner: KeyPair;
11
17
  effectSigner: KeyPair;
package/dist/keys.js CHANGED
@@ -1,4 +1,4 @@
1
- import { generateKeyPairSync } from "node:crypto";
1
+ import { createPublicKey, generateKeyPairSync } from "node:crypto";
2
2
  import { mkdirSync, readFileSync, renameSync, writeFileSync, existsSync } from "node:fs";
3
3
  import { join } from "node:path";
4
4
  import { EX_CONFIG } from "./config.js";
@@ -9,6 +9,25 @@ export class KeysPartialError extends Error {
9
9
  this.name = "KeysPartialError";
10
10
  }
11
11
  }
12
+ export class KeyAlgorithmError extends Error {
13
+ code = EX_CONFIG;
14
+ constructor(which) {
15
+ super(`${which} public key is not ed25519`);
16
+ this.name = "KeyAlgorithmError";
17
+ }
18
+ }
19
+ /** A public key this process will verify with. Anything but Ed25519 stops start. */
20
+ export function assertEd25519PublicKey(pem, which) {
21
+ let type;
22
+ try {
23
+ type = createPublicKey(pem).asymmetricKeyType;
24
+ }
25
+ catch {
26
+ throw new KeyAlgorithmError(which);
27
+ }
28
+ if (type !== "ed25519")
29
+ throw new KeyAlgorithmError(which);
30
+ }
12
31
  function pair() {
13
32
  const { publicKey, privateKey } = generateKeyPairSync("ed25519");
14
33
  return {
@@ -33,23 +52,27 @@ export function loadOrCreateSigners(stateDir) {
33
52
  if (present !== 0 && present !== 4) {
34
53
  throw new KeysPartialError();
35
54
  }
36
- if (present === 0) {
37
- const record = pair();
38
- const effect = pair();
39
- writePemAtomic(recPriv, record.privateKeyPem);
40
- writePemAtomic(recPub, record.publicKeyPem);
41
- writePemAtomic(effPriv, effect.privateKeyPem);
42
- writePemAtomic(effPub, effect.publicKeyPem);
43
- return { recordSigner: record, effectSigner: effect };
44
- }
45
- return {
46
- recordSigner: {
47
- privateKeyPem: readFileSync(recPriv, "utf8"),
48
- publicKeyPem: readFileSync(recPub, "utf8"),
49
- },
50
- effectSigner: {
51
- privateKeyPem: readFileSync(effPriv, "utf8"),
52
- publicKeyPem: readFileSync(effPub, "utf8"),
53
- },
54
- };
55
+ const loaded = present === 0
56
+ ? (() => {
57
+ const record = pair();
58
+ const effect = pair();
59
+ writePemAtomic(recPriv, record.privateKeyPem);
60
+ writePemAtomic(recPub, record.publicKeyPem);
61
+ writePemAtomic(effPriv, effect.privateKeyPem);
62
+ writePemAtomic(effPub, effect.publicKeyPem);
63
+ return { recordSigner: record, effectSigner: effect };
64
+ })()
65
+ : {
66
+ recordSigner: {
67
+ privateKeyPem: readFileSync(recPriv, "utf8"),
68
+ publicKeyPem: readFileSync(recPub, "utf8"),
69
+ },
70
+ effectSigner: {
71
+ privateKeyPem: readFileSync(effPriv, "utf8"),
72
+ publicKeyPem: readFileSync(effPub, "utf8"),
73
+ },
74
+ };
75
+ assertEd25519PublicKey(loaded.recordSigner.publicKeyPem, "record");
76
+ assertEd25519PublicKey(loaded.effectSigner.publicKeyPem, "effect");
77
+ return loaded;
55
78
  }
package/dist/main.js CHANGED
@@ -1,8 +1,10 @@
1
1
  import { JwksFileError } from "./auth.js";
2
2
  import { EX_CONFIG, loadConfig, overlayInventoryArg } from "./config.js";
3
- import { KeysPartialError } from "./keys.js";
3
+ import { watchDesktopParent } from "./desktop-parent.js";
4
+ import { KeyAlgorithmError, KeysPartialError } from "./keys.js";
4
5
  import { listen } from "./server.js";
5
6
  export async function main(env = process.env, argv = process.argv) {
7
+ watchDesktopParent({ env });
6
8
  const overlaid = overlayInventoryArg(env, argv);
7
9
  if ("error" in overlaid) {
8
10
  process.stderr.write(`${overlaid.error}\n`);
@@ -15,6 +17,10 @@ export async function main(env = process.env, argv = process.argv) {
15
17
  }
16
18
  try {
17
19
  const server = await listen(loaded.value);
20
+ const addr = server.address();
21
+ if (addr && typeof addr === "object") {
22
+ process.stderr.write(`listening ${addr.address}:${addr.port}\n`);
23
+ }
18
24
  const shutdown = () => {
19
25
  server.close(() => process.exit(0));
20
26
  };
@@ -26,6 +32,10 @@ export async function main(env = process.env, argv = process.argv) {
26
32
  process.stderr.write("keys-partial\n");
27
33
  process.exit(err.code);
28
34
  }
35
+ if (err instanceof KeyAlgorithmError) {
36
+ process.stderr.write(`${err.message}\n`);
37
+ process.exit(err.code);
38
+ }
29
39
  if (err instanceof JwksFileError) {
30
40
  process.stderr.write(`${err.message}\n`);
31
41
  process.exit(err.code);
@@ -52,8 +52,9 @@ export function scanNoBypass(base = pkgRoot, extra = []) {
52
52
  if (rel === "src/no-bypass-scan.ts")
53
53
  continue;
54
54
  const wiring = rel === "src/wiring.ts";
55
- // Desktop supervises issuer/body/panel. It is not a tool-call path.
56
- const desktop = rel === "src/desktop.ts";
55
+ // Desktop supervises issuer/body/panel. Install copies the body under another
56
+ // account. Neither file is a tool-call path.
57
+ const desktop = rel === "src/desktop.ts" || rel === "src/install.ts";
57
58
  // The downstream spike attaches one stdio MCP server. Not a tool-call path.
58
59
  const downstream = rel === "src/downstream.ts";
59
60
  const originalLines = text.split(/\r?\n/);
@@ -1,6 +1,6 @@
1
1
  import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
2
2
  import { join } from "node:path";
3
- import { loadApprovalsFromDir, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile } from "@verax-ai/proxy";
3
+ import { loadApprovalsFromDir, loadDecisionRefsFromDir, loadEffectsFromDir, loadPolicy, parseCardCsv, parseChannelJsonl, reconcile, } from "@verax-ai/proxy";
4
4
  const SNAPSHOT_NAME = /^[0-9a-f]{64}\.json$/;
5
5
  function descriptorsFromDoc(doc) {
6
6
  const out = {};
@@ -229,11 +229,13 @@ export function runReconcile(argv, writeErr = (s) => process.stderr.write(s)) {
229
229
  : undefined;
230
230
  const channel = card ?? parseChannelJsonl(raw);
231
231
  const effects = loadEffectsFromDir(parsed.stateDir);
232
+ const decisionRefs = loadDecisionRefsFromDir(parsed.stateDir);
232
233
  const loaded = parsed.channel === "card" ? loadDescriptorsFromDir(parsed.stateDir) : undefined;
233
234
  const descriptorsByPayee = loaded?.descriptorsByPayee;
234
235
  const report = reconcile(channel, effects, {
235
236
  toleranceMs: parsed.toleranceMs,
236
237
  window: parsed.window,
238
+ decisionRefs,
237
239
  approvals: parsed.channel === "card" ? loadApprovalsFromDir(parsed.stateDir) : undefined,
238
240
  skipped: card?.skipped,
239
241
  ...(descriptorsByPayee && Object.keys(descriptorsByPayee).length > 0
package/dist/server.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { type Server } from "node:http";
2
- import type { BodyConfig } from "./config.ts";
2
+ import { type BodyConfig } from "./config.ts";
3
3
  import { TOOL_NAMES } from "./wiring.ts";
4
4
  export declare const TOOL_META: ({
5
5
  name: string;
@@ -184,5 +184,18 @@ export declare const TOOL_META: ({
184
184
  required: string[];
185
185
  };
186
186
  })[];
187
+ /** `malformed` falls through so `Host: [` stays the existing bad-request. */
188
+ export declare function loopbackHostDecision(hostHeader: string | undefined, bindPort: number): "allow" | "deny" | "malformed";
189
+ /**
190
+ * Second look at a bearer that already verified. The first look runs before
191
+ * the body is read; this one runs after the body and before dispatch or
192
+ * approval. `exp` is seconds since the epoch. A passed `exp` is dead even
193
+ * inside the verifier's clock tolerance: that tolerance applied at the door,
194
+ * and the body has since arrived. A missing `exp` is dead too.
195
+ */
196
+ export declare function bearerStillLive(payload: {
197
+ jti?: unknown;
198
+ exp?: unknown;
199
+ }, stateDir: string): boolean;
187
200
  export declare function listen(config: BodyConfig): Promise<Server>;
188
201
  export { TOOL_NAMES };
package/dist/server.js CHANGED
@@ -4,6 +4,7 @@ import { createServer } from "node:http";
4
4
  import { Server as McpServer } from "@modelcontextprotocol/sdk/server/index.js";
5
5
  import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
6
6
  import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";
7
+ import { isLoopbackHost } from "./config.js";
7
8
  import { approvalsLogFor, approvePending, createApprovalBudgetGuard, explain, LedgerDenyUnrecorded, loadApprovalsFromDir, } from "@verax-ai/proxy";
8
9
  import { createVerifier, readBearer, resourceMetadataUrl, wwwAuthenticate } from "./auth.js";
9
10
  import { bumpMetric, bumpUnauthenticated } from "./metrics.js";
@@ -11,8 +12,9 @@ import { isRevokedJti } from "./revoke.js";
11
12
  import { loadOrCreateSigners } from "./keys.js";
12
13
  import { matchingInputs } from "./inputs-read.js";
13
14
  import { readPolicySnapshots } from "./policy-store.js";
14
- import { readHeartbeat, readWitnessPulse } from "./health-extras.js";
15
+ import { readHeartbeat, readInstallHealthNonce, readWitnessPulse } from "./health-extras.js";
15
16
  import { agentsWindow } from "./agents.js";
17
+ import { haltBody, readHalt, resumeBody } from "./halt.js";
16
18
  import { inventoryHealth, readInventoryFile } from "./inventory-file.js";
17
19
  import { createBodyServices, TOOL_NAMES } from "./wiring.js";
18
20
  import { openDownstream, parseDownstreamDocument, } from "./downstream.js";
@@ -170,10 +172,48 @@ export const TOOL_META = [
170
172
  },
171
173
  ];
172
174
  const MAX_BODY_BYTES = 1024 * 1024;
175
+ // Node's default requestTimeout is 300s. A stalled body would otherwise stay
176
+ // authorised until that default. Headers are bounded tighter than the body.
177
+ const REQUEST_TIMEOUT_MS = 30_000;
178
+ const HEADERS_TIMEOUT_MS = 20_000;
173
179
  // Unbounded GET /api/ledger stringified ~690 MB at 200k rows and threw Invalid string length (HTTP 500).
174
180
  const DEFAULT_LEDGER_LIMIT = 1000;
175
181
  const MAX_LEDGER_LIMIT = 5000;
176
182
  const responseSlot = new AsyncLocalStorage();
183
+ /** `malformed` falls through so `Host: [` stays the existing bad-request. */
184
+ export function loopbackHostDecision(hostHeader, bindPort) {
185
+ if (hostHeader === undefined || hostHeader.trim() === "")
186
+ return "deny";
187
+ const raw = hostHeader.trim();
188
+ if (raw.startsWith("[") && !raw.includes("]"))
189
+ return "malformed";
190
+ let name = raw;
191
+ let port;
192
+ if (raw.startsWith("[")) {
193
+ const end = raw.indexOf("]");
194
+ name = raw.slice(1, end);
195
+ const rest = raw.slice(end + 1);
196
+ if (rest === "")
197
+ port = undefined;
198
+ else if (/^:[0-9]+$/.test(rest))
199
+ port = Number(rest.slice(1));
200
+ else
201
+ return "deny";
202
+ }
203
+ else {
204
+ const colon = raw.lastIndexOf(":");
205
+ if (colon > 0 && /^[0-9]+$/.test(raw.slice(colon + 1))) {
206
+ name = raw.slice(0, colon);
207
+ port = Number(raw.slice(colon + 1));
208
+ }
209
+ }
210
+ const lowered = name.toLowerCase();
211
+ if (lowered !== "127.0.0.1" && lowered !== "localhost" && lowered !== "::1")
212
+ return "deny";
213
+ if (port === undefined)
214
+ return bindPort === 80 ? "allow" : "deny";
215
+ return port === bindPort ? "allow" : "deny";
216
+ }
177
217
  function contentLengthOverLimit(req) {
178
218
  const raw = req.headers["content-length"];
179
219
  if (raw === undefined)
@@ -209,6 +249,21 @@ async function readJsonBody(req, max) {
209
249
  return { ok: false, bad: true };
210
250
  }
211
251
  }
252
+ /**
253
+ * Second look at a bearer that already verified. The first look runs before
254
+ * the body is read; this one runs after the body and before dispatch or
255
+ * approval. `exp` is seconds since the epoch. A passed `exp` is dead even
256
+ * inside the verifier's clock tolerance: that tolerance applied at the door,
257
+ * and the body has since arrived. A missing `exp` is dead too.
258
+ */
259
+ export function bearerStillLive(payload, stateDir) {
260
+ const jti = typeof payload.jti === "string" ? payload.jti : "";
261
+ if (jti === "" || isRevokedJti(stateDir, jti))
262
+ return false;
263
+ if (typeof payload.exp !== "number" || !Number.isFinite(payload.exp))
264
+ return false;
265
+ return payload.exp * 1000 > Date.now();
266
+ }
212
267
  function isProtectedResourcePath(pathname, audience) {
213
268
  const want = new URL(resourceMetadataUrl(audience)).pathname;
214
269
  return pathname === want || pathname === "/.well-known/oauth-protected-resource";
@@ -308,7 +363,7 @@ export async function listen(config) {
308
363
  throw err;
309
364
  }
310
365
  const toolMeta = [...TOOL_META, ...extraTools.map(downstreamMeta)];
311
- const verify = createVerifier(config.jwksUrl, config.issuer, config.audience, config.jwksFile);
366
+ const verify = createVerifier(config.jwksUrl, config.issuer, config.audience, config.jwksFile, config.jwksPin);
312
367
  const attachHandlers = (mcp) => {
313
368
  mcp.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: toolMeta }));
314
369
  mcp.setRequestHandler(CallToolRequestSchema, async (request, extra) => {
@@ -340,6 +395,25 @@ export async function listen(config) {
340
395
  };
341
396
  const server = createServer(async (req, res) => {
342
397
  try {
398
+ if (isLoopbackHost(config.bindHost)) {
399
+ const hostHeader = Array.isArray(req.headers.host) ? req.headers.host[0] : req.headers.host;
400
+ const listenPort = server.address().port;
401
+ const hostDecision = loopbackHostDecision(hostHeader, listenPort);
402
+ if (hostDecision === "deny") {
403
+ send(res, 400, { error: "host-not-allowed" });
404
+ req.resume();
405
+ return;
406
+ }
407
+ }
408
+ if (req.headers.origin !== undefined) {
409
+ const origin = Array.isArray(req.headers.origin) ? (req.headers.origin[0] ?? "") : req.headers.origin;
410
+ const allowed = config.allowedOrigins ?? [];
411
+ if (!allowed.includes(origin)) {
412
+ send(res, 403, { error: "origin-not-allowed" });
413
+ req.resume();
414
+ return;
415
+ }
416
+ }
343
417
  const url = new URL(req.url ?? "/", `http://${req.headers.host ?? "127.0.0.1"}`);
344
418
  if (req.method === "POST" && contentLengthOverLimit(req)) {
345
419
  send(res, 413, { error: "payload-too-large" });
@@ -347,6 +421,14 @@ export async function listen(config) {
347
421
  return;
348
422
  }
349
423
  if (req.method === "GET" && url.pathname === "/healthz") {
424
+ // Local mode reads VERAX_JWKS_FILE. Any process that can read that key
425
+ // file can mint verax:audit, so counts are not a door here: liveness only.
426
+ const installNonce = readInstallHealthNonce(config.stateDir);
427
+ const nonceField = installNonce === null ? {} : { nonce: installNonce };
428
+ if (config.jwksFile) {
429
+ send(res, 200, { ok: true, ...nonceField });
430
+ return;
431
+ }
350
432
  const token = readBearer(req.headers.authorization);
351
433
  let canRead = false;
352
434
  if (token) {
@@ -367,7 +449,7 @@ export async function listen(config) {
367
449
  }
368
450
  }
369
451
  if (!canRead) {
370
- send(res, 200, { ok: true });
452
+ send(res, 200, { ok: true, ...nonceField });
371
453
  return;
372
454
  }
373
455
  // The ledger counts its own lines as it writes them; asking it is free.
@@ -389,6 +471,7 @@ export async function listen(config) {
389
471
  }
390
472
  send(res, 200, {
391
473
  ok: true,
474
+ ...nonceField,
392
475
  ...counted,
393
476
  lock: services.ledger.lockStatus(),
394
477
  heartbeat: readHeartbeat(config.stateDir),
@@ -421,7 +504,16 @@ export async function listen(config) {
421
504
  const contest = req.method === "POST" && url.pathname.startsWith("/api/contest/");
422
505
  const apiApprove = req.method === "POST" && url.pathname === "/api/approve";
423
506
  const apiAgents = req.method === "GET" && url.pathname === "/api/agents";
424
- if (url.pathname !== "/mcp" && !apiLedger && !apiInventory && !contest && !apiApprove && !apiAgents) {
507
+ const apiHalt = url.pathname === "/api/halt" && (req.method === "GET" || req.method === "POST");
508
+ const apiResume = req.method === "POST" && url.pathname === "/api/resume";
509
+ if (url.pathname !== "/mcp" &&
510
+ !apiLedger &&
511
+ !apiInventory &&
512
+ !contest &&
513
+ !apiApprove &&
514
+ !apiAgents &&
515
+ !apiHalt &&
516
+ !apiResume) {
425
517
  send(res, 404, { error: "not-found" });
426
518
  return;
427
519
  }
@@ -446,7 +538,43 @@ export async function listen(config) {
446
538
  send(res, 401, { error: "unauthorized" }, { "www-authenticate": wwwAuthenticate(config.audience) });
447
539
  return;
448
540
  }
541
+ const refuseStaleBearer = async () => {
542
+ if (bearerStillLive(verified.payload, config.stateDir))
543
+ return false;
544
+ await bumpUnauthenticated(config.stateDir);
545
+ send(res, 401, { error: "unauthorized" }, { "www-authenticate": wwwAuthenticate(config.audience) });
546
+ return true;
547
+ };
548
+ // Local mode: any process that can read the key file can mint
549
+ // verax:approve or verax:audit, so those scopes are not honoured at all.
550
+ // Nothing is written; the refusal is the whole answer.
551
+ if (config.jwksFile &&
552
+ (verified.principal.scopes.has("verax:approve") || verified.principal.scopes.has("verax:audit"))) {
553
+ send(res, 403, { error: "local-mode-operator-scope" });
554
+ return;
555
+ }
449
556
  try {
557
+ if (apiHalt || apiResume) {
558
+ if (await refuseStaleBearer())
559
+ return;
560
+ const scopes = verified.principal.scopes;
561
+ const operator = scopes.has("verax:audit") || scopes.has("verax:approve");
562
+ // Stopping is the safe direction, so any operator session may do it.
563
+ // Lifting a halt lets everything through again: that is an approval of
564
+ // everything at once and needs the approve scope. The agent token
565
+ // carries neither, so an agent cannot stop itself being stopped.
566
+ if (!operator || (apiResume && !scopes.has("verax:approve"))) {
567
+ send(res, 403, { error: "scope-missing" });
568
+ return;
569
+ }
570
+ if (req.method === "GET") {
571
+ send(res, 200, readHalt(config.stateDir));
572
+ return;
573
+ }
574
+ const by = verified.principal.brain;
575
+ send(res, 200, apiResume ? resumeBody(config.stateDir, by, "http") : haltBody(config.stateDir, by, "http"));
576
+ return;
577
+ }
450
578
  if (apiApprove) {
451
579
  // Reading the ledger is not approving from it. The audit scope opens
452
580
  // every door above; this one signs a new decision and lets money go,
@@ -456,6 +584,8 @@ export async function listen(config) {
456
584
  return;
457
585
  }
458
586
  const parsed = await readJsonBody(req, MAX_BODY_BYTES);
587
+ if (await refuseStaleBearer())
588
+ return;
459
589
  if (!parsed.ok) {
460
590
  send(res, 400, { error: "bad-body" });
461
591
  return;
@@ -504,9 +634,14 @@ export async function listen(config) {
504
634
  policy: services.policy,
505
635
  approvals,
506
636
  now: () => Date.now(),
637
+ ledger: services.ledger,
507
638
  }),
508
639
  });
509
640
  if (!outcome.ok) {
641
+ if (outcome.reason === "halted") {
642
+ send(res, 409, { error: "approve-halted" });
643
+ return;
644
+ }
510
645
  const code = outcome.reason === "unknown-ref" || outcome.reason === "snapshot-missing" ? 404 : 409;
511
646
  send(res, code, {
512
647
  error: outcome.reason,
@@ -518,6 +653,8 @@ export async function listen(config) {
518
653
  return;
519
654
  }
520
655
  if (apiLedger || apiInventory || contest || apiAgents) {
656
+ if (await refuseStaleBearer())
657
+ return;
521
658
  // The audit doors hand out the whole ledger: every tenant's decisions, the
522
659
  // inputs documents that name their principals, and the approval snapshots
523
660
  // that carry spend arguments. `verax:read` is a brain scope, so it cannot be
@@ -612,6 +749,8 @@ export async function listen(config) {
612
749
  });
613
750
  await mcp.connect(transport);
614
751
  const parsed = req.method === "POST" ? await readJsonBody(req, MAX_BODY_BYTES) : { ok: true, value: undefined };
752
+ if (await refuseStaleBearer())
753
+ return;
615
754
  if (parsed.ok === false && "tooLarge" in parsed) {
616
755
  send(res, 413, { error: "payload-too-large" });
617
756
  req.destroy();
@@ -696,6 +835,8 @@ export async function listen(config) {
696
835
  // Swallow so a bad Host cannot reject the request listener.
697
836
  }
698
837
  });
838
+ server.requestTimeout = REQUEST_TIMEOUT_MS;
839
+ server.headersTimeout = HEADERS_TIMEOUT_MS;
699
840
  await new Promise((resolve, reject) => {
700
841
  server.once("error", reject);
701
842
  server.listen(config.bindPort, config.bindHost, () => resolve());
@@ -704,6 +845,18 @@ export async function listen(config) {
704
845
  services.ledger.close();
705
846
  void closeAll(sessions);
706
847
  });
848
+ const bound = server.address();
849
+ if (!bound || typeof bound === "string") {
850
+ await new Promise((resolve) => server.close(() => resolve()));
851
+ throw new Error("listen-port");
852
+ }
853
+ try {
854
+ services.ledger.recordListenPort(bound.port);
855
+ }
856
+ catch (err) {
857
+ await new Promise((resolve) => server.close(() => resolve()));
858
+ throw err;
859
+ }
707
860
  return server;
708
861
  }
709
862
  export { TOOL_NAMES };
@@ -1,2 +1,5 @@
1
- import { type ExplainOpts, type Ledger, type ToolCall, type ToolResult } from "@verax-ai/proxy";
2
- export declare function auditExplain(call: ToolCall, ledger: Ledger, opts?: ExplainOpts): Promise<ToolResult>;
1
+ import { type ExplainOpts, type Ledger, type Principal, type ToolCall, type ToolResult } from "@verax-ai/proxy";
2
+ export declare function auditExplain(call: ToolCall, ledger: Ledger, opts?: ExplainOpts, caller?: {
3
+ principal: Principal;
4
+ stateDir: string;
5
+ }): Promise<ToolResult>;
@@ -1,5 +1,6 @@
1
- import { explain, spokenReason, } from "@verax-ai/proxy";
2
- export async function auditExplain(call, ledger, opts) {
1
+ import { explain, spokenReason, tenantKey, } from "@verax-ai/proxy";
2
+ import { inputsPrincipal } from "../inputs-read.js";
3
+ export async function auditExplain(call, ledger, opts, caller) {
3
4
  const ref = call.arguments.ref;
4
5
  if (typeof ref !== "string" || ref === "") {
5
6
  return {
@@ -7,6 +8,15 @@ export async function auditExplain(call, ledger, opts) {
7
8
  isError: true,
8
9
  };
9
10
  }
11
+ if (caller) {
12
+ const owner = await inputsPrincipal(caller.stateDir, ref);
13
+ if (owner !== null && tenantKey(owner) !== tenantKey(caller.principal)) {
14
+ return {
15
+ content: [{ type: "text", text: JSON.stringify({ error: spokenReason("tenant-mismatch") }) }],
16
+ isError: true,
17
+ };
18
+ }
19
+ }
10
20
  let result;
11
21
  try {
12
22
  result = await explain(ledger, ref, opts);
@@ -1,13 +1,27 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { existsSync, readdirSync } from "node:fs";
3
- import { mkdir, readFile, writeFile } from "node:fs/promises";
3
+ import { mkdir, readdir, readFile, stat, writeFile } from "node:fs/promises";
4
4
  import { resolve, sep } from "node:path";
5
5
  import { canonical } from "@cedulon/core";
6
6
  import { tenantKey } from "@verax-ai/proxy";
7
+ import { memoryQuotaBytes } from "../config.js";
7
8
  function sha256Canonical(value) {
8
9
  return createHash("sha256").update(canonical(value), "utf8").digest("hex");
9
10
  }
10
11
  const ID_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/;
12
+ /** One in-process chain per tenant memory directory. The body is one process per state dir. */
13
+ const memoryQuotaChain = new Map();
14
+ function withMemoryQuotaLock(key, fn) {
15
+ const previous = memoryQuotaChain.get(key) ?? Promise.resolve();
16
+ const run = previous.then(fn, fn);
17
+ const settled = run.then(() => undefined, () => undefined);
18
+ memoryQuotaChain.set(key, settled);
19
+ void settled.then(() => {
20
+ if (memoryQuotaChain.get(key) === settled)
21
+ memoryQuotaChain.delete(key);
22
+ });
23
+ return run;
24
+ }
11
25
  function jsonResult(value, isError = false) {
12
26
  return {
13
27
  content: [{ type: "text", text: JSON.stringify(value) }],
@@ -135,10 +149,31 @@ export async function memoryPut(call, stateDir, principal) {
135
149
  versionHash,
136
150
  };
137
151
  const dir = resolve(stateDir, "tenants", tenantKey(principal), "memory");
138
- await mkdir(dir, { recursive: true, mode: 0o700 });
139
- await writeFile(path, `${JSON.stringify(rec)}\n`, {
140
- encoding: "utf8",
141
- mode: 0o600,
152
+ const payload = `${JSON.stringify(rec)}\n`;
153
+ return withMemoryQuotaLock(dir, async () => {
154
+ let used = 0;
155
+ try {
156
+ for (const name of await readdir(dir)) {
157
+ const file = resolve(dir, name);
158
+ if (file === path)
159
+ continue;
160
+ const info = await stat(file);
161
+ if (info.isFile())
162
+ used += info.size;
163
+ }
164
+ }
165
+ catch (err) {
166
+ if (err.code !== "ENOENT")
167
+ throw err;
168
+ }
169
+ if (used + Buffer.byteLength(payload) > memoryQuotaBytes()) {
170
+ return jsonResult({ error: "memory-quota" }, true);
171
+ }
172
+ await mkdir(dir, { recursive: true, mode: 0o700 });
173
+ await writeFile(path, payload, {
174
+ encoding: "utf8",
175
+ mode: 0o600,
176
+ });
177
+ return jsonResult({ ok: true, id, versionHash });
142
178
  });
143
- return jsonResult({ ok: true, id, versionHash });
144
179
  }
@@ -1,7 +1,21 @@
1
- import { appendFileSync, mkdirSync } from "node:fs";
1
+ import { appendFileSync, mkdirSync, statSync } from "node:fs";
2
2
  import { readFile } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import { tenantKey } from "@verax-ai/proxy";
5
+ import { messageQuotaBytes } from "../config.js";
6
+ /** One in-process chain per tenant directory. The body is one process per state dir. */
7
+ const messageQuotaChain = new Map();
8
+ function withMessageQuotaLock(key, fn) {
9
+ const previous = messageQuotaChain.get(key) ?? Promise.resolve();
10
+ const run = previous.then(fn, fn);
11
+ const settled = run.then(() => undefined, () => undefined);
12
+ messageQuotaChain.set(key, settled);
13
+ void settled.then(() => {
14
+ if (messageQuotaChain.get(key) === settled)
15
+ messageQuotaChain.delete(key);
16
+ });
17
+ return run;
18
+ }
5
19
  function tenantDir(stateDir, principal) {
6
20
  return join(stateDir, "tenants", tenantKey(principal));
7
21
  }
@@ -32,13 +46,28 @@ export async function messageSend(call, stateDir, ref, principal) {
32
46
  ref,
33
47
  };
34
48
  const dir = tenantDir(stateDir, principal);
35
- mkdirSync(dir, { recursive: true, mode: 0o700 });
36
- appendFileSync(join(dir, "outbox.jsonl"), `${JSON.stringify(row)}\n`, {
37
- encoding: "utf8",
38
- mode: 0o600,
49
+ const file = join(dir, "outbox.jsonl");
50
+ const payload = `${JSON.stringify(row)}\n`;
51
+ return withMessageQuotaLock(dir, async () => {
52
+ let used = 0;
53
+ try {
54
+ used = statSync(file).size;
55
+ }
56
+ catch (err) {
57
+ if (err.code !== "ENOENT")
58
+ throw err;
59
+ }
60
+ if (used + Buffer.byteLength(payload) > messageQuotaBytes()) {
61
+ return {
62
+ content: [{ type: "text", text: JSON.stringify({ error: "message-quota" }) }],
63
+ isError: true,
64
+ };
65
+ }
66
+ mkdirSync(dir, { recursive: true, mode: 0o700 });
67
+ appendFileSync(file, payload, { encoding: "utf8", mode: 0o600 });
68
+ return {
69
+ content: [{ type: "text", text: JSON.stringify({ queued: true, ref }) }],
70
+ isError: false,
71
+ };
39
72
  });
40
- return {
41
- content: [{ type: "text", text: JSON.stringify({ queued: true, ref }) }],
42
- isError: false,
43
- };
44
73
  }
package/dist/unlock.d.ts CHANGED
@@ -1,8 +1,11 @@
1
1
  export declare function pidAlive(pid: number): boolean;
2
- export declare function readLockFile(path: string): {
2
+ export type LockRecord = {
3
3
  pid: number;
4
4
  startedAt: number;
5
- } | null;
5
+ port?: number;
6
+ };
7
+ /** A lock from before the body recorded its port has no `port`. That is still a lock. */
8
+ export declare function readLockFile(path: string): LockRecord | null;
6
9
  export declare function runUnlock(stateDir: string, writeErr?: (s: string) => void, options?: {
7
10
  force?: boolean;
8
11
  unlink?: (path: string) => void;