System Architecture Overview
_Last updated: September 2025_
Mosaic Builder centres on a single source of truth – the Intermediate Representation (IR). Everything you do in the product flows through the same pipeline:
Conversation → Canon (normalised spec) → IR → { runtime preview │ code generation │ validation }
This document describes that pipeline, the core services behind it, and how data moves through the system end‑to‑end.
---
1. High-Level Flow
graph LR
subgraph Chat Loop
User[User] -->|messages| ChatUI[Chat UI]
ChatUI --> SSE[SSE Stream]
SSE -->|dsl_update| DSLMgr[DSL Manager]
DSLMgr --> Canon[Canonical Model]
Canon --> IRBuilder[IR Builder]
end
subgraph IR World
IRBuilder --> IR[(IR Snapshot)]
IR --> Runtime[Runtime Preview]
IR --> Codegen[Code Generation]
IR --> Validation[Validation Pipeline]
end
IR -. persist .-> PG[(PostgreSQL)]
Runtime --> Browser
Codegen --> Repo[(Exported Project)]
---
2. Core Domains
Chat & Extraction
app/(chat)/api/chat/route.ts
streams assistant responses and DSL deltas.lib/dsl/manager.ts
records DSL events and snapshots (in-memory for tests, Postgres for prod).lib/ir/validation.ts
, lib/runtime/ir-bridge.ts
canonicalise DSL into IR and run structural validation.IR Persistence & Caching
app_models.ir_data
and are cached server-side via lib/runtime/server/ir-store.ts
(5 minute TTL, background eviction).GET /api/runtime/ir/[tenantId]
and GET /api/dsl/[chatId]
return the snapshot plus metadata/event history.Runtime Preview
UIRuntimeProvider
receives a RuntimeSnapshot
and derives a light view model on the fly.RuntimePage
uses the snapshot to resolve routes, render tables/forms/detail views, and coordinate CRUD calls via DataCache
.Code Generation
scripts/codegen/index.ts
converts an IR file into a Next.js project, Drizzle schema, migrations, workflows, and tests.--verify
(optional) runs pnpm exec tsc --noEmit
and pnpm lint
inside the generated project for quick smoke validation.---
3. Data Contracts
| Layer | Artifact | Key Files |
|-------|----------|-----------|
| DSL Extraction | DSLContext
deltas | lib/dsl/types.ts
, lib/dsl/manager.ts
|
| Canon | Normalised domain model | lib/generation/canonizer.ts
|
| IR | ir/v1
schema | lib/ir/types.ts
, lib/runtime/ir-runtime.ts
|
| Runtime Snapshot | UI-focused projection | lib/runtime/ir-runtime.ts
|
| Manifest | Codegen output ledger | scripts/codegen/utils/manifest.ts
|
---
4. Runtime Internals
graph TB
Snapshot[(RuntimeSnapshot)]
Snapshot --> ViewModel[buildViewModelFromSnapshot]
ViewModel --> Provider[UIRuntimeProvider]
Provider --> Registry[ComponentRegistry]
Provider --> Cache[DataCache]
Provider --> View{View Renderer}
View --> TableRenderer
View --> FormRenderer
View --> DetailsRenderer
Cache --> API[/api/runtime/:tenant/]
UIRuntimeProvider
keeps both the lightweight view model (for UI hints) and the raw snapshot (for routing/data decisions).RuntimePage
performs route matching against snapshot.routes
, interpolates parameters, then dispatches to renderers.DataCache
, which handles optimistic updates, IndexedDB persistence, and backend syncing.---
5. Codegen Pipeline
scripts/codegen/pipeline.ts
walks the IR and produces an ArtifactPlan[]
.writeArtifacts()
handles segmented diffing, conflict detection, hashing, and manifest updates..mosaic-conflict
files to resolve manually.--verify
runs type-check + lint inside the output for early feedback.Key templates: scripts/codegen/templates/entity*.ts
, serverActions.ts
, migration.ts
, schema.ts
.
---
6. Testing Surfaces
| Suite | Command | Purpose |
|-------|---------|---------|
| Unit | pnpm test:unit
| IR bridge, runtime helpers, codegen writer |
| Integration | NODE_ENV=test pnpm dev
+ pnpm test:integration
| Chat lifecycle, DSL replay, runtime endpoints |
| Runtime smoke | tests/runtime.test.mjs
| CRUD + migration status via runtime APIs |
| Codegen smoke | invoked from unit suite | Ensures CLI produces expected artifacts |
Playwright E2E is re-enabling soon (chat → preview loop).
---
7. External Integrations
---
8. Where to Go Next
These docs build on the architecture overview and walk through concrete usage of each subsystem.