refactor(rigs): address external-review findings

- Recall router state in applyState: the lightweight (no-rebuild) recall path
  now positionally applies router branch pedal params and the splitter/merger
  blend, instead of silently dropping them for routed rigs. Test added.
- Simplify first-run seeding: RigLibraryStore.save persists asynchronously
  (best-effort, logged not thrown), so the previous per-entry rollback could
  never fire for a real disk failure. Replaced with an honest best-effort
  save + working-rig fallback, and a comment stating the semantics.
- Move FactoryRigBuild to its own file (one public type per file).
- Document that a router's output master gain is intentionally not persisted,
  matching BoardState's treatment of board master gain.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-08-09 13:32:58 -07:00
parent 7dccf596f4
commit 2c970aa344
6 changed files with 78 additions and 40 deletions
@@ -60,6 +60,32 @@ struct ChainRouterPersistenceTests {
#expect(hasNonSilentOutput(samples), "rebuilt router produced silence")
}
@Test func applyStateRecallsRouterParamsLive() throws {
let (engine, url): (SynthesisEngine, URL) = makeNRTEngine(duration: 0.1, numAudioBuses: 128)
defer { try? FileManager.default.removeItem(at: url) }
let session: ShoeGaze = ShoeGaze(engine: engine)
let router: ChainRouter = try session.makeChainRouter()
let fuzz: FuzzPedal = FuzzPedal()
fuzz.fuzz = 20.0
try router.addToA(fuzz)
try router.addToB(ReverbPedal())
router.mode = .a
try router.finalize()
let original: ShoeGazeState = session.captureState()
// Mutate live: change a router pedal param and flip the A/B selection.
fuzz.fuzz = 55.0
router.mode = .b
// Lightweight recall should restore the router pedal param and blend without a rebuild.
session.applyState(original)
#expect(fuzz.fuzz == 20.0, "router pedal param was not recalled")
#expect(router.splitter.branchAEnabled == 1.0, "branch A level not recalled")
#expect(router.splitter.branchBEnabled == 0.0, "branch B level not recalled")
}
@Test func routerlessRigStoresNoRoutersKey() throws {
// A plain single-board session must not gain a `routers` payload (byte-compat with rigs
// saved before router persistence existed).