Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.
Rust agent-to-agentai-agentsclaude-codecodexcoding-agentscursorgrokirohmcpmcp-servermulti-agentpeer-to-peerreason
Windows npm shims, the Hermes version probe and bridge errors: review follow-ups (#148)
Lars Talian5e995a580520
diff --git a/src/agent.rs b/src/agent.rsindex 4657e76..5c69085 100644--- a/src/agent.rs+++ b/src/agent.rs@@ -144,28 +144,44 @@ pub fn find_bin(name: &str) -> Option<PathBuf> {.find(|p| executable(p))}-/// How to run `name`: the program, and the arguments that go before the caller's. On Windows the `<name>.cmd` npm-/// (or pnpm) installs runs through cmd.exe, which can't be handed an argument with a line break (Rust refuses one rather-/// than let cmd.exe cut it short), and a message has them: its Node script is run with node instead, when the shim-/// names one.-pub fn command_for(name: &str) -> Option<(PathBuf, Vec<PathBuf>)> {- let bin = find_bin(name)?;+/// How to run `name`, for the caller to add its arguments to. On Windows the `<name>.cmd` npm (or pnpm) installs runs+/// through cmd.exe, which can't be handed an argument with a line break (Rust refuses one rather than let cmd.exe cut it+/// short), and a message has them: its Node script is run with node instead, when the shim names one.+pub fn command_for(name: &str) -> Option<std::process::Command> {+ find_bin(name).map(|bin| command_at(&bin))+}++/// [`command_for`] for the program at `bin`.+fn command_at(bin: &Path) -> std::process::Command {#[cfg(windows)]if bin.extension().is_some_and(|e| e.eq_ignore_ascii_case("cmd"))&& let Some(dir) = bin.parent()- && let Some(run) = std::fs::read_to_string(&bin).ok().and_then(|shim| node_shim(&shim, dir))+ && let Some((node, script)) = std::fs::read_to_string(bin).ok().and_then(|shim| node_shim(&shim, dir)){- return Some(run);+ let mut command = std::process::Command::new(node);+ command.args(script);+ return command;}- Some((bin, vec![]))+ std::process::Command::new(bin)}/// Node and the script an npm or pnpm `.cmd` shim in `dir` runs (`"%dp0%\\node_modules\\…\\codex.js" %*`): the node/// beside the shim if there is one, as the shim itself prefers, else the one on `PATH`. Only when the shim runs that-/// script with node and nothing else in between (no flags, not bun): any other shim is left to run as it is.+/// script with node and nothing else in between (no flags, not bun), and sets nothing in the environment but its own+/// bookkeeping (`dp0`, `_prog`, `PATHEXT`, `NODE_PATH`): any other shim is left to run as it is.#[cfg(any(windows, test))]fn node_shim(shim: &str, dir: &Path) -> Option<(PathBuf, Vec<PathBuf>)> {let lower = shim.to_ascii_lowercase();+ // Running node directly would skip whatever else the shim sets for it.+ let sets_more = lower.split(['\n', '&', '(']).any(|part| {+ let part = part.trim().trim_start_matches('@');+ let Some(set) = part.strip_prefix("set ").or_else(|| part.strip_prefix("set\t")) else { return false };+ let name = set.trim_start().trim_start_matches('"').split('=').next().unwrap_or_default().trim();+ !["dp0", "_prog", "pathext", "node_path"].contains(&name)+ });+ if sets_more {+ return None;+ }let script = shim.match_indices('"').find_map(|(at, _)| {let part = shim[at + 1..].split('"').next()?;let rest = part.strip_prefix("%dp0%\\").or_else(|| part.strip_prefix("%~dp0\\"))?;@@ -576,15 +592,34 @@ mod tests {);let flags = npm.replace("\"%_prog%\" \"%dp0%", "\"%_prog%\" --no-warnings \"%dp0%");assert_eq!(node_shim(&flags, d.as_path()), None);+ // pnpm's NODE_PATH is fine to skip; a shim that sets anything else is left to set it.+ let node_path =+ format!("@SETLOCAL\r\n@IF NOT DEFINED NODE_PATH (\r\n @SET \"NODE_PATH=C:\\x\"\r\n)\r\n{plain}");+ assert!(node_shim(&node_path, &sub).is_some());+ for set in ["@SET \"NODE_OPTIONS=--max-old-space-size=8192\"", "SET CODEX_HOME=C:\\c", "set /a n=1"] {+ assert_eq!(node_shim(&format!("@SETLOCAL\r\n{set}\r\n{plain}"), &sub), None, "{set}");+ }+ assert_eq!(node_shim(&format!("@SETLOCAL\r\nIF 1==1 (SET X=1)\r\n{plain}"), &sub), None);let _ = std::fs::remove_dir_all(&d);}+ #[test]+ fn a_program_that_is_not_a_cmd_shim_runs_as_it_is() {+ let bin = std::env::temp_dir().join("codex");+ let command = command_at(&bin);+ assert_eq!(command.get_program(), bin.as_os_str());+ assert_eq!(command.get_args().count(), 0);+ }+/// The point of it, on Windows itself: a message with line breaks and cmd.exe's special characters reaches the- /// script npm installed, as it was sent.+ /// script npm installed, as it was sent, through the command delivery runs.#[cfg(windows)]#[test]fn a_message_with_line_breaks_reaches_an_npm_installed_script() {- let Some(node) = find_bin("node") else { return };+ let Some(node) = find_bin("node") else {+ assert!(std::env::var_os("CI").is_none(), "CI must run this with node installed");+ return;+ };let d = std::env::temp_dir().join(format!("reason-network-shim-run-{}", ulid::Ulid::generate()));let bin = d.join("node_modules").join("x").join("bin");std::fs::create_dir_all(&bin).unwrap();@@ -596,10 +631,11 @@ mod tests {.unwrap();let shim ="@ECHO off\r\nSETLOCAL\r\nSET \"_prog=node\"\r\n\"%_prog%\" \"%dp0%\\node_modules\\x\\bin\\x.js\" %*\r\n";- let (prog, pre) = node_shim(shim, &d).unwrap();- assert_eq!(prog, node);+ std::fs::write(d.join("x.cmd"), shim).unwrap();+ let mut command = command_at(&d.join("x.cmd"));+ assert_eq!(command.get_program(), node.as_os_str());let text = "<reason-message from=\"a\">\r\nline & two %PATH% ^ | < > \"quoted\"\n</reason-message>";- let status = std::process::Command::new(prog).args(&pre).args(["queue", "--message", text]).status().unwrap();+ let status = command.args(["queue", "--message", text]).status().unwrap();assert!(status.success());let got: Vec<String> = serde_json::from_str(&std::fs::read_to_string(&out).unwrap()).unwrap();assert_eq!(got, ["queue", "--message", text]);diff --git a/src/bridge.rs b/src/bridge.rsindex c838567..c6404b9 100644--- a/src/bridge.rs+++ b/src/bridge.rs@@ -106,7 +106,8 @@ fn failure(e: anyhow::Error) -> anyhow::Error {anyhow::anyhow!(render::defang(&plugin_tools(&format!("{e:#}"))))}-/// The MCP tool names in an engine error, as the plugin's tools (`list_team` is `reason_network_list_agents`).+/// The MCP tool names in an engine error, as the plugin's tools (`list_team` is `reason_network_list_agents`). Not+/// where one is part of a name or path the error quotes (`'send_message-claude'`, `notes/check_inbox.md`).fn plugin_tools(text: &str) -> String {const TOOLS: [(&str, &str); 4] = [("list_team", "reason_network_list_agents"),@@ -114,16 +115,27 @@ fn plugin_tools(text: &str) -> String {("check_inbox", "reason_network_check_inbox"),("ask_user", "reason_network_ask_user"),];- let between = |c: char| !(c.is_alphanumeric() || c == '_');- text.split_inclusive(between)- .map(|piece| {- let word = piece.trim_end_matches(between);- match TOOLS.iter().find(|(tool, _)| *tool == word) {- Some((_, plugin)) => format!("{plugin}{}", &piece[word.len()..]),- None => piece.to_string(),- }- })- .collect()+ let part = |c: char| c.is_alphanumeric() || c == '_';+ let joins = |c: Option<char>| c.is_some_and(|c| matches!(c, '/' | '\\' | '\'' | '"' | '-' | '.'));+ let mut out = String::with_capacity(text.len());+ let (mut done, mut at) = (0, 0);+ while let Some(start) = text[at..].find(part).map(|n| at + n) {+ let end = text[start..].find(|c| !part(c)).map_or(text.len(), |n| start + n);+ let mut after = text[end..].chars();+ let next = after.next();+ // A full stop ends a sentence; one followed by more of a word starts an extension.+ let joined = joins(text[..start].chars().next_back())+ || (next != Some('.') && joins(next))+ || (next == Some('.') && after.next().is_some_and(part));+ if !joined && let Some((_, plugin)) = TOOLS.iter().find(|(tool, _)| *tool == &text[start..end]) {+ out.push_str(&text[done..start]);+ out.push_str(plugin);+ done = end;+ }+ at = end;+ }+ out.push_str(&text[done..]);+ out}/// The agents with a native adapter on this transport; the rest use the MCP server.@@ -243,6 +255,10 @@ mod tests {plugin_tools(other),"my_list_team list_teams reason_network_send_message reason_network_check_inbox: reason_network_ask_user.");+ // Names and paths the error quotes keep theirs.+ let quoted =+ "'send_message' \"ask_user\" notes/check_inbox.md send_message-claude list_team.txt x-ask_user é.list_team";+ assert_eq!(plugin_tools(quoted), quoted);}#[tokio::test]diff --git a/src/daemon/mail.rs b/src/daemon/mail.rsindex d4b35b7..dd0b1f7 100644--- a/src/daemon/mail.rs+++ b/src/daemon/mail.rs@@ -509,9 +509,8 @@ impl Daemon {let this = self.clone();tokio::spawn(async move {let run = async {- let (program, first) = agent::command_for(bin).ok_or_else(|| format!("{bin} not found"))?;- let out = tokio::process::Command::new(program)- .args(&first)+ let program = agent::command_for(bin).ok_or_else(|| format!("{bin} not found"))?;+ let out = tokio::process::Command::from(program).args(&args).arg(&text).stdin(std::process::Stdio::null())diff --git a/src/setup/hermes.rs b/src/setup/hermes.rsindex 1e1594c..0cbd06b 100644--- a/src/setup/hermes.rs+++ b/src/setup/hermes.rs@@ -50,27 +50,47 @@ fn source() -> Option<PathBuf> {/// The `Install directory:` a Hermes command reports, waiting at most a few seconds for it.fn install_dir(mut command: Command) -> Option<PathBuf> {- let mut child = command- .stdin(std::process::Stdio::null())- .stdout(std::process::Stdio::piped())- .stderr(std::process::Stdio::null())- .spawn()- .ok()?;- let mut stdout = child.stdout.take()?;+ command.stdin(std::process::Stdio::null()).stdout(std::process::Stdio::piped()).stderr(std::process::Stdio::null());+ // In its own process group, so giving up on it ends what it started too (a launcher's Python, a shell's `sleep`).+ #[cfg(unix)]+ std::os::unix::process::CommandExt::process_group(&mut command, 0);+ let mut child = command.spawn().ok()?;+ let stdout = child.stdout.take()?;let (tx, rx) = std::sync::mpsc::channel();std::thread::spawn(move || {- let mut out = Vec::new();- let _ = std::io::Read::read_to_end(&mut stdout, &mut out);- let _ = tx.send(out);+ let found = std::io::BufRead::lines(std::io::BufReader::new(stdout))+ .map_while(Result::ok)+ .find_map(|l| l.trim().strip_prefix("Install directory:").map(|p| PathBuf::from(p.trim())));+ let _ = tx.send(found);});// A Hermes that hangs on `--version` (or leaves something holding its output) mustn't hang setup, init and join.- let out = rx.recv_timeout(std::time::Duration::from_secs(5));- let _ = child.kill();+ let found = rx.recv_timeout(std::time::Duration::from_secs(5)).ok().flatten();+ kill_tree(&mut child);let _ = child.wait();- String::from_utf8_lossy(&out.ok()?)- .lines()- .find_map(|l| l.trim().strip_prefix("Install directory:"))- .map(|p| PathBuf::from(p.trim()))+ found+}++/// Ends `child` and everything it started: its process group on Unix, its process tree on Windows.+fn kill_tree(child: &mut std::process::Child) {+ #[cfg(unix)]+ // SAFETY: signals the process group `install_dir` created for this child, which isn't reaped yet.+ unsafe {+ libc::killpg(child.id() as libc::pid_t, libc::SIGKILL);+ }+ #[cfg(windows)]+ {+ use std::os::windows::process::CommandExt;+ let taskkill = std::env::var_os("SystemRoot")+ .map_or_else(|| PathBuf::from("taskkill.exe"), |r| PathBuf::from(r).join(r"System32\taskkill.exe"));+ let _ = Command::new(taskkill)+ .args(["/T", "/F", "/PID", &child.id().to_string()])+ .stdin(std::process::Stdio::null())+ .stdout(std::process::Stdio::null())+ .stderr(std::process::Stdio::null())+ .creation_flags(0x0800_0000)+ .status();+ }+ let _ = child.kill();}/// The plugin context methods the plugin needs (NousResearch/hermes-agent#143310), wherever in `hermes_cli` they are.@@ -209,10 +229,54 @@ mod tests {c};assert_eq!(install_dir(sh("echo 'Install directory: /opt/hermes'")), Some(PathBuf::from("/opt/hermes")));+ // The line is used as soon as it's there, and what's still running is ended, including its children.+ let pid = temp();let started = std::time::Instant::now();- // The shell's `sleep` keeps the output open after the shell itself is killed.- assert_eq!(install_dir(sh("echo 'Install directory: /opt/hermes'; sleep 30")), None);+ let script = format!("sleep 30 & echo $! > {}; echo 'Install directory: /opt/hermes'; wait", pid.display());+ assert_eq!(install_dir(sh(&script)), Some(PathBuf::from("/opt/hermes")));+ assert!(started.elapsed() < std::time::Duration::from_secs(4), "{:?}", started.elapsed());+ let gone = |pid: &PathBuf| {+ let sleep: libc::pid_t = std::fs::read_to_string(pid).unwrap().trim().parse().unwrap();+ // SAFETY: signal 0 only checks that the process exists.+ (0..50).any(|_| {+ let alive = unsafe { libc::kill(sleep, 0) } == 0;+ if alive {+ std::thread::sleep(std::time::Duration::from_millis(100));+ }+ !alive+ })+ };+ assert!(gone(&pid), "the sleep outlived the probe");+ // Nothing said: given up on after a few seconds, and ended the same way.+ let started = std::time::Instant::now();+ let script = format!("sleep 30 & echo $! > {}; wait", pid.display());+ assert_eq!(install_dir(sh(&script)), None);assert!(started.elapsed() < std::time::Duration::from_secs(15), "{:?}", started.elapsed());+ assert!(gone(&pid), "the sleep outlived the probe");+ let _ = std::fs::remove_file(&pid);+ }++ /// On Windows a pip or uv `hermes.exe` launcher runs Python as its child: ending the launcher alone would leave it.+ #[cfg(windows)]+ #[test]+ fn giving_up_ends_what_the_command_started() {+ let mut child = Command::new("cmd")+ .args(["/C", "ping -n 60 127.0.0.1"])+ .stdin(std::process::Stdio::null())+ .stdout(std::process::Stdio::piped())+ .spawn()+ .unwrap();+ let mut stdout = child.stdout.take().unwrap();+ let (tx, rx) = std::sync::mpsc::channel();+ std::thread::spawn(move || {+ let _ = std::io::Read::read_to_end(&mut stdout, &mut Vec::new());+ let _ = tx.send(());+ });+ std::thread::sleep(std::time::Duration::from_millis(500));+ kill_tree(&mut child);+ let _ = child.wait();+ // `ping` holds the output open until it's gone too.+ assert!(rx.recv_timeout(std::time::Duration::from_secs(10)).is_ok(), "ping outlived cmd");}#[test]