@aestheticfunction/dspack-emit 0.4.1 → 0.5.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 (64) hide show
  1. package/README.md +16 -6
  2. package/dist/cli.js +56 -0
  3. package/dist/index.d.ts +4 -1
  4. package/dist/index.js +3 -1
  5. package/dist/targets/a2ui/diagnostics.d.ts +110 -0
  6. package/dist/targets/a2ui/diagnostics.js +94 -0
  7. package/dist/targets/a2ui/surface.d.ts +28 -9
  8. package/dist/targets/a2ui/surface.js +566 -224
  9. package/dist/transform/desugar.d.ts +32 -0
  10. package/dist/transform/desugar.js +156 -0
  11. package/dist/transform/emit.js +59 -0
  12. package/dist/transform/index.js +4 -0
  13. package/dist/transform/mapping.d.ts +0 -6
  14. package/dist/transform/mapping.js +50 -0
  15. package/dist/transform/model.d.ts +275 -0
  16. package/dist/transform/model.js +148 -0
  17. package/dist/transform/parse-v2.d.ts +78 -0
  18. package/dist/transform/parse-v2.js +305 -0
  19. package/dist/transform/profile-load.d.ts +7 -6
  20. package/dist/transform/profile-load.js +99 -20
  21. package/dist/transform/profile-schema-v2.d.ts +9 -0
  22. package/dist/transform/profile-schema-v2.js +430 -0
  23. package/dist/transform/profiles.d.ts +31 -1
  24. package/dist/transform/scaffold.d.ts +29 -12
  25. package/dist/transform/scaffold.js +91 -31
  26. package/dist/transform/shadcn-v2-respelling.d.ts +12 -0
  27. package/dist/transform/shadcn-v2-respelling.js +49 -0
  28. package/dist/transform/validate-v2.d.ts +36 -0
  29. package/dist/transform/validate-v2.js +100 -0
  30. package/dist/types.d.ts +19 -0
  31. package/dist/validate/ajv.js +96 -25
  32. package/package.json +2 -2
  33. package/src/__snapshots__/byte-neutral.test.ts.snap +66 -0
  34. package/src/byte-neutral.test.ts +246 -0
  35. package/src/cli.ts +60 -0
  36. package/src/dangling-ref.test.ts +118 -0
  37. package/src/desugar.test.ts +162 -0
  38. package/src/engine-boundary.test.ts +248 -0
  39. package/src/eval-fixture.test.ts +89 -0
  40. package/src/functions.test.ts +182 -0
  41. package/src/index.ts +4 -1
  42. package/src/profile-load.test.ts +99 -1
  43. package/src/profile-v2.test.ts +363 -0
  44. package/src/review-findings.test.ts +292 -0
  45. package/src/schema-mirrors.test.ts +5 -0
  46. package/src/surface-fidelity.test.ts +213 -0
  47. package/src/targets/a2ui/diagnostics.ts +136 -0
  48. package/src/targets/a2ui/surface.ts +920 -242
  49. package/src/transform/desugar.ts +194 -0
  50. package/src/transform/emit.ts +65 -0
  51. package/src/transform/index.ts +5 -0
  52. package/src/transform/mapping.ts +42 -0
  53. package/src/transform/model.ts +325 -0
  54. package/src/transform/parse-v2.ts +397 -0
  55. package/src/transform/profile-load.ts +104 -22
  56. package/src/transform/profile-schema-v2.ts +430 -0
  57. package/src/transform/profile.v2.schema.json +422 -0
  58. package/src/transform/profiles.ts +32 -1
  59. package/src/transform/scaffold.ts +124 -47
  60. package/src/transform/shadcn-v2-respelling.ts +61 -0
  61. package/src/transform/validate-v2.ts +138 -0
  62. package/src/types.ts +20 -0
  63. package/src/validate/ajv.ts +89 -26
  64. package/src/validate-v2.test.ts +173 -0
