Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

25.2 KB
Raw Blame
//! The supervised llama.cpp worker.
//!
//! Statically linked, but in its own process on purpose. `GGML_ASSERT` calls
//! `abort()` unconditionally (`ggml/src/ggml.c`), and there are thousands of
//! assert sites plus every CUDA/Vulkan error path; `catch_unwind` cannot
//! contain any of them. Jan survives those today only because inference happens
//! in another process, and this preserves that while removing the *downloaded*
//! `llama-server` binary and the router's per-model subprocesses.
//!
//! Usage:
//!   jan-llama-worker --preset <router.preset.ini> [--port N] [--models-max N]
//!   jan-llama-worker --diagnose [--diagnose-preset quick|full|model]
//!                    [--diagnose-plan JSON] [--device NAME]...
//!   jan-llama-worker --diagnose-run <device> [--mode test|support] [--op RE]
//!                    [--params RE] [--workers N]
//!   jan-llama-worker --diagnose-devices | --diagnose-ops | --list-devices
//!
//! The diagnose modes are the ggml op check. `--diagnose` is the root: it loads
//! no ggml and supervises one `--diagnose-run` leaf per device and op family,
//! printing one JSON event per line (see `engine::diagnose::RootEvent`). Cancel
//! it the same way the serving mode stops, by closing its stdin.
//!
//! The bearer token comes from `JAN_LLAMA_API_KEY`, never argv: argv is
//! readable by any other process on the machine (`ps`, `/proc/<pid>/cmdline`,
//! Task Manager) and the supervisor logs it.
//!
//! Prints one line of JSON on stdout once it is listening, so the supervisor
//! learns the port without racing on a log file:
//!   {"port":39271,"pid":1234,"models":["a","b"]}

use std::sync::Arc;

use tauri_plugin_llamacpp::engine::diag::{DiagMode, DiagRunRequest, FamilyFilter};
use tauri_plugin_llamacpp::engine::model_plan::{self, ModelPlan};
use tauri_plugin_llamacpp::engine::diagnose;
use tauri_plugin_llamacpp::engine::http::EngineServer;
use tauri_plugin_llamacpp::engine::preset;
use tauri_plugin_llamacpp::engine::registry::Registry;
use tauri_plugin_llamacpp::engine::slots::StateStore;
use tauri_plugin_llamacpp::engine::{assert_pinned_version, PINNED_TAG};
use tokio::sync::Mutex;

/// What this invocation is for. All but [`Mode::Diagnose`] need ggml; the root
/// deliberately does not, which is what makes it the crash boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
enum Mode {
    Serve,
    /// Print the offloadable devices as JSON and exit. The desktop process
    /// links the plugin *without* the engine feature, so it has no ggml to ask
    /// -- it shells out here, exactly as it used to shell out to
    /// `llama-server --list-devices`.
    ListDevices,
    /// Orchestrate an op diagnostic over leaf processes of this same binary.
    Diagnose,
    /// One device, one slice of the op list: the leaf a root spawns.
    DiagnoseRun(String),
    /// Every ggml device with its kind and memory, for a root building a plan.
    DiagnoseDevices,
    /// Every op family with its case count, for the Full preset.
    DiagnoseOps,
}

struct Args {
    preset: Option<String>,
    port: u16,
    models_max: usize,
    /// Ceiling on the saved-KV directory, in MiB. 0 turns cross-session KV
    /// persistence off entirely: the directory is what the feature is, so
    /// there is nothing to keep without a budget for it.
    slot_cache_mib: u64,
    mode: Mode,
    diag: DiagArgs,
}

#[derive(Debug, Clone)]
struct DiagArgs {
    preset: diagnose::DiagPreset,
    mode: DiagMode,
    op_filter: Option<String>,
    params_filter: Option<String>,
    /// Devices the root should check. Empty means every device the probe finds.
    devices: Vec<String>,
    /// The model-specific family list, from `--diagnose-plan`. Built by the
    /// caller (which can read a gguf) rather than here, so the root still
    /// holds no model and no ggml.
    plan: Option<ModelPlan>,
    /// 0 means "pick from the machine", which is only sensible in test mode.
    workers: u32,
    family_timeout: std::time::Duration,
    deadline: std::time::Duration,
}

