Skip to content

AI, fully local on your computer.

80.9 KB
Raw Blame
import { guardedHandleFor } from './guarded-handle'
import { clearSpeculativeSuppression } from '../services/runtime/factory'
import { IPC } from '../../shared/ipc'
import type { AppContext } from '../services/context'
import type {
  AppSettings,
  BenchmarkProgressStep,
  BenchmarkResult,
  GpuDevice,
  MemoryClass,
  ModelFileVerdict,
  ModelPlacement,
  MovedDriveNotice,
  PerformanceSnapshot,
  ResidentModelRow
} from '../../shared/types'
import { pickManifestForRole, type ModelManifest } from '../../shared/manifest'
import { detectSystem, runBenchmark, type GpuBenchmarkInput } from '../services/benchmark'
import { effectiveReadOrPersisted, liveChatRecommendation } from './registerModelIpc'
import { notifyPerformanceChanged } from './performance-notify'
import { backfillOutgoing, historyEquals, mergeSampleIntoResult } from '../services/benchmark-persistence'
import {
  eligibleDevicesFor,
  findMachine,
  latestAnswerSpeed,
  loadedAtOnceMb,
  machineKey,
  nextStartMemory,
  nextStartMemoryFor,
  nextStartUsesGpu,
  otherMachines,
  placementVerdict,
  recordAnswerSpeed,
  upsertHistory,
  type NextStartMemory
} from '../services/performance'
import { latestModelPlacement, setModelPlacementObserver } from '../services/runtime/placement'
import { latestEffectiveReadBySource } from '../services/read-speed'
import { EVENTS, type AnswerSpeed } from '../../shared/ipc'
import { displayDevice, eligibleGpuProbe, gpuUsefulForProfile, isUsefulDevice } from '../../shared/gpu-rules'
import { resolveLlamaServerPath } from '../services/runtime/sidecar'
import {
  type CardEvidence,
  cardEvidenceFor,
  checksumCacheView,
  discoverManifests,
  graphicsBudgetMib,
  knownFileVerdict,
  launchContextTokens,
  manifestFilesPresent,
  weightsMib
} from '../services/models'
import { developerLeniency } from '../services/policy'
import { pickVisionManifest, visionCandidates } from '../services/vision/status'
import { getSettings, updateSettings } from '../services/settings'
import { tMain } from '../services/i18n'
import { workspaceAdmitsWork } from '../services/workspace-vault'
import type { Db } from '../services/db'
import { modelBusyLane, modelBusyMessageKey, type ModelBusyLane } from './model-busy'
import { log } from '../services/logging'
import { perfEnabled, perfMark, perfMs } from '../services/perf'
import { isModelStarting, translationRanOnProcessor } from '../../shared/runtime-status'

// IPC for the hardware benchmark + model recommendation (spec §9.1, §11).
//
// `runBenchmark()` detects RAM/CPU/OS, probes drive speed in the workspace, optionally
// estimates tokens/sec from the running runtime, classifies a HardwareProfile, and
// recommends a chat model — STRICTLY LOCAL (no network/telemetry). The result is persisted
// to settings (`lastBenchmark`) so the recommendation pipeline (registerModelIpc) and
// `getAppStatus().hardwareProfile` read the real, detected profile instead of a stub.

/**
 * The benchmark's GPU summary: ONE device's name, memory and picker budget, paired — the
 * BUDGET device for the NEXT start (`nextStartMemory`; PR #308 audit decisions 6, 9 and 10
 * unified with PR #303 audit M8.2 at the merge of the two) — and the bump eligibility over
 * ALL devices (`gpuUsefulForProfile`, unchanged — owner decision G4).
 */
function gpuSummary(devices: readonly GpuDevice[], next: NextStartMemory, card: CardEvidence | null = null): GpuBenchmarkInput {
  return {
    card,
    name: next.device?.name ?? null,
    // The profile bump looks at the HARDWARE (every probed device); the class below looks at
    // the next START — see `GpuBenchmarkInput.useful`.
    useful: gpuUsefulForProfile(devices),
    totalMb: next.device?.totalMb ?? null,
    // The picker's budget (decision 10): the device's free figure, else total − 1024 — the same
    // `graphicsBudgetMib` call `pickerMemoryFor` makes for the Models ★.
    budgetMb: graphicsBudgetMib(next.device),
    memoryClass: next.memoryClass
  }
}

/**
 * Run the (session-cached) GPU probe on the drive's own `llama-server` and persist the
 * result to `settings.gpuProbe` (architecture.md GPU record §5.4) — so Diagnostics +
 * profile classification have device info without re-probing every launch. The probe
 * stays OUT of benchmark.ts (which keeps zero `child_process`); the summary is injected.
 * Never throws: no binary / no devices / probe failure → a null-name, not-useful input.
 *
 * The summary names the BUDGET device for the next start (`nextStartMemory`, PR #308 audit
 * decisions 6 and 9), never `devices[0]`: the class and the card honour `gpuMode` /
 * `gpuAutoDisabled`, and the same helper feeds `listModels` and the Performance screen, so
 * `BenchmarkResult.gpu` / `gpuVramMb` and the Models ★ agree on which card they mean.
 *
 * The persisted probe is STAMPED with this machine's key (PR #303 audit M8.3, owner decision
 * G3): a drive moved to a machine whose probe cannot run (no binary for that OS) used to keep
 * the previous machine's devices, and they decided the memory class, the VRAM budget and the
 * graphics tile there. Readers go through `eligibleGpuProbe`. The write rules, as merged from
 * the two audits: EVERY path that reaches the write persists THIS session's answer, stamped —
 * a probe that resolves (an empty list included — `probeGpuDevices` answers `[]` only for an exit 0
 * that named no device or a binary the integrity check refused; everything else that is not an
 * answer resolves `null`, the #380 exception below), a probe
 * that CANNOT run (no binary resolves, no session probe) and a probe that
 * THREW all replace the old result with `{ devices, probedAt, machineKey: hereKey }` and notify
 * (PR #308 audit decision 6, findings R3/R5; GPU record §5.4). #303's "no binary → no write"
 * guarded against re-stamping OLD devices as local; an empty stamped result re-stamps nothing,
 * and the adopted #308 audit decision requires the Models badge, the benchmark and the tile to
 * agree after a failed refresh on the SAME machine (a stale card used to survive both failure
 * paths, so the ★ kept a card pick while the benchmark reported the RAM pick); the
 * foreign-machine case is covered by the stamp either way. The key and the session epoch are
 * captured BEFORE the await, and admission is re-checked after it, for every path: a probe that
 * outlives a lock, or a lock and a re-unlock, never writes into a session it was not admitted
 * to (the AUD-03 seam `startModelRuntime` uses). The catch branch's own write must not escape
 * either.
 *
 * #380 AMENDS decision 6 with its one exception: a probe that hit its kill-timeout resolves
 * `null`, which is not an answer but the ABSENCE of one. It writes nothing and pushes nothing —
 * the stored stamped probe stands until the next start, the next check or "Try GPU again"
 * (which invalidates the cache and re-probes) — and the summary this call returns is built from
 * what THIS machine has on record (`eligibleDevicesFor`), so the ★, the benchmark's `gpu` and
 * the tile still agree, which is what decision 6 exists for. Persisting the empty stamped probe
 * there would be exactly the #330 failure: the driver never answered under the weight upload,
 * and a card machine was recorded (and labelled, and RAM-classed) as having none. Every OTHER
 * path is unchanged — an empty list, a missing binary and a thrower all still write.
 *
 * #530 and #653 widen what `null` covers, not this rule: a program the OS refused, and a probe
 * that crashed (a Vulkan driver dying during enumeration) or could not be spawned (EMFILE, …), are
 * not answers either. Before #653 the crash wrote `[]` here — and so did #669's heal re-probe
 * (`reprobeGpu`), which could re-stamp a card machine as having none.
 */
