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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//! Script policies: the embedder's rules on what a script may run.
//!
//! The sandbox decides where a script runs and what it can reach; whether a
//! script should run at all — a toolchain preference, a ban on killing by
//! name, a rule against polling, an approval for a server — is the
//! embedder's. Every [`ScriptPolicy`] given to the
//! [`ShellBuilder`](crate::ShellBuilder) sees each script before it runs, parsed when it
//! is shell, with the directory it starts in, the call it came from and the
//! policy's own state for the agent that runs it, and decides [`Pass`],
//! [`Allow`], [`Ask`] or [`Refuse`]. With no policy the sandbox refuses
//! nothing, and a script that stays inside the sandbox on the home
//! environment runs.
//!
//! A script that crosses the sandbox boundary — it runs unsandboxed or on a
//! remote location — runs only after an [`Allow`] or an approved [`Ask`];
//! with only [`Pass`]es it is asked.
//!
//! [`Pass`]: ScriptDecision::Pass
//! [`Allow`]: ScriptDecision::Allow
//! [`Ask`]: ScriptDecision::Ask
//! [`Refuse`]: ScriptDecision::Refuse

use std::fmt;
use std::future::Future;
use std::path::PathBuf;
use std::pin::Pin;
use std::sync::Arc;

use aither_core::llm::tool::{ApprovalRequest, CallIdentity};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::container::ContainerPath;
use crate::interpreter::Interpreter;
use crate::shell_syntax::{ShellParseError, ShellScript};
use crate::terminal::DirectoryResetReason;

/// How a script came to the terminal.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScriptOrigin {
    /// A `terminal` call the caller waits on.
    Foreground,
    /// A `terminal` call that detaches the script (`background: true`).
    Background,
    /// A [`Shell::run`](crate::Shell::run): the embedder's own script,
    /// waited on.
    Run,
}

// Whether a script runs inside the sandbox. Undocumented in rustdoc: the
// schema's descriptions are the embedder's.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[allow(missing_docs, reason = "the schema's descriptions are the embedder's")]
pub enum SandboxMode {
    // Inside the sandbox.
    #[default]
    Sandboxed,
    // Outside it, with the access of the user who runs the agent.
    Unsandboxed,
}

impl SandboxMode {
    /// The mode as a phrase in a sentence about a script.
    #[must_use]
    pub const fn description(self) -> &'static str {
        match self {
            Self::Sandboxed => "sandboxed",
            Self::Unsandboxed => "unsandboxed (no sandbox, full access)",
        }
    }
}

/// A directory a script starts or ends in, as the backend that runs it
/// names it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScriptDir {
    /// A directory of the host the agent runs on.
    Host(PathBuf),
    /// A directory in the container the script runs in.
    Container {
        /// The directory as the script sees it.
        inside: ContainerPath,
        /// The host side of that directory, for resolving what the script
        /// names against the host's files.
        host: PathBuf,
    },
    /// The script runs on a remote server, whose directories the sandbox
    /// does not follow.
    Remote {
        /// The name of the server, as the call gave it as its `location`.
        server: String,
    },
}

impl ScriptDir {
    /// The directory on the host the script's relative paths resolve
    /// against; `None` for a script that runs on a remote server.
    #[must_use]
    pub fn host(&self) -> Option<&std::path::Path> {
        match self {
            Self::Host(dir) | Self::Container { host: dir, .. } => Some(dir),
            Self::Remote { .. } => None,
        }
    }
}

/// A script about to run, as a [`ScriptPolicy`] and the shell's
/// [`TreatOutput`](crate::TreatOutput) see it.
#[derive(Debug, Clone, Copy)]
pub struct ScriptCheck<'a> {
    /// The id the script's task gets when it runs; the [`ScriptEnd`] that
    /// reports how it ended carries the same id.
    pub task_id: &'a str,
    /// The script as written.
    pub script: &'a str,
    /// The language it is written in.
    pub interpreter: Interpreter,
    /// The script parsed as shell: `None` for a script in another language,
    /// an error when it is not valid shell.
    pub shell: Option<Result<&'a ShellScript, &'a ShellParseError>>,
    /// How the script came to the terminal.
    pub origin: ScriptOrigin,
    /// Whether the script runs inside the sandbox.
    pub sandbox: SandboxMode,
    /// The configured server the script runs on; `None` for the home
    /// environment.
    pub location: Option<&'a str>,
    /// The directory the script starts in, fixed when the script was
    /// admitted: for a foreground script of the main agent the directory the
    /// shell carried from the previous one, for a run the directory it
    /// names, otherwise the terminal's own directory. The launch starts in
    /// the same directory.
    pub start: &'a ScriptDir,
    /// The tool call that carries the script and the agent that made it,
    /// when the caller identified one.
    pub call: Option<&'a CallIdentity>,
}

