LAYERS 6–7 · CODEGEN

Architecture & Codegen

Three duals of one Enclave. 37 IO axioms, ~110 lines trusted JS.

3Targets
595Lines
37IO Methods
66Total Files
Codegen

Three-Layer Architecture

The protocol compiles to three deployment targets. Each is a composition of a spec type with an IO boundary. The spec is determined by Enclave. The IO boundary is axiomatized. The composition is canonical — unique implementation.

S

Server

Enclave → Runtime(12) → Service
Cloudflare Durable Object. Routes, DDL, handlers.

C

Client

Enclave → ClientRT(10) → Client
TypeScript SDK. ClientView = Enclave. Zero new types.

F

Frontend

AppView → FrontendRT(8) → Frontend
React app. AppView = (Enclave, AppDef, CSSApp).

Enclave — Single Source of Truth

One structure determines the complete server, SDK, and frontend. The client does not define its own type — ClientView = Enclave.

LeanEnc/Server.lean — Enclave
structure Enclave where
  schema       : SchemaMode        -- .fixed Schema | .dynamic
  hasDataView  : Bool              -- colocated read index
  hasPush      : Bool              -- outbound P/N delivery
  hasBootstrap : Bool              -- _bootstrap endpoint
  addressing   : Addressing        -- .perRequest segment | .singleton name
  encrypted    : Bool              -- transport encryption flag
  extraRoutes  : List Route        -- app-specific routes beyond protocol

Everything derives from these 7 fields:

DerivedFormulaUsed by
allRoutesprotocolRoutes ++ [ws] ++ bootstrap? ++ extraRoutesServer routing
readsextraRoutes.filter(GET)Client SDK, Frontend StateFns
ddl[NODE_TABLES] ++ (hasDataView ? [SYNC_STATE] : [])DO constructor

The Four Services

ServiceSchemaDataViewPushBootstrapAddressingExtra RoutesReads
enc-nodedynamicperRequest20
registrystaticsingleton65
chatstaticperRequest00
timelinestaticperRequest87

Trust Boundaries

37 axiomatized IO methods. Everything else — 14,869 lines of Lean — is derived, proven, or structurally enforced.

BoundaryMethodsImplementation~Lines JS
Server Runtime12Cloudflare DO API50
Client Runtime10fetch + WebSocket30
Frontend Runtime8React hooks + DOM20
Crypto7noble-curves10
Total37~110

Frontend Codegen

58 React components across 5 applications, each fully specified in Lean. Every mapping is a bijection — one output per input, no creative decisions.

@component.state → useReducer init @component.update → pure function @component.view → JSX tree
@component.messages → { type, payload } Component.effects → useEffect Component.handlers → callbacks Component.render → JSX
CSSApp.theme → :root vars CSSApp.layout → grid CSSApp.rules → CSS CSSApp.media → @media

Lean Codebase

66 files organized in 7 layers. Each layer imports only from below. No cycles.

LayerFilesLinesWhat
1 FTA12946Abstract theory — pure math, no ENC knowledge
2 Core223,361Crypto, SMT, CT, Events, RBAC, Validation, Node, SDK
3 Middleware2361ECDH (1-to-1) + MLS (group) encryption
4 Apps197,182Registry, Timeline, DM, Chat, Messenger
5 Typeclasses31,2156 classes × 4 instances, 43 composition theorems
6 Bridge + UI2967Theory↔Protocol mapping, React component DSL
7 Codegen3595Server + Client + Frontend (three duals)
6614,869

SDD Mobile Codegen

The SDD pipeline adds five codegen scripts that generate mobile apps from app.enc.json declarations. These scripts compose with the existing codegen pipeline — they share the same Lean spec source.

ScriptLinesInputOutput
gen_from_enc.py1,628app.enc.jsonLean spec files (App.lean, SDK.lean, Components.lean)
gen_mobile.py1,036Lean @mobile specsExpo Router screen files (tabs, detail views)
gen_service_mobile.py837Lean @service specsService module (identity, signing, pull, create)
gen_test_contract.py130Lean @test_contracttestID constants (btn:, input:, list:, tab:)
gen_test_flow.py244Lean @test_flowE2E test workflow (shared across adapters)
3,875
app.enc.json (34 lines) gen_from_enc.py Lean specs
Lean specs gen_mobile.py app/(tabs)/*.tsx
Lean specs gen_service_mobile.py src/service.ts
Lean specs gen_test_contract.py + gen_test_flow.py test contracts + E2E flow

The encryption level (none/ecdh/mls) flows from JSON through Lean to the generated service module, determining whether create() calls use plain signing, ECDH key agreement, or MLS group encryption.



ENC PROTOCOL · 66 FILES · 14,869 LINES · 162 THEOREMS