@verax-ai/body 0.5.0 → 0.6.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.
- package/README.md +4 -3
- package/dist/approval-signature.d.ts +19 -0
- package/dist/approval-signature.js +70 -5
- package/dist/approve-cli.js +20 -24
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +146 -14
- package/dist/config.d.ts +23 -0
- package/dist/config.js +41 -0
- package/dist/control-signature.d.ts +4 -0
- package/dist/control-signature.js +20 -5
- package/dist/definitions.d.ts +9 -0
- package/dist/definitions.js +53 -0
- package/dist/demo.js +15 -12
- package/dist/doctor.js +72 -0
- package/dist/downstream-pin.d.ts +9 -0
- package/dist/downstream-pin.js +273 -0
- package/dist/downstream.d.ts +22 -2
- package/dist/downstream.js +312 -44
- package/dist/heartbeat.d.ts +26 -2
- package/dist/heartbeat.js +90 -7
- package/dist/held-spend.d.ts +16 -0
- package/dist/held-spend.js +20 -0
- package/dist/http-io.d.ts +14 -0
- package/dist/http-io.js +46 -0
- package/dist/init-local.d.ts +11 -0
- package/dist/init-local.js +32 -3
- package/dist/install.d.ts +5 -0
- package/dist/install.js +37 -5
- package/dist/main.d.ts +1 -0
- package/dist/main.js +34 -5
- package/dist/operator-cli.d.ts +49 -1
- package/dist/operator-cli.js +132 -7
- package/dist/operator-credentials.d.ts +40 -1
- package/dist/operator-credentials.js +97 -7
- package/dist/operator-door-page.d.ts +3 -0
- package/dist/operator-door-page.js +819 -0
- package/dist/operator-door.d.ts +22 -0
- package/dist/operator-door.js +487 -0
- package/dist/operator-handlers.d.ts +28 -0
- package/dist/operator-handlers.js +221 -0
- package/dist/server.d.ts +2 -0
- package/dist/server.js +108 -255
- package/dist/verify-cli.js +30 -10
- package/dist/webauthn-verify.d.ts +48 -4
- package/dist/webauthn-verify.js +183 -25
- package/package.json +4 -4
package/dist/downstream.js
CHANGED
|
@@ -1,12 +1,17 @@
|
|
|
1
|
+
import { createHash, createPublicKey } from "node:crypto";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
1
4
|
import { Readable } from "node:stream";
|
|
2
|
-
import {
|
|
3
|
-
import { effectDescriptor, samePublicKey, sha256Canonical, thirdPartyReceiptRejection } from "@verax-ai/proxy";
|
|
5
|
+
import { canonical } from "@cedulon/core";
|
|
6
|
+
import { DISPLAY_ESCAPE_CLASS, effectDescriptor, samePublicKey, sha256Canonical, terminalText, thirdPartyReceiptRejection } from "@verax-ai/proxy";
|
|
4
7
|
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
5
8
|
import { getDefaultEnvironment, StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
6
9
|
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
7
10
|
import { CallToolResultSchema, ListToolsResultSchema } from "@modelcontextprotocol/sdk/types.js";
|
|
8
11
|
const PREFIX_RE = /^[A-Za-z][A-Za-z0-9_-]{0,31}$/;
|
|
9
|
-
|
|
12
|
+
/** `sha256:` plus 64 lowercase hex digits. The capture is the file name. */
|
|
13
|
+
const PINS_RE = /^sha256:([0-9a-f]{64})$/;
|
|
14
|
+
export const CHILD_TOOL_RE = /^[A-Za-z][A-Za-z0-9._-]{0,63}$/;
|
|
10
15
|
/** Prefixed name a caller may put on extraTools: `prefix.childName`. */
|
|
11
16
|
const EXTRA_NAME_RE = /^[A-Za-z][A-Za-z0-9_-]{0,31}\.[A-Za-z][A-Za-z0-9._-]{0,63}$/;
|
|
12
17
|
// Sanity bound, not a capacity claim. A child that lists more than this is
|
|
@@ -182,6 +187,12 @@ export function parseDownstreamJson(raw, bodyPublicKeyPems = []) {
|
|
|
182
187
|
}
|
|
183
188
|
spec.tools = [...names];
|
|
184
189
|
}
|
|
190
|
+
if (rec.pins !== undefined) {
|
|
191
|
+
if (typeof rec.pins !== "string" || !PINS_RE.test(rec.pins)) {
|
|
192
|
+
throw new Error(`downstream-pins-invalid:${spec.prefix}`);
|
|
193
|
+
}
|
|
194
|
+
spec.pins = rec.pins;
|
|
195
|
+
}
|
|
185
196
|
assertStdioTrusted(spec);
|
|
186
197
|
return spec;
|
|
187
198
|
}
|
|
@@ -236,8 +247,12 @@ async function shut(client, transport) {
|
|
|
236
247
|
await client.close().catch(() => undefined);
|
|
237
248
|
await transport.close().catch(() => undefined);
|
|
238
249
|
}
|
|
239
|
-
/**
|
|
240
|
-
|
|
250
|
+
/**
|
|
251
|
+
* Spawns the child. Stderr is drained so a chatty child cannot block it.
|
|
252
|
+
* Pass `stderrChunks` to keep the bytes instead (the pin command shows them);
|
|
253
|
+
* the same cap, and collection stops at the cap instead of growing without bound.
|
|
254
|
+
*/
|
|
255
|
+
function stdioTransport(spec, stderrChunks) {
|
|
241
256
|
if (spec.command === undefined || spec.command.trim() === "") {
|
|
242
257
|
throw new Error("downstream-command-invalid");
|
|
243
258
|
}
|
|
@@ -258,10 +273,21 @@ function stdioTransport(spec) {
|
|
|
258
273
|
});
|
|
259
274
|
// The SDK types the pipe as Stream; only a Readable can be drained.
|
|
260
275
|
const stderr = transport.stderr;
|
|
261
|
-
if (stderr instanceof Readable)
|
|
262
|
-
|
|
276
|
+
if (stderr instanceof Readable) {
|
|
277
|
+
if (stderrChunks === undefined)
|
|
278
|
+
stderr.resume();
|
|
279
|
+
else
|
|
280
|
+
collectStderr(stderr, stderrChunks);
|
|
281
|
+
}
|
|
263
282
|
return transport;
|
|
264
283
|
}
|
|
284
|
+
function collectStderr(stderr, chunks) {
|
|
285
|
+
stderr.on("data", (chunk) => {
|
|
286
|
+
if (chunks.reduce((n, c) => n + c.length, 0) >= MAX_CHILD_MESSAGE_BYTES)
|
|
287
|
+
return;
|
|
288
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
265
291
|
/**
|
|
266
292
|
* Talks to a server already running. The headers are the operator's, from the
|
|
267
293
|
* document — the body's own bearer is never forwarded, for the same
|
|
@@ -357,6 +383,115 @@ async function connectWithDeadline(client, transport, timeoutMs, prefix) {
|
|
|
357
383
|
clearTimeout(timer);
|
|
358
384
|
}
|
|
359
385
|
}
|
|
386
|
+
/**
|
|
387
|
+
* Object keys in the order `canonical` uses. Array order stays: it can carry
|
|
388
|
+
* meaning. Two schemas that differ only by key order are then published the same.
|
|
389
|
+
*/
|
|
390
|
+
function sortedObject(value) {
|
|
391
|
+
return JSON.parse(canonical(value));
|
|
392
|
+
}
|
|
393
|
+
function manifestInvalid(prefix) {
|
|
394
|
+
throw new Error(`downstream-manifest-invalid:${prefix}`);
|
|
395
|
+
}
|
|
396
|
+
/** One read. The digest is of those bytes, not of a re-encoded document. */
|
|
397
|
+
function readPinnedManifest(prefix, pins, definitionsDir) {
|
|
398
|
+
const digest = PINS_RE.exec(pins)?.[1];
|
|
399
|
+
if (digest === undefined)
|
|
400
|
+
throw new Error(`downstream-pins-invalid:${prefix}`);
|
|
401
|
+
if (definitionsDir === undefined || definitionsDir === "")
|
|
402
|
+
manifestInvalid(prefix);
|
|
403
|
+
let bytes;
|
|
404
|
+
try {
|
|
405
|
+
bytes = readFileSync(join(definitionsDir, "definitions", `${digest}.json`));
|
|
406
|
+
}
|
|
407
|
+
catch (err) {
|
|
408
|
+
// A missing or corrupt file is not the same fact as a file this account
|
|
409
|
+
// is not allowed to read.
|
|
410
|
+
const code = err.code;
|
|
411
|
+
if (code === "EACCES" || code === "EPERM")
|
|
412
|
+
throw new Error(`downstream-manifest-unreadable:${prefix}`);
|
|
413
|
+
manifestInvalid(prefix);
|
|
414
|
+
}
|
|
415
|
+
if (createHash("sha256").update(bytes).digest("hex") !== digest)
|
|
416
|
+
manifestInvalid(prefix);
|
|
417
|
+
let parsed;
|
|
418
|
+
try {
|
|
419
|
+
parsed = JSON.parse(bytes.toString("utf8"));
|
|
420
|
+
}
|
|
421
|
+
catch {
|
|
422
|
+
manifestInvalid(prefix);
|
|
423
|
+
}
|
|
424
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed))
|
|
425
|
+
manifestInvalid(prefix);
|
|
426
|
+
const rec = parsed;
|
|
427
|
+
if (rec.v !== 1)
|
|
428
|
+
manifestInvalid(prefix);
|
|
429
|
+
if (rec.tools === null || typeof rec.tools !== "object" || Array.isArray(rec.tools))
|
|
430
|
+
manifestInvalid(prefix);
|
|
431
|
+
return { prefix: rec.prefix, selected: rec.selected, tools: rec.tools };
|
|
432
|
+
}
|
|
433
|
+
/** Both absent (no `tools`, and `selected` null or missing) is the same selection. */
|
|
434
|
+
function sameSelection(specTools, selected) {
|
|
435
|
+
const left = specTools === undefined ? null : specTools;
|
|
436
|
+
const right = selected === undefined || selected === null ? null : selected;
|
|
437
|
+
if (left === null || right === null)
|
|
438
|
+
return left === null && right === null;
|
|
439
|
+
if (!Array.isArray(right) || right.some((name) => typeof name !== "string"))
|
|
440
|
+
return false;
|
|
441
|
+
const wanted = new Set(left);
|
|
442
|
+
const pinned = new Set(right);
|
|
443
|
+
if (wanted.size !== pinned.size)
|
|
444
|
+
return false;
|
|
445
|
+
for (const name of wanted)
|
|
446
|
+
if (!pinned.has(name))
|
|
447
|
+
return false;
|
|
448
|
+
return true;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* With no selection the listed names and the pinned names are one set.
|
|
452
|
+
* With a selection only those names are compared; a selected name with no
|
|
453
|
+
* pin is unpinned. The digest is the whole parsed entry, not a list of fields.
|
|
454
|
+
*/
|
|
455
|
+
function assertPinnedDefinitions(spec, offered, manifestTools) {
|
|
456
|
+
const listedNames = offered.map((tool) => tool.name);
|
|
457
|
+
if (spec.tools === undefined) {
|
|
458
|
+
for (const name of listedNames) {
|
|
459
|
+
if (!Object.hasOwn(manifestTools, name)) {
|
|
460
|
+
throw new Error(`downstream-tool-unpinned:${spec.prefix}.${name}`);
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
const listed = new Set(listedNames);
|
|
464
|
+
for (const name of Object.keys(manifestTools)) {
|
|
465
|
+
if (!listed.has(name))
|
|
466
|
+
throw new Error(`downstream-tool-unlisted:${spec.prefix}.${name}`);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
else {
|
|
470
|
+
for (const name of spec.tools) {
|
|
471
|
+
if (!Object.hasOwn(manifestTools, name)) {
|
|
472
|
+
throw new Error(`downstream-tool-unpinned:${spec.prefix}.${name}`);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
const byName = new Map(offered.map((tool) => [tool.name, tool]));
|
|
477
|
+
for (const name of spec.tools ?? listedNames) {
|
|
478
|
+
const listed = byName.get(name);
|
|
479
|
+
if (listed === undefined || !Object.hasOwn(manifestTools, name))
|
|
480
|
+
continue;
|
|
481
|
+
let listedDigest;
|
|
482
|
+
let pinnedDigest;
|
|
483
|
+
try {
|
|
484
|
+
listedDigest = sha256Canonical(listed);
|
|
485
|
+
pinnedDigest = sha256Canonical(manifestTools[name]);
|
|
486
|
+
}
|
|
487
|
+
catch {
|
|
488
|
+
throw new Error(`downstream-definition-unencodable:${spec.prefix}.${name}`);
|
|
489
|
+
}
|
|
490
|
+
if (listedDigest !== pinnedDigest) {
|
|
491
|
+
throw new Error(`downstream-definition-changed:${spec.prefix}.${name}`);
|
|
492
|
+
}
|
|
493
|
+
}
|
|
494
|
+
}
|
|
360
495
|
function attachTimeoutError(err, prefix) {
|
|
361
496
|
const msg = err instanceof Error ? err.message : String(err);
|
|
362
497
|
if (msg.startsWith("downstream-attach-timeout:")) {
|
|
@@ -367,7 +502,137 @@ function attachTimeoutError(err, prefix) {
|
|
|
367
502
|
}
|
|
368
503
|
return err instanceof Error ? err : new Error(msg);
|
|
369
504
|
}
|
|
370
|
-
|
|
505
|
+
/**
|
|
506
|
+
* A child string that has not passed the name pattern, as it may be printed.
|
|
507
|
+
* JSON quotes it and writes controls, quotes and backslashes as escapes; the
|
|
508
|
+
* display class then writes every remaining mark, unassigned, private-use or
|
|
509
|
+
* surrogate code point as `\uXXXX`, one escape per UTF-16 unit, the same
|
|
510
|
+
* shape the pin command uses. A name that already matched the pattern is not
|
|
511
|
+
* passed through here.
|
|
512
|
+
*/
|
|
513
|
+
function shown(text) {
|
|
514
|
+
return JSON.stringify(text).replace(new RegExp(`[${DISPLAY_ESCAPE_CLASS}]`, "gu"), (ch) => {
|
|
515
|
+
const cp = ch.codePointAt(0) ?? 0;
|
|
516
|
+
if (cp <= 0xffff)
|
|
517
|
+
return `\\u${cp.toString(16).padStart(4, "0")}`;
|
|
518
|
+
const rest = cp - 0x10000;
|
|
519
|
+
const high = 0xd800 + (rest >> 10);
|
|
520
|
+
const low = 0xdc00 + (rest & 0x3ff);
|
|
521
|
+
return `\\u${high.toString(16).padStart(4, "0")}\\u${low.toString(16).padStart(4, "0")}`;
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
/**
|
|
525
|
+
* The child's `tools/list`, held to one set of bounds. A `tools` selection
|
|
526
|
+
* is applied first: anything not selected is never read past its name (no
|
|
527
|
+
* size check, no collision check). Both attach and the pin command use this,
|
|
528
|
+
* so a name, a size, or a collision the body would refuse is refused here too.
|
|
529
|
+
*/
|
|
530
|
+
function acceptedTools(spec, listed) {
|
|
531
|
+
if (listed.length > MAX_CHILD_TOOLS) {
|
|
532
|
+
throw new Error(`downstream-too-many-tools:${spec.prefix}:${listed.length}`);
|
|
533
|
+
}
|
|
534
|
+
const wanted = spec.tools === undefined ? null : new Set(spec.tools);
|
|
535
|
+
const offered = wanted === null ? [...listed] : listed.filter((tool) => wanted.has(tool.name));
|
|
536
|
+
if (wanted !== null) {
|
|
537
|
+
for (const name of wanted) {
|
|
538
|
+
if (!offered.some((tool) => tool.name === name)) {
|
|
539
|
+
throw new Error(`downstream-tool-missing:${spec.prefix}.${name}`);
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
const seen = new Set();
|
|
544
|
+
for (const tool of offered) {
|
|
545
|
+
if (!CHILD_TOOL_RE.test(tool.name)) {
|
|
546
|
+
throw new Error(`downstream-child-name-invalid:${shown(tool.name)}`);
|
|
547
|
+
}
|
|
548
|
+
const toolBytes = Buffer.byteLength(JSON.stringify(tool), "utf8");
|
|
549
|
+
if (toolBytes > MAX_TOOL_BYTES) {
|
|
550
|
+
throw new Error(`downstream-tool-too-large:${spec.prefix}.${tool.name}:${toolBytes}`);
|
|
551
|
+
}
|
|
552
|
+
const published = prefixedName(spec.prefix, tool.name);
|
|
553
|
+
if (seen.has(published)) {
|
|
554
|
+
throw new Error(`downstream-name-collision:${published}`);
|
|
555
|
+
}
|
|
556
|
+
seen.add(published);
|
|
557
|
+
// Canonical JSON refuses a non-finite number. That entry cannot be pinned
|
|
558
|
+
// or republished. A lone surrogate is left as text and compared as such.
|
|
559
|
+
try {
|
|
560
|
+
canonical(tool);
|
|
561
|
+
}
|
|
562
|
+
catch {
|
|
563
|
+
throw new Error(`downstream-definition-unencodable:${spec.prefix}.${tool.name}`);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
return offered;
|
|
567
|
+
}
|
|
568
|
+
function plainTool(tool) {
|
|
569
|
+
const parsed = JSON.parse(canonical(tool));
|
|
570
|
+
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
571
|
+
throw new Error("downstream-child-name-invalid");
|
|
572
|
+
}
|
|
573
|
+
return parsed;
|
|
574
|
+
}
|
|
575
|
+
/** Entries keyed by the child's own name. A null prototype keeps `__proto__` and `constructor` ordinary. */
|
|
576
|
+
function snapshotRecord(offered) {
|
|
577
|
+
const tools = Object.create(null);
|
|
578
|
+
for (const tool of offered)
|
|
579
|
+
tools[tool.name] = plainTool(tool);
|
|
580
|
+
return tools;
|
|
581
|
+
}
|
|
582
|
+
// Asked as a plain request, not through client.listTools: that call compiles every
|
|
583
|
+
// outputSchema the child lists into a validator, and client.callTool then runs it
|
|
584
|
+
// on the child's own result inside this process. A child's schema is the child's
|
|
585
|
+
// input; a backtracking pattern in it held the body for seconds per call.
|
|
586
|
+
function requestToolList(client, timeoutMs) {
|
|
587
|
+
return client.request({ method: "tools/list" }, ListToolsResultSchema, { timeout: timeoutMs });
|
|
588
|
+
}
|
|
589
|
+
/**
|
|
590
|
+
* Connects the child once, takes `tools/list` under the same bounds as
|
|
591
|
+
* `openDownstream`, collects stderr, and closes. The returned tools are the
|
|
592
|
+
* snapshot that is shown and, after the operator types yes, written. Listing
|
|
593
|
+
* again would let the child answer differently from the text that was read.
|
|
594
|
+
* On failure the error carries `stderr` when the child wrote any.
|
|
595
|
+
*/
|
|
596
|
+
export async function listDownstreamOnce(spec) {
|
|
597
|
+
const timeoutMs = spec.timeoutMs ?? 10_000;
|
|
598
|
+
const overflow = { hit: false, detach: () => undefined };
|
|
599
|
+
const stderrChunks = [];
|
|
600
|
+
const transport = spec.url !== undefined ? httpTransport(spec, overflow) : stdioTransport(spec, stderrChunks);
|
|
601
|
+
const client = new Client({ name: "verax-downstream-pin", version: "0.0.0" });
|
|
602
|
+
overflow.detach = () => void transport.close().catch(() => undefined);
|
|
603
|
+
// On stdio the client's read buffer enforces the bound and closes the child.
|
|
604
|
+
// This is where it says so; the text is the client's.
|
|
605
|
+
client.onerror = (err) => {
|
|
606
|
+
if (/ReadBuffer exceeded maximum size/.test(err.message))
|
|
607
|
+
overflow.hit = true;
|
|
608
|
+
};
|
|
609
|
+
const stderrNow = () => Buffer.concat(stderrChunks).toString("utf8");
|
|
610
|
+
try {
|
|
611
|
+
await connectWithDeadline(client, transport, timeoutMs, spec.prefix);
|
|
612
|
+
const listed = await requestToolList(client, timeoutMs);
|
|
613
|
+
return { tools: snapshotRecord(acceptedTools(spec, listed.tools)), stderr: stderrNow() };
|
|
614
|
+
}
|
|
615
|
+
catch (err) {
|
|
616
|
+
const stderr = stderrNow();
|
|
617
|
+
const message = err instanceof Error ? err.message : "";
|
|
618
|
+
if (overflow.hit && !message.startsWith("downstream-")) {
|
|
619
|
+
const tooLarge = new Error(`${MESSAGE_TOO_LARGE}:${spec.prefix}`);
|
|
620
|
+
tooLarge.stderr = stderr;
|
|
621
|
+
throw tooLarge;
|
|
622
|
+
}
|
|
623
|
+
if (err instanceof Error) {
|
|
624
|
+
err.stderr = stderr;
|
|
625
|
+
throw err;
|
|
626
|
+
}
|
|
627
|
+
const wrapped = new Error("downstream-pin-failed");
|
|
628
|
+
wrapped.stderr = stderr;
|
|
629
|
+
throw wrapped;
|
|
630
|
+
}
|
|
631
|
+
finally {
|
|
632
|
+
await shut(client, transport);
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
export async function openDownstream(spec, opts) {
|
|
371
636
|
if (spec.effectKeyPem !== undefined)
|
|
372
637
|
assertEffectKey(spec.effectKeyPem);
|
|
373
638
|
if (!PREFIX_RE.test(spec.prefix)) {
|
|
@@ -396,11 +661,7 @@ export async function openDownstream(spec) {
|
|
|
396
661
|
let listed;
|
|
397
662
|
try {
|
|
398
663
|
await connectWithDeadline(client, transport, timeoutMs, spec.prefix);
|
|
399
|
-
|
|
400
|
-
// outputSchema the child lists into a validator, and client.callTool then runs it
|
|
401
|
-
// on the child's own result inside this process. A child's schema is the child's
|
|
402
|
-
// input; a backtracking pattern in it held the body for seconds per call.
|
|
403
|
-
listed = await client.request({ method: "tools/list" }, ListToolsResultSchema, { timeout: timeoutMs });
|
|
664
|
+
listed = await requestToolList(client, timeoutMs);
|
|
404
665
|
}
|
|
405
666
|
catch (err) {
|
|
406
667
|
await shut(client, transport);
|
|
@@ -408,47 +669,35 @@ export async function openDownstream(spec) {
|
|
|
408
669
|
throw new Error(`${MESSAGE_TOO_LARGE}:${spec.prefix}`);
|
|
409
670
|
throw attachTimeoutError(err, spec.prefix);
|
|
410
671
|
}
|
|
411
|
-
if (listed.tools.length > MAX_CHILD_TOOLS) {
|
|
412
|
-
await shut(client, transport);
|
|
413
|
-
throw new Error(`downstream-too-many-tools:${spec.prefix}:${listed.tools.length}`);
|
|
414
|
-
}
|
|
415
672
|
// With a `tools` list the rest of the child's list is never read past its
|
|
416
673
|
// name: no size check, no registry entry, nothing republished.
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
}
|
|
674
|
+
let offered;
|
|
675
|
+
try {
|
|
676
|
+
offered = acceptedTools(spec, listed.tools);
|
|
677
|
+
}
|
|
678
|
+
catch (err) {
|
|
679
|
+
await shut(client, transport);
|
|
680
|
+
throw err;
|
|
426
681
|
}
|
|
427
682
|
const tools = [];
|
|
428
|
-
const seen = new Set();
|
|
429
683
|
for (const tool of offered) {
|
|
430
|
-
if (!CHILD_TOOL_RE.test(tool.name)) {
|
|
431
|
-
await shut(client, transport);
|
|
432
|
-
throw new Error(`downstream-child-name-invalid:${tool.name}`);
|
|
433
|
-
}
|
|
434
|
-
// The whole entry as the child sent it, in bytes: a description in three-byte
|
|
435
|
-
// characters, or megabytes in outputSchema or title, count against the bound too.
|
|
436
|
-
const toolBytes = Buffer.byteLength(JSON.stringify(tool), "utf8");
|
|
437
|
-
if (toolBytes > MAX_TOOL_BYTES) {
|
|
438
|
-
await shut(client, transport);
|
|
439
|
-
throw new Error(`downstream-tool-too-large:${spec.prefix}.${tool.name}:${toolBytes}`);
|
|
440
|
-
}
|
|
441
684
|
const name = prefixedName(spec.prefix, tool.name);
|
|
442
|
-
|
|
685
|
+
const childName = tool.name;
|
|
686
|
+
// Key order is published sorted. The encodability of the whole entry was
|
|
687
|
+
// already refused above; a schema that still cannot be written closes the child.
|
|
688
|
+
let inputSchema;
|
|
689
|
+
try {
|
|
690
|
+
if (tool.inputSchema !== undefined)
|
|
691
|
+
inputSchema = sortedObject(tool.inputSchema);
|
|
692
|
+
}
|
|
693
|
+
catch {
|
|
443
694
|
await shut(client, transport);
|
|
444
|
-
throw new Error(`downstream-
|
|
695
|
+
throw new Error(`downstream-definition-unencodable:${spec.prefix}.${childName}`);
|
|
445
696
|
}
|
|
446
|
-
seen.add(name);
|
|
447
|
-
const childName = tool.name;
|
|
448
697
|
tools.push({
|
|
449
698
|
name,
|
|
450
699
|
...(typeof tool.description === "string" ? { description: tool.description } : {}),
|
|
451
|
-
...(
|
|
700
|
+
...(inputSchema !== undefined ? { inputSchema } : {}),
|
|
452
701
|
fn: async (call, _principal, ref) => {
|
|
453
702
|
// A child that sent one message over the bound is not asked again.
|
|
454
703
|
if (overflow.hit)
|
|
@@ -474,7 +723,7 @@ export async function openDownstream(spec) {
|
|
|
474
723
|
const receipt = raw._meta?.["io.cedulon/effect-extract"];
|
|
475
724
|
const reason = thirdPartyReceiptRejection(receipt, spec.effectKeyPem, ref, name, sha256Canonical(effectDescriptor(name, call.arguments)));
|
|
476
725
|
if (reason)
|
|
477
|
-
process.stderr.write(`verax-body: third-party receipt rejected: ${reason}\n`);
|
|
726
|
+
process.stderr.write(`verax-body: third-party receipt rejected: ${terminalText(reason)}\n`);
|
|
478
727
|
else
|
|
479
728
|
result.effectReceipt = receipt;
|
|
480
729
|
}
|
|
@@ -482,6 +731,25 @@ export async function openDownstream(spec) {
|
|
|
482
731
|
},
|
|
483
732
|
});
|
|
484
733
|
}
|
|
734
|
+
// A child can keep a tool's name and change what that name means. The pin
|
|
735
|
+
// names the manifest of the definitions the operator read; the bytes of
|
|
736
|
+
// that file, its prefix and selection, and each listed entry are compared
|
|
737
|
+
// with what this child just returned, so a changed definition cannot attach
|
|
738
|
+
// under the same name.
|
|
739
|
+
if (spec.pins !== undefined) {
|
|
740
|
+
try {
|
|
741
|
+
const manifest = readPinnedManifest(spec.prefix, spec.pins, opts?.definitionsDir);
|
|
742
|
+
if (manifest.prefix !== spec.prefix || !sameSelection(spec.tools, manifest.selected)) {
|
|
743
|
+
throw new Error(`downstream-manifest-mismatch:${spec.prefix}`);
|
|
744
|
+
}
|
|
745
|
+
assertPinnedDefinitions(spec, offered, manifest.tools);
|
|
746
|
+
}
|
|
747
|
+
catch (err) {
|
|
748
|
+
await shut(client, transport);
|
|
749
|
+
throw err;
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
tools.sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
485
753
|
let closed = false;
|
|
486
754
|
return {
|
|
487
755
|
prefix: spec.prefix,
|
package/dist/heartbeat.d.ts
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
1
|
import type { FileLedger, RecordSigner } from "@verax-ai/proxy";
|
|
2
|
+
/** One child named on a start: the digest of its pinned manifest, or null when it has no pins. */
|
|
3
|
+
export type StartDownstream = {
|
|
4
|
+
prefix: string;
|
|
5
|
+
manifest: string | null;
|
|
6
|
+
};
|
|
2
7
|
/** v1 rows carry no `v` and no signature. */
|
|
3
8
|
export type StartRow = {
|
|
4
9
|
action: "start";
|
|
@@ -6,6 +11,11 @@ export type StartRow = {
|
|
|
6
11
|
prevBeatAtMs: number | null;
|
|
7
12
|
gapMs: number | null;
|
|
8
13
|
pid: number;
|
|
14
|
+
/**
|
|
15
|
+
* Children in force at this start. Absent when the body had none, so a row
|
|
16
|
+
* written before this field still verifies.
|
|
17
|
+
*/
|
|
18
|
+
downstream?: StartDownstream[];
|
|
9
19
|
};
|
|
10
20
|
/**
|
|
11
21
|
* A v2 row is signed by the record key over its canonical form without
|
|
@@ -30,9 +40,13 @@ export declare function heartbeatEveryMs(env?: NodeJS.ProcessEnv): number;
|
|
|
30
40
|
/**
|
|
31
41
|
* Separate from halt history: a start is an observation, not a control.
|
|
32
42
|
* With a signer the row is v2 and signed by the record key; without one it
|
|
33
|
-
* stays an unsigned v1 row.
|
|
43
|
+
* stays an unsigned v1 row. A non-empty `downstream` list is part of the row
|
|
44
|
+
* and of the bytes the record key signs. When it is absent the field is not
|
|
45
|
+
* written, so older v1 and v2 rows verify unchanged.
|
|
34
46
|
*/
|
|
35
|
-
export declare function recordBodyStart(stateDir: string, atMs: number, signer?: RecordSigner
|
|
47
|
+
export declare function recordBodyStart(stateDir: string, atMs: number, signer?: RecordSigner, extra?: {
|
|
48
|
+
downstream?: StartDownstream[];
|
|
49
|
+
}): void;
|
|
36
50
|
export declare function startHeartbeat(ledger: FileLedger): () => void;
|
|
37
51
|
export type StartSummary = {
|
|
38
52
|
count: number;
|
|
@@ -48,6 +62,11 @@ export type StartSummary = {
|
|
|
48
62
|
/** Problems that make the ledger not verify: a bad signature, a broken chain, an unsigned row after a signed one. */
|
|
49
63
|
problems: string[];
|
|
50
64
|
warnings: string[];
|
|
65
|
+
/**
|
|
66
|
+
* Children recorded on each start, in the order the rows were read. A
|
|
67
|
+
* start that named none is an empty list.
|
|
68
|
+
*/
|
|
69
|
+
downstream: StartDownstream[][];
|
|
51
70
|
};
|
|
52
71
|
/**
|
|
53
72
|
* `publicKeyPem` is the record key the reader trusts: the one they pinned or
|
|
@@ -57,4 +76,9 @@ export type StartSummary = {
|
|
|
57
76
|
export declare function readStartSummary(stateDir: string, publicKeyPem?: string | null, keySource?: "pinned" | "in-ledger" | "none", opts?: {
|
|
58
77
|
requireSigned?: boolean;
|
|
59
78
|
}): StartSummary;
|
|
79
|
+
/**
|
|
80
|
+
* One printed line of the starts block. A newline inside a prefix stays in
|
|
81
|
+
* that line, so a reader that escapes each line cannot be given a second one.
|
|
82
|
+
*/
|
|
83
|
+
export declare function startLines(summary: StartSummary): string[];
|
|
60
84
|
export declare function renderStarts(summary: StartSummary): string;
|
package/dist/heartbeat.js
CHANGED
|
@@ -63,17 +63,21 @@ export function heartbeatEveryMs(env = process.env) {
|
|
|
63
63
|
/**
|
|
64
64
|
* Separate from halt history: a start is an observation, not a control.
|
|
65
65
|
* With a signer the row is v2 and signed by the record key; without one it
|
|
66
|
-
* stays an unsigned v1 row.
|
|
66
|
+
* stays an unsigned v1 row. A non-empty `downstream` list is part of the row
|
|
67
|
+
* and of the bytes the record key signs. When it is absent the field is not
|
|
68
|
+
* written, so older v1 and v2 rows verify unchanged.
|
|
67
69
|
*/
|
|
68
|
-
export function recordBodyStart(stateDir, atMs, signer) {
|
|
70
|
+
export function recordBodyStart(stateDir, atMs, signer, extra) {
|
|
69
71
|
const path = join(stateDir, "starts.jsonl");
|
|
70
72
|
const prevBeatAtMs = readHeartbeat(stateDir)?.atMs ?? null;
|
|
73
|
+
const listed = extra?.downstream;
|
|
71
74
|
const base = {
|
|
72
75
|
action: "start",
|
|
73
76
|
atMs,
|
|
74
77
|
prevBeatAtMs,
|
|
75
78
|
gapMs: prevBeatAtMs === null ? null : atMs - prevBeatAtMs,
|
|
76
79
|
pid: process.pid,
|
|
80
|
+
...(listed !== undefined && listed.length > 0 ? { downstream: listed } : {}),
|
|
77
81
|
};
|
|
78
82
|
let row = base;
|
|
79
83
|
if (signer) {
|
|
@@ -98,6 +102,19 @@ export function startHeartbeat(ledger) {
|
|
|
98
102
|
timer.unref();
|
|
99
103
|
return () => clearInterval(timer);
|
|
100
104
|
}
|
|
105
|
+
const MANIFEST_RE = /^[0-9a-f]{64}$/;
|
|
106
|
+
function isDownstreamList(value) {
|
|
107
|
+
if (!Array.isArray(value))
|
|
108
|
+
return false;
|
|
109
|
+
return value.every((item) => {
|
|
110
|
+
if (item === null || typeof item !== "object" || Array.isArray(item))
|
|
111
|
+
return false;
|
|
112
|
+
const rec = item;
|
|
113
|
+
if (typeof rec.prefix !== "string")
|
|
114
|
+
return false;
|
|
115
|
+
return rec.manifest === null || (typeof rec.manifest === "string" && MANIFEST_RE.test(rec.manifest));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
101
118
|
function isStartRow(row) {
|
|
102
119
|
return (!!row &&
|
|
103
120
|
row.action === "start" &&
|
|
@@ -105,7 +122,49 @@ function isStartRow(row) {
|
|
|
105
122
|
Number.isInteger(row.pid) &&
|
|
106
123
|
(row.prevBeatAtMs === null || Number.isFinite(row.prevBeatAtMs)) &&
|
|
107
124
|
(row.gapMs === null || Number.isFinite(row.gapMs)) &&
|
|
108
|
-
(row.prevBeatAtMs === null) === (row.gapMs === null)
|
|
125
|
+
(row.prevBeatAtMs === null) === (row.gapMs === null) &&
|
|
126
|
+
(row.downstream === undefined ||
|
|
127
|
+
isDownstreamList(row.downstream)));
|
|
128
|
+
}
|
|
129
|
+
function downstreamManifestProblems(stateDir, row) {
|
|
130
|
+
if (row.downstream === undefined)
|
|
131
|
+
return [];
|
|
132
|
+
const problems = [];
|
|
133
|
+
for (const entry of row.downstream) {
|
|
134
|
+
if (entry.manifest === null)
|
|
135
|
+
continue;
|
|
136
|
+
const file = join(stateDir, "definitions", `${entry.manifest}.json`);
|
|
137
|
+
let bytes;
|
|
138
|
+
try {
|
|
139
|
+
bytes = readFileSync(file);
|
|
140
|
+
}
|
|
141
|
+
catch (err) {
|
|
142
|
+
const missing = err.code === "ENOENT";
|
|
143
|
+
problems.push(`${missing ? "downstream-manifest-missing" : "downstream-manifest-invalid"}:${entry.prefix}:${entry.manifest}`);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (createHash("sha256").update(bytes).digest("hex") !== entry.manifest) {
|
|
147
|
+
problems.push(`downstream-manifest-invalid:${entry.prefix}:${entry.manifest}`);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
// The digest names these bytes. They still have to be the manifest of
|
|
151
|
+
// the child this row recorded, not another prefix under the same hash.
|
|
152
|
+
let recorded;
|
|
153
|
+
try {
|
|
154
|
+
recorded = JSON.parse(bytes.toString("utf8"));
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
problems.push(`downstream-manifest-invalid:${entry.prefix}:${entry.manifest}`);
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
const recordedPrefix = recorded !== null && typeof recorded === "object" && !Array.isArray(recorded)
|
|
161
|
+
? recorded.prefix
|
|
162
|
+
: undefined;
|
|
163
|
+
if (recordedPrefix !== entry.prefix) {
|
|
164
|
+
problems.push(`downstream-manifest-mismatch:${entry.prefix}:${entry.manifest}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return problems;
|
|
109
168
|
}
|
|
110
169
|
/**
|
|
111
170
|
* Whether a line carries a field only a signed start row has. A line that no
|
|
@@ -145,6 +204,7 @@ export function readStartSummary(stateDir, publicKeyPem = null, keySource = publ
|
|
|
145
204
|
startKey: publicKeyPem ? keySource : "none",
|
|
146
205
|
problems: [],
|
|
147
206
|
warnings: [],
|
|
207
|
+
downstream: [],
|
|
148
208
|
};
|
|
149
209
|
let raw;
|
|
150
210
|
try {
|
|
@@ -198,6 +258,7 @@ export function readStartSummary(stateDir, publicKeyPem = null, keySource = publ
|
|
|
198
258
|
continue;
|
|
199
259
|
}
|
|
200
260
|
result.count += 1;
|
|
261
|
+
result.downstream.push(row.downstream ?? []);
|
|
201
262
|
if (row.prevBeatAtMs === null)
|
|
202
263
|
result.missingPreviousBeat.push(row.atMs);
|
|
203
264
|
if (row.gapMs !== null) {
|
|
@@ -249,6 +310,11 @@ export function readStartSummary(stateDir, publicKeyPem = null, keySource = publ
|
|
|
249
310
|
else if (opts.requireSigned)
|
|
250
311
|
result.problems.push(`start-row-unsigned: starts.jsonl line ${n}`);
|
|
251
312
|
}
|
|
313
|
+
// Each named manifest must be the file under that digest. What this shows,
|
|
314
|
+
// and no more: the body that held a trusted record key recorded these
|
|
315
|
+
// definitions at that start. It does not prove that a person read them or
|
|
316
|
+
// that the child's code behaves as described.
|
|
317
|
+
result.problems.push(...downstreamManifestProblems(stateDir, row));
|
|
252
318
|
prevLine = line;
|
|
253
319
|
}
|
|
254
320
|
if (opts.requireSigned) {
|
|
@@ -260,13 +326,30 @@ export function readStartSummary(stateDir, publicKeyPem = null, keySource = publ
|
|
|
260
326
|
}
|
|
261
327
|
return result;
|
|
262
328
|
}
|
|
263
|
-
|
|
329
|
+
/**
|
|
330
|
+
* One printed line of the starts block. A newline inside a prefix stays in
|
|
331
|
+
* that line, so a reader that escapes each line cannot be given a second one.
|
|
332
|
+
*/
|
|
333
|
+
export function startLines(summary) {
|
|
264
334
|
const signedPart = summary.signed === 0
|
|
265
335
|
? "unsigned (not verified)"
|
|
266
336
|
: summary.startKey === "none"
|
|
267
337
|
? `${summary.signed} signed, not checked (no record key)${summary.unsigned ? "; older rows unsigned" : ""}`
|
|
268
338
|
: `${summary.signedVerified} of ${summary.signed} signed verify under the ${summary.startKey === "pinned" ? "pinned" : "in-ledger"} record key${summary.unsigned ? "; older rows unsigned" : ""}`;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
339
|
+
const lines = [
|
|
340
|
+
`starts ${summary.count}; largest gap ${summary.largestGapMs === null ? "none" : `${summary.largestGapMs} ms`}; previous beat missing at ${summary.missingPreviousBeat.join(", ") || "none"}; ${signedPart}`,
|
|
341
|
+
];
|
|
342
|
+
for (const listed of summary.downstream) {
|
|
343
|
+
for (const entry of listed) {
|
|
344
|
+
lines.push(`downstream ${entry.prefix} ${entry.manifest === null ? "unpinned" : `sha256:${entry.manifest}`}`);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
for (const problem of summary.problems)
|
|
348
|
+
lines.push(`problem ${problem}`);
|
|
349
|
+
for (const warning of summary.warnings)
|
|
350
|
+
lines.push(`warning ${warning}`);
|
|
351
|
+
return lines;
|
|
352
|
+
}
|
|
353
|
+
export function renderStarts(summary) {
|
|
354
|
+
return startLines(summary).join("\n");
|
|
272
355
|
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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 type { ApprovalRow } from "@verax-ai/proxy";
|
|
7
|
+
export type HeldSpend = {
|
|
8
|
+
payee: string;
|
|
9
|
+
amountMinor: number | null;
|
|
10
|
+
currency: string;
|
|
11
|
+
reference: string;
|
|
12
|
+
};
|
|
13
|
+
/** An integer minor-unit amount, or null when the stored amount cannot be read as one. */
|
|
14
|
+
export declare function minorUnits(row: ApprovalRow): number | null;
|
|
15
|
+
/** Null for a held call that is not a spend. */
|
|
16
|
+
export declare function heldSpend(row: ApprovalRow): HeldSpend | null;
|