Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ on:

jobs:
build-and-test:
runs-on: macos-15
# mlx-swift-lm 3.x and its dependencies need Swift tools 6.3–6.4 (Xcode 26.x).
runs-on: macos-26

steps:
- uses: actions/checkout@v7

- name: Select the newest Xcode
# Plain shell, not a third-party action: the repo only allows GitHub-owned
# and verified actions, and setup-xcode is neither (startup_failure).
run: |
X=$(ls -d /Applications/Xcode_*.app | grep -v -i beta | sort -V | tail -1)
sudo xcode-select -s "$X"
xcodebuild -version
swift --version

- name: Install Metal Toolchain
# MLX Code bundles mlx-swift, which compiles Metal shaders; recent Xcode no
# longer ships the Metal Toolchain by default, so the build fails without this.
Expand All @@ -25,6 +35,8 @@ jobs:
-project "MLX Code.xcodeproj" \
-scheme "MLX Code" \
-destination 'platform=macOS' \
-skipMacroValidation \
-skipPackagePluginValidation \
CODE_SIGNING_ALLOWED=NO \
CODE_SIGNING_REQUIRED=NO \
SWIFT_TREAT_WARNINGS_AS_ERRORS=NO \
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ build/
Packages/
Package.pins
Package.resolved
# ...but commit the app's pinned dependency graph
!MLX Code.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

# macOS
.DS_Store
Expand Down
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [6.4.0] - 2026-11-06

### Changed
- **mlx-swift-lm 2.30 → 3.32.3** and **mlx-swift 0.30 → 0.32.3** (0.32.3 is the newest mlx-swift that mlx-swift-lm 3.32.3 accepts; it requires `upToNextMinor(0.32.3)`). Migrated per the upstream 3.x upgrade guide: `HubApi`/`defaultHubApi` → `HuggingFace.HubClient`, `loadContainer(hub:)` → `loadContainer(from: URL, using: #huggingFaceTokenizerLoader())`. Added the `MLXHuggingFace`, `HuggingFace` (swift-huggingface 0.13) and `Tokenizers` (swift-transformers 1.4) products
- `Package.resolved` is now committed so every build resolves the same dependency graph
- Default repetition penalty lowered from 1.8 to 1.1; persisted models still carrying the old 1.8 default are migrated on load
- `topK`, `minP` and `repetitionContextSize` are now passed to the sampler (previously only temperature/top-p/penalty were)
- Model catalog refreshed with current mlx-community 4-bit repos supported by mlx-swift-lm 3.x: Qwen3.5 9B (new default), Qwen3-Coder 30B-A3B, Devstral Small 2 24B, Qwen3.6 35B-A3B, Qwen3.6 27B, Gemma 4 E4B, Qwen3.5 2B (Qwen 2.5 7B kept as legacy)
- CI passes `-skipMacroValidation -skipPackagePluginValidation` (required for the MLXHuggingFace macros)

### Added
- **Native tool calling**: ToolRegistry tools are converted to `ToolSpec`s and passed to the chat template; `Generation.toolCall` events (including several per response) are mapped into the existing `<tool>` approval/execution flow. The text `</tool>` parser remains as a fallback
- **KV-cache settings** (Settings → Model): KV quantization none/8-bit/4-bit, max KV size (rotating cache), prefill step size — mapped into `GenerateParameters`
- `InferenceGenerator` protocol + `MLXContainerGenerator`, `GenerationParameterMapper`, `ToolSpecBuilder`, `NativeToolCallFormatter`, `ModelConfigInspector` (new `InferenceEngine.swift`)
- `InferenceEngineTests` and a rewritten `MLXServiceTests` (32 tests, mock generator, no model download) plus an opt-in real-model integration test (`MLXCODE_INTEGRATION=1`)
- Multi-model load balancing: optionally spread each chat across all installed local models (native MLX + Ollama), all frontier models (OpenRouter), and an optional Nova Gateway — health-gated and load-balanced — instead of a single pinned model
- Three independent, persisted toggles in **Settings → Balancer**: All local models, All frontier models (OpenRouter), Nova Gateway (with a configurable gateway URL and Keychain-stored OpenRouter key)
- `ModelRegistry` (model discovery + pool composition), `LoadBalancer` (round-robin / least-busy with health gating), `OpenRouterProvider` / `OpenAICompatibleRequest`, and `KeychainStore` — pure, network-free, unit-tested building blocks ported from AIStudio
Expand All @@ -17,6 +31,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- `ChatViewModel` routes generation through the balancer when any toggle is on; falls back cleanly to the single pinned MLX model when the pool is empty or all toggles are off (existing behavior preserved)

