v1.4.2 release: live-coding editor — Run Selection, slot release, engine panel, console, manual completion

Additive conveniences to the macOS-only Live Scratch editor. No changes to
existing public API.

- Run Selection (⌘E): evaluate just the highlighted text, additively.
- Graceful slot release: context.release(slot:) / free(slot:).
- Engine panel: live oscilloscope (ScopeCapture) + CPU avg/peak + counts.
- Reset Session / Restart to Reclaim Memory.
- Always-open console panel (report messages + errors with line:col).
- Faster typing: debounced highlight, updateNSView fast-path, publish guards.
- Manual code completion (Esc / F5), no auto-trigger on `.`; completion
  replaces the partial word, not the receiver expression.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-21 11:09:30 -07:00
parent abc64df4d7
commit 8519cabe42
21 changed files with 1578 additions and 145 deletions
@@ -64,7 +64,9 @@ struct LyrebirdScratchModelTests {
#expect(model.diagnostics.isEmpty)
#expect(model.statusMessage.contains("replaced"))
#expect(model.statusMessage.contains("Tracked synths: 0"))
#expect(session.lastSnippetMessage == "hello")
// The report message is drained from the session into the console.
#expect(model.consoleEntries.contains { $0.kind == .message && $0.text == "hello" })
#expect(session.lastSnippetMessage == nil) // drained
#expect(model.isCompiling == false)
}
@@ -0,0 +1,76 @@
// ScratchModelConsoleTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
import Testing
import LyrebirdSynth
@testable import LyrebirdLiveCompile
@testable import LyrebirdLiveCompileUI
@Suite("LyrebirdScratchModel console")
@MainActor
struct ScratchModelConsoleTests {
private func makeModel(text: String) -> LyrebirdScratchModel {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 64,
controlBlockSize: 16,
sampleRate: 48_000,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 16,
numControlBuses: 16,
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
driver: OfflineDriver()
)
return LyrebirdScratchModel(session: LiveSession(engine: engine), initialText: text)
}
@Test func successfulRunLogsInfo() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false)
#expect(model.consoleEntries.contains { $0.kind == .info })
}
@Test func failedRunLogsErrorEntries() async {
let model: LyrebirdScratchModel = makeModel(text: "NotAGenerator(freq: 1)")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false)
#expect(model.consoleEntries.contains { $0.kind == .error })
#expect(model.consoleEntries.contains { $0.text.contains("NotAGenerator") })
}
@Test func reportMessagesAppearAsMessages() async {
// A DSL run pushes no messages; drive the log directly to prove the
// message → console wiring (drainMessages → .message entries).
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
model.session.appendMessage("hello from snippet")
_ = await model.run(additive: false)
#expect(model.consoleEntries.contains { $0.kind == .message && $0.text == "hello from snippet" })
}
@Test func clearConsoleEmpties() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false)
#expect(!model.consoleEntries.isEmpty)
model.clearConsole()
#expect(model.consoleEntries.isEmpty)
}
@Test func resetSessionClearsConsole() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false)
#expect(!model.consoleEntries.isEmpty)
model.resetSession()
#expect(model.consoleEntries.isEmpty)
}
}
#endif
@@ -0,0 +1,58 @@
// ScratchModelResetTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
import Testing
import LyrebirdSynth
@testable import LyrebirdLiveCompile
@testable import LyrebirdLiveCompileUI
@Suite("LyrebirdScratchModel resetSession")
@MainActor
struct ScratchModelResetTests {
private func makeModel(text: String) -> LyrebirdScratchModel {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 64,
controlBlockSize: 16,
sampleRate: 48_000,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 16,
numControlBuses: 16,
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
driver: OfflineDriver()
)
return LyrebirdScratchModel(session: LiveSession(engine: engine), initialText: text)
}
@Test func resetStopsAudioAndClearsDiagnosticsButKeepsBuffer() async {
let source: String = "OscSin(freq: 440) * 0.1"
let model: LyrebirdScratchModel = makeModel(text: source)
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false) // tracked 1
#expect(model.trackedSynthCount == 1)
model.resetSession()
#expect(model.trackedSynthCount == 0) // audio stopped
#expect(model.diagnostics.isEmpty)
// Non-destructive: the user's buffer is preserved.
#expect(model.text == source)
#expect(model.statusMessage.contains("reset"))
}
@Test func resetLeavesAFreshDirtyBaseline() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false) // .clean
model.resetSession()
// The placeholder buffer was never run in this fresh session → .dirty.
#expect(model.reevalState == .dirty)
}
}
#endif
@@ -0,0 +1,104 @@
// ScratchModelRunSelectionTests.swift
// Lyrebird
//
// Copyright (c) 2026 Joshua Parmenter / realizedsound
// SPDX-License-Identifier: MIT
#if os(macOS)
import Testing
import LyrebirdSynth
@testable import LyrebirdLiveCompile
@testable import LyrebirdLiveCompileUI
@Suite("LyrebirdScratchModel run selection")
@MainActor
struct ScratchModelRunSelectionTests {
private func makeModel(text: String) -> LyrebirdScratchModel {
let engine: SynthesisEngine = SynthesisEngine(
blockSize: 64,
controlBlockSize: 16,
sampleRate: 48_000,
numOutputChannels: 2,
numInputChannels: 0,
numAudioBuses: 16,
numControlBuses: 16,
eventQueueCapacity: lyrebirdDefaultEventQueueCapacity,
driver: OfflineDriver()
)
return LyrebirdScratchModel(session: LiveSession(engine: engine), initialText: text)
}
@Test func hasSelectionReflectsNonWhitespaceSelection() {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
#expect(model.hasSelection == false)
model.updateSelection("OscSin(freq: 220) * 0.1")
#expect(model.hasSelection)
model.updateSelection(" \n ") // whitespace only
#expect(model.hasSelection == false)
model.updateSelection("")
#expect(model.hasSelection == false)
}
@Test func runSelectionWithNoSelectionIsNoOp() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
let ok: Bool = await model.runSelection()
#expect(ok == false)
#expect(model.trackedSynthCount == 0)
}
@Test func runSelectionAddsToRunningWithoutDisturbingBaseline() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
// Whole-buffer run establishes the baseline → clean, one synth.
_ = await model.run(additive: false)
#expect(model.reevalState == .clean)
#expect(model.trackedSynthCount == 1)
// Run a selected fragment: additive (adds a second synth) and leaves the
// whole-buffer baseline intact (buffer still matches the last whole run).
model.updateSelection("OscSin(freq: 220) * 0.1")
let ok: Bool = await model.runSelection()
#expect(ok)
#expect(model.trackedSynthCount == 2)
#expect(model.reevalState == .clean) // baseline untouched
#expect(model.diagnostics.isEmpty)
}
@Test func selectionRunAddsEvenFromADirtyBuffer() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false) // tracked 1, .clean
// Dirty the buffer (edited since the last whole run).
model.text = "OscSin(freq: 440) * 0.1\n# edited"
#expect(model.reevalState == .dirty)
model.updateSelection("OscSin(freq: 330) * 0.1")
_ = await model.runSelection() // additive → tracked 2
model.updateSelection("OscSin(freq: 110) * 0.1")
_ = await model.runSelection() // additive → tracked 3
#expect(model.trackedSynthCount == 3)
// The selection runs never replaced, and the buffer stays dirty.
#expect(model.reevalState == .dirty)
}
@Test func failedSelectionRunLeavesWholeBufferBadgeUntouched() async {
let model: LyrebirdScratchModel = makeModel(text: "OscSin(freq: 440) * 0.1")
model.switchMode(to: .graphDSL)
_ = await model.run(additive: false) // .clean
#expect(model.reevalState == .clean)
// A selection with an unknown generator fails to build.
model.updateSelection("NotAGenerator(freq: 1)")
let ok: Bool = await model.runSelection()
#expect(ok == false)
#expect(!model.diagnostics.isEmpty)
// The whole-buffer badge must stay clean — the buffer itself is fine.
#expect(model.reevalState == .clean)
#expect(model.statusMessage.hasPrefix("Selection"))
}
}
#endif
@@ -38,14 +38,49 @@ struct ScratchTextViewTests {
#expect(range == NSRange(location: text.utf16.count, length: 0))
}
/// Mid-identifier (no preceding dot) falls through to the default behavior,
/// which covers the whole partial word so it gets replaced on completion.
@Test func partialIdentifierUsesDefaultWordRange() {
/// Mid-identifier (no preceding dot): the range covers the run of identifier
/// characters before the caret, so the partial word gets replaced.
@Test func partialIdentifierCoversWordRange() {
let tv: ScratchTextView = makeTextView("OscSi", caret: 5)
let range: NSRange = tv.rangeForUserCompletion
// Default range spans the partial word `OscSi` (length 5 from offset 0).
#expect(range == NSRange(location: 0, length: 5))
}
/// Right after an open paren, the range is EMPTY at the caret — an argument
/// label (`freq:`) inserts there instead of replacing the receiver
/// (`OscSin.Audio(` → erases `OscSin.Audio` bug).
@Test func afterOpenParenRangeIsEmptyAtCaret() {
let text: String = "OscSin.Audio("
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: text.utf16.count, length: 0))
}
/// After a comma + space (next argument position), the range is empty too.
@Test func afterCommaSpaceRangeIsEmptyAtCaret() {
let text: String = "OscSin.Audio(freq: 440, "
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: text.utf16.count, length: 0))
}
/// A partial argument label inside parens replaces just that partial word,
/// not the receiver expression.
@Test func partialArgLabelCoversOnlyThePartial() {
let text: String = "OscSin.Audio(fr"
let tv: ScratchTextView = makeTextView(text, caret: text.utf16.count)
let range: NSRange = tv.rangeForUserCompletion
// `fr` starts at offset 13 (after `OscSin.Audio(`), length 2.
#expect(range == NSRange(location: 13, length: 2))
}
/// Underscore is an identifier character, so a leading-underscore partial is
/// covered whole.
@Test func underscoreIsIdentifierChar() {
let tv: ScratchTextView = makeTextView("_foo", caret: 4)
let range: NSRange = tv.rangeForUserCompletion
#expect(range == NSRange(location: 0, length: 4))
}
}
#endif