Skip to content

Gemma 4 26B-A4B inference in ~2 GB of RAM on any M-series MacBook

40.8 KB
Raw Blame
import Foundation
import Metal

public enum MultimodalConversationError: Error, CustomStringConvertible {
    case closed
    case busy
    case lineageBroken
    case lineageRecoveryFailed(reason: String)
    case emptyTurn
    case contextExhausted(prompt: Int, reserve: Int, maxContext: Int)
    case imageUnavailable(reason: String?)
    case lineageNotEmpty(kvTokens: Int)
    case invalidReplayImage(reason: String)
    /// A recorded token ID is not one this model has. Restore is the one entry
    /// point that prefills IDs it did not tokenize itself, and the embedding
    /// lookup indexes its table by the raw value.
    case invalidReplayToken(index: Int, token: Int32, vocabSize: Int)

    public var description: String {
        switch self {
        case .closed: "conversation is closed"
        case .busy: "a turn is already generating on this conversation"
        case .lineageBroken:
            "generation failed partway, so the KV no longer matches this "
                + "conversation; call reset() to start over"
        case .lineageRecoveryFailed(let reason):
            "generation failed partway and the KV could not be restored; "
                + "call reset() to start over. \(reason)"
        case .emptyTurn: "a turn needs text or an image"
        case .contextExhausted(let prompt, let reserve, let maxContext):
            "conversation needs \(prompt) tokens plus \(reserve) kept free for a "
                + "reply, beyond the \(maxContext)-token context"
        case .imageUnavailable(let reason):
            reason.map { "image support is unavailable: \($0)" }
                ?? "image support is unavailable: no companion pack is installed"
        case .lineageNotEmpty(let kvTokens):
            "a stored conversation can only be restored into an empty KV; this "
                + "one already holds \(kvTokens) tokens"
        case .invalidReplayImage(let reason):
            "a stored image cannot be replayed: \(reason)"
        case .invalidReplayToken(let index, let token, let vocabSize):
            "the stored record holds token \(token) at position \(index), "
                + "outside this model's \(vocabSize)-entry vocabulary"
        }
    }
}

/// How much context a turn keeps free for the model to answer in.
///
/// The guard used to admit any turn whose prompt alone fit, leaving room for
/// exactly one generated token, while the refusal the user saw promised their
/// whole max response — so a chat near the ceiling was accepted and then
/// answered with a word. One constant now decides both that guard and whether a
/// stored conversation can be continued at the current context length, because
/// a row that says "continue" and a turn that then refuses are the same defect
/// seen from two places.
///
/// It is a floor, not the requested `maxNewTokens`: the reply length is a
/// setting the user can change at any time, and a stored conversation's
/// continuability must not move with it.
public enum ConversationGenerationReserve {
    public static let tokens = 256

    /// What the smallest continuation turn costs before its first word.
    ///
    /// `generate` prefills the lineage plus the message wrapped in the chat
    /// template's turn markers and thought-channel opener, so a lineage that
    /// fits with exactly `tokens` to spare fits nothing the user can type.
    /// Rows within that margin of the ceiling were offered as continuable,
    /// replayed in full, and then refused every message. The figure is an
    /// upper bound on `encodeTextContinuation` with a one-token message; the
    /// tokenizer test pins it against the real template.
    public static let turnEnvelope = 16

    /// Whether a prompt of `tokens` still leaves room to answer.
    ///
    /// The only place the ceiling is written down. `generate` asks it with
    /// the whole prompt; `fitsLineage` asks it for a stored conversation
    /// with the smallest turn added, so a row that offers to continue and a
    /// turn that then refuses cannot disagree — which they did while both
    /// asked this function with different inputs.
    public static func fits(tokens: Int, maxContext: Int) -> Bool {
        tokens + Self.tokens <= maxContext
    }

    /// Whether a stored lineage of `tokens` can take at least one turn.
    ///
    /// `tokens` counts everything the next turn's prompt starts with: the KV
    /// and any boundary token the last run left outside it.
    public static func fitsLineage(tokens: Int, maxContext: Int) -> Bool {
        fits(tokens: tokens + turnEnvelope, maxContext: maxContext)
    }

    /// The smallest context that lets a stored lineage of `tokens` take a
    /// turn: the figure `needsContext` reports.
    public static func contextRequired(forLineage tokens: Int) -> Int {
        tokens + turnEnvelope + Self.tokens
    }
}

/// One image of a stored conversation, as replay needs it: where its soft
/// tokens sit in the lineage, the model-input copy on disk, and the digest that
/// copy has to hash to.
public struct MultimodalReplayImage: Sendable {
    public let tokenRange: Range<Int>
    public let pixelsURL: URL
    public let expectedDigest: String

