# christiannhill/patchcad architecture

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

- Interactive diagram: https://gitdiagram.com/christiannhill/patchcad
- Repository: https://github.com/christiannhill/patchcad

## Overview

#### 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: [`apps/server/src/main.ts`](https://github.com/christiannhill/patchcad/blob/main/apps/server/src/main.ts) (Fastify server), [`apps/server/src/providers.ts`](https://github.com/christiannhill/patchcad/blob/main/apps/server/src/providers.ts) (Providers & verified library), [`packages/engine/src/backend.ts`](https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/backend.ts) (Backend contract), [`packages/engine/src/architect.ts`](https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/architect.ts) (Planning & renegotiation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_studio["Studio"]
  node_studio_app["React Studio<br/>Vite UI<br/>[App.tsx]"]
  node_studio_api_store["Studio API &amp; state<br/>client state<br/>[api.ts]"]
end

subgraph group_server["Server orchestration"]
  node_server["Fastify server<br/>localhost API<br/>[main.ts]"]
  node_projects[("Project persistence<br/>project repository<br/>[project.ts]")]
  node_providers_library["Providers &amp; verified library<br/>server integration<br/>[providers.ts]"]
end

subgraph group_engine["Shared engine"]
  node_shared_model["Graph persistence model<br/>shared types<br/>[graph.ts]"]
  node_engine_orchestrator["Planning &amp; renegotiation<br/>engine orchestration<br/>[architect.ts]"]
  node_graph_store["Port-granular graph store<br/>graph state<br/>[store.ts]"]
  node_cook_pipeline["Cook pipeline<br/>generation workflow<br/>[cook.ts]"]
  node_backend_contract{{"Backend contract<br/>engine interface<br/>[backend.ts]"}}
  node_llm_claude["Claude adapter<br/>LLM adapter<br/>[index.ts]"]
  node_llm_openai["OpenAI-compatible adapter<br/>LLM adapter<br/>[index.ts]"]
end

subgraph group_cad["CAD execution"]
  node_cad_backend["CAD backend<br/>engine backend<br/>[index.ts]"]
  node_cad_assembly["CAD assembly<br/>assembly builder<br/>[assembly.ts]"]
  node_python_kernel["Python CAD kernel<br/>FastAPI/build123d service<br/>[main.py]"]
end

subgraph group_alternate["Reusable web-code path"]
  node_code_backend["Web-code backend<br/>engine backend<br/>[backend.ts]"]
  node_preview_runtime["Preview runtime<br/>iframe runtime<br/>[index.tsx]"]
end

node_studio_app -->|"uses"| node_studio_api_store
node_studio_api_store -->|"API and WebSocket events"| node_server
node_server -->|"loads and saves"| node_projects
node_projects -->|"persists"| node_shared_model
node_server -->|"plan, reprompt, cook"| node_engine_orchestrator
node_engine_orchestrator -->|"applies contract changes"| node_graph_store
node_graph_store -->|"schedules dirty nodes"| node_cook_pipeline
node_engine_orchestrator -->|"targets backend interface"| node_backend_contract
node_cook_pipeline -->|"cooks through"| node_backend_contract
node_providers_library -->|"providers and reuse"| node_cook_pipeline
node_server -->|"configures"| node_providers_library
node_providers_library -.->|"selects"| node_llm_claude
node_providers_library -.->|"selects"| node_llm_openai
node_backend_contract -->|"CAD implementation"| node_cad_backend
node_cad_backend -->|"builds"| node_cad_assembly
node_cad_assembly -->|"assembly and bindings"| node_python_kernel
node_server -->|"manages availability"| node_python_kernel
node_backend_contract -->|"web-code implementation"| node_code_backend
node_code_backend -->|"preview integration"| node_preview_runtime
node_python_kernel -->|"CAD result"| node_studio_app

