From def7c9a03e15e4e7fef8c1c7043c0932716b5bc1 Mon Sep 17 00:00:00 2001 From: CrazyBoyM Date: Mon, 30 Mar 2026 00:06:02 +0800 Subject: [PATCH] Preserve read_file results during micro_compact (PR #72) Skip compacting read_file tool outputs since they are reference material. Compacting them into placeholders forces the agent to re-read the same files, creating loops. Other tool results are still compacted normally. --- agents/s06_context_compact.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/agents/s06_context_compact.py b/agents/s06_context_compact.py index d544fde..622d643 100644 --- a/agents/s06_context_compact.py +++ b/agents/s06_context_compact.py @@ -12,7 +12,7 @@ Three-layer compression pipeline so the agent can work forever: | v [Layer 1: micro_compact] (silent, every turn) - Replace tool_result content older than last 3 + Replace non-read_file tool_result content older than last 3 with "[Previous: used {tool_name}]" | v @@ -57,6 +57,7 @@ SYSTEM = f"You are a coding agent at {WORKDIR}. Use tools to solve tasks." THRESHOLD = 50000 TRANSCRIPT_DIR = WORKDIR / ".transcripts" KEEP_RECENT = 3 +PRESERVE_RESULT_TOOLS = {"read_file"} def estimate_tokens(messages: list) -> int: @@ -84,13 +85,17 @@ def micro_compact(messages: list) -> list: for block in content: if hasattr(block, "type") and block.type == "tool_use": tool_name_map[block.id] = block.name - # Clear old results (keep last KEEP_RECENT) + # Clear old results (keep last KEEP_RECENT). Preserve read_file outputs because + # they are reference material; compacting them forces the agent to re-read files. to_clear = tool_results[:-KEEP_RECENT] for _, _, result in to_clear: - if isinstance(result.get("content"), str) and len(result["content"]) > 100: - tool_id = result.get("tool_use_id", "") - tool_name = tool_name_map.get(tool_id, "unknown") - result["content"] = f"[Previous: used {tool_name}]" + if not isinstance(result.get("content"), str) or len(result["content"]) <= 100: + continue + tool_id = result.get("tool_use_id", "") + tool_name = tool_name_map.get(tool_id, "unknown") + if tool_name in PRESERVE_RESULT_TOOLS: + continue + result["content"] = f"[Previous: used {tool_name}]" return messages