kicad monkey v2026.11.30
On This Page (7)

Why TypeSpec & Contract Architecture

Hardware CAD toolkits rarely stay single-language for long. kicad-monkey parses KiCad S-expressions in Python and Rust, compiles hierarchical schematic graphs and Plotter IR display lists for TypeScript web viewers, emits whole-nanometre fabrication views for Gerber / Excellon / IPC-2581B / Micro-STEP, and hands structured manifests to CI pipelines and AI design-review agents.

Whenever a polyglot toolkit passes ad-hoc JSON dictionaries across those boundaries, silent wire drift sets in almost immediately:

Formal wire contracts solve this by putting the shape of the bytes on the wire in one auditable place and treating every language binding as a mechanically verified projection of that shape.


Why TypeSpec?

New and revised wire contracts in kicad-monkey are authored in TypeSpec (.tsp) and compiled to standard JSON Schema Draft 2020-12 (.json), which then drives code generation for Rust, Python, and TypeScript. TypeSpec reads like concise, reviewable type definitions rather than verbose $defs / anyOf / unevaluatedProperties boilerplate, while still emitting standard JSON Schema that any Draft 2020-12 validator can consume with zero TypeSpec toolchain installed.

Wire Shape vs. Adapter Boundary

kicad-monkey draws a sharp line between what crosses the wire and what lives in runtime code:

text
TypeSpec (.tsp): single source of truth for wire shape
  │
  ▼  @typespec/json-schema (Draft 2020-12, sealed objects, warn-as-error)
Compiled JSON Schema (.json)
  ├── Rust      : kicad-monkey-contracts  (strict serde structs, zero schemars)
  ├── Python    : kicad_monkey.contracts.generated  (frozen msgspec.Struct)
  └── TypeScript: src/ts/kicad_monkey/contracts/generated/  (zero-any strict TS)
          │
          ▼
Handwritten Python, Rust & TypeScript adapters
  ├── UTF-8 / surrogate framing, duplicate-key rejection, finite-float preflight
  ├── cross-field semantic invariants & resource budgets
  ├── out-of-band binary slot transport (source bundles, font payloads)
  └── public APIs (KiCadPcb, PcbView, KiCadFabView, kcr / k3 CLIs)

The Four Contract Tiers in KiCad Monkey

Not every JSON payload in kicad-monkey and kicad-cruncher comes from the same pipeline. Being explicit about which tier owns a contract avoids confusion when extending or auditing a schema:

