use std::{ffi::OsString, path::Path, time::Duration};
use anyhow::{Context, Result, bail, ensure};
use tokio::process::{Child, ChildStdin, ChildStdout, Command};
const DOCKER_TIMEOUT: Duration = Duration::from_secs(30);
const AVAILABILITY_TIMEOUT: Duration = Duration::from_secs(10);
const IDENTIFY_TIMEOUT: Duration = Duration::from_secs(15);
const IDENTIFY_INTERVAL: Duration = Duration::from_millis(200);
const CONTAINER_NAME_PREFIX: &str = "vtcode-cua-";
const CONTAINER_ID_LEN: usize = 64;
const DIAGNOSTIC_MAX_BYTES: usize = 512;
pub struct ContainerSpec {
image: String,
build_dir: String,
}
impl ContainerSpec {
pub async fn approved(image: &str, build_dir: &Path) -> Result<Self> {
ensure!(!image.is_empty() && image.len() <= 512, "invalid browser container image");
ensure!(image.chars().all(|c| !c.is_control() && !c.is_whitespace()), "invalid browser container image");
ensure!(has_image_pin(image) && !image.ends_with(":latest"), "browser container image must be pinned");
let canonical = vtcode_commons::fs::canonicalize_with_context_async(build_dir, "browser build").await?;
ensure!(canonical.is_dir(), "browser build is not a directory");
let Some(dir) = canonical.to_str() else {
bail!("browser build path is not representable");
};
ensure!(mount_spec_safe(dir), "browser build path cannot appear in a mount spec");
Ok(Self { image: image.to_owned(), build_dir: dir.to_owned() })
}
}
pub struct ContainerHandle {
name: String,
id: String,
#[allow(dead_code, reason = "Owned for its kill-on-drop backstop; never read directly.")]
child: Child,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TerminateOutcome {
Removed,
AlreadyGone,
}
impl ContainerHandle {
pub async fn launch(spec: &ContainerSpec) -> Result<(Self, ChildStdout, ChildStdin)> {
let name = format!("{CONTAINER_NAME_PREFIX}{}", uuid::Uuid::new_v4().as_simple());
let mut child = Command::new("docker")
.args(docker_run_argv(spec, &name))
.stdin(std::process::Stdio::piped())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.kill_on_drop(true)
.spawn()
.context("could not start the approved browser container")?;
let replies = child.stdout.take().context("browser container did not pipe its replies")?;
let commands = child.stdin.take().context("browser container did not pipe its commands")?;
let id = inspect_container_id(&name, &mut child).await?;
Ok((Self { name, id, child }, replies, commands))
}
#[must_use]
pub fn name(&self) -> &str {
&self.name
}
#[must_use]
pub fn id(&self) -> &str {
&self.id
}
pub async fn terminate(&self) -> Result<TerminateOutcome> {
if matches!(self.present().await, Ok(Presence::Missing)) {
return Ok(TerminateOutcome::AlreadyGone);
}
let output = run_docker(&[
OsString::from("rm"),
OsString::from("--force"),
OsString::from(&self.id),
])
.await?;
if !output.status.success() {
let diagnostic = diagnostic_text(&output.stderr);
if is_missing_message(&diagnostic) {
return Ok(TerminateOutcome::AlreadyGone);
}
bail!("could not remove browser container {}: {diagnostic}", self.id);
}
for _ in 0..3 {
match self.present().await? {
Presence::Missing => return Ok(TerminateOutcome::Removed),
Presence::Exists => {
bail!("browser container {} is still present", self.id);
}
Presence::Unknown => tokio::time::sleep(IDENTIFY_INTERVAL).await,
}
}
bail!("could not verify removal of browser container {}", self.id);
}
async fn present(&self) -> Result<Presence> {
let output = run_docker(&[OsString::from("inspect"), OsString::from(&self.id)]).await?;
if output.status.success() {
return Ok(Presence::Exists);
}
if is_missing_message(&diagnostic_text(&output.stderr)) {
return Ok(Presence::Missing);
}
Ok(Presence::Unknown)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Presence {
Exists,
Missing,
Unknown,
}
fn has_image_pin(image: &str) -> bool {
if image.contains('@') {
return true;
}
match image.rsplit_once('/') {
Some((_, last)) => last.contains(':'),
None => image.contains(':'),
}
}
fn is_missing_message(diagnostic: &str) -> bool {
let lower = diagnostic.to_ascii_lowercase();
lower.contains("no such container") || lower.contains("no such object")
}
pub async fn docker_available() -> bool {
let Ok(mut child) = Command::new("docker")
.arg("info")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.kill_on_drop(true)
.spawn()
else {
return false;
};
tokio::time::timeout(AVAILABILITY_TIMEOUT, child.wait())
.await
.is_ok_and(|status| status.is_ok_and(|status| status.success()))
}
fn mount_spec_safe(dir: &str) -> bool {
!dir.is_empty() && !dir.chars().any(|c| c.is_control() || matches!(c, ',' | '"' | '\\'))
}
fn docker_run_argv(spec: &ContainerSpec, name: &str) -> Vec<OsString> {
[
"run",
"--rm",
"--interactive",
"--init",
"--pull=never",
"--network=none",
"--read-only",
"--cap-drop=ALL",
"--user=65534",
"--security-opt=no-new-privileges",
"--pids-limit=256",
"--memory=1g",
"--tmpfs=/tmp:rw,nosuid,nodev,noexec,size=512m",
"--env=HOME=/tmp",
]
.into_iter()
.map(OsString::from)
.chain([
OsString::from(format!("--name={name}")),
OsString::from(format!("--mount=type=bind,src={},dst=/build,readonly", spec.build_dir)),
OsString::from(&spec.image),
])
.collect()
}
async fn inspect_container_id(name: &str, child: &mut Child) -> Result<String> {
let start = tokio::time::Instant::now();
loop {
if let Some(status) = child.try_wait().context("could not poll the browser container")? {
bail!(
"browser container exited before its identity could be established: {status}: {}",
drain_stderr(child).await
);
}
let output = run_docker(&[
OsString::from("inspect"),
OsString::from("--format={{.Id}}"),
OsString::from(name),
])
.await?;
if output.status.success() {
if let Ok(id) = parse_container_id(&String::from_utf8_lossy(&output.stdout)) {
return Ok(id);
}
}
ensure!(start.elapsed() < IDENTIFY_TIMEOUT, "could not identify the browser container");
tokio::time::sleep(IDENTIFY_INTERVAL).await;
}
}
async fn drain_stderr(child: &mut Child) -> String {
use tokio::io::AsyncReadExt;
let mut stderr = String::new();
if let Some(mut pipe) = child.stderr.take() {
let _drained = tokio::time::timeout(Duration::from_secs(2), pipe.read_to_string(&mut stderr)).await;
}
diagnostic_text(stderr.as_bytes())
}
fn parse_container_id(raw: &str) -> Result<String> {
let id = raw.trim().to_owned();
ensure!(
id.len() == CONTAINER_ID_LEN && id.chars().all(|c| c.is_ascii_hexdigit()),
"could not identify the browser container"
);
Ok(id)
}
async fn run_docker(args: &[OsString]) -> Result<std::process::Output> {
let child = Command::new("docker")
.args(args)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::piped())
.stderr(std::process::Stdio::piped())
.kill_on_drop(true)
.spawn()
.context("could not run docker")?;
match tokio::time::timeout(DOCKER_TIMEOUT, child.wait_with_output()).await {
Ok(output) => output.context("could not read docker output"),
Err(_) => bail!("docker command timed out"),
}
}
fn diagnostic_text(output: &[u8]) -> String {
let text = String::from_utf8_lossy(output);
let end = text.floor_char_boundary(DIAGNOSTIC_MAX_BYTES.min(text.len()));
text.get(..end).unwrap_or_default().to_owned()
}
#[cfg(test)]
mod tests;