phoebus_os/installer-rs/crates/phoebus-executor/src/admin.rs
Phoebus 38214ea9e7 chore: import Phoebus OS installer and Rust migration
Initial public import of the proven Bash installer (installer/) and the production Rust workspace (installer-rs/). The reference ISO, build output and local agent transcripts are gitignored.

Verified: installer/tests/run_tests.sh and cargo test --workspace both pass.
2026-09-20 02:07:33 -07:00

112 lines
3.8 KiB
Rust

//! System context collection for the AI assistant.
//!
//! Only system sources are read. The privacy policy is enforced here: the
//! collector never walks home directories or credential stores, and rejects
//! any path the policy forbids. This is not prompt-level "please don't read
//! private files"; it is the absence of a code path that could.
use phoebus_ai::privacy::{ContextBundle, PrivacyPolicy};
use crate::command::CommandSpec;
use crate::exec::Exec;
fn read_trim(path: &str) -> Option<String> {
let s = std::fs::read_to_string(path).ok()?;
Some(s.trim().to_string())
}
fn run(exec: &Exec, program: &str, args: &[&str]) -> Option<String> {
let spec = CommandSpec::new("admin", program, program).args(args.iter().map(|s| s.to_string()));
exec.runner.execute(&spec).ok().map(|o| {
let out = o.stdout_lossy();
let err = o.stderr_lossy();
if out.is_empty() { err } else { out }
})
}
/// Collect a system-only context bundle.
pub fn collect_system_context(exec: &Exec, policy: &PrivacyPolicy) -> ContextBundle {
let mut bundle = ContextBundle::default();
if let Some(v) = run(exec, "uname", &["-r"]) {
bundle.insert("kernel", v.trim());
}
if let Some(v) = read_trim("/etc/os-release") {
bundle.insert("os_release", v);
}
if let Some(v) = read_trim("/proc/loadavg") {
bundle.insert("loadavg", v);
}
if let Some(v) = read_trim("/proc/meminfo") {
let total = v
.lines()
.find(|l| l.starts_with("MemTotal:"))
.unwrap_or("MemTotal: unknown");
bundle.insert("memory", total.to_string());
}
if let Some(v) = run(exec, "systemctl", &["is-system-running"]) {
bundle.insert("system_state", v.trim());
}
if let Some(v) = run(exec, "systemctl", &["--failed", "--no-pager"]) {
bundle.insert("failed_units", v.trim());
}
if let Some(v) = run(exec, "findmnt", &["-no", "SOURCE,FSTYPE", "/"]) {
bundle.insert("root_filesystem", v.trim());
}
if let Some(v) = run(exec, "df", &["-h", "/"]) {
bundle.insert("root_usage", v.trim());
}
if phoebus_executor_has("wpctl") {
if let Some(v) = run(exec, "wpctl", &["status"]) {
bundle.insert("pipewire", v.trim());
}
}
if policy.allow_raw_logs {
// Only reachable with explicit opt-in. Even then, journal is a system
// source, not user files.
if let Some(v) = run(exec, "journalctl", &["-b", "-p", "warning", "--no-pager", "-n", "50"]) {
bundle.insert("journal_warnings", v);
}
}
bundle
}
fn phoebus_executor_has(program: &str) -> bool {
crate::exec::find_in_path(program).is_some()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::command::RecordingRunner;
use crate::log::Logger;
use phoebus_core::Redactor;
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)
}
#[test]
fn collector_never_references_home_paths() {
let runner = RecordingRunner::new();
let exec = make_exec(runner.clone());
let bundle = collect_system_context(&exec, &PrivacyPolicy::default());
// The bundle only contains system facts.
for key in bundle.system.keys() {
assert!(!key.contains("home"), "unexpected key {key}");
}
// No command argument walks a home or credential path.
for spec in runner.recorded() {
for arg in &spec.args {
assert!(
!phoebus_ai::privacy::is_forbidden_path(arg),
"forbidden path in argument: {arg}"
);
}
}
}
}