File size: 13,229 Bytes
2569bec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | use std::path::Path;
use std::path::PathBuf;
use std::process::Stdio;
use std::sync::Arc;
use std::sync::Mutex;
use anyhow::Context;
use anyhow::Result;
use codex_exec_server::CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN_ENV_VAR;
use codex_exec_server::CODEX_EXEC_SERVER_NOISE_ENVIRONMENT_ID_ENV_VAR;
use codex_exec_server::CODEX_EXEC_SERVER_NOISE_REGISTRY_URL_ENV_VAR;
use codex_exec_server::CODEX_EXEC_SERVER_URL_ENV_VAR;
use codex_exec_server_test_support::relay::TEST_TIMEOUT as VERSION_SKEW_TIMEOUT;
use codex_exec_server_test_support::relay::accept_websocket;
use codex_exec_server_test_support::relay::assert_relay_data_is_encrypted;
use codex_exec_server_test_support::relay::proxy_relay_frames;
use codex_exec_server_test_support::relay::registered_executor_public_key;
use pretty_assertions::assert_eq;
use serde_json::Value;
use serde_json::json;
use tempfile::TempDir;
use tokio::io::AsyncBufReadExt;
use tokio::io::AsyncWriteExt;
use tokio::io::BufReader;
use tokio::io::Lines;
use tokio::net::TcpListener;
use tokio::process::ChildStdin;
use tokio::process::ChildStdout;
use tokio::process::Command;
use tokio::time::timeout;
use wiremock::Mock;
use wiremock::MockServer;
use wiremock::ResponseTemplate;
use wiremock::matchers::header;
use wiremock::matchers::method;
use wiremock::matchers::path;
const ENVIRONMENT_ID: &str = "env-noise-relay-test";
const EXECUTOR_REGISTRATION_ID: &str = "registration-1";
const HARNESS_KEY_AUTHORIZATION: &str = "harness-key-authorization";
const REGISTRY_TOKEN: &str = "registry-token";
const RELEASED_CODEX_ENV_VAR: &str = "CODEX_TEST_RELEASED_CODEX";
const CURRENT_CODEX_ENV_VAR: &str = "CODEX_TEST_CURRENT_CODEX";
const EXECUTOR_MARKER_ENV_VAR: &str = "CODEX_EXECUTOR_VERSION_SKEW_MARKER";
const EXPECTED_OUTPUT: &str = "executor-version-skew-ok";
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn current_app_server_runs_commands_on_released_exec_server_over_noise() -> Result<()> {
let Some((current, released)) = version_skew_binaries()? else {
return Ok(());
};
assert_noise_version_skew(¤t, &released).await
}
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn released_app_server_runs_commands_on_current_exec_server_over_noise() -> Result<()> {
let Some((current, released)) = version_skew_binaries()? else {
return Ok(());
};
assert_noise_version_skew(&released, ¤t).await
}
fn version_skew_binaries() -> Result<Option<(PathBuf, PathBuf)>> {
let Some(released) = std::env::var_os(RELEASED_CODEX_ENV_VAR) else {
return Ok(None);
};
let current = std::env::var_os(CURRENT_CODEX_ENV_VAR)
.with_context(|| format!("{CURRENT_CODEX_ENV_VAR} must name the current Codex binary"))?;
let current = PathBuf::from(current);
let released = PathBuf::from(released);
anyhow::ensure!(
current.is_file(),
"current Codex does not exist: {}",
current.display()
);
anyhow::ensure!(
released.is_file(),
"released Codex does not exist: {}",
released.display()
);
Ok(Some((current, released)))
}
async fn assert_noise_version_skew(app_binary: &Path, executor_binary: &Path) -> Result<()> {
let codex_home = TempDir::new()?;
let model = mock_model(codex_home.path()).await?;
let model_url = model.uri();
std::fs::write(
codex_home.path().join("config.toml"),
format!(
r#"
model = "mock-model"
approval_policy = "never"
sandbox_mode = "danger-full-access"
model_provider = "mock_provider"
[model_providers.mock_provider]
name = "Mock provider"
base_url = "{model_url}/v1"
wire_api = "responses"
request_max_retries = 0
stream_max_retries = 0
"#
),
)?;
let listener = TcpListener::bind("127.0.0.1:0").await?;
let rendezvous_url = format!("ws://{}", listener.local_addr()?);
let registry = MockServer::start().await;
let registry_url = registry.uri();
Mock::given(method("POST"))
.and(path(format!(
"/cloud/environment/{ENVIRONMENT_ID}/register"
)))
.and(header("authorization", format!("Bearer {REGISTRY_TOKEN}")))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"environment_id": ENVIRONMENT_ID,
"url": format!("{rendezvous_url}/relay?role=environment"),
"security_profile": "noise_hybrid_ik_v1",
"executor_registration_id": EXECUTOR_REGISTRATION_ID,
})))
.expect(1)
.mount(®istry)
.await;
Mock::given(method("POST"))
.and(path(format!(
"/cloud/environment/{ENVIRONMENT_ID}/validate"
)))
.and(header("authorization", format!("Bearer {REGISTRY_TOKEN}")))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({"valid": true})))
.expect(1)
.mount(®istry)
.await;
let mut executor = Command::new(executor_binary)
.args([
"exec-server",
"--remote",
registry_url.as_str(),
"--environment-id",
ENVIRONMENT_ID,
])
.current_dir(codex_home.path())
.env("CODEX_HOME", codex_home.path())
.env("CODEX_API_KEY", REGISTRY_TOKEN)
.env(EXECUTOR_MARKER_ENV_VAR, EXPECTED_OUTPUT)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::inherit())
.kill_on_drop(true)
.spawn()
.with_context(|| format!("start remote executor from {}", executor_binary.display()))?;
let environment_websocket = accept_websocket(&listener, "environment").await?;
let executor_public_key = registered_executor_public_key(®istry).await?;
Mock::given(method("POST"))
.and(path(format!("/cloud/environment/{ENVIRONMENT_ID}/connect")))
.and(header("authorization", format!("Bearer {REGISTRY_TOKEN}")))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"environment_id": ENVIRONMENT_ID,
"url": format!("{rendezvous_url}/relay?role=harness"),
"security_profile": "noise_hybrid_ik_v1",
"executor_registration_id": EXECUTOR_REGISTRATION_ID,
"executor_public_key": executor_public_key,
"harness_key_authorization": HARNESS_KEY_AUTHORIZATION,
})))
.expect(1)
.mount(®istry)
.await;
let captured_frames = Arc::new(Mutex::new(Vec::new()));
let captured_relay_frames = Arc::clone(&captured_frames);
let relay = tokio::spawn(async move {
let harness_websocket = accept_websocket(&listener, "harness").await?;
proxy_relay_frames(
environment_websocket,
harness_websocket,
captured_relay_frames,
)
.await
});
let mut app_server = Command::new(app_binary)
.arg("app-server")
.current_dir(codex_home.path())
.env("CODEX_HOME", codex_home.path())
.env("CODEX_API_KEY", REGISTRY_TOKEN)
.env(
"CODEX_APP_SERVER_MANAGED_CONFIG_PATH",
codex_home.path().join("managed_config.toml"),
)
.env(CODEX_EXEC_SERVER_NOISE_REGISTRY_URL_ENV_VAR, ®istry_url)
.env(
CODEX_EXEC_SERVER_NOISE_ENVIRONMENT_ID_ENV_VAR,
ENVIRONMENT_ID,
)
.env(CODEX_EXEC_SERVER_NOISE_AUTH_TOKEN_ENV_VAR, REGISTRY_TOKEN)
.env_remove(CODEX_EXEC_SERVER_URL_ENV_VAR)
.env_remove(EXECUTOR_MARKER_ENV_VAR)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::inherit())
.kill_on_drop(true)
.spawn()
.with_context(|| format!("start app-server from {}", app_binary.display()))?;
let mut stdin = app_server.stdin.take().context("app-server stdin")?;
let stdout = app_server.stdout.take().context("app-server stdout")?;
let mut stdout = BufReader::new(stdout).lines();
let mut notifications = Vec::new();
app_server_request(
&mut stdin,
&mut stdout,
&mut notifications,
/*id*/ 1,
"initialize",
json!({
"clientInfo": {"name": "noise-version-skew", "version": "0.1.0"},
"capabilities": {"experimentalApi": true},
}),
)
.await?;
stdin.write_all(b"{\"method\":\"initialized\"}\n").await?;
let thread = app_server_request(
&mut stdin,
&mut stdout,
&mut notifications,
/*id*/ 2,
"thread/start",
json!({"cwd": codex_home.path()}),
)
.await?;
let thread_id = thread["thread"]["id"]
.as_str()
.context("thread/start should return a thread id")?;
app_server_request(
&mut stdin,
&mut stdout,
&mut notifications,
/*id*/ 3,
"turn/start",
json!({
"threadId": thread_id,
"input": [{
"type": "text",
"text": "run the Noise compatibility command",
"textElements": [],
}],
}),
)
.await?;
while !notifications
.iter()
.any(|notification: &Value| notification["method"] == "turn/completed")
{
let line = timeout(VERSION_SKEW_TIMEOUT, stdout.next_line())
.await
.context("waiting for turn/completed")??
.context("app-server exited before turn/completed")?;
notifications.push(serde_json::from_str(&line)?);
}
assert_eq!(
std::fs::read_to_string(codex_home.path().join("version-skew-output.txt"))?,
EXPECTED_OUTPUT
);
assert_relay_data_is_encrypted(&captured_frames)?;
registry.verify().await;
model.verify().await;
let _ = app_server.start_kill();
let _ = executor.start_kill();
relay.abort();
Ok(())
}
async fn app_server_request(
stdin: &mut ChildStdin,
stdout: &mut Lines<BufReader<ChildStdout>>,
notifications: &mut Vec<Value>,
id: u64,
method: &str,
params: Value,
) -> Result<Value> {
let request = json!({"id": id, "method": method, "params": params});
stdin.write_all(request.to_string().as_bytes()).await?;
stdin.write_all(b"\n").await?;
loop {
let line = timeout(VERSION_SKEW_TIMEOUT, stdout.next_line())
.await
.with_context(|| format!("waiting for {method} response"))??
.with_context(|| format!("app-server exited before {method} response"))?;
let response: Value = serde_json::from_str(&line)?;
if response["id"] == id {
anyhow::ensure!(
response.get("error").is_none(),
"{method} failed: {}",
response["error"]
);
return response.get("result").cloned().context("missing result");
}
notifications.push(response);
}
}
async fn mock_model(codex_home: &Path) -> Result<MockServer> {
let server = MockServer::start().await;
let arguments = serde_json::to_string(&json!({
"cmd": format!("printf '%s' \"${EXECUTOR_MARKER_ENV_VAR}\" > version-skew-output.txt"),
"workdir": codex_home,
"yield_time_ms": 5_000,
}))?;
let completed = |id| {
json!({
"type": "response.completed",
"response": {
"id": id,
"usage": {
"input_tokens": 0,
"input_tokens_details": null,
"output_tokens": 0,
"output_tokens_details": null,
"total_tokens": 0,
},
},
})
};
let responses = vec![
event_stream(vec![
json!({"type": "response.created", "response": {"id": "response-1"}}),
json!({
"type": "response.output_item.done",
"item": {
"type": "function_call",
"call_id": "noise-version-skew-command",
"name": "exec_command",
"arguments": arguments,
},
}),
completed("response-1"),
])?,
event_stream(vec![
json!({"type": "response.created", "response": {"id": "response-2"}}),
json!({
"type": "response.output_item.done",
"item": {
"type": "message",
"role": "assistant",
"id": "message-1",
"content": [{"type": "output_text", "text": "done"}],
},
}),
completed("response-2"),
])?,
];
for response in responses {
Mock::given(method("POST"))
.and(path("/v1/responses"))
.respond_with(ResponseTemplate::new(200).set_body_raw(response, "text/event-stream"))
.up_to_n_times(1)
.expect(1)
.mount(&server)
.await;
}
Ok(server)
}
fn event_stream(events: Vec<Value>) -> Result<String> {
events
.into_iter()
.map(|event| {
let event_type = event["type"].as_str().context("SSE event type")?;
Ok(format!("event: {event_type}\ndata: {event}\n\n"))
})
.collect()
}
|