#![cfg(unix)]
use std::{
path::{Path, PathBuf},
process::{Command, Output, Stdio},
time::{Duration, Instant},
};
use pretty_assertions::assert_eq;
use serde_json::{json, Value};
use tempfile::TempDir;
const MODE_ENV: &str = "RHO_AUTOMATION_TEST_MODE";
const COMMAND_ENV: &str = "RHO_AUTOMATION_TEST_COMMAND";
const CHECKPOINT_DEADLINE: Duration = Duration::from_secs(25);
const CONFIG: &str = r#"provider = "xai"
model = "grok-fixture"
auth = "xai-oauth"
[web_search]
hosted = false
provider = "disabled"
"#;
const WORKFLOW: &str = r#"def build(inputs):
return workflow(name = "crash-recovery", nodes = [
agent(
name = "inspect",
agent = "fixture",
prompt = "inspect the workspace",
access = "mutating",
timeout_seconds = 600,
max_output_bytes = 100000,
),
])
WORKFLOW = define(inputs = {}, build = build)
"#;
const AGENT: &str = "---
description: Fixture agent for crash recovery
tools: [process]
---
Do the task.
";
#[test]
fn killed_agent_node_continues_from_its_checkpoint() {
let root = TempDir::new().unwrap();
std::fs::create_dir_all(root.path().join("wf/agents")).unwrap();
std::fs::write(root.path().join("config.toml"), CONFIG).unwrap();
std::fs::write(root.path().join("wf/main.star"), WORKFLOW).unwrap();
std::fs::write(root.path().join("wf/agents/fixture.md"), AGENT).unwrap();
let plan = rho(
&root,
"inspect",
&["workflow", "plan", "wf/main.star", "--output", "json"],
);
let plan: Value = serde_json::from_slice(&success(&plan).stdout).unwrap();
let plan_id = plan["manifest"]["plan_id"].as_str().unwrap();
let mut first = command(&root, "process-then-delay");
first
.env(COMMAND_ENV, "true")
.args(["workflow", "run", plan_id, "--yes", "--output", "jsonl"]);
let mut first = first.spawn().unwrap();
let (run_id, checkpoint) = wait_for_tool_checkpoint(&root);
first.kill().unwrap();
first.wait().unwrap();
let saved: Value = serde_json::from_slice(&std::fs::read(&checkpoint).unwrap()).unwrap();
let preview = rho(
&root,
"inspect",
&[
"workflow",
"resume",
&run_id,
"--dry-run",
"--output",
"jsonl",
],
);
let preview: Value = serde_json::from_slice(&success(&preview).stdout).unwrap();
assert_eq!(
preview,
json!({
"run_id": run_id,
"needs_confirmation": true,
"attempts": [{"node": "inspect", "attempt": 1, "action": "continue"}],
})
);
let resumed = rho(
&root,
"inspect",
&[
"workflow",
"resume",
&run_id,
"--recover-uncertain",
"--yes",
"--output",
"jsonl",
],
);
let node_events = String::from_utf8(success(&resumed).stdout.clone())
.unwrap()
.lines()
.map(|line| serde_json::from_str::<Value>(line).unwrap())
.filter(|event| {
matches!(
event["type"].as_str(),
Some("node_started" | "node_finished")
)
})
.map(|event| json!([event["type"], event["attempt"], event["outcome"]]))
.collect::<Vec<_>>();
assert_eq!(
node_events,
[
json!(["node_started", 1, null]),
json!(["node_finished", null, "success"]),
]
);
let answer = checkpoint.with_file_name("answer.txt");
let request: Value = serde_json::from_slice(&std::fs::read(answer).unwrap()).unwrap();
assert_eq!(request["messages"], saved["history"]);
assert!(
!checkpoint.exists(),
"finished attempts drop their checkpoint"
);
}
fn wait_for_tool_checkpoint(root: &TempDir) -> (String, PathBuf) {
let runs = root.path().join(".rho/workflows/runs");
let started = Instant::now();
loop {
if let Some(found) = tool_checkpoint(&runs) {
return found;
}
assert!(
started.elapsed() < CHECKPOINT_DEADLINE,
"no tool checkpoint within {CHECKPOINT_DEADLINE:?}"
);
std::thread::sleep(Duration::from_millis(20));
}
}
fn tool_checkpoint(runs: &Path) -> Option<(String, PathBuf)> {
let run = std::fs::read_dir(runs).ok()?.next()?.ok()?;
let checkpoint = run
.path()
.join("nodes/inspect/attempts/1/agent/session.json");
let snapshot: Value = serde_json::from_slice(&std::fs::read(&checkpoint).ok()?).ok()?;
let has_tool_result = snapshot["history"]
.as_array()?
.iter()
.any(|message| message.get("ToolResult").is_some());
has_tool_result.then(|| (run.file_name().into_string().unwrap(), checkpoint))
}
fn rho(root: &TempDir, mode: &str, args: &[&str]) -> Output {
command(root, mode).args(args).output().unwrap()
}
fn command(root: &TempDir, mode: &str) -> Command {
let mut command = Command::new(env!("CARGO_BIN_EXE_rho"));
command
.current_dir(root.path())
.env("HOME", root.path())
.env("RHO_HOME", root.path().join(".rho"))
.env("XDG_RUNTIME_DIR", root.path())
.env_remove("DBUS_SESSION_BUS_ADDRESS")
.env(MODE_ENV, mode)
.env_remove(COMMAND_ENV)
.env_remove("HERDR_ENV")
.env_remove("HERDR_SOCKET_PATH")
.env_remove("HERDR_PANE_ID")
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.arg("--config")
.arg(root.path().join("config.toml"));
command
}
fn success(output: &Output) -> &Output {
assert!(
output.status.success(),
"rho failed: {}\n{}",
output.status,
String::from_utf8_lossy(&output.stderr)
);
output
}