aither-agent 0.18.0

AI agent framework with planning, memory, and tool execution for aither
Documentation
use std::sync::{Arc, Mutex};
use std::time::Duration;

use aither_sandbox::ForegroundWait;

use super::*;
use crate::clock::ManualClock;

const NORMAL_WAIT: Duration = Duration::from_secs(10);
const HIGH_LOAD_WAIT: Duration = Duration::from_secs(90);

/// A host whose readings the test sets.
#[derive(Clone)]
struct ScriptedHost {
    memory: Arc<Mutex<MemoryPressure>>,
    load: Arc<Mutex<f64>>,
}

impl ScriptedHost {
    fn set_memory(&self, level: MemoryPressure) {
        *self.memory.lock().unwrap() = level;
    }

    fn set_load(&self, load: f64) {
        *self.load.lock().unwrap() = load;
    }
}

impl HostReader for ScriptedHost {
    fn memory_pressure(&mut self) -> Result<MemoryPressure, ReadError> {
        Ok(*self.memory.lock().unwrap())
    }

    fn machine_load(&mut self) -> Result<MachineLoad, ReadError> {
        Ok(MachineLoad(*self.load.lock().unwrap()))
    }
}

/// A monitor over a scripted host and a clock the test advances.
struct Harness {
    host: ScriptedHost,
    clock: ManualClock,
    any_clock: AnyClock,
    wait: ForegroundWait,
    monitor: HostLoadMonitor<ScriptedHost>,
}

impl Harness {
    fn new() -> Self {
        let host = ScriptedHost {
            memory: Arc::new(Mutex::new(MemoryPressure::Normal)),
            load: Arc::new(Mutex::new(0.1)),
        };
        let clock = ManualClock::new();
        let wait = ForegroundWait::new(NORMAL_WAIT);
        let config = HostLoadConfig::new()
            .memory_threshold(MemoryPressure::Warning)
            .load_threshold(MachineLoad(1.0))
            .start_after(Duration::from_secs(30))
            .end_after(Duration::from_secs(60))
            .high_load_foreground_wait(HIGH_LOAD_WAIT);
        Self {
            monitor: HostLoadMonitor::new(host.clone(), config, wait.clone()),
            any_clock: AnyClock::new(clock.clone()),
            host,
            clock,
            wait,
        }
    }

    /// Reads the host now and every 10 seconds for `secs` seconds, and
    /// returns the events raised. A reading that follows another at the same
    /// instant is how a change of the host takes effect.
    fn pass(&mut self, secs: u64) -> Vec<HostLoadEvent> {
        let mut events = self.monitor.tick(&self.any_clock);
        for _ in 0..secs / 10 {
            self.clock.advance(Duration::from_secs(10));
            events.extend(self.monitor.tick(&self.any_clock));
        }
        events
    }
}

#[test]
fn sustained_memory_pressure_yields_one_start_and_one_end() {
    let mut host = Harness::new();
    host.host.set_memory(MemoryPressure::Warning);

    let started = host.pass(30);
    assert_eq!(
        started,
        [HostLoadEvent::MemoryPressureHigh {
            level: MemoryPressure::Warning
        }]
    );

    host.host.set_memory(MemoryPressure::Normal);
    let ended = host.pass(60);
    assert_eq!(
        ended,
        [HostLoadEvent::MemoryPressureEnded {
            level: MemoryPressure::Normal
        }]
    );
}

#[test]
fn sustained_machine_load_yields_one_start_and_one_end() {
    let mut host = Harness::new();
    host.host.set_load(1.8);

    let started = host.pass(30);
    assert_eq!(
        started,
        [HostLoadEvent::LoadHigh {
            load: MachineLoad(1.8),
            foreground_wait: HIGH_LOAD_WAIT
        }]
    );

    host.host.set_load(0.2);
    let ended = host.pass(60);
    assert_eq!(
        ended,
        [HostLoadEvent::LoadEnded {
            load: MachineLoad(0.2),
            foreground_wait: NORMAL_WAIT
        }]
    );
}

#[test]
fn memory_pressure_and_load_are_independent() {
    let mut host = Harness::new();
    host.host.set_memory(MemoryPressure::Critical);
    assert_eq!(host.pass(30).len(), 1);

    // Load starts later and ends while the memory pressure persists.
    host.host.set_load(2.0);
    let events = host.pass(30);
    assert!(
        matches!(events.as_slice(), [HostLoadEvent::LoadHigh { .. }]),
        "{events:?}"
    );
    host.host.set_load(0.0);
    let events = host.pass(60);
    assert!(
        matches!(events.as_slice(), [HostLoadEvent::LoadEnded { .. }]),
        "{events:?}"
    );

    host.host.set_memory(MemoryPressure::Normal);
    let events = host.pass(60);
    assert!(
        matches!(
            events.as_slice(),
            [HostLoadEvent::MemoryPressureEnded { .. }]
        ),
        "{events:?}"
    );
}

