# dencyuinc/layerdraw architecture

> Architecture diagram of the GitHub repository dencyuinc/layerdraw, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-15). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/dencyuinc/layerdraw
- Repository: https://github.com/dencyuinc/layerdraw

## Overview

#### Purpose and Authority
LayerDraw is a typed, layered graph platform: LDL source defines entities, relations, attributes, layers, queries, and views; the engine produces deterministic normalized graph and export/view artifacts. The Go engine is the sole semantic authority; TypeScript packages are transport, embedding, and host adapters. Core specifications live in `docs/compiler-architecture.md`, `docs/ldl-language-specification.md`, and `docs/system-boundary-contracts-specification.md`.

#### Runtime Entry Points
The native and WebAssembly engine executable starts in `cmd/layerdraw-engine/main_native.go` and `cmd/layerdraw-engine/main_js_wasm.go`. Both host the engine facade and select transport/runtime behavior. Native clients communicate over framed stdio; browser/worker hosts communicate through the WASM bridge. The primary public compiler facade is `internal/engine/compiler.go`, with runtime orchestration in `internal/engine/engine.go`.

#### Protocol and Compatibility Boundary
Versioned JSON schemas define the cross-language contract: `schemas/engine-protocol/v1.schema.json`, `schemas/protocol-common/v1.schema.json`, and `schemas/semantic/v1.schema.json`. Generated Go bindings are under `gen/go/engineprotocol/` and generated TypeScript protocol types under `packages/protocol/src/`. Endpoint negotiation validates protocol versions, capabilities, schema digests, and limits before compilation in `internal/engine/endpoint/negotiate.go` and `internal/engine/endpoint/compiler_endpoint.go`.

#### Request Transport and Endpoint Layer
The endpoint maps wire requests into bounded compile inputs, executes compiler plans, and maps outcomes back to protocol envelopes. Relevant stages are `internal/engine/endpoint/compile_input.go`, `internal/engine/endpoint/compile_dispatch.go`, and `internal/engine/endpoint/compile_output.go`. Native framing/session handling is isolated in `internal/transport/stdio/session.go` and `internal/transport/stdio/frame.go`; the WASM equivalent is `internal/transport/wasm/bridge_js_wasm.go` and `internal/transport/wasm/session.go`. This keeps transport concerns outside semantic compilation.

#### LDL Compiler Pipeline
The compiler parses LDL into a CST, resolves cross-document symbols/references, then builds semantic graph, definitions, queries, and views. Syntax authority is `internal/engine/internal/compiler/syntax/lexer.go` and `internal/engine/internal/compiler/syntax/parser.go`. Resolution and diagnostics are in `internal/engine/internal/compiler/resolve/resolve.go`. Semantic graph construction is `internal/engine/internal/compiler/semantic/graph/compile.go`; definition typing is `internal/engine/internal/compiler/semantic/definition/compile.go`. Query and view compilation are separated in `internal/engine/internal/compiler/query/compile.go` and `internal/engine/internal/compiler/view/compile.go`.

#### Deterministic Artifacts and Export
Compiled semantics are normalized, canonicalized, hashed, and emitted as portable project/pack artifacts by `internal/engine/internal/compiler/materialize/compile.go`, `internal/engine/internal/compiler/materialize/canonical.go`, and `internal/engine/internal/compiler/materialize/hashes.go`. Export recipes derive presentation-ready instructions without moving rendering semantics into the engine: `internal/engine/internal/compiler/exportrecipe/compile.go`. Normalized artifact fixtures establish interchange expectations under `schemas/fixtures/normalized/v1/`.

#### JavaScript Integration Surfaces
`packages/engine-client/src/internal/client.ts` provides lifecycle, protocol collection, and transport-neutral client behavior; `packages/engine-client/src/stdio.ts` and `packages/engine-client/src/wasm.ts` adapt it to each engine runtime. `packages/engine-wasm/src/host.ts` and `packages/engine-wasm/src/worker-runtime.ts` package the Go WASM engine for browser or worker execution. No storage provider, renderer, collaboration server, or web application is implemented here; those remain host capabilities and planned surfaces.

