Skip to content

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

feat(tinyagents): add summary_focus_tools set and handle summary preparation failure

Steven Enamakelf2cf6f2fed6e
diff --git a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs
index b12d3489ae..ff3f2adbd1 100644
--- a/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/middleware/tool_output.rs
@@ -113,6 +113,10 @@ pub(crate) struct ToolOutputMiddleware {
/// `summary_focus` values taken out of calls in `before_tool`, keyed by
/// call id, for the summary of the same call's result.
pub(crate) focus_by_call: Mutex<HashMap<String, String>>,
+ /// Tools whose schema carries TinyJuice's `summary_focus` property. Only
+ /// their calls lose the argument; any other tool with a parameter of the
+ /// same name (an MCP server's, say) keeps it.
+ pub(crate) summary_focus_tools: HashSet<String>,
}
impl ToolOutputMiddleware {
@@ -127,12 +131,15 @@ impl ToolOutputMiddleware {
/// Register a summary call bound to this turn, when this agent has a
/// summary model and the result is at least TinyJuice's threshold.
+ ///
+ /// `None` means no summary is wanted; `Some(Err(()))` means one was and
+ /// the call could not be prepared, which the result must disclose.
fn summary_ticket(
&self,
ctx: &RunContext<crate::agent::tinyagents::host::OpenHumanRunContext>,
tool_name: &str,
bytes: usize,
- ) -> Option<GenerateTicket> {
+ ) -> Option<Result<GenerateTicket, ()>> {
let summarizer = self.payload_summarizer.as_ref()?;
let threshold_tokens = self
.runtime_config
@@ -143,14 +150,16 @@ impl ToolOutputMiddleware {
return None;
}
match summarizer.prepare(ctx) {
- Ok(prepared) => Some(crate::inference::tokenjuice::generate::register(prepared)),
+ Ok(prepared) => Some(Ok(crate::inference::tokenjuice::generate::register(
+ prepared,
+ ))),
Err(error) => {
tracing::warn!(
tool = tool_name,
error = %error,
"[tinyagents::mw] could not prepare a summary call; compacting without one"
);
- None
+ Some(Err(()))
}
}
}
@@ -169,10 +178,14 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext> for Too
call: &mut TaToolCall,
) -> TaResult<()> {
// Taken out before validation and before the tool runs: it is an
- // argument to the summary of the result, not to the tool.
- if let Some(focus) =
+ // argument to the summary of the result, not to the tool. Only from a
+ // tool that declared it; for any other tool it is the tool's own.
+ let focus = if self.summary_focus_tools.contains(&call.name) {
crate::inference::tokenjuice::focus::take_summary_focus(&mut call.arguments)
- {
+ } else {
+ None
+ };
+ if let Some(focus) = focus {
tracing::debug!(
tool = %call.name,
call_id = %call.id,
@@ -334,7 +347,19 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext> for Too
// Bind a summary call to this turn only when the result is big
// enough for TinyJuice to want one; building the child context for
// every small result would be waste.
- let ticket = self.summary_ticket(ctx, tool_name, content.len());
+ let (ticket, unprepared) = match self.summary_ticket(ctx, tool_name, content.len()) {
+ Some(Ok(ticket)) => (Some(ticket), false),
+ Some(Err(())) => (None, true),
+ None => (None, false),
+ };
+ // Summary reuse and the failure breaker are per scope. A turn
+ // with no thread still gets one for the life of this run, rather
+ // than a fresh one per call that never reuses or trips.
+ let scope = ctx
+ .data
+ .thread_id
+ .clone()
+ .unwrap_or_else(|| format!("run-{}", ctx.instance_id()));
let before_bytes = content.len();
let compacted = crate::inference::tokenjuice::compact_tool_output(
crate::inference::tokenjuice::ToolOutputCompaction {
@@ -346,7 +371,7 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext> for Too
arguments: None,
focus,
context_token: ticket.as_ref().map(|t| t.token().to_string()),
- scope: ctx.data.thread_id.clone(),
+ scope: Some(scope),
},
)
.await;
@@ -361,7 +386,10 @@ impl Middleware<(), crate::agent::tinyagents::host::OpenHumanRunContext> for Too
);
summarized_from_bytes = Some(bytes);
}
- if let Some(notice) = compacted.notice {
+ let notice = compacted.notice.or_else(|| {
+ unprepared.then(crate::inference::tokenjuice::summary_failed_notice)
+ });
+ if let Some(notice) = notice {
tracing::warn!(
tool = tool_name,
bytes = content.len(),