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
Follow-ups from the review of #21 and #22
WhitePepperLambSouped0a3a87ea80
diff --git a/DESIGN.md b/DESIGN.mdindex 2d851ad..53b2bf6 100644--- a/DESIGN.md+++ b/DESIGN.md@@ -228,9 +228,9 @@ not approve. Nothing changes how an agent works when it isn't messaging.- Per-machine inbound policy: `accept` (default within the team), `hold` (you approve with`reason-network held` / `reason-network approve`), `refuse`; overridable per person. Claude Code adds its own`crossSessionInbound` setting on top: a session that skips permission prompts holds a message from a stranger for- its user unless the setting is `accept`. A session opened before setup has no `reason-network mcp` of its own, so the- daemon posts into it directly, as a stranger; `send` says so when the transcript shows that mode (state `pushed`, note- `held by Claude Code…`).+ its user unless the setting is `accept`. A session without a `reason-network mcp` of its own (open before setup, or+ its server turned off or crashed) gets the daemon's post directly, as a stranger; `send` says so when the transcript+ shows that mode (state `pushed`, note `held by Claude Code…`).- Loop guard: rate limit per sender, and a cap on consecutive agent↔agent messages in a thread withoutany human input.- Group guard: an agent's `*`, `person/*` or `repo:` message is refused before it goes anywhere, so it picks onediff --git a/src/claude.rs b/src/claude.rsindex 599d804..8ae58cb 100644--- a/src/claude.rs+++ b/src/claude.rs@@ -68,13 +68,15 @@ pub fn wired() -> bool {})}-const HELD: &str = "held by Claude Code for its user's approval: the session skips permission prompts and was open before \- Reason Network was set up (restart it and it takes messages straight away)";+const HELD: &str = "held by Claude Code for its user's approval: the session skips permission prompts and has no \+ `reason-network mcp` of its own (restart it, or turn the server on in /mcp, and it takes messages \+ straight away)";/// What to tell the sender of a message posted straight into a Claude Code session, when Claude Code will hold it. A-/// session open before setup has no `reason-network mcp` of its own to post with its token, so the post reaches it as-/// a stranger's, and a session that skips permission prompts holds those for its user unless `crossSessionInbound` is-/// `accept`. `None`: not known to be held (the mode is not in its transcript).+/// session without a `reason-network mcp` of its own (open before setup, or its server turned off or crashed) can't+/// post with its own token, so the post reaches it as a stranger's, and a session that skips permission prompts holds+/// those for its user unless `crossSessionInbound` is `accept`. `None`: not known to be held (the mode is not in its+/// transcript).pub fn held_note(session: Option<&str>) -> Option<&'static str> {let mode = permission_mode(&crate::doing::claude_transcript(session?)?)?;held_by(&mode, inbound_setting().as_deref()).then_some(HELD)@@ -84,7 +86,8 @@ fn held_by(mode: &str, setting: Option<&str>) -> bool {mode == "bypassPermissions" && setting != Some("accept")}-/// `crossSessionInbound` in the user's Claude Code settings, if set.+/// `crossSessionInbound` in the user's own `settings.json`, if set. Managed settings and a session's `--settings`+/// take precedence over it and are not read, so a session can differ.fn inbound_setting() -> Option<String> {let text = std::fs::read_to_string(paths::claude_dir().join("settings.json")).ok()?;let v: serde_json::Value = serde_json::from_str(&text).ok()?;diff --git a/src/setup.rs b/src/setup.rsindex 0a5a9dc..c5ada56 100644--- a/src/setup.rs+++ b/src/setup.rs@@ -683,7 +683,11 @@ const DSH_BEGIN: &str = "# === reason-network MCP BEGIN ===";const DSH_END: &str = "# === reason-network MCP END ===";fn dsh_home() -> PathBuf {- std::env::var_os("DSH_HOME").filter(|h| !h.is_empty()).map(PathBuf::from).unwrap_or_else(|| home().join(".dsh"))+ // dsh takes a blank `DSH_HOME` for unset, spaces included.+ std::env::var_os("DSH_HOME")+ .filter(|h| !h.to_string_lossy().trim().is_empty())+ .map(PathBuf::from)+ .unwrap_or_else(|| home().join(".dsh"))}/// dsh applies this file on top of every profile, those made later too.@@ -708,7 +712,7 @@ fn dsh_block(exe: &Path) -> String {}fn dsh_wired(text: &str, exe: &Path) -> bool {- contains(text, &format!("serverName: {AGENT_NAME}")) && contains(text, &dsh_command(exe))+ dsh_has_server(text) && contains(&dsh_entries(text).join("\n"), &dsh_command(exe))}/// The lines of a patch file that are neither blank nor comments.@@ -716,6 +720,16 @@ fn dsh_entries(text: &str) -> Vec<&str> {text.lines().filter(|l| !l.trim().is_empty() && !l.trim_start().starts_with('#')).collect()}+/// Whether an entry of the file (not a comment) names an MCP server `reason-network`, quoted or not.+fn dsh_has_server(text: &str) -> bool {+ dsh_entries(text).iter().any(|l| {+ let line = l.trim_start().trim_start_matches("- ").trim_start();+ line.strip_prefix("serverName:").is_some_and(|name| {+ name.split(" #").next().unwrap_or_default().trim().trim_matches(['\'', '"']) == AGENT_NAME+ })+ })+}+/// A patch file's text with `block` in it (in place of the one it has), or without ours when `remove`. `None`: nothing/// to change. dsh wants a top-level list (`- …` from the first column), so a file that is anything else is left/// alone, as is one with a server of that name the user wrote; both come back as an error saying so.@@ -743,7 +757,7 @@ fn dsh_patch_text(text: &str, block: &str, remove: bool) -> Result<Option<StringSome((b, e)) => format!("{}{block}{}", &text[..b], &text[e..]),None if remove => return Ok(None),None => {- if text.contains(&format!("serverName: {AGENT_NAME}")) {+ if dsh_has_server(text) {return Err(anyhow!("it already has a {AGENT_NAME} MCP server of your own; left as is"));}let entries = dsh_entries(text);@@ -751,7 +765,8 @@ fn dsh_patch_text(text: &str, block: &str, remove: bool) -> Result<Option<StringNone => format!("{text}{}{block}", if text.is_empty() || text.ends_with('\n') { "" } else { eol }),Some(first) if first.trim_end() == "[]" && entries.len() == 1 => {let rest: Vec<&str> = text.lines().filter(|l| l.trim_end() != "[]").collect();- format!("{}{eol}{block}", rest.join(eol))+ // A file that was only the list becomes our block, with no blank line above it.+ if rest.is_empty() { block } else { format!("{}{eol}{block}", rest.join(eol)) }}Some(first) if first.starts_with("- ") => {let gap = if text.ends_with('\n') { eol.to_string() } else { format!("{eol}{eol}") };@@ -790,7 +805,8 @@ fn dsh(exe: &Path, remove: bool, dry: bool) -> Result<Option<Report>> {}}Ok(None) => r.lines.push(changed(&path, false)),- Err(e) => r.notes.push(format!("DeepSeek Harness: {}: {e:#}", crate::util::tilde(&path.display().to_string()))),+ // Skipped, not set up: `apply` says so, and the wizard doesn't count it among the agents it set up.+ Err(e) => return Err(anyhow!("{}: {e:#}", crate::util::tilde(&path.display().to_string()))),}Ok(Some(r))}@@ -1544,6 +1560,22 @@ mod tests {let why = dsh_patch_text(own, &block, false).unwrap_err().to_string();assert!(why.contains("of your own"), "{why}");assert_eq!(dsh_patch_text(own, &block, true).unwrap(), None, "`--remove` leaves it");+ // However YAML quotes the name; a comment about it is not a server.+ for quoted in ["\"reason-network\"", "'reason-network'", "reason-network # mine"] {+ let own = format!("- insert:\n - config:\n serverName: {quoted}\n");+ assert!(dsh_patch_text(&own, &block, false).is_err(), "{quoted}");+ }+ let commented = "# serverName: reason-network\n- id: agent-loop\n";+ assert!(dsh_patch_text(commented, &block, false).unwrap().unwrap().contains(&block), "commented out");+ assert!(!dsh_wired("# serverName: reason-network\n", Path::new("/x/reason-network")));+ }++ #[test]+ fn dsh_patch_that_is_only_the_empty_list_round_trips() {+ let block = dsh_block(Path::new("/x/reason-network"));+ let with = dsh_patch_text("[]\n", &block, false).unwrap().unwrap();+ assert_eq!(with, block, "no blank line above our block");+ assert_eq!(dsh_patch_text(&with, &block, true).unwrap().as_deref(), Some("[]\n"));}#[test]