Repository navigation
desktop (S12): land Apple Foundation Models behind the local-inference port, dark - #14395
Merged
Merged
Conversation
Register AFMLocalInferenceAdapter so forceLocalInferenceEngine=afm can select on-device guided generation. Default remains local-server. Co-authored-by: Cursor <[email protected]>
… prompts ask for Hardcoding 4096 was half this Mac's live window and is not what contextSize returns. Report SystemLanguageModel.default.contextSize when the model is available; 4096 is only the unavailable/old-OS fallback. Guided generation was omitting overview/sections because only title was required. Gate the live suite on OMI_AFM_LIVE=1. Co-authored-by: Cursor <[email protected]>
LanguageModelError.refusal was falling through to a retryable session_failed. Map the current framework errors, keep GenerationError only on the Xcode 26.6 path, reject out-of-range windows, and wrap local-server DecodingError as invalidResponse so undecodable output is not retried. Co-authored-by: Cursor <[email protected]>
…26.6 pin LanguageModelError and related replacements are macOS 27 SDK types, not 26.4. Naming them behind #if compiler compiled the mapping out of the ship toolchain. Map GenerationError only, with a runtime macOS 27 fallback that documents the retryable-refusal gap until the Xcode pin moves. Co-authored-by: Cursor <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Lands the Apple Foundation Models engine behind the existing local-inference port. Dark: the default engine is still
.localServerandOMI_FORCE_LOCAL_INFERENCE_ENGINE=afmis the only way to select AFM.The free tier replaces cloud conversation summarization with an on-device summary. That pipeline (S10/S11) already shipped dark and complete:
ConversationChunkSummarizer→LocalInferenceRuntime(fail-closed ladder: selected engine → one retry →DeterministicConversationMinimum, never cloud) →LocalProjectionFinalization→ the backendclient_processingprojection. The only registered engine wasLocalServerInferenceAdapter, an OpenAI-compatible loopback client defaulting to127.0.0.1:11434— Ollama. Ordinary users do not run Ollama, so in practice a free-tier conversation never got a summary at all.LocalInferenceEngineID.afmalready existed with the comment "S12 lands the adapter"; this is that.What it does
AFMLocalInferenceAdapterconforms toLocalInferenceService.AFMJSONSchemaBridgewalks the port's JSON Schema intoDynamicGenerationSchema/GenerationSchema— there is nojsonSchema:initializer on this SDK, so the walk is the bridge. Unsupported keywords throwcapabilityUnavailablerather than being silently dropped, and nesting deeper than 8 is rejected instead of exhausting the stack.Availability is fail-closed: anything other than
.availablefromSystemLanguageModel.default.availability— Apple Intelligence off, model not downloaded, ineligible device, OS below 26 — becomesengineUnavailable(.afm), so the runtime degrades to the deterministic minimum.runToolLoopthrowscapabilityUnavailable. The package target is macOS 14.0 and all Foundation Models use sits behindcanImportplus macOS 26 availability guards.Context window is read at runtime.
SystemLanguageModel.contextSizeis a runtime getter, not a constant; on macOS 27 with Apple Intelligence available it reports 8192. An earlier revision of this branch hardcoded 4096, which would have doubled the summarizer's map passes sinceConversationChunkSummarizeris window-agnostic and trusts this number. Values outside 256…131072 fall back to a clearly-labelled constant, because a zero or negative window yields a nominal 64-token budget andInt.minwould trap during the chunker's subtraction, bypassing the fail-closed ladder entirely.Required draft fields.
LocalSummaryDraft's schema previously required onlytitle, so guided generation legitimately omitted every optional field and produced title-only drafts. It now requires the fields the map/reduce prompts actually ask for. This schema is shared by every engine; the downstream contract tolerates absence either way (ProjectedStructure/ClientProcessinghave defaults andClientProcessingContract.assemblealways emits overview and arrays), so the change is safe for the local-server path. That path also now wraps decode failures as.invalidResponseso undecodable output is treated as deterministic and not retried.Known limitation: macOS 27 error classification under the Xcode 26.6 pin
On macOS 27 the framework throws
LanguageModelError, which is@available(macOS 27.0, *)and therefore cannot be named under the pinned ship toolchain (desktop/macos/ci/xcode-pin.json→ Xcode 26.6). So the adapter mapsLanguageModelSession.GenerationErrorand falls back at runtime behindif #available(macOS 27.0, *).Consequence, stated plainly: a 26.6-built binary running on macOS 27 classifies
LanguageModelError.refusalas a retryablesession_failed, so the runtime retries once even though refusal is documented non-retryable.This is not hidden behind a compiler gate. An earlier revision used
#if compiler(>=6.4), whichdesktop/macos/scripts/check-desktop-compiler-gates.pycorrectly rejected — that is the #12867/#13548 class, where a compiler gate compiles Apple SDK APIs out of the pinned toolchain so CI stays green while the shipped app silently builds the fallback. The checker now reportsdesktop compiler gates: none found. Closing this properly needs an Xcode pin bump, which is a separate decision. AFM ships dark, so nothing in production is exposed today.This engine alone does not deliver a free-tier summary
Found while reviewing this branch, verified independently, and out of scope here: the desktop client never renders the projection it uploads.
ServerConversation.init(APIClient+ConversationModels.swift:222-230) builds its visiblestructuredvalue fromwire.structuredalone and dropswire.clientProcessing, while the backend bind endpoint deliberately "updates onlyclient_processing. Never touchesstructured".ConversationProcessingState's docstring makes rendering the projection a client contract. So a free-tier user still sees the deterministic minimum until a client change lands. That is pre-existing, separate work, and tracked.Failure-Class: FC-advertised-capability-acknowledges-and-drops
Evidence
xcrun swift test --package-path Desktopon macOS 27 (this Mac's toolchain is Xcode 27 / Swift 6.4, newer than the 26.6 CI pin, sorun-swift-ci.shwas not used):The two skips are the
GenerationErrorclassification tests: that type isobsoleted: 27.0, so they cannot run on this Mac and will execute on CI's 26.6 runner, which is the toolchain they describe.python3 desktop/macos/scripts/check-desktop-compiler-gates.py→desktop compiler gates: none found.Live, against the real on-device model (
OMI_AFM_LIVE=1, gated so CI never needs Apple Intelligence):That is a real
StoredClientProjectionout ofConversationChunkSummarizer, not the deterministic minimum.Layer measured: unit tests plus a live on-device run on this Mac. Not measured: a macOS 14.0 launch proving dyld weak-linking of FoundationModels, and a build under the pinned Xcode 26.6 (not installed here).
🤖 Generated with Claude Code