@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,830 @@
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.
7
+ import { coerce as coerceBounds, maximum as maxBounds } from "./bounds.js";
8
+ import * as blueprint from "./blueprint.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
+ const CEILING_TOOL_BINDINGS = 128;
15
+ const CEILING_DATA_BINDINGS = 64;
16
+ const CEILING_BUILD_IDENTITIES = 128;
17
+ const CEILING_SIGNATURES = 16;
18
+ const CEILING_ATTESTATIONS = 16;
19
+ const IDENTIFIER_BYTES = 512;
20
+ const CLASSIFICATION = ["public", "internal", "confidential", "restricted"];
21
+ const AUTHORITY_TRAITS = ["none", "local_policy", "external_authority_required"];
22
+ const APPROVAL_TRAITS = ["none", "human_required", "separated_human_required"];
23
+ const IMPACT_CLASSES = ["ordinary", "money", "authority", "secret"];
24
+ const DISCLOSURE_STEPS = ["none", "summary", "detail", "full"];
25
+ const CUSTODY_MODES = ["host_managed", "external_kms", "holder_edge"];
26
+ const BUILD_KINDS = ["package", "build", "adapter", "extension"];
27
+ const LIFECYCLE_STATES = ["draft", "active", "retired"];
28
+ const AS_OF_MODES = ["required", "none"];
29
+ const NUMERIC_CEILINGS = [
30
+ "max_attempts",
31
+ "max_concurrency",
32
+ "max_depth",
33
+ "max_descendants",
34
+ "max_elapsed_ms",
35
+ "max_fan_out",
36
+ "max_tokens",
37
+ ];
38
+ // The 16 digest-covered members; the evidence trio is excluded by §8.2.
39
+ const COVERED_MEMBERS = [
40
+ "authority_requirement",
41
+ "blueprint_release",
42
+ "build_identities",
43
+ "data_bindings",
44
+ "effect_owner",
45
+ "eligibility",
46
+ "evaluation_binding",
47
+ "extensions",
48
+ "host_bounds",
49
+ "lifecycle",
50
+ "model_policy",
51
+ "protocol_revision",
52
+ "required_core_fields",
53
+ "scope_projection",
54
+ "signer_custody",
55
+ "tool_bindings",
56
+ ];
57
+ const EVIDENCE_MEMBERS = ["deployment_digest", "signatures", "attestations"];
58
+ const SEGMENT = /^[a-z0-9][a-z0-9._-]*$/;
59
+ const Z_FORM = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/;
60
+ const CURRENCY = /^[A-Z]{3}$/;
61
+ const EXACT_CHARSET = /^[A-Za-z0-9.+-]+$/;
62
+ const enumSet = (values) => new Set(values);
63
+ // ---- the 19-member table ------------------------------------------------------------
64
+ function identityString(name) {
65
+ return field(name, "string", { check: checkIdentityString });
66
+ }
67
+ function identityProfileMembers() {
68
+ return [identityString("adapter_identity"), identityString("profile_identity")];
69
+ }
70
+ function releaseMembers() {
71
+ return [
72
+ field("blueprint_id", "string", { check: checkProducerQualified }),
73
+ field("release_number", "integer", { check: checkPositive }),
74
+ field("content_digest", "string", { check: checkTaggedDigest }),
75
+ ];
76
+ }
77
+ function buildIdentityElement() {
78
+ return {
79
+ kind: {
80
+ object: {
81
+ members: [
82
+ field("kind", { enum: enumSet(BUILD_KINDS) }),
83
+ identityString("name"),
84
+ field("version", "string", { check: checkExactVersion }),
85
+ field("digest", "string", { check: checkTaggedDigest }),
86
+ ],
87
+ },
88
+ },
89
+ };
90
+ }
91
+ function dataBindingElement() {
92
+ return {
93
+ kind: {
94
+ object: {
95
+ members: [
96
+ field("logical_dataset", "string", { check: checkIdentityString }),
97
+ field("classification_ceiling", { enum: enumSet(CLASSIFICATION) }),
98
+ field("as_of", { object: { members: asOfMembers() } }, { check: checkAsOf }),
99
+ ],
100
+ },
101
+ },
102
+ };
103
+ }
104
+ function asOfMembers() {
105
+ return [field("mode", { enum: enumSet(AS_OF_MODES) }), field("max_age_ms", "custom")];
106
+ }
107
+ function effectOwnerMembers() {
108
+ return [
109
+ identityString("adapter_identity"),
110
+ field("idempotency", {
111
+ object: {
112
+ members: [
113
+ field("key_derivation", { enum: enumSet(["host"]) }),
114
+ field("recovery", { enum: enumSet(["authoritative", "none"]) }),
115
+ ],
116
+ },
117
+ }),
118
+ ];
119
+ }
120
+ function eligibilityMembers() {
121
+ return [
122
+ field("owner", "custom", { check: checkExpression }),
123
+ field("beneficiary", "custom", { check: checkExpression }),
124
+ field("runtime_principal", "custom", { check: checkExpression }),
125
+ ];
126
+ }
127
+ function evaluationMembers() {
128
+ return [
129
+ identityString("adapter_identity"),
130
+ field("corpus", {
131
+ object: {
132
+ members: [
133
+ field("name", "string", { check: checkIdentityString }),
134
+ field("digest", "string", { check: checkTaggedDigest }),
135
+ ],
136
+ },
137
+ }),
138
+ ];
139
+ }
140
+ // Name order, pinned for the mirror: the seven numerics, cost, and the five
141
+ // protected bounds, member-name-sorted (NOT spec-map shape order).
142
+ function hostBoundsMembers() {
143
+ const numeric = NUMERIC_CEILINGS.map((name) => field(name, "integer", { check: checkPositive }));
144
+ const cost = field("max_cost", {
145
+ object: {
146
+ members: [
147
+ field("amount", "integer", { check: checkPositive }),
148
+ field("currency", "string", { check: checkCurrency }),
149
+ ],
150
+ },
151
+ });
152
+ const protectedBounds = [
153
+ field("classification_ceiling", { enum: enumSet(CLASSIFICATION) }),
154
+ field("authority_trait", { enum: enumSet(AUTHORITY_TRAITS) }),
155
+ field("approval_trait", { enum: enumSet(APPROVAL_TRAITS) }),
156
+ field("effect_impact_ceiling", { enum: enumSet(IMPACT_CLASSES) }),
157
+ field("disclosure_ceiling", { enum: enumSet(DISCLOSURE_STEPS) }),
158
+ ];
159
+ return [...numeric, cost, ...protectedBounds].sort((a, b) => (a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
160
+ }
161
+ function lifecycleMembers() {
162
+ return [
163
+ field("state", { enum: enumSet(LIFECYCLE_STATES) }),
164
+ field("activated_at", "string", { required: false, check: checkZForm }),
165
+ field("retired_at", "string", { required: false, check: checkZForm }),
166
+ ];
167
+ }
168
+ function modelPolicyMembers() {
169
+ return [
170
+ field("allowed_model_roles", { array: { kind: "string" } }, {
171
+ uniqueBy: ":value",
172
+ check: checkRoleStrings,
173
+ }),
174
+ field("max_tokens", "integer", { check: checkPositive }),
175
+ field("max_cost", {
176
+ object: {
177
+ members: [
178
+ field("amount", "integer", { check: checkPositive }),
179
+ field("currency", "string", { check: checkCurrency }),
180
+ ],
181
+ },
182
+ }),
183
+ ];
184
+ }
185
+ function toolBindingElement() {
186
+ return {
187
+ kind: {
188
+ object: {
189
+ members: [
190
+ field("logical_operation", "string", { check: checkIdentityString }),
191
+ identityString("adapter_identity"),
192
+ field("descriptor_digest", "string", { check: checkTaggedDigest }),
193
+ field("schema_digest", "string", { check: checkTaggedDigest }),
194
+ field("attested_at", "string", { check: checkZForm }),
195
+ ],
196
+ },
197
+ },
198
+ };
199
+ }
200
+ export function table() {
201
+ return [
202
+ field("authority_requirement", { object: { members: identityProfileMembers() } }),
203
+ field("blueprint_release", { object: { members: releaseMembers() } }),
204
+ field("build_identities", { array: buildIdentityElement() }, {
205
+ minItems: 1,
206
+ maxItems: CEILING_BUILD_IDENTITIES,
207
+ uniqueBy: "name",
208
+ }),
209
+ field("data_bindings", { array: dataBindingElement() }, {
210
+ maxItems: CEILING_DATA_BINDINGS,
211
+ uniqueBy: "logical_dataset",
212
+ }),
213
+ field("effect_owner", { object: { members: effectOwnerMembers() } }),
214
+ field("eligibility", { object: { members: eligibilityMembers() } }),
215
+ field("evaluation_binding", { object: { members: evaluationMembers() } }),
216
+ field("extensions", "custom", { check: (v) => extension.envelopeOk(v) }),
217
+ field("host_bounds", { object: { members: hostBoundsMembers() } }),
218
+ field("lifecycle", { object: { members: lifecycleMembers() } }, { check: checkLifecycle }),
219
+ field("model_policy", { object: { members: modelPolicyMembers() } }),
220
+ field("protocol_revision", "integer", { check: checkPositive }),
221
+ field("required_core_fields", { array: { kind: "string" } }, {
222
+ uniqueBy: ":value",
223
+ check: checkRequiredCoreFields,
224
+ }),
225
+ field("scope_projection", {
226
+ object: { members: [field("adapter_identity", "string", { check: checkIdentityString })] },
227
+ }),
228
+ field("signer_custody", { enum: enumSet(CUSTODY_MODES) }),
229
+ field("tool_bindings", { array: toolBindingElement() }, {
230
+ maxItems: CEILING_TOOL_BINDINGS,
231
+ uniqueBy: "logical_operation",
232
+ }),
233
+ field("signatures", { array: { kind: "custom", check: checkSignatureEntry } }, {
234
+ required: false,
235
+ maxItems: CEILING_SIGNATURES,
236
+ }),
237
+ field("attestations", { array: { kind: "any" } }, {
238
+ required: false,
239
+ maxItems: CEILING_ATTESTATIONS,
240
+ check: checkAttestations,
241
+ }),
242
+ field("deployment_digest", "string", { check: checkTaggedDigest }),
243
+ ];
244
+ }
245
+ // ---- decode -------------------------------------------------------------------------
246
+ export function decode(bytes, bounds) {
247
+ if (typeof bytes !== "string" && !Buffer.isBuffer(bytes))
248
+ return { ok: false, e: "invalid_type" };
249
+ const profile = normalizeBounds(bounds);
250
+ if (!profile.ok)
251
+ return profile;
252
+ const verified = canonical.verify(bytes, profile.v);
253
+ if (!verified.ok)
254
+ return verified;
255
+ const value = verified.v;
256
+ const validated = validate(table(), value);
257
+ if (!validated.ok)
258
+ return validated;
259
+ const scanned = scan(value, new Set());
260
+ if (!scanned.ok)
261
+ return scanned;
262
+ const digestOk = verifyContentDigest({ value });
263
+ if (!digestOk.ok)
264
+ return digestOk;
265
+ return { ok: true, v: { value } };
266
+ }
267
+ export function fromValue(value, opts = {}) {
268
+ const validated = opts.authoredExtensions ?? [];
269
+ if (validated.length > 0) {
270
+ if (value.t !== "obj")
271
+ return { ok: false, e: "invalid_type" };
272
+ const critical = extension.criticalNamespaces(value.v);
273
+ if (!validated.every((ns) => critical.has(ns)))
274
+ return { ok: false, e: "invalid_type" };
275
+ }
276
+ const tabled = validate(table(), value);
277
+ if (!tabled.ok)
278
+ return tabled;
279
+ const scanned = scan(value, new Set(validated));
280
+ if (!scanned.ok)
281
+ return scanned;
282
+ return { ok: true, v: { value } };
283
+ }
284
+ export function toValue(deployment) {
285
+ return deployment.value;
286
+ }
287
+ // ---- digest surface --------------------------------------------------------------------
288
+ export function digestInput(deployment) {
289
+ if (deployment.value.t !== "obj")
290
+ return deployment.value;
291
+ return { t: "obj", v: deployment.value.v.filter(([name]) => !EVIDENCE_MEMBERS.includes(name)) };
292
+ }
293
+ export function canonicalBytes(deployment) {
294
+ return canonical.encode(deployment.value);
295
+ }
296
+ export function contentDigest(deployment) {
297
+ const jcs = canonical.encode(digestInput(deployment));
298
+ if (!jcs.ok)
299
+ throw new Error("deployment digest input must encode");
300
+ return digest.toTagged(digest.hash("deployment_content", Buffer.from(jcs.v, "utf8")));
301
+ }
302
+ export function verifyContentDigest(deployment) {
303
+ const members = deployment.value.t === "obj" ? deployment.value.v : [];
304
+ const declared = keyfind(members, "deployment_digest");
305
+ if (declared === null || declared.t !== "str")
306
+ return { ok: false, e: "invalid_type" };
307
+ const jcs = canonical.encode(digestInput(deployment));
308
+ if (!jcs.ok)
309
+ return jcs;
310
+ return digest.verifyContent("deployment_content", Buffer.from(jcs.v, "utf8"), declared.v);
311
+ }
312
+ // The one total reader every binding stage consumes: duplicate root members,
313
+ // a non-array member, a non-object entry, a non-string operand, or an
314
+ // unparsable descriptor digest is a malformed deployment, never empty.
315
+ function boundToolBindings(deployment) {
316
+ if (deployment.value.t !== "obj")
317
+ return { ok: false, e: "invalid_type" };
318
+ const members = deployment.value.v;
319
+ const names = members.map(([name]) => name);
320
+ if (new Set(names).size !== names.length)
321
+ return { ok: false, e: "invalid_type" };
322
+ const toolBindings = keyfind(members, "tool_bindings");
323
+ if (toolBindings === null)
324
+ return { ok: true, v: [] };
325
+ if (toolBindings.t !== "arr")
326
+ return { ok: false, e: "invalid_type" };
327
+ for (const entry of toolBindings.v) {
328
+ const judgment = entryJudgment(entry);
329
+ if (!judgment.ok)
330
+ return judgment;
331
+ }
332
+ return { ok: true, v: toolBindings.v.filter((entry) => entry.t === "obj") };
333
+ }
334
+ function entryJudgment(entry) {
335
+ if (entry.t !== "obj")
336
+ return { ok: false, e: "invalid_type" };
337
+ const operation = keyfind(entry.v, "logical_operation");
338
+ const attested = keyfind(entry.v, "attested_at");
339
+ const descriptor = keyfind(entry.v, "descriptor_digest");
340
+ if (operation === null || operation.t !== "str")
341
+ return { ok: false, e: "invalid_type" };
342
+ if (attested === null || attested.t !== "str")
343
+ return { ok: false, e: "invalid_type" };
344
+ if (descriptor === null || descriptor.t !== "str")
345
+ return { ok: false, e: "invalid_type" };
346
+ return digest.fromTagged(descriptor.v);
347
+ }
348
+ function boundRelease(deployment) {
349
+ const bindings = boundToolBindings(deployment);
350
+ if (!bindings.ok)
351
+ return bindings;
352
+ if (deployment.value.t !== "obj")
353
+ return { ok: false, e: "invalid_type" };
354
+ const release = keyfind(deployment.value.v, "blueprint_release");
355
+ if (release === null || release.t !== "obj")
356
+ return { ok: false, e: "invalid_type" };
357
+ const id = keyfind(release.v, "blueprint_id");
358
+ const number = keyfind(release.v, "release_number");
359
+ const tagged = keyfind(release.v, "content_digest");
360
+ if (id === null || id.t !== "str")
361
+ return { ok: false, e: "invalid_type" };
362
+ if (number === null || number.t !== "int" || number.v < 1)
363
+ return { ok: false, e: "invalid_type" };
364
+ if (tagged === null || tagged.t !== "str")
365
+ return { ok: false, e: "invalid_type" };
366
+ const parsed = digest.fromTagged(tagged.v);
367
+ if (!parsed.ok)
368
+ return parsed;
369
+ return { ok: true, v: { blueprintId: id.v, releaseNumber: number.v, contentDigest: parsed.v } };
370
+ }
371
+ // The bind-time deny set (order pinned): release digest → release identity →
372
+ // tool-binding completeness → mutation/recovery → attestation staleness →
373
+ // observed rug-pull. Stages 5-6 are observation-gated.
374
+ export function verifyBinding(deployment, bound, obs) {
375
+ const bindings = boundToolBindings(deployment);
376
+ if (!bindings.ok)
377
+ return bindings;
378
+ const releaseDigest = stageReleaseDigest(deployment, bound);
379
+ if (!releaseDigest.ok)
380
+ return releaseDigest;
381
+ const releaseIdentity = stageReleaseIdentity(deployment, bound);
382
+ if (!releaseIdentity.ok)
383
+ return releaseIdentity;
384
+ const completeness = stageCompleteness(bindings.v, bound);
385
+ if (!completeness.ok)
386
+ return completeness;
387
+ const recovery = stageRecovery(deployment, bindings.v, bound);
388
+ if (!recovery.ok)
389
+ return recovery;
390
+ const staleness = stageStaleness(bindings.v, obs);
391
+ if (!staleness.ok)
392
+ return staleness;
393
+ return stageRugPull(bindings.v, obs);
394
+ }
395
+ function stageReleaseDigest(deployment, bound) {
396
+ const release = boundRelease(deployment);
397
+ if (!release.ok)
398
+ return { ok: false, e: "invalid_type" };
399
+ const expected = blueprintContentDigest(bound);
400
+ if (expected === null)
401
+ return { ok: false, e: "invalid_type" };
402
+ return digest.equal(release.v.contentDigest, expected)
403
+ ? { ok: true }
404
+ : { ok: false, e: "deployment_digest_mismatch" };
405
+ }
406
+ function stageReleaseIdentity(deployment, bound) {
407
+ const release = boundRelease(deployment);
408
+ if (!release.ok)
409
+ return { ok: false, e: "invalid_type" };
410
+ if (bound.value.t !== "obj")
411
+ return { ok: false, e: "invalid_type" };
412
+ const id = keyfind(bound.value.v, "blueprint_id");
413
+ const number = keyfind(bound.value.v, "release_number");
414
+ if (id === null || id.t !== "str")
415
+ return { ok: false, e: "invalid_type" };
416
+ if (number === null || number.t !== "int")
417
+ return { ok: false, e: "invalid_type" };
418
+ return release.v.blueprintId === id.v && release.v.releaseNumber === number.v
419
+ ? { ok: true }
420
+ : { ok: false, e: "binding_incomplete" };
421
+ }
422
+ function stageCompleteness(bindings, bound) {
423
+ const known = new Set([...blueprintFamilies(bound), ...blueprintEffects(bound)]);
424
+ for (const entry of bindings) {
425
+ const operation = memberString(entry, "logical_operation");
426
+ if (!known.has(operation))
427
+ return { ok: false, e: "binding_incomplete" };
428
+ }
429
+ return { ok: true };
430
+ }
431
+ function stageRecovery(deployment, bindings, bound) {
432
+ const mode = recoveryMode(deployment);
433
+ if (!mode.ok)
434
+ return mode;
435
+ if (mode.v === "authoritative")
436
+ return { ok: true };
437
+ const families = blueprintFamilyKinds(bound);
438
+ const effects = blueprintEffectKinds(bound);
439
+ for (const entry of bindings) {
440
+ const operation = memberString(entry, "logical_operation");
441
+ const familyKind = families[operation];
442
+ const effectKind = effects[operation];
443
+ if (familyKind === "mutation" || effectKind === "mutation") {
444
+ return { ok: false, e: "no_authoritative_recovery" };
445
+ }
446
+ }
447
+ return { ok: true };
448
+ }
449
+ // now: null skips every staleness judgment (the host's governance choice).
450
+ function stageStaleness(bindings, obs) {
451
+ if (obs.now === null)
452
+ return { ok: true };
453
+ const now = parseTimestamp(obs.now);
454
+ if (now === null)
455
+ return { ok: true }; // parse_time parity: undecodable now skips
456
+ if (obs.maxAttestationAgeMs !== null && !Number.isInteger(obs.maxAttestationAgeMs)) {
457
+ return { ok: false, e: "invalid_type" };
458
+ }
459
+ for (const entry of bindings) {
460
+ const attested = memberString(entry, "attested_at");
461
+ const at = parseTimestamp(attested);
462
+ if (at === null)
463
+ return { ok: false, e: "invalid_type" };
464
+ if (at > now)
465
+ return { ok: false, e: "binding_attestation_stale" };
466
+ if (obs.maxAttestationAgeMs !== null &&
467
+ now - at > obs.maxAttestationAgeMs) {
468
+ return { ok: false, e: "binding_attestation_stale" };
469
+ }
470
+ }
471
+ return { ok: true };
472
+ }
473
+ function stageRugPull(bindings, obs) {
474
+ const observedEntries = Object.entries(obs.observed);
475
+ if (observedEntries.length === 0)
476
+ return { ok: true };
477
+ const attested = new Map();
478
+ for (const entry of bindings) {
479
+ const operation = memberString(entry, "logical_operation");
480
+ const tagged = memberString(entry, "descriptor_digest");
481
+ const parsed = digest.fromTagged(tagged);
482
+ if (parsed.ok)
483
+ attested.set(operation, parsed.v);
484
+ }
485
+ for (const [operation, observedTagged] of observedEntries) {
486
+ const declared = attested.get(operation);
487
+ if (declared === undefined)
488
+ continue;
489
+ const observed = digest.fromTagged(observedTagged);
490
+ if (!observed.ok)
491
+ return observed;
492
+ if (!digest.equal(observed.v, declared)) {
493
+ return { ok: false, e: "binding_descriptor_mismatch" };
494
+ }
495
+ }
496
+ return { ok: true };
497
+ }
498
+ function recoveryMode(deployment) {
499
+ if (deployment.value.t !== "obj")
500
+ return { ok: false, e: "invalid_type" };
501
+ const owner = keyfind(deployment.value.v, "effect_owner");
502
+ if (owner === null || owner.t !== "obj")
503
+ return { ok: false, e: "invalid_type" };
504
+ const idempotency = keyfind(owner.v, "idempotency");
505
+ if (idempotency === null || idempotency.t !== "obj")
506
+ return { ok: false, e: "invalid_type" };
507
+ const recovery = keyfind(idempotency.v, "recovery");
508
+ if (recovery === null || recovery.t !== "str")
509
+ return { ok: false, e: "invalid_type" };
510
+ if (recovery.v !== "authoritative" && recovery.v !== "none")
511
+ return { ok: false, e: "invalid_type" };
512
+ return { ok: true, v: recovery.v };
513
+ }
514
+ function blueprintContentDigest(bound) {
515
+ try {
516
+ const tagged = blueprint.contentDigest(bound);
517
+ const parsed = digest.fromTagged(tagged);
518
+ return parsed.ok ? parsed.v : null;
519
+ }
520
+ catch {
521
+ return null;
522
+ }
523
+ }
524
+ function blueprintFamilies(bound) {
525
+ const out = [];
526
+ for (const entry of memberArrayOf(bound, "capability_requirements")) {
527
+ if (entry.t !== "obj")
528
+ continue;
529
+ const family = keyfind(entry.v, "operation_family");
530
+ if (family !== null && family.t === "str")
531
+ out.push(family.v);
532
+ }
533
+ return out;
534
+ }
535
+ function blueprintEffects(bound) {
536
+ const out = [];
537
+ for (const entry of memberArrayOf(bound, "effect_intents")) {
538
+ if (entry.t !== "obj")
539
+ continue;
540
+ const operation = keyfind(entry.v, "logical_operation");
541
+ if (operation !== null && operation.t === "str")
542
+ out.push(operation.v);
543
+ }
544
+ return out;
545
+ }
546
+ function blueprintFamilyKinds(bound) {
547
+ const out = {};
548
+ for (const entry of memberArrayOf(bound, "capability_requirements")) {
549
+ if (entry.t !== "obj")
550
+ continue;
551
+ const family = keyfind(entry.v, "operation_family");
552
+ const kind = keyfind(entry.v, "operation_kind");
553
+ if (family !== null && family.t === "str" && kind !== null && kind.t === "str") {
554
+ out[family.v] = kind.v;
555
+ }
556
+ }
557
+ return out;
558
+ }
559
+ function blueprintEffectKinds(bound) {
560
+ const out = {};
561
+ for (const entry of memberArrayOf(bound, "effect_intents")) {
562
+ if (entry.t !== "obj")
563
+ continue;
564
+ const operation = keyfind(entry.v, "logical_operation");
565
+ const kind = keyfind(entry.v, "operation_kind");
566
+ if (operation !== null && operation.t === "str" && kind !== null && kind.t === "str") {
567
+ out[operation.v] = kind.v;
568
+ }
569
+ }
570
+ return out;
571
+ }
572
+ function memberArrayOf(artifact, name) {
573
+ if (artifact.value.t !== "obj")
574
+ return [];
575
+ const found = keyfind(artifact.value.v, name);
576
+ return found !== null && found.t === "arr" ? found.v : [];
577
+ }
578
+ // Pre-validated by stage 0: the named operand is a string on every entry.
579
+ function memberString(entry, name) {
580
+ if (entry.t !== "obj")
581
+ return "";
582
+ const found = keyfind(entry.v, name);
583
+ return found !== null && found.t === "str" ? found.v : "";
584
+ }
585
+ // ---- field checks (carried in the table as data) ----------------------------------------
586
+ function checkPositive(value) {
587
+ if (value.t === "int" && value.v >= 1)
588
+ return { ok: true };
589
+ return { ok: false, e: "invalid_constraint" };
590
+ }
591
+ function checkCurrency(value) {
592
+ if (value.t !== "str")
593
+ return { ok: false, e: "invalid_constraint" };
594
+ return CURRENCY.test(value.v) ? { ok: true } : { ok: false, e: "invalid_constraint" };
595
+ }
596
+ function checkProducerQualified(value) {
597
+ if (value.t !== "str")
598
+ return { ok: false, e: "invalid_constraint" };
599
+ const segments = value.v.split("/");
600
+ return (Buffer.byteLength(value.v, "utf8") <= IDENTIFIER_BYTES &&
601
+ segments.length === 2 &&
602
+ segments.every((s) => s !== "" && SEGMENT.test(s)))
603
+ ? { ok: true }
604
+ : { ok: false, e: "invalid_constraint" };
605
+ }
606
+ function checkIdentityString(value) {
607
+ if (value.t !== "str")
608
+ return { ok: false, e: "invalid_constraint" };
609
+ return value.v !== "" && Buffer.byteLength(value.v, "utf8") <= IDENTIFIER_BYTES
610
+ ? { ok: true }
611
+ : { ok: false, e: "invalid_constraint" };
612
+ }
613
+ function checkZForm(value) {
614
+ if (value.t !== "str")
615
+ return { ok: false, e: "invalid_constraint" };
616
+ return Z_FORM.test(value.v) && blueprint.validZFormDate(value.v)
617
+ ? { ok: true }
618
+ : { ok: false, e: "invalid_constraint" };
619
+ }
620
+ function checkTaggedDigest(value) {
621
+ if (value.t !== "str")
622
+ return { ok: false, e: "invalid_constraint" };
623
+ return digest.fromTagged(value.v);
624
+ }
625
+ function checkRoleStrings(value) {
626
+ if (value.t !== "arr")
627
+ return { ok: false, e: "invalid_constraint" };
628
+ const ok = value.v.every((item) => item.t === "str" && item.v !== "");
629
+ return ok ? { ok: true } : { ok: false, e: "invalid_constraint" };
630
+ }
631
+ // The attestation kind registry is empty BY DESIGN, identical to Blueprint.
632
+ function checkAttestations(value) {
633
+ if (value.t === "arr" && value.v.length === 0)
634
+ return { ok: true };
635
+ return { ok: false, e: "attestation_malformed" };
636
+ }
637
+ function checkRequiredCoreFields(value) {
638
+ if (value.t !== "arr")
639
+ return { ok: false, e: "invalid_constraint" };
640
+ const ok = value.v.every((item) => item.t === "str" && COVERED_MEMBERS.includes(item.v));
641
+ return ok ? { ok: true } : { ok: false, e: "invalid_constraint" };
642
+ }
643
+ // The purpose is this artifact's fact: the envelope parses AND must be
644
+ // "deployment" here.
645
+ function checkSignatureEntry(entry) {
646
+ const attrs = blueprint.signatureAttributes(entry);
647
+ if (!attrs.ok)
648
+ return { ok: false, e: attrs.e };
649
+ return attrs.v.purpose === "deployment" ? { ok: true } : { ok: false, e: "invalid_constraint" };
650
+ }
651
+ // An eligibility expression: any non-empty JSON object — the host's policy
652
+ // DSL, held verbatim and name-denylist-scanned.
653
+ function checkExpression(value) {
654
+ if (value.t === "obj" && value.v.length > 0)
655
+ return { ok: true };
656
+ return { ok: false, e: "invalid_constraint" };
657
+ }
658
+ // The as_of total rule: exactly (required + positive age) or (none + null).
659
+ // A MISSING max_age_ms defers to the member recursion; a bad mode defers to
660
+ // its enum — one reason per defect, no collisions.
661
+ function checkAsOf(value) {
662
+ if (value.t !== "obj")
663
+ return { ok: true };
664
+ const members = value.v;
665
+ const mode = keyfind(members, "mode");
666
+ const age = keyfind(members, "max_age_ms");
667
+ if (mode !== null && mode.t === "str") {
668
+ if (mode.v === "required")
669
+ return requiredAge(age);
670
+ if (mode.v === "none")
671
+ return noneAge(age);
672
+ }
673
+ return { ok: true };
674
+ }
675
+ function requiredAge(age) {
676
+ if (age === null)
677
+ return { ok: true };
678
+ if (age.t === "null")
679
+ return { ok: false, e: "invalid_constraint" };
680
+ if (age.t === "int")
681
+ return age.v >= 1 ? { ok: true } : { ok: false, e: "invalid_constraint" };
682
+ return { ok: false, e: "invalid_type" };
683
+ }
684
+ function noneAge(age) {
685
+ if (age === null || age.t === "null")
686
+ return { ok: true };
687
+ if (age.t === "int")
688
+ return { ok: false, e: "invalid_constraint" };
689
+ return { ok: false, e: "invalid_type" };
690
+ }
691
+ // The lifecycle temporal rules (the closed state enum itself denies at the
692
+ // member recursion; this check owns the state/timestamp coupling).
693
+ function checkLifecycle(value) {
694
+ if (value.t !== "obj")
695
+ return { ok: true };
696
+ const members = value.v;
697
+ const state = keyfind(members, "state");
698
+ const activated = keyfind(members, "activated_at");
699
+ const retired = keyfind(members, "retired_at");
700
+ if (state !== null && state.t === "str") {
701
+ if (state.v === "draft") {
702
+ return activated === null && retired === null ? { ok: true } : { ok: false, e: "lifecycle_state_invalid" };
703
+ }
704
+ if (state.v === "active") {
705
+ return activated !== null && retired === null ? { ok: true } : { ok: false, e: "lifecycle_state_invalid" };
706
+ }
707
+ if (state.v === "retired") {
708
+ if (activated !== null &&
709
+ activated.t === "str" &&
710
+ retired !== null &&
711
+ retired.t === "str") {
712
+ // Z-form whole-second strings sort lexicographically = chronologically.
713
+ return activated.v <= retired.v ? { ok: true } : { ok: false, e: "lifecycle_state_invalid" };
714
+ }
715
+ return { ok: false, e: "lifecycle_state_invalid" };
716
+ }
717
+ }
718
+ return { ok: true };
719
+ }
720
+ // The exact-only version pin: the protocol rejects the RANGE VOCABULARY, not
721
+ // semver semantics. The charset rule is total (every range operator sits
722
+ // outside [A-Za-z0-9.+-]); a dot-separated release segment that is exactly
723
+ // x/X/* is a wildcard.
724
+ function checkExactVersion(value) {
725
+ if (value.t !== "str")
726
+ return { ok: false, e: "compatibility_identity_inexact" };
727
+ const version = value.v;
728
+ const release = version.split("-")[0].split("+")[0];
729
+ const segments = release.split(".");
730
+ if (version === "")
731
+ return { ok: false, e: "compatibility_identity_inexact" };
732
+ if (!EXACT_CHARSET.test(version))
733
+ return { ok: false, e: "compatibility_identity_inexact" };
734
+ if (["*", "latest"].includes(version.toLowerCase())) {
735
+ return { ok: false, e: "compatibility_identity_inexact" };
736
+ }
737
+ if (segments.some((s) => ["x", "X", "*"].includes(s))) {
738
+ return { ok: false, e: "compatibility_identity_inexact" };
739
+ }
740
+ return { ok: true };
741
+ }
742
+ // ---- the portability scan ---------------------------------------------------------------
743
+ function scan(value, authoredNs) {
744
+ if (value.t !== "obj")
745
+ return { ok: false, e: "invalid_type" };
746
+ const members = value.v;
747
+ const openRegions = scanOpenRegions(members, authoredNs);
748
+ if (!openRegions.ok)
749
+ return openRegions;
750
+ const coreStrings = scanCoreStrings(members);
751
+ if (!coreStrings.ok)
752
+ return coreStrings;
753
+ return scanEvidenceKeyIds(members);
754
+ }
755
+ // Open regions: extension bodies (strict) and eligibility expressions (names
756
+ // strict, values under the authored exemption).
757
+ function scanOpenRegions(members, validatedNs) {
758
+ const strictBodies = extension.bodies(members).filter(([ns]) => !validatedNs.has(ns));
759
+ const eligibility = members.find(([name]) => name === "eligibility");
760
+ const eligibilityValues = [];
761
+ if (eligibility !== undefined && eligibility[1].t === "obj") {
762
+ for (const [, expression] of eligibility[1].v) {
763
+ if (expression.t === "obj")
764
+ eligibilityValues.push(expression);
765
+ }
766
+ }
767
+ const scans = [
768
+ ...strictBodies.map(([, body]) => portability.scan(body)),
769
+ ...eligibilityValues.map((expression) => portability.scanAuthored(expression)),
770
+ ];
771
+ for (const result of scans) {
772
+ if (!result.ok)
773
+ return result;
774
+ }
775
+ return { ok: true };
776
+ }
777
+ // Value shapes over every other core string, recursively — no member of this
778
+ // artifact is exempt.
779
+ function scanCoreStrings(members) {
780
+ for (const [name, value] of members) {
781
+ if (EVIDENCE_MEMBERS.includes(name) || name === "extensions")
782
+ continue;
783
+ if (name === "eligibility")
784
+ continue; // open region, scanned above
785
+ const result = portability.scanValue(value);
786
+ if (!result.ok)
787
+ return result;
788
+ }
789
+ return { ok: true };
790
+ }
791
+ // The one free string inside an evidence entry: evidence members are
792
+ // digest-UNCOVERED — a secret-shaped key there must still red.
793
+ function scanEvidenceKeyIds(members) {
794
+ const signatures = keyfind(members, "signatures");
795
+ if (signatures === null || signatures.t !== "arr")
796
+ return { ok: true };
797
+ for (const entry of signatures.v) {
798
+ const attrs = blueprint.signatureAttributes(entry);
799
+ if (!attrs.ok)
800
+ continue; // unparseable entries already denied at the table
801
+ const scanned = portability.scanValue({ t: "str", v: attrs.v.keyId });
802
+ if (!scanned.ok)
803
+ return scanned;
804
+ }
805
+ return { ok: true };
806
+ }
807
+ // ---- helpers --------------------------------------------------------------------------------
808
+ function normalizeBounds(bounds) {
809
+ if (bounds === undefined)
810
+ return { ok: true, v: maxBounds() };
811
+ return coerceBounds(bounds);
812
+ }
813
+ // Z-form whole-second timestamps → epoch milliseconds; null when undecodable
814
+ // (DateTime.from_iso8601 parity, restricted to the calendar-valid Z form the
815
+ // tables already enforce).
816
+ export function parseTimestamp(stamped) {
817
+ const matched = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})(Z|[+-]\d{2}:\d{2})$/.exec(stamped);
818
+ if (matched === null)
819
+ return null;
820
+ const [, y, mo, d, h, mi, s, zone] = matched;
821
+ if (!blueprint.validZFormDate(`${y}-${mo}-${d}T${h}:${mi}:${s}Z`))
822
+ return null;
823
+ let ms = Date.UTC(Number(y), Number(mo) - 1, Number(d), Number(h), Number(mi), Number(s));
824
+ if (zone !== "Z") {
825
+ const sign = zone[0] === "-" ? -1 : 1;
826
+ const offset = Number(zone.slice(1, 3)) * 60 + Number(zone.slice(4, 6));
827
+ ms -= sign * offset * 60_000;
828
+ }
829
+ return ms;
830
+ }