fix(agent): collect after large tool results (#70684) - #80974
fix(agent): collect after large tool results (#70684)#80974Christopher-Schulze wants to merge 3 commits into
Conversation
|
This was generated by AI during triage. Summary: Problems:
Solution: Checked against |
aefa3f5 to
cec454a
Compare
|
Maintenance update: corrected the title to reflect the final scope: collection after large tool results only. Removed the duplicate explicit collection from ContextCompressor.compress(). Compression now relies on the existing platform-aware trim_memory post-compression path exactly once; the large-tool-result threshold collector is unchanged. Rebased onto current main; canonical checker and 5/5 regression tests pass on head cec454a. |
What does this PR do?
Fixes the Hermes CLI process memory leak described in #70684 by triggering a targeted
gc.collect()after large tool results are appended. The tool-result collector is gated on a 1 MB UTF-8 JSON threshold so small results do not pay for a full collection every turn. Context compression already runs the platform-awaretrim_memory(reason="post-compression")hook; this update keeps that existing single collection path intact instead of adding a duplicate.Related Issue
Fixes #70684
Type of Change
Changes Made
agent/tool_executor.py:gcand add_GC_COLLECT_THRESHOLD_BYTES = 1_000_000._maybe_collect_gc_after_tool_result(content, threshold_bytes=...)which serializescontentto UTF-8 JSON and callsgc.collect()when the size is >= the threshold.messages.append(...)that adds a tool result inexecute_tool_calls_concurrent,execute_tool_calls_sequential, and the shared_append_cancelled_tool_resultshelper (which also covers the invalid-argument and cancelled/skip paths).tests/agent/test_compression_gc_collect.py: regression test that compression uses the existing post-compression trim path exactly once, so Linux/glibc does not double-collect.tests/run_agent/test_tool_executor_gc_collect.py: regression tests thatgc.collect()fires for a >1 MB tool result and not for a small one.How to Test
main(427584b76) and confirm the large-tool-result test fails:scripts/run_tests.sh tests/agent/test_compression_gc_collect.py tests/run_agent/test_tool_executor_gc_collect.pyscripts/check.sh --project hermes-agent --worktree worktrees/hermes-agent/70684.Checklist
Code
Documentation & Housekeeping
gconly)