Skip to content

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

refactor(tinyagents): extract turn drive event and store setup into submodules

Steven Enamakelb01129c2d57a
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 dce40fdbcb..a041c47bce 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive.rs
@@ -1,5 +1,10 @@
use super::*;
+#[path = "drive/events.rs"]
+mod events;
+#[path = "drive/stores.rs"]
+mod stores;
+
pub(super) async fn drive_turn(
spec: TurnSpec<'_>,
hosted_root: Option<(
@@ -198,36 +203,13 @@ pub(super) async fn drive_turn(
ctx.events = EventSink::with_stream_id(journal_run_id.as_str());
}
let run_instance_id = ctx.instance_id();
- // Assemble the run's store registry: the tool-result artifact index (when
- // present) and — behind the default-ON session dual-write flag — the
- // session KV store, so the harness carries a handle to the same
- // `{workspace}/tinyagents_store/kv` tree the live dual-write mirrors into
- // (issue #4249, 04.1). Both stores share one registry so neither clobbers
- // the other. Reads stay legacy until 04.2; this registration is additive
- // and best-effort (a workspace-resolve failure just skips it).
- let mut stores: Option<StoreRegistry> = None;
- if let Some(index) = tool_result_artifact_index.clone() {
- stores
- .get_or_insert_with(StoreRegistry::new)
- .register(TINYAGENTS_TOOL_RESULT_ARTIFACT_STORE, index);
- }
- // `session_kv_store` self-gates on the dual-write flag (config default ON +
- // env kill switch), returning `None` when disabled or unresolvable.
- if let Some(session_kv) =
- crate::storage::import::live::session_kv_store(&run_context.dispatch_ctx()).await
+ ctx = if let Some(stores) =
+ stores::attach(&run_context, tool_result_artifact_index.clone()).await
{
- stores.get_or_insert_with(StoreRegistry::new).register(
- crate::storage::import::live::TINYAGENTS_SESSION_KV_STORE,
- session_kv,
- );
- tracing::debug!(
- "[session-store] registered session kv store on RunContext.stores under '{}'",
- crate::storage::import::live::TINYAGENTS_SESSION_KV_STORE
- );
- }
- if let Some(stores) = stores {
- ctx = ctx.with_stores(stores);
- }
+ ctx.with_stores(stores)
+ } else {
+ ctx
+ };
let streaming = on_progress.is_some();
// Retain a clone of the progress sink so the turn can emit a terminal
@@ -238,110 +220,32 @@ pub(super) async fn drive_turn(
// #4457 (defect C): suppressed entirely when `defer_turn_completed_to_caller`
// is set — the caller (chat/session path) emits the single terminal
// `TurnCompleted` itself, after its post-run wrap-up finishes streaming.
- let turn_completed_sink = (!is_subagent && !defer_turn_completed_to_caller)
- .then(|| on_progress.clone())
- .flatten();
- // The EventSink is created unconditionally (bridge, cap pauser and the
- // durable journal of issue #4249, 05.1 all subscribe; it is inert when
- // nothing does), so an unobserved turn stays reconstructable.
- //
- // 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 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(),
- });
- }
- // 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 = {
- let bridge = OpenhumanEventBridge::with_scope(
- on_progress,
- model,
- provider_id.clone(),
- max_iterations,
- subagent_scope.clone(),
- cursor.clone(),
- tool_names.clone(),
- failure_map.clone(),
- provider_usage_carry.clone(),
- bridge_tool_sets.clone(),
- run_context.dispatch_ctx(),
- );
- bridge.set_definition(
- run_context
- .memory_identity
- .as_ref()
- .and_then(|identity| identity.agent_id.as_deref()),
- );
- sink.subscribe(bridge.clone());
- bridge
- };
-
- // Durable event journal + status store (issue #4249, 05.1). Attached *in
- // addition to* the bridge above: the EventSink fans out to both, so the
- // existing progress/global-bus path is untouched. Best-effort and non-fatal
- // — a failure to open/attach the journal returns `None` and the turn runs
- // unaffected. The handle stamps the terminal status once the run returns.
- // A sub-agent turn records under its task scope as the status thread id, so
- // `list_by_thread` can enumerate a task's runs (full parent/root lineage is
- // a 05.2/05.3 follow-up).
- let journal_thread_id = subagent_scope
- .as_ref()
- .map(|scope| tinyagents_harness::ids::ThreadId::new(scope.task_id.clone()));
- let turn_journal = journal::attach_turn_journal(
- &run_context.dispatch_ctx(),
- &sink,
- model,
+ let event_wiring = events::attach(
+ on_progress,
+ is_subagent,
+ defer_turn_completed_to_caller,
journal_run_id.clone(),
- journal_thread_id,
- ctx.config.lineage.parent_run_id.clone(),
- ctx.config.lineage.root_run_id.clone(),
+ native_subagent.as_ref(),
+ model,
+ provider_id.clone(),
+ max_iterations,
+ subagent_scope.as_ref(),
+ &cursor,
+ &tool_names,
+ &failure_map,
+ &provider_usage_carry,
+ bridge_tool_sets,
+ &run_context,
+ &mut ctx,
)
.await;
- if let Some(native) = &native_subagent {
- *native
- .child_journal
- .lock()
- .unwrap_or_else(std::sync::PoisonError::into_inner) = turn_journal.clone();
- }
- if !is_subagent {
- if let Some(crate::security::turn_origin::AgentTurnOrigin::WebChat {
- request_id: Some(request_id),
- ..
- }) = run_context.origin.as_ref()
- {
- journal::register_request_journal_run(
- &run_context.dispatch_ctx(),
- request_id,
- journal_run_id.as_str(),
- );
- }
- }
- ctx = ctx.with_events(sink.clone());
+ let events::EventWiring {
+ sink,
+ bridge,
+ turn_journal,
+ turn_completed_sink,
+ } = event_wiring;
- // Per-turn controls for the driver. Best-effort thread label for the
- // delivery/requeue observability events and the metadata on any requeued
- // steer: a sub-agent uses its task id; the interactive/channel parent turn
- // reads the turn origin.
let steer_thread_label =
steering_forwarder::thread_label(subagent_scope.as_ref(), run_context.origin.as_ref());
let controls = TurnControls {
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/events.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/events.rs
new file mode 100644
index 0000000000..01201b5946
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/events.rs
@@ -0,0 +1,139 @@
+use super::*;
+
+pub(super) struct EventWiring {
+ pub(super) sink: EventSink,
+ pub(super) bridge: OpenhumanEventBridge,
+ pub(super) turn_journal: Option<journal::TurnJournalHandle>,
+ pub(super) turn_completed_sink: Option<tokio::sync::mpsc::Sender<AgentProgress>>,
+}
+
+#[allow(clippy::too_many_arguments)]
+pub(super) async fn attach(
+ on_progress: Option<tokio::sync::mpsc::Sender<AgentProgress>>,
+ is_subagent: bool,
+ defer_turn_completed_to_caller: bool,
+ journal_run_id: String,
+ native_subagent: Option<&NativeSubagentRunContext>,
+ model: &str,
+ provider_id: String,
+ max_iterations: usize,
+ subagent_scope: Option<&SubagentScope>,
+ cursor: &tinyagents_harness::events::IterationCursor,
+ tool_names: &crate::agent::tinyagents::observability::ToolNameMap,
+ failure_map: &crate::agent::tinyagents::observability::ToolFailureMap,
+ provider_usage_carry: &crate::agent::tinyagents::observability::ProviderUsageCarry,
+ bridge_tool_sets: Vec<Arc<Vec<Box<dyn tinytools::Tool>>>>,
+ run_context: &OpenHumanRunContext,
+ ctx: &mut tinyagents_harness::context::RunContext<OpenHumanRunContext>,
+) -> EventWiring {
+ let turn_completed_sink = (!is_subagent && !defer_turn_completed_to_caller)
+ .then(|| on_progress.clone())
+ .flatten();
+ // The EventSink is created unconditionally (bridge, cap pauser and the
+ // durable journal of issue #4249, 05.1 all subscribe; it is inert when
+ // nothing does), so an unobserved turn stays reconstructable.
+ //
+ // 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 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(),
+ });
+ }
+ // 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 = {
+ let bridge = OpenhumanEventBridge::with_scope(
+ on_progress,
+ model,
+ provider_id.clone(),
+ max_iterations,
+ subagent_scope.clone(),
+ cursor.clone(),
+ tool_names.clone(),
+ failure_map.clone(),
+ provider_usage_carry.clone(),
+ bridge_tool_sets.clone(),
+ run_context.dispatch_ctx(),
+ );
+ bridge.set_definition(
+ run_context
+ .memory_identity
+ .as_ref()
+ .and_then(|identity| identity.agent_id.as_deref()),
+ );
+ sink.subscribe(bridge.clone());
+ bridge
+ };
+
+ // Durable event journal + status store (issue #4249, 05.1). Attached *in
+ // addition to* the bridge above: the EventSink fans out to both, so the
+ // existing progress/global-bus path is untouched. Best-effort and non-fatal
+ // — a failure to open/attach the journal returns `None` and the turn runs
+ // unaffected. The handle stamps the terminal status once the run returns.
+ // A sub-agent turn records under its task scope as the status thread id, so
+ // `list_by_thread` can enumerate a task's runs (full parent/root lineage is
+ // a 05.2/05.3 follow-up).
+ let journal_thread_id = subagent_scope
+ .as_ref()
+ .map(|scope| tinyagents_harness::ids::ThreadId::new(scope.task_id.clone()));
+ let turn_journal = journal::attach_turn_journal(
+ &run_context.dispatch_ctx(),
+ &sink,
+ model,
+ journal_run_id.clone(),
+ journal_thread_id,
+ ctx.config.lineage.parent_run_id.clone(),
+ ctx.config.lineage.root_run_id.clone(),
+ )
+ .await;
+ if let Some(native) = &native_subagent {
+ *native
+ .child_journal
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = turn_journal.clone();
+ }
+ if !is_subagent {
+ if let Some(crate::security::turn_origin::AgentTurnOrigin::WebChat {
+ request_id: Some(request_id),
+ ..
+ }) = run_context.origin.as_ref()
+ {
+ journal::register_request_journal_run(
+ &run_context.dispatch_ctx(),
+ request_id,
+ journal_run_id.as_str(),
+ );
+ }
+ }
+ ctx = ctx.with_events(sink.clone());
+
+ // Per-turn controls for the driver. Best-effort thread label for the
+ // delivery/requeue observability events and the metadata on any requeued
+ // steer: a sub-agent uses its task id; the interactive/channel parent turn
+ // reads the turn origin.
+ EventWiring {
+ sink,
+ bridge,
+ turn_journal,
+ turn_completed_sink,
+ }
+}
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/stores.rs b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/stores.rs
new file mode 100644
index 0000000000..e0b41f7327
--- /dev/null
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_exec/drive/stores.rs
@@ -0,0 +1,38 @@
+use super::*;
+
+pub(super) async fn attach(
+ run_context: &OpenHumanRunContext,
+ tool_result_artifact_index: Option<ToolResultArtifactIndexStore>,
+) -> Option<StoreRegistry> {
+ // Assemble the run's store registry: the tool-result artifact index (when
+ // present) and — behind the default-ON session dual-write flag — the
+ // session KV store, so the harness carries a handle to the same
+ // `{workspace}/tinyagents_store/kv` tree the live dual-write mirrors into
+ // (issue #4249, 04.1). Both stores share one registry so neither clobbers
+ // the other. Reads stay legacy until 04.2; this registration is additive
+ // and best-effort (a workspace-resolve failure just skips it).
+ let mut stores: Option<StoreRegistry> = None;
+ if let Some(index) = tool_result_artifact_index.clone() {
+ stores
+ .get_or_insert_with(StoreRegistry::new)
+ .register(TINYAGENTS_TOOL_RESULT_ARTIFACT_STORE, index);
+ }
+ // `session_kv_store` self-gates on the dual-write flag (config default ON +
+ // env kill switch), returning `None` when disabled or unresolvable.
+ if let Some(session_kv) =
+ crate::storage::import::live::session_kv_store(&run_context.dispatch_ctx()).await
+ {
+ stores.get_or_insert_with(StoreRegistry::new).register(
+ crate::storage::import::live::TINYAGENTS_SESSION_KV_STORE,
+ session_kv,
+ );
+ tracing::debug!(
+ "[session-store] registered session kv store on RunContext.stores under '{}'",
+ crate::storage::import::live::TINYAGENTS_SESSION_KV_STORE
+ );
+ }
+ if let Some(stores) = stores {
+ ctx = ctx.with_stores(stores);
+ }
+ stores
+}