Skip to content

AI, fully local on your computer.

9.5 KB
Raw Blame
import type { EngineProblemFamily, EngineRecheckResult } from '../../shared/types'
import type { AppContext } from '../services/context'
import {
  checkProgramLoads,
  healEngineLoadState,
  resolveVoiceEngine,
  type LoadCheck,
  type LoadCheckDeps
} from '../services/engine-health'
import {
  clearEngineProblem,
  engineProblemBuilds,
  engineProblems,
  onEngineProblemDropped
} from '../services/runtime/engine-load'
import { clearModelLoadLatches } from '../services/runtime/factory'
import { resolveCpuFallbackServerPath, resolveLlamaServerPath } from '../services/runtime/sidecar'
import { workspaceAdmitsWork } from '../services/workspace-vault'
import { log } from '../services/logging'
import { rearmStartRefusals } from '../services/model-start-check'
import { probeAndPersistGpu } from './registerBenchmarkIpc'
import { startModelRuntime } from './registerModelIpc'
import { isModelStarting } from '../../shared/runtime-status'

// "Check again" (#530, `engine:recheck`; architecture.md "Engine load failures"): the user
// installed the missing library (or the Visual C++ runtime) and wants the engine back without a
// restart. Each family with a verdict is started once more; the ones the OS now accepts lose their
// verdict, and every latch a refusal left behind is re-armed — so the next start, import, question
// or translation simply works. A restart heals too: the verdict is never persisted.

/**
 * Re-arm every chat-engine consumer that latched a failed start: the #372 model latches, the
 * embedder's and reranker's `startFailed`, the vision cooldown and a latched translator slot. Run
 * after a chat-engine install (a new binary — before #530 that re-armed the model latches only, so
 * a repaired engine still failed every import until a restart), after a successful re-check, and
 * when a program the OS had refused starts ({@link rearmWhenChatEngineHeals}, #654).
 */
export function rearmLlamaConsumers(ctx: AppContext): void {
  clearModelLoadLatches()
  // The one re-arm helper (#913 code review), unfiltered: every role runs this engine.
  rearmStartRefusals(ctx)
}

/**
 * #654: heal what a refusal of the chat engine left behind when a program that reported it STARTS —
 * a healthy `LlamaServer` start (chat or any sidecar) or the device probe answering. Before, that
 * dropped the verdict and nothing else: the banner and its Check again were gone while every import
 * still failed on the embedder's latch, and a model the demo runtime stood in for stayed there. Now
 * every consumer is re-armed, and that model comes back on the engine the way Check again brings it
 * back: the device probe re-run and persisted first (the refused probes persisted nothing, and the
 * ladder must read this computer's answer, #380), then the restart (owner calls 2026-10-13). Only when
 * that model really is on the demo runtime with no start in flight: the chat's own healthy start, the
 * usual trigger, neither restarts nor probes beside its weight upload (#473). The other drops heal
 * elsewhere: "Check again" and an install re-arm and restart on their own; `gone` heals nothing (the
 * program left the drive). Subscribed once at startup (`main/index.ts`); returns the unsubscribe. The
 * re-arm spawns nothing, so it cannot feed a new verdict back into the store while the store notifies.
 */
export function rearmWhenChatEngineHeals(ctx: AppContext, deps: Pick<RecheckDeps, 'restartChat'> = {}): () => void {
  return onEngineProblemDropped((family, why) => {
    if (family !== 'llama_cpp' || why !== 'started') return
    rearmLlamaConsumers(ctx)
    if (!demoRuntimeStandsIn(ctx)) return
    const restart = deps.restartChat ?? restartChatOnRealEngine
    void reprobeGpu(ctx)
      .then(() => restart(ctx))
      .catch((err: unknown) => {
        log.warn('Restarting the model after the AI engine ran again failed', { error: String(err) })
      })
  })
}

/** A selected model answers from the demo runtime and no start is in flight. */
function demoRuntimeStandsIn(ctx: AppContext): boolean {
  const status = ctx.runtime.status()
  return status.backend === 'mock' && status.modelId != null && !isModelStarting(status)
}

/**
 * The device probe the refusal left unanswered: re-probe and persist the real answer for this
 * computer (the cache is dropped first so no earlier answer stands in for it). Admission-gated.
 */
async function reprobeGpu(ctx: AppContext): Promise<void> {
  if (!workspaceAdmitsWork(ctx.workspace)) return
  ctx.probeGpu?.invalidate()
  await probeAndPersistGpu(ctx)
}

/**
 * Bring the selected model back on the real engine when the demo runtime is standing in for it
 * (the walk ended on the mock because every rung was refused, or the engine was missing). Run
 * fire-and-forget by the re-check and, since #532, after a chat-engine install: a model load takes
 * as long as it takes, and the screens follow the runtime status as usual. The same discipline as
 * the IPC start paths: an active deep-index build is aborted first. A start already in flight is
 * left alone — it walks the ladder on the engine as it now is, and a second start would cut it off.
 */
