@agent-blueprint-protocol/verifier 0.8.3 → 0.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/README.md +5 -5
  3. package/dist/corpus/cases/blueprint-decode.json +1 -1
  4. package/dist/corpus/cases/deployment-decode.json +1 -1
  5. package/dist/corpus/cases/schema-validate_instance.json +1 -1
  6. package/dist/corpus/index.json +1 -1
  7. package/dist/kit/cli.mjs +1 -4
  8. package/dist/release-metadata.json +1 -1
  9. package/dist/verifier/b64url.d.ts +6 -2
  10. package/dist/verifier/b64url.js +6 -0
  11. package/dist/verifier/blueprint.d.ts +12 -3
  12. package/dist/verifier/blueprint.js +21 -7
  13. package/dist/verifier/bounds.d.ts +1 -1
  14. package/dist/verifier/bounds.js +2 -2
  15. package/dist/verifier/bounds_algebra.js +4 -1
  16. package/dist/verifier/canonical.d.ts +1 -1
  17. package/dist/verifier/canonical.js +41 -8
  18. package/dist/verifier/cli.js +111 -42
  19. package/dist/verifier/compatibility.js +5 -7
  20. package/dist/verifier/corpus.js +5 -1
  21. package/dist/verifier/decode.js +9 -3
  22. package/dist/verifier/deployment.d.ts +12 -3
  23. package/dist/verifier/deployment.js +31 -8
  24. package/dist/verifier/digest.d.ts +12 -0
  25. package/dist/verifier/digest.js +16 -2
  26. package/dist/verifier/extension.js +4 -4
  27. package/dist/verifier/federation.js +16 -72
  28. package/dist/verifier/negotiation.js +6 -0
  29. package/dist/verifier/portability.d.ts +1 -1
  30. package/dist/verifier/portability.js +19 -6
  31. package/dist/verifier/predicate.js +3 -6
  32. package/dist/verifier/registry_engine.js +29 -12
  33. package/dist/verifier/report.d.ts +8 -0
  34. package/dist/verifier/report.js +9 -2
  35. package/dist/verifier/run_corpus.js +4 -80
  36. package/dist/verifier/runner.d.ts +4 -0
  37. package/dist/verifier/runner.js +8 -1
  38. package/dist/verifier/schema.js +14 -55
  39. package/dist/verifier/signature.d.ts +2 -2
  40. package/dist/verifier/signature.js +10 -4
  41. package/dist/verifier/value.d.ts +9 -2
  42. package/dist/verifier/value.js +81 -11
  43. package/package.json +1 -1
@@ -12,9 +12,13 @@
12
12
  // invalid characters.
13
13
  const ALPHABET = /^[A-Za-z0-9_-]*$/;
14
14
  export function encode(data) {
15
+ if (!Buffer.isBuffer(data))
16
+ return { ok: false, e: "invalid_type" };
15
17
  return data.toString("base64url");
16
18
  }
