1use crate::{
2 acquire::{Acquisition, HfTransport},
3 adapters::{BackendAdapter, BackendError, BackendKind, RuntimeAdapter, ServeRequest},
4 command::{CommandRunner, NativeCommandRunner},
5 http::{HttpError, NativeTransport, Request, Response, Transport},
6 identity::{NativeProcessProbe, ProcessIdentity},
7 lifecycle::{Activation, Lifecycle, Registry},
8 ollama_installed::{InstalledModels, LocalVerifier, verify_catalog_manifest},
9 process_control::{
10 NativeProcessControl, ProcessControl, listener, resolve_binary, resolve_bitnet_server,
11 same_process,
12 },
13 pull::{PullRequest, PullService},
14 special_adapters::{LmStudioAdapter, MlxAdapter},
15 state::{Config, ServerState, StateStore},
16};
17use rigspark_core::{
18 catalog::{Catalog, CatalogModel, resolve},
19 reports::strip_control,
20 sizing::{Hardware, SizingRequest, evaluate},
21};
22use serde_json::{Value, json};
23use std::{path::PathBuf, time::Duration};
24use tokio_util::sync::CancellationToken;
25
26pub mod events;
27use events::{
28 LifecycleObserver, LifecycleScope, LifecycleStage, LifecycleWarning, ObservedAdapter,
29 download_progress, observe, warning,
30};
31
32pub struct LifecycleOptions {
33 pub command: String,
34 pub model: Option<String>,
35 pub backend: Option<String>,
36 pub port: Option<u16>,
37 pub context: Option<u32>,
38 pub installed: bool,
39 pub bypass: bool,
40 pub cache: crate::cache::CacheFlags,
41}
42impl LifecycleOptions {
43 pub fn validate(&self) -> Result<(), BackendError> {
44 if !self.cache.is_empty() {
45 if !["up", "switch"].contains(&self.command.as_str()) {
46 return Err(BackendError(format!(
47 "{} does not accept cache options",
48 self.command
49 )));
50 }
51 if self.installed {
52 return Err(BackendError(
53 "cache options apply only to a runtime rigspark starts; --installed attaches to a running Ollama".into(),
54 ));
55 }
56 if let Some(profile) = self.cache.over(None) {
57 profile
58 .validate()
59 .map_err(|error| BackendError(error.to_string()))?;
60 }
61 }
62 if !["up", "switch", "down", "doctor"].contains(&self.command.as_str())
63 || self.port == Some(0)
64 || self
65 .context
66 .is_some_and(|value| !(1..=10000000).contains(&value))
67 || self
68 .backend
69 .as_ref()
70 .is_some_and(|value| !rigspark_core::catalog::BACKENDS.contains(&value.as_str()))
71 {
72 return Err(BackendError("invalid lifecycle options".into()));
73 }
74 if self.command == "down" || self.command == "doctor" {
75 if (self.command == "doctor" && self.model.is_some())
76 || self.backend.is_some()
77 || self.port.is_some()
78 || self.context.is_some()
79 || self.installed
80 || self.bypass
81 {
82 return Err(BackendError(format!(
83 "{} does not accept selection options",
84 self.command
85 )));
86 }
87 } else if self.model.is_none() {
88 return Err(BackendError("model is required".into()));
89 }
90 if let Some(query) = self.model.as_deref() {
91 if query.trim().is_empty() || query.len() > 8192 || query.chars().any(char::is_control)
92 {
93 return Err(BackendError("invalid model reference".into()));
94 }
95 if self.installed {
96 crate::adapters::model_id(query)?;
97 }
98 }
99 if self.installed
100 && (!self.bypass
101 || self
102 .backend
103 .as_ref()
104 .is_some_and(|backend| backend != "ollama"))
105 {
106 return Err(BackendError(
107 "installed models require --bypass and Ollama".into(),
108 ));
109 }
110 Ok(())
111 }
112}
113struct CheckedHttp<'runtime> {
114 inner: &'runtime dyn Transport,
115 probe: &'runtime NativeProcessProbe,
116 expected: ProcessIdentity,
117}
118#[async_trait::async_trait]
119impl Transport for CheckedHttp<'_> {
120 async fn send(&self, request: Request) -> Result<Response, HttpError> {
121 let observed = listener(self.probe, request.url.as_str(), &CancellationToken::new())
122 .await
123 .map_err(|_| HttpError::Invalid)?;
124 if !same_process(&observed.identity, &self.expected) {
125 return Err(HttpError::Invalid);
126 }
127 self.inner.send(request).await
128 }
129}
130struct ContextActivation<'runtime> {
131 source: String,
132 context: Option<u32>,
133 installed: bool,
134 expected_manifest: Option<String>,
135 expected_sha: Option<String>,
136 expected_bytes: Option<u64>,
137 projectors: Vec<rigspark_core::sizing::ProjectorArtifact>,
138 root: PathBuf,
139 http: &'runtime NativeTransport,
140 probe: &'runtime NativeProcessProbe,
141 observer: Option<&'runtime LifecycleObserver>,
142}
143#[async_trait::async_trait]
144impl Activation for ContextActivation<'_> {
145 async fn finalize(
146 &self,
147 handle: &ServerState,
148 cancel: &CancellationToken,
149 ) -> Result<ServerState, BackendError> {
150 let observed = listener(self.probe, &handle.endpoint, cancel).await?;
151 if handle.pid != Some(observed.identity.pid)
152 || handle.process_executable.as_deref() != Some(&observed.identity.executable)
153 || handle.process_started_at.as_deref() != Some(&observed.identity.started)
154 {
155 return Err(BackendError(
156 "Ollama listener changed before activation".into(),
157 ));
158 }
159 let http = CheckedHttp {
160 inner: self.http,
161 probe: self.probe,
162 expected: observed.identity,
163 };
164 let verifier = LocalVerifier {
165 root: self.root.clone(),
166 };
167 let support = InstalledModels::new(&http, &verifier);
168 let model = support
169 .inspect(&handle.endpoint, &self.source, cancel)
170 .await
171 .map_err(|error| BackendError(error.to_string()))?;
172 if let Some(expected) = &self.expected_manifest {
173 let digests = crate::ollama_installed::manifest_digests(&self.root, &self.source)
175 .map_err(|error| BackendError(error.to_string()))?;
176 if digests.first() != Some(expected) || !digests.contains(&model.digest) {
177 return Err(BackendError("installed source changed; retry".into()));
178 }
179 }
180 observe(
181 self.observer,
182 LifecycleScope::Runtime,
183 LifecycleStage::Verification,
184 async {
185 verify_catalog_manifest(
186 &self.root,
187 &model.id,
188 &model.digest,
189 self.expected_sha.as_deref(),
190 self.expected_bytes,
191 &self.projectors,
192 cancel,
193 )
194 .await
195 .map_err(|error| BackendError(error.to_string()))
196 },
197 )
198 .await?;
199 let runtime = observe(
200 self.observer,
201 LifecycleScope::Runtime,
202 LifecycleStage::Activation,
203 async {
204 support
205 .activate(&handle.endpoint, &model, self.context, cancel)
206 .await
207 .map_err(|error| BackendError(error.to_string()))
208 },
209 )
210 .await?;
211 let final_model = support
212 .inspect(&handle.endpoint, &self.source, cancel)
213 .await
214 .map_err(|error| BackendError(error.to_string()))?;
215 if final_model.digest != model.digest {
216 return Err(BackendError(
217 "installed source changed before commit".into(),
218 ));
219 }
220 let mut active = handle.clone();
221 active.runtime_model_id = Some(runtime);
222 active.context = self.context;
223 if self.installed {
224 active.integrity = Some("local-manifest".into());
225 active.local_manifest_digest = Some(model.digest);
226 }
227 Ok(active)
228 }
229}
230fn compatible(model: &CatalogModel, backend: &str) -> bool {
231 model.supports_backend(backend)
232}
233fn backend_known(backend: &str) -> bool {
234 rigspark_core::catalog::BACKENDS.contains(&backend)
235}
236fn backend_index(backend: &str) -> usize {
238 match backend {
239 "llamacpp" => 1,
240 "mlx" => 2,
241 "lmstudio" => 3,
242 "bitnet" => 4,
243 _ => 0,
244 }
245}
246pub fn check_attach_override(
248 command: &str,
249 active: &str,
250 flag: Option<&str>,
251 env: Option<&str>,
252) -> Result<(), BackendError> {
253 if let Some(backend) = flag
254 && backend != active
255 {
256 return Err(BackendError(format!(
257 "{command} cannot change the active backend from {active} to {backend}; use up --backend {backend}"
258 )));
259 }
260 if let Some(backend) = env.map(str::trim).filter(|value| !value.is_empty())
261 && backend != active
262 {
263 return Err(BackendError(format!(
264 "RIGSPARK_BACKEND={} conflicts with the active {active} server; unset it or use up",
265 strip_control(backend)
266 )));
267 }
268 Ok(())
269}
270pub fn preferred_backend(
272 flag: Option<&str>,
273 env: Option<&str>,
274 user: Option<&str>,
275) -> Option<String> {
276 flag.or(env.filter(|value| !value.trim().is_empty()))
277 .or(user)
278 .map(str::to_owned)
279}
280pub fn auto_backend_order(model: &CatalogModel, hardware: &Hardware) -> Vec<&'static str> {
283 let apple = hardware.platform == rigspark_core::sizing::Platform::Darwin
284 && hardware.arch == rigspark_core::sizing::CpuArch::Arm64;
285 let order: &[&str] = if apple {
286 &["mlx", "ollama", "llamacpp", "bitnet"]
287 } else {
288 &["ollama", "llamacpp", "bitnet"]
289 };
290 order
291 .iter()
292 .copied()
293 .filter(|name| compatible(model, name))
294 .collect()
295}
296pub fn check_backend(
297 model: &CatalogModel,
298 backend: &str,
299 hardware: &Hardware,
300 installed: bool,
301) -> Result<(), BackendError> {
302 if !rigspark_core::catalog::BACKENDS.contains(&backend) {
303 return Err(BackendError("invalid backend selection".into()));
304 }
305 validate_backend_platform(backend, hardware.platform.clone(), hardware.arch.clone())?;
306 if backend == "lmstudio" && model.source.gguf.is_none() && model.source.mlx.is_some() {
307 validate_backend_platform("mlx", hardware.platform.clone(), hardware.arch.clone())?;
308 }
309 if !installed || !compatible(model, backend) {
310 return Err(BackendError(
311 "backend is unavailable or model source is unsupported".into(),
312 ));
313 }
314 Ok(())
315}
316#[derive(Debug, Clone, Copy)]
317pub struct PlanOptions {
318 pub simple_switch: bool,
319 pub bypass: bool,
320 pub context: Option<u32>,
321 pub kv_cache: Option<rigspark_core::sizing::KvCacheType>,
323}
324#[derive(Debug)]
325pub struct QuantPlan {
326 pub quant: rigspark_core::sizing::Quantization,
327 pub estimated_fit: bool,
328}
329pub fn plan_quantization(
331 model: &CatalogModel,
332 explicit: Option<&rigspark_core::sizing::Quantization>,
333 backend: &str,
334 hardware: &Hardware,
335 options: PlanOptions,
336) -> Result<QuantPlan, BackendError> {
337 if options.simple_switch && backend != "ollama" {
338 return Err(BackendError(
339 "single-model and delegated runtimes require up to replace models".into(),
340 ));
341 }
342 if backend == "bitnet" {
343 if !model.supports_backend("bitnet") {
344 return Err(BackendError("BitNet requires an I2_S GGUF artifact".into()));
345 }
346 if options.context.is_some_and(|context| {
347 context == 0 || f64::from(context) > model.context_length.min(4096.0)
348 }) {
349 return Err(BackendError(
350 "BitNet context exceeds the model's supported range".into(),
351 ));
352 }
353 }
354 if options.context.is_some() && !["ollama", "llamacpp", "bitnet"].contains(&backend) {
355 return Err(BackendError(
356 "explicit runtime context currently requires Ollama or llama.cpp".into(),
357 ));
358 }
359 let mut sizing = match options.kv_cache {
360 Some(kind) => model
361 .with_kv_cache(kind)
362 .map_err(|error| BackendError(error.to_string()))?
363 .sizing(),
364 None => model.sizing(),
365 };
366 if let Some(quant) = explicit {
367 sizing.quantizations = vec![quant.clone()];
368 }
369 let fit = evaluate(&SizingRequest {
370 model: sizing,
371 hardware: hardware.clone(),
372 context: options.context.map(f64::from),
373 })
374 .map_err(|error| BackendError(error.to_string()))?;
375 if !options.simple_switch
376 && explicit.is_none()
377 && backend != "lmstudio"
378 && fit.fit.reason == Some("disk-bound")
379 {
380 return Err(BackendError("insufficient disk space".into()));
381 }
382 let quant = if options.simple_switch {
383 explicit.or_else(|| model.quantizations.first()).cloned()
384 } else {
385 explicit.cloned().or(fit.fit.quant.clone()).or_else(|| {
386 if options.bypass {
387 model
388 .quantizations
389 .iter()
390 .min_by(|left, right| left.disk_bytes.total_cmp(&right.disk_bytes))
391 .cloned()
392 } else {
393 None
394 }
395 })
396 }
397 .ok_or_else(|| {
398 BackendError("model does not fit; use --bypass to override estimated fit".into())
399 })?;
400 if !options.simple_switch
401 && backend != "lmstudio"
402 && quant.disk_bytes > hardware.free_disk_bytes
403 {
404 return Err(BackendError("insufficient disk space".into()));
405 }
406 if !options.simple_switch
407 && !fit.fit.fits
408 && !options.bypass
409 && (options.context.is_some() || explicit.is_none())
410 {
411 return Err(BackendError(
412 "model does not fit requested context; use --bypass".into(),
413 ));
414 }
415 Ok(QuantPlan {
416 quant,
417 estimated_fit: !options.simple_switch && !fit.fit.fits,
418 })
419}
420#[derive(Debug)]
422pub struct SpeculationPlan<'catalog> {
423 pub profile: Option<crate::cache::CacheProfile>,
424 pub drafter: Option<&'catalog CatalogModel>,
425 pub implicit: bool,
427}
428pub fn plan_speculation<'catalog>(
430 catalog: &'catalog Catalog,
431 model: &CatalogModel,
432 backend: &str,
433 requested: Option<crate::cache::CacheProfile>,
434 flag_given: bool,
435) -> Result<SpeculationPlan<'catalog>, BackendError> {
436 use crate::cache::Speculative;
437 let mut profile = requested.unwrap_or_default();
438 let drafter = catalog.drafter_of(model).filter(|_| backend == "llamacpp");
439 if !flag_given && matches!(profile.speculative, Speculative::Off | Speculative::Draft) {
441 profile.speculative = if drafter.is_some() {
442 Speculative::Auto
443 } else {
444 Speculative::Off
445 };
446 }
447 let implicit = !flag_given && profile.speculative == Speculative::Auto;
448 let drafter = match profile.speculative {
449 Speculative::Auto => {
450 profile.speculative = if drafter.is_some() {
451 Speculative::Draft
452 } else {
453 Speculative::Off
454 };
455 drafter
456 }
457 Speculative::Draft => Some(drafter.ok_or_else(|| {
458 BackendError(if backend == "llamacpp" {
459 "this model has no catalog drafter; try --speculative ngram".into()
460 } else {
461 "draft speculative decoding requires --backend llamacpp".into()
462 })
463 })?),
464 Speculative::Off | Speculative::Ngram => None,
465 };
466 Ok(SpeculationPlan {
467 profile: crate::cache::effective(Some(&profile)),
468 drafter,
469 implicit,
470 })
471}
472pub fn drafter_reserve(hardware: &Hardware, drafter: &CatalogModel) -> Hardware {
474 let memory = drafter
475 .quantizations
476 .iter()
477 .map(|quant| quant.min_ram_bytes)
478 .fold(0.0, f64::max);
479 let disk = drafter
480 .quantizations
481 .iter()
482 .map(|quant| quant.disk_bytes * 1.5)
483 .fold(0.0, f64::max);
484 let mut reduced = hardware.clone();
485 reduced.total_ram_bytes = (reduced.total_ram_bytes - memory).max(0.0);
486 reduced.free_ram_bytes = (reduced.free_ram_bytes - memory).max(0.0);
487 reduced.free_disk_bytes = (reduced.free_disk_bytes - disk).max(0.0);
488 for gpu in &mut reduced.gpu {
489 gpu.vram_bytes = (gpu.vram_bytes - memory).max(0.0);
490 }
491 reduced
492}
493pub fn slot_sizing_model(
496 model: &CatalogModel,
497 profile: Option<&crate::cache::CacheProfile>,
498 backend: &str,
499 context: Option<u32>,
500 bypass: bool,
501) -> Result<CatalogModel, BackendError> {
502 let slots = profile.and_then(|profile| profile.parallel).unwrap_or(1);
503 let mut sized = model.clone();
504 if backend != "ollama" || slots <= 1 {
505 return Ok(sized);
506 }
507 if context.is_none() {
508 return Err(BackendError(
509 "Ollama sizes every slot at the full context; pass --context with --parallel".into(),
510 ));
511 }
512 match model.kv_bytes_per_token {
513 Some(rate) => sized.kv_bytes_per_token = Some(rate * f64::from(slots)),
514 None if bypass => {}
515 None => {
516 return Err(BackendError(
517 "KV geometry for this model is unknown, so memory for parallel slots cannot be sized; use --bypass to accept that".into(),
518 ));
519 }
520 }
521 Ok(sized)
522}
523fn catalog_pull_request(
524 model: &CatalogModel,
525 quant: &rigspark_core::sizing::Quantization,
526 backend: &str,
527) -> Result<PullRequest, BackendError> {
528 Ok(PullRequest {
529 backend: backend.into(),
530 model_id: if backend == "ollama" {
531 model
532 .source
533 .ollama
534 .clone()
535 .ok_or_else(|| BackendError("missing Ollama source".into()))?
536 } else {
537 model.id.clone()
538 },
539 expected_bytes: quant.disk_bytes as u64,
540 expected_sha256: quant.sha256.clone(),
541 projectors: quant.projectors.clone(),
542 gguf: model.source.gguf.clone(),
543 mlx: model.source.mlx.clone(),
544 })
545}
546pub fn validate_backend_platform(
547 backend: &str,
548 platform: rigspark_core::sizing::Platform,
549 arch: rigspark_core::sizing::CpuArch,
550) -> Result<(), BackendError> {
551 if backend == "mlx"
552 && !(platform == rigspark_core::sizing::Platform::Darwin
553 && arch == rigspark_core::sizing::CpuArch::Arm64)
554 {
555 return Err(BackendError(
556 "MLX requires Apple Silicon (darwin/arm64)".into(),
557 ));
558 }
559 Ok(())
560}
561pub(crate) fn studio_trust(home: &std::path::Path) -> Vec<PathBuf> {
562 let paths = if cfg!(target_os = "macos") {
563 vec![
564 PathBuf::from("/Applications/LM Studio.app/Contents/MacOS/LM Studio"),
565 home.join("Applications/LM Studio.app/Contents/MacOS/LM Studio"),
566 ]
567 } else if cfg!(windows) {
568 let mut paths = vec![
569 PathBuf::from("C:\\Program Files\\LM Studio\\LM Studio.exe"),
570 PathBuf::from("C:\\Program Files\\LM Studio\\llmster.exe"),
571 ];
572 if let Some(local) = std::env::var_os("LOCALAPPDATA") {
573 paths.extend([
574 PathBuf::from(&local).join("Programs/LM Studio/LM Studio.exe"),
575 PathBuf::from(local).join("Programs/LM Studio/llmster.exe"),
576 ]);
577 }
578 paths
579 } else {
580 vec![
581 PathBuf::from("/usr/bin/llmster"),
582 PathBuf::from("/usr/local/bin/llmster"),
583 PathBuf::from("/opt/lm-studio/bin/llmster"),
584 ]
585 };
586 paths
587 .into_iter()
588 .map(|path| path.canonicalize().unwrap_or(path))
589 .collect()
590}
591pub async fn run_native(
592 options: &LifecycleOptions,
593 catalog: &Catalog,
594 hardware: Option<&Hardware>,
595 cancel: &CancellationToken,
596) -> Result<(Value, String), BackendError> {
597 run_native_observed(options, catalog, hardware, cancel, None).await
598}
599pub async fn run_native_observed(
600 options: &LifecycleOptions,
601 catalog: &Catalog,
602 hardware: Option<&Hardware>,
603 cancel: &CancellationToken,
604 observer: Option<&LifecycleObserver>,
605) -> Result<(Value, String), BackendError> {
606 let config = Config::load().map_err(|error| BackendError(error.to_string()))?;
607 run_native_with_config_observed(options, catalog, hardware, cancel, config, observer).await
608}
609pub async fn forget_attached_with_config(
611 config: Config,
612 cancel: &CancellationToken,
613) -> Result<(Value, String), BackendError> {
614 let store = StateStore::new(config);
615 let registry = Registry::new(Vec::new());
616 let lifecycle = Lifecycle {
617 store: &store,
618 registry: ®istry,
619 probe: &NativeProcessProbe,
620 };
621 Ok(match lifecycle.forget_attached(cancel).await? {
622 None => (
623 json!({"type":"no-active"}),
624 "No active server to forget.\n".into(),
625 ),
626 Some(active) => (
627 json!({"type":"forgotten","modelId":active.model_id,"endpoint":active.endpoint}),
628 format!(
629 "Forgot {} ({}); no process was signalled.\n",
630 strip_control(&active.model_id),
631 active.endpoint
632 ),
633 ),
634 })
635}
636pub async fn run_native_with_config(
637 options: &LifecycleOptions,
638 catalog: &Catalog,
639 hardware: Option<&Hardware>,
640 cancel: &CancellationToken,
641 config: Config,
642) -> Result<(Value, String), BackendError> {
643 run_native_with_config_observed(options, catalog, hardware, cancel, config, None).await
644}
645pub async fn run_native_with_config_observed(
646 options: &LifecycleOptions,
647 catalog: &Catalog,
648 hardware: Option<&Hardware>,
649 cancel: &CancellationToken,
650 config: Config,
651 observer: Option<&LifecycleObserver>,
652) -> Result<(Value, String), BackendError> {
653 options.validate()?;
654 let observer = observer.filter(|_| options.command != "doctor");
655 let store = StateStore::new(config.clone());
656 let prior = store
657 .read()
658 .map_err(|error| BackendError(error.to_string()))?;
659 if options.command == "switch" {
660 let active = prior
661 .active
662 .as_ref()
663 .ok_or_else(|| BackendError("no active server to switch; run up first".into()))?;
664 check_attach_override(
665 "switch",
666 &active.backend,
667 options.backend.as_deref(),
668 std::env::var("RIGSPARK_BACKEND").ok().as_deref(),
669 )?;
670 if options.context.is_none()
671 && !options.bypass
672 && let Some(port) = options.port
673 && port != active.port
674 {
675 return Err(BackendError(format!(
676 "switch without --context or --bypass cannot change the active port; use up --port {port}"
677 )));
678 }
679 }
680 if options.command == "down"
681 && let Some(active) = &prior.active
682 {
683 check_attach_override(
684 "down",
685 &active.backend,
686 None,
687 std::env::var("RIGSPARK_BACKEND").ok().as_deref(),
688 )?;
689 }
690 let home = std::env::var_os("HOME")
691 .or_else(|| std::env::var_os("USERPROFILE"))
692 .map(PathBuf::from)
693 .ok_or_else(|| BackendError("home unavailable".into()))?;
694 let bitnet_path = resolve_bitnet_server();
695 if ["down", "doctor"].contains(&options.command.as_str())
696 && prior
697 .active
698 .as_ref()
699 .is_some_and(|active| active.backend == "bitnet")
700 {
701 bitnet_path
702 .as_ref()
703 .map_err(|error| BackendError(error.clone()))?;
704 }
705 let mut paths = ["ollama", "llama-server", "python3", "lms"]
706 .map(|name| resolve_binary(name).ok())
707 .to_vec();
708 paths.push(bitnet_path.as_ref().ok().cloned());
709 let binary = |index: usize| {
710 paths[index]
711 .clone()
712 .unwrap_or_else(|| config.home.join(".unavailable").join(index.to_string()))
713 };
714 let http = NativeTransport::new().map_err(|error| BackendError(error.to_string()))?;
715 let probe = NativeProcessProbe;
716 let control = NativeProcessControl;
717 let commands = NativeCommandRunner;
718 let ollama_root = std::env::var_os("OLLAMA_MODELS")
719 .map(PathBuf::from)
720 .unwrap_or_else(|| home.join(".ollama/models"));
721 let ollama = RuntimeAdapter::new(BackendKind::Ollama, binary(0), &http, &probe, &control)
722 .with_ollama_models(ollama_root.clone())?;
723 let llama = RuntimeAdapter::new(BackendKind::LlamaCpp, binary(1), &http, &probe, &control);
724 let bitnet = RuntimeAdapter::new(BackendKind::BitNet, binary(4), &http, &probe, &control);
725 let mlx = MlxAdapter {
726 binary: binary(2),
727 http: &http,
728 probe: &probe,
729 control: &control,
730 commands: &commands,
731 token: None,
732 };
733 let studio = LmStudioAdapter {
734 binary: binary(3),
735 trusted_executables: studio_trust(&home),
736 http: &http,
737 probe: &probe,
738 commands: &commands,
739 token: std::env::var("LM_API_TOKEN").ok(),
740 };
741 let acquisition_daemon =
742 ObservedAdapter::new(&ollama, observer, LifecycleScope::AcquisitionDaemon);
743 let ollama = ObservedAdapter::new(&ollama, observer, LifecycleScope::Runtime);
744 let llama = ObservedAdapter::new(&llama, observer, LifecycleScope::Runtime);
745 let mlx = ObservedAdapter::new(&mlx, observer, LifecycleScope::Runtime);
746 let studio = ObservedAdapter::new(&studio, observer, LifecycleScope::Runtime);
747 let bitnet = ObservedAdapter::new(&bitnet, observer, LifecycleScope::Runtime);
748 let registry = Registry::new(vec![&ollama, &llama, &mlx, &studio, &bitnet]);
749 let lifecycle = Lifecycle {
750 store: &store,
751 registry: ®istry,
752 probe: &probe,
753 };
754 if options.command == "doctor" {
755 let Some(active) = prior.active else {
756 return Ok((
757 json!({"name":"state","status":"ok","detail":"no active server recorded"}),
758 String::new(),
759 ));
760 };
761 let model = catalog
762 .models
763 .iter()
764 .find(|model| model.id == active.model_id);
765 let path = if active.backend == "mlx" {
766 let source = model
767 .and_then(|model| model.source.mlx.as_ref())
768 .ok_or_else(|| BackendError("active MLX source unavailable".into()))?;
769 let (owner, name) = source
770 .repo
771 .split_once('/')
772 .ok_or_else(|| BackendError("invalid source".into()))?;
773 Some(
774 config
775 .home
776 .join("cache/mlx")
777 .join(owner)
778 .join(format!("{name}@{}", source.revision)),
779 )
780 } else if ["llamacpp", "bitnet"].contains(&active.backend.as_str()) {
781 let source = model
782 .and_then(|model| model.source.gguf.as_ref())
783 .ok_or_else(|| BackendError("active GGUF source unavailable".into()))?;
784 let (owner, name) = source
785 .repo
786 .split_once('/')
787 .ok_or_else(|| BackendError("invalid source".into()))?;
788 Some(
789 config
790 .home
791 .join("cache")
792 .join(&active.backend)
793 .join(owner)
794 .join(format!("{name}@{}", source.revision))
795 .join(&source.file),
796 )
797 } else {
798 active.model_path.as_ref().map(PathBuf::from)
799 };
800 let request = ServeRequest {
801 draft_model_path: None,
802 model_id: active.model_id.clone(),
803 endpoint: active.endpoint.clone(),
804 model_path: path,
805 context: None,
806 cache: None,
807 slot_save_path: None,
808 };
809 lifecycle.health(&active, &request, cancel).await?;
810 return Ok((
811 json!({"name":"state","status":"ok","detail":format!("serving {} at {}",strip_control(&active.model_id),active.endpoint)}),
812 String::new(),
813 ));
814 }
815 if options.command == "down" {
816 let stopped = lifecycle
817 .down_with_target(cancel, || {
818 options
819 .model
820 .as_deref()
821 .map(|query| {
822 resolve(catalog, query)
823 .map(|resolved| resolved.model.id.clone())
824 .map_err(|error| BackendError(error.message))
825 })
826 .transpose()
827 })
828 .await?;
829 let Some(active) = stopped else {
830 return Ok((
831 json!({"type":"no-active"}),
832 "No active server to stop.\n".into(),
833 ));
834 };
835 return Ok((
836 json!({"type":if active.owned_by_us{"stopped"}else{"detached"},"modelId":active.model_id,"endpoint":active.endpoint}),
837 if active.owned_by_us {
838 format!(
839 "Stopped {} ({}).\n",
840 strip_control(&active.model_id),
841 active.endpoint
842 )
843 } else {
844 format!(
845 "Detached from {} ({}); it was not started by rigspark and is still running.\n",
846 strip_control(&active.model_id),
847 active.endpoint
848 )
849 },
850 ));
851 }
852 let query = options
853 .model
854 .as_deref()
855 .ok_or_else(|| BackendError("model required".into()))?;
856 let installed_fallback = options.bypass
857 && resolve(catalog, query).is_err_and(|error| error.code == "MODEL_RESOLUTION_ERROR");
858 if options.installed || installed_fallback {
859 warning(observer, LifecycleWarning::InstalledBypass);
860 if options
861 .backend
862 .as_ref()
863 .is_some_and(|backend| backend != "ollama")
864 {
865 return Err(BackendError(
866 "installed model bypass requires Ollama".into(),
867 ));
868 }
869 let port = options
870 .port
871 .or_else(|| {
872 prior
873 .active
874 .as_ref()
875 .filter(|active| active.backend == "ollama")
876 .map(|active| active.port)
877 })
878 .unwrap_or(11434);
879 let endpoint = format!("http://127.0.0.1:{port}");
880 let reviewed = lifecycle.review(query, cancel).await?;
881 if !control.occupied(&endpoint).await? {
882 return Err(BackendError(
883 "installed activation requires an existing Ollama daemon".into(),
884 ));
885 }
886 let observed = listener(&probe, &endpoint, cancel).await?;
887 if !ollama.trusts(&observed.identity) {
888 return Err(BackendError("untrusted Ollama daemon".into()));
889 }
890 let checked = CheckedHttp {
891 inner: &http,
892 probe: &probe,
893 expected: observed.identity,
894 };
895 let verifier = LocalVerifier {
896 root: ollama_root.clone(),
897 };
898 let support = InstalledModels::new(&checked, &verifier);
899 let model = support
900 .inspect(&endpoint, query, cancel)
901 .await
902 .map_err(|error| BackendError(error.to_string()))?;
903 let catalog_quant = catalog
904 .models
905 .iter()
906 .find(|entry| entry.source.ollama.as_deref() == Some(model.id.as_str()))
907 .and_then(|entry| {
908 entry
909 .quantizations
910 .iter()
911 .find(|quant| Some(&quant.name) == model.quant.as_ref())
912 .or_else(|| entry.quantizations.first())
913 });
914 let activation = ContextActivation {
915 source: model.id.clone(),
916 context: options.context,
917 installed: true,
918 expected_manifest: Some(model.digest),
919 expected_sha: catalog_quant.and_then(|quant| quant.sha256.clone()),
920 expected_bytes: catalog_quant.map(|quant| quant.disk_bytes as u64),
921 projectors: catalog_quant
922 .map(|quant| quant.projectors.clone())
923 .unwrap_or_default(),
924 root: ollama_root,
925 http: &http,
926 probe: &probe,
927 observer,
928 };
929 let request = ServeRequest {
930 draft_model_path: None,
931 model_id: model.id,
932 endpoint,
933 model_path: None,
934 context: options.context,
935 cache: options.cache.over(None),
936 slot_save_path: None,
937 };
938 let active = lifecycle
939 .attach_installed(&request, &reviewed, cancel, &activation)
940 .await?;
941 return Ok(up_output(&active, "local-manifest"));
942 }
943 let resolved = resolve(catalog, query).map_err(|error| BackendError(error.message))?;
944 let model = resolved.model;
945 let prior_cache = prior.active.as_ref().and_then(|active| active.cache);
947 let cache_kept = options.command == "switch"
948 && crate::cache::requested(options.cache, true, prior_cache, None) == prior_cache;
949 if options.command == "switch"
950 && prior
951 .active
952 .as_ref()
953 .is_some_and(|active| active.model_id == model.id)
954 && options.context.is_none()
955 && !options.bypass
956 && cache_kept
957 {
958 let active = prior
959 .active
960 .as_ref()
961 .ok_or_else(|| BackendError("no active model".into()))?;
962 return Ok((
963 json!({"type":"already-active","modelId":model.id,"endpoint":active.endpoint}),
964 format!("{} is already active.\n", model.id),
965 ));
966 }
967 let hardware =
968 hardware.ok_or_else(|| BackendError("hardware required for model selection".into()))?;
969 let simple_switch = options.command == "switch"
970 && options.context.is_none()
971 && !options.bypass
972 && cache_kept
973 && prior
974 .active
975 .as_ref()
976 .is_none_or(|active| active.backend != "bitnet");
977 let env_backend = std::env::var("RIGSPARK_BACKEND").ok();
978 let user = config
979 .user_config()
980 .map_err(|error| BackendError(error.to_string()))?;
981 let requested = crate::cache::requested(
982 options.cache,
983 options.command == "switch",
984 prior_cache,
985 user.cache,
986 );
987 let user_backend = user.default_backend;
988 let configured = preferred_backend(
989 options.backend.as_deref(),
990 env_backend.as_deref(),
991 user_backend.as_deref(),
992 );
993 let backend = if options.command == "switch" {
994 prior
995 .active
996 .as_ref()
997 .ok_or_else(|| BackendError("no active model".into()))?
998 .backend
999 .clone()
1000 } else if let Some(backend) = configured {
1001 backend
1002 } else {
1003 let mut selected = None;
1004 for name in auto_backend_order(model, hardware) {
1005 if name == "bitnet" {
1006 bitnet_path
1007 .as_ref()
1008 .map_err(|error| BackendError(error.clone()))?;
1009 }
1010 let index = backend_index(name);
1011 if paths[index].is_some() {
1012 let args = if name == "mlx" {
1013 vec![
1014 "-I".into(),
1015 "-c".into(),
1016 "import importlib.metadata; print(importlib.metadata.version('mlx-lm'))"
1017 .into(),
1018 ]
1019 } else {
1020 vec!["--version".into()]
1021 };
1022 if let Ok(version) = commands
1023 .run(&binary(index), &args, cancel, Duration::from_secs(5))
1024 .await
1025 && (name != "mlx" || version.trim() == "0.31.3")
1026 {
1027 selected = Some(name.to_owned());
1028 break;
1029 }
1030 }
1031 }
1032 selected.ok_or_else(|| BackendError("no installed backend supports this model".into()))?
1033 };
1034 if backend == "bitnet" {
1035 bitnet_path
1036 .as_ref()
1037 .map_err(|error| BackendError(error.clone()))?;
1038 }
1039 check_backend(
1040 model,
1041 &backend,
1042 hardware,
1043 backend_known(&backend) && paths[backend_index(&backend)].is_some(),
1044 )?;
1045 let index = backend_index(&backend);
1046 let speculation = plan_speculation(
1047 catalog,
1048 model,
1049 &backend,
1050 requested,
1051 options.cache.speculative.is_some(),
1052 )?;
1053 let mut requested = speculation.profile;
1054 let mut drafter = speculation.drafter;
1055 crate::cache::launch_delta(requested.as_ref(), &backend, true)
1056 .map_err(|error| BackendError(error.to_string()))?;
1057 let sized = slot_sizing_model(
1058 model,
1059 requested.as_ref(),
1060 &backend,
1061 options.context,
1062 options.bypass,
1063 )?;
1064 let plan_options = |requested: Option<crate::cache::CacheProfile>| PlanOptions {
1065 simple_switch,
1066 bypass: options.bypass,
1067 context: options.context,
1068 kv_cache: requested
1069 .filter(|profile| profile.kv_k == profile.kv_v)
1070 .map(|profile| profile.kv_k),
1071 };
1072 let reserved = drafter.map(|drafter| drafter_reserve(hardware, drafter));
1073 let plan = match plan_quantization(
1074 &sized,
1075 resolved.quant,
1076 &backend,
1077 reserved.as_ref().unwrap_or(hardware),
1078 plan_options(requested),
1079 ) {
1080 Err(_) if speculation.implicit => {
1081 warning(observer, LifecycleWarning::DrafterSkipped);
1082 drafter = None;
1083 requested = crate::cache::effective(
1084 requested
1085 .map(|profile| crate::cache::CacheProfile {
1086 speculative: crate::cache::Speculative::Off,
1087 ..profile
1088 })
1089 .as_ref(),
1090 );
1091 plan_quantization(
1092 &sized,
1093 resolved.quant,
1094 &backend,
1095 hardware,
1096 plan_options(requested),
1097 )?
1098 }
1099 result => result?,
1100 };
1101 let quant = plan.quant;
1102 let reviewed = lifecycle.review(&model.id, cancel).await?;
1103 let pull_request = catalog_pull_request(model, &quant, &backend)?;
1104 if plan.estimated_fit {
1105 warning(observer, LifecycleWarning::EstimatedFit);
1106 }
1107 let diagnostics = observer.and_then(|observer| observer.diagnostics.clone());
1108 let acquisition = Acquisition::new(config.home.join("cache"))?.with_progress(
1109 move |completed, total, file| {
1110 download_progress(diagnostics.as_ref(), completed, total, file)
1111 },
1112 );
1113 let download = HfTransport::new()?;
1114 let studio_root = home.join(".lmstudio/models");
1115 let pulls = PullService {
1116 acquisition: &acquisition,
1117 download: &download,
1118 commands: &commands,
1119 ollama_models: &ollama_root,
1120 studio_models: &studio_root,
1121 };
1122 let port = options
1123 .port
1124 .or_else(|| {
1125 if options.command == "switch" {
1126 prior.active.as_ref().map(|active| active.port)
1127 } else {
1128 None
1129 }
1130 })
1131 .unwrap_or(match backend.as_str() {
1132 "ollama" => 11434,
1133 "lmstudio" => 1234,
1134 _ => 8080,
1135 });
1136 let endpoint = format!("http://127.0.0.1:{port}");
1137 let pull_binary = binary(index);
1138 let pull = pulls.pull_at_observed(&pull_request, &pull_binary, &endpoint, cancel, observer);
1139 let prepared = if backend == "ollama" {
1140 crate::pull::with_ollama_daemon(&acquisition_daemon, &endpoint, cancel, pull).await?
1141 } else {
1142 pull.await?
1143 };
1144 if !prepared.digest_verified {
1145 warning(observer, LifecycleWarning::SizeOnly);
1146 }
1147 let draft_model_path = match drafter {
1148 Some(drafter) => {
1149 let quant = drafter
1150 .quantizations
1151 .first()
1152 .ok_or_else(|| BackendError("drafter has no quantization".into()))?;
1153 let drafted = pulls
1154 .pull_at_observed(
1155 &catalog_pull_request(drafter, quant, &backend)?,
1156 &pull_binary,
1157 &endpoint,
1158 cancel,
1159 observer,
1160 )
1161 .await?;
1162 if !drafted.digest_verified {
1163 return Err(BackendError(
1164 "refusing an unverified speculative drafter".into(),
1165 ));
1166 }
1167 Some(
1168 drafted
1169 .model_path
1170 .ok_or_else(|| BackendError("drafter path unavailable".into()))?,
1171 )
1172 }
1173 None => None,
1174 };
1175 if simple_switch {
1176 let active = lifecycle
1177 .switch_pointer(&model.id, &reviewed, cancel)
1178 .await?;
1179 return Ok((
1180 json!({"type":"switched","modelId":active.model_id,"endpoint":active.endpoint}),
1181 format!("Switched to {} ({}).\n", active.model_id, active.endpoint),
1182 ));
1183 }
1184 let request = ServeRequest {
1185 draft_model_path,
1186 model_id: model.id.clone(),
1187 endpoint,
1188 model_path: prepared.model_path,
1189 context: if backend == "bitnet" {
1190 Some(
1191 options
1192 .context
1193 .unwrap_or(model.context_length.min(4096.0) as u32),
1194 )
1195 } else {
1196 options.context
1197 },
1198 cache: requested,
1199 slot_save_path: kv_slot_directory_for(&backend, &config)?,
1200 };
1201 let active = if options.context.is_some() && backend == "ollama" {
1202 let activation = ContextActivation {
1203 source: pull_request.model_id,
1204 context: options.context,
1205 installed: false,
1206 expected_manifest: prepared.local_manifest_digest,
1207 expected_sha: quant.sha256.clone(),
1208 expected_bytes: Some(quant.disk_bytes as u64),
1209 projectors: quant.projectors.clone(),
1210 root: ollama_root,
1211 http: &http,
1212 probe: &probe,
1213 observer,
1214 };
1215 lifecycle
1216 .replace_with(&backend, &request, &reviewed, cancel, &activation)
1217 .await?
1218 } else {
1219 lifecycle
1220 .replace(&backend, &request, &reviewed, cancel)
1221 .await?
1222 };
1223 if active.backend == "ollama"
1227 && model
1228 .capabilities
1229 .iter()
1230 .any(|capability| capability != "embedding")
1231 {
1232 let tag = active
1233 .runtime_model_id
1234 .clone()
1235 .or_else(|| model.source.ollama.clone())
1236 .unwrap_or_else(|| active.model_id.clone());
1237 crate::ollama_inference::preload(&http, &active.endpoint, &tag, cancel)
1238 .await
1239 .map_err(|reason| {
1240 BackendError(format!(
1241 "{} started but Ollama cannot load it: {reason}. Update Ollama, or start it with another runtime.",
1242 strip_control(&model.id)
1243 ))
1244 })?;
1245 }
1246 Ok(up_output(
1247 &active,
1248 if prepared.digest_verified {
1249 "verified"
1250 } else {
1251 "size-only"
1252 },
1253 ))
1254}
1255fn up_output(active: &ServerState, integrity: &str) -> (Value, String) {
1256 let mut report = json!({"modelId":active.model_id,"backend":active.backend,"endpoint":active.endpoint,"ownership":if active.owned_by_us{"owned"}else{"attached"},"integrity":integrity});
1257 let mut text = format!(
1258 "{} ready at {}\n",
1259 strip_control(&active.model_id),
1260 active.endpoint
1261 );
1262 if let Some(runtime) = &active.runtime_model_id {
1263 text.push_str(&format!("Runtime model: {}", strip_control(runtime)));
1264 if let Some(context) = active.context {
1265 text.push_str(&format!(" (context {context})"));
1266 }
1267 text.push('\n');
1268 }
1269 if let Some(cache) = &active.cache {
1270 report["cache"] = json!(cache);
1271 text.push_str(&format!("Cache: {}\n", cache.summary()));
1272 }
1273 (report, text)
1274}
1275
1276pub async fn installed_inventory(
1277 hardware: &Hardware,
1278 model: Option<&str>,
1279 port: u16,
1280 context: Option<u32>,
1281 fits_only: bool,
1282 cancel: &CancellationToken,
1283) -> Result<(Value, String, u8), BackendError> {
1284 if port == 0 || context.is_some_and(|context| !(1..=10000000).contains(&context)) {
1285 return Err(BackendError("invalid installed comparison options".into()));
1286 }
1287 let binary = resolve_binary("ollama")?;
1288 let endpoint = format!("http://127.0.0.1:{port}");
1289 let http = NativeTransport::new().map_err(|error| BackendError(error.to_string()))?;
1290 let probe = NativeProcessProbe;
1291 let before = listener(&probe, &endpoint, cancel).await?;
1292 if binary.to_str() != Some(before.identity.executable.as_str()) {
1293 return Err(BackendError(
1294 "untrusted installed inventory listener".into(),
1295 ));
1296 }
1297 let checked = CheckedHttp {
1298 inner: &http,
1299 probe: &probe,
1300 expected: before.identity.clone(),
1301 };
1302 let root = std::env::var_os("OLLAMA_MODELS")
1303 .map(PathBuf::from)
1304 .unwrap_or_else(|| {
1305 std::env::var_os("HOME")
1306 .or_else(|| std::env::var_os("USERPROFILE"))
1307 .map(PathBuf::from)
1308 .unwrap_or_default()
1309 .join(".ollama/models")
1310 });
1311 let verifier = LocalVerifier { root };
1312 let support = InstalledModels::new(&checked, &verifier);
1313 let models = if let Some(model) = model {
1314 vec![
1315 support
1316 .inspect(&endpoint, model, cancel)
1317 .await
1318 .map_err(|error| BackendError(error.to_string()))?,
1319 ]
1320 } else {
1321 let mut models = Vec::new();
1322 for entry in support
1323 .list(&endpoint, cancel)
1324 .await
1325 .map_err(|error| BackendError(error.to_string()))?
1326 {
1327 models.push(
1328 support
1329 .inspect(&endpoint, &entry.id, cancel)
1330 .await
1331 .map_err(|error| BackendError(error.to_string()))?,
1332 );
1333 }
1334 models
1335 };
1336 let mut results = Vec::new();
1337 let mut lines = Vec::new();
1338 for entry in models {
1339 let sized = crate::ollama_installed::size_installed(&entry, hardware, context)
1340 .map_err(|error| BackendError(error.to_string()))?;
1341 if fits_only && sized["fit"] != "yes" {
1342 continue;
1343 }
1344 lines.push(format!(
1345 "{}: context {}; estimated {} fit {}; weights {:.2} GiB ({}); throughput unknown",
1346 strip_control(&entry.id),
1347 context
1348 .map(|value| value.to_string())
1349 .unwrap_or_else(|| "default".into()),
1350 sized["memoryKind"].as_str().unwrap_or("ram"),
1351 sized["fit"].as_str().unwrap_or("unknown"),
1352 entry.size_bytes as f64 / 1073741824.0,
1353 if sized["weightsFit"] == true {
1354 "fit"
1355 } else {
1356 "over budget"
1357 }
1358 ));
1359 results.push(sized);
1360 }
1361 let after = listener(&probe, &endpoint, cancel).await?;
1362 if !same_process(&before.identity, &after.identity) {
1363 return Err(BackendError("inventory listener changed".into()));
1364 }
1365 let exit =
1366 u8::from(model.is_some() && results.first().is_some_and(|result| result["fit"] == "no"));
1367 Ok((
1368 json!({"source":"local-runtime-metadata","models":results}),
1369 format!(
1370 "{}\n",
1371 if lines.is_empty() {
1372 "No installed models match.".into()
1373 } else {
1374 lines.join("\n")
1375 }
1376 ),
1377 exit,
1378 ))
1379}
1380
1381pub fn kv_slot_directory(config: &Config) -> Result<PathBuf, BackendError> {
1384 let parent = config
1385 .state
1386 .parent()
1387 .filter(|parent| parent.is_absolute())
1388 .ok_or_else(|| BackendError("runtime state must live in an absolute directory".into()))?;
1389 let directory = parent.join("kv-slots");
1390 std::fs::create_dir_all(&directory).map_err(|error| BackendError(error.to_string()))?;
1391 #[cfg(unix)]
1392 {
1393 use std::os::unix::fs::PermissionsExt;
1394 std::fs::set_permissions(&directory, std::fs::Permissions::from_mode(0o700))
1395 .map_err(|error| BackendError(error.to_string()))?;
1396 }
1397 let metadata =
1398 std::fs::symlink_metadata(&directory).map_err(|error| BackendError(error.to_string()))?;
1399 if !metadata.is_dir() {
1400 return Err(BackendError("KV slot directory is not a directory".into()));
1401 }
1402 Ok(directory)
1403}
1404
1405pub fn kv_slot_directory_for(
1407 backend: &str,
1408 config: &Config,
1409) -> Result<Option<PathBuf>, BackendError> {
1410 if backend == "llamacpp" {
1411 kv_slot_directory(config).map(Some)
1412 } else {
1413 Ok(None)
1414 }
1415}
1416
1417pub async fn kv_slot_with_config(
1420 config: Config,
1421 slot: u32,
1422 action: &crate::openai::KvSlotAction,
1423 cancel: &CancellationToken,
1424) -> Result<crate::openai::KvSlotReport, BackendError> {
1425 let active = StateStore::new(config)
1426 .read()
1427 .map_err(|error| BackendError(error.to_string()))?
1428 .active
1429 .ok_or_else(|| BackendError("no active runtime".into()))?;
1430 if active.backend != "llamacpp" {
1431 return Err(BackendError("KV slots apply only to llama.cpp".into()));
1432 }
1433 let binary = resolve_binary("llama-server")?;
1434 let http = NativeTransport::new().map_err(|error| BackendError(error.to_string()))?;
1435 let probe = NativeProcessProbe;
1436 let control = NativeProcessControl;
1437 let llama = RuntimeAdapter::new(BackendKind::LlamaCpp, binary, &http, &probe, &control);
1438 crate::openai::OpenAiInference {
1439 http: &http,
1440 probe: &probe,
1441 adapter: &llama,
1442 token: None,
1443 }
1444 .kv_slot(&active, slot, action, cancel)
1445 .await
1446}
1447
1448#[cfg(test)]
1449mod acquisition_contract_tests {
1450 use super::*;
1451
1452 #[test]
1453 fn catalog_pull_preserves_quant_digest_size_and_runtime_model_id() {
1454 let mut catalog = Catalog::parse(rigspark_core::MODELS_JSON).unwrap();
1455 let model = catalog
1456 .models
1457 .iter_mut()
1458 .find(|model| model.source.ollama.is_some())
1459 .unwrap();
1460 model.quantizations[0].sha256 = Some("b".repeat(64));
1461 model.quantizations[0].disk_bytes = 5_000_000_000.0;
1462 let id = model.id.clone();
1463 let quant_query = format!("{id}-{}", model.quantizations[0].name);
1464 for query in [&id, &quant_query] {
1465 let resolved = resolve(&catalog, query).unwrap();
1466 let quant = resolved.quant.unwrap_or(&resolved.model.quantizations[0]);
1467 let request = catalog_pull_request(resolved.model, quant, "ollama").unwrap();
1468 assert_eq!(request.expected_sha256, Some("b".repeat(64)));
1469 assert_eq!(request.expected_bytes, 5_000_000_000);
1470 assert_eq!(Some(request.model_id), resolved.model.source.ollama);
1471 }
1472 let model = &mut catalog.models[0];
1473 model.source.ollama = None;
1474 assert!(catalog_pull_request(model, &model.quantizations[0], "ollama").is_err());
1475 }
1476
1477 #[test]
1478 fn success_lines_strip_controls_and_report_integrity_and_ownership() {
1479 let active = ServerState {
1480 backend: "ollama".into(),
1481 model_id: "evil\u{1b}[31m\nid".into(),
1482 endpoint: "http://127.0.0.1:11434".into(),
1483 port: 11434,
1484 owned_by_us: true,
1485 pid: Some(7),
1486 runtime_model_id: Some("variant\u{7}".into()),
1487 context: Some(65_536),
1488 integrity: None,
1489 local_manifest_digest: None,
1490 model_path: None,
1491 process_executable: None,
1492 process_started_at: None,
1493 auth_token: None,
1494 cache: None,
1495 };
1496 let (report, text) = up_output(&active, "size-only");
1497 assert_eq!(
1498 (report["ownership"].as_str(), report["integrity"].as_str()),
1499 (Some("owned"), Some("size-only"))
1500 );
1501 assert!(
1502 !text
1503 .chars()
1504 .any(|character| character.is_control() && character != '\n'),
1505 "{text:?}"
1506 );
1507 assert_eq!(text.lines().count(), 2, "{text:?}");
1508 assert!(text.ends_with("(context 65536)\n"));
1509 }
1510}