use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use std::time::{Duration, Instant};
use anyhow::{anyhow, Context, Result};
use serde::Deserialize;
use crate::fmt::human_size;
#[cfg(any(target_os = "linux", target_os = "windows"))]
use crate::hostgpu::HostGpu;
const REPO_API: &str = "https://api.github.com/repos/ggml-org/llama.cpp";
pub fn default_release() -> &'static str {
include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/LLAMA_CPP_RELEASE")).trim()
}
#[derive(Debug, Deserialize)]
struct Release {
tag_name: String,
assets: Vec<Asset>,
}
#[derive(Debug, Clone, Deserialize)]
struct Asset {
name: String,
browser_download_url: String,
}
pub(crate) fn http_client() -> Result<reqwest::blocking::Client> {
reqwest::blocking::Client::builder()
.timeout(Duration::from_secs(60 * 30))
.build()
.context("build http client")
}
fn http_get(
client: &reqwest::blocking::Client,
url: &str,
what: &str,
) -> Result<reqwest::blocking::Response> {
let resp = client
.get(url)
.header("user-agent", "llmman")
.send()
.with_context(|| format!("query {url}"))?;
if !resp.status().is_success() {
anyhow::bail!("{what} {url} returned {}", resp.status());
}
Ok(resp)
}
fn fetch_release(client: &reqwest::blocking::Client, version: Option<&str>) -> Result<Release> {
let url = match version {
Some(v) => format!("{REPO_API}/releases/tags/{v}"),
None => format!("{REPO_API}/releases/latest"),
};
http_get(client, &url, "GitHub API")?
.json::<Release>()
.with_context(|| format!("parse release metadata from {url}"))
}
const NIGHTLY_POINTER: &str = "nightly-tag.txt";
fn pointer_asset(release: &Release) -> Option<&Asset> {
if release.assets.len() != 1 {
return None;
}
release.assets.iter().find(|a| a.name == NIGHTLY_POINTER)
}
fn parse_pointer_tag(content: &str) -> Option<&str> {
let trimmed = content.trim();
(!trimmed.is_empty()).then_some(trimmed)
}
fn resolve_release(client: &reqwest::blocking::Client, version: Option<&str>) -> Result<Release> {
resolve_release_with(
|v| fetch_release(client, v),
|url| fetch_text(client, url),
version,
)
}
fn resolve_release_with(
fetch_release_fn: impl Fn(Option<&str>) -> Result<Release>,
fetch_text_fn: impl Fn(&str) -> Result<String>,
version: Option<&str>,
) -> Result<Release> {
let release = fetch_release_fn(version)?;
let Some(pointer) = pointer_asset(&release) else {
return Ok(release);
};
let pointer_tag = &release.tag_name;
let content = fetch_text_fn(&pointer.browser_download_url).with_context(|| {
format!("download {NIGHTLY_POINTER} from pointer release {pointer_tag}")
})?;
let tag = parse_pointer_tag(&content)
.ok_or_else(|| anyhow!("pointer release {pointer_tag}'s {NIGHTLY_POINTER} is empty"))?;
let dereferenced = fetch_release_fn(Some(tag)).with_context(|| {
format!("pointer release {pointer_tag} names tag {tag}, but fetching that release failed")
})?;
if pointer_asset(&dereferenced).is_some() {
anyhow::bail!(
"pointer release {pointer_tag} names tag {tag}, which is itself \
only a {NIGHTLY_POINTER} pointer release (refusing to follow a \
pointer chain)"
);
}
Ok(dereferenced)
}
fn fetch_text(client: &reqwest::blocking::Client, url: &str) -> Result<String> {
http_get(client, url, "download")?
.text()
.with_context(|| format!("read body of {url}"))
}
fn find_asset<'a>(release: &'a Release, must_contain: &str) -> Option<&'a Asset> {
release
.assets
.iter()
.find(|a| a.name.contains(must_contain))
}
struct AssetQuery {
must_contain: String,
companion_must_contain: Option<String>,
label: String,
}
fn host_arch_token() -> &'static str {
match std::env::consts::ARCH {
"x86_64" => "x64",
"aarch64" => "arm64",
"s390x" => "s390x",
other => other,
}
}
fn asset_query() -> AssetQuery {
let arch = host_arch_token();
#[cfg(target_os = "macos")]
{
if arch == "arm64" {
AssetQuery {
must_contain: "-bin-macos-arm64.tar.gz".into(),
companion_must_contain: None,
label: "metal".into(),
}
} else {
AssetQuery {
must_contain: "-bin-macos-x64.tar.gz".into(),
companion_must_contain: None,
label: "cpu".into(),
}
}
}
#[cfg(target_os = "linux")]
{
match crate::hostgpu::detect() {
HostGpu::Vulkan => AssetQuery {
must_contain: format!("-bin-ubuntu-vulkan-{arch}.tar.gz"),
companion_must_contain: None,
label: "vulkan".into(),
},
HostGpu::Rocm if arch == "x64" => AssetQuery {
must_contain: "-bin-ubuntu-rocm-".into(),
companion_must_contain: None,
label: "rocm".into(),
},
HostGpu::Cuda { .. } => {
eprintln!(
"[llmman] NVIDIA GPU detected, but llama.cpp does not publish a \
prebuilt Linux CUDA binary — falling back to the CPU build. Use \
`llmman serve --runtime docker` (or `--runtime podman`) for GPU \
acceleration on Linux, or build llama.cpp yourself with \
GGML_CUDA=ON and put llama-server on PATH."
);
AssetQuery {
must_contain: format!("-bin-ubuntu-{arch}.tar.gz"),
companion_must_contain: None,
label: "cpu".into(),
}
}
_ => AssetQuery {
must_contain: format!("-bin-ubuntu-{arch}.tar.gz"),
companion_must_contain: None,
label: "cpu".into(),
},
}
}
#[cfg(target_os = "windows")]
{
match crate::hostgpu::detect() {
HostGpu::Cuda { major } => {
let cuda = if arch == "arm64" {
"13.4"
} else if major >= 13 {
"13.3"
} else {
"12.4"
};
AssetQuery {
must_contain: format!("-bin-win-cuda-{cuda}-{arch}.zip"),
companion_must_contain: Some(format!(
"cudart-llama-bin-win-cuda-{cuda}-{arch}.zip"
)),
label: format!("cuda-{cuda}"),
}
}
HostGpu::Rocm if arch == "x64" => AssetQuery {
must_contain: "-bin-win-rocm-".into(),
companion_must_contain: None,
label: "rocm".into(),
},
HostGpu::Vulkan if arch == "x64" => AssetQuery {
must_contain: format!("-bin-win-vulkan-{arch}.zip"),
companion_must_contain: None,
label: "vulkan".into(),
},
_ => AssetQuery {
must_contain: format!("-bin-win-cpu-{arch}.zip"),
companion_must_contain: None,
label: "cpu".into(),
},
}
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
AssetQuery {
must_contain: format!("-bin-ubuntu-{arch}.tar.gz"),
companion_must_contain: None,
label: "cpu".into(),
}
}
}
fn install_root() -> Result<PathBuf> {
Ok(crate::data_root()?.join("llama.cpp"))
}
fn install_dir(tag: &str, label: &str) -> Result<PathBuf> {
Ok(install_root()?.join(tag).join(label))
}
fn tmp_dir() -> Result<PathBuf> {
if let Some(dir) = tmp_dir_from_env() {
return Ok(dir);
}
Ok(install_root()?.join("tmp"))
}
fn tmp_dir_from_env() -> Option<PathBuf> {
parse_tmp_dir(std::env::var("LLMMAN_TMPDIR").ok().as_deref())
}
fn parse_tmp_dir(value: Option<&str>) -> Option<PathBuf> {
let trimmed = value?.trim();
(!trimmed.is_empty()).then(|| PathBuf::from(trimmed))
}
pub(crate) fn tmp_path(name: &str) -> Result<PathBuf> {
let dir = tmp_dir()?;
std::fs::create_dir_all(&dir).with_context(|| format!("create {}", dir.display()))?;
Ok(dir.join(format!("{name}.tmp-{}", std::process::id())))
}
pub(crate) struct RemoveOnDrop<'a>(pub(crate) &'a Path);
impl Drop for RemoveOnDrop<'_> {
fn drop(&mut self) {
let _ = std::fs::remove_file(self.0);
}
}
pub(crate) fn find_binary(dir: &Path, name: &str) -> Option<PathBuf> {
walkdir::WalkDir::new(dir)
.into_iter()
.filter_map(|e| e.ok())
.find(|e| e.file_type().is_file() && e.file_name().to_str() == Some(name))
.map(|e| e.path().to_path_buf())
}
const PROGRESS_LOG_INTERVAL: Duration = Duration::from_secs(5);
const DOWNLOAD_MARKER_STALE_AFTER: Duration = Duration::from_secs(60);
const MARKER_HEARTBEAT: Duration = Duration::from_secs(15);
fn download_marker_path() -> Result<PathBuf> {
Ok(install_root()?.join(".downloading"))
}
pub fn download_in_progress() -> bool {
download_status().is_some()
}
pub fn download_status() -> Option<String> {
let path = download_marker_path().ok()?;
marker_status(&path, DOWNLOAD_MARKER_STALE_AFTER)
}
fn marker_status(path: &Path, stale_after: Duration) -> Option<String> {
marker_is_fresh(path, stale_after)
.then(|| std::fs::read_to_string(path).unwrap_or_default())
.map(|text| text.trim().to_string())
}
fn marker_is_fresh(path: &Path, stale_after: Duration) -> bool {
let Ok(mtime) = std::fs::metadata(path).and_then(|m| m.modified()) else {
return false;
};
std::time::SystemTime::now()
.duration_since(mtime)
.map_or(true, |age| age < stale_after)
}
pub(crate) struct DownloadMarker {
path: Option<PathBuf>,
status: std::sync::Mutex<String>,
}
impl DownloadMarker {
pub(crate) fn create() -> DownloadMarker {
match download_marker_path() {
Ok(p) => Self::create_at(p),
Err(_) => Self::at(None),
}
}
fn create_at(path: PathBuf) -> DownloadMarker {
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
Self::at(std::fs::write(&path, b"").ok().map(|_| path))
}
fn at(path: Option<PathBuf>) -> DownloadMarker {
DownloadMarker {
path,
status: std::sync::Mutex::new(String::new()),
}
}
pub(crate) fn touch(&self) {
if let Ok(s) = self.status.lock() {
self.write(&s);
}
}
pub(crate) fn set_status(&self, status: &str) {
if let Ok(mut s) = self.status.lock() {
*s = status.to_string();
self.write(&s);
}
}
pub(crate) fn keep_alive_during<T>(&self, work: impl FnOnce() -> T) -> T {
self.keep_alive_every(MARKER_HEARTBEAT, work)
}
fn keep_alive_every<T>(&self, interval: Duration, work: impl FnOnce() -> T) -> T {
let (done_tx, done_rx) = std::sync::mpsc::channel::<()>();
std::thread::scope(|scope| {
scope.spawn(move || loop {
match done_rx.recv_timeout(interval) {
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => self.touch(),
_ => return,
}
});
let out = work();
drop(done_tx);
out
})
}
fn write(&self, text: &str) {
if let Some(path) = &self.path {
let _ = std::fs::write(path, text.as_bytes());
if let Ok(file) = std::fs::File::options().write(true).open(path) {
let _ = file.set_modified(std::time::SystemTime::now());
}
}
}
}
impl Drop for DownloadMarker {
fn drop(&mut self) {
if let Some(path) = &self.path {
let _ = std::fs::remove_file(path);
}
}
}
fn create_new_file(dest: &Path) -> Result<std::fs::File> {
let open = || {
std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(dest)
};
match open() {
Ok(f) => Ok(f),
Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {
std::fs::remove_file(dest)
.with_context(|| format!("remove stale {}", dest.display()))?;
open().with_context(|| format!("create {}", dest.display()))
}
Err(e) => Err(e).with_context(|| format!("create {}", dest.display())),
}
}
pub(crate) fn download_to_file(
client: &reqwest::blocking::Client,
url: &str,
dest: &Path,
label: &str,
marker: Option<&DownloadMarker>,
) -> Result<()> {
let mut resp = client
.get(url)
.send()
.with_context(|| format!("download {url}"))?;
if !resp.status().is_success() {
anyhow::bail!("download {url} returned {}", resp.status());
}
let total = resp.content_length().unwrap_or(0);
let mut file = create_new_file(dest)?;
let mut buf = [0u8; 1 << 16];
let mut downloaded = 0u64;
let mut last_logged = Instant::now();
loop {
let n = resp.read(&mut buf).context("read download stream")?;
if n == 0 {
break;
}
file.write_all(&buf[..n]).context("write downloaded data")?;
downloaded += n as u64;
if last_logged.elapsed() >= PROGRESS_LOG_INTERVAL {
let progress = download_progress_text(label, downloaded, total);
if let Some(marker) = marker {
marker.set_status(&format!("downloading {progress}"));
}
if total > 0 {
eprintln!("[llmman] downloading {progress}");
}
last_logged = Instant::now();
}
}
eprintln!("[llmman] downloaded {label}: {}", human_size(downloaded));
Ok(())
}
fn download_progress_text(label: &str, downloaded: u64, total: u64) -> String {
if total > 0 {
format!(
"{label}: {} / {} ({}%)",
human_size(downloaded),
human_size(total),
downloaded.saturating_mul(100) / total
)
} else {
format!("{label}: {}", human_size(downloaded))
}
}
pub(crate) fn extract_tar_gz(archive_path: &Path, dest: &Path) -> Result<()> {
std::fs::create_dir_all(dest).with_context(|| format!("create {}", dest.display()))?;
let file = std::fs::File::open(archive_path)
.with_context(|| format!("open {}", archive_path.display()))?;
let gz = flate2::read::GzDecoder::new(file);
let mut archive = tar::Archive::new(gz);
archive
.unpack(dest)
.with_context(|| format!("extract {} into {}", archive_path.display(), dest.display()))
}
fn extract_zip(archive_path: &Path, dest: &Path) -> Result<()> {
std::fs::create_dir_all(dest).with_context(|| format!("create {}", dest.display()))?;
let file = std::fs::File::open(archive_path)
.with_context(|| format!("open {}", archive_path.display()))?;
let mut archive = zip::ZipArchive::new(file)
.with_context(|| format!("read zip {}", archive_path.display()))?;
archive
.extract(dest)
.with_context(|| format!("extract {} into {}", archive_path.display(), dest.display()))
}
fn extract(archive_path: &Path, archive_name: &str, dest: &Path) -> Result<()> {
if archive_name.ends_with(".zip") {
extract_zip(archive_path, dest)
} else {
extract_tar_gz(archive_path, dest)
}
}
pub struct Resolved {
pub bin: PathBuf,
pub backend_label: String,
}
pub fn ensure_llama_server(pinned_version: Option<&str>) -> Result<Resolved> {
let query = asset_query();
let bin_name = if cfg!(target_os = "windows") {
"llama-server.exe"
} else {
"llama-server"
};
if let Some(tag) = pinned_version {
let dest = install_dir(tag, &query.label)?;
if let Some(bin) = crate::managed::completed_binary(&dest, bin_name) {
return Ok(Resolved {
bin,
backend_label: query.label,
});
}
} else {
if let Some(bin) = newest_cached(&query.label, bin_name)? {
match try_ensure_from_network(pinned_version, &query, bin_name) {
Ok(resolved) => return Ok(resolved),
Err(e) => {
eprintln!(
"[llmman] could not check for a newer llama-server release ({e:#}); \
using previously downloaded build"
);
return Ok(Resolved {
bin,
backend_label: query.label,
});
}
}
}
}
try_ensure_from_network(pinned_version, &query, bin_name)
}
fn try_ensure_from_network(
pinned_version: Option<&str>,
query: &AssetQuery,
bin_name: &str,
) -> Result<Resolved> {
let client = http_client()?;
let release = resolve_release(&client, pinned_version)?;
let tag = release.tag_name.clone();
let dest = install_dir(&tag, &query.label)?;
let cached = |dest: &Path| crate::managed::completed_binary(dest, bin_name);
let bin = crate::managed::ensure(&install_root()?, &dest, "llama.cpp", cached, |dest| {
let asset = find_asset(&release, &query.must_contain)
.with_context(|| {
format!(
"no {} llama.cpp release asset found in {tag} (looked for a name containing {:?})",
query.label, query.must_contain
)
})?
.clone();
eprintln!(
"[llmman] downloading llama-server ({}) {tag}: {}",
query.label, asset.name
);
let marker = DownloadMarker::create();
let fetch = |asset: &Asset| -> Result<()> {
let tmp = tmp_path(&asset.name)?;
let _cleanup = RemoveOnDrop(&tmp);
download_to_file(
&client,
&asset.browser_download_url,
&tmp,
&asset.name,
Some(&marker),
)?;
marker.set_status(&format!("extracting {}", asset.name));
extract(&tmp, &asset.name, dest)
};
marker.keep_alive_during(|| -> Result<()> {
fetch(&asset)?;
if let Some(companion_substr) = &query.companion_must_contain {
match find_asset(&release, companion_substr) {
Some(companion) => {
eprintln!("[llmman] downloading {}", companion.name);
fetch(companion)?;
}
None => eprintln!(
"[llmman] warning: expected companion asset containing {companion_substr:?} \
not found in release {tag} — {} may be missing runtime libraries it needs",
query.label
),
}
}
Ok(())
})?;
let bin = find_binary(dest, bin_name).with_context(|| {
format!(
"llama-server binary not found after extracting {}",
asset.name
)
})?;
mark_executable(&bin)?;
Ok(bin)
})?;
Ok(Resolved {
bin,
backend_label: query.label.clone(),
})
}
#[cfg(unix)]
pub(crate) fn mark_executable(path: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let mut perm = std::fs::metadata(path)?.permissions();
perm.set_mode(perm.mode() | 0o111);
std::fs::set_permissions(path, perm).with_context(|| format!("chmod +x {}", path.display()))
}
#[cfg(not(unix))]
pub(crate) fn mark_executable(_path: &Path) -> Result<()> {
Ok(())
}
fn build_number(tag: &str) -> Option<u64> {
tag.strip_prefix('b')?.parse().ok()
}
fn newest_cached(label: &str, bin_name: &str) -> Result<Option<PathBuf>> {
let root = install_root()?;
let Ok(entries) = std::fs::read_dir(&root) else {
return Ok(None);
};
let mut best: Option<(u64, PathBuf)> = None;
for entry in entries.flatten() {
if !entry.path().is_dir() {
continue;
}
let tag = entry.file_name().to_string_lossy().into_owned();
let Some(build) = build_number(&tag) else {
continue;
};
let dest = entry.path().join(label);
let Some(bin) = crate::managed::completed_binary(&dest, bin_name) else {
continue;
};
if best.as_ref().map(|(b, _)| build > *b).unwrap_or(true) {
best = Some((build, bin));
}
}
Ok(best.map(|(_, bin)| bin))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_tmp_dir_returns_none_when_unset_or_blank() {
assert_eq!(parse_tmp_dir(None), None);
assert_eq!(parse_tmp_dir(Some("")), None);
assert_eq!(parse_tmp_dir(Some(" ")), None);
}
#[test]
fn parse_tmp_dir_trims_and_returns_the_given_path() {
assert_eq!(
parse_tmp_dir(Some(" /custom/tmp ")),
Some(PathBuf::from("/custom/tmp"))
);
}
#[test]
#[cfg(unix)]
fn create_new_file_never_writes_through_a_planted_symlink() {
use std::os::unix::fs::symlink;
let dir = std::env::temp_dir().join(format!(
"llmman-create-new-file-test-{}",
std::process::id()
));
std::fs::create_dir_all(&dir).unwrap();
let victim = dir.join("victim");
let dest = dir.join("dest");
std::fs::write(&victim, b"do not touch").unwrap();
symlink(&victim, &dest).unwrap();
create_new_file(&dest).unwrap();
assert_eq!(std::fs::read(&victim).unwrap(), b"do not touch");
assert!(dest.is_file() && !dest.is_symlink());
std::fs::remove_dir_all(&dir).unwrap();
}
#[test]
fn find_asset_matches_only_the_intended_substring() {
let release = Release {
tag_name: "b10360".into(),
assets: vec![
Asset {
name: "llama-b10360-bin-ubuntu-x64.tar.gz".into(),
browser_download_url: String::new(),
},
Asset {
name: "llama-b10360-bin-ubuntu-vulkan-x64.tar.gz".into(),
browser_download_url: String::new(),
},
Asset {
name: "llama-b10360-bin-ubuntu-rocm-7.14-x64.tar.gz".into(),
browser_download_url: String::new(),
},
],
};
assert_eq!(
find_asset(&release, "-bin-ubuntu-x64.tar.gz").unwrap().name,
"llama-b10360-bin-ubuntu-x64.tar.gz"
);
assert_eq!(
find_asset(&release, "-bin-ubuntu-vulkan-x64.tar.gz")
.unwrap()
.name,
"llama-b10360-bin-ubuntu-vulkan-x64.tar.gz"
);
assert_eq!(
find_asset(&release, "-bin-ubuntu-rocm-").unwrap().name,
"llama-b10360-bin-ubuntu-rocm-7.14-x64.tar.gz"
);
assert!(find_asset(&release, "-bin-win-cpu-x64.zip").is_none());
}
#[test]
fn pointer_asset_detects_a_semver_pointer_release() {
let pointer_release = Release {
tag_name: "v0.3.0".into(),
assets: vec![Asset {
name: "nightly-tag.txt".into(),
browser_download_url: String::new(),
}],
};
assert_eq!(
pointer_asset(&pointer_release).map(|a| a.name.as_str()),
Some("nightly-tag.txt")
);
let binary_release = Release {
tag_name: "b10621".into(),
assets: vec![Asset {
name: "llama-b10621-bin-ubuntu-vulkan-x64.tar.gz".into(),
browser_download_url: String::new(),
}],
};
assert_eq!(
pointer_asset(&binary_release).map(|a| a.name.as_str()),
None
);
let mixed_release = Release {
tag_name: "b10621".into(),
assets: vec![
Asset {
name: "nightly-tag.txt".into(),
browser_download_url: String::new(),
},
Asset {
name: "llama-b10621-bin-ubuntu-vulkan-x64.tar.gz".into(),
browser_download_url: String::new(),
},
],
};
assert_eq!(pointer_asset(&mixed_release).map(|a| a.name.as_str()), None);
}
#[test]
fn parse_pointer_tag_trims_and_rejects_blank_content() {
assert_eq!(parse_pointer_tag("b10621\n"), Some("b10621"));
assert_eq!(parse_pointer_tag(" b10621 "), Some("b10621"));
assert_eq!(parse_pointer_tag(""), None);
assert_eq!(parse_pointer_tag("\n \n"), None);
}
fn pointer_release(tag: &str) -> Release {
Release {
tag_name: tag.into(),
assets: vec![Asset {
name: NIGHTLY_POINTER.into(),
browser_download_url: format!("https://example.invalid/{tag}/{NIGHTLY_POINTER}"),
}],
}
}
fn binary_release(tag: &str) -> Release {
Release {
tag_name: tag.into(),
assets: vec![Asset {
name: format!("llama-{tag}-bin-ubuntu-x64.tar.gz"),
browser_download_url: String::new(),
}],
}
}
#[test]
fn resolve_release_with_dereferences_a_pointer_release() {
let metadata_fetches = std::cell::RefCell::new(Vec::new());
let release = resolve_release_with(
|v| {
metadata_fetches.borrow_mut().push(v.map(str::to_owned));
Ok(match v {
None => pointer_release("v0.3.0"),
Some("b10621") => binary_release("b10621"),
Some(other) => panic!("unexpected tag fetch: {other}"),
})
},
|_url| Ok("b10621\n".into()),
None,
)
.unwrap();
assert_eq!(release.tag_name, "b10621");
assert_eq!(
*metadata_fetches.borrow(),
vec![None, Some("b10621".to_owned())]
);
}
#[test]
fn resolve_release_with_refuses_a_pointer_chain() {
let err = resolve_release_with(
|v| {
Ok(match v {
None => pointer_release("v0.3.0"),
Some(tag) => pointer_release(tag),
})
},
|_url| Ok("b10621\n".into()),
None,
)
.unwrap_err();
let msg = err.to_string();
assert!(msg.contains("v0.3.0"), "missing pointer tag in: {msg}");
assert!(msg.contains("b10621"), "missing dereferenced tag in: {msg}");
assert!(msg.contains("pointer chain"), "unexpected message: {msg}");
}
#[test]
fn resolve_release_with_rejects_a_blank_pointer_file() {
let err = resolve_release_with(
|_v| Ok(pointer_release("v0.3.0")),
|_url| Ok("\n \n".into()),
None,
)
.unwrap_err();
let msg = err.to_string();
assert!(msg.contains("v0.3.0"), "missing pointer tag in: {msg}");
assert!(msg.contains("is empty"), "unexpected message: {msg}");
}
#[test]
fn resolve_release_with_contextualizes_a_failed_dereferenced_fetch() {
let err = resolve_release_with(
|v| match v {
None => Ok(pointer_release("v0.3.0")),
Some(_) => Err(anyhow!("boom")),
},
|_url| Ok("b10621\n".into()),
None,
)
.unwrap_err();
let msg = format!("{err:#}");
assert!(
msg.contains(
"pointer release v0.3.0 names tag b10621, but fetching that release failed"
),
"unexpected message: {msg}"
);
assert!(msg.contains("boom"), "missing root cause in: {msg}");
}
#[test]
fn resolve_release_with_passes_a_binary_release_through() {
let text_fetches = std::cell::Cell::new(0);
let release = resolve_release_with(
|_v| Ok(binary_release("b10621")),
|_url| {
text_fetches.set(text_fetches.get() + 1);
Ok(String::new())
},
None,
)
.unwrap();
assert_eq!(release.tag_name, "b10621");
assert_eq!(text_fetches.get(), 0);
}
#[test]
fn resolve_release_with_passes_a_pinned_binary_tag_through() {
let text_fetches = std::cell::Cell::new(0);
let release = resolve_release_with(
|v| {
assert_eq!(v, Some("b10360"));
Ok(binary_release("b10360"))
},
|_url| {
text_fetches.set(text_fetches.get() + 1);
Ok(String::new())
},
Some("b10360"),
)
.unwrap();
assert_eq!(release.tag_name, "b10360");
assert_eq!(text_fetches.get(), 0);
}
#[test]
fn marker_is_fresh_is_false_for_an_absent_file() {
let path =
std::env::temp_dir().join(format!("llmman-marker-absent-{}", std::process::id()));
let _ = std::fs::remove_file(&path);
assert!(!marker_is_fresh(&path, Duration::from_secs(60)));
}
#[test]
fn marker_is_fresh_is_true_for_a_just_written_file() {
let path = std::env::temp_dir().join(format!("llmman-marker-fresh-{}", std::process::id()));
std::fs::write(&path, b"").unwrap();
let fresh = marker_is_fresh(&path, Duration::from_secs(60));
let _ = std::fs::remove_file(&path);
assert!(fresh);
}
#[test]
fn marker_is_fresh_is_false_for_a_stale_mtime() {
let path = std::env::temp_dir().join(format!("llmman-marker-stale-{}", std::process::id()));
std::fs::write(&path, b"").unwrap();
let file = std::fs::File::options().write(true).open(&path).unwrap();
file.set_modified(std::time::SystemTime::now() - Duration::from_secs(120))
.unwrap();
drop(file);
let fresh = marker_is_fresh(&path, Duration::from_secs(60));
let _ = std::fs::remove_file(&path);
assert!(!fresh);
}
#[test]
fn download_marker_creates_touches_and_removes_the_marker() {
let path = std::env::temp_dir().join(format!(
"llmman-marker-lifecycle-{}/.downloading",
std::process::id()
));
let _ = std::fs::remove_file(&path);
let marker = DownloadMarker::create_at(path.clone());
assert!(path.is_file(), "marker not created at {}", path.display());
std::fs::File::options()
.write(true)
.open(&path)
.unwrap()
.set_modified(std::time::SystemTime::now() - Duration::from_secs(120))
.unwrap();
assert!(!marker_is_fresh(&path, Duration::from_secs(60)));
marker.touch();
assert!(marker_is_fresh(&path, Duration::from_secs(60)));
drop(marker);
assert!(!path.exists(), "marker not removed on drop");
}
fn temp_marker(name: &str) -> PathBuf {
let path = std::env::temp_dir().join(format!(
"llmman-marker-{name}-{}/.downloading",
std::process::id()
));
let _ = std::fs::remove_file(&path);
path
}
fn status_of(path: &Path) -> Option<String> {
marker_status(path, DOWNLOAD_MARKER_STALE_AFTER)
}
fn backdate(path: &Path) {
std::fs::File::options()
.write(true)
.open(path)
.unwrap()
.set_modified(std::time::SystemTime::now() - 2 * DOWNLOAD_MARKER_STALE_AFTER)
.unwrap();
}
#[test]
fn marker_status_reports_the_last_written_text_while_fresh() {
let path = temp_marker("status");
assert_eq!(status_of(&path), None);
let marker = DownloadMarker::create_at(path.clone());
assert_eq!(status_of(&path).as_deref(), Some(""));
marker.set_status("llama.tar.gz: 12 MB / 40 MB (30%)");
assert_eq!(
status_of(&path).as_deref(),
Some("llama.tar.gz: 12 MB / 40 MB (30%)")
);
std::fs::write(&path, b"").unwrap();
marker.touch();
assert_eq!(
status_of(&path).as_deref(),
Some("llama.tar.gz: 12 MB / 40 MB (30%)")
);
marker.set_status("extracting llama.tar.gz");
backdate(&path);
assert_eq!(status_of(&path), None);
drop(marker);
assert_eq!(status_of(&path), None);
}
#[test]
fn keep_alive_heartbeat_refreshes_the_marker_while_work_runs() {
let path = temp_marker("heartbeat");
let marker = DownloadMarker::create_at(path.clone());
marker.set_status("extracting llama.tar.gz");
backdate(&path);
assert_eq!(status_of(&path), None);
let out = marker.keep_alive_every(Duration::from_millis(5), || {
std::thread::sleep(Duration::from_millis(60));
7
});
assert_eq!(out, 7);
assert_eq!(status_of(&path).as_deref(), Some("extracting llama.tar.gz"));
backdate(&path);
std::thread::sleep(Duration::from_millis(30));
assert_eq!(status_of(&path), None);
drop(marker);
assert!(!path.exists());
}
#[test]
fn download_progress_text_includes_a_percentage_only_with_a_known_total() {
assert_eq!(
download_progress_text("x.tar.gz", 30 * 1024 * 1024, 120 * 1024 * 1024),
format!(
"x.tar.gz: {} / {} (25%)",
human_size(30 * 1024 * 1024),
human_size(120 * 1024 * 1024)
)
);
assert_eq!(
download_progress_text("x.tar.gz", 4096, 0),
format!("x.tar.gz: {}", human_size(4096))
);
}
#[test]
fn host_arch_token_returns_a_nonempty_token() {
assert!(!host_arch_token().is_empty());
}
#[test]
fn build_number_compares_correctly_across_a_digit_count_boundary() {
assert!(build_number("b10000") > build_number("b9999"));
assert_eq!(build_number("b10360"), Some(10360));
assert_eq!(build_number("not-a-tag"), None);
}
#[test]
#[ignore = "hits the network and downloads a real llama.cpp release"]
fn ensure_llama_server_downloads_a_runnable_binary() {
let resolved = ensure_llama_server(None).expect("ensure_llama_server");
assert!(
resolved.bin.is_file(),
"{} is not a file",
resolved.bin.display()
);
let output = std::process::Command::new(&resolved.bin)
.arg("--version")
.output()
.expect("run downloaded llama-server --version");
assert!(
output.status.success(),
"llama-server --version failed: {output:?}"
);
}
}