@@ -0,0 +1,148 @@
1
+ /**
2
+ * Legacy write order. The engine applies routes and collects sorted by this,
3
+ * never by declaration index or traversal happenstance — the same discipline
4
+ * the diagnostics use, applied to output. Object key order is observable
5
+ * (artifacts are hashed), so "when a route writes" is part of the contract and
6
+ * belongs in data rather than in the shape of a method.
7
+ */
8
+ export var WriteOrder;
9
+ (function (WriteOrder) {
10
+ /** Props copied verbatim into same-named destinations; wins over consumption. */
11
+ WriteOrder[WriteOrder["Verbatim"] = 10] = "Verbatim";
12
+ /** Sub-scoped consumption. Resolved by ONE document-order walk (see below). */
13
+ WriteOrder[WriteOrder["Consume"] = 20] = "Consume";
14
+ /** Repetition, and the caption-style routes that belong to the same strategy. */
15
+ WriteOrder[WriteOrder["CollectLead"] = 30] = "CollectLead";
16
+ WriteOrder[WriteOrder["Collect"] = 31] = "Collect";
17
+ /** The node's own text. */
18
+ WriteOrder[WriteOrder["SelfText"] = 40] = "SelfText";
19
+ WriteOrder[WriteOrder["SelfTextChild"] = 41] = "SelfTextChild";
20
+ /** Synthesis with no source in the subtree. */
21
+ WriteOrder[WriteOrder["Action"] = 50] = "Action";
22
+ /** Children as instance references, once every value destination is settled. */
23
+ WriteOrder[WriteOrder["Children"] = 60] = "Children";
24
+ })(WriteOrder || (WriteOrder = {}));
25
+ export const emptySurfaceModel = () => ({
26
+ routes: [],
27
+ collects: [],
28
+ subIdentity: {},
29
+ drops: {},
30
+ consumesSubtree: false,
31
+ });
32
+ /** Narrowing helper: a collect's field may be either shape. */
33
+ export const isCollect = (field) => field.of !== undefined;
34
+ /** Human-readable selector, for warnings and fidelity provenance. */
35
+ export function describeSelector(s) {
36
+ switch (s.kind) {
37
+ case "self-text":
38
+ return "self.text";
39
+ case "self-prop":
40
+ return `self.props.${s.prop}`;
41
+ case "sub-text":
42
+ return `sub(${s.subs.join("|")}).text`;
43
+ case "sub-label":
44
+ return `sub(${s.subs.join("|")}).label`;
45
+ case "sub-text-lift":
46
+ return `sub(${s.subs.join("|")}).firstText`;
47
+ case "subtree-text":
48
+ return `sub(${s.subs.join("|")}).subtreeText`;
49
+ case "children":
50
+ return "children";
51
+ case "synthesized-action":
52
+ return "synthesized.action";
53
+ }
54
+ }
55
+ /** Human-readable destination, for warnings and fidelity provenance. */
56
+ export const describeDestination = (d) => d.kind === "inline" ? "inline" : `${d.kind}:${d.name}`;
57
+ /** Every destination a model writes, for load-time validation against the plan. */
58
+ export function destinationsOf(model) {
59
+ const out = model.routes.map((r) => r.to);
60
+ const walk = (c) => {
61
+ if (c.as.kind !== "inline")
62
+ out.push(c.as);
63
+ for (const field of Object.values(c.item ?? {})) {
64
+ if (isCollect(field))
65
+ walk(field);
66
+ else
67
+ out.push(field.to);
68
+ }
69
+ if (c.scalar)
70
+ out.push(c.scalar.to);
71
+ };
72
+ for (const c of model.collects)
73
+ walk(c);
74
+ return out;
75
+ }
76
+ /**
77
+ * Every sub-component id this model accounts for, and how. This is what makes
78
+ * `subCoverage` derivable instead of authored: a sub is covered when the model
79
+ * consumes it, dissolves it, re-identifies it, or drops it with a reason —
80
+ * and anything else is an unclassified sub, which must fail closed.
81
+ */
82
+ export function referencedSubs(model) {
83
+ const subs = new Map();
84
+ const note = (id, how) => {
85
+ if (!subs.has(id))
86
+ subs.set(id, how);
87
+ };
88
+ const fromSelector = (s, how) => {
89
+ if (s.kind === "sub-text" || s.kind === "sub-label" || s.kind === "sub-text-lift" || s.kind === "subtree-text") {
90
+ for (const id of s.subs)
91
+ note(id, how);
92
+ }
93
+ };
94
+ for (const r of model.routes) {
95
+ for (const s of r.from)
96
+ fromSelector(s, `consumed into ${describeDestination(r.to)}`);
97
+ }
98
+ const walk = (c) => {
99
+ for (const id of c.of)
100
+ note(id, `collected into ${describeDestination(c.as)}`);
101
+ for (const field of Object.values(c.item ?? {})) {
102
+ if (isCollect(field))
103
+ walk(field);
104
+ else
105
+ for (const s of field.from)
106
+ fromSelector(s, `collected into ${describeDestination(field.to)}`);
107
+ }
108
+ if (c.scalar)
109
+ for (const s of c.scalar.from)
110
+ fromSelector(s, `collected as a scalar item`);
111
+ };
112
+ for (const c of model.collects)
113
+ walk(c);
114
+ for (const [id, identity] of Object.entries(model.subIdentity)) {
115
+ note(id, identity.kind === "as-text" ? `re-identified as text (${identity.variant})` : "transparent grouping");
116
+ }
117
+ for (const [id, reason] of Object.entries(model.drops))
118
+ note(id, `dropped: ${reason}`);
119
+ return subs;
120
+ }
121
+ /**
122
+ * Derived sub-component coverage: the honest replacement for the authored
123
+ * `subCoverage` prose, which the engine never reads and which only a test in
124
+ * another repo enforces.
125
+ *
126
+ * A sub is covered when the model NAMES it, or — when the plan absorbs its
127
+ * whole subtree — when it is merely walked through on the way to a named one.
128
+ * That second case is not slack: `alert-dialog-content`, `-header` and
129
+ * `-footer` are real, and a derivation that only counted named subs would
130
+ * report them missing while the emitter handles them correctly.
131
+ *
132
+ * Anything else is unclassified, and must fail closed.
133
+ */
134
+ export function deriveSubCoverage(model, contractSubs) {
135
+ const covered = referencedSubs(model);
136
+ for (const sub of contractSubs) {
137
+ if (covered.has(sub))
138
+ continue;
139
+ if (model.consumesSubtree)
140
+ covered.set(sub, "transparent grouping, traversed by the subtree consumer");
141
+ }
142
+ return covered;
143
+ }
144
+ /** Subs the contract declares that the model cannot account for at all. */
145
+ export const unclassifiedSubs = (model, contractSubs) => {
146
+ const covered = deriveSubCoverage(model, contractSubs);
147
+ return contractSubs.filter((s) => !covered.has(s));
148
+ };
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Profile v2 surface syntax → the internal transformation model.
3
+ *
4
+ * v2 is the first profile language that authors the internal model DIRECTLY:
5
+ * where v1 accumulated ten directives named after collisions (`subText`,
6
+ * `subButtonText`, `subTable`, …), a v2 plan writes Identity / Route / Collect
7
+ * facts in a closed string syntax. This module parses that syntax, derives the
8
+ * things v1 made authors imply (write order, subtree consumption), and REFUSES
9
+ * everything else — a v2 plan that parses is a plan the engine can emit.
10
+ *
11
+ * The grammar is deliberately closed. Selectors and destinations are fixed
12
+ * string forms with two parameterized holes (a prop name, a sub-component id
13
+ * set); there is no expression language, no JSON paths, no predicates, no
14
+ * computed destination names, and no cross-instance routing. Those absences
15
+ * are the design: a plan you cannot write is a plan nobody has to audit.
16
+ *
17
+ * Capability note: v2 in this foundation expresses exactly the transformation
18
+ * surface the engine has today — the v1 semantic surface, respelled. The
19
+ * grammar refuses spellings whose engine support has not landed (plan-level
20
+ * `transparent` identity, `onto` donation, `joinOn` pairing, multi-slot
21
+ * children routing). Each arrives with its capability class (T1–T5), as an
22
+ * addition to this grammar, never as a reinterpretation of it.
23
+ */
24
+ import type { ComponentPlan } from "./profiles.js";
25
+ import { type Destination, type Selector, type SurfaceModel } from "./model.js";
26
+ /** One pathed finding; parse-v2 refusals reuse the loader's issue shape. */
27
+ export interface ParseIssue {
28
+ path: string;
29
+ message: string;
30
+ }
31
+ export declare class ProfileParseError extends Error {
32
+ readonly issues: ParseIssue[];
33
+ constructor(issues: ParseIssue[]);
34
+ }
35
+ export interface SurfaceV2Route {
36
+ /** Ordered fallback chain; the first selector that yields a value wins. */
37
+ from: string[];
38
+ to: string;
39
+ overwrite?: boolean;
40
+ }
41
+ export interface SurfaceV2Collect {
42
+ /** Sections: direct children that open the collection. */
43
+ of: string[];
44
+ /** Array-valued destination (`prop:<name>`). */
45
+ into: string;
46
+ /** `flat`: all rows concatenate into one list. `records`: one record per row. */
47
+ shape: "flat" | "records";
48
+ /** Record field carrying each row's cells; required when shape is `records`. */
49
+ field?: string;
50
+ /** Repetitions inside a section. */
51
+ row: string[];
52
+ /** Cells inside a repetition, flattened to their subtree text. */
53
+ cells: string[];
54
+ }
55
+ export type SurfaceV2Sub = "transparent" | {
56
+ asText: string;
57
+ } | {
58
+ drop: string;
59
+ };
60
+ export interface SurfaceV2 {
61
+ routes?: SurfaceV2Route[];
62
+ collects?: SurfaceV2Collect[];
63
+ /** Identity or disposition of named sub-components met in this node's subtree. */
64
+ subs?: Record<string, SurfaceV2Sub>;
65
+ }
66
+ export declare function parseSelector(raw: string): Selector | null;
67
+ export declare function parseDestination(raw: string): Destination | null;
68
+ /**
69
+ * Parse and statically validate one plan's v2 surface block.
70
+ *
71
+ * `path` roots the pathed findings (e.g. `/components/3/surface`). Validation
72
+ * here needs only the plan itself: destinations against its own declared
73
+ * structural slots and propMap targets, conflict detection under the write
74
+ * discipline, and grammar closure. Contract-facing checks (sub ids exist,
75
+ * self-props are declared, derived coverage is complete) run at transform
76
+ * time, where the contract is in hand — see validate-v2.ts.
77
+ */
78
+ export declare function parseSurfaceV2(surface: SurfaceV2, plan: ComponentPlan, path: string): SurfaceModel;
@@ -0,0 +1,305 @@
1
+ import { WriteOrder, emptySurfaceModel, isCollect, } from "./model.js";
2
+ export class ProfileParseError extends Error {
3
+ issues;
4
+ constructor(issues) {
5
+ super(`profile v2 surface syntax is invalid (${issues.length} issue${issues.length === 1 ? "" : "s"}): ` +
6
+ issues
7
+ .slice(0, 3)
8
+ .map((i) => `${i.path || "$"}: ${i.message}`)
9
+ .join("; ") +
10
+ (issues.length > 3 ? "; …" : ""));
11
+ this.issues = issues;
12
+ this.name = "ProfileParseError";
13
+ }
14
+ }
15
+ // ---------------------------------------------------------------------------
16
+ // Selector / destination string grammar
17
+ // ---------------------------------------------------------------------------
18
+ const ID = "[a-z][a-z0-9-]*";
19
+ const PROP = "[A-Za-z][A-Za-z0-9]*";
20
+ const SELECTOR = new RegExp(`^(self\\.text|self\\.props\\.(${PROP})|sub\\((${ID}(?:\\|${ID})*)\\)\\.(text|label|firstText|subtreeText)|children|synthesized\\.action)$`);
21
+ const DESTINATION = new RegExp(`^(prop|textChild|slot|slots|action):(${PROP})$`);
22
+ export function parseSelector(raw) {
23
+ const m = SELECTOR.exec(raw);
24
+ if (!m)
25
+ return null;
26
+ if (m[1] === "self.text")
27
+ return { kind: "self-text" };
28
+ if (m[1] === "children")
29
+ return { kind: "children" };
30
+ if (m[1] === "synthesized.action")
31
+ return { kind: "synthesized-action" };
32
+ if (m[2])
33
+ return { kind: "self-prop", prop: m[2] };
34
+ const subs = m[3].split("|");
35
+ switch (m[4]) {
36
+ case "text":
37
+ return { kind: "sub-text", subs };
38
+ case "label":
39
+ return { kind: "sub-label", subs };
40
+ case "firstText":
41
+ return { kind: "sub-text-lift", subs };
42
+ default:
43
+ return { kind: "subtree-text", subs };
44
+ }
45
+ }
46
+ export function parseDestination(raw) {
47
+ const m = DESTINATION.exec(raw);
48
+ if (!m)
49
+ return null;
50
+ const name = m[2];
51
+ switch (m[1]) {
52
+ case "prop":
53
+ return { kind: "prop", name };
54
+ case "textChild":
55
+ return { kind: "text-child", name };
56
+ case "slot":
57
+ return { kind: "slot", name };
58
+ case "slots":
59
+ return { kind: "slots", name };
60
+ default:
61
+ return { kind: "action", name };
62
+ }
63
+ }
64
+ // ---------------------------------------------------------------------------
65
+ // Derived write order — v2 authors never write WriteOrder; it is a function
66
+ // of what the route reads and writes, reproducing v1's phase semantics.
67
+ // ---------------------------------------------------------------------------
68
+ function orderOfSelector(s, to) {
69
+ switch (s.kind) {
70
+ case "self-prop":
71
+ return WriteOrder.Verbatim;
72
+ case "sub-text":
73
+ case "sub-label":
74
+ case "sub-text-lift":
75
+ return WriteOrder.Consume;
76
+ case "subtree-text":
77
+ return WriteOrder.CollectLead;
78
+ case "self-text":
79
+ return to.kind === "text-child" ? WriteOrder.SelfTextChild : WriteOrder.SelfText;
80
+ case "synthesized-action":
81
+ return WriteOrder.Action;
82
+ case "children":
83
+ return WriteOrder.Children;
84
+ }
85
+ }
86
+ /** Selector-kind → destination-kind compatibility (closed, refused otherwise). */
87
+ function compatible(s, to) {
88
+ switch (s.kind) {
89
+ case "children":
90
+ return to.kind === "slot" || to.kind === "slots";
91
+ case "synthesized-action":
92
+ return to.kind === "action";
93
+ case "self-text":
94
+ return to.kind === "prop" || to.kind === "text-child";
95
+ default:
96
+ return to.kind === "prop";
97
+ }
98
+ }
99
+ // ---------------------------------------------------------------------------
100
+ // The parser
101
+ // ---------------------------------------------------------------------------
102
+ /**
103
+ * Parse and statically validate one plan's v2 surface block.
104
+ *
105
+ * `path` roots the pathed findings (e.g. `/components/3/surface`). Validation
106
+ * here needs only the plan itself: destinations against its own declared
107
+ * structural slots and propMap targets, conflict detection under the write
108
+ * discipline, and grammar closure. Contract-facing checks (sub ids exist,
109
+ * self-props are declared, derived coverage is complete) run at transform
110
+ * time, where the contract is in hand — see validate-v2.ts.
111
+ */
112
+ export function parseSurfaceV2(surface, plan, path) {
113
+ const issues = [];
114
+ const model = emptySurfaceModel();
115
+ // The plan's own declared destination names: structural slots plus the
116
+ // a2ui-side names its propMap writes. Gate A3 catches an OMITTED property
117
+ // but silently passes an INVENTED one, so this is where invention dies.
118
+ const declared = new Set([
119
+ ...Object.keys(plan.structural ?? {}),
120
+ ...Object.values(plan.propMap ?? {}).map((p) => p.a2ui),
121
+ ]);
122
+ // Write-conflict detection under the declared discipline. Two writers on one
123
+ // destination are LEGAL when they resolve in distinct write orders — the
124
+ // outcome is then fully determined by order plus the overwrite flag (v1's
125
+ // Table does exactly this: a passthrough prop wins over the collect harvest
126
+ // because Verbatim runs first and later writers are first-wins). What is
127
+ // refused is a SAME-order double-claim, where nothing but declaration order
128
+ // would decide the winner — that is ambiguity, not layering.
129
+ const claimed = new Map();
130
+ const claim = (name, order, by, at) => {
131
+ const prior = claimed.get(name) ?? [];
132
+ // The children destination is exclusive in BOTH directions (see the
133
+ // children-route check below for the rationale).
134
+ const children = prior.find((c) => c.order === WriteOrder.Children);
135
+ if (children && order !== WriteOrder.Children) {
136
+ issues.push({
137
+ path: at,
138
+ message: `destination '${name}' is claimed by the children route (${children.by}); instance references do not participate in first-wins layering`,
139
+ });
140
+ }
141
+ const clash = prior.find((c) => c.order === order);
142
+ if (clash) {
143
+ issues.push({
144
+ path: at,
145
+ message: `destination '${name}' is already claimed in the same write phase by ${clash.by}; two same-phase writers would be ordered only by declaration position (fallbacks belong inside a single route's 'from' chain)`,
146
+ });
147
+ }
148
+ prior.push({ order, by });
149
+ claimed.set(name, prior);
150
+ };
151
+ let childrenRoutes = 0;
152
+ for (const [i, r] of (surface.routes ?? []).entries()) {
153
+ const at = `${path}/routes/${i}`;
154
+ const to = parseDestination(r.to);
155
+ if (!to) {
156
+ issues.push({ path: `${at}/to`, message: `'${r.to}' is not a destination (expected prop:|textChild:|slot:|slots:|action: followed by a name)` });
157
+ continue;
158
+ }
159
+ if (!declared.has(to.name)) {
160
+ issues.push({
161
+ path: `${at}/to`,
162
+ message: `destination '${to.name}' is not declared by this plan (declare it in structural or map a prop onto it; inventing properties is refused because gate A3 cannot catch them)`,
163
+ });
164
+ }
165
+ const from = [];
166
+ let order;
167
+ for (const [j, rawSel] of r.from.entries()) {
168
+ const sel = parseSelector(rawSel);
169
+ if (!sel) {
170
+ issues.push({ path: `${at}/from/${j}`, message: `'${rawSel}' is not a selector (closed forms: self.text, self.props.<prop>, sub(<id>|…).<text|label|firstText|subtreeText>, children, synthesized.action)` });
171
+ continue;
172
+ }
173
+ if (!compatible(sel, to)) {
174
+ issues.push({ path: `${at}/from/${j}`, message: `selector '${rawSel}' cannot feed destination kind '${to.kind}'` });
175
+ continue;
176
+ }
177
+ const selOrder = orderOfSelector(sel, to);
178
+ if (order === undefined)
179
+ order = selOrder;
180
+ else if (order !== selOrder) {
181
+ issues.push({
182
+ path: `${at}/from/${j}`,
183
+ message: `fallback chain mixes write phases ('${rawSel}' resolves in a different phase than the first selector); a route resolves in exactly one phase`,
184
+ });
185
+ continue;
186
+ }
187
+ from.push(sel);
188
+ }
189
+ if (from.length === 0) {
190
+ issues.push({ path: `${at}/from`, message: "route has no valid selectors" });
191
+ continue;
192
+ }
193
+ // firstText is a FALLBACK selector: the engine serves it only after a
194
+ // primary label/text selector yielded nothing. As a primary it would
195
+ // parse, derive Consume, and then never resolve — consuming the subtree
196
+ // while writing nothing. Refuse the dead spelling instead of shipping it.
197
+ if (from[0].kind === "sub-text-lift") {
198
+ issues.push({
199
+ path: `${at}/from/0`,
200
+ message: "sub(…).firstText is a fallback selector and cannot lead a route; give the route a primary (sub(…).label or sub(…).text) and put firstText after it",
201
+ });
202
+ continue;
203
+ }
204
+ if (from[0].kind === "children") {
205
+ childrenRoutes++;
206
+ if (from.length > 1) {
207
+ issues.push({ path: `${at}/from`, message: "a children route takes no fallbacks" });
208
+ }
209
+ if (childrenRoutes > 1) {
210
+ issues.push({ path: at, message: "only one children route is supported (multiple named slots arrive with capability class T4)" });
211
+ }
212
+ // A children route produces INSTANCES, not a value: it cannot join
213
+ // first-wins layering, so its destination is exclusively its own. Two
214
+ // writers here would either orphan a synthesized instance in the hashed
215
+ // artifact or silently discard the children — refuse the pair.
216
+ const shared = claimed.get(to.name);
217
+ if (shared && shared.length > 0) {
218
+ issues.push({
219
+ path: `${at}/to`,
220
+ message: `destination '${to.name}' is already claimed by ${shared[0].by}; a children route's destination cannot be layered with value routes (instance references do not participate in first-wins)`,
221
+ });
222
+ }
223
+ }
224
+ claim(to.name, order, `routes/${i}`, `${at}/to`);
225
+ const route = { order: order, from, to, origin: `v2:routes/${i}` };
226
+ if (r.overwrite)
227
+ route.overwrite = true;
228
+ model.routes.push(route);
229
+ }
230
+ for (const [i, c] of (surface.collects ?? []).entries()) {
231
+ const at = `${path}/collects/${i}`;
232
+ const into = parseDestination(c.into);
233
+ if (!into || into.kind !== "prop") {
234
+ issues.push({ path: `${at}/into`, message: `'${c.into}' is not an array-valued destination (collects write prop:<name>)` });
235
+ continue;
236
+ }
237
+ if (!declared.has(into.name)) {
238
+ issues.push({ path: `${at}/into`, message: `destination '${into.name}' is not declared by this plan` });
239
+ }
240
+ if (c.shape === "records" && !c.field) {
241
+ issues.push({ path: `${at}/field`, message: "a records collect names the field its rows' cells land in" });
242
+ continue;
243
+ }
244
+ claim(into.name, WriteOrder.Collect, `collects/${i}`, `${at}/into`);
245
+ const origin = `v2:collects/${i}`;
246
+ const inner = {
247
+ order: WriteOrder.Collect,
248
+ flatten: false,
249
+ of: c.row,
250
+ as: { kind: "inline" },
251
+ scalar: {
252
+ order: WriteOrder.Collect,
253
+ from: [{ kind: "subtree-text", subs: c.cells }],
254
+ to: { kind: "prop", name: c.field ?? "cells" },
255
+ origin,
256
+ },
257
+ origin,
258
+ };
259
+ model.collects.push({
260
+ order: WriteOrder.Collect,
261
+ flatten: c.shape === "flat",
262
+ of: c.of,
263
+ as: into,
264
+ item: { [c.field ?? "cells"]: inner },
265
+ origin,
266
+ });
267
+ }
268
+ for (const [sub, disposition] of Object.entries(surface.subs ?? {})) {
269
+ const at = `${path}/subs/${sub}`;
270
+ if (disposition === "transparent") {
271
+ model.subIdentity[sub] = { kind: "transparent" };
272
+ }
273
+ else if ("asText" in disposition) {
274
+ model.subIdentity[sub] = { kind: "as-text", variant: disposition.asText };
275
+ }
276
+ else {
277
+ model.drops[sub] = disposition.drop;
278
+ }
279
+ // A sub with an identity/drop must not also be a route or collect source —
280
+ // two dispositions for one sub is a contradiction, not a preference.
281
+ const alsoRouted = model.routes.some((r) => r.from.some((s) => "subs" in s && s.subs.includes(sub)));
282
+ const inCollect = (col) => col.of.includes(sub) ||
283
+ Boolean(col.scalar?.from.some((s) => "subs" in s && s.subs.includes(sub))) ||
284
+ Object.values(col.item ?? {}).some((f) => (isCollect(f) ? inCollect(f) : f.from.some((s) => "subs" in s && s.subs.includes(sub))));
285
+ const alsoCollected = model.collects.some(inCollect);
286
+ if (alsoRouted || alsoCollected) {
287
+ issues.push({ path: at, message: `sub-component '${sub}' has a disposition here and is also consumed by a ${alsoRouted ? "route" : "collect"}; one sub, one disposition` });
288
+ }
289
+ }
290
+ // Derived, never authored: a plan that reads its subtree into props does not
291
+ // also emit that subtree as children.
292
+ model.consumesSubtree =
293
+ model.collects.length > 0 ||
294
+ model.routes.some((r) => r.order === WriteOrder.Consume || r.order === WriteOrder.CollectLead);
295
+ const childrenRoute = model.routes.find((r) => r.from[0].kind === "children");
296
+ if (model.consumesSubtree && childrenRoute) {
297
+ issues.push({
298
+ path,
299
+ message: "this plan both consumes its subtree into props and routes children as instances; the two are contradictory (the consumed subtree has no children left to emit)",
300
+ });
301
+ }
302
+ if (issues.length > 0)
303
+ throw new ProfileParseError(issues);
304
+ return model;
305
+ }
@@ -1,6 +1,7 @@
1
1
  import type { Profile } from "./profiles.js";