impl Default for DiagArgs {
    fn default() -> Self {
        Self {
            preset: diagnose::DiagPreset::Quick,
            mode: DiagMode::Test,
            op_filter: None,
            params_filter: None,
            devices: Vec::new(),
            plan: None,
            workers: 0,
            family_timeout: diagnose::DEFAULT_FAMILY_TIMEOUT,
            deadline: diagnose::DEFAULT_DEADLINE,
        }
    }
}

impl DiagArgs {
    /// Upstream threads test mode only, and a support probe builds graphs
    /// rather than running them, so parallelism there buys nothing.
    fn workers(&self) -> u32 {
        if self.mode == DiagMode::Support {
            return 1;
        }
        match self.workers {
            0 => std::thread::available_parallelism().map_or(1, |n| n.get() as u32),
            n => n,
        }
    }
}

/// Read from the environment rather than argv so the token never appears in a
/// process listing. Empty means "no auth", which is only sensible when the
/// caller owns the loopback port outright (tests).
const API_KEY_ENV: &str = "JAN_LLAMA_API_KEY";

fn parse_args() -> Result<Args, String> {
    let mut out = Args {
        preset: None,
        port: 0,
        models_max: 1,
        slot_cache_mib: 0,
        mode: Mode::Serve,
        diag: DiagArgs::default(),
    };
    let mut it = std::env::args().skip(1);
    while let Some(flag) = it.next() {
        let mut value = || {
            it.next()
                .ok_or_else(|| format!("{flag} needs a value"))
        };
        match flag.as_str() {
            "--preset" => out.preset = Some(value()?),
            "--port" => {
                out.port = value()?.parse().map_err(|e| format!("bad --port: {e}"))?
            }
            "--models-max" => {
                out.models_max = value()?
                    .parse()
                    .map_err(|e| format!("bad --models-max: {e}"))?
            }
            "--slot-cache-mib" => {
                out.slot_cache_mib = value()?
                    .parse()
                    .map_err(|e| format!("bad --slot-cache-mib: {e}"))?
            }
            "--list-devices" => out.mode = Mode::ListDevices,
            "--diagnose" => out.mode = Mode::Diagnose,
            "--diagnose-run" => out.mode = Mode::DiagnoseRun(value()?),
            "--diagnose-devices" => out.mode = Mode::DiagnoseDevices,
            "--diagnose-ops" => out.mode = Mode::DiagnoseOps,
            "--diagnose-preset" => {
                out.diag.preset = match value()?.as_str() {
                    "quick" => diagnose::DiagPreset::Quick,
                    "full" => diagnose::DiagPreset::Full,
                    "model" => diagnose::DiagPreset::Model,
                    other => return Err(format!("bad --diagnose-preset: {other}")),
                }
            }
            "--diagnose-plan" => {
                out.diag.plan = Some(
                    serde_json::from_str(&value()?)
                        .map_err(|e| format!("bad --diagnose-plan: {e}"))?,
                );
                out.diag.preset = diagnose::DiagPreset::Model;
            }
            "--mode" => {
                out.diag.mode = match value()?.as_str() {
                    "test" => DiagMode::Test,
                    "support" => DiagMode::Support,
                    other => return Err(format!("bad --mode: {other}")),
                }
            }
            "--op" => out.diag.op_filter = Some(value()?),
            "--params" => out.diag.params_filter = Some(value()?),
            "--device" => out.diag.devices.push(value()?),
            "--workers" => {
                out.diag.workers = value()?
                    .parse()
                    .map_err(|e| format!("bad --workers: {e}"))?
            }
            "--family-timeout" => {
                out.diag.family_timeout = std::time::Duration::from_secs(
                    value()?
                        .parse()
                        .map_err(|e| format!("bad --family-timeout: {e}"))?,
                )
            }
            "--deadline" => {
                out.diag.deadline = std::time::Duration::from_secs(
                    value()?.parse().map_err(|e| format!("bad --deadline: {e}"))?,
                )
            }
            "--version" => {
                println!("jan-llama-worker (llama.cpp {PINNED_TAG})");
                std::process::exit(0);
            }
            other => return Err(format!("unknown flag: {other}")),
        }
    }
    Ok(out)
}

