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): adjust staged attachment repository access
Steven Enamakel8e783f52c2d0
diff --git a/crates/openhuman-core/src/threads/attachments.rs b/crates/openhuman-core/src/threads/attachments.rsindex 63d2601c9b..de841ea6d4 100644--- a/crates/openhuman-core/src/threads/attachments.rs+++ b/crates/openhuman-core/src/threads/attachments.rs@@ -272,7 +272,6 @@ async fn read_staged_entries(let tenant = tenant_for(ctx)?;let repo = staged_repo(ctx, &config.workspace_dir)?;let mut entries = Vec::with_capacity(requested.len());- let repo = staged_repo(ctx, &config.workspace_dir)?;for part in requested {let opaque_id = opaque_media(part).and_then(|media| media.opaque_id.as_deref())@@ -706,6 +705,7 @@ pub(crate) async fn consume_staged_parts(.await.map_err(|error| format!("staged attachment configuration unavailable: {error}"))?;let tenant = tenant_for(ctx)?;+ let repo = staged_repo(ctx, &config.workspace_dir)?;for part in requested {let id = opaque_media(part).and_then(|media| media.opaque_id.as_deref())diff --git a/crates/openhuman-core/src/threads/attachments_tests.rs b/crates/openhuman-core/src/threads/attachments_tests.rsindex 25943bcc33..66b7f0009d 100644--- a/crates/openhuman-core/src/threads/attachments_tests.rs+++ b/crates/openhuman-core/src/threads/attachments_tests.rs@@ -59,6 +59,7 @@ fn public_projection_hides_paths_but_keeps_opaque_attachment_refs() {assert_eq!(metadata, json!({"keep": true}));}+fn claim(message_id: &str, part: ContentPart, canceled: bool) -> StagedAttachmentClaim {StagedAttachmentClaim {tenant: crate::core::runtime::Tenant::default(),@@ -69,83 +70,85 @@ fn claim(message_id: &str, part: ContentPart, canceled: bool) -> StagedAttachmen}}-#[tokio::test]-async fn partial_claim_failure_does_not_revoke_same_owner_retry() {- let dir = tempfile::tempdir().unwrap();- let first_path = dir.path().join("first.claim.json");- let second_path = dir.path().join("second.claim.json");- let first = claim(- "user:req",- image(MediaReference {- opaque_id: Some("first".into()),- ..Default::default()- }),- false,- );- let other_owner = claim(- "user:other",- image(MediaReference {- opaque_id: Some("second".into()),- ..Default::default()- }),- false,- );- let cancel_second = claim("", other_owner.part.clone(), true);+fn test_repo(workspace: &std::path::Path) -> Repo {+ let backend = crate::storage::local::backend_for(workspace).unwrap();+ Repo::on(+ &backend,+ &crate::storage::Scope::local(),+ STAGED_DOMAIN,+ staged_collections,+ )+ .unwrap()+}- publish_staged_claim(&first_path, &first).await.unwrap();- publish_staged_claim(&second_path, &cancel_second)- .await- .unwrap();- assert!(publish_staged_claim(&second_path, &other_owner)- .await- .is_err());- assert!(!publish_staged_claim(&first_path, &first).await.unwrap());- let stored: StagedAttachmentClaim =- serde_json::from_slice(&tokio::fs::read(&first_path).await.unwrap()).unwrap();- assert_eq!(stored, first);+fn staged_record(part: ContentPart, path: &str) -> StagedAttachment {+ StagedAttachment {+ tenant: crate::core::runtime::Tenant::default(),+ thread_id: "thread".into(),+ attachment: StoredAttachment { part, path: path.into() },+ expires_at: chrono::Utc::now().timestamp() + 60,+ claim: None,+ }+}++fn insert_record(repo: &Repo, record: &StagedAttachment) {+ let id = staged_doc_id(+ opaque_media(&record.attachment.part)+ .and_then(|media| media.opaque_id.as_deref())+ .unwrap(),+ ).unwrap();+ let body = serde_json::to_value(record).unwrap();+ repo.run(|docs| async move {+ docs.put(STAGED_COLLECTION, &id, body, Precondition::Absent).await?;+ Ok(())+ }).unwrap();}#[tokio::test]-async fn staged_claim_and_cancellation_have_one_atomic_winner() {+async fn staged_claim_and_cancellation_have_one_atomic_document_winner() {let dir = tempfile::tempdir().unwrap();- let claim_path = dir.path().join("race.claim.json");+ let repo = test_repo(dir.path());let part = image(MediaReference {- opaque_id: Some("race".into()),+ opaque_id: Some(uuid::Uuid::new_v4().simple().to_string()),..Default::default()});+ let record = staged_record(part.clone(), "media.png");+ insert_record(&repo, &record);+ let id = staged_doc_id(opaque_media(&part).unwrap().opaque_id.as_deref().unwrap()).unwrap();let bound = claim("user:req", part.clone(), false);- let canceled = claim("", part, true);+ let canceled = claim("", part.clone(), true);let barrier = std::sync::Arc::new(tokio::sync::Barrier::new(3));let claim_task = {let barrier = barrier.clone();- let path = claim_path.clone();+ let repo = repo.clone();let bound = bound.clone();+ let part = part.clone();tokio::spawn(async move {barrier.wait().await;- publish_staged_claim(&path, &bound).await+ claim_staged_record(&repo, &bound.tenant, "thread", &id, &part, &bound)})};let cancel_task = {let barrier = barrier.clone();- let path = claim_path.clone();+ let repo = repo.clone();let canceled = canceled.clone();+ let part = part.clone();tokio::spawn(async move {barrier.wait().await;- publish_staged_claim(&path, &canceled).await+ cancel_staged_record(&repo, &canceled.tenant, "thread", &id, &part, &canceled)})};barrier.wait().await;let claim_result = claim_task.await.unwrap();let cancel_result = cancel_task.await.unwrap();assert_ne!(claim_result.is_ok(), cancel_result.is_ok());-- let stored: StagedAttachmentClaim =- serde_json::from_slice(&tokio::fs::read(&claim_path).await.unwrap()).unwrap();- if stored.canceled {- assert!(publish_staged_claim(&claim_path, &bound).await.is_err());+ let stored = get_staged_record(&repo, &id).unwrap().unwrap();+ let winner = stored.claim.unwrap();+ if winner.canceled {+ assert!(claim_staged_record(&repo, &bound.tenant, "thread", &id, &part, &bound).is_err());} else {- assert!(publish_staged_claim(&claim_path, &canceled).await.is_err());- assert!(!publish_staged_claim(&claim_path, &bound).await.unwrap());+ assert!(!cancel_staged_record(&repo, &canceled.tenant, "thread", &id, &part, &canceled).unwrap());+ assert!(!claim_staged_record(&repo, &bound.tenant, "thread", &id, &part, &bound).unwrap());}}@@ -155,56 +158,35 @@ async fn multipart_claim_failure_keeps_prior_claim_owned_by_retry() {let mut config = crate::config::Config::default();config.workspace_dir = dir.path().join("workspace");config.action_dir = dir.path().join("actions");- let claim_dir = config.workspace_dir.join(STAGED_DIR);- tokio::fs::create_dir_all(&claim_dir).await.unwrap();- let first_id = uuid::Uuid::new_v4().to_string();- let second_id = uuid::Uuid::new_v4().to_string();- let first = image(MediaReference {- opaque_id: Some(first_id.clone()),- ..Default::default()- });- let second = image(MediaReference {- opaque_id: Some(second_id.clone()),- ..Default::default()- });- let first_path = staged_manifest_path(&config.workspace_dir, &first_id).unwrap();- let second_path = staged_manifest_path(&config.workspace_dir, &second_id).unwrap();- publish_staged_claim(- &second_path.with_extension("claim.json"),- &claim("", second.clone(), true),- )- .await- .unwrap();+ let repo = test_repo(&config.workspace_dir);+ let first_id = uuid::Uuid::new_v4().simple().to_string();+ let second_id = uuid::Uuid::new_v4().simple().to_string();+ let first = image(MediaReference { opaque_id: Some(first_id.clone()), ..Default::default() });+ let second = image(MediaReference { opaque_id: Some(second_id.clone()), ..Default::default() });+ insert_record(&repo, &staged_record(first.clone(), "first.png"));+ insert_record(&repo, &staged_record(second.clone(), "second.png"));+ let second_claim = claim("", second.clone(), true);+ assert!(cancel_staged_record(+ &repo, &second_claim.tenant, "thread", &staged_doc_id(&second_id).unwrap(),+ &second, &second_claim,+ ).unwrap());let entries = vec![- StoredAttachment {- part: first.clone(),- path: "first.png".into(),- },- StoredAttachment {- part: second.clone(),- path: "second.png".into(),- },+ StoredAttachment { part: first.clone(), path: "first.png".into() },+ StoredAttachment { part: second.clone(), path: "second.png".into() },];assert!(claim_staged_refs(&crate::core::runtime::DispatchCtx::ambient(),- "thread",- &config,- "user:req",- &entries,- )- .await- .is_err());-- let first_claim_path = first_path.with_extension("claim.json");- let owner = claim("user:req", first.clone(), false);- assert!(!publish_staged_claim(&first_claim_path, &owner)- .await- .unwrap());- assert!(- publish_staged_claim(&first_claim_path, &claim("user:other", first, false))- .await- .is_err()- );+ "thread", &config, "user:req", &entries,+ ).await.is_err());+ let first_claim = claim("user:req", first.clone(), false);+ assert!(!claim_staged_record(+ &repo, &first_claim.tenant, "thread", &staged_doc_id(&first_id).unwrap(),+ &first, &first_claim,+ ).unwrap());+ let other = claim("user:other", first.clone(), false);+ assert!(claim_staged_record(+ &repo, &other.tenant, "thread", &staged_doc_id(&first_id).unwrap(), &first, &other,+ ).is_err());}#[tokio::test]@@ -213,52 +195,33 @@ async fn discarding_claimed_attachment_preserves_row_owned_file() {let mut config = crate::config::Config::default();config.workspace_dir = dir.path().join("workspace");config.action_dir = dir.path().join("actions");- let opaque_id = uuid::Uuid::new_v4().to_string();+ 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 claim_dir = config.workspace_dir.join(STAGED_DIR);- tokio::fs::create_dir_all(&claim_dir).await.unwrap();- let manifest_path = staged_manifest_path(&config.workspace_dir, &opaque_id).unwrap();- publish_staged_claim(- &manifest_path.with_extension("claim.json"),- &claim("user:req", part.clone(), false),- )- .await- .unwrap();+ let repo = test_repo(&config.workspace_dir);+ insert_record(&repo, &staged_record(part.clone(), "committed.png"));+ let bound = claim("user:req", part.clone(), false);+ assert!(claim_staged_record(+ &repo, &bound.tenant, "thread", &staged_doc_id(&opaque_id).unwrap(), &part, &bound,+ ).unwrap());let workspace = crate::agent::attachments::thread_action_dir(&crate::core::runtime::DispatchCtx::ambient(),&config,&config.workspace_dir,Some("thread"),- )- .await- .unwrap();+ ).await.unwrap();tokio::fs::create_dir_all(&workspace).await.unwrap();- let relative_path = "committed.png";- let file_path = workspace.join(relative_path);- tokio::fs::write(&file_path, b"row-owned bytes")- .await- .unwrap();- let metadata = serde_json::json!({"__openhumanStoredAttachments": [{- "part": part,- "path": relative_path- }]});- let entries = stored(&metadata).unwrap();-+ let file_path = workspace.join("committed.png");+ tokio::fs::write(&file_path, b"row-owned bytes").await.unwrap();discard_staged_entries(&crate::core::runtime::DispatchCtx::ambient(),- "thread",- &config,- entries,- )- .await- .unwrap();- assert_eq!(- tokio::fs::read(file_path).await.unwrap(),- b"row-owned bytes"- );- assert!(manifest_path.with_extension("claim.json").exists());+ "thread", &config,+ vec![StoredAttachment { part: part.clone(), path: "committed.png".into() }],+ ).await.unwrap();+ assert_eq!(tokio::fs::read(file_path).await.unwrap(), b"row-owned bytes");+ let stored = get_staged_record(&repo, &staged_doc_id(&opaque_id).unwrap()).unwrap().unwrap();+ assert_eq!(stored.claim, Some(bound));}