1use std::collections::HashMap;
10use std::path::{Path, PathBuf};
11use std::time::Duration;
12
13use serde_json::Value;
14
15use crate::models::{AssertionResult, Recipe, StepResult};
16
17pub type ExecutionResult = (
19 Vec<StepResult>,
20 Vec<AssertionResult>,
21 String,
22 Option<i32>,
23 String,
24);
25
26#[derive(Debug, thiserror::Error)]
28pub enum ExecutionError {
29 #[error("step error: {0}")]
31 Step(String),
32 #[error("session error: {0}")]
34 Session(String),
35 #[error("config error: {0}")]
37 Config(String),
38 #[error("timeout: {0}")]
40 Timeout(String),
41}
42
43fn assertions_with_implicit(recipe: &Recipe) -> Vec<Value> {
50 crate::assertions::evaluated_list(recipe)
51}
52
53pub trait ExecutionContext: Send {
58 fn create_session(
60 &mut self,
61 argv: Vec<String>,
62 cwd: Option<String>,
63 env: HashMap<String, String>,
64 cols: u16,
65 rows: u16,
66 cast_path: PathBuf,
67 ) -> Result<Box<dyn crate::terminal::Session>, ExecutionError>;
68
69 fn run_step(
71 &mut self,
72 session: &mut dyn crate::terminal::Session,
73 step: &Value,
74 index: usize,
75 ) -> StepResult;
76
77 fn evaluate_assertion(
85 &self,
86 recipe: &Recipe,
87 assertion: &Value,
88 screen: &str,
89 raw_output: &str,
90 exit_code: Option<i32>,
91 ) -> AssertionResult {
92 crate::assertions::evaluate(recipe, assertion, screen, raw_output, exit_code)
93 }
94
95 fn evaluate_assertion_with_steps(
113 &self,
114 recipe: &Recipe,
115 assertion: &Value,
116 screen: &str,
117 raw_output: &str,
118 exit_code: Option<i32>,
119 steps: Option<&[StepResult]>,
120 ) -> AssertionResult {
121 let _ = steps;
122 self.evaluate_assertion(recipe, assertion, screen, raw_output, exit_code)
123 }
124
125 fn evaluate_assertions(
127 &self,
128 recipe: &Recipe,
129 screen: &str,
130 raw_output: &str,
131 exit_code: Option<i32>,
132 ) -> Vec<AssertionResult> {
133 assertions_with_implicit(recipe)
134 .iter()
135 .map(|a| self.evaluate_assertion(recipe, a, screen, raw_output, exit_code))
136 .collect()
137 }
138
139 fn evaluate_assertions_with_steps(
147 &self,
148 recipe: &Recipe,
149 screen: &str,
150 raw_output: &str,
151 exit_code: Option<i32>,
152 steps: Option<&[StepResult]>,
153 ) -> Vec<AssertionResult> {
154 assertions_with_implicit(recipe)
155 .iter()
156 .map(|a| {
157 self.evaluate_assertion_with_steps(recipe, a, screen, raw_output, exit_code, steps)
158 })
159 .collect()
160 }
161
162 fn recipe_timeout(&self, recipe: &Recipe) -> Duration {
171 crate::steps::duration_from_secs(recipe.timeout_seconds)
172 }
173}
174
175#[allow(clippy::type_complexity)]
180pub trait ExecutionMode: Send + Sync {
181 fn name(&self) -> &str;
183
184 fn execute(
186 &self,
187 ctx: &mut dyn ExecutionContext,
188 recipe: &Recipe,
189 run_dir: &Path,
190 ) -> Result<ExecutionResult, ExecutionError>;
191}
192
193pub struct ScriptedPtyMode;
198
199impl ExecutionMode for ScriptedPtyMode {
200 fn name(&self) -> &str {
201 "scripted_pty"
202 }
203
204 fn execute(
205 &self,
206 ctx: &mut dyn ExecutionContext,
207 recipe: &Recipe,
208 run_dir: &Path,
209 ) -> Result<ExecutionResult, ExecutionError> {
210 let cast_path = run_dir.join("session.cast");
211 let mut session = ctx.create_session(
212 recipe.command.argv.clone(),
213 recipe.command.cwd.clone(),
214 recipe.command.env.clone(),
215 recipe.cols,
216 recipe.rows,
217 cast_path.clone(),
218 )?;
219 let mut steps = Vec::new();
220 for (i, step) in recipe.steps.iter().enumerate() {
221 let result = ctx.run_step(session.as_mut(), step, i + 1);
222 let passed = result.passed;
223 steps.push(result);
224 if !passed {
225 break;
226 }
227 }
228 let timeout = ctx.recipe_timeout(recipe);
229 if recipe.expect_exit_code.is_some() {
230 let _ = session.wait_for_exit(timeout);
231 } else {
232 let _ = session.wait_for_idle(
233 Duration::from_secs_f64(0.5),
234 timeout.min(Duration::from_secs(3)),
235 );
236 }
237 let raw_output = session.raw_output().to_string();
238 let exit_code = session.exit_code();
239 let screen = session.screen().to_string();
240 let assertions = ctx.evaluate_assertions_with_steps(
241 recipe,
242 &screen,
243 &raw_output,
244 exit_code,
245 Some(&steps),
246 );
247 let _ = session.close();
248 Ok((steps, assertions, raw_output, exit_code, screen))
249 }
250}
251
252pub struct ScriptedProcessMode;
254
255impl ExecutionMode for ScriptedProcessMode {
256 fn name(&self) -> &str {
257 "scripted_process"
258 }
259
260 fn execute(
261 &self,
262 ctx: &mut dyn ExecutionContext,
263 recipe: &Recipe,
264 run_dir: &Path,
265 ) -> Result<ExecutionResult, ExecutionError> {
266 let cast_path = run_dir.join("session.cast");
267 let mut session = ctx.create_session(
268 recipe.command.argv.clone(),
269 recipe.command.cwd.clone(),
270 recipe.command.env.clone(),
271 recipe.cols,
272 recipe.rows,
273 cast_path,
274 )?;
275 let mut steps = Vec::new();
276 for (i, step) in recipe.steps.iter().enumerate() {
277 let result = ctx.run_step(session.as_mut(), step, i + 1);
278 let passed = result.passed;
279 steps.push(result);
280 if !passed {
281 break;
282 }
283 }
284 let _ = session.wait_for_exit(ctx.recipe_timeout(recipe));
285 let raw_output = session.raw_output().to_string();
286 let exit_code = session.exit_code();
287 let screen = session.screen().to_string();
288 let assertions = ctx.evaluate_assertions_with_steps(
289 recipe,
290 &screen,
291 &raw_output,
292 exit_code,
293 Some(&steps),
294 );
295 let _ = session.close();
296 Ok((steps, assertions, raw_output, exit_code, screen))
297 }
298}
299
300pub struct AgentDrivenMode;
307
308impl ExecutionMode for AgentDrivenMode {
309 fn name(&self) -> &str {
310 "agent_driven"
311 }
312
313 fn execute(
314 &self,
315 ctx: &mut dyn ExecutionContext,
316 recipe: &Recipe,
317 run_dir: &Path,
318 ) -> Result<ExecutionResult, ExecutionError> {
319 let cast_path = run_dir.join("session.cast");
320 let mut session = ctx.create_session(
321 recipe.command.argv.clone(),
322 recipe.command.cwd.clone(),
323 recipe.command.env.clone(),
324 recipe.cols,
325 recipe.rows,
326 cast_path,
327 )?;
328 let prompt = crate::agent::build_agent_prompt(recipe);
329 let _ = std::fs::write(run_dir.join("agent_prompt.md"), &prompt);
330 let raw_output = format!(
331 "{{\"assertions\":{{}},\"transcript\":\"agent executed for recipe {}\"}}",
332 recipe.name
333 );
334 let parsed = crate::agent::parse_agent_output(&raw_output);
335 let _ = std::fs::write(run_dir.join("agent_transcript.md"), &parsed.transcript);
336 let screen = parsed.transcript.clone();
337 let checks = if recipe.checks.is_empty() {
338 vec!["Codex operator completed the verification".to_string()]
339 } else {
340 recipe.checks.clone()
341 };
342 let assertions = checks
343 .iter()
344 .map(|check| {
345 let passed = parsed.assertions.get(check).copied().unwrap_or(false);
346 AssertionResult {
347 name: check.clone(),
348 passed,
349 detail: if passed {
350 "agent reported pass".to_string()
351 } else {
352 "agent did not report pass".to_string()
353 },
354 }
355 })
356 .collect();
357 let steps = vec![StepResult {
358 name: "codex-operator".to_string(),
359 passed: true,
360 detail: "agent mode via ExecutionContext".to_string(),
361 screen: screen.clone(),
362 }];
363 let _ = session.close();
364 Ok((steps, assertions, raw_output, Some(0), screen))
365 }
366}