/// The devices this process initialised, as `[{"id","name"}]`, for the
/// handshake. `null` when they cannot be read: the app treats that as "not
/// reported", which is different from `[]`, "no GPU on this machine". Uses the
/// memory-free listing on purpose; see `Engine::device_identities_json`.
fn device_identities() -> serde_json::Value {
    tauri_plugin_llamacpp::engine::Engine::device_identities_json()
        .ok()
        .and_then(|json| serde_json::from_str(&json).ok())
        .unwrap_or(serde_json::Value::Null)
}

#[tokio::main]
async fn main() {
    // Before `parse_args`, since a failure to parse is one of the reasons the
    // root never starts and the supervisor reads reasons off stdout alone.
    let diagnosing = std::env::args().any(|a| a == "--diagnose");
    let args = match parse_args() {
        Ok(a) => a,
        Err(e) => {
            if diagnosing {
                diag_root_fail(e);
            }
            eprintln!("jan-llama-worker: {e}");
            std::process::exit(2);
        }
    };

    // A mismatched llama.cpp is memory corruption, not a link error:
    // common_params and llama_context_params cross the boundary by value and
    // their layout has changed repeatedly upstream.
    if let Err(e) = assert_pinned_version() {
        if diagnosing {
            diag_root_fail(e.to_string());
        }
        eprintln!("jan-llama-worker: {e}");
        std::process::exit(3);
    }

    // The root is the one mode that must never hold ggml: it supervises the
    // leaves that do, so a kernel that aborts cannot take the run with it.
    if args.mode == Mode::Diagnose {
        diagnose_root(&args.diag).await;
        // Exit rather than return: the cancel watcher is a blocking read on
        // stdin, and tokio waits for its blocking pool when the runtime drops,
        // so a parent still holding the pipe would keep a finished root alive.
        // Every event has already been written and flushed.
        std::process::exit(0);
    }

    // Must precede any ggml call, including device enumeration.
    tauri_plugin_llamacpp::engine::load_backend_modules();

    match &args.mode {
        Mode::ListDevices => {
            match tauri_plugin_llamacpp::engine::Engine::devices_json() {
                Ok(json) => println!("{json}"),
                Err(e) => {
                    eprintln!("jan-llama-worker: could not list devices: {e}");
                    std::process::exit(6);
                }
            }
            return;
        }
        Mode::DiagnoseDevices => {
            arm_leaf_deadline(diagnose::PROBE_TIMEOUT);
            return print_diagnose_devices();
        }
        Mode::DiagnoseOps => {
            arm_leaf_deadline(diagnose::PROBE_TIMEOUT);
            return print_diagnose_ops();
        }
        Mode::DiagnoseRun(device) => {
            arm_leaf_deadline(args.diag.family_timeout);
            return diagnose_leaf(device, &args.diag);
        }
        Mode::Diagnose => unreachable!("handled above, before ggml is loaded"),
        Mode::Serve => {}
    }

    let mut registry = Registry::new(args.models_max);
    let mut models = Vec::new();
    let mut slot_store = None;

    if let Some(path) = &args.preset {
        let ini = match std::fs::read_to_string(path) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("jan-llama-worker: could not read {path}: {e}");
                std::process::exit(4);
            }
        };
        for (section, spec) in preset::specs(path, &ini) {
            registry.register(section.clone(), spec);
            models.push(section);
        }
        models.sort();

        // The directory has to exist before any model loads: llama.cpp's
        // --slot-save-path handler throws when it does not, and that unwinds
        // out of engine startup, so a missing cache directory would take local
        // inference down rather than disabling one feature.
        if let Some(dir) = preset::shared_value(&ini, "slot-save-path") {
            let store = StateStore::new(&dir, args.slot_cache_mib);
            if let Err(e) = store.ensure_dir() {
                eprintln!("jan-llama-worker: could not create {dir}: {e}");
                std::process::exit(7);
            }
            // Pruned here, not only on reload: a budget lowered (or zeroed)
            // between sessions must reclaim the disk it was lowered for, and
            // nothing else walks this directory at startup.
            let dropped = store.prune().len();
            if dropped > 0 {
                eprintln!("jan-llama-worker: pruned {dropped} saved thread cache(s)");
            }
            // Kept even at a zero budget: erasing a deleted thread's state and
            // clearing the directory both need the store, and neither writes.
            slot_store = Some(store);
        }
    }

    let registry = Arc::new(Mutex::new(registry));
    // Taken (not read) so it does not linger in this process's environment,
    // which is also readable via /proc on Linux.
    let api_key = std::env::var(API_KEY_ENV).unwrap_or_default();
    std::env::remove_var(API_KEY_ENV);

    let registry_for_teardown = Arc::clone(&registry);
    let (server, listener) =
        match EngineServer::bind(registry, args.port, api_key).await {
            Ok(v) => v,
            Err(e) => {
                eprintln!("jan-llama-worker: could not bind: {e}");
                std::process::exit(5);
            }
        };
    let server = match slot_store {
        Some(store) => {
            if store.saves_enabled() {
                eprintln!(
                    "jan-llama-worker: keeping up to {} MiB of thread KV cache in {}",
                    args.slot_cache_mib,
                    store.dir().display()
                );
            } else {
                eprintln!(
                    "jan-llama-worker: thread KV cache is off; keeping {} clear",
                    store.dir().display()
                );
            }
            server.with_slot_state(store)
        }
        None => server,
    };

    // The supervisor reads this instead of scraping stderr, which is what the
    // router path had to do.
    println!(
        "{}",
        serde_json::json!({
            "port": server.port,
            "pid": std::process::id(),
            "models": models,
            // Set by build.rs for a CUDA engine; null otherwise. The app sets it
            // against the NVIDIA driver to explain a driver that is too old.
            "cuda_toolkit": option_env!("JAN_ENGINE_CUDA_TOOLKIT"),
            // What this process itself initialised. The app compares it with a
            // later enumeration to notice a GPU that appeared or vanished since.
            "devices": device_identities(),
        })
    );
    // stdout is a pipe to the supervisor, so it is block-buffered.
    use std::io::Write;
    let _ = std::io::stdout().flush();

    let server = Arc::new(server);
    Arc::clone(&server)
        .serve_until(listener, shutdown_requested(), DRAIN_TIMEOUT)
        .await;

    // After draining, before releasing the models: the save reads each slot
    // through its still-live engine. Closing Jan is the common way a thread
    // stops being current, so without this the feature would only ever help
    // across a thread switch, not across a session.
    server.save_resident_slots().await;

    // Releasing the models here is the whole point of exiting in-band: each
    // Engine's Drop runs jan_llama_engine_stop, terminating its server_queue
    // loop and freeing the weights. A force-killed process skips all of that.
    let released = registry_for_teardown.lock().await.shutdown();
    if !released.is_empty() {
        eprintln!("jan-llama-worker: released {}", released.join(", "));
    }
}