export async function probeAndPersistGpu(ctx: AppContext): Promise<GpuBenchmarkInput> {
  let devices: GpuDevice[] = []
  let gpuMode: AppSettings['gpuMode'] = 'auto'
  let gpuAutoDisabled = false
  const hereKey = machineKey(detectSystem())
  const epoch = ctx.workspace?.unlockEpoch()
  /** Persist `list` as this session's stamped probe if the session still admits it; says whether it did. */
  const persistProbe = (list: GpuDevice[]): boolean => {
    if (!workspaceAdmitsWork(ctx.workspace)) {
      log.info('GPU probe finished after the workspace locked; result not persisted')
      return false
    }
    if (ctx.workspace.unlockEpoch() !== epoch) {
      log.info('GPU probe outlived its workspace session; result not persisted')
      return false
    }
    updateSettings(ctx.db, { gpuProbe: { devices: list, probedAt: new Date().toISOString(), machineKey: hereKey } })
    // The graphics tile and the memory class read this probe: an empty device list is a
    // result too (the tile flips to "None"), so every write notifies — the successful probe,
    // the probe that could not run, and the probe that threw alike (PR #303 P3 push).
    notifyPerformanceChanged()
    return true
  }
  try {
    const binPath = resolveLlamaServerPath(ctx.paths.rootPath, process.platform, process.env, {
      isDev: ctx.isDev
    })
    const probed = binPath && ctx.probeGpu ? await ctx.probeGpu(binPath) : []
    if (probed === null) {
      // #380: UNKNOWN, not "no GPU" — write nothing, push nothing, and describe the start that
      // follows with what this machine already has on record (foreign-stamped ⇒ nothing).
      // Admission-gated like the write beside it: a workspace locked during the probe's bound
      // makes `ctx.db` throw, and this branch would then land in the catch below and log a probe
      // FAILURE plus attempt a (refused) empty write — neither is true here, so return the empty
      // summary without touching the DB at all.
      if (!workspaceAdmitsWork(ctx.workspace)) {
        log.info('GPU probe gave no answer (timed out, crashed, or the engine could not start); workspace locked meanwhile')
        return gpuSummary(
          [],
          nextStartMemory({ platform: process.platform, arch: process.arch, devices: [], gpuMode, gpuAutoDisabled })
        )
      }
      log.info('GPU probe gave no answer (timed out, crashed, or the engine could not start — #530, #653); the stored probe stands until the next start or check')
      devices = eligibleDevicesFor(getSettings(ctx.db), hereKey)
    } else {
      devices = probed
      persistProbe(devices)
    }
  } catch (err) {
    log.warn('GPU probe failed (benchmark continues without it)', String(err))
    // `devices` is reset so the summary and the persisted record agree; the thrown path
    // persists the same empty probe, and its own write must not escape (a workspace locked
    // mid-run throws from `ctx.db`) — this function never throws.
    devices = []
    try {
      persistProbe([])
    } catch (persistErr) {
      log.warn('Could not persist the empty GPU probe', String(persistErr))
    }
  }
  // Read the flags AFTER the probe persisted: `tryGpuAgain` clears `gpuAutoDisabled` right
  // before it re-probes, and the summary must describe the start that follows that click. An
  // unreadable settings row (locked) falls back to the GPU-on defaults. On every path that
  // WROTE, the devices are the ones THIS probe enumerated (eligible by construction — it ran
  // here), not a re-read of the store, which a refused write would have left holding another
  // machine's. On the #380 unknown path there is no answer to describe, so they ARE a read of
  // the store — deliberately, and filtered through `eligibleDevicesFor`, so a foreign-stamped
  // record still supplies nothing and the same guarantee holds.
  // §6.8: the same card evidence `pickerMemoryFor` hands the Models ★ (`cardEvidenceFor`).
  let card: CardEvidence | null = null
  try {
    const s = getSettings(ctx.db)
    ;({ gpuMode, gpuAutoDisabled } = s)
    card = cardEvidenceFor(s, hereKey)
  } catch {
    /* defaults */
  }
  return gpuSummary(devices, nextStartMemory({ platform: process.platform, arch: process.arch, devices, gpuMode, gpuAutoDisabled }), card)
}

/**
 * Run the benchmark and persist the result (the shared core of IPC + first-run).
 *
 * #185 — the re-entrancy and busy guard. This is the only entry point (both the Diagnostics
 * button and the first-run background call land here), and it had NO guard at all: two runs
 * started in quick succession, or one started while a chat streamed, both reached the model.
 * The guard is one read of the shared occupancy signal, taken and acted on SYNCHRONOUSLY —
 * before the first `await` — so two callers cannot both see idle, and the span the run then
 * holds covers the whole thing (GPU probe, drive probe, speed probe), which is what makes a
 * second run see the first.
 *
 * The benchmark's own lane is deliberately NOT ignored: a second benchmark seeing the first
 * IS the re-entrancy guard.
 *
 * Throws the friendly, localized refusal (`BenchmarkBusyError`). The Diagnostics button
 * surfaces it; the first-run scheduler (`scheduleFirstBenchmark`) reports it as
 * `'skipped-busy'` and — SD2 — does not retry within the session: the next unlock re-checks,
 * and the Performance screen can run it on demand meanwhile. A refused call emits NO
 * `performance:changed`: the span it saw belongs to the running benchmark, whose own release
 * will announce the idle state — a refusal announcing it would tell the screen the first run
 * had finished.
 *
 * The push (P3, G6): `performance:changed` once the span is taken (`running` flips true —
 * the screen learns about a run it did not start: first-run, moved-drive, another window)
 * and once more in the `finally`, after BOTH the persist and the release, on success and on
 * failure alike. That second push is the idle signal; the progress 'done' step precedes the
 * persist and must not be read as one.
 *
 * Rejects without persisting when the workspace it was admitted to is gone by the time the
 * legs finish (the P7 late-write guard in `runBenchmarkAndPersist`): the measured result is
 * deliberately NOT returned then, because the documented contract of the resolved value is
 * "the reconciled object that was written" (M6), and a lock gate has nothing to show anyway.
 */
export async function runAndPersistBenchmark(
  ctx: AppContext,
  onProgress?: (step: BenchmarkProgressStep) => void
): Promise<BenchmarkResult> {
  const busy = modelBusyLane(ctx)
  if (busy) throw new BenchmarkBusyError(busy)
  const releaseOccupancy = ctx.runtime.occupancy.begin('benchmark')
  const abort = new AbortController()
  runningBenchmarks.set(ctx, abort)
  notifyPerformanceChanged()
  try {
    return await runBenchmarkAndPersist(ctx, abort.signal, onProgress)
  } finally {
    if (runningBenchmarks.get(ctx) === abort) runningBenchmarks.delete(ctx)
    releaseOccupancy()
    notifyPerformanceChanged()
  }
}

/**
 * #606: the running benchmark's abort, one per context (the #185 guard above admits one run at a
 * time on a runtime). Lock and quit used to stop the chat model under the speed leg, which then died
 * on the killed socket (undici's `TypeError: terminated`) and ended as a silent null or a
 * `speedSkipped`. They now abort it first (`ctx.cancelBenchmark`), and the run persists nothing. A
 * model stop or switch does NOT cancel it: the run keeps its other legs and says the speed leg was
 * skipped (#393).
 */
const runningBenchmarks = new WeakMap<AppContext, AbortController>()

/** #606: abort the benchmark running on `ctx` — the lock and quit teardowns, via `ctx.cancelBenchmark`. */
export function cancelRunningBenchmark(ctx: AppContext): void {
  runningBenchmarks.get(ctx)?.abort()
}

/**
 * The #185 busy refusal, typed so the first-run scheduler can tell "another lane holds the
 * model" (`'skipped-busy'`) from a failure. The message is the friendly, localized lane copy
 * the Diagnostics button shows; `lane` names who held it.
 */
export class BenchmarkBusyError extends Error {
  constructor(readonly lane: ModelBusyLane) {
    super(tMain(modelBusyMessageKey(lane)))
    this.name = 'BenchmarkBusyError'
  }
}

/** The benchmark body, run under the `benchmark` occupancy span held by the caller above. */
async function runBenchmarkAndPersist(
  ctx: AppContext,
  signal: AbortSignal,
  onProgress?: (step: BenchmarkProgressStep) => void
): Promise<BenchmarkResult> {
  // AUD-03 for the benchmark (PR #303 audit P7): WHICH unlocked session this run belongs to,
  // captured before the first await. The GPU probe, the drive probe and the speed leg below take
  // seconds — minutes on a wedged probe — and a lock, or a lock AND a re-unlock, can complete
  // inside that window; the guard after the legs mirrors `startModelRuntime`'s post-hash check
  // and `probeAndPersistGpu`'s, so the result never persists into a session it was not admitted
  // to. Undefined only for a context with no workspace at all, which the admission check refuses.
  const epoch = ctx.workspace?.unlockEpoch()
  const manifests = ctx.manifestsDir
    ? discoverManifests(ctx.manifestsDir).manifests.map((m) => m.manifest)
    : []
  const gpu = await probeAndPersistGpu(ctx)

  // #108: the honest read figure is a byproduct of real loads/hashes (read-speed.ts),
  // injected here via the shared latch-vs-persisted resolution (identity-gated — a
  // foreign persisted sample is never a candidate, M2 — and ranking-aware, so a session
  // checksum sample never shadows THIS machine's persisted model-load one) — a re-run
  // never loses an observation.
  const effectiveRead = effectiveReadOrPersisted(ctx)

  // The ONE runtime this run measures. Captured here, after the GPU probe and before the drive
  // probe, so the busy predicate below can tell it apart from whatever the manager holds later (#393).
  const runtime = ctx.runtime.active()

  const measured = await runBenchmark({
    workspacePath: ctx.paths.workspacePath,
    manifests,
    runtime,
    gpu,
    effectiveRead,
    // #185: the admission guard above ran seconds ago — before the GPU + drive probes — so
    // re-check right at the speed probe, ignoring our OWN span (which is held for this whole
    // function and would otherwise report the benchmark as busy against itself).
    //
    // #393: a model START is not an occupancy lane, so `modelBusyLane` cannot see one — yet a
    // manual "Use model" pressed beside this run STOPS the model the leg is streaming on
    // (`RuntimeManager.doStart` stops `current` before loading the next one). `start()` sets
    // `startingModelId` synchronously, strictly before the queued `doStart` reaches that stop,
    // so reading it turns a cut reading into the honest `warnSpeedSkipped`. Measured on
    // hardware in #334 leg S5. Every benchmark trigger awaits its OWN start before running, and
    // a same-model start never sets the flag through `start()` (its idempotency check returns
    // early), so this can never skip every run; `forceRestart` does set it, and is masked only
    // while the old `current` is still up — the identity term below catches the rest of that
    // window.
    //
    // …and the identity term itself: the leg measures ONE runtime, captured above. If the
    // manager no longer hands out that object, whatever it streams now is not the reading this
    // run set out to take — a start that COMPLETED between the capture and the leg (the flag
    // back to null, a new `current` committed) is invisible to both terms above, and the
    // captured runtime is dead. Same-model `forceRestart` lands here too.
    modelBusy: () =>
      modelBusyLane(ctx, { ignore: ['benchmark'] }) != null ||
      modelStartInFlight(ctx) ||
      ctx.runtime.active() !== runtime,
    signal,
    onProgress
  })

  // #606: a lock or quit cancelled the run (`cancelRunningBenchmark`). Both close the workspace, so it
  // is refused like the late write below — without relying on the lock latch still being armed.
  if (signal.aborted) {
    log.info('Benchmark cancelled by a lock or quit; result not persisted')
    throw new Error(tMain('main.benchmark.lockedDuringRun'))
  }

  // The late-write guard (P7): the legs are done, nothing is written yet. A completed lock reads
  // as not admitted; a lock still tearing down reads the same way (`isLocking`, AUD-02); a lock
  // AND a re-unlock look exactly like "still unlocked" — only the session epoch tells that stale
  // run apart. Refused ⇒ nothing is written and the caller's `finally` still releases the span
  // and pushes the idle signal.
  // Both refusals are USER emissions (the Diagnostics button shows them), so they are localized
  // like the sibling `main.benchmark.locked` (A-D2, D-L5): one calm message for both cases —
  // the log lines keep the two apart.
  if (!workspaceAdmitsWork(ctx.workspace)) {
    log.info('Benchmark finished after the workspace locked; result not persisted')
    throw new Error(tMain('main.benchmark.lockedDuringRun'))
  }
  if (ctx.workspace.unlockEpoch() !== epoch) {
    log.info('Benchmark outlived its workspace session; result not persisted')
    throw new Error(tMain('main.benchmark.lockedDuringRun'))
  }

  // M6: the drive and speed legs above take seconds, and a model start or a cold hash can
  // land a NEWER sample meanwhile (the read-speed observer persists it onto the outgoing
  // result mid-run). Re-resolve through the same gate now, after both legs, and fold the
  // newest eligible same-machine sample in (the slow-read warning follows it; `ranAt` is
  // the run's) — the sample captured before the run must never overwrite it at commit.
  const result = mergeSampleIntoResult(measured, effectiveReadOrPersisted(ctx))

  // Persist the last result via the settings store (spec §8 defines no benchmarks table),
  // and file it under this machine in the per-computer history (benchmark.md "History per
  // machine") so a drive that travels keeps one result per computer. The OUTGOING
  // `lastBenchmark` is backfilled into the history first (M4: an upgraded workspace holds
  // the previous computer's result only there, and this run replaces it; a same-machine
  // outgoing copy is simply superseded by the upsert). ONE settings write, history first,
  // then lastBenchmark — the store has no multi-key transaction, so the order guarantees a
  // crash between the two rows loses at most the headline copy, never a machine.
  const settings = getSettings(ctx.db)
  const here = machineKey(result)
  const history = upsertHistory(backfillOutgoing(settings.benchmarkHistory, settings.lastBenchmark, here), result)
  updateSettings(ctx.db, { benchmarkHistory: history, lastBenchmark: result })
  // §5 item 22 (a): this computer now has a result measured just now, so the moved-drive notice
  // has nothing left to say — whichever of its states was standing. Covers the automatic run and
  // the "Check this computer" the notice itself points at. The caller's `finally` pushes.
  setMovedDriveNotice(ctx.db, ctx.workspace.unlockEpoch(), null)
  log.info('Benchmark complete', {
    profile: result.profile,
    recommendedModelId: result.recommendedModelId,
    ramGb: result.ramGb
  })
  return result
}

