Skip to content

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

send_message carries small text files

Lars Talian0cf33cac9c93
diff --git a/DESIGN.md b/DESIGN.md
index 50223a9..84cf94d 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -159,6 +159,18 @@ machine's mailbox under the session name, for a day: a session gone longer is fo
read (not the person's own `listen` mailboxes, such as `terminal`, nor mail held for them; and nothing in the
hours after the daemon starts, while live sessions register again).
+**Files.** A message can carry small text files (`files: [{name, content}]`, at most 10, 256 KB each, 512 KB in all;
+valid UTF-8 without NUL bytes), inside the same envelope, so they are as authenticated as its text. The sender's
+client reads them from paths on its computer; both daemons hold them to the limits and reduce each name to a plain
+file name (no separators or `..`, no leading dot or dash, nothing invisible or reserved, at most 100 characters,
+distinct within the message, defanged like message text). The receiver saves them in
+`<state home>/inbox/<message id>/` (folder 0700, files 0600, never executable, never opened) and the message lists
+their paths. A held message's files are saved only once it is approved; a refused one's never are. They go with
+their message: when it is forgotten, or a day after it was read. A machine's presence lists `features: ["files"]`
+when its engine takes them; an older engine would drop the field without a word, so for a machine that doesn't say
+so, the files are written out in the text as fenced blocks when that fits the 64 KB body limit, and the message is
+refused for that recipient otherwise.
+
Later (M4): an **anchor** is just a member daemon on an always-on box (VPS, Pi) with `role=anchor` that
also holds mail for offline machines and keeps searchable team history. Same binary, no special server.
@@ -214,7 +226,8 @@ Three tools, none of which an agent has to call unless it wants to message someo
- `list_team(filter?, all?)`: the directory, grouped by person then machine. One line per session: name,
agent, status and for how long, repo@branch, `doing`, tier. Marks `← you`.
-- `send_message(to, message, reply_to?, notify?)`: returns per-recipient state (`delivered` / `queued` /
+- `send_message(to, message, reply_to?, notify?, files?)`: `files` are paths of small text files to carry (see
+ Files above). Returns per-recipient state (`delivered` / `queued` /
`held`) and tier, e.g. "queued: alice/web-cursor is idle and turn-only, it sees this when alice next
prompts it". (`reason-network send --fyi` doesn't wake: read at the next natural pause.) `notify` also pops it up on
the person's screen, for an approval or decision only they can make: osascript on macOS, notify-send on Linux,
diff --git a/README.md b/README.md
index c417d4c..deb9f5b 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,9 @@ trust the new hooks.
- **Messages land right away.** Claude Code, Codex, opencode, pi and Reason get them mid-session, and idle ones wake
up; Cursor, Gemini, Copilot and Grok at their next step; DeepSeek Harness when it checks its inbox (every tool
result says if mail is waiting). Claude sessions you already had open join without a restart.
+- **Small text files go along.** A message can carry up to 10 text files (markdown, code, JSON, logs, config; 256 KB
+ each, 512 KB in all). The recipient's engine saves copies under `~/.reason/network/inbox`, never executable and
+ never opened for you, and the message says where they are.
- **Your sign-off, even when you're away from your agent.** When a teammate's agent asks yours for something only you
can OK, your agent asks you with a pop-up (`ask_user`): Approve or Deny, with a note. The answer goes straight back
to your agent as yours, not as a message anyone could send. With `notify`, a message also pops up on the person's
@@ -52,6 +55,7 @@ trust the new hooks.
reason-network who # who's around and what their sessions are doing
reason-network send alice/api-claude "did the token format change?" # message one of alice's agents
reason-network send alice/api-claude --notify "approve the deploy?" # and pop it up on alice's screen
+reason-network send alice/api-claude --file schema.json "the schema I meant" # with a small text file
reason-network send '*' "deploying api at 5" # everyone (or alice/*, repo:api): from you, never an agent
reason-network listen --as terminal # replies to what you sent from this terminal
reason-network link # the invite line again: Join the Reason Network: <link>
diff --git a/src/daemon/ask.rs b/src/daemon/ask.rs
index 89af8d4..9775049 100644
--- a/src/daemon/ask.rs
+++ b/src/daemon/ask.rs
@@ -201,7 +201,10 @@ impl Daemon {
let signed = Box::new(Signed::new(approval, &self.key, team.name(), "approval"));
let live = self.inner.lock().unwrap().live.contains_key(key);
let from = if live { key.to_string() } else { self.terminal().await };
- match self.send(from, &request.from, &said, Some(request.id.clone()), true, false, Some(signed)).await {
+ match self
+ .send(from, &request.from, &said, Some(request.id.clone()), true, false, Some(signed), Vec::new())
+ .await
+ {
Ok(_) => format!(" (Sent to {}, signed.)", request.from),
Err(e) => {
log(&format!("couldn't send {}'s answer to {}: {e:#}", self.person, request.from));
@@ -239,7 +242,7 @@ impl Daemon {
let Some(to) = g.live.get(key).map(|l| self.addr(&l.name)) else {
return log(&format!("{key} had gone before {} answered its question", self.person));
};
- let msg = Message {
+ let mut msg = Message {
id: Ulid::new().to_string(),
from: format!("{}/terminal", self.person),
from_agent: None,
@@ -252,8 +255,9 @@ impl Daemon {
notify: false,
sent_at: util::now_ms(),
approval: None,
+ files: Vec::new(),
};
- if let Err(e) = g.store.insert(&msg, false) {
+ if let Err(e) = g.store.insert(&mut msg, false) {
return log(&format!("couldn't keep a late answer: {e}"));
}
self.deliver(&mut g, key, vec![msg], true);
diff --git a/src/daemon/directory.rs b/src/daemon/directory.rs
index c03c181..cc8ea84 100644
--- a/src/daemon/directory.rs
+++ b/src/daemon/directory.rs
@@ -202,7 +202,11 @@ impl Daemon {
let mut known = g.store.sessions().unwrap_or_default();
known.retain(|r| !live.contains(r.name.as_str()));
known.sort_by_key(|r| -r.last_seen);
- Presence { sessions, known: known.into_iter().take(100).map(|r| r.name).collect() }
+ Presence {
+ sessions,
+ known: known.into_iter().take(100).map(|r| r.name).collect(),
+ features: vec![crate::files::FEATURE.into()],
+ }
}
pub(super) async fn who(
diff --git a/src/daemon/mail.rs b/src/daemon/mail.rs
index 7c2c7df..98e0f94 100644
--- a/src/daemon/mail.rs
+++ b/src/daemon/mail.rs
@@ -5,7 +5,7 @@
//! don't show them a second time.
use super::{Daemon, Inner, directory::Target, log};
-use crate::{agent, claude, config::Policy, proto::*, render, team::Signed, util};
+use crate::{agent, claude, config::Policy, files, proto::*, render, team::Signed, util};
use anyhow::{Result, anyhow, bail};
use iroh::EndpointId;
use std::{
@@ -64,6 +64,10 @@ impl Inner {
}
}
+/// A message to send to a teammate's machine now: where its result goes, the machine, its name, whether it is
+/// connected, and the message.
+type RemoteJob = (usize, EndpointId, String, bool, Message);
+
const UPGRADE: &str =
"update Reason Network (with the Reason CLI, `reason update`): messages go between agent sessions now";
@@ -81,6 +85,7 @@ impl Daemon {
wake: bool,
notify: bool,
approval: Option<Box<Signed<Approval>>>,
+ files: Vec<Attachment>,
) -> Result<SendResult> {
let (body, to) = (body.trim(), to.trim());
if body.is_empty() {
@@ -90,12 +95,14 @@ impl Daemon {
bail!("notify is for one session or one person, not a group");
}
if body.len() > MAX_BODY {
+ // A path means nothing on the recipient's computer.
bail!(
- "message is {} KB; the limit is {} KB (send a path or link instead)",
- body.len() / 1024,
+ "message is {} KB; the limit is {} KB (send a small text file with `files`, or a link)",
+ body.len().div_ceil(1024),
MAX_BODY / 1024
);
}
+ let files = files::check(files)?;
let now = util::now_ms();
let net = self.net();
let remotes = net.as_ref().map(|n| n.remotes()).unwrap_or_default();
@@ -123,6 +130,9 @@ impl Daemon {
// Your own user, not a session: just the pop-up, on each of their machines that is on now. (To get a
// teammate's attention, message one of their sessions with notify.)
let own = notify && to == self.person;
+ if own && !files.is_empty() {
+ bail!("files go to a session: send them to one of {to}'s sessions");
+ }
if own {
let msg = Message {
id: Ulid::new().to_string(),
@@ -137,6 +147,7 @@ impl Daemon {
notify: true,
sent_at: now,
approval: None,
+ files: Vec::new(),
};
let (sent, note) = self.notify(&msg);
let state = if sent { "notified" } else { "not notified" };
@@ -158,7 +169,7 @@ impl Daemon {
}
let targets = if own { Vec::new() } else { self.resolve(&g, &remotes, to, Some(&exclude))? };
for t in targets {
- let msg = Message {
+ let mut msg = Message {
id: Ulid::new().to_string(),
from: from_addr.clone(),
from_agent: from_agent.clone(),
@@ -171,6 +182,7 @@ impl Daemon {
notify,
sent_at: now,
approval: approval.clone(),
+ files: files.clone(),
};
res.ids.push(msg.id.clone());
let here = !matches!(t, Target::Remote { .. });
@@ -179,20 +191,22 @@ impl Daemon {
("refused".into(), LOOP_NOTE.into())
}
Target::Session { key: Some(key), .. } => {
- g.store.insert(&msg, false)?;
+ g.store.insert(&mut msg, false)?;
self.deliver(&mut g, &key, vec![msg.clone()], wake)
}
Target::Session { key: None, .. } => {
- g.store.insert(&msg, false)?;
+ g.store.insert(&mut msg, false)?;
("queued".into(), "queued: the session is offline; delivered when it is back".into())
}
- Target::Remote { machine, machine_name, online, .. } => {
- g.store.outbox_add(&machine.to_string(), &msg)?;
- remote_jobs.push((res.recipients.len(), machine, machine_name.clone(), online, msg.clone()));
- (
- "queued".into(),
- format!("queued: not connected to {machine_name} right now; it goes out as soon as it is"),
- )
+ Target::Remote { machine, .. } => {
+ let takes_files = remotes.get(&machine).is_some_and(|r| r.presence.has(files::FEATURE));
+ match for_machine(msg.clone(), takes_files) {
+ Ok(m) => {
+ msg = m;
+ self.queue_remote(&mut g, t, &msg, &mut remote_jobs, res.recipients.len())?
+ }
+ Err(note) => ("refused".into(), note),
+ }
}
};
if notify && here && state != "refused" {
@@ -230,6 +244,22 @@ impl Daemon {
Ok(res)
}
+ /// Puts a message for a teammate's machine in the outbox, and on the list to send now if it is connected.
+ fn queue_remote(
+ &self,
+ g: &mut Inner,
+ t: Target,
+ msg: &Message,
+ jobs: &mut Vec<RemoteJob>,
+ at: usize,
+ ) -> Result<(String, String)> {
+ let Target::Remote { machine, machine_name, online, .. } = t else { unreachable!("only remote targets") };
+ g.store.outbox_add(&machine.to_string(), msg)?;
+ let note = format!("queued: not connected to {machine_name} right now; it goes out as soon as it is");
+ jobs.push((at, machine, machine_name, online, msg.clone()));
+ Ok(("queued".into(), note))
+ }
+
/// Hands messages to a session here by the fastest way it has. Returns the state and a note for the sender.
pub(super) fn deliver(
self: &Arc<Self>,
@@ -480,8 +510,9 @@ impl Daemon {
let note = format!("refused: agents message one of {}'s sessions, not a group", self.person);
return Ok(("refused".into(), note));
}
+ // A held message's files are saved only once the person approves it.
let held = policy == Policy::Hold;
- g.store.insert(&msg, held)?;
+ g.store.insert(&mut msg, held)?;
let (state, note) = match key {
_ if held => ("held".into(), format!("held until {} approves it", self.person)),
Some(key) => self.deliver(&mut g, &key, vec![msg.clone()], msg.wake),
@@ -624,10 +655,26 @@ fn clean(m: Message) -> Result<Message> {
from_repo: m.from_repo.as_deref().and_then(|s| tidy(s, "-_./", 100)),
via: m.via.as_deref().and_then(|s| tidy(s, "-_./:*@", 100)),
reply_to: m.reply_to.as_deref().and_then(|s| tidy(s, "", 40)),
+ files: files::check(m.files)?,
..m
})
}
+/// A message as it goes to a teammate's machine. One whose engine doesn't take files (its presence doesn't say so)
+/// would drop them without a word: they are written out in the text instead, if that fits, and otherwise it isn't sent.
+fn for_machine(mut msg: Message, takes_files: bool) -> Result<Message, String> {
+ if msg.files.is_empty() || takes_files {
+ return Ok(msg);
+ }
+ match files::inline(&msg.body, &msg.files, &msg.to, MAX_BODY) {
+ Some(body) => {
+ (msg.body, msg.files) = (body, Vec::new());
+ Ok(msg)
+ }
+ None => Err(format!("{} needs to update Reason Network to receive files", msg.to)),
+ }
+}
+
/// Runs futures concurrently and collects their outputs in order.
async fn futures_join<F>(futs: impl Iterator<Item = F>) -> Vec<F::Output>
where
@@ -648,6 +695,7 @@ where
mod tests {
use super::*;
use iroh::SecretKey;
+ use serde::Deserialize;
fn approval(asker: &str, approver: &str) -> Approval {
Approval {
@@ -686,4 +734,44 @@ mod tests {
let big = Signed::new(big, &bob, "acme", "approval");
assert!(!approval_holds(&big, "acme", &bob.public(), "bob", "alice/api"));
}
+
+ #[test]
+ fn an_older_engine_gets_the_files_in_the_text_or_nothing() {
+ let msg = |content: &str| Message {
+ id: "01M".into(),
+ from: "alex/web".into(),
+ from_agent: None,
+ from_repo: None,
+ to: "bob/api".into(),
+ via: None,
+ reply_to: None,
+ body: "the config".into(),
+ wake: true,
+ notify: false,
+ sent_at: 0,
+ approval: None,
+ files: vec![Attachment { name: "app.toml".into(), content: content.into(), path: None }],
+ };
+ // An engine that takes files gets them as they are.
+ assert_eq!(for_machine(msg("port = 1"), true).unwrap().files.len(), 1);
+ // One that doesn't: written out in the text, as nothing it doesn't know would be dropped.
+ let sent = for_machine(msg("port = 1"), false).unwrap();
+ assert!(sent.files.is_empty());
+ assert!(sent.body.starts_with("the config\n\n(bob/api runs an older Reason Network"), "{}", sent.body);
+ assert!(sent.body.ends_with("app.toml:\n```\nport = 1\n```"), "{}", sent.body);
+ // Too much to write out: not sent, and the sender is told why.
+ let e = for_machine(msg(&"x".repeat(MAX_BODY)), false).unwrap_err();
+ assert_eq!(e, "bob/api needs to update Reason Network to receive files");
+ // Nothing to carry: as it is.
+ let mut plain = msg("");
+ plain.files.clear();
+ assert_eq!(for_machine(plain, false).unwrap().body, "the config");
+ // What an older engine reads of a message with files: the message, without them (hence the above).
+ #[derive(Deserialize)]
+ struct Older {
+ body: String,
+ }
+ let wire = serde_json::to_string(&msg("port = 1")).unwrap();
+ assert_eq!(serde_json::from_str::<Older>(&wire).unwrap().body, "the config");
+ }
}
diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs
index 20d12b8..788c8dc 100644
--- a/src/daemon/mod.rs
+++ b/src/daemon/mod.rs
@@ -129,6 +129,8 @@ struct Inner {
polling: HashMap<String, usize>,
/// Recent agent-to-agent message times per (from, to), for the loop guard.
pairs: HashMap<(String, String), VecDeque<Instant>>,
+ /// When the inbox folder was last swept of files no longer kept (ms).
+ swept: i64,
}
/// A session running on this machine.
@@ -169,7 +171,7 @@ pub fn load_key() -> Result<SecretKey> {
impl Daemon {
fn open() -> Result<Self> {
let cfg = Config::load();
- let store = Store::open(&paths::db())?;
+ let store = Store::open(&paths::db(), paths::inbox())?;
let team: Option<TeamState> = store.get("team")?.and_then(|s| serde_json::from_str(&s).ok());
let key = load_key()?;
// In a team, who we are is what the signed roster says, whatever the config file says.
@@ -188,6 +190,7 @@ impl Daemon {
in_flight: HashMap::new(),
polling: HashMap::new(),
pairs: HashMap::new(),
+ swept: 0,
}),
arrived: Notify::new(),
git_cache: Mutex::new(HashMap::new()),
@@ -275,8 +278,8 @@ impl Daemon {
}
Request::SetStatus { key, doing, name } => json!(self.set_status(&key, doing, name)?),
Request::Who { me, filter, all } => json!(self.who(me, filter, all).await?),
- Request::Send { from, to, body, reply_to, wake, notify } => {
- json!(self.send(from, &to, &body, reply_to, wake, notify, None).await?)
+ Request::Send { from, to, body, reply_to, wake, notify, files } => {
+ json!(self.send(from, &to, &body, reply_to, wake, notify, None, files).await?)
}
Request::Inbox { key, wait_secs, peek } => json!(self.inbox(key, wait_secs.min(MAX_WAIT), peek).await?),
Request::Unread { key } => json!(self.unread(&key)?),
diff --git a/src/daemon/sessions.rs b/src/daemon/sessions.rs
index fdb5cc3..615c460 100644
--- a/src/daemon/sessions.rs
+++ b/src/daemon/sessions.rs
@@ -285,6 +285,11 @@ impl Daemon {
let live: HashSet<&str> = g.live.values().map(|l| l.name.as_str()).collect();
let _ = g.store.forget_sessions(now - FORGET_MS, &live, &self.person);
}
+ // Files of messages that are gone, or were read a day ago.
+ if now - g.swept > 10 * 60_000 {
+ g.swept = now;
+ g.store.sweep_files(now);
+ }
}
}
diff --git a/src/files.rs b/src/files.rs
new file mode 100644
index 0000000..464cab3
--- /dev/null
+++ b/src/files.rs
@@ -0,0 +1,327 @@
+//! Small text files carried in a message: read from paths on the sender's computer, checked against the limits on both
+//! sides, and saved in the recipient's inbox folder (`<state home>/inbox/<message id>/<name>`), never executable and
+//! never opened. They travel inside the message itself, so they are as authenticated as its text.
+
+use crate::{proto::Attachment, render};
+use anyhow::{Context, Result, anyhow, bail};
+use std::{collections::HashSet, path::Path};
+
+pub const MAX_FILE: usize = 256 * 1024;
+pub const MAX_TOTAL: usize = 512 * 1024;
+pub const MAX_FILES: usize = 10;
+/// In characters; a longer name keeps its extension.
+const MAX_NAME: usize = 100;
+/// What a machine's presence lists when its engine takes files. One that doesn't would drop them.
+pub const FEATURE: &str = "files";
+/// A message's files are kept while it is unread, and for this long once it was read.
+pub const KEEP_MS: i64 = 86_400_000;
+
+const TEXT_ONLY: &str = "only text files can be sent (markdown, source code, JSON, logs, config)";
+
+/// Reads the files at `paths` (relative ones from `cwd`) for a message.
+pub fn read(paths: &[String], cwd: &Path) -> Result<Vec<Attachment>> {
+ if paths.len() > MAX_FILES {
+ bail!(too_many(paths.len()));
+ }
+ let mut files = Vec::new();
+ for p in paths {
+ let path = cwd.join(p);
+ let meta = std::fs::metadata(&path).with_context(|| format!("can't read {p}"))?;
+ if !meta.is_file() {
+ bail!("{p} is not a file");
+ }
+ if meta.len() > MAX_FILE as u64 {
+ bail!(too_big(p, meta.len() as usize));
+ }
+ let bytes = std::fs::read(&path).with_context(|| format!("can't read {p}"))?;
+ let content = text(p, bytes)?;
+ let name = path.file_name().map_or_else(|| p.clone(), |n| n.to_string_lossy().into_owned());
+ files.push(Attachment { name, content, path: None });
+ }
+ check(files)
+}
+
+/// The text of a file, if it is one: valid UTF-8 without NUL bytes.
+fn text(name: &str, bytes: Vec<u8>) -> Result<String> {
+ if bytes.contains(&0) {
+ bail!("{name} looks like a binary file: {TEXT_ONLY}");
+ }
+ String::from_utf8(bytes).map_err(|_| anyhow!("{name} isn't UTF-8 text: {TEXT_ONLY}"))
+}
+
+/// Holds files to the limits and gives them safe, distinct names. Both sides run it: the sender's daemon, and the
+/// recipient's, which takes nothing a peer sends on trust.
+pub fn check(files: Vec<Attachment>) -> Result<Vec<Attachment>> {
+ if files.len() > MAX_FILES {
+ bail!(too_many(files.len()));
+ }
+ let mut total = 0;
+ let mut taken = HashSet::new();
+ let mut out = Vec::with_capacity(files.len());
+ for f in files {
+ if f.content.len() > MAX_FILE {
+ bail!(too_big(&f.name, f.content.len()));
+ }
+ if f.content.contains('\0') {
+ bail!("{} looks like a binary file: {TEXT_ONLY}", f.name);
+ }
+ total += f.content.len();
+ if total > MAX_TOTAL {
+ bail!(
+ "the files come to more than {} KB; a message carries at most {} KB of files (send a link for more)",
+ MAX_TOTAL / 1024,
+ MAX_TOTAL / 1024
+ );
+ }
+ let name = distinct(&safe_name(&f.name), &mut taken);
+ out.push(Attachment { name, content: f.content, path: None });
+ }
+ Ok(out)
+}
+
+fn too_many(n: usize) -> String {
+ format!("{n} files; a message carries at most {MAX_FILES}")
+}
+
+fn too_big(name: &str, bytes: usize) -> String {
+ format!(
+ "{name} is {} KB; a file can be at most {} KB (send a link for bigger ones)",
+ bytes.div_ceil(1024),
+ MAX_FILE / 1024
+ )
+}
+
+/// A file name that is just a name, here and on Windows: no folders (no separators, no `..`), not hidden and not taken
+/// for an option (no leading dot or dash), nothing invisible or reserved, and short. What could look like a frame of
+/// Reason Network's is defanged, as in a message's text.
+pub fn safe_name(name: &str) -> String {
+ let swapped: String =
+ name.chars()
+ .filter(|c| !invisible(*c))
+ .map(|c| {
+ if c.is_control() || matches!(c, '/' | '\\' | ':' | '<' | '>' | '"' | '|' | '?' | '*') {
+ '_'
+ } else {
+ c
+ }
+ })
+ .collect();
+ let mut s = swapped;
+ while s.contains("..") {
+ s = s.replace("..", ".");
+ }
+ let s = s.trim_start_matches(|c: char| c == '.' || c == '-' || c.is_whitespace());
+ let s = s.trim_end_matches(|c: char| c == '.' || c.is_whitespace());
+ let mut s = render::defang(&shorten(s, MAX_NAME));
+ let stem = s.split('.').next().unwrap_or("").to_ascii_uppercase();
+ let reserved = matches!(stem.as_str(), "CON" | "PRN" | "AUX" | "NUL")
+ || (stem.len() == 4
+ && (stem.starts_with("COM") || stem.starts_with("LPT"))
+ && stem.as_bytes()[3].is_ascii_digit());
+ if reserved {
+ s.insert(0, '_');
+ }
+ if s.is_empty() { "file".into() } else { s }
+}
+
+/// Zero-width, direction and other invisible characters, which could make a name read as another.
+fn invisible(c: char) -> bool {
+ matches!(c, '\u{AD}' | '\u{34F}' | '\u{61C}' | '\u{180E}' | '\u{FEFF}')
+ || ('\u{200B}'..='\u{200F}').contains(&c)
+ || ('\u{202A}'..='\u{202E}').contains(&c)
+ || ('\u{2060}'..='\u{206F}').contains(&c)
+ || ('\u{FE00}'..='\u{FE0F}').contains(&c)
+}
+
+/// Cut to `max` characters, keeping a short extension.
+fn shorten(name: &str, max: usize) -> String {
+ if name.chars().count() <= max {
+ return name.to_string();
+ }
+ let (stem, ext) = split_ext(name);
+ let room = max.saturating_sub(ext.chars().count());
+ let stem: String = stem.chars().take(room).collect();
+ format!("{}{ext}", stem.trim_end_matches(['.', ' ']))
+}
+
+/// `notes.md` -> (`notes`, `.md`); an extension longer than 16 characters isn't one.
+fn split_ext(name: &str) -> (&str, &str) {
+ match name.rfind('.') {
+ Some(i) if i > 0 && name[i..].chars().count() <= 16 => name.split_at(i),
+ _ => (name, ""),
+ }
+}
+
+/// `name`, or `name-2`, `name-3`, … when another file of the message has it (ignoring case, as macOS and Windows do).
+fn distinct(name: &str, taken: &mut HashSet<String>) -> String {
+ let (stem, ext) = split_ext(name);
+ let mut pick = name.to_string();
+ let mut n = 2;
+ while !taken.insert(pick.to_lowercase()) {
+ pick = format!("{stem}-{n}{ext}");
+ n += 1;
+ }
+ pick
+}
+
+/// Saves a message's files (already through [`check`]) in `dir`, its own folder, which must not exist yet: readable by
+/// this user only and never executable. Nothing is left behind if it fails.
+pub fn save(dir: &Path, files: &[Attachment]) -> Result<()> {
+ let mut folder = std::fs::DirBuilder::new();
+ #[cfg(unix)]
+ std::os::unix::fs::DirBuilderExt::mode(&mut folder, 0o700);
+ if let Some(inbox) = dir.parent() {
+ folder.recursive(true).create(inbox).with_context(|| format!("can't create {}", inbox.display()))?;
+ }
+ folder.recursive(false).create(dir).with_context(|| format!("can't create {}", dir.display()))?;
+ for f in files {
+ let path = dir.join(&f.name);
+ let mut opts = std::fs::OpenOptions::new();
+ opts.write(true).create_new(true);
+ #[cfg(unix)]
+ std::os::unix::fs::OpenOptionsExt::mode(&mut opts, 0o600);
+ let written = opts.open(&path).and_then(|mut file| std::io::Write::write_all(&mut file, f.content.as_bytes()));
+ if let Err(e) = written {
+ let _ = std::fs::remove_dir_all(dir);
+ return Err(anyhow!("can't save {}: {e}", path.display()));
+ }
+ }
+ Ok(())
+}
+
+/// Files saved in `dir`, as the recipient sees them: their paths, no text.
+pub fn located(dir: &Path, files: &[Attachment]) -> Vec<Attachment> {
+ files
+ .iter()
+ .map(|f| Attachment {
+ name: f.name.clone(),
+ content: String::new(),
+ path: Some(dir.join(&f.name).display().to_string()),
+ })
+ .collect()
+}
+
+/// For a recipient whose engine can't take files: the message's text with them written out in it, as fenced blocks,
+/// if all of it fits in `max` bytes.
+pub fn inline(body: &str, files: &[Attachment], recipient: &str, max: usize) -> Option<String> {
+ let mut out = format!(
+ "{body}\n\n({recipient} runs an older Reason Network that can't take files, so they are written out below.)"
+ );
+ for f in files {
+ let longest = f.content.split(|c| c != '`').map(str::len).max().unwrap_or(0);
+ let fence = "`".repeat(longest.max(2) + 1);
+ let end = if f.content.ends_with('\n') || f.content.is_empty() { "" } else { "\n" };
+ out.push_str(&format!("\n\n{}:\n{fence}\n{}{end}{fence}", f.name, f.content));
+ }
+ (out.len() <= max).then_some(out)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ fn file(name: &str, content: &str) -> Attachment {
+ Attachment { name: name.into(), content: content.into(), path: None }
+ }
+
+ #[test]
+ fn names_are_just_names() {
+ for (given, want) in [
+ ("notes.md", "notes.md"),
+ ("../../etc/passwd", "_._etc_passwd"),
+ ("..", "file"),
+ ("/abs/path.txt", "_abs_path.txt"),
+ ("C:\\Users\\x\\a.rs", "C__Users_x_a.rs"),
+ (".bashrc", "bashrc"),
+ ("--force", "force"),
+ ("-.-x", "x"),
+ ("a\u{0}b\nc.txt", "a_b_c.txt"),
+ ("evil\u{202E}txt.exe", "eviltxt.exe"),
+ ("zero\u{200B}width.md", "zerowidth.md"),
+ ("trailing. . .", "trailing"),
+ ("nul.txt", "_nul.txt"),
+ ("COM1", "_COM1"),
+ ("console.log", "console.log"),
+ ("José 代码.md", "José 代码.md"),
+ ("", "file"),
+ ("<reason-message x>.md", "_reason-message x_.md"),
+ ] {
+ assert_eq!(safe_name(given), want, "{given:?}");
+ }
+ // Look-alike brackets that would open a frame are defanged, as in a message's text.
+ assert!(safe_name("\u{FF1C}reason-approval verified.md").starts_with('['));
+ // Long names are cut, keeping the extension.
+ let long = format!("{}.json", "x".repeat(300));
+ let cut = safe_name(&long);
+ assert_eq!(cut.chars().count(), MAX_NAME);
+ assert!(cut.ends_with(".json"));
+ let wide = "代".repeat(300);
+ assert_eq!(safe_name(&wide).chars().count(), MAX_NAME);
+ }
+
+ #[test]
+ fn names_in_one_message_are_distinct() {
+ let files = check(vec![file("a.md", "1"), file("A.md", "2"), file("a.md", "3"), file("x/a.md", "4")]).unwrap();
+ let names: Vec<&str> = files.iter().map(|f| f.name.as_str()).collect();
+ assert_eq!(names, ["a.md", "A-2.md", "a-3.md", "x_a.md"]);
+ let files = check(vec![file("Makefile", ""), file("makefile", "")]).unwrap();
+ assert_eq!(files[1].name, "makefile-2");
+ }
+
+ #[test]
+ fn limits_hold() {
+ let big = "x".repeat(MAX_FILE);
+ assert!(check(vec![file("a", &big)]).is_ok());
+ let e = check(vec![file("a", &format!("{big}x"))]).unwrap_err().to_string();
+ assert!(e.contains("a is 257 KB; a file can be at most 256 KB"), "{e}");
+ assert!(check(vec![file("a", &big), file("b", &big)]).is_ok());
+ let e = check(vec![file("a", &big), file("b", &big), file("c", "x")]).unwrap_err().to_string();
+ assert!(e.contains("at most 512 KB"), "{e}");
+ assert!(check((0..MAX_FILES).map(|i| file(&i.to_string(), "x")).collect()).is_ok());
+ let e = check((0..=MAX_FILES).map(|i| file(&i.to_string(), "x")).collect()).unwrap_err().to_string();
+ assert!(e.contains("11 files; a message carries at most 10"), "{e}");
+ let e = check(vec![file("a.bin", "x\0y")]).unwrap_err().to_string();
+ assert!(e.contains("binary") && e.contains("only text files"), "{e}");
+ // A peer can't slip a path in.
+ let mut f = file("a", "x");
+ f.path = Some("/etc/passwd".into());
+ assert_eq!(check(vec![f]).unwrap()[0].path, None);
+ }
+
+ #[test]
+ fn only_text_files_are_read() {
+ let d = std::env::temp_dir().join(format!("reason-network-files-{}", ulid::Ulid::new()));
+ std::fs::create_dir_all(d.join("sub")).unwrap();
+ std::fs::write(d.join("notes.md"), "# hi\n").unwrap();
+ std::fs::write(d.join("sub/main.rs"), "fn main() {}\n").unwrap();
+ std::fs::write(d.join("img.png"), [0x89, b'P', b'N', b'G', 0, 1]).unwrap();
+ std::fs::write(d.join("latin1.txt"), [b'c', b'a', b'f', 0xe9]).unwrap();
+ std::fs::write(d.join("big.log"), "x".repeat(MAX_FILE + 1)).unwrap();
+ let files = read(&["notes.md".into(), d.join("sub/main.rs").display().to_string()], &d).unwrap();
+ assert_eq!(files[0], file("notes.md", "# hi\n"));
+ assert_eq!(files[1], file("main.rs", "fn main() {}\n"));
+ for (p, want) in [
+ ("img.png", "looks like a binary file"),
+ ("latin1.txt", "isn't UTF-8 text"),
+ ("big.log", "a file can be at most 256 KB"),
+ ("sub", "is not a file"),
+ ("missing.txt", "can't read missing.txt"),
+ ] {
+ let e = format!("{:#}", read(&[p.into()], &d).unwrap_err());
+ assert!(e.contains(want), "{p}: {e}");
+ }
+ let many: Vec<String> = (0..11).map(|_| "notes.md".to_string()).collect();
+ assert!(read(&many, &d).unwrap_err().to_string().contains("at most 10"));
+ std::fs::remove_dir_all(&d).unwrap();
+ }
+
+ #[test]
+ fn inlined_files_keep_their_fences_and_fit_or_dont() {
+ let files = vec![file("a.md", "```rust\nfn x() {}\n```\n"), file("b.txt", "plain")];
+ let out = inline("see these", &files, "bob/api", 64 * 1024).unwrap();
+ assert!(out.starts_with("see these\n\n(bob/api runs an older Reason Network"), "{out}");
+ assert!(out.contains("a.md:\n````\n```rust\nfn x() {}\n```\n````"), "{out}");
+ assert!(out.ends_with("b.txt:\n```\nplain\n```"), "{out}");
+ assert_eq!(inline("x", &[file("a", &"y".repeat(100))], "bob/api", 100), None);
+ }
+}
diff --git a/src/main.rs b/src/main.rs
index a34f8db..916ab03 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,6 +4,7 @@ mod client;
mod config;
mod daemon;
mod doing;
+mod files;
mod git;
mod hook;
mod invite;
@@ -149,6 +150,9 @@ enum Cmd {
/// Send as this agent session here (e.g. one made with `listen --as <name>`)
#[arg(long)]
from: Option<String>,
+ /// A text file to send with it (repeat for more): at most 10, 256 KB each, 512 KB in all
+ #[arg(long = "file", value_name = "PATH")]
+ files: Vec<String>,
},
/// Set what the current agent session is doing (run from inside the agent's shell)
Status {
@@ -388,7 +392,7 @@ async fn cli(cmd: Cmd) -> Result<()> {
print!("{}", render::directory(&d, false, render::Paint::terminal()));
}
}
- Cmd::Send { to, message, reply_to, fyi, notify, from } => {
+ Cmd::Send { to, message, reply_to, fyi, notify, from, files } => {
let mut body = message.join(" ");
if message.is_empty() || body == "-" {
body.clear();
@@ -397,6 +401,7 @@ async fn cli(cmd: Cmd) -> Result<()> {
if body.trim().is_empty() {
bail!("empty message");
}
+ let files = files::read(&files, &std::env::current_dir()?)?;
// Messages come from agent sessions: `--from`'s, this agent's, or from a plain terminal, `<you>/terminal`.
// An agent shell Reason Network doesn't know (its tools aren't loaded) counts as a terminal.
let send = |from: String| Request::Send {
@@ -406,6 +411,7 @@ async fn cli(cmd: Cmd) -> Result<()> {
reply_to: reply_to.clone(),
wake: !fyi,
notify,
+ files: files.clone(),
};
// As the terminal, a group send has to come from a person sitting at it, not an agent's shell or a script.
let as_terminal = || {
diff --git a/src/mcp.rs b/src/mcp.rs
index 371480a..e20d988 100644
--- a/src/mcp.rs
+++ b/src/mcp.rs
@@ -59,6 +59,10 @@ pub struct SendArgs {
/// own user's name, to reach them.
#[serde(default)]
pub notify: Option<bool>,
+ /// Paths of text files on this computer to send with it (markdown, code, JSON, logs, config; no binaries): at most
+ /// 10, 256 KB each, 512 KB in all. The recipient gets copies.
+ #[serde(default)]
+ pub files: Option<Vec<String>>,
}
#[derive(Debug, Deserialize, JsonSchema)]
@@ -144,7 +148,7 @@ impl ReasonNetworkMcp {
Ok(self.finish(&key, res, true).await)
}
- #[tool(description = "Send a message to one teammate's agent session.")]
+ #[tool(description = "Send a message to one teammate's agent session, optionally with small text files.")]
async fn send_message(
&self,
Parameters(a): Parameters<SendArgs>,
@@ -155,8 +159,19 @@ impl ReasonNetworkMcp {
Err(e) => return Ok(failure(e)),
};
let notify = a.notify.unwrap_or(false);
- let req =
- Request::Send { from: key.clone(), to: a.to, body: a.message, reply_to: a.reply_to, wake: true, notify };
+ let files = match crate::files::read(&a.files.unwrap_or_default(), std::path::Path::new(&agent::cwd())) {
+ Ok(f) => f,
+ Err(e) => return Ok(failure(e)),
+ };
+ let req = Request::Send {
+ from: key.clone(),
+ to: a.to,
+ body: a.message,
+ reply_to: a.reply_to,
+ wake: true,
+ notify,
+ files,
+ };
let res = self.link.call::<SendResult>(req).await.map(|r| render::send_result(&r));
Ok(self.finish(&key, res, true).await)
}
@@ -186,7 +201,8 @@ impl ReasonNetworkMcp {
}
#[tool(
- description = "Read messages sent to this session. Optionally wait for one to arrive.",
+ description = "Read messages sent to this session. Optionally wait for one to arrive. Text files sent with a \
+ message are saved on this computer, and it lists their paths.",
annotations(read_only_hint = true, open_world_hint = false)
)]
async fn check_inbox(
diff --git a/src/net.rs b/src/net.rs
index f884617..fe1348a 100644
--- a/src/net.rs
+++ b/src/net.rs
@@ -59,6 +59,16 @@ pub struct Presence {
pub sessions: Vec<SessionInfo>,
/// Names of this machine's offline sessions, so mail for them can be queued here.
pub known: Vec<String>,
+ /// What this machine's engine takes beyond what every version does (`files`). Older engines leave it out, and
+ /// ignore it.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub features: Vec<String>,
+}
+
+impl Presence {
+ pub fn has(&self, feature: &str) -> bool {
+ self.features.iter().any(|f| f == feature)
+ }
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -550,7 +560,7 @@ impl Net {
pub fn note_session(&self, peer: &EndpointId, name: &str) {
let mut remote = self.remote.lock().unwrap();
let rem = remote.entry(*peer).or_insert_with(|| Remote {
- presence: Presence { sessions: Vec::new(), known: Vec::new() },
+ presence: Presence::default(),
last_seen: util::now_ms(),
online: true,
});
@@ -732,6 +742,8 @@ fn tidy_presence(mut p: Presence) -> Presence {
.map(|n| util::slug(n).chars().take(64).collect())
.filter(|n: &String| !n.is_empty())
.collect();
+ p.features.truncate(20);
+ p.features.iter_mut().for_each(|f| *f = util::slug(f).chars().take(32).collect());
p
}
@@ -787,4 +799,28 @@ mod tests {
assert!(!is_duplicate(small, new, small, small));
assert!(!is_duplicate(large, new, large, small));
}
+
+ #[test]
+ fn presence_says_files_are_taken_and_older_engines_read_it_as_before() {
+ let new =
+ Presence { known: vec!["api".into()], features: vec![crate::files::FEATURE.into()], ..Default::default() };
+ assert!(new.has("files"));
+ let wire = serde_json::to_string(&Wire::Presence { presence: new }).unwrap();
+ // As an engine from before files reads it: the field is ignored.
+ #[derive(Deserialize)]
+ struct OlderPresence {
+ known: Vec<String>,
+ }
+ #[derive(Deserialize)]
+ #[serde(tag = "t", rename_all = "snake_case")]
+ enum OlderWire {
+ Presence { presence: OlderPresence },
+ }
+ let OlderWire::Presence { presence } = serde_json::from_str(&wire).unwrap();
+ assert_eq!(presence.known, ["api"]);
+ // And an older engine's presence says nothing of files.
+ let older: Presence = serde_json::from_str(r#"{"sessions":[],"known":[]}"#).unwrap();
+ assert!(!older.has("files"));
+ assert!(tidy_presence(Presence { features: vec!["files".into()], ..Default::default() }).has("files"));
+ }
}
diff --git a/src/paths.rs b/src/paths.rs
index a46f806..0ab392c 100644
--- a/src/paths.rs
+++ b/src/paths.rs
@@ -224,6 +224,11 @@ pub fn log() -> PathBuf {
home().join("daemon.log")
}
+/// Files that came with messages, one folder per message.
+pub fn inbox() -> PathBuf {
+ home().join("inbox")
+}
+
pub fn lock() -> PathBuf {
home().join("daemon.lock")
}
diff --git a/src/proto.rs b/src/proto.rs
index 4371fe8..45139f7 100644
--- a/src/proto.rs
+++ b/src/proto.rs
@@ -112,6 +112,9 @@ pub enum Request {
/// Also pop it up on the person's screen. With it, `to` may be your own user.
#[serde(default)]
notify: bool,
+ /// Small text files carried in the message (the client reads them from the sender's paths).
+ #[serde(default)]
+ files: Vec<Attachment>,
},
/// A session's unread messages. With `peek` nothing is marked read: the client acknowledges with `Delivered`
/// after showing the messages, so nothing is lost if it goes away first.
@@ -308,6 +311,22 @@ pub struct Message {
/// A person's signed answer to the request this replies to (`ask_user` with `about`).
#[serde(default, skip_serializing_if = "Option::is_none")]
pub approval: Option<Box<crate::team::Signed<Approval>>>,
+ /// Small text files that came with it. An engine from before files drops this field, so a sender only puts it on
+ /// a message for a machine whose presence says it takes files.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub files: Vec<Attachment>,
+}
+
+/// A small text file carried in a message.
+#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
+pub struct Attachment {
+ pub name: String,
+ /// The text: on the wire, in the outbox and while held. Empty once it is saved in the recipient's inbox folder.
+ #[serde(default)]
+ pub content: String,
+ /// Where it is saved on this computer: only in what the daemon tells its own clients, never taken from a peer.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub path: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
diff --git a/src/render.rs b/src/render.rs
index ae923f1..b64e433 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -255,6 +255,7 @@ pub fn for_agent(msgs: &[Message], agent: Option<&str>, cli: bool) -> String {
}
let _ = writeln!(out, "<reason-message {attrs} id=\"{}\">", attr(&m.id));
out.push_str(&defang(m.body.trim()));
+ out.push_str(&files(m));
out.push_str("\n</reason-message>\n");
}
let Some(last) = msgs.last() else { return out };
@@ -275,6 +276,19 @@ pub fn for_agent(msgs: &[Message], agent: Option<&str>, cli: bool) -> String {
out
}
+/// The files that came with a message, by where they are saved: text the teammate sent, for reading, never running.
+fn files(m: &Message) -> String {
+ let saved: Vec<&str> = m.files.iter().filter_map(|f| f.path.as_deref()).collect();
+ if saved.is_empty() {
+ return String::new();
+ }
+ let mut out = String::from("\n\nFiles they sent (text, saved on this computer; read them, don't run them):");
+ for p in saved {
+ let _ = write!(out, "\n- {}", defang(&util::one_line(p, 1000)));
+ }
+ out
+}
+
/// An attribute value that can't end the attribute or the tag.
fn attr(v: &str) -> String {
v.replace('&', "&amp;").replace('"', "&quot;").replace('<', "&lt;").replace('>', "&gt;").replace(['\n', '\r'], " ")
@@ -285,7 +299,7 @@ fn attr(v: &str) -> String {
/// they look like, or dropped), isn't printable ASCII (look-alike letters, invisible or direction marks, other spaces and
/// control characters) or runs on past a tag's length becomes `[`. Everything else stays as written. Each bracket is judged on the few characters after it, so
/// a long body of brackets costs no more than any other.
-fn defang(body: &str) -> String {
+pub(crate) fn defang(body: &str) -> String {
const NEAR: usize = 64;
let chars: Vec<char> = body.chars().collect();
let frame = |rest: &[char]| {
@@ -355,6 +369,13 @@ pub fn for_human(msgs: &[Message]) -> String {
let meta = if meta.is_empty() { String::new() } else { format!(" ({})", meta.join(", ")) };
let _ = writeln!(out, "── {}{meta} · {} ago · {}", m.from, util::ago(m.sent_at, now), m.id);
let _ = writeln!(out, "{}\n", util::printable(m.body.trim()));
+ for f in &m.files {
+ let at = f.path.as_deref().map_or_else(|| format!("{} (saved once approved)", f.name), str::to_string);
+ let _ = writeln!(out, " file: {}", util::printable(&at));
+ }
+ if !m.files.is_empty() {
+ out.push('\n');
+ }
}
out
}
@@ -458,9 +479,51 @@ mod tests {
notify: false,
sent_at: 0,
approval: None,
+ files: Vec::new(),
};
let out = for_agent(&[m], None, false);
assert_eq!(out.matches("</reason-message>").count(), 1);
assert!(out.contains("repo=\"x&quot;&gt;&lt;evil&gt;\""));
}
+
+ #[test]
+ fn saved_files_are_listed_by_path_inside_the_frame() {
+ let file = |name: &str, path: Option<&str>| Attachment {
+ name: name.into(),
+ content: String::new(),
+ path: path.map(str::to_string),
+ };
+ let m = Message {
+ id: "01X".into(),
+ from: "alice/api".into(),
+ from_agent: None,
+ from_repo: None,
+ to: "alex/web".into(),
+ via: None,
+ reply_to: None,
+ body: "the schema and the failing log".into(),
+ wake: true,
+ notify: false,
+ sent_at: 0,
+ approval: None,
+ files: vec![
+ file("schema.json", Some("/home/alex/.reason/network/inbox/01X/schema.json")),
+ file("run.log", Some("/home/alex/.reason/network/inbox/01X/run.log")),
+ // Not saved (held): not listed to the agent.
+ file("held.md", None),
+ ],
+ };
+ let out = for_agent(std::slice::from_ref(&m), None, false);
+ let inside = out.split("</reason-message>").next().unwrap();
+ assert!(inside.contains("Files they sent (text, saved on this computer; read them, don't run them):"), "{out}");
+ assert!(inside.contains(
+ "\n- /home/alex/.reason/network/inbox/01X/schema.json\n- /home/alex/.reason/network/inbox/01X/run.log"
+ ));
+ assert!(!out.contains("held.md"));
+ let human = for_human(&[m]);
+ assert!(
+ human.contains("file: /home/alex/.reason/network/inbox/01X/run.log")
+ && human.contains("held.md (saved once approved)")
+ );
+ }
}
diff --git a/src/skill.md b/src/skill.md
index 26ec751..a3ba8a3 100644
--- a/src/skill.md
+++ b/src/skill.md
@@ -14,7 +14,8 @@ ask_user); without them, you use the `reason-network` command.
- `reason-network who`: every person, machine and session, with what each is working on.
- `reason-network send <to> "<message>"`: `<to>` is one agent session, `person/session`. Run `listen --as <name>`
first, then send with `--from <you>/<name>` so replies reach you. `--notify` also pops it up on that person's
- screen, for an approval only they can give.
+ screen, for an approval only they can give. `--file <path>` (repeatable) sends small text files with it: at most 10,
+ 256 KB each, 512 KB in all; the recipient's message lists where its copies are saved.
- `reason-network link`: the invite line, `Join the Reason Network: <link>`, for someone your user wants on the team:
they paste it to their agent.
- `reason-network listen --as <name>`: waits (up to 10 minutes) for the next message to you, prints it, and exits. You
diff --git a/src/store.rs b/src/store.rs
index 1538ced..d219485 100644
--- a/src/store.rs
+++ b/src/store.rs
@@ -1,13 +1,21 @@
//! SQLite: messages, and the sessions this machine has seen: live ones, ones gone for less than a day, and the
-//! person's own mailboxes (`cli`, such as `terminal`).
+//! person's own mailboxes (`cli`, such as `terminal`). Files that came with messages are saved in the inbox folder, one
+//! folder per message, and go with their message.
-use crate::proto::{Message, Tier};
+use crate::{
+ files,
+ proto::{Attachment, Message, Tier},
+};
use anyhow::Result;
use rusqlite::{Connection, OptionalExtension, Row, params};
-use std::{collections::HashSet, path::Path};
+use std::{
+ collections::HashSet,
+ path::{Path, PathBuf},
+};
pub struct Store {
db: Connection,
+ inbox: PathBuf,
}
#[derive(Debug, Clone)]
@@ -65,25 +73,54 @@ CREATE INDEX IF NOT EXISTS sessions_key ON sessions (key);
";
const MESSAGE_COLS: &str =
- "id, sender, sender_agent, sender_repo, recipient, via, reply_to, body, wake, sent_at, approval";
+ "id, sender, sender_agent, sender_repo, recipient, via, reply_to, body, wake, sent_at, approval, files";
const SESSION_COLS: &str = "name, key, agent, cwd, repo, branch, doing, tier, last_seen";
impl Store {
- pub fn open(path: &Path) -> Result<Self> {
+ /// The database at `path`, and the folder `inbox` that messages' files are saved in.
+ pub fn open(path: &Path, inbox: PathBuf) -> Result<Self> {
let db = Connection::open(path)?;
db.execute_batch(SCHEMA)?;
// Added in 0.10.7.
if !db.prepare("SELECT 1 FROM pragma_table_info('messages') WHERE name = 'approval'")?.exists([])? {
db.execute("ALTER TABLE messages ADD COLUMN approval TEXT", [])?;
}
- Ok(Store { db })
+ // Added with files: their names, and their text while the message is held (once saved, it is in the inbox
+ // folder instead).
+ if !db.prepare("SELECT 1 FROM pragma_table_info('messages') WHERE name = 'files'")?.exists([])? {
+ db.execute("ALTER TABLE messages ADD COLUMN files TEXT", [])?;
+ }
+ Ok(Store { db, inbox })
+ }
+
+ /// A message's own folder in the inbox folder. Message ids are plain letters and digits (`clean` holds a peer's to
+ /// that), so this is always just inside it.
+ fn files_dir(&self, id: &str) -> Result<PathBuf> {
+ if id.is_empty() || !id.chars().all(|c| c.is_ascii_alphanumeric()) {
+ anyhow::bail!("malformed message id");
+ }
+ Ok(self.inbox.join(id))
}
- /// Stores a message; false if one with this id is already here (a retried delivery).
- pub fn insert(&self, m: &Message, held: bool) -> Result<bool> {
+ /// Stores a message; false if one with this id is already here (a retried delivery). Unless it is held, its files
+ /// are saved in its inbox folder first, and `m` then lists them by path.
+ pub fn insert(&self, m: &mut Message, held: bool) -> Result<bool> {
+ if self.has(&m.id)? {
+ return Ok(false);
+ }
+ let saved = match (held, m.files.is_empty()) {
+ (false, false) => {
+ let dir = self.files_dir(&m.id)?;
+ // Left by an attempt that didn't finish.
+ let _ = std::fs::remove_dir_all(&dir);
+ files::save(&dir, &m.files)?;
+ Some(dir)
+ }
+ _ => None,
+ };
let n = self.db.execute(
&format!(
- "INSERT OR IGNORE INTO messages ({MESSAGE_COLS}, held) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12)"
+ "INSERT OR IGNORE INTO messages ({MESSAGE_COLS}, held) VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13)"
),
params![
m.id,
@@ -97,15 +134,28 @@ impl Store {
m.wake,
m.sent_at,
m.approval.as_ref().map(|a| serde_json::to_string(a).expect("approvals serialize")),
+ files_col(&m.files, held),
held
],
- )?;
- Ok(n == 1)
+ );
+ match (n, saved) {
+ (Ok(n), Some(dir)) => {
+ m.files = files::located(&dir, &m.files);
+ Ok(n == 1)
+ }
+ (Ok(n), None) => Ok(n == 1),
+ (Err(e), dir) => {
+ if let Some(dir) = dir {
+ let _ = std::fs::remove_dir_all(dir);
+ }
+ Err(e.into())
+ }
+ }
}
pub fn message(&self, id: &str) -> Result<Option<Message>> {
- let sql = format!("SELECT {MESSAGE_COLS} FROM messages WHERE id = ?1");
- Ok(self.db.query_row(&sql, [id], message).optional()?)
+ let sql = format!("SELECT {MESSAGE_COLS}, held FROM messages WHERE id = ?1");
+ Ok(self.db.query_row(&sql, [id], |r| self.row(r)).optional()?)
}
pub fn has(&self, id: &str) -> Result<bool> {
@@ -114,9 +164,9 @@ impl Store {
pub fn unread(&self, recipient: &str) -> Result<Vec<Message>> {
let mut st = self.db.prepare_cached(&format!(
- "SELECT {MESSAGE_COLS} FROM messages WHERE recipient = ?1 AND read_at IS NULL AND held = 0 ORDER BY sent_at, id"
+ "SELECT {MESSAGE_COLS}, held FROM messages WHERE recipient = ?1 AND read_at IS NULL AND held = 0 ORDER BY sent_at, id"
))?;
- let rows = st.query_map([recipient], message)?;
+ let rows = st.query_map([recipient], |r| self.row(r))?;
Ok(rows.collect::<rusqlite::Result<_>>()?)
}
@@ -129,22 +179,66 @@ impl Store {
}
pub fn held(&self) -> Result<Vec<Message>> {
- let mut st = self
- .db
- .prepare_cached(&format!("SELECT {MESSAGE_COLS} FROM messages WHERE held = 1 ORDER BY sent_at, id"))?;
- let rows = st.query_map([], message)?;
+ let mut st = self.db.prepare_cached(&format!(
+ "SELECT {MESSAGE_COLS}, held FROM messages WHERE held = 1 ORDER BY sent_at, id"
+ ))?;
+ let rows = st.query_map([], |r| self.row(r))?;
Ok(rows.collect::<rusqlite::Result<_>>()?)
}
- /// Approves (`release`) or drops held messages; returns the ones affected.
+ /// Whether a message's inbox folder stays: while the message is unread, and for a day after it was read. Held
+ /// messages have none, and a message that is gone takes its files with it.
+ fn keep_files(&self, id: &str, now: i64) -> bool {
+ let read_at =
+ self.db.query_row("SELECT read_at FROM messages WHERE id = ?1", [id], |r| r.get::<_, Option<i64>>(0));
+ match read_at.optional() {
+ Ok(Some(None)) => true,
+ Ok(Some(Some(at))) => at > now - files::KEEP_MS,
+ Ok(None) => false,
+ // Not sure: keep it for now.
+ Err(_) => true,
+ }
+ }
+
+ /// Deletes the inbox folders of messages that are gone, or were read more than a day ago.
+ pub fn sweep_files(&self, now: i64) {
+ let Ok(entries) = std::fs::read_dir(&self.inbox) else { return };
+ for e in entries.flatten() {
+ if !self.keep_files(&e.file_name().to_string_lossy(), now) {
+ let _ = std::fs::remove_dir_all(e.path());
+ }
+ }
+ }
+
+ fn drop_files(&self, id: &str) {
+ if let Ok(dir) = self.files_dir(id) {
+ let _ = std::fs::remove_dir_all(dir);
+ }
+ }
+
+ /// Approves (`release`) or drops held messages; returns the ones affected. An approved message's files are saved
+ /// now (one that can't be saved stays held); a dropped one's never are.
pub fn settle_held(&self, ids: &[String], release: bool) -> Result<Vec<Message>> {
let held = self.held()?;
- let picked: Vec<Message> = held.into_iter().filter(|m| ids.is_empty() || ids.contains(&m.id)).collect();
- let sql =
- if release { "UPDATE messages SET held = 0 WHERE id = ?1" } else { "DELETE FROM messages WHERE id = ?1" };
- let mut st = self.db.prepare_cached(sql)?;
- for m in &picked {
- st.execute([&m.id])?;
+ let mut picked: Vec<Message> = held.into_iter().filter(|m| ids.is_empty() || ids.contains(&m.id)).collect();
+ for m in &mut picked {
+ if !release {
+ self.db.execute("DELETE FROM messages WHERE id = ?1", [&m.id])?;
+ continue;
+ }
+ let dir = self.files_dir(&m.id)?;
+ if !m.files.is_empty() {
+ let _ = std::fs::remove_dir_all(&dir);
+ files::save(&dir, &m.files)?;
+ }
+ let col = files_col(&m.files, false);
+ if let Err(e) =
+ self.db.execute("UPDATE messages SET held = 0, files = ?2 WHERE id = ?1", params![m.id, col])
+ {
+ let _ = std::fs::remove_dir_all(&dir);
+ return Err(e.into());
+ }
+ m.files = files::located(&dir, &m.files);
}
Ok(picked)
}
@@ -233,10 +327,14 @@ impl Store {
let mut st = self.db.prepare_cached("SELECT name FROM sessions WHERE last_seen < ?1 AND agent != 'cli'")?;
let gone: Vec<String> = st.query_map([before], |r| r.get(0))?.collect::<rusqlite::Result<_>>()?;
for name in gone.iter().filter(|n| !live.contains(n.as_str())) {
- self.db.execute(
- "DELETE FROM messages WHERE recipient = ?1 AND read_at IS NULL AND held = 0",
- [format!("{person}/{name}")],
- )?;
+ let unread = "FROM messages WHERE recipient = ?1 AND read_at IS NULL AND held = 0";
+ let to = format!("{person}/{name}");
+ let mut ids = self.db.prepare_cached(&format!("SELECT id {unread}"))?;
+ let ids: Vec<String> = ids.query_map([&to], |r| r.get(0))?.collect::<rusqlite::Result<_>>()?;
+ self.db.execute(&format!("DELETE {unread}"), [&to])?;
+ for id in ids {
+ self.drop_files(&id);
+ }
self.db.execute("DELETE FROM sessions WHERE name = ?1", [name])?;
}
Ok(())
@@ -259,7 +357,13 @@ impl Store {
}
}
-fn message(r: &Row) -> rusqlite::Result<Message> {
+impl Store {
+ fn row(&self, r: &Row) -> rusqlite::Result<Message> {
+ message(r, &self.inbox)
+ }
+}
+
+fn message(r: &Row, inbox: &Path) -> rusqlite::Result<Message> {
Ok(Message {
id: r.get(0)?,
from: r.get(1)?,
@@ -273,9 +377,32 @@ fn message(r: &Row) -> rusqlite::Result<Message> {
notify: false,
sent_at: r.get(9)?,
approval: r.get::<_, Option<String>>(10)?.and_then(|a| serde_json::from_str(&a).ok()),
+ files: {
+ let id: String = r.get(0)?;
+ let files: Vec<Attachment> =
+ r.get::<_, Option<String>>(11)?.and_then(|f| serde_json::from_str(&f).ok()).unwrap_or_default();
+ // A held message's files aren't saved yet: their text is here. Once released, they are in its inbox folder.
+ if r.get::<_, bool>(12)? { files } else { files::located(&inbox.join(&id), &files) }
+ },
})
}
+/// The `files` column: names, and the text too while held.
+fn files_col(files: &[Attachment], held: bool) -> Option<String> {
+ if files.is_empty() {
+ return None;
+ }
+ let kept: Vec<Attachment> = files
+ .iter()
+ .map(|f| Attachment {
+ name: f.name.clone(),
+ content: if held { f.content.clone() } else { String::new() },
+ path: None,
+ })
+ .collect();
+ Some(serde_json::to_string(&kept).expect("files serialize"))
+}
+
fn remembered(r: &Row) -> rusqlite::Result<Remembered> {
Ok(Remembered {
name: r.get(0)?,
@@ -297,7 +424,7 @@ mod tests {
#[test]
fn a_session_gone_a_day_is_forgotten_with_the_mail_it_never_read() {
// (Not one that is live, nor the person's own mailbox, nor mail held for the person.)
- let store = Store::open(Path::new(":memory:")).unwrap();
+ let store = Store::open(Path::new(":memory:"), temp_inbox()).unwrap();
let day = 86_400_000;
let session = |name: &str, agent: &str, last_seen: i64| Remembered {
name: name.into(),
@@ -331,11 +458,12 @@ mod tests {
notify: false,
sent_at: 0,
approval: None,
+ files: Vec::new(),
};
- store.insert(&to("1", "gone"), false).unwrap();
- store.insert(&to("2", "recent"), false).unwrap();
- store.insert(&to("3", "terminal"), false).unwrap();
- store.insert(&to("4", "gone"), true).unwrap();
+ store.insert(&mut to("1", "gone"), false).unwrap();
+ store.insert(&mut to("2", "recent"), false).unwrap();
+ store.insert(&mut to("3", "terminal"), false).unwrap();
+ store.insert(&mut to("4", "gone"), true).unwrap();
store.forget_sessions(day + 1, &HashSet::from(["still-up"]), "lars").unwrap();
let names: Vec<String> = store.sessions().unwrap().into_iter().map(|s| s.name).collect();
assert_eq!(names, ["recent", "still-up", "terminal"]);
@@ -344,4 +472,150 @@ mod tests {
assert_eq!(store.unread_count("lars/terminal").unwrap(), 1);
assert_eq!(store.held().unwrap().len(), 1);
}
+
+ fn temp_inbox() -> PathBuf {
+ std::env::temp_dir().join(format!("reason-network-inbox-{}", ulid::Ulid::new()))
+ }
+
+ fn with_files(id: &str, to: &str) -> Message {
+ let file = |name: &str, content: &str| Attachment { name: name.into(), content: content.into(), path: None };
+ Message {
+ id: id.into(),
+ from: "sven/claude".into(),
+ from_agent: None,
+ from_repo: None,
+ to: to.into(),
+ via: None,
+ reply_to: None,
+ body: "see the files".into(),
+ wake: true,
+ notify: false,
+ sent_at: 0,
+ approval: None,
+ files: vec![file("notes.md", "# notes\n"), file("run.log", "ok\n")],
+ }
+ }
+
+ /// The files of a message as saved: names, whether each is readable by its user and executable by anyone.
+ fn saved(dir: &Path) -> Vec<(String, String, bool)> {
+ let mut out: Vec<(String, String, bool)> = std::fs::read_dir(dir)
+ .map(|d| {
+ d.flatten()
+ .map(|e| {
+ #[cfg(unix)]
+ let exec =
+ std::os::unix::fs::PermissionsExt::mode(&e.metadata().unwrap().permissions()) & 0o111 != 0;
+ #[cfg(not(unix))]
+ let exec = false;
+ (e.file_name().to_string_lossy().into_owned(), std::fs::read_to_string(e.path()).unwrap(), exec)
+ })
+ .collect()
+ })
+ .unwrap_or_default();
+ out.sort();
+ out
+ }
+
+ fn stored_text(store: &Store, id: &str) -> String {
+ store.db.query_row("SELECT files FROM messages WHERE id = ?1", [id], |r| r.get(0)).unwrap()
+ }
+
+ #[test]
+ fn files_are_saved_with_their_message_and_kept_out_of_the_database() {
+ let inbox = temp_inbox();
+ let store = Store::open(Path::new(":memory:"), inbox.clone()).unwrap();
+ let mut m = with_files("01A", "lars/api");
+ assert!(store.insert(&mut m, false).unwrap());
+ let dir = inbox.join("01A");
+ let want =
+ vec![("notes.md".to_string(), "# notes\n".to_string(), false), ("run.log".into(), "ok\n".into(), false)];
+ assert_eq!(saved(&dir), want);
+ #[cfg(unix)]
+ {
+ use std::os::unix::fs::PermissionsExt;
+ assert_eq!(std::fs::metadata(dir.join("notes.md")).unwrap().permissions().mode() & 0o777, 0o600);
+ assert_eq!(std::fs::metadata(&dir).unwrap().permissions().mode() & 0o777, 0o700);
+ }
+ // The message now lists them by path, here and when read back; the database keeps only their names.
+ let paths: Vec<Option<String>> = m.files.iter().map(|f| f.path.clone()).collect();
+ assert_eq!(
+ paths,
+ [Some(dir.join("notes.md").display().to_string()), Some(dir.join("run.log").display().to_string())]
+ );
+ assert!(m.files.iter().all(|f| f.content.is_empty()));
+ assert_eq!(store.unread("lars/api").unwrap()[0].files, m.files);
+ assert!(!stored_text(&store, "01A").contains("notes\\n"));
+ // A retried delivery changes nothing.
+ std::fs::write(dir.join("run.log"), "kept").unwrap();
+ assert!(!store.insert(&mut with_files("01A", "lars/api"), false).unwrap());
+ assert_eq!(std::fs::read_to_string(dir.join("run.log")).unwrap(), "kept");
+ // An id that isn't plain letters and digits never names a folder.
+ assert!(store.insert(&mut with_files("../../x", "lars/api"), false).is_err());
+ assert!(!inbox.parent().unwrap().join("x").exists());
+ std::fs::remove_dir_all(&inbox).unwrap();
+ }
+
+ #[test]
+ fn a_held_messages_files_are_saved_only_once_approved() {
+ let inbox = temp_inbox();
+ let store = Store::open(Path::new(":memory:"), inbox.clone()).unwrap();
+ store.insert(&mut with_files("01H", "lars/api"), true).unwrap();
+ store.insert(&mut with_files("01D", "lars/api"), true).unwrap();
+ assert!(!inbox.join("01H").exists() && !inbox.join("01D").exists());
+ // Held, the text is kept to save later (and shown to the person by name, with no path).
+ let held = store.held().unwrap();
+ assert_eq!(held[0].files[0].content, "# notes\n");
+ assert_eq!(held[0].files[0].path, None);
+ // Dropped: nothing is ever written.
+ assert_eq!(store.settle_held(&["01D".into()], false).unwrap().len(), 1);
+ assert!(!inbox.join("01D").exists() && store.message("01D").unwrap().is_none());
+ // Approved: saved now, and listed by path.
+ let released = store.settle_held(&["01H".into()], true).unwrap();
+ assert_eq!(saved(&inbox.join("01H")).len(), 2);
+ assert_eq!(released[0].files[0].path, Some(inbox.join("01H").join("notes.md").display().to_string()));
+ assert_eq!(store.unread("lars/api").unwrap()[0].files, released[0].files);
+ assert!(!stored_text(&store, "01H").contains("notes\\n"));
+ std::fs::remove_dir_all(&inbox).unwrap();
+ }
+
+ #[test]
+ fn files_go_with_their_message() {
+ let inbox = temp_inbox();
+ let store = Store::open(Path::new(":memory:"), inbox.clone()).unwrap();
+ let day = 86_400_000;
+ let session = |name: &str| Remembered {
+ name: name.into(),
+ key: name.into(),
+ agent: "claude-code".into(),
+ cwd: "~".into(),
+ repo: None,
+ branch: None,
+ doing: None,
+ tier: Tier::Push,
+ last_seen: 0,
+ };
+ store.upsert_session(&session("gone")).unwrap();
+ store.upsert_session(&session("api")).unwrap();
+ for (id, to) in
+ [("01GONE", "lars/gone"), ("01UNREAD", "lars/api"), ("01RECENT", "lars/api"), ("01OLD", "lars/api")]
+ {
+ store.insert(&mut with_files(id, to), false).unwrap();
+ }
+ std::fs::create_dir_all(inbox.join("01ORPHAN")).unwrap();
+ // The unread mail of a session forgotten after a day goes, and its files with it.
+ store.forget_sessions(day, &HashSet::from(["api"]), "lars").unwrap();
+ assert!(!inbox.join("01GONE").exists());
+ // Read files stay a day; unread ones until read; a folder with no message goes.
+ let now = 10 * day;
+ store.mark_read(&["01RECENT".into()], now - day / 2).unwrap();
+ store.mark_read(&["01OLD".into()], now - day - 1).unwrap();
+ store.sweep_files(now);
+ let left: HashSet<String> = std::fs::read_dir(&inbox)
+ .unwrap()
+ .flatten()
+ .map(|e| e.file_name().to_string_lossy().into_owned())
+ .collect();
+ assert_eq!(left, HashSet::from(["01UNREAD".to_string(), "01RECENT".to_string()]));
+ std::fs::remove_dir_all(&inbox).unwrap();
+ }
}
diff --git a/tests/e2e/two_machines.py b/tests/e2e/two_machines.py
index 91d841d..ade5ed4 100644
--- a/tests/e2e/two_machines.py
+++ b/tests/e2e/two_machines.py
@@ -322,6 +322,49 @@ out = cli("alex", "send", "alice/work-codex", "reply while you wait")
t.join()
check("long-poll returns the message that arrives", "reply while you wait" in waited.get("out", ""), waited)
+# --- small text files ---
+FILES = os.path.join(D, "files")
+os.makedirs(FILES, exist_ok=True)
+def write(name, data):
+ path = os.path.join(FILES, name)
+ with open(path, "wb") as f:
+ f.write(data if isinstance(data, bytes) else data.encode())
+ return path
+def inbox(who):
+ p = os.path.join(H[who], "inbox")
+ return sorted(os.listdir(p)) if os.path.isdir(p) else []
+def saved(out, who):
+ """The file paths a message lists, if each is in `who`'s inbox folder, readable and not executable."""
+ paths = re.findall(r"^- (/.+)$", out, re.M)
+ ok = all(p.startswith(os.path.join(H[who], "inbox") + os.sep) and os.path.isfile(p) and not os.stat(p).st_mode & 0o111
+ for p in paths)
+ return paths if ok else []
+notes = write("notes.md", "# Notes\nline two \u2014 caf\u00e9\n")
+config = write("app.json", '{"port": 8080}\n')
+out = cli("alex", "send", "alice/work-codex", "--file", notes, "--file", config, "the notes and the config (e2e test)")
+check("a send with files is accepted", "→ alice/work-codex" in out and "refused" not in out, out)
+out = codex.tool("check_inbox", {}, meta)
+paths = saved(out, "alice")
+check("files arrive in the message, listed by where they are saved",
+ "Files they sent (text, saved on this computer" in out and [os.path.basename(p) for p in paths] == ["notes.md", "app.json"], out)
+check("…with the same text, readable and not executable",
+ len(paths) == 2 and open(paths[0], encoding="utf-8").read() == "# Notes\nline two \u2014 caf\u00e9\n" and open(paths[1]).read() == '{"port": 8080}\n', paths)
+with open(os.path.join(D, "work", "reply.md"), "w") as f:
+ f.write("relative paths are from the agent's folder\n")
+out = codex.tool("send_message", {"to": "alex/terminal", "message": "a file back (e2e test)", "files": ["reply.md"]}, meta)
+check("an agent sends a file by a path relative to its folder", "→ alex/terminal" in out and "refused" not in out, out)
+out = cli("alex", "listen", "--as", "terminal", "--wait", "5")
+paths = saved(out, "alex")
+check("…and it lands on the other machine", len(paths) == 1 and open(paths[0]).read().startswith("relative paths"), out)
+out = codex.tool("send_message", {"to": "alex/terminal", "message": "x", "files": [write("logo.png", b"\x89PNG\r\n\x1a\n\x00\x00")]}, meta)
+check("a binary file is refused, saying why", "looks like a binary file" in out and "only text files" in out and "→" not in out, out)
+out = codex.tool("send_message", {"to": "alex/terminal", "message": "x", "files": [write("big.log", "x" * (256 * 1024 + 1))]}, meta)
+check("a file over 256 KB is refused", "a file can be at most 256 KB" in out and "→" not in out, out)
+out = codex.tool("send_message", {"to": "alex/terminal", "message": "x", "files": [notes] * 11}, meta)
+check("more than 10 files are refused", "at most 10" in out and "→" not in out, out)
+out = cli("alex", "send", "alice/work-codex", "--file", FILES, "x")
+check("a folder is not a file", "is not a file" in out, out)
+
# --- Codex hooks: session via hooks, delivery via codex queue ---
start = hook("alice", "codex", "session-start", {"session_id": "22222222-bbbb", "cwd": ROOT, "hook_event_name": "SessionStart"})
codex_name = os.path.basename(ROOT).lower() + "-codex"
@@ -344,15 +387,33 @@ cli("alice", "stop")
time.sleep(3)
cli("alice", "team")
time.sleep(5)
-out = cli("alex", "send", "alice/work-codex", "held message")
+before = inbox("alice")
+out = cli("alex", "send", "alice/work-codex", "--file", notes, "held message")
check("message to alice is held by her policy", "held until alice approves" in out, out)
out = codex.tool("check_inbox", {}, meta)
check("held message is not delivered", "held message" not in out, out)
+check("…and its files aren't saved yet", inbox("alice") == before, inbox("alice"))
out = cli("alice", "held")
-check("Reason Network held lists it", "held message" in out, out)
+check("Reason Network held lists it", "held message" in out and "notes.md (saved once approved)" in out, out)
cli("alice", "approve", "--all")
out = codex.tool("check_inbox", {}, meta)
check("approved message is delivered", "held message" in out, out)
+check("…with its files saved now", [os.path.basename(p) for p in saved(out, "alice")] == ["notes.md"], out)
+# Refused: nothing of it is kept.
+text = open(cfg).read()
+open(cfg, "w").write(text.replace('alex = "hold"', 'alex = "refuse"'))
+cli("alice", "stop")
+time.sleep(3)
+cli("alice", "team")
+time.sleep(5)
+before = inbox("alice")
+out = cli("alex", "send", "alice/work-codex", "--file", notes, "refused message")
+check("a refused message's files are never saved", "doesn't accept messages from alex" in out and inbox("alice") == before, out)
+open(cfg, "w").write(text)
+cli("alice", "stop")
+time.sleep(3)
+cli("alice", "team")
+time.sleep(5)
# --- offline machine: outbox ---
codex.close()