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
files: an agent in its home folder sends none; one teammate can't fill the inbox
Lars Talian0f899f060715
diff --git a/DESIGN.md b/DESIGN.mdindex 8760f4a..14d8be9 100644--- a/DESIGN.md+++ b/DESIGN.md@@ -163,7 +163,7 @@ hours after the daemon starts, while live sessions register again).UTF-8 with no control characters but tab and line ends), inside the same envelope, so they are as authenticated as itstext. The sender's client reads them from paths on its computer: a person's `send --file` from anywhere, an agent's`send_message` only from its own folder (its repository, or where it works; links resolved), none of that folder's-hidden files or folders, and nothing under `~/.config`, `~/.ssh`, `~/.aws` or the state home, so a prompt-injected agent+hidden files or folders, and nothing under `~/.config`, `~/.ssh`, `~/.aws`, `~/Library`, `%APPDATA%`, `%LOCALAPPDATA%` or the state home (a session working in the home folder or above it sends none), so a prompt-injected agentcan't mail out what its harness wouldn't let it read. Both daemons hold them to the limits and reduce each name to aplain file name (no separators or `..`, no leading dot or dash, nothing invisible, no Windows device names such as`CON`, `COM¹` or `CONIN$`, at most 100 characters and 200 bytes, distinct within the message, defanged like message@@ -174,7 +174,7 @@ folds into one already there gets `-2`), and lists their paths after the messagethem. A held message's files are saved only once it is approved (one that can't be saved stays held); a refused one'snever are. They go with their message: when it is forgotten, a day after it was read, or a week after they came while itis unread; a message held a week with files goes. A machine keeps at most 100 MB of teammates' files (saved and held)-and takes at most 20 MB an hour from one person; past either, the sender's machine is told to retry later and keeps the+keeps at most 25 MB from one person, and takes at most 20 MB an hour from one person; past either, the sender's machine is told to retry later and keeps themessage in its outbox. A machine's presence lists `features: ["files"]` when its engine takes them; an older enginewould drop the field without a word, so for a machine that doesn't say so, the files are written out in the text asfenced blocks when that fits the 64 KB body limit, and the message is refused for that recipient otherwise.diff --git a/src/daemon/mail.rs b/src/daemon/mail.rsindex 41ded4b..225277d 100644--- a/src/daemon/mail.rs+++ b/src/daemon/mail.rs@@ -504,8 +504,8 @@ impl Daemon {// Files take room here: past the quota, the sender's machine keeps the message and tries again later.let bytes = files::size(&msg.files);if bytes > 0 {- let kept = g.store.files_kept()?;- if let Err(why) = g.quota.admit(&sender, bytes, kept, Instant::now()) {+ let (kept, from_sender) = (g.store.files_kept()?, g.store.files_kept_from(&sender)?);+ if let Err(why) = g.quota.admit(&sender, bytes, kept, from_sender, Instant::now()) {return Ok(("retry".into(),format!("queued: {} can't take these files now ({why}); retrying later", self.machine),diff --git a/src/files.rs b/src/files.rsindex 35983c1..e4751eb 100644--- a/src/files.rs+++ b/src/files.rs@@ -28,6 +28,8 @@ pub const KEEP_UNREAD_MS: i64 = 7 * 86_400_000;/// hour. Past either, a teammate's machine is asked to retry later.pub const QUOTA_TOTAL: usize = 100 * 1024 * 1024;pub const QUOTA_SENDER_HOUR: usize = 20 * 1024 * 1024;+/// And keeps at most this much from one person, so no teammate can take all of it.+pub const QUOTA_SENDER_KEPT: usize = 25 * 1024 * 1024;const TEXT_ONLY: &str = "only text files can be sent (markdown, source code, JSON, logs, config)";@@ -42,13 +44,18 @@ pub struct Confine {impl Confine {pub fn new(root: &Path, home: &Path, state: &Path) -> Result<Confine> {let root = std::fs::canonicalize(root).map_err(|_| anyhow!("can't find this session's folder"))?;- if root.parent().is_none() {- bail!("this session has no folder of its own to send files from");+ let real_home = std::fs::canonicalize(home).unwrap_or_else(|_| home.to_path_buf());+ // The home folder, or a folder holding it (`/`, `/Users`), is everything the person has, not a project.+ if root.parent().is_none() || real_home.starts_with(&root) {+ bail!(+ "this session works in {}, not a project folder: start the session in a project folder to send files",+ root.display()+ );}- let forbidden = [home.join(".config"), home.join(".ssh"), home.join(".aws"), state.to_path_buf()]- .iter()- .map(|p| std::fs::canonicalize(p).unwrap_or_else(|_| p.clone()))- .collect();+ let mut refused =+ vec![home.join(".config"), home.join(".ssh"), home.join(".aws"), home.join("Library"), state.to_path_buf()];+ refused.extend(["APPDATA", "LOCALAPPDATA"].iter().filter_map(std::env::var_os).map(PathBuf::from));+ let forbidden = refused.iter().map(|p| std::fs::canonicalize(p).unwrap_or_else(|_| p.clone())).collect();Ok(Confine { root, forbidden })}@@ -358,15 +365,28 @@ pub struct Quota {}impl Quota {- /// Takes `bytes` of files from `sender`, with `kept` bytes of teammates' files on this machine already, or says why- /// not (a teammate's machine retries later).- pub fn admit(&mut self, sender: &str, bytes: usize, kept: usize, now: Instant) -> Result<(), String> {+ /// Takes `bytes` of files from `sender`, with `kept` bytes of teammates' files on this machine already (`from_sender`+ /// of them that person's), or says why not (a teammate's machine retries later).+ pub fn admit(+ &mut self,+ sender: &str,+ bytes: usize,+ kept: usize,+ from_sender: usize,+ now: Instant,+ ) -> Result<(), String> {if kept + bytes > QUOTA_TOTAL {return Err(format!("it keeps {} MB of teammates' files at most and has that many now; it takes these once some are read",QUOTA_TOTAL >> 20));}+ if from_sender + bytes > QUOTA_SENDER_KEPT {+ return Err(format!(+ "it keeps {} MB of files from {sender} at most and has that many now; it takes these once some are read",+ QUOTA_SENDER_KEPT >> 20+ ));+ }let q = self.recent.entry(sender.to_string()).or_default();while q.front().is_some_and(|(at, _)| now.duration_since(*at) > Duration::from_secs(3600)) {q.pop_front();@@ -556,13 +576,23 @@ mod tests {let e = read_within(&[p.to_string()], &repo, &confine).unwrap_err().to_string();assert!(e.contains(want), "{p}: {e}");}- // Working in the home folder itself: its hidden folders and the state stay out of reach.- let at_home = Confine::new(&home, &home, &home.join("src")).unwrap();- let e = read_within(&[".ssh/id_ed25519".into()], &home, &at_home).unwrap_err().to_string();- assert!(e.contains("hidden"), "{e}");- let e = read_within(&["src/api/docs/plan.md".into()], &home, &at_home).unwrap_err().to_string();+ // A session working in the home folder, or a folder above it, has no project folder: nothing is sent.+ for root in [home.as_path(), d.as_path(), Path::new("/")] {+ let e = Confine::new(root, &home, &state).err().map(|e| e.to_string()).unwrap_or_default();+ assert!(e.contains("start the session in a project folder to send files"), "{}: {e}", root.display());+ }+ // A project folder holding the state, or ~/Library: those stay out of reach.+ std::fs::create_dir_all(home.join("Library/Keychains")).unwrap();+ std::fs::write(home.join("Library/Keychains/login.txt"), "keychain").unwrap();+ let in_state = Confine::new(&home.join("src"), &home, &home.join("src/api/docs")).unwrap();+ let e = read_within(&["api/docs/plan.md".into()], &home.join("src"), &in_state).unwrap_err().to_string();assert!(e.contains("Reason Network's own state"), "{e}");- assert!(Confine::new(Path::new("/"), &home, &state).is_err());+ std::os::unix::fs::symlink(home.join("Library"), repo.join("lib")).unwrap();+ let e = read_within(&["lib/Keychains/login.txt".into()], &repo, &confine).unwrap_err().to_string();+ assert!(e.contains("outside this session's folder"), "{e}");+ let library = Confine::new(&home.join("Library"), &home, &state).unwrap();+ let e = read_within(&["Keychains/login.txt".into()], &home.join("Library"), &library).unwrap_err().to_string();+ assert!(e.contains("credentials"), "{e}");// A person's own `send --file` reads anywhere.assert_eq!(read(&[home.join(".ssh/id_ed25519").display().to_string()], &repo).unwrap()[0].content, "key");std::fs::remove_dir_all(&d).unwrap();@@ -619,14 +649,18 @@ mod tests {let mut q = Quota::default();let t = Instant::now();let mb = 1 << 20;- assert!(q.admit("bob", 15 * mb, 0, t).is_ok());- let e = q.admit("bob", 6 * mb, 15 * mb, t).unwrap_err();+ assert!(q.admit("bob", 15 * mb, 0, 0, t).is_ok());+ let e = q.admit("bob", 6 * mb, 15 * mb, 15 * mb, t).unwrap_err();assert!(e.contains("20 MB of files an hour from bob"), "{e}");// Another teammate has their own hour; an hour later bob has his again.- assert!(q.admit("alice", 6 * mb, 15 * mb, t).is_ok());- assert!(q.admit("bob", 6 * mb, 21 * mb, t + Duration::from_secs(3601)).is_ok());- let e = q.admit("carol", mb, QUOTA_TOTAL, t).unwrap_err();+ assert!(q.admit("alice", 6 * mb, 15 * mb, 0, t).is_ok());+ assert!(q.admit("bob", 6 * mb, 21 * mb, 15 * mb, t + Duration::from_secs(3601)).is_ok());+ let e = q.admit("carol", mb, QUOTA_TOTAL, 0, t).unwrap_err();assert!(e.contains("100 MB of teammates' files at most"), "{e}");+ // Nor does one teammate keep more than 25 MB of it, however slowly sent.+ let e = q.admit("bob", 5 * mb, 21 * mb, 21 * mb, t + Duration::from_secs(7300)).unwrap_err();+ assert!(e.contains("25 MB of files from bob at most"), "{e}");+ assert!(q.admit("dave", 5 * mb, 21 * mb, 0, t + Duration::from_secs(7300)).is_ok());}#[test]diff --git a/src/store.rs b/src/store.rsindex 7630619..93abb08 100644--- a/src/store.rs+++ b/src/store.rs@@ -212,6 +212,13 @@ impl Store {Ok(self.db.query_row(sql, [], |r| r.get::<_, i64>(0))?.max(0) as usize)}+ /// The same, of files from `person`'s sessions.+ pub fn files_kept_from(&self, person: &str) -> Result<usize> {+ let sql = "SELECT COALESCE(SUM(json_extract(files, '$.bytes')), 0) FROM messages+ WHERE files IS NOT NULL AND substr(sender, 1, length(?1) + 1) = ?1 || '/'";+ Ok(self.db.query_row(sql, [person], |r| r.get::<_, i64>(0))?.max(0) as usize)+ }+/// Lets files go: a day after their message was read, a week after they came while it is unread (the message stays,/// without them), and a message held a week with files goes altogether. Then deletes every folder in the inbox/// folder that no kept message names.@@ -691,6 +698,8 @@ mod tests {// What they take counts against the quota: saved or held.let one = files::size(&with_files("x", "y").files);assert_eq!(store.files_kept().unwrap(), 5 * one);+ // Counted per sending person too (`sven/claude` sent them all; `sv` is someone else).+ assert_eq!((store.files_kept_from("sven").unwrap(), store.files_kept_from("sv").unwrap()), (5 * one, 0));// The unread mail of a session forgotten after a day goes, and its files with it.store.forget_sessions(i64::MAX, &HashSet::from(["api"]), "lars").unwrap();assert!(!folder(&gone).exists());