// ---- The first-run benchmark: cheap preparation, then the measurement behind the auto-start ----
//
// Spec §2.1 "first-run hardware benchmark": a workspace that was never benchmarked — or a drive
// that arrives on a computer with no stored result (benchmark.md "History per machine") — is
// benchmarked once in the background, so the hardware profile + model recommendation appear
// without the user finding the Diagnostics button. Fired after the workspace becomes usable
// (the plaintext open at startup, or unlock/create). Strictly local; never blocks anything.
//
// WHY TWO HALVES (PR #303 audit L1 / SD2, owner decision G5). The three post-unlock seams used
// to fire the first benchmark and `maybeAutoStartActiveModel` back to back. A model start
// is not an occupancy lane, so the benchmark's 8 MiB write/fsync probe contended with the
// start's multi-GB weight hash + load on the same drive; and `runtime.active()` was captured
// before the drive probe, so a start that reached ready seconds later never got its speed leg.
// The moved-drive path made that reachable on EVERY unlock of a moved drive. So:
//
//   prepareFirstBenchmark   the CHEAP half, synchronous, BEFORE the auto-start is even called:
//                           the admission guard, the session capture, the GPU probe refresh,
//                           and the restore / seed / backfill writes with their pushes — a known
//                           computer's profile and ★ pick come back promptly. Returns a DECISION.
//   scheduleFirstBenchmark  the MEASUREMENT half: waits for the auto-start to settle (success or
//                           failure — a failed start still permits the benchmark, just without
//                           the speed leg), re-checks the world at settlement, then runs.
//
// #380 adds ONE ordering step inside the cheap half: the probe refresh is no longer
// fire-and-forget. It rides back on `decision.probed`, and the seams gate the auto-start on
// that — because the `--list-devices` child and the multi-GB weight upload were competing for
// the same driver, and the ladder shares the probe's in-flight promise, so the race decided the
// backend label (#330: a card decoding at 98–100 tok/s reported as "cpu", with an empty stamped
// probe persisted behind it). ≈1 s on an idle driver, nothing at all without a binary.
//
// The wait is BOUNDED, but the bound is a deferral boundary, not permission to overlap the load:
// at the ceiling the scheduler releases its own outcome (`'deferred'`) and leaves exactly ONE
// continuation on the settlement, which re-checks and runs once the start actually settles; a
// session or process that ends first runs nothing (the next launch re-checks). Nothing here ever
// cancels the user's model start, and no new occupancy lane is introduced.
//
// SD2 — one automatic attempt per unlock session. Since the moved-drive check, a key mismatch
// re-triggered a background run at every unlock until a same-machine result persisted, with no
// bound on a machine whose run keeps failing (no binary, an unwritable workspace). `attemptMemo`
// records the attempt per (DB handle, session epoch): a second prepare/schedule in the same
// session is `'skipped-attempted'`; a lock + unlock (a new `Db`, a new epoch) re-checks; and a
// successful MANUAL run persists a same-machine `lastBenchmark`, so the next prepare owes nothing.

/** The measurement `prepareFirstBenchmark` found owed: the workspace was never benchmarked, or
 *  the drive is on a computer with no stored result. */
export type FirstBenchmarkRun = 'first-run' | 'new-machine'

/**
 * What `prepareFirstBenchmark` decided, handed to `scheduleFirstBenchmark`. Plain data (plus
 * `probed`): the scheduler re-reads everything live at settlement and trusts only the session
 * identity here.
 */
export interface FirstBenchmarkDecision {
  /** The measurement to schedule; null when nothing is owed (a restore, the same machine, a
   *  legacy result, a locked workspace) or this session already made its one attempt. */
  run: FirstBenchmarkRun | null
  /** True when `run` is null ONLY because this session's automatic attempt was already made (SD2). */
  attempted: boolean
  /**
   * The unlock session the decision was made in — `undefined` on the `nothing` decision (a
   * workspace that admits no work, or settings it could not read).
   */
  epoch: number | undefined
  /** This machine's fingerprint at decision time (null when detection failed). */
  hereKey: string | null
  /**
   * #380: settles when this session's probe refresh settled; resolved at once when none was
   * fired. NEVER rejects. The seams gate the model auto-start on it, so the start ladder finds
   * a settled device list instead of racing the weight upload for the driver's attention.
   */
  probed: Promise<void>
}

/** How the scheduled measurement ended. Production callers `void` it; tests await it. */
export type FirstBenchmarkOutcome =
  /** Nothing was owed (restored, same machine, legacy result, or the workspace was locked). */
  | 'not-needed'
  /** The measurement ran and persisted. */
  | 'ran'
  /** Another lane held the model at settlement (a benchmark span, a chat, a doc task, a skill run). */
  | 'skipped-busy'
  /** The workspace locked (or a lock was under way) while the start settled. */
  | 'skipped-admission'
  /** A lock AND a re-unlock completed meanwhile — the new session re-checks on its own. */
  | 'skipped-epoch'
  /** The quit latch is armed. */
  | 'skipped-shutdown'
  /** A result for this computer was persisted meanwhile (a manual run, another window). */
  | 'skipped-already-current'
  /** This session already made its one automatic attempt (SD2). */
  | 'skipped-attempted'
  /** The start did not settle within `FIRST_BENCHMARK_SETTLE_TIMEOUT_MS`; one continuation remains. */
  | 'deferred'
  /** The run threw (a refused late persist included) — logged; the Performance screen can run it on demand. */
  | 'failed'

/**
 * How long the scheduler waits for the auto-start before it reports `'deferred'`. Sized to the
 * common slow case, not the worst: a ~5 GB GGUF on the ~70 MB/s USB stick #108 measured is hashed
 * (cold checksum cache) and then loaded — roughly a minute each — so a healthy start on slow media
 * settles around this bound, while the pathological ones (a ladder walk of serial 180 s health
 * timeouts plus the 90 s warm-up) are what the continuation exists for. Since #380 the promise
 * the seams hand over is the probe→auto-start chain, so this budget also covers the session's
 * device probe ahead of the start — about 1 s idle, its 10 s bound on a wedged driver, nothing
 * at all without a binary; it does not move the sizing. The value never decides
 * whether the benchmark may overlap the load — it may not; it only decides when the caller's
 * outcome stops waiting.
 */
const FIRST_BENCHMARK_SETTLE_TIMEOUT_MS = 120_000

function armTimer(fn: () => void, ms: number): () => void {
  const handle = setTimeout(fn, ms)
  // A pending deferral must never keep the process alive on its own: at the ceiling the wait is
  // released and nothing runs unless the start settles.
  handle.unref()
  return () => clearTimeout(handle)
}

/**
 * SD2 — the one automatic attempt this session made, keyed like the persisted-sample memo in
 * registerModelIpc: the workspace DB handle AND the session epoch. A lock/unlock yields a new
 * `Db` and a new epoch, so the next session re-evaluates. Set when a scheduling is ACCEPTED (the
 * attempt), never on a `run: null` decision — a session that owed nothing has attempted nothing.
 */
let attemptMemo: { db: Db; epoch: number | undefined } | null = null

