@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,795 @@
1
+ // The Blueprint artifact — the mirror of AgentBlueprintProtocol.Blueprint:
2
+ // canonical verify → the 18-member registry table → the portability scan →
3
+ // the content-digest comparison, over the digest-covered members minus the
4
+ // evidence trio. Also hosts the Signature.attributes twin (the TS signature
5
+ // twin keeps its parts parser private; the artifact tables need the
6
+ // attributes projection for the purpose checks).
7
+ import * as b64 from "./b64url.js";
8
+ import { coerce as coerceBounds, maximum as maxBounds } from "./bounds.js";
9
+ import * as canonical from "./canonical.js";
10
+ import * as digest from "./digest.js";
11
+ import * as extension from "./extension.js";
12
+ import { field, keyfind, validate } from "./registry_engine.js";
13
+ import * as portability from "./portability.js";
14
+ import * as predicate from "./predicate.js";
15
+ import * as schema from "./schema.js";
16
+ const CEILING_PORTS = 64;
17
+ const CEILING_CAPABILITIES = 64;
18
+ const CEILING_EFFECTS = 64;
19
+ const CEILING_ASSERTIONS = 128;
20
+ const CEILING_SIGNATURES = 16;
21
+ const CEILING_ATTESTATIONS = 16;
22
+ const IDENTIFIER_BYTES = 512;
23
+ const CLASSIFICATION = ["public", "internal", "confidential", "restricted"];
24
+ const OPERATION_KINDS = ["read", "computation", "mutation"];
25
+ const IMPACT_CLASSES = ["ordinary", "money", "authority", "secret"];
26
+ const APPROVAL_TRAITS = ["none", "human_required", "separated_human_required"];
27
+ const AUTHORITY_TRAITS = ["none", "local_policy", "external_authority_required"];
28
+ const TRIGGER_KINDS = ["manual", "schedule", "condition", "evaluation", "delegated"];
29
+ const ASSERTION_KINDS = [
30
+ "output_schema",
31
+ "deterministic_predicate",
32
+ "required_capability_use",
33
+ "forbidden_capability_use",
34
+ "grounding_presence",
35
+ "policy_denial_expected",
36
+ "approval_expected",
37
+ "parameter_bound",
38
+ "provenance_tie_out",
39
+ "ceiling",
40
+ ];
41
+ const NUMERIC_CEILINGS = [
42
+ "max_attempts",
43
+ "max_concurrency",
44
+ "max_depth",
45
+ "max_descendants",
46
+ "max_elapsed_ms",
47
+ "max_fan_out",
48
+ "max_tokens",
49
+ ];
50
+ // The 15 digest-covered members; the evidence trio is excluded by §8.2.
51
+ const COVERED_MEMBERS = [
52
+ "blueprint_id",
53
+ "capability_requirements",
54
+ "ceilings",
55
+ "classification_ceiling",
56
+ "effect_intents",
57
+ "evaluation_assertions",
58
+ "extensions",
59
+ "input_ports",
60
+ "output_contract",
61
+ "output_ports",
62
+ "producer",
63
+ "protocol_revision",
64
+ "release_number",
65
+ "required_core_fields",
66
+ "triggers",
67
+ ];
68
+ const EVIDENCE_MEMBERS = ["content_digest", "signatures", "attestations"];
69
+ const SEGMENT = /^[a-z0-9][a-z0-9._-]*$/;
70
+ const Z_FORM = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
71
+ const CURRENCY = /^[A-Z]{3}$/;
72
+ const enumSet = (values) => new Set(values);
73
+ // ---- the 18-member table (base §6): data for the generic engine ---------------
74
+ function portElement() {
75
+ return {
76
+ kind: {
77
+ object: {
78
+ members: [
79
+ field("name", "string"),
80
+ field("schema", "custom", { check: checkSchemaDocument }),
81
+ field("classification_ceiling", { enum: enumSet(CLASSIFICATION) }),
82
+ field("required", "boolean"),
83
+ ],
84
+ },
85
+ },
86
+ };
87
+ }
88
+ function capabilityElement() {
89
+ return {
90
+ kind: {
91
+ object: {
92
+ members: [
93
+ field("operation_family", "string"),
94
+ field("argument_schema", "custom", { check: checkSchemaDocument }),
95
+ field("result_schema", "custom", { check: checkSchemaDocument }),
96
+ field("operation_kind", { enum: enumSet(OPERATION_KINDS) }),
97
+ field("impact_class", { enum: enumSet(IMPACT_CLASSES) }),
98
+ field("classification_ceiling", { enum: enumSet(CLASSIFICATION) }),
99
+ field("approval_trait", { enum: enumSet(APPROVAL_TRAITS) }),
100
+ field("authority_trait", { enum: enumSet(AUTHORITY_TRAITS) }),
101
+ ],
102
+ },
103
+ },
104
+ };
105
+ }
106
+ function effectElement() {
107
+ return {
108
+ kind: {
109
+ object: {
110
+ members: [
111
+ field("logical_operation", "string"),
112
+ field("operation_kind", { enum: enumSet(OPERATION_KINDS) }),
113
+ field("impact_class", { enum: enumSet(IMPACT_CLASSES) }),
114
+ ],
115
+ },
116
+ },
117
+ };
118
+ }
119
+ function ceilingsMembers() {
120
+ const numeric = NUMERIC_CEILINGS.map((name) => field(name, "integer", { check: checkPositive }));
121
+ const cost = field("max_cost", {
122
+ object: {
123
+ members: [
124
+ field("amount", "integer", { check: checkPositive }),
125
+ field("currency", "string", { check: checkCurrency }),
126
+ ],
127
+ },
128
+ });
129
+ return [...numeric, cost];
130
+ }
131
+ function producerMembers() {
132
+ return [
133
+ field("identity", "string", { check: checkProducerName }),
134
+ field("created_at", "string", { check: checkZForm }),
135
+ field("toolchain", "string"),
136
+ ];
137
+ }
138
+ function outputContractMembers() {
139
+ return [
140
+ field("port", "string"),
141
+ field("classification_ceiling", { enum: enumSet(CLASSIFICATION) }),
142
+ ];
143
+ }
144
+ // The assertion element carries the closed per-kind operand check; the
145
+ // predicate and the cross-field references validate in the root hook.
146
+ function assertionElement() {
147
+ return { kind: "custom", check: checkAssertion };
148
+ }
149
+ export function table() {
150
+ return [
151
+ field("blueprint_id", "string", { check: checkProducerQualified }),
152
+ field("capability_requirements", { array: capabilityElement() }, {
153
+ maxItems: CEILING_CAPABILITIES,
154
+ uniqueBy: "operation_family",
155
+ }),
156
+ field("ceilings", { object: { members: ceilingsMembers() } }),
157
+ field("classification_ceiling", { enum: enumSet(CLASSIFICATION) }),
158
+ field("effect_intents", { array: effectElement() }, {
159
+ maxItems: CEILING_EFFECTS,
160
+ uniqueBy: "logical_operation",
161
+ }),
162
+ field("evaluation_assertions", { array: assertionElement() }, {
163
+ maxItems: CEILING_ASSERTIONS,
164
+ rootHook: hookAssertions,
165
+ }),
166
+ field("extensions", "custom", { check: (v) => extension.envelopeOk(v) }),
167
+ field("input_ports", { array: portElement() }, { maxItems: CEILING_PORTS, uniqueBy: "name" }),
168
+ field("output_contract", { object: { members: outputContractMembers() } }, {
169
+ rootHook: hookOutputContract,
170
+ }),
171
+ field("output_ports", { array: portElement() }, { maxItems: CEILING_PORTS, uniqueBy: "name" }),
172
+ field("producer", { object: { members: producerMembers() } }),
173
+ field("protocol_revision", "integer", { check: checkPositive }),
174
+ field("release_number", "integer", { check: checkPositive }),
175
+ field("required_core_fields", { array: { kind: "string" } }, {
176
+ uniqueBy: ":value",
177
+ check: checkRequiredCoreFields,
178
+ }),
179
+ field("triggers", { array: { kind: { enum: enumSet(TRIGGER_KINDS) } } }, {
180
+ minItems: 1,
181
+ uniqueBy: ":value",
182
+ }),
183
+ field("signatures", { array: { kind: "custom", check: checkSignatureEntry } }, {
184
+ required: false,
185
+ maxItems: CEILING_SIGNATURES,
186
+ }),
187
+ field("attestations", { array: { kind: "any" } }, {
188
+ required: false,
189
+ maxItems: CEILING_ATTESTATIONS,
190
+ check: checkAttestations,
191
+ }),
192
+ field("content_digest", "string", { check: checkTaggedDigest }),
193
+ ];
194
+ }
195
+ // ---- decode ---------------------------------------------------------------------
196
+ export function decode(bytes, bounds) {
197
+ if (typeof bytes !== "string" && !Buffer.isBuffer(bytes))
198
+ return { ok: false, e: "invalid_type" };
199
+ const profile = normalizeBounds(bounds);
200
+ if (!profile.ok)
201
+ return profile;
202
+ const verified = canonical.verify(bytes, profile.v);
203
+ if (!verified.ok)
204
+ return verified;
205
+ const value = verified.v;
206
+ const validated = validate(table(), value);
207
+ if (!validated.ok)
208
+ return validated;
209
+ const scanned = scan(value, new Set());
210
+ if (!scanned.ok)
211
+ return scanned;
212
+ const digestOk = verifyContentDigest({ value });
213
+ if (!digestOk.ok)
214
+ return digestOk;
215
+ return { ok: true, v: { value } };
216
+ }
217
+ export function fromValue(value, opts = {}) {
218
+ const validated = opts.authoredExtensions ?? [];
219
+ if (validated.length > 0) {
220
+ if (value.t !== "obj")
221
+ return { ok: false, e: "invalid_type" };
222
+ const critical = extension.criticalNamespaces(value.v);
223
+ if (!validated.every((ns) => critical.has(ns)))
224
+ return { ok: false, e: "invalid_type" };
225
+ }
226
+ const tabled = validate(table(), value);
227
+ if (!tabled.ok)
228
+ return tabled;
229
+ const scanned = scan(value, new Set(validated));
230
+ if (!scanned.ok)
231
+ return scanned;
232
+ return { ok: true, v: { value } };
233
+ }
234
+ export function toValue(blueprint) {
235
+ return blueprint.value;
236
+ }
237
+ // ---- digest surface ----------------------------------------------------------------
238
+ export function digestInput(blueprint) {
239
+ if (blueprint.value.t !== "obj")
240
+ return blueprint.value;
241
+ return { t: "obj", v: blueprint.value.v.filter(([name]) => !EVIDENCE_MEMBERS.includes(name)) };
242
+ }
243
+ export function canonicalBytes(blueprint) {
244
+ return canonical.encode(blueprint.value);
245
+ }
246
+ // The honest content digest over the covered members' canonical bytes.
247
+ export function contentDigest(blueprint) {
248
+ const jcs = canonical.encode(digestInput(blueprint));
249
+ if (!jcs.ok)
250
+ throw new Error("blueprint digest input must encode");
251
+ return digest.toTagged(digest.hash("blueprint_content", Buffer.from(jcs.v, "utf8")));
252
+ }
253
+ export function verifyContentDigest(blueprint) {
254
+ const members = blueprint.value.t === "obj" ? blueprint.value.v : [];
255
+ const declared = keyfind(members, "content_digest");
256
+ if (declared === null || declared.t !== "str")
257
+ return { ok: false, e: "invalid_type" };
258
+ const jcs = canonical.encode(digestInput(blueprint));
259
+ if (!jcs.ok)
260
+ return jcs;
261
+ return digest.verifyContent("blueprint_content", Buffer.from(jcs.v, "utf8"), declared.v);
262
+ }
263
+ // ---- bounds normalization (the twin's Bounds.coerce over override maps) -------------
264
+ function normalizeBounds(bounds) {
265
+ if (bounds === undefined)
266
+ return { ok: true, v: maxBounds() };
267
+ return coerceBounds(bounds);
268
+ }
269
+ // ---- field checks (carried in the table as data) ------------------------------------
270
+ function checkPositive(value) {
271
+ if (value.t === "int" && value.v >= 1)
272
+ return { ok: true };
273
+ return { ok: false, e: "invalid_constraint" };
274
+ }
275
+ function checkProducerQualified(value) {
276
+ if (value.t !== "str")
277
+ return { ok: false, e: "invalid_constraint" };
278
+ const segments = value.v.split("/");
279
+ return (Buffer.byteLength(value.v, "utf8") <= IDENTIFIER_BYTES &&
280
+ segments.length === 2 &&
281
+ segments.every((s) => s !== "" && SEGMENT.test(s)))
282
+ ? { ok: true }
283
+ : { ok: false, e: "invalid_constraint" };
284
+ }
285
+ function checkProducerName(value) {
286
+ if (value.t !== "str")
287
+ return { ok: false, e: "invalid_constraint" };
288
+ return Buffer.byteLength(value.v, "utf8") <= IDENTIFIER_BYTES && SEGMENT.test(value.v)
289
+ ? { ok: true }
290
+ : { ok: false, e: "invalid_constraint" };
291
+ }
292
+ function checkZForm(value) {
293
+ if (value.t !== "str")
294
+ return { ok: false, e: "invalid_constraint" };
295
+ return Z_FORM.test(value.v) && validZFormDate(value.v)
296
+ ? { ok: true }
297
+ : { ok: false, e: "invalid_constraint" };
298
+ }
299
+ function checkCurrency(value) {
300
+ if (value.t !== "str")
301
+ return { ok: false, e: "invalid_constraint" };
302
+ return CURRENCY.test(value.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
303
+ }
304
+ function checkTaggedDigest(value) {
305
+ if (value.t !== "str")
306
+ return { ok: false, e: "invalid_constraint" };
307
+ return digest.fromTagged(value.v);
308
+ }
309
+ function checkSchemaDocument(value) {
310
+ const parsed = schema.parse(value, schema.dialect());
311
+ if (parsed.ok)
312
+ return { ok: true };
313
+ return { ok: false, e: parsed.e };
314
+ }
315
+ // The purpose is the artifact's fact: the envelope parses AND must be
316
+ // "blueprint" here — a deployment-purpose signature riding a Blueprint is a
317
+ // wrong-position entry, denied as a table constraint.
318
+ function checkSignatureEntry(entry) {
319
+ const attrs = signatureAttributes(entry);
320
+ if (!attrs.ok)
321
+ return { ok: false, e: attrs.e };
322
+ return attrs.v.purpose === "blueprint" ? { ok: true } : { ok: false, e: "invalid_constraint" };
323
+ }
324
+ // The attestation kind registry is empty BY DESIGN; a non-empty array denies.
325
+ function checkAttestations(value) {
326
+ if (value.t === "arr" && value.v.length === 0)
327
+ return { ok: true };
328
+ return { ok: false, e: "attestation_malformed" };
329
+ }
330
+ function checkRequiredCoreFields(value) {
331
+ if (value.t !== "arr")
332
+ return { ok: false, e: "invalid_constraint" };
333
+ const ok = value.v.every((item) => item.t === "str" && COVERED_MEMBERS.includes(item.v));
334
+ return ok ? { ok: true } : { ok: false, e: "invalid_constraint" };
335
+ }
336
+ const OPERAND_SPECS = {
337
+ output_schema: { port: ["string", true], schema: ["schema_document", true] },
338
+ deterministic_predicate: { predicate: ["any", true] },
339
+ required_capability_use: { operation_family: ["string", true] },
340
+ forbidden_capability_use: { operation_family: ["string", true] },
341
+ grounding_presence: { dataset: ["nonempty_string", true] },
342
+ policy_denial_expected: { operation_family: ["string", true] },
343
+ approval_expected: {
344
+ operation_family: ["string", true],
345
+ approval_trait: [{ enum: enumSet(APPROVAL_TRAITS) }, true],
346
+ },
347
+ parameter_bound: {
348
+ parameter: ["nonempty_string", true],
349
+ minimum: ["optional_number", false],
350
+ maximum: ["optional_number", false],
351
+ },
352
+ provenance_tie_out: { member: ["string", true] },
353
+ ceiling: {
354
+ ceiling: [{ enum: enumSet(NUMERIC_CEILINGS) }, true],
355
+ at_most: ["positive_number", true],
356
+ },
357
+ };
358
+ function checkAssertion(value) {
359
+ if (value.t !== "obj")
360
+ return { ok: false, e: "invalid_type" };
361
+ const members = value.v;
362
+ const kindFound = keyfind(members, "kind");
363
+ if (kindFound === null)
364
+ return { ok: false, e: "missing_required_field" };
365
+ if (kindFound.t !== "str")
366
+ return { ok: false, e: "invalid_type" };
367
+ if (!ASSERTION_KINDS.includes(kindFound.v))
368
+ return { ok: false, e: "invalid_constraint" };
369
+ const kind = kindFound.v;
370
+ const operands = OPERAND_SPECS[kind];
371
+ const allowed = new Set(["kind", ...Object.keys(operands)]);
372
+ for (const [name] of members) {
373
+ if (!allowed.has(name))
374
+ return { ok: false, e: "unknown_member" };
375
+ }
376
+ for (const [name, [, required]] of Object.entries(operands)) {
377
+ if (required && keyfind(members, name) === null)
378
+ return { ok: false, e: "missing_required_field" };
379
+ }
380
+ // Operand judgments run in the operands map's (alphabetical) key order.
381
+ for (const name of Object.keys(operands).sort()) {
382
+ const found = keyfind(members, name);
383
+ if (found === null)
384
+ continue;
385
+ const judged = operandResult(operands[name][0], found);
386
+ if (!judged.ok)
387
+ return judged;
388
+ }
389
+ // parameter_bound: at least one of minimum/maximum must be present.
390
+ if (kind === "parameter_bound") {
391
+ const has = keyfind(members, "minimum") !== null || keyfind(members, "maximum") !== null;
392
+ return has ? { ok: true } : { ok: false, e: "invalid_constraint" };
393
+ }
394
+ return { ok: true };
395
+ }
396
+ function operandResult(operandKind, value) {
397
+ switch (operandKind) {
398
+ case "string":
399
+ return value.t === "str" ? { ok: true } : { ok: false, e: "invalid_type" };
400
+ case "nonempty_string":
401
+ return value.t === "str" && value.v !== "" ? { ok: true } : { ok: false, e: "invalid_type" };
402
+ case "any":
403
+ return wellFormed(value) ? { ok: true } : { ok: false, e: "invalid_type" };
404
+ case "schema_document":
405
+ return checkSchemaDocument(value);
406
+ case "optional_number":
407
+ return value.t === "int" || value.t === "float" ? { ok: true } : { ok: false, e: "invalid_type" };
408
+ case "positive_number":
409
+ if (value.t === "int" && value.v >= 1)
410
+ return { ok: true };
411
+ if (value.t === "float" && value.v >= 1)
412
+ return { ok: true };
413
+ return { ok: false, e: "invalid_type" };
414
+ default:
415
+ if (value.t !== "str")
416
+ return { ok: false, e: "invalid_type" };
417
+ return operandKind.enum.has(value.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
418
+ }
419
+ }
420
+ function wellFormed(value) {
421
+ switch (value.t) {
422
+ case "null":
423
+ case "bool":
424
+ case "int":
425
+ case "float":
426
+ case "str":
427
+ return true;
428
+ case "arr":
429
+ return value.v.every(wellFormed);
430
+ case "obj":
431
+ return value.v.every(([name, member]) => typeof name === "string" && wellFormed(member));
432
+ }
433
+ }
434
+ // ---- root hooks -----------------------------------------------------------------------
435
+ function hookAssertions(members) {
436
+ const inputNames = portNames(members, "input_ports");
437
+ const outputNames = portNames(members, "output_ports");
438
+ const allPortNames = new Set([...inputNames, ...outputNames]);
439
+ const families = new Set(capabilityFamilies(members));
440
+ // One flat port namespace: an input/output name collision makes every
441
+ // predicate root ambiguous.
442
+ if (inputNames.some((name) => outputNames.includes(name))) {
443
+ return { ok: false, e: "invalid_cardinality" };
444
+ }
445
+ const assertions = memberArray(members, "evaluation_assertions");
446
+ for (const assertion of assertions) {
447
+ if (assertion.t !== "obj")
448
+ continue;
449
+ const result = assertionHookCheck(assertion, allPortNames, families);
450
+ if (!result.ok)
451
+ return result;
452
+ }
453
+ return { ok: true };
454
+ }
455
+ function assertionHookCheck(assertion, portNames, families) {
456
+ const kind = keyfind(assertion.v, "kind");
457
+ if (kind === null || kind.t !== "str")
458
+ return { ok: true };
459
+ switch (kind.v) {
460
+ case "deterministic_predicate": {
461
+ const predicateValue = keyfind(assertion.v, "predicate");
462
+ if (predicateValue === null)
463
+ return { ok: true };
464
+ return predicate.validate(predicateValue, [...portNames]);
465
+ }
466
+ case "output_schema": {
467
+ const port = keyfind(assertion.v, "port");
468
+ if (port === null || port.t !== "str")
469
+ return { ok: true };
470
+ return portNames.has(port.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
471
+ }
472
+ case "required_capability_use":
473
+ case "approval_expected": {
474
+ const family = keyfind(assertion.v, "operation_family");
475
+ if (family === null || family.t !== "str")
476
+ return { ok: true };
477
+ return families.has(family.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
478
+ }
479
+ case "provenance_tie_out": {
480
+ const member = keyfind(assertion.v, "member");
481
+ if (member === null || member.t !== "str")
482
+ return { ok: true };
483
+ return COVERED_MEMBERS.includes(member.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
484
+ }
485
+ default:
486
+ return { ok: true };
487
+ }
488
+ }
489
+ function hookOutputContract(members) {
490
+ const outputNames = new Set(portNames(members, "output_ports"));
491
+ const contract = members.get("output_contract");
492
+ if (contract === undefined || contract.t !== "obj")
493
+ return { ok: true };
494
+ const port = keyfind(contract.v, "port");
495
+ if (port === null || port.t !== "str")
496
+ return { ok: true };
497
+ return outputNames.has(port.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
498
+ }
499
+ function portNames(members, memberName) {
500
+ const out = [];
501
+ for (const entry of memberArray(members, memberName)) {
502
+ if (entry.t !== "obj")
503
+ continue;
504
+ const name = keyfind(entry.v, "name");
505
+ if (name !== null && name.t === "str")
506
+ out.push(name.v);
507
+ }
508
+ return out;
509
+ }
510
+ function capabilityFamilies(members) {
511
+ const out = [];
512
+ for (const entry of memberArray(members, "capability_requirements")) {
513
+ if (entry.t !== "obj")
514
+ continue;
515
+ const family = keyfind(entry.v, "operation_family");
516
+ if (family !== null && family.t === "str")
517
+ out.push(family.v);
518
+ }
519
+ return out;
520
+ }
521
+ function memberArray(members, name) {
522
+ const found = members.get(name);
523
+ return found !== undefined && found.t === "arr" ? found.v : [];
524
+ }
525
+ // ---- the portability scan -----------------------------------------------------------
526
+ function scan(value, authoredNs) {
527
+ if (value.t !== "obj")
528
+ return { ok: false, e: "invalid_type" };
529
+ const members = value.v;
530
+ const openRegions = scanOpenRegions(members, authoredNs);
531
+ if (!openRegions.ok)
532
+ return openRegions;
533
+ const coreStrings = scanCoreStrings(members);
534
+ if (!coreStrings.ok)
535
+ return coreStrings;
536
+ return scanEvidenceKeyIds(members);
537
+ }
538
+ // Extension bodies: names + strict values — EXCEPT negotiation-validated
539
+ // critical namespaces. Schema documents and predicate operands: names +
540
+ // exempting values.
541
+ function scanOpenRegions(members, validatedNs) {
542
+ const strictBodies = extension.bodies(members).filter(([ns]) => !validatedNs.has(ns));
543
+ const scans = [
544
+ ...strictBodies.map(([, body]) => portability.scan(body)),
545
+ ...[...schemaDocuments(members), ...predicateOperands(members)].map((doc) => portability.scanAuthored(doc)),
546
+ ];
547
+ for (const result of scans) {
548
+ if (!result.ok)
549
+ return result;
550
+ }
551
+ return { ok: true };
552
+ }
553
+ // The arbitrary-JSON operands a deterministic_predicate carries — open
554
+ // content in a digest-covered core position.
555
+ function predicateOperands(members) {
556
+ const out = [];
557
+ const assertions = memberListOf(members, "evaluation_assertions");
558
+ for (const assertion of assertions) {
559
+ if (assertion.t !== "obj")
560
+ continue;
561
+ const kind = keyfind(assertion.v, "kind");
562
+ if (kind === null || kind.t !== "str" || kind.v !== "deterministic_predicate")
563
+ continue;
564
+ const predicateValue = keyfind(assertion.v, "predicate");
565
+ if (predicateValue === null || predicateValue.t !== "obj")
566
+ continue;
567
+ for (const operandName of ["value", "values"]) {
568
+ const operand = keyfind(predicateValue.v, operandName);
569
+ if (operand !== null)
570
+ out.push(operand);
571
+ }
572
+ }
573
+ return out;
574
+ }
575
+ function schemaDocuments(members) {
576
+ return [
577
+ ...portSchemas(members, "input_ports"),
578
+ ...portSchemas(members, "output_ports"),
579
+ ...capabilitySchemas(members),
580
+ ...assertionSchemas(members),
581
+ ];
582
+ }
583
+ function portSchemas(members, memberName) {
584
+ const out = [];
585
+ for (const entry of memberListOf(members, memberName)) {
586
+ if (entry.t !== "obj")
587
+ continue;
588
+ const found = keyfind(entry.v, "schema");
589
+ if (found !== null)
590
+ out.push(found);
591
+ }
592
+ return out;
593
+ }
594
+ function capabilitySchemas(members) {
595
+ const out = [];
596
+ for (const entry of memberListOf(members, "capability_requirements")) {
597
+ if (entry.t !== "obj")
598
+ continue;
599
+ for (const name of ["argument_schema", "result_schema"]) {
600
+ const found = keyfind(entry.v, name);
601
+ if (found !== null)
602
+ out.push(found);
603
+ }
604
+ }
605
+ return out;
606
+ }
607
+ function assertionSchemas(members) {
608
+ const out = [];
609
+ for (const assertion of memberListOf(members, "evaluation_assertions")) {
610
+ if (assertion.t !== "obj")
611
+ continue;
612
+ const kind = keyfind(assertion.v, "kind");
613
+ if (kind === null || kind.t !== "str" || kind.v !== "output_schema")
614
+ continue;
615
+ const found = keyfind(assertion.v, "schema");
616
+ if (found !== null)
617
+ out.push(found);
618
+ }
619
+ return out;
620
+ }
621
+ // Value shapes over every other core string. Identifier-convention positions
622
+ // keep the exemption; everything else is strict.
623
+ function scanCoreStrings(members) {
624
+ for (const [name, value] of members) {
625
+ if (EVIDENCE_MEMBERS.includes(name) || name === "extensions")
626
+ continue;
627
+ const result = coreMemberScan(name, value);
628
+ if (!result.ok)
629
+ return result;
630
+ }
631
+ return { ok: true };
632
+ }
633
+ function coreMemberScan(name, value) {
634
+ switch (name) {
635
+ case "input_ports":
636
+ case "output_ports":
637
+ return identifierPositions(value, ["name"]);
638
+ case "capability_requirements":
639
+ return identifierPositions(value, ["operation_family"]);
640
+ case "effect_intents":
641
+ return identifierPositions(value, ["logical_operation"]);
642
+ case "evaluation_assertions":
643
+ return identifierPositions(value, ["dataset", "member", "operation_family", "parameter"]);
644
+ default:
645
+ return portability.scanValue(value);
646
+ }
647
+ }
648
+ // Element objects: ONLY the named identifier members are scanned here
649
+ // (exempting mode).
650
+ function identifierPositions(value, names) {
651
+ if (value.t !== "arr")
652
+ return { ok: true };
653
+ for (const element of value.v) {
654
+ if (element.t !== "obj")
655
+ continue;
656
+ for (const [name, memberValue] of element.v) {
657
+ if (!names.includes(name))
658
+ continue;
659
+ const result = portability.scanIdentifier(memberValue);
660
+ if (!result.ok)
661
+ return result;
662
+ }
663
+ }
664
+ return { ok: true };
665
+ }
666
+ // The one free string inside an evidence entry: a secret-shaped key_id there
667
+ // must still red.
668
+ function scanEvidenceKeyIds(members) {
669
+ for (const entry of memberListOf(members, "signatures")) {
670
+ const attrs = signatureAttributes(entry);
671
+ if (!attrs.ok)
672
+ continue; // unparseable entries already denied at the table
673
+ const scanned = portability.scanValue({ t: "str", v: attrs.v.keyId });
674
+ if (!scanned.ok)
675
+ return scanned;
676
+ }
677
+ return { ok: true };
678
+ }
679
+ function memberListOf(members, name) {
680
+ const found = keyfind(members, name);
681
+ return found !== null && found.t === "arr" ? found.v : [];
682
+ }
683
+ const PURPOSES = new Set(["blueprint", "deployment", "federation-envelope"]);
684
+ const ENTRY_MEMBERS = ["protected", "signature", "signed_attributes"];
685
+ const HEADER_MEMBERS = ["alg", "b64", "crit", "kid"];
686
+ const ATTR_MEMBERS = ["algorithm", "content_digest", "created_at", "key_id", "purpose"];
687
+ function malformed() {
688
+ return { ok: false, e: "signature_malformed" };
689
+ }
690
+ export function signatureAttributes(entry) {
691
+ if (entry.t !== "obj")
692
+ return malformed();
693
+ const entryPairs = entry.v;
694
+ const entryNames = entryPairs.map(([name]) => name).sort();
695
+ if (entryNames.length !== 3 ||
696
+ entryNames.join("\u0000") !== ENTRY_MEMBERS.join("\u0000")) {
697
+ return malformed();
698
+ }
699
+ const header = keyfind(entryPairs, "protected");
700
+ const attrs = keyfind(entryPairs, "signed_attributes");
701
+ const signatureBody = keyfind(entryPairs, "signature");
702
+ // Header: exactly {alg, b64, crit, kid}; the one algorithm-unsupported
703
+ // denial fires only when alg is a string naming another algorithm.
704
+ if (header.t !== "obj")
705
+ return malformed();
706
+ const headerNames = header.v.map(([name]) => name).sort();
707
+ if (headerNames.length !== 4 || headerNames.join("\u0000") !== HEADER_MEMBERS.join("\u0000")) {
708
+ return malformed();
709
+ }
710
+ const alg = keyfind(header.v, "alg");
711
+ if (alg.t === "str" && alg.v !== "EdDSA") {
712
+ return { ok: false, e: "signature_algorithm_unsupported" };
713
+ }
714
+ if (alg.t !== "str")
715
+ return malformed();
716
+ const b64Flag = keyfind(header.v, "b64");
717
+ if (b64Flag.t !== "bool" || b64Flag.v !== false)
718
+ return malformed();
719
+ const crit = keyfind(header.v, "crit");
720
+ if (crit.t !== "arr" ||
721
+ crit.v.length !== 1 ||
722
+ crit.v[0].t !== "str" ||
723
+ crit.v[0].v !== "b64") {
724
+ return malformed();
725
+ }
726
+ const kid = keyfind(header.v, "kid");
727
+ if (kid.t !== "str" || kid.v === "")
728
+ return malformed();
729
+ // Attributes: exactly the five closed members.
730
+ if (attrs.t !== "obj")
731
+ return malformed();
732
+ const attrNames = attrs.v.map(([name]) => name).sort();
733
+ if (attrNames.length !== 5 || attrNames.join("\u0000") !== ATTR_MEMBERS.join("\u0000")) {
734
+ return malformed();
735
+ }
736
+ const algorithm = keyfind(attrs.v, "algorithm");
737
+ if (algorithm.t === "str" && algorithm.v !== "Ed25519") {
738
+ return { ok: false, e: "signature_algorithm_unsupported" };
739
+ }
740
+ if (algorithm.t !== "str")
741
+ return malformed();
742
+ const contentDigest = keyfind(attrs.v, "content_digest");
743
+ if (contentDigest.t !== "str" || !digest.fromTagged(contentDigest.v).ok)
744
+ return malformed();
745
+ const createdAt = keyfind(attrs.v, "created_at");
746
+ if (createdAt.t !== "str" || !Z_FORM.test(createdAt.v) || !validZFormDate(createdAt.v)) {
747
+ return malformed();
748
+ }
749
+ const keyId = keyfind(attrs.v, "key_id");
750
+ if (keyId.t !== "str" || keyId.v === "" || keyId.v.includes("."))
751
+ return malformed();
752
+ const purpose = keyfind(attrs.v, "purpose");
753
+ if (purpose.t !== "str" || !PURPOSES.has(purpose.v))
754
+ return malformed();
755
+ if (kid.v !== keyId.v)
756
+ return malformed();
757
+ if (signatureBody.t !== "str")
758
+ return malformed();
759
+ const decodedSignature = b64.decodeStrict(signatureBody.v);
760
+ if (!decodedSignature.ok || decodedSignature.v.length !== 64)
761
+ return malformed();
762
+ // canonical_input: both halves must re-encode (invalid UTF-8 in a
763
+ // hand-built entry denies malformed).
764
+ const headerJson = canonical.encode(header);
765
+ const attrsJson = canonical.encode(attrs);
766
+ if (!headerJson.ok || !attrsJson.ok)
767
+ return malformed();
768
+ const parsedDigest = digest.fromTagged(contentDigest.v);
769
+ if (!parsedDigest.ok)
770
+ return malformed();
771
+ return {
772
+ ok: true,
773
+ v: {
774
+ contentDigest: parsedDigest.v,
775
+ createdAt: createdAt.v,
776
+ keyId: keyId.v,
777
+ purpose: purpose.v,
778
+ },
779
+ };
780
+ }
781
+ // DateTime.from_iso8601's field validation for the Z whole-second form.
782
+ export function validZFormDate(stamped) {
783
+ const year = Number(stamped.slice(0, 4));
784
+ const month = Number(stamped.slice(5, 7));
785
+ const day = Number(stamped.slice(8, 10));
786
+ const hour = Number(stamped.slice(11, 13));
787
+ const minute = Number(stamped.slice(14, 16));
788
+ const second = Number(stamped.slice(17, 19));
789
+ if (Number.isNaN(year) || month < 1 || month > 12 || day < 1 || hour > 23 || minute > 59 || second > 59) {
790
+ return false;
791
+ }
792
+ const leap = year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
793
+ const lengths = [31, leap ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
794
+ return day <= lengths[month - 1];
795
+ }