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.

AppDef.interfaces → types.ts AppDef.sdkFuncs → sdk.ts AppDef.stateFns → api.ts
Component.state → useState 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


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