How christiannhill/patchcad works
Purpose and entry points
PatchCAD is a local, single-user prompt-to-application system centered on CAD: an LLM plans a contract-pinned node graph, generates isolated node implementations, verifies/cooks them, and displays the assembled result in a live studio. Node monorepo entry points are the Fastify server at apps/server/src/main.ts, React/Vite studio at apps/studio/src/main.tsx, and shared engine exports at packages/engine/src/index.ts.
Persisted project and graph model
Projects persist a graph without code plus versioned per-node source files; examples and server fixtures follow this format. Shared graph types, events, hashes, and IDs define the cross-package persistence boundary in packages/shared/src/graph.ts, packages/shared/src/events.ts, and packages/shared/src/hash.ts. Server project loading/saving and example discovery are centered in apps/server/src/project.ts.
Planning, contracts, and change tiers
The domain-agnostic engine runs the architect pass, contract diffing, tier classification, renegotiation, and cook scheduling. Its key boundary is a backend interface plus LLM-provider interface, allowing CAD and web-code implementations to share orchestration: packages/engine/src/architect.ts, packages/engine/src/renegotiate.ts, packages/engine/src/backend.ts. Graph changes are stored and propagated port-granularly through packages/engine/src/graph/store.ts and packages/engine/src/graph/diff.ts; T0 parameter edits avoid LLM calls, while interface changes require approval and dirty downstream nodes.
Generation, verification, and reuse
After plan approval, independent nodes generate in parallel from their own contract and neighboring contracts—not neighbor source. The engine cook pipeline is packages/engine/src/cook.ts; server wiring selects providers and captures/reuses verified contract-hash library entries via apps/server/src/providers.ts and apps/server/src/library.ts. LLM adapters support Anthropic and OpenAI-compatible endpoints (OpenRouter, Ollama, LM Studio): packages/llm-claude/src/index.ts, packages/llm-openai-compat/src/index.ts.
CAD execution boundary
The CAD backend translates graph contracts into assembly/bindings and communicates with a separate Python kernel: packages/backend-cad/src/index.ts, packages/backend-cad/src/kernel.ts, packages/backend-cad/src/assembly.ts. The server manages kernel availability rather than embedding geometry execution. The kernel is a FastAPI/build123d Python service with warm isolated workers and geometry gates, exposed from packages/backend-cad/kernel/src/patchcad_kernel/main.py, with worker orchestration in packages/backend-cad/kernel/src/patchcad_kernel/pool.py and validation in packages/backend-cad/kernel/src/patchcad_kernel/gates.py. It requires uv, Python 3.12, build123d, and OCP; default service port is 8621.
Alternate web-code backend and preview
A parallel backend supports existing web-app graphs: source assembly, execution/type checks, contract verification, and preview integration live in packages/backend-code/src/backend.ts, packages/backend-code/src/assemble.ts, and packages/backend-code/src/verify.ts. The iframe-side preview runtime provides live parameters and per-node error isolation at packages/preview-runtime/src/index.tsx. This is architectural reuse, though studio planning currently targets CAD.
Server API, events, and studio
The localhost Fastify server owns persistence, plan/cook/reprompt operations, undo checkpoints, WebSocket event streaming, provider resolution, and kernel lifecycle: apps/server/src/main.ts, apps/server/src/cad-acceptance.ts. The React studio consumes that API through apps/studio/src/api.ts and state store apps/studio/src/store.ts; it renders plan controls, graph canvas, inspection, and CAD viewport in apps/studio/src/App.tsx, apps/studio/src/canvas/Canvas.tsx, and apps/studio/src/CadViewport.tsx. Development runs server on 4100, studio on 5173, and preview on 5174.
Main entry points: main.ts Fastify server · providers.ts Providers & verified library · backend.ts Backend contract · architect.ts Planning & renegotiation
Components 17
Studio
- React Studio · Plan controls, graph inspection, and CAD viewport.
apps/studio/src/App.tsx - Studio API & state
apps/studio/src/api.ts
Server orchestration
- Fastify server · Owns project operations, events, providers, and kernel lifecycle.
apps/server/src/main.ts - Project persistence · Stores code-free graphs and versioned per-node sources.
apps/server/src/project.ts - Providers & verified library · Resolves generation providers and reuses verified contract-hash entries.
apps/server/src/providers.ts
Shared engine
- Graph persistence model · Cross-package graph IDs, events, and hashes.
packages/shared/src/graph.ts - Planning & renegotiation · Runs architecting, contract diffing, tiers, approvals, and scheduling.
packages/engine/src/architect.ts - Port-granular graph store · Propagates changes and marks downstream nodes dirty.
packages/engine/src/graph/store.ts - Cook pipeline · Generates independent nodes from contracts and verifies/cooks them.
packages/engine/src/cook.ts - Backend contract · Allows domain backends and LLM providers to share orchestration.
packages/engine/src/backend.ts - Claude adapter
packages/llm-claude/src/index.ts - OpenAI-compatible adapter · Supports OpenRouter, Ollama, and LM Studio endpoints.
packages/llm-openai-compat/src/index.ts
CAD execution
- CAD backend · Translates graph contracts into assembly and kernel bindings.
packages/backend-cad/src/index.ts - CAD assembly
packages/backend-cad/src/assembly.ts - Python CAD kernel · Separate geometry service with warm isolated workers and geometry gates.
packages/backend-cad/kernel/src/patchcad_kernel/main.py
Reusable web-code path
- Web-code backend · Assembles, executes, type-checks, and verifies web-app graphs.
packages/backend-code/src/backend.ts - Preview runtime · Provides live parameters and per-node error isolation.
packages/preview-runtime/src/index.tsx
Connections 20
- React Studio → Studio API & state: uses
- Studio API & state → Fastify server: API and WebSocket events
- Fastify server → Project persistence: loads and saves
- Project persistence → Graph persistence model: persists
- Fastify server → Planning & renegotiation: plan, reprompt, cook
- Planning & renegotiation → Port-granular graph store: applies contract changes
- Port-granular graph store → Cook pipeline: schedules dirty nodes
- Planning & renegotiation → Backend contract: targets backend interface
- Cook pipeline → Backend contract: cooks through
- Providers & verified library → Cook pipeline: providers and reuse
- Fastify server → Providers & verified library: configures
- Providers & verified library → Claude adapter: selects
- Providers & verified library → OpenAI-compatible adapter: selects
- Backend contract → CAD backend: CAD implementation
- CAD backend → CAD assembly: builds
- CAD assembly → Python CAD kernel: assembly and bindings
- Fastify server → Python CAD kernel: manages availability
- Backend contract → Web-code backend: web-code implementation
- Web-code backend → Preview runtime: preview integration
- Python CAD kernel → React Studio: CAD result