    public init(tokenRange: Range<Int>, pixelsURL: URL, expectedDigest: String) {
        self.tokenRange = tokenRange
        self.pixelsURL = pixelsURL
        self.expectedDigest = expectedDigest
    }
}

/// A conversation's KV as a sequence of token IDs, ready to be prefilled back.
///
/// `tokenIDs` is the effective sequence — every turn's prompt and reply
/// concatenated in order, exactly what `kvTokenIDs` held. The embedding
/// sequence is not carried: it differs from the effective one only inside image
/// spans, so it is derived from `images` rather than stored twice and given the
/// chance to disagree.
public struct MultimodalConversationLineage: Sendable {
    public let tokenIDs: [Int32]
    public let images: [MultimodalReplayImage]
    public let uncommittedBoundary: [Int32]
    public let boundaryNeedsReplay: Bool

    public init(tokenIDs: [Int32],
                images: [MultimodalReplayImage] = [],
                uncommittedBoundary: [Int32] = [],
                boundaryNeedsReplay: Bool = false) {
        self.tokenIDs = tokenIDs
        self.images = images
        self.uncommittedBoundary = uncommittedBoundary
        self.boundaryNeedsReplay = boundaryNeedsReplay
    }

    /// Everything the next turn will put in front of its message: the KV and
    /// the boundary token a stopped run left outside it, when that token is
    /// replayed ahead of the turn.
    public var promptPrefixCount: Int {
        tokenIDs.count + (boundaryNeedsReplay ? uncommittedBoundary.count : 0)
    }

    /// Refuses a record that names a token this model does not have.
    ///
    /// Every other prefill feeds the tokenizer's own output; this record came
    /// off disk, or over the decode service's socket, and the embedding kernel
    /// reads its table at `token * rowBytes` with nothing in between. A
    /// negative or too-large ID was an out-of-bounds GPU read: either a KV
    /// built from whatever memory lay past the table, reported as a restored
    /// conversation, or a GPU fault the app could only attribute to a lost
    /// connection. The boundary tokens are checked too; they enter the same
    /// prefill.
    public func validateTokenIDs(vocabSize: Int) throws {
        for (index, token) in tokenIDs.enumerated()
        where token < 0 || Int(token) >= vocabSize {
            throw MultimodalConversationError.invalidReplayToken(
                index: index, token: token, vocabSize: vocabSize)
        }
        for (offset, token) in uncommittedBoundary.enumerated()
        where token < 0 || Int(token) >= vocabSize {
            throw MultimodalConversationError.invalidReplayToken(
                index: tokenIDs.count + offset, token: token, vocabSize: vocabSize)
        }
    }
}

enum MultimodalTurnRecord {
    /// The tokens a turn's reply left in the KV.
    ///
    /// Taken as a suffix of the post-turn record rather than counted by the
    /// decode loop, so a stop-string trim is already applied: those tokens are
    /// gone from the cache, and a transcript that still listed them would
    /// replay a context the model no longer has. The clamp is not padding — a
    /// trim that lands on the prompt boundary leaves nothing generated at all.
    static func generatedTokenIDs(kvTokenIDs: [Int32], promptCount: Int) -> [Int32] {
        guard kvTokenIDs.count > promptCount else { return [] }
        return Array(kvTokenIDs.suffix(kvTokenIDs.count - promptCount))
    }
}

enum MultimodalConversationKVRecovery {
    static func restoreAfterFailure(
        positionBefore: Int,
        generationError: Error,
        rewind: (Int) throws -> Void,
        reset: () -> Void
    ) throws {
        guard positionBefore > 0 else {
            reset()
            return
        }
        do {
            try rewind(positionBefore)
        } catch {
            throw MultimodalConversationError.lineageRecoveryFailed(
                reason: "The turn failed with \(generationError). Rewinding to "
                    + "token \(positionBefore) then failed with \(error).")
        }
    }