export async function restartChatOnRealEngine(ctx: AppContext): Promise<void> {
  const modelId = ctx.runtime.status().modelId
  if (!demoRuntimeStandsIn(ctx) || !modelId) return
  if (!workspaceAdmitsWork(ctx.workspace)) return
  log.info('The real AI engine is on the drive — restarting the selected model on it', { modelId })
  ctx.docTasks?.abortActiveBuild()
  await ctx.runtime.stop({ startFollows: true })
  await startModelRuntime(ctx, modelId)
}

export interface RecheckDeps extends LoadCheckDeps {
  /** Test seam: the restart that follows a healed chat engine (default: fire-and-forget restart). */
  restartChat?: (ctx: AppContext) => Promise<void>
}

/**
 * Single flight: two "Check again" buttons can be on screen at once (the chat banner and the voice
 * hint), and a second concurrent pass would stop/start the model a second time — possibly cancelling
 * the first restart or a start the user just made. A click while a pass runs joins it.
 */
let inFlight: Promise<EngineRecheckResult> | null = null

/** Re-check every engine with a verdict; see the module note. Never throws. */
export function recheckEngines(ctx: AppContext, deps: RecheckDeps = {}): Promise<EngineRecheckResult> {
  if (inFlight) return inFlight
  const run = runRecheck(ctx, deps).finally(() => {
    inFlight = null
  })
  inFlight = run
  return run
}

async function runRecheck(ctx: AppContext, deps: RecheckDeps): Promise<EngineRecheckResult> {
  const healed: EngineProblemFamily[] = []
  for (const problem of engineProblems()) {
    const family = problem.family
    try {
      const outcome = await recheckFamily(ctx, family, deps)
      // 'loads': healed here — unless a healthy start dropped the verdict while the check ran, whose
      // own heal (`rearmWhenChatEngineHeals`) already re-armed and restarted; a second pass would
      // re-probe beside that restart's weight upload. 'gone': the program left the drive — its
      // verdict no longer describes anything (the missing-engine banner speaks now; a `cpu/` net that
      // left is simply no rung 3), but nothing was healed, so nothing is re-armed. 'unchecked': it could not be started at all (integrity
      // check, a spawn error that says nothing about the loader) — the verdict stands.
      if (outcome === 'loads') {
        if (clearEngineProblem(family, 'rechecked')) healed.push(family)
      } else if (outcome === 'gone') {
        clearEngineProblem(family, 'gone')
      }
    } catch (err) {
      log.warn('Engine re-check failed', { family, error: String(err) })
    }
  }
  if (healed.length > 0) log.info('Engine re-check: the operating system now starts it', { healed })
  if (healed.includes('llama_cpp')) {
    rearmLlamaConsumers(ctx)
    // A compatibility-mode flag the refusal had set, and rows that still carry its raw line.
    healEngineLoadState(ctx, deps)
    await reprobeGpu(ctx)
    const restart = deps.restartChat ?? restartChatOnRealEngine
    void restart(ctx).catch((err: unknown) => {
      log.warn('Restarting the model after the engine re-check failed', { error: String(err) })
    })
  }
  return { problems: engineProblems() }
}

/**
 * Start the family's program just far enough to know whether the OS accepts it. An explicit spawn
 * rather than the GPU probe: the probe answers `[]` for a binary it never ran (a failed integrity
 * check, which would read as "loads") and the same `null` for a refusal as for a crash or a spawn
 * error (#653). `checkProgramLoads` tells them apart: a failed integrity check or a non-refusal
 * spawn error is 'unchecked', a crash is 'loads' (the loader accepted the program), a refusal is
 * 'refused'. The chat engine's program is the main build, whose start clears any verdict it reported
 * (#655) — unless only the `cpu/` net reported it (#678): the main build loading says nothing about
 * that one, and "runs again" would come back with the next walk that reaches rung 3.
 */
async function recheckFamily(
  ctx: AppContext,
  family: EngineProblemFamily,
  deps: RecheckDeps
): Promise<LoadCheck | 'gone'> {
  const builds = engineProblemBuilds(family)
  const binPath =
    family !== 'llama_cpp'
      ? resolveVoiceEngine(ctx)
      : builds?.cpuNet === true && !builds.main
        ? resolveCpuFallbackServerPath(ctx.paths.rootPath)
        : resolveLlamaServerPath(ctx.paths.rootPath, process.platform, process.env, { isDev: ctx.isDev })
  // The program is gone (deleted, or the drive changed).
  if (!binPath) return 'gone'
  return checkProgramLoads(family, binPath, family === 'llama_cpp' ? ['--version'] : ['--help'], deps)
}