Skip to content

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

fix(agent): bill a required-output repair cut off by the turn deadline

M3gA-Mindc0260ccf61b9
diff --git a/crates/openhuman-core/src/agent/session_host/driver.rs b/crates/openhuman-core/src/agent/session_host/driver.rs
index 4a5253326d..ecf49c29b4 100644
--- a/crates/openhuman-core/src/agent/session_host/driver.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver.rs
@@ -313,6 +313,7 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
.as_deref()
.is_some_and(|reason| reason.starts_with("Stopping after "));
let time_left = close_by.map(|at| at.saturating_duration_since(Instant::now()));
+ let mut repair_usage = grounded_close::RepairUsage::for_model(&self.model_name);
let required_repair = match required_output.as_ref() {
Some(contract) if classified_halt || time_left.is_some_and(|left| left.is_zero()) => {
append_synthesized_block(&mut output, &mut history, contract);
@@ -331,6 +332,7 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
close.is_none() && request.run_context.data.progress.is_some(),
request.run_context.data.progress.as_ref(),
outcome.model_calls.saturating_add(1) as u32,
+ &mut repair_usage,
);
match time_left {
Some(left) => {
@@ -350,8 +352,8 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
}
None => None,
};
- if let Some(repair) = &required_repair {
- output = repair.output.clone();
+ if let Some(repaired) = required_repair {
+ output = repaired;
if history
.last()
.is_some_and(|message| matches!(message, Message::Assistant(_)))
@@ -366,7 +368,9 @@ impl SessionDriver<OpenHumanRunContext> for OpenHumanSessionDriver {
// `after_commit`, so a failed/cancelled turn cannot publish its usage
// or tool outcomes as if they had become durable.
{
- let repair_usage = required_repair.as_ref().map(|repair| &repair.usage);
+ // Always billed: a repair that timed out after its call answered
+ // still recorded that call here.
+ let repair_usage = Some(&repair_usage);
let mut observed = sidecar
.lock()
.unwrap_or_else(|poisoned| poisoned.into_inner());
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
index 022f7d4e03..c14dc596e9 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close.rs
@@ -90,6 +90,8 @@ fn classified_halt_close(outcome: &TinyagentsTurnOutcome) -> Option<GroundedClos
/// Repair an otherwise valid terminal reply which omits the host's required
/// structured-output block. This mirrors the legacy session contract while
/// keeping the repair call and its usage inside the explicit driver sidecar.
+/// The call records into the caller-owned `usage` before streaming, so a
+/// repair cut off by the turn deadline is still billed.
#[allow(clippy::too_many_arguments)]
pub(super) async fn repair_required_output(
source: &TurnModelSource,
@@ -103,7 +105,8 @@ pub(super) async fn repair_required_output(
reply_already_streamed: bool,
progress: Option<&tokio::sync::mpsc::Sender<crate::agent::progress::AgentProgress>>,
iteration: u32,
-) -> Option<GroundedClose> {
+ usage: &mut RepairUsage,
+) -> Option<String> {
use tinyagents_harness::config as required;
if required::output_satisfies_contract(reply, contract) {
@@ -119,7 +122,6 @@ pub(super) async fn repair_required_output(
)));
let (candidate, candidate_usage) =
completion(source, model, temperature, thread_id, prompt_history).await;
- let mut usage = RepairUsage::for_model(model);
usage.record(candidate_usage, true);
let candidate = candidate.trim().to_owned();
let candidate_is_usable = !candidate.is_empty()
@@ -132,7 +134,7 @@ pub(super) async fn repair_required_output(
} else {
format!("{}\n\n{reply}", required::synthesize_block(contract))
};
- return Some(GroundedClose { output, usage });
+ return Some(output);
}
// The main loop may have already emitted the original reply. Never
@@ -157,10 +159,7 @@ pub(super) async fn repair_required_output(
tracing::debug!(%error, "[session-runtime] required-output repair progress receiver closed");
}
}
- Some(GroundedClose {
- output: format!("{reply}{continuation}"),
- usage,
- })
+ Some(format!("{reply}{continuation}"))
}
/// The tool-less closing instruction for `outcome`: the cap checkpoint when the
diff --git a/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs b/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs
index 9c5d38a3c7..9a54239ac2 100644
--- a/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs
+++ b/crates/openhuman-core/src/agent/session_host/driver/grounded_close_tests.rs
@@ -295,3 +295,49 @@ async fn timed_out_close_keeps_the_usage_of_finished_calls() {
assert_eq!(output, FALLBACK);
assert_eq!(usage.model_calls, 1, "the answered ask is recorded");
}
+
+/// The same for the required-output repair (#7721 review): its call answered,
+/// then streaming the correction outlived the turn budget. The caller-owned
+/// accumulator still holds the call.
+#[tokio::test(start_paused = true)]
+async fn timed_out_repair_keeps_the_usage_of_its_answered_call() {
+ use crate::agent::progress::AgentProgress;
+ let source = crate::agent::tinyagents::TurnModelSource::from_model(std::sync::Arc::new(
+ tinyagents_harness::testkit::ScriptedModel::replies(vec!["no block"]),
+ ));
+ let contract = tinyagents_harness::config::RequiredOutput {
+ block_key: "result".into(),
+ required_keys: vec!["status".into()],
+ };
+ // A full channel whose receiver is alive: the correction's send blocks.
+ let (progress, _receiver) = tokio::sync::mpsc::channel(1);
+ assert!(progress
+ .try_send(AgentProgress::TextDelta {
+ delta: "earlier".into(),
+ iteration: 1,
+ })
+ .is_ok());
+ let mut usage = RepairUsage::for_model("test-model");
+
+ let repaired = tokio::time::timeout(
+ std::time::Duration::from_secs(20),
+ repair_required_output(
+ &source,
+ "test-model",
+ 0.0,
+ None,
+ &tinytools_agent::dialect::NativeDialect,
+ &contract,
+ &[],
+ "Done.",
+ true,
+ Some(&progress),
+ 2,
+ &mut usage,
+ ),
+ )
+ .await;
+
+ assert!(repaired.is_err(), "the blocked send outlives the budget");
+ assert_eq!(usage.model_calls, 1, "the answered repair call is recorded");
+}