@spellkave/world-author 0.0.0-bootstrap
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/LICENSE +89 -0
- package/README.md +62 -0
- package/package.json +57 -0
- package/src/applyset-diff-types.ts +158 -0
- package/src/applyset.ts +82 -0
- package/src/authoring-harness.ts +487 -0
- package/src/browser.ts +114 -0
- package/src/compose.ts +699 -0
- package/src/corpus.generated.ts +279 -0
- package/src/corpus.ts +1180 -0
- package/src/damage.ts +75 -0
- package/src/diagnostics.ts +59 -0
- package/src/diff-manifest-core.ts +393 -0
- package/src/diff-manifest.ts +5 -0
- package/src/docs-index.ts +105 -0
- package/src/dsl.ts +10 -0
- package/src/evaluate.ts +100 -0
- package/src/explain-core.ts +219 -0
- package/src/explain.ts +43 -0
- package/src/filter-manifest-core.ts +122 -0
- package/src/filter-manifest.ts +41 -0
- package/src/grants-core.ts +38 -0
- package/src/grants.ts +10 -0
- package/src/included-manifest-metadata-core.ts +63 -0
- package/src/included-manifest-metadata.ts +21 -0
- package/src/index.ts +30 -0
- package/src/inspect.ts +35 -0
- package/src/inspection.ts +239 -0
- package/src/lint.ts +540 -0
- package/src/manifest-text.ts +21 -0
- package/src/manifest.ts +403 -0
- package/src/metadata.ts +137 -0
- package/src/npc-dm-authority-projection.ts +95 -0
- package/src/npc-dm-documents.ts +339 -0
- package/src/npc-dm-presentation.ts +17 -0
- package/src/npc-dm-srd.ts +51 -0
- package/src/npc-dm-types.ts +723 -0
- package/src/npc-kinds.ts +404 -0
- package/src/npc-quests.ts +319 -0
- package/src/observation-tokens.browser.ts +31 -0
- package/src/observation-tokens.ts +27 -0
- package/src/owner-instance-read.ts +765 -0
- package/src/patch.ts +47 -0
- package/src/ref-tags.ts +149 -0
- package/src/reference.ts +48 -0
- package/src/row-identity.ts +105 -0
- package/src/row-kinds-data.ts +35 -0
- package/src/row-kinds.ts +93 -0
- package/src/scenario-guidance.ts +180 -0
- package/src/scoped-instance-read.ts +187 -0
- package/src/service-only-audience.ts +14 -0
- package/src/session-predicates.ts +6 -0
- package/src/spell.ts +119 -0
- package/src/tag-prefix-families.ts +143 -0
- package/src/tooling.browser.ts +397 -0
- package/src/tooling.ts +38 -0
- package/src/validate.ts +255 -0
package/src/manifest.ts
ADDED
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// embedded-manifest: {"category": "production_schema_constructor", "rationale": "production renderer constructs a schema document from typed caller input", "signatures": ["schema_object", "schema_shape"]}
|
|
2
|
+
// Typed world-manifest authoring surface (#504).
|
|
3
|
+
//
|
|
4
|
+
// Builds typed manifest rows → a single-file world manifest (TOML). This is the
|
|
5
|
+
// authoring counterpart to @spellkave/sdk's runtime actor surface, and it obeys
|
|
6
|
+
// the same boundary (sdk CLAUDE §1/§3): it serializes *typed rows*, it does NOT
|
|
7
|
+
// validate. `crates/spellkave-validate` (via `r_apply_world_manifest`) is the
|
|
8
|
+
// only source of truth — a validator rejection is a finding, not a bug here. A
|
|
9
|
+
// future addition may *call* the wasm validator (`spellkave-validate-wasm`) for
|
|
10
|
+
// pre-apply feedback, but must never re-implement its rules.
|
|
11
|
+
//
|
|
12
|
+
// The row vocabulary is the union of what the world-explorer co-evolution loops
|
|
13
|
+
// author: presence (entities + actors + spatial locations) and an actionable
|
|
14
|
+
// affordance chain (capability → niche → affordance → action → consequence).
|
|
15
|
+
// Every array is optional; `renderManifest` emits only the rows present.
|
|
16
|
+
|
|
17
|
+
import { render_manifest_json_to_toml } from "@spellkave/validate-wasm";
|
|
18
|
+
import {
|
|
19
|
+
type MetadataAttachmentRow,
|
|
20
|
+
type MetadataNamespaceRow,
|
|
21
|
+
toSchemaMetadataNamespaceRows,
|
|
22
|
+
toSchemaMetadataRows,
|
|
23
|
+
} from "./metadata";
|
|
24
|
+
|
|
25
|
+
// ── Row types ────────────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
export interface BundleHeader {
|
|
28
|
+
bundleId: string;
|
|
29
|
+
displayName: string;
|
|
30
|
+
author: string;
|
|
31
|
+
version: string;
|
|
32
|
+
description: string;
|
|
33
|
+
dependsOn: string[];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface EntityRow {
|
|
37
|
+
entityKey: string;
|
|
38
|
+
name: string;
|
|
39
|
+
kind: string;
|
|
40
|
+
placeNodeId: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ActorRow {
|
|
44
|
+
entityKey: string;
|
|
45
|
+
name: string;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface ActorSpatialLocationRow {
|
|
49
|
+
entityKey: string;
|
|
50
|
+
placeNodeId: string;
|
|
51
|
+
selectionPath: string;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ObjectStateDefinitionRow {
|
|
55
|
+
stateId: string;
|
|
56
|
+
/** What the flag attaches to: "entity" | "item". */
|
|
57
|
+
targetKind: string;
|
|
58
|
+
displayName: string;
|
|
59
|
+
defaultVisibility?: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export interface CapabilityDefinitionRow {
|
|
63
|
+
capabilityId: string;
|
|
64
|
+
actorKind: string;
|
|
65
|
+
displayName: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ExtentKindDefinitionRow {
|
|
69
|
+
kindId: string;
|
|
70
|
+
resolutionShape: "PlaceGraph" | "Party";
|
|
71
|
+
description: string;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export interface NicheDefinitionRow {
|
|
75
|
+
nicheId: string;
|
|
76
|
+
displayName: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Binds a niche to a place node, so any target standing at that node exposes
|
|
81
|
+
* the niche to affordance selection. Without this an affordance is perceived
|
|
82
|
+
* but `manipulate` rejects with `predicate_rejected` (no template selected) —
|
|
83
|
+
* the niche is the seam between "perceivable" and "actionable" (#504 L0c).
|
|
84
|
+
*/
|
|
85
|
+
export interface SpatialNicheAttachmentRow {
|
|
86
|
+
placeNodeId: string;
|
|
87
|
+
nicheId: string;
|
|
88
|
+
/** Optional action dispatched after successful entry into `placeNodeId`. */
|
|
89
|
+
onEntryActionId?: string;
|
|
90
|
+
/** Manifest entity key passed as the entry action's target. */
|
|
91
|
+
onEntryTargetEntityKey?: string;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface OutcomeTemplateRow {
|
|
95
|
+
outcomeTemplateId: string;
|
|
96
|
+
displayName: string;
|
|
97
|
+
/** Action id this outcome resolves to (rendered as a `manipulation.action_id=` tag). */
|
|
98
|
+
actionId: string;
|
|
99
|
+
/** Consequence applied when an outcome graph commits this template. */
|
|
100
|
+
consequenceTemplateId?: string;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export interface AffordanceTemplateRow {
|
|
104
|
+
templateId: string;
|
|
105
|
+
nicheId: string;
|
|
106
|
+
capabilityId: string;
|
|
107
|
+
displayName: string;
|
|
108
|
+
outcomeTemplateId: string;
|
|
109
|
+
rangeM?: number;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface ActionDefinitionRow {
|
|
113
|
+
actionId: string;
|
|
114
|
+
requiredCapability: string;
|
|
115
|
+
onSuccess: string;
|
|
116
|
+
displayName: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A single SetEntityFlag frame write — the simplest write (no item args). */
|
|
120
|
+
export interface SetEntityFlagWrite {
|
|
121
|
+
/** Binding token: "TargetEntity" | "Actor" | a literal entity ref. */
|
|
122
|
+
entity: string;
|
|
123
|
+
/** Must match an authored object_state_definition.state_id. */
|
|
124
|
+
flag: string;
|
|
125
|
+
value: boolean;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface ConsequenceRow {
|
|
129
|
+
templateId: string;
|
|
130
|
+
writes: SetEntityFlagWrite[];
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export interface ActorCapabilityGrant {
|
|
134
|
+
/**
|
|
135
|
+
* Manifest key of the actor body. Cross-bundle grants (to a base actor) must
|
|
136
|
+
* use the fully-qualified `bundle:key` form (e.g. "core:core/player"); a bare
|
|
137
|
+
* key is resolved within the authoring bundle only (#504 L0c finding).
|
|
138
|
+
*/
|
|
139
|
+
actorEntityKey: string;
|
|
140
|
+
capabilityId: string;
|
|
141
|
+
source: string;
|
|
142
|
+
extentRef?: string;
|
|
143
|
+
extentKindRef?: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* A complete single-file world manifest. Every array is optional; presence
|
|
148
|
+
* bundles populate {entities, actors, actorSpatialLocations}, actionable
|
|
149
|
+
* bundles add the affordance chain. The validator decides what is admissible.
|
|
150
|
+
*/
|
|
151
|
+
export interface WorldManifest {
|
|
152
|
+
header: BundleHeader;
|
|
153
|
+
entities?: EntityRow[];
|
|
154
|
+
actors?: ActorRow[];
|
|
155
|
+
actorSpatialLocations?: ActorSpatialLocationRow[];
|
|
156
|
+
objectStateDefinitions?: ObjectStateDefinitionRow[];
|
|
157
|
+
capabilityDefinitions?: CapabilityDefinitionRow[];
|
|
158
|
+
extentKindDefinitions?: ExtentKindDefinitionRow[];
|
|
159
|
+
nicheDefinitions?: NicheDefinitionRow[];
|
|
160
|
+
spatialNicheAttachments?: SpatialNicheAttachmentRow[];
|
|
161
|
+
outcomeTemplates?: OutcomeTemplateRow[];
|
|
162
|
+
affordanceTemplates?: AffordanceTemplateRow[];
|
|
163
|
+
actionDefinitions?: ActionDefinitionRow[];
|
|
164
|
+
consequences?: ConsequenceRow[];
|
|
165
|
+
actorCapabilities?: ActorCapabilityGrant[];
|
|
166
|
+
metadataNamespaces?: MetadataNamespaceRow[];
|
|
167
|
+
metadataAttachments?: MetadataAttachmentRow[];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
type SchemaManifest = Record<string, unknown>;
|
|
171
|
+
|
|
172
|
+
function withRows<T>(target: SchemaManifest, key: string, rows: T[] | undefined): void {
|
|
173
|
+
if (rows && rows.length > 0) target[key] = rows;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function toSchemaManifest(manifest: WorldManifest): SchemaManifest {
|
|
177
|
+
const b = manifest.header;
|
|
178
|
+
const schema: SchemaManifest = {
|
|
179
|
+
schema_version: "1.0.0",
|
|
180
|
+
metadata: { profile: b.bundleId },
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
withRows(schema, "bundles", [
|
|
184
|
+
{
|
|
185
|
+
bundle_id: b.bundleId,
|
|
186
|
+
display_name: b.displayName,
|
|
187
|
+
author: b.author,
|
|
188
|
+
version: b.version,
|
|
189
|
+
depends_on: b.dependsOn,
|
|
190
|
+
description: b.description,
|
|
191
|
+
},
|
|
192
|
+
]);
|
|
193
|
+
withRows(
|
|
194
|
+
schema,
|
|
195
|
+
"entities",
|
|
196
|
+
manifest.entities?.map((e) => ({
|
|
197
|
+
entity_key: e.entityKey,
|
|
198
|
+
bundle_id: b.bundleId,
|
|
199
|
+
name: e.name,
|
|
200
|
+
kind: e.kind,
|
|
201
|
+
place_node_id: e.placeNodeId,
|
|
202
|
+
})),
|
|
203
|
+
);
|
|
204
|
+
withRows(
|
|
205
|
+
schema,
|
|
206
|
+
"actors",
|
|
207
|
+
manifest.actors?.map((a) => ({
|
|
208
|
+
entity_key: a.entityKey,
|
|
209
|
+
bundle_id: b.bundleId,
|
|
210
|
+
name: a.name,
|
|
211
|
+
})),
|
|
212
|
+
);
|
|
213
|
+
withRows(
|
|
214
|
+
schema,
|
|
215
|
+
"actor_spatial_locations",
|
|
216
|
+
manifest.actorSpatialLocations?.map((l) => ({
|
|
217
|
+
entity_key: l.entityKey,
|
|
218
|
+
place_node_id: l.placeNodeId,
|
|
219
|
+
bundle_id: b.bundleId,
|
|
220
|
+
selection_path: l.selectionPath,
|
|
221
|
+
})),
|
|
222
|
+
);
|
|
223
|
+
withRows(
|
|
224
|
+
schema,
|
|
225
|
+
"object_state_definitions",
|
|
226
|
+
manifest.objectStateDefinitions?.map((s) => ({
|
|
227
|
+
state_id: s.stateId,
|
|
228
|
+
bundle_id: b.bundleId,
|
|
229
|
+
target_kind: s.targetKind,
|
|
230
|
+
display_name: s.displayName,
|
|
231
|
+
default_visibility: s.defaultVisibility ?? "Public",
|
|
232
|
+
})),
|
|
233
|
+
);
|
|
234
|
+
withRows(
|
|
235
|
+
schema,
|
|
236
|
+
"capability_definitions",
|
|
237
|
+
manifest.capabilityDefinitions?.map((c) => ({
|
|
238
|
+
capability_id: c.capabilityId,
|
|
239
|
+
bundle_id: b.bundleId,
|
|
240
|
+
actor_kind: c.actorKind,
|
|
241
|
+
display_name: c.displayName,
|
|
242
|
+
description: c.displayName,
|
|
243
|
+
})),
|
|
244
|
+
);
|
|
245
|
+
withRows(
|
|
246
|
+
schema,
|
|
247
|
+
"extent_kind_definitions",
|
|
248
|
+
manifest.extentKindDefinitions?.map((e) => ({
|
|
249
|
+
kind_id: e.kindId,
|
|
250
|
+
resolution_shape: e.resolutionShape,
|
|
251
|
+
bundle_id: b.bundleId,
|
|
252
|
+
description: e.description,
|
|
253
|
+
})),
|
|
254
|
+
);
|
|
255
|
+
withRows(
|
|
256
|
+
schema,
|
|
257
|
+
"niche_definitions",
|
|
258
|
+
manifest.nicheDefinitions?.map((n) => ({
|
|
259
|
+
niche_id: n.nicheId,
|
|
260
|
+
bundle_id: b.bundleId,
|
|
261
|
+
display_name: n.displayName,
|
|
262
|
+
description: n.displayName,
|
|
263
|
+
})),
|
|
264
|
+
);
|
|
265
|
+
withRows(
|
|
266
|
+
schema,
|
|
267
|
+
"spatial_niche_attachments",
|
|
268
|
+
manifest.spatialNicheAttachments?.map((a) => ({
|
|
269
|
+
bundle_id: b.bundleId,
|
|
270
|
+
place_node_id: a.placeNodeId,
|
|
271
|
+
niche_id: a.nicheId,
|
|
272
|
+
...(a.onEntryActionId === undefined ? {} : { on_entry_action_id: a.onEntryActionId }),
|
|
273
|
+
...(a.onEntryTargetEntityKey === undefined
|
|
274
|
+
? {}
|
|
275
|
+
: { on_entry_target_entity_key: a.onEntryTargetEntityKey }),
|
|
276
|
+
})),
|
|
277
|
+
);
|
|
278
|
+
withRows(
|
|
279
|
+
schema,
|
|
280
|
+
"outcome_templates",
|
|
281
|
+
manifest.outcomeTemplates?.map((o) => ({
|
|
282
|
+
outcome_template_id: o.outcomeTemplateId,
|
|
283
|
+
bundle_id: b.bundleId,
|
|
284
|
+
display_name: o.displayName,
|
|
285
|
+
outcome_bool: true,
|
|
286
|
+
manipulation_action_id: o.actionId,
|
|
287
|
+
...(o.consequenceTemplateId === undefined
|
|
288
|
+
? {}
|
|
289
|
+
: { consequence_template_id: o.consequenceTemplateId }),
|
|
290
|
+
})),
|
|
291
|
+
);
|
|
292
|
+
withRows(
|
|
293
|
+
schema,
|
|
294
|
+
"affordance_templates",
|
|
295
|
+
manifest.affordanceTemplates?.map((a) => ({
|
|
296
|
+
template_id: a.templateId,
|
|
297
|
+
bundle_id: b.bundleId,
|
|
298
|
+
niche_id: a.nicheId,
|
|
299
|
+
capability_id: a.capabilityId,
|
|
300
|
+
display_name: a.displayName,
|
|
301
|
+
predicate_bool: true,
|
|
302
|
+
outcome_template_id: a.outcomeTemplateId,
|
|
303
|
+
...(a.rangeM === undefined ? {} : { range_m: a.rangeM }),
|
|
304
|
+
})),
|
|
305
|
+
);
|
|
306
|
+
withRows(
|
|
307
|
+
schema,
|
|
308
|
+
"action_definitions",
|
|
309
|
+
manifest.actionDefinitions?.map((a) => ({
|
|
310
|
+
action_id: a.actionId,
|
|
311
|
+
bundle_id: b.bundleId,
|
|
312
|
+
required_capability: a.requiredCapability,
|
|
313
|
+
on_success: a.onSuccess,
|
|
314
|
+
display_name: a.displayName,
|
|
315
|
+
})),
|
|
316
|
+
);
|
|
317
|
+
withRows(
|
|
318
|
+
schema,
|
|
319
|
+
"consequence_templates",
|
|
320
|
+
manifest.consequences?.map((c) => ({
|
|
321
|
+
template_id: c.templateId,
|
|
322
|
+
bundle_id: b.bundleId,
|
|
323
|
+
})),
|
|
324
|
+
);
|
|
325
|
+
withRows(
|
|
326
|
+
schema,
|
|
327
|
+
"consequence_frame_writes",
|
|
328
|
+
manifest.consequences?.flatMap((c) =>
|
|
329
|
+
c.writes.map((write, ordinal) => ({
|
|
330
|
+
template_id: c.templateId,
|
|
331
|
+
ordinal,
|
|
332
|
+
frame_write: {
|
|
333
|
+
SetEntityFlag: {
|
|
334
|
+
entity: write.entity,
|
|
335
|
+
flag: write.flag,
|
|
336
|
+
value: write.value,
|
|
337
|
+
},
|
|
338
|
+
},
|
|
339
|
+
})),
|
|
340
|
+
),
|
|
341
|
+
);
|
|
342
|
+
withRows(
|
|
343
|
+
schema,
|
|
344
|
+
"actor_capabilities",
|
|
345
|
+
manifest.actorCapabilities?.map((g) => ({
|
|
346
|
+
actor_entity_key: g.actorEntityKey,
|
|
347
|
+
capability_id: g.capabilityId,
|
|
348
|
+
source: g.source,
|
|
349
|
+
bundle_id: b.bundleId,
|
|
350
|
+
extent_ref: g.extentRef ?? "",
|
|
351
|
+
extent_kind_ref: g.extentKindRef ?? "",
|
|
352
|
+
})),
|
|
353
|
+
);
|
|
354
|
+
withRows(
|
|
355
|
+
schema,
|
|
356
|
+
"metadata_namespaces",
|
|
357
|
+
toSchemaMetadataNamespaceRows(b.bundleId, manifest.metadataNamespaces),
|
|
358
|
+
);
|
|
359
|
+
withRows(
|
|
360
|
+
schema,
|
|
361
|
+
"metadata_attachments",
|
|
362
|
+
toSchemaMetadataRows(b.bundleId, manifest.metadataAttachments),
|
|
363
|
+
);
|
|
364
|
+
|
|
365
|
+
return schema;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/** Serialize a manifest to a single-file world manifest. Validated on apply. */
|
|
369
|
+
export function renderManifest(manifest: WorldManifest): string {
|
|
370
|
+
return render_manifest_json_to_toml(JSON.stringify(toSchemaManifest(manifest)));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** Render a typed schema-shaped manifest object through the Rust/wasm renderer. */
|
|
374
|
+
export function renderSchemaManifest(manifest: Record<string, unknown>): string {
|
|
375
|
+
return render_manifest_json_to_toml(JSON.stringify(manifest));
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
const APPLYSET_NAME = /^[a-z0-9][a-z0-9._-]*$/;
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Name the applyset a ROOT manifest belongs to (ADR-0145 D-1): sets the
|
|
382
|
+
* top-level `applyset = "<name>"` right after `schema_version`, replacing an
|
|
383
|
+
* existing one. Generators call this on every root they write; include
|
|
384
|
+
* fragments never carry the field.
|
|
385
|
+
*/
|
|
386
|
+
export function withRootApplyset(rootToml: string, name: string): string {
|
|
387
|
+
if (!APPLYSET_NAME.test(name)) {
|
|
388
|
+
throw new Error(`invalid applyset name '${name}': use [a-z0-9][a-z0-9._-]*`);
|
|
389
|
+
}
|
|
390
|
+
const line = `applyset = "${name}"`;
|
|
391
|
+
if (/^applyset\s*=.*$/m.test(rootToml)) {
|
|
392
|
+
return rootToml.replace(/^applyset\s*=.*$/m, line);
|
|
393
|
+
}
|
|
394
|
+
if (!/^schema_version\s*=.*$/m.test(rootToml)) {
|
|
395
|
+
throw new Error("withRootApplyset: not a root manifest (no schema_version line)");
|
|
396
|
+
}
|
|
397
|
+
return rootToml.replace(/^(schema_version\s*=.*)$/m, `$1\n${line}`);
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** Render schema-shaped manifest JSON through the Rust/wasm renderer. */
|
|
401
|
+
export function renderSchemaManifestJson(manifestJson: string): string {
|
|
402
|
+
return render_manifest_json_to_toml(manifestJson);
|
|
403
|
+
}
|
package/src/metadata.ts
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Diagnostic } from "./diagnostics";
|
|
2
|
+
|
|
3
|
+
export type MetadataNamespace = string;
|
|
4
|
+
|
|
5
|
+
export interface MetadataNamespaceRow {
|
|
6
|
+
namespace: string;
|
|
7
|
+
audience: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
/** ADR-0130 D-4: reference content (rules text, creation data, docs) that
|
|
10
|
+
* ships only to clients that subscribe `public_reference_metadata`, never
|
|
11
|
+
* to a play session on connect. Default `false` (kernel default). */
|
|
12
|
+
onDemand?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface MetadataAttachmentRow {
|
|
16
|
+
targetKind:
|
|
17
|
+
| "entity"
|
|
18
|
+
| "entity_definition"
|
|
19
|
+
| "place_graph"
|
|
20
|
+
| "place_node"
|
|
21
|
+
| "place_edge"
|
|
22
|
+
| "item_definition"
|
|
23
|
+
| "resource_definition"
|
|
24
|
+
| "affordance_template"
|
|
25
|
+
| "capability_definition"
|
|
26
|
+
| "bundle"
|
|
27
|
+
| "prompt_definition";
|
|
28
|
+
targetRef: string;
|
|
29
|
+
namespace: MetadataNamespace | string;
|
|
30
|
+
payload: unknown;
|
|
31
|
+
source?: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SchemaMetadataAttachmentRow {
|
|
35
|
+
target_kind: string;
|
|
36
|
+
target_ref: string;
|
|
37
|
+
namespace: string;
|
|
38
|
+
payload_json: string;
|
|
39
|
+
bundle_id: string;
|
|
40
|
+
source?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface SchemaMetadataNamespaceRow {
|
|
44
|
+
namespace: string;
|
|
45
|
+
audience: string;
|
|
46
|
+
bundle_id: string;
|
|
47
|
+
description?: string;
|
|
48
|
+
/** ADR-0130 D-4: see {@link MetadataNamespaceRow.onDemand}. */
|
|
49
|
+
on_demand?: boolean;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export function toSchemaMetadataNamespaceRows(
|
|
53
|
+
bundleId: string,
|
|
54
|
+
rows: MetadataNamespaceRow[] | undefined,
|
|
55
|
+
): SchemaMetadataNamespaceRow[] | undefined {
|
|
56
|
+
if (!rows || rows.length === 0) return undefined;
|
|
57
|
+
return rows.map((row) => ({
|
|
58
|
+
namespace: row.namespace,
|
|
59
|
+
audience: row.audience,
|
|
60
|
+
bundle_id: bundleId,
|
|
61
|
+
...(row.description === undefined ? {} : { description: row.description }),
|
|
62
|
+
...(row.onDemand === undefined ? {} : { on_demand: row.onDemand }),
|
|
63
|
+
}));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function toSchemaMetadataRows(
|
|
67
|
+
bundleId: string,
|
|
68
|
+
rows: MetadataAttachmentRow[] | undefined,
|
|
69
|
+
): SchemaMetadataAttachmentRow[] | undefined {
|
|
70
|
+
if (!rows || rows.length === 0) return undefined;
|
|
71
|
+
return rows.map((row) => ({
|
|
72
|
+
target_kind: row.targetKind,
|
|
73
|
+
target_ref: row.targetRef,
|
|
74
|
+
namespace: row.namespace,
|
|
75
|
+
payload_json: JSON.stringify(row.payload),
|
|
76
|
+
bundle_id: bundleId,
|
|
77
|
+
...(row.source === undefined ? {} : { source: row.source }),
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function validateMetadataRows(
|
|
82
|
+
rows: SchemaMetadataAttachmentRow[] | undefined,
|
|
83
|
+
namespaces: SchemaMetadataNamespaceRow[] | undefined,
|
|
84
|
+
pathPrefix = "metadata",
|
|
85
|
+
): Diagnostic[] {
|
|
86
|
+
const diags: Diagnostic[] = [];
|
|
87
|
+
const registeredNamespaces = new Set<string>();
|
|
88
|
+
for (const [index, row] of (namespaces ?? []).entries()) {
|
|
89
|
+
if (registeredNamespaces.has(row.namespace)) {
|
|
90
|
+
diags.push(
|
|
91
|
+
metadataDiag(
|
|
92
|
+
`metadata_namespaces[${index}]`,
|
|
93
|
+
`duplicate metadata namespace '${row.namespace}'`,
|
|
94
|
+
row,
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
registeredNamespaces.add(row.namespace);
|
|
100
|
+
}
|
|
101
|
+
for (const [index, row] of (rows ?? []).entries()) {
|
|
102
|
+
const path = `${pathPrefix}[${index}]`;
|
|
103
|
+
if (!registeredNamespaces.has(row.namespace)) {
|
|
104
|
+
diags.push(metadataDiag(path, `unknown metadata namespace '${row.namespace}'`, row));
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
let payload: unknown;
|
|
108
|
+
try {
|
|
109
|
+
payload = JSON.parse(row.payload_json);
|
|
110
|
+
} catch (err) {
|
|
111
|
+
diags.push(
|
|
112
|
+
metadataDiag(path, `payload_json is not valid JSON: ${String(err)}`, {
|
|
113
|
+
namespace: row.namespace,
|
|
114
|
+
}),
|
|
115
|
+
);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (!isRecord(payload)) {
|
|
119
|
+
diags.push(metadataDiag(path, "payload_json must decode to a JSON object", row));
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return diags;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function metadataDiag(path: string, message: string, details: unknown): Diagnostic {
|
|
126
|
+
return {
|
|
127
|
+
code: "WA-META-001",
|
|
128
|
+
severity: "fatal",
|
|
129
|
+
message,
|
|
130
|
+
path,
|
|
131
|
+
details,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
136
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
137
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
/** Internal carrier facet. Roots must already have passed publisher admission.
|
|
2
|
+
* This projection supplies no proof of full duty reference closure or readiness. */
|
|
3
|
+
type Json = null | boolean | number | string | Json[] | { [key: string]: Json };
|
|
4
|
+
type Row = { [key: string]: Json };
|
|
5
|
+
|
|
6
|
+
export interface DmAuthorityWorld {
|
|
7
|
+
role_definitions: Row[];
|
|
8
|
+
role_aggregations: Row[];
|
|
9
|
+
role_bindings: { binding_id: string; role_id: string }[];
|
|
10
|
+
action_definitions: { required_capability: string }[];
|
|
11
|
+
actor_capabilities: { actor_entity_key: string; capability_id: string }[];
|
|
12
|
+
metadata_namespaces: Row[];
|
|
13
|
+
capability_definitions: Row[];
|
|
14
|
+
bundles: { bundle_id: string }[];
|
|
15
|
+
entities: { entity_key: string; bundle_id: string }[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Same existing Job/facet bound as dm_document_authority::MAX_ARTIFACT_BYTES.
|
|
19
|
+
const MAX_AUTHORITY_BYTES = 8 * 1024 * 1024;
|
|
20
|
+
|
|
21
|
+
function object(value: unknown): value is Record<string, unknown> {
|
|
22
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Copy JSON without dropping undefined/NaN or invoking class serialization. */
|
|
26
|
+
function copyJson(value: unknown, ancestors = new Set<object>()): Json {
|
|
27
|
+
if (value === null || typeof value === "string" || typeof value === "boolean") return value;
|
|
28
|
+
if (typeof value === "number" && Number.isFinite(value)) return value;
|
|
29
|
+
if (typeof value !== "object" || ancestors.has(value))
|
|
30
|
+
throw new Error("DM authority projection requires acyclic JSON rows");
|
|
31
|
+
const prototype: unknown = Object.getPrototypeOf(value);
|
|
32
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null)
|
|
33
|
+
throw new Error("DM authority projection requires plain JSON rows");
|
|
34
|
+
ancestors.add(value);
|
|
35
|
+
try {
|
|
36
|
+
return Array.isArray(value)
|
|
37
|
+
? value.map((item) => copyJson(item, ancestors))
|
|
38
|
+
: Object.fromEntries(
|
|
39
|
+
Object.entries(value).map(([key, item]) => [key, copyJson(item, ancestors)]),
|
|
40
|
+
);
|
|
41
|
+
} finally {
|
|
42
|
+
ancestors.delete(value);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function rows(world: Record<string, unknown>, table: string): Record<string, unknown>[] {
|
|
47
|
+
if (!Object.hasOwn(world, table)) return [];
|
|
48
|
+
const value = world[table];
|
|
49
|
+
if (!Array.isArray(value) || !value.every(object))
|
|
50
|
+
throw new Error(`Invalid DM authority table: ${table}`);
|
|
51
|
+
return value;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function field(row: Record<string, unknown>, key: string): string {
|
|
55
|
+
const value = row[key];
|
|
56
|
+
if (typeof value !== "string") throw new Error(`Invalid DM authority column: ${key}`);
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Preserve the complete authority population from every admitted root. No
|
|
61
|
+
* bundle/role/actor filtering or identity deduplication occurs here. Full rows
|
|
62
|
+
* retain optional-column distinctions for shared Rust collision checks.
|
|
63
|
+
* Duplicate raw JSON keys must be refused before roots reach this adapter. */
|
|
64
|
+
export function projectDmAuthorityWorlds(canonicalWorlds: readonly unknown[]): DmAuthorityWorld[] {
|
|
65
|
+
if (!canonicalWorlds.length) throw new Error("DM authority requires canonical roots");
|
|
66
|
+
const projection = canonicalWorlds.map((world): DmAuthorityWorld => {
|
|
67
|
+
if (!object(world)) throw new Error("Invalid DM authority canonical root");
|
|
68
|
+
const full = (table: string): Row[] => rows(world, table).map((row) => copyJson(row) as Row);
|
|
69
|
+
return {
|
|
70
|
+
role_definitions: full("role_definitions"),
|
|
71
|
+
role_aggregations: full("role_aggregations"),
|
|
72
|
+
role_bindings: rows(world, "role_bindings").map((row) => ({
|
|
73
|
+
binding_id: field(row, "binding_id"),
|
|
74
|
+
role_id: field(row, "role_id"),
|
|
75
|
+
})),
|
|
76
|
+
action_definitions: rows(world, "action_definitions").map((row) => ({
|
|
77
|
+
required_capability: field(row, "required_capability"),
|
|
78
|
+
})),
|
|
79
|
+
actor_capabilities: rows(world, "actor_capabilities").map((row) => ({
|
|
80
|
+
actor_entity_key: field(row, "actor_entity_key"),
|
|
81
|
+
capability_id: field(row, "capability_id"),
|
|
82
|
+
})),
|
|
83
|
+
metadata_namespaces: full("metadata_namespaces"),
|
|
84
|
+
capability_definitions: full("capability_definitions"),
|
|
85
|
+
bundles: rows(world, "bundles").map((row) => ({ bundle_id: field(row, "bundle_id") })),
|
|
86
|
+
entities: rows(world, "entities").map((row) => ({
|
|
87
|
+
entity_key: field(row, "entity_key"),
|
|
88
|
+
bundle_id: field(row, "bundle_id"),
|
|
89
|
+
})),
|
|
90
|
+
};
|
|
91
|
+
});
|
|
92
|
+
if (new TextEncoder().encode(JSON.stringify(projection)).byteLength > MAX_AUTHORITY_BYTES)
|
|
93
|
+
throw new Error("DM authority projection exceeds 8 MiB");
|
|
94
|
+
return projection;
|
|
95
|
+
}
|