diff --git a/apps/rocmd/src/artifact_prefetch.rs b/apps/rocmd/src/artifact_prefetch.rs new file mode 100644 index 000000000..d24530516 --- /dev/null +++ b/apps/rocmd/src/artifact_prefetch.rs @@ -0,0 +1,1001 @@ +// Copyright © Advanced Micro Devices, Inc., or its affiliates. +// +// SPDX-License-Identifier: MIT + +//! The `prefetch_artifact` sandbox tool's download policy and driver: whether a +//! recipe artifact may be fetched under the approved source policy, the +//! host-scoped Hugging Face URL checks that gate a token, and the streamed, +//! sha256-verified download that records the cache marker. [`crate::sandbox`] +//! resolves the artifact reference and calls in; nothing here calls back. + +use crate::sandbox_tool::{SandboxToolArg, SandboxToolPolicy}; +use anyhow::{Context, Result}; +use rocm_core::atomic_write::write_file_atomically; +use rocm_core::{ + AppPaths, ModelRecipeArtifactRecord, model_artifact_cache_status, unix_time_millis, +}; +use serde_json::Value; +use serde_json::json; +use std::path::{Path, PathBuf}; +use std::time::Duration; + +const ARTIFACT_PREFETCH_TIMEOUT: Duration = Duration::from_mins(10); + +pub(crate) fn prefetch_artifact_value_with_policy( + paths: &AppPaths, + artifact_ref: &str, + model: &str, + artifact: ModelRecipeArtifactRecord, + policy: SandboxToolPolicy, +) -> Result { + let cache = model_artifact_cache_status(paths, model, &artifact); + if !policy.allow_artifact_download { + return Ok(json!({ + "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), + "artifact_ref": artifact_ref, + "model": model, + "artifact": artifact, + "cache": cache, + "status": "source_policy_required", + "mutating": false, + "network_used": false, + "message": "artifact prefetch requires an approved source policy before network access; no artifact bytes were downloaded", + })); + } + + if cache.marker_path.is_file() { + return Ok(json!({ + "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), + "artifact_ref": artifact_ref, + "model": model, + "artifact": artifact, + "cache": cache, + "status": "cached", + "mutating": false, + "network_used": false, + "message": "artifact cache marker already exists; no network request was made", + })); + } + + if let Some(message) = declared_source_policy_block_message(&artifact) { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + &message, + )); + } + let source_policy_requires_huggingface_auth = + artifact_declares_huggingface_authenticated_policy(&artifact); + let artifact_kind = artifact.kind.to_ascii_lowercase(); + let huggingface_artifact = artifact_is_huggingface(&artifact); + if artifact.gated.unwrap_or(false) && !huggingface_artifact { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "gated artifacts require a source-specific authentication policy before download", + )); + } + if !matches!(artifact_kind.as_str(), "url" | "http" | "https" | "direct") + && !huggingface_artifact + { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "approved live prefetch currently supports direct HTTP(S) artifacts only", + )); + } + if !artifact.uri.starts_with("https://") && !artifact.uri.starts_with("http://") { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + if huggingface_artifact { + "authenticated Hugging Face prefetch requires an HTTP(S) artifact URI in the signed recipe metadata" + } else { + "approved live prefetch requires an HTTP(S) artifact URI" + }, + )); + } + let mut request_headers = Vec::<(&str, String)>::new(); + let source_policy = if huggingface_artifact + && (artifact.gated.unwrap_or(false) + || policy.allow_huggingface_download + || source_policy_requires_huggingface_auth) + { + if !policy.allow_huggingface_download { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "Hugging Face artifacts require --allow-huggingface-download before rocm-cli may use an authentication token", + )); + } + if !artifact.uri.starts_with("https://") { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "rocm-cli will not send a Hugging Face token over plain HTTP; use an HTTPS Hugging Face artifact URI", + )); + } + if !is_huggingface_url(&artifact.uri) { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "rocm-cli will not send a Hugging Face token to a non-Hugging Face URL", + )); + } + let Some(token) = policy + .huggingface_token + .as_deref() + .map(str::trim) + .filter(|token| !token.is_empty()) + else { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "Hugging Face artifact prefetch needs ROCM_CLI_HUGGINGFACE_TOKEN, HF_TOKEN, or HUGGING_FACE_HUB_TOKEN", + )); + }; + request_headers.push(("Authorization", format!("Bearer {token}"))); + "huggingface_authenticated" + } else { + "explicit_allow_artifact_download" + }; + let Some(expected_sha256) = artifact + .sha256 + .as_deref() + .map(|value| value.trim().to_ascii_lowercase()) + .filter(|value| value.len() == 64 && value.chars().all(|ch| ch.is_ascii_hexdigit())) + else { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "approved live prefetch requires a valid sha256 in the recipe artifact metadata", + )); + }; + let Some(size_bytes) = artifact.size_bytes else { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "approved live prefetch requires size_bytes in the recipe artifact metadata", + )); + }; + let max_bytes = policy.artifact_max_bytes.unwrap_or(size_bytes); + if size_bytes > max_bytes { + return Ok(prefetch_blocked_value( + artifact_ref, + model, + artifact, + cache, + "artifact size exceeds the approved download byte limit", + )); + } + + // Streamed straight to its final path: model artifacts run to gigabytes, so + // buffering one to hash it would hold the whole file in memory. The size and + // digest from the recipe are enforced inside the download, which also gives + // this path a free-space preflight and resume on a dropped connection. + let artifact_path = artifact_bytes_path_for_marker(&cache.marker_path); + let mut headers: Vec<(&str, &str)> = vec![("User-Agent", "rocm-cli")]; + headers.extend( + request_headers + .iter() + .map(|(name, value)| (*name, value.as_str())), + ); + let outcome = rocm_core::download_file_streaming(&rocm_core::DownloadRequest { + url: &artifact.uri, + destination: &artifact_path, + timeout: ARTIFACT_PREFETCH_TIMEOUT, + headers: &headers, + max_bytes: Some(max_bytes), + expected_len: Some(size_bytes), + expected_sha256: Some(&expected_sha256), + }) + .with_context(|| format!("failed to prefetch artifact `{artifact_ref}`"))?; + let actual_sha256 = outcome.sha256; + write_file_atomically( + &cache.marker_path, + &serde_json::to_vec_pretty(&json!({ + "artifact_ref": artifact_ref, + "model": model, + "artifact": artifact, + "bytes_path": artifact_path, + "size_bytes": size_bytes, + "sha256": actual_sha256, + "prefetched_at_unix_ms": unix_time_millis(), + "source_policy": source_policy, + })) + .context("failed to serialize artifact cache marker")?, + )?; + let cache = model_artifact_cache_status(paths, model, &artifact); + Ok(json!({ + "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), + "artifact_ref": artifact_ref, + "model": model, + "artifact": artifact, + "cache": cache, + "status": "prefetched", + "mutating": true, + "network_used": true, + "bytes_path": artifact_path, + "size_bytes": size_bytes, + "sha256": actual_sha256, + "source_policy": source_policy, + "message": "artifact downloaded and verified with recipe sha256", + })) +} + +fn declared_source_policy_block_message(artifact: &ModelRecipeArtifactRecord) -> Option { + let source_policy = artifact.source_policy.as_ref()?; + if !source_policy.required_hosts.is_empty() { + let Some(host) = http_url_host(&artifact.uri) else { + return Some( + "recipe source policy declares required hosts but the artifact URI is not HTTP(S)" + .to_owned(), + ); + }; + if !source_policy + .required_hosts + .iter() + .any(|required| required.eq_ignore_ascii_case(&host)) + { + return Some(format!( + "artifact host `{host}` is not allowed by the recipe source policy" + )); + } + } + + match source_policy.policy.as_str() { + "direct_https_sha256" => { + if artifact.uri.starts_with("https://") { + None + } else { + Some( + "recipe source policy direct_https_sha256 requires an HTTPS artifact URI" + .to_owned(), + ) + } + } + "huggingface_public" => { + if artifact.gated.unwrap_or(false) { + Some( + "recipe source policy marks this as public Hugging Face, but the artifact is gated" + .to_owned(), + ) + } else { + huggingface_policy_uri_block_message(artifact, "huggingface_public") + } + } + "huggingface_authenticated" => { + huggingface_policy_uri_block_message(artifact, "huggingface_authenticated") + } + "manual_only" => Some( + "recipe source policy marks this artifact as manual-only; rocm-cli will not download it" + .to_owned(), + ), + other => Some(format!( + "recipe source policy `{other}` is not supported by this rocm-cli build" + )), + } +} + +fn huggingface_policy_uri_block_message( + artifact: &ModelRecipeArtifactRecord, + policy: &str, +) -> Option { + if !artifact.uri.starts_with("https://") { + return Some(format!( + "recipe source policy {policy} requires an HTTPS Hugging Face artifact URI" + )); + } + if !is_huggingface_url(&artifact.uri) { + return Some(format!( + "recipe source policy {policy} requires a Hugging Face artifact URI" + )); + } + None +} + +fn artifact_declares_huggingface_authenticated_policy( + artifact: &ModelRecipeArtifactRecord, +) -> bool { + artifact + .source_policy + .as_ref() + .is_some_and(|policy| policy.policy == "huggingface_authenticated") +} + +fn artifact_is_huggingface(artifact: &ModelRecipeArtifactRecord) -> bool { + let kind = artifact.kind.to_ascii_lowercase(); + matches!(kind.as_str(), "huggingface" | "hf" | "hugging_face") + || is_huggingface_url(&artifact.uri) +} + +fn is_huggingface_url(url: &str) -> bool { + http_url_host(url).is_some_and(|host| { + host == "huggingface.co" + || host.ends_with(".huggingface.co") + || host == "hf.co" + || host.ends_with(".hf.co") + }) +} + +fn http_url_host(url: &str) -> Option { + let rest = url + .strip_prefix("https://") + .or_else(|| url.strip_prefix("http://"))?; + let authority = rest + .split(['/', '?', '#']) + .next() + .unwrap_or_default() + .trim(); + if authority.is_empty() || authority.contains('@') { + return None; + } + let host = authority + .strip_prefix('[') + .and_then(|value| value.split_once(']').map(|(host, _)| host)) + .unwrap_or_else(|| authority.split(':').next().unwrap_or_default()) + .trim() + .trim_end_matches('.') + .to_ascii_lowercase(); + (!host.is_empty()).then_some(host) +} + +fn prefetch_blocked_value( + artifact_ref: &str, + model: &str, + artifact: ModelRecipeArtifactRecord, + cache: rocm_core::ModelArtifactCacheStatus, + reason: &str, +) -> Value { + json!({ + "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), + "artifact_ref": artifact_ref, + "model": model, + "artifact": artifact, + "cache": cache, + "status": "blocked", + "mutating": false, + "network_used": false, + "message": reason, + }) +} + +fn artifact_bytes_path_for_marker(marker_path: &Path) -> PathBuf { + marker_path.with_extension("bin") +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::test_support::temp_app_paths; + use sha2::Digest; + use std::fs; + use std::io::{Read, Write}; + use std::thread; + + /// Hex digest of a buffer. Production hashing happens incrementally inside the + /// streaming download; this exists only so tests can state an expected digest. + fn sha256_hex(bytes: &[u8]) -> String { + format!("{:x}", sha2::Sha256::digest(bytes)) + } + + fn serve_one_http_response(bytes: Vec) -> Result { + let listener = std::net::TcpListener::bind(("127.0.0.1", 0))?; + let addr = listener.local_addr()?; + thread::spawn(move || { + if let Ok((mut stream, _)) = listener.accept() { + let mut request = [0_u8; 1024]; + let _ = stream.read(&mut request); + let header = format!( + "HTTP/1.1 200 OK\r\ncontent-length: {}\r\nconnection: close\r\n\r\n", + bytes.len() + ); + let _ = stream.write_all(header.as_bytes()); + let _ = stream.write_all(&bytes); + } + }); + Ok(format!("http://{addr}/artifact.bin")) + } + + #[test] + fn sandbox_prefetch_reports_policy_required_without_network() { + let (root, paths) = temp_app_paths("sandbox-prefetch-policy"); + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#hf-main", + "Qwen/Test-1B", + ModelRecipeArtifactRecord { + artifact_id: "hf-main".to_owned(), + kind: "huggingface".to_owned(), + uri: "Qwen/Test-1B".to_owned(), + revision: Some("main".to_owned()), + sha256: Some("a".repeat(64)), + size_bytes: Some(1024), + license: Some("apache-2.0".to_owned()), + gated: Some(false), + quantization: Some("bf16".to_owned()), + engines: vec!["vllm".to_owned()], + source_policy: None, + }, + SandboxToolPolicy::default(), + ) + .expect("policy value should render"); + fs::remove_dir_all(root).ok(); + + assert_eq!( + value.get("status").and_then(Value::as_str), + Some("source_policy_required") + ); + assert_eq!(value.get("mutating").and_then(Value::as_bool), Some(false)); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("cache") + .and_then(|cache| cache.get("status")) + .and_then(Value::as_str) + .is_some_and(|status| status == "missing") + ); + } + + #[test] + fn sandbox_prefetch_cached_marker_skips_network() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-cached"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "direct-bin".to_owned(), + kind: "url".to_owned(), + uri: "https://example.invalid/should-not-be-fetched.bin".to_owned(), + revision: None, + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + let cache = model_artifact_cache_status(&paths, "Qwen/Test-1B", &artifact); + write_file_atomically(&cache.marker_path, br#"{"cached":true}"#)?; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#direct-bin", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("cached")); + assert_eq!(value.get("mutating").and_then(Value::as_bool), Some(false)); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_blocks_artifact_larger_than_approved_limit() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-byte-limit"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "direct-bin".to_owned(), + kind: "url".to_owned(), + uri: "https://example.invalid/too-large.bin".to_owned(), + revision: None, + sha256: Some("a".repeat(64)), + size_bytes: Some(2048), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#direct-bin", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("byte limit")) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_blocks_non_direct_non_huggingface_source() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-non-direct"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "torrent-bin".to_owned(), + kind: "torrent".to_owned(), + uri: "https://example.invalid/artifact.torrent".to_owned(), + revision: None, + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#torrent-bin", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("direct HTTP(S)")) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_blocks_gated_huggingface_without_source_policy() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-hf-policy"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "hf-main".to_owned(), + kind: "huggingface".to_owned(), + uri: "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors".to_owned(), + revision: Some("main".to_owned()), + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(true), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#hf-main", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("--allow-huggingface-download")) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_blocks_huggingface_source_policy_without_token() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-hf-token"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "hf-main".to_owned(), + kind: "huggingface".to_owned(), + uri: "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors".to_owned(), + revision: Some("main".to_owned()), + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(true), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#hf-main", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + allow_huggingface_download: true, + huggingface_token: None, + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("ROCM_CLI_HUGGINGFACE_TOKEN")) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_respects_manual_only_source_policy() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-manual-policy"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "manual-bin".to_owned(), + kind: "url".to_owned(), + uri: "https://example.invalid/manual.bin".to_owned(), + revision: None, + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: Some(rocm_core::ModelRecipeArtifactSourcePolicyRecord { + policy: "manual_only".to_owned(), + required_hosts: Vec::new(), + notes: vec!["requires manual license review".to_owned()], + }), + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#manual-bin", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("manual-only")) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_respects_declared_huggingface_auth_policy() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-hf-declared-auth"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "hf-main".to_owned(), + kind: "huggingface".to_owned(), + uri: "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors".to_owned(), + revision: Some("main".to_owned()), + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: Some(rocm_core::ModelRecipeArtifactSourcePolicyRecord { + policy: "huggingface_authenticated".to_owned(), + required_hosts: vec!["huggingface.co".to_owned()], + notes: Vec::new(), + }), + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#hf-main", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("--allow-huggingface-download")) + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_never_sends_huggingface_token_to_non_huggingface_url() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-hf-host"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "hf-main".to_owned(), + kind: "huggingface".to_owned(), + uri: "https://example.invalid/Qwen/Test-1B/model.safetensors".to_owned(), + revision: Some("main".to_owned()), + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(true), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#hf-main", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + allow_huggingface_download: true, + huggingface_token: Some("hf_secret_should_not_leak".to_owned()), + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("non-Hugging Face URL")) + ); + assert!( + !serde_json::to_string(&value)?.contains("hf_secret_should_not_leak"), + "prefetch report must not include authentication tokens" + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_never_sends_huggingface_token_over_plain_http() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-hf-https"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "hf-main".to_owned(), + kind: "huggingface".to_owned(), + uri: "http://huggingface.co/Qwen/Test-1B/model.safetensors".to_owned(), + revision: Some("main".to_owned()), + sha256: Some("a".repeat(64)), + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(true), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#hf-main", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + allow_huggingface_download: true, + huggingface_token: Some("hf_secret_should_not_leak".to_owned()), + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("plain HTTP")) + ); + assert!( + !serde_json::to_string(&value)?.contains("hf_secret_should_not_leak"), + "prefetch report must not include authentication tokens" + ); + Ok(()) + } + + #[test] + fn sandbox_prefetch_downloads_direct_artifact_when_approved() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-download"); + let bytes = b"tiny verified artifact".to_vec(); + let uri = serve_one_http_response(bytes.clone())?; + let sha256 = sha256_hex(&bytes); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "direct-bin".to_owned(), + kind: "url".to_owned(), + uri, + revision: None, + sha256: Some(sha256.clone()), + size_bytes: Some(bytes.len() as u64), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#direct-bin", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + let bytes_path = value + .get("bytes_path") + .and_then(Value::as_str) + .map(PathBuf::from) + .expect("bytes_path should be present"); + let marker_path = value + .get("cache") + .and_then(|cache| cache.get("marker_path")) + .and_then(Value::as_str) + .map(PathBuf::from) + .expect("marker path should be present"); + + assert_eq!( + value.get("status").and_then(Value::as_str), + Some("prefetched") + ); + assert_eq!(value.get("mutating").and_then(Value::as_bool), Some(true)); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(true) + ); + assert_eq!( + value.get("sha256").and_then(Value::as_str), + Some(sha256.as_str()) + ); + assert_eq!( + value.get("source_policy").and_then(Value::as_str), + Some("explicit_allow_artifact_download") + ); + assert_eq!(fs::read(&bytes_path)?, bytes); + assert!(marker_path.is_file()); + fs::remove_dir_all(root).ok(); + Ok(()) + } + + #[test] + fn sandbox_prefetch_blocks_approved_artifact_without_sha256() -> Result<()> { + let (root, paths) = temp_app_paths("sandbox-prefetch-no-sha"); + let artifact = ModelRecipeArtifactRecord { + artifact_id: "direct-bin".to_owned(), + kind: "url".to_owned(), + uri: "https://example.invalid/artifact.bin".to_owned(), + revision: None, + sha256: None, + size_bytes: Some(12), + license: Some("test-only".to_owned()), + gated: Some(false), + quantization: None, + engines: vec!["vllm".to_owned()], + source_policy: None, + }; + + let value = prefetch_artifact_value_with_policy( + &paths, + "qwen#direct-bin", + "Qwen/Test-1B", + artifact, + SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + }, + )?; + fs::remove_dir_all(root).ok(); + + assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); + assert_eq!( + value.get("network_used").and_then(Value::as_bool), + Some(false) + ); + assert!( + value + .get("message") + .and_then(Value::as_str) + .is_some_and(|message| message.contains("sha256")) + ); + Ok(()) + } + + #[test] + fn huggingface_url_detection_is_host_scoped() { + assert!(is_huggingface_url( + "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors" + )); + assert!(is_huggingface_url( + "https://cdn-lfs.huggingface.co/repos/example" + )); + assert!(is_huggingface_url("https://hf.co/Qwen/Test-1B")); + assert!(!is_huggingface_url( + "https://huggingface.co.evil.example/Qwen/Test-1B" + )); + assert!(!is_huggingface_url( + "https://huggingface.co@evil.example/Qwen/Test-1B" + )); + } +} diff --git a/apps/rocmd/src/lib.rs b/apps/rocmd/src/lib.rs index 4fd74a7df..37ed77163 100644 --- a/apps/rocmd/src/lib.rs +++ b/apps/rocmd/src/lib.rs @@ -5,6 +5,7 @@ #![allow(clippy::items_after_test_module)] pub mod anthropic_proxy; +mod artifact_prefetch; mod cli; mod common; mod daemon; diff --git a/apps/rocmd/src/sandbox.rs b/apps/rocmd/src/sandbox.rs index 41fd001c4..1b9bcdfdf 100644 --- a/apps/rocmd/src/sandbox.rs +++ b/apps/rocmd/src/sandbox.rs @@ -2,35 +2,25 @@ // // SPDX-License-Identifier: MIT +use crate::artifact_prefetch::prefetch_artifact_value_with_policy; use crate::common; use crate::persistence::load_managed_services; use crate::sandbox_tool::{SandboxToolArg, SandboxToolPolicy}; use crate::service::{stop_managed_service, stop_report_confirmed}; use crate::supervise::restart_managed_service; use anyhow::{Context, Result, bail}; -use rocm_core::atomic_write::write_file_atomically; use rocm_core::{ - AppPaths, AuditEventRecord, CommandCapture, ExamineSummary, ModelRecipeArtifactRecord, - append_audit_event, model_artifact_cache_status, resolve_model_recipe_artifact, - unix_time_millis, + AppPaths, AuditEventRecord, CommandCapture, ExamineSummary, append_audit_event, + resolve_model_recipe_artifact, unix_time_millis, }; use serde_json::Value; use serde_json::json; -#[cfg(test)] -use sha2::Digest; #[cfg(any(test, target_os = "linux"))] use std::fs; -use std::path::{Path, PathBuf}; #[cfg(target_os = "linux")] use std::process::{Command as ProcessCommand, Stdio}; -// Only the tests still park a thread here; the sandbox spawn now goes through -// `rocm_core::process::run_with_timeout`, which owns its own reader threads. -#[cfg(test)] -use std::thread; use std::time::Duration; -const ARTIFACT_PREFETCH_TIMEOUT: Duration = Duration::from_mins(10); - pub(crate) fn run_sandbox_runner( paths: &AppPaths, tool: SandboxToolArg, @@ -297,376 +287,6 @@ pub(crate) fn record_notification_audit( ) } -fn prefetch_artifact_value_with_policy( - paths: &AppPaths, - artifact_ref: &str, - model: &str, - artifact: ModelRecipeArtifactRecord, - policy: SandboxToolPolicy, -) -> Result { - let cache = model_artifact_cache_status(paths, model, &artifact); - if !policy.allow_artifact_download { - return Ok(json!({ - "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), - "artifact_ref": artifact_ref, - "model": model, - "artifact": artifact, - "cache": cache, - "status": "source_policy_required", - "mutating": false, - "network_used": false, - "message": "artifact prefetch requires an approved source policy before network access; no artifact bytes were downloaded", - })); - } - - if cache.marker_path.is_file() { - return Ok(json!({ - "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), - "artifact_ref": artifact_ref, - "model": model, - "artifact": artifact, - "cache": cache, - "status": "cached", - "mutating": false, - "network_used": false, - "message": "artifact cache marker already exists; no network request was made", - })); - } - - if let Some(message) = declared_source_policy_block_message(&artifact) { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - &message, - )); - } - let source_policy_requires_huggingface_auth = - artifact_declares_huggingface_authenticated_policy(&artifact); - let artifact_kind = artifact.kind.to_ascii_lowercase(); - let huggingface_artifact = artifact_is_huggingface(&artifact); - if artifact.gated.unwrap_or(false) && !huggingface_artifact { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "gated artifacts require a source-specific authentication policy before download", - )); - } - if !matches!(artifact_kind.as_str(), "url" | "http" | "https" | "direct") - && !huggingface_artifact - { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "approved live prefetch currently supports direct HTTP(S) artifacts only", - )); - } - if !artifact.uri.starts_with("https://") && !artifact.uri.starts_with("http://") { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - if huggingface_artifact { - "authenticated Hugging Face prefetch requires an HTTP(S) artifact URI in the signed recipe metadata" - } else { - "approved live prefetch requires an HTTP(S) artifact URI" - }, - )); - } - let mut request_headers = Vec::<(&str, String)>::new(); - let source_policy = if huggingface_artifact - && (artifact.gated.unwrap_or(false) - || policy.allow_huggingface_download - || source_policy_requires_huggingface_auth) - { - if !policy.allow_huggingface_download { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "Hugging Face artifacts require --allow-huggingface-download before rocm-cli may use an authentication token", - )); - } - if !artifact.uri.starts_with("https://") { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "rocm-cli will not send a Hugging Face token over plain HTTP; use an HTTPS Hugging Face artifact URI", - )); - } - if !is_huggingface_url(&artifact.uri) { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "rocm-cli will not send a Hugging Face token to a non-Hugging Face URL", - )); - } - let Some(token) = policy - .huggingface_token - .as_deref() - .map(str::trim) - .filter(|token| !token.is_empty()) - else { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "Hugging Face artifact prefetch needs ROCM_CLI_HUGGINGFACE_TOKEN, HF_TOKEN, or HUGGING_FACE_HUB_TOKEN", - )); - }; - request_headers.push(("Authorization", format!("Bearer {token}"))); - "huggingface_authenticated" - } else { - "explicit_allow_artifact_download" - }; - let Some(expected_sha256) = artifact - .sha256 - .as_deref() - .map(|value| value.trim().to_ascii_lowercase()) - .filter(|value| value.len() == 64 && value.chars().all(|ch| ch.is_ascii_hexdigit())) - else { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "approved live prefetch requires a valid sha256 in the recipe artifact metadata", - )); - }; - let Some(size_bytes) = artifact.size_bytes else { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "approved live prefetch requires size_bytes in the recipe artifact metadata", - )); - }; - let max_bytes = policy.artifact_max_bytes.unwrap_or(size_bytes); - if size_bytes > max_bytes { - return Ok(prefetch_blocked_value( - artifact_ref, - model, - artifact, - cache, - "artifact size exceeds the approved download byte limit", - )); - } - - // Streamed straight to its final path: model artifacts run to gigabytes, so - // buffering one to hash it would hold the whole file in memory. The size and - // digest from the recipe are enforced inside the download, which also gives - // this path a free-space preflight and resume on a dropped connection. - let artifact_path = artifact_bytes_path_for_marker(&cache.marker_path); - let mut headers: Vec<(&str, &str)> = vec![("User-Agent", "rocm-cli")]; - headers.extend( - request_headers - .iter() - .map(|(name, value)| (*name, value.as_str())), - ); - let outcome = rocm_core::download_file_streaming(&rocm_core::DownloadRequest { - url: &artifact.uri, - destination: &artifact_path, - timeout: ARTIFACT_PREFETCH_TIMEOUT, - headers: &headers, - max_bytes: Some(max_bytes), - expected_len: Some(size_bytes), - expected_sha256: Some(&expected_sha256), - }) - .with_context(|| format!("failed to prefetch artifact `{artifact_ref}`"))?; - let actual_sha256 = outcome.sha256; - write_file_atomically( - &cache.marker_path, - &serde_json::to_vec_pretty(&json!({ - "artifact_ref": artifact_ref, - "model": model, - "artifact": artifact, - "bytes_path": artifact_path, - "size_bytes": size_bytes, - "sha256": actual_sha256, - "prefetched_at_unix_ms": unix_time_millis(), - "source_policy": source_policy, - })) - .context("failed to serialize artifact cache marker")?, - )?; - let cache = model_artifact_cache_status(paths, model, &artifact); - Ok(json!({ - "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), - "artifact_ref": artifact_ref, - "model": model, - "artifact": artifact, - "cache": cache, - "status": "prefetched", - "mutating": true, - "network_used": true, - "bytes_path": artifact_path, - "size_bytes": size_bytes, - "sha256": actual_sha256, - "source_policy": source_policy, - "message": "artifact downloaded and verified with recipe sha256", - })) -} - -fn declared_source_policy_block_message(artifact: &ModelRecipeArtifactRecord) -> Option { - let source_policy = artifact.source_policy.as_ref()?; - if !source_policy.required_hosts.is_empty() { - let Some(host) = http_url_host(&artifact.uri) else { - return Some( - "recipe source policy declares required hosts but the artifact URI is not HTTP(S)" - .to_owned(), - ); - }; - if !source_policy - .required_hosts - .iter() - .any(|required| required.eq_ignore_ascii_case(&host)) - { - return Some(format!( - "artifact host `{host}` is not allowed by the recipe source policy" - )); - } - } - - match source_policy.policy.as_str() { - "direct_https_sha256" => { - if artifact.uri.starts_with("https://") { - None - } else { - Some( - "recipe source policy direct_https_sha256 requires an HTTPS artifact URI" - .to_owned(), - ) - } - } - "huggingface_public" => { - if artifact.gated.unwrap_or(false) { - Some( - "recipe source policy marks this as public Hugging Face, but the artifact is gated" - .to_owned(), - ) - } else { - huggingface_policy_uri_block_message(artifact, "huggingface_public") - } - } - "huggingface_authenticated" => { - huggingface_policy_uri_block_message(artifact, "huggingface_authenticated") - } - "manual_only" => Some( - "recipe source policy marks this artifact as manual-only; rocm-cli will not download it" - .to_owned(), - ), - other => Some(format!( - "recipe source policy `{other}` is not supported by this rocm-cli build" - )), - } -} - -fn huggingface_policy_uri_block_message( - artifact: &ModelRecipeArtifactRecord, - policy: &str, -) -> Option { - if !artifact.uri.starts_with("https://") { - return Some(format!( - "recipe source policy {policy} requires an HTTPS Hugging Face artifact URI" - )); - } - if !is_huggingface_url(&artifact.uri) { - return Some(format!( - "recipe source policy {policy} requires a Hugging Face artifact URI" - )); - } - None -} - -fn artifact_declares_huggingface_authenticated_policy( - artifact: &ModelRecipeArtifactRecord, -) -> bool { - artifact - .source_policy - .as_ref() - .is_some_and(|policy| policy.policy == "huggingface_authenticated") -} - -fn artifact_is_huggingface(artifact: &ModelRecipeArtifactRecord) -> bool { - let kind = artifact.kind.to_ascii_lowercase(); - matches!(kind.as_str(), "huggingface" | "hf" | "hugging_face") - || is_huggingface_url(&artifact.uri) -} - -fn is_huggingface_url(url: &str) -> bool { - http_url_host(url).is_some_and(|host| { - host == "huggingface.co" - || host.ends_with(".huggingface.co") - || host == "hf.co" - || host.ends_with(".hf.co") - }) -} - -fn http_url_host(url: &str) -> Option { - let rest = url - .strip_prefix("https://") - .or_else(|| url.strip_prefix("http://"))?; - let authority = rest - .split(['/', '?', '#']) - .next() - .unwrap_or_default() - .trim(); - if authority.is_empty() || authority.contains('@') { - return None; - } - let host = authority - .strip_prefix('[') - .and_then(|value| value.split_once(']').map(|(host, _)| host)) - .unwrap_or_else(|| authority.split(':').next().unwrap_or_default()) - .trim() - .trim_end_matches('.') - .to_ascii_lowercase(); - (!host.is_empty()).then_some(host) -} - -fn prefetch_blocked_value( - artifact_ref: &str, - model: &str, - artifact: ModelRecipeArtifactRecord, - cache: rocm_core::ModelArtifactCacheStatus, - reason: &str, -) -> Value { - json!({ - "tool": SandboxToolArg::PrefetchArtifact.as_cli_value(), - "artifact_ref": artifact_ref, - "model": model, - "artifact": artifact, - "cache": cache, - "status": "blocked", - "mutating": false, - "network_used": false, - "message": reason, - }) -} - -fn artifact_bytes_path_for_marker(marker_path: &Path) -> PathBuf { - marker_path.with_extension("bin") -} - -/// Hex digest of a buffer. Production hashing happens incrementally inside the -/// streaming download; this exists only so tests can state an expected digest. -#[cfg(test)] -fn sha256_hex(bytes: &[u8]) -> String { - format!("{:x}", sha2::Sha256::digest(bytes)) -} - pub(crate) fn sandbox_check_updates_value(output: CommandCapture) -> Value { let status = update_check_status(&output); let update_available = @@ -839,29 +459,12 @@ mod tests { use super::*; use crate::persistence::load_service_record; use crate::test_support::{ - assert_unconfirmed_stop_kept_the_service, seed_keyed_service, stop_with_outcome, - temp_app_paths, + RecipeIndexEnv, assert_unconfirmed_stop_kept_the_service, seed_keyed_service, + stop_with_outcome, temp_app_paths, }; use rocm_core::ManagedServiceRecord; - use std::io::{Read, Write}; - - fn serve_one_http_response(bytes: Vec) -> Result { - let listener = std::net::TcpListener::bind(("127.0.0.1", 0))?; - let addr = listener.local_addr()?; - thread::spawn(move || { - if let Ok((mut stream, _)) = listener.accept() { - let mut request = [0_u8; 1024]; - let _ = stream.read(&mut request); - let header = format!( - "HTTP/1.1 200 OK\r\ncontent-length: {}\r\nconnection: close\r\n\r\n", - bytes.len() - ); - let _ = stream.write_all(header.as_bytes()); - let _ = stream.write_all(&bytes); - } - }); - Ok(format!("http://{addr}/artifact.bin")) - } + #[cfg(target_os = "linux")] + use std::path::Path; #[test] fn sandbox_check_updates_value_is_read_only_and_preserves_output() { @@ -1327,52 +930,12 @@ mod tests { ); } - #[test] - fn sandbox_prefetch_reports_policy_required_without_network() { - let (root, paths) = temp_app_paths("sandbox-prefetch-policy"); - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#hf-main", - "Qwen/Test-1B", - ModelRecipeArtifactRecord { - artifact_id: "hf-main".to_owned(), - kind: "huggingface".to_owned(), - uri: "Qwen/Test-1B".to_owned(), - revision: Some("main".to_owned()), - sha256: Some("a".repeat(64)), - size_bytes: Some(1024), - license: Some("apache-2.0".to_owned()), - gated: Some(false), - quantization: Some("bf16".to_owned()), - engines: vec!["vllm".to_owned()], - source_policy: None, - }, - SandboxToolPolicy::default(), - ) - .expect("policy value should render"); - fs::remove_dir_all(root).ok(); - - assert_eq!( - value.get("status").and_then(Value::as_str), - Some("source_policy_required") - ); - assert_eq!(value.get("mutating").and_then(Value::as_bool), Some(false)); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("cache") - .and_then(|cache| cache.get("status")) - .and_then(Value::as_str) - .is_some_and(|status| status == "missing") - ); - } - #[test] fn sandbox_prefetch_unknown_artifact_ref_errors() { let (root, paths) = temp_app_paths("sandbox-prefetch-unknown-ref"); + // The registry is read from the process environment, which the dispatch + // test below sets, so resolve against the built-in catalog under the guard. + let _env = RecipeIndexEnv::builtin_catalog(); let error = run_sandbox_tool( &paths, SandboxToolArg::PrefetchArtifact, @@ -1387,545 +950,111 @@ mod tests { assert!(error.to_string().contains("was not found"), "{error:#}"); } + /// Drives `prefetch_artifact` through `run_sandbox_tool` with an artifact + /// reference that the recipe registry resolves (the built-in catalog lists no + /// artifacts, so a signed recipe index supplies one). The callee's own tests + /// in `artifact_prefetch.rs` call it directly, and the two tests above stop + /// before it, so this is the one test that covers the dispatch seam: the + /// reference resolves to its recipe's canonical model id, and the caller's + /// policy reaches the policy decision. #[test] - fn sandbox_prefetch_cached_marker_skips_network() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-cached"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "direct-bin".to_owned(), - kind: "url".to_owned(), - uri: "https://example.invalid/should-not-be-fetched.bin".to_owned(), - revision: None, - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - let cache = model_artifact_cache_status(&paths, "Qwen/Test-1B", &artifact); - write_file_atomically(&cache.marker_path, br#"{"cached":true}"#)?; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#direct-bin", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("cached")); - assert_eq!(value.get("mutating").and_then(Value::as_bool), Some(false)); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_blocks_artifact_larger_than_approved_limit() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-byte-limit"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "direct-bin".to_owned(), - kind: "url".to_owned(), - uri: "https://example.invalid/too-large.bin".to_owned(), - revision: None, - sha256: Some("a".repeat(64)), - size_bytes: Some(2048), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#direct-bin", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("byte limit")) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_blocks_non_direct_non_huggingface_source() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-non-direct"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "torrent-bin".to_owned(), - kind: "torrent".to_owned(), - uri: "https://example.invalid/artifact.torrent".to_owned(), - revision: None, - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#torrent-bin", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("direct HTTP(S)")) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_blocks_gated_huggingface_without_source_policy() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-hf-policy"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "hf-main".to_owned(), - kind: "huggingface".to_owned(), - uri: "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors".to_owned(), - revision: Some("main".to_owned()), - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(true), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#hf-main", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("--allow-huggingface-download")) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_blocks_huggingface_source_policy_without_token() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-hf-token"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "hf-main".to_owned(), - kind: "huggingface".to_owned(), - uri: "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors".to_owned(), - revision: Some("main".to_owned()), - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(true), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#hf-main", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - allow_huggingface_download: true, - huggingface_token: None, - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("ROCM_CLI_HUGGINGFACE_TOKEN")) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_respects_manual_only_source_policy() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-manual-policy"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "manual-bin".to_owned(), - kind: "url".to_owned(), - uri: "https://example.invalid/manual.bin".to_owned(), - revision: None, - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: Some(rocm_core::ModelRecipeArtifactSourcePolicyRecord { - policy: "manual_only".to_owned(), - required_hosts: Vec::new(), - notes: vec!["requires manual license review".to_owned()], - }), - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#manual-bin", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("manual-only")) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_respects_declared_huggingface_auth_policy() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-hf-declared-auth"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "hf-main".to_owned(), - kind: "huggingface".to_owned(), - uri: "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors".to_owned(), - revision: Some("main".to_owned()), - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: Some(rocm_core::ModelRecipeArtifactSourcePolicyRecord { - policy: "huggingface_authenticated".to_owned(), - required_hosts: vec!["huggingface.co".to_owned()], - notes: Vec::new(), - }), - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#hf-main", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("--allow-huggingface-download")) - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_never_sends_huggingface_token_to_non_huggingface_url() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-hf-host"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "hf-main".to_owned(), - kind: "huggingface".to_owned(), - uri: "https://example.invalid/Qwen/Test-1B/model.safetensors".to_owned(), - revision: Some("main".to_owned()), - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(true), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#hf-main", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - allow_huggingface_download: true, - huggingface_token: Some("hf_secret_should_not_leak".to_owned()), - }, + fn sandbox_prefetch_resolves_artifact_and_applies_policy_through_run_sandbox_tool() -> Result<()> + { + let (root, paths) = temp_app_paths("sandbox-prefetch-dispatch"); + paths.ensure()?; + let index = root.join("recipes.json"); + let signature = root.join("recipes.json.sig"); + let public_key = root.join("recipes-public.pem"); + let document = json!({ + "schema_version": 1, + "source": "sandbox-prefetch-dispatch-test", + "recipes": [{ + "canonical_model_id": "Dispatch/Test-1B", + "aliases": [], + "task": "chat", + "source": "signed_recipe_index", + "revision": "main", + "loader": "transformers", + "trust_remote_code": false, + "dtype": "float16", + "device_policy": "gpu_required", + "artifacts": [{ + "artifact_id": "direct-bin", + "kind": "url", + "uri": "https://example.invalid/should-not-be-fetched.bin", + "revision": null, + "sha256": "a".repeat(64), + "size_bytes": 12, + "license": "test-only", + "gated": false, + "quantization": null, + "engines": ["vllm"] + }], + "engine_recipes": [], + "manual_alternatives": [], + "featured": false, + "chat_template_mode": "auto", + "preferred_engines": ["vllm"], + "warnings": [] + }] + }); + let payload = serde_json::to_vec_pretty(&document)?; + let (private_pem, public_pem) = rocm_core::generate_rsa_signing_keypair()?; + fs::write(&index, &payload)?; + fs::write( + &signature, + rocm_core::sign_rsa_pkcs1_sha256_signature(&private_pem, &payload)?, )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("non-Hugging Face URL")) - ); - assert!( - !serde_json::to_string(&value)?.contains("hf_secret_should_not_leak"), - "prefetch report must not include authentication tokens" - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_never_sends_huggingface_token_over_plain_http() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-hf-https"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "hf-main".to_owned(), - kind: "huggingface".to_owned(), - uri: "http://huggingface.co/Qwen/Test-1B/model.safetensors".to_owned(), - revision: Some("main".to_owned()), - sha256: Some("a".repeat(64)), - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(true), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, + fs::write(&public_key, public_pem)?; + + let run = |policy: SandboxToolPolicy| { + let _env = RecipeIndexEnv::signed_index(&index, &signature, &public_key); + run_sandbox_tool( + &paths, + SandboxToolArg::PrefetchArtifact, + None, + Some("Dispatch/Test-1B#direct-bin".to_owned()), + None, + policy, + ) }; - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#hf-main", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - allow_huggingface_download: true, - huggingface_token: Some("hf_secret_should_not_leak".to_owned()), - }, - )?; - fs::remove_dir_all(root).ok(); - - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); + // No download approval: the reference resolves, the answer names the + // recipe's canonical model, and nothing is fetched or cached. + let blocked = run(SandboxToolPolicy::default())?; + let marker_path = std::path::PathBuf::from( + blocked["cache"]["marker_path"] + .as_str() + .expect("the report names the cache marker path"), + ); + assert_eq!(blocked["status"], "source_policy_required", "{blocked}"); + assert_eq!(blocked["model"], "Dispatch/Test-1B", "{blocked}"); + assert_eq!(blocked["artifact_ref"], "Dispatch/Test-1B#direct-bin"); + assert_eq!(blocked["artifact"]["artifact_id"], "direct-bin"); + assert_eq!(blocked["network_used"], false); + assert_eq!(blocked["mutating"], false); assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("plain HTTP")) - ); - assert!( - !serde_json::to_string(&value)?.contains("hf_secret_should_not_leak"), - "prefetch report must not include authentication tokens" - ); - Ok(()) - } - - #[test] - fn sandbox_prefetch_downloads_direct_artifact_when_approved() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-download"); - let bytes = b"tiny verified artifact".to_vec(); - let uri = serve_one_http_response(bytes.clone())?; - let sha256 = sha256_hex(&bytes); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "direct-bin".to_owned(), - kind: "url".to_owned(), - uri, - revision: None, - sha256: Some(sha256.clone()), - size_bytes: Some(bytes.len() as u64), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#direct-bin", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; - let bytes_path = value - .get("bytes_path") - .and_then(Value::as_str) - .map(PathBuf::from) - .expect("bytes_path should be present"); - let marker_path = value - .get("cache") - .and_then(|cache| cache.get("marker_path")) - .and_then(Value::as_str) - .map(PathBuf::from) - .expect("marker path should be present"); - - assert_eq!( - value.get("status").and_then(Value::as_str), - Some("prefetched") - ); - assert_eq!(value.get("mutating").and_then(Value::as_bool), Some(true)); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(true) - ); - assert_eq!( - value.get("sha256").and_then(Value::as_str), - Some(sha256.as_str()) - ); - assert_eq!( - value.get("source_policy").and_then(Value::as_str), - Some("explicit_allow_artifact_download") - ); - assert_eq!(fs::read(&bytes_path)?, bytes); - assert!(marker_path.is_file()); - fs::remove_dir_all(root).ok(); - Ok(()) - } - - #[test] - fn sandbox_prefetch_blocks_approved_artifact_without_sha256() -> Result<()> { - let (root, paths) = temp_app_paths("sandbox-prefetch-no-sha"); - let artifact = ModelRecipeArtifactRecord { - artifact_id: "direct-bin".to_owned(), - kind: "url".to_owned(), - uri: "https://example.invalid/artifact.bin".to_owned(), - revision: None, - sha256: None, - size_bytes: Some(12), - license: Some("test-only".to_owned()), - gated: Some(false), - quantization: None, - engines: vec!["vllm".to_owned()], - source_policy: None, - }; - - let value = prefetch_artifact_value_with_policy( - &paths, - "qwen#direct-bin", - "Qwen/Test-1B", - artifact, - SandboxToolPolicy { - allow_artifact_download: true, - artifact_max_bytes: Some(1024), - ..SandboxToolPolicy::default() - }, - )?; + !marker_path.exists(), + "a blocked prefetch must not write the cache marker" + ); + + // Download approved and a marker already present: the policy the caller + // passed is the one that is applied, so the answer is `cached`, still + // without a network request. + rocm_core::atomic_write::write_file_atomically(&marker_path, br#"{"cached":true}"#)?; + let cached = run(SandboxToolPolicy { + allow_artifact_download: true, + artifact_max_bytes: Some(1024), + ..SandboxToolPolicy::default() + })?; fs::remove_dir_all(root).ok(); - assert_eq!(value.get("status").and_then(Value::as_str), Some("blocked")); - assert_eq!( - value.get("network_used").and_then(Value::as_bool), - Some(false) - ); - assert!( - value - .get("message") - .and_then(Value::as_str) - .is_some_and(|message| message.contains("sha256")) - ); + assert_eq!(cached["status"], "cached", "{cached}"); + assert_eq!(cached["model"], "Dispatch/Test-1B", "{cached}"); + assert_eq!(cached["network_used"], false); Ok(()) } - #[test] - fn huggingface_url_detection_is_host_scoped() { - assert!(is_huggingface_url( - "https://huggingface.co/Qwen/Test-1B/resolve/main/model.safetensors" - )); - assert!(is_huggingface_url( - "https://cdn-lfs.huggingface.co/repos/example" - )); - assert!(is_huggingface_url("https://hf.co/Qwen/Test-1B")); - assert!(!is_huggingface_url( - "https://huggingface.co.evil.example/Qwen/Test-1B" - )); - assert!(!is_huggingface_url( - "https://huggingface.co@evil.example/Qwen/Test-1B" - )); - } - #[test] fn sandbox_tool_stop_server_updates_manifest_and_skips_current_pid() -> Result<()> { let (root, paths) = temp_app_paths("sandbox-stop-current-pid"); diff --git a/apps/rocmd/src/test_support.rs b/apps/rocmd/src/test_support.rs index 876adc522..254706a97 100644 --- a/apps/rocmd/src/test_support.rs +++ b/apps/rocmd/src/test_support.rs @@ -15,8 +15,9 @@ use rocm_core::{ AppPaths, AutomationRuntimeState, ManagedServiceRecord, WatcherMode, WatcherRuntimeSnapshot, unix_time_millis, }; +use std::ffi::OsString; use std::fs; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicU64, Ordering}; pub(crate) fn temp_app_paths(name: &str) -> (PathBuf, AppPaths) { @@ -314,3 +315,84 @@ pub(crate) fn write_executable_script( file.sync_all()?; Ok(()) } + +const RECIPE_INDEX_PATH_ENV: &str = "ROCM_CLI_MODEL_RECIPE_INDEX_PATH"; +const RECIPE_INDEX_SIGNATURE_ENV: &str = "ROCM_CLI_MODEL_RECIPE_INDEX_SIGNATURE_PATH"; +const RECIPE_INDEX_PUBLIC_KEY_ENV: &str = "ROCM_CLI_MODEL_RECIPE_INDEX_PUBLIC_KEY_PATH"; +const REQUIRE_SIGNED_RECIPE_INDEX_ENV: &str = "ROCM_CLI_REQUIRE_MODEL_RECIPE_SIGNATURE"; + +static RECIPE_INDEX_ENV: std::sync::Mutex<()> = std::sync::Mutex::new(()); + +/// Holds the process-wide model recipe registry configuration for one test. +/// +/// `rocm_core::resolve_model_recipe_artifact` (reached through +/// `run_sandbox_tool`'s `prefetch_artifact` arm) reads the recipe-index +/// environment variables on every call, and the environment is shared by every +/// test thread. A test that resolves a recipe reference, or sets those +/// variables, therefore takes this guard first: it serialises them against each +/// other, so a resolver never sees the index path set without its public key, +/// or the variables mid-restore. The previous values come back on drop. +pub(crate) struct RecipeIndexEnv { + previous: Vec<(&'static str, Option)>, + _serialised: std::sync::MutexGuard<'static, ()>, +} + +impl RecipeIndexEnv { + /// The built-in catalog: no index configured and no signature required, whatever + /// the developer's shell exports. + pub(crate) fn builtin_catalog() -> Self { + Self::apply(&[ + (RECIPE_INDEX_PATH_ENV, None), + (RECIPE_INDEX_SIGNATURE_ENV, None), + (RECIPE_INDEX_PUBLIC_KEY_ENV, None), + (REQUIRE_SIGNED_RECIPE_INDEX_ENV, None), + ]) + } + + /// A signed recipe index, set as the registry for as long as the guard lives. + pub(crate) fn signed_index(index: &Path, signature: &Path, public_key: &Path) -> Self { + Self::apply(&[ + (RECIPE_INDEX_PATH_ENV, Some(index)), + (RECIPE_INDEX_SIGNATURE_ENV, Some(signature)), + (RECIPE_INDEX_PUBLIC_KEY_ENV, Some(public_key)), + (REQUIRE_SIGNED_RECIPE_INDEX_ENV, None), + ]) + } + + #[allow(unsafe_code)] // std::env::set_var/remove_var are unsafe in edition 2024 + fn apply(vars: &[(&'static str, Option<&Path>)]) -> Self { + let serialised = RECIPE_INDEX_ENV + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner); + let previous = vars + .iter() + .map(|(key, _)| (*key, std::env::var_os(key))) + .collect(); + for (key, value) in vars { + unsafe { + match value { + Some(value) => std::env::set_var(key, value), + None => std::env::remove_var(key), + } + } + } + Self { + previous, + _serialised: serialised, + } + } +} + +impl Drop for RecipeIndexEnv { + #[allow(unsafe_code)] // std::env::set_var/remove_var are unsafe in edition 2024 + fn drop(&mut self) { + for (key, value) in &self.previous { + unsafe { + match value { + Some(value) => std::env::set_var(key, value), + None => std::env::remove_var(key), + } + } + } + } +} diff --git a/docs/architecture.md b/docs/architecture.md index 76461c8b1..12b06a75c 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -154,6 +154,7 @@ Modularized (ROCMAI-83, Phase 5 of ROCMAI-27's sequencing): - [`cli.rs`](../apps/rocmd/src/cli.rs) — the `Cli`/`Command` clap definitions, the bridge-snapshot diagnostic printer, the pretty-JSON printer for the subcommands that print a result (`print_json`), and the top-level dispatch in `run_cli`/`run_bin_cli`/`run_from_args`. - [`anthropic_proxy.rs`](../apps/rocmd/src/anthropic_proxy.rs) — the Anthropic-compatible `/v1/messages` front for a served model: `spawn_alongside_engine` waits for the engine to list its model, then binds a translation proxy, even for an engine that has its own `/v1/messages` route because that route only accepts the served model's name (`anthropic_to_openai`, `openai_to_anthropic`, `StreamTranslator` for `stream: true` requests, with tool use translated both ways for non-streaming ones, an axum router over `/v1/messages` and `/v1/models`) on a port of its own, publishing the result in the engine state file for the service record to adopt. It is a library module, not a daemon task: `rocm`'s `run_builtin_engine_serve_http` in [`engine_ipc.rs`](../apps/rocm/src/engine_ipc.rs) starts it on a thread of the process that owns the engine, so it lives and dies with the service. +- [`artifact_prefetch.rs`](../apps/rocmd/src/artifact_prefetch.rs) — the `prefetch_artifact` sandbox tool's artifact download: `prefetch_artifact_value_with_policy` decides from the recipe artifact's source policy and the approved `SandboxToolPolicy` whether the artifact may be fetched at all, and answers `source_policy_required`, `cached` or `blocked` without touching the network when it may not (no download approval, a cache marker already present, a manual-only or unsupported source policy, a gated or non-HTTP(S) source, a Hugging Face token that would leave HTTPS or a Hugging Face host, a missing sha256 or size, a size over the approved byte limit). Otherwise it streams the download through [`download.rs`](../crates/rocm-core/src/download.rs)'s `download_file_streaming` under `ARTIFACT_PREFETCH_TIMEOUT`, which enforces the recipe's size and sha256, and records the cache marker with [`atomic_write.rs`](../crates/rocm-core/src/atomic_write.rs)'s `write_file_atomically`. It also holds the host-scoped URL checks (`is_huggingface_url`, `http_url_host`). [`sandbox.rs`](../apps/rocmd/src/sandbox.rs) is its only caller and this module does not import it back; it takes `SandboxToolArg` and `SandboxToolPolicy` from [`sandbox_tool.rs`](../apps/rocmd/src/sandbox_tool.rs). Its own `mod tests` holds the prefetch policy and download tests, the host-scoping test and the one-shot local HTTP server they download from, reaching `temp_app_paths` through `crate::test_support`. - [`common.rs`](../apps/rocmd/src/common.rs) — helpers the other modules call into: the read-only `rocm update` message, the `rocm`-subprocess capture wrapper (`run_rocm_capture_for_paths`) the sandbox and MCP clusters both call (it only chooses the child's `ROCM_CLI_*_DIR` environment; `CommandCapture` and the spawn itself live in [`command_capture.rs`](../crates/rocm-core/src/command_capture.rs), shared with `rocm`'s chat tool bridge, and the bounded-execution half in [`process/bounded.rs`](../crates/rocm-core/src/process/bounded.rs)), and small arg/JSON-payload utilities (`payload_string`, for example, is read by the [`watchers/cache_warm.rs`](../apps/rocmd/src/watchers/cache_warm.rs), [`watchers/driver_upgrade.rs`](../apps/rocmd/src/watchers/driver_upgrade.rs) and [`watchers/gpu_thermal_protect.rs`](../apps/rocmd/src/watchers/gpu_thermal_protect.rs) handlers and by [`webhook.rs`](../apps/rocmd/src/webhook.rs)). - [`daemon.rs`](../apps/rocmd/src/daemon.rs) — the foreground `rocmd run` loop (`run_daemon`: evaluate the watchers each tick, dispatch local webhook events, write the runtime state, stop on a signal; with no watcher enabled it exits by itself, deciding that under the autostart lock and publishing "stopped" and clearing its spawn claim before releasing it — `record_daemon_stop`, `release_own_autostart_claim`) and `rocmd status` (`print_status`), with the watcher tick interval, which `LoopHooks` makes injectable for tests. It uses [`watchers.rs`](../apps/rocmd/src/watchers.rs), [`webhook.rs`](../apps/rocmd/src/webhook.rs) and [`persistence.rs`](../apps/rocmd/src/persistence.rs); [`cli.rs`](../apps/rocmd/src/cli.rs) calls it. - [`daemon_host.rs`](../apps/rocmd/src/daemon_host.rs) — which binary a daemon spawn runs. The daemon runs as the `rocmd` binary or embedded in `rocm` (`rocm daemon`, which enters through `run_from_args` and marks the process), and `supervise` is a command of `rocmd` alone. The hidden engine commands (`__engine-serve-http`, `__engine-stdio`) are `rocm`'s alone. `DaemonHost` holds the one executable path every choice and sibling lookup is made from (`rocm_core::sibling_binary_path_from`). `rocmd_binary` returns the current executable when the daemon is `rocmd` and the `rocmd` installed beside `rocm` when it is embedded; `rocm_binary` is the mirror, the current executable when embedded and the `rocm` installed beside `rocmd` when not. Each names what was missing when there is none. `detect` returns the real host. Under test it returns the test binary for both commands and looks for no sibling, and the two stand-in hosts (`test_binary_standing_in_for_rocm` and `test_binary_standing_in_for_rocmd`) name only the binary they stand in for and fail when asked for the other, so none of them can run a `rocm` or `rocmd` built beside the test binary; a test that needs another binary passes the code it exercises a host of its own. A leaf: it imports no other crate module. @@ -163,7 +164,7 @@ Modularized (ROCMAI-83, Phase 5 of ROCMAI-27's sequencing): - [`mcp.rs`](../apps/rocmd/src/mcp.rs) — the MCP stdio server, tool schema table, tool dispatch, and the result-shaping helpers behind the MCP tools (the `rocm` subprocess itself is spawned through [`common.rs`](../apps/rocmd/src/common.rs), via `run_rocm_capture` here). The argument handling the dispatch calls is in [`mcp_argv.rs`](../apps/rocmd/src/mcp_argv.rs). - [`mcp_argv.rs`](../apps/rocmd/src/mcp_argv.rs) — the argument handling for the MCP tools that spawn `rocm`: `normalized_rocm_command_args` and the read-only allowlist `ensure_rocm_command_is_read_only` behind the `rocm_command` tool, and the argv builders for `install_sdk`, `install_engine`, `launch_server` and `watcher_enable` (`build_install_sdk_args`, `build_install_engine_args`, `build_launch_server_args`, `build_watcher_enable_args`, with the public-bind and system-prefix acknowledgement checks they apply). Functions over the tool's JSON arguments: it uses no other module of the crate, spawns nothing and writes nothing, and [`mcp.rs`](../apps/rocmd/src/mcp.rs) is its only caller. Its own `mod tests` holds the allowlist and argv-builder tests. - [`persistence.rs`](../apps/rocmd/src/persistence.rs) — `record_event`/`load_managed_services`/`load_service_record`, the automation-event/audit-log and managed-service-registry I/O shared across the daemon's sandbox, MCP, service-lifecycle, and watcher code. -- [`sandbox.rs`](../apps/rocmd/src/sandbox.rs) — bubblewrap/native sandbox execution, artifact prefetch policy gating, and the sandbox-tool result shaping for `check_updates`/`driver_plan`/`stop_server`. +- [`sandbox.rs`](../apps/rocmd/src/sandbox.rs) — bubblewrap/native sandbox execution, the sandbox-tool dispatch (`run_sandbox_tool`), and the sandbox-tool result shaping for `check_updates`/`driver_plan`/`stop_server`. For `prefetch_artifact` it resolves the artifact reference against the model recipe registry and hands the artifact to [`artifact_prefetch.rs`](../apps/rocmd/src/artifact_prefetch.rs), which does not import it back. The tests that drive that tool through `run_sandbox_tool` stay in this module's `mod tests`: a missing and an unknown artifact reference, and one that resolves a reference through a signed recipe index built in the test and checks both the recipe's model id and that the caller's policy reaches the download decision. - [`sandbox_tool.rs`](../apps/rocmd/src/sandbox_tool.rs) — the sandbox-tool vocabulary: the `SandboxToolArg` value enum naming each sandbox tool, and `SandboxToolPolicy`, the artifact-download policy built from the `--allow-*` flags (including the Hugging Face token read from the environment when authenticated downloads are allowed). It uses no other module of the crate, so modules that need the vocabulary do not import [`cli.rs`](../apps/rocmd/src/cli.rs). - [`service.rs`](../apps/rocmd/src/service.rs) — managed-service stop: the engine `Stop`, the manifest and endpoint-key writes, and the stop report (`stop_managed_service`, `stop_report_confirmed`) that the sandbox and MCP tools read. The termination of the recorded processes is the routine shared with `rocm services stop`, in [`proc_lifecycle.rs`](../crates/rocm-core/src/proc_lifecycle.rs); this module passes it `terminate_recorded_pid` and keeps no copy of it or of its grace constant. - [`supervise.rs`](../apps/rocmd/src/supervise.rs) — launching and re-launching a managed service's supervisor: `supervise_service` (the `rocmd supervise` entry point: launch the engine child, record its identity, wait for readiness, record the exit) with the serve-log startup-phase polling and readiness wait it uses (its engine child runs on the `rocm` binary that `DaemonHost::rocm_binary` finds, before the first write, since `rocmd` has no engine commands, and it polls readiness through the same host), and `restart_managed_service` (the recovery respawn: re-launch `rocmd supervise` detached for a service and record the new supervisor; the binary that runs `supervise` comes from [`daemon_host.rs`](../apps/rocmd/src/daemon_host.rs), and a restart that cannot find it fails before it writes the record). Writes after a supervisor's first go through the private `update_supervised_record`, which re-reads the record and leaves a stop marker or another supervisor's record alone. Its production code uses only [`common.rs`](../apps/rocmd/src/common.rs), [`daemon_host.rs`](../apps/rocmd/src/daemon_host.rs), [`engine_ipc.rs`](../apps/rocmd/src/engine_ipc.rs), [`persistence.rs`](../apps/rocmd/src/persistence.rs) and `stop_requested` from [`watchers.rs`](../apps/rocmd/src/watchers.rs). [`cli.rs`](../apps/rocmd/src/cli.rs) calls `supervise_service`; [`watchers/server_recover.rs`](../apps/rocmd/src/watchers/server_recover.rs) (the server-recover handler) and [`sandbox.rs`](../apps/rocmd/src/sandbox.rs) (the `restart_server` tool) call `restart_managed_service`.