Skip to content

The agent that grows with you

fix(agent): keep a held turn's guardrail halt reply out of the stream

Teknium3c701ffc29ec
diff --git a/agent/turn_tool_round.py b/agent/turn_tool_round.py
index 457fe024d1b..7155ce72cdc 100644
--- a/agent/turn_tool_round.py
+++ b/agent/turn_tool_round.py
@@ -183,10 +183,10 @@ def run_tool_round(
agent._emit_diagnostic_status(f"⚠️ Tool guardrail halted {decision.tool_name}: {decision.code}")
append_message(messages, {"role": "assistant", "content": final_response})
# Emit the halt so it isn't mistaken for a crash; the stream callback is still
- # alive, so SSE/TUI clients see the explanation.
+ # alive, so SSE/TUI clients see the explanation. A held turn shows it as the final only.
if final_response:
agent._safe_print(f"\n{final_response}\n")
- if agent.stream_delta_callback:
+ if agent.stream_delta_callback and not getattr(agent, "_hold_stream", False):
with suppress(Exception):
agent.stream_delta_callback(final_response)
agent.stream_delta_callback(None)
diff --git a/tests/agent/test_hold_stream_agent.py b/tests/agent/test_hold_stream_agent.py
index 04c46fef540..23cf46897b4 100644
--- a/tests/agent/test_hold_stream_agent.py
+++ b/tests/agent/test_hold_stream_agent.py
@@ -13,7 +13,7 @@ from pathlib import Path
import pytest
-from tests.fakes.fake_llm_provider import FakeLLMServer, Raw, Text, _chunk
+from tests.fakes.fake_llm_provider import FakeLLMServer, Raw, Text, ToolCall, _chunk
from tests.fakes.redactor_plugin import COMMENTARY, RAW, REWRITE, raw_leaks, redactor_home, redactor_turn
@@ -58,3 +58,30 @@ def test_held_turns_show_only_the_rewritten_final(hold, turn):
assert [text for text in shown if text] == []
else: # the plain transform rewrites the final only; the stream and commentary showed raw text
assert COMMENTARY in shown and raw_leaks(shown)
+
+
+def test_a_held_guardrail_halt_reply_is_not_streamed_ahead_of_the_rewrite():
+ """A turn the tool-loop guardrail halts ends with Hermes' halt explanation, pushed straight to
+ the display callback so SSE/TUI clients see why the agent stopped. Held, it arrives only as the
+ final (after ``transform_llm_output``), never as a stream delta first."""
+ from hermes_cli import plugins
+ from run_agent import AIAgent
+
+ home = Path(os.environ["HERMES_HOME"])
+ failing = ToolCall("read_file", {"path": str(home / "missing" / "SECRET-9.txt")})
+ with FakeLLMServer([failing] * 6 + [Text(RAW)]) as srv:
+ redactor_home(home, srv.base_url, hold=True)
+ with (home / "config.yaml").open("a", encoding="utf-8") as config:
+ config.write("tool_loop_guardrails:\n hard_stop_enabled: true\n hard_stop_after:\n exact_failure: 2\n")
+ plugins._reset_plugin_managers_for_tests()
+ try:
+ agent = AIAgent(base_url=srv.base_url, api_key="sk-fake-e2e", provider="custom", model="fake-model",
+ quiet_mode=True, skip_context_files=True, skip_memory=True, enabled_toolsets=["file"])
+ shown: list = []
+ agent.stream_delta_callback = shown.append
+ result = agent.run_conversation("read the secret file")
+ finally:
+ plugins._reset_plugin_managers_for_tests()
+
+ assert result["turn_exit_reason"] == "guardrail_halt" and result["final_response"]
+ assert [text for text in shown if text] == []