Skip to content

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

feat(web_chat): track turn activity across background tasks

Steven Enamakelc6c8656ef3d2
diff --git a/crates/openhuman-core/src/profiles/background.rs b/crates/openhuman-core/src/profiles/background.rs
index 01ea7aa4b3..18aaec1725 100644
--- a/crates/openhuman-core/src/profiles/background.rs
+++ b/crates/openhuman-core/src/profiles/background.rs
@@ -41,7 +41,7 @@ pub struct TickReport {
/// Start the loop. It runs until the process exits.
pub fn spawn(host: Arc<ProfileHost>) -> tokio::task::JoinHandle<()> {
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ crate::core::runtime::DispatchCtx::host_default().spawn(async move {
log::info!(
"[profiles][background] started (every {}s)",
TICK_INTERVAL.as_secs()
diff --git a/crates/openhuman-core/src/storage/blob_sweep.rs b/crates/openhuman-core/src/storage/blob_sweep.rs
index 7e5e471839..95890d5a38 100644
--- a/crates/openhuman-core/src/storage/blob_sweep.rs
+++ b/crates/openhuman-core/src/storage/blob_sweep.rs
@@ -174,7 +174,7 @@ pub async fn sweep_installed() -> u64 {
/// Spawns the sweeper: after `first_delay`, then every `interval`, until the
/// task is aborted. Missed ticks are skipped, not replayed.
pub fn spawn(first_delay: Duration, interval: Duration) -> tokio::task::JoinHandle<()> {
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ crate::core::runtime::DispatchCtx::host_default().spawn(async move {
tracing::debug!(
first_delay_secs = first_delay.as_secs(),
interval_secs = interval.as_secs(),
diff --git a/crates/openhuman-core/src/tinymemes/callbacks.rs b/crates/openhuman-core/src/tinymemes/callbacks.rs
index e9e0d97c90..32725231dd 100644
--- a/crates/openhuman-core/src/tinymemes/callbacks.rs
+++ b/crates/openhuman-core/src/tinymemes/callbacks.rs
@@ -257,7 +257,7 @@ async fn search(
async fn load_index(session: &Session, request: LoadIndexRequest) -> HostResult<Option<String>> {
let config = session.config.clone();
let kind: IndexKind = request.kind;
- match crate::core::runtime::DispatchCtx::ambient().spawn_blocking(move || docs::load(&config, kind)).await {
+ match session.dispatch_ctx().spawn_blocking(move || docs::load(&config, kind)).await {
Ok(Ok(document)) => Ok(document),
Ok(Err(error)) => {
log::warn!("[tinymemes] cannot read index: {error}");
@@ -272,7 +272,7 @@ async fn load_index(session: &Session, request: LoadIndexRequest) -> HostResult<
async fn save_index(session: &Session, request: SaveIndexRequest) -> HostResult<()> {
let config = session.config.clone();
- match crate::core::runtime::DispatchCtx::ambient().spawn_blocking(move || {
+ match session.dispatch_ctx().spawn_blocking(move || {
docs::save(&config, request.kind, request.json)
})
.await
diff --git a/crates/openhuman-core/src/web_chat/run_task.rs b/crates/openhuman-core/src/web_chat/run_task.rs
index abc82c5971..8b48b899af 100644
--- a/crates/openhuman-core/src/web_chat/run_task.rs
+++ b/crates/openhuman-core/src/web_chat/run_task.rs
@@ -164,7 +164,7 @@ pub(crate) async fn run_chat_task(
let (progress_tx, progress_rx) = tokio::sync::mpsc::channel(256);
// Under the web-turn guard, fold every event into the turn's activity
// record (the idle guard's input) on its way to the bridge.
- let progress_rx = super::turn_activity::tap(activity, progress_rx, 256);
+ let progress_rx = super::turn_activity::tap(ctx, activity, progress_rx, 256);
// The channel is fresh here. Record the user input even if the turn fails
// before a committed reply can emit its final TurnContent event.
let _ = progress_tx.try_send(AgentProgress::TurnContent {
diff --git a/crates/openhuman-core/src/web_chat/turn_activity.rs b/crates/openhuman-core/src/web_chat/turn_activity.rs
index 4a2e7a4759..4ed7e9e4cd 100644
--- a/crates/openhuman-core/src/web_chat/turn_activity.rs
+++ b/crates/openhuman-core/src/web_chat/turn_activity.rs
@@ -284,12 +284,13 @@ pub(crate) async fn idle_watchdog(activity: TurnActivity, window: Duration) -> D
/// it drains the agent-side channel faster than the bridge, which publishes.
/// It ends when the agent's sender drops, closing the bridge's stream in turn.
pub(crate) fn tap(
+ ctx: &crate::core::runtime::DispatchCtx,
activity: TurnActivity,
mut agent_rx: mpsc::Receiver<AgentProgress>,
capacity: usize,
) -> mpsc::Receiver<AgentProgress> {
let (bridge_tx, bridge_rx) = mpsc::channel(capacity);
- crate::core::runtime::DispatchCtx::ambient().spawn(async move {
+ ctx.spawn(async move {
while let Some(event) = agent_rx.recv().await {
activity.observe(&event);
if bridge_tx.send(event).await.is_err() {
diff --git a/crates/openhuman-core/src/web_chat/turn_activity_tests.rs b/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
index 419c9e232d..807d1d97fa 100644
--- a/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
+++ b/crates/openhuman-core/src/web_chat/turn_activity_tests.rs
@@ -173,7 +173,7 @@ async fn a_long_tool_call_holds_off_the_watchdog_until_it_closes() {
async fn tap_records_and_forwards_in_order() {
let activity = TurnActivity::new();
let (tx, rx) = mpsc::channel(4);
- let mut bridge_rx = tap(activity.clone(), rx, 4);
+ let mut bridge_rx = tap(&crate::core::runtime::DispatchCtx::ambient(), activity.clone(), rx, 4);
tx.send(tool_started("a")).await.unwrap();
tx.send(text("x")).await.unwrap();
drop(tx);
diff --git a/crates/openhuman-core/src/web_chat/turn_close.rs b/crates/openhuman-core/src/web_chat/turn_close.rs
index dfc5f3c489..3013d9aedf 100644
--- a/crates/openhuman-core/src/web_chat/turn_close.rs
+++ b/crates/openhuman-core/src/web_chat/turn_close.rs
@@ -284,12 +284,13 @@ async fn model_reply(
/// read, and a stalled store must not pin the runtime thread. The scoped spawn
/// carries the caller's tenant, which selects the store.
async fn read_snapshot(
+ ctx: &crate::core::runtime::DispatchCtx,
workspace: std::path::PathBuf,
thread_id: &str,
request_id: &str,
) -> Option<TurnState> {
let (thread_id, request_id) = (thread_id.to_string(), request_id.to_string());
- let read = crate::core::runtime::DispatchCtx::ambient().spawn_blocking(move || {
+ let read = ctx.spawn_blocking(move || {
crate::agent::session_store::turn_states_for(&workspace).get_turn(&thread_id, &request_id)
})
.await;
@@ -324,6 +325,7 @@ pub(crate) async fn close_reply(turn: StoppedTurn<'_>) -> Option<WebChatTaskResu
return None;
};
let snapshot = read_snapshot(
+ &turn.ctx,
config.workspace_dir.clone(),
turn.thread_id,
turn.request_id,