mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:04:08 +03:00
f29772123f
Direct tests for close_interrupted_tool_sequence(stopped=...) and TurnRunner._stamp_segment through _publish_execution (red on main). The stopped docstring now says every interrupt gets the mark, as main already marks interrupted partial replies.
84 lines
3.2 KiB
Python
84 lines
3.2 KiB
Python
"""Regression tests for ``close_interrupted_tool_sequence`` (#48879 follow-up).
|
|
|
|
#48879 closed the tool-call sequence on interrupt inside ``finalize_turn``,
|
|
but the retry/backoff/error interrupt aborts in ``conversation_loop`` ``return``
|
|
early and never reach it — so they persisted a raw ``tool`` tail. The next user
|
|
message then lands as ``... tool → user``, the role-alternation violation that
|
|
makes strict providers (Gemini, Claude) hallucinate a continuation and ignore
|
|
prior context (what the user perceives as "lost context").
|
|
|
|
The fix routes every interrupt abort through this one shared helper. These tests
|
|
pin the helper's contract and prove the post-interrupt + next-user-message
|
|
transcript is alternation-safe.
|
|
"""
|
|
|
|
from agent.message_sanitization import close_interrupted_tool_sequence
|
|
|
|
|
|
def _tool_tail():
|
|
return [
|
|
{"role": "user", "content": "edit the file"},
|
|
{
|
|
"role": "assistant",
|
|
"content": "",
|
|
"tool_calls": [{"id": "c1", "function": {"name": "patch", "arguments": "{}"}}],
|
|
},
|
|
{"role": "tool", "tool_call_id": "c1", "content": "ok edited"},
|
|
]
|
|
|
|
|
|
def _assert_no_tool_then_user(messages):
|
|
for i in range(len(messages) - 1):
|
|
if messages[i].get("role") == "tool":
|
|
assert messages[i + 1].get("role") != "user", (
|
|
f"role-alternation violation: tool → user at index {i}"
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_closing_makes_next_user_message_alternation_safe():
|
|
"""The whole point: appending a user turn after the close must not
|
|
produce the ``tool → user`` shape strict providers choke on."""
|
|
messages = _tool_tail()
|
|
close_interrupted_tool_sequence(messages, None)
|
|
assert isinstance(messages[-1]["timestamp"], float)
|
|
follow_on = messages + [{"role": "user", "content": "they do! increase the timing"}]
|
|
_assert_no_tool_then_user(follow_on)
|
|
# A caller banner is the turn's only explanation: it stays visible, unhidden.
|
|
bannered = _tool_tail()
|
|
assert close_interrupted_tool_sequence(bannered, "Response truncated.") is True
|
|
assert bannered[-1]["content"] == "Response truncated."
|
|
assert "display_kind" not in bannered[-1]
|
|
|
|
|
|
def test_assistant_tail_is_left_untouched():
|
|
messages = [
|
|
{"role": "user", "content": "hi"},
|
|
{"role": "assistant", "content": "partial reply"},
|
|
]
|
|
before = [dict(m) for m in messages]
|
|
assert close_interrupted_tool_sequence(messages, "interrupted") is False
|
|
assert messages == before
|
|
|
|
|
|
def test_user_tail_is_left_untouched():
|
|
messages = [{"role": "user", "content": "hi"}]
|
|
assert close_interrupted_tool_sequence(messages, None) is False
|
|
assert len(messages) == 1
|
|
|
|
|
|
|
|
def test_only_an_interrupted_close_carries_the_stopped_mark():
|
|
"""The closing row is the turn's last stored row: it alone tells a reopened transcript the turn was stopped."""
|
|
stopped = _tool_tail()
|
|
assert close_interrupted_tool_sequence(stopped, None, stopped=True) is True
|
|
assert stopped[-1]["display_metadata"] == {"interrupted": True}
|
|
truncated = _tool_tail()
|
|
assert close_interrupted_tool_sequence(truncated, "Response truncated.") is True
|
|
assert "display_metadata" not in truncated[-1]
|