/// Exit code for a diagnostic that could not start. A leaf that merely found a
/// failing case exits 0: its verdict is in the event stream.
const DIAG_EXIT: i32 = 8;

/// For the probe leaves (`--diagnose-devices`, `--diagnose-ops`), whose stdout
/// is a single JSON document the root parses -- an event line printed there
/// would corrupt it. Their reason still reaches the user: a nonzero exit makes
/// `diagnose::probe` fold the last stderr lines into its error, and the root
/// then reports that through [`diag_root_fail`].
fn diag_fail(msg: String) -> ! {
    eprintln!("jan-llama-worker: {msg}");
    std::process::exit(DIAG_EXIT);
}

/// The same, for the root, whose stdout *is* the event stream. Every run ends
/// with a `done`, so a setup failure reports its reason there rather than only
/// on a stderr the user never sees.
fn diag_root_fail(msg: String) -> ! {
    let done = diagnose::RootEvent::Done {
        ok: false,
        cancelled: false,
        inconclusive: false,
        error: Some(msg.clone()),
    };
    if let Ok(line) = serde_json::to_string(&done) {
        print_event(&line);
    }
    diag_fail(msg)
}

/// Exit code for a leaf whose reader is gone. Distinct from [`DIAG_EXIT`]
/// because nothing is left to read either one.
const DIAG_EXIT_NO_READER: i32 = 9;

