Add opt-in gRPC over stdio for the code-mode host (#52723)

## What changed

- Add `grpc+stdio://` as a host transport and the disabled-by-default `code_mode_host_grpc` feature flag to select it.
- Share a lazy HTTP/2 channel and host process across code-mode sessions while keeping session state isolated.
- Use HTTP/2 keepalives to detect unresponsive hosts, kill and reap disconnected children, and spawn a fresh host on reconnect. Exit the host when its stdio connection closes.

## Testing

Add coverage for shared-host session isolation, host exit on stdin EOF, and recovery after an unresponsive host is reaped. Exercise nested tool calls with both legacy stdio and gRPC stdio, asserting that the feature flag selects the expected transport.

GitOrigin-RevId: 31389d8540de236b8e0571d3c24e29e117b75c4a
This commit is contained in:
Channing Conger
2026-10-09 23:26:34 +00:00
committed by copyberry
parent 07757f448f
commit 2dc0791c7d
17 changed files with 566 additions and 18 deletions
@@ -0,0 +1,59 @@
//! Records launches of the real code-mode host through a Unix wrapper. Each log
//! line represents one process spawn and its arguments, independently of sessions.
use std::fs;
use std::path::PathBuf;
use anyhow::Context;
use anyhow::Result;
use tempfile::TempDir;
/// Observes host process launches without replacing either transport implementation.
pub struct CodeModeHostRecorder {
_directory: TempDir,
program: PathBuf,
invocations: PathBuf,
pid: PathBuf,
}
impl CodeModeHostRecorder {
pub fn new() -> Result<Self> {
let directory = tempfile::tempdir()?;
let program = directory.path().join("code mode host wrapper");
let invocations = directory.path().join("host invocations");
let pid = directory.path().join("host pid");
fs::write(&invocations, "")?;
let host_program = codex_utils_cargo_bin::cargo_bin("codex-code-mode-host")?;
let quoted_host =
shlex::try_quote(host_program.to_str().context("host path is not UTF-8")?)?;
let quoted_log = shlex::try_quote(invocations.to_str().context("log path is not UTF-8")?)?;
let quoted_pid = shlex::try_quote(pid.to_str().context("PID path is not UTF-8")?)?;
codex_utils_cargo_bin::write_executable(
&program,
&format!(
"#!/bin/sh\nprintf '%s\\n' \"$*\" >> {quoted_log}\nprintf '%s\\n' \"$$\" > {quoted_pid}\nexec {quoted_host} \"$@\"\n"
),
)?;
Ok(Self {
_directory: directory,
program,
invocations,
pid,
})
}
pub fn program(&self) -> PathBuf {
self.program.clone()
}
pub fn invocations(&self) -> Result<Vec<String>> {
Ok(fs::read_to_string(&self.invocations)?
.lines()
.map(str::to_string)
.collect())
}
pub fn pid(&self) -> Result<u32> {
Ok(fs::read_to_string(&self.pid)?.trim().parse()?)
}
}
+2
View File
@@ -25,6 +25,8 @@ use std::path::Path;
use std::path::PathBuf;
pub mod apps_test_server;
#[cfg(unix)]
pub mod code_mode_host;
pub mod context_snapshot;
pub mod exec_server;
pub mod hooks;
+41 -11
View File
@@ -77,6 +77,8 @@ use core_test_support::apps_test_server::DIRECT_CALENDAR_APP_ONLY_TOOL;
use core_test_support::apps_test_server::recorded_apps_tool_calls;
use core_test_support::apps_test_server::search_capable_apps_builder;
use core_test_support::assert_regex_match;
#[cfg(unix)]
use core_test_support::code_mode_host::CodeModeHostRecorder;
use core_test_support::responses;
use core_test_support::responses::ResponseMock;
use core_test_support::responses::ResponsesRequest;
@@ -3958,23 +3960,34 @@ text(JSON.stringify({{
}
#[cfg_attr(windows, ignore = "no exec_command on Windows")]
#[test_case(false; "legacy_stdio")]
#[test_case(true; "grpc_stdio")]
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn code_mode_only_can_call_nested_tools() -> Result<()> {
async fn code_mode_only_can_call_nested_tools(grpc_stdio: bool) -> Result<()> {
skip_if_no_network!(Ok(()));
#[cfg(unix)]
let recorder = CodeModeHostRecorder::new()?;
let server = responses::start_mock_server().await;
let command = match core_test_support::test_target_os() {
core_test_support::TestTargetOs::Windows => {
"[Console]::Write('code_mode_only_nested_tool_marker')"
}
core_test_support::TestTargetOs::Linux | core_test_support::TestTargetOs::MacOs => {
"printf code_mode_only_nested_tool_marker"
}
};
let source = format!(
r#"
const output = await tools.exec_command({{ cmd: {command:?} }});
text(output.output);
"#,
);
responses::mount_sse_once(
&server,
sse(vec![
ev_response_created("resp-1"),
ev_custom_tool_call(
"call-1",
"exec",
r#"
const output = await tools.exec_command({ cmd: "printf code_mode_only_nested_tool_marker" });
text(output.output);
"#,
),
ev_custom_tool_call("call-1", "exec", &source),
ev_completed("resp-1"),
]),
)
@@ -3988,10 +4001,17 @@ text(output.output);
)
.await;
let mut builder = test_codex().with_config(|config| {
let mut builder = test_codex().with_config(move |config| {
let _ = config.features.enable(Feature::CodeModeOnly);
if grpc_stdio {
let _ = config.features.enable(Feature::CodeModeHostGrpc);
}
});
let test = builder.build(&server).await?;
#[cfg(unix)]
{
builder = builder.with_code_mode_host_program(recorder.program());
}
let test = builder.build_with_auto_env(&server).await?;
test.submit_turn("use exec to run nested tool in code mode only")
.await?;
@@ -4003,6 +4023,16 @@ text(output.output);
"code_mode_only nested tool call failed unexpectedly: {output}"
);
assert_eq!(output, "code_mode_only_nested_tool_marker");
#[cfg(unix)]
assert_eq!(
recorder.invocations()?,
vec![if grpc_stdio {
"--listen grpc+stdio://".to_string()
} else {
String::new()
}],
"the feature flag must select the expected host transport"
);
assert!(
request
.custom_tool_call_output("call-1")