Skip to content

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

refactor(tinyagents): extract supplemental call filtering into helper

Steven Enamakel40698711ebb9
diff --git a/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs b/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs
index 196b77db90..91f0f5216c 100644
--- a/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/runtime_adapter_tests.rs
@@ -19,141 +19,6 @@ use crate::integrations::announcement_notes::{
integration_announcement_note, mcp_announcement_note, skill_announcement_note,
};
-struct GatedRepairModel {
- calls: std::sync::atomic::AtomicUsize,
- repair_started: tokio::sync::Notify,
-}
-
-#[async_trait]
-impl ChatModel<()> for GatedRepairModel {
- fn profile(&self) -> Option<&ModelProfile> {
- static PROFILE: std::sync::LazyLock<ModelProfile> =
- std::sync::LazyLock::new(|| ModelProfile {
- provider: Some("openai".into()),
- tool_calling: true,
- ..ModelProfile::default()
- });
- Some(&PROFILE)
- }
-
- async fn invoke(
- &self,
- _state: &(),
- _request: ModelRequest,
- ) -> tinyinference_llm::Result<ModelResponse> {
- if self.calls.fetch_add(1, std::sync::atomic::Ordering::SeqCst) == 0 {
- return Ok(ModelResponse::assistant(
- "The answer omits its required block.",
- ));
- }
- self.repair_started.notify_one();
- std::future::pending().await
- }
-}
-
-/// Cancellation during the host's required-output repair happens after the
-/// harness already emitted RunCompleted. The host terminal hook must append a
-/// stronger cancelled fact and finalize the same durable run after Tiny saves
-/// its recoverable partial.
-#[tokio::test]
-async fn cancellation_during_required_output_repair_finalizes_native_run() {
- use crate::storage::sessions::ExecutionStatus;
- use tinyagents_harness::events::AgentEvent;
- use tinyagents_harness::terminal::TerminalReason;
-
- let scratch = tempfile::tempdir().expect("isolated workspace");
- let workspace = scratch.path().join("workspace");
- let mut config = crate::config::Config::default();
- config.workspace_dir = workspace.clone();
- config.agent.required_output = Some(crate::config::RequiredOutputContract::new("answer"));
- config.components.juice = false;
- let core = crate::core::runtime::CoreContext::for_test_with_config(
- crate::core::runtime::DomainSet::full(),
- config.clone(),
- );
- let model = Arc::new(GatedRepairModel {
- calls: std::sync::atomic::AtomicUsize::new(0),
- repair_started: tokio::sync::Notify::new(),
- });
- let chat_model: Arc<dyn ChatModel<()>> = model.clone();
- let mut host = crate::agent::SessionHostBuilder::new()
- .chat_model_with_config(chat_model, Arc::new(config.clone()))
- .config(config.agent.clone())
- .tools(Vec::new())
- .workspace_dir(workspace.clone())
- .action_dir(scratch.path().to_path_buf())
- .tool_dispatcher(Box::new(tinytools_agent::dialect::NativeDialect))
- .with_session_history_locator(
- crate::storage::sessions::splice_fixture_tests::memory_locator(),
- )
- .build()
- .expect("host session");
- let thread_id = "trace-terminal-cancel-repair";
- let request_id = uuid::Uuid::new_v4().to_string();
- host.set_thread_id(Some(thread_id));
- let cancellation = tinyagents_harness::cancel::CancellationToken::new();
- host.set_host_env(crate::agent::turn_env::HostTurnEnv {
- cancellation: Some(cancellation.clone()),
- core: Some(Arc::downgrade(&core)),
- ..Default::default()
- });
-
- let origin = crate::security::turn_origin::AgentTurnOrigin::WebChat {
- thread_id: thread_id.into(),
- client_id: "trace-terminal-test".into(),
- request_id: Some(request_id.clone()),
- };
- let turn =
- tokio::spawn(async move { host.run_single_with_origin("hello", Some(origin)).await });
- model.repair_started.notified().await;
- cancellation.cancel();
- assert!(turn.await.expect("turn task").is_err());
-
- let dispatch = crate::core::runtime::DispatchCtx::new(Arc::clone(&core));
- let run_id =
- crate::agent::tinyagents::journal::take_request_journal_run(&dispatch, &request_id)
- .expect("the web-chat request maps to its actual native run");
- let observations = crate::agent::tinyagents::journal::read_run_events(&dispatch, &run_id, 0)
- .await
- .expect("durable native event stream");
- let failure = observations
- .iter()
- .position(|record| {
- matches!(
- &record.event,
- AgentEvent::RunFailed { run_id: failed, outcome: Some(outcome), .. }
- if failed.as_str() == run_id && outcome.reason == TerminalReason::Cancelled
- )
- })
- .expect("the durable run keeps its actual cancellation outcome");
- let finalized = observations
- .iter()
- .position(|record| {
- matches!(
- &record.event,
- AgentEvent::RunFinalized { run_id: finalized } if finalized.as_str() == run_id
- )
- })
- .expect("the native run is finalized");
- assert!(
- finalized > failure,
- "finalization follows the cancellation fact"
- );
- assert!(
- observations.iter().any(|record| matches!(
- &record.event,
- AgentEvent::RunCompleted { run_id: completed, .. } if completed.as_str() == run_id
- )),
- "the original harness completion is preserved before the host cancellation"
- );
- let status = crate::storage::sessions::status_store_for(&workspace)
- .get_status(&run_id)
- .await
- .expect("status lookup")
- .expect("run status persisted");
- assert_eq!(status.status, ExecutionStatus::Cancelled);
-}
-
struct RecordingDriver(Arc<Mutex<Vec<OpenHumanRunContext>>>);
#[async_trait]
diff --git a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs
index e6c0e3a6b4..5de96ca5b5 100644
--- a/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/observability/event_projection.rs
@@ -13,13 +13,10 @@ use super::event_bridge::OpenhumanEventBridge;
impl EventListener for OpenhumanEventBridge {
fn on_event(&self, record: &EventRecord) {
+ if self.project_supplemental(record) {
+ return;
+ }
match &record.event {
- AgentEvent::ModelStarted { call_id, .. }
- if call_id.as_str().contains("-host-supplemental-") =>
- {
- self.supplemental_call_active
- .store(true, std::sync::atomic::Ordering::SeqCst);
- }
AgentEvent::ModelStarted { .. } => {
let iteration = tinyagents_harness::events::advance_iteration(&self.cursor);
match &self.scope {
@@ -134,12 +131,7 @@ impl EventListener for OpenhumanEventBridge {
// exactly once per model call; prefer it over `ModelCompleted`'s
// optional usage to avoid double counting.
AgentEvent::UsageRecorded { usage } => {
- if !self
- .supplemental_call_active
- .load(std::sync::atomic::Ordering::SeqCst)
- {
- self.record_usage(usage);
- }
+ self.record_usage(usage);
}
// Per-call generation telemetry. `ModelCompleted` fires exactly once
// per model call, after `UsageRecorded`, and is the only event
@@ -149,12 +141,6 @@ impl EventListener for OpenhumanEventBridge {
// child call carries its owning `subagent_task_id` so the trace
// exporter nests the generation under the subagent span (this is
// what makes the Context Scout's model calls visible in Langfuse).
- AgentEvent::ModelCompleted { call_id, .. }
- if call_id.as_str().contains("-host-supplemental-") =>
- {
- self.supplemental_call_active
- .store(false, std::sync::atomic::Ordering::SeqCst);
- }
AgentEvent::ModelCompleted {
usage,
input,
@@ -217,12 +203,6 @@ impl EventListener for OpenhumanEventBridge {
cost_usd: call_cost,
});
}
- AgentEvent::ModelFailed { call_id, .. }
- if call_id.as_str().contains("-host-supplemental-") =>
- {
- self.supplemental_call_active
- .store(false, std::sync::atomic::Ordering::SeqCst);
- }
AgentEvent::CostRecorded { cost } => {
tracing::debug!(
cost = ?cost,
@@ -729,6 +709,38 @@ impl EventListener for OpenhumanEventBridge {
}
}
+impl OpenhumanEventBridge {
+ /// Supplemental host calls belong in the durable trace, but they are not
+ /// agent-loop iterations or live wallet updates.
+ fn project_supplemental(&self, record: &EventRecord) -> bool {
+ match &record.event {
+ AgentEvent::ModelStarted { call_id, .. }
+ if call_id.as_str().contains("-host-supplemental-") =>
+ {
+ self.supplemental_call_active
+ .store(true, std::sync::atomic::Ordering::SeqCst);
+ true
+ }
+ AgentEvent::UsageRecorded { .. }
+ if self
+ .supplemental_call_active
+ .load(std::sync::atomic::Ordering::SeqCst) =>
+ {
+ true
+ }
+ AgentEvent::ModelCompleted { call_id, .. }
+ | AgentEvent::ModelFailed { call_id, .. }
+ if call_id.as_str().contains("-host-supplemental-") =>
+ {
+ self.supplemental_call_active
+ .store(false, std::sync::atomic::Ordering::SeqCst);
+ true
+ }
+ _ => false,
+ }
+ }
+}
+
impl OpenhumanEventBridge {
/// The "unavailable" label/detail pair for a call the crate answered as an
/// unknown tool, or `None` for an ordinary call.