/// How a script a [`ScriptPolicy`] was asked about ended.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ScriptOutcome {
    /// It never started: a policy, the command gate or an approval refused
    /// it, or its launch could not be prepared.
    NotRun,
    /// It started but could not be run to an exit status.
    Failed,
    /// It exited with `code`.
    Exited {
        /// The exit status.
        code: i32,
    },
}

/// What a script did to the directory the shell carries to the next
/// foreground script.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Carry {
    /// The shell ended in another directory, which the next foreground
    /// script starts in.
    Moved,
    /// The directory the shell ended in is not carried; the next script
    /// starts where this one did.
    Reset(DirectoryResetReason),
    /// The shell ended where the script started.
    Unchanged,
    /// The script carries nothing: it ran in the background, failed to
    /// start or was refused, or the terminal carries no directory.
    NotCarried,
}

/// The end of a script a [`ScriptPolicy`] was asked about.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ScriptEnd<'a> {
    /// The task id the [`ScriptCheck`] carried.
    pub task_id: &'a str,
    /// How it ended.
    pub outcome: ScriptOutcome,
    /// The directory the terminal's next foreground script starts in now
    /// this one has ended, settled under the lock that updates the carried
    /// directory: the end of one script is the start of the next, whatever
    /// calls run in parallel.
    pub end: &'a ScriptDir,
    /// What the script did to the carried directory.
    pub carry: Carry,
}

/// A script a [`ScriptPolicy`] refused; the model reads [`Self::text`] as
/// the call's result.
///
/// Tools return it as their error, so an agent loop tells a policy's
/// refusal from any other failure by downcasting.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
#[error("{text}")]
pub struct ScriptRefusal {
    text: String,
}

impl ScriptRefusal {
    /// A refusal the model reads as `text`.
    #[must_use]
    pub fn new(text: impl Into<String>) -> Self {
        Self { text: text.into() }
    }

    /// The text the model reads.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }
}

/// What a [`ScriptPolicy`] decided about one script.
///
/// When several policies decide, the strictest wins: [`Refuse`], then
/// [`Ask`], then [`Allow`], then [`Pass`].
///
/// [`Pass`]: Self::Pass
/// [`Allow`]: Self::Allow
/// [`Ask`]: Self::Ask
/// [`Refuse`]: Self::Refuse
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScriptDecision {
    /// The policy has no opinion on the script.
    Pass,
    /// The policy approves the script: one that crosses the sandbox
    /// boundary runs without being asked.
    Allow,
    /// The script runs only if the approver approves `request`.
    Ask(ApprovalRequest),
    /// The script does not run; the model reads the refusal in its place.
    Refuse(ScriptRefusal),
}

impl ScriptDecision {
    const fn strictness(&self) -> u8 {
        match self {
            Self::Pass => 0,
            Self::Allow => 1,
            Self::Ask(_) => 2,
            Self::Refuse(_) => 3,
        }
    }

    /// Folds `decisions`, in registration order, into the strictest; of
    /// equally strict ones the first stands, so the model reads the first
    /// refusal and the approver is asked the first question.
    #[must_use]
    pub fn strictest(decisions: impl IntoIterator<Item = Self>) -> Self {
        decisions
            .into_iter()
            .fold(Self::Pass, |strictest, decision| {
                if decision.strictness() > strictest.strictness() {
                    decision
                } else {
                    strictest
                }
            })
    }
}

/// Decides whether a script may run, before it does.
///
/// Every script of the `terminal` tool, the main agent's and every
/// subagent's, and every [`Shell::run`](crate::Shell::run),
/// is put to every policy of its shell ahead of the approval it needs. The
/// policies of one shell decide concurrently and the strictest decision wins.
///
/// A policy keeps what it remembers in its [`State`](Self::State): every
/// agent's terminal, each subagent's included, starts with a fresh one and
/// shares it with nobody else. Calls run in parallel, so the state is shared
/// by reference and the policy chooses how to synchronise it.
pub trait ScriptPolicy: Send + Sync + 'static {
    /// What the policy remembers for one agent.
    type State: Default + Send + Sync + 'static;

    /// Decides on `script`.
    fn check(
        &self,
        script: &ScriptCheck<'_>,
        state: &Self::State,
    ) -> impl Future<Output = ScriptDecision> + Send;

    /// Told once how every script put to [`Self::check`] ended, whichever
    /// policy decided what: refused, never started, failed or exited. The
    /// result of a foreground script reaches the caller only after this
    /// returns, so a state it records holds for the agent's next call; a
    /// background script's end is told after its call returned.
    fn ended(&self, end: &ScriptEnd<'_>, state: &Self::State) -> impl Future<Output = ()> + Send {
        let _ = (end, state);
        std::future::ready(())
    }
}

