The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.
Rust agent-orchestrationai-agentsai-assistantdesktopllmlocal-firstmcppersonal-aiprivacyrustsecond-braintauri
refactor(attachments): introduce cleanup reading functions
Steven Enamakel6f2c35784d1e
diff --git a/crates/openhuman-core/src/threads/attachments/staged.rs b/crates/openhuman-core/src/threads/attachments/staged.rsindex 96ce19b4eb..3d7d6f79ea 100644--- a/crates/openhuman-core/src/threads/attachments/staged.rs+++ b/crates/openhuman-core/src/threads/attachments/staged.rs@@ -80,6 +80,25 @@ pub(super) async fn read_staged_entries(thread_id: &str,requested: &[ContentPart],config: &crate::config::Config,+) -> Result<Vec<StoredAttachment>, String> {+ read_staged_entries_with_canceled_cleanup(ctx, thread_id, requested, config, false).await+}++pub(super) async fn read_staged_entries_for_cleanup(+ ctx: &crate::core::runtime::DispatchCtx,+ thread_id: &str,+ requested: &[ContentPart],+ config: &crate::config::Config,+) -> Result<Vec<StoredAttachment>, String> {+ read_staged_entries_with_canceled_cleanup(ctx, thread_id, requested, config, true).await+}++async fn read_staged_entries_with_canceled_cleanup(+ ctx: &crate::core::runtime::DispatchCtx,+ thread_id: &str,+ requested: &[ContentPart],+ config: &crate::config::Config,+ allow_canceled_cleanup: bool,) -> Result<Vec<StoredAttachment>, String> {validate_opaque_parts(requested)?;let tenant = tenant_for(ctx)?;@@ -97,10 +116,11 @@ pub(super) async fn read_staged_entries(}if manifest.expires_at < chrono::Utc::now().timestamp()&& manifest.claim.as_ref().is_none_or(|claim| claim.canceled)+ && !allow_canceled_cleanup{return Err("staged attachment is missing or expired".into());}- if manifest.claim.as_ref().is_some_and(|claim| claim.canceled) {+ if manifest.claim.as_ref().is_some_and(|claim| claim.canceled) && !allow_canceled_cleanup {return Err("staged attachment was canceled".into());}if &manifest.attachment.part != part {diff --git a/crates/openhuman-core/src/threads/attachments/staged/native.rs b/crates/openhuman-core/src/threads/attachments/staged/native.rsindex 85f215ad94..7e9f122a54 100644--- a/crates/openhuman-core/src/threads/attachments/staged/native.rs+++ b/crates/openhuman-core/src/threads/attachments/staged/native.rs@@ -261,7 +261,7 @@ pub(crate) async fn discard_native_channel_parts(if parts.is_empty() {return Ok(());}- let entries = read_staged_entries(ctx, thread_id, parts, config).await?;+ let entries = read_staged_entries_for_cleanup(ctx, thread_id, parts, config).await?;discard_staged_entries(ctx, thread_id, config, entries).await}diff --git a/crates/openhuman-core/src/threads/attachments_staged_tests.rs b/crates/openhuman-core/src/threads/attachments_staged_tests.rsnew file mode 100644index 0000000000..d1e7f2eba4--- /dev/null+++ b/crates/openhuman-core/src/threads/attachments_staged_tests.rs@@ -0,0 +1,301 @@+use super::*;++#[tokio::test]+async fn native_staged_lease_cleans_dropped_queue_and_preserves_committed_claims() {+ let dir = tempfile::tempdir().unwrap();+ let mut config = crate::config::Config::default();+ config.workspace_dir = dir.path().join("workspace");+ config.action_dir = dir.path().join("actions");+ let ctx = crate::core::runtime::DispatchCtx::ambient();+ let thread_id = "telegram:account-a:conversation-42";+ let workspace = crate::agent::attachments::thread_action_dir(+ &ctx,+ &config,+ &config.workspace_dir,+ Some(thread_id),+ )+ .await+ .unwrap();+ tokio::fs::create_dir_all(&workspace).await.unwrap();+ let repo = test_repo(&config.workspace_dir);++ let pending_id = uuid::Uuid::new_v4().simple().to_string();+ let pending = image(MediaReference {+ opaque_id: Some(pending_id.clone()),+ content_type: Some("image/png".into()),+ ..Default::default()+ });+ let pending_path = "pending.png";+ let pending_file = workspace.join(pending_path);+ tokio::fs::write(&pending_file, b"pending bytes")+ .await+ .unwrap();+ let mut pending_record = staged_record(pending.clone(), pending_path);+ pending_record.thread_id = thread_id.into();+ insert_record(&repo, &pending_record);+ let lease = NativeStagedLease::new(+ ctx.clone(),+ thread_id.into(),+ config.clone(),+ vec![pending.clone()],+ );+ let queued_clone = lease.clone();+ drop(lease);+ assert!(+ get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())+ .unwrap()+ .unwrap()+ .claim+ .is_none()+ );+ drop(queued_clone);+ tokio::time::timeout(std::time::Duration::from_secs(2), async {+ loop {+ let record = get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())+ .unwrap()+ .unwrap();+ if record.claim.is_some_and(|claim| claim.canceled) && !pending_file.exists() {+ break;+ }+ tokio::task::yield_now().await;+ }+ })+ .await+ .expect("dropping the final queued payload clone should discard its staged ref");+ assert!(!pending_file.exists());++ let removed_id = uuid::Uuid::new_v4().simple().to_string();+ let removed = image(MediaReference {+ opaque_id: Some(removed_id.clone()),+ content_type: Some("image/png".into()),+ ..Default::default()+ });+ let removed_path = "removed-from-queue.png";+ let removed_file = workspace.join(removed_path);+ tokio::fs::write(&removed_file, b"removed bytes")+ .await+ .unwrap();+ let mut removed_record = staged_record(removed.clone(), removed_path);+ removed_record.thread_id = thread_id.into();+ insert_record(&repo, &removed_record);+ let queue_lease = NativeStagedLease::new(+ ctx.clone(),+ thread_id.into(),+ config.clone(),+ vec![removed.clone()],+ );+ let queue = tinyagents_harness::run_queue::RunQueue::new();+ let queued = tinyagents_harness::turn_runner::TurnRequest {+ scope: tinyagents_harness::turn_runner::ScopeKey {+ agent_id: "orchestrator".into(),+ profile_id: None,+ },+ thread_id: thread_id.into(),+ request_id: "remove-native-media".into(),+ text: "queued image".into(),+ payload: crate::agent::queued_turn::QueuedPayload {+ native_staged_lease: Some(queue_lease),+ ..Default::default()+ },+ host_authored: false,+ disposition: tinyagents_harness::turn_runner::QueueDisposition::Followup,+ deadline: None,+ idle_timeout: None,+ };+ queue+ .push(tinyagents_harness::run_queue::QueueLane::Followup, queued)+ .await;+ assert_eq!(+ queue+ .remove_where(|request| request.request_id == "remove-native-media")+ .await,+ 1+ );+ tokio::time::timeout(std::time::Duration::from_secs(2), async {+ loop {+ let record = get_staged_record(&repo, &staged_doc_id(&removed_id).unwrap())+ .unwrap()+ .unwrap();+ if record.claim.is_some_and(|claim| claim.canceled) && !removed_file.exists() {+ break;+ }+ tokio::task::yield_now().await;+ }+ })+ .await+ .expect("removing the queued request should discard its staged media");+ assert!(!removed_file.exists());++ let committed_id = uuid::Uuid::new_v4().simple().to_string();+ let committed = image(MediaReference {+ opaque_id: Some(committed_id.clone()),+ content_type: Some("image/png".into()),+ ..Default::default()+ });+ let committed_path = "committed.png";+ let committed_file = workspace.join(committed_path);+ tokio::fs::write(&committed_file, b"committed bytes")+ .await+ .unwrap();+ let mut committed_record = staged_record(committed.clone(), committed_path);+ committed_record.thread_id = thread_id.into();+ insert_record(&repo, &committed_record);+ let durable_claim = claim("user:request", committed.clone(), false);+ claim_staged_record(+ &repo,+ &durable_claim.tenant,+ thread_id,+ &staged_doc_id(&committed_id).unwrap(),+ &committed,+ &durable_claim,+ )+ .unwrap();+ let committed_lease =+ NativeStagedLease::new(ctx, thread_id.into(), config, vec![committed.clone()]);+ let queue_clone = committed_lease.clone();+ committed_lease.mark_committed();+ drop(committed_lease);+ drop(queue_clone);+ tokio::task::yield_now().await;+ let stored = get_staged_record(&repo, &staged_doc_id(&committed_id).unwrap())+ .unwrap()+ .unwrap();+ assert_eq!(stored.claim, Some(durable_claim));+ assert_eq!(+ tokio::fs::read(committed_file).await.unwrap(),+ b"committed bytes"+ );+}++#[tokio::test]+async fn native_staged_lease_retries_cleanup_for_its_canceled_tombstone() {+ let dir = tempfile::tempdir().unwrap();+ let mut config = crate::config::Config::default();+ config.workspace_dir = dir.path().join("workspace");+ config.action_dir = dir.path().join("actions");+ let ctx = crate::core::runtime::DispatchCtx::ambient();+ let thread_id = "telegram:account-a:conversation-canceled";+ let workspace = crate::agent::attachments::thread_action_dir(+ &ctx,+ &config,+ &config.workspace_dir,+ Some(thread_id),+ )+ .await+ .unwrap();+ tokio::fs::create_dir_all(&workspace).await.unwrap();+ let repo = test_repo(&config.workspace_dir);+ let opaque_id = uuid::Uuid::new_v4().simple().to_string();+ let part = image(MediaReference {+ opaque_id: Some(opaque_id.clone()),+ content_type: Some("image/png".into()),+ ..Default::default()+ });+ let path = "canceled-before-retry.png";+ let file = workspace.join(path);+ tokio::fs::write(&file, b"canceled cleanup bytes")+ .await+ .unwrap();+ let mut record = staged_record(part.clone(), path);+ record.thread_id = thread_id.into();+ record.claim = Some(StagedAttachmentClaim {+ tenant: crate::core::runtime::Tenant::default(),+ thread_id: thread_id.into(),+ message_id: String::new(),+ part: part.clone(),+ canceled: true,+ });+ insert_record(&repo, &record);++ drop(NativeStagedLease::new(+ ctx,+ thread_id.into(),+ config,+ vec![part],+ ));+ tokio::time::timeout(std::time::Duration::from_secs(2), async {+ while file.exists() {+ tokio::task::yield_now().await;+ }+ })+ .await+ .expect("dropping the lease should retry cleanup after its exact canceled tombstone");+}++#[tokio::test]+async fn canceling_native_media_resolution_cleans_the_first_staged_part() {+ let dir = tempfile::tempdir().unwrap();+ let mut config = crate::config::Config::default();+ config.workspace_dir = dir.path().join("workspace");+ config.action_dir = dir.path().join("actions");+ let ctx = crate::core::runtime::DispatchCtx::ambient();+ let thread_id = "telegram:account-a:conversation-42";+ let (staged_tx, staged_rx) = tokio::sync::oneshot::channel();+ let task_ctx = ctx.clone();+ let task_config = config.clone();+ let task = tokio::spawn(async move {+ let lease = NativeStagedLease::new(+ task_ctx.clone(),+ thread_id.into(),+ task_config.clone(),+ Vec::new(),+ );+ let source = image(MediaReference {+ content_type: Some("image/png".into()),+ kind: tinychannels_bus::MediaKind::Image,+ ..Default::default()+ });+ let staged = crate::threads::attachments::stage_native_channel_part(+ &task_ctx,+ thread_id,+ &task_config,+ &source,+ "first-native.png",+ "image/png",+ b"cancellation lifecycle fixture",+ &lease,+ )+ .await+ .unwrap();+ staged_tx.send(staged).unwrap();+ std::future::pending::<()>().await;+ });+ let staged = staged_rx.await.unwrap();+ let opaque_id = opaque_media(&staged)+ .and_then(|media| media.opaque_id.as_deref())+ .unwrap()+ .to_owned();+ let workspace = crate::agent::attachments::thread_action_dir(+ &ctx,+ &config,+ &config.workspace_dir,+ Some(thread_id),+ )+ .await+ .unwrap();+ let repo = test_repo(&config.workspace_dir);+ let record = get_staged_record(&repo, &staged_doc_id(&opaque_id).unwrap())+ .unwrap()+ .unwrap();+ let staged_file = workspace.join(record.attachment.path);+ assert!(+ staged_file.exists(),+ "the first staged part is durable before cancellation"+ );++ task.abort();+ let _ = task.await;+ tokio::time::timeout(std::time::Duration::from_secs(2), async {+ loop {+ let record = get_staged_record(&repo, &staged_doc_id(&opaque_id).unwrap())+ .unwrap()+ .unwrap();+ if record.claim.is_some_and(|claim| claim.canceled) && !staged_file.exists() {+ break;+ }+ tokio::task::yield_now().await;+ }+ })+ .await+ .expect("canceling resolution must discard already-staged media");+}diff --git a/crates/openhuman-core/src/threads/attachments_tests.rs b/crates/openhuman-core/src/threads/attachments_tests.rsindex a2b5eee4c7..431ce134c3 100644--- a/crates/openhuman-core/src/threads/attachments_tests.rs+++ b/crates/openhuman-core/src/threads/attachments_tests.rs@@ -463,302 +463,5 @@ async fn discarding_claimed_attachment_preserves_row_owned_file() {assert_eq!(stored.claim, Some(bound));}-#[tokio::test]-async fn native_staged_lease_cleans_dropped_queue_and_preserves_committed_claims() {- let dir = tempfile::tempdir().unwrap();- let mut config = crate::config::Config::default();- config.workspace_dir = dir.path().join("workspace");- config.action_dir = dir.path().join("actions");- let ctx = crate::core::runtime::DispatchCtx::ambient();- let thread_id = "telegram:account-a:conversation-42";- let workspace = crate::agent::attachments::thread_action_dir(- &ctx,- &config,- &config.workspace_dir,- Some(thread_id),- )- .await- .unwrap();- tokio::fs::create_dir_all(&workspace).await.unwrap();- let repo = test_repo(&config.workspace_dir);-- let pending_id = uuid::Uuid::new_v4().simple().to_string();- let pending = image(MediaReference {- opaque_id: Some(pending_id.clone()),- content_type: Some("image/png".into()),- ..Default::default()- });- let pending_path = "pending.png";- let pending_file = workspace.join(pending_path);- tokio::fs::write(&pending_file, b"pending bytes")- .await- .unwrap();- let mut pending_record = staged_record(pending.clone(), pending_path);- pending_record.thread_id = thread_id.into();- insert_record(&repo, &pending_record);- let lease = NativeStagedLease::new(- ctx.clone(),- thread_id.into(),- config.clone(),- vec![pending.clone()],- );- let queued_clone = lease.clone();- drop(lease);- assert!(- get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())- .unwrap()- .unwrap()- .claim- .is_none()- );- drop(queued_clone);- tokio::time::timeout(std::time::Duration::from_secs(2), async {- loop {- let record = get_staged_record(&repo, &staged_doc_id(&pending_id).unwrap())- .unwrap()- .unwrap();- if record.claim.is_some_and(|claim| claim.canceled) && !pending_file.exists() {- break;- }- tokio::task::yield_now().await;- }- })- .await- .expect("dropping the final queued payload clone should discard its staged ref");- assert!(!pending_file.exists());-- let removed_id = uuid::Uuid::new_v4().simple().to_string();- let removed = image(MediaReference {- opaque_id: Some(removed_id.clone()),- content_type: Some("image/png".into()),- ..Default::default()- });- let removed_path = "removed-from-queue.png";- let removed_file = workspace.join(removed_path);- tokio::fs::write(&removed_file, b"removed bytes")- .await- .unwrap();- let mut removed_record = staged_record(removed.clone(), removed_path);- removed_record.thread_id = thread_id.into();- insert_record(&repo, &removed_record);- let queue_lease = NativeStagedLease::new(- ctx.clone(),- thread_id.into(),- config.clone(),- vec![removed.clone()],- );- let queue = tinyagents_harness::run_queue::RunQueue::new();- let queued = tinyagents_harness::turn_runner::TurnRequest {- scope: tinyagents_harness::turn_runner::ScopeKey {- agent_id: "orchestrator".into(),- profile_id: None,- },- thread_id: thread_id.into(),- request_id: "remove-native-media".into(),- text: "queued image".into(),- payload: crate::agent::queued_turn::QueuedPayload {- native_staged_lease: Some(queue_lease),- ..Default::default()- },- host_authored: false,- disposition: tinyagents_harness::turn_runner::QueueDisposition::Followup,- deadline: None,- idle_timeout: None,- };- queue- .push(tinyagents_harness::run_queue::QueueLane::Followup, queued)- .await;- assert_eq!(- queue- .remove_where(|request| request.request_id == "remove-native-media")- .await,- 1- );- tokio::time::timeout(std::time::Duration::from_secs(2), async {- loop {- let record = get_staged_record(&repo, &staged_doc_id(&removed_id).unwrap())- .unwrap()- .unwrap();- if record.claim.is_some_and(|claim| claim.canceled) && !removed_file.exists() {- break;- }- tokio::task::yield_now().await;- }- })- .await- .expect("removing the queued request should discard its staged media");- assert!(!removed_file.exists());-- let committed_id = uuid::Uuid::new_v4().simple().to_string();- let committed = image(MediaReference {- opaque_id: Some(committed_id.clone()),- content_type: Some("image/png".into()),- ..Default::default()- });- let committed_path = "committed.png";- let committed_file = workspace.join(committed_path);- tokio::fs::write(&committed_file, b"committed bytes")- .await- .unwrap();- let mut committed_record = staged_record(committed.clone(), committed_path);- committed_record.thread_id = thread_id.into();- insert_record(&repo, &committed_record);- let durable_claim = claim("user:request", committed.clone(), false);- claim_staged_record(- &repo,- &durable_claim.tenant,- thread_id,- &staged_doc_id(&committed_id).unwrap(),- &committed,- &durable_claim,- )- .unwrap();- let committed_lease =- NativeStagedLease::new(ctx, thread_id.into(), config, vec![committed.clone()]);- let queue_clone = committed_lease.clone();- committed_lease.mark_committed();- drop(committed_lease);- drop(queue_clone);- tokio::task::yield_now().await;- let stored = get_staged_record(&repo, &staged_doc_id(&committed_id).unwrap())- .unwrap()- .unwrap();- assert_eq!(stored.claim, Some(durable_claim));- assert_eq!(- tokio::fs::read(committed_file).await.unwrap(),- b"committed bytes"- );-}--#[tokio::test]-async fn native_staged_lease_retries_cleanup_for_its_canceled_tombstone() {- let dir = tempfile::tempdir().unwrap();- let mut config = crate::config::Config::default();- config.workspace_dir = dir.path().join("workspace");- config.action_dir = dir.path().join("actions");- let ctx = crate::core::runtime::DispatchCtx::ambient();- let thread_id = "telegram:account-a:conversation-canceled";- let workspace = crate::agent::attachments::thread_action_dir(- &ctx,- &config,- &config.workspace_dir,- Some(thread_id),- )- .await- .unwrap();- tokio::fs::create_dir_all(&workspace).await.unwrap();- let repo = test_repo(&config.workspace_dir);- let opaque_id = uuid::Uuid::new_v4().simple().to_string();- let part = image(MediaReference {- opaque_id: Some(opaque_id.clone()),- content_type: Some("image/png".into()),- ..Default::default()- });- let path = "canceled-before-retry.png";- let file = workspace.join(path);- tokio::fs::write(&file, b"canceled cleanup bytes")- .await- .unwrap();- let mut record = staged_record(part.clone(), path);- record.thread_id = thread_id.into();- record.claim = Some(StagedAttachmentClaim {- tenant: crate::core::runtime::Tenant::default(),- thread_id: thread_id.into(),- message_id: String::new(),- part: part.clone(),- canceled: true,- });- insert_record(&repo, &record);-- drop(NativeStagedLease::new(- ctx,- thread_id.into(),- config,- vec![part],- ));- tokio::time::timeout(std::time::Duration::from_secs(2), async {- while file.exists() {- tokio::task::yield_now().await;- }- })- .await- .expect("dropping the lease should retry cleanup after its exact canceled tombstone");-}--#[tokio::test]-async fn canceling_native_media_resolution_cleans_the_first_staged_part() {- let dir = tempfile::tempdir().unwrap();- let mut config = crate::config::Config::default();- config.workspace_dir = dir.path().join("workspace");- config.action_dir = dir.path().join("actions");- let ctx = crate::core::runtime::DispatchCtx::ambient();- let thread_id = "telegram:account-a:conversation-42";- let (staged_tx, staged_rx) = tokio::sync::oneshot::channel();- let task_ctx = ctx.clone();- let task_config = config.clone();- let task = tokio::spawn(async move {- let lease = NativeStagedLease::new(- task_ctx.clone(),- thread_id.into(),- task_config.clone(),- Vec::new(),- );- let source = image(MediaReference {- content_type: Some("image/png".into()),- kind: tinychannels_bus::MediaKind::Image,- ..Default::default()- });- let staged = crate::threads::attachments::stage_native_channel_part(- &task_ctx,- thread_id,- &task_config,- &source,- "first-native.png",- "image/png",- b"cancellation lifecycle fixture",- &lease,- )- .await- .unwrap();- staged_tx.send(staged).unwrap();- std::future::pending::<()>().await;- });- let staged = staged_rx.await.unwrap();- let opaque_id = opaque_media(&staged)- .and_then(|media| media.opaque_id.as_deref())- .unwrap()- .to_owned();- let workspace = crate::agent::attachments::thread_action_dir(- &ctx,- &config,- &config.workspace_dir,- Some(thread_id),- )- .await- .unwrap();- let repo = test_repo(&config.workspace_dir);- let record = get_staged_record(&repo, &staged_doc_id(&opaque_id).unwrap())- .unwrap()- .unwrap();- let staged_file = workspace.join(record.attachment.path);- assert!(- staged_file.exists(),- "the first staged part is durable before cancellation"- );-- task.abort();- let _ = task.await;- tokio::time::timeout(std::time::Duration::from_secs(2), async {- loop {- let record = get_staged_record(&repo, &staged_doc_id(&opaque_id).unwrap())- .unwrap()- .unwrap();- if record.claim.is_some_and(|claim| claim.canceled) && !staged_file.exists() {- break;- }- tokio::task::yield_now().await;- }- })- .await- .expect("canceling resolution must discard already-staged media");-}+#[path = "attachments_staged_tests.rs"]+mod staged_tests;