How dencyuinc/layerdraw works
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: compiler_endpoint.go Compile endpoint · main_native.go Engine executable · client.ts Engine client · resolve.go Symbol resolution
Components 18
Protocol Contracts
Versioned cross-language compatibility boundary.
- Versioned JSON schemas · Protocol, common, and semantic wire contracts.
schemas/engine-protocol/v1.schema.json - Generated Go bindings
gen/go/engineprotocol/types.gen.go - Generated TS protocol types
packages/protocol/src/engine.gen.ts
Go Engine
Sole semantic authority and compilation pipeline.
- Engine executable · Selects native or WebAssembly runtime behavior.
cmd/layerdraw-engine/main_native.go - Compiler facade · Runtime orchestration delegates semantic compilation here.
internal/engine/compiler.go - Endpoint negotiation · Validates version, capabilities, schema digests, and limits.
internal/engine/endpoint/negotiate.go - Compile endpoint · Bounds inputs, dispatches plans, and returns protocol envelopes.
internal/engine/endpoint/compiler_endpoint.go - LDL syntax · Parses LDL source into a CST.
internal/engine/internal/compiler/syntax/parser.go - Symbol resolution · Resolves cross-document references and emits diagnostics.
internal/engine/internal/compiler/resolve/resolve.go - Semantic graph · Builds typed entities, relations, attributes, and layers.
internal/engine/internal/compiler/semantic/graph/compile.go - Definitions · Types semantic definitions.
internal/engine/internal/compiler/semantic/definition/compile.go - Queries and views · Compiles queries and views from resolved semantics.
internal/engine/internal/compiler/query/compile.go - Normalized artifacts · Canonicalizes, hashes, and emits portable project and pack artifacts.
internal/engine/internal/compiler/materialize/compile.go - Export recipes · Derives presentation instructions without rendering semantics.
internal/engine/internal/compiler/exportrecipe/compile.go
Runtime Transports
Runtime-specific transport adapters outside compilation.
- Native stdio transport · Isolates native framing and session handling.
internal/transport/stdio/session.go - WASM bridge · 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 · Manages lifecycle, protocol collection, and client behavior.
packages/engine-client/src/internal/client.ts - WASM host package · Packages the Go WASM engine for browser and worker execution.
packages/engine-wasm/src/host.ts
Connections 23
- 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