Skip to content

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

refactor(memory): rename write operation types and tidy formatting

Steven Enamakeld80b3c18e6c5
diff --git a/crates/openhuman-core/src/memory/deletion.rs b/crates/openhuman-core/src/memory/deletion.rs
index eef3b6997b..e9bd32331a 100644
--- a/crates/openhuman-core/src/memory/deletion.rs
+++ b/crates/openhuman-core/src/memory/deletion.rs
@@ -124,7 +124,9 @@ impl DeletionExecutor for Executor<'_> {
async fn run(&self, deletion: &PendingDeletion) -> Result<usize, String> {
let result = match deletion {
- PendingDeletion::Thread { thread_id } => forget_thread_now(self.config, thread_id).await,
+ PendingDeletion::Thread { thread_id } => {
+ forget_thread_now(self.config, thread_id).await
+ }
PendingDeletion::Source { source_id } => {
super::sources::forget_items(self.config, source_id).await
}
diff --git a/crates/openhuman-core/src/memory/deletion_tests.rs b/crates/openhuman-core/src/memory/deletion_tests.rs
index 23750f4646..b54409133c 100644
--- a/crates/openhuman-core/src/memory/deletion_tests.rs
+++ b/crates/openhuman-core/src/memory/deletion_tests.rs
@@ -75,20 +75,6 @@ async fn a_failed_forget_is_kept_for_the_next_drain() {
assert_eq!(pending(&config.workspace_dir), vec![thread("t-2")]);
}
-#[test]
-fn a_deletion_is_queued_once() {
- let tmp = tempfile::tempdir().unwrap();
- enqueue(tmp.path(), thread("t"));
- enqueue(tmp.path(), thread("t"));
- enqueue(
- tmp.path(),
- PendingDeletion::Source {
- source_id: "src-1".into(),
- },
- );
- assert_eq!(pending(tmp.path()).len(), 2);
-}
-
#[test]
fn an_unparsable_queue_is_set_aside_not_lost() {
let tmp = tempfile::tempdir().unwrap();
diff --git a/crates/openhuman-core/src/memory/tool_writes.rs b/crates/openhuman-core/src/memory/tool_writes.rs
index a9bd4e661c..fd6cff155c 100644
--- a/crates/openhuman-core/src/memory/tool_writes.rs
+++ b/crates/openhuman-core/src/memory/tool_writes.rs
@@ -185,7 +185,7 @@ pub(super) fn enqueue(
"learning namespace is outside the trusted reach",
));
}
- Operation::Learn {
+ WriteOperation::Learn {
item: super::guard::scrub(item),
}
}
@@ -203,11 +203,11 @@ pub(super) fn enqueue(
if ids.is_empty() {
return Err(MemoryError::invalid("forget needs at least one id"));
}
- Operation::Forget { ids, reach }
+ WriteOperation::Forget { ids, reach }
}
_ => return Err(MemoryError::invalid("expected a memory write")),
};
- let _guard = LOCK
+ let _guard = LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if !file.exists() && partitions(config)?.len() >= MAX_PENDING {
@@ -217,7 +217,7 @@ pub(super) fn enqueue(
}
let owner = owner_state(config);
let ack = queue(&file)
- .enqueue(operation, acknowledgement_scope, binding(config), &owner.gate)
+ .enqueue(operation, scope_root, binding(config), &owner.gate)
.map_err(map_pending)?;
Ok(match ack {
Ack::Learned { id } => serde_json::json!({"id":id, "status":QUEUED}),
@@ -558,18 +558,13 @@ impl WriteExecutor for Executor<'_> {
};
match tokio::time::timeout(WRITE_TIMEOUT, work).await {
Ok(Ok(())) => Ok(()),
- Ok(Err(error)) => Err(MemoryError::code(&error).to_string()),
+ Ok(Err(error)) => Err(error.code().to_string()),
Err(_) => Err("TIMEOUT".to_string()),
}
}
}
-async fn drain_locked(
- config: &Config,
- file: &Path,
- state: &OwnerState,
- generation: u64,
-) -> usize {
+async fn drain_locked(config: &Config, file: &Path, state: &OwnerState, generation: u64) -> usize {
if config.memory.engine == super::engine::DISABLED_ENGINE {
return 0;
}
diff --git a/crates/openhuman-core/src/memory/tool_writes_tests.rs b/crates/openhuman-core/src/memory/tool_writes_tests.rs
index edb4e2c106..1d7f582b87 100644
--- a/crates/openhuman-core/src/memory/tool_writes_tests.rs
+++ b/crates/openhuman-core/src/memory/tool_writes_tests.rs
@@ -1,4 +1,5 @@
use super::*;
+use tinymemory_api::Reach;
use crate::memory::scope::MemoryIdentity;
use crate::memory::test_fixtures::{bind_reference, config_in, stored, RefusingEngine};
use serde_json::json;
@@ -262,7 +263,7 @@ async fn a_different_owner_cannot_replay_the_original_owners_queue() {
{
let _guard = LOCK.lock().unwrap();
let mut entries = read(&config.workspace_dir).unwrap();
- entries[0].root = Some("org:another-user".into());
+ entries[0].binding = binding_of(Some("org:another-user".into()), false);
write(&config.workspace_dir, &entries).unwrap();
}
let engine = bind_reference(&config);
@@ -428,7 +429,7 @@ async fn an_erase_fence_waits_for_only_the_current_write_and_preserves_later_enq
let fence_config = config.clone();
let fencing = tokio::spawn(async move { fence_and_clear(&fence_config).await });
let state = owner_state(&config);
- while state.fences.load(Ordering::SeqCst) == 0 {
+ while !state.gate.is_fenced() {
tokio::task::yield_now().await;
}
assert!(
@@ -538,7 +539,7 @@ async fn erasing_one_owner_fences_pending_writes_from_every_team_root() {
}
#[tokio::test]
-async fn a_later_erase_clears_pending_writes_when_the_owner_state_was_released() {
+async fn a_later_erase_clears_pending_writes_admitted_during_the_first_erase() {
let tmp = tempfile::tempdir().unwrap();
let config = config_in(&tmp);
let first = fence_and_clear(&config).await.unwrap();
@@ -548,11 +549,7 @@ async fn a_later_erase_clears_pending_writes_when_the_owner_state_was_released()
&facts(&config),
)
.unwrap();
- let previous_owner = Arc::downgrade(&first.state);
drop(first);
- // The default current-thread test runtime has not polled the scheduled
- // retry yet; the weak map no longer owns the previous fence generation.
- assert!(previous_owner.upgrade().is_none());
let second = fence_and_clear(&config).await.unwrap();
assert!(
read(&config.workspace_dir).unwrap().is_empty(),
@@ -560,3 +557,26 @@ async fn a_later_erase_clears_pending_writes_when_the_owner_state_was_released()
);
drop(second);
}
+
+#[test]
+fn an_outbox_written_before_the_binding_field_is_still_read() {
+ let tmp = tempfile::tempdir().unwrap();
+ let config = config_in(&tmp);
+ let file = path(&config.workspace_dir);
+ std::fs::create_dir_all(file.parent().unwrap()).unwrap();
+ let legacy = serde_json::json!([{
+ "ticket": "t-1",
+ "root": null,
+ "layout_v3": false,
+ "scope_root": "",
+ "epoch": ["old-process", 3],
+ "operation": {"action": "forget", "ids": ["a"], "reach": serde_json::to_value(Reach::subtree(Namespace::ROOT)).unwrap()},
+ "status": "TIMEOUT"
+ }]);
+ std::fs::write(&file, serde_json::to_vec(&legacy).unwrap()).unwrap();
+ let entries = read(&config.workspace_dir).unwrap();
+ assert_eq!(entries.len(), 1);
+ assert_eq!(entries[0].binding, binding_of(None, false));
+ assert_eq!(entries[0].status.as_deref(), Some("TIMEOUT"));
+ assert_eq!(entries[0].epoch.as_ref().unwrap().generation, 3);
+}