Skip to content

AI, fully local on your computer.

21.0 KB
Raw Blame
import type { AppSettings } from '../../../shared/types'
import { DEFAULT_SETTINGS } from '../../../shared/types'
import type { RendererSettingsKey } from '../../../shared/settings-writers'
import { primaryUsefulDevice } from '../../../shared/gpu-rules'
import { eligibleDevicesFor, machineKey } from '../performance'
import { detectSystem } from '../benchmark'
import { chatGraphicsShareMib, estimateGraphicsNeedMib, findManifestById, graphicsBudgetMib } from '../models'
import { latestModelPlacement } from '../runtime/placement'
import { defaultThreadCount } from '../runtime/sidecar'
import { totalCandidateCapFor } from '../zim/arm'
import type { RuntimeManager } from '../runtime'
import type { Translator } from '../translation'
import type { TranslationHeadroomVerdict } from '../translation/runtime'
import {
  rerankerDeviceFor,
  resolveRerankProfile,
  rerankScopeFor,
  type RerankerDevice,
  type RerankProfileInput
} from './rerank-profile'

/**
 * Wave 6 ruling (c) — the ONE shared posture helper (`programme-state/steps/G1-bundle-selection/
 * rulings-wave6.md`). `main/index.ts`'s `rerankerDevicePosture` (the sidecar's own lazy-start
 * posture) and `registerRagIpc.ts`'s `resolveAskCandidateScope` (the per-ask candidate-scope
 * coupling, Wave 6 ruling (a)) both call THIS, with the SAME settings snapshot and the SAME
 * occupancy inputs, so the two cannot disagree — closing the gap the scoped Opus review of step
 * 4-5 found (finding C1): `resolveAskCandidateScope`'s own doc comment already promised "the SAME
 * hardware-profile rule the reranker sidecar's device posture uses" and nothing enforced it; each
 * call site independently re-derived the answer.
 *
 * Step 4-7 (Wave 7 ruling (a)) found a second gap (finding C2, scoped Opus review of step 4-6):
 * this helper alone guarantees the SAME function on the SAME inputs, not the SAME moment — the
 * sidecar held its posture for the whole session and only a `gpuMode`/`gpuAutoDisabled` flip
 * suspended it, so an `activeModelId` change (feeding this helper through `chatModelNeedMib`)
 * could move the posture without suspending anything. Step 4-7 added an event-time suspend on
 * every settings-writing channel that touches `activeModelId` — early release, not a guarantee.
 *
 * **Wave 8 ruling (a) (step 4-8) resolved a THIRD gap, finding C3 (scoped Opus review of step
 * 4-7):** `settings.activeModelId` is a PROXY for "the chat model that will be loaded" — a proxy
 * this helper trusted at the one moment it is guaranteed false, the whole `runtime:use` hash +
 * load window, during which the OLD model is still resident while the setting already names the
 * NEW one. This helper's chat-model input is now the RUNTIME's COMMITTED model
 * (`RuntimeManager.activeModelId()`, `occupancy.committedModelId` below) — never the setting, and
 * never a fallback to it. A null committed model, or ANY absent occupancy input, means `cpu`:
 * headroom is not provable while a chat-model start is in flight or pending
 * (`occupancy.chatStartBusy`) or while a GPU-posture translation sidecar occupies the card
 * (`occupancy.translationOccupied`, ruling (b)(T) — `Translator.gpuOccupied()`,
 * `translation/index.ts`). The use-time re-check that makes this correct at every MOMENT, not
 * only at cold-start, is Q (`reranker/llama.ts`'s `resolveServer`) — this helper is consulted
 * fresh on every `rerank()` call, not once per sidecar lifetime. Wave 7's three event-time
 * suspends (`gpuMode`/`gpuAutoDisabled`/`activeModelId` via `settings:update`/`models:select`/
 * `runtime:use`) stay as early release — a head start before the next `rerank()` would have
 * resolved the new posture on its own — not what makes the posture correct; Q is.
 *
 * Impure (reads `settings.gpuProbe`, resolves a manifest off disk, reads the runtime manager and
 * the live translator) — lives here, outside `rag/rerank-profile.ts`, which must stay free of
 * `node:`/`electron` imports for the renderer boundary (see that module's `CPU_HI_MIN_THREADS`
 * doc comment). Both callers are already main-process/Electron modules.
 *
 * `settings: null` (a locked workspace) and a `null`/unresolvable `manifestsDir` both mean
 * `'cpu'` — the existing "not provable ⇒ CPU" semantics, unchanged.
 */
