aither-sandbox 0.31.0

Terminal-first command execution with heel sandboxing
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
//! The facts a shell raises about its jobs.
//!
//! A [`ShellEvent`] carries no text: the embedder turns each fact into the
//! alert, reminder or wake its agent reads. Every event reaches exactly one
//! reader of the [`ShellEvents`] it was raised on: clones share their queues,
//! so an embedder that aggregates the jobs of several shells hands each the
//! same [`ShellEvents`] and reads them all from one place.

use std::time::Duration;

use aither_core::llm::{AgentPath, CallIdentity, StoredFile};
use async_channel::{Receiver, Sender};
use futures_lite::future::or;

use crate::job_registry::PendingCompletion;
use crate::monitor::{MonitorEnded, MonitorIncrement, MonitorStalled};
use crate::process_tree::JobProcess;
use crate::terminal::{CompletedCommand, TerminalError, TimeoutKill};

/// A job the shell has just detached: started with `background`, or promoted
/// past [`ShellLimits::foreground_wait`](crate::ShellLimits).
///
/// A job raises exactly one.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct JobStarted {
    pub(crate) agent: AgentPath,
    pub(crate) task_id: String,
    pub(crate) output_preview: String,
    pub(crate) output_file: StoredFile,
}

impl JobStarted {
    /// The agent whose shell raised the event.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// Task ID of the job, a word leased from the shell's id allocator.
    #[must_use]
    pub fn task_id(&self) -> &str {
        &self.task_id
    }

    /// First lines of the redirected output; empty when nothing was written
    /// yet.
    #[must_use]
    pub fn output_preview(&self) -> &str {
        &self.output_preview
    }

    /// File receiving the full redirected output.
    #[must_use]
    pub const fn output_file(&self) -> &StoredFile {
        &self.output_file
    }
}

/// A job that ran to its end: one promoted past the foreground window or
/// started in the background. A script whose call returned its result, and
/// every [`Shell::run`](crate::Shell::run), raises none.
#[derive(Debug)]
#[non_exhaustive]
pub struct JobEnded {
    pub(crate) agent: AgentPath,
    pub(crate) info: crate::JobInfo,
    pub(crate) result: Result<CompletedCommand, TerminalError>,
    pub(crate) timeout_kill: Option<TimeoutKill>,
    pub(crate) pending: Option<PendingCompletion>,
}

impl JobEnded {
    /// The agent whose shell raised the event.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// The job as it ended: its task id (a word leased from the shell's id
    /// allocator), script, status with the cause of a kill, and the path of
    /// its stored output.
    #[must_use]
    pub const fn info(&self) -> &crate::JobInfo {
        &self.info
    }

    /// The result of the execution: the output as the model reads it and
    /// the outages the job ran through.
    #[must_use]
    pub const fn result(&self) -> &Result<CompletedCommand, TerminalError> {
        &self.result
    }

    /// Set when the kill was the job's `timeout` bound passing.
    #[must_use]
    pub const fn timeout_kill(&self) -> Option<&TimeoutKill> {
        self.timeout_kill.as_ref()
    }

    /// The job, split into what it ended as, its result, the bound that
    /// killed it and the hold that keeps it in its [`Jobs`](crate::Jobs).
    ///
    /// The hold keeps the job listed, and its task id leased, until the
    /// consumer takes the completion by dropping it; it is `None` when a
    /// waiter ([`Job::wait`](crate::Job::wait)) took the completion first, so
    /// nothing is owed to the agent.
    pub fn into_parts(
        self,
    ) -> (
        crate::JobInfo,
        Result<CompletedCommand, TerminalError>,
        Option<TimeoutKill>,
        Option<PendingCompletion>,
    ) {
        (self.info, self.result, self.timeout_kill, self.pending)
    }
}

/// A job that crossed the long-running threshold; raised once per job.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct LongRunning {
    pub(crate) agent: AgentPath,
    pub(crate) task_id: String,
    pub(crate) script: String,
    pub(crate) after: Duration,
}