/// Exit code for a leaf that outlived the deadline the root gave it.
const DIAG_EXIT_DEADLINE: i32 = 10;

/// How long after the root's own limit a leaf waits before ending itself. The
/// root kills an overrunning leaf at the limit exactly, so this only ever
/// fires when there is no root left to do it.
const LEAF_DEADLINE_GRACE: std::time::Duration = std::time::Duration::from_secs(30);

/// Ends this leaf if it is still alive well after the root would have killed
/// it.
///
/// The backstop for an orphan: on Windows the plugin cannot kill a
/// grandchild (see `engine::diagnose_commands::kill_process_group`), and a
/// leaf wedged inside a driver call writes nothing, so the broken-stdout exit
/// in [`print_event`] never gets a chance to fire. A plain OS thread is what
/// makes this work -- it keeps running while the main thread is blocked
/// inside ggml, which is the state being bounded.
fn arm_leaf_deadline(limit: std::time::Duration) {
    let deadline = limit + LEAF_DEADLINE_GRACE;
    std::thread::spawn(move || {
        std::thread::sleep(deadline);
        eprintln!(
            "jan-llama-worker: no root took this leaf down within {}s; exiting",
            deadline.as_secs()
        );
        std::process::exit(DIAG_EXIT_DEADLINE);
    });
}

/// stdout is a pipe to the root, so it is block-buffered; a run that streams
/// has to flush each line or the UI sees nothing until the leaf exits.
///
/// A failed write means the root is gone, and this process is then an orphan
/// holding a device with nobody to report to, so it stops. On Unix the broken
/// pipe would raise SIGPIPE and end it anyway; Windows has no such signal,
/// which is where this is load-bearing -- the plugin cannot kill a
/// grandchild there (see `kill_process_group`). It does not bound a leaf hung
/// inside a driver call, which by definition prints nothing.
fn print_event(line: &str) {
    use std::io::Write;
    let mut out = std::io::stdout().lock();
    if writeln!(out, "{line}").and_then(|()| out.flush()).is_err() {
        std::process::exit(DIAG_EXIT_NO_READER);
    }
}

fn print_diagnose_devices() {
    use tauri_plugin_llamacpp::engine::diag::{merge_devices, DiagBackend, DiagDeviceMemory};
    use tauri_plugin_llamacpp::engine::Engine;

    let backends: Vec<DiagBackend> = Engine::diag_backends_json()
        .map_err(|e| e.to_string())
        .and_then(|json| serde_json::from_str(&json).map_err(|e| e.to_string()))
        .unwrap_or_else(|e| diag_fail(format!("could not list ggml devices: {e}")));
    // Memory is a nice-to-have: a backend that will not report it must not cost
    // us the device list the plan is built from.
    let memory: Vec<DiagDeviceMemory> = Engine::devices_json()
        .ok()
        .and_then(|json| serde_json::from_str(&json).ok())
        .unwrap_or_default();
    println!(
        "{}",
        serde_json::to_string(&merge_devices(backends, &memory)).unwrap_or_else(|_| "[]".into())
    );
}

fn print_diagnose_ops() {
    match tauri_plugin_llamacpp::engine::Engine::diag_ops_json() {
        Ok(json) => println!("{json}"),
        Err(e) => diag_fail(format!("could not list ops: {e}")),
    }
}

/// One device, one slice of the op list. Everything this prints on stdout is an
/// event; ggml's own logging goes to stderr, which the root reads separately.
fn diagnose_leaf(device: &str, diag: &DiagArgs) {
    let request = DiagRunRequest {
        mode: diag.mode,
        backend: device.to_string(),
        op_filter: diag.op_filter.clone(),
        params_filter: diag.params_filter.clone(),
        workers: diag.workers(),
    };
    let outcome = tauri_plugin_llamacpp::engine::Engine::diag_run(&request, &|line| {
        print_event(line)
    });
    // The wrapper emits `done` on every path it reaches, including a throw.
    // This covers the one it cannot: a build with no engine to call at all.
    if let Err(e) = outcome {
        print_event(
            &serde_json::json!({"event": "done", "ok": false, "error": e.to_string()}).to_string(),
        );
    }
}

