fix(agent): deduplicate tool_call_id across pre-API sanitizers (#58327) - #58350
Merged
kshitijk4poor merged 1 commit intoJul 4, 2026
Merged
Conversation
…ousResearch#58327) Strict providers (DeepSeek) reject a payload where the same tool_call_id appears more than once with HTTP 400 'Duplicate value for tool_call_id'. The issue was filed as an 'orphaned tool message' compression bug, but the pasted error is a DUPLICATE tool_call_id — orphans are already handled on main; duplicates were not. Reproduced live on main: both shapes leaked through repair_message_sequence and sanitize_api_messages. Two chokepoints, two shapes: - repair_message_sequence: consume the id from known_tool_ids on first match so a SECOND tool result reusing it falls into the drop branch (duplicate tool-result shape). This is @Robinlovelace's kernel from NousResearch#55436 (applied manually — that PR was ~800 commits stale and bundled an unrelated duplicate-DB-write change for NousResearch#860, which is dropped here). - sanitize_api_messages (final pre-API pass): add a dedup pass covering BOTH (a) duplicate tool_calls sharing an id WITHIN one assistant message (the message[6] shape) and (b) later tool result messages reusing an already-seen id. NousResearch#55436 covered neither of these at this chokepoint. Tests: duplicate-tool-result dedup at both functions, duplicate-assistant- tool_call-id collapse, and a negative control proving distinct ids are never dropped (no over-dedup). Credit: @Robinlovelace (NousResearch#55436) for the repair_message_sequence dedup kernel. Closes NousResearch#58327.
|
Can I be listed as coauthor please? Many thanks! |
14 tasks
habarmc1223-sudo
pushed a commit
to habarmc1223-sudo/hermes-agent-fluxmem
that referenced
this pull request
Jul 8, 2026
…p-tool-call-id fix(agent): deduplicate tool_call_id across pre-API sanitizers (NousResearch#58327)
santhreal
pushed a commit
to santhreal/hermes-agent
that referenced
this pull request
Jul 13, 2026
…p-tool-call-id fix(agent): deduplicate tool_call_id across pre-API sanitizers (NousResearch#58327)
This was referenced Jul 15, 2026
Gravezzz
pushed a commit
to Gravezzz/hermes-agent
that referenced
this pull request
Jul 21, 2026
…p-tool-call-id fix(agent): deduplicate tool_call_id across pre-API sanitizers (NousResearch#58327)
This was referenced Aug 3, 2026
leewenjie
pushed a commit
to leewenjie/hermes-agent
that referenced
this pull request
Aug 7, 2026
…p-tool-call-id fix(agent): deduplicate tool_call_id across pre-API sanitizers (NousResearch#58327)
teknium1
pushed a commit
that referenced
this pull request
Aug 28, 2026
…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 (#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 #58357
melon-xf
added a commit
to melon-xf/hermes-agent
that referenced
this pull request
Sep 3, 2026
…p-tool-call-id fix(agent): deduplicate tool_call_id across pre-API sanitizers (NousResearch#58327)
melon-xf
added a commit
to melon-xf/hermes-agent
that referenced
this pull request
Sep 3, 2026
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Strict providers (DeepSeek) reject a payload where the same
tool_call_idappears more than once withHTTP 400: Duplicate value for 'tool_call_id'. This deduplicates tool_call_ids at both pre-API sanitizer chokepoints so the duplicate never reaches the provider.Note on the issue framing: #58327 was filed as an orphaned tool message (tool without a preceding assistant
tool_calls). Orphans are already handled onmain(bothrepair_message_sequenceandsanitize_api_messagesdrop them). The reporter's actual pasted error is a duplicatetool_call_id— a different, unhandled gap. Reproduced live onmain: both duplicate shapes leaked through.Changes (
agent/agent_runtime_helpers.py)repair_message_sequence: consume the id fromknown_tool_idson first match, so a second tool result reusing the same id falls into the drop/repair branch instead of being replayed. (This is @Robinlovelace's kernel from fix(agent): deduplicate tool responses for same tool_call_id #55436 — applied manually; that PR was ~800 commits stale and bundled an unrelated duplicate-DB-write change for bug: SQLite session transcript accumulates duplicate messages (3-4x token inflation) #860, which is not included here.)sanitize_api_messages(final pre-API pass): add a dedup pass covering both shapes —tool_callssharing an id within one assistant message (themessage[6]shape from the report), andtool_call_id.fix(agent): deduplicate tool responses for same tool_call_id #55436 covered neither shape at this chokepoint.
Validation
tests/run_agent/test_message_sequence_repair.py— 33 passed (existing + 4 new: duplicate-tool-result dedup at both functions, duplicate-assistant-tool_call-id collapse, and a negative control proving distinct ids are never dropped).tests/run_agent/test_message_sequence_repair.py+ fulltests/agent/test_context_compressor.py— 178 passed.api_messagescopy (not the cached prompt prefix), so prompt caching is unaffected.ruffclean;tylint-diff 🆕 New issues: none.toolmsgs sharing onetool_call_idtool_callssharing an idFollow-up (not in this PR)
agent/context_compressor.py::_sanitize_tool_pairsis a parallel reimplementation of the orphan-repair logic that also lacks dedup (analogous to the whitespace divergence noted in #55845). It is not a live bypass — compressor output re-passes throughsanitize_api_messagesbefore any provider call, so duplicates it emits are still caught downstream. Left unpatched here; worth consolidating the two sanitizers for defense-in-depth/consistency in a separate change.Closes #58327. Credit: @Robinlovelace (#55436) for the
repair_message_sequencededup kernel.