use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::time::Duration;
use serde_json::Value;
use crate::models::{AssertionResult, Recipe, StepResult};
pub type ExecutionResult = (
Vec<StepResult>,
Vec<AssertionResult>,
String,
Option<i32>,
String,
);
#[derive(Debug, thiserror::Error)]
pub enum ExecutionError {
#[error("step error: {0}")]
Step(String),
#[error("session error: {0}")]
Session(String),
#[error("config error: {0}")]
Config(String),
#[error("timeout: {0}")]
Timeout(String),
}
fn assertions_with_implicit(recipe: &Recipe) -> Vec<Value> {
crate::assertions::evaluated_list(recipe)
}
pub trait ExecutionContext: Send {
fn create_session(
&mut self,
argv: Vec<String>,
cwd: Option<String>,
env: HashMap<String, String>,
cols: u16,
rows: u16,
cast_path: PathBuf,
) -> Result<Box<dyn crate::terminal::Session>, ExecutionError>;
fn run_step(
&mut self,
session: &mut dyn crate::terminal::Session,
step: &Value,
index: usize,
) -> StepResult;
fn evaluate_assertion(
&self,
recipe: &Recipe,
assertion: &Value,
screen: &str,
raw_output: &str,
exit_code: Option<i32>,
) -> AssertionResult {
crate::assertions::evaluate(recipe, assertion, screen, raw_output, exit_code)
}
fn evaluate_assertion_with_steps(
&self,
recipe: &Recipe,
assertion: &Value,
screen: &str,
raw_output: &str,
exit_code: Option<i32>,
steps: Option<&[StepResult]>,
) -> AssertionResult {
let _ = steps;
self.evaluate_assertion(recipe, assertion, screen, raw_output, exit_code)
}
fn evaluate_assertions(
&self,
recipe: &Recipe,
screen: &str,
raw_output: &str,
exit_code: Option<i32>,
) -> Vec<AssertionResult> {
assertions_with_implicit(recipe)
.iter()
.map(|a| self.evaluate_assertion(recipe, a, screen, raw_output, exit_code))
.collect()
}
fn evaluate_assertions_with_steps(
&self,
recipe: &Recipe,
screen: &str,
raw_output: &str,
exit_code: Option<i32>,
steps: Option<&[StepResult]>,
) -> Vec<AssertionResult> {
assertions_with_implicit(recipe)
.iter()
.map(|a| {
self.evaluate_assertion_with_steps(recipe, a, screen, raw_output, exit_code, steps)
})
.collect()
}
fn recipe_timeout(&self, recipe: &Recipe) -> Duration {
crate::steps::duration_from_secs(recipe.timeout_seconds)
}
}
#[allow(clippy::type_complexity)]
pub trait ExecutionMode: Send + Sync {
fn name(&self) -> &str;
fn execute(
&self,
ctx: &mut dyn ExecutionContext,
recipe: &Recipe,
run_dir: &Path,
) -> Result<ExecutionResult, ExecutionError>;
}
pub struct ScriptedPtyMode;
impl ExecutionMode for ScriptedPtyMode {
fn name(&self) -> &str {
"scripted_pty"
}
fn execute(
&self,
ctx: &mut dyn ExecutionContext,
recipe: &Recipe,
run_dir: &Path,
) -> Result<ExecutionResult, ExecutionError> {
let cast_path = run_dir.join("session.cast");
let mut session = ctx.create_session(
recipe.command.argv.clone(),
recipe.command.cwd.clone(),
recipe.command.env.clone(),
recipe.cols,
recipe.rows,
cast_path.clone(),
)?;
let mut steps = Vec::new();
for (i, step) in recipe.steps.iter().enumerate() {
let result = ctx.run_step(session.as_mut(), step, i + 1);
let passed = result.passed;
steps.push(result);
if !passed {
break;
}
}
let timeout = ctx.recipe_timeout(recipe);
if recipe.expect_exit_code.is_some() {
let _ = session.wait_for_exit(timeout);
} else {
let _ = session.wait_for_idle(
Duration::from_secs_f64(0.5),
timeout.min(Duration::from_secs(3)),
);
}
let raw_output = session.raw_output().to_string();
let exit_code = session.exit_code();
let screen = session.screen().to_string();
let assertions = ctx.evaluate_assertions_with_steps(
recipe,
&screen,
&raw_output,
exit_code,
Some(&steps),
);
let _ = session.close();
Ok((steps, assertions, raw_output, exit_code, screen))
}
}
pub struct ScriptedProcessMode;
impl ExecutionMode for ScriptedProcessMode {
fn name(&self) -> &str {
"scripted_process"
}
fn execute(
&self,
ctx: &mut dyn ExecutionContext,
recipe: &Recipe,
run_dir: &Path,
) -> Result<ExecutionResult, ExecutionError> {
let cast_path = run_dir.join("session.cast");
let mut session = ctx.create_session(
recipe.command.argv.clone(),
recipe.command.cwd.clone(),
recipe.command.env.clone(),
recipe.cols,
recipe.rows,
cast_path,
)?;
let mut steps = Vec::new();
for (i, step) in recipe.steps.iter().enumerate() {
let result = ctx.run_step(session.as_mut(), step, i + 1);
let passed = result.passed;
steps.push(result);
if !passed {
break;
}
}
let _ = session.wait_for_exit(ctx.recipe_timeout(recipe));
let raw_output = session.raw_output().to_string();
let exit_code = session.exit_code();
let screen = session.screen().to_string();
let assertions = ctx.evaluate_assertions_with_steps(
recipe,
&screen,
&raw_output,
exit_code,
Some(&steps),
);
let _ = session.close();
Ok((steps, assertions, raw_output, exit_code, screen))
}
}
pub struct AgentDrivenMode;
impl ExecutionMode for AgentDrivenMode {
fn name(&self) -> &str {
"agent_driven"
}
fn execute(
&self,
ctx: &mut dyn ExecutionContext,
recipe: &Recipe,
run_dir: &Path,
) -> Result<ExecutionResult, ExecutionError> {
let cast_path = run_dir.join("session.cast");
let mut session = ctx.create_session(
recipe.command.argv.clone(),
recipe.command.cwd.clone(),
recipe.command.env.clone(),
recipe.cols,
recipe.rows,
cast_path,
)?;
let prompt = crate::agent::build_agent_prompt(recipe);
let _ = std::fs::write(run_dir.join("agent_prompt.md"), &prompt);
let raw_output = format!(
"{{\"assertions\":{{}},\"transcript\":\"agent executed for recipe {}\"}}",
recipe.name
);
let parsed = crate::agent::parse_agent_output(&raw_output);
let _ = std::fs::write(run_dir.join("agent_transcript.md"), &parsed.transcript);
let screen = parsed.transcript.clone();
let checks = if recipe.checks.is_empty() {
vec!["Codex operator completed the verification".to_string()]
} else {
recipe.checks.clone()
};
let assertions = checks
.iter()
.map(|check| {
let passed = parsed.assertions.get(check).copied().unwrap_or(false);
AssertionResult {
name: check.clone(),
passed,
detail: if passed {
"agent reported pass".to_string()
} else {
"agent did not report pass".to_string()
},
}
})
.collect();
let steps = vec![StepResult {
name: "codex-operator".to_string(),
passed: true,
detail: "agent mode via ExecutionContext".to_string(),
screen: screen.clone(),
}];
let _ = session.close();
Ok((steps, assertions, raw_output, Some(0), screen))
}
}