Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

37.7 KB
Raw Blame
//! Which profile a kwant turn runs on, the driver for it, and the chat notices when a turn
//! moves to another profile or every profile is out.

use super::AppState;
use crate::db::models::LlmProfile;
use crate::db::spaces::SpaceId;
use crate::drivers::engines::{EngineAccount, EngineId, EngineReport, EngineStatus};
use crate::drivers::{find_cli_binary, ApiDriver, CliDriver, Driver};
use std::collections::HashSet;
use std::sync::atomic::{AtomicBool, Ordering};
use crate::profiles::{
    failover_order, global_order, preference_order, Availability, Engine, FailClass, GlobalPlan,
    OrderRules, ProfileStatus, ProviderFailure, SignInBar,
};

/// The space's profiles and what decides whether each can take a turn now, from the db and the
/// cached sign-in checks (never waits on a check). `None` when the db can't be read.
async fn accounts(state: &AppState, space: &SpaceId) -> Option<(Vec<LlmProfile>, Availability)> {
    let db = &state.db;
    let (Ok(profiles), Ok(states), Ok(with_key)) = (
        db.list_profiles(space).await,
        db.list_profile_states().await,
        db.profile_ids_with_key().await,
    ) else {
        return None;
    };
    let avail = Availability {
        states,
        with_key,
        signed_out: state.profiles.signed_out(),
        signed_in: state.profiles.signed_in(),
    };
    Some((profiles, avail))
}

/// Profiles of the kwant's space to try for a turn on `engine`, in order. Empty when the model
/// has no profiles (e.g. Ollama) or the engine has none enabled: the turn then runs on the
/// host, unless [`host_off`] says This machine is off in that space.
pub async fn plan(state: &AppState, engine: Option<Engine>, kwant_id: &str) -> Vec<LlmProfile> {
    let Some(engine) = engine else {
        return Vec::new();
    };
    let Ok(Some(space)) = state.db.space_of_kwant(kwant_id).await else {
        return Vec::new();
    };
    let (Some((profiles, avail)), Ok(binding)) =
        (accounts(state, &space).await, state.db.get_kwant_binding(kwant_id).await)
    else {
        return Vec::new();
    };
    let (bound, failover) = binding.unwrap_or((None, true));
    failover_order(
        &profiles,
        engine,
        bound.as_deref(),
        failover,
        &avail,
        chrono::Utc::now(),
    )
    .into_iter()
    .filter(|p| profile_engine_on(state, p))
    .collect()
}

/// What a turn of a bot on `engine` may try across all the space's accounts, in order (see
/// [`crate::profiles::global_order`]). A bot on a model without accounts (Ollama) just runs.
pub async fn plan_global(state: &AppState, engine: Option<Engine>, kwant_id: &str) -> GlobalPlan {
    let Some(engine) = engine else {
        return GlobalPlan { attempts: vec![None], ..Default::default() };
    };
    let Ok(Some(space)) = state.db.space_of_kwant(kwant_id).await else {
        return GlobalPlan { attempts: vec![None], ..Default::default() };
    };
    let host_off = host_off(state, &space).await.contains(&engine);
    let (Some((profiles, mut avail)), Ok(binding)) =
        (accounts(state, &space).await, state.db.get_kwant_binding(kwant_id).await)
    else {
        return GlobalPlan { attempts: if host_off { Vec::new() } else { vec![None] }, ..Default::default() };
    };
    let (bound, failover) = binding.unwrap_or((None, true));
    let reports = state.engines.snapshot();
    // Another engine's own login (This machine) only counts when that CLI is ready here
    let host_ready = |family: Engine| {
        let (cli, _) = EngineId::of_family(family);
        // An engine out of usage until a known time is limited, not unready: its account stays
        // in the plan and its own limited-until time decides when it is tried again
        state.engines.quota_until(cli).is_some()
            || reports.iter().any(|r| r.id == cli && r.enabled && r.status == EngineStatus::Ready)
    };
    // The host's own login is judged by its engine check (ready here), not by an account probe
    // that may predate it: a "This machine" account never counts as signed out then
    for p in profiles.iter().filter(|p| p.kind == "system") {
        if Engine::parse(&p.engine).is_some_and(host_ready) {
            avail.signed_out.remove(&p.id);
        }
    }
    // An engine the person switched off, or that isn't installed, is a setup matter, not an
    // outage: a bot on it is told so rather than answered on another engine
    let own_engine_off = {
        let (cli, _) = EngineId::of_family(engine);
        reports.iter().any(|r| r.id == cli && (!r.enabled || r.status == EngineStatus::NotInstalled))
    };
    let eligible = |p: &LlmProfile| {
        profile_engine_on(state, p)
            && (!own_engine_off || Engine::parse(&p.engine) == Some(engine))
            && (p.kind != "system" || Engine::parse(&p.engine).is_some_and(|f| f == engine || host_ready(f)))
    };
    global_order(
        &profiles,
        engine,
        bound.as_deref(),
        failover,
        &avail,
        chrono::Utc::now(),
        &OrderRules { host_head: !host_off, eligible: &eligible },
    )
}

