pub fn truncate_output(
result: &mut ToolResult,
tool_name: &str,
context_window: usize,
)Expand description
Dispatch to per-tool truncation based on tool name, then enforce universal upper bounds.
Per-tool truncation is the first line of defense (bash strips build noise, read_file
extracts outlines, etc.). The universal caps below are the LAST line of defense —
they cap result.output regardless of which tool produced it, so a single oversized
ToolResult can never dominate the ctx budget:
UNIVERSAL_MAX_LINES: line-count ceiling (head 50 + tail 50 + “[N lines omitted]”)hard_char_limit: char ceiling scaled to ~8K tokens, never more than 1/8 of window
2026-04-13 context: a 14072-line find output contributed to a sent=0 cascade.
Per-tool truncate handled that case (head 10 + tail 20), but other pathological
outputs (unknown tools, huge grep, edit results with diffs) could still slip through
the old char_limit = max(16000, context_window) formula which scaled UP with ctx
window and let a single message consume 25% of a 64K budget.