@aestheticfunction/dspack-emit 0.6.0 → 0.8.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 (41) hide show
  1. package/README.md +23 -15
  2. package/dist/cli.js +4 -2
  3. package/dist/index.d.ts +1 -1
  4. package/dist/index.js +1 -1
  5. package/dist/targets/a2ui/surface.js +108 -4
  6. package/dist/transform/emit-v1_0.d.ts +45 -0
  7. package/dist/transform/emit-v1_0.js +190 -0
  8. package/dist/transform/emit.d.ts +6 -9
  9. package/dist/transform/emit.js +5 -44
  10. package/dist/transform/index.js +3 -1
  11. package/dist/types.d.ts +4 -0
  12. package/dist/validate/ajv.d.ts +26 -0
  13. package/dist/validate/ajv.js +150 -23
  14. package/dist/validate/meta/a2ui-v1_0.d.ts +9 -0
  15. package/dist/validate/meta/a2ui-v1_0.js +811 -0
  16. package/dist/validate/meta/catalog-meta.js +34 -21
  17. package/package.json +6 -2
  18. package/src/byte-neutral.test.ts +6 -3
  19. package/src/cli.ts +5 -2
  20. package/src/engine-boundary.test.ts +10 -0
  21. package/src/functions.test.ts +48 -1
  22. package/src/index.ts +1 -1
  23. package/src/instance-diagnostics.test.ts +194 -0
  24. package/src/profile-v2.test.ts +1 -1
  25. package/src/review-findings.test.ts +4 -1
  26. package/src/surface-emit.test.ts +43 -11
  27. package/src/surface-fidelity.test.ts +12 -0
  28. package/src/t1.test.ts +9 -8
  29. package/src/targets/a2ui/surface.ts +124 -4
  30. package/src/transform/emit-v1_0.ts +215 -0
  31. package/src/transform/emit.ts +10 -54
  32. package/src/transform/index.ts +3 -1
  33. package/src/transform.test.ts +33 -1
  34. package/src/types.ts +4 -0
  35. package/src/v1_0-catalog.test.ts +267 -0
  36. package/src/validate/ajv.ts +169 -22
  37. package/src/validate/meta/a2ui-catalog.meta.1_0.json +67 -13
  38. package/src/validate/meta/a2ui-v1_0.ts +820 -0
  39. package/src/validate/meta/catalog-meta.ts +34 -21
  40. package/dist/validate/meta/a2ui-catalog.meta.0_9_1.json +0 -39
  41. package/dist/validate/meta/a2ui-catalog.meta.1_0.json +0 -34
@@ -4,12 +4,38 @@ export interface GateResult {
4
4
  pass: boolean;
5
5
  detail: string;
6
6
  errors?: string[];
7
+ /**
8
+ * Structured per-instance evidence for gate 3 failures (advanced channel
9
+ * for downstream UIs). The strings in `errors` remain the primary
10
+ * user-facing form; this preserves ajv's keyword/params/schemaPath, which
11
+ * the strings deliberately compress.
12
+ */
13
+ errorDetails?: Array<{
14
+ /** `component#id` — same prefix the string form carries. */
15
+ instance: string;
16
+ component: string;
17
+ id: string;
18
+ errors: Array<{
19
+ instancePath?: string;
20
+ schemaPath?: string;
21
+ keyword?: string;
22
+ params?: unknown;
23
+ message?: string;
24
+ }>;
25
+ }>;
7
26
  }
8
27
  export interface ValidationReport {
9
28
  version: A2uiVersion;
10
29
  pass: boolean;
11
30
  gates: GateResult[];
12
31
  }
32
+ /**
33
+ * The schemas a consumer must register beside a catalog before compiling it with its own JSON Schema validator.
34
+ * A 0.9.1 catalog is self-contained: none. A 1.0 catalog references the standard types as `common_types.json#/$defs/…`
35
+ * (specification rule 3), so it needs the pinned common_types at the URL that reference resolves to; the specification
36
+ * forbids fetching it. For example, with ajv: `for (const s of catalogCompanionSchemas(catalog)) ajv.addSchema(s)`.
37
+ */
38
+ export declare function catalogCompanionSchemas(catalog: A2uiCatalog): Json[];
13
39
  /** Pull every component instance (object with string `component` + `id`) from a surface. */
14
40
  export declare function extractInstances(surface: unknown, acc?: Json[]): Json[];
15
41
  export declare function validateCatalog(catalog: A2uiCatalog, version: A2uiVersion, surface?: unknown): ValidationReport;
@@ -12,11 +12,16 @@
12
12
  * a2ui-catalog.meta.<ver>.json (the literal "catalog schema" check; this is what
13
13
  * makes v0.9.1 vs v1.0 conformance distinct — theme vs surfaceProperties).