/// Picks the work, then supervises one leaf per device and family.
async fn diagnose_root(diag: &DiagArgs) {
    let exe = std::env::current_exe()
        .unwrap_or_else(|e| diag_root_fail(format!("could not find my own path: {e}")));
    let spec = diagnose::LeafSpec::new(exe);

    let (mut devices, backend_modules) = diagnose::probe_devices(&spec)
        .await
        .unwrap_or_else(|e| diag_root_fail(e));
    if !diag.devices.is_empty() {
        devices.retain(|d| diag.devices.iter().any(|name| name == &d.name));
        if devices.is_empty() {
            diag_root_fail(format!("no ggml device named {}", diag.devices.join(", ")));
        }
    }

    // Asked for even on Quick: a family name upstream does not know runs no
    // cases and reports a clean pass, which is the one wrong answer a
    // diagnostic must not give.
    let tested = diagnose::probe_families(&spec)
        .await
        .unwrap_or_else(|e| diag_root_fail(e));
    let wanted: Vec<FamilyFilter> = match (&diag.plan, diag.preset) {
        (Some(plan), _) => plan.families.clone(),
        (None, diagnose::DiagPreset::Full) => tested
            .iter()
            .map(|t| FamilyFilter::new(t.clone(), None))
            .collect(),
        (None, diagnose::DiagPreset::Quick) => model_plan::quick_families(),
        (None, diagnose::DiagPreset::Model) => {
            diag_root_fail("--diagnose-preset model needs a --diagnose-plan".to_string())
        }
    };
    let families = diagnose::tested_families(wanted, &tested).unwrap_or_else(|e| diag_root_fail(e));

    let plan = diagnose::DiagnosePlan {
        preset: diag.preset,
        engine: tauri_plugin_llamacpp::diagnostics::EngineBuild::pinned(),
        devices,
        families,
        model: diag.plan.as_ref().map(|p| p.model.clone()),
        support_probe: true,
        workers: diag.workers(),
        backend_modules,
        family_timeout: diag.family_timeout,
        deadline: diag.deadline,
    };

    diagnose::run(&plan, &spec, shutdown_requested(), &mut |event| {
        match serde_json::to_string(&event) {
            Ok(line) => print_event(&line),
            Err(e) => eprintln!("jan-llama-worker: could not encode an event: {e}"),
        }
    })
    .await;
}

/// How long in-flight requests get to finish once shutdown is requested.
const DRAIN_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

/// Resolves when the supervisor asks us to stop.
///
/// The signal is **stdin reaching EOF**, which happens when the supervisor drops
/// its end of the pipe. Chosen over a signal because Windows has no deliverable
/// SIGTERM equivalent (`GenerateConsoleCtrlEvent(CTRL_C_EVENT)` requires process
/// group 0, which would take Jan down with it), and over an HTTP route because
/// process lifetime is not the transport's business -- and because only our
/// parent holds the write end, so it needs no authentication.
///
/// SIGTERM is still honoured on Unix so `kill` on a stuck worker behaves.
async fn shutdown_requested() {
    #[cfg(unix)]
    {
        use tokio::signal::unix::{signal, SignalKind};
        let mut term = match signal(SignalKind::terminate()) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("jan-llama-worker: could not watch SIGTERM: {e}");
                stdin_eof().await;
                return;
            }
        };
        tokio::select! {
            _ = stdin_eof() => {}
            _ = term.recv() => {}
        }
    }
    #[cfg(not(unix))]
    stdin_eof().await;
}

async fn stdin_eof() {
    use tokio::io::AsyncReadExt;
    let mut stdin = tokio::io::stdin();
    let mut buf = [0u8; 64];
    loop {
        match stdin.read(&mut buf).await {
            // EOF: the supervisor closed the pipe.
            Ok(0) => return,
            // Nothing is expected to be written; ignore it rather than exiting,
            // so a stray byte cannot shut the engine down.
            Ok(_) => continue,
            Err(_) => return,
        }
    }
}