Persist I/O + input via the rig, not UserDefaults

The rig snapshot already stores inputSource + input/output channel counts and
syncs them via the autosave envelope and the iCloud rig library. Make that the
single source of truth instead of the parallel UserDefaults path.

- RigStore.restore applies the saved channel layout (applyChannelLayout) before
  loadRig, while stopped at launch, so a saved live-input channel validates
  against the restored count instead of falling back to the test loop. The
  general loadRig policy (don't force layout mid-session) is unchanged.
- applyInputSource and the engine-config onConfigApplied now autosave, so input
  and I/O changes are captured into the rig immediately.
- Remove the UserDefaults keys and read/write sites: ShoeGaze.inputSelection
  (builder view + iOS model) and the ShoeGaze.inputChannels/outputChannels keys
  added earlier (EngineConfigView + both app targets build at the default layout
  now; RigStore.restore supplies the rig's layout).
- Fresh-launch default: the seed paths set inputSource = liveInput(0) when the
  engine has an input channel, so a first launch listens to the instrument. A
  restored rig keeps its own saved input; the view's on-appear hook only requests
  mic access for a restored live selection (test-loop fallback on denial).

Test: RigStore.restoreAppliesSavedChannelLayoutAndInput. 461 tests pass; both
app targets build.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-11 15:34:23 -07:00
parent 5987ec7b78
commit cc3a986cde
7 changed files with 121 additions and 133 deletions
@@ -45,25 +45,39 @@ call `engine.start()` directly and are unaffected — they preserve the current
mic access and selects `.liveInput(channel: 0)` (falling back to test loop on denial / no
input), instead of only auto-selecting when already authorised.
## (b) Persist In/Out channel counts (+ reliable last-input)
## (b) Persist In/Out + input via the rig (iCloud), not UserDefaults
The last-used input **selection** already persists (`ShoeGaze.inputSelection`), but the
engine's **channel counts** do not — so on relaunch the engine reverts to the app default
(1 in / 2 out). A saved input channel `>=` the smaller default count then fails validation
and silently falls back to the test loop. That is the "In doesn't persist" bug.
The rig snapshot (`ShoeGazeState`) **already** persists `inputSource`, `numInputChannels`,
and `numOutputChannels`, and the autosave envelope + rig library (iCloud) already carry
them. Two problems remained:
Fix: persist the channel counts too.
1. `loadRig(from:)` deliberately does **not** apply the saved channel layout (forcing it on
every mid-session load would fight the global engine config / the iOS live-input rebuild).
So at **launch**, the engine kept the app default (1 in / 2 out); a saved live-input
channel `>=` that count failed validation and fell back to the test loop — the "In
doesn't persist" bug.
2. A **second, competing** persistence path (`ShoeGazeBuilderView`'s UserDefaults
`ShoeGaze.inputSelection`, plus the iOS model's copy) fought the rig's own `inputSource`.
- WRITE: `EngineConfigView` persists `pendingInputs` / `pendingOutputs` to UserDefaults
(`ShoeGaze.inputChannels` / `ShoeGaze.outputChannels`) whenever they change while stopped.
- READ/APPLY at launch, where the engine is actually constructed:
- macOS `AppDelegate`: seed `numInputChannels` / `numOutputChannels` from the saved values
(fallback 1 / 2).
- iOS `ShoeGazeBuilderiOSAppModel`: seed `numOutputChannels` from saved (fallback 2). Input
stays on the existing permission-gated rebuild flow.
Fix — make the rig the single source of truth:
Key strings are duplicated in the app targets with a "must match" comment, matching the
existing convention for `inputSelectionDefaultsKey`.
- **Launch applies the saved layout.** `RigStore.restore` calls
`applyChannelLayout(from: state)` **before** `loadRig`, while the engine is stopped, so the
input-channel validation runs against the restored count. A launch restore is exactly the
place this is wanted (unlike a mid-session load), so the general `loadRig` policy is
unchanged.
- **Input changes autosave.** `applyInputSource` / the engine-config `onConfigApplied` call
`autosave()`, so a changed input or I/O layout is captured into the rig immediately.
- **Removed both UserDefaults paths.** The `ShoeGaze.inputSelection` / `ShoeGaze.inputChannels`
/ `ShoeGaze.outputChannels` keys and their read/write sites (view + both app targets) are
deleted. The app targets build the engine at the default layout; `RigStore.restore` supplies
the rig's layout.
- **Fresh-launch default.** When there is no autosaved rig, the seed paths
(`AppDelegate.seedInitialBoard` / iOS `seedFallbackBoard`) set `inputSource = .liveInput(0)`
when the engine has an input channel, so a first launch listens to the instrument rather
than the test loop. A restored rig keeps its own saved input. The builder view's on-appear
hook now only requests mic access for a restored live-input selection (falling back to the
test loop on denial).
## Tests