Skip to content

The agent that grows with you

fix(agent): reject repetitive arguments from dropped tool streams

itsflownium34624eea44a5
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 5b6307042fa..2fbe12be028 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -47,6 +47,7 @@ from agent.message_sanitization import (
sanitize_outbound_kwargs, strip_images_for_rejecting_model,
)
from agent.reasoning_summaries import append_streamed_reasoning_detail, separate_glued_reasoning_blocks
+from agent.repetition_guard import is_repetition_dominated
from agent.stream_single_writer import claim_stream_writer, stream_writer_is_current
from tools.terminal_tool_lifecycle import is_persistent_env
from utils import base_url_host_matches, base_url_hostname, env_float, env_int
@@ -3242,6 +3243,14 @@ class _StreamingCall(StreamingWaitMonitor):
arguments = repaired
else:
has_truncated_tool_args = True
+ # Parseable JSON does not prove that a dropped stream completed its
+ # action. Treat degenerate argument loops as partial calls too.
+ # A provider-confirmed call may legitimately write repetitive data.
+ if finish_reason is None and is_repetition_dominated(arguments):
+ logger.warning(
+ "Tool call '%s' has repetition-dominated arguments without a "
+ "finish_reason; treating as a dropped tool call.", tc["function"]["name"] or "?")
+ has_truncated_tool_args = True
elif finish_reason is None:
# Name arrived, zero arg bytes, no finish_reason: unflagged this
# becomes a "stop" turn executing "{}" with no retry.
diff --git a/tests/agent/test_repeated_dropped_tool_args.py b/tests/agent/test_repeated_dropped_tool_args.py
new file mode 100644
index 00000000000..3c2be293854
--- /dev/null
+++ b/tests/agent/test_repeated_dropped_tool_args.py
@@ -0,0 +1,70 @@
+"""Dropped streams must not dispatch repetition-dominated tool arguments."""
+
+import json
+from types import SimpleNamespace
+from unittest.mock import MagicMock
+
+import pytest
+
+from hermes_constants import PARTIAL_STREAM_STUB_ID
+from run_agent import AIAgent
+
+
+REPEATED_COMMAND = "cat >> /tmp/example.py <<'PYEOF'\n" + (
+ "# The model repeats this same rambling fragment instead of writing the script.\n" * 150
+) + "PYEOF"
+
+
+def _stream_response(monkeypatch, arguments, finish_reason=None):
+ agent = AIAgent(
+ api_key="test-key", base_url="https://example.com/v1", model="test/model",
+ quiet_mode=True, skip_context_files=True, skip_memory=True,
+ )
+ agent.api_mode = "chat_completions"
+ agent._interrupt_requested = False
+ monkeypatch.setenv("HERMES_STREAM_RETRIES", "0")
+
+ def chunk(calls=None, reason=None):
+ delta = SimpleNamespace(content=None, tool_calls=calls, reasoning_content=None, reasoning=None)
+ return SimpleNamespace(
+ choices=[SimpleNamespace(index=0, delta=delta, finish_reason=reason)],
+ model=None, usage=None,
+ )
+
+ def stream():
+ # A complete sibling must not run if the same action batch is discarded.
+ yield chunk([SimpleNamespace(index=0, id="sibling", function=SimpleNamespace(
+ name="read_file", arguments='{"path":"/tmp/example.py"}'))])
+ yield chunk([SimpleNamespace(index=1, id="write", function=SimpleNamespace(
+ name="terminal", arguments=arguments))])
+ if finish_reason:
+ yield chunk(reason=finish_reason)
+
+ client = MagicMock()
+ client.chat.completions.create.side_effect = lambda **kwargs: stream()
+ monkeypatch.setattr(agent, "_create_request_openai_client", lambda **kwargs: client)
+ monkeypatch.setattr(agent, "_close_request_openai_client", lambda *args, **kwargs: None)
+ return agent._interruptible_streaming_api_call({})
+
+
[email protected]("repair_needed", [False, True], ids=["valid-json", "repairable-json"])
+def test_dropped_repetitive_arguments_never_reach_dispatch(monkeypatch, repair_needed):
+ arguments = json.dumps({"command": REPEATED_COMMAND})
+ if repair_needed:
+ arguments = arguments[:-1] # Missing closing object, repairable by the existing helper.
+ response = _stream_response(monkeypatch, arguments)
+ assert response.id == PARTIAL_STREAM_STUB_ID
+ assert response.choices[0].message.tool_calls is None
+ assert set(response._dropped_tool_names) == {"read_file", "terminal"}
+
+
[email protected]("command,finish_reason", [
+ ("pwd", None),
+ ("\n".join(f"echo unique row {i}: value {i * 7}" for i in range(150)), None),
+ (REPEATED_COMMAND, "tool_calls"),
+])
+def test_healthy_or_confirmed_arguments_are_preserved(monkeypatch, command, finish_reason):
+ arguments = json.dumps({"command": command})
+ response = _stream_response(monkeypatch, arguments, finish_reason)
+ assert response.id != PARTIAL_STREAM_STUB_ID
+ assert response.choices[0].message.tool_calls[1].function.arguments == arguments