Skip to content

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

feat(memory): add tool write support for memory operations

Steven Enamakel73a6ab95f82c
diff --git a/crates/openhuman-core/src/memory/ops.rs b/crates/openhuman-core/src/memory/ops.rs
index 464ca58767..467741974c 100644
--- a/crates/openhuman-core/src/memory/ops.rs
+++ b/crates/openhuman-core/src/memory/ops.rs
@@ -428,7 +428,11 @@ pub async fn forget(config: &Config, params: ForgetParams) -> MemoryResult<Forge
/// layout v3, and the retired `user:<id>` one while it is still read); a legacy tree is left alone, because on a self-hosted CortexDB
/// other accounts may share it (see `layout_migration`). An engine that
/// cannot erase answers `UNSUPPORTED`.
-pub async fn erase_all(config: &Config, params: EraseAllParams) -> MemoryResult<EraseAllView> {
+pub async fn erase_all(
+ ctx: &crate::core::runtime::DispatchCtx,
+ config: &Config,
+ params: EraseAllParams,
+) -> MemoryResult<EraseAllView> {
if !params.confirm {
return Err(MemoryError::invalid(
"erasing all memory needs confirm: true; nothing erased comes back",
@@ -437,7 +441,7 @@ pub async fn erase_all(config: &Config, params: EraseAllParams) -> MemoryResult<
let bound = bound(config)?;
// Earlier durable tool writes must finish or be discarded before the
// remote wipe; hold the owner barrier until the erase has completed.
- let _writes = super::tool_writes::fence_and_clear(config).await?;
+ let _writes = super::tool_writes::fence_and_clear(ctx, config).await?;
super::lifecycle::prefetch::invalidate(config);
let mut request = EraseRequest::new(tinymemory_api::Reach::subtree(
tinymemory_api::Namespace::ROOT,
diff --git a/crates/openhuman-core/src/memory/schemas/handlers.rs b/crates/openhuman-core/src/memory/schemas/handlers.rs
index c156df8bc4..d3ad6f24df 100644
--- a/crates/openhuman-core/src/memory/schemas/handlers.rs
+++ b/crates/openhuman-core/src/memory/schemas/handlers.rs
@@ -105,7 +105,7 @@ pub(super) fn forget(ctx: DispatchCtx, params: Map<String, Value>) -> Controller
pub(super) fn erase_all(ctx: DispatchCtx, params: Map<String, Value>) -> ControllerFuture {
Box::pin(async move {
let params = parse::<EraseAllParams>(params)?;
- finish(ops::erase_all(&load(&ctx).await?, params).await)
+ finish(ops::erase_all(&ctx, &load(&ctx).await?, params).await)
})
}
diff --git a/crates/openhuman-core/src/memory/tool_writes.rs b/crates/openhuman-core/src/memory/tool_writes.rs
index 96c8a43d61..aae3a8292a 100644
--- a/crates/openhuman-core/src/memory/tool_writes.rs
+++ b/crates/openhuman-core/src/memory/tool_writes.rs
@@ -407,6 +407,7 @@ pub(crate) struct WriteFence {
token: Option<FenceToken<'static>>,
state: &'static OwnerState,
config: Arc<Config>,
+ ctx: crate::core::runtime::DispatchCtx,
}
impl Drop for WriteFence {
@@ -414,7 +415,7 @@ impl Drop for WriteFence {
drop(self.token.take());
drop(self.guard.take());
if !self.state.gate.is_fenced() && tokio::runtime::Handle::try_current().is_ok() {
- schedule_all(self.config.clone());
+ schedule_all(&self.ctx, self.config.clone());
}
}
}
@@ -422,7 +423,7 @@ impl Drop for WriteFence {
/// Stops each owner partition after its current mutation, durably clears
/// earlier pending writes, and fences subsequent writes through explicit erase.
/// Other owners sharing a workspace keep independent queues and barriers.
-pub(crate) async fn fence_and_clear(config: &Config) -> MemoryResult<WriteFence> {
+pub(crate) async fn fence_and_clear(ctx: &crate::core::runtime::DispatchCtx, config: &Config) -> MemoryResult<WriteFence> {
let state = owner_state(config);
let token = state.gate.begin_fence();
let fencing_generation = token.generation();
@@ -431,10 +432,11 @@ pub(crate) async fn fence_and_clear(config: &Config) -> MemoryResult<WriteFence>
token: Some(token),
state,
config: Arc::new(config.clone()),
+ ctx: ctx.clone(),
};
fence.guard = Some(state.barrier.clone().write_owned().await);
let snapshot_config = config.clone();
- crate::core::runtime::DispatchCtx::ambient().spawn_blocking(move || {
+ ctx.spawn_blocking(move || {
let _guard = LOCK
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
@@ -454,20 +456,21 @@ pub(crate) async fn fence_and_clear(config: &Config) -> MemoryResult<WriteFence>
/// Starts a scoped background drain, retaining config only in memory.
///
/// `root` is the layout root the write was queued under (`CallFacts::reach`).
-pub(super) fn schedule(config: Arc<Config>, root: &Namespace) {
+pub(super) fn schedule(ctx: &crate::core::runtime::DispatchCtx, config: Arc<Config>, root: &Namespace) {
let file = queue_path(&config, root);
- schedule_partition(config, file);
+ schedule_partition(ctx, config, file);
}
/// Retries all durable partitions belonging to this owner on auth/cron ticks.
-pub(super) fn schedule_all(config: Arc<Config>) {
+pub(super) fn schedule_all(ctx: &crate::core::runtime::DispatchCtx, config: Arc<Config>) {
if super::engine::switched_off(&config) {
return;
}
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ let scan_ctx = ctx.clone();
+ ctx.spawn(async move {
let scan_config = config.clone();
let files =
- match crate::core::runtime::DispatchCtx::ambient().spawn_blocking(move || partitions(&scan_config))
+ match scan_ctx.spawn_blocking(move || partitions(&scan_config))
.await
{
Ok(Ok(files)) => files,
@@ -480,12 +483,12 @@ pub(super) fn schedule_all(config: Arc<Config>) {
}
};
for file in files {
- schedule_partition(config.clone(), file);
+ schedule_partition(&scan_ctx, config.clone(), file);
}
});
}
-fn schedule_partition(config: Arc<Config>, file: PathBuf) {
+fn schedule_partition(ctx: &crate::core::runtime::DispatchCtx, config: Arc<Config>, file: PathBuf) {
let owner = owner_state(&config);
if owner.gate.is_fenced() {
return;
@@ -498,7 +501,7 @@ fn schedule_partition(config: Arc<Config>, file: PathBuf) {
let Ok(guard) = state.barrier.clone().try_lock_owned() else {
return;
};
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ ctx.spawn(async move {
let _guard = guard;
let _owner_guard = owner_guard;
let _state = state;
diff --git a/crates/openhuman-core/src/memory/tools.rs b/crates/openhuman-core/src/memory/tools.rs
index de73401d60..0448d82567 100644
--- a/crates/openhuman-core/src/memory/tools.rs
+++ b/crates/openhuman-core/src/memory/tools.rs
@@ -402,7 +402,7 @@ impl Tool for MemoryTool {
))
}) {
Ok(value) => {
- super::tool_writes::schedule(self.config.clone(), &queued_root);
+ super::tool_writes::schedule(&dispatch, self.config.clone(), &queued_root);
ToolResult::success(value.to_string())
}
Err(error) => ToolResult::error(render_error(error)),