fixes for Swift 6 and threading concerns

This commit is contained in:
2026-06-02 12:09:44 -07:00
parent 2fa4bfd45e
commit 6d3a0930a1
327 changed files with 1596 additions and 199 deletions
+76
View File
@@ -9852,3 +9852,79 @@ commits total; one session.
- `8fb88e0` Phase 198.8-198.10: drop dead .duplicateDevice case, add F11 TODO, delete POST_197_FIXES.md
- `216650a` Phase 198 review fix: Task.isCancelled guard for build-thrown CancellationError + regression test
- `84961ae` Phase 198 re-review nits: rethrow caught CancellationError, robust >= 1 result count, onTermination comment
---
## Phase 203 — `LiveCompile` UI to Swift 6 language mode
**Goal.** Flip the last two `.swiftLanguageMode(.v5)` pins in the repo —
`LyrebirdLiveCompileUI` and `LyrebirdLiveCompileUITests` in
`LiveCompile/Package.swift` — to Swift 6 language mode, so the entire codebase
(core + UI + CLI + LiveCompile + tests + the regression-fixture consumer) is
uniformly v6 and can track Swift 7/8+. This is the only target left on `.v5`
after Phase 202; unlike `LyrebirdUI`/`LyrebirdCLI` (pure caution, flipped with
zero changes in 202), this one has a real — but small — concurrency issue.
**The one real error (spiked + confirmed).** At v6 the only build error is:
```
LyrebirdScratchModel.swift:149:31: error: sending 'self.session' risks causing
data races [#SendingRisksDataRace]
```
`LyrebirdScratchModel` is `@MainActor` (an `ObservableObject`); its
`run(additive:)` calls `await session.compileAndRunAsync(...)`. `LiveSession` is
a plain non-`Sendable` `final class`, so passing it into the `nonisolated async`
method from the main actor (where it is also retained) is flagged as a send race.
**The fix (minimal, spiked → 0 errors).** Mark `LiveSession` `@unchecked
Sendable`. It is already thread-safe by construction:
- `engine` + all `-I`/`-L`/`-l`/`rpath` config are immutable `let`.
- The only mutable graph state (`trackedSynths`, `loadedSnippets`) is guarded by
`trackingLock: NSLock` at every access (`track`, `clearTracked`,
`trackedSynthCount`, `runEntry`).
- `reportedSampleRate` / `lastSnippetMessage` are test-only side channels written
from the single in-flight snippet entry (documented one-invocation-at-a-time
precondition).
Spike: `LiveSession: @unchecked Sendable` + flipping both UI targets to v6 →
`swift build --build-tests` reports **0 errors** for the `LiveCompile` sub-package.
**Plan of work.**
1. **`LiveSession.swift`** — add `: @unchecked Sendable` to the class with a doc
block justifying it (immutable config + lock-guarded mutable state). Tighten
the two test-only side-channel `var`s: either guard them under `trackingLock`
too, or mark them clearly single-writer/test-only so the `@unchecked` promise
is fully accurate (decide during implementation — guarding is cheap and
removes the only unprotected `var`s).
2. **`LiveCompile/Package.swift`** — remove `.swiftLanguageMode(.v5)` from
`LyrebirdLiveCompileUI` and `LyrebirdLiveCompileUITests` (drop the trailing
comma on the preceding `path:` line, as in the Phase 202 flips).
3. **Verify snippet runtime threading.** `compileAndRunAsync` is `nonisolated`:
it compiles off-main via `Task.detached`, then resumes on the generic
executor (NOT the caller's main actor) to call `runEntry` → the snippet's
`context.play(...)`. `SnippetContext.play` is `@MainActor` (Phase 201).
Confirm a live snippet still adds synths correctly post-flip — either the
snippet path already hops correctly, or `runEntry`/`play` needs the
`lyrebirdRunMainActorMutation` bridge treatment. **Check this at runtime, not
just at compile time** (the doc comment claims "snippet entry executes on the
caller's actor" — verify that claim holds, since a `nonisolated async` method
does not resume on the caller's actor).
4. **Docs.** `LiveCompile/.../LiveCoding.md` and any LiveCompile README — drop
`.v5` mentions if present (none found in a quick grep, but recheck).
**Acceptance.**
- `(cd LiveCompile && swift build)` and `(cd LiveCompile && swift test)` green at
v6 (current baseline: 36/36).
- A live-compiled snippet that calls `context.play(_:)` actually adds and renders
a synth (manual run of `LyrebirdScratchDemo`, or an integration test in
`LyrebirdLiveCompileTests` that drives `compileAndRunAsync` and asserts
`trackedSynthCount` increments).
- `grep -rn "swiftLanguageMode(.v5)"` across the repo (excl. `legacy/`,
`.build/`) returns **zero** hits.
**Effort.** Small — likely a single-commit phase (one `@unchecked Sendable` line +
optional lock tidy + 2 Package.swift edits + a runtime-verification test). The
only open risk is item 3 (snippet `@MainActor play` from the off-main resume),
which the runtime check resolves.
**Branch:** `phase-203-livecompile-swift6` off `main`. Standard checklist +
`/lyrebird_review` before merge.