Skip to main content

rigspark_runtime/
pull.rs

1use crate::{
2    acquire::{Acquisition, Artifact, DownloadTransport, SizePolicy, hash_file},
3    adapters::{BackendAdapter, BackendError, ServeRequest, model_id},
4    application::events::{LifecycleObserver, LifecycleScope, LifecycleStage, observe},
5    command::{CommandRunner, OllamaCommandContext},
6    ollama_installed::{model_path, verify_catalog_manifest},
7    state::secure_read,
8};
9use rigspark_core::catalog::{GgufSource, MlxSource};
10use serde::{Deserialize, Serialize};
11use sha2::{Digest, Sha256};
12use std::{
13    path::{Path, PathBuf},
14    time::Duration,
15};
16use tokio_util::sync::CancellationToken;
17
18pub async fn with_ollama_daemon<T>(
19    adapter: &dyn BackendAdapter,
20    endpoint: &str,
21    cancel: &CancellationToken,
22    operation: impl std::future::Future<Output = Result<T, BackendError>>,
23) -> Result<T, BackendError> {
24    if adapter.name() != "ollama" || cancel.is_cancelled() {
25        return Err(BackendError(
26            "invalid or cancelled acquisition daemon request".into(),
27        ));
28    }
29    let request = ServeRequest {
30        model_id: "llmup-acquisition".into(),
31        endpoint: endpoint.into(),
32        model_path: None,
33        context: None,
34        cache: None,
35    };
36    let handle = adapter.serve(&request, cancel).await?;
37    let result = tokio::select! { biased; _ = cancel.cancelled() => Err(BackendError("pull cancelled".into())), result = operation => result };
38    if handle.owned_by_us {
39        let cleanup = tokio::time::timeout(
40            Duration::from_secs(15),
41            adapter.stop(&handle, &CancellationToken::new()),
42        )
43        .await;
44        let failure = match cleanup {
45            Ok(Ok(())) => None,
46            Ok(Err(error)) => Some(error.0),
47            Err(_) => Some("cleanup timed out".into()),
48        };
49        if let Some(failure) = failure {
50            return Err(BackendError(format!(
51                "{}; acquisition daemon cleanup failed (pid {:?}): {failure}",
52                result
53                    .as_ref()
54                    .err()
55                    .map(|error| error.0.as_str())
56                    .unwrap_or("pull completed"),
57                handle.pid
58            )));
59        }
60    }
61    result
62}
63
64#[derive(Clone)]
65pub struct PullRequest {
66    pub backend: String,
67    pub model_id: String,
68    pub expected_bytes: u64,
69    pub expected_sha256: Option<String>,
70    pub projectors: Vec<rigspark_core::sizing::ProjectorArtifact>,
71    pub gguf: Option<GgufSource>,
72    pub mlx: Option<MlxSource>,
73}
74#[derive(Debug, Clone, Serialize)]
75#[serde(rename_all = "camelCase")]
76pub struct PreparedModel {
77    pub model_id: String,
78    pub model_path: Option<PathBuf>,
79    pub digest_verified: bool,
80    pub local_manifest_digest: Option<String>,
81}
82pub struct PullService<'runtime> {
83    pub acquisition: &'runtime Acquisition,
84    pub download: &'runtime dyn DownloadTransport,
85    pub commands: &'runtime dyn CommandRunner,
86    pub ollama_models: &'runtime Path,
87    pub studio_models: &'runtime Path,
88}
89impl PullService<'_> {
90    pub async fn pull(
91        &self,
92        request: &PullRequest,
93        binary: &Path,
94        cancel: &CancellationToken,
95    ) -> Result<PreparedModel, BackendError> {
96        self.pull_at(request, binary, "http://127.0.0.1:11434", cancel)
97            .await
98    }
99    pub async fn pull_at(
100        &self,
101        request: &PullRequest,
102        binary: &Path,
103        endpoint: &str,
104        cancel: &CancellationToken,
105    ) -> Result<PreparedModel, BackendError> {
106        self.pull_at_observed(request, binary, endpoint, cancel, None)
107            .await
108    }
109    pub async fn pull_at_observed(
110        &self,
111        request: &PullRequest,
112        binary: &Path,
113        endpoint: &str,
114        cancel: &CancellationToken,
115        observer: Option<&LifecycleObserver>,
116    ) -> Result<PreparedModel, BackendError> {
117        model_id(&request.model_id)?;
118        let mut projector_digests = std::collections::HashSet::new();
119        let projector_bytes = request
120            .projectors
121            .iter()
122            .try_fold(0_u64, |total, projector| {
123                if projector.bytes == 0
124                    || projector.sha256.len() != 64
125                    || !projector
126                        .sha256
127                        .bytes()
128                        .all(|byte| byte.is_ascii_hexdigit())
129                    || !projector_digests.insert(&projector.sha256)
130                {
131                    return None;
132                }
133                total.checked_add(projector.bytes)
134            });
135        if request.projectors.len() > 16
136            || projector_bytes.is_none_or(|total| total >= request.expected_bytes)
137            || (!request.projectors.is_empty() && request.expected_sha256.is_none())
138        {
139            return Err(BackendError("invalid expected projector integrity".into()));
140        }
141        if request.expected_bytes == 0
142            || request.expected_bytes > 9007199254740991
143            || request.expected_sha256.as_ref().is_some_and(|value| {
144                value.len() != 64 || !value.bytes().all(|byte| byte.is_ascii_hexdigit())
145            })
146        {
147            return Err(BackendError("invalid expected model integrity".into()));
148        }
149        if cancel.is_cancelled() {
150            return Err(BackendError("cancelled".into()));
151        }
152        let mut result = PreparedModel {
153            model_id: request.model_id.clone(),
154            model_path: None,
155            digest_verified: false,
156            local_manifest_digest: None,
157        };
158        match request.backend.as_str() {
159            "ollama" => {
160                let context = OllamaCommandContext::new(endpoint, self.ollama_models)?;
161                observe(
162                    observer,
163                    LifecycleScope::Runtime,
164                    LifecycleStage::Acquisition,
165                    async {
166                        self.commands
167                            .run_ollama(
168                                binary,
169                                &["pull".into(), "--".into(), request.model_id.clone()],
170                                &context,
171                                cancel,
172                                Duration::from_secs(1800),
173                            )
174                            .await
175                            .map_err(BackendError)
176                    },
177                )
178                .await?;
179                let digest = observe(
180                    observer,
181                    LifecycleScope::Runtime,
182                    LifecycleStage::Verification,
183                    async {
184                        let path = model_path(self.ollama_models, &request.model_id)
185                            .map_err(|error| BackendError(error.to_string()))?;
186                        let raw = secure_read(&path, 4 * 1024 * 1024, false)
187                            .map_err(|error| BackendError(error.to_string()))?;
188                        let digest = format!("{:x}", Sha256::digest(raw.as_bytes()));
189                        verify_catalog_manifest(
190                            self.ollama_models,
191                            &request.model_id,
192                            &digest,
193                            request.expected_sha256.as_deref(),
194                            Some(request.expected_bytes),
195                            &request.projectors,
196                            cancel,
197                        )
198                        .await
199                        .map_err(|error| BackendError(error.to_string()))?;
200                        Ok(digest)
201                    },
202                )
203                .await?;
204                result.digest_verified = request.expected_sha256.is_some();
205                result.local_manifest_digest = Some(digest);
206            }
207            "llamacpp" | "bitnet" => {
208                let source = request.gguf.as_ref().ok_or_else(|| {
209                    BackendError(format!("{} requires a pinned GGUF source", request.backend))
210                })?;
211                let ceiling = request
212                    .expected_bytes
213                    .checked_add(request.expected_bytes.div_ceil(2))
214                    .ok_or_else(|| BackendError("model size overflow".into()))?
215                    .max(
216                        request
217                            .expected_bytes
218                            .checked_add(64 * 1024 * 1024)
219                            .ok_or_else(|| BackendError("model size overflow".into()))?,
220                    );
221                let artifact = Artifact {
222                    backend: request.backend.clone(),
223                    repo: source.repo.clone(),
224                    revision: source.revision.clone(),
225                    file: source.file.clone(),
226                    sha256: source.sha256.clone(),
227                    bytes: ceiling,
228                };
229                let acquired = observe(
230                    observer,
231                    LifecycleScope::Runtime,
232                    LifecycleStage::AcquisitionVerification,
233                    async {
234                        self.acquisition
235                            .acquire_sized(&artifact, SizePolicy::Ceiling, self.download, cancel)
236                            .await
237                            .map_err(BackendError)
238                    },
239                )
240                .await?;
241                result.model_path = Some(acquired.path);
242                result.digest_verified = true;
243            }
244            "mlx" => {
245                let source = request.mlx.as_ref().ok_or_else(|| {
246                    BackendError("MLX requires pinned repository manifest".into())
247                })?;
248                let files = repository_artifacts(source, &request.backend, request.expected_bytes)?;
249                result.model_path = Some(
250                    observe(
251                        observer,
252                        LifecycleScope::Runtime,
253                        LifecycleStage::AcquisitionVerification,
254                        async {
255                            self.acquisition
256                                .repository(&files, self.download, cancel)
257                                .await
258                                .map_err(BackendError)
259                        },
260                    )
261                    .await?,
262                );
263                result.digest_verified = true;
264            }
265            "lmstudio" => {
266                let raw = self
267                    .commands
268                    .run(
269                        binary,
270                        &[
271                            "ls".into(),
272                            "--json".into(),
273                            "--llm".into(),
274                            "--quiet".into(),
275                        ],
276                        cancel,
277                        Duration::from_secs(5),
278                    )
279                    .await?;
280                let models = studio_models(&raw)?;
281                let mut matches = models.into_iter().filter(|model| {
282                    let path = model.path.replace('\\', "/");
283                    if let Some(source) = &request.gguf {
284                        let suffix = format!("{}/{}", source.repo, source.file);
285                        path == suffix || path.ends_with(&format!("/{suffix}"))
286                    } else if let Some(source) = &request.mlx {
287                        path == source.repo || path.ends_with(&format!("/{}", source.repo))
288                    } else {
289                        model.model_key.as_deref() == Some(request.model_id.as_str())
290                            || path == request.model_id
291                    }
292                });
293                let selected = matches
294                    .next()
295                    .ok_or_else(|| BackendError("model is not downloaded in LM Studio".into()))?;
296                if matches.next().is_some() {
297                    return Err(BackendError("ambiguous LM Studio model selection".into()));
298                }
299                let local = studio_path(self.studio_models, &selected.path)?;
300                observe(
301                    observer,
302                    LifecycleScope::Runtime,
303                    LifecycleStage::Verification,
304                    async {
305                        if let Some(source) = &request.gguf {
306                            let (digest, bytes) = hash_file(&local, cancel).await?;
307                            if !digest.eq_ignore_ascii_case(&source.sha256)
308                                || bytes < request.expected_bytes.div_ceil(2)
309                            {
310                                return Err(BackendError(
311                                    "delegated GGUF integrity mismatch".into(),
312                                ));
313                            }
314                            result.digest_verified = true;
315                        } else if let Some(source) = &request.mlx {
316                            let files =
317                                repository_artifacts(source, "mlx", request.expected_bytes)?;
318                            for artifact in files {
319                                let path = studio_path(&local, &artifact.file)?;
320                                let (digest, bytes) = hash_file(&path, cancel).await?;
321                                if !digest.eq_ignore_ascii_case(&artifact.sha256)
322                                    || bytes != artifact.bytes
323                                {
324                                    return Err(BackendError(
325                                        "delegated MLX integrity mismatch".into(),
326                                    ));
327                                }
328                            }
329                            crate::special_adapters::validate_mlx_directory(&local)?;
330                            result.digest_verified = true;
331                        } else {
332                            return Err(BackendError(
333                                "delegated model needs catalog integrity evidence".into(),
334                            ));
335                        }
336                        Ok(())
337                    },
338                )
339                .await?;
340                result.model_path = Some(PathBuf::from(selected.path));
341            }
342            _ => return Err(BackendError("unknown backend".into())),
343        }
344        Ok(result)
345    }
346}
347fn repository_artifacts(
348    source: &MlxSource,
349    backend: &str,
350    expected: u64,
351) -> Result<Vec<Artifact>, BackendError> {
352    let mut total = 0u64;
353    let mut artifacts = Vec::new();
354    for file in &source.files {
355        if !file.bytes.is_finite()
356            || file.bytes.fract() != 0.0
357            || file.bytes <= 0.0
358            || file.bytes > 9007199254740991.0
359        {
360            return Err(BackendError("invalid repository file size".into()));
361        }
362        total = total
363            .checked_add(file.bytes as u64)
364            .ok_or_else(|| BackendError("repository size overflow".into()))?;
365        let artifact = Artifact {
366            backend: backend.into(),
367            repo: source.repo.clone(),
368            revision: source.revision.clone(),
369            file: file.file.clone(),
370            sha256: file.sha256.clone(),
371            bytes: file.bytes as u64,
372        };
373        artifact.validate()?;
374        artifacts.push(artifact);
375    }
376    if total != expected {
377        return Err(BackendError("repository/catalog size mismatch".into()));
378    }
379    Ok(artifacts)
380}
381#[derive(Deserialize)]
382#[serde(rename_all = "camelCase")]
383struct StudioModel {
384    path: String,
385    model_key: Option<String>,
386}
387fn studio_models(raw: &str) -> Result<Vec<StudioModel>, BackendError> {
388    #[derive(Deserialize)]
389    struct Group {
390        model: serde_json::Value,
391        variants: Vec<StudioModel>,
392    }
393    #[derive(Deserialize)]
394    #[serde(untagged)]
395    enum Entry {
396        Model(StudioModel),
397        Group(Group),
398    }
399    let entries: Vec<Entry> = serde_json::from_str(raw)
400        .map_err(|_| BackendError("invalid LM Studio inventory".into()))?;
401    if entries.len() > 10000 {
402        return Err(BackendError("LM Studio inventory exceeds limit".into()));
403    }
404    let mut models = Vec::new();
405    for entry in entries {
406        match entry {
407            Entry::Model(model) => models.push(model),
408            Entry::Group(group) => {
409                let key = group
410                    .model
411                    .get("modelKey")
412                    .and_then(serde_json::Value::as_str);
413                for mut model in group.variants {
414                    if model.model_key.is_none() {
415                        model.model_key = key.map(str::to_owned);
416                    }
417                    models.push(model);
418                }
419            }
420        }
421    }
422    if models.len() > 10000
423        || models.iter().any(|model| {
424            model.path.is_empty()
425                || model.path.len() > 4096
426                || model.path.chars().any(char::is_control)
427                || model.model_key.as_ref().is_some_and(|key| {
428                    key.is_empty() || key.len() > 1024 || key.chars().any(char::is_control)
429                })
430        })
431    {
432        return Err(BackendError("invalid delegated model path".into()));
433    }
434    Ok(models)
435}
436fn studio_path(root: &Path, value: &str) -> Result<PathBuf, BackendError> {
437    let normalized = value.replace('\\', "/");
438    if normalized.split('/').any(|part| part == "..") {
439        return Err(BackendError("delegated path traversal".into()));
440    }
441    let root = root
442        .canonicalize()
443        .map_err(|_| BackendError("delegated root unavailable".into()))?;
444    let path = Path::new(&normalized);
445    let path = if path.is_absolute() {
446        path.to_owned()
447    } else {
448        root.join(path)
449    };
450    let path = path
451        .canonicalize()
452        .map_err(|_| BackendError("delegated artifact unavailable".into()))?;
453    if !path.starts_with(&root) || path == root {
454        return Err(BackendError("delegated artifact escaped root".into()));
455    }
456    Ok(path)
457}