/// Object-safe twin of [`ScriptPolicy`]: the policy, before it is bound to
/// an agent.
trait ScriptPolicyImpl: Send + Sync {
    fn bind(self: Arc<Self>) -> Arc<dyn BoundPolicyImpl>;
}

impl<T: ScriptPolicy> ScriptPolicyImpl for T {
    fn bind(self: Arc<Self>) -> Arc<dyn BoundPolicyImpl> {
        Arc::new(Bound {
            policy: self,
            state: T::State::default(),
        })
    }
}

/// A policy with one agent's state.
struct Bound<T: ScriptPolicy> {
    policy: Arc<T>,
    state: T::State,
}

trait BoundPolicyImpl: Send + Sync {
    fn check<'a>(
        &'a self,
        script: &'a ScriptCheck<'a>,
    ) -> Pin<Box<dyn Future<Output = ScriptDecision> + Send + 'a>>;

    fn ended<'a>(&'a self, end: &'a ScriptEnd<'a>)
    -> Pin<Box<dyn Future<Output = ()> + Send + 'a>>;
}

impl<T: ScriptPolicy> BoundPolicyImpl for Bound<T> {
    fn check<'a>(
        &'a self,
        script: &'a ScriptCheck<'a>,
    ) -> Pin<Box<dyn Future<Output = ScriptDecision> + Send + 'a>> {
        Box::pin(self.policy.check(script, &self.state))
    }

    fn ended<'a>(
        &'a self,
        end: &'a ScriptEnd<'a>,
    ) -> Pin<Box<dyn Future<Output = ()> + Send + 'a>> {
        Box::pin(self.policy.ended(end, &self.state))
    }
}

/// A type-erased [`ScriptPolicy`].
#[derive(Clone)]
pub struct AnyScriptPolicy(Arc<dyn ScriptPolicyImpl>);

impl AnyScriptPolicy {
    /// Erases `policy`.
    #[must_use]
    pub fn new(policy: impl ScriptPolicy) -> Self {
        Self(Arc::new(policy))
    }
}

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

/// A shell's policies, each bound to the state of the agent the shell
/// serves.
#[derive(Clone, Default)]
pub struct ScriptPolicies {
    policies: Arc<[AnyScriptPolicy]>,
    bound: Arc<[Arc<dyn BoundPolicyImpl>]>,
}

impl ScriptPolicies {
    /// `policies`, bound to a fresh agent.
    #[must_use]
    pub fn new(policies: Vec<AnyScriptPolicy>) -> Self {
        let policies: Arc<[AnyScriptPolicy]> = policies.into();
        Self {
            bound: Self::bind(&policies),
            policies,
        }
    }

    fn bind(policies: &[AnyScriptPolicy]) -> Arc<[Arc<dyn BoundPolicyImpl>]> {
        policies
            .iter()
            .map(|policy| policy.0.clone().bind())
            .collect()
    }

    /// The same policies, bound to another agent with fresh state.
    #[must_use]
    pub fn for_agent(&self) -> Self {
        Self {
            policies: self.policies.clone(),
            bound: Self::bind(&self.policies),
        }
    }

    /// Whether no policy is registered.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.bound.is_empty()
    }

    /// Puts `script` to every policy; the strictest decision wins, the
    /// first of equally strict ones in registration order.
    pub async fn check(&self, script: &ScriptCheck<'_>) -> ScriptDecision {
        let decisions =
            futures::future::join_all(self.bound.iter().map(|policy| policy.check(script))).await;
        ScriptDecision::strictest(decisions)
    }

    /// Tells every policy how a checked script ended, and where the
    /// terminal's next foreground script starts.
    pub async fn ended(
        &self,
        task_id: &str,
        outcome: ScriptOutcome,
        end: &ScriptDir,
        carry: Carry,
    ) {
        if self.is_empty() {
            return;
        }
        let end = ScriptEnd {
            task_id,
            outcome,
            end,
            carry,
        };
        futures::future::join_all(self.bound.iter().map(|policy| policy.ended(&end))).await;
    }
}

