memlnaut-nisps/codegen
monkey-w1n5t0n f5b571412f refactor(codegen): codegen owns mode identity, per-mode schemas and net dims
Phase 3 (S1, S5, S6, S25, L11, L37, ST11, ST12). Behaviour-preserving by
construction: the diff on the generated directories is PURELY ADDITIVE (217
insertions, 0 deletions), so no emitted constant changed value. This moves where
truth lives; it does not change what truth says.

- S5: nine mode headers each carried a mechanically identical 12-field
  positional ParamSchema aggregate. codegen now emits one
  `inline constexpr ParamSchema k<Mode>Schema` per mode, and the struct itself
  moved into generated/schema_types.hpp. Each param_schema() is a one-line
  return.
- S6 + S25: every mode hand-typed its net shape a second time as MLP template
  args, duplicating the schema's own dims. codegen emits a `<Mode>MLP` alias
  built from the already-emitted constants (not re-literalled), and all nine
  modes use it. NMaxExamples still defaults from kDefaultMaxExamples (Phase 2).
- S1: model.ts hand-imported all nine schemas by name and hand-paired each with
  its overlay — so the SET of modes was hand-maintained and could silently drift
  from codegen. codegen now emits ALL_MODE_SCHEMAS; SCHEMA_MODE_OVERLAYS is
  purely display truth (label/glyph/css/order), which stays hand-curated.
- L37: deleted the hand-written modeEngineId switch, which duplicated
  schema.engine_id and silently defaulted unknown modes to 'thru'. Routes on
  MFMode.engineId with exactly one documented exception: sound_analysis_midi
  declares engine_id 'thru' because its ModeBase audio slot really is
  NoOpEngine, while it separately drives the real AnalysisEngine.
- L11: ExternalSynthMIDIMode's shape was literal in two places; now named once
  in an ext_synth_defaults namespace with an ExtSynthMIDIMLP alias. Full folding
  into the JSON pipeline is NOT done and the reason is recorded in-file: it is a
  template family over an externally-supplied Device and variable NOut, with no
  single (device, NOut) schema to author.
- ST12: extracted codegen/lib.ts for the helpers both generators duplicated, and
  corrected the comment that claimed they had to be separate. Proof the
  extraction was behaviour-free: regenerating the MIDI-device outputs produces a
  byte-identical tree.
- ST11: deleted codegen/templates/ — dead "reference" files no generator reads,
  already drifted from the real emitters.

Gates: run-all-tests.sh ALL GREEN; codegen idempotent (re-running both
generators yields no further diff), which is what CI's dirty-diff gate checks.
2026-07-21 14:02:23 +02:00
..
tests refactor(codegen): codegen owns mode identity, per-mode schemas and net dims 2026-07-21 14:02:23 +02:00
.gitignore Add codegen tool: schemas -> C++ headers + TS modules 2026-04-29 15:29:08 +03:00
generate-midi-devices.ts refactor(codegen): codegen owns mode identity, per-mode schemas and net dims 2026-07-21 14:02:23 +02:00
generate.ts refactor(codegen): codegen owns mode identity, per-mode schemas and net dims 2026-07-21 14:02:23 +02:00
lib.ts refactor(codegen): codegen owns mode identity, per-mode schemas and net dims 2026-07-21 14:02:23 +02:00
package.json Add codegen tool: schemas -> C++ headers + TS modules 2026-04-29 15:29:08 +03:00
README.md Add codegen tool: schemas -> C++ headers + TS modules 2026-04-29 15:29:08 +03:00
seed-midi-devices.ts feat(midi-devices): canonical external-synth CC templates + dual codegen 2026-06-28 20:03:17 +02:00
tsconfig.json Add codegen tool: schemas -> C++ headers + TS modules 2026-04-29 15:29:08 +03:00

MEMLNaut Mode-Schema Codegen

Bun + TypeScript tool that turns schemas/modes/*.json into:

  • C++ headers under nisps/modes/generated/<mode_id>_schema.hpp (constexpr data, no runtime cost).
  • TypeScript modules under playground/src/modes/generated/<mode_id>_schema.ts (typed ModeSchema objects).

The schemas are validated against schemas/schema.json (JSON Schema Draft 2020-12) on every run. Codegen exits non-zero if any schema fails validation.

Run

cd codegen
bun install        # one-shot, fetches ajv + types
bun run generate.ts   # or: bun run generate

The script writes everything into the two output dirs in one shot. Re-running with no schema changes is a no-op (byte-identical output).

Tests

tests/golden_test.ts regenerates from the live schemas into a temp dir and diffs against tests/golden/. The golden directory contains a snapshot of paf_synth_schema.{hpp,ts}. To refresh after intentional codegen changes:

bun run generate.ts
cp ../nisps/modes/generated/paf_synth_schema.hpp tests/golden/
cp ../playground/src/modes/generated/paf_synth_schema.ts tests/golden/

Run the test:

bun run test

Adding a new mode

  1. Drop a new <mode_id>.json into schemas/modes/ (must validate against schemas/schema.json).
  2. Run bun run generate.ts.
  3. Commit the JSON + the regenerated C++/TS pair.

Layout

codegen/
├── package.json         # ajv (+ formats) + bun-types
├── tsconfig.json
├── generate.ts          # ~400 lines, single entrypoint
├── README.md            # this file
├── templates/           # reference templates (NOT consumed — for review)
│   ├── cpp_schema.hpp.template
│   └── ts_schema.ts.template
└── tests/
    ├── golden_test.ts   # diffs latest output against tests/golden/
    └── golden/
        ├── paf_synth_schema.hpp
        └── paf_synth_schema.ts

Conventions

  • C++ namespace: nisps::modes::generated. All generated symbols are inline constexpr so #include-ing the header in multiple TUs is safe.
  • C++ types: std::array, std::string_view, std::size_t. No std::vector, no heap.
  • Curve enum: declared in nisps/modes/generated/schema_types.hpp as a temporary local copy. Once stream 1 lands nisps/core/math.hpp, replace the local enum with an #include (search for TODO(stream-1) in the generated header).
  • Float literals: emitted with explicit .f suffix and decimal point, per the perf contract (architecture §3.3).
  • Order: schemas are processed in alphabetical order of mode_id so output is stable.
  • Naming: mode_id is snake_case in JSON; the generated C++ const prefix is k + PascalCase (e.g. kPafSynthParams); TS const is PascalCase + Schema (e.g. PafSynthSchema).