impl LongRunning {
    /// The agent whose shell raised the event.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// Task ID of the job.
    #[must_use]
    pub fn task_id(&self) -> &str {
        &self.task_id
    }

    /// The script the job runs.
    #[must_use]
    pub fn script(&self) -> &str {
        &self.script
    }

    /// How long the job had been running.
    #[must_use]
    pub const fn after(&self) -> Duration {
        self.after
    }
}

/// A job whose stdin pipe has a reader waiting for input; raised once per
/// blocking episode, so again only after the job was fed and blocks anew.
///
/// Only a job started in the background has a stdin pipe: a foreground
/// command reads the null device, and keeps it when it is promoted.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StdinBlocked {
    pub(crate) agent: AgentPath,
    pub(crate) task_id: String,
    pub(crate) script: String,
}

impl StdinBlocked {
    /// The agent whose shell raised the event.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// Task ID of the job.
    #[must_use]
    pub fn task_id(&self) -> &str {
        &self.task_id
    }

    /// The script the job runs.
    #[must_use]
    pub fn script(&self) -> &str {
        &self.script
    }
}

/// A job whose stdin watch failed; raised once, after which the job can no
/// longer report being blocked on stdin.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct StdinWatchFailed {
    pub(crate) agent: AgentPath,
    pub(crate) task_id: String,
    pub(crate) script: String,
    pub(crate) error: String,
}

impl StdinWatchFailed {
    /// The agent whose shell raised the event.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// Task ID of the job.
    #[must_use]
    pub fn task_id(&self) -> &str {
        &self.task_id
    }

    /// The script the job runs.
    #[must_use]
    pub fn script(&self) -> &str {
        &self.script
    }

    /// Why the watch failed.
    #[must_use]
    pub fn error(&self) -> &str {
        &self.error
    }
}

/// Processes a foreground command left running when its shell exited, killed
/// with it.
///
/// Raised once per command, at the moment they were killed.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct DescendantsKilled {
    pub(crate) agent: AgentPath,
    pub(crate) task_id: String,
    pub(crate) call: Option<CallIdentity>,
    pub(crate) processes: Vec<JobProcess>,
}

impl DescendantsKilled {
    /// The agent whose shell raised the event.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// Task ID of the command.
    #[must_use]
    pub fn task_id(&self) -> &str {
        &self.task_id
    }

    /// The tool call that ran the command, when it came from one.
    #[must_use]
    pub const fn call(&self) -> Option<&CallIdentity> {
        self.call.as_ref()
    }

    /// Every process killed.
    #[must_use]
    pub fn processes(&self) -> &[JobProcess] {
        &self.processes
    }
}

/// A fact about a job of a shell.
#[derive(Debug)]
#[non_exhaustive]
pub enum ShellEvent {
    /// A job was detached as a background task.
    JobStarted(JobStarted),
    /// A job ended; boxed to keep the enum small.
    JobEnded(Box<JobEnded>),
    /// A job crossed the long-running threshold.
    LongRunning(LongRunning),
    /// A job is waiting for input on its stdin.
    StdinBlocked(StdinBlocked),
    /// Watching a job's stdin failed.
    StdinWatchFailed(StdinWatchFailed),
    /// A foreground command's shell exited while descendants were still
    /// running, and they were killed.
    DescendantsKilled(DescendantsKilled),
    /// A monitor collected new output lines from its watched job.
    MonitorIncrement(MonitorIncrement),
    /// A monitor saw no new output for its stall window.
    MonitorStalled(MonitorStalled),
    /// A monitor ended, reporting why.
    MonitorEnded(MonitorEnded),
}