/**
 * The MOVED-DRIVE NOTICE (§5 item 22 (a), owner decision 2026-09-08). `prepareFirstBenchmark`
 * runs after every unlock and, on a drive that has changed computers, either restores this
 * machine's stored entry into `lastBenchmark` (nothing is re-measured, so the figures on
 * Performance are older than this launch) or owes a background measurement. The user was told
 * NEITHER. Home now says which of the two happened.
 *
 * Why a latch and not a derivation: after the restore, `lastBenchmark` is this machine's and is
 * indistinguishable from an ordinary same-machine state, so nothing in settings can answer "was
 * this restored just now?". The latch is keyed like `attemptMemo` — the workspace DB handle AND
 * the unlock epoch — so a lock/unlock retires it automatically, exactly as it retires the
 * one-attempt memo; a new session makes its own decision.
 *
 * A `first-run` on a fresh workspace deliberately sets NOTHING: that is not a moved drive, and
 * this notice is the moved-drive check's voice, not a general "no benchmark yet" banner.
 */
let movedDriveMemo: { db: Db; epoch: number | undefined; notice: MovedDriveNotice } | null = null

/** Record (or clear) this session's moved-drive state. `null` retires it. */
function setMovedDriveNotice(db: Db, epoch: number | undefined, notice: MovedDriveNotice | null): void {
  movedDriveMemo = notice === null ? null : { db, epoch, notice }
}

/**
 * How a measurement ends once its start settled: what `runMeasurementOnceSettled` can return. A
 * deferral, a repeat scheduling and a decision with nothing owed all end before that point, so the
 * notice standing then stays as it is ("a check is running" is still true for a deferral).
 */
type SettledBenchmarkOutcome = Exclude<FirstBenchmarkOutcome, 'not-needed' | 'skipped-attempted' | 'deferred'>

/**
 * What the measurement's terminal outcome means for the notice. Only a `new-machine` decision
 * ever reaches this with a `measuring` notice standing.
 *   - the result landed (`ran`), or one for this computer was saved meanwhile → nothing to say;
 *   - anything else (busy, failed, locked, quitting) → no check ran and none will this session,
 *     so the notice must stop claiming one is running and offer the check instead.
 */
function settleMovedDriveNotice(ctx: AppContext, decision: FirstBenchmarkDecision, outcome: SettledBenchmarkOutcome): void {
  if (decision.run !== 'new-machine') return
  let db: Db
  try {
    db = ctx.db
  } catch {
    return // locked: the memo's key no longer matches anyway, so the getter is already silent
  }
  if (!movedDriveMemo || movedDriveMemo.db !== db || movedDriveMemo.epoch !== decision.epoch) return
  const done = outcome === 'ran' || outcome === 'skipped-already-current'
  setMovedDriveNotice(db, decision.epoch, done ? null : { kind: 'owed' })
}

/**
 * The notice for THIS unlock session, or null when there is nothing to say — a same-machine
 * launch, a fresh workspace, a lock since the decision, or a completed measurement. Never throws.
 */
export function movedDriveNotice(ctx: AppContext): MovedDriveNotice | null {
  if (!movedDriveMemo) return null
  try {
    if (movedDriveMemo.db !== ctx.db) return null
  } catch {
    return null // locked
  }
  return movedDriveMemo.epoch === ctx.workspace.unlockEpoch() ? movedDriveMemo.notice : null
}

/**
 * G3 — a result whose machine key is unknown, or equal to this machine's, counts as this
 * machine's. The moved-drive check, the settlement re-check and the snapshot's `currentMachine`
 * all make this one call, so they can never contradict each other.
 */
function countsAsThisMachine(key: string | null, here: string | null): boolean {
  return here == null || key == null || here === key
}

/**
 * The cheap half (see the header above): decide whether a measurement is owed, doing the
 * restore / seed / backfill writes and the per-session GPU probe refresh on the way. Synchronous
 * (the probe refresh is handed back as `decision.probed`, not awaited here); never throws
 * (settings unreadable — e.g. just locked again — reads as "nothing owed", and a manual run
 * still works).
 *
 * #380: the probe used to be fire-and-forget, and the seams called the model auto-start in the
 * same tick — so the `--list-devices` child competed with the multi-GB weight upload for the
 * driver, and the ladder (which shares the very same in-flight promise) labelled the rung from
 * whatever that race produced. `probed` lets the seams sequence the two.
 */
export function prepareFirstBenchmark(ctx: AppContext): FirstBenchmarkDecision {
  const nothing: FirstBenchmarkDecision = {
    run: null,
    attempted: false,
    epoch: undefined,
    hereKey: null,
    probed: Promise.resolve()
  }
  /**
   * #380: this session's probe refresh, once it has been fired; `Promise.resolve()` until then.
   * Declared OUTSIDE the try so the throw path below hands back a probe that IS in flight —
   * `settings.lastBenchmark` is read before the refresh, but the restore/backfill writes after
   * it can throw (a lock landing mid-decision), and returning the bare `nothing` there would
   * break the invariant the seams rely on: `probed` settles when this session's probe did.
   */
  let probed: Promise<void> = Promise.resolve()
  try {
    // AUD-02: also skipped while a lock teardown runs — the benchmark spawns a sidecar and
    // persists settings, and the DB stays open for that whole window.
    if (!workspaceAdmitsWork(ctx.workspace)) return nothing
    // AUD-03: the session this decision belongs to. The scheduler compares it at settlement.
    const epoch = ctx.workspace.unlockEpoch()
    const db = ctx.db
    const settings = getSettings(db)
    const here = machineKey(detectSystem())
    const owed = (run: FirstBenchmarkRun): FirstBenchmarkDecision => {
      if (attemptMemo && attemptMemo.db === db && attemptMemo.epoch === epoch) {
        log.info('First-run benchmark already attempted in this session; not retrying before the next unlock')
        return { run: null, attempted: true, epoch, hereKey: here, probed }
      }
      log.info(
        run === 'first-run'
          ? 'First run: benchmarking hardware in the background once the model start settles'
          : 'Drive is on a new computer: benchmarking it in the background once the model start settles'
      )
      // Item 22 (a): a new computer means a check IS running (or about to be) — Home says so and
      // deliberately offers NO action for it, so the notice never asks for a check that is
      // already under way. A `first-run` sets nothing: that is not a moved drive.
      if (run === 'new-machine') setMovedDriveNotice(db, epoch, { kind: 'measuring' })
      return { run, attempted: false, epoch, hereKey: here, probed }
    }
    // Refresh the persisted GPU probe for THIS machine/session in the background: a drive moved
    // between machines would otherwise keep showing the previous machine's GPU in Diagnostics
    // until a manual re-benchmark (and older workspaces may have no `gpuProbe` at all). A
    // `--list-devices` subprocess, session-cached — not the measurement.
    // #380: kept off this function's critical path (it stays synchronous), but no longer
    // fire-and-forget — the promise travels on the decision so the seams can start the model
    // AFTER it settles. It never rejects: `probeAndPersistGpu` swallows everything, and the
    // second handler is belt-and-braces so `probed` can never reject into a seam.
    // #473: a FIRST run is sequenced too. It used to fire nothing here, so a workspace whose
    // first run never stored a result (failed, cancelled by a lock) auto-started its model beside
    // the ladder's own probe — and a probe that answered mid-upload was cached and persisted with
    // the chat model already on the card, a baseline the reranker gate then subtracted chat from
    // twice. Only a TIMED-OUT probe was ever re-taken on an idle driver. A probe that gives no
    // answer HERE (a timeout, a refusal, since #653 a crash) still leaves the ladder's own probe
    // to run beside the upload, and an answer there is the session's: accepted, rare, and on
    // NVIDIA/Windows harmless (its free figure does not move, #318).
    probed = probeAndPersistGpu(ctx).then(
      () => undefined,
      () => undefined
    )
    if (settings.lastBenchmark === null) return owed('first-run')
    // The moved-drive check (benchmark.md "History per machine"): the last result belongs to a
    // DIFFERENT computer than the one we are on. With a stored result for this one, restore it
    // (the recommendation follows the machine, not the drive); without one, this is a first run
    // on this computer, so the measurement is owed. A result with no machine identity (an old
    // blob, a failed detection) never counts as moved — "unknown" keeps what we have.
    const last = machineKey(settings.lastBenchmark)
    if (countsAsThisMachine(last, here)) {
      // The same-machine upgrade seed (M4): a workspace from before the history existed
      // carries its one result only in `lastBenchmark` — file a KEYED result under its
      // machine now, so a later move keeps it; an unkeyed legacy result is left alone
      // entirely (it could never be matched again). `backfillOutgoing` also repairs a
      // history copy that is older than the headline one; nothing is re-run.
      const seeded = backfillOutgoing(settings.benchmarkHistory, settings.lastBenchmark, here)
      if (!historyEquals(seeded, settings.benchmarkHistory)) {
        updateSettings(db, { benchmarkHistory: seeded })
        log.info('Filed the last benchmark result under this computer in the history')
        notifyPerformanceChanged()
      }
      return { run: null, attempted: false, epoch, hereKey: here, probed }
    }
    // Capture the restore destination BEFORE the backfill: the cap protects this
    // machine's entry (`backfillOutgoing` evicts the oldest OTHER machine), and the copy
    // restored is the one read here either way.
    const known = findMachine(settings.benchmarkHistory, here)
    // M4: the outgoing foreign result is seeded into the history before anything replaces
    // it (restore below, or the background run — whose own backfill then finds it already
    // filed and does nothing, so the entry is never duplicated).
    const history = backfillOutgoing(settings.benchmarkHistory, settings.lastBenchmark, here)
    if (known) {
      // History first, then lastBenchmark (the same ordering as the run's persist).
      updateSettings(db, { benchmarkHistory: history, lastBenchmark: known })
      log.info('Drive is back on a known computer: restored its benchmark result', {
        profile: known.profile,
        recommendedModelId: known.recommendedModelId
      })
      // Item 22 (a): NOTHING was re-measured here — the figures the user is about to read were
      // measured on `known.ranAt`, possibly long ago. Home says so, and offers the check.
      setMovedDriveNotice(db, epoch, { kind: 'restored', ranAt: known.ranAt })
      // The screen may already be open on the outgoing computer's result: tell it.
      notifyPerformanceChanged()
      return { run: null, attempted: false, epoch, hereKey: here, probed }
    }
    if (!historyEquals(history, settings.benchmarkHistory)) {
      updateSettings(db, { benchmarkHistory: history })
      notifyPerformanceChanged()
    }
    return owed('new-machine')
  } catch {
    // Settings unreadable (e.g. just locked again) — a manual run still works. `probed` rides
    // along: a refresh already fired must still be waitable, or the seam's auto-start would
    // race it again on exactly the path #380 fixed.
    return { ...nothing, probed }
  }
}