impl fmt::Debug for ScriptPolicies {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("ScriptPolicies")
            .field("policies", &self.policies.len())
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::{AtomicUsize, Ordering};

    use aither_core::llm::tool::ApprovalKind;

    use super::*;

    /// Refuses every script after the first `allowed` its agent ran.
    struct Quota {
        allowed: usize,
    }

    impl ScriptPolicy for Quota {
        type State = AtomicUsize;

        fn check(
            &self,
            _script: &ScriptCheck<'_>,
            state: &AtomicUsize,
        ) -> impl Future<Output = ScriptDecision> + Send {
            std::future::ready(if state.fetch_add(1, Ordering::SeqCst) < self.allowed {
                ScriptDecision::Allow
            } else {
                ScriptDecision::Refuse(ScriptRefusal::new("over quota"))
            })
        }
    }

    /// Decides the same way on every script.
    struct Always(ScriptDecision);

    impl ScriptPolicy for Always {
        type State = ();

        fn check(
            &self,
            _script: &ScriptCheck<'_>,
            (): &(),
        ) -> impl Future<Output = ScriptDecision> + Send {
            std::future::ready(self.0.clone())
        }
    }

    fn refuse(text: &str) -> ScriptDecision {
        ScriptDecision::Refuse(ScriptRefusal::new(text))
    }

    fn ask(subject: &str) -> ScriptDecision {
        ScriptDecision::Ask(ApprovalRequest::new(ApprovalKind::new("test"), subject))
    }

    fn check(task_id: &str) -> ScriptCheck<'_> {
        static START: std::sync::LazyLock<ScriptDir> =
            std::sync::LazyLock::new(|| ScriptDir::Host(PathBuf::from("/")));
        ScriptCheck {
            task_id,
            script: "true",
            interpreter: Interpreter::Shell,
            shell: None,
            origin: ScriptOrigin::Foreground,
            sandbox: SandboxMode::Sandboxed,
            location: None,
            start: &START,
            call: None,
        }
    }

    fn policies(decisions: impl IntoIterator<Item = ScriptDecision>) -> ScriptPolicies {
        ScriptPolicies::new(
            decisions
                .into_iter()
                .map(|decision| AnyScriptPolicy::new(Always(decision)))
                .collect(),
        )
    }

    #[tokio::test]
    async fn each_agent_binds_fresh_state() {
        let main = ScriptPolicies::new(vec![AnyScriptPolicy::new(Quota { allowed: 1 })]);
        let view = main.clone();
        let subagent = main.for_agent();
        assert_eq!(main.check(&check("a")).await, ScriptDecision::Allow);
        assert_eq!(main.check(&check("b")).await, refuse("over quota"));
        assert!(
            matches!(view.check(&check("c")).await, ScriptDecision::Refuse(_)),
            "a view of the main agent's registry shares its state"
        );
        assert_eq!(
            subagent.check(&check("d")).await,
            ScriptDecision::Allow,
            "the subagent's state is its own"
        );
    }

    #[tokio::test]
    async fn the_strictest_decision_wins() {
        let order = [
            ScriptDecision::Pass,
            ScriptDecision::Allow,
            ask("first question"),
            refuse("first refusal"),
        ];
        // Whatever the registration order, the stricter of two stands.
        for (weak, strong) in order
            .iter()
            .enumerate()
            .flat_map(|(i, weak)| order[i + 1..].iter().map(move |strong| (weak, strong)))
        {
            for pair in [[weak, strong], [strong, weak]] {
                let policies = policies(pair.into_iter().cloned());
                assert_eq!(&policies.check(&check("a")).await, strong);
            }
        }
        assert_eq!(
            policies([ScriptDecision::Allow, ScriptDecision::Pass])
                .check(&check("b"))
                .await,
            ScriptDecision::Allow
        );
    }

    #[tokio::test]
    async fn the_first_of_equally_strict_decisions_in_registration_order_stands() {
        let policies = policies([
            ScriptDecision::Allow,
            ask("first question"),
            ask("second question"),
            refuse("first refusal"),
            refuse("second refusal"),
        ]);
        assert_eq!(policies.check(&check("a")).await, refuse("first refusal"));
        let asking = super::ScriptPolicies::new(vec![
            AnyScriptPolicy::new(Always(ask("first question"))),
            AnyScriptPolicy::new(Always(ask("second question"))),
        ]);
        assert_eq!(asking.check(&check("b")).await, ask("first question"));
        assert!(ScriptPolicies::default().is_empty());
        assert_eq!(
            ScriptPolicies::default().check(&check("c")).await,
            ScriptDecision::Pass
        );
    }
}