export function resolveRerankerDevicePosture(
  settings: AppSettings | null,
  manifestsDir: string | null,
  occupancy: RerankerOccupancySnapshot
): RerankerDevice {
  const here = machineKey(detectSystem())
  const gpuAllowed = settings != null && settings.gpuMode === 'auto' && !settings.gpuAutoDisabled
  const probeDevices = gpuAllowed ? eligibleDevicesFor(settings, here) : null
  const budgetDevice = Array.isArray(probeDevices) ? primaryUsefulDevice(probeDevices) : null
  const budgetMib = graphicsBudgetMib(budgetDevice)
  // Wave 8 ruling (a): a chat start in flight/pending, or a GPU-posture translation occupant,
  // makes headroom not provable regardless of what the committed model's own placement would
  // say — checked BEFORE resolving the manifest, so neither occupancy input needs a manifest
  // lookup to short-circuit to `cpu`. #495 follow-up: a reranker already demoted this session
  // (the sidecar's own `gpuFellBack` latch) forces `cpu` here too, so the ask-site candidate
  // scope (`registerRagIpc.ts`'s `resolveAskCandidateScope`, the one caller that can see this
  // field) agrees with the sidecar instead of asking for a GPU-sized scope G would refuse.
  if (occupancy.chatStartBusy || occupancy.translationOccupied || occupancy.rerankerDemoted) return 'cpu'
  return rerankerDeviceFor({ probeDevices, budgetMib, chatModelNeedMib: committedChatShareMib(manifestsDir, occupancy) })
}

/**
 * #473 ruling 3: what the committed chat model holds on the card, MiB — measured when this
 * session's load log describes it at its launched window, else the estimate scaled to that window
 * (`chatGraphicsShareMib`); 0 when it runs on the processor, which holds nothing there. Null when
 * no model is committed or its manifest cannot be resolved — each gate decides what that means.
 */
function committedChatShareMib(manifestsDir: string | null, occupancy: RerankerOccupancySnapshot): number | null {
  const manifest = findManifestById(manifestsDir, occupancy.committedModelId)
  if (!manifest) return null
  if (occupancy.committedBackend === 'cpu') return 0
  return chatGraphicsShareMib(manifest, occupancy.committedContextTokens, latestModelPlacement())
}

/**
 * #473 ruling 1 (owner, 2026-10-12): the translation sidecar's headroom gate — the reranker's kind
 * of check. A GPU-posture start ('auto') only when the translation model FULLY fits on the budget
 * device beside the chat model the app already has there; otherwise the processor ('cpu').
 *
 * Why a gate and not `--fit-target`: llama.cpp's own `--fit` sizes a process by the free figure,
 * and on NVIDIA/Windows (`model-benchmarks.md` §6.6 finding 2) and Apple Silicon (Metal reports
 * free memory per process) that figure never sees a resident chat model, so a translation start
 * beside one overcommitted the card. Where the figure does track usage (Linux), `--fit` squeezed
 * translation into a partial offload that ran at about processor speed (§11.4), so giving up the
 * partial offload costs little. Nothing is passed to llama.cpp's fit.
 *
 * 'auto' — today's behaviour, llama.cpp decides — whenever there is nothing to judge: a locked
 * workspace, no stored probe for this machine (a probe that timed out this session), no usable
 * discrete card (an integrated-only machine shares RAM, out of scope), or no translation manifest.
 * Otherwise the processor, with the reason the Translate screen names: 'chat-starting' while a chat
 * start is in flight or pending (ruling 2: chat takes the card first), 'too-large' when the model
 * does not fit on this card even alone, 'beside-chat' when it does not fit beside the chat model
 * (or that model's manifest cannot be resolved). The reranker is not counted: a GPU-posture
 * translation start first takes it off the card (`translationGpuYield`).
 */
export function resolveTranslationDevicePosture(
  settings: AppSettings | null,
  manifestsDir: string | null,
  occupancy: RerankerOccupancySnapshot,
  translationModelId: string
): TranslationHeadroomVerdict {
  if (settings == null) return 'auto'
  const budgetMib = graphicsBudgetMib(primaryUsefulDevice(eligibleDevicesFor(settings, machineKey(detectSystem()))))
  if (budgetMib == null) return 'auto'
  const translation = findManifestById(manifestsDir, translationModelId)
  if (!translation) return 'auto'
  if (occupancy.chatStartBusy || occupancy.chatStartRequested) return 'chat-starting'
  const needMib = estimateGraphicsNeedMib(translation)
  if (budgetMib < needMib) return 'too-large'
  const chatShareMib = occupancy.committedModelId == null ? 0 : committedChatShareMib(manifestsDir, occupancy)
  if (chatShareMib == null) return 'beside-chat'
  return budgetMib - chatShareMib >= needMib ? 'auto' : 'beside-chat'
}