14
14
  * 3. instance: every component instance in the hand-authored surface validates
15
- * against the catalog's own #/$defs/anyComponent.
15
+ * against the catalog's own #/$defs/anyComponent — checked by manual
16
+ * discrimination (each instance against its own #/components/<name>
17
+ * branch), which is semantically equivalent and keeps every reported
18
+ * error inside the instance's own branch. See the gate 3 body.
16
19
  */
17
20
  import Ajv2020 from "ajv/dist/2020.js";
18
21
  import addFormats from "ajv-formats";
19
22
  import { catalogMetaSchemas } from "./meta/catalog-meta.js";
23
+ import { catalogDefinitionV1_0, commonTypesV1_0 } from "./meta/a2ui-v1_0.js";
24
+ import { COMMON_TYPES_V1_0 } from "../transform/emit-v1_0.js";
20
25
  function newAjv() {
21
26
  const ajv = new Ajv2020({ strict: false, allErrors: true, validateFormats: true });
22
27
  addFormats(ajv);
@@ -38,6 +43,32 @@ function externalRefs(node, acc = []) {
38
43
  }
39
44
  return acc;
40
45
  }
46
+ /**
47
+ * A2UI 1.0: the one external reference a catalog may make is to a listed standard type in common_types.json, written
48
+ * relative (`common_types.json#/$defs/<Type>`); anything else makes the catalog invalid (specification rule 3).
49
+ */
50
+ const COMMON_REF = /^common_types\.json#\/\$defs\/([A-Za-z]+)$/;
51
+ function allowedV1_0Ref(ref) {
52
+ const m = COMMON_REF.exec(ref);
53
+ return !!m && COMMON_TYPES_V1_0.includes(m[1]);
54
+ }
55
+ /**
56
+ * The vendored 1.0 common_types, registered where a catalog's relative `common_types.json` reference resolves (beside
57
+ * the catalog's own $id). Its `catalog.json#/$defs/anyFunction` then resolves to that same catalog, offline: the
58
+ * specification forbids fetching either over the network.
59
+ */
60
+ function commonTypesBeside(catalogId) {
61
+ return { ...structuredClone(commonTypesV1_0), $id: new URL("common_types.json", catalogId).href };
62
+ }
63
+ /**
64
+ * The schemas a consumer must register beside a catalog before compiling it with its own JSON Schema validator.
65
+ * A 0.9.1 catalog is self-contained: none. A 1.0 catalog references the standard types as `common_types.json#/$defs/…`
66
+ * (specification rule 3), so it needs the pinned common_types at the URL that reference resolves to; the specification
67
+ * forbids fetching it. For example, with ajv: `for (const s of catalogCompanionSchemas(catalog)) ajv.addSchema(s)`.
68
+ */
69
+ export function catalogCompanionSchemas(catalog) {
70
+ return catalog.protocolVersion === "1.0" ? [commonTypesBeside(catalog.$id)] : [];
71
+ }
41
72
  /**
42
73
  * Collect every INTERNAL `$ref` that does not resolve inside the catalog
43
74
  * document, with the JSON path of the offending `$ref` keyword.
@@ -110,13 +141,18 @@ export function extractInstances(surface, acc = []) {
110
141
  }
111
142
  export function validateCatalog(catalog, version, surface) {
112
143
  const gates = [];
113
- // Gate 1 — schema compile + no external $ref + every internal $ref resolves.
114
- const exts = externalRefs(catalog);
144
+ // Gate 1 — schema compile + no external $ref + every internal $ref resolves. In 1.0 the listed standard types in
145
+ // common_types.json are the one permitted external target, resolved against the vendored copy.
146
+ const v1 = version === "1.0";
147
+ const exts = externalRefs(catalog).filter((r) => !(v1 && allowedV1_0Ref(r)));
115
148
  const dangling = danglingInternalRefs(catalog);
116
149
  let compiled = false;
117
150
  let compileErr = "";
118
151
  try {
119
- newAjv().compile(catalog);
152
+ const ajv = newAjv();
153
+ if (v1)
154
+ ajv.addSchema(commonTypesBeside(catalog.$id));
155
+ ajv.compile(catalog);
120
156
  compiled = true;
121
157
  }
122
158
  catch (e) {
@@ -127,7 +163,9 @@ export function validateCatalog(catalog, version, surface) {
127
163
  name: "schema-compile + no-external-ref",
128
164
  pass: selfContained,
129
165
  detail: selfContained
130
- ? "Catalog compiles as a draft-2020-12 JSON Schema; every $ref is internal and resolves."
166
+ ? v1
167
+ ? "Catalog compiles as a draft-2020-12 JSON Schema; every $ref is internal or a listed common_types.json type, and resolves."
168
+ : "Catalog compiles as a draft-2020-12 JSON Schema; every $ref is internal and resolves."
131
169
  : !compiled
132
170
  ? `ajv failed to compile the catalog as a schema: ${compileErr}`
133
171
  : exts.length
@@ -135,17 +173,32 @@ export function validateCatalog(catalog, version, surface) {
135
173
  : `Catalog is not self-contained: ${dangling.length} internal $ref(s) do not resolve.`,
136
174
  errors: exts.length ? [...new Set(exts)] : dangling.length ? dangling : undefined,
137
175
  });
138
- // Gate 2 — catalog shape.
176
+ // Gate 2 — catalog shape. For 1.0 that is the official catalog definition (vendored, pinned) as well as dspack's own
177
+ // invariants; for 0.9.1, the derived meta-schema as before.
139
178
  const meta = catalogMetaSchemas[version];
140
179
  const validateShape = newAjv().compile(meta);
141
- const shapeOk = validateShape(catalog);
180
+ let shapeOk = validateShape(catalog);
181
+ const shapeErrors = shapeOk ? [] : (validateShape.errors ?? []).map(fmtErr);
182
+ if (v1) {
183
+ const official = newAjv();
184
+ official.addSchema(commonTypesV1_0);
185
+ const validateOfficial = official.compile(catalogDefinitionV1_0);
186
+ if (!validateOfficial(catalog)) {
187
+ shapeOk = false;
188
+ shapeErrors.push(...(validateOfficial.errors ?? []).map((e) => `catalog_definition: ${fmtErr(e)}`));
189
+ }
190
+ }
142
191
  gates.push({
143
192
  name: "catalog-shape",
144
193
  pass: shapeOk,
145
194
  detail: shapeOk
146
- ? `Catalog satisfies the A2UI v${version} catalog-shape meta-schema.`
147
- : `Catalog violates the v${version} catalog-shape meta-schema.`,
148
- errors: shapeOk ? undefined : (validateShape.errors ?? []).map(fmtErr),
195
+ ? v1
196
+ ? "Catalog satisfies the official A2UI v1.0 catalog definition and dspack's v1.0 invariants."
197
+ : `Catalog satisfies the A2UI v${version} catalog-shape meta-schema.`
198
+ : v1
199
+ ? "Catalog violates the official A2UI v1.0 catalog definition or dspack's v1.0 invariants."
200
+ : `Catalog violates the v${version} catalog-shape meta-schema.`,
201
+ errors: shapeOk ? undefined : shapeErrors,
149
202
  });
150
203
  // Gate 3 — instances (only if the catalog is usable and a surface was supplied).
151
204
  if (surface !== undefined) {
@@ -162,22 +215,73 @@ export function validateCatalog(catalog, version, surface) {
162
215
  // Belt and braces: gate 1 has already proven every ref resolves, but a
163
216
  // raw ajv throw must never escape this function regardless.
164
217
  const failures = [];
218
+ const errorDetails = [];
165
219
  let instances = [];
166
220
  try {
167
221
  const ajv = newAjv();
222
+ const commonTypes = v1 ? commonTypesBeside(catalog.$id) : undefined;
223
+ if (commonTypes)
224
+ ajv.addSchema(commonTypes);
168
225
  ajv.addSchema(catalog, catalog.$id);
169
- const validateAny = ajv.getSchema(`${catalog.$id}#/$defs/anyComponent`);
170
226
  instances = extractInstances(surface);
171
- if (!validateAny) {
172
- failures.push("Could not resolve #/$defs/anyComponent from the catalog.");
173
- }
174
- else {
175
- for (const inst of instances) {
176
- if (!validateAny(inst)) {
177
- const where = `${inst.component}#${inst.id}`;
178
- for (const e of validateAny.errors ?? [])
179
- failures.push(`${where}: ${fmtErr(e)}`);
180
- }
227
+ // Manual discrimination instead of the flat `#/$defs/anyComponent`
228
+ // oneOf. Semantically equivalent: every branch of the emitted
229
+ // anyComponent is `{$ref: "#/components/<name>"}` and each component
230
+ // schema pins `component: {const: <name>}` inside its allOf (and
231
+ // requires it), so an instance tagged component X can only ever match
232
+ // branch X — validating the instance against ITS OWN branch accepts
233
+ // and rejects exactly the same instances the oneOf does. What changes
234
+ // is the report: allErrors WITHIN the matching branch (rigor
235
+ // preserved, every genuine error surfaced) without echoing every
236
+ // other branch's const/required/unevaluatedProperties noise.
237
+ const admitted = Object.keys(catalog.components ?? {});
238
+ for (const inst of instances) {
239
+ const comp = String(inst.component);
240
+ const id = String(inst.id);
241
+ const where = `${comp}#${id}`;
242
+ if (!admitted.includes(comp)) {
243
+ failures.push(`${where}: component '${comp}' is not in this catalog (${admitted.length} components admitted)`);
244
+ errorDetails.push({
245
+ instance: where,
246
+ component: comp,
247
+ id,
248
+ errors: [
249
+ {
250
+ message: `component '${comp}' is not in this catalog`,
251
+ params: { admittedComponents: admitted },
252
+ },
253
+ ],
254
+ });
255
+ continue;
256
+ }
257
+ // A 1.0 component is validated the way the envelope composes it (agent_to_renderer.json `Component`):
258
+ // ComponentCommon plus the catalog component, with no other property. A 0.9.1 component carries its own.
259
+ const validateBranch = commonTypes
260
+ ? ajv.compile({
261
+ allOf: [{ $ref: `${commonTypes.$id}#/$defs/ComponentCommon` }, { $ref: `${catalog.$id}#/components/${comp}` }],
262
+ unevaluatedProperties: false,
263
+ })
264
+ : ajv.getSchema(`${catalog.$id}#/components/${comp}`);
265
+ if (!validateBranch) {
266
+ failures.push(`${where}: could not resolve #/components/${comp} from the catalog.`);
267
+ continue;
268
+ }
269
+ if (!validateBranch(inst)) {
270
+ const errs = validateBranch.errors ?? [];
271
+ for (const e of errs)
272
+ failures.push(`${where}: ${fmtErr(e)}`);
273
+ errorDetails.push({
274
+ instance: where,
275
+ component: comp,
276
+ id,
277
+ errors: errs.map((e) => ({
278
+ instancePath: e.instancePath,
279
+ schemaPath: e.schemaPath,
280
+ keyword: e.keyword,
281
+ params: e.params,
282
+ message: e.message,
283
+ })),
284
+ });
181
285
  }
182
286
  }
183
287
  }
@@ -188,14 +292,37 @@ export function validateCatalog(catalog, version, surface) {
188
292
  name: "instance",
189
293
  pass: failures.length === 0,
190
294
  detail: failures.length === 0
191
- ? `All ${instances.length} surface component instance(s) validate against #/$defs/anyComponent.`
295
+ ? version === "1.0"
296
+ ? `All ${instances.length} surface component instance(s) validate as the 1.0 envelope composes them: common_types.json ComponentCommon with the catalog component, closed.`
297
+ : `All ${instances.length} surface component instance(s) validate against #/$defs/anyComponent.`
192
298
  : `${failures.length} instance validation error(s).`,
193
299
  errors: failures.length ? failures : undefined,
300
+ ...(errorDetails.length ? { errorDetails } : {}),
194
301
  });
195
302
  }
196
303
  }
197
304
  return { version, pass: gates.every((g) => g.pass), gates };
198
305
  }
306
+ /**
307
+ * One ajv error as a user-facing line: instancePath + message, enriched from
308
+ * the error's params where they carry the actionable detail ajv's message
309
+ * omits — the allowed enum values, the expected const, or the name of the
310
+ * offending unevaluated/additional property.
311
+ */
199
312
  function fmtErr(e) {
200
- return `${e.instancePath || "(root)"} ${e.message ?? ""}`.trim();
313
+ const params = (e.params ?? {});
314
+ let out = `${e.instancePath || "(root)"} ${e.message ?? ""}`.trim();
315
+ if (e.keyword === "enum" && Array.isArray(params.allowedValues)) {
316
+ out += ` (allowed: ${params.allowedValues.map((v) => String(v)).join(", ")})`;
317
+ }
318
+ else if (e.keyword === "const" && "allowedValue" in params) {
319
+ out += ` (expected: ${JSON.stringify(params.allowedValue)})`;
320
+ }
321
+ else if (e.keyword === "unevaluatedProperties" && typeof params.unevaluatedProperty === "string") {
322
+ out += ` ('${params.unevaluatedProperty}')`;
323
+ }
324
+ else if (e.keyword === "additionalProperties" && typeof params.additionalProperty === "string") {
325
+ out += ` ('${params.additionalProperty}')`;
326
+ }
327
+ return out;
201
328
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Runtime mirrors of the pinned A2UI 1.0 schemas (fixtures/a2ui/PIN.md): the official catalog definition, which is the
3
+ * 1.0 catalog-shape gate, and the final common_types, which a 1.0 catalog references for every standard type. They are
4
+ * constants because validation must also run in a browser bundle; src/schema-mirrors.test.ts keeps each identical to
5
+ * its vendored file. Do not edit by hand: re-vendor the fixture and regenerate this module.
6
+ */
7
+ import type { Json } from "../../types.js";
8
+ export declare const catalogDefinitionV1_0: Json;
9
+ export declare const commonTypesV1_0: Json;