adk_devtools/launcher.rs
1//! [`CommandLauncher`] — how `bash` turns a script into a process (spec 3.1 §10 D1).
2
3use adk_core::ToolContext;
4use adk_core::egress::ToolEgress;
5
6use crate::error::DevToolError;
7use crate::workspace::Workspace;
8
9/// Builds the process that runs one `bash` script.
10///
11/// [`BashTool`](crate::BashTool) owns everything around the process: the timeout ceiling,
12/// the output cap, streaming, standard input, the new session and the process-group kill.
13/// A launcher decides only what runs and where: the program and its arguments, the working
14/// directory and the environment. That lets a sandbox wrap the script without forking the
15/// tool. [`HostLauncher`] is the default and runs `sh -c <script>` on the host.
16///
17/// The returned command must not set a process group or a session: `BashTool` starts it in
18/// a new session (`setsid`), which fails for a process that already leads a group. It also
19/// replaces the command's standard streams and sets `kill_on_drop`. On Windows it replaces
20/// the command's creation flags with `CREATE_SUSPENDED`, so the process runs only once it is
21/// in the call's Job Object (spec 3.2 §10 W1). A Windows launcher whose confinement cannot
22/// be set on a command, such as a restricted token, starts the process itself through
23/// `spawn_contained` instead (spec 3.2 §10 W5).
24///
25/// Implements spec 3.1 §10 D1.
26///
27/// # Example
28///
29/// ```rust
30/// use std::sync::Arc;
31///
32/// use adk_core::ToolContext;
33/// use adk_devtools::{BashTool, CommandLauncher, DevToolError, Workspace};
34///
35/// /// Runs every script under `nice`, otherwise as the host launcher does.
36/// struct NiceLauncher;
37///
38/// impl CommandLauncher for NiceLauncher {
39/// fn command(
40/// &self,
41/// ws: &Workspace,
42/// script: &str,
43/// _ctx: &dyn ToolContext,
44/// ) -> Result<tokio::process::Command, DevToolError> {
45/// let mut cmd = tokio::process::Command::new("nice");
46/// cmd.args(["sh", "-c", script]).current_dir(ws.root());
47/// if !ws.inherits_env() {
48/// cmd.env_clear().envs(ws.bash_env());
49/// }
50/// Ok(cmd)
51/// }
52///
53/// fn isolation(&self) -> &str {
54/// "host-nice"
55/// }
56/// }
57///
58/// let workspace = Workspace::new("./my-repo").allow_bash(true);
59/// let bash = BashTool::new(workspace).with_launcher(Arc::new(NiceLauncher));
60/// # let _ = bash;
61/// ```
62pub trait CommandLauncher: Send + Sync {
63 /// Builds the command that runs `script` for the call described by `ctx`.
64 ///
65 /// # Errors
66 ///
67 /// Returns a [`DevToolError`] when the command cannot be built, for example when a
68 /// sandbox the launcher requires is unavailable. The call then fails without running
69 /// anything.
70 fn command(
71 &self,
72 ws: &Workspace,
73 script: &str,
74 ctx: &dyn ToolContext,
75 ) -> Result<tokio::process::Command, DevToolError>;
76
77 /// A short label for the isolation this launcher provides, such as `"host"`.
78 fn isolation(&self) -> &str;
79
80 /// Builds the command as [`command`](Self::command) does, with what must live until its
81 /// process has ended, such as the egress proxy the process reaches the network through.
82 /// [`BashTool`](crate::BashTool) holds the guard until the process exits. The default keeps
83 /// nothing; a wrapping launcher forwards its inner launcher's guard.
84 ///
85 /// # Errors
86 ///
87 /// As for [`command`](Self::command).
88 fn guarded_command(
89 &self,
90 ws: &Workspace,
91 script: &str,
92 ctx: &dyn ToolContext,
93 ) -> Result<(tokio::process::Command, Option<LaunchGuard>), DevToolError> {
94 Ok((self.command(ws, script, ctx)?, None))
95 }
96
97 /// What the commands this launcher builds reach on the network, which `bash` declares as
98 /// its [`Tool::egress`](adk_core::Tool::egress). The default,
99 /// [`ToolEgress::Undeclared`], is refused under a run egress policy; a wrapping launcher
100 /// forwards its inner launcher's.
101 fn egress(&self) -> ToolEgress {
102 ToolEgress::Undeclared
103 }
104
105 /// Starts the process that runs `script` itself, for a launcher whose confinement is set
106 /// when the process is created, such as a restricted token on Windows (spec 3.2 §10 W5).
107 ///
108 /// `None`, the default, has [`BashTool`](crate::BashTool) spawn
109 /// [`command`](Self::command)'s result instead. A launcher that returns a child must
110 /// have started its process in the child's Job Object, with its standard streams on the
111 /// child's pipes and `env` added to its environment last; `env` carries what a wrapping
112 /// launcher adds, as it would add it to a command, and `BashTool` passes none. `BashTool`
113 /// closes standard input at once and keeps the timeout ceiling, output cap, streaming and
114 /// the job's kill around the process, as for a command it spawns. The call runs on Tokio's
115 /// blocking pool, since starting a confined process can take long.
116 ///
117 /// # Errors
118 ///
119 /// `Some(Err(..))` fails the call without running anything, for example when the
120 /// sandbox cannot start the process.
121 #[cfg(windows)]
122 fn spawn_contained(
123 &self,
124 ws: &Workspace,
125 script: &str,
126 env: &[(String, String)],
127 ctx: &dyn ToolContext,
128 ) -> Option<Result<crate::ContainedChild, DevToolError>> {
129 let _ = (ws, script, env, ctx);
130 None
131 }
132}
133
134/// What a [`CommandLauncher`] keeps alive until a command's process has ended
135/// ([`CommandLauncher::guarded_command`]).
136pub type LaunchGuard = Box<dyn std::any::Any + Send + Sync>;
137
138/// Runs a script on the host as `sh -c <script>`, the default [`CommandLauncher`].
139///
140/// The working directory is the workspace root, and the environment follows the workspace
141/// policy: cleared, then [`Workspace::bash_env`], unless [`Workspace::inherit_env`] is set.
142/// The working directory is not an OS boundary; a command can reach absolute paths and the
143/// network.
144///
145/// Implements spec 3.1 §10 D1.
146///
147/// # Example
148///
149/// ```rust
150/// use adk_devtools::{CommandLauncher, HostLauncher};
151///
152/// assert_eq!(HostLauncher.isolation(), "host");
153/// ```
154#[derive(Debug, Clone, Copy, Default)]
155pub struct HostLauncher;
156
157impl CommandLauncher for HostLauncher {
158 fn command(
159 &self,
160 ws: &Workspace,
161 script: &str,
162 _ctx: &dyn ToolContext,
163 ) -> Result<tokio::process::Command, DevToolError> {
164 let mut cmd = tokio::process::Command::new("sh");
165 cmd.arg("-c").arg(script).current_dir(ws.root());
166 // The parent environment of an agent process routinely holds provider API keys,
167 // and `env` would print them. Pass through only what tools need, unless the
168 // workspace opts into inheriting.
169 if !ws.inherits_env() {
170 cmd.env_clear();
171 for (key, value) in ws.bash_env() {
172 cmd.env(key, value);
173 }
174 }
175 Ok(cmd)
176 }
177
178 fn isolation(&self) -> &str {
179 "host"
180 }
181}