Skip to main content

aither_sandbox/spawn/
ipc.rs

1//! The commands an embedder lets a shell's scripts call back into.
2
3use std::fmt;
4use std::sync::Arc;
5
6use super::LaunchInfo;
7
8/// Supplies the [`heel::IpcRouter`] a script of a shell can call back into
9/// the host's commands through.
10///
11/// A shell asks once per process it starts on a backend that can serve a
12/// router (the host and a container; a server has no way back to this
13/// machine), with what that launch is, so the commands it serves may depend
14/// on the job, the tool call and the mode. A subagent's shell asks the same
15/// provider as the shell that launched it.
16pub trait IpcProvider: Send + Sync + 'static {
17    /// The commands a process launched as `launch` may call, or `None` for a
18    /// process with no way back to the host.
19    fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter>;
20}
21
22/// Object-safe twin of [`IpcProvider`].
23trait IpcProviderImpl: Send + Sync {
24    fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter>;
25}
26
27impl<T: IpcProvider> IpcProviderImpl for T {
28    fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
29        IpcProvider::router(self, launch)
30    }
31}
32
33/// A type-erased [`IpcProvider`].
34#[derive(Clone)]
35pub struct AnyIpcProvider(Arc<dyn IpcProviderImpl>);
36
37impl AnyIpcProvider {
38    /// Erases `provider`.
39    #[must_use]
40    pub fn new(provider: impl IpcProvider) -> Self {
41        Self(Arc::new(provider))
42    }
43
44    /// The commands a process launched as `launch` may call.
45    #[must_use]
46    pub fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
47        self.0.router(launch)
48    }
49}
50
51impl fmt::Debug for AnyIpcProvider {
52    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53        f.write_str("AnyIpcProvider")
54    }
55}
56
57/// A provider for tests: it serves one command, `probe`, and records every
58/// launch it was asked about.
59#[cfg(test)]
60pub mod fixture {
61    use std::sync::{Arc, Mutex};
62
63    use super::{IpcProvider, LaunchInfo};
64
65    /// What `probe` answers.
66    pub const ANSWER: &str = "probe_ok";
67
68    struct Probe;
69
70    #[derive(serde::Deserialize, schemars::JsonSchema)]
71    struct ProbeArgs {}
72
73    impl heel::IpcCommand for Probe {
74        fn name(&self) -> std::borrow::Cow<'static, str> {
75            "probe".into()
76        }
77        type Args = ProbeArgs;
78        type Response = String;
79        fn handle(
80            &self,
81            _args: ProbeArgs,
82            _context: heel::IpcContext,
83        ) -> impl Future<Output = Result<String, heel::IpcError>> {
84            std::future::ready(Ok(ANSWER.into()))
85        }
86    }
87
88    #[derive(Clone, Default)]
89    pub struct Recording {
90        pub launches: Arc<Mutex<Vec<LaunchInfo>>>,
91    }
92
93    impl IpcProvider for Recording {
94        fn router(&self, launch: &LaunchInfo) -> Option<heel::IpcRouter> {
95            self.launches.lock().unwrap().push(launch.clone());
96            Some(heel::IpcRouter::new().register(Probe))
97        }
98    }
99}