Skip to content

The agent that grows with you

fix(agent): retry a dropped stream's tool call instead of running its repaired prefix

AdamPlatin12302f98f21f814
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.py
index 1db9226036a..02b4c254f86 100644
--- a/agent/chat_completion_helpers.py
+++ b/agent/chat_completion_helpers.py
@@ -3301,12 +3301,17 @@ class _StreamingCall(StreamingWaitMonitor):
try:
json.loads(arguments)
except json.JSONDecodeError:
- # Repair before flagging (GLM via Ollama); "{}" = unrepairable.
- repaired = _repair_tool_call_arguments(arguments, tc["function"]["name"] or "?")
- if repaired != "{}":
- arguments = repaired
- else:
+ # A dropped stream is never repaired: closing its prefix yields valid
+ # JSON that silently lacks every key and digit not yet streamed.
+ if finish_reason is None:
has_truncated_tool_args = True
+ else:
+ # Repair before flagging (GLM via Ollama); "{}" = unrepairable.
+ repaired = _repair_tool_call_arguments(arguments, tc["function"]["name"] or "?")
+ if repaired != "{}":
+ 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.
diff --git a/tests/agent/test_run_agent.py b/tests/agent/test_run_agent.py
index 31757e744ac..489ffe4f01f 100644
--- a/tests/agent/test_run_agent.py
+++ b/tests/agent/test_run_agent.py
@@ -6295,6 +6295,28 @@ class TestStreamingApiCall:
assert tc[0].function.arguments == '{"path":"x.txt","content":"hel'
assert resp.choices[0].finish_reason == "length"
+ @pytest.mark.parametrize("finish_reason", [None, "tool_calls"])
+ def test_cut_tool_args_are_repaired_only_once_the_provider_finished(self, agent, finish_reason):
+ # Cut after the first digit of "timeout": 600. Every string is closed, so the
+ # prefix repairs to valid JSON that carries timeout=6. Without a finish_reason
+ # nothing says the model was done: retry, never run what happened to arrive.
+ from hermes_constants import PARTIAL_STREAM_STUB_ID
+ raw = '{"command": "make deploy", "timeout": 6'
+ chunks = [_make_chunk(tool_calls=[_make_tc_delta(0, "call_1", "terminal", raw)])]
+ if finish_reason:
+ chunks.append(_make_chunk(finish_reason=finish_reason))
+ agent.client.chat.completions.create.return_value = iter(chunks)
+
+ resp = agent._interruptible_streaming_api_call({"messages": []})
+
+ if finish_reason is None:
+ assert resp.id == PARTIAL_STREAM_STUB_ID
+ assert resp.choices[0].message.tool_calls is None
+ assert resp._dropped_tool_names == ["terminal"]
+ else:
+ args = resp.choices[0].message.tool_calls[0].function.arguments
+ assert json.loads(args) == {"command": "make deploy", "timeout": 6}
+
def test_ollama_reused_index_separate_tool_calls(self, agent):
"""Ollama sends every tool call at index 0 with different ids.