Skip to content

Commit 84f499c

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dnpub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta
authored
fix(buzz-agent): cap tool-result text at 50 KiB with middle elision (#952)
Signed-off-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1mprnacetjua2xx3p5eddmhxyk6wv929ymm5py8kd2xfxurxahspqqlgyta <d8473ee32b973aa31a21a65adddcc4b69cc2a8a4dee8121ecd51926e0cddbc02@sprout-oss.stage.blox.sqprod.co>
1 parent 2846a96 commit 84f499c

5 files changed

Lines changed: 192 additions & 78 deletions

File tree

‎crates/buzz-agent/README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ Everything is environment variables. No flags, no config files. (We are a subpro
153153
| `BUZZ_AGENT_MAX_SESSIONS` | unlimited | Max concurrent ACP sessions. Sessions are cheap; default has no cap. |
154154
| `BUZZ_AGENT_MAX_LINE_BYTES` | `4194304` | 4 MiB. Hard cap on inbound JSON-RPC frames. |
155155
| `BUZZ_AGENT_MAX_HISTORY_BYTES` | `1048576` | 1 MiB. Old turns are evicted past this. |
156+
| `BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES` | `51200` | 50 KiB. Per-result cap on tool-output text; oversize is middle-elided (head + tail kept) with an inline marker. Images are exempt. |
156157

157158

158159
## Providers
@@ -234,7 +235,8 @@ The trust boundary is **the operator who launched the agent**. The harness, MCP
234235
| History window | 1 MiB | `BUZZ_AGENT_MAX_HISTORY_BYTES` |
235236
| LLM response body | 16 MiB | `MAX_LLM_RESPONSE_BYTES` |
236237
| LLM error body | 4 KiB | `MAX_LLM_ERROR_BODY_BYTES` |
237-
| Tool result body | 256 KiB | `MAX_TOOL_RESULT_BYTES` |
238+
| Tool result body (total, incl. images) | 8 MiB | `MAX_TOOL_RESULT_BYTES` |
239+
| Tool result text | 50 KiB | `BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES` |
238240
| MCP servers / session | 16 | `MAX_MCP_SERVERS` |
239241
| Tools / session | 128 | `MAX_TOOLS_PER_SESSION` |
240242
| Tool description bytes | 1 KiB | `MAX_DESCRIPTION_BYTES` |

‎crates/buzz-agent/src/agent.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::config::{Config, MAX_PROMPT_BYTES, MAX_TOOL_CALLS_PER_TURN, MAX_TOOL_
88
use crate::handoff::HandoffOutcome;
99
use crate::llm::Llm;
1010
use crate::mcp::McpRegistry;
11+
use crate::mcp::ResultBudget;
1112

1213
use crate::types::{
1314
AgentError, ContentBlock, HistoryItem, ProviderStop, StopReason, ToolCall, ToolResult,
@@ -304,6 +305,10 @@ impl RunCtx<'_> {
304305
let wire = self.wire.clone();
305306
let session_id = self.session_id.to_owned();
306307
let timeout = self.cfg.tool_timeout;
308+
let budget = ResultBudget {
309+
total: MAX_TOOL_RESULT_BYTES,
310+
text: self.cfg.max_tool_result_text_bytes,
311+
};
307312
let cancel = self.cancel.clone();
308313
let sem = Arc::clone(&sem);
309314
set.spawn(async move {
@@ -317,7 +322,7 @@ impl RunCtx<'_> {
317322
}
318323
};
319324
emit_in_progress(&wire, &session_id, &call).await;
320-
let outcome = invoke_tool_inner(&mcp, &call, timeout, cancel).await;
325+
let outcome = invoke_tool_inner(&mcp, &call, timeout, budget, cancel).await;
321326
match &outcome {
322327
InvokeOutcome::Done(result) => {
323328
emit_completed(&wire, &session_id, &call, result).await;
@@ -419,6 +424,7 @@ async fn invoke_tool_inner(
419424
mcp: &Arc<McpRegistry>,
420425
call: &ToolCall,
421426
tool_timeout: std::time::Duration,
427+
budget: ResultBudget,
422428
mut cancel: watch::Receiver<bool>,
423429
) -> InvokeOutcome {
424430
if *cancel.borrow() {
@@ -430,7 +436,7 @@ async fn invoke_tool_inner(
430436
&call.name,
431437
&call.provider_id,
432438
&call.arguments,
433-
MAX_TOOL_RESULT_BYTES,
439+
budget,
434440
&mut cancel,
435441
),
436442
)

‎crates/buzz-agent/src/config.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ use std::time::Duration;
33
pub const PROTOCOL_VERSION: u32 = 1;
44

55
pub const MAX_PROMPT_BYTES: usize = 1024 * 1024;
6+
/// Total per-result byte ceiling (text + images). Sized for image-bearing
7+
/// results — view_image can legitimately return multi-MiB base64 payloads.
8+
/// Text is governed by the much smaller `BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES`.
69
pub const MAX_TOOL_RESULT_BYTES: usize = 8 * 1024 * 1024;
10+
/// Default cap on the *text* portion of a single tool result. Oversized text
11+
/// is middle-elided before it enters history; without this, one fat `cat`
12+
/// burns the context window and forces a lossy handoff. 50 KiB matches the
13+
/// shell-output caps in sprout-dev-mcp, goose, and pi; codex defaults to
14+
/// 10 KB. Tunable via `BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES`.
15+
pub const DEFAULT_TOOL_RESULT_TEXT_BYTES: usize = 50 * 1024;
716
pub const MAX_TOOL_CALLS_PER_TURN: usize = 64;
817

918
pub const HANDOFF_MAX_OUTPUT_TOKENS: u32 = 8192;
@@ -56,6 +65,11 @@ pub struct Config {
5665
pub max_sessions: usize,
5766
pub max_line_bytes: usize,
5867
pub max_history_bytes: usize,
68+
/// Per-tool-result cap on text content. Oversized text is middle-elided
69+
/// (head + tail kept) before entering history. Images are exempt — they
70+
/// are bounded by [`MAX_TOOL_RESULT_BYTES`] and accounted separately.
71+
/// Set via `BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES`.
72+
pub max_tool_result_text_bytes: usize,
5973
/// Provider context window in tokens used to gate handoff. The handoff
6074
/// fires when the previous request's (cache-summed) input tokens cross the
6175
/// handoff threshold for this budget, before the next request can exceed
@@ -163,6 +177,10 @@ impl Config {
163177
max_sessions: parse_env("BUZZ_AGENT_MAX_SESSIONS", usize::MAX)?,
164178
max_line_bytes: parse_env("BUZZ_AGENT_MAX_LINE_BYTES", 4 * 1024 * 1024)?,
165179
max_history_bytes: parse_env("BUZZ_AGENT_MAX_HISTORY_BYTES", 16 * 1024 * 1024)?,
180+
max_tool_result_text_bytes: parse_env(
181+
"BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES",
182+
DEFAULT_TOOL_RESULT_TEXT_BYTES,
183+
)?,
166184
max_context_tokens: parse_env("BUZZ_AGENT_MAX_CONTEXT_TOKENS", 200_000u64)?,
167185
max_handoffs: parse_env("BUZZ_AGENT_MAX_HANDOFFS", 10)?,
168186
max_parallel_tools: parse_env("BUZZ_AGENT_MAX_PARALLEL_TOOLS", 8usize)?,
@@ -178,6 +196,7 @@ impl Config {
178196
fn validate(&self) -> Result<(), String> {
179197
const MIN_HISTORY_BYTES: usize = 4096;
180198
const MIN_LINE_BYTES: usize = 1024;
199+
const MIN_TOOL_RESULT_TEXT_BYTES: usize = 1024;
181200
const MIN_TIMEOUT: Duration = Duration::from_secs(1);
182201

183202
if self.max_output_tokens < 1 {
@@ -205,6 +224,13 @@ impl Config {
205224
"config: BUZZ_AGENT_MAX_LINE_BYTES must be >= {MIN_LINE_BYTES}"
206225
));
207226
}
227+
if self.max_tool_result_text_bytes < MIN_TOOL_RESULT_TEXT_BYTES
228+
|| self.max_tool_result_text_bytes > MAX_TOOL_RESULT_BYTES
229+
{
230+
return Err(format!(
231+
"config: BUZZ_AGENT_MAX_TOOL_RESULT_TEXT_BYTES must be in {MIN_TOOL_RESULT_TEXT_BYTES}..={MAX_TOOL_RESULT_BYTES}"
232+
));
233+
}
208234
if self.llm_timeout < MIN_TIMEOUT {
209235
return Err("config: BUZZ_AGENT_LLM_TIMEOUT_SECS must be >= 1".into());
210236
}

‎crates/buzz-agent/src/llm.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,7 @@ mod tests {
924924
max_sessions: 1,
925925
max_line_bytes: 1024 * 1024,
926926
max_history_bytes: 16 * 1024 * 1024,
927+
max_tool_result_text_bytes: 50 * 1024,
927928
max_context_tokens: 200_000,
928929
max_handoffs: 1,
929930
max_parallel_tools: 1,

0 commit comments

Comments
 (0)