|
| 1 | +//! Subprocess isolation keeps the harness environment immutable in parallel tests. |
| 2 | +use super::PREFIX_ENV; |
| 3 | +use crate::{ |
| 4 | + acp::AcpClient, |
| 5 | + config::{CliArgs, Config}, |
| 6 | + usage::StandardAdapterKind, |
| 7 | +}; |
| 8 | +use clap::Parser; |
| 9 | +use serde_json::{json, Value}; |
| 10 | +use std::{fs, os::unix::fs::PermissionsExt, path::PathBuf, process::Command}; |
| 11 | + |
| 12 | +const CHILD: &str = "BUZZ_LAUNCH_TEST_CHILD"; |
| 13 | +const LITERAL: &str = "policy with spaces, ; $(not-a-shell)"; |
| 14 | + |
| 15 | +struct Fixture(PathBuf); |
| 16 | +impl Fixture { |
| 17 | + fn new() -> Self { |
| 18 | + let dir = std::env::temp_dir().join(format!("buzz launch {}", uuid::Uuid::new_v4())); |
| 19 | + fs::create_dir(&dir).unwrap(); |
| 20 | + let wrapper = dir.join("wrapper.py"); |
| 21 | + fs::write( |
| 22 | + &wrapper, |
| 23 | + r#"import json, os, sys |
| 24 | +with open('wrapper.jsonl', 'a') as log: |
| 25 | + log.write(json.dumps(sys.argv[1:]) + '\n') |
| 26 | +os.execv(sys.argv[2], sys.argv[2:]) |
| 27 | +"#, |
| 28 | + ) |
| 29 | + .unwrap(); |
| 30 | + for name in [ |
| 31 | + "goose", |
| 32 | + "codex-acp", |
| 33 | + "claude-agent-acp", |
| 34 | + "hermes-acp", |
| 35 | + "buzz-pi-acp", |
| 36 | + ] { |
| 37 | + let path = dir.join(name); |
| 38 | + fs::write( |
| 39 | + &path, |
| 40 | + r#"#!/usr/bin/env python3 |
| 41 | +import json, os, sys |
| 42 | +with open('worker-started', 'a') as log: |
| 43 | + log.write('started\n') |
| 44 | +for line in sys.stdin: |
| 45 | + request = json.loads(line) |
| 46 | + result = {'protocolVersion': 1, 'argv': sys.argv[1:], |
| 47 | + 'hermes': os.environ.get('HERMES_ACP_SKIP_CONFIGURED_MCP'), |
| 48 | + 'codex': os.environ.get('CODEX_CONFIG'), |
| 49 | + 'prefix': os.environ.get('BUZZ_ACP_LAUNCH_PREFIX')} |
| 50 | + print(json.dumps({'jsonrpc': '2.0', 'id': request['id'], 'result': result}), flush=True) |
| 51 | +"#, |
| 52 | + ) |
| 53 | + .unwrap(); |
| 54 | + fs::set_permissions(path, fs::Permissions::from_mode(0o700)).unwrap(); |
| 55 | + } |
| 56 | + Self(dir) |
| 57 | + } |
| 58 | + fn prefix(&self) -> String { |
| 59 | + json!([ |
| 60 | + "/usr/bin/env", |
| 61 | + "python3", |
| 62 | + self.0.join("wrapper.py"), |
| 63 | + LITERAL |
| 64 | + ]) |
| 65 | + .to_string() |
| 66 | + } |
| 67 | + fn run(&self, mode: &str, prefix: Option<&str>) { |
| 68 | + let mut cmd = Command::new(std::env::current_exe().unwrap()); |
| 69 | + for (name, _) in std::env::vars_os() { |
| 70 | + if name.to_string_lossy().starts_with("BUZZ_") { |
| 71 | + cmd.env_remove(name); |
| 72 | + } |
| 73 | + } |
| 74 | + cmd.args(["--exact", "acp::launch::tests::spawn_child", "--nocapture"]) |
| 75 | + .env(CHILD, mode) |
| 76 | + .env_remove("CODEX_CONFIG") |
| 77 | + .env_remove("HERMES_ACP_SKIP_CONFIGURED_MCP") |
| 78 | + .current_dir(&self.0); |
| 79 | + if let Some(prefix) = prefix { |
| 80 | + cmd.env(PREFIX_ENV, prefix); |
| 81 | + } |
| 82 | + let output = cmd.output().unwrap(); |
| 83 | + assert!( |
| 84 | + output.status.success(), |
| 85 | + "{}\n{}", |
| 86 | + String::from_utf8_lossy(&output.stdout), |
| 87 | + String::from_utf8_lossy(&output.stderr) |
| 88 | + ); |
| 89 | + } |
| 90 | +} |
| 91 | +impl Drop for Fixture { |
| 92 | + fn drop(&mut self) { |
| 93 | + let _ = fs::remove_dir_all(&self.0); |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn wrapper_preserves_worker_identity_and_runs_on_each_spawn() { |
| 99 | + for wrapped in [false, true] { |
| 100 | + let f = Fixture::new(); |
| 101 | + let prefix = f.prefix(); |
| 102 | + f.run("success", wrapped.then_some(prefix.as_str())); |
| 103 | + assert_eq!( |
| 104 | + fs::read_to_string(f.0.join("worker-started")) |
| 105 | + .unwrap() |
| 106 | + .lines() |
| 107 | + .count(), |
| 108 | + 10 |
| 109 | + ); |
| 110 | + if wrapped { |
| 111 | + let calls: Vec<Value> = fs::read_to_string(f.0.join("wrapper.jsonl")) |
| 112 | + .unwrap() |
| 113 | + .lines() |
| 114 | + .map(|line| serde_json::from_str(line).unwrap()) |
| 115 | + .collect(); |
| 116 | + assert_eq!(calls.len(), 10); |
| 117 | + for call in calls { |
| 118 | + assert_eq!(call[0], LITERAL); |
| 119 | + } |
| 120 | + } else { |
| 121 | + assert!(!f.0.join("wrapper.jsonl").exists()); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +#[test] |
| 127 | +fn invalid_or_unavailable_wrapper_never_falls_back_to_worker() { |
| 128 | + let f = Fixture::new(); |
| 129 | + for prefix in [ |
| 130 | + "", |
| 131 | + "[]", |
| 132 | + "null", |
| 133 | + "{}", |
| 134 | + "[1]", |
| 135 | + "[\"\"]", |
| 136 | + "[\"relative\"]", |
| 137 | + "[\"/tmp/\\u0000\"]", |
| 138 | + "[\"/missing-buzz-launcher\"]", |
| 139 | + ] { |
| 140 | + f.run("failure", Some(prefix)); |
| 141 | + assert!(!f.0.join("worker-started").exists()); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +#[tokio::test] |
| 146 | +async fn spawn_child() { |
| 147 | + let Ok(mode) = std::env::var(CHILD) else { |
| 148 | + return; |
| 149 | + }; |
| 150 | + // Bound a broken wrapper/stdio regression; no arbitrary timing gates. |
| 151 | + tokio::time::timeout(std::time::Duration::from_secs(10), async { |
| 152 | + for name in [ |
| 153 | + "goose", |
| 154 | + "codex-acp", |
| 155 | + "claude-agent-acp", |
| 156 | + "hermes-acp", |
| 157 | + "buzz-pi-acp", |
| 158 | + ] { |
| 159 | + let worker = std::env::current_dir().unwrap().join(name); |
| 160 | + let args = CliArgs::try_parse_from([ |
| 161 | + "buzz-acp", |
| 162 | + "--private-key", |
| 163 | + "0000000000000000000000000000000000000000000000000000000000000001", |
| 164 | + "--agent-command", |
| 165 | + worker.to_str().unwrap(), |
| 166 | + "--agent-args", |
| 167 | + "", |
| 168 | + ]) |
| 169 | + .unwrap(); |
| 170 | + let config = Config::from_args(args).unwrap(); |
| 171 | + // A second spawn uses the same host configuration after shutdown, |
| 172 | + // just as a replacement worker does. No prefix is consumed once. |
| 173 | + for _ in 0..2 { |
| 174 | + let spawned = AcpClient::spawn( |
| 175 | + &config.agent_command, |
| 176 | + &config.agent_args, |
| 177 | + &config.persona_env_vars, |
| 178 | + config.has_generated_codex_config, |
| 179 | + ) |
| 180 | + .await; |
| 181 | + if mode == "failure" { |
| 182 | + let error = spawned.err().expect("invalid wrapper launched a worker"); |
| 183 | + if std::env::var(PREFIX_ENV) |
| 184 | + .unwrap() |
| 185 | + .contains("/missing-buzz-launcher") |
| 186 | + { |
| 187 | + assert!(error.to_string().contains("/missing-buzz-launcher")); |
| 188 | + } |
| 189 | + return; |
| 190 | + } |
| 191 | + let mut client = spawned.unwrap(); |
| 192 | + let probe = client.initialize().await.unwrap(); |
| 193 | + let expected = match name { |
| 194 | + "goose" => json!(["acp"]), |
| 195 | + "hermes-acp" => json!([]), |
| 196 | + "buzz-pi-acp" => json!([ |
| 197 | + "--", |
| 198 | + "--skill", |
| 199 | + std::env::current_dir().unwrap().join(".agents/skills") |
| 200 | + ]), |
| 201 | + _ => json!([]), |
| 202 | + }; |
| 203 | + assert_eq!(probe["argv"], expected, "{name}"); |
| 204 | + assert!(probe["prefix"].is_null()); |
| 205 | + if name == "hermes-acp" { |
| 206 | + assert_eq!(probe["hermes"], "1"); |
| 207 | + } |
| 208 | + let adapter = match name { |
| 209 | + "codex-acp" => { |
| 210 | + let config: Value = |
| 211 | + serde_json::from_str(probe["codex"].as_str().unwrap()).unwrap(); |
| 212 | + assert_eq!(config["sandbox_workspace_write"]["network_access"], true); |
| 213 | + Some(StandardAdapterKind::Codex) |
| 214 | + } |
| 215 | + "claude-agent-acp" => Some(StandardAdapterKind::Claude), |
| 216 | + _ => None, |
| 217 | + }; |
| 218 | + assert_eq!(client.standard_adapter, adapter); |
| 219 | + client.shutdown().await; |
| 220 | + } |
| 221 | + } |
| 222 | + }) |
| 223 | + .await |
| 224 | + .expect("wrapped ACP handshake exceeded test deadline"); |
| 225 | +} |
0 commit comments