/// Engines whose every enabled account of `space` is known to be out right now (out of usage,
/// signed out, no key): Laya never routes a message to one of them. An engine with no account
/// rows is not listed; the host's own login is judged by its engine check instead.
pub async fn unusable_engines(state: &AppState, space: &SpaceId) -> HashSet<Engine> {
    let Some((profiles, avail)) = accounts(state, space).await else {
        return HashSet::new();
    };
    let now = chrono::Utc::now();
    [Engine::Claude, Engine::Codex, Engine::Grok, Engine::Custom]
        .into_iter()
        .filter(|f| {
            let mine: Vec<&LlmProfile> = profiles
                .iter()
                .filter(|p| p.enabled && Engine::parse(&p.engine) == Some(*f) && profile_engine_on(state, p))
                .collect();
            !mine.is_empty() && mine.iter().all(|p| !avail.usable(p, now))
        })
        .collect()
}

/// Engines whose This machine profile is off in `space`. A turn on one of them never falls back
/// to the host's own login or Settings key there; it needs one of the space's own accounts.
pub async fn host_off(state: &AppState, space: &SpaceId) -> HashSet<Engine> {
    let Ok(profiles) = state.db.list_profiles(space).await else {
        return HashSet::new();
    };
    profiles
        .iter()
        .filter(|p| p.kind == "system" && !p.enabled)
        .filter_map(|p| Engine::parse(&p.engine))
        .collect()
}

/// The engine a profile's turns run on: its family's API for a key, else its CLI. The system
/// profile runs on whichever host engine routing picks, so it has none of its own.
pub fn profile_engine(p: &LlmProfile) -> Option<EngineId> {
    let family = Engine::parse(&p.engine)?;
    let (cli, api) = EngineId::of_family(family);
    match p.kind.as_str() {
        "system" => None,
        "api_key" => Some(api),
        _ => Some(cli),
    }
}

/// The account a turn on `engine` runs on, for its "Running on" notice: the profile's name,
/// else "This machine" for a CLI's own login. API keys from Settings and Ollama have none.
pub fn account_name(profile: Option<&LlmProfile>, engine: EngineId) -> Option<String> {
    match profile {
        Some(p) => Some(p.name.clone()),
        None => (engine.kind() == crate::drivers::engines::EngineKind::Cli).then(|| "This machine".into()),
    }
}

/// An engine turned off in Settings is off for every account on it too.
fn profile_engine_on(state: &AppState, p: &LlmProfile) -> bool {
    profile_engine(p).is_none_or(|e| state.engines.is_enabled(e))
}

/// The driver for one attempt, and the engine it runs on when it's the host's own (no profile
/// or the system profile; see [`crate::drivers::resolve_driver_for_model`]). `Err` when that engine can't run.
pub async fn driver_for(
    state: &AppState,
    profile: Option<&LlmProfile>,
    target_model: &str,
    configured_key: Option<String>,
    endpoint: Option<String>,
) -> Result<(Box<dyn Driver>, Option<EngineId>), String> {
    let Some(p) = profile.filter(|p| p.kind != "system") else {
        // This computer's Claude sign-in runs isolated too; its API key never does
        let isolation = isolation_for(state, Engine::Claude, None).await;
        return crate::drivers::resolve_driver_isolated(target_model, configured_key, endpoint, &state.engines, isolation)
            .map(|(d, e)| (d, Some(e)));
    };
    let engine = Engine::parse(&p.engine).unwrap_or(Engine::Claude);
    if p.kind == "custom_api" {
        let (endpoint, key) = custom_endpoint(state, p).await?;
        let protocol = crate::drivers::custom::Protocol::parse(&endpoint.protocol)
            .ok_or_else(|| format!("Custom API account \"{}\" has an unknown protocol", p.name))?;
        let chat = crate::drivers::custom::chat_url(&endpoint.base_url, protocol);
        return Ok((Box::new(ApiDriver::for_custom(key, chat, endpoint.base_url)), None));
    }
    if p.kind == "api_key" {
        let key = state
            .db
            .get_profile_key(&p.id)
            .await
            .ok()
            .flatten()
            .map(|k| k.0);
        return Ok((Box::new(ApiDriver::for_profile(key)), None));
    }
    let home = state.profiles.home_of(p).unwrap_or_default();
    let isolation = isolation_for(state, engine, Some(home.clone())).await;
    Ok((
        Box::new(
            CliDriver::new(
                find_cli_binary(engine.as_str()),
                Some(engine.as_str().into()),
            )
            .with_profile_home(engine, home)
            .with_account(p.name.clone())
            .with_isolation(isolation),
        ),
        None,
    ))
}