impl ShellEvent {
    /// The agent whose shell raised the event: the root path for the main
    /// shell, the subagent's path for a subagent's shell.
    #[must_use]
    pub const fn agent(&self) -> &AgentPath {
        match self {
            Self::JobStarted(event) => event.agent(),
            Self::JobEnded(event) => event.agent(),
            Self::LongRunning(event) => event.agent(),
            Self::StdinBlocked(event) => event.agent(),
            Self::StdinWatchFailed(event) => event.agent(),
            Self::DescendantsKilled(event) => event.agent(),
            Self::MonitorIncrement(event) => event.agent(),
            Self::MonitorStalled(event) => event.agent(),
            Self::MonitorEnded(event) => event.agent(),
        }
    }
}

/// The raising half of a shell's events, cloned into every task that reports
/// a fact.
///
/// Events of different kinds queue apart, so a reader of one kind never waits
/// behind another; [`ShellEvents`] reads them back in the order a job lives
/// them: started, then the facts about it, then its end. It knows the agent
/// whose shell raises through it, which every event it builds names.
#[derive(Clone, Debug)]
pub struct EventSender {
    agent: AgentPath,
    started: Sender<ShellEvent>,
    facts: Sender<ShellEvent>,
    ended: Sender<ShellEvent>,
}

impl EventSender {
    /// The agent whose shell this raises for.
    pub(crate) const fn agent(&self) -> &AgentPath {
        &self.agent
    }

    /// Raises `event`.
    ///
    /// # Errors
    ///
    /// Returns the event when every [`ShellEvents`] of its queue is gone.
    pub(crate) async fn send(
        &self,
        event: ShellEvent,
    ) -> Result<(), Box<async_channel::SendError<ShellEvent>>> {
        match event {
            ShellEvent::JobStarted(_) => self.started.send(event).await,
            ShellEvent::JobEnded(_) => self.ended.send(event).await,
            ShellEvent::LongRunning(_)
            | ShellEvent::StdinBlocked(_)
            | ShellEvent::StdinWatchFailed(_)
            | ShellEvent::DescendantsKilled(_)
            | ShellEvent::MonitorIncrement(_)
            | ShellEvent::MonitorStalled(_)
            | ShellEvent::MonitorEnded(_) => self.facts.send(event).await,
        }
        .map_err(Box::new)
    }
}

/// One queue of events: both halves, so it never closes while a
/// [`ShellEvents`] holds it.
#[derive(Clone, Debug)]
struct Queue {
    tx: Sender<ShellEvent>,
    rx: Receiver<ShellEvent>,
}

impl Queue {
    fn new() -> Self {
        let (tx, rx) = async_channel::unbounded();
        Self { tx, rx }
    }
}

/// The events of one or several shells, and where they are read.
///
/// Cloning shares the queues: an event goes to one of the clones that read. A
/// handle owns both halves of its queues, so they never close: [`recv`]
/// waits for as long as the embedder keeps the handle.
///
/// A shell built [with a handle](crate::ShellBuilder::events) raises its
/// events on it, and so does every subagent shell it launches.
///
/// [`recv`]: Self::recv
#[derive(Clone, Debug)]
pub struct ShellEvents {
    started: Queue,
    facts: Queue,
    ended: Queue,
}

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

impl ShellEvents {
    /// A handle with no events raised on it yet.
    #[must_use]
    pub fn new() -> Self {
        Self {
            started: Queue::new(),
            facts: Queue::new(),
            ended: Queue::new(),
        }
    }

    /// A raising half of these queues, for the shell of `agent`.
    pub(crate) fn sender(&self, agent: AgentPath) -> EventSender {
        EventSender {
            agent,
            started: self.started.tx.clone(),
            facts: self.facts.tx.clone(),
            ended: self.ended.tx.clone(),
        }
    }

    /// The next event already raised, without waiting.
    #[must_use]
    pub fn try_recv(&self) -> Option<ShellEvent> {
        self.started
            .rx
            .try_recv()
            .or_else(|_| self.facts.rx.try_recv())
            .or_else(|_| self.ended.rx.try_recv())
            .ok()
    }

