@agent-blueprint-protocol/verifier 0.8.2 → 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 (49) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/README.md +6 -6
  3. package/dist/corpus/cases/blueprint-decode.json +1 -1
  4. package/dist/corpus/cases/bounds-new.json +1 -1
  5. package/dist/corpus/cases/bounds_algebra-intersect.json +1 -1
  6. package/dist/corpus/cases/canonicalization-encode.json +1 -1
  7. package/dist/corpus/cases/deployment-decode.json +1 -1
  8. package/dist/corpus/cases/federation-decode.json +1 -1
  9. package/dist/corpus/cases/json-decode.json +1 -1
  10. package/dist/corpus/cases/portability-scan.json +1 -1
  11. package/dist/corpus/cases/schema-validate_instance.json +1 -1
  12. package/dist/corpus/index.json +1 -1
  13. package/dist/kit/cli.mjs +1 -4
  14. package/dist/release-metadata.json +1 -1
  15. package/dist/verifier/b64url.d.ts +6 -2
  16. package/dist/verifier/b64url.js +6 -0
  17. package/dist/verifier/blueprint.d.ts +12 -3
  18. package/dist/verifier/blueprint.js +21 -7
  19. package/dist/verifier/bounds.d.ts +1 -1
  20. package/dist/verifier/bounds.js +3 -3
  21. package/dist/verifier/bounds_algebra.js +7 -4
  22. package/dist/verifier/canonical.d.ts +1 -1
  23. package/dist/verifier/canonical.js +41 -8
  24. package/dist/verifier/cli.js +111 -42
  25. package/dist/verifier/compatibility.js +6 -8
  26. package/dist/verifier/corpus.js +5 -1
  27. package/dist/verifier/decode.js +9 -3
  28. package/dist/verifier/deployment.d.ts +12 -3
  29. package/dist/verifier/deployment.js +33 -10
  30. package/dist/verifier/digest.d.ts +12 -0
  31. package/dist/verifier/digest.js +16 -2
  32. package/dist/verifier/extension.js +4 -4
  33. package/dist/verifier/federation.js +18 -74
  34. package/dist/verifier/negotiation.js +8 -2
  35. package/dist/verifier/portability.d.ts +1 -1
  36. package/dist/verifier/portability.js +45 -8
  37. package/dist/verifier/predicate.js +4 -7
  38. package/dist/verifier/registry_engine.js +29 -12
  39. package/dist/verifier/report.d.ts +8 -0
  40. package/dist/verifier/report.js +9 -2
  41. package/dist/verifier/run_corpus.js +4 -80
  42. package/dist/verifier/runner.d.ts +4 -0
  43. package/dist/verifier/runner.js +13 -15
  44. package/dist/verifier/schema.js +14 -55
  45. package/dist/verifier/signature.d.ts +2 -2
  46. package/dist/verifier/signature.js +10 -4
  47. package/dist/verifier/value.d.ts +9 -2
  48. package/dist/verifier/value.js +81 -11
  49. package/package.json +1 -1
