d10bb2ab6f
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.
Parallel directories for one source package, folded into the mirror:
tests/acp -> tests/acp_adapter (its __init__/conftest move with it)
tests/cli -> tests/hermes_cli (prompt_toolkit fixture merged into
hermes_cli/conftest.py)
tests/run_agent -> tests/agent (backoff fixture becomes
agent/conftest.py)
tests/relay -> tests/gateway/relay
tests/state -> tests/hermes_state
246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.
Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.
Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).
Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
120 lines
4.4 KiB
Python
120 lines
4.4 KiB
Python
"""The Anthropic streaming path must not accept a tool_use block whose stream
|
|
died before its input arrived (#80498 sibling).
|
|
|
|
The chat_completions accumulator flags zero-byte tool-call arguments on a
|
|
clean no-finish_reason stream end and routes them through the
|
|
partial-stream-stub retry path. The Anthropic path had the same gap in a
|
|
different shape: a clean SSE close after ``content_block_start(tool_use)``
|
|
but before any ``input_json_delta``/``message_delta`` yields an SDK
|
|
final-message snapshot whose content is NON-empty (the tool_use block is
|
|
there, ``input={}``) and whose ``stop_reason`` is None — which sailed past
|
|
both empty-stream guards and executed the tool with empty input, no retry.
|
|
|
|
The fix raises EmptyStreamError for a tool_use-bearing message with no
|
|
stop_reason, riding the same bounded stream-retry the eventless case uses.
|
|
"""
|
|
from types import SimpleNamespace
|
|
from unittest.mock import MagicMock
|
|
|
|
import pytest
|
|
|
|
|
|
def _make_anthropic_agent(**kwargs):
|
|
from run_agent import AIAgent
|
|
|
|
defaults = dict(
|
|
api_key="test-key",
|
|
base_url="https://example.com/v1",
|
|
model="claude-opus-4-7",
|
|
quiet_mode=True,
|
|
skip_context_files=True,
|
|
skip_memory=True,
|
|
)
|
|
defaults.update(kwargs)
|
|
agent = AIAgent(**defaults)
|
|
agent.api_mode = "anthropic_messages"
|
|
agent._anthropic_client = MagicMock()
|
|
agent._anthropic_api_key = "test-anthropic-key"
|
|
agent._create_request_anthropic_client = lambda *a, **k: agent._anthropic_client
|
|
return agent
|
|
|
|
|
|
def _stream_cm(final_message, events=()):
|
|
cm = MagicMock()
|
|
stream = MagicMock()
|
|
stream.__iter__ = MagicMock(return_value=iter(list(events)))
|
|
stream.get_final_message = MagicMock(return_value=final_message)
|
|
cm.__enter__ = MagicMock(return_value=stream)
|
|
cm.__exit__ = MagicMock(return_value=False)
|
|
return cm
|
|
|
|
|
|
def _tool_use_block(name="write_file", input_obj=None):
|
|
return SimpleNamespace(
|
|
type="tool_use",
|
|
id="toolu_x",
|
|
name=name,
|
|
input=input_obj if input_obj is not None else {},
|
|
)
|
|
|
|
|
|
def _tool_use_start_event(name="write_file"):
|
|
return SimpleNamespace(
|
|
type="content_block_start",
|
|
content_block=SimpleNamespace(type="tool_use", name=name),
|
|
)
|
|
|
|
|
|
class TestAnthropicMidToolCallStreamDrop:
|
|
def test_tool_use_without_stop_reason_raises_empty_stream(self):
|
|
"""The #80498-sibling shape: tool_use block present, stop_reason None."""
|
|
from agent.chat_completion_helpers import EmptyStreamError
|
|
|
|
dropped = MagicMock()
|
|
dropped.content = [_tool_use_block()]
|
|
dropped.stop_reason = None
|
|
dropped.usage = SimpleNamespace(input_tokens=10, output_tokens=2)
|
|
|
|
agent = _make_anthropic_agent()
|
|
agent._anthropic_client.messages.stream = MagicMock(
|
|
return_value=_stream_cm(dropped, events=[_tool_use_start_event()])
|
|
)
|
|
|
|
with pytest.raises(EmptyStreamError, match="tool_use"):
|
|
agent._interruptible_streaming_api_call({"model": "claude-opus-4-7"})
|
|
|
|
def test_completed_tool_use_with_stop_reason_passes(self):
|
|
"""A legitimate tool_use completion (stop_reason set) is untouched."""
|
|
done = MagicMock()
|
|
done.content = [_tool_use_block(input_obj={"path": "a.txt"})]
|
|
done.stop_reason = "tool_use"
|
|
done.usage = SimpleNamespace(input_tokens=10, output_tokens=5)
|
|
|
|
agent = _make_anthropic_agent()
|
|
agent._anthropic_client.messages.stream = MagicMock(
|
|
return_value=_stream_cm(done, events=[_tool_use_start_event()])
|
|
)
|
|
|
|
response = agent._interruptible_streaming_api_call(
|
|
{"model": "claude-opus-4-7"}
|
|
)
|
|
assert response is done
|
|
|
|
def test_text_only_message_without_stop_reason_passes(self):
|
|
"""No tool_use block -> the new gate stays out of the way (text-only
|
|
no-stop_reason handling keeps its pre-existing behavior)."""
|
|
text_only = MagicMock()
|
|
text_only.content = [SimpleNamespace(type="text", text="partial answer")]
|
|
text_only.stop_reason = None
|
|
text_only.usage = SimpleNamespace(input_tokens=10, output_tokens=5)
|
|
|
|
agent = _make_anthropic_agent()
|
|
agent._anthropic_client.messages.stream = MagicMock(
|
|
return_value=_stream_cm(text_only)
|
|
)
|
|
|
|
response = agent._interruptible_streaming_api_call(
|
|
{"model": "claude-opus-4-7"}
|
|
)
|
|
assert response is text_only
|