/// Set when a Claude sign-in turn runs in the bot's agent container: "Run Claude inside the VM"
/// is on, the computer is in vm mode and this OS has isolation (D1: outside vm mode the switch
/// does nothing). API-key and Custom accounts never get here.
pub async fn isolation_for(
    state: &AppState,
    engine: Engine,
    profile_home: Option<std::path::PathBuf>,
) -> Option<crate::drivers::cli::Isolation> {
    if engine != Engine::Claude || crate::sandbox::agent_vm::supported_os().is_err() {
        return None;
    }
    if !super::computer::isolate_claude_setting(state).await
        || super::computer::current_mode(state).await != crate::sandbox::computer::ComputerMode::Vm
    {
        return None;
    }
    Some(crate::drivers::cli::Isolation {
        vm: state.agent_vm.clone(),
        profile_home,
        kwnt_origin: super::mcp_runtime::local_base_url(state),
    })
}

/// A Custom API account's server and key. `Err` when its server was never saved.
pub async fn custom_endpoint(
    state: &AppState,
    p: &LlmProfile,
) -> Result<(crate::db::custom_api::ProfileEndpoint, Option<String>), String> {
    let endpoint = state
        .db
        .get_profile_endpoint(&p.id)
        .await
        .ok()
        .flatten()
        .ok_or_else(|| format!("Custom API account \"{}\" has no server set", p.name))?;
    let key = state.db.get_profile_key(&p.id).await.ok().flatten().map(|k| k.0);
    Ok((endpoint, key))
}

/// Checks a Custom API account against its server (GET {base}/models) and caches the result.
/// Reuses a fresh check unless `force`.
pub async fn refresh_custom(
    state: &AppState,
    p: &LlmProfile,
    force: bool,
) -> Option<crate::drivers::custom::CustomCheck> {
    use crate::drivers::custom::{check, CheckStatus, CustomCheck, Protocol};
    if p.kind != "custom_api" {
        return None;
    }
    if !force {
        if let Some(c) = state.profiles.custom_check(&p.id) {
            return Some(c);
        }
    }
    let result = match custom_endpoint(state, p).await {
        Ok((endpoint, key)) => match Protocol::parse(&endpoint.protocol) {
            Some(proto) => check(proto, &endpoint.base_url, key.as_deref(), endpoint.model.as_deref()).await,
            None => CustomCheck::new(CheckStatus::Error, "Unknown protocol", Vec::new()),
        },
        Err(e) => CustomCheck::new(CheckStatus::Error, e, Vec::new()),
    };
    state.profiles.set_custom(&p.id, result.clone());
    Some(result)
}

/// The model a turn on a Custom API account runs: the bot's pinned model as is (the server
/// knows its own ids best), else the account's own model, else the one its server lists for the
/// tier. `None` when none is known; the driver then says so.
pub async fn custom_model(
    state: &AppState,
    p: &LlmProfile,
    pinned: Option<&str>,
    tier: crate::drivers::engines::Tier,
) -> Option<crate::drivers::engines::ModelChoice> {
    use crate::drivers::engines::{choose_model, ModelChoice};
    let named = |id: &str| ModelChoice {
        id: id.to_string(),
        label: id.to_string(),
        tier: crate::drivers::engines::tier_from_name(id),
    };
    if let Some(pin) = pinned.map(str::trim).filter(|m| !m.is_empty()) {
        return Some(named(pin));
    }
    let stored = state.db.get_profile_endpoint(&p.id).await.ok().flatten().and_then(|e| e.model);
    if let Some(m) = stored {
        return Some(named(&m));
    }
    let models = refresh_custom(state, p, false).await.map(|c| c.models).unwrap_or_default();
    choose_model(&models, None, tier)
}

/// `family`'s own accounts (not the system profile) that pass `bar` and whose engine is on,
/// in the order failover prefers them. The one rule for "this account can take a turn".
fn own_accounts<'a>(
    state: &AppState,
    profiles: &'a [LlmProfile],
    family: Engine,
    avail: &Availability,
    bar: SignInBar,
) -> Vec<&'a LlmProfile> {
    let now = chrono::Utc::now();
    preference_order(profiles, family, None)
        .into_iter()
        .filter(|p| p.kind != "system" && avail.admits(p, now, bar))
        .filter(|p| profile_engine_on(state, p))
        .collect()
}

/// Engines with an enabled account of their own (not the system profile) that failover
/// would use: these engines can run even when the host's own CLI or key can't. Routing keeps
/// the lenient bar (an unchecked profile counts) so turns right after startup, before any
/// sign-in check, still reach a profile that is in fact signed in.
pub async fn profile_engines(state: &AppState, space: &SpaceId) -> HashSet<Engine> {
    let Some((profiles, avail)) = accounts(state, space).await else {
        return HashSet::new();
    };
    [Engine::Claude, Engine::Codex, Engine::Grok, Engine::Custom]
        .into_iter()
        .filter(|f| !own_accounts(state, &profiles, *f, &avail, SignInBar::NotSignedOut).is_empty())
        .collect()
}

