Skip to content

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

refactor(tinyagents): move turn setup helpers into setup module

Steven Enamakelf398cffac621
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive.rs
index 0a52583fef..1d7524c49e 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive.rs
@@ -2,10 +2,10 @@ use super::*;
#[path = "drive/controls.rs"]
mod controls;
-#[path = "drive/stores.rs"]
-mod stores;
#[path = "drive/setup.rs"]
mod setup;
+#[path = "drive/stores.rs"]
+mod stores;
pub(super) async fn drive_turn(
spec: TurnSpec<'_>,
@@ -189,26 +189,9 @@ pub(super) async fn drive_turn(
};
let streaming = on_progress.is_some();
- let turn_completed_sink = setup::turn_completed_sink(
- &on_progress,
- is_subagent,
- defer_turn_completed_to_caller,
- );
- //
- // Mint the durable run id *before* the sink and seed the sink stream prefix
- // with it (`with_stream_id`), so every persisted observation's `event_id` is
- // the restart-stable `{run_id}-evt-{offset}` a late-attach replay
- // reconstructs the timeline from (05.1). The same id keys the journal + status.
+ let turn_completed_sink =
+ setup::turn_completed_sink(&on_progress, is_subagent, defer_turn_completed_to_caller);
let sink = setup::event_sink(&journal_run_id, native_subagent.as_ref());
- // Attach the event bridge for EVERY turn — including an unobserved
- // (`on_progress = None`) background/cron turn (#4467, item 3). The bridge's
- // `record_usage` feeds the global cost tracker on each `UsageRecorded` event
- // *during* the run, so a run that burns N model calls and then fails still
- // contributes that spend to the wallet/cost surfaces — the post-run
- // `record_unobserved_turn_usage` fallback only runs on the success path
- // and never sees a failed run's usage. With `on_progress = None` the bridge
- // still records cost but its progress `send`s are inert no-ops, so there is
- // no spurious streaming; every turn has an event sink.
let bridge = setup::event_bridge(
&sink,
on_progress,
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/setup.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/setup.rs
index 948245abdf..c733ffd72c 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/setup.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/setup.rs
@@ -8,7 +8,10 @@ pub(super) fn run_config(
max_iterations: usize,
max_output_tokens: Option<u32>,
model: &str,
-) -> (tinyagents_harness::context::RunConfig, tinyagents_harness::ids::RunId) {
+) -> (
+ tinyagents_harness::context::RunConfig,
+ tinyagents_harness::ids::RunId,
+) {
let mut config = native_subagent
.map(|native| native.config.clone())
.unwrap_or_else(|| {
@@ -53,6 +56,8 @@ pub(super) fn event_bridge(
bridge_tool_sets: Vec<Arc<Vec<Box<dyn tinytools::Tool>>>>,
run_context: &OpenHumanRunContext,
) -> Arc<OpenhumanEventBridge> {
+ // Attach for observed and background turns alike: this bridge records
+ // usage even when progress streaming is disabled.
let bridge = OpenhumanEventBridge::with_scope(
on_progress,
model,
@@ -75,3 +80,37 @@ pub(super) fn event_bridge(
sink.subscribe(bridge.clone());
bridge
}
+
+pub(super) fn turn_completed_sink(
+ on_progress: &Option<tokio::sync::mpsc::Sender<crate::progress::AgentProgress>>,
+ is_subagent: bool,
+ defer_to_caller: bool,
+) -> Option<tokio::sync::mpsc::Sender<crate::progress::AgentProgress>> {
+ // Only the caller emits a top-level completion; session callers may defer
+ // that event until post-run wrap-up is finished.
+ (!is_subagent && !defer_to_caller)
+ .then(|| on_progress.clone())
+ .flatten()
+}
+
+pub(super) fn event_sink(
+ journal_run_id: &tinyagents_harness::ids::RunId,
+ native_subagent: Option<&NativeSubagentRunContext>,
+) -> EventSink {
+ // The stream prefix makes replayed event ids stable across process restart.
+ let sink = EventSink::with_stream_id(journal_run_id.as_str());
+ if let Some(native) = native_subagent {
+ *native
+ .child_events
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(sink.clone());
+ native
+ .parent_events
+ .emit(tinyagents_harness::events::AgentEvent::SubAgentRunStarted {
+ name: native.name.clone(),
+ depth: native.depth,
+ run_id: journal_run_id.clone(),
+ });
+ }
+ sink
+}