/**
 * #473 ruling 2's scope: does this machine have a card the gates share out — GPU on, and a usable
 * discrete device (or Apple's Metal pool) on record for it? The chat-first hook yields only then;
 * a machine without one has nothing on a card to give back (an integrated GPU is out of scope).
 */
export function cardIsShared(settings: AppSettings | null): boolean {
  if (settings == null || settings.gpuMode !== 'auto' || settings.gpuAutoDisabled) return false
  return primaryUsefulDevice(eligibleDevicesFor(settings, machineKey(detectSystem()))) != null
}

/** The translation gate as the sidecar's `gpuHeadroom` callback, from the reranker callbacks' own
 *  runtime/counter/settings dependencies (one occupancy snapshot builder for both gates). */
export function createTranslationHeadroom(
  deps: Omit<RerankerCallbackDeps, 'getTranslator'>
): (modelId: string) => TranslationHeadroomVerdict {
  return (modelId) =>
    resolveTranslationDevicePosture(
      deps.getSettings(),
      deps.manifestsDir,
      snapshotRerankerOccupancy(deps.runtimeManager, deps.pendingModelSwitches, () => null),
      modelId
    )
}

/**
 * Wave 8 ruling (a) — the occupancy inputs `resolveRerankerDevicePosture` reads instead of
 * `settings.activeModelId`. Every field but one is REQUIRED: an omitted one is a compile error, not
 * a silent "no occupancy" default — the analysis's own finding (F16) is that an optional wire is
 * how a posture input silently goes missing. `rerankerDemoted` says why it is the exception.
 */
export interface RerankerOccupancySnapshot {
  /**
   * `RuntimeManager.activeModelId()` — the chat model actually committed right now, or null (no
   * runtime, a stop, a failed load, or the kill half of a switch still in progress). NEVER
   * `settings.activeModelId`, and never a fallback to it.
   */
  committedModelId: string | null
  /**
   * True while a chat-model start is in flight (`RuntimeManager.status().startingModelId`) or
   * PENDING in `startModelRuntime` (the per-call counter Wave 8 ruling (a) adds, covering the
   * window `startModelRuntime` spends awaiting the reranker's own single-flight suspend after
   * committing to a real model switch — see `registerModelIpc.ts`).
   */
  chatStartBusy: boolean
  /**
   * True while a GPU-posture translation sidecar is loading, resident, or still being torn down
   * (Wave 8 ruling (b)(T) — `Translator.gpuOccupied()`, `translation/index.ts`).
   */
  translationOccupied: boolean
  /**
   * #495 follow-up: true once the reranker's own session
   * GPU-fallback latch is armed (`Reranker.gpuDemoted()` — a pure field read, never
   * `devicePosture()`, which would recurse for the sidecar's own posture callback below).
   * OPTIONAL: `snapshotRerankerOccupancy` cannot read the latch, so its callers without a reranker
   * in reach (the sidecar's own posture, the translation gate) resolve an omitted field to "not
   * known to be demoted". The one production reader that can actually see the latch is
   * `registerRagIpc.ts`'s ask site, which sets it explicitly from `ctx.reranker?.gpuDemoted?.()`.
   */
  rerankerDemoted?: boolean
  /**
   * #473 ruling 3: the committed runtime's launched context window (`contextWindow()`) and the
   * backend it landed on. Null when no runtime reports one: an unscaled estimate on the card,
   * never a smaller share.
   */
  committedContextTokens: number | null
  committedBackend: 'gpu' | 'cpu' | null
  /**
   * #473: a chat start is requested but not yet in the manager (#599's pre-start window — the
   * weight check). Read by the TRANSLATION gate only, so a translation cold start does not load
   * onto the card moments before the chat start takes it back; the reranker's posture is unchanged.
   */
  chatStartRequested: boolean
}