    static func trimHiddenStopTokens(
        _ tokenIDs: [Int32],
        withheld: Int,
        rewind: (Int) throws -> Void,
        reset: () -> Void
    ) throws -> [Int32] {
        guard withheld > 0, withheld <= tokenIDs.count else {
            throw MultimodalConversationError.lineageRecoveryFailed(
                reason: "Stop-string cleanup reported \(withheld) hidden tokens "
                    + "for a \(tokenIDs.count)-token KV.")
        }
        let target = tokenIDs.count - withheld
        if target == 0 {
            reset()
        } else {
            do {
                try rewind(target)
            } catch {
                throw MultimodalConversationError.lineageRecoveryFailed(
                    reason: "Removing stop-string tokens required rewinding to "
                        + "token \(target), which failed with \(error).")
            }
        }
        return Array(tokenIDs.prefix(target))
    }
}
public struct MultimodalTurnResult: Sendable {
    public let text: String
    public let promptTokens: Int
    /// Tokens served from the retained KV rather than prefilled again.
    public let cachedTokens: Int
    /// Tokens this turn put through prefill. `RawCompletion` computes it as
    /// `promptIds.count - cachedPromptTokens`, so it is a derivation of the two
    /// figures beside it, not an independent count: asserting that it equals
    /// `promptTokens - cachedTokens` cannot fail and proves nothing. What it is
    /// good for is reporting — the figure a reader wants when asking what a
    /// turn cost. The claim that the KV was reused rests on `cachedTokens`
    /// matching the previous turn's `kvTokens`, and on `prefillSeconds`.
    public let computedPrefillTokens: Int
    /// Tokens the KV holds after this turn. Not `promptTokens +
    /// completionTokens`: a run that stops on max tokens or is cancelled holds
    /// its final token outside the KV, so that sum is one too many.
    public let kvTokens: Int
    public let completionTokens: Int
    /// Why the turn ended; `.cancelled` when `checkCancellation` stopped the
    /// decode mid-turn, in which case `text` holds the partial reply and the
    /// conversation remains resumable.
    public let reason: StopReason
    /// Wall time spent prefilling this turn's own tokens. Reported so a caller
    /// can show what resuming actually saved rather than asserting that it did.
    public let prefillSeconds: Double
    public let decodeSeconds: Double
    /// What this turn's user half put into the KV: the continuation-encoded
    /// prompt, behind any boundary token the previous run left outside the
    /// cache and this one replayed. Not the whole prompt — the earlier turns
    /// were already recorded when they ran.
    public let promptTokenIDs: [Int32]
    /// What this turn's assistant half left in the KV, after a stop-string
    /// match has had its hidden tokens trimmed. Not the tokens the model
    /// emitted: a run that stops on max tokens or is cancelled holds its final
    /// token outside the cache, and that token is in `uncommittedBoundaryTokenIDs`.
    ///
    /// Concatenating every turn's `promptTokenIDs` and `generatedTokenIDs` in
    /// order reproduces the KV exactly. Re-tokenising the transcript's text
    /// cannot: the pinned template strips historical thought spans and trims
    /// content, so the record has to be the IDs.
    public let generatedTokenIDs: [Int32]
    public let uncommittedBoundaryTokenIDs: [Int32]
    public let boundaryNeedsReplay: Bool
}