click node_studio_app "https://github.com/christiannhill/patchcad/blob/main/apps/studio/src/App.tsx"
click node_studio_api_store "https://github.com/christiannhill/patchcad/blob/main/apps/studio/src/api.ts"
click node_server "https://github.com/christiannhill/patchcad/blob/main/apps/server/src/main.ts"
click node_projects "https://github.com/christiannhill/patchcad/blob/main/apps/server/src/project.ts"
click node_providers_library "https://github.com/christiannhill/patchcad/blob/main/apps/server/src/providers.ts"
click node_shared_model "https://github.com/christiannhill/patchcad/blob/main/packages/shared/src/graph.ts"
click node_engine_orchestrator "https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/architect.ts"
click node_graph_store "https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/graph/store.ts"
click node_cook_pipeline "https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/cook.ts"
click node_backend_contract "https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/backend.ts"
click node_llm_claude "https://github.com/christiannhill/patchcad/blob/main/packages/llm-claude/src/index.ts"
click node_llm_openai "https://github.com/christiannhill/patchcad/blob/main/packages/llm-openai-compat/src/index.ts"
click node_cad_backend "https://github.com/christiannhill/patchcad/blob/main/packages/backend-cad/src/index.ts"
click node_cad_assembly "https://github.com/christiannhill/patchcad/blob/main/packages/backend-cad/src/assembly.ts"
click node_python_kernel "https://github.com/christiannhill/patchcad/blob/main/packages/backend-cad/kernel/src/patchcad_kernel/main.py"
click node_code_backend "https://github.com/christiannhill/patchcad/blob/main/packages/backend-code/src/backend.ts"
click node_preview_runtime "https://github.com/christiannhill/patchcad/blob/main/packages/preview-runtime/src/index.tsx"

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_studio_app,node_studio_api_store toneBlue
class node_server,node_projects,node_providers_library toneAmber
class node_shared_model,node_engine_orchestrator,node_graph_store,node_cook_pipeline,node_backend_contract,node_llm_claude,node_llm_openai toneMint
class node_cad_backend,node_cad_assembly,node_python_kernel toneRose
class node_code_backend,node_preview_runtime toneIndigo
```

## Components

### Studio

- [React Studio](https://github.com/christiannhill/patchcad/blob/main/apps/studio/src/App.tsx): Vite UI · Plan controls, graph inspection, and CAD viewport. · `apps/studio/src/App.tsx`
- [Studio API & state](https://github.com/christiannhill/patchcad/blob/main/apps/studio/src/api.ts): client state · `apps/studio/src/api.ts`

### Server orchestration

- [Fastify server](https://github.com/christiannhill/patchcad/blob/main/apps/server/src/main.ts): localhost API · Owns project operations, events, providers, and kernel lifecycle. · `apps/server/src/main.ts`
- [Project persistence](https://github.com/christiannhill/patchcad/blob/main/apps/server/src/project.ts): project repository · Stores code-free graphs and versioned per-node sources. · `apps/server/src/project.ts`
- [Providers & verified library](https://github.com/christiannhill/patchcad/blob/main/apps/server/src/providers.ts): server integration · Resolves generation providers and reuses verified contract-hash entries. · `apps/server/src/providers.ts`

### Shared engine

- [Graph persistence model](https://github.com/christiannhill/patchcad/blob/main/packages/shared/src/graph.ts): shared types · Cross-package graph IDs, events, and hashes. · `packages/shared/src/graph.ts`
- [Planning & renegotiation](https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/architect.ts): engine orchestration · Runs architecting, contract diffing, tiers, approvals, and scheduling. · `packages/engine/src/architect.ts`
- [Port-granular graph store](https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/graph/store.ts): graph state · Propagates changes and marks downstream nodes dirty. · `packages/engine/src/graph/store.ts`
- [Cook pipeline](https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/cook.ts): generation workflow · Generates independent nodes from contracts and verifies/cooks them. · `packages/engine/src/cook.ts`
- [Backend contract](https://github.com/christiannhill/patchcad/blob/main/packages/engine/src/backend.ts): engine interface · Allows domain backends and LLM providers to share orchestration. · `packages/engine/src/backend.ts`
- [Claude adapter](https://github.com/christiannhill/patchcad/blob/main/packages/llm-claude/src/index.ts): LLM adapter · `packages/llm-claude/src/index.ts`
- [OpenAI-compatible adapter](https://github.com/christiannhill/patchcad/blob/main/packages/llm-openai-compat/src/index.ts): LLM adapter · Supports OpenRouter, Ollama, and LM Studio endpoints. · `packages/llm-openai-compat/src/index.ts`

### CAD execution

- [CAD backend](https://github.com/christiannhill/patchcad/blob/main/packages/backend-cad/src/index.ts): engine backend · Translates graph contracts into assembly and kernel bindings. · `packages/backend-cad/src/index.ts`
- [CAD assembly](https://github.com/christiannhill/patchcad/blob/main/packages/backend-cad/src/assembly.ts): assembly builder · `packages/backend-cad/src/assembly.ts`
- [Python CAD kernel](https://github.com/christiannhill/patchcad/blob/main/packages/backend-cad/kernel/src/patchcad_kernel/main.py): FastAPI/build123d service · 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](https://github.com/christiannhill/patchcad/blob/main/packages/backend-code/src/backend.ts): engine backend · Assembles, executes, type-checks, and verifies web-app graphs. · `packages/backend-code/src/backend.ts`
- [Preview runtime](https://github.com/christiannhill/patchcad/blob/main/packages/preview-runtime/src/index.tsx): iframe runtime · Provides live parameters and per-node error isolation. · `packages/preview-runtime/src/index.tsx`

## Connections

- 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
