car-inference 0.23.0

Local model inference for CAR — Candle backend with Qwen3 models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! Proactive model concierge — turns "your hardware can run a great model for
//! X, but you have none installed" into a single, non-nagging suggestion.
//!
//! Sibling of [`crate::nudge`]: the upgrade nudge handles the *installed →
//! newer* case; the concierge handles the *acquisition gap* — a use-case lane
//! with no working model at all. Both are pure decision functions the daemon
//! drives on its periodic tick, and both reuse [`NudgeState`] for throttle +
//! dismissal bookkeeping and the [`UpdatePolicy`] `Off` switch, so the two
//! together stay quiet rather than each nagging on its own schedule. Keeping
//! the decision here makes it pure and unit-testable: the caller injects
//! `now_secs` and `throttle_secs`.
//!
//! Rules:
//! - `policy == Off` → do nothing.
//! - Never interrupt active inference — defer to a later, idle tick.
//! - A use-case lane is "served" when any model the recommender ranks for it
//!   is already installed; served lanes never produce a suggestion. This is
//!   what keeps the concierge distinct from the upgrade nudge — it only fires
//!   when the user has *nothing* for a lane, never to push a marginal upgrade.
//! - Only on-device, memory-fitting picks are suggested — an acquisition the
//!   user can act on immediately, with no cloud consent and no "needs more
//!   RAM" caveat.
//! - At most one suggestion round per `throttle_secs` (shared with the nudge),
//!   and a suggestion the user dismissed is never repeated.

use serde::{Deserialize, Serialize};

use crate::hardware::HardwareInfo;
use crate::intent::{Privacy, QualityTier, UseCase};
use crate::nudge::NudgeState;
use crate::recommend::{recommend, FitStatus, Recommendation};
use crate::schema::ModelSchema;
use crate::update_prefs::{UpdatePolicy, UpdatePreferences};

/// A single, plain-language acquisition suggestion for one unserved lane.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ConciergeSuggestion {
    /// The use-case lane that has no installed model.
    pub use_case: UseCase,
    /// Registry id of the model to acquire — the recommender's top on-device,
    /// memory-fitting pick for this lane. The caller pulls by this.
    pub model_id: String,
    /// Human-readable name shown to the user.
    pub display_name: String,
    /// Download size in MB.
    pub download_mb: u64,
    /// One-line, jargon-free message: "You have no model for … — install it?".
    pub message: String,
    /// Stable key the client echoes back to dismiss this suggestion. Encodes
    /// the lane and target model, so a *different* later pick can still fire
    /// even after this one is dismissed.
    pub dismiss_key: String,
}

/// Default throttle: at most one acquisition suggestion per week. Deliberately
/// rarer than the upgrade nudge's daily cadence — "you have nothing for X" is a
/// bigger, more deliberate prompt than "a newer version exists", and should not
/// nag. Independent of the nudge throttle (separate `NudgeState` field).
pub const DEFAULT_CONCIERGE_THROTTLE_SECS: u64 = 7 * 24 * 60 * 60;

/// The default lanes the concierge watches when the caller passes none.
///
/// Core interactive uses that almost every user benefits from — deliberately
/// NOT the specialist lanes (`Vision` / `Transcription` / `Search`), which
/// many users never touch; suggesting those unprompted would nag. A caller
/// that knows the user's actual interests should pass them explicitly.
pub const DEFAULT_WATCHED_USE_CASES: &[UseCase] = &[UseCase::Assistant, UseCase::Coding];

/// Stable string slug for a use-case lane. Used in the persisted dismiss key,
/// so it MUST be stable across releases — never `{:?}`/`Debug`, which is not a
/// stability contract (a variant rename would silently un-dismiss). Exhaustive
/// so a new lane is a compile error here, not a silently-changed key.
fn use_case_slug(use_case: UseCase) -> &'static str {
    match use_case {
        UseCase::Assistant => "assistant",
        UseCase::Coding => "coding",
        UseCase::Summarize => "summarize",
        UseCase::Vision => "vision",
        UseCase::Transcription => "transcription",
        UseCase::Search => "search",
    }
}

/// Stable dismiss key for a (lane, target) pair. The `concierge:` prefix keeps
/// the namespace disjoint from the upgrade nudge's bare `from=>to` keys, which
/// share the same `NudgeState.dismissed` Vec.
fn dismiss_key(use_case: UseCase, model_id: &str) -> String {
    format!("concierge:{}=>{model_id}", use_case_slug(use_case))
}

