Skip to content

The agent that grows with you

fix(cli): leave the turn untouched when a prompt is refused behind a lost turn

unsupportedpastels1227a2d5bb62
diff --git a/hermes_cli/cli_chat_turn_mixin.py b/hermes_cli/cli_chat_turn_mixin.py
index 5a3e99591ed..e49e32f8cd0 100644
--- a/hermes_cli/cli_chat_turn_mixin.py
+++ b/hermes_cli/cli_chat_turn_mixin.py
@@ -316,10 +316,12 @@ class CLIChatTurnMixin:
self.agent._voice_turn_pending = bool(turn.voice_prefix) # auxiliary.voice_chat route
# One-shot /model and /reload-skills notes; _prepend_note_to_message also handles
# multimodal content-part lists (string concat raised TypeError with an image).
+ _taken_notes = {}
for _note_attr in ("_pending_model_switch_note", "_pending_skills_reload_note"):
_note = getattr(self, _note_attr, None)
if _note:
agent_message = _prepend_note_to_message(agent_message, _note)
+ _taken_notes[_note_attr] = _note
setattr(self, _note_attr, None)
# Barged mid-speech (VAD or record key)? Tell the model it was cut off.
from tools.tts_streaming import SPEECH_INTERRUPTED_NOTE, take_speech_interrupted
@@ -362,7 +364,11 @@ class CLIChatTurnMixin:
"completed": False, "failed": True, "error": _summary,
}
finally:
- if _one_turn_model_restore and (turn.result or {}).get("failure_reason") == "session_busy":
+ _unadmitted = (turn.result or {}).get("admitted") is False
+ if _unadmitted: # refused before the submit: the resend still owes the notes
+ for _note_attr, _note in _taken_notes.items():
+ setattr(self, _note_attr, _note)
+ if _one_turn_model_restore and (_unadmitted or (turn.result or {}).get("failure_reason") == "session_busy"):
# The owner admitted nothing: the resend is still the one turn the pick was for.
self._pending_one_turn_model_restore = _one_turn_model_restore
elif _one_turn_model_restore:
diff --git a/hermes_cli/gateway_agent.py b/hermes_cli/gateway_agent.py
index cd141e0a805..fce0bea2925 100644
--- a/hermes_cli/gateway_agent.py
+++ b/hermes_cli/gateway_agent.py
@@ -773,7 +773,10 @@ class GatewayAgent:
# Building the params consumes a ``/model --once`` pick (``_take_turn_route``).
notice = self._prepare_submit(turn)
if notice:
- return self._failed(history, user_message, notice, failure_reason="gateway_connection")
+ # Refused before prompt.submit: nothing was admitted, so the CLI keeps the turn's
+ # local state (no history row, the /model --once pick and notes) for the resend.
+ return {**self._failed(history, user_message, notice, failure_reason="gateway_connection"),
+ "messages": history, "admitted": False}
params = self._submit_params(user_message, persist_user_message, turn_author)
if self._stop(turn)():
raise CallStopped("prompt.submit")
diff --git a/tests/hermes_cli/test_cli_gateway_model_scope.py b/tests/hermes_cli/test_cli_gateway_model_scope.py
index 79ba9b53942..e6703c5e460 100644
--- a/tests/hermes_cli/test_cli_gateway_model_scope.py
+++ b/tests/hermes_cli/test_cli_gateway_model_scope.py
@@ -392,6 +392,55 @@ def test_a_once_pick_refused_as_session_busy_rides_the_resend(monkeypatch):
assert (stub.model, stub.agent.model) == ("old/model", "old/model")
+class LostTurnConnection(RecordingConnection):
+ """The owner restarted mid-turn and fenced ``adm-lost`` unknown; ``/discard`` resolves it."""
+
+ def __init__(self, info):
+ super().__init__(info)
+ self.pending = [{"admission_id": "adm-lost", "status": "unknown", "execution_generation": 4}]
+ self._state.pending = list(self.pending)
+
+ def reattach(self, session_id, abandon=None):
+ snapshot = self.call("session.resume", session_id=session_id)
+ self._state.adopt_snapshot(snapshot)
+ return snapshot
+
+ def call(self, method, *, timeout=None, **params):
+ if method == "session.resume":
+ return {**super().call(method, timeout=timeout, **params), "pending": list(self.pending)}
+ if method == "prompt.resolve_unknown":
+ self.calls.append((method, params))
+ self.pending = []
+ return {}
+ return super().call(method, timeout=timeout, **params)
+
+
+def test_a_prompt_refused_behind_a_lost_turn_leaves_its_turn_for_the_resend(monkeypatch):
+ """Review of #143595: the refusal admitted nothing, so after /discard the resend carries the
+ /model --once pick and its note, and the CLI's history holds the prompt once, as the owner's does."""
+ conn = LostTurnConnection({"model": "old/model", "provider": "openrouter"})
+ cli_mod, stub, _printed = _chat_cli(monkeypatch, conn)
+ stub._chat_stage_user_message = cli_mod.HermesCLI._chat_stage_user_message.__get__(stub)
+ stub._chat_settle_turn = cli_mod.HermesCLI._chat_settle_turn.__get__(stub)
+ stub._flush_stream = lambda: None
+ monkeypatch.setattr("hermes_cli.gateway_side.connection_for", lambda cli: conn)
+ cli_mod.HermesCLI._handle_model_switch(stub, "/model claude-sonnet-4.6 --provider anthropic --once")
+ note = stub._pending_model_switch_note
+
+ stub.chat("second ask") # refused with the /discard banner
+ assert not _submits(conn)
+ cli_mod.HermesCLI._handle_discard_command(stub, "/discard adm-lost")
+ assert stub.chat("second ask") == "ok"
+ assert stub.chat("the turn after") == "ok"
+
+ resend, after = _submits(conn)
+ assert resend.get("model_override") == {"model": "claude-sonnet-4.6", "provider": "anthropic"}, resend
+ assert note and note in resend.get("api_text", ""), resend
+ assert "model_override" not in after, after
+ users = [m["content"] for m in stub.conversation_history if m["role"] == "user"]
+ assert users == ["second ask", "the turn after"], users
+
+
def test_the_once_pick_is_taken_when_the_admission_is_built(monkeypatch):
"""dokterdok on #140969: consume the pick when the params are built, not in the call's ``finally``, so
a second ``run_conversation`` submitting while the first ``prompt.submit`` is still blocked cannot carry