fix(runtime): compact historical write_file tool-call args#3137
fix(runtime): compact historical write_file tool-call args#3137kibabsquirrel wants to merge 4 commits into
Conversation
|
Minimal case:
A few design choices behind this patch:
|
There was a problem hiding this comment.
Pull request overview
This PR mitigates model-context inflation caused by large write_file tool-call arguments lingering in historical AIMessage.tool_calls.args and being re-serialized in subsequent model requests. It introduces a request-scoped middleware that compacts only completed write_file tool-call/result pairs in the model-bound request view (without mutating persisted history), while keeping raw provider tool-call metadata consistent with the rewritten args.
Changes:
- Add
ToolArgsCompactionMiddlewareto replace oversized completedwrite_file.args.contentwith a marker string in outgoing model requests. - Extend tool-call metadata cloning utilities to optionally sync/replace raw provider serialized args when tool_calls are rewritten.
- Add targeted unit/regression tests across middleware behavior and provider serialization paths (Codex, MindIE), and assert middleware ordering in the lead agent.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| backend/packages/harness/deerflow/agents/middlewares/tool_args_compaction_middleware.py | New middleware that compacts completed large write_file args in the request-visible message list. |
| backend/packages/harness/deerflow/agents/middlewares/tool_call_metadata.py | Adds raw tool-call cloning with updated args + new API to keep provider metadata in sync when tool_calls are rewritten. |
| backend/packages/harness/deerflow/agents/middlewares/dangling_tool_call_middleware.py | Marks synthetic placeholder ToolMessages via additional_kwargs so compaction doesn’t treat them as completed tool results. |
| backend/packages/harness/deerflow/agents/lead_agent/agent.py | Inserts compaction middleware into the lead-agent middleware chain (after summarization). |
| backend/tests/test_tool_args_compaction_middleware.py | New unit tests covering compaction conditions, non-mutation, dangling-tool-result handling, and raw metadata syncing. |
| backend/tests/test_mindie_provider.py | Regression test ensuring compacted write_file args don’t bloat MindIE XML serialization. |
| backend/tests/test_codex_provider.py | Regression test ensuring compacted write_file args don’t bloat Codex function-call serialization. |
| backend/tests/test_lead_agent_model_resolution.py | Verifies middleware ordering places compaction after summarization and before title/memory. |
|
For broader direction, I’d like to check whether this matches how maintainers want to evolve this area. I see the original
The trade-off in this PR is intentionally conservative: reduce provider-bound payloads without mutating persisted state, at the cost of recomputing compaction per request and hiding historical I’m not trying to bundle all remaining surfaces into this PR, but I’d appreciate guidance on whether follow-ups should stay narrowly focused on |
e371326 to
5a30b5e
Compare
|
Updated the PR with occurrence-aware completed-pair detection for repeated |
|
@kibabsquirrel Thanks for working on this. I took another look and I think the request-local compaction approach makes sense, but two model-bound paths may still be uncovered:
Would you mind checking these paths and adding focused regression tests for summary-model input and subagent request serialization if you agree? |
|
@ShenAC-SAC Thanks, agreed these are additional model-bound paths worth covering. I updated the patch to reuse the same request-local compaction for the summary-bound message view before summary formatting, and wired |
Related to #3114
Summary
This PR fixes the model-bound history inflation surface of
write_file.After a large
write_filecall completes, its originalcontentcan remain in historicalAIMessage.tool_calls.argsand be serialized again in later model requests. This patch adds request-local compaction for model-bound message views, so completedwrite_filetool-call/result pairs no longer re-enter later model context verbatim.Key Decisions
write_filetool-call/result pairsargs.contentTests
Validated:
write_filecalls are compactedtool_call_idhistories compact only the occurrence paired with a real tool resultwrite_fileentriesargs,function.arguments, andfunction.argswrite_file.content