#[test]
fn a_spike_shorter_than_the_start_window_yields_no_alert() {
    let mut host = Harness::new();
    host.host.set_memory(MemoryPressure::Critical);
    host.host.set_load(3.0);
    assert_eq!(host.pass(20), Vec::new());
    host.host.set_memory(MemoryPressure::Normal);
    host.host.set_load(0.1);
    assert_eq!(host.pass(120), Vec::new(), "the spike left nothing to end");
    assert_eq!(host.wait.get(), NORMAL_WAIT);

    // The spike's earlier seconds do not count towards a later one.
    host.host.set_load(3.0);
    assert_eq!(host.pass(20), Vec::new());
}

#[test]
fn a_dip_shorter_than_the_end_window_does_not_end_the_condition() {
    let mut host = Harness::new();
    host.host.set_load(2.0);
    assert_eq!(host.pass(30).len(), 1);

    host.host.set_load(0.1);
    assert_eq!(host.pass(40), Vec::new());
    host.host.set_load(2.0);
    assert_eq!(host.pass(120), Vec::new());
    assert_eq!(host.wait.get(), HIGH_LOAD_WAIT);
}

#[test]
fn nothing_is_raised_while_a_condition_persists() {
    let mut host = Harness::new();
    host.host.set_memory(MemoryPressure::Warning);
    host.host.set_load(2.0);
    assert_eq!(host.pass(30).len(), 2);
    assert_eq!(host.pass(3_600), Vec::new());
}

#[test]
fn the_foreground_window_is_longer_only_while_high_load_lasts() {
    let mut host = Harness::new();
    host.host.set_load(2.0);
    host.pass(20);
    assert_eq!(host.wait.get(), NORMAL_WAIT, "not before the load starts");
    host.pass(10);
    assert_eq!(host.wait.get(), HIGH_LOAD_WAIT);

    host.host.set_load(0.1);
    host.pass(50);
    assert_eq!(host.wait.get(), HIGH_LOAD_WAIT, "not before the load ends");
    host.pass(10);
    assert_eq!(host.wait.get(), NORMAL_WAIT);

    // Memory pressure never touches the window.
    host.host.set_memory(MemoryPressure::Critical);
    host.pass(60);
    assert_eq!(host.wait.get(), NORMAL_WAIT);
}

#[test]
fn a_failed_reading_leaves_the_condition_as_it_was() {
    struct Failing;
    impl HostReader for Failing {
        fn memory_pressure(&mut self) -> Result<MemoryPressure, ReadError> {
            Err(ReadError::Unsupported { what: "memory" })
        }
        fn machine_load(&mut self) -> Result<MachineLoad, ReadError> {
            Err(ReadError::Unsupported { what: "load" })
        }
    }
    let clock = ManualClock::new();
    let any_clock = AnyClock::new(clock.clone());
    let mut monitor = HostLoadMonitor::new(
        Failing,
        HostLoadConfig::new(),
        ForegroundWait::new(NORMAL_WAIT),
    );
    for _ in 0..20 {
        clock.advance(Duration::from_secs(10));
        assert_eq!(monitor.tick(&any_clock), Vec::new());
    }
}

#[tokio::test]
async fn the_task_delivers_events_once_and_stops_with_the_receiver() {
    let host = ScriptedHost {
        memory: Arc::new(Mutex::new(MemoryPressure::Critical)),
        load: Arc::new(Mutex::new(0.0)),
    };
    // Sustained from the first reading.
    let config = HostLoadConfig::new()
        .start_after(Duration::ZERO)
        .poll_interval(Duration::from_millis(5));
    let monitor = HostLoadMonitor::new(host, config, ForegroundWait::new(NORMAL_WAIT));
    let events = spawn(
        &executor_core::tokio::TokioGlobal,
        monitor,
        AnyClock::default(),
    );

    let first = tokio::time::timeout(Duration::from_secs(10), events.recv())
        .await
        .expect("the event arrives")
        .expect("the task is running");
    assert_eq!(
        first,
        HostLoadEvent::MemoryPressureHigh {
            level: MemoryPressure::Critical
        }
    );
    tokio::time::sleep(Duration::from_millis(100)).await;
    assert!(events.is_empty(), "a persisting condition raises nothing");
}

#[test]
fn the_alerts_name_the_condition_and_the_window() {
    let high = HostLoadEvent::LoadHigh {
        load: MachineLoad(1.84),
        foreground_wait: Duration::from_secs(90),
    }
    .alert_text();
    assert!(high.contains("load is high (184%"), "{high}");
    assert!(high.contains("up to 90s"), "{high}");

    let memory = HostLoadEvent::MemoryPressureHigh {
        level: MemoryPressure::Critical,
    }
    .alert_text();
    assert!(
        memory.contains("high memory pressure (critical)"),
        "{memory}"
    );

    let ended = HostLoadEvent::LoadEnded {
        load: MachineLoad(0.2),
        foreground_wait: Duration::from_secs(10),
    }
    .alert_text();
    assert!(
        ended.contains("has ended") && ended.contains("10s"),
        "{ended}"
    );
}