/// Whether `report`'s engine is ready and the account a kwant on it uses: the first of the
/// engine's accounts in failover order (the default first) that can take a turn, the system
/// profile ("This machine") counting when the engine's own login is ready. Off ⇒ not ready, and
/// This machine turned off means the host's own login doesn't count either.
pub fn readiness(
    report: &EngineReport,
    profiles: &[LlmProfile],
    avail: &Availability,
    now: chrono::DateTime<chrono::Utc>,
) -> (bool, Option<EngineAccount>) {
    if !report.enabled {
        return (false, None);
    }
    let host = report.status == EngineStatus::Ready;
    let Some(family) = report.id.family() else {
        return (host, None);
    };
    let system = |p: &&LlmProfile| p.kind == "system";
    let host_off = profiles.iter().any(|p| system(&p) && p.engine == family.as_str() && !p.enabled);
    // Custom API runs only on the space's own accounts, never on anything of the host's
    let host = host && !host_off && family != Engine::Custom;
    let picked = preference_order(profiles, family, None)
        .into_iter()
        .filter(|p| system(p) || profile_engine(p) == Some(report.id))
        .find(|p| {
            if system(p) {
                host
            } else {
                avail.admits(p, now, SignInBar::SignedIn)
            }
        });
    let uses = picked.map(|p| EngineAccount { profile_id: p.id.clone(), label: p.name.clone() });
    (host || uses.is_some(), uses)
}

/// Fills in `ready` and `uses` on every report, for `space`'s accounts.
pub async fn with_accounts(state: &AppState, space: &SpaceId, mut reports: Vec<EngineReport>) -> Vec<EngineReport> {
    let accounts = accounts(state, space).await;
    let now = chrono::Utc::now();
    for r in &mut reports {
        let (ready, uses) = match &accounts {
            Some((profiles, avail)) => readiness(r, profiles, avail, now),
            None => (r.enabled && r.status == EngineStatus::Ready, None),
        };
        // An engine switched off is off for its accounts too (see profile_engine_on)
        r.ready = ready;
        r.uses = uses;
    }
    reports
}

/// How many of `space`'s enabled accounts could take a turn now: This machine when its
/// engine's own login is ready and the engine is on, any other account when it is signed in
/// and its engine is on, plus each engine that runs with no account at all (Ollama) when it is
/// on and ready, so the space never says "no account yet" while the sidebar says Ollama is ready.
pub async fn ready_accounts(state: &AppState, space: &SpaceId) -> usize {
    let reports = state.engines.snapshot();
    let accountless = reports.iter().filter(|r| runs_without_account(r)).count();
    let Some((profiles, avail)) = accounts(state, space).await else {
        return accountless;
    };
    let now = chrono::Utc::now();
    accountless + profiles.iter().filter(|p| p.enabled && is_ready(state, p, &avail, &reports, now)).count()
}

/// An engine with no accounts (no model family: Ollama) that is on and passed its check.
fn runs_without_account(r: &EngineReport) -> bool {
    r.id.family().is_none() && r.enabled && r.status == EngineStatus::Ready
}

/// Whether an enabled account could take a turn now (see [`ready_accounts`]).
fn is_ready(state: &AppState, p: &LlmProfile, avail: &Availability, reports: &[EngineReport], now: chrono::DateTime<chrono::Utc>) -> bool {
    match (p.kind.as_str(), Engine::parse(&p.engine)) {
        // This machine runs on the engine's CLI login or on its API key from Settings, as the
        // sidebar's `readiness` counts either
        ("system", Some(family)) => {
            let (cli, api) = EngineId::of_family(family);
            reports.iter().any(|r| (r.id == cli || r.id == api) && r.enabled && r.status == EngineStatus::Ready)
        }
        ("system", None) => false,
        _ => avail.admits(p, now, SignInBar::SignedIn) && profile_engine_on(state, p),
    }
}

/// The accounts a one-off model call outside a chat (drafting a team from the person's words)
/// may run on, in the Accounts list order (`priority`, then oldest), as failover tries them.
#[derive(Debug, Default)]
pub struct CallAccounts {
    /// Ready now and not out of usage.
    pub ready: Vec<LlmProfile>,
    /// When none is ready only because of usage limits: the account that comes back first, and when.
    pub limited: Option<(String, chrono::DateTime<chrono::Utc>)>,
}

