aither-agent 0.17.0

AI agent framework with planning, memory, and tool execution for aither
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Alerts for high host memory pressure and high machine load.
//!
//! A [`HostLoadMonitor`] reads the host through a [`HostReader`] and turns
//! each of the two conditions into events: a condition *starts* when it stays
//! above its threshold for the start window, and *ends* when it stays below
//! for the end window, so a brief spike does not flap. The agent hears each
//! event once; nothing is injected while a condition persists.
//!
//! While high load lasts, the monitor also lengthens the terminal's
//! foreground window, because commands slowed by the load would otherwise
//! outlast the normal one and move to the background.
//!
//! The monitor never kills or pauses anything.

mod system;

use std::fmt;
use std::time::Duration;

use aither_sandbox::ForegroundWait;
use askama::Template;
use async_channel::{Receiver, Sender};

use crate::clock::{AnyClock, Instant};

pub use system::SystemReader;

/// How hard the host's memory is pressed.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MemoryPressure {
    /// Memory is plentiful.
    Normal,
    /// The host is reclaiming memory and tasks start to stall on it.
    Warning,
    /// The host is thrashing: nothing can make progress without memory.
    Critical,
}

impl fmt::Display for MemoryPressure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::Normal => "normal",
            Self::Warning => "warning",
            Self::Critical => "critical",
        })
    }
}

/// How much of the machine's CPU capacity is in demand.
///
/// `1.0` is a machine whose cores are all busy. What exceeds it is platform
/// specific: where the host reports a load average (macOS) it is that load
/// divided by the number of cores, so a queue shows as more than `1.0`;
/// on Linux it is the share of the last ten seconds during which runnable
/// tasks waited for a CPU (PSI `cpu`), and on Windows the share of CPU time
/// not spent idle since the previous reading.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct MachineLoad(pub f64);

impl MachineLoad {
    /// The load as a whole percentage of the machine's capacity.
    #[must_use]
    pub fn percent(self) -> u64 {
        // Truncation is the point: the percentage is for a human to read.
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let percent = (self.0 * 100.0).round().max(0.0) as u64;
        percent
    }
}

/// Why a reading could not be taken.
#[derive(Debug, thiserror::Error)]
pub enum ReadError {
    /// The host's source could not be read.
    #[error("reading {what} failed: {source}")]
    Io {
        /// The source that was read.
        what: &'static str,
        /// What the host answered.
        source: std::io::Error,
    },
    /// The host's source answered in a form not understood.
    #[error("{what} answered `{content}`, which is not understood")]
    Malformed {
        /// The source that was read.
        what: &'static str,
        /// What it answered.
        content: String,
    },
    /// The host offers no such source.
    #[error("{what} is not available on this host")]
    Unsupported {
        /// The source that is missing.
        what: &'static str,
    },
}

/// Reads the host's memory pressure and machine load.
///
/// Reads may block; the monitor runs them off the agent loop. A host
/// implements this for the platform it reads; tests inject their own.
pub trait HostReader: Send + 'static {
    /// The host's memory pressure now.
    ///
    /// # Errors
    ///
    /// Fails when the host's source cannot be read.
    fn memory_pressure(&mut self) -> Result<MemoryPressure, ReadError>;

    /// The machine's load now.
    ///
    /// # Errors
    ///
    /// Fails when the host's source cannot be read.
    fn machine_load(&mut self) -> Result<MachineLoad, ReadError>;
}

/// A type-erased [`HostReader`], what the builders store.
pub struct AnyHostReader(Box<dyn HostReader>);

impl AnyHostReader {
    /// Erases `reader`.
    #[must_use]
    pub fn new(reader: impl HostReader) -> Self {
        Self(Box::new(reader))
    }
}

impl HostReader for AnyHostReader {
    fn memory_pressure(&mut self) -> Result<MemoryPressure, ReadError> {
        self.0.memory_pressure()
    }

    fn machine_load(&mut self) -> Result<MachineLoad, ReadError> {
        self.0.machine_load()
    }
}

impl fmt::Debug for AnyHostReader {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("AnyHostReader")
    }
}

