phoebus_os/installer-rs/crates/phoebus-core/src/proactive.rs
2026-09-24 19:14:16 -07:00

131 lines
3.9 KiB
Rust

//! Quiet proactive assistance types and models.
//!
//! Phoebus detects a bounded set of deterministic system events:
//! - Updates ready for review
//! - Repeated service failures
//! - Low storage (< 5 GiB free)
//! - Failed/interrupted installations
//! - Required reboots
//!
//! All proactive events are deduplicated, snoozable, and can be disabled.
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProactiveCategory {
UpdatesReady,
ServiceFailure,
LowStorage,
FailedInstallation,
RebootRequired,
}
impl ProactiveCategory {
pub fn as_str(self) -> &'static str {
match self {
ProactiveCategory::UpdatesReady => "updates_ready",
ProactiveCategory::ServiceFailure => "service_failure",
ProactiveCategory::LowStorage => "low_storage",
ProactiveCategory::FailedInstallation => "failed_installation",
ProactiveCategory::RebootRequired => "reboot_required",
}
}
}
/// A single bounded proactive alert.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ProactiveAlert {
pub id: String,
pub category: ProactiveCategory,
pub title: String,
pub message: String,
pub severity: String, // "info", "warning", "critical"
pub actionable: bool,
pub suggested_phrase: Option<String>,
pub created_unix: u64,
pub fingerprint: String,
}
/// A deferred request (e.g. "update when I finish working").
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct DeferredSchedule {
pub id: String,
pub instruction: String,
pub conditions: String,
pub scheduled_unix: u64,
pub approval_digest: String,
}
/// Persistent state for proactive assistance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ProactiveState {
pub enabled: bool,
pub snoozed_until_unix: u64,
pub dismissed_fingerprints: Vec<String>,
pub last_checked_unix: u64,
pub deferred_schedule: Option<DeferredSchedule>,
}
impl Default for ProactiveState {
fn default() -> Self {
Self {
enabled: true,
snoozed_until_unix: 0,
dismissed_fingerprints: Vec::new(),
last_checked_unix: 0,
deferred_schedule: None,
}
}
}
impl ProactiveState {
pub fn is_active(&self, now_unix: u64) -> bool {
self.enabled && now_unix >= self.snoozed_until_unix
}
pub fn is_dismissed(&self, fingerprint: &str) -> bool {
self.dismissed_fingerprints.iter().any(|f| f == fingerprint)
}
pub fn dismiss(&mut self, fingerprint: String) {
if !self.is_dismissed(&fingerprint) {
self.dismissed_fingerprints.push(fingerprint);
}
// Bound the state so it cannot grow without limit.
const MAX_DISMISSED: usize = 256;
if self.dismissed_fingerprints.len() > MAX_DISMISSED {
let overflow = self.dismissed_fingerprints.len() - MAX_DISMISSED;
self.dismissed_fingerprints.drain(0..overflow);
}
}
pub fn snooze(&mut self, duration_seconds: u64, now_unix: u64) {
self.snoozed_until_unix = now_unix.saturating_add(duration_seconds);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn proactive_state_snooze_and_dismiss() {
let mut state = ProactiveState::default();
assert!(state.is_active(100));
state.snooze(300, 100);
assert!(!state.is_active(200));
assert!(state.is_active(401));
// Dismissals are bounded so persisted state cannot grow without limit.
for i in 0..400 {
state.dismiss(format!("fingerprint-{i}"));
}
assert!(state.dismissed_fingerprints.len() <= 256);
state.dismiss("fp1".into());
assert!(state.is_dismissed("fp1"));
assert!(!state.is_dismissed("fp2"));
}
}