/// A stateful multi-turn conversation that owns its own KV lineage.
///
/// The server has to *match* a stateless request against a cached prefix and
/// fail closed when it cannot. A conversation does not: it appended every token
/// in the KV itself, so it always knows the boundary and always resumes. Each
/// turn prefills only the new tokens, and an image is encoded once, when its
/// turn is appended.
public actor MultimodalConversation {
    private let model: Model
    private let context: MetalContext
    private let tokenizer: GFTokenizer
    private let runner: RealForwardRunner
    private let scratch: RawCompletionScratch
    private let visionRuntime: VisionRuntime?
    private let visionRuntimeError: Error?
    private let visionResidency: VisionResidencyPolicy
    private let maxContext: Int

    /// Exactly the tokens the KV holds, in order.
    private var kvTokenIDs: [Int32] = []
    private var pending: (parts: [MultimodalContinuationPart], images: [URL])?
    private var closed = false
    /// One generation at a time. `RawCompletionScratch` documents that its
    /// buffers and sampler belong to a single run and that the guard is the
    /// caller's job; the actor alone does not provide it, because `generate`
    /// suspends for the whole decode.
    private var generating = false
    /// Resumed by `finishGeneration()`; see `waitForGeneration()`.
    private var generationWaiters: [CheckedContinuation<Void, Never>] = []
    /// Barrier for `reset()`, set before it awaits the in-flight decode so a
    /// `generate()` racing the reset cannot start and have its KV wiped.
    private var resetting = false
    /// Set when a run throws after prefill. The runner's KV has advanced but
    /// `kvTokenIDs` has not, so the lineage is unusable until reset.
    private var lineageBroken = false
    /// Tokens the model emitted that never entered the KV, which happens when
    /// a run stops on max tokens or is cancelled. The next turn must replay
    /// them, exactly as the server's prefix cache does.
    private var uncommittedBoundary: [Int32] = []
    private var boundaryNeedsReplay = false

    public init(model: Model,
                context: MetalContext,
                tokenizer: GFTokenizer,
                runner: RealForwardRunner,
                scratch: RawCompletionScratch,
                visionRuntime: VisionRuntime? = nil,
                visionRuntimeError: Error? = nil,
                visionResidency: VisionResidencyPolicy = .defaultPolicy,
                maxContext: Int) {
        self.model = model
        self.context = context
        self.tokenizer = tokenizer
        self.runner = runner
        self.scratch = scratch
        self.visionRuntime = visionRuntime
        self.visionRuntimeError = visionRuntimeError
        self.visionResidency = visionResidency
        self.maxContext = maxContext
    }

    public var kvTokenCount: Int { kvTokenIDs.count }
    public var hasStagedTurn: Bool { pending != nil }
    public var isClosed: Bool { closed }
    public var isUsable: Bool { !closed && !lineageBroken }

    /// Marks this conversation unusable without touching the shared runner or
    /// vision runtime. The session calls it when handing out a replacement, so a
    /// stale reference cannot reset or resume onto the live conversation's KV.
    public func invalidate() async {
        closed = true
        pending = nil
        // Wait for any run still decoding: the session resets the shared runner
        // straight after this, and resetting under a live decode either aborts
        // that turn mid-stream or lets two turns drive one runner at once.
        await waitForGeneration()
    }

    /// Suspends until no turn is decoding.
    ///
    /// This was a `while generating { await Task.yield() }` spin, which hot-looped
    /// the cooperative executor for the whole remaining decode — minutes at
    /// single-digit tok/s — and re-entered the actor it was waiting on at every
    /// iteration. Waiters are now resumed by the run itself.
    private func waitForGeneration() async {
        guard generating else { return }
        await withCheckedContinuation { continuation in
            generationWaiters.append(continuation)
        }
    }

    private func finishGeneration() {
        generating = false
        let waiters = generationWaiters
        generationWaiters = []
        for waiter in waiters { waiter.resume() }
    }

    /// Whether a turn is decoding right now.
    public var isGenerating: Bool { generating }

    /// Stages the next user turn without prefilling it.
    public func append(parts: [MultimodalContinuationPart], images: [URL] = []) throws {
        guard !closed else { throw MultimodalConversationError.closed }
        // A turn staged while another is decoding used to be accepted and then
        // dropped: `generate()` clears `pending` when it commits, so the newly
        // staged turn vanished and the next `generate()` reported an empty turn
        // with the user's message gone.
        guard !generating else { throw MultimodalConversationError.busy }
        let imageCount = parts.filter {
            if case .image = $0 { return true } else { return false }
        }.count
        guard !parts.isEmpty, imageCount == images.count else {
            throw MultimodalConversationError.emptyTurn
        }
        pending = (parts, images)
    }

    /// Drops a staged turn that has not been generated yet.
    ///
    /// It cannot undo a generated turn. The KV cache has no rewind — the runner
    /// only validates that a continuation resumes at the current position — so
    /// once a turn is prefilled it is part of the lineage. Use `reset()` to
    /// abandon it, at the cost of re-prefilling the conversation.
    public func clear() {
        pending = nil
    }

    /// Drops the KV and starts over. The model stays loaded.
    ///
    /// Waits for a decode in flight. The actor accepts calls while `generate()`
    /// is suspended mid-run, so resetting here used to wipe the KV under a live
    /// turn: it then attended against an empty cache and either produced garbage
    /// or died on the prefill cursor, leaving `kvTokenIDs` describing a KV that
    /// no longer existed.
    public func reset() async {
        guard !closed else { return }
        // The barrier mirrors `close()` and `invalidate()`, which set theirs
        // before awaiting. Without one, a `generate()` enqueued while this
        // waiter was suspended could win the actor, start decoding, and have
        // its KV wiped underneath it — leaving `kvTokenIDs` restored from the
        // run's own token array while the cursor sat at zero, and
        // `lineageBroken` false, so `isUsable` kept saying yes forever. It
        // also stops back-to-back turns from a higher-priority task starving
        // the reset: the loop alone re-raced each freshly enqueued turn.
        resetting = true
        defer { resetting = false }
        while generating {
            await waitForGeneration()
        }
        guard !closed else { return }
        runner.reset()
        kvTokenIDs.removeAll(keepingCapacity: true)
        pending = nil
        lineageBroken = false
        uncommittedBoundary = []
        boundaryNeedsReplay = false
    }

    /// Ends this conversation and clears the KV it was using. It deliberately
    /// does not touch the session-owned vision runtime: those mappings are
    /// shared, and releasing them here would corrupt another conversation's
    /// state. `TurboFieldfareModelSession.close()` owns that.
    public func close() async {
        guard !closed else { return }
        closed = true
        // Same hazard as `reset()`: the runner is shared and the decode is still
        // using it.
        await waitForGeneration()
        runner.reset()
        kvTokenIDs.removeAll(keepingCapacity: false)
        pending = nil
    }

    /// Appends one user turn and generates the reply, prefilling only the new
    /// tokens. `images` are file URLs in the order they appear among `parts`.
    /// Appends a turn, generates the reply, and commits it.
    public func send(
        parts: [MultimodalContinuationPart],
        images: [URL] = [],
        config: GenerationConfig,
        prefillConfig: PrefillRuntimeConfig = .defaultChunked,
        checkCancellation: @Sendable () throws -> Void = {},
        shouldStop: (@Sendable () -> Bool)? = nil,
        onProgress: (@Sendable (RawDecodeProgress) -> Void)? = nil
    ) async throws -> MultimodalTurnResult {
        try append(parts: parts, images: images)
        return try await generate(
            config: config, prefillConfig: prefillConfig,
            checkCancellation: checkCancellation, shouldStop: shouldStop,
            onProgress: onProgress)
    }

    /// Generates a reply to the staged turn. The turn joins the lineage as soon
    /// as it is prefilled, because the KV cannot rewind.
    /// `onProgress` receives the same events `runRawCompletion` reports, so a
    /// caller can stream tokens and prefill progress. Without it a turn was
    /// only observable once it had finished, which is unusable for a UI: the
    /// text was accumulated internally and returned in one piece.
    /// `shouldStop` ends the turn at a token boundary and **keeps** what it
    /// produced: the run returns normally with `.cancelled` as its stop reason,
    /// the partial reply is in the KV, and the next turn resumes on it. That is
    /// a different thing from cancelling the task, which throws out of the
    /// decode loop and makes this rewind the whole turn. A Stop button wants
    /// the first; a teardown wants the second.
    public func generate(
        config: GenerationConfig,
        prefillConfig: PrefillRuntimeConfig = .defaultChunked,
        checkCancellation: @Sendable () throws -> Void = {},
        shouldStop: (@Sendable () -> Bool)? = nil,
        onProgress: (@Sendable (RawDecodeProgress) -> Void)? = nil
    ) async throws -> MultimodalTurnResult {
        guard !closed else { throw MultimodalConversationError.closed }
        guard !lineageBroken else { throw MultimodalConversationError.lineageBroken }
        guard !generating, !resetting else { throw MultimodalConversationError.busy }
        guard let staged = pending else {
            throw MultimodalConversationError.emptyTurn
        }
        generating = true
        defer { finishGeneration() }
        let parts = staged.parts
        let images = staged.images
        let imageCount = parts.filter {
            if case .image = $0 { return true } else { return false }
        }.count
        if imageCount > 0, visionRuntime == nil {
            throw MultimodalConversationError.imageUnavailable(
                reason: visionRuntimeError.map(String.init(describing:)))
        }

        let cached = kvTokenIDs.count
        let turn = try await encodeTurn(parts: parts, images: images,
                                        checkCancellation: checkCancellation)
        // A run that stopped on max tokens or was cancelled left its final
        // token outside the KV. Replay it ahead of this turn, exactly as the
        // server's prefix cache does, or the model's context is missing a
        // token it already emitted.
        let boundary = boundaryNeedsReplay ? uncommittedBoundary : []
        let promptIDs = kvTokenIDs + boundary + turn.effectiveTokenIDs
        guard ConversationGenerationReserve.fits(
            tokens: promptIDs.count, maxContext: maxContext) else {
            throw MultimodalConversationError.contextExhausted(
                prompt: promptIDs.count,
                reserve: ConversationGenerationReserve.tokens,
                maxContext: maxContext)
        }

        // Same coercion as the other entry points: a turn carrying image spans
        // cannot run under a non-chunked mode, and refusing it after the images
        // are encoded helps nobody.
        var effectivePrefill = prefillConfig
        if turn.prefillInput != nil,
           let coerced = effectivePrefill.coercedForImagePrompt() {
            effectivePrefill = coerced
        }
        var generation = config
        generation.maxNewTokens = min(config.maxNewTokens, maxContext - promptIDs.count)
        var text = ""
        // Marked only once the run has actually written to the KV. Setting it
        // before the attempt condemned the conversation for failures that never
        // touched the cache — a cancellation caught by the first
        // `checkCancellation()`, a rejected prefill config, a resume-position
        // mismatch — where the recorded lineage still matched the KV exactly and
        // the turn could simply have been retried.
        // The runner's own cursor, not a progress callback. Inferring it from
        // the first `.prefill` report missed a chunk that committed and then
        // threw — the KV had moved, nothing said so, and every later turn
        // resumed at a position the runner no longer had, failing forever while
        // `isUsable` still said yes.
        let positionBefore = runner.continuationPosition
        var kvAdvanced = false
        let result: RawDecodeResult
        do {
            result = try await runRawCompletion(
            producer: runner,
            tokenizer: tokenizer,
            promptIds: promptIDs,
            multimodalInput: try turn.prefillInput?.prepending(boundary),
            config: generation,
            context: context,
            scratch: scratch,
            prefillConfig: effectivePrefill,
            start: cached == 0 ? .reset : .resume(cachedPromptTokens: cached),
                // Cancellation mid-decode ends the turn at a token boundary
                // and returns the partial result; throwing here instead would
                // condemn the lineage for a KV that is perfectly resumable.
                shouldStop: {
                    if shouldStop?() == true { return true }
                    do { try checkCancellation(); return false } catch { return true }
                }) { progress in
                    onProgress?(progress)
                    switch progress {
                    case .token(_, _, let delta):
                        text += delta
                    case .tail(let tail):
                        // The detokenizer flush at the stop boundary. Dropping
                        // it returned turn text missing its final characters
                        // while the KV kept those very tokens.
                        text += tail
                    case .prefill:
                        // The first progress report is the proof the KV moved.
                        kvAdvanced = true
                    }
                }
        } catch let generationError {
            if runner.continuationPosition != positionBefore { kvAdvanced = true }
            // A failure that moved the KV used to condemn the lineage outright,
            // but the same rewind the stop-string path uses can usually put the
            // cursor back at the turn start: a cancellation caught between
            // prefill chunks or at a token boundary leaves the committed prefix
            // intact under a rewindable tail. The staged turn stays pending, so
            // a successful rewind makes the turn retryable. Only a rewind past
            // the SWA ring slack fails, and only then is the lineage unusable.
            if kvAdvanced {
                do {
                    try MultimodalConversationKVRecovery.restoreAfterFailure(
                        positionBefore: positionBefore,
                        generationError: generationError,
                        rewind: runner.rewind(to:),
                        reset: runner.reset)
                } catch {
                    lineageBroken = true
                    throw error
                }
            }
            throw generationError
        }

        // The KV now holds exactly what the run reported, including the tokens
        // the model generated; keeping the model's own tokens rather than
        // re-tokenising its text is what makes the next turn resumable.
        // The turn is in the KV now, so it is no longer staged. Leaving it
        // pending let a repeated generate() append the same user turn again,
        // behind its own reply, and corrupt the rest of the conversation.
        pending = nil
        kvTokenIDs = result.kvBackedTokenIDs
        // A stop-string match discards the text of the tokens that formed it,
        // but all except the final one were already committed to the KV. Left
        // there, every later turn resumes on a context holding assistant text
        // the caller never saw. Drop them from the record and rewind the
        // runner so KV and transcript agree again; a token that showed a
        // visible prefix before the match began stays, so at most a few
        // characters of one boundary token remain hidden.
        if result.reason == .stopString, result.withheldTrailingKVTokens > 0 {
            do {
                kvTokenIDs = try MultimodalConversationKVRecovery.trimHiddenStopTokens(
                    result.kvBackedTokenIDs,
                    withheld: result.withheldTrailingKVTokens,
                    rewind: runner.rewind(to:),
                    reset: runner.reset)
            } catch {
                lineageBroken = true
                throw error
            }
        }
        uncommittedBoundary = result.uncommittedBoundaryTokenIDs
        boundaryNeedsReplay = result.reason == .maxTokens || result.reason == .cancelled
        let generatedTokenIDs = MultimodalTurnRecord.generatedTokenIDs(
            kvTokenIDs: kvTokenIDs, promptCount: promptIDs.count)
        return MultimodalTurnResult(
            text: text,
            promptTokens: promptIDs.count,
            cachedTokens: cached,
            // The runner's own count, not a reconstruction from KV lengths.
            // Reconstructing it made the figure depend on the stop reason —
            // end-of-turn, EOS, tool-call and stop-string stops each reported
            // one fewer — so the same run was counted differently here and in
            // `ServerInference`, which has always reported this value.
            computedPrefillTokens: result.computedPrefillTokens,
            kvTokens: kvTokenIDs.count,
            completionTokens: result.newTokens,
            reason: result.reason,
            prefillSeconds: result.prefillSeconds,
            decodeSeconds: result.decodeSeconds,
            promptTokenIDs: boundary + turn.effectiveTokenIDs,
            generatedTokenIDs: generatedTokenIDs,
            uncommittedBoundaryTokenIDs: uncommittedBoundary,
            boundaryNeedsReplay: boundaryNeedsReplay)
    }

    /// Rebuilds the KV of a stored conversation by prefilling its own token IDs,
    /// with no decode.
    ///
    /// This is the whole of resume. There is no KV on disk: the record is the
    /// sequence the model saw, and replaying it is what makes the continued
    /// conversation the one the transcript shows. Restoring is refused unless
    /// the KV is empty, so a partially filled cache can never be mistaken for a
    /// restored one — the failure mode every checkpoint-restore bug in this
    /// space shares.
    ///
    /// The replayed KV is equivalent, not byte-identical: the original
    /// assistant tokens entered through the decode path and the chunk
    /// boundaries here are the planner's, not the turn's. The token sequence is
    /// exact, which is what the next turn resumes on.
    public func restore(
        lineage: MultimodalConversationLineage,
        prefillConfig: PrefillRuntimeConfig = .defaultChunked,
        checkCancellation: @Sendable () throws -> Void = {},
        onProgress: (@Sendable (RawDecodeProgress) -> Void)? = nil
    ) async throws {
        guard !closed else { throw MultimodalConversationError.closed }
        guard !lineageBroken else { throw MultimodalConversationError.lineageBroken }
        guard !generating, !resetting else { throw MultimodalConversationError.busy }
        guard kvTokenIDs.isEmpty else {
            throw MultimodalConversationError.lineageNotEmpty(kvTokens: kvTokenIDs.count)
        }
        guard !lineage.tokenIDs.isEmpty else {
            throw MultimodalConversationError.emptyTurn
        }
        try lineage.validateTokenIDs(vocabSize: model.config.vocabSize)
        // The headroom `generate` will require of the smallest turn, not just
        // of the lineage. Restoring a conversation that could not then take a
        // turn would put minutes of prefill behind a composer that refuses
        // everything typed into it.
        guard ConversationGenerationReserve.fitsLineage(
            tokens: lineage.promptPrefixCount, maxContext: maxContext) else {
            throw MultimodalConversationError.contextExhausted(
                prompt: lineage.promptPrefixCount
                    + ConversationGenerationReserve.turnEnvelope,
                reserve: ConversationGenerationReserve.tokens,
                maxContext: maxContext)
        }
        if !lineage.images.isEmpty, visionRuntime == nil {
            throw MultimodalConversationError.imageUnavailable(
                reason: visionRuntimeError.map(String.init(describing:)))
        }
        generating = true
        defer { finishGeneration() }

        // Images are re-encoded before anything touches the KV, so a stored
        // image whose pixels no longer hash to what was recorded fails the
        // restore with the cache still empty and this conversation still usable.
        var embeddingTokenIDs = lineage.tokenIDs
        var spans: [MultimodalImageSpan] = []
        var previousUpperBound = 0
        for image in lineage.images {
            try checkCancellation()
            let range = image.tokenRange
            guard !range.isEmpty, range.lowerBound >= previousUpperBound,
                  range.upperBound <= lineage.tokenIDs.count else {
                throw MultimodalConversationError.invalidReplayImage(
                    reason: "token range \(range) does not fit the "
                        + "\(lineage.tokenIDs.count)-token lineage in order")
            }
            previousUpperBound = range.upperBound
            let features: VisionFeatures
            do {
                features = try visionRuntime!.encodeStoredImage(
                    at: image.pixelsURL,
                    expectedDigest: image.expectedDigest,
                    languageModel: model,
                    residencyPolicy: visionResidency,
                    checkCancellation: checkCancellation)
            } catch is CancellationError {
                // A cancelled restore is not a broken record; the caller asked
                // for it to stop and the KV is still empty.
                throw CancellationError()
            } catch {
                // Mapped here rather than let through as a vision error: to the
                // caller this is one thing — the stored conversation cannot be
                // replayed — and it has to be classifiable as that, with the
                // underlying reason kept so the row can say which image and why.
                throw MultimodalConversationError.invalidReplayImage(
                    reason: "\(error)")
            }
            guard features.tokenCount == range.count else {
                throw MultimodalConversationError.invalidReplayImage(
                    reason: "the stored image encodes to \(features.tokenCount) "
                        + "soft tokens, not the \(range.count) its turn recorded")
            }
            spans.append(MultimodalImageSpan(tokenRange: range, features: features))
            // The encoder writes zero where an image's soft tokens go, and the
            // effective sequence carries the placeholder ID. Deriving the
            // embedding sequence here keeps the two from ever disagreeing on
            // disk.
            for index in range { embeddingTokenIDs[index] = 0 }
        }

        let prefillInput = spans.isEmpty ? nil : try MultimodalPrefillInput(
            effectiveTokenIDs: lineage.tokenIDs,
            embeddingTokenIDs: embeddingTokenIDs,
            imageSpans: spans)
        var effectivePrefill = prefillConfig
        if prefillInput != nil, let coerced = effectivePrefill.coercedForImagePrompt() {
            effectivePrefill = coerced
        }

        do {
            _ = try await runRawPrefill(
                producer: runner,
                promptIds: lineage.tokenIDs,
                multimodalInput: prefillInput,
                prefillConfig: effectivePrefill,
                start: .reset,
                // No decode follows, so the seed is discarded. Asking for logits
                // would only make the final row pay for a head nobody reads.
                outputMode: .greedyIfAvailable,
                seedUse: .discarded,
                historyReserve: lineage.tokenIDs.count,
                scratch: scratch,
                onProgress: { onProgress?($0) })
        } catch {
            // A mid-conversation failure has to condemn the lineage because
            // nothing knows how far the KV advanced past a recorded prefix.
            // Here it does: restore starts from an empty cache, so resetting
            // puts the conversation back exactly where it began and it stays
            // usable for a fresh chat.
            runner.reset()
            kvTokenIDs.removeAll(keepingCapacity: true)
            uncommittedBoundary = []
            boundaryNeedsReplay = false
            throw error
        }

        kvTokenIDs = lineage.tokenIDs
        uncommittedBoundary = lineage.uncommittedBoundary
        boundaryNeedsReplay = lineage.boundaryNeedsReplay
    }

    private struct EncodedTurn {
        let effectiveTokenIDs: [Int32]
        let prefillInput: MultimodalPrefillInput?
    }

    private func encodeTurn(
        parts: [MultimodalContinuationPart],
        images: [URL],
        checkCancellation: @Sendable () throws -> Void
    ) async throws -> EncodedTurn {
        guard !images.isEmpty, let visionRuntime else {
            var text = ""
            for part in parts { if case .text(let value) = part { text += value } }
            let ids = kvTokenIDs.isEmpty
                ? tokenizer.encode(
                    try tokenizer.applyChatTemplate(
                        [GFTokenizer.Message(role: .user, content: text)]),
                    addBOS: false)
                : tokenizer.encodeTextContinuation(userContent: text)
            return EncodedTurn(effectiveTokenIDs: ids, prefillInput: nil)
        }

        // Token counts come from geometry, so the turn's shape is known before
        // any image is encoded.
        let preprocessor = Gemma4ImagePreprocessor(
            device: context.device, config: visionRuntime.config)
        // Kept, not discarded. Planning for the token count and then letting
        // `encodeImage(at:)` re-plan internally opened, sniffed and parsed every
        // image twice per turn — and a file rewritten between the two opens gave
        // a count that no longer matched the span the tokenizer had laid out,
        // failing the turn with a placeholder mismatch that names nothing the
        // user did.
        let plans = try images.map { try preprocessor.plan(fileURL: $0) }
        let counts = plans.map(\.geometry.softTokenCount)
        let bridge = try tokenizer.encodeMultimodalUserContinuation(
            textAndImages: parts, imageTokenCounts: counts,
            // An empty KV means this is the conversation's first turn, which
            // needs the chat template's opening rather than a continuation's.
            openingConversation: kvTokenIDs.isEmpty)
        var spans: [MultimodalImageSpan] = []
        for (range, plan) in zip(bridge.imageTokenRanges, plans) {
            try checkCancellation()
            let features = try visionRuntime.encodeImage(
                plan: plan, languageModel: model,
                residencyPolicy: visionResidency,
                checkCancellation: checkCancellation)
            guard features.tokenCount == range.count else {
                throw MultimodalPromptRendererError.placeholderMismatch
            }
            spans.append(MultimodalImageSpan(tokenRange: range, features: features))
        }
        return EncodedTurn(
            effectiveTokenIDs: bridge.effectiveTokenIDs,
            prefillInput: try MultimodalPrefillInput(
                effectiveTokenIDs: bridge.effectiveTokenIDs,
                embeddingTokenIDs: bridge.embeddingTokenIDs,
                imageSpans: spans))
    }
}