Skip to content

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

test(session_host): cover cancellation during required-output repair

Steven Enamakel551e40988baa
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 8d696565d7..47e8741f99 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
@@ -9,6 +9,7 @@ use tinyagents_runtime::{
use tinyagents_session::port::{InMemorySessionStores, SessionStoreProvider};
use tinyagents_session::transcript::TranscriptMeta;
use tinyinference_llm::message::Message;
+use tinyinference_llm::model::{ChatModel, ModelProfile, ModelRequest, ModelResponse};
use super::runtime_session::tool_surface::reconcile_synthesized_visibility;
use super::runtime_session::{account_committed_turn_against_goal, holistic_last_turn_usage};
@@ -18,6 +19,124 @@ 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 tinyagents_harness::events::AgentEvent;
+ use tinyagents_harness::terminal::TerminalReason;
+ use crate::storage::sessions::ExecutionStatus;
+
+ 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]