Skip to content

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

refactor(web_chat): introduce progress helpers module

Steven Enamakel14cb44726542
diff --git a/crates/openhuman-core/src/web_chat/turn_host.rs b/crates/openhuman-core/src/web_chat/turn_host.rs
index dbf8e84b54..d734628e8c 100644
--- a/crates/openhuman-core/src/web_chat/turn_host.rs
+++ b/crates/openhuman-core/src/web_chat/turn_host.rs
@@ -42,6 +42,11 @@ use crate::core::events::DomainEvent;
use crate::core::runtime::DispatchCtx;
use tinyagents_harness::progress::AgentProgress;
+#[path = "turn_host_progress.rs"]
+mod progress_helpers;
+use progress_helpers::{settle_turn_snapshot, spawn_heartbeat, tap_progress, AbortOnDrop};
+
+
/// How long a finished turn waits for its progress tap to forward every event
/// the turn queued before the terminal `chat_done`/`chat_error` is published.
/// Bounded: a detached sub-agent can hold a sender clone and keep the channel
@@ -59,15 +64,6 @@ pub(super) struct WebHost {
shared: Arc<Shared>,
}
-/// Aborts a task when dropped.
-struct AbortOnDrop(tokio::task::JoinHandle<()>);
-
-impl Drop for AbortOnDrop {
- fn drop(&mut self) {
- self.0.abort();
- }
-}
-
impl WebHost {
pub(super) fn new(shared: Arc<Shared>) -> Self {
Self { shared }
@@ -592,105 +588,6 @@ async fn speak_and_publish(
}
}
-/// Settle the turn's own snapshot now the turn is over.
-///
-/// The projection marks the snapshot terminal on its way out, but it only
-/// finishes once the terminal event has been published, and a detached
-/// sub-agent may still hold a progress sender past the turn. A snapshot left
-/// `Streaming` on disk would hydrate a live "Thinking..." indicator under a
-/// reply that was already delivered. The turn has ended here by construction,
-/// so record that; a projection that later observes `TurnCompleted` overwrites
-/// it with `Completed`, terminal either way.
-fn settle_turn_snapshot(
- config: &crate::config::Config,
- request: &TurnRequest<QueuedPayload>,
- completed: bool,
-) {
- let lifecycle = if completed {
- tinyagents_session::turn_state::TurnLifecycle::Completed
- } else {
- tinyagents_session::turn_state::TurnLifecycle::Interrupted
- };
- let now = chrono::Utc::now().to_rfc3339();
- if let Err(err) = crate::agent::session_store::turn_states_for(&config.workspace_dir)
- .settle_turn(&request.thread_id, &request.request_id, lifecycle, &now)
- {
- log::warn!(
- "[web-channel] failed to settle turn snapshot client={} thread={} request_id={}: {err}",
- request.payload.client_id,
- request.thread_id,
- request.request_id
- );
- }
-}
-
-/// Forward the agent's progress to the runner, folding each event into the
-/// idle record and the work tracker on the way. Signals `drained` once the
-/// parent's `TurnCompleted` was forwarded or the stream ended.
-fn tap_progress(
- ctx: &DispatchCtx,
- mut rx: mpsc::Receiver<AgentProgress>,
- progress: mpsc::Sender<TurnSignal>,
- activity: TurnActivity,
- drained: watch::Sender<bool>,
-) -> tokio::task::JoinHandle<()> {
- ctx.spawn(async move {
- let mut work = WorkTracker::default();
- while let Some(event) = rx.recv().await {
- activity.observe(&event);
- let completed = matches!(event, AgentProgress::TurnCompleted { .. });
- if let Some(signal) = work.observe(&event) {
- if progress.send(signal).await.is_err() {
- break;
- }
- }
- if progress
- .send(TurnSignal::Progress(Box::new(event)))
- .await
- .is_err()
- {
- break;
- }
- if completed {
- let _ = drained.send(true);
- }
- }
- let _ = drained.send(true);
- })
-}
-
-/// The `inference_heartbeat` liveness beat for the turn's projection.
-fn spawn_heartbeat(
- ctx: &DispatchCtx,
- projection: Arc<AsyncMutex<ProgressProjection>>,
-) -> tokio::task::JoinHandle<()> {
- spawn_heartbeat_with_interval(
- ctx,
- projection,
- Duration::from_secs(INFERENCE_HEARTBEAT_SECS),
- )
-}
-
-fn spawn_heartbeat_with_interval(
- ctx: &DispatchCtx,
- projection: Arc<AsyncMutex<ProgressProjection>>,
- interval: Duration,
-) -> tokio::task::JoinHandle<()> {
- ctx.spawn(async move {
- let mut beat = tokio::time::interval(interval);
- // Wall-clock cadence: a slow turn must not produce a burst of catch-up
- // beats once it finally yields control.
- beat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
- // The first tick resolves at once; the first real beat lands one full
- // interval after the turn begins.
- beat.tick().await;
- loop {
- beat.tick().await;
- projection.lock().await.heartbeat().await;
- }
- })
-}
-
#[async_trait]
impl TurnHost<QueuedPayload> for WebHost {
async fn checkout(&self, request: &TurnRequest<QueuedPayload>) -> Result<TurnSession, String> {
diff --git a/crates/openhuman-core/src/web_chat/turn_host_progress.rs b/crates/openhuman-core/src/web_chat/turn_host_progress.rs
new file mode 100644
index 0000000000..b72be95e94
--- /dev/null
+++ b/crates/openhuman-core/src/web_chat/turn_host_progress.rs
@@ -0,0 +1,119 @@
+use std::time::Duration;
+
+use super::progress::{ProgressProjection, INFERENCE_HEARTBEAT_SECS};
+use super::turn_activity::TurnActivity;
+use super::types::WebChatTaskResult;
+use crate::core::runtime::DispatchCtx;
+use tinyagents_harness::progress::AgentProgress;
+use tinyagents_harness::turn_runner::TurnRequest;
+use crate::agent::queued_turn::QueuedPayload;
+use tokio::sync::{mpsc, watch};
+
+/// Aborts a task when dropped.
+pub(super) struct AbortOnDrop(tokio::task::JoinHandle<()>);
+
+impl Drop for AbortOnDrop {
+ fn drop(&mut self) {
+ self.0.abort();
+ }
+}
+
+/// Settle the turn's own snapshot now the turn is over.
+///
+/// The projection marks the snapshot terminal on its way out, but it only
+/// finishes once the terminal event has been published, and a detached
+/// sub-agent may still hold a progress sender past the turn. A snapshot left
+/// `Streaming` on disk would hydrate a live "Thinking..." indicator under a
+/// reply that was already delivered. The turn has ended here by construction,
+/// so record that; a projection that later observes `TurnCompleted` overwrites
+/// it with `Completed`, terminal either way.
+pub(super) fn settle_turn_snapshot(
+ config: &crate::config::Config,
+ request: &TurnRequest<QueuedPayload>,
+ completed: bool,
+) {
+ let lifecycle = if completed {
+ tinyagents_session::turn_state::TurnLifecycle::Completed
+ } else {
+ tinyagents_session::turn_state::TurnLifecycle::Interrupted
+ };
+ let now = chrono::Utc::now().to_rfc3339();
+ if let Err(err) = crate::agent::session_store::turn_states_for(&config.workspace_dir)
+ .settle_turn(&request.thread_id, &request.request_id, lifecycle, &now)
+ {
+ log::warn!(
+ "[web-channel] failed to settle turn snapshot client={} thread={} request_id={}: {err}",
+ request.payload.client_id,
+ request.thread_id,
+ request.request_id
+ );
+ }
+}
+
+/// Forward the agent's progress to the runner, folding each event into the
+/// idle record and the work tracker on the way. Signals `drained` once the
+/// parent's `TurnCompleted` was forwarded or the stream ended.
+pub(super) fn tap_progress(
+ ctx: &DispatchCtx,
+ mut rx: mpsc::Receiver<AgentProgress>,
+ progress: mpsc::Sender<TurnSignal>,
+ activity: TurnActivity,
+ drained: watch::Sender<bool>,
+) -> tokio::task::JoinHandle<()> {
+ ctx.spawn(async move {
+ let mut work = WorkTracker::default();
+ while let Some(event) = rx.recv().await {
+ activity.observe(&event);
+ let completed = matches!(event, AgentProgress::TurnCompleted { .. });
+ if let Some(signal) = work.observe(&event) {
+ if progress.send(signal).await.is_err() {
+ break;
+ }
+ }
+ if progress
+ .send(TurnSignal::Progress(Box::new(event)))
+ .await
+ .is_err()
+ {
+ break;
+ }
+ if completed {
+ let _ = drained.send(true);
+ }
+ }
+ let _ = drained.send(true);
+ })
+}
+
+/// The `inference_heartbeat` liveness beat for the turn's projection.
+pub(super) fn spawn_heartbeat(
+ ctx: &DispatchCtx,
+ projection: Arc<AsyncMutex<ProgressProjection>>,
+) -> tokio::task::JoinHandle<()> {
+ spawn_heartbeat_with_interval(
+ ctx,
+ projection,
+ Duration::from_secs(INFERENCE_HEARTBEAT_SECS),
+ )
+}
+
+pub(super) fn spawn_heartbeat_with_interval(
+ ctx: &DispatchCtx,
+ projection: Arc<AsyncMutex<ProgressProjection>>,
+ interval: Duration,
+) -> tokio::task::JoinHandle<()> {
+ ctx.spawn(async move {
+ let mut beat = tokio::time::interval(interval);
+ // Wall-clock cadence: a slow turn must not produce a burst of catch-up
+ // beats once it finally yields control.
+ beat.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip);
+ // The first tick resolves at once; the first real beat lands one full
+ // interval after the turn begins.
+ beat.tick().await;
+ loop {
+ beat.tick().await;
+ projection.lock().await.heartbeat().await;
+ }
+ })
+}
+