Skip to content

Reason Network: your team's coding agents, talking to each other. The engine behind `reason network`.

Where the Reason CLI manages it, the daemon carries cloud agents' calls

Lars Talian5973a4b57d21
diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs
index 6f87d37..1f38a79 100644
--- a/src/daemon/mod.rs
+++ b/src/daemon/mod.rs
@@ -66,6 +66,9 @@ pub async fn run(service: bool) -> Result<()> {
if daemon.cfg.reason {
tokio::spawn(daemon.clone().reason_loop());
}
+ if crate::managed() {
+ tokio::spawn(daemon.clone().relay_loop());
+ }
tokio::spawn(crate::migrate::run(log));
if update::allowed(daemon.cfg.auto_update) {
tokio::spawn(daemon.clone().update_loop());
diff --git a/src/daemon/reason.rs b/src/daemon/reason.rs
index 0ab1f6f..53c4120 100644
--- a/src/daemon/reason.rs
+++ b/src/daemon/reason.rs
@@ -1,6 +1,9 @@
//! Reason sessions. Reason's agent runs in its cloud, so there is no hook or MCP server here: when `reason` is on
//! (`reason network setup`), the daemon lists your live Reason sessions (`reason session list --json`) every few seconds,
//! and delivers to them with `reason session send`. They reply through the Reason Network CLI, which runs here.
+//!
+//! Where the Reason CLI manages this engine, the daemon also keeps the CLI's `reason network __relay` running: it
+//! carries the workspace's cloud agents' calls (Reason's hosted tools) onto the network, for as long as the daemon runs.
use super::{Daemon, log};
use crate::{agent, git::GitInfo, proto::*, util};
@@ -9,6 +12,9 @@ use serde_json::Value;
use std::{collections::HashSet, sync::Arc, time::Duration};
const EVERY: Duration = Duration::from_secs(15);
+/// The relay is started again this long after it stops, doubling up to `RELAY_MAX_WAIT` while it keeps stopping.
+const RELAY_WAIT: Duration = Duration::from_secs(30);
+const RELAY_MAX_WAIT: Duration = Duration::from_secs(600);
impl Daemon {
pub(super) async fn reason_loop(self: Arc<Self>) {
@@ -26,6 +32,35 @@ impl Daemon {
}
}
+ /// Keeps `reason network __relay` running. It reads nothing from its stdin, and ends when that closes: with this
+ /// daemon, however it ends. Its output goes where the daemon's does (the log).
+ pub(super) async fn relay_loop(self: Arc<Self>) {
+ let Some(bin) = agent::find_bin("reason") else { return };
+ let mut wait = RELAY_WAIT;
+ loop {
+ let started = std::time::Instant::now();
+ let child = tokio::process::Command::new(&bin)
+ .args(["network", "__relay"])
+ .stdin(std::process::Stdio::piped())
+ .stdout(std::process::Stdio::null())
+ .kill_on_drop(true)
+ .spawn();
+ match child {
+ // Held here, not left to `wait`, which would close it first.
+ Ok(mut child) => match (child.stdin.take(), child.wait().await).1 {
+ Ok(status) => log(&format!("reason network __relay stopped ({status})")),
+ Err(e) => log(&format!("reason network __relay: {e}")),
+ },
+ Err(e) => log(&format!("starting reason network __relay: {e}")),
+ }
+ if started.elapsed() > RELAY_MAX_WAIT {
+ wait = RELAY_WAIT;
+ }
+ tokio::time::sleep(wait).await;
+ wait = (wait * 2).min(RELAY_MAX_WAIT);
+ }
+ }
+
async fn reason_sync(self: &Arc<Self>) -> Result<()> {
let Some(bin) = agent::find_bin("reason") else { return Ok(()) };
let out = tokio::process::Command::new(bin)