test(streaming): cover mixed tool-call and retry-exhaustion paths for #80498
Locks in two gaps left by 015a114a2 (#80623): a mixed response where one tool call completes validly while a sibling has zero argument bytes still gets discarded whole via the shared partial-stream-stub path, and the zero-byte trigger now has an end-to-end test through run_conversation's retry loop, not just at the chat_completion_helpers unit level.
This commit is contained in:
@@ -207,6 +207,68 @@ class TestCleanStreamEndBeforeAnyToolArgs:
|
||||
assert getattr(response, "_dropped_tool_names", None) == ["write_file"]
|
||||
|
||||
|
||||
# ── Mixed response: one complete call, one dropped (#80498) ─────────────────
|
||||
|
||||
class TestMixedToolCallsOneDroppedOneComplete:
|
||||
"""A single response can carry more than one tool call in parallel. If
|
||||
the stream dies before one call's arguments ever start while a sibling
|
||||
call in the SAME response already completed with valid, parseable
|
||||
arguments, the whole response is still discarded as one partial-stream
|
||||
stub — ``_build_partial_stream_stub`` unconditionally sets
|
||||
``tool_calls=None``, so the valid sibling is not selectively kept.
|
||||
|
||||
This all-or-nothing behavior predates #80498 (it already applied to the
|
||||
partial-but-nonempty-JSON case); this test just locks it in now that the
|
||||
zero-byte case routes through the same stub path, so a future change
|
||||
towards per-tool-call granularity doesn't silently regress either case.
|
||||
"""
|
||||
|
||||
@patch("run_agent.AIAgent._create_request_openai_client")
|
||||
@patch("run_agent.AIAgent._close_request_openai_client")
|
||||
def test_valid_sibling_tool_call_is_discarded_with_dropped_one(
|
||||
self, _mock_close, mock_create, monkeypatch,
|
||||
):
|
||||
def _clean_ending_stream():
|
||||
# Tool call 0: complete, valid JSON arguments.
|
||||
yield _make_stream_chunk(tool_calls=[
|
||||
_make_tool_call_delta(index=0, tc_id="call_ok", name="read_file"),
|
||||
])
|
||||
yield _make_stream_chunk(tool_calls=[
|
||||
_make_tool_call_delta(index=0, arguments='{"path": "a.txt"}'),
|
||||
])
|
||||
# Tool call 1: name arrives, then the stream dies before a
|
||||
# single argument byte — the #80498 zero-byte case.
|
||||
yield _make_stream_chunk(tool_calls=[
|
||||
_make_tool_call_delta(index=1, tc_id="call_x", name="write_file"),
|
||||
])
|
||||
# falls off the end — clean close, no terminator
|
||||
|
||||
mock_client = MagicMock()
|
||||
mock_client.chat.completions.create.side_effect = (
|
||||
lambda *a, **kw: _clean_ending_stream()
|
||||
)
|
||||
mock_create.return_value = mock_client
|
||||
|
||||
agent = _make_agent()
|
||||
agent._fire_stream_delta = lambda text: None
|
||||
|
||||
response = agent._interruptible_streaming_api_call({})
|
||||
|
||||
assert response.id == PARTIAL_STREAM_STUB_ID
|
||||
assert response.choices[0].finish_reason == FINISH_REASON_LENGTH
|
||||
assert response.choices[0].message.tool_calls is None, (
|
||||
"The valid 'read_file' call must not be selectively kept — the "
|
||||
"whole response is discarded as one stub so the retry "
|
||||
"regenerates every call in it together."
|
||||
)
|
||||
assert getattr(response, "_dropped_tool_names", None) == [
|
||||
"read_file", "write_file",
|
||||
], (
|
||||
"_dropped_tool_names lists every tool call in the response, "
|
||||
"not just the one that actually failed to complete."
|
||||
)
|
||||
|
||||
|
||||
# ── Length-continuation prompt branching ──────────────────────────────────
|
||||
|
||||
class TestLengthContinuationPromptBranching:
|
||||
|
||||
@@ -4030,6 +4030,47 @@ class TestRunConversation:
|
||||
mock_hfc.assert_called_once()
|
||||
assert result["final_response"] == "Done!"
|
||||
|
||||
def test_zero_byte_tool_args_stub_recovers_within_retries(self, agent):
|
||||
"""#80498: a stream that dies before a single argument byte arrives
|
||||
(name-only tool call) produces a stub with tool_calls=None and
|
||||
_dropped_tool_names set — the real shape _build_partial_stream_stub
|
||||
returns, distinct from the truncated-JSON stub above (which still
|
||||
carries a tool_calls list). Confirms the zero-byte trigger is wired
|
||||
end-to-end through the retry loop, not just detected at the
|
||||
chat_completion_helpers unit level."""
|
||||
from hermes_constants import PARTIAL_STREAM_STUB_ID
|
||||
|
||||
self._setup_agent(agent)
|
||||
agent.valid_tool_names.add("write_file")
|
||||
|
||||
stall = _mock_response(content="", finish_reason="length", tool_calls=None)
|
||||
stall.id = PARTIAL_STREAM_STUB_ID
|
||||
stall._dropped_tool_names = ["write_file"]
|
||||
|
||||
good_tc = _mock_tool_call(
|
||||
name="write_file",
|
||||
arguments='{"path":"report.md","content":"full content"}',
|
||||
call_id="c2",
|
||||
)
|
||||
good_resp = _mock_response(content="", finish_reason="stop", tool_calls=[good_tc])
|
||||
final_resp = _mock_response(content="Done!", finish_reason="stop")
|
||||
|
||||
with (
|
||||
patch("run_agent.handle_function_call", return_value='{"success":true}') as mock_hfc,
|
||||
patch.object(agent, "_persist_session"),
|
||||
patch.object(agent, "_save_trajectory"),
|
||||
patch.object(agent, "_cleanup_task_resources"),
|
||||
):
|
||||
agent.client.chat.completions.create.side_effect = [
|
||||
stall, good_resp, final_resp,
|
||||
]
|
||||
result = agent.run_conversation("write the report")
|
||||
|
||||
# The zero-byte stub must trigger a retry, not silently execute
|
||||
# write_file with coerced empty arguments (the #80498 regression).
|
||||
mock_hfc.assert_called_once()
|
||||
assert result["final_response"] == "Done!"
|
||||
|
||||
|
||||
def test_truncated_tool_json_after_tool_batch_closes_tool_tail(self, agent):
|
||||
"""finish_reason=tool_calls + truncated args after a real tool must close tool→user."""
|
||||
|
||||
Reference in New Issue
Block a user