refactor(tools): terminal, execute_code, MCP and the bounded collector truncate through one head/tail helper

Four copies of the 40/60 head/tail algorithm with a near-identical notice
(terminal_tool_result, mcp_tool_content, code_execution_tool,
environments/base_output) collapse into tools/tool_output_truncate.py, so the
ratio and the `... [<LABEL> TRUNCATED - N <unit> omitted out of T total] ...`
marker are defined once. execute_code keeps byte mode + spill path and only
shares the notice/split. Visible change: the terminal notice now uses
thousands separators like the other three (`9,000 chars` not `9000 chars`).

kanban_specify._truncate: comment claimed escape stripping the body never did;
comment now says what the plain clamp is for.
This commit is contained in:
teknium1
2026-09-12 19:42:52 -07:00
committed by Teknium
parent 3f59b5c594
commit 1512edfb85
5 changed files with 13 additions and 31 deletions
+4 -3
View File
@@ -31,6 +31,7 @@ from tools.registry import registry, tool_error
from hermes_time import get_timezone_name
from tools.code_execution_env import _resolve_child_cwd, _resolve_child_python
from tools.code_execution_rpc import _rpc_poll_loop
from tools.tool_output_truncate import head_tail_split, truncation_notice
logger = logging.getLogger(__name__)
@@ -62,10 +63,10 @@ def _truncate_stdout_text(stdout_text: str) -> Tuple[str, Dict[str, Any]]:
"stdout_bytes_total": total, "stdout_bytes_omitted": total - captured}
if total <= MAX_STDOUT_BYTES:
return stdout_bytes.decode("utf-8", errors="replace"), metadata
head_bytes = int(MAX_STDOUT_BYTES * 0.4)
head_bytes, tail_bytes = head_tail_split(MAX_STDOUT_BYTES)
text = (stdout_bytes[:head_bytes].decode("utf-8", errors="replace")
+ f"\n\n... [OUTPUT TRUNCATED - {total - captured:,} bytes omitted out of {total:,} total] ...\n\n"
+ stdout_bytes[head_bytes - MAX_STDOUT_BYTES:].decode("utf-8", errors="replace"))
+ truncation_notice(total - captured, total, unit="bytes")
+ stdout_bytes[-tail_bytes:].decode("utf-8", errors="replace"))
metadata["warning"] = ("execute_code stdout was truncated; the script did run, but only "
"the captured head/tail output is included. Re-run only with "
"narrower output if the omitted data is required.")