Skip to content

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

fix(threads): make channel projection binding atomic

Steven Enamakelc3cde2896dcc
diff --git a/crates/openhuman-core/src/threads/channel_projection.rs b/crates/openhuman-core/src/threads/channel_projection.rs
index b27b0d3a11..89d353f5b5 100644
--- a/crates/openhuman-core/src/threads/channel_projection.rs
+++ b/crates/openhuman-core/src/threads/channel_projection.rs
@@ -15,7 +15,7 @@ use tinychannels_bus::{ChannelRef, ConversationRef};
use tinystoragedrivers::{CollectionSpec, ErrorKind, Precondition};
use crate::core::runtime::{DispatchCtx, Tenant, tenant::tenant_key};
-use crate::storage::{self, documents::Repo};
+use crate::storage::{self, documents::{Repo, compare_and_swap}};
const DOMAIN: &str = "threads::channel_projection";
const COLLECTION: &str = "thread_channel_messages";
@@ -296,13 +296,11 @@ fn bind_row_in(repo: &Repo, key: MessageBindingKey, thread_id: &str, row_id: &st
let id = doc_id(&key)?;
let (thread_id, row_id) = (thread_id.to_string(), row_id.to_string());
repo.run(|docs| async move {
- let stored = docs.get(COLLECTION, &id).await?;
- let Some(stored) = stored else {
+ let Some(stored) = docs.get(COLLECTION, &id).await? else {
return Err(tinystoragedrivers::StorageError::not_found(
"provider message has no trusted thread binding",
));
};
- let precondition = stored.unchanged();
let mut binding: MessageBinding = serde_json::from_value(stored.doc)
.map_err(|e| tinystoragedrivers::StorageError::invalid_input(e.to_string()))?;
if binding.key != key || binding.thread_id != thread_id {
@@ -310,20 +308,56 @@ fn bind_row_in(repo: &Repo, key: MessageBindingKey, thread_id: &str, row_id: &st
"provider message thread binding does not match persisted row",
));
}
- if binding
- .row_id
- .as_deref()
- .is_some_and(|existing| existing != row_id)
+ if let Some(existing) = binding.row_id.as_deref() {
+ return if existing == row_id {
+ Ok(())
+ } else {
+ Err(tinystoragedrivers::StorageError::conflict(
+ "provider message is already bound to another OpenHuman row",
+ ))
+ };
+ }
+
+ let expected_key = key.clone();
+ let expected_thread = thread_id.clone();
+ let expected_row = row_id.clone();
+ let changed = compare_and_swap(&docs, COLLECTION, &id, |doc| {
+ let mut current: MessageBinding = serde_json::from_value(doc.clone()).ok()?;
+ if current.key != expected_key || current.thread_id != expected_thread {
+ return None;
+ }
+ match current.row_id.as_deref() {
+ Some(existing) if existing == expected_row => None,
+ Some(_) => None,
+ None => {
+ current.row_id = Some(expected_row.clone());
+ serde_json::to_value(current).ok()
+ }
+ }
+ })
+ .await?;
+ if changed.is_some() {
+ return Ok(());
+ }
+
+ // A competing identical bind is success; any other state fails closed.
+ let stored = docs.get(COLLECTION, &id).await?.ok_or_else(|| {
+ tinystoragedrivers::StorageError::not_found(
+ "provider message has no trusted thread binding",
+ )
+ })?;
+ let current: MessageBinding = serde_json::from_value(stored.doc)
+ .map_err(|e| tinystoragedrivers::StorageError::invalid_input(e.to_string()))?;
+ if current.key == key
+ && current.thread_id == thread_id
+ && current.row_id.as_deref() == Some(row_id.as_str())
{
- return Err(tinystoragedrivers::StorageError::conflict(
+ Ok(())
+ } else {
+ Err(tinystoragedrivers::StorageError::conflict(
"provider message is already bound to another OpenHuman row",
- ));
+ ))
}
- binding.row_id = Some(row_id);
- let body: Value = serde_json::to_value(binding)
- .map_err(|e| tinystoragedrivers::StorageError::invalid_input(e.to_string()))?;
- docs.put(COLLECTION, &id, json!(body), precondition).await?;
- Ok(())
})?;
Ok(())
}