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
send: say when Claude Code will hold a message posted straight into a session
WhitePepperLambSoupcb8cdb043f67
diff --git a/DESIGN.md b/DESIGN.mdindex 44fcf35..c917375 100644--- a/DESIGN.md+++ b/DESIGN.md@@ -226,7 +226,10 @@ not approve. Nothing changes how an agent works when it isn't messaging.- Peer text is framed as peer input. Agents are told not to treat it as user instructions.- 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.+ `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…`).- 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 16d7912..599d804 100644--- a/src/claude.rs+++ b/src/claude.rs@@ -68,6 +68,44 @@ 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)";++/// 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).+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)+}++fn held_by(mode: &str, setting: Option<&str>) -> bool {+ mode == "bypassPermissions" && setting != Some("accept")+}++/// `crossSessionInbound` in the user's Claude Code settings, if set.+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()?;+ v["crossSessionInbound"].as_str().map(str::to_string)+}++/// The permission mode a session's transcript last records (Claude Code writes it with the session's entries).+fn permission_mode(transcript: &std::path::Path) -> Option<String> {+ use std::io::{Read, Seek, SeekFrom};+ let mut file = std::fs::File::open(transcript).ok()?;+ let len = file.metadata().ok()?.len();+ file.seek(SeekFrom::Start(len.saturating_sub(2 << 20))).ok()?;+ let mut tail = Vec::new();+ file.read_to_end(&mut tail).ok()?;+ // The first line may be cut by the seek; it just doesn't parse.+ tail.split(|&b| b == b'\n').rev().find_map(|line| {+ let v: serde_json::Value = serde_json::from_slice(line).ok()?;+ v.get("permissionMode")?.as_str().map(str::to_string)+ })+}+/// The messaging socket of a live session, by its id or process.pub fn socket_for(session_id: Option<&str>, pid: Option<u32>) -> Option<ClaudeSocket> {let c = scan().into_iter().find(|c| {@@ -92,3 +130,36 @@ pub fn peer_token(pid: u32) -> Option<String> {v["peerToken"].as_str().map(str::to_string)})}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn held_only_when_the_session_skips_prompts_and_the_setting_doesnt_accept() {+ assert!(held_by("bypassPermissions", None));+ assert!(held_by("bypassPermissions", Some("hold")));+ assert!(!held_by("bypassPermissions", Some("accept")));+ assert!(!held_by("auto", None));+ assert!(!held_by("default", Some("hold")));+ }++ #[test]+ fn the_last_permission_mode_in_the_transcript_counts() {+ let dir = std::env::temp_dir().join(format!("reason-network-mode-{}", std::process::id()));+ std::fs::create_dir_all(&dir).unwrap();+ let path = dir.join("session.jsonl");+ let lines = [+ r#"{"type":"user","permissionMode":"bypassPermissions","message":"a"}"#,+ r#"{"type":"assistant","message":"no mode here"}"#,+ r#"{"type":"permission-mode","permissionMode":"auto"}"#,+ r#"{"type":"assistant","message":"still no mode"}"#,+ ];+ std::fs::write(&path, lines.join("\n") + "\n").unwrap();+ assert_eq!(permission_mode(&path).as_deref(), Some("auto"));+ std::fs::write(&path, "not json\n{\"type\":\"user\"}\n").unwrap();+ assert_eq!(permission_mode(&path), None, "no mode recorded");+ assert_eq!(permission_mode(&dir.join("missing.jsonl")), None);+ let _ = std::fs::remove_dir_all(&dir);+ }+}diff --git a/src/daemon/mail.rs b/src/daemon/mail.rsindex 998d389..4b7bd99 100644--- a/src/daemon/mail.rs+++ b/src/daemon/mail.rs@@ -268,6 +268,7 @@ impl Daemon {let ids: Vec<String> = msgs.iter().map(|m| m.id.clone()).collect();let cli = matches!(channel, Channel::ClaudeDirect(_));let text = render::for_agent(&msgs, Some(&l.reg.agent), cli);+ let claude_session = l.reg.agent_session_id.clone();let hook_note = if matches!(l.reg.agent.as_str(), "opencode" | "pi") {"queued: delivered within seconds once the session is idle"} else if l.reg.agent == "cli" {@@ -308,10 +309,9 @@ impl Daemon {g.in_flight.insert(id.clone(), Instant::now());}self.post_direct(key.to_string(), sock, ids, text);- (- "pushed".into(),- "delivered into the session: it reads it between tool calls, or wakes up if idle".into(),- )+ let note = claude::held_note(claude_session.as_deref())+ .unwrap_or("delivered into the session: it reads it between tool calls, or wakes up if idle");+ ("pushed".into(), note.into())}Channel::Hook => ("queued".into(), hook_note.into()),Channel::Pull => ("queued".into(), "queued: this agent only sees it when it checks its inbox".into()),diff --git a/src/doing.rs b/src/doing.rsindex af4a42d..c111e72 100644--- a/src/doing.rs+++ b/src/doing.rs@@ -156,7 +156,7 @@ fn scan(path: &Path) -> Seen {}/// `~/.claude/projects/<project>/<session>.jsonl`.-fn claude_transcript(session: &str) -> Option<PathBuf> {+pub(crate) fn claude_transcript(session: &str) -> Option<PathBuf> {static FOUND: OnceLock<Mutex<HashMap<String, PathBuf>>> = OnceLock::new();let mut found = FOUND.get_or_init(Default::default).lock().unwrap();if let Some(p) = found.get(session).filter(|p| p.exists()) {