Skip to content

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

fix(agent): scope background completion store to the dispatch context

Steven Enamakel4908b8bd2e61
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 3d6f2e3b2d..aabc77cf31 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -140,12 +140,20 @@ pub(super) fn state() -> std::sync::MutexGuard<'static, HostState> {
/// The durable completion queue of `workspace_dir` in the storage backend,
/// under the calling tenant's scope.
-pub(super) fn open_driver_store(workspace_dir: &Path) -> anyhow::Result<Arc<dyn CompletionStore>> {
- let opened = crate::storage::local::open("background_completions", workspace_dir)?;
- let scoped = opened.scoped()?;
- let store = DriverCompletionStore::new(Arc::clone(scoped.documents()))
- .map_err(|error| anyhow::anyhow!("start the completion store bridge: {error}"))?;
- Ok(Arc::new(store))
+pub(super) fn open_driver_store(
+ ctx: &DispatchCtx,
+ workspace_dir: &Path,
+) -> anyhow::Result<Arc<dyn CompletionStore>> {
+ let core_context = ctx
+ .resolved()
+ .ok_or_else(|| anyhow::anyhow!("completion store requires a resolved dispatch context"))?;
+ crate::core::runtime::CoreContext::sync_scope(core_context, || {
+ let opened = crate::storage::local::open("background_completions", workspace_dir)?;
+ let scoped = opened.scoped()?;
+ let store = DriverCompletionStore::new(Arc::clone(scoped.documents()))
+ .map_err(|error| anyhow::anyhow!("start the completion store bridge: {error}"))?;
+ Ok(Arc::new(store) as Arc<dyn CompletionStore>)
+ })
}
pub(super) fn router_pool() -> &'static CompletionRouterPool<RouterScope> {
@@ -160,7 +168,9 @@ pub(super) fn router_pool() -> &'static CompletionRouterPool<RouterScope> {
{
return Arc::new(new_router(store));
}
- let store = match open_driver_store(&scope.workspace_dir) {
+ // `get_or_open` invokes its opener synchronously, so carry the
+ // caller's explicit profile context into storage::local::open.
+ let store = match open_driver_store(&DispatchCtx::ambient(), &scope.workspace_dir) {
Ok(store) => store,
Err(error) => {
log::error!(
@@ -186,12 +196,22 @@ pub(super) fn test_store_overrides(
pub(super) fn entry_for_ctx(ctx: &DispatchCtx, workspace_dir: &Path) -> Arc<Entry> {
let scope = router_scope_for(ctx, workspace_dir);
- let router = match router_pool().get_or_open(&scope) {
- Ok(router) => router,
- Err(error) => {
+ let router = match ctx.resolved().map(|core_context| {
+ crate::core::runtime::CoreContext::sync_scope(core_context, || {
+ router_pool().get_or_open(&scope)
+ })
+ }) {
+ Some(Ok(router)) => router,
+ Some(Err(error)) => {
log::error!("[background_completions] router pool lookup failed: {error}");
Arc::new(new_router(Arc::new(InMemoryCompletionStore::new())))
}
+ None => {
+ log::error!(
+ "[background_completions] router lookup requires a resolved dispatch context"
+ );
+ Arc::new(new_router(Arc::new(InMemoryCompletionStore::new())))
+ }
};
let store = router.store();
state().stores.insert(scope, store.clone());
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_target.rs b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
index 639fc2d084..236fb8be2a 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_target.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
@@ -152,7 +152,7 @@ pub(crate) fn forget_recovery_ctx(ctx: &crate::core::runtime::DispatchCtx, works
if Arc::strong_count(&router) != 2 {
return;
}
- let Ok(store) = super::background_completions::open_driver_store(workspace_dir) else {
+ let Ok(store) = super::background_completions::open_driver_store(ctx, workspace_dir) else {
return;
};
let replacement = Arc::new(super::background_completions::new_router(store));