Make rig library loads/resolve fully off-main

Move the remaining synchronous main-thread I/O off the main actor:

- RigLibraryStore now constructs instantly with a local placeholder and
  exposes an awaitable async start(): it resolves the iCloud-or-local
  location off-main (the blocking ubiquity lookup), loads entries off-main
  via the shared ioQueue (so a reload can't drop a just-saved rig), then
  sets isLoaded. reload() is async; iCloud metadata updates re-read via it.
- Seeding moves into RigLibraryBootstrap.start(_:seedFrom:), which seeds a
  "Default" rig only AFTER the (possibly iCloud) library has loaded and is
  still empty — preventing a duplicate "Default" on every synced device on
  first launch.
- Sidebar shows a Loading state until isLoaded; the builder restores the
  persisted current-rig highlight when isLoaded flips true (initialRigID
  param dropped).
- Shells construct the store synchronously and start it asynchronously
  (macOS via Task in didFinishLaunching; iOS via .task). Codec + file
  helpers are nonisolated static so background work needs no actor hop.
- Tests await store.start().

Full suite: 206 passing.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-06-27 19:50:38 -07:00
parent 849240e6e2
commit 9a8e5461c0
9 changed files with 175 additions and 133 deletions
+12 -9
View File
@@ -60,16 +60,19 @@ still works; the sidebar shows the `icloud.slash` glyph.
### Known limitations / deferred (from pre-PR review)
- **Background writes.** Save/delete/erase update the in-memory `entries` on the main actor
- **Off-main I/O end to end.** Save/delete/erase update the in-memory `entries` on the main actor
instantly (UI never blocks) and run the file write/delete — including `NSFileCoordinator` — on a
serial background queue (`ioQueue`). `flushPendingWrites()` drains it (used by tests and at app
termination / iOS background). This removed the save-time UI stall.
- **Remaining main-thread I/O.** `RigLibraryLocation.resolve()` still does a blocking ubiquity
lookup (short-circuited to local when no iCloud account is signed in), and the *reads* in
`reloadFromDirectory()` (launch + iCloud metadata updates) are still synchronous on the main
actor. A full fix needs an async resolve + a loading state in the builder, and must move seeding
*after* the iCloud state is known — otherwise a first-launch seed races the synced library and
creates a spurious "Default" rig on every device. Deferred deliberately.
serial background queue (`ioQueue`). Location resolution (`RigLibraryLocation.resolve()`, the
blocking ubiquity lookup) and the initial/refresh *reads* now also run off the main thread:
`RigLibraryStore` constructs instantly with a local placeholder, and `await start()` resolves the
real iCloud-or-local location, loads via `ioQueue`, then sets `isLoaded`. Reads share `ioQueue`
with writes, so a reload never drops a just-saved rig. `flushPendingWrites()` drains the queue
(tests, app termination / iOS background).
- **Loading + correct first-run seeding.** The sidebar shows a "Loading…" state until `isLoaded`.
`RigLibraryBootstrap.start(_:seedFrom:)` seeds the working rig as "Default" **only after** the
(possibly iCloud) library has loaded and is still empty — so a first launch on a device that
already has synced rigs does not create a duplicate "Default" everywhere. The builder restores the
persisted current-rig highlight once `isLoaded` flips true.
- **`eraseAll()`** clears the in-memory list even if a file deletion fails (it still throws the
first error). A re-`start()` re-scans and re-surfaces any undeleted rigs, so state self-heals.
- Possible later work: conflict UI for simultaneous multi-device edits; the async-resolve redesign