Skip to main content

qualia_core_db/inference/
application_profile.rs

1//! Application profiles — **how** inference is used, not only **which** GPU path.
2//!
3//! Timothy (2026-07-10): on a local device, work need not be live. High-stakes
4//! multi-system health eval / differential analysis can run overnight and deliver
5//! a verified HTML (or email body). Different applications want different modes:
6//!
7//! | Profile | Latency | Mid-decode | Post-turn | Decode budget | Timeout |
8//! |---------|---------|------------|-----------|---------------|---------|
9//! | **Interactive** | low | optional | light | 256 | 30s |
10//! | **LiveFast** | lowest | off (FastVerify) | graph heal | 256 | 30s |
11//! | **BatchOvernight** | irrelevant | off | full HTML+CML | 2048 | 8h |
12//!
13//! No Ollama API: all profiles stay in-process Qualia (P64 + resident GEMV + graph).
14
15use std::sync::atomic::{AtomicU8, Ordering};
16
17use crate::inference_modes::{set_inference_mode, InferenceMode};
18#[cfg(not(target_arch = "wasm32"))]
19use crate::llm_bench::{set_decode_budget_override, set_inference_timeout_override_ms};
20
21#[cfg(target_arch = "wasm32")]
22fn set_decode_budget_override(_n: u32) {}
23#[cfg(target_arch = "wasm32")]
24fn set_inference_timeout_override_ms(_ms: u64) {}
25
26#[repr(u8)]
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
28pub enum ApplicationProfile {
29    /// Chat / UI — path selector + portable defaults.
30    Interactive = 0,
31    /// Streaming-feel: FastVerify (generate then post-heal).
32    LiveFast = 1,
33    /// Overnight / high-stakes batch: long budget, HTML verification surface.
34    BatchOvernight = 2,
35}
36
37impl ApplicationProfile {
38    pub const ALL: [ApplicationProfile; 3] = [
39        ApplicationProfile::Interactive,
40        ApplicationProfile::LiveFast,
41        ApplicationProfile::BatchOvernight,
42    ];
43
44    /// Stable string form of the profile.
45    pub fn as_str(self) -> &'static str {
46        match self {
47            Self::Interactive => "interactive",
48            Self::LiveFast => "live-fast",
49            Self::BatchOvernight => "batch",
50        }
51    }
52
53    /// Parses a profile name; `None` when unknown.
54    pub fn parse(s: &str) -> Option<Self> {
55        match s.trim().to_ascii_lowercase().as_str() {
56            "interactive" | "chat" | "ui" | "0" => Some(Self::Interactive),
57            "live-fast" | "live_fast" | "live" | "fast" | "1" => Some(Self::LiveFast),
58            "batch" | "batch-overnight" | "overnight" | "offline" | "async" | "email" | "2" => {
59                Some(Self::BatchOvernight)
60            }
61            _ => None,
62        }
63    }
64
65    /// Human-readable profile description.
66    pub fn description(self) -> &'static str {
67        match self {
68            Self::Interactive => {
69                "interactive chat: path-selected GPU backend, 256-tok budget, 30s wall-clock"
70            }
71            Self::LiveFast => {
72                "live-fast: FastVerify (uninterrupted decode → post graph/CML heal), still local"
73            }
74            Self::BatchOvernight => {
75                "batch/overnight: up to 2048 tokens, 8h wall-clock, HTML+CML verify — multi-system eval, email-ready"
76            }
77        }
78    }
79}
80
81static PROFILE: AtomicU8 = AtomicU8::new(ApplicationProfile::Interactive as u8);
82
83/// Currently active global profile.
84pub fn active_application_profile() -> ApplicationProfile {
85    if let Ok(s) = std::env::var("QUALIA_APP_PROFILE") {
86        if let Some(p) = ApplicationProfile::parse(&s) {
87            return p;
88        }
89    }
90    match PROFILE.load(Ordering::Relaxed) {
91        1 => ApplicationProfile::LiveFast,
92        2 => ApplicationProfile::BatchOvernight,
93        _ => ApplicationProfile::Interactive,
94    }
95}
96
97/// Apply profile: inference mode, budgets, timeouts, HTML return for batch.
98pub fn set_application_profile(profile: ApplicationProfile) {
99    PROFILE.store(profile as u8, Ordering::Relaxed);
100    apply_application_profile(profile);
101    log::info!("APP_PROFILE|{}|{}", profile.as_str(), profile.description());
102}
103
104/// Activates `profile` globally.
105pub fn apply_application_profile(profile: ApplicationProfile) {
106    match profile {
107        ApplicationProfile::Interactive => {
108            set_decode_budget_override(0); // production 256
109            set_inference_timeout_override_ms(0); // 30s default
110            if std::env::var("QUALIA_INFERENCE_MODE").is_err() {
111                set_inference_mode(InferenceMode::Portable);
112            }
113        }
114        ApplicationProfile::LiveFast => {
115            set_decode_budget_override(0);
116            set_inference_timeout_override_ms(0);
117            if std::env::var("QUALIA_INFERENCE_MODE").is_err() {
118                set_inference_mode(InferenceMode::FastVerify);
119            }
120        }
121        ApplicationProfile::BatchOvernight => {
122            // Long-form reasoning for differential / multi-system jobs.
123            set_decode_budget_override(2048);
124            set_inference_timeout_override_ms(8 * 60 * 60 * 1000); // 8 hours
125                                                                   // Always post-verify + HTML surface for email / archival.
126            std::env::set_var("QUALIA_RETURN_VERIFY_HTML", "1");
127            if std::env::var("QUALIA_INFERENCE_MODE").is_err() {
128                set_inference_mode(InferenceMode::FastVerify);
129            }
130            // Ensure fact graph is warm.
131            let n = crate::quant_graph_grounding::seed_facts_from_bundled();
132            log::info!("APP_PROFILE|batch|facts_seeded|{n}|budget=2048|timeout=8h|html=1");
133        }
134    }
135}
136
137/// Bootstrap from env `QUALIA_APP_PROFILE` (call early with path selector).
138pub fn bootstrap_application_profile() -> ApplicationProfile {
139    let p = active_application_profile();
140    apply_application_profile(p);
141    p
142}
143
144#[cfg(test)]
145mod tests {
146    use super::*;
147
148    #[test]
149    fn parse_profiles() {
150        assert_eq!(
151            ApplicationProfile::parse("overnight"),
152            Some(ApplicationProfile::BatchOvernight)
153        );
154        assert_eq!(
155            ApplicationProfile::parse("live-fast"),
156            Some(ApplicationProfile::LiveFast)
157        );
158    }
159}