/// When the monitor calls a condition high, and how long it must hold.
#[derive(Debug, Clone, PartialEq)]
pub struct HostLoadConfig {
    /// Memory pressure at or above this level is high.
    pub memory_threshold: MemoryPressure,
    /// Machine load at or above this is high; see [`MachineLoad`] for what
    /// the platform measures.
    pub load_threshold: MachineLoad,
    /// How long a condition must stay above its threshold to start.
    pub start_after: Duration,
    /// How long a condition must stay below its threshold to end.
    pub end_after: Duration,
    /// How often the host is read.
    pub poll_interval: Duration,
    /// The terminal's foreground window while high machine load lasts.
    pub high_load_foreground_wait: Duration,
}

impl HostLoadConfig {
    /// The defaults for the platform the agent runs on.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            memory_threshold: MemoryPressure::Warning,
            load_threshold: MachineLoad(SystemReader::DEFAULT_LOAD_THRESHOLD),
            start_after: Duration::from_secs(30),
            end_after: Duration::from_secs(60),
            poll_interval: Duration::from_secs(5),
            high_load_foreground_wait: Duration::from_secs(60),
        }
    }

    /// Sets the memory pressure at which the condition is high.
    #[must_use]
    pub const fn memory_threshold(mut self, threshold: MemoryPressure) -> Self {
        self.memory_threshold = threshold;
        self
    }

    /// Sets the machine load at which the condition is high.
    #[must_use]
    pub const fn load_threshold(mut self, threshold: MachineLoad) -> Self {
        self.load_threshold = threshold;
        self
    }

    /// Sets how long a condition must stay above its threshold to start.
    #[must_use]
    pub const fn start_after(mut self, window: Duration) -> Self {
        self.start_after = window;
        self
    }

    /// Sets how long a condition must stay below its threshold to end.
    #[must_use]
    pub const fn end_after(mut self, window: Duration) -> Self {
        self.end_after = window;
        self
    }

    /// Sets how often the host is read.
    #[must_use]
    pub const fn poll_interval(mut self, interval: Duration) -> Self {
        self.poll_interval = interval;
        self
    }

    /// Sets the terminal's foreground window while high load lasts.
    #[must_use]
    pub const fn high_load_foreground_wait(mut self, window: Duration) -> Self {
        self.high_load_foreground_wait = window;
        self
    }
}

impl Default for HostLoadConfig {
    fn default() -> Self {
        Self::new()
    }
}

/// A condition starting or ending, as the agent is told.
#[derive(Debug, Clone, PartialEq)]
pub enum HostLoadEvent {
    /// Memory pressure stayed high for the start window.
    MemoryPressureHigh {
        /// The pressure when the window passed.
        level: MemoryPressure,
    },
    /// Memory pressure stayed below the threshold for the end window.
    MemoryPressureEnded {
        /// The pressure when the window passed.
        level: MemoryPressure,
    },
    /// Machine load stayed high for the start window.
    LoadHigh {
        /// The load when the window passed.
        load: MachineLoad,
        /// The terminal's foreground window while the load lasts.
        foreground_wait: Duration,
    },
    /// Machine load stayed below the threshold for the end window.
    LoadEnded {
        /// The load when the window passed.
        load: MachineLoad,
        /// The terminal's foreground window again.
        foreground_wait: Duration,
    },
}

/// The alert text for a host load event.
#[derive(Template)]
#[template(path = "host_load_alert.txt", escape = "none")]
struct HostLoadAlert<'a> {
    event: &'a HostLoadEvent,
}

impl HostLoadEvent {
    /// The event as the agent reads it.
    pub(crate) fn alert_text(&self) -> String {
        HostLoadAlert { event: self }
            .render()
            .expect("host load alert template must render")
    }
}

/// One condition's progress towards starting or ending.
#[derive(Debug, Default)]
struct Condition {
    active: bool,
    /// Since when readings have disagreed with the state: above the
    /// threshold while inactive, below it while active.
    contrary_since: Option<Instant>,
}

/// What a reading did to a condition.
#[derive(Debug, PartialEq, Eq)]
enum Transition {
    Started,
    Ended,
}

impl Condition {
    fn observe(
        &mut self,
        now: Instant,
        high: bool,
        start_after: Duration,
        end_after: Duration,
    ) -> Option<Transition> {
        if high == self.active {
            self.contrary_since = None;
            return None;
        }
        let since = *self.contrary_since.get_or_insert(now);
        let window = if self.active { end_after } else { start_after };
        if now.duration_since(since) < window {
            return None;
        }
        self.contrary_since = None;
        self.active = high;
        Some(if high {
            Transition::Started
        } else {
            Transition::Ended
        })
    }
}

/// One reading of the host; a part is missing when its source failed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct HostSample {
    memory: Option<MemoryPressure>,
    load: Option<MachineLoad>,
}

