1mod environment;
5
6use std::{
7 ffi::OsString,
8 future::Future,
9 io,
10 path::{Path, PathBuf},
11 time::Duration,
12};
13
14use thiserror::Error;
15use tokio_util::sync::CancellationToken;
16
17use crate::process::{self, ChildIdentity, Output};
18
19pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);
22pub const MAX_TIMEOUT: Duration = Duration::from_millis(crate::config::validation::MAX_TIMEOUT_MS);
24
25#[derive(Debug)]
29pub struct Command {
30 pub cwd: PathBuf,
32 command: process::Command,
33 directory: PathBuf,
34}
35
36impl Command {
37 pub fn prepare(
41 program: OsString,
42 args: Vec<OsString>,
43 workspace: &Path,
44 run_dir: &Path,
45 timeout: Option<Duration>,
46 ) -> Result<Self, Error> {
47 let timeout = timeout.unwrap_or(DEFAULT_TIMEOUT);
48 if timeout.is_zero() || timeout > MAX_TIMEOUT {
49 return Err(Error::InvalidTimeout);
50 }
51 let (cwd, directory, env) = environment::prepare(workspace, run_dir)?;
52 Ok(Self {
53 command: process::Command {
54 program,
55 args,
56 cwd: cwd.clone(),
57 env,
58 timeout,
59 },
60 cwd,
61 directory,
62 })
63 }
64
65 pub(crate) fn session_environment(&mut self, directory: &Path) {
66 crate::platform::environment::set(
67 &mut self.command.env,
68 "ARTIFACTIZE_SESSION_DIR",
69 directory,
70 );
71 }
72
73 pub(crate) fn inherit_session(
75 &mut self,
76 environment: &std::collections::BTreeMap<OsString, OsString>,
77 ) {
78 if let Some(value) =
79 crate::platform::environment::lookup(environment, "ARTIFACTIZE_SESSION_DIR")
80 {
81 crate::platform::environment::set(
82 &mut self.command.env,
83 "ARTIFACTIZE_SESSION_DIR",
84 value,
85 );
86 }
87 }
88
89 pub(crate) fn into_process(mut self) -> process::Command {
90 self.command.cwd = self.cwd;
91 self.command
92 }
93
94 pub fn directory(&self) -> &Path {
95 &self.directory
96 }
97
98 pub fn timeout(&self) -> Duration {
99 self.command.timeout
100 }
101
102 pub(crate) async fn output(
104 mut self,
105 input: Vec<u8>,
106 cancellation: CancellationToken,
107 ) -> Result<Output, process::Error> {
108 self.command.cwd = self.cwd;
109 process::run_unregistered(
110 self.command,
111 Some(input),
112 process::OUTPUT_LIMIT,
113 cancellation,
114 )
115 .await
116 }
117
118 pub(crate) async fn tool_output(
120 mut self,
121 input: Vec<u8>,
122 output_limit: usize,
123 cancellation: CancellationToken,
124 ) -> Result<Output, process::Error> {
125 self.command.cwd = self.cwd;
126 process::run_unregistered(self.command, Some(input), output_limit, cancellation).await
127 }
128}
129
130#[derive(
131 Debug,
132 Clone,
133 Copy,
134 PartialEq,
135 Eq,
136 PartialOrd,
137 Ord,
138 serde::Serialize,
139 serde::Deserialize,
140 clap::ValueEnum,
141)]
142#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
143#[value(rename_all = "SCREAMING_SNAKE_CASE")]
144pub enum Verdict {
145 Green,
146 Red,
147}
148
149impl Verdict {
150 pub fn as_str(self) -> &'static str {
151 match self {
152 Self::Green => "GREEN",
153 Self::Red => "RED",
154 }
155 }
156}
157
158impl std::fmt::Display for Verdict {
159 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160 formatter.write_str(self.as_str())
161 }
162}
163
164#[derive(Debug)]
165pub struct ReviewResult {
166 pub exit_code: i32,
167 pub output: Output,
168}
169
170impl ReviewResult {
171 pub fn verdict(&self) -> Verdict {
172 if self.exit_code == 0 {
173 Verdict::Green
174 } else {
175 Verdict::Red
176 }
177 }
178}
179
180#[derive(Debug)]
182pub enum Outcome {
183 Completed(ReviewResult),
184 OperationalError(Error),
185}
186
187#[derive(Debug, Error)]
188pub enum Error {
189 #[error("timeoutMs must be between 1 and 2147483647")]
190 InvalidTimeout,
191 #[error("runtime output directory must be outside the review workspace")]
192 OutputInsideWorkspace,
193 #[error("runtime environment preparation failed: {0}")]
194 Environment(#[from] io::Error),
195 #[error(transparent)]
196 Process(#[from] process::Error),
197 #[error("runtime process terminated without a verdict (signal {signal:?})")]
198 AbnormalExit { signal: Option<i32>, output: Output },
199}
200
201pub struct Execution {
203 cancellation: CancellationToken,
204 supervisor: tokio::task::JoinHandle<Outcome>,
205}
206
207impl Execution {
208 pub async fn wait(&mut self) -> Outcome {
211 let _cancel_on_drop = self.cancellation.clone().drop_guard();
212 crate::task::joined((&mut self.supervisor).await)
213 .unwrap_or_else(|error| Outcome::OperationalError(Error::Process(error.into())))
214 }
215}
216
217impl Drop for Execution {
218 fn drop(&mut self) {
219 self.cancellation.cancel();
220 }
221}
222
223pub async fn execute<F, R>(
226 command: Command,
227 cancellation: CancellationToken,
228 register: F,
229) -> Outcome
230where
231 F: FnOnce(ChildIdentity) -> R + Send + 'static,
232 R: Future<Output = io::Result<()>> + Send + 'static,
233{
234 start(command, cancellation, register).wait().await
235}
236
237pub fn start<F, R>(command: Command, cancellation: CancellationToken, register: F) -> Execution
239where
240 F: FnOnce(ChildIdentity) -> R + Send + 'static,
241 R: Future<Output = io::Result<()>> + Send + 'static,
242{
243 let cancellation = cancellation.child_token();
244 let supervisor = tokio::spawn(supervise(command, cancellation.clone(), register));
245 Execution {
246 cancellation,
247 supervisor,
248 }
249}
250
251async fn supervise<F, R>(
252 mut command: Command,
253 cancellation: CancellationToken,
254 register: F,
255) -> Outcome
256where
257 F: FnOnce(ChildIdentity) -> R + Send + 'static,
258 R: Future<Output = io::Result<()>> + Send + 'static,
259{
260 command.command.cwd = command.cwd;
261 let scratch = command.directory.join("tmp");
264 let result =
265 process::run_logged(command.command, command.directory, cancellation, register).await;
266 let cleanup = crate::task::joined(
267 tokio::task::spawn_blocking(move || crate::platform::remove_tree(&scratch)).await,
268 )
269 .map_err(io::Error::other)
270 .and_then(std::convert::identity);
271 if let Err(error) = cleanup
272 && error.kind() != io::ErrorKind::NotFound
273 && result.is_ok()
274 {
275 return Outcome::OperationalError(Error::Environment(error));
276 }
277 match result {
278 Ok(mut output) => {
279 output.stdout = clean_output(&output.stdout);
280 output.stderr = clean_output(&output.stderr);
281 match output.status.end() {
282 crate::platform::ProcessEnd::Exited(exit_code) => {
283 Outcome::Completed(ReviewResult { exit_code, output })
284 }
285 crate::platform::ProcessEnd::Signaled(signal) => {
286 Outcome::OperationalError(Error::AbnormalExit { signal, output })
287 }
288 }
289 }
290 Err(error) => Outcome::OperationalError(Error::Process(error)),
291 }
292}
293
294pub(crate) use artifactize_tools::result::clean_output;