//! Resolve real backend metadata into selection candidates and apply the //! shared, hardware-aware policy. //! //! This module is the bridge between the pure policy in `phoebus-core::selection` //! and the actual system. It only reads: it never installs anything. Backend //! metadata that cannot be resolved (offline, daemon down, package absent) is //! reported as an *unavailable* candidate so the review explains why a lane was //! not chosen instead of silently inventing availability. use std::path::Path; use phoebus_core::selection::{ select, Backend, Candidate, HardwareFacts, Need, PackageKind, Preferences, SandboxLevel, Selection, UpdateReliability, }; use crate::command::CommandSpec; use crate::error::{ExecError, Result}; use crate::exec::Exec; /// Persisted preferences live here, relative to the target root. pub const PREFERENCES_REL: &str = "etc/phoebus/package-preferences.toml"; /// Load persisted preferences (global default and per-application choices). pub fn load_preferences(root: &Path) -> Preferences { let path = root.join(PREFERENCES_REL); std::fs::read_to_string(&path) .ok() .and_then(|text| toml::from_str(&text).ok()) .unwrap_or_default() } /// Persist preferences so an explicit choice survives across sessions. pub fn save_preferences(exec: &Exec, root: &Path, prefs: &Preferences) -> Result<()> { let path = root.join(PREFERENCES_REL); if let Some(parent) = path.parent() { std::fs::create_dir_all(parent).map_err(|e| ExecError::io(parent.to_path_buf(), e))?; } let text = toml::to_string_pretty(prefs) .map_err(|e| ExecError::operation("sys", "save preferences", e.to_string()))?; if exec.dry_run { exec.logger .info("[dry-run] would persist package preferences"); return Ok(()); } std::fs::write(&path, text).map_err(|e| ExecError::io(path, e)) } /// Read free storage for the filesystem containing `root` using `df -Pk`. fn free_mib(exec: &Exec, root: &Path) -> u64 { let path = if root.exists() { root.to_string_lossy().to_string() } else { "/".to_string() }; let out = exec.runner.execute( &CommandSpec::new("sys", "probe free space", "df") .args(["-Pk"]) .arg(path), ); match out { Ok(out) if out.success => out .stdout_lossy() .lines() .nth(1) .and_then(|line| line.split_whitespace().nth(3)) .and_then(|kb| kb.parse::().ok()) .map(|kb| kb / 1024) .unwrap_or(0), _ => 0, } } /// Assemble the bounded hardware facts the policy consumes. pub fn hardware_facts(exec: &Exec, root: &Path) -> HardwareFacts { let hw = crate::probe::probe_hardware(exec.runner.as_ref()); let arch = match std::env::consts::ARCH { "x86_64" => "x86_64", "aarch64" => "aarch64", "x86" => "x86", "arm" => "arm", "riscv64" => "riscv64", other => other, } .to_string(); let cores = std::thread::available_parallelism() .map(|n| n.get() as u32) .unwrap_or(1); let gpus = hw.gpu_vendors(); let wayland = std::env::var("XDG_SESSION_TYPE") .map(|v| v.eq_ignore_ascii_case("wayland")) .unwrap_or(true); let binhost_available = root.join("etc/portage/binrepos.conf").exists(); let mut available_backends = std::collections::BTreeSet::new(); available_backends.insert(Backend::PortageSource); if binhost_available { available_backends.insert(Backend::PortageBinary); } available_backends.insert(Backend::PortageVendor); available_backends.insert(Backend::AppImage); available_backends.insert(Backend::CustomEbuild); if crate::nix::installed(root) { available_backends.insert(Backend::Nix); } if crate::exec::find_in_path("flatpak").is_some() { available_backends.insert(Backend::Flatpak); } if matches!( crate::snap::status(exec), Ok(status) if status.usable() ) { available_backends.insert(Backend::Snap); } let history = phoebus_core::load_history(&root.join("var/lib/phoebus/compile-times.tsv")); HardwareFacts { arch, cores, memory_mib: hw.memory_mib, storage_free_mib: free_mib(exec, root), gpus, wayland, binhost_available, available_backends, compile_history: history.into_iter().collect(), } } /// Parse the first `cat/pkg-version` token from an `emerge --pretend` line. pub fn parse_pretend_version(text: &str) -> Option { for line in text.lines() { let trimmed = line.trim_start(); if !trimmed.starts_with('[') { continue; } let close = trimmed.find(']')?; let rest = trimmed[close + 1..].trim(); let atom = rest.split_whitespace().next()?; if atom.contains('/') { return Some(atom.to_string()); } } None } /// A rough capability profile for a plain Portage source/binary candidate. fn portage_candidate(id: &str, binary: bool, facts: &HardwareFacts) -> Candidate { let mut c = Candidate::new( if binary { Backend::PortageBinary } else { Backend::PortageSource }, id, if binary { "official signed Gentoo binhost" } else { "Gentoo source tree (/var/db/repos)" }, ); c.verified = true; c.arch = facts.arch.clone(); c.update_reliability = UpdateReliability::Automatic; if !binary { c.build_seconds = facts.estimated_build_seconds(id); c.build_measured = c.build_seconds.is_some(); } c } /// Build the candidate set for a request from real backend metadata. /// /// Every candidate reflects something the backend actually reported. When a /// backend cannot be reached, the candidate is marked unavailable with a note. pub fn resolve_candidates(exec: &Exec, root: &Path, need: &Need) -> Vec { let facts = hardware_facts(exec, root); let mut out = Vec::new(); let name = &need.name; // --- Portage ------------------------------------------------------ let pretend = exec.runner.execute( &CommandSpec::new("sys", "resolve Portage candidate", "emerge") .args(["--pretend", "--quiet", "--color=n", "--"]) .arg(name), ); match pretend { Ok(resolved) if resolved.success => { let text = resolved.stdout_lossy(); let id = parse_pretend_version(&text).unwrap_or_else(|| name.clone()); out.push(portage_candidate(&id, false, &facts)); if facts.binhost_available { out.push(portage_candidate(&id, true, &facts)); } } _ => { let mut c = portage_candidate(name, false, &facts); c.available = false; c.notes .push("emerge could not resolve this name from the Gentoo tree".into()); out.push(c); } } // --- Nix ---------------------------------------------------------- if crate::nix::installed(root) { let mut c = Candidate::new(Backend::Nix, name, "nixpkgs (pinned)"); c.verified = true; c.update_reliability = UpdateReliability::Automatic; c.sandbox = SandboxLevel::None; if !phoebus_core::action::valid_nix_attribute(name) { c.available = false; c.notes .push("not a bounded nixpkgs attribute (URLs/flakes are refused)".into()); } out.push(c); } // --- Flatpak ------------------------------------------------------ if crate::exec::find_in_path("flatpak").is_some() { let mut c = Candidate::new(Backend::Flatpak, name, "flathub"); c.sandbox = SandboxLevel::Strong; c.update_reliability = UpdateReliability::Automatic; c.verified = true; c.shared_runtime_mib = Some(400); if !phoebus_core::action::valid_flatpak_app_id(name) { c.available = false; c.notes .push("not a reverse-DNS Flatpak application id".into()); } else { // Confirm the id actually exists in the configured remotes. let found = exec.runner.execute( &CommandSpec::new("sys", "resolve Flatpak candidate", "flatpak").args([ "search", "--columns=application", "--", name, ]), ); match found { Ok(o) if o.success && o.stdout_lossy().contains(name) => { c.verified = true; } _ => { c.available = false; c.notes.push( "flathub could not confirm this application id (offline or unknown)".into(), ); } } } out.push(c); } // --- Snap --------------------------------------------------------- if matches!(crate::snap::status(exec), Ok(s) if s.usable()) { let mut c = Candidate::new(Backend::Snap, name, "Snap Store"); c.sandbox = SandboxLevel::Strong; c.update_reliability = UpdateReliability::Automatic; c.verified = true; c.shared_runtime_mib = Some(200); if !phoebus_core::action::valid_snap_name(name) { c.available = false; c.notes.push("not a valid snap name".into()); } else { // Confirm the snap exists in the store rather than assuming it. let found = exec.runner.execute( &CommandSpec::new("sys", "resolve snap candidate", "snap").args([ "find", "--color=never", "--", name, ]), ); let confirmed = found .map(|o| { o.success && o.stdout_lossy() .lines() .any(|l| l.split_whitespace().next() == Some(name)) }) .unwrap_or(false); if !confirmed { c.available = false; c.notes .push("the Snap Store could not confirm this snap (offline or unknown)".into()); } } out.push(c); } // --- AppImage (only when already managed) ------------------------- if let Ok(record) = crate::appimage::info(root, name) { let mut c = Candidate::new( Backend::AppImage, record.name.clone(), record.provenance.clone(), ); c.version = Some(record.version.clone()); c.verified = true; c.sandbox = SandboxLevel::None; c.update_reliability = UpdateReliability::Manual; c.arch = record.arch.clone(); c.install_mib = Some(record.size_bytes / (1024 * 1024)); out.push(c); } // --- Custom ebuild (only when staged in the Phoebus overlay) ------- if crate::ebuild::staged(root, name).is_some() { let mut c = Candidate::new( Backend::CustomEbuild, name, "Phoebus overlay (/var/db/repos/phoebus)", ); c.verified = false; c.sandbox = SandboxLevel::None; c.update_reliability = UpdateReliability::Manual; c.notes .push("generated ebuild; treated as untrusted until reviewed".into()); out.push(c); } out } /// Determine which backend (if any) already owns an installed copy of `name`. pub fn installed_backend(exec: &Exec, root: &Path, name: &str) -> Option { // AppImage: a managed record. if crate::appimage::info(root, name).is_ok() { return Some(Backend::AppImage); } // Flatpak: parse the installed list. if crate::exec::find_in_path("flatpak").is_some() { if let Ok(out) = exec.runner.execute( &CommandSpec::new("sys", "detect installed flatpak", "flatpak").args([ "list", "--app", "--columns=application", ]), ) { if out.success && out.stdout_lossy().lines().any(|l| l.trim() == name) { return Some(Backend::Flatpak); } } } // Snap: parse the installed list. if let Ok(entries) = crate::snap::list(exec) { if entries.iter().any(|e| e.name == name) { return Some(Backend::Snap); } } // Portage: a VDB entry for the package (bare name or full atom). if portage_installed(root, name) { return Some(Backend::PortageSource); } None } fn portage_installed(root: &Path, name: &str) -> bool { let vdb = root.join("var/db/pkg"); let (category, package) = match name.split_once('/') { Some((c, p)) => (Some(c.to_string()), p.to_string()), None => (None, name.to_string()), }; let Ok(categories) = std::fs::read_dir(&vdb) else { return false; }; for category_entry in categories.flatten() { let category_name = category_entry.file_name().to_string_lossy().to_string(); if let Some(want) = &category { if &category_name != want { continue; } } let Ok(pkgs) = std::fs::read_dir(category_entry.path()) else { continue; }; for pkg in pkgs.flatten() { let dir = pkg.file_name().to_string_lossy().to_string(); // VDB dirs are `name-version`; match the name prefix. if dir == package || dir.starts_with(&format!("{package}-")) { return true; } } } false } /// Decide the backend for a request using the shared policy. pub fn choose(exec: &Exec, root: &Path, need: &Need) -> Result { let facts = hardware_facts(exec, root); let prefs = load_preferences(root); let installed = installed_backend(exec, root, &need.name); let candidates = resolve_candidates(exec, root, need); select(need, &candidates, &facts, &prefs, installed) .map_err(|e| ExecError::operation("sys", "choose package", e.to_string())) } /// Convenience: the kind inferred from the request when the caller did not /// classify it. Kept conservative — `Unclassified` never forces a lane. pub fn infer_kind(_name: &str) -> PackageKind { PackageKind::Unclassified } /// A plain-text inventory of the available lanes, for `sys choose` with no /// request and for diagnostics. pub fn lanes_report(exec: &Exec, root: &Path) -> String { let facts = hardware_facts(exec, root); let mut out = format!( "arch={} cores={} memory={}MiB free={}MiB wayland={} binhost={}\n", facts.arch, facts.cores, facts.memory_mib, facts.storage_free_mib, facts.wayland, facts.binhost_available ); for backend in Backend::all() { let state = if facts.backend_available(*backend) { "available" } else { "unavailable" }; out.push_str(&format!(" {:<16} {state}\n", backend.as_str())); } let _ = exec; out } #[cfg(test)] mod tests { use super::*; use crate::command::{CommandOutput, RecordingRunner}; use crate::log::Logger; use phoebus_core::Redactor; use std::time::Duration; fn make_exec(runner: RecordingRunner) -> Exec { let path = tempfile::tempdir().unwrap().keep(); let logger = Logger::open(path.join("l.log"), Redactor::new(), false); Exec::new(Box::new(runner), logger, false, path.join("root"), path) } fn ok(stdout: &str) -> CommandOutput { CommandOutput { status: 0, success: true, stdout: stdout.as_bytes().to_vec(), stderr: Vec::new(), duration: Duration::from_millis(1), } } #[test] fn pretend_version_parses_the_atom() { let text = "Calculating dependencies... done!\n[ebuild N ] www-client/firefox-128.0\n"; assert_eq!( parse_pretend_version(text).as_deref(), Some("www-client/firefox-128.0") ); assert_eq!(parse_pretend_version("nothing here"), None); } #[test] fn preferences_roundtrip_through_disk() { let root = tempfile::tempdir().unwrap(); let mut prefs = Preferences::default(); prefs.per_app.insert("firefox".into(), Backend::Flatpak); prefs.default = Some(Backend::PortageBinary); let exec = make_exec(RecordingRunner::new()); save_preferences(&exec, root.path(), &prefs).unwrap(); let back = load_preferences(root.path()); assert_eq!(back, prefs); } #[test] fn missing_preferences_file_is_the_default() { let root = tempfile::tempdir().unwrap(); assert_eq!(load_preferences(root.path()), Preferences::default()); } #[test] fn portage_installed_matches_vdb_directories() { let root = tempfile::tempdir().unwrap(); let vdb = root.path().join("var/db/pkg/www-client"); std::fs::create_dir_all(vdb.join("firefox-128.0")).unwrap(); std::fs::create_dir_all(vdb.join("firefox-bin-128.0")).unwrap(); assert!(portage_installed(root.path(), "www-client/firefox")); assert!(portage_installed(root.path(), "firefox")); assert!(!portage_installed(root.path(), "www-client/chromium")); } #[test] fn resolve_includes_a_portage_candidate_from_real_metadata() { let runner = RecordingRunner::new(); runner.set_response( "emerge", ok("Calculating dependencies... done!\n[ebuild N ] app-editors/vim-9.1.0\n"), ); let exec = make_exec(runner); let root = tempfile::tempdir().unwrap(); let need = Need::new("vim").kind(PackageKind::CliTool); let candidates = resolve_candidates(&exec, root.path(), &need); assert!(candidates .iter() .any(|c| c.backend == Backend::PortageSource && c.available)); } #[test] fn unresolved_portage_candidate_is_marked_unavailable() { let runner = RecordingRunner::new(); runner.fail("emerge", "no such package"); let exec = make_exec(runner); let root = tempfile::tempdir().unwrap(); let need = Need::new("not-a-real-package"); let candidates = resolve_candidates(&exec, root.path(), &need); let portage = candidates .iter() .find(|c| c.backend == Backend::PortageSource) .unwrap(); assert!(!portage.available); assert!(!portage.notes.is_empty()); } #[test] fn lanes_report_names_every_backend() { let exec = make_exec(RecordingRunner::new()); let root = tempfile::tempdir().unwrap(); let report = lanes_report(&exec, root.path()); for backend in Backend::all() { assert!(report.contains(backend.as_str()), "missing {backend:?}"); } } }