From 7ff201d9ed440d1b9d82380595b9b549c3ccbd5f Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Thu, 19 Nov 2026 15:07:08 -0800 Subject: [PATCH 1/2] Handle steering cases in the Generation and stop-reason switches mlx-swift-lm main (merged into PR SharpAI/mlx-swift-lm#125) adds Generation.steering and GenerateStopReason.steered/.interrupted. The SwiftLM server never enables steering, so the new cases are no-ops, and steered/interrupted map to "stop" like cancelled does. Without this, Sources/SwiftLM/Server.swift fails "switch must be exhaustive" against the merged library (CI SwiftLM integration job). Verified: swift build --target SwiftLM passes against the merged mlx-swift-lm (sync/upstream-4c21c84-merge). Co-Authored-By: Claude Haiku 5.5 --- Sources/SwiftLM/Server.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftLM/Server.swift b/Sources/SwiftLM/Server.swift index 9acedd9..5e7ea12 100644 --- a/Sources/SwiftLM/Server.swift +++ b/Sources/SwiftLM/Server.swift @@ -3525,6 +3525,9 @@ func handleChatStreaming( // contain sensitive argument text. print("[SwiftLM] Rejected tool call: reason=\(rejection.reason) tool=\(rejection.toolName ?? "?") detail=\(rejection.detail ?? "n/a")") + case .steering: + // SwiftLM never enables steering; a steering event carries no output to emit. + break case .info(let info): // `.info` is the producer's last yield, so the cache is no longer written once // it arrives. The prompt is saved here, not on the first chunk: that raced the @@ -3547,7 +3550,7 @@ func handleChatStreaming( switch info.stopReason { case .length: reason = "length" - case .cancelled, .stop: + case .cancelled, .stop, .steered, .interrupted: reason = hasToolCalls ? "tool_calls" : "stop" } // A JSON-mode reply shorter than the flush threshold is still in the @@ -3715,6 +3718,9 @@ func handleChatNonStreaming( case .rejectedToolCall(let rejection): // See the matching comment in `handleChatStreaming`: log only, no wire shape. print("[SwiftLM] Rejected tool call: reason=\(rejection.reason) tool=\(rejection.toolName ?? "?") detail=\(rejection.detail ?? "n/a")") + case .steering: + // SwiftLM never enables steering; a steering event carries no output to emit. + break case .info(let info): // The producer's last yield; see the matching comment in `handleChatStreaming`. // The stop-sequence `break` above exits before `.info`; the paths that save at the @@ -4048,6 +4054,9 @@ func handleTextStreaming( case .toolCall, .rejectedToolCall: // Text-completion endpoint: tool calling has no wire representation here. break + case .steering: + // SwiftLM never enables steering; a steering event carries no output to emit. + break case .info(let info): // The chunk count undercounts: tokens the decoder buffers (tool-call // bodies, partial text) emit no chunk. `info` carries the real count. @@ -4061,7 +4070,7 @@ func handleTextStreaming( switch info.stopReason { case .length: reason = "length" - case .cancelled, .stop: + case .cancelled, .stop, .steered, .interrupted: reason = "stop" } if !heldStopTail.isEmpty { @@ -4126,6 +4135,9 @@ func handleTextNonStreaming( if completionTokenCount % 8 == 0 { try? await Task.sleep(for: .microseconds(50)) } + case .steering: + // SwiftLM never enables steering; a steering event carries no output to emit. + break case .info(let info): generationStopReason = info.stopReason completionTokenCount = info.generationTokenCount // real count; see handleChatStreaming From d9a997e4adbf3648fae8989d812f49de82de9f97 Mon Sep 17 00:00:00 2001 From: Simba Zhang Date: Thu, 19 Nov 2026 15:28:40 -0800 Subject: [PATCH 2/2] Use @unknown default for the new stop reasons; drop the steering cases CI build_and_unit_test failed: SwiftLM builds against its pinned mlx-swift-lm (69346d0e), which has no Generation.steering, so the `case .steering` added in the previous commit does not compile there. The Generation switches already have a `default:` that covers steering, so they need nothing. Only the two stop-reason switches must change, and `@unknown default` covers GenerateStopReason.steered/.interrupted on the merged library without naming them, so the file compiles against both the pin and the merged mlx-swift-lm. Verified: swift build --target SwiftLM passes against the old pin (69346d0e) and against sync/upstream-4c21c84-merge. Co-Authored-By: Claude Haiku 5.5 --- Sources/SwiftLM/Server.swift | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/Sources/SwiftLM/Server.swift b/Sources/SwiftLM/Server.swift index 5e7ea12..01ecb3b 100644 --- a/Sources/SwiftLM/Server.swift +++ b/Sources/SwiftLM/Server.swift @@ -3525,9 +3525,6 @@ func handleChatStreaming( // contain sensitive argument text. print("[SwiftLM] Rejected tool call: reason=\(rejection.reason) tool=\(rejection.toolName ?? "?") detail=\(rejection.detail ?? "n/a")") - case .steering: - // SwiftLM never enables steering; a steering event carries no output to emit. - break case .info(let info): // `.info` is the producer's last yield, so the cache is no longer written once // it arrives. The prompt is saved here, not on the first chunk: that raced the @@ -3550,8 +3547,10 @@ func handleChatStreaming( switch info.stopReason { case .length: reason = "length" - case .cancelled, .stop, .steered, .interrupted: + case .cancelled, .stop: reason = hasToolCalls ? "tool_calls" : "stop" + @unknown default: + reason = "stop" } // A JSON-mode reply shorter than the flush threshold is still in the // buffer; it goes out before the finish chunk. @@ -3718,9 +3717,6 @@ func handleChatNonStreaming( case .rejectedToolCall(let rejection): // See the matching comment in `handleChatStreaming`: log only, no wire shape. print("[SwiftLM] Rejected tool call: reason=\(rejection.reason) tool=\(rejection.toolName ?? "?") detail=\(rejection.detail ?? "n/a")") - case .steering: - // SwiftLM never enables steering; a steering event carries no output to emit. - break case .info(let info): // The producer's last yield; see the matching comment in `handleChatStreaming`. // The stop-sequence `break` above exits before `.info`; the paths that save at the @@ -4054,9 +4050,6 @@ func handleTextStreaming( case .toolCall, .rejectedToolCall: // Text-completion endpoint: tool calling has no wire representation here. break - case .steering: - // SwiftLM never enables steering; a steering event carries no output to emit. - break case .info(let info): // The chunk count undercounts: tokens the decoder buffers (tool-call // bodies, partial text) emit no chunk. `info` carries the real count. @@ -4070,7 +4063,9 @@ func handleTextStreaming( switch info.stopReason { case .length: reason = "length" - case .cancelled, .stop, .steered, .interrupted: + case .cancelled, .stop: + reason = "stop" + @unknown default: reason = "stop" } if !heldStopTail.isEmpty { @@ -4135,9 +4130,6 @@ func handleTextNonStreaming( if completionTokenCount % 8 == 0 { try? await Task.sleep(for: .microseconds(50)) } - case .steering: - // SwiftLM never enables steering; a steering event carries no output to emit. - break case .info(let info): generationStopReason = info.stopReason completionTokenCount = info.generationTokenCount // real count; see handleChatStreaming