Skip to main content

aither_sandbox/
limits.rs

1//! The time and size budgets of a shell, set by its embedder.
2
3use std::sync::Arc;
4use std::sync::atomic::{AtomicU64, Ordering};
5use std::time::Duration;
6
7use crate::foreground_wait::ForegroundWait;
8use crate::memory::ResponseWindow;
9use crate::subagent::ImportBudget;
10
11/// A duration adjustable while the shell runs. Every clone shares it.
12#[derive(Debug, Clone)]
13pub struct LiveDuration(Arc<AtomicU64>);
14
15impl LiveDuration {
16    /// A duration of `duration`.
17    #[must_use]
18    pub fn new(duration: Duration) -> Self {
19        Self(Arc::new(AtomicU64::new(Self::nanos(duration))))
20    }
21
22    fn nanos(duration: Duration) -> u64 {
23        u64::try_from(duration.as_nanos()).expect("a budget fits in u64 nanoseconds")
24    }
25
26    /// Changes the duration for every handle.
27    pub fn set(&self, duration: Duration) {
28        self.0.store(Self::nanos(duration), Ordering::Relaxed);
29    }
30
31    /// The duration now.
32    #[must_use]
33    pub fn get(&self) -> Duration {
34        Duration::from_nanos(self.0.load(Ordering::Relaxed))
35    }
36}
37
38/// The budgets a shell runs its scripts under.
39///
40/// There is no `Default`: the values are the embedder's, and each is a live
41/// handle, so changing one reaches the shells already running.
42#[derive(Debug, Clone)]
43pub struct ShellLimits {
44    /// How long a foreground call waits before a still-running script
45    /// promotes to a job.
46    pub foreground_wait: ForegroundWait,
47    /// The largest `timeout` a script accepts.
48    pub timeout_cap: LiveDuration,
49    /// How long a job runs before it raises one [`ShellEvent::LongRunning`].
50    ///
51    /// [`ShellEvent::LongRunning`]: crate::ShellEvent::LongRunning
52    pub long_job_alert: LiveDuration,
53    /// The most bytes a subagent's sandbox may send the host while its
54    /// commits are imported.
55    pub subagent_import: ImportBudget,
56    /// How long the agent has to act on a final memory warning.
57    pub response_window: ResponseWindow,
58}
59
60#[cfg(any(test, feature = "test-budgets"))]
61pub mod fixture {
62    use std::time::Duration;
63
64    use super::{ForegroundWait, ImportBudget, LiveDuration, ResponseWindow, ShellLimits};
65
66    /// Limits for tests, whose numbers stand for nothing.
67    pub fn limits() -> ShellLimits {
68        ShellLimits {
69            foreground_wait: ForegroundWait::new(Duration::from_secs(10)),
70            timeout_cap: LiveDuration::new(Duration::from_secs(120 * 60)),
71            long_job_alert: LiveDuration::new(Duration::from_secs(40 * 60)),
72            subagent_import: ImportBudget::new(1024 * 1024 * 1024),
73            response_window: ResponseWindow::new(Duration::from_secs(120)),
74        }
75    }
76}