### Fixed
- **Stop now stops generation.** Previously Stop only flipped UI flags while MLXService kept generating to `maxTokens`, so the next request failed with "inference already in progress". Generation now runs in a stored, cancellable Task; MLXService checks cancellation in the stream loop, terminates the mlx-swift-lm token stream, and always resets its busy flag. The agentic tool loop does not continue after Stop, and the load balancer no longer fails over on cancellation
- Repetition/length guards now actually stop the model instead of discarding tokens while generation continues
- Model downloads only fetched `*.safetensors` and `*.json`, silently dropping `chat_template.jinja`, `merges.txt`, `tokenizer.model` etc. Patterns now live in `MLXService.downloadPatterns` and include `*.jinja`, `*.txt`, `*.model`
- Context window is now read from nested `text_config` as well as top-level `config.json` keys (Qwen3.5/3.6, Gemma 4, Devstral 2 previously fell back to 8K); discovered and downloaded models record it too
- `Python/setup.sh`: version specifiers are quoted (unquoted `mlx>=…` was a shell redirect that created the stray `Python/=0.0.10`-style files)

### Removed
- Dead Python inference scripts (`mlx_inference.py`, `mlx_daemon.py`) and the unused `huggingface_downloader.py` / `rag_system.py`, their bundle Resources entries, and the junk `Python/=*` files
- `MLXServiceTests` cases that exercised the removed Python daemon protocol

### Notes
- Nova is never a hard requirement — the balancer works with zero Nova present; the Nova Gateway is one optional, health-probed entry that drops out if unavailable

Expand Down
286 changes: 286 additions & 0 deletions MLX Code Tests/InferenceEngineTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
//
// InferenceEngineTests.swift
// MLX Code Tests
//
// Pure unit tests for the mlx-swift-lm 3.x seam: parameter mapping, KV-cache
// settings, ModelParameters decoding/migration, model catalog, config.json
// context-window parsing, ToolRegistry -> ToolSpec, native tool-call rendering.
// No model download required.
//

import XCTest
@testable import MLX_Code

/// Minimal tool used to exercise schema conversion of enum/items/default.
private struct SchemaProbeTool: Tool {
let name = "schema_probe"
let description = "Probe tool for schema tests"
let parameters = ToolParameterSchema(
properties: [
"mode": ParameterProperty(type: "string", description: "Mode", enum: ["fast", "slow"], default: "fast"),
"paths": ParameterProperty(
type: "array",
description: "Paths",
items: ParameterProperty(type: "string", description: "A path")
),
"limit": ParameterProperty(type: "integer", description: "Max results"),
],
required: ["paths"]
)

func execute(parameters: [String: Any], context: ToolContext) async throws -> ToolResult {
.success("ok")
}
}

