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
Forget sessions gone for a day, with the mail they never read
Lars Talian24d559b5738e
diff --git a/DESIGN.md b/DESIGN.mdindex b4f22bb..f29cb6c 100644--- a/DESIGN.md+++ b/DESIGN.md@@ -127,7 +127,8 @@ that machine offline (sessions shown with last-seen).machine on ALPN `tincan/1`, length-prefixed JSON frames. The receiver persists, then acks with`stored | delivered | held | refused`; later receipts (`delivered`, `read`) flow back. The sender keeps anoutbox and retries when the target machine comes back. Mail for an offline session waits in the target-machine's mailbox under the session name.+machine's mailbox under the session name, for a day: a session gone longer is forgotten, with the mail it never+read.Later (M4): an **anchor** is just a member daemon on an always-on box (VPS, Pi) with `role=anchor` thatalso holds mail for offline machines and keeps searchable team history. Same binary, no special server.diff --git a/src/daemon/sessions.rs b/src/daemon/sessions.rsindex ea1d549..91a7b3e 100644--- a/src/daemon/sessions.rs+++ b/src/daemon/sessions.rs@@ -8,6 +8,8 @@ use std::{collections::HashSet, sync::Arc, time::Instant};/// A hook-only session that hasn't fired a hook for this long is considered gone.const HOOK_TTL_MS: i64 = 6 * 3_600_000;+/// A session gone this long isn't coming back: it is forgotten, with the mail it never read.+const FORGET_MS: i64 = 24 * 3_600_000;impl Daemon {/// How fast hooks let us reach an agent's session (`push_failed`: its push channel didn't work).@@ -277,6 +279,8 @@ impl Daemon {let _ = g.store.touch_session(&l.name, now);}}+ 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);}}diff --git a/src/main.rs b/src/main.rsindex f68300e..b3e5b9d 100644--- a/src/main.rs+++ b/src/main.rs@@ -119,7 +119,7 @@ enum Cmd {Who {/// Only sessions matching this textfilter: Option<String>,- /// Include offline sessions and machines+ /// Include sessions that went offline in the last day, and offline machines#[arg(long)]all: bool,#[arg(long)]diff --git a/src/mcp.rs b/src/mcp.rsindex d61228e..371480a 100644--- a/src/mcp.rs+++ b/src/mcp.rs@@ -41,7 +41,7 @@ pub struct ListArgs {/// Only sessions matching this text (person, session, agent, repo, branch, directory or what it's doing).#[serde(default)]pub filter: Option<String>,- /// Also list sessions that are offline.+ /// Also list sessions that went offline in the last day.#[serde(default)]pub all: Option<bool>,}diff --git a/src/store.rs b/src/store.rsindex 18e09fa..9d06f5f 100644--- a/src/store.rs+++ b/src/store.rs@@ -4,7 +4,7 @@use crate::proto::{Message, Tier};use anyhow::Result;use rusqlite::{Connection, OptionalExtension, Row, params};-use std::path::Path;+use std::{collections::HashSet, path::Path};pub struct Store {db: Connection,@@ -226,6 +226,21 @@ impl Store {Ok(())}+ /// Forgets the sessions not seen since `before`, other than `live` ones, and deletes the mail they never read, so a+ /// session that takes the name later doesn't get it.+ pub fn forget_sessions(&self, before: i64, live: &HashSet<&str>, person: &str) -> Result<()> {+ let mut st = self.db.prepare_cached("SELECT name FROM sessions WHERE last_seen < ?1")?;+ 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",+ [format!("{person}/{name}")],+ )?;+ self.db.execute("DELETE FROM sessions WHERE name = ?1", [name])?;+ }+ Ok(())+ }+pub fn session_by_key(&self, key: &str) -> Result<Option<Remembered>> {let sql = format!("SELECT {SESSION_COLS} FROM sessions WHERE key = ?1 ORDER BY last_seen DESC LIMIT 1");Ok(self.db.query_row(&sql, [key], remembered).optional()?)@@ -273,3 +288,49 @@ fn remembered(r: &Row) -> rusqlite::Result<Remembered> {last_seen: r.get(8)?,})}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn a_session_gone_a_day_is_forgotten_with_the_mail_it_never_read() {+ let store = Store::open(Path::new(":memory:")).unwrap();+ let day = 86_400_000;+ let session = |name: &str, last_seen: i64| Remembered {+ name: name.into(),+ key: name.into(),+ agent: "claude-code".into(),+ cwd: "~".into(),+ repo: None,+ branch: None,+ doing: None,+ tier: Tier::Push,+ last_seen,+ };+ for s in [session("gone", 0), session("recent", 2 * day), session("still-up", 0)] {+ store.upsert_session(&s).unwrap();+ }+ let to = |id: &str, name: &str| Message {+ id: id.into(),+ from: "sven/claude".into(),+ from_agent: None,+ from_repo: None,+ to: format!("lars/{name}"),+ via: None,+ reply_to: None,+ body: "hi".into(),+ wake: false,+ notify: false,+ sent_at: 0,+ approval: None,+ };+ store.insert(&to("1", "gone"), false).unwrap();+ store.insert(&to("2", "recent"), false).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"]);+ assert_eq!(store.unread_count("lars/gone").unwrap(), 0);+ assert_eq!(store.unread_count("lars/recent").unwrap(), 1);+ }+}