@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.
- package/CHANGELOG.md +891 -0
- package/LICENSE +190 -0
- package/NOTICE +4 -0
- package/README.md +199 -0
- package/dist/corpus/cases/base64url-decode.json +1 -0
- package/dist/corpus/cases/blueprint-decode.json +1 -0
- package/dist/corpus/cases/bounds-new.json +1 -0
- package/dist/corpus/cases/bounds_algebra-intersect.json +1 -0
- package/dist/corpus/cases/canonicalization-encode.json +1 -0
- package/dist/corpus/cases/compatibility-verify.json +1 -0
- package/dist/corpus/cases/deployment-decode.json +1 -0
- package/dist/corpus/cases/digest-tagged.json +1 -0
- package/dist/corpus/cases/extension-resolve.json +1 -0
- package/dist/corpus/cases/federation-decode.json +1 -0
- package/dist/corpus/cases/federation-verify_commitment.json +1 -0
- package/dist/corpus/cases/json-decode.json +1 -0
- package/dist/corpus/cases/negotiation-negotiate.json +1 -0
- package/dist/corpus/cases/portability-scan.json +1 -0
- package/dist/corpus/cases/schema-validate_instance.json +1 -0
- package/dist/corpus/cases/signature-verify.json +1 -0
- package/dist/corpus/index.json +1 -0
- package/dist/corpus/schemas/estate-contract.schema.json +1 -0
- package/dist/corpus/schemas/object.schema.json +1 -0
- package/dist/corpus/vectors/blueprint-golden.json +1 -0
- package/dist/corpus/vectors/deployment-golden.json +1 -0
- package/dist/corpus/vectors/rfc8785-numbers.json +1 -0
- package/dist/kit/cli.mjs +24 -0
- package/dist/verifier/b64url.d.ts +16 -0
- package/dist/verifier/b64url.js +36 -0
- package/dist/verifier/blueprint.d.ts +56 -0
- package/dist/verifier/blueprint.js +795 -0
- package/dist/verifier/bounds.d.ts +19 -0
- package/dist/verifier/bounds.js +45 -0
- package/dist/verifier/bounds_algebra.d.ts +27 -0
- package/dist/verifier/bounds_algebra.js +282 -0
- package/dist/verifier/canonical.d.ts +17 -0
- package/dist/verifier/canonical.js +160 -0
- package/dist/verifier/cli.d.ts +1 -0
- package/dist/verifier/cli.js +149 -0
- package/dist/verifier/compatibility.d.ts +22 -0
- package/dist/verifier/compatibility.js +112 -0
- package/dist/verifier/corpus.d.ts +42 -0
- package/dist/verifier/corpus.js +672 -0
- package/dist/verifier/decode.d.ts +16 -0
- package/dist/verifier/decode.js +457 -0
- package/dist/verifier/deployment.d.ts +49 -0
- package/dist/verifier/deployment.js +830 -0
- package/dist/verifier/digest.d.ts +23 -0
- package/dist/verifier/digest.js +58 -0
- package/dist/verifier/extension.d.ts +11 -0
- package/dist/verifier/extension.js +75 -0
- package/dist/verifier/federation.d.ts +76 -0
- package/dist/verifier/federation.js +817 -0
- package/dist/verifier/negotiation.d.ts +29 -0
- package/dist/verifier/negotiation.js +324 -0
- package/dist/verifier/portability.d.ts +12 -0
- package/dist/verifier/portability.js +184 -0
- package/dist/verifier/predicate.d.ts +10 -0
- package/dist/verifier/predicate.js +172 -0
- package/dist/verifier/registry.d.ts +16 -0
- package/dist/verifier/registry.js +120 -0
- package/dist/verifier/registry_engine.d.ts +42 -0
- package/dist/verifier/registry_engine.js +220 -0
- package/dist/verifier/report.d.ts +13 -0
- package/dist/verifier/report.js +29 -0
- package/dist/verifier/run_corpus.d.ts +1 -0
- package/dist/verifier/run_corpus.js +81 -0
- package/dist/verifier/runner.d.ts +20 -0
- package/dist/verifier/runner.js +778 -0
- package/dist/verifier/schema.d.ts +20 -0
- package/dist/verifier/schema.js +623 -0
- package/dist/verifier/self_checks.d.ts +1 -0
- package/dist/verifier/self_checks.js +316 -0
- package/dist/verifier/signature.d.ts +23 -0
- package/dist/verifier/signature.js +226 -0
- package/dist/verifier/value.d.ts +44 -0
- package/dist/verifier/value.js +77 -0
- package/package.json +42 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface Bounds {
|
|
2
|
+
bytes: number;
|
|
3
|
+
depth: number;
|
|
4
|
+
members: number;
|
|
5
|
+
items: number;
|
|
6
|
+
nodes: number;
|
|
7
|
+
string: number;
|
|
8
|
+
key: number;
|
|
9
|
+
number_lexeme: number;
|
|
10
|
+
}
|
|
11
|
+
export declare const MAXIMA: Bounds;
|
|
12
|
+
export declare function maximum(): Bounds;
|
|
13
|
+
export declare function coerce(overrides: unknown): {
|
|
14
|
+
ok: true;
|
|
15
|
+
v: Bounds;
|
|
16
|
+
} | {
|
|
17
|
+
ok: false;
|
|
18
|
+
e: string;
|
|
19
|
+
};
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Parse ceilings — the mirror of AgentBlueprintProtocol.Bounds: the immutable
|
|
2
|
+
// profile maxima and the tighten-only override rule (unknown key denies
|
|
3
|
+
// `unknown_bound`; a non-positive, non-integer, or super-maximum value denies
|
|
4
|
+
// `ceiling:<field>`).
|
|
5
|
+
export const MAXIMA = {
|
|
6
|
+
bytes: 5_000_000,
|
|
7
|
+
depth: 64,
|
|
8
|
+
members: 1_000,
|
|
9
|
+
items: 10_000,
|
|
10
|
+
nodes: 200_000,
|
|
11
|
+
string: 100_000,
|
|
12
|
+
key: 1_000,
|
|
13
|
+
number_lexeme: 64,
|
|
14
|
+
};
|
|
15
|
+
const FIELDS = Object.keys(MAXIMA);
|
|
16
|
+
export function maximum() {
|
|
17
|
+
return { ...MAXIMA };
|
|
18
|
+
}
|
|
19
|
+
// Overrides arrive as a plain object of string keys (corpus case input); an
|
|
20
|
+
// off-lattice key passes through untouched so the denial comes from this
|
|
21
|
+
// module, not from a pre-filtering caller.
|
|
22
|
+
export function coerce(overrides) {
|
|
23
|
+
if (overrides === null || typeof overrides !== "object" || Array.isArray(overrides)) {
|
|
24
|
+
return { ok: false, e: "invalid_type" };
|
|
25
|
+
}
|
|
26
|
+
const source = overrides;
|
|
27
|
+
const out = maximum();
|
|
28
|
+
for (const key of Object.keys(source)) {
|
|
29
|
+
if (!FIELDS.includes(key))
|
|
30
|
+
return { ok: false, e: "unknown_bound" };
|
|
31
|
+
}
|
|
32
|
+
for (const key of FIELDS) {
|
|
33
|
+
if (!(key in source))
|
|
34
|
+
continue;
|
|
35
|
+
const value = source[key];
|
|
36
|
+
if (typeof value !== "number" ||
|
|
37
|
+
!Number.isInteger(value) ||
|
|
38
|
+
value < 1 ||
|
|
39
|
+
value > MAXIMA[key]) {
|
|
40
|
+
return { ok: false, e: `ceiling:${key}` };
|
|
41
|
+
}
|
|
42
|
+
out[key] = value;
|
|
43
|
+
}
|
|
44
|
+
return { ok: true, v: out };
|
|
45
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type BoundClass = "operational" | "protected";
|
|
2
|
+
type BoundValue = number | string | {
|
|
3
|
+
ordinal: string;
|
|
4
|
+
markers: Set<string>;
|
|
5
|
+
} | {
|
|
6
|
+
amount: number;
|
|
7
|
+
currency: string;
|
|
8
|
+
};
|
|
9
|
+
interface Evidence {
|
|
10
|
+
field: string;
|
|
11
|
+
cls: BoundClass;
|
|
12
|
+
requested: BoundValue;
|
|
13
|
+
effective: BoundValue;
|
|
14
|
+
source: "deployment" | "host";
|
|
15
|
+
acknowledged: boolean;
|
|
16
|
+
}
|
|
17
|
+
export declare function intersect(blueprint: Record<string, unknown>, deployment: Record<string, unknown>, host: Record<string, unknown>, posture: "acknowledge" | "deny"): {
|
|
18
|
+
ok: true;
|
|
19
|
+
v: {
|
|
20
|
+
effective: Record<string, unknown>;
|
|
21
|
+
clamps: Evidence[];
|
|
22
|
+
};
|
|
23
|
+
} | {
|
|
24
|
+
ok: false;
|
|
25
|
+
e: string;
|
|
26
|
+
};
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// The bounds algebra — the mirror of AgentBlueprintProtocol.BoundsAlgebra:
|
|
2
|
+
// the pointwise narrowest intersection of three total bound sets over the
|
|
3
|
+
// closed 13-bound vocabulary. Scope families meet at the written-order
|
|
4
|
+
// minimum with marker-union retention; obligation families at the
|
|
5
|
+
// strictest value; operational numerics at the minimum; money at the minimum
|
|
6
|
+
// amount within one currency. A protected narrowing denies under the :deny
|
|
7
|
+
// posture (protected_bound_clamp_denied) or clamps with acknowledged evidence
|
|
8
|
+
// under :acknowledge. Effective values leave as the runner's projection:
|
|
9
|
+
// scopes {ordinal, markers(sorted)}, costs {amount, currency}, atoms as
|
|
10
|
+
// strings, numerics as numbers.
|
|
11
|
+
const IJSON_MAX = 9_007_199_254_740_991;
|
|
12
|
+
const LATTICES = {
|
|
13
|
+
classification: ["public", "internal", "confidential", "restricted"],
|
|
14
|
+
authority: ["none", "local_policy", "external_authority_required"],
|
|
15
|
+
approval: ["none", "human_required", "separated_human_required"],
|
|
16
|
+
impact: ["ordinary", "money", "authority", "secret"],
|
|
17
|
+
disclosure: ["none", "summary", "detail", "full"],
|
|
18
|
+
};
|
|
19
|
+
const MARKERS = ["pci", "phi"];
|
|
20
|
+
const CURRENCY = /^[A-Z]{3}$/;
|
|
21
|
+
// name → {class, value-kind}
|
|
22
|
+
const TABLE = {
|
|
23
|
+
max_attempts: { cls: "operational", kind: "pos_integer" },
|
|
24
|
+
max_concurrency: { cls: "operational", kind: "pos_integer" },
|
|
25
|
+
max_depth: { cls: "operational", kind: "pos_integer" },
|
|
26
|
+
max_descendants: { cls: "operational", kind: "pos_integer" },
|
|
27
|
+
max_elapsed_ms: { cls: "operational", kind: "pos_integer" },
|
|
28
|
+
max_fan_out: { cls: "operational", kind: "pos_integer" },
|
|
29
|
+
max_tokens: { cls: "operational", kind: "pos_integer" },
|
|
30
|
+
max_cost: { cls: "operational", kind: "cost" },
|
|
31
|
+
classification_ceiling: { cls: "protected", kind: { scope: "classification", markers: true } },
|
|
32
|
+
authority_trait: { cls: "protected", kind: { obligation: "authority" } },
|
|
33
|
+
approval_trait: { cls: "protected", kind: { obligation: "approval" } },
|
|
34
|
+
effect_impact_ceiling: { cls: "protected", kind: { obligation: "impact" } },
|
|
35
|
+
disclosure_ceiling: { cls: "protected", kind: { scope: "disclosure" } },
|
|
36
|
+
};
|
|
37
|
+
// Name-sorted vocabulary (the clamps order).
|
|
38
|
+
const NAMES = Object.keys(TABLE).sort();
|
|
39
|
+
// Missing-member precedence, pinned: sources in role order; per source the
|
|
40
|
+
// seven numerics name-sorted, then max_cost, then the five protected
|
|
41
|
+
// name-sorted; the first missing member fires.
|
|
42
|
+
const MISSING_PRECEDENCE = [
|
|
43
|
+
"max_attempts",
|
|
44
|
+
"max_concurrency",
|
|
45
|
+
"max_depth",
|
|
46
|
+
"max_descendants",
|
|
47
|
+
"max_elapsed_ms",
|
|
48
|
+
"max_fan_out",
|
|
49
|
+
"max_tokens",
|
|
50
|
+
"max_cost",
|
|
51
|
+
"approval_trait",
|
|
52
|
+
"authority_trait",
|
|
53
|
+
"classification_ceiling",
|
|
54
|
+
"disclosure_ceiling",
|
|
55
|
+
"effect_impact_ceiling",
|
|
56
|
+
];
|
|
57
|
+
const SOURCE_ORDER = ["blueprint", "deployment", "host"];
|
|
58
|
+
export function intersect(blueprint, deployment, host, posture) {
|
|
59
|
+
if (posture !== "acknowledge" && posture !== "deny") {
|
|
60
|
+
return { ok: false, e: "invalid_constraint" };
|
|
61
|
+
}
|
|
62
|
+
const sources = {};
|
|
63
|
+
for (const role of SOURCE_ORDER) {
|
|
64
|
+
const raw = role === "blueprint" ? blueprint : role === "deployment" ? deployment : host;
|
|
65
|
+
const built = buildSet(raw);
|
|
66
|
+
if (!built.ok)
|
|
67
|
+
return built;
|
|
68
|
+
sources[role] = built.v;
|
|
69
|
+
}
|
|
70
|
+
// Totality under the pinned precedence (all three total over the 13).
|
|
71
|
+
const total = totalOf(sources);
|
|
72
|
+
if (!total.ok)
|
|
73
|
+
return total;
|
|
74
|
+
// The within-currency cost rule.
|
|
75
|
+
const currencies = SOURCE_ORDER.map((role) => {
|
|
76
|
+
const cost = sources[role]["max_cost"];
|
|
77
|
+
return cost.currency;
|
|
78
|
+
});
|
|
79
|
+
if (new Set(currencies).size !== 1) {
|
|
80
|
+
return { ok: false, e: "bound_unit_mismatch" };
|
|
81
|
+
}
|
|
82
|
+
return compose(sources, posture);
|
|
83
|
+
}
|
|
84
|
+
// ---- construction ------------------------------------------------------------------
|
|
85
|
+
//
|
|
86
|
+
// The TS runner passes corpus bound-set objects through verbatim (the Elixir
|
|
87
|
+
// runner pre-filters unknown member names to :invalid_type at add_bound_entry;
|
|
88
|
+
// here that delegated filter runs first, then BoundSet.new's value checks).
|
|
89
|
+
function buildSet(raw) {
|
|
90
|
+
const out = {};
|
|
91
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
92
|
+
if (!(name in TABLE))
|
|
93
|
+
return { ok: false, e: "invalid_type" };
|
|
94
|
+
const built = buildBound(name, value);
|
|
95
|
+
if (!built.ok)
|
|
96
|
+
return built;
|
|
97
|
+
out[name] = built.v;
|
|
98
|
+
}
|
|
99
|
+
return { ok: true, v: out };
|
|
100
|
+
}
|
|
101
|
+
function buildBound(name, raw) {
|
|
102
|
+
const def = TABLE[name];
|
|
103
|
+
const kind = def.kind;
|
|
104
|
+
if (kind === "pos_integer") {
|
|
105
|
+
if (typeof raw === "number" && Number.isInteger(raw) && raw > 0 && raw <= IJSON_MAX) {
|
|
106
|
+
return { ok: true, v: raw };
|
|
107
|
+
}
|
|
108
|
+
return { ok: false, e: "bound_value_invalid" };
|
|
109
|
+
}
|
|
110
|
+
if (kind === "cost") {
|
|
111
|
+
if (isPlainObject(raw) && Object.keys(raw).length === 2) {
|
|
112
|
+
const record = raw;
|
|
113
|
+
const amount = record["amount"];
|
|
114
|
+
const currency = record["currency"];
|
|
115
|
+
if (typeof amount === "number" && Number.isInteger(amount) && amount > 0 && amount <= IJSON_MAX &&
|
|
116
|
+
typeof currency === "string" && CURRENCY.test(currency)) {
|
|
117
|
+
return { ok: true, v: { amount, currency } };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return { ok: false, e: "bound_value_invalid" };
|
|
121
|
+
}
|
|
122
|
+
if (typeof kind === "object" && "markers" in kind) {
|
|
123
|
+
if (isPlainObject(raw) && Object.keys(raw).length === 2) {
|
|
124
|
+
const record = raw;
|
|
125
|
+
const ordinal = record["ordinal"];
|
|
126
|
+
const markersRaw = record["markers"];
|
|
127
|
+
if (typeof ordinal === "string" && LATTICES[kind.scope].includes(ordinal) &&
|
|
128
|
+
Array.isArray(markersRaw) && markersRaw.every((m) => typeof m === "string" && MARKERS.includes(m))) {
|
|
129
|
+
return { ok: true, v: { ordinal, markers: new Set(markersRaw) } };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return { ok: false, e: "bound_value_invalid" };
|
|
133
|
+
}
|
|
134
|
+
if (typeof kind === "object" && "scope" in kind) {
|
|
135
|
+
const lattice = LATTICES[kind.scope];
|
|
136
|
+
if (typeof raw === "string" && lattice.includes(raw)) {
|
|
137
|
+
return { ok: true, v: raw };
|
|
138
|
+
}
|
|
139
|
+
return { ok: false, e: "bound_value_invalid" };
|
|
140
|
+
}
|
|
141
|
+
// obligation: a bare lattice atom
|
|
142
|
+
const lattice = LATTICES[kind.obligation];
|
|
143
|
+
if (typeof raw === "string" && lattice.includes(raw)) {
|
|
144
|
+
return { ok: true, v: raw };
|
|
145
|
+
}
|
|
146
|
+
return { ok: false, e: "bound_value_invalid" };
|
|
147
|
+
}
|
|
148
|
+
function isPlainObject(value) {
|
|
149
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
150
|
+
}
|
|
151
|
+
// ---- totality ----------------------------------------------------------------------
|
|
152
|
+
function totalOf(sources) {
|
|
153
|
+
for (const role of SOURCE_ORDER) {
|
|
154
|
+
const set = sources[role];
|
|
155
|
+
const present = new Set(Object.keys(set));
|
|
156
|
+
const expected = new Set(NAMES);
|
|
157
|
+
const extra = [...present].filter((name) => !expected.has(name));
|
|
158
|
+
if (extra.length > 0)
|
|
159
|
+
return { ok: false, e: "bound_unknown" };
|
|
160
|
+
if (present.size === expected.size && NAMES.every((n) => present.has(n))) {
|
|
161
|
+
// re-validate present values (bypassed-set parity)
|
|
162
|
+
for (const name of NAMES) {
|
|
163
|
+
const value = set[name];
|
|
164
|
+
const rebuilt = rebuild(name, value);
|
|
165
|
+
if (!rebuilt.ok)
|
|
166
|
+
return rebuilt;
|
|
167
|
+
}
|
|
168
|
+
continue;
|
|
169
|
+
}
|
|
170
|
+
for (const name of MISSING_PRECEDENCE) {
|
|
171
|
+
if (present.has(name)) {
|
|
172
|
+
const rebuilt = rebuild(name, set[name]);
|
|
173
|
+
if (!rebuilt.ok)
|
|
174
|
+
return rebuilt;
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
return { ok: false, e: TABLE[name].cls === "operational" ? "missing_ceiling" : "bound_source_missing" };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return { ok: true };
|
|
182
|
+
}
|
|
183
|
+
// Values already validated at buildSet; this mirrors the twin's per-source
|
|
184
|
+
// re-validation of struct-bypassed sets (always :ok on this path).
|
|
185
|
+
function rebuild(name, value) {
|
|
186
|
+
const projected = projectValue(value);
|
|
187
|
+
return buildBound(name, projected).ok ? { ok: true } : { ok: false, e: "bound_value_invalid" };
|
|
188
|
+
}
|
|
189
|
+
// ---- the meet -----------------------------------------------------------------------
|
|
190
|
+
function compose(sources, posture) {
|
|
191
|
+
const effective = {};
|
|
192
|
+
const clamps = [];
|
|
193
|
+
for (const name of NAMES) {
|
|
194
|
+
const def = TABLE[name];
|
|
195
|
+
const requested = sources.blueprint[name];
|
|
196
|
+
const met = meet(name, sources);
|
|
197
|
+
if (boundValueEqual(met, requested)) {
|
|
198
|
+
effective[name] = projectValue(met);
|
|
199
|
+
continue;
|
|
200
|
+
}
|
|
201
|
+
if (def.cls === "protected" && posture === "deny") {
|
|
202
|
+
return { ok: false, e: "protected_bound_clamp_denied" };
|
|
203
|
+
}
|
|
204
|
+
clamps.push({
|
|
205
|
+
field: name,
|
|
206
|
+
cls: def.cls,
|
|
207
|
+
requested,
|
|
208
|
+
effective: met,
|
|
209
|
+
source: attribution(name, sources, met),
|
|
210
|
+
acknowledged: def.cls === "protected" && posture === "acknowledge",
|
|
211
|
+
});
|
|
212
|
+
effective[name] = projectValue(met);
|
|
213
|
+
}
|
|
214
|
+
return { ok: true, v: { effective, clamps } };
|
|
215
|
+
}
|
|
216
|
+
function meet(name, sources) {
|
|
217
|
+
const kind = TABLE[name].kind;
|
|
218
|
+
const values = SOURCE_ORDER.map((role) => sources[role][name]);
|
|
219
|
+
if (kind === "cost") {
|
|
220
|
+
const amounts = values.map((v) => v.amount);
|
|
221
|
+
const currency = sources.blueprint["max_cost"].currency;
|
|
222
|
+
return { amount: Math.min(...amounts), currency };
|
|
223
|
+
}
|
|
224
|
+
if (kind === "pos_integer") {
|
|
225
|
+
return Math.min(...values);
|
|
226
|
+
}
|
|
227
|
+
if (typeof kind === "object" && "markers" in kind) {
|
|
228
|
+
const lattice = LATTICES[kind.scope];
|
|
229
|
+
const ordinal = values
|
|
230
|
+
.map((v) => v.ordinal)
|
|
231
|
+
.reduce((a, b) => (lattice.indexOf(a) <= lattice.indexOf(b) ? a : b));
|
|
232
|
+
const markers = new Set();
|
|
233
|
+
for (const v of values) {
|
|
234
|
+
for (const m of v.markers)
|
|
235
|
+
markers.add(m);
|
|
236
|
+
}
|
|
237
|
+
return { ordinal, markers };
|
|
238
|
+
}
|
|
239
|
+
if (typeof kind === "object" && "scope" in kind) {
|
|
240
|
+
const lattice = LATTICES[kind.scope];
|
|
241
|
+
return values.reduce((a, b) => (lattice.indexOf(a) <= lattice.indexOf(b) ? a : b));
|
|
242
|
+
}
|
|
243
|
+
// obligation: the strictest (max lattice index) wins
|
|
244
|
+
const lattice = LATTICES[kind.obligation];
|
|
245
|
+
return values.reduce((a, b) => (lattice.indexOf(a) >= lattice.indexOf(b) ? a : b));
|
|
246
|
+
}
|
|
247
|
+
// The non-blueprint source whose value IS the effective; ties and composites
|
|
248
|
+
// attribute :host (the live policy is the operative constraint).
|
|
249
|
+
function attribution(name, sources, effective) {
|
|
250
|
+
if (boundValueEqual(sources.host[name], effective))
|
|
251
|
+
return "host";
|
|
252
|
+
if (boundValueEqual(sources.deployment[name], effective))
|
|
253
|
+
return "deployment";
|
|
254
|
+
return "host";
|
|
255
|
+
}
|
|
256
|
+
function boundValueEqual(a, b) {
|
|
257
|
+
if (typeof a === "number" || typeof b === "number")
|
|
258
|
+
return a === b;
|
|
259
|
+
if (typeof a === "string" || typeof b === "string")
|
|
260
|
+
return a === b;
|
|
261
|
+
if (isScope(a) && isScope(b)) {
|
|
262
|
+
return a.ordinal === b.ordinal && a.markers.size === b.markers.size && [...a.markers].every((m) => b.markers.has(m));
|
|
263
|
+
}
|
|
264
|
+
if (isCost(a) && isCost(b))
|
|
265
|
+
return a.amount === b.amount && a.currency === b.currency;
|
|
266
|
+
return false;
|
|
267
|
+
}
|
|
268
|
+
function isScope(v) {
|
|
269
|
+
return typeof v === "object" && v !== null && "ordinal" in v && "markers" in v;
|
|
270
|
+
}
|
|
271
|
+
function isCost(v) {
|
|
272
|
+
return typeof v === "object" && v !== null && "amount" in v && "currency" in v;
|
|
273
|
+
}
|
|
274
|
+
// The runner's project_bound_value: scopes as {ordinal, markers sorted};
|
|
275
|
+
// costs as {amount, currency}; the rest as carried.
|
|
276
|
+
function projectValue(value) {
|
|
277
|
+
if (isScope(value))
|
|
278
|
+
return { ordinal: value.ordinal, markers: [...value.markers].sort() };
|
|
279
|
+
if (isCost(value))
|
|
280
|
+
return { amount: value.amount, currency: value.currency };
|
|
281
|
+
return value;
|
|
282
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { Bounds } from "./bounds.ts";
|
|
2
|
+
import type { Value } from "./value.ts";
|
|
3
|
+
export declare function encode(value: Value, bounds?: Bounds | Record<string, unknown>): {
|
|
4
|
+
ok: true;
|
|
5
|
+
v: string;
|
|
6
|
+
} | {
|
|
7
|
+
ok: false;
|
|
8
|
+
e: string;
|
|
9
|
+
};
|
|
10
|
+
export declare function verify(input: Buffer | string, bounds?: Bounds): {
|
|
11
|
+
ok: true;
|
|
12
|
+
v: Value;
|
|
13
|
+
} | {
|
|
14
|
+
ok: false;
|
|
15
|
+
e: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function number(f: number): string;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// RFC 8785 JCS encoder — the mirror of AgentBlueprintProtocol.Canonicalization.
|
|
2
|
+
//
|
|
3
|
+
// The ES6 number oracle lives HERE and only here: canonical
|
|
4
|
+
// number emission and the decoder's window admission share one decision, the
|
|
5
|
+
// same single-serializer rule the Elixir side enforces by having Json consume
|
|
6
|
+
// Canonicalization.number/1. Digits delegate to JSON.stringify (ECMA-262
|
|
7
|
+
// §7.1.12.1 IS the native behavior); structure, sorting, and escaping are
|
|
8
|
+
// hand-rolled so the corpus exercises this implementation, not V8's wholesale.
|
|
9
|
+
//
|
|
10
|
+
// Member sort: plain JS string comparison — `<` on strings is UTF-16 code-unit
|
|
11
|
+
// lexicographic, exactly RFC 8785 §3.2.3 (U+10000 sorts before U+FF3A).
|
|
12
|
+
//
|
|
13
|
+
// The decode ↔ canonical import cycle (verify needs decode; decode's window
|
|
14
|
+
// rule needs the number oracle) is a node-ESM circular import over hoisted
|
|
15
|
+
// function declarations — resolved at call time, never at module init.
|
|
16
|
+
import { maximum } from "./bounds.js";
|
|
17
|
+
import { decode } from "./decode.js";
|
|
18
|
+
import { err, ok, IJSON_MAX } from "./value.js";
|
|
19
|
+
const SHORT_ESCAPES = {
|
|
20
|
+
0x08: "b",
|
|
21
|
+
0x09: "t",
|
|
22
|
+
0x0a: "n",
|
|
23
|
+
0x0c: "f",
|
|
24
|
+
0x0d: "r",
|
|
25
|
+
};
|
|
26
|
+
export function encode(value, bounds) {
|
|
27
|
+
// The profile is read field-wise (bytes) whatever the caller passed.
|
|
28
|
+
const profile = (bounds ?? maximum());
|
|
29
|
+
const parts = [];
|
|
30
|
+
const walked = walk(value, parts);
|
|
31
|
+
if (!walked.ok)
|
|
32
|
+
return walked;
|
|
33
|
+
const out = parts.join("");
|
|
34
|
+
if (Buffer.byteLength(out, "utf8") > profile.bytes)
|
|
35
|
+
return err("ceiling:bytes");
|
|
36
|
+
return ok(out);
|
|
37
|
+
}
|
|
38
|
+
export function verify(input, bounds) {
|
|
39
|
+
const decoded = decode(input, bounds);
|
|
40
|
+
if (!decoded.ok)
|
|
41
|
+
return decoded;
|
|
42
|
+
const reEncoded = encode(decoded.v, bounds);
|
|
43
|
+
if (!reEncoded.ok)
|
|
44
|
+
return reEncoded;
|
|
45
|
+
const original = typeof input === "string" ? input : input.toString("utf8");
|
|
46
|
+
if (reEncoded.v === original)
|
|
47
|
+
return ok(decoded.v);
|
|
48
|
+
return err("non_canonical_bytes");
|
|
49
|
+
}
|
|
50
|
+
// ---- the ES6 number oracle ------------------------------------------------------
|
|
51
|
+
export function number(f) {
|
|
52
|
+
if (!Number.isFinite(f))
|
|
53
|
+
throw new Error("canonical.number: non-finite float");
|
|
54
|
+
if (f === 0)
|
|
55
|
+
return "0";
|
|
56
|
+
if (f < 0)
|
|
57
|
+
return "-" + number(-f);
|
|
58
|
+
return JSON.stringify(f);
|
|
59
|
+
}
|
|
60
|
+
// ---- encode walk ----------------------------------------------------------------
|
|
61
|
+
function walk(value, out) {
|
|
62
|
+
switch (value.t) {
|
|
63
|
+
case "null":
|
|
64
|
+
out.push("null");
|
|
65
|
+
return ok(undefined);
|
|
66
|
+
case "bool":
|
|
67
|
+
out.push(value.v ? "true" : "false");
|
|
68
|
+
return ok(undefined);
|
|
69
|
+
case "int":
|
|
70
|
+
if (!Number.isSafeInteger(value.v) || Math.abs(value.v) > IJSON_MAX) {
|
|
71
|
+
return err("integer_magnitude");
|
|
72
|
+
}
|
|
73
|
+
out.push(String(value.v));
|
|
74
|
+
return ok(undefined);
|
|
75
|
+
case "float":
|
|
76
|
+
out.push(number(value.v));
|
|
77
|
+
return ok(undefined);
|
|
78
|
+
case "str": {
|
|
79
|
+
const escaped = escapeString(value.v);
|
|
80
|
+
if (!escaped.ok)
|
|
81
|
+
return escaped;
|
|
82
|
+
out.push('"' + escaped.v + '"');
|
|
83
|
+
return ok(undefined);
|
|
84
|
+
}
|
|
85
|
+
case "arr": {
|
|
86
|
+
out.push("[");
|
|
87
|
+
for (let i = 0; i < value.v.length; i++) {
|
|
88
|
+
if (i > 0)
|
|
89
|
+
out.push(",");
|
|
90
|
+
const element = walk(value.v[i], out);
|
|
91
|
+
if (!element.ok)
|
|
92
|
+
return element;
|
|
93
|
+
}
|
|
94
|
+
out.push("]");
|
|
95
|
+
return ok(undefined);
|
|
96
|
+
}
|
|
97
|
+
case "obj": {
|
|
98
|
+
for (const [name] of value.v) {
|
|
99
|
+
if (!validString(name))
|
|
100
|
+
return err("invalid_encoding");
|
|
101
|
+
}
|
|
102
|
+
const sorted = [...value.v].sort((a, b) => (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0));
|
|
103
|
+
for (let i = 1; i < sorted.length; i++) {
|
|
104
|
+
if (sorted[i - 1][0] === sorted[i][0])
|
|
105
|
+
return err("duplicate_member");
|
|
106
|
+
}
|
|
107
|
+
out.push("{");
|
|
108
|
+
for (let i = 0; i < sorted.length; i++) {
|
|
109
|
+
if (i > 0)
|
|
110
|
+
out.push(",");
|
|
111
|
+
const key = escapeString(sorted[i][0]);
|
|
112
|
+
if (!key.ok)
|
|
113
|
+
return key;
|
|
114
|
+
out.push('"' + key.v + '":');
|
|
115
|
+
const member = walk(sorted[i][1], out);
|
|
116
|
+
if (!member.ok)
|
|
117
|
+
return member;
|
|
118
|
+
}
|
|
119
|
+
out.push("}");
|
|
120
|
+
return ok(undefined);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function escapeString(s) {
|
|
125
|
+
if (!validString(s))
|
|
126
|
+
return err("invalid_encoding");
|
|
127
|
+
let out = "";
|
|
128
|
+
for (let i = 0; i < s.length; i++) {
|
|
129
|
+
const c = s.charCodeAt(i);
|
|
130
|
+
if (c === 0x22)
|
|
131
|
+
out += '\\"';
|
|
132
|
+
else if (c === 0x5c)
|
|
133
|
+
out += "\\\\";
|
|
134
|
+
else if (SHORT_ESCAPES[c] !== undefined)
|
|
135
|
+
out += "\\" + SHORT_ESCAPES[c];
|
|
136
|
+
else if (c < 0x20)
|
|
137
|
+
out += "\\u" + c.toString(16).padStart(4, "0");
|
|
138
|
+
else
|
|
139
|
+
out += s[i];
|
|
140
|
+
}
|
|
141
|
+
return ok(out);
|
|
142
|
+
}
|
|
143
|
+
// Valid for canonical emission: no lone surrogates. JS strings built by the
|
|
144
|
+
// scanner are already UTF-8-validated; this is the fail-closed guard for
|
|
145
|
+
// directly-constructed values (the Elixir twin's String.valid? check).
|
|
146
|
+
function validString(s) {
|
|
147
|
+
for (let i = 0; i < s.length; i++) {
|
|
148
|
+
const c = s.charCodeAt(i);
|
|
149
|
+
if (c >= 0xd800 && c <= 0xdbff) {
|
|
150
|
+
const next = s.charCodeAt(i + 1);
|
|
151
|
+
if (!(next >= 0xdc00 && next <= 0xdfff))
|
|
152
|
+
return false;
|
|
153
|
+
i++;
|
|
154
|
+
}
|
|
155
|
+
else if (c >= 0xdc00 && c <= 0xdfff) {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|