TierSource of TruthCompiled ArtifactsGenerated Language BindingsScope & Notes
Tier A — kicad-monkey TypeSpec Core8 .tsp modules in src/tsp/kicad_monkey/ (rooted at main.tsp)30 Draft 2020-12 JSON Schemas in contracts/generated/schema/*.jsonRust (kicad-monkey-contracts), Python (kicad_monkey.contracts.generated via msgspec), TypeScript (src/ts/kicad_monkey/contracts/generated/)S-expression scan/build, footprint & symbol library read/edit, Plotter IR (MOD/PCB/SYM/SCH), CompiledSchematicGraph, SourceBundleManifest, FontBundleManifest, ShapingRecord, OutlineVector.
Tier B — kicad-cruncher Standalone TypeSpecpackages/kicad_cruncher/src/tsp/kicad_cruncher/main.tsppcb_svg_config.a0.schema.jsonTypeScript (pcb-svg-config.ts) + Rust (kicad-cruncher-cli, projected with Field<T> tri-state presence)Cruncher-owned pcb.svg.config schema. Intentionally separate from the core Monkey catalog; the Python loader is handwritten.
Tier C — Vendored Generated Python msgspec ContractsShared Wavenumber Data Models / Altium Monkey contract lineage (no local .tsp in cad/kicad_monkey)11 embedded Draft 2020-12 schemas in kicad_monkey.fab_view_contracts.generated + urn:dm:schema:geom.a0 in kicad_monkey.geom.generatedPython (msgspec structs & strict codecs)FabView (kicad_monkey.fab_view.a0), fab_diagnostics.a0, fab_*_options.a0, and analytic 2D geometry (geom.a0). Note: Rust kicad-monkey-fab uses its own handwritten serde model (FabViewData), not these generated Python DTOs.
Tier D — Handwritten JSON Schemas & Code-Only PayloadsHandwritten .schema.json files in docs/contracts/ and packages/kicad_cruncher/docs/contracts/, plus code-only JSONC loadersStandalone JSON SchemasHandwritten validators / loaders in Python and Rustkicad_plotter_ir_a0.schema.json, pcb_svg_enrichment_a0.schema.json, schematic_svg_enrichment_a0.schema.json, Cruncher configs (bom_pnp_config.a0.schema.json, pcb_layer_step_config.a0.schema.json, pcb_clean_config.a0.schema.json), design_review_manifest.d0.schema.json (plus a0/b0 predecessors), toon_manifest.a0.schema.json, toon_timings.a0.schema.json, library_extraction_bundle.a0.schema.json, source_relink.a0.schema.json, plus code-only fab.jsonc and vdoc.jsonc.

Keeping those four tiers distinct matters in practice: Tier A is the full tri-language TypeSpec pipeline; Tier B proves a downstream package can own its own TypeSpec root without modifying kicad-monkey; Tier C gives Python KiCadFabView exact wire parity with sibling Wavenumber CAD libraries without pretending the .tsp source lives in this repo; and Tier D covers legacy or CLI-local payloads that have not needed multi-language codegen.


Benefits for Downstream Consumers

  1. Python callers: Strict msgspec.Struct models (forbid_unknown_fields=True, frozen=True) decode megabyte-scale Plotter IR and FabView payloads at C speed while catching unknown keys, wrong numeric bounds, and missing discriminators at the boundary.
  2. Rust integrators: kicad-monkey-contracts exposes idiomatic, #![forbid(unsafe_code)], serde-only structs and validated newtypes (JavaScriptSafeInteger, NonNegativeFiniteFloat, PositiveU32, StableTextId) with zero schemars runtime dependency — suitable for tight CLI binaries and WASM targets.
  3. TypeScript & web viewers: Generated .ts modules forbid any, respect exactOptionalPropertyTypes, and guarantee every numeric field fits cleanly inside JavaScript's IEEE-754 safe-integer range (or is explicitly encoded as a canonical decimal string for uint64).
  4. AI agents & CI automation: Every published contract ships as standard JSON Schema Draft 2020-12 with explicit schema / type / version discriminators, letting automated tools validate review manifests, schematic graphs, and enriched SVGs without importing kicad-monkey.

Revision Scheme & Immutability

Every versioned wire contract uses a <letter><digit> revision token (a0, a1, b0, d0) decoupled from the kicad-monkey / kicad-cruncher package release version:

Concrete Example: design_review_manifest (a0 → b0 → d0)

kcr design / k3 design illustrates how the revision scheme works in real life:

  1. kicad_cruncher.design_review_manifest.a0 (design_review_manifest.a0.schema.json) shipped the original flat review bundle manifest.
  2. kicad_cruncher.design_review_manifest.b0 (design_review_manifest.b0.schema.json) restructured the bundle around compiled schematic graphs and per-page drawing companions — a breaking root change, so the major letter moved from a to b.
  3. kicad_cruncher.design_review_manifest.d0 (design_review_manifest.d0.schema.json) introduced the current D0 bundle layout (review_summary.json, compilation.json, bom.csv, notes.json, schematic SVGs + .layout.json, and pcb/ artifacts). (The intermediate c0 revision was an internal staging schema never published externally; the manifest version d0 indexes the governed core Design B0 data model and its companion artifacts.)

Immutability rule: Once a revision ID ships, its wire contract is frozen. A breaking change always mints a new major letter rather than silently mutating an existing a0 or d0 schema in place. Consumers should match the contract's schema (or type + version) string explicitly instead of assuming forward compatibility across major letters.


Explore the Contracts Section

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