/**
 * The measurement half (see the header above): run the decided measurement once `settled` has
 * settled, within the bounded wait. Since #380 the seams pass the whole probe→auto-start chain
 * (`decision.probed.then(() => maybeAutoStartActiveModel(ctx))`), not the auto-start alone, so
 * `settled` covers the session's device probe as well; either half being a no-op — no binary to
 * probe, nothing to start — simply settles it sooner. Resolves to the outcome; never rejects.
 * The three post-unlock seams `void` it and stay non-blocking.
 */
export function scheduleFirstBenchmark(
  ctx: AppContext,
  decision: FirstBenchmarkDecision,
  settled: Promise<unknown>
): Promise<FirstBenchmarkOutcome> {
  if (decision.run === null) return Promise.resolve(decision.attempted ? 'skipped-attempted' : 'not-needed')
  let db: Db
  try {
    db = ctx.db
  } catch {
    return Promise.resolve('skipped-admission') // locked between prepare and schedule
  }
  // SD2: the memo is checked here too, so a stale decision re-scheduled in the same session
  // (or the same decision scheduled twice) cannot buy a second attempt.
  if (attemptMemo && attemptMemo.db === db && attemptMemo.epoch === decision.epoch) {
    log.info('First-run benchmark already attempted in this session; not scheduling again')
    return Promise.resolve('skipped-attempted')
  }
  attemptMemo = { db, epoch: decision.epoch }
  return runAfterSettled(ctx, decision, settled)
}

async function runAfterSettled(
  ctx: AppContext,
  decision: FirstBenchmarkDecision,
  settled: Promise<unknown>
): Promise<FirstBenchmarkOutcome> {
  // A FAILED start settles the wait too: the benchmark still runs, just without the speed leg.
  const quiet = settled.then(
    () => undefined,
    () => undefined
  )
  let cancel: () => void = () => undefined
  const expired = new Promise<'expired'>((resolve) => {
    cancel = armTimer(() => resolve('expired'), FIRST_BENCHMARK_SETTLE_TIMEOUT_MS)
  })
  const first = await Promise.race([quiet.then(() => 'settled' as const), expired])
  // The start settled first: the timer is cleared; it fired first: clearing a fired timer is a no-op.
  cancel()
  if (first === 'settled') return runOnceSettled(ctx, decision)
  // The deferral boundary: release this outcome, retain exactly ONE continuation on the same
  // settlement. It re-runs the settlement re-checks before anything else, so a session that
  // ended meanwhile runs nothing, and a start that never settles leaves the next launch to
  // re-check. NEVER a run into the unfinished load.
  log.info('First-run benchmark deferred: the model start has not settled within the wait bound', {
    waitedMs: FIRST_BENCHMARK_SETTLE_TIMEOUT_MS
  })
  void quiet.then(() => runOnceSettled(ctx, decision))
  return 'deferred'
}

/**
 * The settlement re-checks and the run, plus the moved-drive notice's terminal update (§5 item
 * 22 (a)). Every terminal outcome of an owed measurement passes through here — the direct path
 * and the deferred continuation alike — so the notice cannot be left claiming "a check is
 * running" after the run was skipped as busy, failed, or found the workspace locked. The push
 * is what makes an already-open Home correct itself without a remount.
 */
async function runOnceSettled(ctx: AppContext, decision: FirstBenchmarkDecision): Promise<SettledBenchmarkOutcome> {
  const before = movedDriveMemo?.notice ?? null
  const outcome = await runMeasurementOnceSettled(ctx, decision)
  settleMovedDriveNotice(ctx, decision, outcome)
  // Push only when the notice actually changed AND nothing else already announced it. A run that
  // PERSISTED clears the notice at persist time and its own `finally` pushes after that, so its
  // push already carries the cleared state — a second one would be churn. Every other terminal
  // outcome either pushed before the notice moved (a failed run's idle bracket) or never pushed
  // at all (the skips run no measurement), so this is their only signal.
  if (outcome !== 'ran' && (movedDriveMemo?.notice ?? null) !== before) notifyPerformanceChanged()
  return outcome
}

/** The settlement re-checks, then the run. Never throws. */
async function runMeasurementOnceSettled(ctx: AppContext, decision: FirstBenchmarkDecision): Promise<SettledBenchmarkOutcome> {
  const label = decision.run === 'first-run' ? 'First-run benchmark' : 'New-computer benchmark'
  try {
    // The same three guards `startModelRuntime` applies after its long pre-start window
    // (AUD-02 / AUD-03 / CODE-3): the workspace this decision was admitted to must still be the
    // one that is open, and the app must not be quitting.
    if (!workspaceAdmitsWork(ctx.workspace)) {
      log.info(`${label} skipped: the workspace locked while the model start settled`)
      return 'skipped-admission'
    }
    if (ctx.workspace.unlockEpoch() !== decision.epoch) {
      log.info(`${label} skipped: the workspace was locked and re-opened meanwhile (the new session re-checks)`)
      return 'skipped-epoch'
    }
    if (ctx.runtime.isShutdown()) {
      log.info(`${label} skipped: the app is quitting`)
      return 'skipped-shutdown'
    }
    // #185: the busy refusal lands here too (right after unlock, a doc task or the user's first
    // message can already own the model; a benchmark span means another run is in progress).
    // The same predicate `runAndPersistBenchmark` refuses on, read in the same tick, so the two
    // never disagree. Not retried within the session (SD2); the Performance screen can run it on demand.
    const busy = modelBusyLane(ctx)
    if (busy) {
      log.info(`${label} skipped: the model is busy (${busy}); re-run from the Performance screen`)
      return 'skipped-busy'
    }
    // A manual run, or another window's, may have supplied this computer's result while the
    // start settled: measuring again would only replace it with a same-session duplicate.
    const last = getSettings(ctx.db).lastBenchmark
    if (last !== null && countsAsThisMachine(machineKey(last), decision.hereKey)) {
      log.info(`${label} skipped: a result for this computer was saved meanwhile`)
      return 'skipped-already-current'
    }
  } catch (err) {
    log.warn(`${label} skipped: the workspace could not be read`, String(err))
    return 'failed'
  }
  try {
    await runAndPersistBenchmark(ctx)
    return 'ran'
  } catch (err) {
    // Unreachable in practice (the lane check above ran in the same tick) — kept so the outcome
    // can never misreport a refusal as a failure.
    if (err instanceof BenchmarkBusyError) return 'skipped-busy'
    log.warn('First-run benchmark skipped or failed (re-run from the Performance screen)', String(err))
    return 'failed'
  }
}

/**
 * "Try GPU again" (Diagnostics): clearing the flags alone is not enough — a cached ANSWER
 * (`[]` from a machine whose driver was not installed yet, a list from before a card was
 * pulled) stays the session's answer and would keep labeling the machine wrongly. Invalidate
 * the cache, clear the flags (and push — A-D1), re-probe + persist, and hand the renderer the
 * fresh settings. Since #380 a probe that TIMED OUT is not cached at all (it resolves `null`
 * and drops its own entry; since #653 so does one that crashed or could not be spawned), so
 * this button is no longer the only escape from a wedged driver — it is the user's override for
 * an answer that is stale rather than absent.
 *
 * #182: the session latch that switches the speculative rung off after one bad attempt is
 * the same shape of sticky, hardware-derived "no" — this button is the user asking for the
 * accelerated paths back, so it re-arms that too (the ladder's VRAM/probe guard still has
 * the final say on the next start).
 */
export async function tryGpuAgain(ctx: AppContext): Promise<AppSettings> {
  ctx.probeGpu?.invalidate()
  clearSpeculativeSuppression()
  updateSettings(ctx.db, { gpuAutoDisabled: false, gpuLastError: null })
  // The cleared flags are snapshot inputs of their own (`cpuOnly` → the resident rows, the
  // effective class, the verdict), so this write pushes right here (PR #303 audit A-D1): the
  // probe's own push follows its WRITE — on this branch every path writes (PR #308 decision 6:
  // the empty stamped probe when the probe cannot run or threw) EXCEPT a probe that gave no
  // answer — timed out (#380), refused (#530), crashed or failed to spawn (#653) — which is
  // unknown and writes nothing, but only after the probe has run, and under #303's no-write
  // paths the screen used to keep its processor-forced rows until something else pushed. The
  // second push, after the re-probe's write, is idempotent — the renderer serialises its
  // refetches; a re-probe that gives no answer simply leaves this first push as the only one.
  notifyPerformanceChanged()
  await probeAndPersistGpu(ctx)
  return getSettings(ctx.db)
}