/// Decide which acquisition suggestions to surface, given the registry,
/// hardware, lanes to watch, prefs, and bookkeeping. Pure over its inputs.
///
/// Does **not** mutate `state` — the caller records `last_nudge_secs` when it
/// actually surfaces a suggestion (so a dropped one can re-fire), and appends
/// to `dismissed` when the user waves one away.
#[allow(clippy::too_many_arguments)]
pub fn decide_concierge(
    models: &[&ModelSchema],
    hw: &HardwareInfo,
    use_cases: &[UseCase],
    tier: QualityTier,
    prefs: &UpdatePreferences,
    state: &NudgeState,
    now_secs: u64,
    throttle_secs: u64,
    inference_active: bool,
) -> Vec<ConciergeSuggestion> {
    // Defer entirely while inference is running (memory/compute contention) or
    // when the user has turned proactive prompts off.
    if inference_active || matches!(prefs.policy, UpdatePolicy::Off) {
        return Vec::new();
    }

    // Throttle: at most one round per window, off the concierge's OWN
    // `last_concierge_secs` — independent of the upgrade nudge's
    // `last_nudge_secs`. The two have different cadences ("you have nothing"
    // should be rarer and more deliberate than "newer exists") and must not
    // starve each other: sharing one field let whichever ran first each tick
    // burn the window forever.
    let throttled = state.last_concierge_secs != 0
        && now_secs.saturating_sub(state.last_concierge_secs) < throttle_secs;
    if throttled {
        return Vec::new();
    }

    let mut out = Vec::new();
    for &use_case in use_cases {
        // On-device only — a suggestion the user can act on now.
        let set = recommend(models, hw, use_case, tier, Privacy::OnDevice);

        // Lane already served by an installed model → nothing to suggest. This
        // is the line that separates "you have nothing" (concierge) from
        // "something better exists" (upgrade nudge).
        if set.picks.iter().any(|p| p.already_installed) {
            continue;
        }

        // The acquisition: first on-device, memory-fitting, not-installed pick.
        let pick = match set
            .picks
            .iter()
            .find(|p| !p.already_installed && p.is_local && p.fit == FitStatus::Fits)
        {
            Some(p) => p,
            None => continue,
        };

        let key = dismiss_key(use_case, &pick.model_id);
        if state.dismissed.iter().any(|k| k == &key) {
            continue;
        }

        out.push(ConciergeSuggestion {
            use_case,
            model_id: pick.model_id.clone(),
            display_name: pick.display_name.clone(),
            download_mb: pick.download_mb,
            message: suggestion_message(use_case, pick),
            dismiss_key: key,
        });
    }
    out
}

