Skip to content

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

refactor(runtime): thread dispatch context through import and typing tasks

Steven Enamakel65e4602cdd70
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
index 1430171449..902f59a767 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/helpers.rs
@@ -17,6 +17,7 @@ pub(super) fn log_worker_join_result(result: Result<(), tokio::task::JoinError>)
/// Start the portable typing lifecycle with OpenHuman's configured interval.
pub(super) fn spawn_typing_task(
+ dispatch: &crate::core::runtime::DispatchCtx,
channel: std::sync::Arc<dyn crate::channels::Channel>,
recipient: String,
cancellation_token: tokio_util::sync::CancellationToken,
@@ -24,7 +25,7 @@ pub(super) fn spawn_typing_task(
let (finished_tx, finished_rx) = tokio::sync::oneshot::channel();
// The worker retains ownership across cancellation of the caller while
// StartTypingRefresh is in flight, then closes the returned module handle.
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ dispatch.spawn(async move {
match channel
.begin_typing_refresh(&recipient, CHANNEL_TYPING_REFRESH_INTERVAL_SECS)
.await
@@ -43,7 +44,7 @@ pub(super) fn spawn_typing_task(
}
let _ = finished_tx.send(());
});
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ dispatch.spawn(async move {
let _ = finished_rx.await;
})
}
diff --git a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
index 9ec2da5ae9..64730cccf6 100644
--- a/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
+++ b/crates/openhuman-core/src/channels/runtime/dispatch/processor/turn.rs
@@ -388,6 +388,7 @@ pub(crate) async fn process_channel_runtime_message(
// spawn the background refresh task that keeps the indicator alive during long turns.
let typing_task = match (target_channel.as_ref(), typing_cancellation.as_ref()) {
(Some(channel), Some(token)) => Some(AbortOnDropHandle::new(spawn_typing_task(
+ &ctx.dispatch_ctx(),
Arc::clone(channel),
msg.reply_target.clone(),
token.clone(),
diff --git a/crates/openhuman-core/src/core/runtime/bootstrap.rs b/crates/openhuman-core/src/core/runtime/bootstrap.rs
index 280583d039..c001f32347 100644
--- a/crates/openhuman-core/src/core/runtime/bootstrap.rs
+++ b/crates/openhuman-core/src/core/runtime/bootstrap.rs
@@ -39,7 +39,7 @@ pub(crate) async fn bootstrap_core_runtime(
// SQLite files (approvals, devices, notifications, task sources) into it
// once, read-only, before anything below opens those stores. A no-op
// without a backend; failures are logged and retried at the next boot.
- for report in crate::storage::import::run(&cfg).await {
+ for report in crate::storage::import::run(ctx, &cfg).await {
if let Err(error) = report.outcome {
log::warn!(
"[runtime] legacy {} store import failed: {error:#}",
diff --git a/crates/openhuman-core/src/storage/import/mod.rs b/crates/openhuman-core/src/storage/import/mod.rs
index 68597bb126..f68e0dd981 100644
--- a/crates/openhuman-core/src/storage/import/mod.rs
+++ b/crates/openhuman-core/src/storage/import/mod.rs
@@ -178,7 +178,7 @@ pub fn sources() -> Vec<Source> {
///
/// Failures are logged and returned in the reports; none stops boot, since
/// the legacy files are untouched and the import is retried next time.
-pub async fn run(config: &Config) -> Vec<Report> {
+pub async fn run(ctx: &crate::core::runtime::DispatchCtx, config: &Config) -> Vec<Report> {
let Some(backend) = super::installed() else {
return Vec::new();
};
@@ -190,8 +190,9 @@ pub async fn run(config: &Config) -> Vec<Report> {
return Vec::new();
}
let config = config.clone();
- let joined =
- crate::core::runtime::DispatchCtx::ambient().spawn_blocking(move || run_all(&backend, &config)).await;
+ let joined = ctx
+ .spawn_blocking(move || run_all(&backend, &config))
+ .await;
match joined {
Ok(reports) => reports,
Err(error) => {
diff --git a/tests/storage_desktop_upgrade_e2e.rs b/tests/storage_desktop_upgrade_e2e.rs
index ef3e8907f0..b4576ed8f1 100644
--- a/tests/storage_desktop_upgrade_e2e.rs
+++ b/tests/storage_desktop_upgrade_e2e.rs
@@ -442,7 +442,7 @@ async fn a_desktop_upgrade_is_lossless_and_leaves_the_legacy_files_untouched(cas
};
// First boot: every host importer runs and none is left without a source.
- let reports = import::run(&config).await;
+ let reports = import::run(&openhuman_core::core::runtime::DispatchCtx::process_default(), &config).await;
assert!(!reports.is_empty());
for report in &reports {
match report.outcome.as_ref().unwrap_or_else(|error| {
@@ -542,7 +542,7 @@ async fn a_desktop_upgrade_is_lossless_and_leaves_the_legacy_files_untouched(cas
let _ = fixture.ccr_originals;
// Second boot: nothing runs again, so nothing deleted comes back.
- let again = import::run(&config).await;
+ let again = import::run(&openhuman_core::core::runtime::DispatchCtx::process_default(), &config).await;
assert_eq!(again.len(), reports.len());
for report in &again {
assert_eq!(
diff --git a/tests/storage_flows_e2e.rs b/tests/storage_flows_e2e.rs
index 8e093622e7..95c4b00dd4 100644
--- a/tests/storage_flows_e2e.rs
+++ b/tests/storage_flows_e2e.rs
@@ -175,7 +175,10 @@ async fn legacy_tinyflows_files_are_imported_into_a_configured_backend(case: Cas
let before = digest(workspace.path());
case.install();
- for report in openhuman_core::storage::import::run(&config).await {
+ for report in openhuman_core::storage::import::run(
+ &openhuman_core::core::runtime::DispatchCtx::process_default(),
+ &config,
+ ).await {
report.outcome.unwrap();
}
@@ -191,7 +194,10 @@ async fn legacy_tinyflows_files_are_imported_into_a_configured_backend(case: Cas
.unwrap();
// Run again: the markers say it is done, and nothing is rewritten.
- for report in openhuman_core::storage::import::run(&config).await {
+ for report in openhuman_core::storage::import::run(
+ &openhuman_core::core::runtime::DispatchCtx::process_default(),
+ &config,
+ ).await {
report.outcome.unwrap();
}
assert_eq!(