/**
 * The chat engine was just installed (issue #323; wired by `registerEngineIpc` through
 * `EngineDownloadManager.onInstalled`). A benchmark run BEFORE the binary existed could not
 * probe and persisted an EMPTY stamped probe (PR #308 decision 6) — honest then, but frozen:
 * nothing re-ran the probe until the next unlock (`prepareFirstBenchmark`), the next check or
 * "Try GPU again", so the recommendation stayed RAM-only on a machine that may have a usable
 * card. This re-runs the same once-per-session refresh those paths use (`probeAndPersistGpu`:
 * its own admission + unlock-epoch re-check before the write, the `performance:changed` push
 * after it) — the benchmark itself is NOT re-run, the saved result stands.
 *
 * Left alone: this machine's ELIGIBLE probe (`eligibleGpuProbe` — stamped here, or unstamped
 * under G3) when it already lists a device — a machine that had a binary all along keeps the
 * answer it has (a re-install over a marker-less binary, or an upgrade, is not a reason to
 * re-probe here). A foreign-stamped probe, no probe, or the empty stamped probe all refresh.
 * The session cache is invalidated first: an entry cached under the old binary path must not
 * answer for the new one. Never throws; resolves true when a refresh ran.
 */
export async function refreshGpuProbeAfterRuntimeInstall(ctx: AppContext): Promise<boolean> {
  try {
    if (!workspaceAdmitsWork(ctx.workspace)) {
      log.info('Chat engine installed while the workspace is locked; the GPU probe refresh waits for the next unlock')
      return false
    }
    const eligible = eligibleGpuProbe(getSettings(ctx.db).gpuProbe, machineKey(detectSystem()))
    if (eligible && eligible.devices.length > 0) return false
    log.info('Chat engine installed: refreshing the GPU probe for this computer')
    ctx.probeGpu?.invalidate()
    await probeAndPersistGpu(ctx)
    return true
  } catch (err) {
    log.warn('GPU probe refresh after the engine install failed', String(err))
    return false
  }
}

/**
 * Is the ACTIVE chat model resident and ready? Read from the runtime's state, never from
 * `active() != null` alone and never from the placement latch (DR6): the placement is
 * recorded when the rung is healthy but BEFORE the #109 warm-up generation finishes, and
 * `status().running` stays false for that whole window — so a loading model reads "not
 * loaded" until it is actually ready. A start in flight (a first start or a switch) reads
 * the same way, and a runtime running some OTHER model than the active one does not count
 * for this row.
 */
function chatModelResident(ctx: AppContext, activeId: string | null): boolean {
  const status = ctx.runtime.status()
  if (!status.running || !status.healthy || status.startingModelId) return false
  return activeId == null || status.modelId === activeId
}

/**
 * #393: is a model start in flight? Deliberately NOT an occupancy lane — that would touch
 * `occupancy.ts`, `model-busy.ts` and the refusal copy of the chat / doc-task / skill lanes for a
 * harm the measurement bounded (issue #393). Read straight off the manager's status, the field
 * `chatModelResident` and the engine-install guard (`registerEngineIpc.ts`) already read.
 */
function modelStartInFlight(ctx: AppContext): boolean {
  return isModelStarting(ctx.runtime.status()) // #599: the weight check counts too
}

/**
 * The "Your model" block: the active model against this computer's memory. `memory` is the
 * NEXT start's class and budget device (`nextStartMemoryFor`: the ELIGIBLE probe —
 * `eligibleGpuProbe`, this machine's or unstamped, never a known-foreign one — and the two GPU
 * flags): the memory class, the VRAM budget, the picker budget and the device the observed
 * figures are attributed to all come from it, and from nothing else (PR #303 audit M8 and
 * PR #308 audit decisions 6/9, one source).
 */
