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:
Michael Bolin
2026-11-18 02:18:36 +00:00
committed by copyberry
parent b65d9a3b2d
commit bb6b4b63b9
11 changed files with 83 additions and 276 deletions
+2 -2
View File
@@ -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 }
+48
View File
@@ -0,0 +1,48 @@
//! Relays bytes between the CLI's stdio and the app-server control socket.
#![deny(clippy::print_stdout)]
use std::io;
use std::path::Path;
use anyhow::Context;
use codex_uds::UnixStream;
use tokio::io::AsyncWriteExt;
/// Connects to the Unix Domain Socket at `socket_path` and relays data between
/// standard input/output and the socket.
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()))?;
let (mut socket_reader, mut socket_writer) = tokio::io::split(stream);
let copy_socket_to_stdout = async {
let mut stdout = tokio::io::stdout();
tokio::io::copy(&mut socket_reader, &mut stdout).await?;
stdout.flush().await?;
Ok(())
};
let copy_stdin_to_socket = async {
let mut stdin = tokio::io::stdin();
tokio::io::copy(&mut stdin, &mut socket_writer)
.await
.context("failed to copy data from stdin to socket")?;
// The peer can close immediately after sending its response; in that
// race, half-closing our write side can report NotConnected on some
// platforms.
if let Err(err) = socket_writer.shutdown().await
&& err.kind() != io::ErrorKind::NotConnected
{
return Err(err).context("failed to shutdown socket writer");
}
anyhow::Ok(())
};
tokio::try_join!(copy_stdin_to_socket, copy_socket_to_stdout)
.context("failed to relay data between stdio and socket")?;
Ok(())
}
+2 -22
View File
@@ -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"),
}
+76
View File
@@ -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(())
}