Skip to content

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

Merge main into PR 7724

Steven Enamakel705c1dc19548
diff --cc crates/openhuman-core/src/agent/session_host/driver.rs
index b854f69bd0,b14d1d3cb5..bbbc1ea9da
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@@ -623,69 -645,28 +639,91 @@@ fn driver_error_with_snapshot
}
}
+/// A graph error or a Stop drops the harness's `AgentRun`, but its snapshot
+/// has already observed provider-accepted usage and completed tool outcomes.
+/// Copy those into the per-turn sidecar *before* producing the partial so
+/// runtime's single atomic append carries the same usage/failure truth as a
+/// successful turn. The partial history itself remains accepted-only.
+fn record_snapshot_usage(
+ guard: &crate::agent::tinyagents::TranscriptSnapshot,
+ sidecar: &std::sync::Arc<
+ std::sync::Mutex<crate::agent::tinyagents::host::run_context::SessionTurnSidecar>,
+ >,
+ elapsed: std::time::Duration,
+) {
+ let mut observed = sidecar
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ observed.model_calls = guard.model_calls as usize;
+ observed.tool_calls = guard.tool_outcomes.len();
+ observed.input_tokens = guard.input_tokens;
+ observed.output_tokens = guard.output_tokens;
+ observed.cached_input_tokens = guard.cached_input_tokens;
+ observed.last_call_input_tokens = guard.last_call_input_tokens;
+ observed.last_call_output_tokens = guard.last_call_output_tokens;
+ // Each answered call was priced as it arrived (reported charge, else
+ // catalog estimate, else unknown); nothing is re-estimated here.
+ observed.cost = guard.cost;
+ observed.duration = Some(elapsed);
+ observed.tool_outcomes = guard.tool_outcomes.clone();
+ observed.resolved_route = guard.resolved_route.clone();
+}
+
+/// A stopped turn's partial (#7715): the prompt and the accepted rounds, or
+/// just the request before the first model call. No display partial: the
+/// turn_state mirror already records the streamed text.
+fn driver_cancelled_with_snapshot(
+ snapshot: &crate::agent::tinyagents::TranscriptSnapshotSink,
+ sidecar: &std::sync::Arc<
+ std::sync::Mutex<crate::agent::tinyagents::host::run_context::SessionTurnSidecar>,
+ >,
+ elapsed: std::time::Duration,
+ request_history: Vec<Message>,
+) -> DriverFailure {
+ let guard = snapshot
+ .lock()
+ .unwrap_or_else(|poisoned| poisoned.into_inner());
+ record_snapshot_usage(&guard, sidecar, elapsed);
+ let history = if guard.messages.is_empty() {
+ request_history
+ } else {
+ guard.messages[..guard.accepted_end()].to_vec()
+ };
+ DriverFailure {
+ error: RuntimeError::Cancelled,
+ partial: Some(DriverOutcome {
+ history,
+ output: None,
+ partial: None,
+ interrupted: true,
+ outcome: None,
+ }),
+ outcome: None,
+ }
+}
+
+ /// Append the deterministic required-output block when `output` lacks it, and
+ /// make that the durable final assistant row. Used when no model repair may
+ /// run: a classified halt, or a turn out of time before the backstop.
+ fn append_synthesized_block(
+ output: &mut String,
+ history: &mut Vec<Message>,
+ contract: &tinyagents_harness::config::RequiredOutput,
+ ) {
+ if tinyagents_harness::config::output_satisfies_contract(output, contract) {
+ return;
+ }
+ output.push_str("\n\n");
+ output.push_str(&tinyagents_harness::config::synthesize_block(contract));
+ if history
+ .last()
+ .is_some_and(|message| matches!(message, Message::Assistant(_)))
+ {
+ history.pop();
+ }
+ history.push(Message::assistant(output.clone()));
+ }
+
#[cfg(test)]
#[path = "driver_tests.rs"]
mod tests;
diff --cc crates/openhuman-core/src/agent/session_host/driver_tests.rs
index c959066202,6f07a6f45f..75339efade
--- a/crates/openhuman-core/src/agent/session_host/driver_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver_tests.rs
@@@ -269,64 -269,28 +269,90 @@@ fn recorded_declarations_allow_missing_
assert!(!error.error.to_string().contains("retired_tool"));
}
+/// A Stop keeps the prompt and every round the provider accepted (#7715), and
+/// nothing past the last answered request: not streamed text, and not a tool
+/// call or result still in flight, which would replay as a dangling call.
+#[test]
+fn stopped_turn_hands_back_only_its_accepted_rounds() {
+ let request = || vec![Message::user("earlier"), Message::user("stop me")];
+ let cases: [(&str, Vec<Message>, usize, &[&str]); 3] = [
+ (
+ "mid-stream text",
+ vec![
+ Message::user("stop me"),
+ Message::tool("call-1", "committed 6b75a4c"),
+ Message::assistant("streamed, never accepted"),
+ ],
+ 2,
+ &["stop me", "committed 6b75a4c"],
+ ),
+ (
+ "during a tool call",
+ vec![
+ Message::user("stop me"),
+ Message::assistant("response that requested the tool"),
+ Message::tool("call-2", "half-written output"),
+ ],
+ 1,
+ &["stop me"],
+ ),
+ // Before the first model call the snapshot is empty: keep the request.
+ (
+ "before the first model call",
+ vec![],
+ 0,
+ &["earlier", "stop me"],
+ ),
+ ];
+ for (case, messages, accepted_len, expected) in cases {
+ let snapshot = Arc::new(std::sync::Mutex::new(
+ crate::agent::tinyagents::TranscriptSnapshot {
+ messages,
+ accepted_len,
+ request_base_len: 1,
+ model_calls: 2,
+ ..Default::default()
+ },
+ ));
+ let sidecar = sidecar();
+ let failure = driver_cancelled_with_snapshot(
+ &snapshot,
+ &sidecar,
+ std::time::Duration::from_millis(7),
+ request(),
+ );
+ assert_eq!(failure.error, RuntimeError::Cancelled, "{case}");
+ let partial = failure.partial.expect(case);
+ let texts: Vec<String> = partial.history.iter().map(|m| m.text()).collect();
+ assert_eq!(texts, expected, "{case}");
+ assert!(partial.interrupted, "{case}");
+ assert!(partial.partial.is_none(), "{case}: the mirror owns display");
+ assert_eq!(sidecar.lock().unwrap().model_calls, 2, "{case}");
+ }
+}
++
+ /// A turn out of time skips the model repair (#7716); the required block is
+ /// then synthesized once and becomes the durable final row.
+ #[test]
+ fn synthesized_block_is_appended_once_and_replaces_the_final_row() {
+ let contract = tinyagents_harness::config::RequiredOutput {
+ block_key: "result".into(),
+ required_keys: vec!["status".into()],
+ };
+ let mut output = "Done.".to_string();
+ let mut history = vec![Message::user("go"), Message::assistant("Done.")];
+
+ append_synthesized_block(&mut output, &mut history, &contract);
+ assert!(tinyagents_harness::config::output_satisfies_contract(
+ &output, &contract
+ ));
+ assert_eq!(
+ history.len(),
+ 2,
+ "the final row is replaced, not duplicated"
+ );
+
+ let once = output.clone();
+ append_synthesized_block(&mut output, &mut history, &contract);
+ assert_eq!(output, once, "a satisfied output is left alone");
+ }