    /// Waits for the next event.
    ///
    /// Cancel-safe: an event is taken only when this future completes, so a
    /// caller racing it against other work loses nothing.
    ///
    /// # Panics
    /// Never in practice: the handle holds the senders of its own queues, so
    /// they cannot close.
    pub async fn recv(&self) -> ShellEvent {
        or(
            self.started.rx.recv(),
            or(self.facts.rx.recv(), self.ended.rx.recv()),
        )
        .await
        .expect("a handle holds the senders of its own queues")
    }

    /// Waits for the next event, for at most `duration`.
    pub async fn recv_timeout(&self, duration: Duration) -> Option<ShellEvent> {
        or(async { Some(self.recv().await) }, async {
            async_io::Timer::after(duration).await;
            None
        })
        .await
    }

    /// Whether no event is waiting.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.started.rx.is_empty() && self.facts.rx.is_empty() && self.ended.rx.is_empty()
    }

    /// The events of one kind, for tests that assert on a single stream of
    /// facts.
    #[cfg(test)]
    pub(crate) fn take_started(&self) -> Vec<JobStarted> {
        std::iter::from_fn(|| self.started.rx.try_recv().ok())
            .filter_map(|event| match event {
                ShellEvent::JobStarted(started) => Some(started),
                _ => None,
            })
            .collect()
    }

    /// Waits for the next job to end, leaving every other event queued.
    #[cfg(test)]
    pub(crate) async fn recv_completed(&self) -> JobEnded {
        match self
            .ended
            .rx
            .recv()
            .await
            .expect("a handle holds the senders of its own queues")
        {
            ShellEvent::JobEnded(ended) => *ended,
            _ => unreachable!("the ended queue holds ended jobs"),
        }
    }

    /// The jobs that ended and were not yet read.
    #[cfg(test)]
    pub(crate) fn take_completed(&self) -> Vec<JobEnded> {
        std::iter::from_fn(|| self.ended.rx.try_recv().ok())
            .filter_map(|event| match event {
                ShellEvent::JobEnded(ended) => Some(*ended),
                _ => None,
            })
            .collect()
    }

    /// The facts raised and not yet read.
    #[cfg(test)]
    pub(crate) fn take_job_alerts(&self) -> Vec<ShellEvent> {
        std::iter::from_fn(|| self.facts.rx.try_recv().ok()).collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::monitor::MonitorStalled;

    fn stalled(task_id: &str) -> ShellEvent {
        ShellEvent::MonitorStalled(MonitorStalled {
            agent: AgentPath::main(),
            task_id: task_id.to_string(),
            stall_secs: 1,
        })
    }

    #[tokio::test]
    async fn a_handle_waits_for_events_it_never_loses_the_queues_of() {
        let events = ShellEvents::new();
        let reader = events.clone();
        // Nothing is raised: the read waits, it neither ends nor spins.
        assert!(
            reader
                .recv_timeout(Duration::from_millis(50))
                .await
                .is_none()
        );
        events
            .sender(AgentPath::main())
            .send(stalled("late"))
            .await
            .unwrap();
        let ShellEvent::MonitorStalled(event) = reader.recv().await else {
            panic!("the raised event is the one read");
        };
        assert_eq!(event.task_id(), "late");
    }

    #[tokio::test]
    async fn senders_of_one_handle_reach_every_reader_of_it_once() {
        let events = ShellEvents::new();
        let (first, second) = (
            events.sender(AgentPath::main()),
            events.sender(AgentPath::main()),
        );
        first.send(stalled("one")).await.unwrap();
        second.send(stalled("two")).await.unwrap();
        let reader = events.clone();
        assert!(
            matches!(events.try_recv(), Some(ShellEvent::MonitorStalled(e)) if e.task_id() == "one")
        );
        assert!(
            matches!(reader.try_recv(), Some(ShellEvent::MonitorStalled(e)) if e.task_id() == "two")
        );
        assert!(events.is_empty());
    }
}