/// [`CallAccounts`] for `space`.
pub async fn call_accounts(state: &AppState, space: &SpaceId) -> CallAccounts {
    let Some((mut profiles, avail)) = accounts(state, space).await else {
        return CallAccounts::default();
    };
    profiles.sort_by(|a, b| (a.priority, a.created_at, &a.id).cmp(&(b.priority, b.created_at, &b.id)));
    let reports = state.engines.snapshot();
    let now = chrono::Utc::now();
    let limited_until = |p: &LlmProfile| avail.states.get(&p.id).and_then(|s| s.limited_until).filter(|t| *t > now);
    let mut out = CallAccounts::default();
    for p in profiles.iter().filter(|p| p.enabled) {
        // An account out of usage is ready apart from its limit when it would be without it
        if let Some(until) = limited_until(p) {
            let signed_in = match p.kind.as_str() {
                "system" => is_ready(state, p, &avail, &reports, now),
                "api_key" => avail.with_key.contains(&p.id),
                _ => avail.signed_in.contains(&p.id),
            };
            if signed_in && profile_engine_on(state, p) && out.limited.as_ref().is_none_or(|(_, t)| until < *t) {
                out.limited = Some((p.name.clone(), until));
            }
            continue;
        }
        if is_ready(state, p, &avail, &reports, now) {
            out.ready.push(p.clone());
        }
    }
    if !out.ready.is_empty() {
        out.limited = None;
    }
    out
}

/// The account a bot's next turn would run on, for its card: the first account failover would
/// try, else This machine when the engine's own login may be used there. `blocked` when This
/// machine is off in the space and no account of its own can take the turn. Bots on "auto" or
/// on a model without accounts (Ollama) have none.
pub async fn runs_on(state: &AppState, kwant: &crate::db::models::Kwant) -> serde_json::Value {
    let model = kwant.preferred_model.as_str();
    let engine = Engine::parse(model).or_else(|| Engine::from_model(model));
    let Some(engine) = engine else {
        return serde_json::json!({ "auto": model == "auto", "engine": null, "account": null, "profile_id": null, "blocked": false });
    };
    if let Some(p) = plan(state, Some(engine), &kwant.id).await.into_iter().next() {
        return serde_json::json!({ "auto": false, "engine": engine.as_str(), "account": p.name, "profile_id": p.id, "blocked": false });
    }
    let space = SpaceId::new(&kwant.space_id);
    let blocked = host_off(state, &space).await.contains(&engine);
    serde_json::json!({
        "auto": false,
        "engine": engine.as_str(),
        "account": if blocked { None } else { Some("This machine") },
        "profile_id": null,
        "blocked": blocked,
    })
}

static REFRESHING: AtomicBool = AtomicBool::new(false);

/// Runs the stale profile sign-in checks so `ready` is current; one run at a time, others
/// return at once.
pub async fn refresh_sign_ins(state: &AppState) {
    if REFRESHING.swap(true, Ordering::AcqRel) {
        return;
    }
    for space in state.db.list_spaces().await.unwrap_or_default() {
        if let Ok(profiles) = state.db.list_profiles(&SpaceId::new(&space.id)).await {
            let keyed = super::profile_routes::keyed_ids(state, &profiles).await;
            state.profiles.statuses(&profiles, &keyed).await;
            for p in profiles.iter().filter(|p| p.kind == "custom_api" && p.enabled) {
                refresh_custom(state, p, false).await;
            }
        }
    }
    REFRESHING.store(false, Ordering::Release);
}

/// Remembers a refusal so failover skips the profile: until its quota resets, or until it is
/// signed in again.
pub async fn record_failure(state: &AppState, profile: &LlmProfile, failure: &ProviderFailure) {
    let until = (failure.class == FailClass::Quota)
        .then_some(failure.retry_at)
        .flatten();
    let _ = state
        .db
        .record_profile_failure(&profile.id, failure.class.as_str(), until)
        .await;
    if failure.class == FailClass::Auth && profile.kind != "api_key" {
        state.profiles.set_status(
            &profile.id,
            ProfileStatus {
                signed_in: Some(false),
                account_label: None,
            },
        );
    }
}

fn reason(failure: &ProviderFailure) -> String {
    match (failure.class, failure.retry_at) {
        (FailClass::Quota, Some(at)) => format!("is out of usage until {} UTC", at.format("%H:%M")),
        (FailClass::Quota, None) => "is out of usage".into(),
        _ => "is signed out or its key was rejected".into(),
    }
}

/// Shown while the turn moves on: "Work ran out of usage until 17:00 UTC; switching to Personal."
pub fn switch_notice(from: &LlmProfile, to: &LlmProfile, failure: &ProviderFailure) -> String {
    format!(
        "🔁 {} account \"{}\" {}; switching to \"{}\".",
        from.engine,
        from.name,
        reason(failure),
        to.name
    )
}

