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.
This commit is contained in:
CrazyBoyM
2026-03-30 00:06:02 +08:00
parent e4edd82c8c
commit def7c9a03e

View File

@@ -12,7 +12,7 @@ Three-layer compression pipeline so the agent can work forever:
| |
v v
[Layer 1: micro_compact] (silent, every turn) [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}]" with "[Previous: used {tool_name}]"
| |
v v
@@ -57,6 +57,7 @@ SYSTEM = f"You are a coding agent at {WORKDIR}. Use tools to solve tasks."
THRESHOLD = 50000 THRESHOLD = 50000
TRANSCRIPT_DIR = WORKDIR / ".transcripts" TRANSCRIPT_DIR = WORKDIR / ".transcripts"
KEEP_RECENT = 3 KEEP_RECENT = 3
PRESERVE_RESULT_TOOLS = {"read_file"}
def estimate_tokens(messages: list) -> int: def estimate_tokens(messages: list) -> int:
@@ -84,13 +85,17 @@ def micro_compact(messages: list) -> list:
for block in content: for block in content:
if hasattr(block, "type") and block.type == "tool_use": if hasattr(block, "type") and block.type == "tool_use":
tool_name_map[block.id] = block.name 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] to_clear = tool_results[:-KEEP_RECENT]
for _, _, result in to_clear: for _, _, result in to_clear:
if isinstance(result.get("content"), str) and len(result["content"]) > 100: if not isinstance(result.get("content"), str) or len(result["content"]) <= 100:
tool_id = result.get("tool_use_id", "") continue
tool_name = tool_name_map.get(tool_id, "unknown") tool_id = result.get("tool_use_id", "")
result["content"] = f"[Previous: used {tool_name}]" 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 return messages