Skip to content

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

refactor(orchestration): thread dispatch context through completion router lookups

Steven Enamakel25828492a19e
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index 85ea6cac8b..ac253ff39a 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -373,7 +373,8 @@ pub(crate) async fn record_outcome(
)
.with_notify_mode(NotifyMode::Followup)
.with_label(AWAITING_INPUT_LABEL);
- record_completion_record(workspace_dir, &router, record, &task_id, &thread_id).await;
+ record_completion_record(ctx, workspace_dir, &router, record, &task_id, &thread_id)
+ .await;
return;
}
};
@@ -405,6 +406,7 @@ pub(crate) async fn record_outcome(
}
async fn record_completion_record(
+ ctx: &DispatchCtx,
workspace_dir: &Path,
router: &Arc<CompletionRouter>,
record: CompletionRecord,
@@ -418,7 +420,7 @@ async fn record_completion_record(
log::error!(
"[background_completions] store write failed after retries; continuing in memory task_id={task_id} thread_id={thread_id} error={error}"
);
- let _ = degrade_to_memory(&DispatchCtx::ambient(), workspace_dir, router)
+ let _ = degrade_to_memory(ctx, workspace_dir, router)
.router
.record(record)
.await;
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions/thread_index.rs b/crates/openhuman-core/src/agent/orchestration/background_completions/thread_index.rs
index 62916f2308..00a9963b6e 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions/thread_index.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions/thread_index.rs
@@ -37,7 +37,7 @@ pub(crate) fn router_for_thread(
.thread_workspaces
.get(&key(ctx, thread_id))
.cloned()?;
- Some(router_for_workspace(&workspace))
+ Some(super::router_for_workspace_ctx(ctx, &workspace))
}
/// The workspace holding `thread_id`'s completions, if known to this process.
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 271fbe02a4..5f4a4ac58d 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
@@ -325,7 +325,7 @@ fn recover_fenced(ctx: &DispatchCtx, workspace_dir: &Path, fence: DrainFence) ->
}
// Once per workspace per process: the bootstrap workspace at startup, any
// other the first time a spawn opens it.
- if !background_completions::claim_recovery(workspace_dir) {
+ if !background_completions::claim_recovery(ctx, workspace_dir) {
return 0;
}
let threads = background_completions::recover_pending_threads(ctx, workspace_dir);
@@ -353,12 +353,12 @@ fn recover_fenced(ctx: &DispatchCtx, workspace_dir: &Path, fence: DrainFence) ->
/// that comes due after this node lost that lease is dropped, and the records
/// stay pending for the node that holds it now.
pub(crate) fn recover_on_open(ctx: &DispatchCtx, workspace_dir: &Path, fence: DrainFence) -> usize {
- background_completions::forget_recovery(workspace_dir);
+ background_completions::forget_recovery(ctx, workspace_dir);
let scheduled = recover_fenced(ctx, workspace_dir, fence);
if scheduled == 0 {
// Nothing pending: do not keep this profile's log handle cached for a
// workspace that may never complete anything on this node.
- background_completions::forget_recovery(workspace_dir);
+ background_completions::forget_recovery(ctx, workspace_dir);
}
scheduled
}
@@ -405,7 +405,7 @@ async fn try_deliver(
log::debug!("[background_delivery] no workspace for thread_id={thread_id}");
return DeliveryOutcome::Empty;
};
- let router = background_completions::router_for_workspace(&workspace_dir);
+ let router = background_completions::router_for_workspace_ctx(&busy_ctx, &workspace_dir);
let delivery = CompletionDelivery::new(
CompletionDeliveryConfig {
debounce: DEBOUNCE,
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_target.rs b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
index 479795d814..29855363f1 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_target.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
@@ -120,18 +120,18 @@ impl CompletionTarget {
/// Claim `workspace_dir`'s boot recovery for this process. `true` exactly once
/// per workspace, so the host can scan every workspace it opens (the bootstrap
/// one, then any other a spawn later opens) without rescanning.
-pub(crate) fn claim_recovery(workspace_dir: &Path) -> bool {
+pub(crate) fn claim_recovery(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path) -> bool {
state()
.recovered_workspaces
- .insert(super::background_completions::router_scope(workspace_dir))
+ .insert(super::background_completions::router_scope_for(ctx, workspace_dir))
}
/// Let `workspace_dir` be recovered again (a profile re-leased after release),
/// and drop its cached router when nothing is using it so the next scan replays
/// the log another holder may have appended to. A router still in use stays,
/// so a log never has two writers.
-pub(crate) fn forget_recovery(workspace_dir: &Path) {
- let scope = super::background_completions::router_scope(workspace_dir);
+pub(crate) fn forget_recovery(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path) {
+ let scope = super::background_completions::router_scope_for(ctx, workspace_dir);
state().recovered_workspaces.remove(&scope);
let Ok(Some(router)) = super::background_completions::router_pool().get(&scope) else {
return;
diff --git a/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs b/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs
index 02c18d849e..0fe1505fbc 100644
--- a/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs
+++ b/crates/openhuman-core/src/agent/orchestration/running_subagents/wait.rs
@@ -24,7 +24,7 @@ pub(crate) async fn wait(
wait_detached_or_durable(
registry(),
super::task_ledger::task_store_for_workspace(&workspace_dir).as_ref(),
- &super::super::background_completions::router_for_workspace(&workspace_dir),
+ &super::super::background_completions::router_for_workspace_ctx(ctx, &workspace_dir),
task_id,
&owner,
timeout,
@@ -44,7 +44,7 @@ pub(crate) async fn wait_in_workspace(
wait_detached_or_durable(
registry(),
super::task_ledger::task_store_for_workspace(workspace_dir).as_ref(),
- &super::super::background_completions::router_for_workspace(workspace_dir),
+ &super::super::background_completions::router_for_workspace_ctx(ctx, workspace_dir),
task_id,
&owner,
timeout,