/// The reply when every account tried refused the turn.
pub fn give_up_notice(
    kwant_name: &str,
    last: &LlmProfile,
    failure: &ProviderFailure,
    tried: usize,
) -> String {
    let fix = match failure.class {
        FailClass::Quota => "Add another account under Settings › This space › Accounts, or wait for the limit to reset.",
        _ => "Sign in again (or update the key) under Settings › This space › Accounts.",
    };
    let which = if tried > 1 {
        format!(
            "All {tried} {} accounts tried are unavailable; the last, \"{}\",",
            last.engine, last.name
        )
    } else {
        format!("The {} account \"{}\"", last.engine, last.name)
    };
    format!("⚠️ [{kwant_name}] {which} {}. {fix}", reason(failure))
}

/// How a person names an account's engine: "Claude", "Codex", "Grok", or a Custom API
/// account by its own name ("Maestro").
pub fn engine_label(p: &LlmProfile) -> String {
    match Engine::parse(&p.engine) {
        Some(Engine::Claude) => "Claude".into(),
        Some(Engine::Codex) => "Codex".into(),
        Some(Engine::Grok) => "Grok".into(),
        _ => p.name.clone(),
    }
}

/// `at` as a clock time in the person's zone that names it: "09:00 CET". The zone is the saved one,
/// else this computer's, else UTC (`zombie::settings::local_tz`).
pub async fn clock_text(state: &AppState, at: chrono::DateTime<chrono::Utc>) -> String {
    clock_text_in(&crate::zombie::settings::local_tz(&state.db).await, at)
}

/// [`clock_text`] in `tz`. A zone that cannot be read, or has no abbreviation, gives the time
/// converted to UTC and labelled "UTC", never a local time under the wrong label.
pub fn clock_text_in(tz: &str, at: chrono::DateTime<chrono::Utc>) -> String {
    crate::routines::clock_with_zone(tz, at).unwrap_or_else(|_| format!("{} UTC", at.format("%H:%M")))
}

/// `at` as RFC 3339 with an explicit offset ("2026-11-03T08:00:00+00:00"), for `meta.failover`.
pub fn rfc3339(at: chrono::DateTime<chrono::Utc>) -> String {
    at.to_rfc3339_opts(chrono::SecondsFormat::Secs, false)
}

/// The quiet line when a turn runs on another engine because its own is out:
/// "Claude is out until 03:00 UTC · Architect is answering on Codex (gpt-6.1)".
pub fn cross_engine_notice(
    from: &LlmProfile,
    until_text: Option<&str>,
    kwant_name: &str,
    to: &LlmProfile,
    to_model: Option<&str>,
) -> String {
    let why = match until_text {
        Some(t) => format!("is out until {t}"),
        None => "is unavailable".to_string(),
    };
    let model = to_model.map(|m| format!(" ({m})")).unwrap_or_default();
    format!("🔁 {} {why} · {kwant_name} is answering on {}{model}", engine_label(from), engine_label(to))
}

/// The one line when no account of the space can answer: the turn waits for the earliest reset.
pub fn all_out_line(until_text: &str) -> String {
    format!("Every model is out until {until_text}; I'll answer then.")
}

/// Outages already announced in a chat: (chat, account that ran out) to when that outage ends.
static ANNOUNCED: std::sync::Mutex<Vec<(String, String, chrono::DateTime<chrono::Utc>)>> =
    std::sync::Mutex::new(Vec::new());

