Skip to content

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

refactor(attachments): introduce native staged lease mechanism

Steven Enamakeld142ccf8656f
diff --git a/crates/openhuman-core/src/agent/queued_turn.rs b/crates/openhuman-core/src/agent/queued_turn.rs
index bbd39776d7..b5e997380f 100644
--- a/crates/openhuman-core/src/agent/queued_turn.rs
+++ b/crates/openhuman-core/src/agent/queued_turn.rs
@@ -59,6 +59,9 @@ pub struct QueuedPayload {
/// Host-created staged refs authorized for this exact external turn.
/// Requeued synthetic placeholders never inherit this grant.
pub(crate) native_staged_parts: Vec<tinychannels_bus::ContentPart>,
+ /// Final-owner cleanup for uncommitted native refs, shared by all queue
+ /// clones and disarmed only after durable claim persistence.
+ pub(crate) native_staged_lease: Option<crate::threads::attachments::NativeStagedLease>,
pub(crate) native_message_id: Option<String>,
/// A preparation failure is sent through the shared runner's terminal
/// path without invoking the model.
@@ -85,6 +88,7 @@ impl std::fmt::Debug for QueuedPayload {
&self.native_message_input.is_some(),
)
.field("native_staged_parts", &self.native_staged_parts.len())
+ .field("has_native_staged_lease", &self.native_staged_lease.is_some())
.field("has_native_preparation_error", &self.native_preparation_error.is_some())
.finish_non_exhaustive()
}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs
index 2f98ae36f3..4fdc3d6a51 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/native_runner.rs
@@ -184,6 +184,7 @@ impl TurnHost<QueuedPayload> for NativeHost {
message: request.text.clone(),
message_input: request.payload.native_message_input.clone(),
staged_parts: request.payload.native_staged_parts.clone(),
+ staged_lease: request.payload.native_staged_lease.clone(),
staged_message_id: request.payload.native_message_id.clone(),
origin: trusted.origin.clone(),
progress: Some(agent_progress),
@@ -575,9 +576,14 @@ pub(crate) async fn run(
}
};
runtime.shared.begin_delivery_wait(&request_id);
- let cleanup_staged_parts = staged_parts.clone();
- let cleanup_config = trusted.host_agent.config.clone();
- let cleanup_thread_id = thread_id.clone();
+ let staged_lease = (!staged_parts.is_empty()).then(|| {
+ crate::threads::attachments::NativeStagedLease::new(
+ dispatch.clone(),
+ thread_id.clone(),
+ trusted.host_agent.config.clone(),
+ staged_parts.clone(),
+ )
+ });
let request = TurnRequest {
scope: ScopeKey {
agent_id: trusted.host_agent.definition.id.clone(),
@@ -591,6 +597,7 @@ pub(crate) async fn run(
trusted_turn: Some(trusted),
native_message_input: message_input,
native_staged_parts: staged_parts,
+ native_staged_lease: staged_lease.clone(),
native_message_id: staged_message_id,
native_preparation_error: preparation_error,
..Default::default()
@@ -606,14 +613,9 @@ pub(crate) async fn run(
Err(tinyagents_harness::turn_runner::TurnRunnerError::Queued)
);
let result = runner_result.map_err(|error| error.to_string());
- if !queued && result.is_err() && !cleanup_staged_parts.is_empty() {
- if let Err(error) = crate::threads::attachments::discard_native_channel_parts(
- dispatch,
- &cleanup_thread_id,
- &cleanup_config,
- &cleanup_staged_parts,
- )
- .await
+ if !queued && result.is_err() {
+ if let Some(staged_lease) = staged_lease.as_ref()
+ && staged_lease.discard().await.is_err()
{
tracing::warn!("[channels::dispatch] failed to clean rejected native media staging");
}
diff --git a/crates/openhuman-core/src/threads/attachments.rs b/crates/openhuman-core/src/threads/attachments.rs
index 94305e9f50..ecf7d3ce60 100644
--- a/crates/openhuman-core/src/threads/attachments.rs
+++ b/crates/openhuman-core/src/threads/attachments.rs
@@ -21,7 +21,7 @@ use staged::*;
pub(crate) use staged::{
bind_staged_refs, commit_native_channel_parts, consume_staged_parts, discard_native_channel_parts,
discard_staged_parts,
- native_parts_in_committed_message, stage_native_channel_part, stage_queued_parts,
+ native_parts_in_committed_message, NativeStagedLease, stage_native_channel_part, stage_queued_parts,
read_authorized_native_attachment, validate_opaque_parts,
};
diff --git a/crates/openhuman-core/src/threads/attachments/staged.rs b/crates/openhuman-core/src/threads/attachments/staged.rs
index d6f0f44d38..e5ab2d4f05 100644
--- a/crates/openhuman-core/src/threads/attachments/staged.rs
+++ b/crates/openhuman-core/src/threads/attachments/staged.rs
@@ -1,5 +1,6 @@
use super::*;
use crate::storage::documents::compare_and_swap;
+use std::sync::atomic::{AtomicU8, Ordering};
use tinystoragedrivers::CollectionSpec;
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -34,6 +35,101 @@ pub(super) const STAGED_DOMAIN: &str = "threads::attachments";
pub(super) const STAGED_COLLECTION: &str = crate::threads::store::STAGED_ATTACHMENTS;
pub(super) const STAGED_TTL_SECS: i64 = 7 * 24 * 60 * 60;
+const NATIVE_STAGED_ACTIVE: u8 = 0;
+const NATIVE_STAGED_COMMITTED: u8 = 1;
+const NATIVE_STAGED_DISCARDED: u8 = 2;
+
+/// Owns staged refs from one native request until the durable transcript claim
+/// succeeds or every queued request clone is dropped.
+#[derive(Clone)]
+pub(crate) struct NativeStagedLease(std::sync::Arc<NativeStagedLeaseState>);
+
+struct NativeStagedLeaseState {
+ dispatch: crate::core::runtime::DispatchCtx,
+ thread_id: String,
+ config: crate::config::Config,
+ parts: Vec<ContentPart>,
+ state: AtomicU8,
+}
+
+impl NativeStagedLease {
+ pub(crate) fn new(
+ dispatch: crate::core::runtime::DispatchCtx,
+ thread_id: String,
+ config: crate::config::Config,
+ parts: Vec<ContentPart>,
+ ) -> Self {
+ Self(std::sync::Arc::new(NativeStagedLeaseState {
+ dispatch,
+ thread_id,
+ config,
+ parts,
+ state: AtomicU8::new(NATIVE_STAGED_ACTIVE),
+ }))
+ }
+
+ pub(crate) fn mark_committed(&self) {
+ let _ = self.0.state.compare_exchange(
+ NATIVE_STAGED_ACTIVE,
+ NATIVE_STAGED_COMMITTED,
+ Ordering::AcqRel,
+ Ordering::Acquire,
+ );
+ }
+
+ pub(crate) async fn discard(&self) -> Result<(), String> {
+ if self
+ .0
+ .state
+ .compare_exchange(
+ NATIVE_STAGED_ACTIVE,
+ NATIVE_STAGED_DISCARDED,
+ Ordering::AcqRel,
+ Ordering::Acquire,
+ )
+ .is_err()
+ {
+ return Ok(());
+ }
+ discard_native_channel_parts(
+ &self.0.dispatch,
+ &self.0.thread_id,
+ &self.0.config,
+ &self.0.parts,
+ )
+ .await
+ }
+}
+
+impl Drop for NativeStagedLeaseState {
+ fn drop(&mut self) {
+ if self
+ .state
+ .compare_exchange(
+ NATIVE_STAGED_ACTIVE,
+ NATIVE_STAGED_DISCARDED,
+ Ordering::AcqRel,
+ Ordering::Acquire,
+ )
+ .is_err()
+ {
+ return;
+ }
+ let dispatch = self.dispatch.clone();
+ let thread_id = self.thread_id.clone();
+ let config = self.config.clone();
+ let parts = self.parts.clone();
+ dispatch.clone().spawn(async move {
+ if discard_native_channel_parts(&dispatch, &thread_id, &config, &parts)
+ .await
+ .is_err()
+ {
+ tracing::warn!("[threads] queued native attachment cleanup failed");
+ }
+ });
+ }
+}
+
pub(super) fn staged_doc_id(opaque_id: &str) -> Result<String, String> {
let uuid = uuid::Uuid::parse_str(opaque_id)
.map_err(|_| "staged attachment reference is invalid".to_string())?;