function buildPlacement(
  ctx: AppContext,
  settings: AppSettings,
  here: string | null,
  ramGb: number,
  memory: NextStartMemory,
  manifests: ModelManifest[],
  recommendedModelId: string | null
): PerformanceSnapshot['placement'] {
  // The class and the card are the NEXT start's (GPU off / auto-disabled → no card, and class `cpu`
  // everywhere but Apple Silicon); the
  // observed placement below is what the LAST start did, and it counts only while the current
  // configuration would repeat it (`matchesConfig` below — a forced-CPU configuration admits
  // no GPU record; the record itself is kept and travels as `observedMismatch`).
  const { memoryClass } = memory
  const ramMb = ramGb > 0 ? Math.round(ramGb * 1024) : null
  const vramMb = memory.device?.totalMb ?? null
  const activeId = settings.activeModelId
  // Manifests state decimal GB; the screen's other figures are GiB (RAM, VRAM, the observed
  // buffers), so convert once here rather than show 19.8 "on disk" beside 18.9 "takes". The
  // one-decimal rounding is DISPLAY-ONLY: the verdict below gets the unrounded weights
  // (`weightsMib`, the picker's own conversion — PR #308 audit decision 8).
  const gib = (m: ModelManifest | undefined): number | null =>
    m ? Math.round((weightsMib(m) / 1024) * 10) / 10 : null
  // The context a start would ACTUALLY use, resolved by the launch path's own helper
  // (`launchContextTokens`) rather than re-derived here (PR #303 audit M5 residual). The old
  // `??` chain differed from it in one real case: a manifest whose `recommended_context_tokens`
  // is missing or 0 (the parser returns 0) showed a "0-token context" while the runtime starts
  // on `settings.contextTokens`. One helper, one answer, for the active AND the recommended
  // model — the screen no longer recomputes either.
  const contextFor = (modelId: string | null): number | null =>
    modelId == null ? null : launchContextTokens(settings, manifests.find((m) => m.id === modelId) ?? null)
  // #914: a row still names the catalog's model when its files are not here (the planning view the docs promise), and
  // says so: `onDrive` is `manifestFilesPresent`, presence only (1–3 `existsSync` per row, never a hash: the rows are
  // display-only). The screen then shows "not on this drive" instead of "not loaded", and `loadedAtOnceMb` leaves the
  // row out of the processor total, because nothing will load it. A RESIDENT model is never either (#914 code review:
  // loaded beats `onDrive` — a weight deleted while the runtime holds it open, a manifest removed under a running
  // model): the screen and the total check `loaded` first. The chat row asks the same of the active model: with its
  // files gone a start is refused. An active model outside the catalog names no files, so it is not on the drive.
  const onDrive = (m: ModelManifest | null | undefined): boolean =>
    m != null && manifestFilesPresent(ctx.paths.rootPath, m)
  // #933: what the checksum cache already knows about a present model's files: `damaged` (a check found a mismatch) or
  // `unverified` (no checksum on record, outside developer leniency), both refused at every start, so the row says so
  // and the totals leave it out (`loadedAtOnceMb`; loaded beats it). Cache only (`knownFileVerdict` hashes nothing),
  // over a READ-ONLY view of the `checksumCache` this snapshot has already read (A S-A8: the settings store's `get`
  // re-reads settings per file and can write them). The leniency is the one every start uses (`developerLeniency`).
  const fileHashes = checksumCacheView(settings.checksumCache, ctx.paths.rootPath)
  const lenient = developerLeniency(ctx, settings.developerMode)
  const fileCheck = (m: ModelManifest | null | undefined): ModelFileVerdict =>
    m != null && onDrive(m)
      ? knownFileVerdict(m, ctx.paths.rootPath, { developerMode: lenient, hashStore: fileHashes })
      : 'ok'
  const chatResident = chatModelResident(ctx, activeId)
  let model: PerformanceSnapshot['placement']['model'] = null
  const activeManifest = activeId ? manifests.find((m) => m.id === activeId) ?? null : null
  const chatFileCheck = fileCheck(activeManifest)
  if (activeId) {
    model = {
      id: activeId,
      // #914: a model outside the catalog has no size the app knows. Null says so; the old `0` printed "0.0 GB".
      sizeOnDiskGb: gib(activeManifest ?? undefined),
      // #914 code review: computed once here, for "Your model" — its files are on this drive, or it is resident
      // right now (loaded beats `onDrive`, as on the rows). The screen then claims no size on disk and no fit.
      onDrive: onDrive(activeManifest) || chatResident,
      // #933: its row's verdict; a resident model is in memory whatever the cache says (loaded beats it).
      fileCheck: chatResident ? 'ok' : chatFileCheck,
      contextTokens: contextFor(activeId) ?? settings.contextTokens
    }
  }
  // The LIVE recommendation's context (`snapshot.recommendation.modelId`, the model the CTA
  // starts), not the id saved with the check: the screen quotes this figure beside the live
  // name, and the saved pick is history (PR #308 decision 8 over PR #303 M5's saved-id read).
  const recommendedContextTokens = contextFor(recommendedModelId)
  // The session latch wins (this start), else the persisted record; either counts only for
  // the active model on THIS machine (an unknown machine on either side is accepted).
  const latched = latestModelPlacement()
  const candidate = activeId
    ? latched?.modelId === activeId
      ? latched
      : (settings.modelPlacements[activeId] ?? null)
    : null
  const mine =
    candidate && (here == null || candidate.machineKey == null || candidate.machineKey === here) ? candidate : null
  // MEASURED EVIDENCE MUST MATCH THE CONFIGURATION (PR #303 audit). A placement is a
  // measurement of ONE start: this model, this machine (above), with a specific context and on
  // a specific backend. Change the context size, or force the processor after a GPU start, and
  // the stored buffers describe a run the app would no longer perform — presenting them as
  // "measured" would state a fit the current settings never asked for. The record is KEPT (the
  // next matching start restores it, and it is still the truth about that start); the row falls
  // back to the weights-only estimate, and the mismatch travels with the snapshot so the copy
  // can say when the earlier measurement was taken. A configuration that forces the processor
  // (`gpuMode: 'off'`, or the auto-disable after a GPU failure) admits only a `cpu` record; an
  // 'auto' configuration admits either, because the ladder itself decides per start. Whether
  // the configuration forces the processor is `nextStartMemory`'s answer (`cpuForced`), not a
  // second read of the flags.
  const cpuOnly = memory.cpuForced
  const matchesConfig = (p: ModelPlacement): boolean =>
    p.contextTokens === (model?.contextTokens ?? null) && (!cpuOnly || p.backend === 'cpu')
  const observed = mine && matchesConfig(mine) ? mine : null
  const observedMismatch =
    mine && !matchesConfig(mine) ? { contextTokens: mine.contextTokens, backend: mine.backend, at: mine.at } : null
  // Every model the app can hold (benchmark.md "Models on this computer"), each marked with whether its files are on
  // this drive (#914, owner decision G-5 (c); see `onDrive` below): chat and translation
  // auto-fit onto the card; images and voice are pinned to the processor by design (contention
  // immunity; vision/runtime.ts, embeddings/e5.ts — the embedder half of "document search" is
  // pinned too). The reranker half of document search instead follows its own headroom-gated
  // device posture (Wave 8 ruling (a), reranker/llama.ts, rag/device-posture.ts) — read below
  // through the `Reranker` interface's optional `devicePosture()` member, never hard-coded.
  // Liveness comes from each service's own handle; whisper is a CLI that runs only while
  // transcribing.
  //
  // CAPABILITY IS NOT EXECUTION (PR #303 audit DR1): a row goes to the card only when the NEXT
  // start will use it (`nextStartUsesGpu`: not with no usable card, not when the GPU is switched
  // off or auto-disabled); chat says 'cpu' as well when the matching observed start
  // landed on the CPU backend, translation when its sidecar's posture is the forced
  // `--device none` (`deviceStatus().device === 'cpu'`, the session fallback latch) or its last
  // start's load log put no buffer on a graphics device (`gpuBuffers === false`, #671).
  // #848 review: a role's model is the one the app composes (`pickManifestForRole`, the function behind
  // `resolveModelByRole`: the flagged default, else the first in walk order), so a row can never name a model the
  // app does not run. #890: the embeddings row too (no composer reads `activeEmbeddingModelId`), and the images row
  // names the images pick (`pickVisionManifest`, the same rule over the vision models on the drive), the model
  // `getVisionStatus` runs whenever it verifies (one whose files fail their check is passed over there).
  const byRole = (role: ModelManifest['role']): ModelManifest | undefined => pickManifestForRole(manifests, role)
  const translationManifest = byRole('translation')
  const rerankerManifest = byRole('reranker')
  const embeddingsManifest = byRole('embeddings')
  const transcriberManifest = byRole('transcriber')
  // With no vision model on the drive the row still names the catalog's (every model the app can hold). #933 (A S-A7):
  // like `getVisionStatus`, the row passes over a candidate whose files the cache knows are damaged (or have no
  // checksum on record) for the next one; with none that passes, it names the first, which then says so. Loaded beats
  // the verdict, as on every row (#933 review N1): while the images service holds a model, the row names THAT model
  // (`loadedModelId`: the pick, or the next candidate when the pick was passed over at its start), so its name, size and
  // "loaded now" describe the one in memory. A service that cannot say which (an unknown id) leaves the pick.
  const visionLoaded = ctx.vision?.isLoaded?.() ?? false
  const visionLoadedId = visionLoaded ? (ctx.vision?.loadedModelId?.() ?? null) : null
  const loadedVision =
    visionLoadedId != null ? manifests.find((m) => m.id === visionLoadedId && m.role === 'vision') : undefined
  const visionManifest =
    loadedVision ??
    (visionLoaded ? undefined : visionCandidates(manifests, ctx.paths.rootPath).find((m) => fileCheck(m) === 'ok')) ??
    pickVisionManifest(manifests, ctx.paths.rootPath) ??
    byRole('vision')
  // Each row's `onDrive` is the presence rule defined with `model` above (#914).
  const cardEligible = nextStartUsesGpu(memory)
  const chatDevice: ResidentModelRow['device'] = cardEligible && observed?.backend !== 'cpu' ? 'gpu' : 'cpu'
  const translation = ctx.translator?.deviceStatus?.() ?? null
  // #671: a start whose own load log put no buffer on a graphics device ran on the processor, and
  // its `N/N layers` split is no evidence: a forced-CPU start and a PC with no usable card print it.
  const translationDevice: ResidentModelRow['device'] =
    cardEligible && !(translation != null && translationRanOnProcessor(translation)) ? 'gpu' : 'cpu'
  // #476: Wave 8 ruling (d)'s resident posture (`Reranker.devicePosture()`, the `isLoaded?`
  // pattern) — the SAME two reads the 'reranker' row below uses, hoisted so `rerankerOnCard` can
  // share them without re-deriving.
  const rerankerDevice: ResidentModelRow['device'] = ctx.reranker?.devicePosture?.() ?? 'cpu'
  const rerankerLoaded = ctx.reranker?.isLoaded?.() ?? false
  const allRows: ResidentModelRow[] = [
    {
      role: 'chat',
      modelId: activeId,
      sizeOnDiskGb: model?.sizeOnDiskGb ?? null,
      onDrive: onDrive(activeManifest),
      fileCheck: chatFileCheck,
      device: chatDevice,
      loaded: chatResident,
      lifetime: 'session',
      gpuLayers: null,
      totalLayers: null
    },
    {
      role: 'translation',
      modelId: translationManifest?.id ?? null,
      sizeOnDiskGb: gib(translationManifest),
      onDrive: onDrive(translationManifest),
      fileCheck: fileCheck(translationManifest),
      device: translationDevice,
      loaded: translation?.live ?? false,
      lifetime: 'idle',
      // A row that says processor claims no layers on the card (#671), whatever the sidecar printed.
      gpuLayers: translation?.live && translationDevice === 'gpu' ? translation.gpuLayers : null,
      totalLayers: translation?.live && translationDevice === 'gpu' ? translation.totalLayers : null
    },
    {
      role: 'vision',
      modelId: visionManifest?.id ?? null,
      sizeOnDiskGb: gib(visionManifest),
      onDrive: onDrive(visionManifest),
      fileCheck: fileCheck(visionManifest),
      device: 'cpu',
      loaded: visionLoaded,
      lifetime: 'idle',
      gpuLayers: null,
      totalLayers: null
    },
    {
      role: 'reranker',
      modelId: rerankerManifest?.id ?? null,
      sizeOnDiskGb: gib(rerankerManifest),
      onDrive: onDrive(rerankerManifest),
      fileCheck: fileCheck(rerankerManifest),
      // Wave 8 ruling (d): the resident sidecar's ACTUAL posture, or — when nothing is resident
      // — the posture a cold start would take now (`Reranker.devicePosture()`, the `isLoaded?`
      // pattern). A `Reranker` without that optional member reports `'cpu'` here.
      device: rerankerDevice,
      loaded: rerankerLoaded,
      lifetime: 'session',
      gpuLayers: null,
      totalLayers: null
    },
    {
      role: 'embeddings',
      modelId: embeddingsManifest?.id ?? null,
      sizeOnDiskGb: gib(embeddingsManifest),
      onDrive: onDrive(embeddingsManifest),
      fileCheck: fileCheck(embeddingsManifest),
      device: 'cpu',
      loaded: ctx.embedder?.isLoaded?.() ?? false,
      lifetime: 'session',
      gpuLayers: null,
      totalLayers: null
    },
    {
      role: 'transcriber',
      modelId: transcriberManifest?.id ?? null,
      sizeOnDiskGb: gib(transcriberManifest),
      onDrive: onDrive(transcriberManifest),
      fileCheck: fileCheck(transcriberManifest),
      device: 'cpu',
      loaded: false,
      lifetime: 'per-use',
      gpuLayers: null,
      totalLayers: null
    }
  ]
  const rows = allRows.filter((r) => r.role === 'chat' || r.modelId != null)
  // The verdict is asked for the EFFECTIVE class: the next start's, and `cpu` when the
  // configuration forces the processor (the RAM estimate, "Will run on the processor", never
  // "Should fit in graphics memory" for a card the start would not use, DR1; Apple Silicon keeps
  // its class there, #746) or for a matching start OBSERVED on the CPU backend (the ladder fell
  // through under 'auto'), which is judged against RAM too, as its row says. The profile bump's
  // gate (`gpuUsefulForProfile`, all devices) is unchanged (G4).
  const effectiveClass: MemoryClass = cardEligible && observed?.backend !== 'cpu' ? memoryClass : 'cpu'
  const verdict = placementVerdict({
    memoryClass: effectiveClass,
    ramMb,
    vramMb,
    // The picker's budget for the same device (free, else total − 1024): the pre-start
    // estimate is the Models ★'s fit, so the row and the star never disagree (PR #308 decision 8).
    graphicsBudgetMb: graphicsBudgetMib(memory.device),
    sizeOnDiskGb: activeManifest ? weightsMib(activeManifest) / 1024 : null,
    manifest: activeManifest,
    observed,
    gpuName: memory.device?.name ?? null
  })
  // Both on the card (the start-order contention): only rows that SAY 'gpu', with the chat
  // model resident and its observed start actually on the GPU with layers offloaded (no
  // observation under a GPU-eligible configuration counts too), and the translation sidecar
  // live with layers on the card — a live sidecar at 0 offloaded layers is not on the card.
  const chatOnCard =
    chatDevice === 'gpu' &&
    chatResident &&
    (observed == null || (observed.backend === 'gpu' && (observed.gpuLayers ?? 0) > 0))
  const translationOnCard =
    translationDevice === 'gpu' && (translation?.live ?? false) && (translation?.gpuLayers ?? 0) > 0
  // #476: a GPU-resident reranker (device 'gpu' AND actually loaded now — no offload split is
  // tracked for it, so residency is the whole check) is a THIRD source of the same start-order
  // contention `bothOnCard` warns about.
  const rerankerOnCard = rerankerDevice === 'gpu' && rerankerLoaded
  const totals = {
    ramAllMb: loadedAtOnceMb({ memoryClass, rows, verdict }),
    bothOnCard: [chatOnCard, translationOnCard, rerankerOnCard].filter(Boolean).length >= 2,
    // #495 follow-up: chat AND translation SPECIFICALLY — the one pair `perf.models.cardBoth`'s
    // copy actually names. `bothOnCard` widened to a >=2-of-3 count when the reranker joined it
    // (#476), but the chat/translation summary line kept appending that pair-naming sentence
    // whenever `bothOnCard` was true, including when the true contending pair was chat+reranker
    // or translation+reranker and the OTHER of chat/translation wasn't on the card at all.
    chatAndTranslationOnCard: chatOnCard && translationOnCard
  }
  return {
    memoryClass,
    ramMb,
    vramMb,
    model,
    recommendedContextTokens,
    observed,
    observedMismatch,
    verdict,
    models: rows,
    totals
  }
}

