@aestheticfunction/dspack-emit 0.5.0 → 0.6.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.
@@ -13,6 +13,18 @@ export function mapDspack(doc, profile) {
13
13
  const fidelity = [];
14
14
  const warnings = [];
15
15
  const addComponent = (plan) => {
16
+ // T1: a transparent plan emits no catalog entry — nothing can instantiate
17
+ // a component that dissolves at emission. Its disposition is recorded in
18
+ // coverage; its absence from anyComponent is what keeps A3 honest.
19
+ if (surfaceModelOf(plan).transparent) {
20
+ fidelity.push({
21
+ source: `plan.${plan.dspackId ?? plan.a2ui}`,
22
+ target: "(no catalog entry)",
23
+ class: "synthesis-defaults",
24
+ note: "transparent identity: dissolves at emission; children rise to the parent's destination.",
25
+ });
26
+ return;
27
+ }
16
28
  const dspackComp = plan.dspackId ? doc.components?.[plan.dspackId] : undefined;
17
29
  if (plan.dspackId && !dspackComp) {
18
30
  warnings.push({
@@ -65,10 +77,13 @@ export function mapDspack(doc, profile) {
65
77
  }
66
78
  function computeCoverage(doc, profile, warnings, fidelity) {
67
79
  const mapped = new Map(); // dspackId -> A2UI name
80
+ const byId = new Map();
68
81
  // Both `components` and any `synthesized` plan that declares a `dspackId` count as mapped.
69
82
  for (const p of [...profile.components, ...profile.synthesized]) {
70
- if (p.dspackId)
83
+ if (p.dspackId) {
71
84
  mapped.set(p.dspackId, p.a2ui);
85
+ byId.set(p.dspackId, p);
86
+ }
72
87
  }
73
88
  const casualties = new Map();
74
89
  for (const c of profile.casualtyComponents)
@@ -77,7 +92,11 @@ function computeCoverage(doc, profile, warnings, fidelity) {
77
92
  const coverage = [];
78
93
  for (const id of Object.keys(doc.components ?? {})) {
79
94
  if (mapped.has(id)) {
80
- coverage.push({ id, disposition: "mapped", detail: `-> ${mapped.get(id)}` });
95
+ coverage.push({
96
+ id,
97
+ disposition: "mapped",
98
+ detail: byId.get(id) && surfaceModelOf(byId.get(id)).transparent ? "-> (transparent: dissolves at emission)" : `-> ${mapped.get(id)}`,
99
+ });
81
100
  }
82
101
  else if (omitted.has(id)) {
83
102
  coverage.push({ id, disposition: "omitted", detail: "declared in profile.intentionallyOmitted" });
@@ -113,11 +132,6 @@ function computeCoverage(doc, profile, warnings, fidelity) {
113
132
  // validateProfileAgainstContract refuses them at entry. Measured before
114
133
  // enforcement: pinned shadcn v2.3.0, astryx and acme all derive zero
115
134
  // unresolved subs, so no shipped artifact changes.
116
- const byId = new Map();
117
- for (const p of [...profile.components, ...profile.synthesized]) {
118
- if (p.dspackId)
119
- byId.set(p.dspackId, p);
120
- }
121
135
  for (const [id, component] of Object.entries(doc.components ?? {})) {
122
136
  const subs = component.composition?.subComponents ?? [];
123
137
  if (subs.length === 0)
@@ -54,15 +54,40 @@ export type Identity =
54
54
  kind: "mapped";
55
55
  a2ui: string;
56
56
  }
57
- /** Emits nothing; its children rise to the destination its parent would use. */
57
+ /**
58
+ * Emits nothing; its children rise to the destination its parent would use.
59
+ * A transparent identity may carry DONATIONS (T1's constrained control
60
+ * donation): each dissolved instance harvests the donated values from its
61
+ * own subtree and hands them to the single eligible emitted control within
62
+ * that subtree — the boundary IS the scope, which is what makes a
63
+ * three-field form donate three labels to three inputs instead of refusing.
64
+ */
58
65
  | {
59
66
  kind: "transparent";
67
+ donate?: Donation[];
60
68
  }
61
69
  /** Emits as the profile's synthesized text primitive, carrying its subtree text. */
62
70
  | {
63
71
  kind: "as-text";
64
72
  variant: string;
65
73
  };
74
+ /**
75
+ * One constrained control donation (the ratified `onto: "control"` mechanism):
76
+ * a value harvested inside a dissolving boundary, written onto the boundary's
77
+ * single eligible control. Fail-closed at emit: zero or multiple eligible
78
+ * controls refuse; the receiving plan must declare the destination. The write
79
+ * is carried EXPLICITLY in the rewritten child list and applied as a pre-write
80
+ * when the control's own emission constructs its instance — never by mutating
81
+ * another node's committed instance mid-traversal.
82
+ */
83
+ export interface Donation {
84
+ /** Where the value comes from, inside the dissolving boundary. */
85
+ from: Selector;
86
+ /** The receiving prop on the control (`prop:` destinations only). */
87
+ to: Destination;
88
+ /** Which authored rule produced this — provenance for the ledger. */
89
+ origin: string;
90
+ }
66
91
  /**
67
92
  * Where information comes from. A closed enumeration: every selector is
68
93
  * statically checkable against the contract before emission runs.
@@ -108,6 +133,27 @@ export type Selector =
108
133
  | {
109
134
  kind: "children";
110
135
  }
136
+ /**
137
+ * T4: the ordered children of exactly one descendant instance of the named
138
+ * sub, emitted as instances into a slot. The sub is the compound's declared
139
+ * structural region (dialog-content, sheet-footer, …); its children go
140
+ * through the same rewriting as any parent's child list, so nested subs
141
+ * resolve by THIS plan's dispositions and claimed subs are skipped. Zero
142
+ * instances leave the destination absent (gate A3 arbitrates); more than
143
+ * one refuses — a slot names one region, never a series.
144
+ */
145
+ | {
146
+ kind: "sub-children";
147
+ subs: string[];
148
+ }
149
+ /**
150
+ * T2: the node's own surface `id`. Item-local — only valid inside a
151
+ * collect's item fields, where each repeated item contributes the one
152
+ * datum it carries (the production radio-group-item carries nothing else).
153
+ */
154
+ | {
155
+ kind: "self-id";
156
+ }
111
157
  /** No source: a declarative A2UI action synthesized from a deterministic slug. */
112
158
  | {
113
159
  kind: "synthesized-action";
@@ -164,6 +210,8 @@ export declare enum WriteOrder {
164
210
  /** The node's own text. */
165
211
  SelfText = 40,
166
212
  SelfTextChild = 41,
213
+ /** T4 slot routes: instance emission from a named sub's children. */
214
+ Slot = 45,
167
215
  /** Synthesis with no source in the subtree. */
168
216
  Action = 50,
169
217
  /** Children as instance references, once every value destination is settled. */
@@ -195,6 +243,21 @@ export interface Route {
195
243
  export interface Collect {
196
244
  /** When this collect writes, relative to every other route and collect. */
197
245
  order: WriteOrder;
246
+ /**
247
+ * T2 item-mode: each descendant matching `of` becomes ONE RECORD whose
248
+ * fields resolve against that item's own node (self.text, self.props.X,
249
+ * self.id — never joins, never siblings). Mutually exclusive with the
250
+ * table-mode `item`/`scalar` nesting.
251
+ */
252
+ fields?: Record<string, Selector>;
253
+ /**
254
+ * T3: a DECLARED key join onto a sibling sub-family. Each left item's key
255
+ * must match exactly one counterpart (0..1 when `optional`); zero, several,
256
+ * duplicate keys on either side, missing key values, and dangling
257
+ * counterparts all refuse. The profile names the relation; the emitter
258
+ * never infers one — no positions, no nearest-sibling, no similarity.
259
+ */
260
+ join?: CollectJoin;
198
261
  /**
199
262
  * True when every repetition's cells concatenate into ONE flat list (v1's
200
263
  * header semantics: two header rows append into a single column list);
@@ -223,9 +286,39 @@ export interface Collect {
223
286
  * Sub-components this plan explicitly discards, with the authored reason. v1
224
287
  * spelled this inside `subTable.drops`; it is not a table concept.
225
288
  */
289
+ export interface CollectJoin {
290
+ /** The joined sub-family (sub-component or component ids). */
291
+ with: string[];
292
+ /** Key extracted from each LEFT item (item-local selector). */
293
+ leftKey: Selector;
294
+ /** Key extracted from each JOINED item (item-local selector). */
295
+ rightKey: Selector;
296
+ /**
297
+ * Record fields sourced from the JOINED side. Item-local selectors, plus
298
+ * the slot-valued `{ kind: "joined-children" }`: the counterpart's children
299
+ * emit as instances and the record carries the reference — Repetition's
300
+ * ratified slot-valued field, NOT T4 multi-slot routing.
301
+ */
302
+ fields: Record<string, Selector | {
303
+ kind: "joined-children";
304
+ }>;
305
+ /** 0..1 counterparts allowed when true; exactly one required otherwise. */
306
+ optional: boolean;
307
+ /** Which authored rule produced this — provenance for the ledger. */
308
+ origin: string;
309
+ }
226
310
  export type Drops = Record<string, string>;
227
311
  /** The whole of how a plan projects a surface node. Replaces SurfacePlanDirectives. */
228
312
  export interface SurfaceModel {
313
+ /**
314
+ * T1: this plan's component emits NO instance — every node using it
315
+ * dissolves at its parent's child-collection, children rising in place,
316
+ * with `transparentDonate` harvested per dissolved instance. Refused for
317
+ * the surface root at emit (renderers need components[0] = "root").
318
+ */
319
+ transparent: boolean;
320
+ /** Donations carried by a transparent PLAN (per dissolved instance). */
321
+ transparentDonate: Donation[];
229
322
  routes: Route[];
230
323
  collects: Collect[];
231
324
  /** Identity assigned to named sub-components met inside this node's subtree. */
@@ -17,12 +17,16 @@ export var WriteOrder;
17
17
  /** The node's own text. */
18
18
  WriteOrder[WriteOrder["SelfText"] = 40] = "SelfText";
19
19
  WriteOrder[WriteOrder["SelfTextChild"] = 41] = "SelfTextChild";
20
+ /** T4 slot routes: instance emission from a named sub's children. */
21
+ WriteOrder[WriteOrder["Slot"] = 45] = "Slot";
20
22
  /** Synthesis with no source in the subtree. */
21
23
  WriteOrder[WriteOrder["Action"] = 50] = "Action";
22
24
  /** Children as instance references, once every value destination is settled. */
23
25
  WriteOrder[WriteOrder["Children"] = 60] = "Children";
24
26
  })(WriteOrder || (WriteOrder = {}));
25
27
  export const emptySurfaceModel = () => ({
28
+ transparent: false,
29
+ transparentDonate: [],
26
30
  routes: [],
27
31
  collects: [],
28
32
  subIdentity: {},
@@ -48,6 +52,10 @@ export function describeSelector(s) {
48
52
  return `sub(${s.subs.join("|")}).subtreeText`;
49
53
  case "children":
50
54
  return "children";
55
+ case "sub-children":
56
+ return `sub(${s.subs.join("|")}).children`;
57
+ case "self-id":
58
+ return "self.id";
51
59
  case "synthesized-action":
52
60
  return "synthesized.action";
53
61
  }
@@ -86,18 +94,22 @@ export function referencedSubs(model) {
86
94
  subs.set(id, how);
87
95
  };
88
96
  const fromSelector = (s, how) => {
89
- if (s.kind === "sub-text" || s.kind === "sub-label" || s.kind === "sub-text-lift" || s.kind === "subtree-text") {
97
+ if (s.kind === "sub-text" || s.kind === "sub-label" || s.kind === "sub-text-lift" || s.kind === "subtree-text" || s.kind === "sub-children") {
90
98
  for (const id of s.subs)
91
99
  note(id, how);
92
100
  }
93
101
  };
94
102
  for (const r of model.routes) {
95
- for (const s of r.from)
96
- fromSelector(s, `consumed into ${describeDestination(r.to)}`);
103
+ for (const s of r.from) {
104
+ fromSelector(s, s.kind === "sub-children" ? `routed to ${describeDestination(r.to)}` : `consumed into ${describeDestination(r.to)}`);
105
+ }
97
106
  }
98
107
  const walk = (c) => {
99
108
  for (const id of c.of)
100
109
  note(id, `collected into ${describeDestination(c.as)}`);
110
+ if (c.join)
111
+ for (const id of c.join.with)
112
+ note(id, `joined into ${describeDestination(c.as)}`);
101
113
  for (const field of Object.values(c.item ?? {})) {
102
114
  if (isCollect(field))
103
115
  walk(field);
@@ -112,8 +124,17 @@ export function referencedSubs(model) {
112
124
  for (const c of model.collects)
113
125
  walk(c);
114
126
  for (const [id, identity] of Object.entries(model.subIdentity)) {
115
- note(id, identity.kind === "as-text" ? `re-identified as text (${identity.variant})` : "transparent grouping");
127
+ if (identity.kind === "as-text") {
128
+ note(id, `re-identified as text (${identity.variant})`);
129
+ }
130
+ else if (identity.kind === "transparent") {
131
+ note(id, "transparent grouping");
132
+ for (const d of identity.donate ?? [])
133
+ fromSelector(d.from, `donated to ${describeDestination(d.to)}`);
134
+ }
116
135
  }
136
+ for (const d of model.transparentDonate)
137
+ fromSelector(d.from, `donated to ${describeDestination(d.to)}`);
117
138
  for (const [id, reason] of Object.entries(model.drops))
118
139
  note(id, `dropped: ${reason}`);
119
140
  return subs;
@@ -14,12 +14,11 @@
14
14
  * computed destination names, and no cross-instance routing. Those absences
15
15
  * are the design: a plan you cannot write is a plan nobody has to audit.
16
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.
17
+ * Capability note: the grammar admits exactly what the engine can emit. T1
18
+ * (transparent identity + constrained control donation) landed as the
19
+ * `transparent` / `donate` spellings below; spellings whose engine support
20
+ * has not landed (`joinOn` pairing — T3, multi-slot children routing — T4,
21
+ * variant identity — T5) are still refused, never reinterpreted.
23
22
  */
24
23
  import type { ComponentPlan } from "./profiles.js";
25
24
  import { type Destination, type Selector, type SurfaceModel } from "./model.js";
@@ -39,25 +38,67 @@ export interface SurfaceV2Route {
39
38
  overwrite?: boolean;
40
39
  }
41
40
  export interface SurfaceV2Collect {
42
- /** Sections: direct children that open the collection. */
41
+ /** Sections (table-mode) or the repeated items themselves (item-mode). */
43
42
  of: string[];
44
43
  /** Array-valued destination (`prop:<name>`). */
45
44
  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`. */
45
+ /** table-mode: `flat` concatenates all rows into one list; `records` keeps one record per row. */
46
+ shape?: "flat" | "records";
47
+ /** table-mode: record field carrying each row's cells; required when shape is `records`. */
49
48
  field?: string;
50
- /** Repetitions inside a section. */
51
- row: string[];
52
- /** Cells inside a repetition, flattened to their subtree text. */
53
- cells: string[];
49
+ /** table-mode: repetitions inside a section. */
50
+ row?: string[];
51
+ /** table-mode: cells inside a repetition, flattened to their subtree text. */
52
+ cells?: string[];
53
+ /**
54
+ * T2 item-mode: each matching descendant becomes one record; every field
55
+ * resolves against THAT item's own node. Selectors: `self.text`,
56
+ * `self.props.<prop>`, `self.id`.
57
+ */
58
+ item?: Record<string, string>;
59
+ /**
60
+ * T3: a DECLARED key join onto a sibling sub-family. `on.left` extracts the
61
+ * key from each item, `on.right` from each counterpart; `fields` source
62
+ * record fields from the JOINED side (item-local selectors, or the literal
63
+ * `"children"` for the slot-valued field: the counterpart's children emit
64
+ * as instances and the record carries the reference). `optional: true`
65
+ * relaxes exactly-one to 0..1. The profile names the relation; nothing is
66
+ * ever inferred from position or adjacency.
67
+ */
68
+ join?: {
69
+ with: string[];
70
+ on: {
71
+ left: string;
72
+ right: string;
73
+ };
74
+ fields: Record<string, string>;
75
+ optional?: boolean;
76
+ };
77
+ }
78
+ export interface SurfaceV2Donate {
79
+ /** `self.text` or `sub(<id>|…).text` — where the donated value comes from. */
80
+ from: string;
81
+ /** `prop:<name>` on the boundary's single eligible control. */
82
+ to: string;
54
83
  }
55
84
  export type SurfaceV2Sub = "transparent" | {
85
+ transparent: true | {
86
+ donate: SurfaceV2Donate[];
87
+ };
88
+ } | {
56
89
  asText: string;
57
90
  } | {
58
91
  drop: string;
59
92
  };
60
93
  export interface SurfaceV2 {
94
+ /**
95
+ * T1 transparent identity: this component emits no instance; children rise.
96
+ * `true`, or an object carrying constrained control donations scoped to
97
+ * each dissolved instance's subtree.
98
+ */
99
+ transparent?: true | {
100
+ donate: SurfaceV2Donate[];
101
+ };
61
102
  routes?: SurfaceV2Route[];
62
103
  collects?: SurfaceV2Collect[];
63
104
  /** Identity or disposition of named sub-components met in this node's subtree. */
@@ -65,14 +106,4 @@ export interface SurfaceV2 {
65
106
  }
66
107
  export declare function parseSelector(raw: string): Selector | null;
67
108
  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
109
  export declare function parseSurfaceV2(surface: SurfaceV2, plan: ComponentPlan, path: string): SurfaceModel;
@@ -17,7 +17,7 @@ export class ProfileParseError extends Error {
17
17
  // ---------------------------------------------------------------------------
18
18
  const ID = "[a-z][a-z0-9-]*";
19
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)$`);
20
+ const SELECTOR = new RegExp(`^(self\\.text|self\\.id|self\\.props\\.(${PROP})|sub\\((${ID}(?:\\|${ID})*)\\)\\.(text|label|firstText|subtreeText|children)|children|synthesized\\.action)$`);
21
21
  const DESTINATION = new RegExp(`^(prop|textChild|slot|slots|action):(${PROP})$`);
22
22
  export function parseSelector(raw) {
23
23
  const m = SELECTOR.exec(raw);
@@ -25,6 +25,8 @@ export function parseSelector(raw) {
25
25
  return null;
26
26
  if (m[1] === "self.text")
27
27
  return { kind: "self-text" };
28
+ if (m[1] === "self.id")
29
+ return { kind: "self-id" };
28
30
  if (m[1] === "children")
29
31
  return { kind: "children" };
30
32
  if (m[1] === "synthesized.action")
@@ -39,6 +41,8 @@ export function parseSelector(raw) {
39
41
  return { kind: "sub-label", subs };
40
42
  case "firstText":
41
43
  return { kind: "sub-text-lift", subs };
44
+ case "children":
45
+ return { kind: "sub-children", subs };
42
46
  default:
43
47
  return { kind: "subtree-text", subs };
44
48
  }
@@ -77,8 +81,12 @@ function orderOfSelector(s, to) {
77
81
  return WriteOrder.CollectLead;
78
82
  case "self-text":
79
83
  return to.kind === "text-child" ? WriteOrder.SelfTextChild : WriteOrder.SelfText;
84
+ case "self-id":
85
+ return WriteOrder.SelfText; // unreachable in routes: `compatible` refuses it
80
86
  case "synthesized-action":
81
87
  return WriteOrder.Action;
88
+ case "sub-children":
89
+ return WriteOrder.Slot;
82
90
  case "children":
83
91
  return WriteOrder.Children;
84
92
  }
@@ -88,10 +96,15 @@ function compatible(s, to) {
88
96
  switch (s.kind) {
89
97
  case "children":
90
98
  return to.kind === "slot" || to.kind === "slots";
99
+ case "sub-children":
100
+ // T4: instance emission from a named region lands in exactly one slot.
101
+ return to.kind === "slot";
91
102
  case "synthesized-action":
92
103
  return to.kind === "action";
93
104
  case "self-text":
94
105
  return to.kind === "prop" || to.kind === "text-child";
106
+ case "self-id":
107
+ return false; // item-field-only: a route may not read self.id
95
108
  default:
96
109
  return to.kind === "prop";
97
110
  }
@@ -109,9 +122,51 @@ function compatible(s, to) {
109
122
  * self-props are declared, derived coverage is complete) run at transform
110
123
  * time, where the contract is in hand — see validate-v2.ts.
111
124
  */
125
+ /** Parses one donate entry; donation selectors and destinations are narrower than routes'. */
126
+ function parseDonation(d, at, issues, origin) {
127
+ const from = parseSelector(d.from);
128
+ if (!from || (from.kind !== "self-text" && from.kind !== "sub-text")) {
129
+ issues.push({ path: `${at}/from`, message: `'${d.from}' is not a donation source (donations harvest text: self.text or sub(<id>|…).text)` });
130
+ return null;
131
+ }
132
+ const to = parseDestination(d.to);
133
+ if (!to || to.kind !== "prop") {
134
+ issues.push({ path: `${at}/to`, message: `'${d.to}' is not a donation destination (donations write prop:<name> on the receiving control)` });
135
+ return null;
136
+ }
137
+ return { from, to, origin };
138
+ }
112
139
  export function parseSurfaceV2(surface, plan, path) {
113
140
  const issues = [];
114
141
  const model = emptySurfaceModel();
142
+ // T1: transparent identity. A transparent plan emits nothing, so nothing it
143
+ // could declare for an instance is meaningful — routes, collects, structural
144
+ // slots, propMap, required all refuse. Only sub dispositions (for its own
145
+ // dissolved subtree) and donations survive.
146
+ if (surface.transparent !== undefined) {
147
+ model.transparent = true;
148
+ if (typeof surface.transparent === "object") {
149
+ surface.transparent.donate.forEach((d, i) => {
150
+ const parsed = parseDonation(d, `${path}/transparent/donate/${i}`, issues, `v2:transparent/donate/${i}`);
151
+ if (parsed)
152
+ model.transparentDonate.push(parsed);
153
+ });
154
+ }
155
+ if ((surface.routes ?? []).length > 0 || (surface.collects ?? []).length > 0) {
156
+ issues.push({ path, message: "a transparent plan emits no instance; routes and collects have nowhere to land (donations are the only writes it may carry)" });
157
+ }
158
+ for (const [field, label] of [
159
+ [plan.structural ?? {}, "structural"],
160
+ [plan.propMap ?? {}, "propMap"],
161
+ ]) {
162
+ if (Object.keys(field).length > 0) {
163
+ issues.push({ path, message: `a transparent plan declares no catalog surface; remove its ${label} block` });
164
+ }
165
+ }
166
+ if ((plan.required ?? []).length > 0) {
167
+ issues.push({ path, message: "a transparent plan declares no catalog surface; remove its required list" });
168
+ }
169
+ }
115
170
  // The plan's own declared destination names: structural slots plus the
116
171
  // a2ui-side names its propMap writes. Gate A3 catches an OMITTED property
117
172
  // but silently passes an INVENTED one, so this is where invention dies.
@@ -237,6 +292,98 @@ export function parseSurfaceV2(surface, plan, path) {
237
292
  if (!declared.has(into.name)) {
238
293
  issues.push({ path: `${at}/into`, message: `destination '${into.name}' is not declared by this plan` });
239
294
  }
295
+ // T2 item-mode and table-mode are distinct shapes; mixing them is a
296
+ // contradiction, not a preference.
297
+ const itemMode = c.item !== undefined;
298
+ const tableKeys = [c.shape, c.field, c.row, c.cells].some((v) => v !== undefined);
299
+ if (itemMode && tableKeys) {
300
+ issues.push({ path: at, message: "a collect is either item-mode (item) or table-mode (shape/row/cells/field), never both" });
301
+ continue;
302
+ }
303
+ if (itemMode) {
304
+ const fields = {};
305
+ let ok = true;
306
+ for (const [fieldName, raw] of Object.entries(c.item)) {
307
+ const sel = parseSelector(raw);
308
+ const itemLocal = sel && (sel.kind === "self-text" || sel.kind === "self-prop" || sel.kind === "self-id");
309
+ if (!itemLocal) {
310
+ issues.push({
311
+ path: `${at}/item/${fieldName}`,
312
+ message: `'${raw}' is not an item field source (item fields resolve on the item's own node: self.text, self.props.<prop>, self.id — sibling reads and joins are T3)`,
313
+ });
314
+ ok = false;
315
+ continue;
316
+ }
317
+ fields[fieldName] = sel;
318
+ }
319
+ if (!ok || Object.keys(fields).length === 0) {
320
+ if (Object.keys(c.item).length === 0)
321
+ issues.push({ path: `${at}/item`, message: "an item-mode collect names at least one field" });
322
+ continue;
323
+ }
324
+ // T3 join, only on item-mode collects.
325
+ let join;
326
+ if (c.join) {
327
+ const jAt = `${at}/join`;
328
+ const left = parseSelector(c.join.on.left);
329
+ const right = parseSelector(c.join.on.right);
330
+ const itemLocal = (sel) => sel !== null && (sel.kind === "self-text" || sel.kind === "self-prop" || sel.kind === "self-id");
331
+ let jOk = true;
332
+ if (!itemLocal(left)) {
333
+ issues.push({ path: `${jAt}/on/left`, message: `'${c.join.on.left}' is not a join key source (item-local: self.id, self.props.<prop>, self.text)` });
334
+ jOk = false;
335
+ }
336
+ if (!itemLocal(right)) {
337
+ issues.push({ path: `${jAt}/on/right`, message: `'${c.join.on.right}' is not a join key source (item-local: self.id, self.props.<prop>, self.text)` });
338
+ jOk = false;
339
+ }
340
+ const jFields = {};
341
+ for (const [fieldName, raw] of Object.entries(c.join.fields)) {
342
+ if (raw === "children") {
343
+ jFields[fieldName] = { kind: "joined-children" };
344
+ continue;
345
+ }
346
+ const sel = parseSelector(raw);
347
+ if (!itemLocal(sel)) {
348
+ issues.push({ path: `${jAt}/fields/${fieldName}`, message: `'${raw}' is not a joined-side field source (item-local selectors, or "children" for the slot-valued field)` });
349
+ jOk = false;
350
+ continue;
351
+ }
352
+ jFields[fieldName] = sel;
353
+ }
354
+ for (const fieldName of Object.keys(jFields)) {
355
+ if (fieldName in fields) {
356
+ issues.push({ path: `${jAt}/fields/${fieldName}`, message: `field '${fieldName}' is already sourced from the left item; one record field, one source` });
357
+ jOk = false;
358
+ }
359
+ }
360
+ if (Object.keys(jFields).length === 0) {
361
+ issues.push({ path: `${jAt}/fields`, message: "a join names at least one joined-side field — a join that contributes nothing is not a relation" });
362
+ jOk = false;
363
+ }
364
+ if (jOk) {
365
+ join = { with: c.join.with, leftKey: left, rightKey: right, fields: jFields, optional: c.join.optional === true, origin: `v2:collects/${i}/join` };
366
+ }
367
+ else {
368
+ continue;
369
+ }
370
+ }
371
+ claim(into.name, WriteOrder.Collect, `collects/${i}`, `${at}/into`);
372
+ model.collects.push({
373
+ order: WriteOrder.Collect,
374
+ flatten: false,
375
+ of: c.of,
376
+ as: into,
377
+ fields,
378
+ ...(join ? { join } : {}),
379
+ origin: `v2:collects/${i}`,
380
+ });
381
+ continue;
382
+ }
383
+ if (c.shape === undefined || c.row === undefined || c.cells === undefined) {
384
+ issues.push({ path: at, message: "a table-mode collect declares shape, row, and cells (or use item-mode with `item`)" });
385
+ continue;
386
+ }
240
387
  if (c.shape === "records" && !c.field) {
241
388
  issues.push({ path: `${at}/field`, message: "a records collect names the field its rows' cells land in" });
242
389
  continue;
@@ -270,6 +417,17 @@ export function parseSurfaceV2(surface, plan, path) {
270
417
  if (disposition === "transparent") {
271
418
  model.subIdentity[sub] = { kind: "transparent" };
272
419
  }
420
+ else if (typeof disposition === "object" && "transparent" in disposition) {
421
+ const donate = [];
422
+ if (typeof disposition.transparent === "object") {
423
+ disposition.transparent.donate.forEach((d, i) => {
424
+ const parsed = parseDonation(d, `${at}/transparent/donate/${i}`, issues, `v2:subs/${sub}/donate/${i}`);
425
+ if (parsed)
426
+ donate.push(parsed);
427
+ });
428
+ }
429
+ model.subIdentity[sub] = donate.length > 0 ? { kind: "transparent", donate } : { kind: "transparent" };
430
+ }
273
431
  else if ("asText" in disposition) {
274
432
  model.subIdentity[sub] = { kind: "as-text", variant: disposition.asText };
275
433
  }
@@ -291,7 +449,23 @@ export function parseSurfaceV2(surface, plan, path) {
291
449
  // also emit that subtree as children.
292
450
  model.consumesSubtree =
293
451
  model.collects.length > 0 ||
294
- model.routes.some((r) => r.order === WriteOrder.Consume || r.order === WriteOrder.CollectLead);
452
+ model.routes.some((r) => r.order === WriteOrder.Consume || r.order === WriteOrder.CollectLead || r.order === WriteOrder.Slot);
453
+ // T4: one region, one slot — a sub named by several sub-children routes
454
+ // (or the same route naming several subs) would make one child list land
455
+ // twice; and a mixed from-list has no fallback semantics for regions.
456
+ const slotRoutes = model.routes.filter((r) => r.from.some((sel) => sel.kind === "sub-children"));
457
+ for (const r of slotRoutes) {
458
+ if (r.from.length !== 1 || r.from[0].kind !== "sub-children") {
459
+ issues.push({ path, message: `a sub(x).children route takes exactly one selector — regions have no fallback chain` });
460
+ }
461
+ else if (r.from[0].subs.length !== 1) {
462
+ issues.push({ path, message: `sub(${r.from[0].subs.join("|")}).children names ${r.from[0].subs.length} subs; a slot route names exactly one region` });
463
+ }
464
+ }
465
+ const slotSources = slotRoutes.flatMap((r) => (r.from[0].kind === "sub-children" ? r.from[0].subs : []));
466
+ for (const dup of slotSources.filter((id, i) => slotSources.indexOf(id) !== i)) {
467
+ issues.push({ path, message: `sub-component '${dup}' is the source of more than one slot route; one region, one slot` });
468
+ }
295
469
  const childrenRoute = model.routes.find((r) => r.from[0].kind === "children");
296
470
  if (model.consumesSubtree && childrenRoute) {
297
471
  issues.push({