Main entry points: [`internal/engine/endpoint/compiler_endpoint.go`](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/endpoint/compiler_endpoint.go) (Compile endpoint), [`cmd/layerdraw-engine/main_native.go`](https://github.com/dencyuinc/layerdraw/blob/main/cmd/layerdraw-engine/main_native.go) (Engine executable), [`packages/engine-client/src/internal/client.ts`](https://github.com/dencyuinc/layerdraw/blob/main/packages/engine-client/src/internal/client.ts) (Engine client), [`internal/engine/internal/compiler/resolve/resolve.go`](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/resolve/resolve.go) (Symbol resolution)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_contracts["Protocol Contracts"]
  node_schemas["Versioned JSON schemas<br/>schema contracts<br/>[v1.schema.json]"]
  node_go_bindings["Generated Go bindings<br/>generated bindings<br/>[types.gen.go]"]
  node_ts_protocol["Generated TS protocol types<br/>generated types<br/>[engine.gen.ts]"]
end

subgraph group_engine["Go Engine"]
  node_engine_entry{{"Engine executable<br/>native/WASM entrypoint<br/>[main_native.go]"}}
  node_engine_facade["Compiler facade<br/>public compiler API<br/>[compiler.go]"]
  node_negotiation["Endpoint negotiation<br/>compatibility gate<br/>[negotiate.go]"]
  node_endpoint["Compile endpoint<br/>request boundary"]
  node_syntax["LDL syntax<br/>lexer and parser<br/>[parser.go]"]
  node_resolution["Symbol resolution<br/>resolver<br/>[resolve.go]"]
  node_semantic_graph["Semantic graph<br/>semantic compiler<br/>[compile.go]"]
  node_definitions["Definitions<br/>definition compiler<br/>[compile.go]"]
  node_queries_views["Queries and views<br/>projection compilers<br/>[compile.go]"]
  node_materialization["Normalized artifacts<br/>materializer<br/>[compile.go]"]
  node_export_recipes["Export recipes<br/>export compiler<br/>[compile.go]"]
end

subgraph group_transports["Runtime Transports"]
  node_stdio_transport["Native stdio transport<br/>framed session<br/>[session.go]"]
  node_wasm_transport["WASM bridge<br/>WASM session<br/>[bridge_js_wasm.go]"]
end

subgraph group_typescript["TypeScript Hosts"]
  node_engine_client["Engine client<br/>transport-neutral client<br/>[client.ts]"]
  node_wasm_host["WASM host package<br/>browser/worker host<br/>[host.ts]"]
end

node_schemas -->|"generates"| node_go_bindings
node_schemas -->|"generates"| node_ts_protocol
node_engine_entry -->|"hosts"| node_engine_facade
node_engine_entry -->|"native runtime"| node_stdio_transport
node_engine_entry -->|"WASM runtime"| node_wasm_transport
node_stdio_transport -->|"framed requests"| node_endpoint
node_wasm_transport -->|"bridge requests"| node_endpoint
node_endpoint -->|"negotiates first"| node_negotiation
node_negotiation -.->|"validates contract"| node_schemas
node_endpoint -->|"dispatches compile plan"| node_engine_facade
node_engine_facade -->|"parses LDL"| node_syntax
node_syntax -->|"CST and references"| node_resolution
node_resolution -->|"resolved symbols"| node_semantic_graph
node_resolution -->|"resolved definitions"| node_definitions
node_semantic_graph -->|"semantic model"| node_queries_views
node_definitions -->|"typed definitions"| node_queries_views
node_semantic_graph -->|"normalized semantics"| node_materialization
node_queries_views -->|"compiled projections"| node_materialization
node_materialization -->|"portable artifacts"| node_export_recipes
node_engine_client -.->|"uses"| node_ts_protocol
node_engine_client -->|"native adapter"| node_stdio_transport
node_engine_client -->|"WASM adapter"| node_wasm_host
node_wasm_host -->|"hosts engine"| node_wasm_transport

click node_schemas "https://github.com/dencyuinc/layerdraw/blob/main/schemas/engine-protocol/v1.schema.json"
click node_go_bindings "https://github.com/dencyuinc/layerdraw/blob/main/gen/go/engineprotocol/types.gen.go"
click node_ts_protocol "https://github.com/dencyuinc/layerdraw/blob/main/packages/protocol/src/engine.gen.ts"
click node_engine_entry "https://github.com/dencyuinc/layerdraw/blob/main/cmd/layerdraw-engine/main_native.go"
click node_engine_facade "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/compiler.go"
click node_negotiation "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/endpoint/negotiate.go"
click node_endpoint "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/endpoint/compiler_endpoint.go"
click node_syntax "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/syntax/parser.go"
click node_resolution "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/resolve/resolve.go"
click node_semantic_graph "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/semantic/graph/compile.go"
click node_definitions "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/semantic/definition/compile.go"
click node_queries_views "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/query/compile.go"
click node_materialization "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/materialize/compile.go"
click node_export_recipes "https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/exportrecipe/compile.go"
click node_stdio_transport "https://github.com/dencyuinc/layerdraw/blob/main/internal/transport/stdio/session.go"
click node_wasm_transport "https://github.com/dencyuinc/layerdraw/blob/main/internal/transport/wasm/bridge_js_wasm.go"
click node_engine_client "https://github.com/dencyuinc/layerdraw/blob/main/packages/engine-client/src/internal/client.ts"
click node_wasm_host "https://github.com/dencyuinc/layerdraw/blob/main/packages/engine-wasm/src/host.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_schemas,node_go_bindings,node_ts_protocol toneBlue
class node_engine_entry,node_engine_facade,node_negotiation,node_endpoint,node_syntax,node_resolution,node_semantic_graph,node_definitions,node_queries_views,node_materialization,node_export_recipes toneAmber
class node_stdio_transport,node_wasm_transport toneMint
class node_engine_client,node_wasm_host toneRose
```

## Components

### Protocol Contracts

Versioned cross-language compatibility boundary.

- [Versioned JSON schemas](https://github.com/dencyuinc/layerdraw/blob/main/schemas/engine-protocol/v1.schema.json): schema contracts · Protocol, common, and semantic wire contracts. · `schemas/engine-protocol/v1.schema.json`
- [Generated Go bindings](https://github.com/dencyuinc/layerdraw/blob/main/gen/go/engineprotocol/types.gen.go): generated bindings · `gen/go/engineprotocol/types.gen.go`
- [Generated TS protocol types](https://github.com/dencyuinc/layerdraw/blob/main/packages/protocol/src/engine.gen.ts): generated types · `packages/protocol/src/engine.gen.ts`

### Go Engine

Sole semantic authority and compilation pipeline.

- [Engine executable](https://github.com/dencyuinc/layerdraw/blob/main/cmd/layerdraw-engine/main_native.go): native/WASM entrypoint · Selects native or WebAssembly runtime behavior. · `cmd/layerdraw-engine/main_native.go`
- [Compiler facade](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/compiler.go): public compiler API · Runtime orchestration delegates semantic compilation here. · `internal/engine/compiler.go`
- [Endpoint negotiation](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/endpoint/negotiate.go): compatibility gate · Validates version, capabilities, schema digests, and limits. · `internal/engine/endpoint/negotiate.go`
- [Compile endpoint](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/endpoint/compiler_endpoint.go): request boundary · Bounds inputs, dispatches plans, and returns protocol envelopes. · `internal/engine/endpoint/compiler_endpoint.go`
- [LDL syntax](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/syntax/parser.go): lexer and parser · Parses LDL source into a CST. · `internal/engine/internal/compiler/syntax/parser.go`
- [Symbol resolution](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/resolve/resolve.go): resolver · Resolves cross-document references and emits diagnostics. · `internal/engine/internal/compiler/resolve/resolve.go`
- [Semantic graph](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/semantic/graph/compile.go): semantic compiler · Builds typed entities, relations, attributes, and layers. · `internal/engine/internal/compiler/semantic/graph/compile.go`
- [Definitions](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/semantic/definition/compile.go): definition compiler · Types semantic definitions. · `internal/engine/internal/compiler/semantic/definition/compile.go`
- [Queries and views](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/query/compile.go): projection compilers · Compiles queries and views from resolved semantics. · `internal/engine/internal/compiler/query/compile.go`
- [Normalized artifacts](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/materialize/compile.go): materializer · Canonicalizes, hashes, and emits portable project and pack artifacts. · `internal/engine/internal/compiler/materialize/compile.go`
- [Export recipes](https://github.com/dencyuinc/layerdraw/blob/main/internal/engine/internal/compiler/exportrecipe/compile.go): export compiler · Derives presentation instructions without rendering semantics. · `internal/engine/internal/compiler/exportrecipe/compile.go`

### Runtime Transports

Runtime-specific transport adapters outside compilation.

- [Native stdio transport](https://github.com/dencyuinc/layerdraw/blob/main/internal/transport/stdio/session.go): framed session · Isolates native framing and session handling. · `internal/transport/stdio/session.go`
- [WASM bridge](https://github.com/dencyuinc/layerdraw/blob/main/internal/transport/wasm/bridge_js_wasm.go): WASM session · Connects browser or worker hosts to the engine. · `internal/transport/wasm/bridge_js_wasm.go`

### TypeScript Hosts

Client and WASM host integration surfaces.

- [Engine client](https://github.com/dencyuinc/layerdraw/blob/main/packages/engine-client/src/internal/client.ts): transport-neutral client · Manages lifecycle, protocol collection, and client behavior. · `packages/engine-client/src/internal/client.ts`
- [WASM host package](https://github.com/dencyuinc/layerdraw/blob/main/packages/engine-wasm/src/host.ts): browser/worker host · Packages the Go WASM engine for browser and worker execution. · `packages/engine-wasm/src/host.ts`

## Connections

- Versioned JSON schemas → Generated Go bindings: generates
- Versioned JSON schemas → Generated TS protocol types: generates
- Engine executable → Compiler facade: hosts
- Engine executable → Native stdio transport: native runtime
- Engine executable → WASM bridge: WASM runtime
- Native stdio transport → Compile endpoint: framed requests
- WASM bridge → Compile endpoint: bridge requests
- Compile endpoint → Endpoint negotiation: negotiates first
- Endpoint negotiation → Versioned JSON schemas: validates contract
- Compile endpoint → Compiler facade: dispatches compile plan
- Compiler facade → LDL syntax: parses LDL
- LDL syntax → Symbol resolution: CST and references
- Symbol resolution → Semantic graph: resolved symbols
- Symbol resolution → Definitions: resolved definitions
- Semantic graph → Queries and views: semantic model
- Definitions → Queries and views: typed definitions
- Semantic graph → Normalized artifacts: normalized semantics
- Queries and views → Normalized artifacts: compiled projections
- Normalized artifacts → Export recipes: portable artifacts
- Engine client → Generated TS protocol types: uses
- Engine client → Native stdio transport: native adapter
- Engine client → WASM host package: WASM adapter
- WASM host package → WASM bridge: hosts engine
