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 recovery helpers

Steven Enamakelf643783a240d
diff --git a/crates/openhuman-core/src/agent/orchestration/background_completions.rs b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
index ac253ff39a..dda3c4be19 100644
--- a/crates/openhuman-core/src/agent/orchestration/background_completions.rs
+++ b/crates/openhuman-core/src/agent/orchestration/background_completions.rs
@@ -51,6 +51,8 @@ pub(crate) use super::completion_target::tests::{
};
pub(crate) use super::completion_target::CompletionTarget;
use super::completion_target::{awaiting_input_record_id, awaiting_input_record_prefix};
+pub(crate) use super::completion_target::{claim_recovery_ctx, forget_recovery_ctx};
+#[cfg(test)]
pub(crate) use super::completion_target::{claim_recovery, forget_recovery};
/// How long a settled record (delivered / gave up / tombstoned) is kept before
diff --git a/crates/openhuman-core/src/agent/orchestration/background_delivery.rs b/crates/openhuman-core/src/agent/orchestration/background_delivery.rs
index 5f4a4ac58d..00a9f00ff9 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(ctx, workspace_dir) {
+ if !background_completions::claim_recovery_ctx(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(ctx, workspace_dir);
+ background_completions::forget_recovery_ctx(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(ctx, workspace_dir);
+ background_completions::forget_recovery_ctx(ctx, workspace_dir);
}
scheduled
}
diff --git a/crates/openhuman-core/src/agent/orchestration/completion_target.rs b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
index 29855363f1..3710f4e5f9 100644
--- a/crates/openhuman-core/src/agent/orchestration/completion_target.rs
+++ b/crates/openhuman-core/src/agent/orchestration/completion_target.rs
@@ -120,17 +120,28 @@ 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(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path) -> bool {
+pub(crate) fn claim_recovery_ctx(
+ ctx: &crate::core::runtime::DispatchCtx,
+ workspace_dir: &Path,
+) -> bool {
state()
.recovered_workspaces
.insert(super::background_completions::router_scope_for(ctx, workspace_dir))
}
+#[cfg(test)]
+pub(crate) fn claim_recovery(workspace_dir: &Path) -> bool {
+ claim_recovery_ctx(&crate::core::runtime::DispatchCtx::ambient(), 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(ctx: &crate::core::runtime::DispatchCtx, workspace_dir: &Path) {
+pub(crate) fn forget_recovery_ctx(
+ 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 {
@@ -155,3 +166,8 @@ pub(crate) fn forget_recovery(ctx: &crate::core::runtime::DispatchCtx, workspace
state().stores.insert(scope, router.store());
}
}
+
+#[cfg(test)]
+pub(crate) fn forget_recovery(workspace_dir: &Path) {
+ forget_recovery_ctx(&crate::core::runtime::DispatchCtx::ambient(), workspace_dir)
+}