Skip to content

Commit def7c9a

Browse files
committed
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.
1 parent e4edd82 commit def7c9a

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

agents/s06_context_compact.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
|
1313
v
1414
[Layer 1: micro_compact] (silent, every turn)
15-
Replace tool_result content older than last 3
15+
Replace non-read_file tool_result content older than last 3
1616
with "[Previous: used {tool_name}]"
1717
|
1818
v
@@ -57,6 +57,7 @@
5757
THRESHOLD = 50000
5858
TRANSCRIPT_DIR = WORKDIR / ".transcripts"
5959
KEEP_RECENT = 3
60+
PRESERVE_RESULT_TOOLS = {"read_file"}
6061

6162

6263
def estimate_tokens(messages: list) -> int:
@@ -84,13 +85,17 @@ def micro_compact(messages: list) -> list:
8485
for block in content:
8586
if hasattr(block, "type") and block.type == "tool_use":
8687
tool_name_map[block.id] = block.name
87-
# Clear old results (keep last KEEP_RECENT)
88+
# Clear old results (keep last KEEP_RECENT). Preserve read_file outputs because
89+
# they are reference material; compacting them forces the agent to re-read files.
8890
to_clear = tool_results[:-KEEP_RECENT]
8991
for _, _, result in to_clear:
90-
if isinstance(result.get("content"), str) and len(result["content"]) > 100:
91-
tool_id = result.get("tool_use_id", "")
92-
tool_name = tool_name_map.get(tool_id, "unknown")
93-
result["content"] = f"[Previous: used {tool_name}]"
92+
if not isinstance(result.get("content"), str) or len(result["content"]) <= 100:
93+
continue
94+
tool_id = result.get("tool_use_id", "")
95+
tool_name = tool_name_map.get(tool_id, "unknown")
96+
if tool_name in PRESERVE_RESULT_TOOLS:
97+
continue
98+
result["content"] = f"[Previous: used {tool_name}]"
9499
return messages
95100

96101

0 commit comments

Comments
 (0)