From d3db7a43319a9b23c45b005ca3bc70dae75bd8ca Mon Sep 17 00:00:00 2001 From: blockmandev <194144408+blockmandev@users.noreply.github.com> Date: Sat, 24 Oct 2026 09:59:43 -0600 Subject: [PATCH] fix: a turn whose tool server never started fails and says why (fixes #68) Root cause, measured on Windows 11 under sbx (native, network allow): sbx's write-restricted token runs the command as the user, and a named pipe created with default security grants write only to SYSTEM, Administrators and the creating user, not to the token's restricting SIDs. Node creates such pipes for a child's piped stdio, so `child_process.spawn` with stdio 'pipe' fails with EPERM ('ignore' and 'inherit' work). Claude Code therefore starts no MCP server: its init line reports incode's server as "failed", the model runs with none of incode's tools, writes tool calls out as plain text that nothing runs, and incode reported the turn as success (or it ran on until the wall clock). That sandbox limit is sbx's to lift (the #38 design). This change makes incode stop treating such a turn as a result: - tool_bridge: the bridge records when the tool server asks, with the turn's token, for the tools (`Bridge::tools_listed`); `require_tools` and `no_tools` give the one refusal; the server half now traces its start, a failed start and a failed connect, and the parent traces the list request, so INCODE_TRACE names which half broke. - Claude route: an init line naming incode's server "failed" ends the turn at once; a turn that produced a result without the list request is refused at the end. `finish` now kills the CLI's tree on any error, not only on cancel, as the codex route does, so a stopped turn leaves nothing running. - `drive`'s line handler may end the turn with an error (`LineHandler`). - The codex route is not held to the list check: whether codex always lists before the model runs is not yet measured (#19). Tests: the stand-in CLIs now ask for the tools as a real one does; new tests for a toolless turn, a reported server failure (stopped within 5 s, and on Unix the CLI no longer running), the init-line reader, and run_bridged directly. The bridged codex flow test now runs on Windows too. Each new check fails with its fix taken out. --- src/agent/claude_code/tests.rs | 174 +++++++++++++++++++++++++++- src/agent/claude_code/turn.rs | 30 +++-- src/agent/claude_code/wire.rs | 37 ++++++ src/agent/claude_code/wire_tests.rs | 27 +++++ src/agent/codex/bridged.rs | 8 +- src/agent/codex/bridged_tests.rs | 42 +++++++ src/agent/codex/mod.rs | 6 +- src/agent/codex/tests.rs | 140 +++++++++++++++------- src/tool_bridge.rs | 102 ++++++++++++++-- 9 files changed, 496 insertions(+), 70 deletions(-) create mode 100644 src/agent/codex/bridged_tests.rs diff --git a/src/agent/claude_code/tests.rs b/src/agent/claude_code/tests.rs index 779683a..00e7960 100644 --- a/src/agent/claude_code/tests.rs +++ b/src/agent/claude_code/tests.rs @@ -281,6 +281,46 @@ fn an_agent_that_floods_stderr_fails_promptly_with_a_bounded_clean_reason() { ); } +/// What a stand-in CLI runs first, as the real one does when it starts: its +/// tool server reaches the bridge with the turn's token and asks for the +/// tools. Without it a turn is refused, as a real one is when the server +/// never started (`tool_bridge::require_tools`). +/// +/// One line of the stand-in's script. On Windows it calls a PowerShell file +/// written beside it (quoting JSON through `cmd` is not worth it); on Unix, +/// bash's `/dev/tcp`, so those stand-ins start with `#!/bin/bash`. +pub(crate) fn lists_the_tools(dir: &std::path::Path) -> String { + #[cfg(windows)] + { + let script = dir.join("list-tools.ps1"); + std::fs::write( + &script, + "$a = $env:INCODE_TOOL_BRIDGE.Split(':')\r\n\ + $c = New-Object Net.Sockets.TcpClient($a[0], [int]$a[1])\r\n\ + $s = $c.GetStream()\r\n\ + $w = New-Object IO.StreamWriter($s)\r\n\ + $w.WriteLine('{\"op\":\"list\",\"token\":\"' + $env:INCODE_TOOL_TOKEN + '\"}')\r\n\ + $w.Flush()\r\n\ + [void](New-Object IO.StreamReader($s)).ReadLine()\r\n\ + $c.Close()\r\n", + ) + .expect("write the stand-in's tool server"); + format!( + "\"%SystemRoot%\\System32\\WindowsPowerShell\\v1.0\\powershell.exe\" -NoProfile \ + -ExecutionPolicy Bypass -File \"{}\" >nul\r\n", + script.display() + ) + } + #[cfg(not(windows))] + { + let _ = dir; + "exec 3<>\"/dev/tcp/${INCODE_TOOL_BRIDGE%:*}/${INCODE_TOOL_BRIDGE##*:}\"\n\ + printf '{\"op\":\"list\",\"token\":\"%s\"}\\n' \"$INCODE_TOOL_TOKEN\" >&3\n\ + read -r _ <&3\nexec 3>&-\n" + .to_string() + } +} + /// A stand-in CLI whose option parser does not know the thinking flag: /// named on its argv, it says so on stderr and exits 1 before any /// output; otherwise it prints a short turn. Every start is counted in @@ -300,6 +340,7 @@ fn a_cli_without_the_thinking_flag(dir: &std::path::Path) -> PathBuf { echo error: unknown option '--thinking-display' 1>&2\r\n\ exit /b 1\r\n)\r\n", ); + body.push_str(&lists_the_tools(dir)); for line in LINES { body.push_str(&format!("echo {line}\r\n")); } @@ -311,10 +352,11 @@ fn a_cli_without_the_thinking_flag(dir: &std::path::Path) -> PathBuf { { use std::os::unix::fs::PermissionsExt; let mut body = String::from( - "#!/bin/sh\necho x >> \"$(dirname \"$0\")/starts.txt\"\n\ + "#!/bin/bash\necho x >> \"$(dirname \"$0\")/starts.txt\"\n\ case \"$*\" in *--thinking-display*) \ echo \"error: unknown option '--thinking-display'\" >&2; exit 1;; esac\n", ); + body.push_str(&lists_the_tools(dir)); for line in LINES { body.push_str(&format!("echo '{line}'\n")); } @@ -389,7 +431,7 @@ pub(crate) fn stand_in_cli(dir: &std::path::Path, waits: bool) -> PathBuf { ]; #[cfg(windows)] { - let mut body = format!("@echo off\r\necho {}\r\n", LINES[0]); + let mut body = format!("@echo off\r\n{}echo {}\r\n", lists_the_tools(dir), LINES[0]); if waits { body.push_str("ping -n 30 127.0.0.1 >nul\r\n"); } @@ -403,7 +445,7 @@ pub(crate) fn stand_in_cli(dir: &std::path::Path, waits: bool) -> PathBuf { #[cfg(not(windows))] { use std::os::unix::fs::PermissionsExt; - let mut body = format!("#!/bin/sh\necho '{}'\n", LINES[0]); + let mut body = format!("#!/bin/bash\n{}echo '{}'\n", lists_the_tools(dir), LINES[0]); if waits { body.push_str("sleep 30\n"); } @@ -454,6 +496,132 @@ fn a_whole_turn_reports_these_events_and_this_outcome() { assert_eq!(backend.session_id(), Some("sub-conv-1")); } +/// A stand-in that prints a whole, successful-looking turn but never starts +/// incode's tool server: the shape measured under sbx on Windows, where the +/// CLI could not start any child with piped stdio (#68). +fn a_cli_whose_tool_server_never_starts(dir: &std::path::Path) -> PathBuf { + const LINES: [&str; 3] = [ + r#"{"type":"system","subtype":"init","session_id":"no-tools-1","model":"m"}"#, + r#"{"type":"assistant","message":{"content":[{"type":"text","text":"I read the file: all good"}]},"parent_tool_use_id":null}"#, + r#"{"type":"result","is_error":false,"stop_reason":"end_turn","session_id":"no-tools-1","usage":{"input_tokens":1,"output_tokens":1}}"#, + ]; + #[cfg(windows)] + { + let mut body = String::from("@echo off\r\n"); + for line in LINES { + body.push_str(&format!("echo {line}\r\n")); + } + let path = dir.join("toolless-agent.cmd"); + std::fs::write(&path, body).expect("write the stand-in agent"); + path + } + #[cfg(not(windows))] + { + use std::os::unix::fs::PermissionsExt; + let mut body = String::from("#!/bin/sh\n"); + for line in LINES { + body.push_str(&format!("echo '{line}'\n")); + } + let path = dir.join("toolless-agent.sh"); + std::fs::write(&path, body).expect("write the stand-in agent"); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) + .expect("make it runnable"); + path + } +} + +/// **A TURN THAT NEVER REACHED incode's TOOLS IS NOT A RESULT.** The model +/// had nothing to act with, so a reply like "I read the file" is made up; +/// it was reported as `is_error: false`. Now the turn fails and says why. +#[test] +fn a_turn_whose_tool_server_never_started_fails_and_says_so() { + let dir = tempfile::tempdir().expect("scratch"); + let fake = a_cli_whose_tool_server_never_starts(dir.path()); + let mut backend = + ClaudeCodeBackend::new(dir.path().to_path_buf()).with_binary(fake.to_string_lossy()); + let err = backend + .run_turn("read hello.txt", &mut |_| {}) + .expect_err("a turn with none of incode's tools is not a finished one"); + assert!( + err.to_string() + .starts_with("Claude Code never started incode's tool server"), + "{err}" + ); +} + +/// A stand-in whose `init` line says incode's server failed, as Claude Code +/// says it under sbx on Windows (#68), and which then goes on, as the +/// toolless model did until the wall clock: about five seconds later it +/// writes `still-running.txt` beside itself, then a result. +fn a_cli_reporting_the_tool_server_failed(dir: &std::path::Path) -> PathBuf { + const INIT: &str = r#"{"type":"system","subtype":"init","session_id":"failed-1","model":"m","mcp_servers":[{"name":"incode","status":"failed","source":"dynamic"}]}"#; + const RESULT: &str = r#"{"type":"result","is_error":false,"stop_reason":"end_turn","session_id":"failed-1","usage":{"input_tokens":1,"output_tokens":1}}"#; + #[cfg(windows)] + { + let path = dir.join("failed-server-agent.cmd"); + std::fs::write( + &path, + format!( + "@echo off\r\necho {INIT}\r\nping -n 6 127.0.0.1 >nul\r\n\ + echo x>\"%~dp0still-running.txt\"\r\necho {RESULT}\r\n" + ), + ) + .expect("write the stand-in agent"); + path + } + #[cfg(not(windows))] + { + use std::os::unix::fs::PermissionsExt; + let path = dir.join("failed-server-agent.sh"); + std::fs::write( + &path, + format!( + "#!/bin/sh\necho '{INIT}'\nsleep 5\n\ + touch \"$(dirname \"$0\")/still-running.txt\"\necho '{RESULT}'\n" + ), + ) + .expect("write the stand-in agent"); + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)) + .expect("make it runnable"); + path + } +} + +/// **WHEN THE CLI SAYS AT ONCE THAT incode's SERVER FAILED, THE TURN STOPS +/// AT ONCE**, with the reason, and the CLI is not left running. Before, the +/// toolless model ran on until the wall clock (281 s measured). +#[test] +fn a_reported_server_failure_stops_the_turn_at_once() { + let dir = tempfile::tempdir().expect("scratch"); + let fake = a_cli_reporting_the_tool_server_failed(dir.path()); + let mut backend = + ClaudeCodeBackend::new(dir.path().to_path_buf()).with_binary(fake.to_string_lossy()); + let started = std::time::Instant::now(); + let err = backend + .run_turn("read hello.txt", &mut |_| {}) + .expect_err("a turn whose tool server failed is not a finished one"); + assert!( + err.to_string() + .starts_with("Claude Code never started incode's tool server"), + "{err}" + ); + assert!( + started.elapsed() < std::time::Duration::from_secs(5), + "stopped only when the stand-in finished: {:?}", + started.elapsed() + ); + // Past the moment it would have written its marker: a CLI left running + // would have. This catches a missing kill on Linux and macOS, where + // nothing else ends the process group; on Windows the turn's job object + // ends the CLI on drop as well (`agent/process_tree.rs`), so there the + // marker stays absent either way. + std::thread::sleep(std::time::Duration::from_secs(9)); + assert!( + !dir.path().join("still-running.txt").exists(), + "the CLI was left running after its turn was stopped" + ); +} + /// A CLI that cannot be started names itself in the error. #[test] fn a_cli_that_cannot_start_is_named_in_the_error() { diff --git a/src/agent/claude_code/turn.rs b/src/agent/claude_code/turn.rs index b025b2e..965d6e8 100644 --- a/src/agent/claude_code/turn.rs +++ b/src/agent/claude_code/turn.rs @@ -7,8 +7,8 @@ use std::io::BufReader; use std::process::{Child, Command, Stdio}; use super::command::{servers_for, user_message}; -use super::wire::map_line; -use super::{ClaudeCodeBackend, CANCELLED}; +use super::wire::{incode_server_failed, map_line}; +use super::ClaudeCodeBackend; use crate::agent::process_tree::Tree; use crate::agent::stderr_tail::StderrTail; use crate::agent::{AgentError, AgentEvent, TurnOutcome}; @@ -61,7 +61,13 @@ pub(super) fn consume( } } if line.trim().is_empty() { - return Vec::new(); + return Ok(Vec::new()); + } + // Stopped here rather than at the end: a model with no tools runs on + // until it gives up or the wall clock ends it, and its reply is not + // a result either way. + if incode_server_failed(&line) { + return Err(crate::tool_bridge::no_tools("Claude Code")); } let events = map_line(&line, &mut outcome); for event in &events { @@ -74,7 +80,7 @@ pub(super) fn consume( } } } - events + Ok(events) })?; Ok(outcome) } @@ -161,10 +167,13 @@ impl ClaudeCodeBackend { &mut self.tools, &mut self.calls, ); + let listed = bridge.tools_listed(); // Closed as soon as the turn's loop is done: a call that arrives from // here on is refused rather than left waiting on a loop that is gone. drop(bridge); - self.finish(outcome, &mut child, &tree, stderr) + let outcome = self.finish(outcome, &mut child, &tree, stderr)?; + crate::tool_bridge::require_tools(listed, "Claude Code")?; + Ok(outcome) } /// WHAT WAS ACTUALLY SPAWNED, on the ordinary trace. The flags decide the @@ -278,11 +287,12 @@ impl ClaudeCodeBackend { tree: &Tree, stderr: StderrTail, ) -> Result { - // Whatever happened, the child must not outlive this call. A cancelled - // turn that left the agent running would keep writing files with - // nobody reading its output — and that includes whatever it started. - let cancelled = matches!(outcome, Err(ref e) if e.to_string() == CANCELLED); - if cancelled { + // Whatever happened, the child must not outlive this call. A turn that + // ended in an error (stopped, its tool server failed, its output + // unreadable) and left the agent running would keep writing files + // with nobody reading its output — and that includes whatever it + // started. The codex route does the same (`codex/bridged.rs`). + if outcome.is_err() { tree.kill(child); } let outcome = outcome?; diff --git a/src/agent/claude_code/wire.rs b/src/agent/claude_code/wire.rs index 8991a8d..830ead1 100644 --- a/src/agent/claude_code/wire.rs +++ b/src/agent/claude_code/wire.rs @@ -257,6 +257,43 @@ pub(super) fn map_line(line: &str, outcome: &mut Option) -> Vec bool { + if !line.contains("\"init\"") { + return false; + } + let Ok(value) = serde_json::from_str::(line) else { + return false; + }; + if value.get("type").and_then(serde_json::Value::as_str) != Some("system") + || value.get("subtype").and_then(serde_json::Value::as_str) != Some("init") + { + return false; + } + value + .get("mcp_servers") + .and_then(serde_json::Value::as_array) + .into_iter() + .flatten() + .any(|server| { + server.get("name").and_then(serde_json::Value::as_str) + == Some(crate::tool_bridge::CLIENT_KEY) + && server.get("status").and_then(serde_json::Value::as_str) == Some("failed") + }) +} + /// The session's start, from the `init` line. Every other `system` subtype — /// the hook events that precede `init`, among others — says nothing. /// diff --git a/src/agent/claude_code/wire_tests.rs b/src/agent/claude_code/wire_tests.rs index 3e28ac8..fd7e3d6 100644 --- a/src/agent/claude_code/wire_tests.rs +++ b/src/agent/claude_code/wire_tests.rs @@ -31,6 +31,33 @@ fn a_cancelled_turn_stops_and_names_itself() { assert!(seen.is_empty(), "it must stop BEFORE handling more output"); } +/// **Only the init line naming incode's server as `failed` counts**, the +/// measured shape; a connected server, another server failing, an init +/// line without a server list, and any other line do not. +#[test] +fn only_a_failed_incode_server_in_the_init_line_counts() { + use super::wire::incode_server_failed; + let init = |servers: &str| { + format!(r#"{{"type":"system","subtype":"init","session_id":"s","mcp_servers":{servers}}}"#) + }; + assert!(incode_server_failed(&init( + r#"[{"name":"incode","status":"failed","source":"dynamic"}]"# + ))); + assert!(!incode_server_failed(&init( + r#"[{"name":"incode","status":"connected","source":"dynamic"}]"# + ))); + assert!(!incode_server_failed(&init( + r#"[{"name":"other","status":"failed"},{"name":"incode","status":"connected"}]"# + ))); + assert!(!incode_server_failed( + r#"{"type":"system","subtype":"init","session_id":"s"}"# + )); + assert!(!incode_server_failed( + r#"{"type":"assistant","message":{"content":[{"type":"text","text":"\"init\" incode failed"}]}}"# + )); + assert!(!incode_server_failed("not json, but \"init\"")); +} + /// Without cancellation the same stream is read normally — the control /// that proves the test above is measuring the flag and not the fixture. #[test] diff --git a/src/agent/codex/bridged.rs b/src/agent/codex/bridged.rs index 5f402ba..8b533b6 100644 --- a/src/agent/codex/bridged.rs +++ b/src/agent/codex/bridged.rs @@ -2,6 +2,8 @@ //! by incode's toolbox over the tool bridge. //! //! Wired to: [`super::CodexBackend`]'s `run_turn`, on [`super::CodexTools::Incode`]. +//! Tested end to end, on every platform, by `agent/codex/tests.rs` +//! `a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one`. use std::io::BufReader; use std::process::{Command, Stdio}; @@ -72,7 +74,7 @@ impl CodexBackend { tools.set_session(session_id); } } - events + Ok(events) }, ); let (outcome, usage) = reading.finish(); @@ -109,3 +111,7 @@ impl CodexBackend { }) } } + +#[cfg(test)] +#[path = "bridged_tests.rs"] +mod tests; diff --git a/src/agent/codex/bridged_tests.rs b/src/agent/codex/bridged_tests.rs new file mode 100644 index 0000000..7196993 --- /dev/null +++ b/src/agent/codex/bridged_tests.rs @@ -0,0 +1,42 @@ +//! `run_bridged` itself, without `run_turn` around it. The whole flow through +//! `run_turn`, with the argv codex is started with, is pinned in +//! `codex/tests.rs` `a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one`; +//! both use its stand-in codex and tool server (`stand_in_bridged_backend`). + +use crate::agent::{AgentBackend, AgentEvent}; + +/// **`run_bridged` RUNS THE TURN IT IS HANDED**: given the bridged command +/// and an open turn as `run_turn` builds them (`codex/mod.rs`), it runs the +/// stand-in's calls in incode's toolbox — one written, one refused by a deny +/// rule — and its line handler turns each codex line into the events +/// asserted here. +#[test] +fn run_bridged_runs_the_turn_it_is_handed() { + let ws = tempfile::tempdir().expect("ws"); + let bin = tempfile::tempdir().expect("bin"); + let (mut backend, server) = + super::super::tests::stand_in_bridged_backend(ws.path(), bin.path()); + let turn = crate::tool_bridge::Turn::open(None).expect("the channel opens"); + let command = backend.bridged_command("write two files", &server); + + let mut seen = Vec::new(); + let outcome = backend + .run_bridged(command, turn, &mut |e| seen.push(e.clone())) + .expect("the turn completes"); + assert!(!outcome.is_error, "{outcome:?}"); + assert_eq!(outcome.session_id, "fake-thread"); + assert_eq!(backend.session_id(), Some("fake-thread")); + assert_eq!( + std::fs::read_to_string(ws.path().join("notes.txt")).expect("written by incode"), + "written" + ); + assert!( + !ws.path().join("secret.txt").exists(), + "the deny rule did not hold" + ); + assert!( + seen.iter() + .any(|e| matches!(e, AgentEvent::Text(t) if t == "done")), + "codex's lines became events: {seen:?}" + ); +} diff --git a/src/agent/codex/mod.rs b/src/agent/codex/mod.rs index 78a0248..bcac73b 100644 --- a/src/agent/codex/mod.rs +++ b/src/agent/codex/mod.rs @@ -286,9 +286,9 @@ impl CodexBackend { } /// Have codex start `exe` as the tool server instead of this binary. - /// Unix only, as its one caller is (the bridged-turn test's fakes are - /// shell scripts). - #[cfg(all(test, unix))] + /// Called by the bridged-turn test's stand-ins (`tests.rs` + /// `stand_in_bridged_backend`). + #[cfg(test)] fn with_tool_server(mut self, exe: PathBuf) -> Self { self.tool_server = Some(exe); self diff --git a/src/agent/codex/tests.rs b/src/agent/codex/tests.rs index 0b0430a..849be9b 100644 --- a/src/agent/codex/tests.rs +++ b/src/agent/codex/tests.rs @@ -476,13 +476,24 @@ fn fake_codex_child() { return; }; let dir = PathBuf::from(dir); - let argv = std::fs::read(dir.join("argv")).expect("argv"); - let args: Vec = argv - .split(|b| *b == 0) - .filter(|a| !a.is_empty()) - .map(|a| String::from_utf8_lossy(a).into_owned()) - .collect(); - let overrides = overrides_in(&args); + // The registration codex was handed: off its real argv where the + // stand-in's wrapper could record it (`printf '%s\0' "$@"`, Unix), else + // as the backend builds it (`argv.rs` `bridge_registration`), written by + // the test — `cmd` cannot hand on an argv carrying quotes intact. + let overrides = match std::fs::read(dir.join("argv")) { + Ok(argv) => { + let args: Vec = argv + .split(|b| *b == 0) + .filter(|a| !a.is_empty()) + .map(|a| String::from_utf8_lossy(a).into_owned()) + .collect(); + overrides_in(&args) + } + Err(_) => serde_json::from_slice( + &std::fs::read(dir.join("registration")).expect("the registration"), + ) + .expect("a list of overrides"), + }; let command = override_value(&overrides, "mcp_servers.incode.command") .trim_matches('"') .replace("\\\\", "\\"); @@ -491,13 +502,21 @@ fn fake_codex_child() { let passed: Vec = serde_json::from_str(override_value(&overrides, "mcp_servers.incode.env_vars")) .expect("env_vars"); + // Codex's own short list beside the named ones (`rmcp-client` + // `create_env_for_mcp_server`); on Windows that includes what a process + // needs to open a socket or run a `.cmd` at all. + #[cfg(windows)] + let defaults = ["PATH", "SystemRoot", "ComSpec", "PATHEXT", "TEMP", "TMP"]; + #[cfg(not(windows))] + let defaults = ["PATH"]; let mut server = std::process::Command::new(&command) .args(&server_args) .env_clear() - .env("PATH", std::env::var_os("PATH").unwrap_or_default()) .envs( - passed + defaults .iter() + .copied() + .chain(passed.iter().map(String::as_str)) .filter_map(|name| Some((name, std::env::var_os(name)?))), ) .stdin(Stdio::piped()) @@ -567,45 +586,70 @@ fn fake_codex_child() { std::process::exit(0); } -/// A shell script at `path` that runs this test binary's `test` with -/// `marker` set to `value`, after `before`. -#[cfg(unix)] -fn child_script(path: &Path, before: &str, marker: &str, value: &Path, test: &str) { - use std::os::unix::fs::PermissionsExt; +/// A script at `path` that runs this test binary's `test` with `marker` set +/// to `value`: a shell script on Unix, which first records its argv to +/// `records_argv` when given; a `.cmd` on Windows, which records nothing +/// (see [`fake_codex_child`]). +fn child_script(path: &Path, records_argv: Option<&Path>, marker: &str, value: &Path, test: &str) { let exe = std::env::current_exe().expect("test binary"); - let body = format!( - "#!/bin/sh\n{before}\n{marker}='{}' exec '{}' --exact '{test}' --nocapture -q --test-threads=1\n", - value.display(), - exe.display() - ); - std::fs::write(path, body).expect("write the script"); - std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).expect("runnable"); + #[cfg(unix)] + { + use std::os::unix::fs::PermissionsExt; + let before = records_argv + .map(|argv| format!("printf '%s\\0' \"$@\" > '{}'", argv.display())) + .unwrap_or_default(); + let body = format!( + "#!/bin/sh\n{before}\n{marker}='{}' exec '{}' --exact '{test}' --nocapture -q --test-threads=1\n", + value.display(), + exe.display() + ); + std::fs::write(path, body).expect("write the script"); + std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o755)).expect("runnable"); + } + #[cfg(windows)] + { + let _ = records_argv; + let body = format!( + "@echo off\r\nset \"{marker}={}\"\r\n\"{}\" --exact {test} --nocapture -q --test-threads=1\r\n", + value.display(), + exe.display() + ); + std::fs::write(path, body).expect("write the script"); + } } /// A bridged backend in `ws` whose "codex" is the stand-in in `bin` — which -/// records its argv to `bin/argv` — and whose tool server is the stand-in -/// server, returned beside it; with a deny rule on `Write(secret.txt)` in -/// incode's toolbox. +/// records its argv to `bin/argv` on Unix, and on Windows is handed the +/// registration the backend builds, in `bin/registration` — and whose tool +/// server is the stand-in server, returned beside it; with a deny rule on +/// `Write(secret.txt)` in incode's toolbox. /// -/// Called by [`a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one`]. -#[cfg(unix)] -fn stand_in_bridged_backend(ws: &Path, bin: &Path) -> (CodexBackend, PathBuf) { - let codex = bin.join("codex.sh"); - let server = bin.join("server.sh"); +/// Called by [`a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one`] +/// and by `bridged_tests.rs`. +pub(super) fn stand_in_bridged_backend(ws: &Path, bin: &Path) -> (CodexBackend, PathBuf) { + let script = if cfg!(windows) { "cmd" } else { "sh" }; + let codex = bin.join(format!("codex.{script}")); + let server = bin.join(format!("server.{script}")); child_script( &codex, - &format!("printf '%s\\0' \"$@\" > '{}'", bin.join("argv").display()), + Some(&bin.join("argv")), FAKE_CODEX, bin, "agent::codex::tests::fake_codex_child", ); child_script( &server, - "", + None, FAKE_TOOL_SERVER, Path::new("1"), "agent::codex::tests::tool_server_child", ); + #[cfg(windows)] + std::fs::write( + bin.join("registration"), + serde_json::to_vec(&super::argv::bridge_registration(&server)).expect("json"), + ) + .expect("write the registration"); let mut backend = CodexBackend::new(ws.to_path_buf()) .with_binary(codex.to_string_lossy()) @@ -631,7 +675,6 @@ fn stand_in_bridged_backend(ws: &Path, bin: &Path) -> (CodexBackend, PathBuf) { /// Every tool use a turn showed, as `(id, name)`. /// /// Called by [`a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one`]. -#[cfg(unix)] fn uses_in(seen: &[AgentEvent]) -> Vec<(&str, &str)> { seen.iter() .filter_map(|e| match e { @@ -644,7 +687,6 @@ fn uses_in(seen: &[AgentEvent]) -> Vec<(&str, &str)> { /// Every tool result a turn showed, as `(id, is_error, output)`. /// /// Called by [`a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one`]. -#[cfg(unix)] fn results_in(seen: &[AgentEvent]) -> Vec<(&str, bool, &str)> { seen.iter() .filter_map(|e| match e { @@ -666,7 +708,10 @@ fn results_in(seen: &[AgentEvent]) -> Vec<(&str, bool, &str)> { /// second is refused by a deny rule in incode's toolbox and nothing is /// written. Each call is shown once, under incode's id; codex's record of /// it is not shown again. -#[cfg(unix)] +/// +/// The test of `agent/codex/bridged.rs` `run_bridged`, on every platform. +/// The argv codex was started with is checked on Unix only, where the +/// stand-in can record it (see [`child_script`]). #[test] fn a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one() { let ws = tempfile::tempdir().expect("ws"); @@ -682,17 +727,22 @@ fn a_bridged_turn_runs_its_calls_in_incodes_toolbox_and_a_deny_rule_refuses_one( assert_eq!(backend.session_id(), Some("fake-thread")); // The argv codex was really started with: the bridged one. - let argv = - String::from_utf8(std::fs::read(bin.path().join("argv")).expect("argv")).expect("utf-8"); - assert!(argv.contains("--ignore-user-config"), "{argv:?}"); - assert!(argv.contains("features.shell_tool=false"), "{argv:?}"); - assert!( - argv.contains(&format!( - "mcp_servers.incode.command=\"{}\"", - server.display() - )), - "{argv:?}" - ); + #[cfg(unix)] + { + let argv = String::from_utf8(std::fs::read(bin.path().join("argv")).expect("argv")) + .expect("utf-8"); + assert!(argv.contains("--ignore-user-config"), "{argv:?}"); + assert!(argv.contains("features.shell_tool=false"), "{argv:?}"); + assert!( + argv.contains(&format!( + "mcp_servers.incode.command=\"{}\"", + server.display() + )), + "{argv:?}" + ); + } + #[cfg(windows)] + let _ = &server; // The model saw incode's tools. assert!( diff --git a/src/tool_bridge.rs b/src/tool_bridge.rs index 27f31a6..6a844cd 100644 --- a/src/tool_bridge.rs +++ b/src/tool_bridge.rs @@ -96,6 +96,8 @@ pub struct Bridge { addr: SocketAddr, token: String, closed: Arc, + /// Set when the tool server asked, with the turn's token, for the tools. + listed: Arc, listener: Option>, } @@ -120,9 +122,11 @@ impl Bridge { uuid::Uuid::new_v4().simple() ); let closed = Arc::new(AtomicBool::new(false)); + let listed = Arc::new(AtomicBool::new(false)); let gate = Gate { token: token.clone(), closed: Arc::clone(&closed), + listed: Arc::clone(&listed), cancel, in_flight: Arc::new(AtomicUsize::new(0)), }; @@ -132,10 +136,22 @@ impl Bridge { addr, token, closed, + listed, listener: Some(handle), }) } + /// Whether the tool server reached this bridge and asked for the tools. + /// + /// An agent CLI lists its MCP servers' tools when it starts, before the + /// model runs. A turn that ends without that request ran with none of + /// incode's tools, whatever its reply says: the server never started or + /// never got through (see [`require_tools`]). + #[must_use] + pub fn tools_listed(&self) -> bool { + self.listed.load(Ordering::SeqCst) + } + /// What the agent CLI's environment must carry for its tool server to /// reach this bridge. #[must_use] @@ -161,6 +177,7 @@ impl Drop for Bridge { struct Gate { token: String, closed: Arc, + listed: Arc, cancel: Option, in_flight: Arc, } @@ -195,6 +212,7 @@ fn accept_until_closed( let conn = Gate { token: gate.token.clone(), closed: Arc::clone(&gate.closed), + listed: Arc::clone(&gate.listed), cancel: gate.cancel.clone(), in_flight: Arc::clone(&gate.in_flight), }; @@ -253,6 +271,7 @@ fn answer_connection( let answer = match request.get("op").and_then(Value::as_str) { Some("list") => { + gate.listed.store(true, Ordering::SeqCst); let (reply, back) = std::sync::mpsc::channel(); forward(to, wrap(Request::List { reply })); match back.recv() { @@ -344,7 +363,13 @@ pub fn answer( ) { match request { Request::List { reply } => { - let _ = reply.send(Ok(mcp_definitions(tools))); + let definitions = mcp_definitions(tools); + crate::trace!( + "bridge", + "the tool server asked for the tools: {} offered", + definitions.len() + ); + let _ = reply.send(Ok(definitions)); } Request::Call { reply, .. } if stopped => { let _ = reply.send(Err( @@ -459,23 +484,69 @@ impl Turn { } } +/// Refuse a finished turn whose tool server never asked for the tools. +/// +/// Such a turn ran with none of incode's tools: the model had nothing to act +/// with, and a model with no tools may write tool calls out as plain text +/// that nothing ran. Its reply is not a result, so the turn fails and says +/// why rather than reporting success. Measured: under a Windows +/// write-restricted token (sbx, native), Node cannot give a child piped +/// stdio, so Claude Code starts no MCP server at all (#68). +/// +/// Called by the Claude Code route (`agent/claude_code/turn.rs` `run_once`) +/// once its turn produced a result. Not by the codex route +/// (`agent/codex/bridged.rs`): whether codex always lists its servers' tools +/// before the model runs is not yet measured (#19). +/// +/// # Errors +/// +/// `listed` is false. +pub fn require_tools(listed: bool, agent: &str) -> Result<(), crate::agent::AgentError> { + if listed { + return Ok(()); + } + Err(no_tools(agent)) +} + +/// Why a turn without incode's tools was stopped: the one wording for the +/// end-of-turn check ([`require_tools`]) and for a CLI that says at once +/// that the server failed (the Claude route's init line, +/// `agent/claude_code/wire.rs` `incode_server_failed`). +#[must_use] +pub fn no_tools(agent: &str) -> crate::agent::AgentError { + crate::trace!("bridge", "{agent} never started incode's tool server"); + crate::agent::AgentError::new(format!( + "{agent} never started incode's tool server, so the model had none of incode's tools \ + and its reply was not used. If it runs inside a sandbox, the sandbox may stop it \ + starting child processes; INCODE_TRACE= shows the bridge's side" + )) +} + +/// What [`drive`] hands each line the CLI prints: the events it means, or +/// the error that ends the turn. +pub type LineHandler<'a> = + dyn FnMut(String, &mut Toolbox) -> Result, crate::agent::AgentError> + 'a; + /// The turn itself: every tool call the model makes run by `tools`, and /// every line the CLI prints handed to `on_line`, whose events are shown — /// until the CLI finishes or the turn is stopped. /// /// `on_line` is given the toolbox too, so a CLI that names its conversation -/// can tell the engine which one the calls belong to. +/// can tell the engine which one the calls belong to. It may end the turn +/// with an error: a line that says the turn cannot go on (the CLI reporting +/// that incode's tool server failed to start). /// /// # Errors /// -/// [`CANCELLED`] when the turn was stopped; the CLI's output could not be read. +/// [`CANCELLED`] when the turn was stopped; the CLI's output could not be +/// read; `on_line` refused a line. pub fn drive( from: &std::sync::mpsc::Receiver, cancel: Option<&CancelToken>, tools: &mut Toolbox, calls: &mut u64, on_event: &mut dyn FnMut(&AgentEvent), - on_line: &mut dyn FnMut(String, &mut Toolbox) -> Vec, + on_line: &mut LineHandler<'_>, ) -> Result<(), crate::agent::AgentError> { let stopped = || cancel.is_some_and(CancelToken::is_cancelled); loop { @@ -502,7 +573,7 @@ pub fn drive( Ok(Incoming::Ended) | Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => break, Err(std::sync::mpsc::RecvTimeoutError::Timeout) => continue, }; - for event in on_line(line, tools) { + for event in on_line(line, tools)? { on_event(&event); } } @@ -564,7 +635,14 @@ impl ToolServer { /// One request to the parent, and its answer. fn ask(&self, mut body: Value) -> Result { body["token"] = json!(self.token); - let reach = |e: std::io::Error| format!("incode could not be reached: {e}"); + let addr = self.addr; + let reach = |e: std::io::Error| { + crate::trace!( + "bridge", + "tool server could not reach incode at {addr}: {e}" + ); + format!("incode could not be reached: {e}") + }; let mut stream = TcpStream::connect_timeout(&self.addr, CONNECT_BOUND).map_err(reach)?; stream.set_read_timeout(Some(REPLY_BOUND)).map_err(reach)?; writeln!(stream, "{body}").map_err(reach)?; @@ -630,8 +708,16 @@ impl crate::mcp_stdio::Handler for ToolServer { /// /// The parent could not be named from the environment, or the pipe failed. pub fn run_stdio() -> std::io::Result<()> { - let mut server = ToolServer::from_env() - .map_err(|why| std::io::Error::new(std::io::ErrorKind::InvalidInput, why))?; + let mut server = ToolServer::from_env().map_err(|why| { + crate::trace!("bridge", "tool server could not start: {why}"); + std::io::Error::new(std::io::ErrorKind::InvalidInput, why) + })?; + crate::trace!( + "bridge", + "tool server started (pid {}), answering to {}", + std::process::id(), + server.addr + ); let stdin = std::io::stdin(); let mut input = stdin.lock(); let mut output = std::io::stdout();