Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(threads): add bus module to thread store

Steven Enamakel968cc66fec6b
diff --git a/crates/openhuman-core/src/threads/store/bus.rs b/crates/openhuman-core/src/threads/store/bus.rs
index f770fc72bd..7f24a38884 100644
--- a/crates/openhuman-core/src/threads/store/bus.rs
+++ b/crates/openhuman-core/src/threads/store/bus.rs
@@ -77,6 +77,44 @@ pub fn register_conversation_persistence_subscriber(workspace_dir: PathBuf) {
}
}
+/// How many claimed channel turns are remembered. The subscriber sees an
+/// event within moments of its publication, so a short FIFO is plenty.
+const CLAIMED_TURNS_CAPACITY: usize = 1024;
+
+/// Channel turns whose caller persists them itself (the hosted-channel relay,
+/// which writes under the caller's own scope and thread id). Keyed by
+/// `(channel, message_id)`, oldest first.
+static CLAIMED_TURNS: OnceLock<std::sync::Mutex<std::collections::VecDeque<(String, String)>>> =
+ OnceLock::new();
+
+fn claimed_turns() -> &'static std::sync::Mutex<std::collections::VecDeque<(String, String)>> {
+ CLAIMED_TURNS.get_or_init(|| std::sync::Mutex::new(std::collections::VecDeque::new()))
+}
+
+/// Mark the channel message `(channel, message_id)` as persisted by its
+/// caller, so this subscriber does not mirror it a second time under the
+/// listener-derived thread id.
+pub(crate) fn claim_channel_turn(channel: &str, message_id: &str) {
+ let mut claimed = claimed_turns().lock().unwrap_or_else(|e| e.into_inner());
+ let key = (channel.to_string(), message_id.to_string());
+ if claimed.contains(&key) {
+ return;
+ }
+ if claimed.len() >= CLAIMED_TURNS_CAPACITY {
+ claimed.pop_front();
+ }
+ claimed.push_back(key);
+}
+
+/// Whether `(channel, message_id)` was claimed by [`claim_channel_turn`].
+pub(crate) fn is_claimed_channel_turn(channel: &str, message_id: &str) -> bool {
+ claimed_turns()
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
+ .iter()
+ .any(|(c, m)| c == channel && m == message_id)
+}
+
pub struct ConversationPersistenceSubscriber {
workspace_dir: Arc<RwLock<PathBuf>>,
}
@@ -111,6 +149,24 @@ impl EventHandler<DomainEvent> for ConversationPersistenceSubscriber {
}
async fn handle(&self, event: &DomainEvent) {
+ if let DomainEvent::ChannelMessageReceived {
+ channel,
+ message_id,
+ ..
+ }
+ | DomainEvent::ChannelMessageProcessed {
+ channel,
+ message_id,
+ ..
+ } = event
+ {
+ if is_claimed_channel_turn(channel, message_id) {
+ log::debug!(
+ "{LOG_PREFIX} skipping channel turn persisted by its caller channel={channel}"
+ );
+ return;
+ }
+ }
match event {
DomainEvent::ChannelMessageReceived {
channel,
diff --git a/crates/openhuman-core/src/threads/store/mod.rs b/crates/openhuman-core/src/threads/store/mod.rs
index 34cf18b092..41df175466 100644
--- a/crates/openhuman-core/src/threads/store/mod.rs
+++ b/crates/openhuman-core/src/threads/store/mod.rs
@@ -32,6 +32,7 @@ mod bus;
use tinyagents_session::threads as store;
pub use bus::register_conversation_persistence_subscriber;
+pub(crate) use bus::claim_channel_turn;
pub use store::{
append_message, delete_messages_from, delete_thread, ensure_thread, get_messages,
is_deterministic_message_id, list_threads, purge_threads, reply_run_id, run_reply_message_id,