T-019 — Plugin state and presets: a sectioned state v4 with the model’s identity, a text preset format, factory and user presets through CLAP preset discovery

Status: done (D1–D5 accepted by the maintainer, 2026-12-03) · Implementation: Codex · Area: rust · Depends on: T-009, T-025

Read for that: T-025 (Decisions D3, Outcome: state v3), Spec 003 (Presets, the theme in state, T-047’s model-keyed overrides), Plugin formats (audio-thread rules), crates/params/src/state.rs, crates/piano/src/{lib,params,model}.rs, clack-extensions 0.2 src/preset_discovery/ (features preset-discovery).

Why: state v3 (T-025) is versioned and keyed by stable ids, but it holds only parameter values. Spec 003 puts more into state (the editor’s theme, T-044; per-note overrides keyed to model identity, T-047), and there are no presets: a user cannot keep or exchange a setting outside a host project, and hosts have nothing to list. T-030 (export/import from the GUI) builds on the format this task defines.

What

  1. State v4 (crates/params): a container of tagged sections, so that later tasks add a section instead of a version.
  2. Model identity in state: the model the state was made with; a mismatch is reported, never fatal.
  3. Preset format: a text file holding the same content as state plus metadata (name, creator, description).
  4. Factory and user presets in the host: CLAP preset-discovery factory and preset-load extension, so hosts list and load them.

Out of scope: a preset browser or save button in the editor (T-030 or later), per-note overrides (T-047), the theme itself (T-044; it only gets its section here once it exists), VST3/AU program lists (clap-wrapper, later).

Decisions (accepted by the maintainer, 2026-12-03)

QuestionOptionsProposal
D1State container(a) v4: [4, count: u16 LE, count × (tag: u32 LE, len: u32 LE, len bytes)]; section PARM = T-025’s v3 records (count: u16, (id u32, value f32)…), section MODL = model identity; unknown sections skipped, a section repeated or PARM missing ⇒ refused, total size capped (64 KiB); v1–v3 still load; save always writes v4; (b) keep v3 and append trailing data; (c) a self-describing encoding (CBOR, TOML) for state too(a): binary like today, small and cheap to parse on the main thread; one rule for every later addition (EDIT for the theme, OVRD for overrides) and an older plugin skips what it does not know
D2Model identity(a) name and model_version from the manifest (salamander-c5, 2026-12-02), stored in MODL as two length-prefixed UTF-8 strings; on load with another identity the parameters still load (they are instrument-wide), the host log gets one line, and model-keyed sections (none yet) are dropped and reported; (b) a hash of model.safetensors(a): readable, stable across rebuilds; a new model directory is a new model_version already (T-039 H5)
D3Preset file(a) TOML, extension .inpiano: format = "instruments-piano-preset", format_version = 1, [preset] name, creator, description, [model] name, version, [params] keyed by the stable id as a string with the value ("1" = -6.0) and the parameter’s name in a comment written on save; unknown ids, unknown keys and a model mismatch are tolerated as in state, a wrong format or a newer format_version refused; size cap 64 KiB; (b) the state v4 bytes in a file; (c) JSON(a): diffable and hand-editable, factory presets live in the repo as text; toml and serde are already in the workspace (model-format); parsing happens on the main thread only
D4Host integration(a) a preset-discovery factory beside the plugin factory with one provider: factory presets compiled in (location Plugin, load key = file stem) and the user directory (location File, $XDG_DATA_HOME/Instruments/Piano/Presets, macOS ~/Library/Application Support/Instruments/Piano/Presets, Windows %APPDATA%\Instruments\Piano\Presets), file type .inpiano; plus the preset-load extension (load_from_location reads, parses, applies through the store as a state load, then calls the host’s loaded); (b) only preset-load, no discovery; (c) no host integration now, presets only via T-030(a): hosts that browse presets (Bitwig, Reaper) list them; clap-validator has preset-discovery tests that check it; the entry changes from SinglePluginEntry to a small custom entry
D5Factory presets(a) three: Default (all defaults), Baroque pitch (A4 = 415 Hz), Long sustain (Sustain at its maximum); more come with the parameters of T-018 and Piano 14’s quality levels; (b) only Default(a): enough to exercise the path; cheap to change later

