A guided tour of the metadata spine for a curious engineer. architecture.md
is the app doc; this is the MetaObjects doc. Everything below is grounded in real files.
Typed metadata under metaobjects/ is the durable spine. Every generated
file under src/**/generated/ is disposable output — regenerated by npm run gen:db
(meta gen) and diffed by npm run demo:drift (meta verify). You never hand-edit generated
code; you change the metadata and regenerate.
Two object kinds matter (ADR-0028):
object.entity— owns data: has identity + asource.rdb, so it becomes a DB table. OnlyCouncilandCouncilTurnare entities here.object.value— a pure shape with no identity and no source: an interface + Zod schema, never a table. Everything else (WizardOutput,TurnLens, the prompt payloads, …) is a value object — the in-memory shapes for prompts, LLM I/O, and projections.storage: jsonbis a column attribute, so it's meaningful on an entity field and inert on a value object.
Take WizardOutput (the structured thing each wizard emits) declared in
metaobjects/meta-wizard-output.yaml. Its stance
field extends the shared abstract enum wizardStance
(abstracts/meta-stance-enums.yaml) — declared
once, reused by WizardOutput, TurnLens, and CouncilTurn. From that one spine:
entityFileemitssrc/db/generated/WizardOutput.ts(Zod) and, for the entities that persist these fields, Drizzle tables; the ownedcompositionLoadersemits the typed council read. Because several fields sharewizardStance, codegen also emits a singlesrc/db/generated/enums.tsthe entities reference — the de-duplication carries through to the generated code.promptRenderemits a typed render payload + handles insrc/render/generated/prompts.ts.structuredCompletion(a custom generator) reads thetemplate.toolcallthat namesWizardOutputas itspayloadRefand emitscallEmitWizardTakeinsrc/llm/generated/toolcalls.ts— a wrapper whose runtime tool schema is the ZodWizardOutputInsertSchema.sseFrames(custom) emits thewizardarm of theCouncilFrameunion insrc/web/generated/council-frames.ts, carrying aWizardOutput. The orchestrator yields these frames, the Worker encodes them withencodeCouncilFrame, and the client narrows them withtoCouncilFrame.lineageMappers(custom) emits the copies betweenWizardOutputand the objects whose fieldsextendsit:CouncilTurn.lineage.ts(persist a take, rehydrate it) andTurnLens.lineage.ts(project a prior take into the next prompt). A field added to all three can no longer be dropped by a hand-written copy. The same generator builds the publicSharedCouncilview thatGET /c/:slug.jsonserves: its declared fields are the exposure, sovisitorIdstays private and a newCouncilcolumn stays hidden until someone adds it toSharedCouncilon purpose. That spec setsnullForAbsent, so every declared key is always served and a NULL column arrives asnull. The turns in that response go throughSharedTurnthe same way, socouncilId,ordinaland the deprecatedroundcolumn stay private.Council.lineage.tsstores aVerdictOutputon the council's verdict columns (eachextendstheVerdictOutputfield it holds) and reads it back, asnulluntil the council completes.
Rename a field on WizardOutput, or on the WizardUserPayload that the prompts bind, and the
drift gate fails across all of it at once — the money shot in the README.
- Payload — a
object.valuepayload (e.g.WizardUserPayload) declares exactly what a prompt may reference. Payload bloat becomes a reviewable diff. - Declaration — a
template.prompt(metaobjects/prompts/) binds a payload to external text viatextRef; the text lives indata/templates/Mustache, outsidemetaobjects/(soloadMemory()doesn't try to parse it as metadata). - Render —
promptRendergenerates a typedrender<Name>handle.mustacheTemplates(custom) inlines the.mustachefiles intosrc/render/generated/templates.tsso the Worker — which has no filesystem — can resolve them via anInMemoryProvider. - Compose —
src/council/prompts.tsis the seam where the generated handles meet hand-written composition. A byte-equivalence snapshot test pins the rendered output. - Tool call —
template.toolcall+structuredCompletiongeneratecallEmitWizardTake/callEmitVerdict: try → retry-with-stricter-reminder → optional typed fallback. - Persist / display — the lineage mappers copy the take onto its D1 columns and back;
the SSR share page (
src/render/share-page.tsx) loads the council with the generatedfindCouncilWithTurns(fromCouncil'sturnscomposition).
meta verify --templates checks that every {{var}} in a template is on its declared payload —
so a renamed payload field can't silently degrade a prompt.
src/codegen/wizardsofodd-provider.ts registers this
project's own vocabulary, strict-provenance-clean (ADR-0023):
template.streamFrame— a brand-new project-local subtype (MetaObjects core has no SSE concept). It pairs an@eventNamewith a@payloadRef;sseFrameswalks these nodes.@retryReminderon coretemplate.toolcall— a vendor attr added to a core subtype viaregistry.extend. The structured fallback payload rides the registeredattr.propertiesbag (an undeclared@fallbackwould fail load under strict provenance —ERR_UNKNOWN_ATTR).
The custom generators (sseFrames, structuredCompletion, wizardRegistry, lineageMappers,
plus mustacheTemplates + wizardData) implement the same Generator interface as the
built-ins, each writing under a target outDir so the drift gate covers their output.
npm run gen:db(meta gen) regenerates everything to the committed locations.npm run demo:drift(meta verify --templates --codegen --prompts data/templates) checks template drift ({{var}}↔ payload) and codegen drift (regenerate to a temp dir, diff the committed output).- CI (
.github/workflows/meta-verify.yml) runs that exact command on every PR — CI and local are byte-identical.
Reference projects that admit friction are believed. Real things this project hit:
- The drift gate had a blind spot. Four generated files were written outside the
drift-checked directories via
../../path escapes, so a breaking wire-format change (@eventName: wizard → wizardTake) passeddemo:driftclean. Fixed by declaring per-target output dirs and putting every generator on atarget— now the diff walks the whole surface. - The official
meta-verify-actiononly runs--templates. It has no--codegeninput, so it would miss codegen drift. CI runsnpm run demo:driftdirectly instead, so the gate is complete. - Abstract-attr hoisting doesn't work for every subtype.
field.enumshares itsvaluesvia an abstract +extends(see the stance enums), buttemplate.promptrequires@textRefas an own attr — the loader's required-attr check is own-only — so an abstracttemplate.promptthat omitstextReffails to load. The prompt declarations were consolidated into two files but keep their per-nodepayloadRef/format. - Lineage through a concrete enum loses the shared enum module.
CouncilTurn.stancecould sayextends: WizardOutput.stance, but an enum that extends a concrete field gets its own inlined enum instead of the sharedenums.tsone. So enum fields keep extending the abstract (wizardStance), andlineageMappersalso pairs same-named fields that share an abstract. - Lineage carries validators, and validators carry DB constraints. Linking
Council's verdict columns toVerdictOutputwithextendspulls inconfidence's 0–1 validator, whichmeta verify --dbreads as a new CHECK constraint oncouncils.verdict_confidence. That kept the columns hand-mapped until migration 0007 (written bymeta migrate --dialect d1) rebuiltcouncilswith the CHECK; they are now linked and mapped by the generatedCouncil.lineage.ts. - Generated writes validate. The stock
create/updatequery verbs parse their input with the insert/update schema. This app's writes don't, so adopting the verbs would start rejecting rows written today (a client-supplied visitor cookie longer thanvisitorId's 64 chars, for example). So noqueriesFile()is wired: the one generated read is the composition loader (below), and the writes are hand-written Drizzle over the generated tables and enum constants. - A nested-value-object FQN codegen bug in an earlier
@metaobjectsdev/codegen-tssurfaced from this project and was fixed upstream (thepromptRenderFQN handling in 0.15.18).
The entity modules, the barrel, the <Entity>Names constants, the prompt render handles and the
docs site run as shipped from @metaobjectsdev/codegen-ts/generators; the project keeps no copy of
them. Until MetaObjects 1.2.0 it carried ejected copies of entity, barrel and queries; the
first two had no edits of their own (only an older header) and are gone. Each generator the project
still owns emits something no shipped generator does (meta gen --list --probe is the catalog it
was checked against), and says so in its header:
| Generator | Emits | Why no shipped generator covers it |
|---|---|---|
compositionLoaders (codegen/generators/) |
Council.queries.ts: findCouncilById + findCouncilWithTurns |
stock queries emits full CRUD for every entity, has no verb filter and no composition loader |
structuredCompletion |
toolcalls.ts: forced tool_use wrappers with the toolcall's @model and @maxTokens, retry and fallback |
nothing shipped reads template.toolcall; the shipped LLM generators parse a template.prompt text reply |
sseFrames |
council-frames.ts |
template.streamFrame is project-local vocabulary |
lineageMappers |
*.lineage.ts copies along field extends |
no shipped generator maps between related objects |
mustacheTemplates |
templates.ts, the template text inlined for a Worker |
shipped render handles resolve text through a provider at runtime |
wizardData, wizardRegistry |
the typed roster and its render registry | they read instance data under data/, which shipped generators never read |
The cost test the audit applies (authored lines against emitted lines) fails for most of these: they are larger than what they emit. They stay because the point is drift protection, not line count: each turns a metadata or data change into a regenerated, drift-gated file instead of a hand edit in several places.
metaobjects.config.ts— the generator wiring + targets.docs/adr/— the durable decisions.- Upstream: metaobjects.dev.