impl HostSample {
    /// A sample of the given readings.
    #[must_use]
    pub const fn new(memory: Option<MemoryPressure>, load: Option<MachineLoad>) -> Self {
        Self { memory, load }
    }
}

/// Watches the host and turns sustained memory pressure and machine load
/// into [`HostLoadEvent`]s.
#[derive(Debug)]
pub struct HostLoadMonitor<R> {
    reader: R,
    config: HostLoadConfig,
    foreground_wait: ForegroundWait,
    memory: Condition,
    load: Condition,
}

impl<R: HostReader> HostLoadMonitor<R> {
    /// A monitor reading `reader`, lengthening `foreground_wait` while high
    /// load lasts.
    #[must_use]
    pub fn new(reader: R, config: HostLoadConfig, foreground_wait: ForegroundWait) -> Self {
        Self {
            reader,
            config,
            foreground_wait,
            memory: Condition::default(),
            load: Condition::default(),
        }
    }

    /// Reads the host. A source that fails is logged and left out of the
    /// sample, which leaves its condition as it was.
    pub fn read(&mut self) -> HostSample {
        let memory = self
            .reader
            .memory_pressure()
            .inspect_err(|error| tracing::warn!(%error, "memory pressure could not be read"))
            .ok();
        let load = self
            .reader
            .machine_load()
            .inspect_err(|error| tracing::warn!(%error, "machine load could not be read"))
            .ok();
        HostSample { memory, load }
    }

    /// Applies a sample taken at `now`; returns the events it completes.
    ///
    /// Starting high load lengthens the terminal's foreground window, and
    /// ending it restores the normal one.
    pub fn observe(&mut self, now: Instant, sample: HostSample) -> Vec<HostLoadEvent> {
        let config = &self.config;
        let mut events = Vec::new();
        if let Some(level) = sample.memory {
            let high = level >= config.memory_threshold;
            match self
                .memory
                .observe(now, high, config.start_after, config.end_after)
            {
                Some(Transition::Started) => {
                    events.push(HostLoadEvent::MemoryPressureHigh { level });
                }
                Some(Transition::Ended) => {
                    events.push(HostLoadEvent::MemoryPressureEnded { level });
                }
                None => {}
            }
        }
        if let Some(load) = sample.load {
            let high = load >= config.load_threshold;
            match self
                .load
                .observe(now, high, config.start_after, config.end_after)
            {
                Some(Transition::Started) => {
                    let window = config.high_load_foreground_wait;
                    self.foreground_wait.extend(window);
                    events.push(HostLoadEvent::LoadHigh {
                        load,
                        foreground_wait: window,
                    });
                }
                Some(Transition::Ended) => {
                    self.foreground_wait.restore();
                    events.push(HostLoadEvent::LoadEnded {
                        load,
                        foreground_wait: self.foreground_wait.normal(),
                    });
                }
                None => {}
            }
        }
        events
    }

    /// Reads and observes in one step, as the monitor task does.
    pub fn tick(&mut self, clock: &AnyClock) -> Vec<HostLoadEvent> {
        let sample = self.read();
        self.observe(clock.now(), sample)
    }
}

/// Runs `monitor` on `executor` until the returned receiver is dropped.
///
/// The reads run on the blocking pool, so a slow host source never stalls
/// the agent loop or the executor.
pub fn spawn<R, E>(
    executor: &E,
    monitor: HostLoadMonitor<R>,
    clock: AnyClock,
) -> Receiver<HostLoadEvent>
where
    R: HostReader,
    E: executor_core::Executor,
{
    use executor_core::Task;

    let (sender, receiver) = async_channel::unbounded();
    executor.spawn(run(monitor, clock, sender)).detach();
    receiver
}

async fn run<R: HostReader>(
    mut monitor: HostLoadMonitor<R>,
    clock: AnyClock,
    events: Sender<HostLoadEvent>,
) {
    let interval = monitor.config.poll_interval;
    while !events.is_closed() {
        let (back, sample) = blocking::unblock(move || {
            let sample = monitor.read();
            (monitor, sample)
        })
        .await;
        monitor = back;
        for event in monitor.observe(clock.now(), sample) {
            tracing::info!(?event, "host load condition changed");
            if events.send(event).await.is_err() {
                return;
            }
        }
        async_io::Timer::after(interval).await;
    }
}

#[cfg(test)]
mod tests;