mirror of
https://github.com/openai/codex.git
synced 2026-11-18 22:20:48 +00:00
Move the app-server socket relay into the Codex CLI (#62163)
## What changed Move the stdio-to-Unix-socket relay used by `codex app-server proxy` into the CLI crate. Remove the standalone `codex-stdio-to-uds` crate and binary and the hidden `codex stdio-to-uds` subcommand. ## Testing Replace the standalone binary test with a CLI integration test that runs `codex app-server proxy --sock` and checks that the socket receives the bytes from stdin and the reply appears on stdout. GitOrigin-RevId: e630f8f70e93ce90b0005e4992a3d0b257b208db
This commit is contained in:
Generated
-13
@@ -2621,7 +2621,6 @@ dependencies = [
|
||||
"codex-sandboxing",
|
||||
"codex-skills-extension",
|
||||
"codex-state",
|
||||
"codex-stdio-to-uds",
|
||||
"codex-tcp-tunnel",
|
||||
"codex-terminal-detection",
|
||||
"codex-thread-store",
|
||||
@@ -5126,18 +5125,6 @@ dependencies = [
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "codex-stdio-to-uds"
|
||||
version = "0.0.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"codex-uds",
|
||||
"codex-utils-cargo-bin",
|
||||
"pretty_assertions",
|
||||
"tempfile",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "codex-tcp-tunnel"
|
||||
version = "0.0.0"
|
||||
|
||||
@@ -113,7 +113,6 @@ members = [
|
||||
"runtime-agent-instance",
|
||||
"response-debug-context",
|
||||
"sandboxing",
|
||||
"stdio-to-uds",
|
||||
"otel",
|
||||
"otel-trace-websocket",
|
||||
"tcp-tunnel",
|
||||
@@ -302,7 +301,6 @@ codex-shell-command = { path = "shell-command" }
|
||||
codex-skills-extension = { path = "ext/skills" }
|
||||
codex-skills = { path = "skills" }
|
||||
codex-state = { path = "state" }
|
||||
codex-stdio-to-uds = { path = "stdio-to-uds" }
|
||||
codex-terminal-detection = { path = "terminal-detection" }
|
||||
codex-test-binary-support = { path = "test-binary-support" }
|
||||
codex-thread-store = { path = "thread-store" }
|
||||
|
||||
@@ -74,10 +74,10 @@ codex-rollout-trace = { workspace = true }
|
||||
codex-sandboxing = { workspace = true }
|
||||
codex-skills-extension = { workspace = true }
|
||||
codex-state = { workspace = true }
|
||||
codex-stdio-to-uds = { workspace = true }
|
||||
codex-terminal-detection = { workspace = true }
|
||||
codex-thread-store = { workspace = true }
|
||||
codex-tui = { workspace = true }
|
||||
codex-uds = { workspace = true }
|
||||
codex-utils-absolute-path = { workspace = true }
|
||||
codex-utils-path = { workspace = true }
|
||||
codex-utils-path-uri = { workspace = true }
|
||||
@@ -98,6 +98,7 @@ sys-locale = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
tokio = { workspace = true, features = [
|
||||
"io-std",
|
||||
"io-util",
|
||||
"macros",
|
||||
"net",
|
||||
"process",
|
||||
@@ -130,7 +131,6 @@ app_test_support = { workspace = true }
|
||||
assert_cmd = { workspace = true }
|
||||
assert_matches = { workspace = true }
|
||||
codex-app-server-client = { workspace = true }
|
||||
codex-uds = { workspace = true }
|
||||
codex-utils-cargo-bin = { workspace = true }
|
||||
codex-utils-pty = { workspace = true }
|
||||
core_test_support = { workspace = true }
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//! Relays bytes between the CLI's stdio and the app-server control socket.
|
||||
|
||||
#![deny(clippy::print_stdout)]
|
||||
|
||||
use std::io;
|
||||
@@ -9,7 +11,7 @@ use tokio::io::AsyncWriteExt;
|
||||
|
||||
/// Connects to the Unix Domain Socket at `socket_path` and relays data between
|
||||
/// standard input/output and the socket.
|
||||
pub async fn run(socket_path: &Path) -> anyhow::Result<()> {
|
||||
pub(crate) async fn run(socket_path: &Path) -> anyhow::Result<()> {
|
||||
let stream = UnixStream::connect(socket_path)
|
||||
.await
|
||||
.with_context(|| format!("failed to connect to socket at {}", socket_path.display()))?;
|
||||
@@ -54,6 +54,7 @@ static ALLOCATOR: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
|
||||
|
||||
#[cfg(any(target_os = "macos", target_os = "windows"))]
|
||||
mod app_cmd;
|
||||
mod app_server_proxy;
|
||||
mod config_override_args;
|
||||
mod daemon_install;
|
||||
mod daemon_telemetry;
|
||||
@@ -240,10 +241,6 @@ enum Subcommand {
|
||||
#[clap(hide = true)]
|
||||
ResponsesApiProxy(ResponsesApiProxyArgs),
|
||||
|
||||
/// Internal: relay stdio to a Unix domain socket.
|
||||
#[clap(hide = true, name = "stdio-to-uds")]
|
||||
StdioToUds(StdioToUdsCommand),
|
||||
|
||||
/// [EXPERIMENTAL] Run the standalone exec-server service.
|
||||
ExecServer(ExecServerCommand),
|
||||
|
||||
@@ -763,13 +760,6 @@ struct GenerateInternalJsonSchemaCommand {
|
||||
out_dir: PathBuf,
|
||||
}
|
||||
|
||||
#[derive(Debug, Parser)]
|
||||
struct StdioToUdsCommand {
|
||||
/// Path to the Unix domain socket to connect to.
|
||||
#[arg(value_name = "SOCKET_PATH", value_parser = parse_socket_path)]
|
||||
socket_path: AbsolutePathBuf,
|
||||
}
|
||||
|
||||
fn parse_socket_path(raw: &str) -> Result<AbsolutePathBuf, String> {
|
||||
AbsolutePathBuf::relative_to_current_dir(raw)
|
||||
.map_err(|err| format!("failed to resolve socket path `{raw}`: {err}"))
|
||||
@@ -1536,7 +1526,7 @@ async fn dispatch_cli(
|
||||
codex_app_server::app_server_control_socket_path(&codex_home)?
|
||||
}
|
||||
};
|
||||
codex_stdio_to_uds::run(socket_path.as_path()).await?;
|
||||
app_server_proxy::run(socket_path.as_path()).await?;
|
||||
}
|
||||
Some(AppServerSubcommand::GenerateTs(gen_cli)) => {
|
||||
let options = codex_app_server_protocol::GenerateTsOptions {
|
||||
@@ -1959,15 +1949,6 @@ async fn dispatch_cli(
|
||||
tokio::task::spawn_blocking(move || codex_responses_api_proxy::run_main(args))
|
||||
.await??;
|
||||
}
|
||||
Some(Subcommand::StdioToUds(cmd)) => {
|
||||
reject_remote_mode_for_subcommand(
|
||||
root_remote.as_deref(),
|
||||
root_remote_auth_token_env.as_deref(),
|
||||
"stdio-to-uds",
|
||||
)?;
|
||||
let socket_path = cmd.socket_path;
|
||||
codex_stdio_to_uds::run(socket_path.as_path()).await?;
|
||||
}
|
||||
Some(Subcommand::ExecServer(mut cmd)) => {
|
||||
reject_remote_mode_for_subcommand(
|
||||
root_remote.as_deref(),
|
||||
@@ -2465,7 +2446,6 @@ fn unsupported_subcommand_name_for_strict_config(
|
||||
Some(Subcommand::Execpolicy(_)) => Some("execpolicy"),
|
||||
Some(Subcommand::Apply(_)) => Some("apply"),
|
||||
Some(Subcommand::ResponsesApiProxy(_)) => Some("responses-api-proxy"),
|
||||
Some(Subcommand::StdioToUds(_)) => Some("stdio-to-uds"),
|
||||
Some(Subcommand::Features(_)) => Some("features"),
|
||||
Some(Subcommand::TcpTunnel(_)) => Some("tcp-tunnel"),
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
use std::io::ErrorKind;
|
||||
|
||||
use anyhow::Context;
|
||||
use codex_uds::UnixListener;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
use tokio::process::Command;
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipes_stdin_and_stdout_through_socket() -> anyhow::Result<()> {
|
||||
const REQUEST: &[u8] = b"request";
|
||||
|
||||
let dir = tempfile::TempDir::new().context("failed to create temp dir")?;
|
||||
let socket_path = dir.path().join("socket");
|
||||
let request_path = dir.path().join("request.txt");
|
||||
std::fs::write(&request_path, REQUEST).context("failed to write child stdin fixture")?;
|
||||
let mut listener = match UnixListener::bind(&socket_path).await {
|
||||
Ok(listener) => listener,
|
||||
Err(err) if err.kind() == ErrorKind::PermissionDenied => {
|
||||
eprintln!("skipping test: failed to bind unix socket: {err}");
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(err).context("failed to bind test unix socket");
|
||||
}
|
||||
};
|
||||
|
||||
let exchange = async {
|
||||
let mut connection = listener
|
||||
.accept()
|
||||
.await
|
||||
.context("failed to accept test connection")?;
|
||||
// Reply as soon as the full request arrives, without waiting for EOF.
|
||||
let mut received = [0; REQUEST.len()];
|
||||
connection
|
||||
.read_exact(&mut received)
|
||||
.await
|
||||
.context("failed to read data from client")?;
|
||||
connection
|
||||
.write_all(b"response")
|
||||
.await
|
||||
.context("failed to write response to client")?;
|
||||
anyhow::Ok(received)
|
||||
};
|
||||
|
||||
let proxy = async {
|
||||
let stdin =
|
||||
std::fs::File::open(&request_path).context("failed to open child stdin fixture")?;
|
||||
let command =
|
||||
codex_utils_process::background_command(codex_utils_cargo_bin::cargo_bin("codex")?);
|
||||
let output = Command::from(command)
|
||||
.args(["app-server", "proxy", "--sock"])
|
||||
.arg(&socket_path)
|
||||
.env("CODEX_HOME", dir.path())
|
||||
.stdin(stdin)
|
||||
.kill_on_drop(true)
|
||||
.output()
|
||||
.await
|
||||
.context("failed to run codex app-server proxy")?;
|
||||
anyhow::ensure!(
|
||||
output.status.success(),
|
||||
"codex app-server proxy exited with {}: {}",
|
||||
output.status,
|
||||
String::from_utf8_lossy(&output.stderr).trim_end()
|
||||
);
|
||||
anyhow::Ok(output.stdout)
|
||||
};
|
||||
|
||||
let (received, stdout) = tokio::try_join!(exchange, proxy)?;
|
||||
assert_eq!(
|
||||
(received.as_slice(), stdout.as_slice()),
|
||||
(REQUEST, b"response".as_slice())
|
||||
);
|
||||
Ok(())
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
load("//:defs.bzl", "codex_rust_crate")
|
||||
|
||||
codex_rust_crate(
|
||||
name = "stdio-to-uds",
|
||||
crate_name = "codex_stdio_to_uds",
|
||||
)
|
||||
@@ -1,33 +0,0 @@
|
||||
[package]
|
||||
name = "codex-stdio-to-uds"
|
||||
version.workspace = true
|
||||
edition.workspace = true
|
||||
license.workspace = true
|
||||
|
||||
[[bin]]
|
||||
name = "codex-stdio-to-uds"
|
||||
path = "src/main.rs"
|
||||
|
||||
[lib]
|
||||
name = "codex_stdio_to_uds"
|
||||
path = "src/lib.rs"
|
||||
test = false
|
||||
doctest = false
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = { workspace = true }
|
||||
codex-uds = { workspace = true }
|
||||
tokio = { workspace = true, features = [
|
||||
"io-std",
|
||||
"io-util",
|
||||
"macros",
|
||||
"rt-multi-thread",
|
||||
] }
|
||||
|
||||
[dev-dependencies]
|
||||
codex-utils-cargo-bin = { workspace = true }
|
||||
pretty_assertions = { workspace = true }
|
||||
tempfile = { workspace = true }
|
||||
@@ -1,20 +0,0 @@
|
||||
# codex-stdio-to-uds
|
||||
|
||||
Traditionally, there are two transport mechanisms for an MCP server: stdio and HTTP.
|
||||
|
||||
This crate helps enable a third, which is UNIX domain socket, because it has the advantages that:
|
||||
|
||||
- The UDS can be attached to long-running process, like an HTTP server.
|
||||
- The UDS can leverage UNIX file permissions to restrict access.
|
||||
|
||||
To that end, this crate provides an adapter between a UDS and stdio. The idea is that someone could start an MCP server that communicates over `/tmp/mcp.sock`. Then the user could specify this on the fly like so:
|
||||
|
||||
```
|
||||
codex --config mcp_servers.example={command="codex-stdio-to-uds",args=["/tmp/mcp.sock"]}
|
||||
```
|
||||
|
||||
Unfortunately, the Rust standard library does not provide support for UNIX domain sockets on Windows today even though support was added in October 2018 in Windows 10:
|
||||
|
||||
https://github.com/rust-lang/rust/issues/56533
|
||||
|
||||
As a workaround, this crate uses `codex-uds`, which provides a cross-platform async UDS API backed by https://crates.io/crates/uds_windows on Windows.
|
||||
@@ -1,20 +0,0 @@
|
||||
use std::env;
|
||||
use std::path::PathBuf;
|
||||
use std::process;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> anyhow::Result<()> {
|
||||
let mut args = env::args_os().skip(1);
|
||||
let Some(socket_path) = args.next() else {
|
||||
eprintln!("Usage: codex-stdio-to-uds <socket-path>");
|
||||
process::exit(1);
|
||||
};
|
||||
|
||||
if args.next().is_some() {
|
||||
eprintln!("Expected exactly one argument: <socket-path>");
|
||||
process::exit(1);
|
||||
}
|
||||
|
||||
let socket_path = PathBuf::from(socket_path);
|
||||
codex_stdio_to_uds::run(&socket_path).await
|
||||
}
|
||||
@@ -1,157 +0,0 @@
|
||||
use std::io::ErrorKind;
|
||||
use std::io::Read;
|
||||
use std::process::Command;
|
||||
use std::process::ExitStatus;
|
||||
use std::process::Stdio;
|
||||
use std::sync::mpsc;
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
use anyhow::Context;
|
||||
use codex_uds::UnixListener;
|
||||
use pretty_assertions::assert_eq;
|
||||
use tokio::io::AsyncReadExt;
|
||||
use tokio::io::AsyncWriteExt;
|
||||
|
||||
#[tokio::test]
|
||||
async fn pipes_stdin_and_stdout_through_socket() -> anyhow::Result<()> {
|
||||
// This test intentionally avoids `read_to_end()` on the server side because
|
||||
// waiting for EOF can race with socket half-close behavior on slower runners.
|
||||
// Reading the exact request length keeps the test deterministic.
|
||||
//
|
||||
// We also use `std::process::Command` (instead of `assert_cmd`) so we can
|
||||
// poll/kill on timeout and include incremental server events + stderr in
|
||||
// failure output, which makes flaky failures actionable to debug.
|
||||
let dir = tempfile::TempDir::new().context("failed to create temp dir")?;
|
||||
let socket_path = dir.path().join("socket");
|
||||
let request = b"request";
|
||||
let request_path = dir.path().join("request.txt");
|
||||
std::fs::write(&request_path, request).context("failed to write child stdin fixture")?;
|
||||
let listener = match UnixListener::bind(&socket_path).await {
|
||||
Ok(listener) => listener,
|
||||
Err(err) if err.kind() == ErrorKind::PermissionDenied => {
|
||||
eprintln!("skipping test: failed to bind unix socket: {err}");
|
||||
return Ok(());
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(err).context("failed to bind test unix socket");
|
||||
}
|
||||
};
|
||||
|
||||
let (event_tx, event_rx) = mpsc::channel();
|
||||
let server_task = tokio::spawn(async move {
|
||||
let mut listener = listener;
|
||||
let _ = event_tx.send("waiting for accept".to_string());
|
||||
let mut connection = listener
|
||||
.accept()
|
||||
.await
|
||||
.context("failed to accept test connection")?;
|
||||
let _ = event_tx.send("accepted connection".to_string());
|
||||
let mut received = vec![0; request.len()];
|
||||
connection
|
||||
.read_exact(&mut received)
|
||||
.await
|
||||
.context("failed to read data from client")?;
|
||||
let _ = event_tx.send(format!("read {} bytes", received.len()));
|
||||
connection
|
||||
.write_all(b"response")
|
||||
.await
|
||||
.context("failed to write response to client")?;
|
||||
let _ = event_tx.send("wrote response".to_string());
|
||||
anyhow::Ok(received)
|
||||
});
|
||||
|
||||
struct ChildOutput {
|
||||
status: ExitStatus,
|
||||
stdout: Vec<u8>,
|
||||
stderr: Vec<u8>,
|
||||
server_events: Vec<String>,
|
||||
}
|
||||
|
||||
let child_task = tokio::task::spawn_blocking(move || -> anyhow::Result<ChildOutput> {
|
||||
let stdin =
|
||||
std::fs::File::open(&request_path).context("failed to open child stdin fixture")?;
|
||||
let mut child = Command::new(codex_utils_cargo_bin::cargo_bin("codex-stdio-to-uds")?)
|
||||
.arg(&socket_path)
|
||||
.stdin(Stdio::from(stdin))
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
.spawn()
|
||||
.context("failed to spawn codex-stdio-to-uds")?;
|
||||
|
||||
let mut child_stdout = child.stdout.take().context("missing child stdout")?;
|
||||
let mut child_stderr = child.stderr.take().context("missing child stderr")?;
|
||||
let (stdout_tx, stdout_rx) = mpsc::channel();
|
||||
let (stderr_tx, stderr_rx) = mpsc::channel();
|
||||
thread::spawn(move || {
|
||||
let mut stdout = Vec::new();
|
||||
let result = child_stdout.read_to_end(&mut stdout).map(|_| stdout);
|
||||
let _ = stdout_tx.send(result);
|
||||
});
|
||||
thread::spawn(move || {
|
||||
let mut stderr = Vec::new();
|
||||
let result = child_stderr.read_to_end(&mut stderr).map(|_| stderr);
|
||||
let _ = stderr_tx.send(result);
|
||||
});
|
||||
|
||||
let mut server_events = Vec::new();
|
||||
let deadline = Instant::now() + Duration::from_secs(5);
|
||||
let status = loop {
|
||||
while let Ok(event) = event_rx.try_recv() {
|
||||
server_events.push(event);
|
||||
}
|
||||
|
||||
if let Some(status) = child.try_wait().context("failed to poll child status")? {
|
||||
break status;
|
||||
}
|
||||
|
||||
if Instant::now() >= deadline {
|
||||
let _ = child.kill();
|
||||
let _ = child.wait();
|
||||
let stderr = stderr_rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.context("timed out waiting for child stderr after kill")?
|
||||
.context("failed to read child stderr")?;
|
||||
anyhow::bail!(
|
||||
"codex-stdio-to-uds did not exit in time; server events: {:?}; stderr: {}",
|
||||
server_events,
|
||||
String::from_utf8_lossy(&stderr).trim_end()
|
||||
);
|
||||
}
|
||||
|
||||
thread::sleep(Duration::from_millis(25));
|
||||
};
|
||||
|
||||
let stdout = stdout_rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.context("timed out waiting for child stdout")?
|
||||
.context("failed to read child stdout")?;
|
||||
let stderr = stderr_rx
|
||||
.recv_timeout(Duration::from_secs(1))
|
||||
.context("timed out waiting for child stderr")?
|
||||
.context("failed to read child stderr")?;
|
||||
|
||||
Ok(ChildOutput {
|
||||
status,
|
||||
stdout,
|
||||
stderr,
|
||||
server_events,
|
||||
})
|
||||
});
|
||||
|
||||
let child_output = child_task.await.context("child task panicked")??;
|
||||
assert!(
|
||||
child_output.status.success(),
|
||||
"codex-stdio-to-uds exited with {status}; server events: {:?}; stderr: {}",
|
||||
child_output.server_events,
|
||||
String::from_utf8_lossy(&child_output.stderr).trim_end(),
|
||||
status = child_output.status
|
||||
);
|
||||
assert_eq!(child_output.stdout, b"response");
|
||||
|
||||
let received = server_task.await.context("server task panicked")??;
|
||||
assert_eq!(received, request);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user