|
12 | 12 | | |
13 | 13 | v |
14 | 14 | [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 |
16 | 16 | with "[Previous: used {tool_name}]" |
17 | 17 | | |
18 | 18 | v |
|
57 | 57 | THRESHOLD = 50000 |
58 | 58 | TRANSCRIPT_DIR = WORKDIR / ".transcripts" |
59 | 59 | KEEP_RECENT = 3 |
| 60 | +PRESERVE_RESULT_TOOLS = {"read_file"} |
60 | 61 |
|
61 | 62 |
|
62 | 63 | def estimate_tokens(messages: list) -> int: |
@@ -84,13 +85,17 @@ def micro_compact(messages: list) -> list: |
84 | 85 | for block in content: |
85 | 86 | if hasattr(block, "type") and block.type == "tool_use": |
86 | 87 | 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. |
88 | 90 | to_clear = tool_results[:-KEEP_RECENT] |
89 | 91 | 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}]" |
94 | 99 | return messages |
95 | 100 |
|
96 | 101 |
|
|
0 commit comments