The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(agent): accept marker-only finish_reason after stream supersession
rainbowgits622883bad7f5
diff --git a/agent/chat_completion_helpers.py b/agent/chat_completion_helpers.pyindex e24c71e52d3..1fb33e6161c 100644--- a/agent/chat_completion_helpers.py+++ b/agent/chat_completion_helpers.py@@ -4130,6 +4130,29 @@ def interruptible_streaming_api_call(agent, api_kwargs: dict, *, on_first_delta=provider_tool_in_flight["yes"] = Trueexcept Exception:pass+ # Payload-empty terminal chunk: the provider completed the+ # stream (`finish_reason` set, no further writable delta). The+ # attempt/writer fence exists to stop a superseded stream from+ # writing *more* text. Fending this marker-only chunk discards+ # the only completion signal, which the drop-guard then+ # mislabels as a mid-stream drop. A finish chunk that still+ # carries content/tool_calls remains gated.+ try:+ _choices = getattr(_chunk, "choices", None)+ if _choices:+ _choice = _choices[0]+ if getattr(_choice, "finish_reason", None):+ _delta = getattr(_choice, "delta", None)+ _has_write = bool(+ getattr(_delta, "content", None)+ or getattr(_delta, "tool_calls", None)+ or getattr(_delta, "reasoning_content", None)+ or getattr(_delta, "reasoning", None)+ )+ if not _has_write:+ return True+ except Exception:+ passif not _stream_attempt_is_active(stream_attempt_id):return Falsetoken = _writer_token["value"]diff --git a/tests/run_agent/test_partial_stream_finish_reason.py b/tests/run_agent/test_partial_stream_finish_reason.pyindex 2fd563a5f03..975cfacc541 100644--- a/tests/run_agent/test_partial_stream_finish_reason.py+++ b/tests/run_agent/test_partial_stream_finish_reason.py@@ -91,6 +91,95 @@ class TestPartialStreamStubFinishReason:assert response.choices[0].message.tool_calls is None+class TestTerminalChunkFenceException:+ """A superseded writer must still accept the provider's terminal+ finish_reason chunk. Fending that chunk leaves finish_reason None+ after real text was delivered, which the drop-guard mislabels as a+ mid-stream drop even though the provider completed the stream.+ """++ @patch("run_agent.AIAgent._create_request_openai_client")+ @patch("run_agent.AIAgent._close_request_openai_client")+ def test_superseded_writer_accepts_finish_reason_chunk(+ self, _mock_close, mock_create, monkeypatch,+ ):+ monkeypatch.setenv("HERMES_STREAM_RETRIES", "0")+ agent_box = {}++ class SupersedeBeforeFinish:+ response = SimpleNamespace(headers={})++ def __iter__(self):+ yield _make_stream_chunk(content="Long prose that is complete.")+ agent_box["agent"]._claim_stream_writer()+ # Marker-only terminal chunk (empty delta), as vLLM emits.+ yield _make_stream_chunk(finish_reason="stop")++ mock_client = MagicMock()+ mock_client.chat.completions.create.return_value = SupersedeBeforeFinish()+ mock_create.return_value = mock_client++ agent = _make_agent()+ agent_box["agent"] = agent+ response = agent._interruptible_streaming_api_call({})++ assert response.id != PARTIAL_STREAM_STUB_ID+ assert response.choices[0].finish_reason == "stop"+ assert response.choices[0].message.content == "Long prose that is complete."++ @patch("run_agent.AIAgent._create_request_openai_client")+ @patch("run_agent.AIAgent._close_request_openai_client")+ def test_superseded_writer_still_fences_further_content(+ self, _mock_close, mock_create, monkeypatch,+ ):+ monkeypatch.setenv("HERMES_STREAM_RETRIES", "0")+ agent_box = {}++ class SupersedeBeforeMoreText:+ response = SimpleNamespace(headers={})++ def __iter__(self):+ yield _make_stream_chunk(content="kept ")+ agent_box["agent"]._claim_stream_writer()+ # A False accept_chunk ends consumption; this text must+ # never reach the accumulator, and the later finish chunk+ # is never seen (the fence still stops *further* content).+ yield _make_stream_chunk(content="must-not-append")+ yield _make_stream_chunk(finish_reason="stop")++ mock_client = MagicMock()+ mock_client.chat.completions.create.return_value = SupersedeBeforeMoreText()+ mock_create.return_value = mock_client++ agent = _make_agent()+ agent_box["agent"] = agent+ response = agent._interruptible_streaming_api_call({})++ content = response.choices[0].message.content or ""+ assert "must-not-append" not in content+ assert "kept" in content+ assert response.id == PARTIAL_STREAM_STUB_ID++ @patch("run_agent.AIAgent._create_request_openai_client")+ @patch("run_agent.AIAgent._close_request_openai_client")+ def test_genuine_truncation_without_finish_still_drops(+ self, _mock_close, mock_create, monkeypatch,+ ):+ monkeypatch.setenv("HERMES_STREAM_RETRIES", "0")++ def _truncated():+ yield _make_stream_chunk(content="cut off with no terminal chunk")++ mock_client = MagicMock()+ mock_client.chat.completions.create.side_effect = lambda *a, **kw: _truncated()+ mock_create.return_value = mock_client++ agent = _make_agent()+ response = agent._interruptible_streaming_api_call({})++ assert response.id == PARTIAL_STREAM_STUB_ID+ assert response.choices[0].finish_reason == FINISH_REASON_LENGTH+# ── Clean stream-end mid-tool-call (no exception, no finish_reason) ─────────