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
Daemon calls time out; a request the daemon can't read is answered under its id (#109)
Lars Taliand9e86b020a47
diff --git a/src/client.rs b/src/client.rsindex 76cada6..026daa9 100644--- a/src/client.rs+++ b/src/client.rs@@ -26,6 +26,28 @@ struct Conn {tx: mpsc::UnboundedSender<String>,pending: Pending,alive: Arc<AtomicBool>,+ /// The last failure the daemon answered with no request's id: an older daemon can't read a request it doesn't+ /// know, and says so this way. Told to a call that then gets no answer.+ unmatched: Arc<Mutex<Option<String>>>,+}++/// How long a call waits for the daemon's answer, at most: an older daemon never answers a request it doesn't know+/// (it fails it under no id), and the caller mustn't wait for good. Calls that wait on purpose get that long, and more.+fn patience(req: &Request) -> Duration {+ match req {+ // Up to 10 minutes each, for a message or the person's answer.+ Request::Inbox { .. } | Request::Ask { .. } => Duration::from_secs(660),+ // Over the network.+ Request::CreateTeam { .. }+ | Request::Join { .. }+ | Request::Invite { .. }+ | Request::Link { .. }+ | Request::Remove { .. }+ | Request::Leave { .. }+ | Request::SetRelay { .. }+ | Request::Proof { .. } => Duration::from_secs(600),+ _ => Duration::from_secs(120),+ }}pub struct Link {@@ -70,12 +92,24 @@ impl Link {}async fn raw(&self, conn: &Conn, req: &Request) -> Result<Value> {+ self.raw_within(conn, req, patience(req)).await+ }++ async fn raw_within(&self, conn: &Conn, req: &Request, patience: Duration) -> Result<Value> {let id = self.next_id.fetch_add(1, Ordering::Relaxed);let (tx, rx) = oneshot::channel();conn.pending.lock().unwrap().insert(id, tx);let line = serde_json::to_string(&Frame { id, body: req })?;conn.tx.send(line).map_err(|_| anyhow!("network daemon connection closed"))?;- let reply = rx.await.map_err(|_| anyhow!("network daemon connection closed"))?;+ let reply = match tokio::time::timeout(patience, rx).await {+ Ok(reply) => reply.map_err(|_| anyhow!("network daemon connection closed"))?,+ Err(_) => {+ conn.pending.lock().unwrap().remove(&id);+ let said =+ conn.unmatched.lock().unwrap().take().map(|e| format!(" (it said: {e})")).unwrap_or_default();+ bail!("no answer from the network daemon in {}s{said}; is it an older version?", patience.as_secs())+ }+ };if reply.ok {Ok(reply.result.unwrap_or(Value::Null))} else {@@ -108,7 +142,12 @@ impl Link {fn spawn_io(self: &Arc<Self>, stream: ipc::Stream) -> Conn {let (rd, mut wr) = tokio::io::split(stream);let (tx, mut rx) = mpsc::unbounded_channel::<String>();- let conn = Conn { tx, pending: Pending::default(), alive: Arc::new(AtomicBool::new(true)) };+ let conn = Conn {+ tx,+ pending: Pending::default(),+ alive: Arc::new(AtomicBool::new(true)),+ unmatched: Default::default(),+ };tokio::spawn(async move {while let Some(mut line) = rx.recv().await {line.push('\n');@@ -117,21 +156,13 @@ impl Link {}}});- let (pending, alive, events, link) =- (conn.pending.clone(), conn.alive.clone(), self.events.clone(), Arc::downgrade(self));+ let (reader, alive, events, link) =+ (conn.clone(), conn.alive.clone(), self.events.clone(), Arc::downgrade(self));+ let pending = conn.pending.clone();tokio::spawn(async move {let mut lines = BufReader::new(rd).lines();while let Ok(Some(line)) = lines.next_line().await {- let Ok(v) = serde_json::from_str::<Value>(&line) else { continue };- if v.get("event").is_some() {- if let (Some(events), Ok(ev)) = (&events, serde_json::from_value::<Event>(v)) {- let _ = events.send(ev);- }- } else if let Ok(reply) = serde_json::from_value::<Reply>(v)- && let Some(tx) = pending.lock().unwrap().remove(&reply.id)- {- let _ = tx.send(reply);- }+ reader.dispatch(&line, events.as_ref());}alive.store(false, Ordering::Relaxed);pending.lock().unwrap().clear();@@ -143,6 +174,27 @@ impl Link {}}+impl Conn {+ /// A line from the daemon: an event, or the reply to a call, which it hands over.+ fn dispatch(&self, line: &str, events: Option<&mpsc::UnboundedSender<Event>>) {+ let Ok(v) = serde_json::from_str::<Value>(line) else { return };+ if v.get("event").is_some() {+ if let (Some(events), Ok(ev)) = (events, serde_json::from_value::<Event>(v)) {+ let _ = events.send(ev);+ }+ return;+ }+ let Ok(reply) = serde_json::from_value::<Reply>(v) else { return };+ match self.pending.lock().unwrap().remove(&reply.id) {+ Some(tx) => {+ let _ = tx.send(reply);+ }+ None if !reply.ok => *self.unmatched.lock().unwrap() = reply.error,+ None => {}+ }+ }+}+async fn reconnect(link: Weak<Link>) {// Backs off: every agent session runs one of these, and they must not storm a daemon that can't start.let mut wait = Duration::from_secs(1);@@ -258,6 +310,44 @@ fn remember(regs: &mut HashMap<String, SessionReg>, reg: SessionReg) {#[cfg(test)]mod tests {+ use super::*;++ #[tokio::test]+ async fn a_request_an_older_daemon_cant_read_fails_instead_of_waiting_for_good() {+ let link = Link::existing();+ let (tx, mut sent) = mpsc::unbounded_channel();+ let conn = Conn {+ tx,+ pending: Pending::default(),+ alive: Arc::new(AtomicBool::new(true)),+ unmatched: Default::default(),+ };+ let req = Request::PushFailed { key: "claude-code:a".into(), ids: vec!["01M".into()] };+ let call = {+ let (link, conn) = (link.clone(), conn.clone());+ tokio::spawn(async move { link.raw_within(&conn, &req, Duration::from_millis(300)).await })+ };+ // What 0.12.6 answers: a failure under no request's id.+ let line = sent.recv().await.unwrap();+ assert!(line.contains("push_failed"), "{line}");+ conn.dispatch(r#"{"id":0,"ok":false,"error":"bad request: unknown variant `push_failed`"}"#, None);+ let e = call.await.unwrap().unwrap_err().to_string();+ assert!(+ e.starts_with("no answer from the network daemon") && e.contains("unknown variant `push_failed`"),+ "{e}"+ );+ assert!(conn.pending.lock().unwrap().is_empty());+ // A reply to its own id still comes through.+ let call = {+ let (link, conn) = (link.clone(), conn.clone());+ tokio::spawn(async move { link.raw_within(&conn, &Request::Team, Duration::from_secs(5)).await })+ };+ let line = sent.recv().await.unwrap();+ let id = serde_json::from_str::<Value>(&line).unwrap()["id"].as_u64().unwrap();+ conn.dispatch(&serde_json::to_string(&Reply::ok(id, serde_json::json!(7))).unwrap(), None);+ assert_eq!(call.await.unwrap().unwrap(), serde_json::json!(7));+ }+#[test]fn a_reconnect_registers_only_the_conversation_a_server_is_in_now() {let reg = |key: &str, agent: &str| crate::proto::SessionReg {diff --git a/src/daemon/mod.rs b/src/daemon/mod.rsindex dfca248..f5a76e0 100644--- a/src/daemon/mod.rs+++ b/src/daemon/mod.rs@@ -94,6 +94,13 @@ async fn tray() {}}+/// The answer to a request this daemon can't read (from a newer client, say): under its id when that much reads, so+/// the caller gets it rather than waiting.+fn bad_request(line: &str, e: serde_json::Error) -> Reply {+ let id = serde_json::from_str::<Value>(line).ok().and_then(|v| v.get("id")?.as_u64()).unwrap_or(0);+ Reply::err(id, anyhow!("bad request: {e}"))+}+fn log(msg: &str) {eprintln!("[{}] {msg}", util::now_ms());}@@ -274,9 +281,7 @@ impl Daemon {let f = match serde_json::from_str::<Frame<Request>>(&line) {Ok(f) => f,Err(e) => {- let _ = tx.send(- serde_json::to_string(&Reply::err(0, anyhow!("bad request: {e}"))).expect("reply serializes"),- );+ let _ = tx.send(serde_json::to_string(&bad_request(&line, e)).expect("reply serializes"));continue;}};@@ -403,3 +408,17 @@ fn test_daemon(cfg: Config) -> Daemon {let store = Store::open(std::path::Path::new(":memory:"), inbox).unwrap();Daemon::with(cfg, store, SecretKey::generate()).unwrap()}++#[cfg(test)]+mod tests {+ #[test]+ fn a_request_this_daemon_cant_read_is_answered_under_its_id() {+ let line = r#"{"id":42,"op":"something_newer","key":"x"}"#;+ let e = serde_json::from_str::<super::Frame<super::Request>>(line).unwrap_err();+ let r = super::bad_request(line, e);+ assert_eq!((r.id, r.ok), (42, false));+ assert!(r.error.unwrap().starts_with("bad request:"));+ let e = serde_json::from_str::<super::Frame<super::Request>>("not json").unwrap_err();+ assert_eq!(super::bad_request("not json", e).id, 0);+ }+}diff --git a/src/mcp.rs b/src/mcp.rsindex 77b3b22..1f86fb4 100644--- a/src/mcp.rs+++ b/src/mcp.rs@@ -373,9 +373,13 @@ impl ReasonNetworkMcp {async fn on_event(&self, ev: Event) {let Event::Deliver { key, ids, text } = ev;- // No inbox to put it in: the daemon takes it back, and doesn't push here again until there is one.+ // No inbox to put it in: the daemon takes it back, and doesn't push here again until there is one. Off this+ // task, which every push goes through: an older daemon doesn't know the request (its answer is a timeout).let Some(sock) = self.claude() else {- let _ = self.link.call::<Value>(Request::PushFailed { key, ids }).await;+ let link = self.link.clone();+ tokio::spawn(async move {+ let _ = link.call::<Value>(Request::PushFailed { key, ids }).await;+ });return;};match tokio::task::spawn_blocking(move || agent::post_to_claude(&sock, &text)).await {diff --git a/tests/e2e/mixed_versions.py b/tests/e2e/mixed_versions.pyindex 06b1e78..66b503b 100644--- a/tests/e2e/mixed_versions.py+++ b/tests/e2e/mixed_versions.py@@ -137,6 +137,63 @@ try:out = until("alex", ["team"], lambda o: re.search(r"olga\s+oldbox\s+(direct|relay)", o))check("after both restart, people stay verified", "alex (verified)" in out and "nina (verified)" in outand re.search(r"olga\s+oldbox\s+(direct|relay)", out) is not None, out)++ # This build's MCP server for a Claude Code session on the older machine: with no inbox to push into yet, it tells+ # the daemon so, which an older daemon can't read. That mustn't hold up the pushes after it.+ import json, socket, threading+ claude_dir = os.path.join(D, "claude")+ os.makedirs(os.path.join(claude_dir, "sessions"), exist_ok=True)+ with open(os.path.join(claude_dir, ".claude.json"), "w") as f:+ json.dump({"mcpServers": {"reason-network": {"type": "stdio", "command": NEW, "args": ["mcp"]}}}, f)+ cli("olga", "stop")+ cli("olga", "team") # the older daemon again, now seeing Claude Code set up+ proj = os.path.join(D, "mv-proj")+ os.makedirs(proj)+ claude_proc = subprocess.Popen(["sleep", "300"])+ entry = os.path.join(claude_dir, "sessions", f"{claude_proc.pid}.json")+ inbox_path = os.path.join(D, "mv.sock")+ def list_session(inbox):+ with open(entry, "w") as f:+ json.dump({"pid": claude_proc.pid, "sessionId": "mv-1", "cwd": proj, "kind": "interactive",+ **({"messagingSocketPath": inbox_path} if inbox else {})}, f)+ list_session(False)+ until("olga", ["who"], lambda o: "mv-proj-claude" in o)+ srv = socket.socket(socket.AF_UNIX)+ srv.bind(inbox_path)+ srv.listen(4)+ frames = []+ def serve_inbox():+ while True:+ c, _ = srv.accept()+ frames.append(c.makefile().read())+ threading.Thread(target=serve_inbox, daemon=True).start()+ mcp = subprocess.Popen([NEW, "mcp"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, cwd=proj, text=True,+ env=dict(env("olga"), CLAUDE_PID=str(claude_proc.pid), CLAUDE_CODE_SESSION_ID="mv-1"))+ def rpc(msg):+ mcp.stdin.write(json.dumps(msg) + "\n")+ mcp.stdin.flush()+ def reply(want):+ while True:+ m = json.loads(mcp.stdout.readline())+ if m.get("id") == want:+ return m+ rpc({"jsonrpc": "2.0", "id": 1, "method": "initialize", "params": {"protocolVersion": "2025-06-18", "capabilities": {}, "clientInfo": {"name": "claude-code", "version": "1"}}})+ reply(1)+ rpc({"jsonrpc": "2.0", "method": "notifications/initialized"})+ rpc({"jsonrpc": "2.0", "id": 2, "method": "tools/call", "params": {"name": "list_team", "arguments": {}}})+ reply(2)+ out = cli("olga", "send", "mv-proj-claude", "first, with no inbox yet")+ check("the older daemon pushes to this build's Claude Code server, which has no inbox yet", "→ olga/mv-proj-claude" in out, out)+ time.sleep(2)+ list_session(True)+ cli("olga", "send", "mv-proj-claude", "second, once there is one")+ deadline = time.time() + 10+ while time.time() < deadline and "second, once there is one" not in "".join(frames):+ time.sleep(0.5)+ check("…and the next push still goes into the inbox once there is one", "second, once there is one" in "".join(frames),+ "".join(frames)[-500:])+ mcp.kill()+ claude_proc.kill()finally:for w in WHO:subprocess.run([WHO[w][0], "stop"], env=env(w), capture_output=True, timeout=30)