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 store for thread message delivery

Steven Enamakele46f80c5cebf
diff --git a/crates/openhuman-core/src/threads/store/bus.rs b/crates/openhuman-core/src/threads/store/bus.rs
index 7f24a38884..2ba0a2720b 100644
--- a/crates/openhuman-core/src/threads/store/bus.rs
+++ b/crates/openhuman-core/src/threads/store/bus.rs
@@ -83,36 +83,58 @@ 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();
+/// `(channel, message_id)`, oldest first, at most `capacity` long.
+#[derive(Debug)]
+struct ClaimedTurns {
+ capacity: usize,
+ keys: std::collections::VecDeque<(String, String)>,
+}
+
+impl ClaimedTurns {
+ fn new(capacity: usize) -> Self {
+ Self {
+ capacity,
+ keys: std::collections::VecDeque::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()))
+ fn claim(&mut self, channel: &str, message_id: &str) {
+ if self.contains(channel, message_id) {
+ return;
+ }
+ if self.keys.len() >= self.capacity {
+ self.keys.pop_front();
+ }
+ self.keys
+ .push_back((channel.to_string(), message_id.to_string()));
+ }
+
+ fn contains(&self, channel: &str, message_id: &str) -> bool {
+ self.keys
+ .iter()
+ .any(|(c, m)| c == channel && m == message_id)
+ }
+}
+
+static CLAIMED_TURNS: OnceLock<std::sync::Mutex<ClaimedTurns>> = OnceLock::new();
+
+fn claimed_turns() -> std::sync::MutexGuard<'static, ClaimedTurns> {
+ CLAIMED_TURNS
+ .get_or_init(|| std::sync::Mutex::new(ClaimedTurns::new(CLAIMED_TURNS_CAPACITY)))
+ .lock()
+ .unwrap_or_else(|e| e.into_inner())
}
/// 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);
+ claimed_turns().claim(channel, message_id);
}
/// 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)
+ claimed_turns().contains(channel, message_id)
}
pub struct ConversationPersistenceSubscriber {
diff --git a/crates/openhuman-core/src/threads/store/bus_tests.rs b/crates/openhuman-core/src/threads/store/bus_tests.rs
index b11fd2c72c..9e49e7bd21 100644
--- a/crates/openhuman-core/src/threads/store/bus_tests.rs
+++ b/crates/openhuman-core/src/threads/store/bus_tests.rs
@@ -530,12 +530,15 @@ async fn claimed_channel_turns_are_left_to_their_caller() {
#[test]
fn the_claim_list_is_bounded() {
- for i in 0..(CLAIMED_TURNS_CAPACITY + 10) {
- claim_channel_turn("relay-bound-test", &format!("m{i}"));
+ let mut claimed = ClaimedTurns::new(3);
+ for i in 0..5 {
+ claimed.claim("telegram", &format!("m{i}"));
}
- assert!(!is_claimed_channel_turn("relay-bound-test", "m0"), "oldest evicted");
- assert!(is_claimed_channel_turn(
- "relay-bound-test",
- &format!("m{}", CLAIMED_TURNS_CAPACITY + 9)
- ));
+ claimed.claim("telegram", "m4");
+ assert!(!claimed.contains("telegram", "m0"), "oldest evicted");
+ assert!(!claimed.contains("telegram", "m1"), "oldest evicted");
+ assert!(claimed.contains("telegram", "m2"));
+ assert!(claimed.contains("telegram", "m4"));
+ assert!(!claimed.contains("discord", "m4"), "keyed by channel too");
+ assert_eq!(claimed.keys.len(), 3, "a repeated claim is not stored twice");
}