[Feature]: 工具结果返回驱逐后导致llm上下文超限
agent调用工具如果是返回一长串json时,驱逐大结果工具会往文件中一行写入,导致read_file读取文件时只读取到一行导致llm上下文超限,read_file工具是在白名单内,从而导致agent运行不了
我看了一下当前实现,问题应该在于分页维度不一致:ToolResultEvictionMiddleware 会把完整结果原样写入文件,而 read_file 的 offset / limit 只按行分页。对于单行超长 JSON,即使只读取一行,也会把整行重新放回模型上下文,因此驱逐并没有真正限制下一次读取量。
我不太建议只对 JSON 做 pretty-print:如果某个字段本身是超长字符串,或者结果不是 JSON,仍然会出现超长单行。更通用的方案是给 read_file 增加可选的字符分页参数,例如 char_offset 和 char_limit:
- 保留现有
offset/limit的行分页语义; - 先按行选择内容,再对返回文本应用字符窗口;
- 超出窗口时附加明确的 continuation 提示,例如下一次使用
char_offset=20000; - 不修改驱逐文件的原始内容,保证完整结果仍可分段读取;
- 保留现有 Java 方法作为兼容重载,避免已有直接调用受影响;
- 截断时避免拆开 UTF-16 surrogate pair。
计划补充的测试包括:单行超长 JSON 的首次读取和续读、多行文件现有分页行为、短文件不截断、中文/emoji 边界,以及驱逐文件内容保持不变。驱逐后的占位提示也可以明确建议带 char_limit 调用 read_file。
请问维护者是否接受按这个“字符分页 + 保留原文件”的方向处理?如果方向认可,我可以再基于最新 main 提交一个聚焦的修复和回归测试。
Issue Evaluation
Category: enhancement | Status: Evaluated
This is the same underlying defect as #2951, reported from the eviction angle: when a large tool result is evicted to a file it is written as a single long line, and read_file (which paginates by line only and is excluded from eviction via DEFAULT_EXCLUDED_TOOLS) returns the entire line, re-inflating the model context.
We have independently confirmed the root cause in #2951 against current main: LocalFilesystem.read() applies no character/byte cap, and read_file is in ToolResultEvictionConfig.DEFAULT_EXCLUDED_TOOLS, so an oversized single-line result bypasses eviction. The direction discussed above (char-level pagination, e.g. char_offset/char_limit, keeping line semantics and the eviction file intact) is consistent with the fix being tracked under #2951.
Recommendation: consolidate with #2951 — the fix belongs in the filesystem read path (char/byte budget + continuation metadata), which resolves both the direct-read case (#2951) and the evicted-artifact case described here. We recommend maintainers keep this issue linked to #2951 rather than treating it as a separate change.
Automated evaluation by github-manager-bot