use std::path::Path;
use std::process::{Command, Output, Stdio};
use std::time::Duration;
use serde_json::Value;
use tam_daemon::daemon::Daemon;
use tam_proto::{Request, Response, ServerMessage};
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::UnixStream;
async fn send(sock: &Path, request: &Request) -> Response {
let mut stream = UnixStream::connect(sock).await.unwrap();
let mut json = serde_json::to_string(request).unwrap();
json.push('\n');
stream.write_all(json.as_bytes()).await.unwrap();
let mut reader = BufReader::new(&mut stream);
let mut line = String::new();
loop {
line.clear();
reader.read_line(&mut line).await.unwrap();
match serde_json::from_str::<ServerMessage>(line.trim()).unwrap() {
ServerMessage::Response(resp) => return resp,
ServerMessage::Event(_) => continue,
}
}
}
async fn start_daemon(sock: &Path, ledger: &Path) {
let daemon = Daemon::new(sock.to_path_buf(), ledger.to_path_buf())
.reconcile_every(Duration::from_millis(200));
tokio::spawn(async move {
let _ = daemon.run().await;
});
for _ in 0..60 {
if UnixStream::connect(sock).await.is_ok() {
return;
}
tokio::time::sleep(Duration::from_millis(50)).await;
}
panic!("daemon did not start");
}
fn tam(root: &Path, dir: &Path, args: &[&str]) -> Command {
let path = format!(
"{}:{}",
root.join("bin").display(),
std::env::var("PATH").unwrap()
);
let mut cmd = Command::new(env!("CARGO_BIN_EXE_tam"));
cmd.args(args)
.current_dir(dir)
.env("XDG_RUNTIME_DIR", root.join("rt"))
.env("XDG_DATA_HOME", root.join("data"))
.env("XDG_CONFIG_HOME", root.join("config"))
.env("HOME", root.join("home"))
.env("PATH", path)
.env_remove("DAEMON_ONLY")
.env("TAM_AGENT_ID", "m-caller")
.env("TAM_SOCKET", root.join("elsewhere.sock"))
.env("CLAUDECODE", "1")
.env("CLAUDE_CODE_SESSION_ID", "caller-session")
.env("CLAUDE_CONFIG_DIR", root.join("claude"))
.env("TMUX", "/tmp/tmux-1000/default,1,0")
.stdin(Stdio::null());
tam_worktree::git::without_local_env(&mut cmd);
cmd
}
async fn output(mut cmd: Command) -> Output {
tokio::task::spawn_blocking(move || cmd.output().unwrap())
.await
.unwrap()
}
fn ok(out: &Output) {
assert!(
out.status.success(),
"{:?}\nstdout: {}\nstderr: {}",
out.status,
String::from_utf8_lossy(&out.stdout),
String::from_utf8_lossy(&out.stderr)
);
}
fn fake_claude(bin: &Path, report: &Path) {
let script = format!(
r#"#!/bin/sh
{{
echo "caller=$CALLER"
echo "daemon_only=$DAEMON_ONLY"
echo "id=$TAM_AGENT_ID"
echo "socket=$TAM_SOCKET"
echo "claude_session=$CLAUDECODE$CLAUDE_CODE_SESSION_ID"
echo "claude_config=$CLAUDE_CONFIG_DIR"
echo "tmux=$TMUX"
echo "pwd=$PWD"
echo "wrapped=$WRAPPED"
}} > '{r}.tmp'
mv '{r}.tmp' '{r}'
exec sleep 3600
"#,
r = report.display()
);
let path = bin.join("claude");
std::fs::write(&path, script).unwrap();
std::fs::set_permissions(&path, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
}
async fn report(file: &Path) -> std::collections::BTreeMap<String, String> {
for _ in 0..200 {
if let Ok(text) = std::fs::read_to_string(file) {
let _ = std::fs::remove_file(file);
return text
.lines()
.filter_map(|l| l.split_once('='))
.map(|(k, v)| (k.to_string(), v.to_string()))
.collect();
}
tokio::time::sleep(Duration::from_millis(10)).await;
}
panic!("the agent did not start");
}
fn write_config(root: &Path, text: &str) {
let macos = root
.join("home")
.join("Library")
.join("Application Support");
for dir in [root.join("config"), macos] {
let dir = dir.join("tam");
std::fs::create_dir_all(&dir).unwrap();
std::fs::write(dir.join("config.toml"), text).unwrap();
}
}
fn git(dir: &Path, args: &[&str]) {
let status = tam_worktree::git::command()
.args(["-c", "user.email=t@t", "-c", "user.name=t"])
.args(args)
.current_dir(dir)
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.unwrap();
assert!(status.success(), "git {args:?}");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn agents_run_in_the_environment_of_the_command_that_starts_them() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path().canonicalize().unwrap();
let root = root.as_path();
std::env::remove_var("TAM_AGENT_ID");
std::env::remove_var("TAM_SOCKET");
std::env::remove_var("CLAUDE_CONFIG_DIR");
std::env::set_var("HOME", root.join("home"));
std::env::set_var("XDG_CONFIG_HOME", root.join("config"));
std::env::set_var("DAEMON_ONLY", "1");
let bin = root.join("bin");
std::fs::create_dir_all(&bin).unwrap();
let report_file = root.join("agent-env");
fake_claude(&bin, &report_file);
let worktrees = root.join("wt");
write_config(
root,
&format!("[worktree]\nroot = \"{}\"\n", worktrees.display()),
);
let repo = root.join("app");
std::fs::create_dir_all(&repo).unwrap();
git(&repo, &["init", "-q"]);
git(&repo, &["commit", "-q", "--allow-empty", "-m", "init"]);
let sock = root.join("rt").join("tam").join("sock");
let ledger = root.join("data").join("tam").join("ledger.jsonl");
start_daemon(&sock, &ledger).await;
let mut new = tam(root, &repo, &["new", "feat", "--detach", "--json"]);
new.env("CALLER", "new");
let out = output(new).await;
ok(&out);
let receipt: Value = serde_json::from_slice(&out.stdout).unwrap();
let member = receipt["members"][0]["id"].as_str().unwrap().to_string();
let dir = receipt["members"][0]["dir"].as_str().unwrap().to_string();
let found = report(&report_file).await;
assert_eq!(found["caller"], "new");
assert_eq!(found["daemon_only"], "");
assert_eq!(found["id"], member);
assert_eq!(found["socket"], sock.display().to_string());
assert_eq!(found["claude_session"], "");
assert_eq!(
found["claude_config"],
root.join("claude").display().to_string()
);
assert_eq!(found["tmux"], "");
assert_eq!(found["pwd"], dir);
ok(&output(tam(root, &repo, &["stop", "feat"])).await);
let mut run = tam(root, &repo, &["run", "feat", "--detach"]);
run.env("CALLER", "run");
ok(&output(run).await);
let found = report(&report_file).await;
assert_eq!(found["caller"], "run");
assert_eq!(found["id"], member);
assert_eq!(found["wrapped"], "");
let wrap = bin.join("wrap");
std::fs::write(
&wrap,
"#!/bin/sh\nexport WRAPPED=\"$CALLER\"\nexec \"$@\"\n",
)
.unwrap();
std::fs::set_permissions(&wrap, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
write_config(
root,
&format!(
"[spawn]\nwrapper = [\"wrap\"]\n\n[worktree]\nroot = \"{}\"\n",
worktrees.display()
),
);
ok(&output(tam(root, &repo, &["stop", "feat"])).await);
let mut run = tam(root, &repo, &["run", "feat", "--detach"]);
run.env("CALLER", "wrapped run");
ok(&output(run).await);
let found = report(&report_file).await;
assert_eq!(found["wrapped"], "wrapped run");
assert_eq!(found["id"], member);
assert_eq!(found["pwd"], dir);
ok(&output(tam(root, &repo, &["stop", "feat"])).await);
assert!(matches!(
send(&sock, &Request::Shutdown).await,
Response::Ok
));
}