adopt_unanswered_turn only adopted the DM when it was the transcript's literal tail row. A turn that failed transiently mid-way (HTTP 503 on the API call after a tool round) persists user + assistant(tool_calls) + tool rows before agent.turn_recovery builds the failure text (_persist_session runs first), exits 1 with transient prose on stdout, so the policy-gated re-run fires — and found a tool-row tail, adopted nothing, and appended a second copy of the DM: exactly the duplicate this PR set out to remove, just one API call later. The DM is unanswered as long as nothing after it is a plain assistant reply. The re-run now walks back to the last user row, requires it to match the payload and every later row to be tool scaffolding (assistant with tool_calls, or tool), adopts that row and drops the scaffolding from the in-memory turn so the agent starts the turn over from the DM. The scaffolding stays in the DB as the record of the failed attempt; the re-run's answer lands after it as a valid continuation. An answered tail (a real assistant reply) and a missing marker still adopt nothing.
79 lines
3.9 KiB
Python
79 lines
3.9 KiB
Python
"""``hermes chat -Q`` as a dispatcher's re-run of a failed bot delivery (#111721).
|
|
|
|
A failed delivery turn persists its user row before the provider call; the policy-gated re-run
|
|
replays the same session and payload in a fresh process. Told so via HERMES_RESUME_UNANSWERED_TURN,
|
|
the re-run resumes that row instead of appending a second copy of the DM.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
from types import SimpleNamespace
|
|
|
|
import cli
|
|
from agent.context_compressor import _DB_PERSISTED_MARKER
|
|
from tools.bot_relay import RESUME_UNANSWERED_TURN_ENV
|
|
|
|
|
|
def _quiet_turn(monkeypatch, history, marker):
|
|
monkeypatch.delenv("HERMES_KANBAN_GOAL_MODE", raising=False)
|
|
monkeypatch.delenv("HERMES_KANBAN_TASK", raising=False)
|
|
if marker is None:
|
|
monkeypatch.delenv(RESUME_UNANSWERED_TURN_ENV, raising=False)
|
|
else:
|
|
monkeypatch.setenv(RESUME_UNANSWERED_TURN_ENV, marker)
|
|
seen = {}
|
|
|
|
def run_conversation(**kwargs):
|
|
seen["history"] = list(kwargs["conversation_history"])
|
|
seen["env"] = os.environ.get(RESUME_UNANSWERED_TURN_ENV)
|
|
return {"final_response": "ok"}
|
|
|
|
agent = SimpleNamespace(run_conversation=run_conversation, session_id="s-1")
|
|
the_cli = SimpleNamespace(agent=agent, conversation_history=history, session_id="s-1")
|
|
try:
|
|
cli._run_quiet_single_query(the_cli, "hello")
|
|
except SystemExit as exc:
|
|
assert exc.code == 0
|
|
return agent, seen
|
|
|
|
|
|
def test_rerun_resumes_the_unanswered_user_row_it_already_persisted(monkeypatch):
|
|
tail = {"role": "user", "content": "hello"}
|
|
agent, seen = _quiet_turn(monkeypatch, [{"role": "assistant", "content": "earlier"}, tail], "1")
|
|
# The persisted row leaves the history and becomes this turn's staged user dict, already
|
|
# durable — so the agent reuses it as the turn's user message and the flush writes no new row.
|
|
assert seen["history"] == [{"role": "assistant", "content": "earlier"}]
|
|
assert agent._pending_cli_user_message is tail
|
|
assert tail[_DB_PERSISTED_MARKER] is True
|
|
assert seen["env"] is None, "the marker is consumed before the turn so tool subprocesses never inherit it"
|
|
|
|
|
|
def test_without_the_marker_or_an_unanswered_tail_nothing_is_adopted(monkeypatch):
|
|
"""A person re-sending the same word after a failed turn has sent a second real message; an
|
|
answered tail is not a re-run either. Only the dispatcher's explicit marker plus an identical
|
|
unanswered tail adopts a row."""
|
|
tail = {"role": "user", "content": "hello"}
|
|
agent, seen = _quiet_turn(monkeypatch, [tail], None)
|
|
assert seen["history"] == [tail] and not hasattr(agent, "_pending_cli_user_message")
|
|
assert _DB_PERSISTED_MARKER not in tail
|
|
|
|
answered = [{"role": "user", "content": "hello"}, {"role": "assistant", "content": "done"}]
|
|
agent, seen = _quiet_turn(monkeypatch, list(answered), "1")
|
|
assert seen["history"] == answered and not hasattr(agent, "_pending_cli_user_message")
|
|
|
|
|
|
def test_rerun_adopts_the_dm_behind_the_failed_attempts_tool_scaffolding(monkeypatch):
|
|
"""A 503 after a tool round persisted user + assistant(tool_calls) + tool before the failure text,
|
|
and the dispatcher retries it. The DM is still unanswered: the re-run adopts it and drops the
|
|
failed attempt's scaffolding from the in-memory turn instead of appending a second copy."""
|
|
tail = {"role": "user", "content": "hello"}
|
|
scaffolding = [
|
|
{"role": "assistant", "content": None, "tool_calls": [{"id": "c1", "type": "function",
|
|
"function": {"name": "t", "arguments": "{}"}}]},
|
|
{"role": "tool", "tool_call_id": "c1", "content": "result"},
|
|
]
|
|
agent, seen = _quiet_turn(monkeypatch, [{"role": "assistant", "content": "earlier"}, tail, *scaffolding], "1")
|
|
assert seen["history"] == [{"role": "assistant", "content": "earlier"}]
|
|
assert agent._pending_cli_user_message is tail and tail[_DB_PERSISTED_MARKER] is True
|