/// One plain-language line. No model ids, quant, or repo jargon — exhaustive
/// over `UseCase` so a new lane is a compile error here, not a silent gap.
fn suggestion_message(use_case: UseCase, pick: &Recommendation) -> String {
    let purpose = match use_case {
        UseCase::Assistant => "chat & general help",
        UseCase::Coding => "coding",
        UseCase::Summarize => "summarizing",
        UseCase::Vision => "understanding images",
        UseCase::Transcription => "transcription",
        UseCase::Search => "semantic search",
    };
    let mb = pick.download_mb;
    let size = if mb >= 1024 {
        format!("{:.1} GB", mb as f64 / 1024.0)
    } else {
        format!("{mb} MB")
    };
    format!(
        "You have no model for {purpose}. {} fits your machine ({size}) — install it?",
        pick.display_name
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::hardware::GpuBackend;
    use crate::schema::{
        CostModel, ModelCapability, ModelSchema, ModelSource, PerformanceEnvelope, TrustTier,
    };

    // A local model schema parameterized by id, install state, download size,
    // and capabilities. `available == installed`. Mirrors recommend.rs's test
    // builder so the recommender ranks it the same way.
    fn model(id: &str, installed: bool, download_mb: u64, caps: &[ModelCapability]) -> ModelSchema {
        ModelSchema {
            id: id.into(),
            name: id.into(),
            provider: "qwen".into(),
            family: "qwen3".into(),
            version: String::new(),
            capabilities: caps.to_vec(),
            context_length: 32768,
            param_count: "4B".into(),
            quantization: Some("Q4_K_M".into()),
            performance: PerformanceEnvelope::default(),
            cost: CostModel {
                size_mb: Some(download_mb),
                ram_mb: Some(download_mb),
                ..Default::default()
            },
            source: ModelSource::Local {
                hf_repo: "x/y".into(),
                hf_filename: "m.gguf".into(),
                tokenizer_repo: "x/y".into(),
            },
            tags: vec![],
            supported_params: vec![],
            public_benchmarks: vec![],
            trust_tier: TrustTier::Curated,
            deprecated: false,
            available: installed,
        }
    }

    fn prefs(policy: UpdatePolicy) -> UpdatePreferences {
        UpdatePreferences {
            policy,
            ..Default::default()
        }
    }

    // 16 GB Apple Silicon (Metal ⇒ Apple tier) so everything here fits.
    fn hw() -> HardwareInfo {
        HardwareInfo {
            os: "test".into(),
            arch: "test".into(),
            cpu_cores: 8,
            total_ram_mb: 16_384,
            gpu_backend: GpuBackend::Metal,
            gpu_memory_mb: None,
            gpu_devices: vec![],
            recommended_model: String::new(),
            recommended_context: 4096,
            max_model_mb: 0,
        }
    }

    #[test]
    fn off_policy_suggests_nothing() {
        let m = [model("chat-a", false, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Off),
            &NudgeState::default(),
            100,
            10,
            false,
        );
        assert!(out.is_empty());
    }

    #[test]
    fn active_inference_defers() {
        let m = [model("chat-a", false, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Notify),
            &NudgeState::default(),
            100,
            10,
            true,
        );
        assert!(out.is_empty());
    }

    #[test]
    fn suggests_when_lane_unserved() {
        let m = [model("chat-a", false, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Notify),
            &NudgeState::default(),
            100,
            10,
            false,
        );
        assert_eq!(out.len(), 1);
        assert_eq!(out[0].use_case, UseCase::Assistant);
        assert_eq!(out[0].model_id, "chat-a");
        assert!(out[0].message.contains("install it?"));
    }

    #[test]
    fn served_lane_suggests_nothing() {
        // An installed model for the lane → no acquisition suggestion.
        let m = [model("chat-a", true, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Notify),
            &NudgeState::default(),
            100,
            10,
            false,
        );
        assert!(out.is_empty());
    }

    #[test]
    fn dismissed_suggestion_not_repeated() {
        let m = [model("chat-a", false, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let mut state = NudgeState::default();
        state.dismiss(&dismiss_key(UseCase::Assistant, "chat-a"));
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Notify),
            &state,
            100,
            10,
            false,
        );
        assert!(out.is_empty());
    }

    #[test]
    fn throttled_within_window() {
        let m = [model("chat-a", false, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let state = NudgeState {
            last_concierge_secs: 95,
            ..Default::default()
        };
        // now=100, last=95, throttle=10 → still throttled.
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Notify),
            &state,
            100,
            10,
            false,
        );
        assert!(out.is_empty());
    }

    #[test]
    fn not_throttled_by_upgrade_nudge_field() {
        // Regression guard for the shared-throttle starvation bug: a recent
        // *upgrade nudge* (last_nudge_secs) must NOT throttle the concierge,
        // which throttles only on its own last_concierge_secs.
        let m = [model("chat-a", false, 2000, &[ModelCapability::Generate])];
        let refs: Vec<&ModelSchema> = m.iter().collect();
        let state = NudgeState {
            last_nudge_secs: 99, // upgrade nudge fired one tick ago
            last_concierge_secs: 0, // concierge never has
            ..Default::default()
        };
        let out = decide_concierge(
            &refs,
            &hw(),
            &[UseCase::Assistant],
            QualityTier::Balanced,
            &prefs(UpdatePolicy::Notify),
            &state,
            100,
            10,
            false,
        );
        assert_eq!(out.len(), 1, "concierge must fire regardless of the upgrade nudge's window");
    }

    #[test]
    fn dismiss_key_is_stable_slug_not_debug() {
        // The persisted key must use the stable slug, not Debug formatting.
        assert_eq!(dismiss_key(UseCase::Assistant, "chat-a"), "concierge:assistant=>chat-a");
        assert_eq!(dismiss_key(UseCase::Coding, "code-x"), "concierge:coding=>code-x");
    }
}