@MainActor
final class InferenceEngineTests: XCTestCase {

// MARK: - Parameter mapping

func testParameterMappingCoversAllFields() {
let params = ModelParameters(
temperature: 0.7,
maxTokens: 1234,
topP: 0.85,
topK: 33,
minP: 0.05,
repetitionPenalty: 1.15,
repetitionContextSize: 128
)
let kv = KVCacheSettings(bits: 8, maxSize: 16384, prefillStepSize: 2048)
let mapped = GenerationParameterMapper.map(params, kvCache: kv)

XCTAssertEqual(mapped.maxTokens, 1234)
XCTAssertEqual(mapped.temperature, 0.7, accuracy: 1e-6)
XCTAssertEqual(mapped.topP, 0.85, accuracy: 1e-6)
XCTAssertEqual(mapped.topK, 33)
XCTAssertEqual(mapped.minP, 0.05, accuracy: 1e-6)
XCTAssertEqual(mapped.repetitionPenalty ?? 0, 1.15, accuracy: 1e-6)
XCTAssertEqual(mapped.repetitionContextSize, 128)
XCTAssertEqual(mapped.kvBits, 8)
XCTAssertEqual(mapped.maxKVSize, 16384)
XCTAssertEqual(mapped.prefill.stepSize, 2048)
}

func testParameterMappingDefaults() {
let mapped = GenerationParameterMapper.map(ModelParameters())
XCTAssertNil(mapped.kvBits, "Default KV cache is full precision")
XCTAssertNil(mapped.maxKVSize, "Default KV cache is unbounded")
XCTAssertEqual(mapped.prefill.stepSize, 512)
XCTAssertEqual(mapped.topK, 40)
XCTAssertEqual(mapped.minP, 0, accuracy: 1e-6)
XCTAssertEqual(mapped.repetitionPenalty ?? 0, 1.1, accuracy: 1e-6)
XCTAssertEqual(mapped.repetitionContextSize, 256)
}

func testKVCacheSettingsSanitized() {
let bad = KVCacheSettings(bits: 3, maxSize: 100, prefillStepSize: -5).sanitized()
XCTAssertEqual(bad, KVCacheSettings(bits: 0, maxSize: 0, prefillStepSize: 0))

let huge = KVCacheSettings(bits: 4, maxSize: 10_000_000, prefillStepSize: 100_000).sanitized()
XCTAssertEqual(huge.bits, 4)
XCTAssertEqual(huge.maxSize, 1_048_576)
XCTAssertEqual(huge.prefillStepSize, 8192)

// Invalid values never reach GenerateParameters
let mapped = GenerationParameterMapper.map(ModelParameters(), kvCache: KVCacheSettings(bits: 3, maxSize: 10, prefillStepSize: 0))
XCTAssertNil(mapped.kvBits)
XCTAssertNil(mapped.maxKVSize)
XCTAssertNil(mapped.prefill.stepSize, "0 = let the model choose")
}

func testAppSettingsExposeSanitizedKVSettings() {
let settings = AppSettings.shared
let saved = (settings.kvCacheBits, settings.maxKVSize, settings.prefillStepSize)
defer {
settings.kvCacheBits = saved.0
settings.maxKVSize = saved.1
settings.prefillStepSize = saved.2
}
settings.kvCacheBits = 4
settings.maxKVSize = 32768
settings.prefillStepSize = 1024
XCTAssertEqual(settings.kvCacheSettings, KVCacheSettings(bits: 4, maxSize: 32768, prefillStepSize: 1024))
settings.kvCacheBits = 5
XCTAssertEqual(settings.kvCacheSettings.bits, 0)
}

// MARK: - ModelParameters

func testDefaultRepetitionPenaltyLowered() {
XCTAssertEqual(ModelParameters().repetitionPenalty, 1.1, accuracy: 1e-9)
XCTAssertTrue(ModelParameters().isValid())
}

func testMinPValidation() {
XCTAssertTrue(ModelParameters(minP: 0).isValid())
XCTAssertTrue(ModelParameters(minP: 1).isValid())
XCTAssertFalse(ModelParameters(minP: -0.1).isValid())
XCTAssertFalse(ModelParameters(minP: 1.1).isValid())
}

func testLegacyParametersDecodeWithMigration() throws {
// Persisted before minP existed, with the old 1.8 default penalty.
let legacy = #"{"temperature":0.3,"maxTokens":999,"topP":0.8,"topK":20,"repetitionPenalty":1.8,"repetitionContextSize":64}"#
let decoded = try JSONDecoder().decode(ModelParameters.self, from: Data(legacy.utf8))
XCTAssertEqual(decoded.minP, 0)
XCTAssertEqual(decoded.repetitionPenalty, 1.1, accuracy: 1e-9, "Legacy 1.8 default migrates to 1.1")
XCTAssertEqual(decoded.maxTokens, 999)
XCTAssertEqual(decoded.repetitionContextSize, 64)

// A deliberately chosen penalty is preserved.
let custom = #"{"temperature":0.3,"maxTokens":999,"topP":0.8,"topK":20,"repetitionPenalty":1.3,"repetitionContextSize":64,"minP":0.1}"#
let decodedCustom = try JSONDecoder().decode(ModelParameters.self, from: Data(custom.utf8))
XCTAssertEqual(decodedCustom.repetitionPenalty, 1.3, accuracy: 1e-9)
XCTAssertEqual(decodedCustom.minP, 0.1, accuracy: 1e-9)

// Round-trip
let data = try JSONEncoder().encode(decodedCustom)
XCTAssertEqual(try JSONDecoder().decode(ModelParameters.self, from: data), decodedCustom)
}

// MARK: - Model catalog

func testCatalogIdsAreMLXCommunityQuantizedRepos() {
let models = MLXModel.commonModels(basePath: "/tmp/catalog-test") + [MLXModel.default(basePath: "/tmp/catalog-test")]
let quantized = try! NSRegularExpression(pattern: #"-(3|4|5|6|8)bit$"#)
for model in models {
let id = model.huggingFaceId ?? ""
XCTAssertTrue(id.hasPrefix("mlx-community/"), "\(id) is not an mlx-community repo")
let range = NSRange(id.startIndex..., in: id)
XCTAssertNotNil(quantized.firstMatch(in: id, range: range), "\(id) is not a quantized repo")
XCTAssertGreaterThan(model.contextWindowSize ?? 0, 0, "\(model.name) needs a context window")
XCTAssertTrue(model.isValid())
}
let ids = models.dropLast().compactMap(\.huggingFaceId)
XCTAssertEqual(Set(ids).count, ids.count, "Catalog IDs must be unique")
}

func testCatalogIncludesCurrentGeneration() {
let ids = Set(MLXModel.commonModels(basePath: "/tmp/catalog-test").compactMap(\.huggingFaceId))
for expected in [
"mlx-community/Qwen3-Coder-30B-A3B-Instruct-4bit",
"mlx-community/Qwen3.5-9B-4bit",
"mlx-community/Qwen3.6-27B-4bit",
"mlx-community/Qwen3.6-35B-A3B-4bit",
"mlx-community/gemma-4-e4b-it-4bit",
"mlx-community/Devstral-Small-2-24B-Instruct-2512-4bit",
"mlx-community/Qwen3.5-2B-4bit",
] {
XCTAssertTrue(ids.contains(expected), "Catalog is missing \(expected)")
}
}

// MARK: - config.json context window

func testContextWindowTopLevel() {
let json = #"{"model_type":"qwen3_moe","max_position_embeddings":262144}"#
XCTAssertEqual(ModelConfigInspector.contextWindow(fromConfig: Data(json.utf8)), 262144)
}

func testContextWindowNestedTextConfig() {
// Shape of Qwen3.5 / Gemma 4 / Mistral 3 configs on mlx-community
let json = #"{"model_type":"gemma4","vision_config":{"max_position_embeddings":4096},"text_config":{"max_position_embeddings":131072,"sliding_window":512}}"#
XCTAssertEqual(ModelConfigInspector.contextWindow(fromConfig: Data(json.utf8)), 131072)
}

func testContextWindowTopLevelWinsOverNested() {
let json = #"{"max_position_embeddings":8192,"text_config":{"max_position_embeddings":131072}}"#
XCTAssertEqual(ModelConfigInspector.contextWindow(fromConfig: Data(json.utf8)), 8192)
}

func testContextWindowAlternateKeys() {
XCTAssertEqual(ModelConfigInspector.contextWindow(fromConfig: Data(#"{"seq_length":32768}"#.utf8)), 32768)
XCTAssertEqual(ModelConfigInspector.contextWindow(fromConfig: Data(#"{"text_config":{"max_sequence_length":65536}}"#.utf8)), 65536)
}

func testContextWindowMissingOrInvalid() {
XCTAssertNil(ModelConfigInspector.contextWindow(fromConfig: Data(#"{"model_type":"llama"}"#.utf8)))
XCTAssertNil(ModelConfigInspector.contextWindow(fromConfig: Data(#"{"max_position_embeddings":0}"#.utf8)))
XCTAssertNil(ModelConfigInspector.contextWindow(fromConfig: Data("not json".utf8)))
}

// MARK: - ToolRegistry -> ToolSpec

func testToolRegistryProducesValidToolSpecs() throws {
let tools = ToolRegistry.shared.getAllTools()
XCTAssertFalse(tools.isEmpty)
let specs = ToolSpecBuilder.specs(for: tools)
XCTAssertEqual(specs.count, tools.count)

var names: [String] = []
for spec in specs {
XCTAssertEqual(spec["type"] as? String, "function")
let function = try XCTUnwrap(spec["function"] as? [String: any Sendable])
let name = try XCTUnwrap(function["name"] as? String)
names.append(name)
XCTAssertFalse((function["description"] as? String ?? "").isEmpty, "\(name) needs a description")

let parameters = try XCTUnwrap(function["parameters"] as? [String: any Sendable])
XCTAssertEqual(parameters["type"] as? String, "object")
let properties = try XCTUnwrap(parameters["properties"] as? [String: any Sendable])
let required = try XCTUnwrap(parameters["required"] as? [String])
for key in required {
XCTAssertNotNil(properties[key], "\(name): required '\(key)' missing from properties")
}
for (_, value) in properties {
let property = try XCTUnwrap(value as? [String: any Sendable])
XCTAssertNotNil(property["type"] as? String)
}
// Must be serializable to JSON (chat templates render it)
XCTAssertTrue(JSONSerialization.isValidJSONObject(spec), "\(name) spec is not JSON-serializable")
}
XCTAssertEqual(names, names.sorted(), "Specs are sorted for a stable prompt")
XCTAssertEqual(Set(names), Set(tools.map(\.name)))
}

func testToolSpecSchemaDetails() throws {
let spec = ToolSpecBuilder.spec(for: SchemaProbeTool())
let function = try XCTUnwrap(spec["function"] as? [String: any Sendable])
XCTAssertEqual(function["name"] as? String, "schema_probe")
let parameters = try XCTUnwrap(function["parameters"] as? [String: any Sendable])
XCTAssertEqual(parameters["required"] as? [String], ["paths"])
let properties = try XCTUnwrap(parameters["properties"] as? [String: any Sendable])

let mode = try XCTUnwrap(properties["mode"] as? [String: any Sendable])
XCTAssertEqual(mode["enum"] as? [String], ["fast", "slow"])
XCTAssertEqual(mode["default"] as? String, "fast")

let paths = try XCTUnwrap(properties["paths"] as? [String: any Sendable])
XCTAssertEqual(paths["type"] as? String, "array")
let items = try XCTUnwrap(paths["items"] as? [String: any Sendable])
XCTAssertEqual(items["type"] as? String, "string")

let limit = try XCTUnwrap(properties["limit"] as? [String: any Sendable])
XCTAssertEqual(limit["type"] as? String, "integer")
XCTAssertNil(limit["enum"])
}

// MARK: - Native tool call rendering

func testNativeToolCallRendersParsableToolBlock() throws {
let call = NativeToolCall(name: "bash", arguments: ["command": "ls -la", "timeout": 30, "dry_run": false])
let text = NativeToolCallFormatter.render(call)
XCTAssertTrue(text.contains("<tool>"))
XCTAssertTrue(text.contains("</tool>"))

let vm = ChatViewModel(conversationsDirectory: nil)
let raw = try XCTUnwrap(vm.extractToolCalls(text).first)
let parsed = try XCTUnwrap(ToolRegistry.shared.parseToolCallJSON(raw))
XCTAssertEqual(parsed.name, "bash")
XCTAssertEqual(parsed.parameters["command"] as? String, "ls -la")
XCTAssertEqual(parsed.parameters["timeout"] as? String, "30")
XCTAssertEqual(parsed.parameters["dry_run"] as? String, "false")
}

func testTwoNativeToolCallsInOneResponseBothExtracted() {
let response = "Checking." +
NativeToolCallFormatter.render(NativeToolCall(name: "glob", arguments: ["pattern": "*.swift"])) +
NativeToolCallFormatter.render(NativeToolCall(name: "grep", arguments: ["pattern": "FIXME"]))
let vm = ChatViewModel(conversationsDirectory: nil)
let calls = vm.extractToolCalls(response)
XCTAssertEqual(calls.count, 2)
XCTAssertEqual(calls.compactMap { ToolRegistry.shared.parseToolCallJSON($0)?.name }, ["glob", "grep"])
}
}
Loading
Loading