/**
 * Wave 8 ruling (a) — a per-call counter of `startModelRuntime` calls presently awaiting the
 * reranker's single-flight suspend after committing to a REAL model switch. A COUNTER, never a
 * boolean: the unlock auto-start and a Use click can overlap (`registerModelIpc.ts`'s
 * `maybeAutoStartActiveModel` and `useModel`/`startRuntime` IPC handlers can both be mid-flight
 * at once), so a boolean cleared by the first call to settle would silently stop covering the
 * second. `decrement()` is called from a `finally` whose `try` begins immediately after
 * `increment()` — never a function-wide `finally`, which would also decrement for a start
 * refused BEFORE the increment (the unknown-id/role guard, the install gate, the RAM gate) and
 * drive the count negative.
 */
export interface PendingModelSwitchCounter {
  readonly count: number
  increment(): void
  decrement(): void
}

/** A fresh, zeroed `PendingModelSwitchCounter` — one instance per app session, held on
 *  `AppContext` and shared between `registerModelIpc.ts` and the posture-callback factory below. */
export function createPendingModelSwitchCounter(): PendingModelSwitchCounter {
  let count = 0
  return {
    get count() {
      return count
    },
    increment() {
      count++
    },
    decrement() {
      count--
    }
  }
}

/**
 * Wave 8 ruling (b)(G) — the CPU posture's per-call request ceiling:
 * `2 × ragTopKInitial + totalCandidateCapFor(rerankScopeFor(profile, input, 'cpu'))`, computed
 * from settings with the EXISTING pure functions (`resolveRerankProfile`, `rerankScopeFor`,
 * `totalCandidateCapFor`) — never a literal 48/72. The profile classification still matters on
 * the `cpu` posture: a `gpu`-profile machine whose headroom gate landed on `cpu` gets the
 * `capped` cap (48), while a `cpu-hi` machine with the wide-scope opt-in gets `top48`'s cap (72).
 * A null `settings` snapshot (a momentarily locked workspace, mirroring the posture resolver's
 * own defensive degrade) falls back to `DEFAULT_SETTINGS`'s values.
 */
export function cpuRequestCeilingFor(settings: AppSettings | null): number {
  const s = settings ?? DEFAULT_SETTINGS
  const here = machineKey(detectSystem())
  const input: RerankProfileInput = {
    gpuMode: s.gpuMode,
    gpuAutoDisabled: s.gpuAutoDisabled,
    probeDevices: eligibleDevicesFor(s, here),
    threads: defaultThreadCount(),
    rerankerAvailable: true,
    wideScopeOptIn: s.ragRerankWideScope
  }
  const profile = resolveRerankProfile(input)
  const scope = rerankScopeFor(profile, input, 'cpu')
  return 2 * s.ragTopKInitial + totalCandidateCapFor(scope)
}

/**
 * Wave 8 ruling (a)/(b)/NF-1 — the exported closure FACTORY the posture and G callbacks are
 * built from. Every dependency is REQUIRED (no optional field): a composition test targets this
 * factory with `// @ts-expect-error` cases proving so, because a composition test that only
 * reaches `compose-services.ts` → `reranker/factory.ts` cannot see an omission at `main/index.ts`
 * or at the ask site (the ask handler's `resolveAskCandidateScope` call in `registerRagIpc.ts`) —
 * the analysis's finding NF-1. `main/index.ts` calls this ONCE and threads both returned
 * callbacks into `composeServices`.
 */
export interface RerankerCallbackDeps {
  /** The chat runtime manager — only the three read-only methods the posture needs. */
  runtimeManager: Pick<RuntimeManager, 'activeModelId' | 'status' | 'active'>
  /** Wave 8 ruling (a)'s pending-switch counter — shared with `registerModelIpc.ts`. */
  pendingModelSwitches: PendingModelSwitchCounter
  /**
   * Read live, never captured: `ctx.translator` is REASSIGNED by `onModelInstalled` when a
   * mid-session download makes translation available (`main/index.ts`), so a closure that
   * captured the startup value would consult a permanently-null or stale instance.
   */
  getTranslator: () => Translator | null
  /** Settings getter — the locked-workspace fallback (→ null) stays at the call site. */
  getSettings: () => AppSettings | null
  manifestsDir: string | null
}

/**
 * Build the reranker's device-posture and CPU-request-ceiling callbacks from ONE set of
 * dependencies (Wave 8 rulings (a), (b)(G), (b)(T)). Threaded into `composeServices` as its two
 * now-required reranker options.
 */