Shared code: a Snapshot { params: Vec<(u32, f32)>, model: Option<ModelId>, unknown sections… } in crates/params with to_state/from_state (D1) and to_preset/from_preset (D3); the store applies a snapshot exactly as load_state does today (one state-load write per cell, missing ids → default). The plugin’s PluginStateImpl and preset-load both go through it, so a preset load and a state load cannot behave differently.

Done when

  • The gates of AGENTS.md pass, including cargo xtask bundle piano --release, clap-validator validate (its preset-discovery tests among the passed) and the GUI smoke test.
  • Tests (written from this page): v4 round-trip; v1, v2, v3 still load; malformed v4 refused (truncated section, len past the end, repeated section, no PARM, over the cap, count > 1024); unknown sections skipped; a mismatched model loads the parameters and reports; preset round-trip; preset with unknown ids or keys loads; wrong format, newer format_version, non-finite values (→ default) and over-long files handled; every factory preset parses and loads; a preset load and the equivalent state load leave the store identical; no_alloc still passes.
  • A host-side test (clack-host) loads a factory preset through preset-load and sees the values.
  • The default sound is unchanged (golden test).

Outcome

T-044 adds EDIT to project state and leaves the theme outside the preset format. T-047 adds NOTE (per-note edits) and a preset [notes] table; its D6 keeps those edits across a model change, revising D2’s dropped for that section.

  • crates/params: Snapshot { params, model: Option<ModelId> } with to_state/from_state (v4 sections; v1–v3 still read) in state.rs and to_preset/from_preset (TOML) in preset.rs; ParamStore::{snapshot, apply_snapshot}; load_state parses fully before writing any cell. MODL strings carry a u16 byte length each.
  • crates/piano: state save writes PARM and MODL (manifest name, model_version); PianoMainThread::apply_snapshot is the one path for a state load and a preset load (store, sync_recompute, model-mismatch warning to the host’s log, value rescan, GUI repaint). presets.rs: the preset-discovery factory (provider io.github.danielboigk.instruments.piano.presets) and preset-load; lib.rs has a custom PianoEntry serving both factories.
  • Factory presets in crates/piano/presets/*.inpiano (load keys default, baroque-pitch, long-sustain), compiled in; a test requires each to name the built-in model, so a new model means updating their [model] table.
  • Deviation from D4: the user directory is declared only when it exists, since clap-validator fails to crawl a declared directory that does not exist; a host re-crawl picks it up once created. The macOS and Windows path branches are not run-tested.
  • clap-validator pinned past 0.4.1 (flake.nix: master 55f0ff0, 2026-11-29): 0.4.1 deadlocks in its own metadata receiver on the second begin_preset() of a location (free-audio/clap-validator#54, fixed upstream in 91ee840, not yet released). Found by a backtrace of the hung preset-discovery-crawl.
  • Gates: cargo nextest run → 134 passed, 1 skipped; clap-validator validate → 44 run, 38 passed, 0 failed, 6 skipped (the three preset-discovery tests now pass); GUI smoke test passed; golden and no_alloc tests unchanged and passing.

Review triage (Mistral Large 4, 2026-12-03)

Mistral wrote nine probes but could not run them (headless process approval denied) and stopped at the turn limit; I ran them.

PointRight?Done
A preset with an unknown id holding a non-numeric value ("999" = "x") is refused, contrary to D3right (probe failed)a non-numeric value now reads as NaN (known id → default, unknown id ignored); regression test preset_tolerates_non_numeric_values
v4 with 1025 sections; repeated MODL; count = 0; invalid UTF-8 in MODL; 64 KiB boundaries of state and preset; NaN records; preset without a model; preset load ≡ state loadprobes passednone