fix(doom_loop): normalize tool-call args before hashing#119
Open
voidborne-d wants to merge 1 commit intohuggingface:mainfrom
Open
fix(doom_loop): normalize tool-call args before hashing#119voidborne-d wants to merge 1 commit intohuggingface:mainfrom
voidborne-d wants to merge 1 commit intohuggingface:mainfrom
Conversation
The doom-loop detector hashed raw `function.arguments` strings, so
semantically-identical tool calls hashed differently when the LLM emitted
them with different key orderings (`{"a":1,"b":2}` vs `{"b":2,"a":1}`)
or whitespace (`{"a":1}` vs `{"a": 1}`). This silently broke
`detect_identical_consecutive` and `detect_repeating_sequence`: the
agent could be calling the same tool with the same args repeatedly and
the detector would see three distinct signatures and stay quiet.
Issue huggingface#61 P1 explicitly calls this out:
> Add semantic-similarity or normalized-task matching for `research`.
Fix: parse-and-redump JSON via `json.dumps(..., sort_keys=True,
separators=(",", ":"))` before hashing. Falls back to the raw string
when the input isn't valid JSON so non-JSON `arguments` strings (rare
edge for some providers) keep the legacy behaviour and never raise.
Tests: 23 new cases in `tests/unit/test_doom_loop.py` covering
`_normalize_args`, `_hash_args`, `extract_recent_tool_signatures`,
`detect_identical_consecutive`, `detect_repeating_sequence`, and the
`check_for_doom_loop` entry point. Includes the headline regression —
three reordered-key calls collapsing to one signature — plus negative
cases (different values, different array orderings, sub-threshold
counts, broken pattern).
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.
Problem
agent/core/doom_loop.py:_hash_argshashes the rawfunction.argumentsJSON string. LLMs can emit semantically-identical tool calls with different key orderings ({"a":1,"b":2}vs{"b":2,"a":1}) or whitespace ({"a":1}vs{"a": 1}), and these hash to different values. That silently breaksdetect_identical_consecutiveanddetect_repeating_sequence: the agent could be calling the same tool with the same args repeatedly and the detector sees three distinct signatures and stays quiet.Issue #61 P1 explicitly calls this out:
This is the smallest version of that — pure key/whitespace normalisation, no semantic similarity yet.
Reproduction
A research subagent that hits a 529 retry can re-emit the same call with shuffled keys; pre-fix that gets logged as three separate signatures.
Fix
Parse-and-redump the args string with
json.dumps(..., sort_keys=True, separators=(",", ":"))before hashing. Falls back to the raw string when input isn't valid JSON so non-JSONargumentsstrings (rare edge for some providers / partial streaming residue) keep the legacy behaviour and never raise.agent/core/doom_loop.py: 31 inserts / 2 deletes — adds_normalize_argshelper, threads it through_hash_args. No call-site changes.Tests
23 new cases in
tests/unit/test_doom_loop.pycovering:_normalize_args— key reordering, whitespace, nested structure, array-order is significant, invalid-JSON fallback, empty string_hash_args— semantic-equality regression + value-difference negativeextract_recent_tool_signatures— three reordered-key calls collapse to one signature, skips non-assistant / no-tool-calls messagesdetect_identical_consecutive— fires at threshold, silent below, resets on break, catches reordered-args run (the headline regression)detect_repeating_sequence— alternating pair, broken pattern silent, normalises args inside cyclecheck_for_doom_loop— full entry point: silent below 3 signatures, returns corrective prompt for identical run AND for cycle, quiet when args meaningfully differLocal gates
The 9 pre-existing
test_user_quotas.pyfailures are missing-pytest-asyncioissues onmainand are unrelated to this PR.Scope discipline
Deliberately narrow: only key-order + whitespace normalisation. Did not touch:
detect_repeating_sequencecycle-detection algorithmlookback=30window (currently messages,shouldcould be tool-call count — separate issue)research-style calls (would need an embedding hop)Each of those is its own discrete PR; piling them in here would dilute the regression coverage.