export function createRerankerCallbacks(deps: RerankerCallbackDeps): {
  devicePosture: () => RerankerDevice
  requestCeiling: () => number
} {
  const occupancy = (): RerankerOccupancySnapshot =>
    snapshotRerankerOccupancy(deps.runtimeManager, deps.pendingModelSwitches, deps.getTranslator)
  return {
    devicePosture: () => resolveRerankerDevicePosture(deps.getSettings(), deps.manifestsDir, occupancy()),
    requestCeiling: () => cpuRequestCeilingFor(deps.getSettings())
  }
}

/**
 * Wave 8 ruling (a)/(b)(T) — build one `RerankerOccupancySnapshot` from the runtime manager, the
 * pending-switch counter and a live translator getter. Exported so `createRerankerCallbacks`
 * (the sidecar's own posture seam) and `registerRagIpc.ts`'s ask-site call to
 * `resolveAskCandidateScope` (in the ask handler) build the occupancy snapshot the SAME way —
 * never two independent re-derivations that could drift.
 */
export function snapshotRerankerOccupancy(
  runtimeManager: Pick<RuntimeManager, 'activeModelId' | 'status' | 'active'>,
  pendingModelSwitches: PendingModelSwitchCounter,
  getTranslator: () => Translator | null
): RerankerOccupancySnapshot {
  const status = runtimeManager.status()
  const active = runtimeManager.active()
  return {
    committedModelId: runtimeManager.activeModelId(),
    chatStartBusy: status.startingModelId != null || pendingModelSwitches.count > 0,
    translationOccupied: getTranslator()?.gpuOccupied?.() ?? false,
    committedContextTokens: active?.contextWindow?.() ?? null,
    committedBackend: active?.backend === 'gpu' || active?.backend === 'cpu' ? active.backend : null,
    chatStartRequested: status.startRequested === true
  }
}

/**
 * The settings keys whose presence in a `settings:update` patch makes that handler take the
 * EVENT-TIME suspend check below (step 4-4 watched `gpuMode` and `gpuAutoDisabled`; Wave 7 ruling
 * (a), step 4-7, added `activeModelId`). Since #868 that channel carries renderer-owned keys only, so `gpuMode` is
 * the one left: `gpuAutoDisabled` and `activeModelId` are main-owned and dropped from the patch,
 * and `models:select` / `runtime:use` run the `activeModelId` check themselves
 * (`registerModelIpc.ts`). Typed to the renderer-owned keys so a main-owned one cannot come back
 * here. The suspend is early release only: Q (`reranker/llama.ts`, Wave 8) is what makes the
 * posture correct. This is NOT the list of everything the posture resolver reads (it also reads
 * `gpuAutoDisabled`, `settings.gpuProbe` — a known, pre-existing, out-of-scope residual,
 * `channels.json` id 4 — the runtime's committed model and in-flight/pending state and the
 * translator's occupancy). `activeEmbeddingModelId` is not a posture input at all (the
 * embedder is a separate slot the reranker's placement estimate never contends with).
 */
export const RERANKER_POSTURE_SETTINGS_KEYS: ReadonlyArray<RendererSettingsKey> = ['gpuMode']

/** The posture's settings inputs, snapshotted before or after a write (Wave 7 ruling (c)). */
export interface RerankerPostureSnapshot {
  gpuMode: AppSettings['gpuMode']
  gpuAutoDisabled: boolean
  activeModelId: string | null
}

/**
 * Wave 7 ruling (c) — the ONE shared predicate answering "does this settings change invalidate
 * the resident reranker sidecar's EARLY-RELEASE posture?" (Wave 8: Q, not this event-time check,
 * is what makes the posture correct at every moment — see `resolveRerankerDevicePosture`'s own
 * doc comment), called from `registerCoreIpc.ts`'s `settings:update` handler and from both of
 * `registerModelIpc.ts`'s `selectModel` call sites (`models:select`, `runtime:use`) — never three
 * independent copies of the same comparison.
 *
 * REAL-flip only: a caller must snapshot `before` off `getSettings` BEFORE the write and pass the
 * POST-write result as `after` — this function compares actual values, never patch key presence,
 * so a patch that merely repeats today's value (or touches an unrelated key) never suspends.
 */
export function rerankerPostureInputsChanged(
  before: RerankerPostureSnapshot,
  after: RerankerPostureSnapshot
): boolean {
  return (
    before.gpuMode !== after.gpuMode ||
    before.gpuAutoDisabled !== after.gpuAutoDisabled ||
    before.activeModelId !== after.activeModelId
  )
}