The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(bot-mode): the retried delivery adopts the DM behind the failed attempt's tool scaffolding too
teknium1d0a016db0059
diff --git a/hermes_cli/quiet_single_query.py b/hermes_cli/quiet_single_query.pyindex abc59af81a8..a228da6b014 100644--- a/hermes_cli/quiet_single_query.py+++ b/hermes_cli/quiet_single_query.py@@ -102,20 +102,33 @@ def adopt_unanswered_turn(cli: Any, query: Any, environ: MutableMapping[str, strfresh process cannot know that by itself (``_DB_PERSISTED_MARKER`` is in-process only), andinferring it from an identical tail alone would swallow a person's deliberate re-send — so thedispatcher must say so with ``tools.bot_relay.RESUME_UNANSWERED_TURN_ENV``, consumed (popped) here- before the turn so tool subprocesses never inherit it. The tail is re-staged as the pending CLI+ before the turn so tool subprocesses never inherit it. The row is re-staged as the pending CLIdict already stamped durable: ``_stage_turn_user_message`` reuses it as this turn's user message and- the flush writes no second row."""+ the flush writes no second row.++ The DM is not always the literal tail: a turn that died mid-way (HTTP 503 on the call after a tool+ round) persisted its tool scaffolding — assistant ``tool_calls`` rows and their ``tool`` results —+ behind the DM before ``agent.turn_recovery`` built the failure text, and the dispatcher retries that+ too. The DM is still unanswered while nothing after it is a plain assistant reply, so it is adopted+ and the failed attempt's scaffolding leaves the in-memory transcript: the re-run starts the turn+ over from the DM (the rows stay in the DB as the record of the failed attempt; the re-run's answer+ lands after them as a valid continuation)."""from tools.bot_relay import RESUME_UNANSWERED_TURN_ENVif environ.pop(RESUME_UNANSWERED_TURN_ENV, None) != "1":return False- history = getattr(cli, "conversation_history", None)- tail = history[-1] if history else None- if not (isinstance(tail, dict) and tail.get("role") == "user" and tail.get("content") == query):+ history = getattr(cli, "conversation_history", None) or []+ idx = next((i for i in range(len(history) - 1, -1, -1)+ if isinstance(history[i], dict) and history[i].get("role") == "user"), None)+ if idx is None or history[idx].get("content") != query:+ return False+ if not all(isinstance(row, dict) and (row.get("role") == "tool" or (row.get("role") == "assistant" and row.get("tool_calls")))+ for row in history[idx + 1:]):return Falsefrom agent.context_compressor import _DB_PERSISTED_MARKER- history.pop()+ tail = history[idx]+ del history[idx:]tail[_DB_PERSISTED_MARKER] = Truecli.agent._pending_cli_user_message = tailreturn Truediff --git a/tests/hermes_cli/test_quiet_single_query.py b/tests/hermes_cli/test_quiet_single_query.pyindex f7e0c04711c..aafba310296 100644--- a/tests/hermes_cli/test_quiet_single_query.py+++ b/tests/hermes_cli/test_quiet_single_query.py@@ -61,3 +61,18 @@ def test_without_the_marker_or_an_unanswered_tail_nothing_is_adopted(monkeypatchanswered = [{"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