refactor(sanitizer): extract shared _classify_tool_call_orphans to eliminate drift - #59434
isheng-eqi wants to merge 3 commits into
Conversation
…iminate drift sanitize_api_messages (agent_runtime_helpers) and _sanitize_tool_pairs (context_compressor) both collected tool-call IDs and classified orphans with near-identical logic that had already drifted: the canonical sanitizer added dedup (NousResearch#58350), but the compressor's copy did not. Extract the shared orphan-detection logic into _classify_tool_call_orphans(messages) in agent_runtime_helpers. Both call sites now delegate to it, preserving their divergent remediation strategies (insert-stubs vs strip-orphans) while ensuring id-resolution rules and dedup stay in sync. Closes NousResearch#58357
|
@kshitijk4poor @alt-glitch This addresses #58357 — preventing the same drift that caused #55845 and #58327. Already has 5 unit tests, all compression/dedup suites pass. Ready for review when you have time. |
|
Thanks for consolidating the duplicated orphan-classification path; current main still has the two implementations ( Problems
Suggested changes
Automated hermes-sweeper review. |
|
Merged via PR #97167 (#97167) — your commits cherry-picked onto current main with authorship preserved in git log. The shared _classify_tool_call_orphans was reconciled to main's newer alias-aware variant-set semantics so both call sites now delegate to one classifier with byte-identical behavior — exactly the drift elimination you were after. (The release.py hunk was dropped; contributor mapping moved to per-email files.) Thanks! |
What
sanitize_api_messagesinagent_runtime_helpers.pyand_sanitize_tool_pairsincontext_compressor.pyhad near-identical orphan-detection logic (collect tool-call IDs from assistanttool_callsandtoolmessages, then classify orphans). The two copies had already drifted: the canonical sanitizer gained dedup in #58350, but the compressor copy did not.Fix
Extract the shared orphan-classification logic into
_classify_tool_call_orphans(messages)inagent_runtime_helpers.py. Both call sites now delegate to it for ID collection and orphan detection, preserving their divergent remediation strategies (insert-stubs vs strip-orphans) while ensuring id-resolution rules (call_id || id) and future dedup additions never drift again.Changes
agent/agent_runtime_helpers.py: +23 −13 — new_classify_tool_call_orphans()function;sanitize_api_messagesdelegates orphan detection to itagent/context_compressor.py: +21 −21 —_sanitize_tool_pairsdelegates to the shared function instead of reimplementing ID collectionTest plan
All 58 tests in
tests/run_agent/test_413_compression.py,test_860_dedup.py, andtest_anthropic_prompt_cache_policy.pypass — the compression, dedup, and prompt-caching behaviours are unchanged.Related
Closes #58357