Skip to content

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

fix(agent): handle missing turn checkpoint in finalize path

Steven Enamakel90eb589a688e
diff --git a/crates/openhuman-core/src/agent/session_host/turn_checkpoint.rs b/crates/openhuman-core/src/agent/session_host/turn_checkpoint.rs
index 38c981d783..8eb5f1d473 100644
--- a/crates/openhuman-core/src/agent/session_host/turn_checkpoint.rs
+++ b/crates/openhuman-core/src/agent/session_host/turn_checkpoint.rs
@@ -284,8 +284,29 @@ pub(crate) const GROUNDING_TOTAL_CHARS: usize = 16_000;
/// directives forbid the three shapes that reached a user's screen; the frame
/// [`wrap_harness_instruction`] adds is what makes it structurally distinct
/// from the conversation in the first place.
-pub(crate) const FINAL_ANSWER_INSTRUCTION: &str = "\
-You have finished using tools for this turn but have not yet written a reply to the user. \
+pub(crate) const FINAL_ANSWER_INSTRUCTION: &str = concat!(
+ "You have finished using tools for this turn but have not yet written a reply to the user. ",
+ final_answer_body!()
+);
+
+/// [`FINAL_ANSWER_INSTRUCTION`] for a turn whose last reply ran out of output
+/// tokens while the model was still reasoning, with no tool call (#6951). The
+/// generic lead ("you have finished using tools") was false there and drew
+/// replies that presented unfinished work as done. This lead names the real
+/// cause and asks the model to say what is left.
+pub(crate) const TRUNCATED_ANSWER_INSTRUCTION: &str = concat!(
+ "Your last reply ran out of output tokens while you were still reasoning, so this turn ended \
+ before you made your next tool call or wrote a reply to the user. The work is unfinished: say so \
+ plainly and name what is still left to do. ",
+ final_answer_body!()
+);
+
+/// The body shared by [`FINAL_ANSWER_INSTRUCTION`] and
+/// [`TRUNCATED_ANSWER_INSTRUCTION`]; a macro so both stay `&'static str`
+/// constants that the quotation guard can split.
+macro_rules! final_answer_body {
+ () => {
+ "\
Tools are no longer available and nothing more will run this turn, so do not call any tools and do not \
describe steps you are about to take. Write a self-contained final message that reports what actually happened: \
what you found, changed or established, grounded in the tool results above and the tool records below. \
@@ -296,7 +317,10 @@ If nothing conclusive resulted, say so plainly.\n\
These directions are addressed to you and are not part of the conversation. Do not quote or restate them, \
in whole or in part. Do not list or describe the tools available to you. Do not narrate your deliberation: \
no thinking aloud, no correcting yourself mid-reply, no weighing what to do. Write only the message the user \
-will read.";
+will read."
+ };
+}
+use final_answer_body;
/// The lead-in that hands the breaker's stop note to the closing call.
///
@@ -337,6 +361,7 @@ const MIN_QUOTED_WORDS: usize = 10;
fn harness_instruction_needles(stop_reason: Option<&str>) -> Vec<String> {
let mut sources = vec![
FINAL_ANSWER_INSTRUCTION,
+ TRUNCATED_ANSWER_INSTRUCTION,
MAX_ITER_CHECKPOINT_INSTRUCTION,
FINAL_WRITE_INSTRUCTION,
STOP_NOTE_PREAMBLE,
@@ -412,7 +437,8 @@ pub(crate) fn close_repair_instruction(instruction: &str, violation: CloseViolat
format!("{named} Write the message again.\n\n{instruction}")
}
-/// The full closing-message instruction: [`FINAL_ANSWER_INSTRUCTION`], the
+/// The full closing-message instruction: [`FINAL_ANSWER_INSTRUCTION`] (or
+/// [`TRUNCATED_ANSWER_INSTRUCTION`] when `truncated` and not halted), the
/// breaker's stop note when the run was halted (issue #6279), and this turn's
/// rendered tool records.
///
@@ -421,7 +447,7 @@ pub(crate) fn close_repair_instruction(instruction: &str, violation: CloseViolat
/// sub-agent's parent and wrong on a user's screen.
pub(crate) fn final_answer_instruction(
stop_reason: Option<&str>,
- _truncated: bool,
+ truncated: bool,
records: &str,
) -> String {
let mut directive = String::new();
@@ -431,7 +457,13 @@ pub(crate) fn final_answer_instruction(
directive.push_str(reason.trim());
directive.push_str("\n</stop_note>\n\n");
}
- directive.push_str(FINAL_ANSWER_INSTRUCTION);
+ // A breaker halt already explains the stop; truncation only replaces the
+ // "you have finished" lead when nothing more specific is known.
+ directive.push_str(if truncated && stop_reason.is_none() {
+ TRUNCATED_ANSWER_INSTRUCTION
+ } else {
+ FINAL_ANSWER_INSTRUCTION
+ });
let mut out = wrap_harness_instruction(&directive);
out.push_str("\n\n<tool_records>\n");
out.push_str(if records.trim().is_empty() {
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs b/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs
index 2d5db57247..8a015ca2ca 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_outcome.rs
@@ -84,9 +84,13 @@ pub(crate) struct TinyagentsTurnOutcome {
/// before producing anything: `finish_reason = length`, no visible text and no
/// tool call. See [`TinyagentsTurnOutcome::truncated`].
pub(crate) fn ended_out_of_output_budget(
- _final_response: Option<&tinyinference_llm::model::ModelResponse>,
+ final_response: Option<&tinyinference_llm::model::ModelResponse>,
) -> bool {
- false
+ final_response.is_some_and(|response| {
+ response.finish_reason.as_deref() == Some("length")
+ && response.message.tool_calls.is_empty()
+ && response.text().trim().is_empty()
+ })
}
/// One tool call's execution outcome, captured at the tool boundary before the
diff --git a/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs b/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs
index 5243d7458c..a5d585e6ff 100644
--- a/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/turn_run_finalize.rs
@@ -232,6 +232,18 @@ pub(super) async fn finalize_turn_outcome(
text = summary.clone();
}
+ // #6951: the run finished on a reply that spent the whole output budget
+ // reasoning. The closing call names that cause instead of "finished".
+ let truncated = super::turn_outcome::ended_out_of_output_budget(run.final_response.as_ref());
+ if truncated {
+ tracing::warn!(
+ model,
+ model_calls = run.model_calls,
+ tool_calls = run.tool_calls,
+ "[tinyagents] turn ended on a reply that ran out of output tokens while reasoning"
+ );
+ }
+
let tool_outcomes = tool_outcome_sink
.lock()
.map(|guard| guard.clone())
@@ -286,7 +298,7 @@ pub(super) async fn finalize_turn_outcome(
hit_cap,
wrap_up_injected,
breaker_halt,
- truncated: false,
+ truncated,
tool_outcomes,
}
}