2
- /** The profile schema, as shipped (also exported for hosts that surface it in UI). */
2
+ /** The profile schemas, as shipped (also exported for hosts that surface them in UI). */
3
3
  export { profileSchema } from "./profile-schema.js";
4
+ export { profileSchemaV2 } from "./profile-schema-v2.js";
4
5
  export interface ProfileLoadIssue {
5
6
  /** JSON path into the offending document (ajv instancePath). */
6
7
  path: string;
@@ -8,12 +9,12 @@ export interface ProfileLoadIssue {
8
9
  }
9
10
  export declare class ProfileLoadError extends Error {
10
11
  readonly issues: ProfileLoadIssue[];
11
- constructor(issues: ProfileLoadIssue[]);
12
+ constructor(issues: ProfileLoadIssue[], schemaName?: string);
12
13
  }
13
14
  /**
14
- * Validate `json` as a v1 profile document and return the engine-ready
15
- * `Profile`. The `profileVersion` envelope field is stripped — it belongs to
16
- * the document format, not the engine. Top-level `x-` extension keys are
17
- * preserved (the engine ignores them; provenance tools may not).
15
+ * Validate `json` as a profile document and return the engine-ready `Profile`.
16
+ * The `profileVersion` envelope field is stripped — it belongs to the document
17
+ * format, not the engine. Top-level `x-` extension keys are preserved (the
18
+ * engine ignores them; provenance tools may not).
18
19
  */
19
20
  export declare function loadProfile(json: unknown): Profile;