/// Whether the notice for `account` running out in `chat` hasn't been shown yet for this outage
/// (until `until`, else an hour). Marks it shown, so a chat gets one line per outage, however
/// many turns it takes. A later outage of the same account is announced again.
pub fn announce_once(chat: &str, account: &str, until: Option<chrono::DateTime<chrono::Utc>>) -> bool {
    let now = chrono::Utc::now();
    let end = until.unwrap_or(now + chrono::Duration::hours(1));
    let Ok(mut seen) = ANNOUNCED.lock() else { return true };
    seen.retain(|(_, _, t)| *t > now);
    if seen.iter().any(|(c, a, _)| c == chat && a == account) {
        return false;
    }
    seen.push((chat.to_string(), account.to_string(), end));
    true
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::models::LlmProfileState;
    use chrono::{DateTime, Duration, TimeZone, Utc};
    use std::collections::HashMap;

    fn now() -> DateTime<Utc> {
        Utc.with_ymd_and_hms(2026, 10, 17, 12, 0, 0).unwrap()
    }

    fn profile(id: &str, name: &str, kind: &str, priority: i64, default: bool) -> LlmProfile {
        LlmProfile {
            id: id.into(),
            engine: "claude".into(),
            name: name.into(),
            kind: kind.into(),
            home_dir: None,
            priority,
            enabled: true,
            is_default: default,
            created_at: None,
            space_id: "spc_default".into(),
        }
    }

    #[test]
    fn give_up_points_to_where_accounts_and_keys_live() {
        // QA d: the old Profiles page is gone; keys and sign-ins are in Settings › This space › Accounts
        let p = profile("p1", "Work", "api", 0, true);
        let auth = ProviderFailure { class: FailClass::Auth, retry_at: None, detail: "401".into() };
        let quota = ProviderFailure { class: FailClass::Quota, retry_at: None, detail: String::new() };
        for t in [
            give_up_notice("Ann", &p, &auth, 1),
            give_up_notice("Ann", &p, &auth, 2),
            give_up_notice("Ann", &p, &quota, 1),
            switch_notice(&p, &profile("p2", "Personal", "subscription", 1, false), &auth),
        ] {
            assert!(!t.to_lowercase().contains("profile"), "{t}");
        }
        let wrong_key = give_up_notice("Ann", &p, &auth, 1);
        assert!(wrong_key.contains("Settings › This space › Accounts"), "{wrong_key}");
        assert!(give_up_notice("Ann", &p, &quota, 1).contains("Settings › This space › Accounts"));
    }

    #[test]
    fn out_until_text_names_its_zone() {
        let winter = Utc.with_ymd_and_hms(2026, 11, 3, 8, 0, 0).unwrap();
        let summer = Utc.with_ymd_and_hms(2026, 7, 3, 8, 0, 0).unwrap();
        // The person's zone, with its abbreviation at that instant
        assert_eq!(clock_text_in("Europe/Ljubljana", winter), "09:00 CET");
        assert_eq!(clock_text_in("Europe/Ljubljana", summer), "10:00 CEST");
        assert_eq!(clock_text_in("America/New_York", winter), "03:00 EST");
        assert_eq!(clock_text_in("Europe/Istanbul", winter), "11:00 +03");
        assert_eq!(clock_text_in("UTC", winter), "08:00 UTC");
        // Unreadable zones are converted to UTC, not labelled local
        for bad in ["Not/AZone", "", "../etc/passwd", "Europe/Ljubljana/../x"] {
            assert_eq!(clock_text_in(bad, winter), "08:00 UTC", "{bad:?}");
        }
        let line = all_out_line(&clock_text_in("Europe/Ljubljana", winter));
        assert_eq!(line, "Every model is out until 09:00 CET; I'll answer then.");
        let claude = profile("c", "This machine", "system", 0, false);
        let codex = profile("x", "Codex", "system", 1, false);
        let line = cross_engine_notice(&claude, Some(&clock_text_in("Europe/Ljubljana", winter)), "Ann", &codex, Some("gpt-6.1"));
        assert!(line.contains("is out until 09:00 CET · Ann is answering on"), "{line}");
        let line = cross_engine_notice(&claude, None, "Ann", &codex, None);
        assert!(line.contains("is unavailable") && !line.contains("until"), "{line}");
    }

    #[test]
    fn retry_at_is_rfc3339_with_an_offset() {
        let at = Utc.with_ymd_and_hms(2026, 11, 3, 8, 0, 5).unwrap();
        assert_eq!(rfc3339(at), "2026-11-03T08:00:05+00:00");
        assert_eq!(DateTime::parse_from_rfc3339(&rfc3339(at)).unwrap().with_timezone(&Utc), at);
    }

    fn report(id: EngineId, enabled: bool, status: EngineStatus) -> EngineReport {
        EngineReport {
            id,
            name: id.name().into(),
            kind: id.kind(),
            enabled,
            status,
            detail: None,
            version: None,
            models: Vec::new(),
            checked_at: None,
            ready: false,
            uses: None,
        }
    }

    fn signed_in(ids: &[&str]) -> Availability {
        Availability {
            signed_in: ids.iter().map(|s| s.to_string()).collect(),
            ..Default::default()
        }
    }

    fn uses(r: (bool, Option<EngineAccount>)) -> (bool, Option<String>) {
        (r.0, r.1.map(|a| a.label))
    }

    /// Alex's setup: This machine plus a Personal Claude account that is the default.
    fn alex() -> Vec<LlmProfile> {
        vec![
            profile("prf_system_claude", "This machine", "system", 0, false),
            profile("p_personal", "Personal", "cli_home", 100, true),
        ]
    }

    #[test]
    fn engine_off_is_not_ready_even_with_a_signed_in_account() {
        let r = report(EngineId::Claude, false, EngineStatus::Disabled);
        let avail = signed_in(&["p_personal"]);
        assert_eq!(readiness(&r, &alex(), &avail, now()), (false, None));
    }

    #[test]
    fn host_signed_out_but_personal_signed_in_is_ready_on_personal() {
        let r = report(EngineId::Claude, true, EngineStatus::SignedOut);
        let got = readiness(&r, &alex(), &signed_in(&["p_personal"]), now());
        assert!(got.0);
        assert_eq!(
            got.1,
            Some(EngineAccount { profile_id: "p_personal".into(), label: "Personal".into() })
        );
        // Host signed out and no account signed in: not ready
        let got = readiness(&r, &alex(), &Availability::default(), now());
        assert_eq!(got, (false, None));
    }

    #[test]
    fn unknown_sign_in_is_not_ready_but_routing_still_tries_it() {
        let r = report(EngineId::Claude, true, EngineStatus::SignedOut);
        let unknown = Availability::default();
        assert_eq!(readiness(&r, &alex(), &unknown, now()), (false, None));
        // Routing keeps the lenient bar: an unchecked profile is still tried by a turn
        let personal = &alex()[1];
        assert!(unknown.admits(personal, now(), SignInBar::NotSignedOut));
        assert!(!unknown.admits(personal, now(), SignInBar::SignedIn));
    }

    #[test]
    fn quota_limited_account_is_not_ready_until_it_resets() {
        let r = report(EngineId::Claude, true, EngineStatus::SignedOut);
        let limited = |until: DateTime<Utc>| Availability {
            states: HashMap::from([(
                "p_personal".to_string(),
                LlmProfileState {
                    profile_id: "p_personal".into(),
                    limited_until: Some(until),
                    last_error: Some("quota".into()),
                },
            )]),
            ..signed_in(&["p_personal"])
        };
        let avail = limited(now() + Duration::hours(1));
        assert_eq!(readiness(&r, &alex(), &avail, now()), (false, None));
        let avail = limited(now() - Duration::minutes(1));
        assert_eq!(uses(readiness(&r, &alex(), &avail, now())), (true, Some("Personal".into())));
    }

    #[test]
    fn uses_the_default_first_then_priority_order() {
        let r = report(EngineId::Claude, true, EngineStatus::Ready);
        let profiles = vec![
            profile("prf_system_claude", "This machine", "system", 0, false),
            profile("p_work", "Work", "cli_home", 10, false),
            profile("p_personal", "Personal", "cli_home", 20, true),
        ];
        // The default wins over a higher priority, signed-in account and the host's own login
        let all = signed_in(&["p_work", "p_personal"]);
        assert_eq!(uses(readiness(&r, &profiles, &all, now())), (true, Some("Personal".into())));
        // Default not signed in: next by priority, here This machine (host ready)
        let work = signed_in(&["p_work"]);
        assert_eq!(uses(readiness(&r, &profiles, &work, now())), (true, Some("This machine".into())));
        // Host signed out too: Work
        let r = report(EngineId::Claude, true, EngineStatus::SignedOut);
        assert_eq!(uses(readiness(&r, &profiles, &work, now())), (true, Some("Work".into())));
    }

    #[test]
    fn disabled_profiles_and_other_engines_accounts_do_not_count() {
        let r = report(EngineId::Claude, true, EngineStatus::SignedOut);
        let mut profiles = alex();
        profiles[1].enabled = false;
        assert_eq!(readiness(&r, &profiles, &signed_in(&["p_personal"]), now()), (false, None));
        // An API-key account runs on the Anthropic API, not the Claude CLI, and needs its key
        let profiles = vec![
            profile("prf_system_claude", "This machine", "system", 0, true),
            profile("p_key", "Team key", "api_key", 10, false),
        ];
        assert_eq!(readiness(&r, &profiles, &Availability::default(), now()), (false, None));
        let api = report(EngineId::AnthropicApi, true, EngineStatus::SignedOut);
        assert_eq!(readiness(&api, &profiles, &Availability::default(), now()), (false, None));
        let keyed = Availability {
            with_key: HashSet::from(["p_key".to_string()]),
            ..Default::default()
        };
        assert_eq!(uses(readiness(&api, &profiles, &keyed, now())), (true, Some("Team key".into())));
        assert_eq!(readiness(&r, &profiles, &keyed, now()), (false, None));
    }

    #[test]
    fn this_machine_off_is_never_a_fallback_even_when_the_host_is_ready() {
        let r = report(EngineId::Claude, true, EngineStatus::Ready);
        let mut profiles = alex();
        profiles[0].enabled = false;
        assert_eq!(readiness(&r, &profiles, &Availability::default(), now()), (false, None));
        // Another account of the space still makes it ready
        assert_eq!(
            uses(readiness(&r, &profiles, &signed_in(&["p_personal"]), now())),
            (true, Some("Personal".into()))
        );
        // Custom API is ready only through a checked account of the space, never the host
        let c = report(EngineId::CustomApi, true, EngineStatus::Ready);
        let mut maestro = profile("p_maestro", "Maestro", "custom_api", 10, true);
        maestro.engine = "custom".into();
        assert_eq!(readiness(&c, &[], &Availability::default(), now()), (false, None));
        assert_eq!(readiness(&c, std::slice::from_ref(&maestro), &Availability::default(), now()), (false, None));
        assert_eq!(
            uses(readiness(&c, std::slice::from_ref(&maestro), &signed_in(&["p_maestro"]), now())),
            (true, Some("Maestro".into()))
        );
        // Ollama has no accounts: ready from its own check alone
        let o = report(EngineId::Ollama, true, EngineStatus::Ready);
        assert_eq!(readiness(&o, &profiles, &Availability::default(), now()), (true, None));
    }
}