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
//! [`CommandLauncher`] — how `bash` turns a script into a process (spec 3.1 §10 D1).
use ToolContext;
use ToolEgress;
use crateDevToolError;
use crateWorkspace;
/// Builds the process that runs one `bash` script.
///
/// [`BashTool`](crate::BashTool) owns everything around the process: the timeout ceiling,
/// the output cap, streaming, standard input, the new session and the process-group kill.
/// A launcher decides only what runs and where: the program and its arguments, the working
/// directory and the environment. That lets a sandbox wrap the script without forking the
/// tool. [`HostLauncher`] is the default and runs `sh -c <script>` on the host.
///
/// The returned command must not set a process group or a session: `BashTool` starts it in
/// a new session (`setsid`), which fails for a process that already leads a group. It also
/// replaces the command's standard streams and sets `kill_on_drop`. On Windows it replaces
/// the command's creation flags with `CREATE_SUSPENDED`, so the process runs only once it is
/// in the call's Job Object (spec 3.2 §10 W1). A Windows launcher whose confinement cannot
/// be set on a command, such as a restricted token, starts the process itself through
/// `spawn_contained` instead (spec 3.2 §10 W5).
///
/// Implements spec 3.1 §10 D1.
///
/// # Example
///
/// ```rust
/// use std::sync::Arc;
///
/// use adk_core::ToolContext;
/// use adk_devtools::{BashTool, CommandLauncher, DevToolError, Workspace};
///
/// /// Runs every script under `nice`, otherwise as the host launcher does.
/// struct NiceLauncher;
///
/// impl CommandLauncher for NiceLauncher {
/// fn command(
/// &self,
/// ws: &Workspace,
/// script: &str,
/// _ctx: &dyn ToolContext,
/// ) -> Result<tokio::process::Command, DevToolError> {
/// let mut cmd = tokio::process::Command::new("nice");
/// cmd.args(["sh", "-c", script]).current_dir(ws.root());
/// if !ws.inherits_env() {
/// cmd.env_clear().envs(ws.bash_env());
/// }
/// Ok(cmd)
/// }
///
/// fn isolation(&self) -> &str {
/// "host-nice"
/// }
/// }
///
/// let workspace = Workspace::new("./my-repo").allow_bash(true);
/// let bash = BashTool::new(workspace).with_launcher(Arc::new(NiceLauncher));
/// # let _ = bash;
/// ```
/// What a [`CommandLauncher`] keeps alive until a command's process has ended
/// ([`CommandLauncher::guarded_command`]).
pub type LaunchGuard = ;
/// Runs a script on the host as `sh -c <script>`, the default [`CommandLauncher`].
///
/// The working directory is the workspace root, and the environment follows the workspace
/// policy: cleared, then [`Workspace::bash_env`], unless [`Workspace::inherit_env`] is set.
/// The working directory is not an OS boundary; a command can reach absolute paths and the
/// network.
///
/// Implements spec 3.1 §10 D1.
///
/// # Example
///
/// ```rust
/// use adk_devtools::{CommandLauncher, HostLauncher};
///
/// assert_eq!(HostLauncher.isolation(), "host");
/// ```
;