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