use crate::{
build_docker_stop_command, build_normalized_result_metadata, build_screen_stop_command,
execute_detached_with_env,
run_deadline::{IsolationTarget, MAX_TIMER_MS},
start_command_with_env, Agent, AgentResult, AgentStartOptions, BuildMetadataOptions,
};
use std::time::Duration;
pub fn validate_run_timeout(
timeout: Option<Duration>,
close_timeout: Option<Duration>,
detached: bool,
) -> Result<(), String> {
let max = Duration::from_millis(MAX_TIMER_MS);
if timeout.is_some_and(|value| value < Duration::from_millis(1) || value > max) {
return Err(format!(
"timeout (--timeout-ms) must be from 1 to {MAX_TIMER_MS} milliseconds"
));
}
if close_timeout.is_some_and(|value| value > max) {
return Err(format!(
"close timeout (--close-timeout-ms) must be from 0 to {MAX_TIMER_MS} milliseconds"
));
}
if timeout.is_some() && detached {
return Err("timeout (--timeout-ms) requires an attached run".into());
}
Ok(())
}
impl Agent {
pub async fn isolation_target(
&mut self,
start: &AgentStartOptions,
) -> Result<Option<IsolationTarget>, String> {
if start.dry_run || start.detached {
return Ok(None);
}
Ok(match self.options.isolation.as_str() {
"docker" => {
let file = self.ensure_prompt_temp_dir().await?.join("container.cid");
Some(IsolationTarget::Docker(file.to_string_lossy().into_owned()))
}
"screen" => self
.options
.screen_name
.clone()
.map(IsolationTarget::Screen),
_ => None,
})
}
pub async fn start_detached(
&self,
command: &str,
child_env: &[(String, String)],
) -> Result<(), String> {
if self.options.isolation == "docker" {
let mut handle = start_command_with_env(command, false, false, child_env)
.await
.map_err(|error| error.to_string())?;
if handle
.wait_for_exit()
.await
.map_err(|error| error.to_string())?
!= 0
{
let (_, stderr, _) = handle.get_output();
return Err(stderr.trim().to_owned());
}
} else {
execute_detached_with_env(command, Some(child_env))
.await
.map_err(|error| error.to_string())?;
}
println!("Agent started in detached mode");
match self.options.isolation.as_str() {
"screen" => println!(
"Screen session: {}",
self.options.screen_name.as_deref().unwrap_or_default()
),
"docker" => println!(
"Container: {}",
self.options.container_name.as_deref().unwrap_or_default()
),
_ => {}
}
Ok(())
}
pub async fn stop_isolated(&mut self, dry_run: bool) -> Result<AgentResult, String> {
let command = if self.options.isolation == "screen" {
let name = self
.options
.screen_name
.as_deref()
.ok_or("screen_name is required to stop screen session")?;
build_screen_stop_command(name)
} else {
let name = self
.options
.container_name
.as_deref()
.ok_or("container_name is required to stop docker container")?;
build_docker_stop_command(name)
};
let collected = async {
if dry_run {
println!("Dry run - command that would be executed:\n{command}");
return Ok((0, String::new(), None));
}
let env = self.child_environment()?;
let mut handle = start_command_with_env(&command, true, false, &env)
.await
.map_err(|error| error.to_string())?;
let exit = handle
.wait_for_exit()
.await
.map_err(|error| error.to_string())?;
Ok::<_, String>((
exit,
handle.get_output().0.to_owned(),
handle.signal().map(str::to_owned),
))
}
.await;
if !dry_run {
self.cleanup_prompt_temp_dir().await;
}
let (exit_code, plain_output, signal) = collected?;
let metadata = build_normalized_result_metadata(BuildMetadataOptions {
tool: &self.options.tool,
exit_code,
plain_output: &plain_output,
signal,
..Default::default()
});
Ok(AgentResult {
exit_code: Some(exit_code),
error_type: metadata.error_type.clone(),
plain_output,
metadata,
..Default::default()
})
}
}