Skip to content

Harden the Foundation Models test fixtures - #599

Merged
thechriswebb merged 1 commit into
ml-explore:mainfrom
thechriswebb:webb/style-pr1-fixtures
Sep 1, 2026
Merged

thechriswebb merged 1 commit into
ml-explore:mainfrom
thechriswebb:webb/style-pr1-fixtures

Conversation

@thechriswebb

Copy link
Copy Markdown
Collaborator

The problem

An audit of MLXFoundationModels against Swift's published style guidance surfaced eight test fixture files that broke concrete rules, not just style preferences. Two channel drains swallowed every error in an empty catch block. Six load and warmup tasks discarded their result with try?. Five stub types asserted thread safety with @unchecked Sendable instead of the compiler proving it. One stub enforced an OS availability requirement with a runtime guard the compiler could enforce instead. Each of these could hide a real failure or a real race and let a broken fixture report a passing test.

What this PR does

  • Extracts one channel-drain helper in GenerationEventObserverTests and asserts its result. A drain that fails with anything but cancellation now fails the test, instead of vanishing into an empty catch {}.
  • Asserts the specific error every load and warmup task throws in AvailabilityTests and ModelCacheEvictionTests, instead of discarding it with try?.
  • Converts five stateless downloader and tokenizer-loader stubs from classes marked @unchecked Sendable to structs, so the compiler proves Sendable instead of the annotation asserting it.
  • Replaces NSLock and @unchecked Sendable on EventBox and SeedRecorder, the two fixtures that hold mutable state across a task boundary, with the standard library's Mutex.
  • Spells out a two-closure call in GenerationEventObserverTests that swift-format's OnlyOneTrailingClosureArgument rule flagged, so it is unambiguous which parameter each closure binds to.
  • Marks EvictBiasStubTokenizerLoader with the OS 27 availability its returned tokenizer already requires, replacing a runtime guard and a preconditionFailure that could never fire.

Tests

  • pre-commit run --all (swift-format): passes with zero files changed.
  • xcodebuild build-for-testing for mlx-swift-lm-Package: builds clean.
  • xcrun xctest MLXFoundationModelsTests.xctest: 139 tests in 21 suites pass, run 20 times in a row with no flakes.
  • xcrun xctest MLXLMTests.xctest: 722 tests in 55 suites pass. This is CI's only required suite, but it covers none of this PR's diff; MLXFoundationModelsTests.xctest is the suite that exercises it.
  • Mutation check: inverted one #expect in each of the six affected test files, confirmed all six produce the expected failure, then reverted and confirmed the suite returns to 139 tests in 21 suites passing.

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

AI usage

  • I have read this PR description in full and approve it as my own, and it
    accurately describes the code changes.
  • AI usage disclosure: I used AI for all of this, but understand the code that I'm contributing.

- In `GenerationEventObserverTests`, two tasks drained the generation channel with an empty `catch {}` block. A read failure there would pass the test silently. Extracts one drain helper. Each test now awaits its result and accepts only a cancellation error.
- Asserts the expected error from every load and warmup task instead of discarding it with `try?`.
- The downloader and tokenizer-loader test stubs were classes with no stored state, marked `@unchecked Sendable`. That annotation tells the compiler to trust the type is safe to share across threads, without the compiler checking it. Converts the stubs to structs, which are `Sendable` by construction, so the compiler proves the guarantee instead of the annotation asserting it.
- `EventBox` and `SeedRecorder` hold mutable state a closure captures across a task boundary, so a struct will not work for them. They used `NSLock` with the same `@unchecked Sendable` escape hatch as the stubs above. Replaces the lock with the standard library's `Mutex`, so these types are provably `Sendable` too, instead of asserting it.
- In `GenerationEventObserverTests`, `withValue` took two closures, one passed as an argument and one as a trailing closure. swift-format's `OnlyOneTrailingClosureArgument` rule flags that form because it is easy to misread which parameter each closure binds to. Spells out the second closure with its argument label instead.
- `EvictBiasStubTokenizerLoader.load()` returns a `CountingTokenizer`, which requires OS 27. The old code checked that at runtime with a `guard` and a `preconditionFailure` that could never actually fire. Marks the stub itself with the OS 27 requirement, so the compiler enforces it and the dead runtime check is gone.

An empty catch and a discarded task result both hid a real loader failure, so a broken load could not fail a test. The fixtures also claimed thread safety by annotation where value semantics or a mutex states it, and asserted an availability requirement at runtime that the type system can enforce at compile time.

@davidkoski davidkoski left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good to me, thanks!

@thechriswebb
thechriswebb merged commit 5694a2f into ml-explore:main Sep 1, 2026
3 checks passed
djandrewgibbons pushed a commit to djandrewgibbons/mlx-swift-lm that referenced this pull request Sep 26, 2026
- In `GenerationEventObserverTests`, two tasks drained the generation channel with an empty `catch {}` block. A read failure there would pass the test silently. Extracts one drain helper. Each test now awaits its result and accepts only a cancellation error.
- Asserts the expected error from every load and warmup task instead of discarding it with `try?`.
- The downloader and tokenizer-loader test stubs were classes with no stored state, marked `@unchecked Sendable`. That annotation tells the compiler to trust the type is safe to share across threads, without the compiler checking it. Converts the stubs to structs, which are `Sendable` by construction, so the compiler proves the guarantee instead of the annotation asserting it.
- `EventBox` and `SeedRecorder` hold mutable state a closure captures across a task boundary, so a struct will not work for them. They used `NSLock` with the same `@unchecked Sendable` escape hatch as the stubs above. Replaces the lock with the standard library's `Mutex`, so these types are provably `Sendable` too, instead of asserting it.
- In `GenerationEventObserverTests`, `withValue` took two closures, one passed as an argument and one as a trailing closure. swift-format's `OnlyOneTrailingClosureArgument` rule flags that form because it is easy to misread which parameter each closure binds to. Spells out the second closure with its argument label instead.
- `EvictBiasStubTokenizerLoader.load()` returns a `CountingTokenizer`, which requires OS 27. The old code checked that at runtime with a `guard` and a `preconditionFailure` that could never actually fire. Marks the stub itself with the OS 27 requirement, so the compiler enforces it and the dead runtime check is gone.

An empty catch and a discarded task result both hid a real loader failure, so a broken load could not fail a test. The fixtures also claimed thread safety by annotation where value semantics or a mutex states it, and asserted an availability requirement at runtime that the type system can enforce at compile time.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants