@agent-blueprint-protocol/verifier 0.7.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.
Files changed (78) hide show
  1. package/CHANGELOG.md +891 -0
  2. package/LICENSE +190 -0
  3. package/NOTICE +4 -0
  4. package/README.md +199 -0
  5. package/dist/corpus/cases/base64url-decode.json +1 -0
  6. package/dist/corpus/cases/blueprint-decode.json +1 -0
  7. package/dist/corpus/cases/bounds-new.json +1 -0
  8. package/dist/corpus/cases/bounds_algebra-intersect.json +1 -0
  9. package/dist/corpus/cases/canonicalization-encode.json +1 -0
  10. package/dist/corpus/cases/compatibility-verify.json +1 -0
  11. package/dist/corpus/cases/deployment-decode.json +1 -0
  12. package/dist/corpus/cases/digest-tagged.json +1 -0
  13. package/dist/corpus/cases/extension-resolve.json +1 -0
  14. package/dist/corpus/cases/federation-decode.json +1 -0
  15. package/dist/corpus/cases/federation-verify_commitment.json +1 -0
  16. package/dist/corpus/cases/json-decode.json +1 -0
  17. package/dist/corpus/cases/negotiation-negotiate.json +1 -0
  18. package/dist/corpus/cases/portability-scan.json +1 -0
  19. package/dist/corpus/cases/schema-validate_instance.json +1 -0
  20. package/dist/corpus/cases/signature-verify.json +1 -0
  21. package/dist/corpus/index.json +1 -0
  22. package/dist/corpus/schemas/estate-contract.schema.json +1 -0
  23. package/dist/corpus/schemas/object.schema.json +1 -0
  24. package/dist/corpus/vectors/blueprint-golden.json +1 -0
  25. package/dist/corpus/vectors/deployment-golden.json +1 -0
  26. package/dist/corpus/vectors/rfc8785-numbers.json +1 -0
  27. package/dist/kit/cli.mjs +24 -0
  28. package/dist/verifier/b64url.d.ts +16 -0
  29. package/dist/verifier/b64url.js +36 -0
  30. package/dist/verifier/blueprint.d.ts +56 -0
  31. package/dist/verifier/blueprint.js +795 -0
  32. package/dist/verifier/bounds.d.ts +19 -0
  33. package/dist/verifier/bounds.js +45 -0
  34. package/dist/verifier/bounds_algebra.d.ts +27 -0
  35. package/dist/verifier/bounds_algebra.js +282 -0
  36. package/dist/verifier/canonical.d.ts +17 -0
  37. package/dist/verifier/canonical.js +160 -0
  38. package/dist/verifier/cli.d.ts +1 -0
  39. package/dist/verifier/cli.js +149 -0
  40. package/dist/verifier/compatibility.d.ts +22 -0
  41. package/dist/verifier/compatibility.js +112 -0
  42. package/dist/verifier/corpus.d.ts +42 -0
  43. package/dist/verifier/corpus.js +672 -0
  44. package/dist/verifier/decode.d.ts +16 -0
  45. package/dist/verifier/decode.js +457 -0
  46. package/dist/verifier/deployment.d.ts +49 -0
  47. package/dist/verifier/deployment.js +830 -0
  48. package/dist/verifier/digest.d.ts +23 -0
  49. package/dist/verifier/digest.js +58 -0
  50. package/dist/verifier/extension.d.ts +11 -0
  51. package/dist/verifier/extension.js +75 -0
  52. package/dist/verifier/federation.d.ts +76 -0
  53. package/dist/verifier/federation.js +817 -0
  54. package/dist/verifier/negotiation.d.ts +29 -0
  55. package/dist/verifier/negotiation.js +324 -0
  56. package/dist/verifier/portability.d.ts +12 -0
  57. package/dist/verifier/portability.js +184 -0
  58. package/dist/verifier/predicate.d.ts +10 -0
  59. package/dist/verifier/predicate.js +172 -0
  60. package/dist/verifier/registry.d.ts +16 -0
  61. package/dist/verifier/registry.js +120 -0
  62. package/dist/verifier/registry_engine.d.ts +42 -0
  63. package/dist/verifier/registry_engine.js +220 -0
  64. package/dist/verifier/report.d.ts +13 -0
  65. package/dist/verifier/report.js +29 -0
  66. package/dist/verifier/run_corpus.d.ts +1 -0
  67. package/dist/verifier/run_corpus.js +81 -0
  68. package/dist/verifier/runner.d.ts +20 -0
  69. package/dist/verifier/runner.js +778 -0
  70. package/dist/verifier/schema.d.ts +20 -0
  71. package/dist/verifier/schema.js +623 -0
  72. package/dist/verifier/self_checks.d.ts +1 -0
  73. package/dist/verifier/self_checks.js +316 -0
  74. package/dist/verifier/signature.d.ts +23 -0
  75. package/dist/verifier/signature.js +226 -0
  76. package/dist/verifier/value.d.ts +44 -0
  77. package/dist/verifier/value.js +77 -0
  78. package/package.json +42 -0
