429 lines
16 KiB
Rust
429 lines
16 KiB
Rust
//! Quiet proactive assistance execution and system checks.
|
|
//!
|
|
//! Deterministically probes:
|
|
//! - Updates ready for review
|
|
//! - Repeated service failures
|
|
//! - Low storage (< 5 GiB)
|
|
//! - Failed installations
|
|
//! - Required reboots
|
|
//!
|
|
//! All alerts are deduplicated, snoozable, and can be disabled.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
use phoebus_core::proactive::{
|
|
DeferredSchedule, ProactiveAlert, ProactiveCategory, ProactiveState,
|
|
};
|
|
|
|
use crate::command::CommandSpec;
|
|
use crate::error::{ExecError, Result};
|
|
use crate::exec::Exec;
|
|
use crate::personal::UserStorage;
|
|
use crate::transaction;
|
|
use crate::update::check_unified_updates;
|
|
|
|
/// The user-owned state file (relative to the user's state dir).
|
|
pub const PROACTIVE_STATE_FILE: &str = "proactive-state.toml";
|
|
|
|
/// The legacy root-owned state path, kept as a read-only migration source only.
|
|
pub fn legacy_state_path(root: &Path) -> PathBuf {
|
|
root.join("var/lib/phoebus/proactive-state.toml")
|
|
}
|
|
|
|
/// Read state from an explicit path (used by tests and callers that already
|
|
/// resolved the user's storage).
|
|
pub fn load_state_at(path: &Path) -> ProactiveState {
|
|
if !path.exists() {
|
|
return ProactiveState::default();
|
|
}
|
|
std::fs::read_to_string(path)
|
|
.ok()
|
|
.and_then(|t| toml::from_str(&t).ok())
|
|
.unwrap_or_default()
|
|
}
|
|
|
|
/// The user-owned proactive state file for this user.
|
|
pub fn state_path(storage: &UserStorage) -> PathBuf {
|
|
storage.state_file(PROACTIVE_STATE_FILE)
|
|
}
|
|
|
|
/// Load a user's proactive state. Falls back to the legacy root-owned file only
|
|
/// when the user has no state yet, so existing dismissals are not lost.
|
|
pub fn load_state(storage: &UserStorage, root: &Path) -> ProactiveState {
|
|
let path = state_path(storage);
|
|
if path.exists() {
|
|
return load_state_at(&path);
|
|
}
|
|
let legacy = legacy_state_path(root);
|
|
if legacy.exists() {
|
|
return load_state_at(&legacy);
|
|
}
|
|
ProactiveState::default()
|
|
}
|
|
|
|
pub fn save_state(exec: &Exec, storage: &UserStorage, state: &ProactiveState) -> Result<()> {
|
|
let path = state_path(storage);
|
|
let dir = storage.state_dir();
|
|
let text = toml::to_string_pretty(state)
|
|
.map_err(|e| ExecError::operation("proactive", "serialize state", e.to_string()))?;
|
|
// Route through `UserStorage` so an elevated write is chowned to the
|
|
// initiating user and cannot escape their home through a symlink.
|
|
storage.write_private(&dir, &path, text.as_bytes())?;
|
|
exec.logger
|
|
.info(format!("proactive state -> {}", path.display()));
|
|
Ok(())
|
|
}
|
|
|
|
fn now_unix() -> u64 {
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.map(|d| d.as_secs())
|
|
.unwrap_or(0)
|
|
}
|
|
|
|
/// Run deterministic checks against the system. Returns all currently detected alerts.
|
|
pub fn check_system(exec: &Exec, root: &Path) -> Vec<ProactiveAlert> {
|
|
check_system_inner(exec, root, true)
|
|
}
|
|
|
|
/// Fast startup checks. Update discovery is performed when the Alerts or
|
|
/// Updates view is opened so it cannot hold the assistant handshake open.
|
|
pub fn check_system_quick(exec: &Exec, root: &Path) -> Vec<ProactiveAlert> {
|
|
check_system_inner(exec, root, false)
|
|
}
|
|
|
|
fn check_system_inner(exec: &Exec, root: &Path, include_updates: bool) -> Vec<ProactiveAlert> {
|
|
let now = now_unix();
|
|
let mut alerts = Vec::new();
|
|
|
|
// 1. Required reboot check
|
|
if root.join("run/reboot-required").exists()
|
|
|| root.join("var/run/reboot-required").exists()
|
|
|| Path::new("/run/reboot-required").exists()
|
|
{
|
|
alerts.push(ProactiveAlert {
|
|
id: format!("reboot-{}", now),
|
|
category: ProactiveCategory::RebootRequired,
|
|
title: "System reboot required".into(),
|
|
message:
|
|
"A kernel or critical component was updated and requires a restart to take effect."
|
|
.into(),
|
|
severity: "info".into(),
|
|
actionable: false,
|
|
suggested_phrase: Some("Reboot system".into()),
|
|
created_unix: now,
|
|
fingerprint: "reboot_required".into(),
|
|
});
|
|
}
|
|
|
|
// 2. Low storage check (< 5 GiB free on root partition)
|
|
let df_cmd =
|
|
CommandSpec::new("proactive", "storage", "df").args(["-Pk", root.to_str().unwrap_or("/")]);
|
|
if let Ok(out) = exec.run(df_cmd) {
|
|
if out.success {
|
|
let out_str = String::from_utf8_lossy(&out.stdout);
|
|
for line in out_str.lines().skip(1) {
|
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
|
if parts.len() >= 4 {
|
|
if let Ok(avail_kib) = parts[3].parse::<u64>() {
|
|
const FIVE_GIB_KIB: u64 = 5 * 1024 * 1024;
|
|
if avail_kib < FIVE_GIB_KIB {
|
|
let avail_gib = (avail_kib as f64) / (1024.0 * 1024.0);
|
|
alerts.push(ProactiveAlert {
|
|
id: format!("storage-{}", now),
|
|
category: ProactiveCategory::LowStorage,
|
|
title: "Low disk storage".into(),
|
|
message: format!("Less than 5 GiB remaining on root filesystem ({:.1} GiB available).", avail_gib),
|
|
severity: "warning".into(),
|
|
actionable: true,
|
|
suggested_phrase: Some("Clean up system package caches".into()),
|
|
created_unix: now,
|
|
fingerprint: "low_storage_root".into(),
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Failed transactions check
|
|
if let Ok(tx_log) = transaction::read(root) {
|
|
for record in tx_log.records.iter().rev().take(5) {
|
|
let is_failure = record.result.starts_with("failed")
|
|
|| record.result == "interrupted"
|
|
|| record
|
|
.outcomes
|
|
.iter()
|
|
.any(|o| o.status == phoebus_core::transaction::ActionStatus::Failed);
|
|
if is_failure {
|
|
alerts.push(ProactiveAlert {
|
|
id: format!("failed-tx-{}", record.id),
|
|
category: ProactiveCategory::FailedInstallation,
|
|
title: format!("Operation failed: {}", record.summary),
|
|
message: format!(
|
|
"Transaction #{} ({}) did not complete successfully: {}.",
|
|
record.id, record.summary, record.result
|
|
),
|
|
severity: "warning".into(),
|
|
actionable: true,
|
|
suggested_phrase: Some("sys undo --legacy".into()),
|
|
created_unix: record.created_unix,
|
|
fingerprint: format!("failed_tx:{}", record.id),
|
|
});
|
|
break; // Only report the most recent failed transaction
|
|
}
|
|
}
|
|
}
|
|
|
|
// 4. Failed systemd services check
|
|
let systemctl_cmd = CommandSpec::new("proactive", "services", "systemctl").args([
|
|
"--failed",
|
|
"--no-legend",
|
|
"--plain",
|
|
]);
|
|
if let Ok(out) = exec.run(systemctl_cmd) {
|
|
if out.success {
|
|
let out_str = String::from_utf8_lossy(&out.stdout);
|
|
for line in out_str.lines() {
|
|
let parts: Vec<&str> = line.split_whitespace().collect();
|
|
if !parts.is_empty() {
|
|
let unit = parts[0];
|
|
if unit.ends_with(".service") {
|
|
alerts.push(ProactiveAlert {
|
|
id: format!("svc-{}", unit),
|
|
category: ProactiveCategory::ServiceFailure,
|
|
title: format!("Service failed: {}", unit),
|
|
message: format!(
|
|
"System service '{}' has failed and is not running.",
|
|
unit
|
|
),
|
|
severity: "warning".into(),
|
|
actionable: true,
|
|
suggested_phrase: Some(format!("Why is {} failing?", unit)),
|
|
created_unix: now,
|
|
fingerprint: format!("service_failure:{}", unit),
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 5. Updates ready for review check
|
|
if include_updates {
|
|
let overview = check_unified_updates(exec, root);
|
|
if overview.total_updates() > 0 {
|
|
alerts.push(ProactiveAlert {
|
|
id: format!("updates-{}", now),
|
|
category: ProactiveCategory::UpdatesReady,
|
|
title: format!("{} updates ready for review", overview.total_updates()),
|
|
message: format!(
|
|
"Updates available across package sources: {} Portage, {} Flatpak, {} Snap.",
|
|
overview.updates_for(phoebus_core::selection::Backend::PortageBinary)
|
|
+ overview.updates_for(phoebus_core::selection::Backend::PortageSource),
|
|
overview.updates_for(phoebus_core::selection::Backend::Flatpak),
|
|
overview.updates_for(phoebus_core::selection::Backend::Snap),
|
|
),
|
|
severity: "info".into(),
|
|
actionable: true,
|
|
suggested_phrase: Some("Show me what needs updating".into()),
|
|
created_unix: now,
|
|
fingerprint: format!("updates_ready:{}", overview.total_updates()),
|
|
});
|
|
}
|
|
}
|
|
|
|
alerts
|
|
}
|
|
|
|
/// Retrieve only active, non-dismissed alerts according to the user's persisted
|
|
/// state. Recording `last_checked_unix` is best-effort: a read path must still
|
|
/// return the current alerts even if the user's storage is temporarily
|
|
/// unwritable.
|
|
pub fn active_alerts(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
storage: &UserStorage,
|
|
) -> (Vec<ProactiveAlert>, ProactiveState) {
|
|
active_alerts_inner(exec, root, storage, true)
|
|
}
|
|
|
|
pub fn active_alerts_quick(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
storage: &UserStorage,
|
|
) -> (Vec<ProactiveAlert>, ProactiveState) {
|
|
active_alerts_inner(exec, root, storage, false)
|
|
}
|
|
|
|
fn active_alerts_inner(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
storage: &UserStorage,
|
|
include_updates: bool,
|
|
) -> (Vec<ProactiveAlert>, ProactiveState) {
|
|
let now = now_unix();
|
|
let mut state = load_state(storage, root);
|
|
state.last_checked_unix = now;
|
|
let _ = save_state(exec, storage, &state);
|
|
|
|
if !state.is_active(now) {
|
|
return (Vec::new(), state);
|
|
}
|
|
|
|
let all = if include_updates {
|
|
check_system(exec, root)
|
|
} else {
|
|
check_system_quick(exec, root)
|
|
};
|
|
let filtered = all
|
|
.into_iter()
|
|
.filter(|a| !state.is_dismissed(&a.fingerprint))
|
|
.collect();
|
|
|
|
(filtered, state)
|
|
}
|
|
|
|
pub fn snooze(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
storage: &UserStorage,
|
|
duration_seconds: u64,
|
|
) -> Result<()> {
|
|
let mut state = load_state(storage, root);
|
|
state.snooze(duration_seconds, now_unix());
|
|
save_state(exec, storage, &state)
|
|
}
|
|
|
|
pub fn dismiss(exec: &Exec, root: &Path, storage: &UserStorage, fingerprint: &str) -> Result<()> {
|
|
let mut state = load_state(storage, root);
|
|
state.dismiss(fingerprint.to_string());
|
|
save_state(exec, storage, &state)
|
|
}
|
|
|
|
pub fn set_enabled(exec: &Exec, root: &Path, storage: &UserStorage, enabled: bool) -> Result<()> {
|
|
let mut state = load_state(storage, root);
|
|
state.enabled = enabled;
|
|
save_state(exec, storage, &state)
|
|
}
|
|
|
|
pub fn schedule_deferred(
|
|
exec: &Exec,
|
|
root: &Path,
|
|
storage: &UserStorage,
|
|
schedule: DeferredSchedule,
|
|
) -> Result<()> {
|
|
let mut state = load_state(storage, root);
|
|
state.deferred_schedule = Some(schedule);
|
|
save_state(exec, storage, &state)
|
|
}
|
|
|
|
pub fn cancel_deferred(exec: &Exec, root: &Path, storage: &UserStorage) -> Result<()> {
|
|
let mut state = load_state(storage, root);
|
|
state.deferred_schedule = None;
|
|
save_state(exec, storage, &state)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::command::RecordingRunner;
|
|
use crate::log::Logger;
|
|
use phoebus_core::Redactor;
|
|
use tempfile::tempdir;
|
|
|
|
fn make_exec(runner: RecordingRunner, root: &Path) -> Exec {
|
|
let temp = tempdir().unwrap().keep();
|
|
let logger = Logger::open(temp.join("l.log"), Redactor::new(), false);
|
|
Exec::new(Box::new(runner), logger, false, root.to_path_buf(), temp)
|
|
}
|
|
|
|
/// Storage owned by this process, so the ownership check passes.
|
|
fn local_storage(base: &Path) -> UserStorage {
|
|
UserStorage::with_base(crate::personal::effective_uid(), base)
|
|
}
|
|
|
|
#[test]
|
|
fn test_proactive_state_persistence_and_filtering() {
|
|
let temp = tempdir().unwrap();
|
|
let root = temp.path();
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner, root);
|
|
let store = local_storage(&tempdir().unwrap().keep());
|
|
|
|
let state = load_state(&store, root);
|
|
assert!(state.enabled);
|
|
assert!(state.dismissed_fingerprints.is_empty());
|
|
|
|
// Test dismiss
|
|
dismiss(&exec, root, &store, "reboot_required").unwrap();
|
|
let state2 = load_state(&store, root);
|
|
assert!(state2.is_dismissed("reboot_required"));
|
|
|
|
// Create reboot required file
|
|
let run_dir = root.join("run");
|
|
std::fs::create_dir_all(&run_dir).unwrap();
|
|
std::fs::write(run_dir.join("reboot-required"), "").unwrap();
|
|
|
|
// System check finds reboot alert
|
|
let all = check_system(&exec, root);
|
|
assert!(all.iter().any(|a| a.fingerprint == "reboot_required"));
|
|
|
|
// But active_alerts filters it out because it was dismissed
|
|
let (active, _) = active_alerts(&exec, root, &store);
|
|
assert!(!active.iter().any(|a| a.fingerprint == "reboot_required"));
|
|
|
|
// Test snooze
|
|
snooze(&exec, root, &store, 3600).unwrap();
|
|
let state3 = load_state(&store, root);
|
|
assert!(!state3.is_active(now_unix()));
|
|
}
|
|
|
|
#[test]
|
|
fn startup_alert_check_skips_expensive_update_discovery() {
|
|
let temp = tempdir().unwrap();
|
|
let root = temp.path();
|
|
let runner = RecordingRunner::new();
|
|
let exec = make_exec(runner.clone(), root);
|
|
let store = local_storage(&tempdir().unwrap().keep());
|
|
|
|
let _ = active_alerts_quick(&exec, root, &store);
|
|
assert_eq!(runner.programs(), vec!["df", "systemctl"]);
|
|
}
|
|
|
|
#[test]
|
|
fn alert_state_is_isolated_per_user() {
|
|
let temp = tempdir().unwrap();
|
|
let root = temp.path();
|
|
let base_a = tempdir().unwrap();
|
|
let base_b = tempdir().unwrap();
|
|
let exec = make_exec(RecordingRunner::new(), root);
|
|
let alice = local_storage(base_a.path());
|
|
let bob = local_storage(base_b.path());
|
|
|
|
dismiss(&exec, root, &alice, "reboot_required").unwrap();
|
|
|
|
assert!(load_state(&alice, root).is_dismissed("reboot_required"));
|
|
assert!(!load_state(&bob, root).is_dismissed("reboot_required"));
|
|
}
|
|
|
|
#[test]
|
|
fn a_failed_persistence_write_is_reported_truthfully() {
|
|
let temp = tempdir().unwrap();
|
|
let root = temp.path();
|
|
let exec = make_exec(RecordingRunner::new(), root);
|
|
// Point the state file at a path that cannot be created: a file used as
|
|
// a parent directory.
|
|
let blocker = temp.path().join("blocker");
|
|
std::fs::write(&blocker, "x").unwrap();
|
|
let store = local_storage(&blocker.join("not-a-dir"));
|
|
let err = dismiss(&exec, root, &store, "reboot_required").unwrap_err();
|
|
assert!(
|
|
matches!(err, ExecError::Io { .. } | ExecError::Core(_)),
|
|
"{err}"
|
|
);
|
|
}
|
|
}
|