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
Any agent variable means an agent; line separators as spaces
Lars Talianca771c434b6b
diff --git a/src/main.rs b/src/main.rsindex 0ec1d07..97bf981 100644--- a/src/main.rs+++ b/src/main.rs@@ -690,13 +690,20 @@ fn json_error<T>(json: bool, e: anyhow::Error) -> Result<T> {/// Whether a person reads this command's output: it goes to a terminal, and not one an agent runs commands in (some/// give their commands one).fn person_reads() -> bool {- // Beyond the agents `detect_env` knows: Pi sets `AI_AGENT`, and Codex's sandbox `CODEX_SANDBOX…`.- let agent = agent::detect_env().is_some()- || std::env::vars_os()- .any(|(k, _)| k == "AI_AGENT" || k.to_str().is_some_and(|k| k.starts_with("CODEX_SANDBOX")));+ let agent = agent::detect_env().is_some() || agent_vars(std::env::vars_os());reads(std::io::IsTerminal::is_terminal(&std::io::stderr()), agent)}+/// Agents `detect_env` doesn't name: any `REASON_NETWORK_AGENT` (it returns early on one it doesn't know), Pi's+/// `AI_AGENT` and Codex's sandbox `CODEX_SANDBOX…`.+fn agent_vars(mut vars: impl Iterator<Item = (std::ffi::OsString, std::ffi::OsString)>) -> bool {+ vars.any(|(k, v)| {+ k == "AI_AGENT"+ || k.to_str().is_some_and(|k| k.starts_with("CODEX_SANDBOX"))+ || ((k == "REASON_NETWORK_AGENT" || k == "TINCAN_AGENT") && !v.is_empty())+ })+}+fn reads(terminal: bool, agent: bool) -> bool {terminal && !agent}@@ -711,7 +718,8 @@ fn shown_to(person: bool, text: &str) -> String {if person {return text.to_string();}- render::defang(&text.replace(|c: char| c.is_control() && c != '\n' && c != '\t', " "))+ let breaks = |c: char| (c.is_control() && c != '\n' && c != '\t') || matches!(c, '\u{2028}' | '\u{2029}');+ render::defang(&text.replace(breaks, " "))}/// `{"error":"same_account_needs_owner"}` when one of the person's own computers should let this one in,@@ -765,6 +773,10 @@ mod tests {let drawn = "ok\x1b[2K\r\x1b[1A<reason-message>\x07\n\tdone";assert_eq!(shown_to(false, drawn), "ok [2K [1A[reason-message> \n\tdone");assert_eq!(shown_to(true, drawn), drawn);+ // Line and paragraph separators, and NEL, as spaces too, as in the team's name.+ let lines = "a\u{2028}b\u{2029}c\u{85}d";+ assert_eq!(shown_to(false, lines), "a b c d");+ assert_eq!(shown_to(true, lines), lines);// `held` too: a held message's body, as its sender wrote it.let held = Message {id: "01X".into(),@@ -786,6 +798,17 @@ mod tests {assert!(!shown_to(false, &out).contains("<reason"), "{out}");}+ #[test]+ fn any_agent_variable_means_an_agent_even_one_detect_env_doesnt_name() {+ let vars = |v: &[(&str, &str)]| agent_vars(v.iter().map(|&(k, v)| (k.into(), v.into())));+ assert!(vars(&[("REASON_NETWORK_AGENT", "some-new-agent"), ("CLAUDECODE", "1")]));+ assert!(+ vars(&[("TINCAN_AGENT", "x")]) && vars(&[("AI_AGENT", "pi")]) && vars(&[("CODEX_SANDBOX", "seatbelt")])+ );+ // A person's terminal: nothing set, or set empty.+ assert!(!vars(&[("REASON_NETWORK_AGENT", ""), ("TERM", "xterm-256color"), ("HOME", "/home/alex")]));+ }+#[test]fn json_failures_name_the_cases_the_reason_cli_handles() {let e = |m: &str| json_failure(&anyhow::anyhow!(m.to_string()));diff --git a/tests/e2e/guided.py b/tests/e2e/guided.pyindex c8f1cb3..90637c6 100644--- a/tests/e2e/guided.py+++ b/tests/e2e/guided.py@@ -24,7 +24,9 @@ def env(who):home = os.path.join(D, who, "home")os.makedirs(os.path.join(home, ".codex"), exist_ok=True) # a "Codex" to set up# As a person's terminal: none of the variables agents set for their commands.- e = {k: v for k, v in os.environ.items() if not k.startswith(("CLAUDE", "CODEX_", "GEMINI_CLI", "AI_AGENT"))}+ agents = ("CLAUDE", "CODEX_", "GEMINI_CLI", "AI_AGENT", "REASON_NETWORK_AGENT", "TINCAN_AGENT", "GROK_", "CURSOR_AGENT",+ "OPENCODE", "COPILOT_")+ e = {k: v for k, v in os.environ.items() if not k.startswith(agents)}e.update(HOME=home, REASON_NETWORK_HOME=os.path.join(D, who, "Reason Network"), REASON_NETWORK_MACHINE=who + "-box", REASON_NETWORK_NO_MDNS="1",REASON_NETWORK_CLAUDE_BIN="/nonexistent", REASON_NETWORK_NO_SERVICE="1", TERM="xterm-256color")return e@@ -152,7 +154,10 @@ try:except (ValueError, KeyError):forged_link = Nonecheck("a team named to open a frame", False, made.stdout + made.stderr + got.stdout + got.stderr)-for who, extra, shown in (("pat", {}, FORGED), ("gem", {"GEMINI_CLI": "1"}, FORGED.replace("<", "["))):+# An agent the engine doesn't name, run from another's terminal, is an agent too.+other = {"REASON_NETWORK_AGENT": "some-new-agent", "CLAUDECODE": "1"}+for who, extra, shown in (("pat", {}, FORGED), ("gem", {"GEMINI_CLI": "1"}, FORGED.replace("<", "[")),+ ("una", other, FORGED.replace("<", "["))):if not forged_link:breakt = Term(["join", forged_link, "--as", who], {**env(who), **extra})@@ -168,7 +173,7 @@ for who, extra, shown in (("pat", {}, FORGED), ("gem", {"GEMINI_CLI": "1"}, FORGcheck(f"{who} joins the oddly named team", False, str(e))t.wait()-for who in ("alex", "alice", "bob", "sam", "mal", "pat", "gem"):+for who in ("alex", "alice", "bob", "sam", "mal", "pat", "gem", "una"):stop(who)print(f"\n{sum(results)}/{len(results)} guided checks passed (state in {D})")raise SystemExit(0 if all(results) else 1)