kicad monkey v2026.11.30
On This Page (11)

The Multi-Language Codegen Pipeline

Writing a contract once in TypeSpec only pays off if every target language stays mechanically locked to the compiled schemas. kicad-monkey enforces that lock with deterministic generators and --check CI gates across JSON Schema, Python, TypeScript, and Rust.


Toolchain & Exact Commands

Pipeline 1 — Core 30 Contracts (kicad-monkey)

The core catalog lives under src/tsp/kicad_monkey/ (rooted at main.tsp) and compiles with @typespec/compiler and @typespec/json-schema (tspconfig.yaml sets seal-object-schemas: true and warn-as-error: true):

text
src/tsp/kicad_monkey/*.tsp (8 modules + main.tsp)
  │
  │  npm run generate:contracts
  ▼
contracts/generated/schema/*.json (30 Draft 2020-12 schemas)
  ├── npm run generate:python      ──▶ src/py/kicad_monkey/contracts/generated.py (msgspec)
  ├── npm run generate:typescript  ──▶ src/ts/kicad_monkey/contracts/generated/*.ts (strict TS)
  └── cargo run -p kicad-monkey-codegen --locked
                                   ──▶ src/rs/kicad-monkey-contracts/src/generated/*.rs (serde)
StageGenerate CommandCheck CommandGenerator Tooling
TypeSpec → JSON Schemanpm run generate:contractsnpm run check:typespec (compile check: --no-emit, warn-as-error)@typespec/compiler + @typespec/json-schema (seal-object-schemas: true, warn-as-error: true)
JSON Schema → Pythonnpm run generate:pythonnpm run check:python-generationscripts/generate-contract-bindings.mjs --python → msgspec.Struct
JSON Schema → TypeScriptnpm run generate:typescriptnpm run check:typescript-generationscripts/generate-contract-bindings.mjs --typescript (json-schema-to-typescript + tsc --noEmit -p tsconfig.contracts.json)
JSON Schema → Rustcargo run --package kicad-monkey-codegen --lockedcargo run --package kicad-monkey-codegen --locked -- --checkkicad-monkey-codegen (schemars + typify + syn + prettyplease + rustfmt)

npm run check:typespec verifies TypeSpec compilation (--no-emit with warn-as-error), while the three binding --check commands above — plus node packages/kicad_cruncher/scripts/generate-svg-contracts.mjs --check for standalone PcbSvgConfig — regenerate in memory and fail with a non-zero exit code if any committed artifact differs by even one byte.

Pipeline 2 — Cruncher Standalone Config (PcbSvgConfig)

kicad-cruncher owns its own standalone TypeSpec root for pcb.svg.config without touching the core kicad-monkey contract catalog:

text
packages/kicad_cruncher/src/tsp/kicad_cruncher/main.tsp
  │
  │  node packages/kicad_cruncher/scripts/generate-svg-contracts.mjs [--check]
  ▼
  ├── packages/kicad_cruncher/docs/contracts/pcb_svg_config.a0.schema.json
  ├── packages/kicad_cruncher/src/ts/contracts/pcb-svg-config.ts
  └── packages/kicad_cruncher/src/rs/kicad-cruncher-cli/src/pcb_svg/generated.rs
        (via `cargo run --locked -p kicad-monkey-codegen -- --standalone ...`)

The --standalone mode of kicad-monkey-codegen reuses the same Draft 2020-12 normalization and typify/syn machinery as the core pipeline, then runs a dedicated config projection pass (config_projection.rs) that wraps optional override fields in tri-state Field<T> structs so the native Rust k3 pcb-svg merger can distinguish omitted keys from explicit null resets. The Python pcb-svg config loader remains handwritten.


Key Engineering Projections

Raw JSON Schema code generators rarely produce sound, ergonomic bindings out of the box across Python, Rust, and TypeScript. kicad-monkey's codegen scripts and kicad-monkey-codegen apply five deliberate engineering projections so every target language enforces the same invariants.

1. Safe Integers & Decimal-String uint64 Across JS/WASM Boundaries

JavaScript numbers are IEEE-754 float64 values that silently lose integer precision outside ±(2^53 - 1) (±9,007,199,254,740,991). Two contract scalars eliminate silent truncation:

2. Out-of-Band Slot-Indexed Binary Payloads

Real KiCad projects routinely embed megabytes of .kicad_pcb S-expressions, raster sheet images, and OpenType/TrueType font files. Inlining those bytes as base64 inside JSON envelopes multiplies memory usage, slows parsing, and bloats wire logs.

Instead, the two manifest contracts keep raw bytes out-of-band in parallel binary buffers indexed by zero-based slot: SourceBundleManifest (source-bundle-a0.tsp) carries SourceSlot indices and CanonicalUint64Decimal source_bytes lengths, while FontBundleManifest (font-text-a0.tsp) carries uint32 slot indices alongside Sha256Hex sha256 digests. JSON stays compact and fast to validate; binary payloads stay zero-copy.

3. Closed Object Enforcement Across All Three Languages

tspconfig.yaml enables seal-object-schemas: true, which causes @typespec/json-schema to emit "unevaluatedProperties": { "not": {} } on every closed model. Each language generator lowers that seal to native strictness:

4. kicad-monkey-codegen Rust AST Transforms

typify expects JSON Schema Draft 07 / schemars::schema::RootSchema shapes and produces verbose generic boilerplate if fed raw Draft 2020-12 output. kicad-monkey-codegen bridges that gap cleanly in three stages (schema normalization → typify::TypeSpace → syn AST rewrite + prettyplease + rustfmt):

5. Python Strict JSON Preflight (preflight_strict_json)

JSON parsers — including fast native decoders — often accept duplicate object keys (silently keeping the last value) or non-standard float tokens (NaN, Infinity). Before decoding generated contract payloads, Python's _generated_codecs.py runs preflight_strict_json (_strict_json.py) to reject:

Python msgspec tagged unions also handle an edge case automatically in scripts/generate-contract-bindings.mjs: because msgspec cannot assign multiple literal tag values to a single Struct, union members whose discriminator is an enum expand into one tagged Struct per tag value beneath a Union alias that preserves the published type name.


Public Repository vs. Development Monorepo Boundary

The public wavenumber-eng/kicad_monkey repository ships everything a downstream consumer needs to validate and use every contract immediately — with no Node.js or TypeSpec installation required:

Regenerating bindings after editing a .tsp module requires Node.js (npm + @typespec/compiler) and Cargo (kicad-monkey-codegen) in the full development workspace. Downstream users consuming kicad-monkey from PyPI or embedding the Rust crates never need to invoke TypeSpec or Node.js.


kicad-monkey v2026.11.30·Docs updated 2026.12.01 15:43 UTC (fd9b75b)