Skip to content

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

feat(media): add generation progress tracking

Steven Enamakel3b2a916bd796
diff --git a/crates/openhuman-core/src/media/generation/progress.rs b/crates/openhuman-core/src/media/generation/progress.rs
index e6d929d52f..462a2206f1 100644
--- a/crates/openhuman-core/src/media/generation/progress.rs
+++ b/crates/openhuman-core/src/media/generation/progress.rs
@@ -9,10 +9,11 @@
//! [`HEARTBEAT_INTERVAL`] while the inner tool runs, naming the elapsed time;
//! - the **video job state** ([`video_status_observer`]), the
//! `WaitPolicy::progress` hook the TinyInference poll loop calls on every
-//! poll. The policy is built once per tool and shared by every call, so the
-//! observer cannot hold a call's context; it records the state into a
-//! task-local [`with_progress_heartbeat`] scopes around the call (the poll
-//! loop runs inline on the tool's task), and the next heartbeat includes it.
+//! poll. [`MediaArtifactTool`](super::artifact_tool::MediaArtifactTool)
+//! builds a fresh video tool for each call, bound to a [`JobStateSlot`] it
+//! owns, so the observer records into that call's slot and the next
+//! heartbeat reads it. Nothing is ambient: two concurrent calls each have
+//! their own slot.
//!
//! The host projects `ToolProgressDetail` onto the web channel as a
//! `tool_progress` event for the parent call
@@ -27,26 +28,20 @@ use tinytools::{ToolProgress, ToolRunContext};
/// How often a running media call reports that it is still working.
pub(crate) const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(15);
-tokio::task_local! {
- /// The latest provider job state seen by [`video_status_observer`] for the
- /// call running on this task.
- static JOB_STATE: Arc<Mutex<Option<String>>>;
-}
+/// The latest provider job state seen by [`video_status_observer`] for one
+/// media call. Owned by that call and shared with its heartbeat.
+pub(crate) type JobStateSlot = Arc<Mutex<Option<String>>>;
-/// The `WaitPolicy::progress` observer: records each poll's job state for the
-/// heartbeat of the call it runs under. A poll outside a heartbeat scope (a
-/// direct `execute` with no context) is dropped.
-pub(crate) fn video_status_observer() -> ProgressFn {
- Arc::new(|status: &VideoJobStatus| {
+/// The `WaitPolicy::progress` observer for one call: records each poll's job
+/// state into `slot` for that call's heartbeat.
+pub(crate) fn video_status_observer(slot: JobStateSlot) -> ProgressFn {
+ Arc::new(move |status: &VideoJobStatus| {
let state = status.state.to_string();
- let recorded = JOB_STATE.try_with(|slot| {
- *slot
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(state.clone());
- });
+ *slot
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(state.clone());
tracing::trace!(
job_state = %state,
- recorded = recorded.is_ok(),
"[media_generation] video job polled"
);
})
@@ -67,20 +62,20 @@ pub(crate) fn heartbeat_message(label: &str, elapsed: Duration, job_state: Optio
}
/// Run `fut`, reporting a heartbeat through `context` every `interval` until
-/// it finishes. Without a context there is nowhere to report, so `fut` runs
-/// as is.
+/// it finishes, naming the job state recorded in `state`. Without a context
+/// there is nowhere to report, so `fut` runs as is.
pub(crate) async fn with_progress_heartbeat<F: std::future::Future>(
context: Option<&dyn ToolRunContext>,
label: &str,
interval: Duration,
+ state: JobStateSlot,
fut: F,
) -> F::Output {
let Some(context) = context else {
return fut.await;
};
- let state: Arc<Mutex<Option<String>>> = Arc::default();
let started = tokio::time::Instant::now();
- let work = JOB_STATE.scope(state.clone(), fut);
+ let work = fut;
tokio::pin!(work);
let mut ticker = tokio::time::interval_at(tokio::time::Instant::now() + interval, interval);
loop {
diff --git a/crates/openhuman-core/src/media/generation/progress_tests.rs b/crates/openhuman-core/src/media/generation/progress_tests.rs
index 62e07a51f1..799f5c3e22 100644
--- a/crates/openhuman-core/src/media/generation/progress_tests.rs
+++ b/crates/openhuman-core/src/media/generation/progress_tests.rs
@@ -46,7 +46,8 @@ fn heartbeat_names_the_elapsed_time_and_job_state() {
#[tokio::test(start_paused = true)]
async fn a_long_call_reports_progress_with_the_polled_job_state() {
let recorder = Recorder::default();
- let observer = video_status_observer();
+ let slot = JobStateSlot::default();
+ let observer = video_status_observer(slot.clone());
let work = async {
observer(&status(JobState::Pending));
tokio::time::sleep(Duration::from_secs(20)).await;
@@ -58,6 +59,7 @@ async fn a_long_call_reports_progress_with_the_polled_job_state() {
Some(&recorder),
"Generating video",
HEARTBEAT_INTERVAL,
+ slot,
work,
)
.await;
@@ -79,6 +81,7 @@ async fn a_quick_call_reports_nothing() {
Some(&recorder),
"Generating image",
HEARTBEAT_INTERVAL,
+ JobStateSlot::default(),
async {
tokio::time::sleep(Duration::from_secs(3)).await;
1
@@ -90,7 +93,10 @@ async fn a_quick_call_reports_nothing() {
}
#[test]
-fn a_poll_outside_a_call_is_dropped() {
- // No heartbeat scope: the observer must not panic.
- video_status_observer()(&status(JobState::InProgress));
+fn concurrent_calls_record_into_their_own_slots() {
+ let (a, b) = (JobStateSlot::default(), JobStateSlot::default());
+ video_status_observer(a.clone())(&status(JobState::Pending));
+ video_status_observer(b.clone())(&status(JobState::InProgress));
+ assert_eq!(a.lock().unwrap().as_deref(), Some("pending"));
+ assert_eq!(b.lock().unwrap().as_deref(), Some("in_progress"));
}