@@ -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;
@@ -74,7 +72,7 @@ function validatedObserved(identities) {
74
72
  const wellFormed = typeof record === "object" &&
75
73
  record !== null &&
76
74
  Object.keys(record).length === 4 &&
77
- IDENTITY_KEYS.every((key) => typeof record[key] === "string");
75
+ IDENTITY_KEYS.every((key) => Object.hasOwn(record, key) && typeof record[key] === "string");
78
76
  if (!wellFormed)
79
77
  return { ok: false, e: "invalid_type" };
80
78
  const shaped = identity;
@@ -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;
@@ -1,9 +1,4 @@
1
- // The Deployment Manifest artifact — the mirror of AgentBlueprintProtocol.Deployment:
2
- // canonical verify → the 19-member registry table (exact-only build-identity
3
- // versions, lifecycle temporal rules, the as_of total rule) → the portability
4
- // scan (eligibility expressions as the open region) → the content-digest
5
- // comparison under the deployment_content domain; plus the six-stage
6
- // verify_binding deny set with its pinned order.
1
+ import { isValue } from "./value.js";
7
2
  import { coerce as coerceBounds, maximum as maxBounds } from "./bounds.js";
8
3
  import * as blueprint from "./blueprint.js";
9
4
  import * as canonical from "./canonical.js";
@@ -265,7 +260,14 @@ export function decode(bytes, bounds) {
265
260
  return { ok: true, v: { value } };
266
261
  }
267
262
  export function fromValue(value, opts = {}) {
263
+ if (opts === null || typeof opts !== "object" || Array.isArray(opts))
264
+ return { ok: false, e: "invalid_type" };
268
265
  const validated = opts.authoredExtensions ?? [];
266
+ if (!Array.isArray(validated) || validated.some((ns) => typeof ns !== "string"))
267
+ return { ok: false, e: "invalid_type" };
268
+ const shaped = canonical.encode(value);
269
+ if (!shaped.ok)
270
+ return shaped;
269
271
  if (validated.length > 0) {
270
272
  if (value.t !== "obj")
271
273
  return { ok: false, e: "invalid_type" };
@@ -282,24 +284,30 @@ export function fromValue(value, opts = {}) {
282
284
  return { ok: true, v: { value } };
283
285
  }
284
286
  export function toValue(deployment) {
285
- return deployment.value;
287
+ return deployment && isValue(deployment.value) ? deployment.value : { ok: false, e: "invalid_type" };
286
288
  }
287
289
  // ---- digest surface --------------------------------------------------------------------
288
290
  export function digestInput(deployment) {
291
+ if (!deployment || !isValue(deployment.value))
292
+ return { ok: false, e: "invalid_type" };
289
293
  if (deployment.value.t !== "obj")
290
294
  return deployment.value;
291
295
  return { t: "obj", v: deployment.value.v.filter(([name]) => !EVIDENCE_MEMBERS.includes(name)) };
292
296
  }
293
297
  export function canonicalBytes(deployment) {
298
+ if (!deployment || typeof deployment !== "object")
299
+ return { ok: false, e: "invalid_type" };
294
300
  return canonical.encode(deployment.value);
295
301
  }
296
302
  export function contentDigest(deployment) {
297
303
  const jcs = canonical.encode(digestInput(deployment));
298
304
  if (!jcs.ok)
299
- throw new Error("deployment digest input must encode");
305
+ return jcs;
300
306
  return digest.toTagged(digest.hash("deployment_content", Buffer.from(jcs.v, "utf8")));
301
307
  }
302
308
  export function verifyContentDigest(deployment) {
309
+ if (!deployment || !isValue(deployment.value))
310
+ return { ok: false, e: "invalid_type" };
303
311
  const members = deployment.value.t === "obj" ? deployment.value.v : [];
304
312
  const declared = keyfind(members, "deployment_digest");
305
313
  if (declared === null || declared.t !== "str")
@@ -313,6 +321,8 @@ export function verifyContentDigest(deployment) {
313
321
  // a non-array member, a non-object entry, a non-string operand, or an
314
322
  // unparsable descriptor digest is a malformed deployment, never empty.
315
323
  function boundToolBindings(deployment) {
324
+ if (!deployment || !isValue(deployment.value))
325
+ return { ok: false, e: "invalid_type" };
316
326
  if (deployment.value.t !== "obj")
317
327
  return { ok: false, e: "invalid_type" };
318
328
  const members = deployment.value.v;
@@ -349,6 +359,8 @@ function boundRelease(deployment) {
349
359
  const bindings = boundToolBindings(deployment);
350
360
  if (!bindings.ok)
351
361
  return bindings;
362
+ if (!deployment || !isValue(deployment.value))
363
+ return { ok: false, e: "invalid_type" };
352
364
  if (deployment.value.t !== "obj")
353
365
  return { ok: false, e: "invalid_type" };
354
366
  const release = keyfind(deployment.value.v, "blueprint_release");
@@ -372,6 +384,9 @@ function boundRelease(deployment) {
372
384
  // tool-binding completeness → mutation/recovery → attestation staleness →
373
385
  // observed rug-pull. Stages 5-6 are observation-gated.
374
386
  export function verifyBinding(deployment, bound, obs) {
387
+ if (!deployment || !isValue(deployment.value) || !bound || !isValue(bound.value) || !obs || typeof obs !== "object" || !obs.observed || typeof obs.observed !== "object" ||
388
+ (Object.getPrototypeOf(obs.observed) !== Object.prototype && Object.getPrototypeOf(obs.observed) !== null))
389
+ return { ok: false, e: "invalid_type" };
375
390
  const bindings = boundToolBindings(deployment);
376
391
  if (!bindings.ok)
377
392
  return bindings;
@@ -478,6 +493,8 @@ function stageRugPull(bindings, obs) {
478
493
  for (const entry of bindings) {
479
494
  const operation = memberString(entry, "logical_operation");
480
495
  const tagged = memberString(entry, "descriptor_digest");
496
+ if (typeof tagged !== "string")
497
+ return { ok: false, e: "invalid_type" };
481
498
  const parsed = digest.fromTagged(tagged);
482
499
  if (parsed.ok)
483
500
  attested.set(operation, parsed.v);
@@ -496,6 +513,8 @@ function stageRugPull(bindings, obs) {
496
513
  return { ok: true };
497
514
  }
498
515
  function recoveryMode(deployment) {
516
+ if (!deployment || !isValue(deployment.value))
517
+ return { ok: false, e: "invalid_type" };
499
518
  if (deployment.value.t !== "obj")
500
519
  return { ok: false, e: "invalid_type" };
501
520
  const owner = keyfind(deployment.value.v, "effect_owner");
@@ -514,6 +533,8 @@ function recoveryMode(deployment) {
514
533
  function blueprintContentDigest(bound) {
515
534
  try {
516
535
  const tagged = blueprint.contentDigest(bound);
536
+ if (typeof tagged !== "string")
537
+ return null;
517
538
  const parsed = digest.fromTagged(tagged);
518
539
  return parsed.ok ? parsed.v : null;
519
540
  }
@@ -544,7 +565,7 @@ function blueprintEffects(bound) {
544
565
  return out;
545
566
  }
546
567
  function blueprintFamilyKinds(bound) {
547
- const out = {};
568
+ const out = Object.create(null);
548
569
  for (const entry of memberArrayOf(bound, "capability_requirements")) {
549
570
  if (entry.t !== "obj")
550
571
  continue;
@@ -557,7 +578,7 @@ function blueprintFamilyKinds(bound) {
557
578
  return out;
558
579
  }
559
580
  function blueprintEffectKinds(bound) {
560
- const out = {};
581
+ const out = Object.create(null);
561
582
  for (const entry of memberArrayOf(bound, "effect_intents")) {
562
583
  if (entry.t !== "obj")
563
584
  continue;
@@ -814,6 +835,8 @@ function normalizeBounds(bounds) {
814
835
  // (DateTime.from_iso8601 parity, restricted to the calendar-valid Z form the
815
836
  // tables already enforce).
816
837
  export function parseTimestamp(stamped) {
838
+ if (typeof stamped !== "string")
839
+ return null;
817
840
  const matched = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|[+-]\d{2}:\d{2})$/.exec(stamped);
818
841
  if (matched === null)
819
842
  return null;
@@ -5,8 +5,20 @@ export interface Digest {
5
5
  bytes: Buffer;
6
6
  }
7
7
  export declare function of(data: Buffer): Digest;
8
+ export declare function of(data: unknown): Digest | {
9
+ ok: false;
10
+ e: string;
11
+ };
8
12
  export declare function hash(domain: Domain, data: Buffer): Digest;
13
+ export declare function hash(domain: unknown, data: unknown): Digest | {
14
+ ok: false;
15
+ e: string;
16
+ };
9
17
  export declare function toTagged(digest: Digest): string;
18
+ export declare function toTagged(digest: unknown): string | {
19
+ ok: false;
20
+ e: string;
21
+ };
10
22
  export declare function fromTagged(input: string): {
11
23
  ok: true;
12
24
  v: Digest;
@@ -16,15 +16,25 @@ export const SEPARATORS = {
16
16
  const SIZES = { "sha-256": 32 };
17
17
  const TAGS = Object.keys(SIZES);
18
18
  export function of(data) {
19
+ if (!Buffer.isBuffer(data))
20
+ return { ok: false, e: "invalid_type" };
19
21
  return { algorithm: "sha-256", bytes: createHash("sha256").update(data).digest() };
20
22
  }
21
- export function hash(domain, data) {
23
+ export function hash(domainInput, data) {
24
+ const domain = domainInput;
25
+ if (typeof domain !== "string" || !Object.hasOwn(SEPARATORS, domain) || !Buffer.isBuffer(data))
26
+ return { ok: false, e: "invalid_type" };
22
27
  return of(Buffer.concat([Buffer.from(SEPARATORS[domain], "utf8"), Buffer.from([0]), data]));
23
28
  }
24
- export function toTagged(digest) {
29
+ export function toTagged(input) {
30
+ const digest = input;
31
+ if (!digest || digest.algorithm !== "sha-256" || !Buffer.isBuffer(digest.bytes) || digest.bytes.length !== 32)
32
+ return { ok: false, e: "invalid_type" };
25
33
  return "sha-256:" + b64.encode(digest.bytes);
26
34
  }
27
35
  export function fromTagged(input) {
36
+ if (typeof input !== "string")
37
+ return { ok: false, e: "invalid_type" };
28
38
  const colon = input.indexOf(":");
29
39
  if (colon === -1)
30
40
  return { ok: false, e: "digest_encoding_invalid" };
@@ -41,6 +51,8 @@ export function fromTagged(input) {
41
51
  }
42
52
  // Constant-time equality over the digest bytes (the twin's xor-accumulate).
43
53
  export function equal(a, b) {
54
+ if (!a || !b || !Buffer.isBuffer(a.bytes) || !Buffer.isBuffer(b.bytes))
55
+ return false;
44
56
  if (a.bytes.length !== b.bytes.length || a.algorithm !== b.algorithm)
45
57
  return false;
46
58
  let diff = 0;
@@ -49,6 +61,8 @@ export function equal(a, b) {
49
61
  return diff === 0;
50
62
  }
51
63
  export function verifyContent(domain, jcsBytes, tagged) {
64
+ if (typeof domain !== "string" || !Object.hasOwn(SEPARATORS, domain) || !Buffer.isBuffer(jcsBytes))
65
+ return { ok: false, e: "invalid_type" };
52
66
  const declared = fromTagged(tagged);
53
67
  if (!declared.ok)
54
68
  return declared;