/**
 * The Performance screen's one read: the last result and whether it is this computer's,
 * the other computers the drive has been checked on, whether a run is in progress, the
 * placement block, and the session's observed figures. A GETTER: it must never call
 * `notifyPerformanceChanged` (the push would fan a read out into more reads).
 *
 * `observed` (M3 / G2) is SESSION latches only — the last finished answer, the newest
 * `model_load` sample, the newest `checksum` sample, each latched in the main process and
 * cleared with it (they survive a workspace lock/unlock, never a restart). The persisted
 * `current.effectiveRead` is deliberately NOT a fallback here, for a same-machine result as
 * much as a foreign one: the copy promises "while you worked", and a weeks-old sample is not
 * that. The Drive tile reads the persisted figure with its own source and date instead.
 */
export function buildPerformanceSnapshot(ctx: AppContext): PerformanceSnapshot {
  // Every record below is already VALIDATED: `getSettings` normalizes `lastBenchmark`,
  // `benchmarkHistory` and `modelPlacements` on read (PR #303 audit H1/L8), so the snapshot
  // composes trustworthy records and nothing here has to defend against `{}`.
  const settings = getSettings(ctx.db)
  const sys = detectSystem()
  const here = machineKey(sys)
  const current = settings.lastBenchmark
  const currentKey = machineKey(current)
  // ONE source for every graphics figure (PR #303 audit M8, owner decisions G3/G4; PR #308
  // audit decisions 6 and 9): the ELIGIBLE probe — stamped here, or unstamped (a legacy probe,
  // unverifiable until a local refresh replaces it), NOTHING when stamped with another
  // machine's key — and, of it, the BUDGET device for the next start, the same one
  // `probeAndPersistGpu` and `listModels` name — never the first device the driver happened to
  // list; null with no usable card or the GPU switched off / auto-disabled.
  const memory = nextStartMemoryFor(settings, here)
  const probed = memory.device
  // The device the graphics tile NAMES (owner decision 2026-09-07): the budget device when there
  // is one, else the largest card that does not look integrated even under the usable gate (the
  // common 6 GB laptop card Vulkan reports at 5,9xx MiB, #321), else the integrated one —
  // `displayDevice` over the SAME eligible probe. Null with the GPU switched off / auto-disabled,
  // exactly like `probed`, so the tile's "acceleration is off" copy stands. Whether the card is
  // USED stays `probed`'s question: the record, the budget and the ★ never read this device.
  const shown = memory.cpuForced ? null : displayDevice(eligibleGpuProbe(settings.gpuProbe, here)?.devices ?? [])
  // An unknown identity on either side reads as "this machine": the moved-drive check in
  // prepareFirstBenchmark makes the same call, so the two never contradict each other.
  const currentMachine = countsAsThisMachine(currentKey, here)
  // A result persisted before `gpuVramMb` existed (or one whose probe came back empty) gets
  // the eligible probe's device folded in — name AND memory, from the same device, so an old
  // iGPU name is never paired with a dGPU's figure — for THIS machine only: the graphics tile
  // must not wait for a re-run when the app already knows the card.
  const filled =
    current && current.gpuVramMb == null && currentMachine && probed
      ? { ...current, gpu: probed.name, gpuVramMb: probed.totalMb }
      : current
  // The catalog, read once for the "Your model" row and the live recommendation below.
  const manifests: ModelManifest[] = ctx.manifestsDir
    ? discoverManifests(ctx.manifestsDir).manifests.map((m) => m.manifest)
    : []
  // LIVE, through the same inputs the `listModels` ★ uses (PR #308 audit decision 8, R4): a
  // fresh probe, a flipped GPU toggle or a new speed sample moves this at once, while
  // `current.recommendedModelId` keeps saying what the check said at the time. The
  // `listModels` handler returns no list without a catalog, so there is no ★ to agree with.
  // Computed before the placement block, which quotes this model's launch context.
  const recommendation = ctx.manifestsDir ? liveChatRecommendation(settings, manifests) : null
  return {
    current: filled,
    recommendation,
    // `useful` is the shared rule's verdict on the budget device (true for a discrete card by
    // construction; the Metal pool device on unified memory is rated by the same predicate).
    currentGpu: probed ? { name: probed.name, totalMb: probed.totalMb, useful: isUsefulDevice(probed) } : null,
    graphicsDevice: shown ? { name: shown.device.name, totalMb: shown.device.totalMb, useful: shown.useful } : null,
    currentMachine,
    otherMachines: otherMachines(settings.benchmarkHistory, currentKey ?? here),
    // The benchmark's OWN span, read directly (M1): `modelBusyLane` answers "chat" first, so a
    // permitted foreground answer used to hide a held benchmark span and the screen re-enabled
    // its button mid-run.
    running: ctx.runtime.occupancy.held('benchmark'),
    placement: buildPlacement(ctx, settings, here, sys.ramGb, memory, manifests, recommendation?.modelId ?? null),
    observed: {
      lastAnswer: latestAnswerSpeed(),
      lastModelLoad: latestEffectiveReadBySource('model_load'),
      lastChecksum: latestEffectiveReadBySource('checksum')
    }
  }
}

/**
 * The chat-stream answer-speed observer's body (wired in `initBackend` through
 * `setAnswerSpeedObserver`): latch the finished answer's #290 payload with the model that
 * produced it, then push. The latch itself (`recordAnswerSpeed`) stays pure; the push lives
 * here in the IPC layer. Local-API answers never pass through the chat observer, so they
 * never latch (documented, not changed).
 */
export function observeAnswerSpeed(ctx: AppContext, speed: AnswerSpeed): void {
  recordAnswerSpeed(speed, ctx.runtime.active()?.modelId ?? null)
  notifyPerformanceChanged()
}

export function registerBenchmarkIpc(ctx: AppContext): void {
  const ipcHandle = guardedHandleFor(ctx)
  // #606: lock and quit cancel the running benchmark before they stop the chat model.
  ctx.cancelBenchmark = () => cancelRunningBenchmark(ctx)
  // Persist every observed placement under its model id (benchmark.md "Your model"), so the
  // row survives a restart. Skipped while locked; a failure is logged, never thrown into a start.
  // The session latch already moved before this observer ran, and the snapshot reads the
  // latch first — so the push follows the observation even when the persist is skipped.
  setModelPlacementObserver((placement) => {
    try {
      if (workspaceAdmitsWork(ctx.workspace)) {
        const placements = { ...getSettings(ctx.db).modelPlacements, [placement.modelId]: placement }
        updateSettings(ctx.db, { modelPlacements: placements })
      }
    } catch (err) {
      log.warn('Could not persist the model placement', { error: String(err) })
    }
    notifyPerformanceChanged()
  })

  // SEC-N2: both handlers touch ctx.db (via updateSettings/getSettings). The ctx.db getter already
  // fail-closes when the workspace is locked, but it throws a raw English string; mirror every other
  // DB-touching handler with an explicit requireUnlocked() so a locked call surfaces the localized
  // main.benchmark.locked instead (parity, and the parametrized lock test now covers these too).
  const requireUnlocked = (): void => {
    // AUD-02: `workspaceAdmitsWork`, never a bare `isUnlocked()` — the workspace DB stays
    // OPEN for the whole multi-second lock teardown, so a bare check admits work that then
    // lazily respawns the sidecars that teardown just killed. This module's copy is unchanged.
    if (!workspaceAdmitsWork(ctx.workspace)) throw new Error(tMain('main.benchmark.locked'))
  }
  ipcHandle(IPC.runBenchmark, (event): Promise<BenchmarkResult> => {
    requireUnlocked()
    // Steps go to the window that pressed the button (the Performance screen's step list);
    // a window closed mid-run simply stops receiving them.
    return runAndPersistBenchmark(ctx, (step) => {
      if (!event.sender.isDestroyed()) event.sender.send(EVENTS.benchmarkProgress, step)
    })
  })
  ipcHandle(IPC.getPerformance, (): PerformanceSnapshot => {
    requireUnlocked()
    // #333: the END-TO-END cost of one snapshot read, marked HERE rather than inside
    // `buildPerformanceSnapshot` so it spans exactly what the renderer awaits — the settings
    // read, `detectSystem`, the manifest scan and the pure composition over them. The scan's
    // own share is the `discover_manifests` line immediately before this one (they are
    // synchronous and adjacent; `scripts/measure-manifest-read.mjs --log` pairs them on that
    // adjacency and reports any unpaired scan as another caller's).
    //
    // The screen is pushed, not polled, but the pushes include one per finished chat answer
    // and one per model start — so the figure that matters is the repeat cost while the screen
    // sits open, not the cost of opening it once.
    const timed = perfEnabled()
    const startedAt = timed ? performance.now() : 0
    const snapshot = buildPerformanceSnapshot(ctx)
    if (timed) perfMark('performance_get', { ms: perfMs(startedAt) })
    return snapshot
  })
  // §5 item 22 (a). Session state only — no DB read, nothing persisted, and `movedDriveNotice`
  // itself fail-closes to null on a locked workspace, so this stays a pure getter that a Home
  // mount can call without caring about the unlock race.
  ipcHandle(IPC.getMovedDriveNotice, (): MovedDriveNotice | null => {
    requireUnlocked()
    return movedDriveNotice(ctx)
  })
  ipcHandle(IPC.tryGpuAgain, (): Promise<AppSettings> => {
    requireUnlocked()
    return tryGpuAgain(ctx)
  })
}