17
19
  export function decodeStrict(input) {
20
+ if (typeof input !== "string")
21
+ return { ok: false, e: "invalid_type" };
18
22
  if (input.includes("="))
19
23
  return { ok: false, e: "base64url_padded" };
20
24
  if (!ALPHABET.test(input))
@@ -28,6 +32,8 @@ export function decodeStrict(input) {
28
32
  }
29
33
  const LENIENT = /^[A-Za-z0-9_-]+={0,2}$/;
30
34
  export function decodeLenient(input) {
35
+ if (typeof input !== "string")
36
+ return { ok: false, e: "invalid_type" };
31
37
  if (!LENIENT.test(input))
32
38
  return { ok: false, e: "lenient_invalid" };
33
39
  if (input.length % 4 === 1)
@@ -28,8 +28,14 @@ export declare function fromValue(value: Value, opts?: {
28
28
  ok: false;
29
29
  e: string;
30
30
  };
31
- export declare function toValue(blueprint: Blueprint): Value;
32
- export declare function digestInput(blueprint: Blueprint): Value;
31
+ export declare function toValue(blueprint: Blueprint): Value | {
32
+ ok: false;
33
+ e: string;
34
+ };
35
+ export declare function digestInput(blueprint: Blueprint): Value | {
36
+ ok: false;
37
+ e: string;
38
+ };
33
39
  export declare function canonicalBytes(blueprint: Blueprint): {
34
40
  ok: true;
35
41
  v: string;
@@ -37,7 +43,10 @@ export declare function canonicalBytes(blueprint: Blueprint): {
37
43
  ok: false;
38
44
  e: string;
39
45
  };
40
- export declare function contentDigest(blueprint: Blueprint): string;
46
+ export declare function contentDigest(blueprint: Blueprint): string | {
47
+ ok: false;
48
+ e: string;
49
+ };
41
50
  export declare function verifyContentDigest(blueprint: Blueprint): VResult;
42
51
  export interface SignatureAttributes {
43
52
  contentDigest: digest.Digest;
@@ -1,3 +1,4 @@
1
+ import { isValue } from "./value.js";
1
2
  // The Blueprint artifact — the mirror of AgentBlueprintProtocol.Blueprint:
2
3
  // canonical verify → the 18-member registry table → the portability scan →
3
4
  // the content-digest comparison, over the digest-covered members minus the
@@ -215,7 +216,14 @@ export function decode(bytes, bounds) {
215
216
  return { ok: true, v: { value } };
216
217
  }
217
218
  export function fromValue(value, opts = {}) {
219
+ if (opts === null || typeof opts !== "object" || Array.isArray(opts))
220
+ return { ok: false, e: "invalid_type" };
218
221
  const validated = opts.authoredExtensions ?? [];
222
+ if (!Array.isArray(validated) || validated.some((ns) => typeof ns !== "string"))
223
+ return { ok: false, e: "invalid_type" };
224
+ const shaped = canonical.encode(value);
225
+ if (!shaped.ok)
226
+ return shaped;
219
227
  if (validated.length > 0) {
220
228
  if (value.t !== "obj")
221
229
  return { ok: false, e: "invalid_type" };
@@ -232,25 +240,31 @@ export function fromValue(value, opts = {}) {
232
240
  return { ok: true, v: { value } };
233
241
  }
234
242
  export function toValue(blueprint) {
235
- return blueprint.value;
243
+ return blueprint && isValue(blueprint.value) ? blueprint.value : { ok: false, e: "invalid_type" };
236
244
  }
237
245
  // ---- digest surface ----------------------------------------------------------------
238
246
  export function digestInput(blueprint) {
247
+ if (!blueprint || !isValue(blueprint.value))
248
+ return { ok: false, e: "invalid_type" };
239
249
  if (blueprint.value.t !== "obj")
240
250
  return blueprint.value;
241
251
  return { t: "obj", v: blueprint.value.v.filter(([name]) => !EVIDENCE_MEMBERS.includes(name)) };
242
252
  }
243
253
  export function canonicalBytes(blueprint) {
254
+ if (!blueprint || typeof blueprint !== "object")
255
+ return { ok: false, e: "invalid_type" };
244
256
  return canonical.encode(blueprint.value);
245
257
  }
246
258
  // The honest content digest over the covered members' canonical bytes.
247
259
  export function contentDigest(blueprint) {
248
260
  const jcs = canonical.encode(digestInput(blueprint));
249
261
  if (!jcs.ok)
250
- throw new Error("blueprint digest input must encode");
262
+ return jcs;
251
263
  return digest.toTagged(digest.hash("blueprint_content", Buffer.from(jcs.v, "utf8")));
252
264
  }
253
265
  export function verifyContentDigest(blueprint) {
266
+ if (!blueprint || !isValue(blueprint.value))
267
+ return { ok: false, e: "invalid_type" };
254
268
  const members = blueprint.value.t === "obj" ? blueprint.value.v : [];
255
269
  const declared = keyfind(members, "content_digest");
256
270
  if (declared === null || declared.t !== "str")
@@ -564,11 +578,7 @@ function predicateOperands(members) {
564
578
  const predicateValue = keyfind(assertion.v, "predicate");
565
579
  if (predicateValue === null || predicateValue.t !== "obj")
566
580
  continue;
567
- for (const operandName of ["value", "values"]) {
568
- const operand = keyfind(predicateValue.v, operandName);
569
- if (operand !== null)
570
- out.push(operand);
571
- }
581
+ out.push(predicateValue);
572
582
  }
573
583
  return out;
574
584
  }
@@ -688,6 +698,8 @@ function malformed() {
688
698
  return { ok: false, e: "signature_malformed" };
689
699
  }
690
700
  export function signatureAttributes(entry) {
701
+ if (!isValue(entry))
702
+ return { ok: false, e: "invalid_type" };
691
703
  if (entry.t !== "obj")
692
704
  return malformed();
693
705
  const entryPairs = entry.v;
@@ -780,6 +792,8 @@ export function signatureAttributes(entry) {
780
792
  }
781
793
  // DateTime.from_iso8601's field validation for the Z whole-second form.
782
794
  export function validZFormDate(stamped) {
795
+ if (typeof stamped !== "string")
796
+ return false;
783
797
  const year = Number(stamped.slice(0, 4));
784
798
  const month = Number(stamped.slice(5, 7));
785
799
  const day = Number(stamped.slice(8, 10));
@@ -8,7 +8,7 @@ export interface Bounds {
8
8
  key: number;
9
9
  number_lexeme: number;
10
10
  }
11
- export declare const MAXIMA: Bounds;
11
+ export declare const MAXIMA: Readonly<Bounds>;
12
12
  export declare function maximum(): Bounds;
13
13
  export declare function coerce(overrides: unknown): {
14
14
  ok: true;
@@ -2,7 +2,7 @@
2
2
  // profile maxima and the tighten-only override rule (unknown key denies
3
3
  // `unknown_bound`; a non-positive, non-integer, or super-maximum value denies
4
4
  // `ceiling:<field>`).
5
- export const MAXIMA = {
5
+ export const MAXIMA = Object.freeze({
6
6
  bytes: 5_000_000,
7
7
  depth: 64,
8
8
  members: 1_000,
@@ -11,7 +11,7 @@ export const MAXIMA = {
11
11
  string: 100_000,
12
12
  key: 1_000,
13
13
  number_lexeme: 64,
14
- };
14
+ });
15
15
  const FIELDS = Object.keys(MAXIMA);
16
16
  export function maximum() {
17
17
  return { ...MAXIMA };
@@ -87,6 +87,8 @@ export function intersect(blueprint, deployment, host, posture) {
87
87
  // runner pre-filters unknown member names to :invalid_type at add_bound_entry;
88
88
  // here that delegated filter runs first, then BoundSet.new's value checks).
89
89
  function buildSet(raw) {
90
+ if (!isPlainObject(raw))
91
+ return { ok: false, e: "invalid_type" };
90
92
  const out = {};
91
93
  for (const [name, value] of Object.entries(raw)) {
92
94
  if (!Object.hasOwn(TABLE, name))
@@ -146,7 +148,8 @@ function buildBound(name, raw) {
146
148
  return { ok: false, e: "bound_value_invalid" };
147
149
  }
148
150
  function isPlainObject(value) {
149
- return typeof value === "object" && value !== null && !Array.isArray(value);
151
+ return typeof value === "object" && value !== null &&
152
+ (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null);
150
153
  }
151
154
  // ---- totality ----------------------------------------------------------------------
152
155
  function totalOf(sources) {
@@ -1,6 +1,6 @@
1
1
  import type { Bounds } from "./bounds.ts";
2
2
  import type { Value } from "./value.ts";
3
- export declare function encode(value: Value, bounds?: Bounds | Record<string, unknown>): {
3
+ export declare function encode(value: unknown, bounds?: Bounds | Record<string, unknown>): {
4
4
  ok: true;
5
5
  v: string;
6
6
  } | {
@@ -13,7 +13,7 @@
13
13
  // The decode ↔ canonical import cycle (verify needs decode; decode's window
14
14
  // rule needs the number oracle) is a node-ESM circular import over hoisted
15
15
  // function declarations — resolved at call time, never at module init.
16
- import { maximum } from "./bounds.js";
16
+ import { coerce, maximum } from "./bounds.js";
17
17
  import { decode } from "./decode.js";
18
18
  import { err, ok, IJSON_MAX } from "./value.js";
19
19
  const SHORT_ESCAPES = {
@@ -24,10 +24,12 @@ const SHORT_ESCAPES = {
24
24
  0x0d: "r",
25
25
  };
26
26
  export function encode(value, bounds) {
27
- // The profile is read field-wise (bytes) whatever the caller passed.
28
- const profile = (bounds ?? maximum());
27
+ const normalized = coerce(bounds === undefined ? maximum() : bounds);
28
+ if (!normalized.ok)
29
+ return normalized;
30
+ const profile = normalized.v;
29
31
  const parts = [];
30
- const walked = walk(value, parts);
32
+ const walked = walk(value, parts, profile, 0, { nodes: 0 });
31
33
  if (!walked.ok)
32
34
  return walked;
33
35
  const out = parts.join("");
@@ -58,12 +60,27 @@ export function number(f) {
58
60
  return JSON.stringify(f);
59
61
  }
60
62
  // ---- encode walk ----------------------------------------------------------------
61
- function walk(value, out) {
63
+ function walk(value, out, bounds, depth, counter) {
64
+ if (value === null || typeof value !== "object")
65
+ return err("invalid_type");
66
+ if (++counter.nodes > bounds.nodes)
67
+ return err("ceiling:nodes");
68
+ if (value.t === "arr" || value.t === "obj") {
69
+ if (!Array.isArray(value.v))
70
+ return err("invalid_type");
71
+ if (depth >= bounds.depth)
72
+ return err("ceiling:depth");
73
+ const field = value.t === "arr" ? "items" : "members";
74
+ if (value.v.length > bounds[field])
75
+ return err(`ceiling:${field}`);
76
+ }
62
77
  switch (value.t) {
63
78
  case "null":
64
79
  out.push("null");
65
80
  return ok(undefined);
66
81
  case "bool":
82
+ if (typeof value.v !== "boolean")
83
+ return err("invalid_type");
67
84
  out.push(value.v ? "true" : "false");
68
85
  return ok(undefined);
69
86
  case "int":
@@ -73,9 +90,19 @@ function walk(value, out) {
73
90
  out.push(String(value.v));
74
91
  return ok(undefined);
75
92
  case "float":
93
+ if (typeof value.v !== "number")
94
+ return err("invalid_type");
95
+ if (!Number.isFinite(value.v))
96
+ return err("invalid_number");
76
97
  out.push(number(value.v));
77
98
  return ok(undefined);
78
99
  case "str": {
100
+ if (typeof value.v !== "string")
101
+ return err("invalid_type");
102
+ if (Buffer.byteLength(value.v) > bounds.bytes)
103
+ return err("ceiling:bytes");
104
+ if (Buffer.byteLength(value.v) > bounds.string)
105
+ return err("ceiling:string");
79
106
  const escaped = escapeString(value.v);
80
107
  if (!escaped.ok)
81
108
  return escaped;
@@ -87,7 +114,7 @@ function walk(value, out) {
87
114
  for (let i = 0; i < value.v.length; i++) {
88
115
  if (i > 0)
89
116
  out.push(",");
90
- const element = walk(value.v[i], out);
117
+ const element = walk(value.v[i], out, bounds, depth + 1, counter);
91
118
  if (!element.ok)
92
119
  return element;
93
120
  }
@@ -95,7 +122,12 @@ function walk(value, out) {
95
122
  return ok(undefined);
96
123
  }
97
124
  case "obj": {
98
- for (const [name] of value.v) {
125
+ for (const pair of value.v) {
126
+ if (!Array.isArray(pair) || pair.length !== 2 || typeof pair[0] !== "string")
127
+ return err("invalid_type");
128
+ const name = pair[0];
129
+ if (Buffer.byteLength(name) > bounds.key)
130
+ return err("ceiling:key");
99
131
  if (!validString(name))
100
132
  return err("invalid_encoding");
101
133
  }
@@ -112,13 +144,14 @@ function walk(value, out) {
112
144
  if (!key.ok)
113
145
  return key;
114
146
  out.push('"' + key.v + '":');
115
- const member = walk(sorted[i][1], out);
147
+ const member = walk(sorted[i][1], out, bounds, depth + 1, counter);
116
148
  if (!member.ok)
117
149
  return member;
118
150
  }
119
151
  out.push("}");
120
152
  return ok(undefined);
121
153
  }
154
+ default: return err("invalid_type");
122
155
  }
123
156
  }
124
157
  function escapeString(s) {
@@ -6,8 +6,9 @@
6
6
  // IO.binwrite parity). Exit 3 marks an internal invariant (outside the
7
7
  // 0/1/2 contract, exactly as an escript crash is — loud, never laundered
8
8
  // into "disagreement").
9
- import { readdirSync, readFileSync, realpathSync, statSync } from "node:fs";
10
- import { join } from "node:path";
9
+ import { readdirSync, readFileSync, lstatSync, openSync, closeSync, readSync, fstatSync } from "node:fs";
10
+ import { join, relative, sep } from "node:path";
11
+ import { member } from "./corpus.js";
11
12
  import { load, loadPriorCensus } from "./corpus.js";
12
13
  import { encode } from "./canonical.js";
13
14
  import { toBytes } from "./report.js";
@@ -16,9 +17,36 @@ import { decode as decodeBlueprint } from "./blueprint.js";
16
17
  import { decode as decodeDeployment } from "./deployment.js";
17
18
  import { decode as decodeEnvelope } from "./federation.js";
18
19
  import { obj, str } from "./value.js";
19
- const USAGE = "usage: node verifier/cli.ts --corpus <dir> | --prior-corpus <dir> | --artifact <file>";
20
+ const USAGE = `usage: agent-blueprint-protocol [--corpus <dir> | --prior-corpus <dir> | --artifact <file> | --help | --version]
21
+ No arguments: verify the installed release's embedded corpus.
22
+ --corpus <dir>: verify a corpus and report agreement.
23
+ --prior-corpus <dir>: replay a released corpus under current verification.
24
+ --artifact <file>: verify a Blueprint, Deployment Manifest, or Federation TaskEnvelope.
25
+ --help: print this help. --version: print the package version.
26
+ Exit codes: 0 agreement or valid artifact (also help/version); 1 disagreement or invalid artifact;
27
+ 2 usage, unreadable input, or corpus integrity failure; 3 internal error.`;
20
28
  const BYTE_CAP = 5_000_000;
29
+ function releaseMetadata() {
30
+ for (const path of ["../release-metadata.json", "../priv/release-metadata.json", "./release-metadata.json"]) {
31
+ try {
32
+ return JSON.parse(readFileSync(new URL(path, import.meta.url), "utf8"));
33
+ }
34
+ catch (error) {
35
+ if (error.code !== "ENOENT")
36
+ throw error;
37
+ }
38
+ }
39
+ throw new Error("release metadata missing");
40
+ }
21
41
  function main(argv) {
42
+ if (argv.length === 1 && argv[0] === "--help") {
43
+ process.stdout.write(USAGE + "\n");
44
+ return 0;
45
+ }
46
+ if (argv.length === 1 && argv[0] === "--version") {
47
+ process.stdout.write(releaseMetadata().package_version + "\n");
48
+ return 0;
49
+ }
22
50
  if (argv.length === 2 && argv[0] === "--artifact" && argv[1] !== "" && typeof argv[1] === "string") {
23
51
  return verifyArtifact(argv[1]);
24
52
  }
@@ -38,7 +66,7 @@ function main(argv) {
38
66
  map = readCorpus(dir);
39
67
  }
40
68
  catch {
41
- process.stderr.write("corpus unreadable: a file is missing, unreadable, or over the byte ceiling\n");
69
+ process.stderr.write("corpus unreadable: missing, unreadable, not regular, or over the file or byte ceiling\n");
42
70
  return 2;
43
71
  }
44
72
  const corpus = prior ? loadPriorCensus(map) : load(map);
@@ -48,6 +76,12 @@ function main(argv) {
48
76
  process.stderr.write("corpus integrity failure: :" + corpus.e + "\n");
49
77
  return 2;
50
78
  }
79
+ if (!corpus.v.cases.every((file) => file.cases.every(readerInput))) {
80
+ process.stderr.write("corpus integrity failure: :invalid_type\n");
81
+ return 2;
82
+ }
83
+ const certified = corpus.v.identity === releaseMetadata().corpus_digest ? "matches" : "does not match";
84
+ process.stderr.write(`corpus digest ${certified} the release-certified corpus\n`);
51
85
  const results = run(corpus.v);
52
86
  const bytes = toBytes(corpus.v.identity, results);
53
87
  // exitCode (not process.exit) so piped stdout drains before exit.
@@ -55,6 +89,28 @@ function main(argv) {
55
89
  const agreed = results.flat().filter((r) => r.agree).length;
56
90
  return agreed === results.flat().length && agreed > 0 ? 0 : 1;
57
91
  }
92
+ function fieldShape(input, key, predicate) {
93
+ const value = member(input, key);
94
+ return value === null || predicate(value);
95
+ }
96
+ function readerInput(row) {
97
+ const input = row.input;
98
+ if (row.surface === "negotiation.negotiate" || row.surface === "extension.resolve") {
99
+ const support = member(input, "support");
100
+ if (support === null || support.t === "null")
101
+ return true;
102
+ if (support.t !== "obj")
103
+ return false;
104
+ return ["revisions", "core_fields"].every((key) => fieldShape(support, key, (v) => v.t === "arr")) &&
105
+ fieldShape(support, "schemas", (v) => v.t === "obj") &&
106
+ fieldShape(support, "registry", (v) => v.t === "obj" && v.v.every(([, entry]) => entry.t === "obj"));
107
+ }
108
+ if (row.surface === "bounds_algebra.intersect") {
109
+ return ["blueprint", "deployment", "host"].every((key) => fieldShape(input, key, (v) => v.t === "obj" && v.v.every(([, bound]) => bound.t !== "obj" || member(bound, "ordinal") === null ||
110
+ fieldShape(bound, "markers", (markers) => markers.t === "arr"))));
111
+ }
112
+ return true;
113
+ }
58
114
  // Single-artifact mode — a kit-side convenience the escript deliberately
59
115
  // does NOT mirror (its CLI contract stays --corpus-only, so a vacuous run
60
116
  // stays impossible there). One artifact file, verified against all three
@@ -66,12 +122,12 @@ function main(argv) {
66
122
  function verifyArtifact(path) {
67
123
  let bytes;
68
124
  try {
69
- const info = statSync(path);
125
+ const info = lstatSync(path);
70
126
  if (!info.isFile())
71
127
  throw new Error("not a regular file");
72
128
  if (info.size > BYTE_CAP)
73
129
  throw new Error("over byte ceiling");
74
- bytes = readFileSync(path);
130
+ bytes = readRegular(path, BYTE_CAP);
75
131
  }
76
132
  catch {
77
133
  process.stderr.write("artifact unreadable: missing, not a regular file, or over the byte ceiling\n");
@@ -97,54 +153,67 @@ function verifyArtifact(path) {
97
153
  process.stdout.write(encoded.v);
98
154
  return kind === null ? 1 : 0;
99
155
  }
100
- // Reads EVERY file under the corpus directory — dotfiles included — so the
101
- // loader's file-set equality sees exactly what the directory carries.
102
- // Per-file size cap at the Bounds byte ceiling; a hostile corpus directory
103
- // cannot exhaust memory or crash past the exit-code contract. Directory
104
- // symlinks are followed (Path.wildcard parity).
156
+ // At most 1,000 entries and 5,000,000 bytes across the entire tree.
157
+ // Symlinks and special files deny before reading; reads are bounded even if a file grows.
158
+ function readRegular(path, cap) {
159
+ const fd = openSync(path, "r");
160
+ try {
161
+ const info = fstatSync(fd);
162
+ if (!info.isFile() || info.size > cap)
163
+ throw new Error("unreadable");
164
+ const chunks = [];
165
+ let size = 0;
166
+ for (;;) {
167
+ const chunk = Buffer.alloc(Math.min(64 * 1024, cap - size + 1));
168
+ const count = readSync(fd, chunk);
169
+ if (count === 0)
170
+ return Buffer.concat(chunks, size);
171
+ size += count;
172
+ if (size > cap)
173
+ throw new Error("over byte ceiling");
174
+ chunks.push(chunk.subarray(0, count));
175
+ }
176
+ }
177
+ finally {
178
+ closeSync(fd);
179
+ }
180
+ }
105
181
  function readCorpus(dir) {
106
182
  const map = new Map();
107
- const visited = new Set();
183
+ let count = 0;
184
+ let total = 0;
108
185
  const walk = (current) => {
109
- // Cycle bound: directory symlinks repeat their realpath; a repeat is
110
- // skipped so the walk terminates inside the corpus-error contract.
111
- const real = realpathSync(current);
112
- if (visited.has(real))
113
- return;
114
- visited.add(real);
115
186
  for (const entry of readdirSync(current, { withFileTypes: true })) {
187
+ if (entry.name === ".DS_Store" || entry.name.startsWith("._"))
188
+ continue;
189
+ if (++count > 1_000)
190
+ throw new Error("over file ceiling");
116
191
  const path = join(current, entry.name);
117
- if (entry.isDirectory() || (entry.isSymbolicLink() && statSync(path).isDirectory())) {
192
+ const info = lstatSync(path);
193
+ if (info.isDirectory())
118
194
  walk(path);
195
+ else if (info.isFile()) {
196
+ if (info.size > BYTE_CAP - total)
197
+ throw new Error("over byte ceiling");
198
+ const bytes = readRegular(path, BYTE_CAP - total);
199
+ total += bytes.length;
200
+ map.set(relative(dir, path).split(sep).join("/"), bytes);
119
201
  }
120
- else {
121
- const info = statSync(path);
122
- // Regular files only: a FIFO/socket/device planted in the corpus
123
- // directory would block readFileSync past the exit contract — skip
124
- // it and let the loader's file-set equality deny (loud, not a hang).
125
- if (info.isFile()) {
126
- if (info.size > BYTE_CAP)
127
- throw new Error("over byte ceiling");
128
- map.set(path.slice(dir.length + 1), readFileSync(path));
129
- }
130
- }
202
+ else
203
+ throw new Error("not a regular file");
131
204
  }
132
205
  };
133
- // A missing (or non-directory) corpus root yields an EMPTY map — the
134
- // loader then denies corpus_index_invalid, exactly as the escript's
135
- // empty-wildcard path does (integrity failure, not unreadable).
136
- if (!existsDirectory(dir))
137
- return map;
138
- walk(dir);
139
- return map;
140
- }
141
- function existsDirectory(dir) {
142
206
  try {
143
- return statSync(dir).isDirectory();
207
+ if (!lstatSync(dir).isDirectory())
208
+ throw new Error("not a directory");
144
209
  }
145
- catch {
146
- return false;
210
+ catch (error) {
211
+ if (error.code === "ENOENT")
212
+ return map;
213
+ throw error;
147
214
  }
215
+ walk(dir);
216
+ return map;
148
217
  }
149
218
  try {
150
219
  process.exitCode = main(process.argv.slice(2));
@@ -1,15 +1,11 @@
1
- // The compatibility surface — the mirror of AgentBlueprintProtocol.Compatibility:
2
- // identity-exact or error. A manifest identity (one build_identities entry)
3
- // is matched only by an observed identity carrying the EXACT
4
- // (kind, name, version, digest) tuple; ranges deny
5
- // compatibility_identity_inexact on BOTH sides; a manifest identity with no
6
- // exact observed counterpart denies compatibility_entry_missing; duplicate
7
- // candidates deny compatibility_duplicate_entry.
1
+ import { isValue } from "./value.js";
8
2
  import * as digest from "./digest.js";
9
3
  import { keyfind } from "./registry_engine.js";
10
4
  const IDENTITY_KEYS = ["kind", "name", "version", "digest"];
11
5
  const EXACT_CHARSET = /^[A-Za-z0-9.+-]+$/;
12
6
  export function verify(deployment, observed) {
7
+ if (!deployment || !isValue(deployment.value) || !observed || !Array.isArray(observed.identities))
8
+ return { ok: false, e: "invalid_type" };
13
9
  const manifest = manifestIdentities(deployment);
14
10
  if (!manifest.ok)
15
11
  return manifest;
@@ -21,6 +17,8 @@ export function verify(deployment, observed) {
21
17
  // The exactness predicate (public on the twin): total over junk input — any
22
18
  // non-conforming shape answers false.
23
19
  export function exact(identity) {
20
+ if (!identity || typeof identity !== "object")
21
+ return false;
24
22
  const version = identity.version;
25
23
  if (typeof version !== "string")
26
24
  return false;
@@ -235,6 +235,8 @@ export function loadPriorCensus(map) {
235
235
  return loadWithMode(map, true);
236
236
  }
237
237
  function loadWithMode(map, priorCensus) {
238
+ if (!(map instanceof Map) || [...map].some(([key, bytes]) => typeof key !== "string" || !Buffer.isBuffer(bytes)))
239
+ return indexInvalid();
238
240
  const indexBytes = map.get("index.json");
239
241
  if (indexBytes === undefined)
240
242
  return indexInvalid();
@@ -709,7 +711,9 @@ function verifyRawBindings(cases, raws) {
709
711
  }
710
712
  // ---- tagged-object helpers ----------------------------------------------------------------------
711
713
  export function member(container, key) {
712
- if (container.t !== "obj")
714
+ if (!container || container.t !== "obj" || !Array.isArray(container.v))
715
+ return null;
716
+ if (Array.from(container.v).some((pair) => !Array.isArray(pair) || pair.length !== 2 || typeof pair[0] !== "string" || !pair[1] || typeof pair[1] !== "object"))
713
717
  return null;
714
718
  const found = container.v.find(([k]) => k === key);
715
719
  return found ? found[1] : null;
@@ -11,7 +11,7 @@
11
11
  // twin's classify_parse_error): invalid_encoding when any byte is bad, else
12
12
  // invalid_syntax. Ceiling/code failures carry their own reason verbatim —
13
13
  // the twin throws those from inside :json's hooks with the reason attached.
14
- import { maximum } from "./bounds.js";
14
+ import { coerce, maximum } from "./bounds.js";
15
15
  import { number } from "./canonical.js";
16
16
  import { err, ok } from "./value.js";
17
17
  // Thrown for ceiling/code denials — reason rides the exception.
@@ -28,11 +28,15 @@ export class SyntaxFail extends Error {
28
28
  export function decode(input, bounds) {
29
29
  if (typeof input !== "string" && !Buffer.isBuffer(input))
30
30
  return err("invalid_type");
31
+ const normalized = coerce(bounds === undefined ? maximum() : bounds);
32
+ if (!normalized.ok)
33
+ return normalized;
31
34
  const bytes = typeof input === "string" ? Buffer.from(input, "utf8") : input;
32
- // The profile is read field-wise (bytes/items/…) whatever the caller passed.
33
- const profile = (bounds ?? maximum());
35
+ const profile = normalized.v;
34
36
  if (bytes.length > profile.bytes)
35
37
  return err("ceiling:bytes");
38
+ if (typeof input === "string" && bytes.toString("utf8") !== input)
39
+ return err("invalid_encoding");
36
40
  const validUtf8 = isUtf8(bytes);
37
41
  const s = new Scanner(bytes, profile);
38
42
  try {
@@ -425,6 +429,8 @@ function utf8Len(firstByte) {
425
429
  }
426
430
  // Strict UTF-8 validation: rejects overlong forms, surrogates, > U+10FFFF.
427
431
  export function isUtf8(bytes) {
432
+ if (!Buffer.isBuffer(bytes))
433
+ return false;
428
434
  let i = 0;
429
435
  while (i < bytes.length) {
430
436
  const c = bytes[i];
@@ -33,8 +33,14 @@ export declare function fromValue(value: Value, opts?: {
33
33
  ok: false;
34
34
  e: string;
35
35
  };
36
- export declare function toValue(deployment: Deployment): Value;
37
- export declare function digestInput(deployment: Deployment): Value;
36
+ export declare function toValue(deployment: Deployment): Value | {
37
+ ok: false;
38
+ e: string;
39
+ };
40
+ export declare function digestInput(deployment: Deployment): Value | {
41
+ ok: false;
42
+ e: string;
43
+ };
38
44
  export declare function canonicalBytes(deployment: Deployment): {
39
45
  ok: true;
40
46
  v: string;
@@ -42,7 +48,10 @@ export declare function canonicalBytes(deployment: Deployment): {
42
48
  ok: false;
43
49
  e: string;
44
50
  };
45
- export declare function contentDigest(deployment: Deployment): string;
51
+ export declare function contentDigest(deployment: Deployment): string | {
52
+ ok: false;
53
+ e: string;
54
+ };
46
55
  export declare function verifyContentDigest(deployment: Deployment): VResult;
47
56
  export declare function verifyBinding(deployment: Deployment, bound: blueprint.Blueprint, obs: Observations): VResult;
48
57
  export declare function parseTimestamp(stamped: string): number | null;