The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test(bot-mode): pin the retry gates against the real -Q stream split and the resumed user row
teknium1ea4991469323
diff --git a/tests/hermes_cli/test_quiet_single_query.py b/tests/hermes_cli/test_quiet_single_query.pynew file mode 100644index 00000000000..f7e0c04711c--- /dev/null+++ b/tests/hermes_cli/test_quiet_single_query.py@@ -0,0 +1,63 @@+"""``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")diff --git a/tests/tools/test_bot_retry_policy.py b/tests/tools/test_bot_retry_policy.pyindex a7e800abfa7..307924ecbae 100644--- a/tests/tools/test_bot_retry_policy.py+++ b/tests/tools/test_bot_retry_policy.py@@ -216,3 +216,71 @@ def test_run_delivery_no_retry_for_missing_config(monkeypatch, tmp_path):)assert rc == 1assert len(calls) == 1+++# ── the streams a real failed `-Q` turn writes (#111721) ─────────────────────++# `hermes … -Q` prints the turn's final_response (the provider prose) on STDOUT and the session+# bookkeeping on STDERR — on every run, so a `stderr or stdout` read never saw the provider error.+_REAL_FAILED_STDOUT = (+ "Custom endpoint reported it was overloaded on all 1 attempts — it looks temporarily "+ "unavailable. Wait a minute and send /retry.\n\nProvider said: HTTP 503: Overloaded\n"+)+_REAL_FAILED_STDERR = "Session 20260916_095917_b1a5cd found but has no messages. Starting fresh.\n\nsession_id: 20260916_095917_b1a5cd\n"+++def test_deliver_retry_reads_the_stream_the_cli_writes_and_resumes_the_persisted_row(home, monkeypatch):+ """A relay delivery whose first turn fails the way the CLI really fails (provider prose on+ stdout, `session_id:` banner on stderr) gets its one re-run, and that re-run is told to resume+ the user row the failed attempt already persisted; a still-failing turn hands the sender the+ typed reason instead of `unknown`."""+ from tools.bot_relay import RESUME_UNANSWERED_TURN_ENV++ envs = []++ def _fake_run(argv, **kwargs):+ if not _is_hermes_cli(list(argv)):+ return _Proc(0)+ envs.append(kwargs.get("env") or {})+ if len(envs) == 1:+ return _Proc(1, stdout=_REAL_FAILED_STDOUT, stderr=_REAL_FAILED_STDERR)+ return _Proc(0, stdout="recovered reply")++ monkeypatch.setattr("subprocess.run", _fake_run)+ out = _deliver({"profile": "ops", "message": "ping"})+ assert out["result"]["reply"] == "recovered reply"+ assert [RESUME_UNANSWERED_TURN_ENV in env for env in envs] == [False, True]+ assert envs[1][RESUME_UNANSWERED_TURN_ENV] == "1"++ monkeypatch.setattr(+ "subprocess.run",+ lambda argv, **k: _Proc(1, stdout=_REAL_FAILED_STDOUT, stderr=_REAL_FAILED_STDERR)+ if _is_hermes_cli(list(argv)) else _Proc(0),+ )+ out = _deliver({"profile": "ops", "message": "ping"})+ assert out["error"]["data"]["reason"] == bfr.PROVIDER_SERVER_ERROR+++def test_run_local_turn_retry_reads_the_stream_the_cli_writes_and_resumes_the_persisted_row(monkeypatch, tmp_path, capsys):+ """Same invariant on the same-install `message_agent` runner: the real stdout/stderr split opens+ the retry gate once, and only the re-run carries the resume marker (the first attempt's env is+ otherwise kept)."""+ from tools import bot_mode_dm+ from tools.bot_relay import RESUME_UNANSWERED_TURN_ENV++ dm = tmp_path / "dm.txt"+ dm.write_text("hello")+ envs = []++ def _fake_run(argv, **kwargs):+ envs.append(kwargs.get("env") or {})+ if len(envs) == 1:+ return _Proc(1, stdout=_REAL_FAILED_STDOUT, stderr=_REAL_FAILED_STDERR)+ return _Proc(0, stdout="the reply text")++ monkeypatch.setattr(bot_mode_dm.subprocess, "run", _fake_run)+ rc = bot_mode_dm._run_local_turn(["hermes", "-p", "ops", "chat"], str(dm), env={"HERMES_HOME": str(tmp_path)})+ assert rc == 0+ assert envs[0] == {"HERMES_HOME": str(tmp_path)}+ assert envs[1] == {"HERMES_HOME": str(tmp_path), RESUME_UNANSWERED_TURN_ENV: "1"}+ assert "the reply text" in capsys.readouterr().out