Skip to content

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

opencode 2.x: take a session off the network when the plugin unloads

Lars Taliana495fdf73994
diff --git a/src/daemon/mail.rs b/src/daemon/mail.rs
index fb6c38a..c511da1 100644
--- a/src/daemon/mail.rs
+++ b/src/daemon/mail.rs
@@ -1,8 +1,8 @@
//! Sending, receiving and delivering messages.
//!
//! Delivery is at least once: a message stays unread until whoever showed it to the agent confirms
-//! with `Delivered`. Messages handed to a push channel are "in flight" meanwhile, so the inbox and hooks don't
-//! show them a second time.
+//! with `Delivered`. Messages handed to a push channel or a hook are "in flight" meanwhile, so the inbox and hooks
+//! don't show them a second time.
use super::{Daemon, Inner, directory::Target, log};
use crate::{agent, claude, config::Policy, proto::*, render, util};
@@ -20,7 +20,7 @@ const RATE_LIMIT: usize = 60;
/// Agent-to-agent messages allowed between one pair in `LOOP_WINDOW`; past that it is probably a loop.
const LOOP_LIMIT: usize = 12;
const LOOP_WINDOW: Duration = Duration::from_secs(600);
-/// A push not confirmed within this long is shown through the inbox and hooks again.
+/// A push or hook not confirmed within this long is shown through the inbox and hooks again.
const IN_FLIGHT_TTL: Duration = Duration::from_secs(90);
/// How a session on this machine gets a message into its conversation.
@@ -38,7 +38,7 @@ enum Channel {
}
impl Inner {
- /// Unread mail for an address that isn't already on its way through a push.
+ /// Unread mail for an address that isn't already on its way through a push or a hook.
pub(super) fn pending(&mut self, address: &str) -> Result<Vec<Message>> {
self.in_flight.retain(|_, at| at.elapsed() < IN_FLIGHT_TTL);
let msgs = self.store.unread(address)?;
diff --git a/src/daemon/mod.rs b/src/daemon/mod.rs
index 1f38a79..c52f317 100644
--- a/src/daemon/mod.rs
+++ b/src/daemon/mod.rs
@@ -118,7 +118,7 @@ struct Inner {
conns: HashMap<u64, mpsc::UnboundedSender<String>>,
next_conn: u64,
team: Option<TeamState>,
- /// Messages handed to a push channel and not yet confirmed; hidden from inbox and hooks meanwhile.
+ /// Messages handed to a push channel or a hook and not yet confirmed; hidden from inbox and hooks meanwhile.
in_flight: HashMap<String, Instant>,
/// Sessions waiting in `check_inbox` right now (they get new mail there instead of a push).
polling: HashMap<String, usize>,
diff --git a/src/opencode-plugin.ts b/src/opencode-plugin.ts
index 6d66075..a157398 100644
--- a/src/opencode-plugin.ts
+++ b/src/opencode-plugin.ts
@@ -63,6 +63,15 @@ const network = (prompt: (id: string, text: string) => Promise<unknown>) => {
} else idle.delete(id)
},
stop: () => clearInterval(timer),
+ // Off the network: the session is gone, or opencode unloaded this plugin and nothing delivers to it any more.
+ end: (id?: string) => {
+ for (const [s, dir] of [...dirs]) {
+ if (id && s !== id) continue
+ idle.delete(s)
+ dirs.delete(s)
+ void run(["hook", "opencode", "session-end", "--session", s], dir)
+ }
+ },
}
}
@@ -120,11 +129,15 @@ export default {
if (e.type === "session.created" && d.parentID) net.subagent(d.sessionID)
if (e.type === "session.renamed") net.title(d.sessionID, d.title, dir)
if (STATE[e.type]) void net.status(d.sessionID, STATE[e.type], dir)
+ if (e.type === "session.deleted") net.end(d.sessionID)
}
})().catch(() => {})
+ // opencode unloads a folder's plugins when it has been idle a while; its background server stays up, so the
+ // daemon can't tell its sessions went away.
return () => {
abort.abort()
net.stop()
+ net.end()
}
},
}