@@ -0,0 +1,778 @@
1
+ // The case executor — the mirror of AgentBlueprintProtocol.Conformance.Runner:
2
+ // dispatches each corpus case against the verifier's own surface twins and
3
+ // compares to the case's code-level, shape-agnostic expectation (invalid →
4
+ // projected error-code string; valid → per-surface projection members with
5
+ // the byte_fallback and Elixir-`==`-style numeric-tolerant comparison).
6
+ import * as b64 from "./b64url.js";
7
+ import { coerce } from "./bounds.js";
8
+ import { member, memberNumber, memberString } from "./corpus.js";
9
+ import * as digest from "./digest.js";
10
+ import { float, int, nil, str } from "./value.js";
11
+ // Surface twins (implemented in their own modules):
12
+ import * as blueprint from "./blueprint.js";
13
+ import * as deployment from "./deployment.js";
14
+ import * as negotiation from "./negotiation.js";
15
+ import * as boundsAlgebra from "./bounds_algebra.js";
16
+ import * as compatibility from "./compatibility.js";
17
+ import * as federation from "./federation.js";
18
+ import * as portability from "./portability.js";
19
+ import * as schema from "./schema.js";
20
+ import * as signature from "./signature.js";
21
+ export function run(corpus) {
22
+ return corpus.cases.map((file) => file.cases.map((caseObj) => ({
23
+ caseId: caseObj.id,
24
+ agree: execute(caseObj, corpus.data, corpus.raws).agree,
25
+ })));
26
+ }
27
+ export function execute(caseObj, data, raws) {
28
+ const input = caseObj.input;
29
+ const expected = caseObj.expected;
30
+ const actual = dispatch(caseObj.surface, input, data, raws);
31
+ return { actual, agree: agrees(expected, actual) };
32
+ }
33
+ // --- agreement -------------------------------------------------------------------
34
+ function agrees(expected, actual) {
35
+ if (expected.t !== "obj")
36
+ return false;
37
+ const verdict = memberString(expected, "verdict");
38
+ if (verdict === "invalid") {
39
+ if (!actual.ok)
40
+ return actual.e === memberString(expected, "code");
41
+ return false;
42
+ }
43
+ if (verdict === "valid") {
44
+ const projections = expected.v.filter(([k]) => k !== "verdict");
45
+ if (projections.length === 0)
46
+ return false; // vacuous-green refusal
47
+ if (!actual.ok)
48
+ return false;
49
+ return projections.every(([key, want]) => {
50
+ const got = actual.v[key];
51
+ if (got === undefined)
52
+ return false;
53
+ return compare(want, got);
54
+ });
55
+ }
56
+ return false;
57
+ }
58
+ function compare(want, got) {
59
+ if (want.t === "str" && typeof got === "string") {
60
+ if (want.v === got)
61
+ return true;
62
+ const decoded = b64.decodeLenient(want.v);
63
+ return decoded.ok && decoded.v.equals(Buffer.from(got, "utf8"));
64
+ }
65
+ // Byte-bearing projections keep Buffers: the expected b64url spelling
66
+ // decodes to the actual bytes (the twin's byte_fallback).
67
+ if (want.t === "str" && Buffer.isBuffer(got)) {
68
+ const decoded = b64.decodeLenient(want.v);
69
+ return decoded.ok && decoded.v.equals(got);
70
+ }
71
+ // Numeric-tolerant deep comparison (Elixir == semantics: int 2 == float 2.0).
72
+ return deepEqual(want, got);
73
+ }
74
+ function deepEqual(want, got) {
75
+ if (want.t === "int" || want.t === "float") {
76
+ return typeof got === "number" && want.v === got;
77
+ }
78
+ if (want.t === "bool")
79
+ return typeof got === "boolean" && want.v === got;
80
+ if (want.t === "null")
81
+ return got === null;
82
+ if (want.t === "str")
83
+ return typeof got === "string" && want.v === got;
84
+ if (want.t === "arr") {
85
+ return (Array.isArray(got) && want.v.length === got.length && want.v.every((w, i) => deepEqual(w, got[i])));
86
+ }
87
+ if (want.t === "obj") {
88
+ if (got === null || typeof got !== "object" || Array.isArray(got))
89
+ return false;
90
+ const gotRecord = got;
91
+ if (Object.keys(gotRecord).length !== want.v.length)
92
+ return false;
93
+ return want.v.every(([k, w]) => k in gotRecord && deepEqual(w, gotRecord[k]));
94
+ }
95
+ return false;
96
+ }
97
+ // --- dispatch ----------------------------------------------------------------------
98
+ const BOUNDS_KEYS = {
99
+ bytes: "bytes",
100
+ depth: "depth",
101
+ members: "members",
102
+ items: "items",
103
+ nodes: "nodes",
104
+ string: "string",
105
+ key: "key",
106
+ number_lexeme: "number_lexeme",
107
+ };
108
+ const DIGEST_DOMAINS = [
109
+ "blueprint_content",
110
+ "deployment_content",
111
+ "federation_envelope",
112
+ "signature",
113
+ "extension_schema",
114
+ "extension_registry",
115
+ "conformance_report",
116
+ "corpus_index",
117
+ ];
118
+ const LOGICAL_STATES = [
119
+ "submitted",
120
+ "working",
121
+ "completed",
122
+ "failed",
123
+ "canceled",
124
+ "rejected",
125
+ "input_required",
126
+ "auth_required",
127
+ ];
128
+ const FEDERATION_CODEC_KEYS = [
129
+ "from_a2a_state",
130
+ "to_a2a_state",
131
+ "from_mcp_state",
132
+ "to_mcp_state",
133
+ "from_a2a_carrier",
134
+ "from_mcp_carrier",
135
+ ];
136
+ const LATTICE = [
137
+ "public",
138
+ "internal",
139
+ "confidential",
140
+ "restricted",
141
+ "none",
142
+ "local_policy",
143
+ "external_authority_required",
144
+ "human_required",
145
+ "separated_human_required",
146
+ "ordinary",
147
+ "money",
148
+ "authority",
149
+ "secret",
150
+ "summary",
151
+ "detail",
152
+ "full",
153
+ "pci",
154
+ "phi",
155
+ ];
156
+ function dispatch(surface, input, data, raws) {
157
+ switch (surface) {
158
+ case "json.decode": {
159
+ const overrides = boundsOverrides(input);
160
+ if (!overrides.ok)
161
+ return overrides;
162
+ const bytes = inputBytes(input, raws);
163
+ if (!bytes.ok)
164
+ return bytes;
165
+ const { decode } = require_decode();
166
+ const decoded = decode(bytes.v === NON_BINARY ? 123 : bytes.v, overrides.v);
167
+ if (!decoded.ok)
168
+ return decoded;
169
+ return { ok: true, v: { value: toPlainValue(decoded.v) } };
170
+ }
171
+ case "canonicalization.encode": {
172
+ const bytes = inputBytes(input, raws);
173
+ if (!bytes.ok)
174
+ return bytes;
175
+ const { decode } = require_decode();
176
+ const decoded = decode(bytes.v === NON_BINARY ? 123 : bytes.v);
177
+ if (!decoded.ok)
178
+ return decoded;
179
+ const overrides = boundsOverrides(input);
180
+ if (!overrides.ok)
181
+ return overrides;
182
+ const { encode } = require_encode();
183
+ const encoded = encode(decoded.v, overrides.v);
184
+ if (!encoded.ok)
185
+ return encoded;
186
+ return { ok: true, v: { encoded: encoded.v } };
187
+ }
188
+ case "base64url.decode": {
189
+ const segment = memberString(input, "base64url");
190
+ if (segment === null)
191
+ return { ok: false, e: "invalid_type" };
192
+ const decoded = b64.decodeStrict(segment);
193
+ if (!decoded.ok)
194
+ return decoded;
195
+ // Keep the raw bytes: toString("utf8") is lossy for non-UTF8 decodes,
196
+ // and agreement runs through the b64 byte-fallback (the twin's
197
+ // %{"decoded" => binary} + byte_fallback semantics).
198
+ return { ok: true, v: { decoded: decoded.v } };
199
+ }
200
+ case "digest.tagged": {
201
+ const tagged = memberString(input, "tagged");
202
+ if (tagged !== null) {
203
+ const parsed = digest.fromTagged(tagged);
204
+ if (!parsed.ok)
205
+ return parsed;
206
+ return { ok: true, v: { tagged: digest.toTagged(parsed.v) } };
207
+ }
208
+ const text = memberString(input, "text");
209
+ const declared = memberString(input, "declared");
210
+ if (text !== null && declared !== null) {
211
+ const bytes = inputBytes(input, raws);
212
+ if (!bytes.ok)
213
+ return bytes;
214
+ const domain = memberString(input, "domain");
215
+ if (domain === null || !DIGEST_DOMAINS.includes(domain)) {
216
+ return { ok: false, e: "invalid_type" };
217
+ }
218
+ const verified = digest.verifyContent(domain, bytes.v, declared);
219
+ if (!verified.ok)
220
+ return verified;
221
+ return { ok: true, v: { verified: true } };
222
+ }
223
+ return { ok: false, e: "invalid_type" };
224
+ }
225
+ case "blueprint.decode": {
226
+ const overrides = boundsOverrides(input);
227
+ if (!overrides.ok)
228
+ return overrides;
229
+ const bytes = inputBytes(input, raws);
230
+ if (!bytes.ok)
231
+ return bytes;
232
+ const decoded = blueprint.decode(bytes.v, overrides.v);
233
+ if (!decoded.ok)
234
+ return decoded;
235
+ return { ok: true, v: { digest: blueprint.contentDigest(decoded.v) } };
236
+ }
237
+ case "deployment.decode": {
238
+ const overrides = boundsOverrides(input);
239
+ if (!overrides.ok)
240
+ return overrides;
241
+ const bytes = inputBytes(input, raws);
242
+ if (!bytes.ok)
243
+ return bytes;
244
+ const decoded = deployment.decode(bytes.v, overrides.v);
245
+ if (!decoded.ok)
246
+ return decoded;
247
+ return projectDeployment(decoded.v, input);
248
+ }
249
+ case "signature.verify": {
250
+ const entryText = memberString(input, "entry");
251
+ if (entryText === null)
252
+ return { ok: false, e: "invalid_type" };
253
+ const { decode } = require_decode();
254
+ const entry = decode(entryText);
255
+ if (!entry.ok)
256
+ return entry;
257
+ const keys = buildKeys(input);
258
+ if (!keys.ok)
259
+ return keys;
260
+ const verified = signature.verify(entry.v, keys.v);
261
+ if (!verified.ok)
262
+ return verified;
263
+ return { ok: true, v: { verified: true } };
264
+ }
265
+ case "schema.validate_instance": {
266
+ const schemaInput = schemaInputValue(input, data);
267
+ if (!schemaInput.ok)
268
+ return schemaInput;
269
+ const instance = taggedField(input, "instance");
270
+ if (!instance.ok)
271
+ return instance;
272
+ const dialect = memberString(input, "dialect") ?? schema.dialect();
273
+ const validated = schema.validateInstance(schemaInput.v, instance.v, dialect);
274
+ if ("ok" in validated && !validated.ok)
275
+ return validated;
276
+ return { ok: true, v: { valid: true } };
277
+ }
278
+ case "negotiation.negotiate":
279
+ case "extension.resolve": {
280
+ const artifact = taggedField(input, "artifact");
281
+ if (!artifact.ok)
282
+ return artifact;
283
+ const support = buildSupport(input);
284
+ if (!support.ok)
285
+ return support;
286
+ return projectNegotiation(negotiation.negotiate(artifact.v, support.v));
287
+ }
288
+ case "bounds.new": {
289
+ const overrides = boundsOverrides(input);
290
+ if (!overrides.ok)
291
+ return overrides;
292
+ const bounds = coerce(overrides.v);
293
+ if (!bounds.ok)
294
+ return bounds;
295
+ return { ok: true, v: { bounds: bounds.v } };
296
+ }
297
+ case "bounds_algebra.intersect": {
298
+ const blueprintSet = boundSet(input, "blueprint");
299
+ if (!blueprintSet.ok)
300
+ return blueprintSet;
301
+ const deploymentSet = boundSet(input, "deployment");
302
+ if (!deploymentSet.ok)
303
+ return deploymentSet;
304
+ const hostSet = boundSet(input, "host");
305
+ if (!hostSet.ok)
306
+ return hostSet;
307
+ const result = boundsAlgebra.intersect(blueprintSet.v, deploymentSet.v, hostSet.v, memberString(input, "protected_clamp") === "acknowledge" ? "acknowledge" : "deny");
308
+ if (!result.ok)
309
+ return result;
310
+ const effective = {};
311
+ for (const [k, v] of Object.entries(result.v.effective))
312
+ effective[k] = v;
313
+ return { ok: true, v: { effective, clamp_count: result.v.clamps.length } };
314
+ }
315
+ case "portability.scan": {
316
+ const bytes = inputBytes(input, raws);
317
+ if (!bytes.ok)
318
+ return bytes;
319
+ const { decode } = require_decode();
320
+ const decoded = decode(bytes.v);
321
+ if (!decoded.ok)
322
+ return decoded;
323
+ const scanned = portability.scan(decoded.v);
324
+ if (!scanned.ok)
325
+ return scanned;
326
+ return { ok: true, v: { clean: true } };
327
+ }
328
+ case "compatibility.verify": {
329
+ const bytes = inputBytes(input, raws);
330
+ if (!bytes.ok)
331
+ return bytes;
332
+ const decoded = deployment.decode(bytes.v);
333
+ if (!decoded.ok)
334
+ return decoded;
335
+ const observed = buildObserved(input);
336
+ if (!observed.ok)
337
+ return observed;
338
+ const verified = compatibility.verify(decoded.v, observed.v);
339
+ if (!verified.ok)
340
+ return verified;
341
+ return { ok: true, v: { verified: true } };
342
+ }
343
+ case "federation.decode": {
344
+ const text = memberString(input, "text");
345
+ const b64Input = memberString(input, "base64url");
346
+ const nonBinary = member(input, "non_binary");
347
+ if (text !== null || b64Input !== null || (nonBinary !== null && nonBinary.t === "bool" && nonBinary.v)) {
348
+ const overrides = boundsOverrides(input);
349
+ if (!overrides.ok)
350
+ return overrides;
351
+ const bytes = inputBytes(input, raws);
352
+ if (!bytes.ok)
353
+ return bytes;
354
+ const decoded = federation.decode(bytes.v, overrides.v);
355
+ if (!decoded.ok)
356
+ return decoded;
357
+ const canonical = federation.canonicalBytes(decoded.v);
358
+ if (!canonical.ok)
359
+ return canonical;
360
+ return { ok: true, v: { canonical: canonical.v } };
361
+ }
362
+ if (FEDERATION_CODEC_KEYS.some((k) => member(input, k) !== null)) {
363
+ return dispatchFederationCodec(input);
364
+ }
365
+ return { ok: false, e: "invalid_type" };
366
+ }
367
+ case "federation.verify_commitment": {
368
+ const envelopeText = memberString(input, "envelope");
369
+ if (envelopeText === null)
370
+ return { ok: false, e: "invalid_type" };
371
+ const decoded = federation.decode(envelopeText);
372
+ if (!decoded.ok)
373
+ return decoded;
374
+ const context = buildContext(input);
375
+ if (!context.ok)
376
+ return context;
377
+ const fact = federation.verifyCommitment(decoded.v, context.v);
378
+ if (!fact.ok)
379
+ return fact;
380
+ return { ok: true, v: { task_identity: fact.v.taskIdentity } };
381
+ }
382
+ default:
383
+ return { ok: false, e: "invalid_type" };
384
+ }
385
+ }
386
+ // decode/encode are imported lazily through these accessors to keep the
387
+ // module-init graph acyclic under node ESM (decode ↔ canonical already
388
+ // cycle by design; runner sits above both).
389
+ import { decode } from "./decode.js";
390
+ import { encode } from "./canonical.js";
391
+ function require_decode() {
392
+ return { decode };
393
+ }
394
+ function require_encode() {
395
+ return { encode };
396
+ }
397
+ // --- input builders ------------------------------------------------------------------
398
+ const NON_BINARY = Symbol("non-binary");
399
+ function inputBytes(input, raws) {
400
+ const nonBinary = member(input, "non_binary");
401
+ if (nonBinary !== null && nonBinary.t === "bool" && nonBinary.v)
402
+ return { ok: true, v: NON_BINARY };
403
+ const text = memberString(input, "text");
404
+ if (text !== null)
405
+ return { ok: true, v: Buffer.from(text, "utf8") };
406
+ const encoded = memberString(input, "base64url");
407
+ if (encoded !== null) {
408
+ const decoded = b64.decodeLenient(encoded);
409
+ if (!decoded.ok)
410
+ return { ok: false, e: "invalid_type" };
411
+ return { ok: true, v: decoded.v };
412
+ }
413
+ const rawFile = memberString(input, "raw_file");
414
+ if (rawFile !== null) {
415
+ const bytes = raws.get(rawFile);
416
+ if (bytes === undefined)
417
+ return { ok: false, e: "invalid_type" };
418
+ return { ok: true, v: bytes };
419
+ }
420
+ return { ok: false, e: "invalid_type" };
421
+ }
422
+ function boundsOverrides(input) {
423
+ const overrides = member(input, "bounds");
424
+ if (overrides === null)
425
+ return { ok: true, v: {} };
426
+ if (overrides.t !== "obj")
427
+ return { ok: false, e: "invalid_type" };
428
+ // Null prototype: a corpus-carried "__proto__" key stays a plain member
429
+ // (Elixir denies it as unknown_bound; a plain object would silently drop it).
430
+ const out = Object.create(null);
431
+ for (const [key, value] of overrides.v) {
432
+ // Unknown keys pass through so coerce() denies them (unknown_bound).
433
+ const mapped = BOUNDS_KEYS[key] ?? key;
434
+ out[mapped] = value.t === "int" || value.t === "float" ? value.v : plainOf(value);
435
+ }
436
+ return { ok: true, v: out };
437
+ }
438
+ function plainOf(value) {
439
+ switch (value.t) {
440
+ case "null":
441
+ return null;
442
+ case "bool":
443
+ case "int":
444
+ case "float":
445
+ case "str":
446
+ return value.v;
447
+ case "arr":
448
+ return value.v.map(plainOf);
449
+ case "obj":
450
+ return Object.fromEntries(value.v.map(([k, v]) => [k, plainOf(v)]));
451
+ }
452
+ }
453
+ function taggedField(input, key) {
454
+ const found = member(input, key);
455
+ if (found === null)
456
+ return { ok: false, e: "invalid_type" };
457
+ return { ok: true, v: found };
458
+ }
459
+ function schemaInputValue(input, data) {
460
+ const schemaFile = memberString(input, "schema_file");
461
+ if (schemaFile !== null) {
462
+ const found = data.get(schemaFile);
463
+ if (found === undefined)
464
+ return { ok: false, e: "invalid_type" };
465
+ // A loaded non-map value passes through raw (the twin's schema_input);
466
+ // parse/validate owns its judgment.
467
+ return { ok: true, v: found };
468
+ }
469
+ const inline = member(input, "schema");
470
+ // The twin's schema_input accepts only inline MAP schemas; anything else
471
+ // denies invalid_type at the input boundary.
472
+ if (inline !== null && inline.t === "obj")
473
+ return { ok: true, v: inline };
474
+ return { ok: false, e: "invalid_type" };
475
+ }
476
+ function buildKeys(input) {
477
+ const keys = member(input, "keys");
478
+ if (keys === null || keys.t !== "arr")
479
+ return { ok: false, e: "invalid_type" };
480
+ const out = [];
481
+ for (const entry of keys.v) {
482
+ if (entry.t !== "obj")
483
+ return { ok: false, e: "invalid_type" };
484
+ const keyId = memberString(entry, "key_id");
485
+ const encoded = memberString(entry, "key");
486
+ if (keyId === null || encoded === null)
487
+ return { ok: false, e: "invalid_type" };
488
+ const decoded = b64.decodeLenient(encoded);
489
+ if (!decoded.ok || decoded.v.length !== 32)
490
+ return { ok: false, e: "invalid_type" };
491
+ out.push({ keyId, key: decoded.v });
492
+ }
493
+ return { ok: true, v: out };
494
+ }
495
+ function buildSupport(input) {
496
+ const support = member(input, "support");
497
+ const supportInput = support === null ? null : support.t === "obj" ? support : null;
498
+ if (support !== null && support.t !== "obj")
499
+ return { ok: false, e: "invalid_type" };
500
+ const source = supportInput ?? { t: "obj", v: [] };
501
+ const revisionsRaw = member(source, "revisions");
502
+ const revisions = revisionsRaw !== null && revisionsRaw.t === "arr"
503
+ ? revisionsRaw.v.map((r) => (r.t === "int" ? r.v : plainOf(r)))
504
+ : [1];
505
+ const coreFieldsRaw = member(source, "core_fields");
506
+ const coreFields = coreFieldsRaw !== null && coreFieldsRaw.t === "arr"
507
+ ? coreFieldsRaw.v.map((r) => (r.t === "str" ? r.v : String(plainOf(r))))
508
+ : [];
509
+ const registryRaw = member(source, "registry");
510
+ const registry = Object.create(null);
511
+ if (registryRaw !== null && registryRaw.t === "obj") {
512
+ for (const [ns, entryValue] of registryRaw.v) {
513
+ if (entryValue.t !== "obj")
514
+ continue;
515
+ const criticality = memberString(entryValue, "criticality") ?? "optional";
516
+ const state = memberString(entryValue, "state") ?? "active";
517
+ registry[ns] = {
518
+ owner: memberString(entryValue, "owner") ?? "corpus",
519
+ criticality: criticality === "critical" ? "critical" : "optional",
520
+ state: (["reserved", "deprecated", "retired"].includes(state) ? state : "active"),
521
+ schemaDigest: memberString(entryValue, "schema_digest") ?? null,
522
+ a2aUri: memberString(entryValue, "a2a_uri") ?? `https://example.com/extensions/${ns}`,
523
+ promotedAtRevision: memberNumber(entryValue, "promoted_at_revision") ?? null,
524
+ };
525
+ }
526
+ }
527
+ const schemasRaw = member(source, "schemas");
528
+ const schemas = {};
529
+ if (schemasRaw !== null && schemasRaw.t === "obj") {
530
+ for (const [ns, schemaValue] of schemasRaw.v)
531
+ schemas[ns] = schemaValue;
532
+ }
533
+ return {
534
+ ok: true,
535
+ v: {
536
+ revisions: new Set(revisions),
537
+ coreFields: new Set(coreFields),
538
+ registry,
539
+ schemas,
540
+ },
541
+ };
542
+ }
543
+ function boundSet(input, key) {
544
+ const found = member(input, key);
545
+ if (found === null || found.t !== "obj")
546
+ return { ok: false, e: "invalid_type" };
547
+ const out = Object.create(null);
548
+ for (const [name, value] of found.v) {
549
+ if (!LATTICE.includes(name) && !isBoundName(name)) {
550
+ // Off-lattice names pass through so the algebra denies them.
551
+ out[name] = plainOf(value);
552
+ continue;
553
+ }
554
+ out[name] = descopeValue(value);
555
+ }
556
+ return { ok: true, v: out };
557
+ }
558
+ // The 13-bound vocabulary (BoundsAlgebra.names()): every protected bound
559
+ // routes through descopeValue exactly as the twin's add_bound_entry does.
560
+ const BOUND_NAMES = [
561
+ "approval_trait",
562
+ "authority_trait",
563
+ "classification_ceiling",
564
+ "disclosure_ceiling",
565
+ "effect_impact_ceiling",
566
+ "max_attempts",
567
+ "max_concurrency",
568
+ "max_cost",
569
+ "max_depth",
570
+ "max_descendants",
571
+ "max_elapsed_ms",
572
+ "max_fan_out",
573
+ "max_tokens",
574
+ ];
575
+ function isBoundName(name) {
576
+ return BOUND_NAMES.includes(name);
577
+ }
578
+ // Scope values arrive as {ordinal, markers[]}; costs as {amount, currency};
579
+ // bare strings as lattice atoms.
580
+ function descopeValue(value) {
581
+ if (value.t === "obj") {
582
+ const ordinal = memberString(value, "ordinal");
583
+ if (ordinal !== null) {
584
+ const markersRaw = member(value, "markers");
585
+ return {
586
+ ordinal,
587
+ markers: markersRaw !== null && markersRaw.t === "arr"
588
+ ? markersRaw.v.map((m) => (m.t === "str" ? m.v : String(plainOf(m))))
589
+ : [],
590
+ };
591
+ }
592
+ const amount = memberNumber(value, "amount");
593
+ const currency = memberString(value, "currency");
594
+ if (amount !== null && currency !== null) {
595
+ return { amount, currency };
596
+ }
597
+ return plainOf(value);
598
+ }
599
+ if (value.t === "str")
600
+ return value.v;
601
+ return plainOf(value);
602
+ }
603
+ function buildObserved(input) {
604
+ const observed = member(input, "observed");
605
+ if (observed === null || observed.t !== "obj")
606
+ return { ok: false, e: "invalid_type" };
607
+ const identities = member(observed, "identities");
608
+ if (identities === null || identities.t !== "arr")
609
+ return { ok: false, e: "invalid_type" };
610
+ const out = [];
611
+ // A malformed identity element denies typed at the RUNNER (the twin's
612
+ // prior_receipts pattern) — never reaches Compatibility, never crashes.
613
+ for (const entry of identities.v) {
614
+ if (entry.t !== "obj")
615
+ return { ok: false, e: "invalid_type" };
616
+ const kind = entry.v.find(([k]) => k === "kind");
617
+ const name = entry.v.find(([k]) => k === "name");
618
+ const version = entry.v.find(([k]) => k === "version");
619
+ const digestEntry = entry.v.find(([k]) => k === "digest");
620
+ if (!kind || !name || !version || !digestEntry)
621
+ return { ok: false, e: "invalid_type" };
622
+ out.push({
623
+ kind: kind[1].t === "str" ? kind[1].v : kind[1],
624
+ name: name[1].t === "str" ? name[1].v : name[1],
625
+ version: version[1].t === "str" ? version[1].v : version[1],
626
+ digest: digestEntry[1].t === "str" ? digestEntry[1].v : digestEntry[1],
627
+ });
628
+ }
629
+ return { ok: true, v: { identities: out } };
630
+ }
631
+ function buildContext(input) {
632
+ const ctxValue = member(input, "context");
633
+ if (ctxValue === null) {
634
+ return { ok: true, v: { keys: [], issuerKeySets: null, priorReceipts: [] } };
635
+ }
636
+ if (ctxValue.t !== "obj")
637
+ return { ok: false, e: "invalid_type" };
638
+ const ctx = ctxValue;
639
+ const keysRaw = member(ctx, "keys") ?? { t: "arr", v: [] };
640
+ const keys = buildKeys({ t: "obj", v: [["keys", keysRaw]] });
641
+ if (!keys.ok)
642
+ return keys;
643
+ const issuerKeySetsRaw = member(ctx, "issuer_key_sets");
644
+ let issuerKeySets = null;
645
+ if (issuerKeySetsRaw !== null && issuerKeySetsRaw.t === "obj") {
646
+ issuerKeySets = Object.create(null);
647
+ for (const [issuer, keyList] of issuerKeySetsRaw.v) {
648
+ if (keyList.t !== "arr")
649
+ return { ok: false, e: "invalid_type" };
650
+ const built = buildKeys({ t: "obj", v: [["keys", keyList]] });
651
+ if (!built.ok)
652
+ return built;
653
+ issuerKeySets[issuer] = built.v;
654
+ }
655
+ }
656
+ const receiptsRaw = member(ctx, "prior_receipts") ?? { t: "arr", v: [] };
657
+ if (receiptsRaw.t !== "arr")
658
+ return { ok: false, e: "invalid_type" };
659
+ const priorReceipts = [];
660
+ {
661
+ for (const receipt of receiptsRaw.v) {
662
+ if (receipt.t !== "obj")
663
+ return { ok: false, e: "invalid_type" };
664
+ const taskIdentity = memberString(receipt, "task_identity");
665
+ const terminalState = memberString(receipt, "terminal_state");
666
+ const tagged = memberString(receipt, "commitment");
667
+ if (taskIdentity === null || terminalState === null || tagged === null) {
668
+ return { ok: false, e: "invalid_type" };
669
+ }
670
+ const parsed = digest.fromTagged(tagged);
671
+ if (!parsed.ok)
672
+ return { ok: false, e: "invalid_type" };
673
+ priorReceipts.push({ taskIdentity, terminalState, terminalCommitment: parsed.v });
674
+ }
675
+ }
676
+ return {
677
+ ok: true,
678
+ v: {
679
+ keys: keys.v,
680
+ // Pins ride as RAW tagged values: the twin compares members[m] against
681
+ // {:string, pin} exactly, so a present non-string pin DENIES (the
682
+ // audience constraint is enforced, never silently dropped).
683
+ issuer: member(ctx, "issuer"),
684
+ subject: member(ctx, "subject"),
685
+ audience: member(ctx, "audience"),
686
+ issuerKeySets,
687
+ createdAfter: memberString(ctx, "created_after") ?? undefined,
688
+ createdBefore: memberString(ctx, "created_before") ?? undefined,
689
+ priorReceipts,
690
+ },
691
+ };
692
+ }
693
+ function projectDeployment(deploymentValue, input) {
694
+ const binding = member(input, "binding");
695
+ if (binding !== null && binding.t === "obj") {
696
+ const blueprintBytes = memberString(binding, "blueprint");
697
+ if (blueprintBytes === null)
698
+ return { ok: false, e: "invalid_type" };
699
+ const decoded = blueprint.decode(Buffer.from(blueprintBytes, "utf8"));
700
+ if (!decoded.ok)
701
+ return decoded;
702
+ const now = memberString(binding, "now");
703
+ const observations = {
704
+ now: now !== null ? now : null,
705
+ maxAttestationAgeMs: memberNumber(binding, "max_attestation_age_ms"),
706
+ observed: {},
707
+ };
708
+ const verified = deployment.verifyBinding(deploymentValue, decoded.v, observations);
709
+ if (!verified.ok)
710
+ return verified;
711
+ }
712
+ return { ok: true, v: { digest: deployment.contentDigest(deploymentValue) } };
713
+ }
714
+ function projectNegotiation(outcome) {
715
+ if (!outcome.ok)
716
+ return outcome;
717
+ return {
718
+ ok: true,
719
+ v: {
720
+ revision: outcome.v.protocolRevision,
721
+ critical: [...outcome.v.criticalExtensions].sort(),
722
+ quarantined: [...outcome.v.quarantinedExtensions].sort(),
723
+ notices: [...outcome.v.notices].sort(),
724
+ },
725
+ };
726
+ }
727
+ function dispatchFederationCodec(input) {
728
+ const fromA2aState = memberString(input, "from_a2a_state");
729
+ if (fromA2aState !== null) {
730
+ const result = federation.fromA2aState(fromA2aState);
731
+ return projectCodec(result);
732
+ }
733
+ const toA2aState = memberString(input, "to_a2a_state");
734
+ if (toA2aState !== null) {
735
+ return projectCodec(federation.toA2aState(toA2aState));
736
+ }
737
+ const fromMcpState = memberString(input, "from_mcp_state");
738
+ if (fromMcpState !== null) {
739
+ const result = federation.fromMcpState(fromMcpState);
740
+ return projectCodec(result);
741
+ }
742
+ const toMcpState = memberString(input, "to_mcp_state");
743
+ if (toMcpState !== null) {
744
+ return projectCodec(federation.toMcpState(toMcpState));
745
+ }
746
+ const fromA2aCarrier = member(input, "from_a2a_carrier");
747
+ if (fromA2aCarrier !== null && fromA2aCarrier.t === "obj") {
748
+ return projectCodec(federation.fromA2aCarrier(fromA2aCarrier));
749
+ }
750
+ const fromMcpCarrier = member(input, "from_mcp_carrier");
751
+ if (fromMcpCarrier !== null && fromMcpCarrier.t === "obj") {
752
+ return projectCodec(federation.fromMcpCarrier(fromMcpCarrier));
753
+ }
754
+ return { ok: false, e: "invalid_type" };
755
+ }
756
+ function projectCodec(result) {
757
+ if (!result.ok)
758
+ return result;
759
+ if (typeof result.v === "string")
760
+ return { ok: true, v: { value: result.v } };
761
+ return { ok: true, v: result.v };
762
+ }
763
+ function toPlainValue(value) {
764
+ switch (value.t) {
765
+ case "null":
766
+ return null;
767
+ case "bool":
768
+ case "int":
769
+ case "float":
770
+ case "str":
771
+ return value.v;
772
+ case "arr":
773
+ return value.v.map(toPlainValue);
774
+ case "obj":
775
+ return Object.fromEntries(value.v.map(([k, v]) => [k, toPlainValue(v)]));
776
+ }
777
+ }
778
+ export { str, int, float, nil };