fix(agent): preserve completed replies before terminal silence

Reuse an eligible housekeeping reply before output transforms, persistence
and receipt generation when the local host requires a reply. Keep permitted
silence and marker-only warnings unchanged.

Reconcile recovered stream finals with their own delivered segment,
including transformed text and split previews. Treat short recovered
answers as successful replies and sanitize them before persistence.

Cover local gateway composition, output hooks, recovery, receipt identity,
stream transports and the unchanged silence controls.
This commit is contained in:
Victor Kyriazakos
2026-10-25 01:45:00 +00:00
committed by Teknium
parent dc82bec3bd
commit 5cba2718c0
12 changed files with 774 additions and 20 deletions

View File

@@ -16,6 +16,7 @@ from typing import Any, Dict, List, Optional
from agent import empty_response_guard as _empty_guard
from agent.message_metadata import append_message
from agent.message_sanitization import _sanitize_surrogates
from agent.turn_context_compaction import _refund_api_call
from agent.turn_recovery import interruptible_backoff_sleep
@@ -137,6 +138,33 @@ def _terminal_empty(agent: Any, assistant_message: Any, finish_reason: str, mess
return "(empty)"
def housekeeping_reply_line(agent: Any) -> Optional[str]:
"""The line written beside a housekeeping-only tool round (think blocks stripped), the turn's
reply when the follow-up adds nothing; None when there is none (substantive tools mean it was
narration)."""
fallback = getattr(agent, '_last_content_with_tools', None)
if not (fallback and getattr(agent, '_last_content_tools_all_housekeeping', False)):
return None
return _sanitize_surrogates(agent._strip_think_blocks(fallback).strip())
def take_housekeeping_reply_line(agent: Any) -> Optional[str]:
"""Consume ``housekeeping_reply_line`` as the turn's final response."""
final_response = housekeeping_reply_line(agent)
if final_response is None:
return None
agent._last_content_with_tools = None
agent._last_content_tools_all_housekeeping = False
# Do NOT modify the assistant message content (injected text poisoned history).
# The final IS the response streamed before the housekeeping tool: name that identity
# so a client settles the text it already shows instead of painting it a second time.
# A held turn never showed it: its rewrite goes out as a new final.
if getattr(agent, "_hold_stream", False) is not True:
agent._response_was_previewed = True
agent._reused_response_text = final_response
return final_response
def recover_empty_response(
agent: Any, assistant_message: Any, response: Any, finish_reason: str, *, final_response: Any,
messages: list[dict[str, Any]], api_messages: Any, conversation_history: Any,
@@ -179,22 +207,12 @@ def recover_empty_response(
# Prior turn had real content + ONLY housekeeping tools: model is done, reuse it.
# With substantive tools it was mid-task narration and the empty reply is a choke;
# let the post-tool nudge handle it.
fallback = getattr(agent, '_last_content_with_tools', None)
if fallback and getattr(agent, '_last_content_tools_all_housekeeping', False):
if (final := take_housekeeping_reply_line(agent)) is not None:
_turn_exit_reason = "fallback_prior_turn_content"
logger.info("Empty follow-up after tool calls — using prior turn content as final response")
agent._emit_diagnostic_status("↻ Empty response after tool calls — using earlier content as final answer")
agent._last_content_with_tools = None
agent._last_content_tools_all_housekeeping = False
agent._empty_content_retries = 0
# Do NOT modify the assistant message content (injected text poisoned history).
final_response = agent._strip_think_blocks(fallback).strip()
# The final IS the response streamed before the housekeeping tool: name that identity
# so a client settles the text it already shows instead of painting it a second time.
# A held turn never showed it: its rewrite goes out as a new final.
if getattr(agent, "_hold_stream", False) is not True:
agent._response_was_previewed = True
agent._reused_response_text = final_response
final_response = final
return _verdict("break")
# Post-tool-call empty (no prior content, or only mid-task narration): nudge once.

View File

@@ -9,14 +9,16 @@ from __future__ import annotations
from dataclasses import dataclass
import logging
from typing import Any, Dict, Optional
from typing import Any, Callable, Dict, Optional
from agent.message_metadata import append_message
from agent.reasoning_promotion import answer_in_reasoning_capability
from agent.repetition_guard import STOP_PATH_MIN_CHARS, is_runaway_repetition
from agent.stream_delivery import publish_held_final_row
from agent.turn_failure_copy import stamp_failure
from agent.turn_empty_response import recover_empty_response
from agent.turn_empty_response import (
housekeeping_reply_line, recover_empty_response, take_housekeeping_reply_line,
)
from agent.turn_stop_gates import apply_stop_gates
from agent.turn_truncation import partial_result, repetition_copy
@@ -54,6 +56,39 @@ class FinalResponseVerdict:
result: Optional[dict[str, Any]] = None
def _bare_marker_after_reply(agent: Any, final_response: Any, *, truncated_parts: Any = None,
promoted: Any = None) -> bool:
"""End this turn on the reply written beside a housekeeping round instead of its bare marker:
only when the host says a marker may not stay silent (``_silence_marker_needs_reply``), for a
complete uninterrupted response, and only when a real (non-marker) reply line exists."""
from gateway.response_filters import is_intentional_silence_response
if (getattr(agent, "_silence_marker_needs_reply", False) is not True or truncated_parts or promoted
or getattr(agent, "_interrupt_requested", False)):
return False
reply = housekeeping_reply_line(agent)
return (bool(reply) and not is_intentional_silence_response(reply)
and is_intentional_silence_response(agent._strip_think_blocks(final_response or "").strip()))
def _recover_marker_reply(
agent: Any, final_response: Any, verdict: Callable[[str], FinalResponseVerdict], *,
truncated_parts: Any, promoted: Any,
) -> Optional[FinalResponseVerdict]:
"""Select a completed housekeeping reply before output transforms and persistence.
Keep the caller's loop state, changing only the chosen final and its exit reason.
Other hosts and permitted silence continue through normal text handling.
"""
if not _bare_marker_after_reply(agent, final_response, truncated_parts=truncated_parts, promoted=promoted):
return None
result = verdict("break")
result.final_response = take_housekeeping_reply_line(agent)
result._turn_exit_reason = "fallback_prior_turn_content"
logger.info("Silence marker after a written reply: using that reply as the final response")
return result
def _absorb_continuation_fragments(final_response: Any, parts: Any, messages: Any) -> tuple[Any, bool]:
"""Stitch length-continuation fragments in front of the closing piece; True when there were
any. The continuation recovered, so the fragment rows stay in the transcript, unflagged."""
@@ -155,11 +190,7 @@ def finish_text_response(
active_system_prompt = _ev.active_system_prompt
_preflight_compression_blocked = _ev.preflight_compression_blocked
api_call_count = _ev.api_call_count
if _ev.action == "return":
return _verdict("return", _ev.result)
if _ev.action == "break":
return _verdict("break")
return _verdict("continue")
return _verdict(_ev.action, _ev.result)
agent._empty_content_retries = 0
agent._thinking_prefill_retries = 0
@@ -168,6 +199,10 @@ def finish_text_response(
agent._emit_pending_fallback_notice()
agent._clear_status_buffer()
if marker_verdict := _recover_marker_reply(
agent, final_response, _verdict, truncated_parts=truncated_response_parts, promoted=_promoted):
return marker_verdict
# Defensive: repair malformed role-alternation before API call. Catches cases where the history got
# wedged into a ``tool → user`` or ``user → user`` tail (e.g. after empty- response scaffolding was
# stripped and a new user message landed after an orphan tool result). Most providers return empty

View File

@@ -437,6 +437,7 @@ def _explain_abnormal_exit(agent, final_response, _turn_exit_reason, preserved_v
not _is_empty_terminal
and not preserved_verification_fallback
and not str(_turn_exit_reason).startswith("text_response")
and _turn_exit_reason != "fallback_prior_turn_content"
and len(_stripped) <= 24
and _stripped[-1:] not in _SENTENCE_END
)

View File

@@ -1017,7 +1017,15 @@ class TurnRunner(GatewayTurnProgressMixin, GatewaySessionAgentMixin):
before = (getattr(agent, 'session_prompt_tokens', 0) or 0,
getattr(agent, 'session_completion_tokens', 0) or 0)
with notification_turn(agent, muted=ctx.mute_notification_reply, session_id=ctx.session_id or ""):
result = agent.run_conversation(api_message, **kwargs)
from gateway.response_filters import silence_allowed
# Where a bare marker would become the "needed a reply" warning, the agent ends a
# marker that follows a written reply on that reply, before transform/persistence.
agent._silence_marker_needs_reply = not silence_allowed(
ctx.persist_user_display_kind, ctx.reply_expected)
try:
result = agent.run_conversation(api_message, **kwargs)
finally:
agent._silence_marker_needs_reply = False
if captured is not None:
incoming = max(0, (getattr(agent, 'session_prompt_tokens', 0) or 0) - before[0])
outgoing = max(0, (getattr(agent, 'session_completion_tokens', 0) or 0) - before[1])

View File

@@ -160,6 +160,8 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThi
self._current_edit_interval = self.cfg.edit_interval # adaptive backoff
self._delivered_commentary_texts: list[str] = []
self._delivered_segment_texts: list[str] = [] # finalized text per past segment
self._last_segment_delivery: Optional[tuple[str, Optional[float], str, set[str]]] = None
self._superseded_reply_ids: set[str] = set()
self._in_think_block = False # think-tag filter state (mirrors CLI _stream_delta)
self._think_buffer = ""
self._before_finalize_notified = False
@@ -456,6 +458,12 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThi
finalized = self._clean_for_display(self._last_sent_text).strip()
if finalized:
self._delivered_segment_texts.append(finalized)
self._last_segment_delivery = None
if self._message_id and self._message_id != "__no_edit__":
self._last_segment_delivery = (
self._message_id, self._message_created_ts, self._last_sent_text,
self._stale_preview_ids(segment_only=True),
)
# Also clears the final flags: what we delivered was an interim preamble. Safe:
# got_done returns before any reset; run.py reads flags after the task exits.
self._reset_message_state()
@@ -694,6 +702,31 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThi
else:
self._filter_and_accumulate(item)
def _adopt_recovered_reply(self, final_raw: str) -> bool:
"""Reconcile a marker-followed reply against its own prior segment, not older commentary."""
if (self._message_id or self._cumulative_transport() or self._last_segment_delivery is None
or not _is_intentional_silence_response(self._clean_for_display(self._accumulated))
or _is_intentional_silence_response(self._clean_for_display(final_raw))):
return False
message_id, created_ts, prior_text, prior_ids = self._last_segment_delivery
if len(prior_ids) == 1 and self._display_payload(prior_text) == self._display_payload(final_raw):
self._accumulated = self._stream_ledger = ""
self._mark_final_delivered(record=final_raw)
return True
# The final may be transformed or augmented. Reopen its own message so the
# normal edit/fallback path replaces it; retire old split heads only after success.
self._message_id, self._message_created_ts = message_id, created_ts
self._last_sent_text = prior_text
self._already_sent = True
self._segment_preview_message_ids = {message_id}
# Every replacement path, including fresh-final, owns only this segment.
self._preview_message_ids = set(prior_ids)
self._superseded_reply_ids = prior_ids - {message_id}
self._delivered_segment_texts.pop()
self._last_segment_delivery = None
self._accumulated = self._stream_ledger = final_raw
return True
def _adopt_final_text(self, final_raw: str) -> None:
"""Adopt the authoritative final (see finish()) as the finalize content — only if this
consumer streamed something (a no-stream turn keeps the gateway's final-send
@@ -702,6 +735,8 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThi
strictly prefix-extends the ledger."""
if not (self._accumulated or self._message_id or self._last_sent_text):
return
if self._adopt_recovered_reply(final_raw):
return
if not self._turn_split_delivery:
final_payload = self._clean_for_display(final_raw)
if final_payload and final_payload != self._clean_for_display(self._accumulated):
@@ -879,6 +914,10 @@ class GatewayStreamConsumer(StreamTransportMixin, StreamFallbackMixin, StreamThi
self._mark_final_delivered()
elif self._accumulated:
await self._finalize_edit_path(tick)
if self._final_content_delivered and self._superseded_reply_ids:
await self._delete_previews(self._superseded_reply_ids, skip=self._message_id,
label="Recovered reply")
self._superseded_reply_ids.clear()
async def _finalize_edit_path(self, tick: _Tick) -> None:
"""Edit-transport finalize (the non-native got_done branches, in priority order)."""

View File

@@ -0,0 +1,175 @@
"""A bare silence marker after the model already wrote its reply ends the turn on that reply.
When the follow-up after a housekeeping round is EMPTY the loop already ends the turn on the
line written beside that tool (``fallback_prior_turn_content``). A follow-up that is only a
silence marker says the same thing ("nothing to add"). On a turn whose host says a marker may not
stay silent (``_silence_marker_needs_reply``, set by the gateway from ``silence_allowed``), the
loop takes the same path, so the reply runs through the normal output transform, persistence and
receipt. Without that flag (cron, CLI, machinery or unaddressed turns) the marker is kept.
"""
from unittest.mock import patch
from agent.persisted_turn_receipt import persisted_turn_receipt
from tests.agent import test_empty_response_recovery_persistence as _recovery
from tests.agent import test_hold_stream_exits as _held_exits
real_loop = _recovery.real_loop
held = _held_exits.held
_call, _response = _recovery._call, _recovery._response
ANSWER = "Your meeting is at 3pm."
def _register_todo(agent):
agent.valid_tool_names.add("todo_list")
agent.tools.append({"type": "function", "function": {"name": "todo_list", "parameters": {}}})
def _reply_then_marker(answer=ANSWER):
return [
_response(answer, finish_reason="tool_calls", tool_calls=[_call("todo_list", {"todos": []}, "call_todo")]),
_response("NO_REPLY"),
]
def _stored_final(real_loop):
rows = real_loop.db.get_messages_as_conversation(real_loop.sid)
return [m for m in rows if m.get("role") == "assistant" and not m.get("tool_calls")][-1]["content"]
def _receipt(agent, result):
return persisted_turn_receipt(
result["messages"], agent._persist_user_message_idx, [], result["final_response"], "complete",
compression_unchanged=True, prefix_row_matches=lambda before, after: before is after)
def test_marker_after_a_written_reply_ends_the_turn_on_that_reply(real_loop):
agent = real_loop.agent
_register_todo(agent)
agent._silence_marker_needs_reply = True
result = real_loop.run(_reply_then_marker(), "when is my meeting?")
assert result["final_response"] == ANSWER
assert result["completed"] is True
# Same identity as the empty-follow-up fallback: a client settles the text it already shows.
assert result["response_reused"] is True
assert _stored_final(real_loop) == ANSWER
assert "final_assistant_row_id" in _receipt(agent, result)
def test_a_crash_left_turn_recovers_the_reply_not_the_warning(real_loop):
# Crash recovery judges the persisted final row as live delivery would have.
from gateway.config import Platform
from gateway.run import GatewayRunner
from gateway.session import SessionSource
_register_todo(real_loop.agent)
real_loop.agent._silence_marker_needs_reply = True
real_loop.run(_reply_then_marker(), "when is my meeting?")
history = real_loop.db.get_messages_as_conversation(real_loop.sid)
origin = SessionSource(platform=Platform.SLACK, chat_id="D1", chat_type="dm", user_id="u1")
assert GatewayRunner._crash_left_reply(object(), history, 0, origin) == ANSWER
def test_only_a_complete_uninterrupted_marker_turn_takes_the_reply(real_loop):
# The exact gate finish_text_response applies before transform and persistence.
from agent.turn_final_response import _bare_marker_after_reply
agent = real_loop.agent
agent._last_content_with_tools, agent._last_content_tools_all_housekeeping = ANSWER, True
agent._silence_marker_needs_reply = True
assert _bare_marker_after_reply(agent, "NO_REPLY") is True
assert _bare_marker_after_reply(agent, "Something else.") is False
assert _bare_marker_after_reply(agent, "NO_REPLY", truncated_parts=[("NO", True)]) is False
assert _bare_marker_after_reply(agent, "NO_REPLY", promoted="NO_REPLY") is False
agent._interrupt_requested = True
assert _bare_marker_after_reply(agent, "NO_REPLY") is False
agent._interrupt_requested = False
agent._silence_marker_needs_reply = False
assert _bare_marker_after_reply(agent, "NO_REPLY") is False
def test_the_chosen_reply_runs_through_the_output_transform(real_loop):
agent = real_loop.agent
_register_todo(agent)
agent._silence_marker_needs_reply = True
hook_inputs = []
def hook(name, **kwargs):
if name == "transform_llm_output":
hook_inputs.append(kwargs["response_text"])
return ["Answer [REDACTED]." if "SECRET_TOKEN" in kwargs["response_text"] else kwargs["response_text"]]
return []
with patch("hermes_cli.lifecycle.invoke_hook", hook):
result = real_loop.run(_reply_then_marker("Answer SECRET_TOKEN"), "token?")
assert hook_inputs == ["Answer SECRET_TOKEN"]
assert result["final_response"] == "Answer [REDACTED]."
assert result["response_transformed"] is True
assert _stored_final(real_loop) == "Answer [REDACTED]."
assert "final_assistant_row_id" in _receipt(agent, result)
def test_without_the_host_flag_the_marker_is_kept(real_loop):
# Cron, CLI, machinery and unaddressed turns: the marker is the deliberate result.
_register_todo(real_loop.agent)
result = real_loop.run(_reply_then_marker(), "when is my meeting?")
assert result["final_response"] == "NO_REPLY"
assert _stored_final(real_loop) == "NO_REPLY"
def test_narration_beside_a_substantive_tool_is_not_a_reply(real_loop):
real_loop.agent._silence_marker_needs_reply = True
result = real_loop.run(
[
_response("Let me check the file.", finish_reason="tool_calls",
tool_calls=[_call("read_file", {"path": "missing.txt"}, "call_read")]),
_response("NO_REPLY"),
],
"is the file there?",
)
assert result["final_response"] == "NO_REPLY"
def test_a_later_substantive_round_or_a_new_turn_drops_the_earlier_line(real_loop):
agent = real_loop.agent
_register_todo(agent)
agent._silence_marker_needs_reply = True
result = real_loop.run(
[
_response("Answer A", finish_reason="tool_calls", tool_calls=[_call("todo_list", {"todos": []}, "t1")]),
_response("", finish_reason="tool_calls", tool_calls=[_call("read_file", {"path": "missing"}, "r1")]),
_response("NO_REPLY"),
],
"question",
)
second = real_loop.run([_response("NO_REPLY")], "second question")
assert result["final_response"] == "NO_REPLY"
assert second["final_response"] == "NO_REPLY"
def test_held_turn_rewrites_the_recovered_reply_as_a_new_final(held, monkeypatch):
# A hold_stream transform withheld the reply line, so it was never shown: the recovered reply
# goes out as the rewrite and must not be flagged as previewed/reused (same rule as the
# empty-follow-up path on main).
from run_agent import AIAgent
from tests.fakes.fake_llm_provider import Text, ToolCall
from tests.fakes.redactor_plugin import RAW, REWRITE
monkeypatch.setattr(AIAgent, "_silence_marker_needs_reply", True, raising=False)
result, shown, displayed, _, _ = held([ToolCall("todo_list", {}, text=RAW), Text("NO_REPLY")])
assert result["final_response"] == REWRITE and shown == []
assert not result["response_previewed"] and not result["response_reused"]
assert "SECRET-" not in str(displayed)
assert REWRITE in [row["text"] for row in displayed if row["role"] == "assistant"]

View File

@@ -0,0 +1,22 @@
"""Recovered housekeeping replies remain successful even when short or transformed."""
import pytest
from tests.agent import test_reply_line_before_silence as cases
real_loop = cases.real_loop
@pytest.mark.parametrize("terminal", ["NO_REPLY", ""], ids=["marker", "empty"])
@pytest.mark.parametrize(("raw", "clean"), [("3pm", "3pm"), ("A\ud800B", "A\ufffdB")])
def test_short_recovered_reply_matches_history_and_receipt(real_loop, terminal, raw, clean):
cases._register_todo(real_loop.agent)
real_loop.agent._silence_marker_needs_reply = True
result = real_loop.run([
cases._response(raw, finish_reason="tool_calls", tool_calls=[
cases._call("todo_list", {"todos": []}, "todo")]),
cases._response(terminal),
], "reply")
assert result["completed"] is True
assert result["final_response"] == clean
assert cases._stored_final(real_loop) == clean
assert "final_assistant_row_id" in cases._receipt(real_loop.agent, result)

View File

@@ -0,0 +1,41 @@
"""A transformed recovered final replaces its prior streamed segment, not its neighbors."""
from types import SimpleNamespace
from unittest.mock import AsyncMock
import pytest
from gateway.stream_consumer import GatewayStreamConsumer, StreamConsumerConfig
from tests.gateway.test_stream_consumer_silence import _draft_adapter, _numbered_adapter, _visible
@pytest.mark.asyncio
@pytest.mark.parametrize("transport", ["edit", "draft"])
@pytest.mark.parametrize("raw", ["Answer PRIVATE_VALUE", "PRIVATE_VALUE " * 200])
@pytest.mark.parametrize("final", ["Answer [REDACTED].", "Answer [REDACTED].\n\nFinal advisory."])
@pytest.mark.parametrize("fresh_final", [False, True])
async def test_changed_reply_replaces_only_its_prior_segment(transport, raw, final, fresh_final):
adapter = _draft_adapter() if transport == "draft" else _numbered_adapter()
ids = iter(range(1, 100))
async def send(**kwargs):
return SimpleNamespace(success=True, message_id=f"m{next(ids)}")
adapter.MAX_MESSAGE_LENGTH = 1000
adapter.send = AsyncMock(side_effect=send)
adapter.edit_message = AsyncMock(return_value=SimpleNamespace(success=True))
adapter.delete_message = AsyncMock(return_value=True)
adapter.prefers_fresh_final_streaming = lambda *args, **kwargs: fresh_final
consumer = GatewayStreamConsumer(adapter, "chat", StreamConsumerConfig(
transport=transport, chat_type="dm", cursor="", edit_interval=0.01, buffer_threshold=1))
consumer.on_delta("Earlier independent explanation.")
consumer.on_delta(None)
consumer.on_delta(raw)
consumer.on_delta(None)
consumer.on_delta("NO_REPLY")
consumer.finish(final)
await consumer.run()
assert _visible(adapter) == ["Earlier independent explanation.", final]
assert consumer.delivered_final_matches(final) is True
assert all(call.args[1] != "m1" for call in adapter.delete_message.call_args_list)
assert not consumer.has_durably_delivered_text(raw)

View File

@@ -0,0 +1,97 @@
"""The gateway tells the agent, per turn, when a bare silence marker may not stay silent.
A human turn that ends on a bare marker gets a visible "needed a reply" fallback (#110952). When
the marker follows a reply the model already wrote beside a housekeeping tool, the agent ends the
turn on that reply instead (``agent/turn_final_response.py``), before the output transform and
persistence. The agent cannot judge the turn's silence policy itself, so ``TurnRunner`` sets
``_silence_marker_needs_reply`` from the same ``silence_allowed`` verdict the outer shaping uses,
for the duration of that run only, including each queued follow-up's own verdict.
Driven through the real ``GatewayRunner._run_agent`` turn wiring; only the agent is a recording
double (the real agent half is covered in ``tests/agent/test_reply_line_before_silence.py`` and,
composed end to end, in ``test_silence_reply_composed*.py``).
"""
import importlib
import sys
import types
import pytest
from gateway.config import Platform
from gateway.platforms.event import MessageEvent, MessageType
from gateway.response_filters import INTERNAL_NOTIFICATION_DISPLAY_KIND
from gateway.session import SessionSource
from tests.gateway.test_run_progress_topics import _ACTIVE_ADAPTER, ProgressCaptureAdapter, _make_runner
class RecordingAgent:
"""Records the per-turn flag the gateway set while ``run_conversation`` runs."""
seen: list = []
instances: list = []
def __init__(self, **kwargs):
self.tools = []
type(self).instances.append(self)
def run_conversation(self, message, conversation_history=None, task_id=None, **kwargs):
type(self).seen.append(getattr(self, "_silence_marker_needs_reply", None))
return {"final_response": "ok", "messages": [], "api_calls": 1}
def _source():
return SessionSource(platform=Platform.TELEGRAM, chat_id="dm-1", chat_type="dm", user_id="u1")
async def _run(monkeypatch, tmp_path, *, queued_reply_expected=None, queued=False, **turn_kwargs):
import hermes_yaml as yaml
(tmp_path / "config.yaml").write_text(yaml.safe_dump({"display": {}}), encoding="utf-8")
fake_dotenv = types.ModuleType("dotenv")
fake_dotenv.load_dotenv = lambda *args, **kwargs: None
monkeypatch.setitem(sys.modules, "dotenv", fake_dotenv)
fake_run_agent = types.ModuleType("run_agent")
fake_run_agent.AIAgent = RecordingAgent
monkeypatch.setitem(sys.modules, "run_agent", fake_run_agent)
monkeypatch.setattr(RecordingAgent, "seen", [])
monkeypatch.setattr(RecordingAgent, "instances", [])
adapter = ProgressCaptureAdapter(platform=Platform.TELEGRAM)
monkeypatch.setitem(_ACTIVE_ADAPTER, "adapter", adapter)
runner = _make_runner(adapter)
gateway_run = importlib.import_module("gateway.run")
monkeypatch.setattr(gateway_run, "_hermes_home", tmp_path)
monkeypatch.setattr(gateway_run, "_resolve_runtime_agent_kwargs", lambda: {"api_key": "***"})
session_key = "agent:main:telegram:dm:dm-1"
if queued:
adapter._pending_messages[session_key] = MessageEvent(
text="and tomorrow?", message_type=MessageType.TEXT, source=_source(), message_id="queued-1",
reply_expected=queued_reply_expected)
await runner._run_agent(
message="when is my meeting?", context_prompt="", history=[], source=_source(),
session_id="sess-reply-flag", session_key=session_key, **turn_kwargs,
)
return RecordingAgent.seen
@pytest.mark.asyncio
@pytest.mark.parametrize(("turn_kwargs", "expected"), [
({"reply_expected": True}, True),
({}, True), # unknown addressing on a human turn: the marker would warn
({"reply_expected": False}, False),
({"reply_expected": True, "persist_user_display_kind": INTERNAL_NOTIFICATION_DISPLAY_KIND}, False),
], ids=["addressed", "unknown", "not-addressed", "machinery"])
async def test_flag_follows_the_turns_silence_verdict(monkeypatch, tmp_path, turn_kwargs, expected):
seen = await _run(monkeypatch, tmp_path, **turn_kwargs)
assert seen == [expected]
# Scoped to the run: a cached agent never carries it into another host's turn.
assert all(agent._silence_marker_needs_reply is False for agent in RecordingAgent.instances)
@pytest.mark.asyncio
async def test_a_queued_follow_up_gets_its_own_verdict(monkeypatch, tmp_path):
seen = await _run(monkeypatch, tmp_path, reply_expected=True, queued=True, queued_reply_expected=False)
assert seen == [True, False]

View File

@@ -0,0 +1,71 @@
"""Real provider-scripted AIAgent through TurnRunner and gateway response shaping.
External provider and platform IO are doubles; the agent loop, tool execution,
result finalizer, TurnRunner and final warning/deduplication decision are real.
"""
from unittest.mock import MagicMock
import pytest
import gateway.run as gateway_run
import hermes_yaml as yaml
import run_agent
from gateway.config import Platform
from gateway.platforms.event import MessageEvent
from gateway.session import SessionSource
from tests.agent import test_empty_response_recovery_persistence as recovery
from tests.gateway.test_gateway_silence_tokens import _runner
from tests.gateway.test_run_progress_topics import ProgressCaptureAdapter
real_loop = recovery.real_loop
ANSWER = "Your meeting is at 3pm."
WARNING = "The model returned only a silence marker"
@pytest.mark.asyncio
@pytest.mark.parametrize("platform", [Platform.SLACK, Platform.WHATSAPP])
@pytest.mark.parametrize("interim", [False, True])
@pytest.mark.parametrize("housekeeping", [False, True], ids=["marker-only", "written-reply"])
async def test_real_loop_silence_delivery(monkeypatch, tmp_path, real_loop, platform, interim, housekeeping):
(tmp_path / "config.yaml").write_text(yaml.safe_dump({
"display": {"interim_assistant_messages": interim, "tool_progress": "off"},
}))
agent = real_loop.agent
agent.valid_tool_names.add("todo_list")
agent.tools.append({"type": "function", "function": {"name": "todo_list", "parameters": {}}})
script = []
if housekeeping:
script.append(recovery._response(ANSWER, finish_reason="tool_calls", tool_calls=[
recovery._call("todo_list", {"todos": []}, "call_todo"),
]))
script.append(recovery._response("NO_REPLY"))
agent.client = MagicMock()
agent.client.chat.completions.create.side_effect = lambda **_kw: script.pop(0)
# Supply the real, isolated agent to the runtime constructor seam. Do not
# mock run_conversation, _run_agent, finalization or gateway shaping.
class IsolatedAgent(run_agent.AIAgent):
def __new__(cls, **kwargs):
for key, value in kwargs.items():
if key.endswith("callback"):
setattr(agent, key, value)
return agent
monkeypatch.setattr(run_agent, "AIAgent", IsolatedAgent)
adapter = ProgressCaptureAdapter(platform=platform)
runner = _runner(monkeypatch, tmp_path)
runner.adapters = {platform: adapter}
runner.session_store.get_model_override.return_value = None
source = SessionSource(platform=platform, chat_id="dm-composed", chat_type="dm", user_id="human")
key = f"agent:main:{platform.value}:dm:dm-composed"
entry = runner.session_store.get_or_create_session.return_value
entry.session_key, entry.session_id, entry.platform, entry.chat_type = key, real_loop.sid, platform, "dm"
event = MessageEvent(text="when is my meeting?", source=source, message_id="m1", reply_expected=True)
reply = await runner._handle_message_with_agent(event, source, key, 1)
sent = [call["content"] for call in adapter.sent]
if housekeeping:
assert not any(WARNING in text for text in sent + [reply or ""])
assert sent.count(ANSWER) + int(reply == ANSWER) == 1
else:
assert WARNING in (reply or "")
assert not script, "the real agent must consume the entire scripted provider sequence"

View File

@@ -0,0 +1,138 @@
"""Streamed variant: a real AIAgent streams through TurnRunner into the real stream consumer.
The provider streams the reply beside a housekeeping tool, then a bare marker. Only the
provider and the platform IO are doubles; the agent loop, tool execution, finalizer,
TurnRunner, stream consumer and the final send/dedupe decision are real.
"""
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
import hermes_yaml as yaml
import run_agent
from gateway.config import Platform, StreamingConfig
from gateway.platforms.base import SendResult
from gateway.platforms.event import MessageEvent
from gateway.session import SessionSource
from tests.agent import test_empty_response_recovery_persistence as recovery
from tests.gateway.test_gateway_silence_tokens import _runner
from tests.gateway.test_run_progress_topics import ProgressCaptureAdapter
real_loop = recovery.real_loop
ANSWER = "Your meeting is at 3pm."
WARNING = "The model returned only a silence marker"
def _chunk(content=None, tool_calls=None, finish_reason=None):
delta = SimpleNamespace(content=content, tool_calls=tool_calls, reasoning_content=None, reasoning=None)
return SimpleNamespace(model="test/model", usage=None,
choices=[SimpleNamespace(index=0, delta=delta, finish_reason=finish_reason)])
def _tool_delta(call_id, name, arguments):
return SimpleNamespace(index=0, id=call_id, type="function",
function=SimpleNamespace(name=name, arguments=arguments))
class _Stream:
"""Token stream stand-in; no ``choices`` attribute, so it is read as a stream."""
response = None
def __init__(self, chunks):
self._chunks = chunks
def __iter__(self):
return iter(self._chunks)
def close(self):
pass
class EditingAdapter(ProgressCaptureAdapter):
"""Edit-capable adapter: each send is a new message id, deletes retract it."""
def __init__(self, platform):
super().__init__(platform=platform)
self.deleted = []
async def send(self, chat_id, content, reply_to=None, metadata=None) -> SendResult:
self.sent.append({"chat_id": chat_id, "content": content, "metadata": metadata})
return SendResult(success=True, message_id=f"m{len(self.sent)}")
async def edit_message(self, chat_id, message_id, content, *, finalize=False, metadata=None) -> SendResult:
self.edits.append({"chat_id": chat_id, "message_id": message_id, "content": content})
return SendResult(success=True, message_id=message_id)
async def delete_message(self, chat_id, message_id) -> bool:
self.deleted.append(message_id)
return True
def visible(self):
shown = {f"m{i}": call["content"] for i, call in enumerate(self.sent, 1)}
for edit in self.edits:
shown[edit["message_id"]] = edit["content"]
for message_id in self.deleted:
shown.pop(message_id, None)
return list(shown.values())
@pytest.mark.asyncio
@pytest.mark.parametrize("platform", [Platform.SLACK, Platform.WHATSAPP])
@pytest.mark.parametrize("transform", [False, True])
async def test_streamed_reply_then_marker_shows_the_reply_once(monkeypatch, tmp_path, real_loop, platform, transform):
raw = "Answer PRIVATE_VALUE" if transform else ANSWER
expected = "Answer [REDACTED]." if transform else ANSWER
def output_hook(name, **kwargs):
if transform and name == "transform_llm_output" and kwargs["response_text"] == raw:
return [expected]
return []
monkeypatch.setattr("hermes_cli.lifecycle.invoke_hook", output_hook)
(tmp_path / "config.yaml").write_text(yaml.safe_dump({
"display": {"interim_assistant_messages": False, "tool_progress": "off"},
}))
agent = real_loop.agent
agent.valid_tool_names.add("todo_list")
agent.tools.append({"type": "function", "function": {"name": "todo_list", "parameters": {}}})
script = [
[_chunk(content=raw), _chunk(tool_calls=[_tool_delta("call_todo", "todo_list", '{"todos": []}')]),
_chunk(finish_reason="tool_calls")],
[_chunk(content="NO_REPLY"), _chunk(finish_reason="stop")],
]
agent.client = MagicMock()
agent.client.chat.completions.create.side_effect = (
lambda **kw: _Stream(script.pop(0)) if kw.get("stream") else pytest.fail("expected a streamed request")
)
class IsolatedAgent(run_agent.AIAgent):
def __new__(cls, **kwargs):
for key, value in kwargs.items():
if key.endswith("callback"):
setattr(agent, key, value)
return agent
monkeypatch.setattr(run_agent, "AIAgent", IsolatedAgent)
adapter = EditingAdapter(platform)
runner = _runner(monkeypatch, tmp_path)
runner._hmwa_first_contact_notes = AsyncMock()
runner.config.streaming = StreamingConfig(enabled=True, transport="edit", edit_interval=0.01,
buffer_threshold=1, cursor="")
runner.adapters = {platform: adapter}
runner.session_store.get_model_override.return_value = None
source = SessionSource(platform=platform, chat_id="dm-composed", chat_type="dm", user_id="human")
key = f"agent:main:{platform.value}:dm:dm-composed"
entry = runner.session_store.get_or_create_session.return_value
entry.session_key, entry.session_id, entry.platform, entry.chat_type = key, real_loop.sid, platform, "dm"
event = MessageEvent(text="when is my meeting?", source=source, message_id="m0", reply_expected=True)
reply = await runner._handle_message_with_agent(event, source, key, 1)
assert not script, "the real agent must consume the whole streamed provider script"
shown = adapter.visible() + ([reply] if reply else [])
assert shown == [expected], (adapter.sent, adapter.edits, adapter.deleted, reply)
history = real_loop.db.get_messages_as_conversation(real_loop.sid)
assert history[-1]["content"] == expected
assert not any(WARNING in text or "NO_REPLY" in text for text in shown)

View File

@@ -123,3 +123,112 @@ class TestStreamedSilenceSuppression:
assert consumer.already_sent is False
# --------------------------------------------------------------------------
# Reply written beside a housekeeping tool, then a bare marker follow-up
# --------------------------------------------------------------------------
ANSWER = "Your meeting is at 3pm."
def _numbered_adapter():
"""Edit-transport adapter whose sends get fresh ids, so segments are distinct messages."""
adapter = _make_adapter()
counter = iter(range(1, 100))
async def _send(**kwargs):
return SimpleNamespace(success=True, message_id=f"m{next(counter)}")
adapter.send = AsyncMock(side_effect=_send)
return adapter
def _visible(adapter) -> list:
shown = {}
for call in adapter.send.call_args_list:
shown[f"m{len(shown) + 1}"] = call.kwargs.get("content", "")
for call in adapter.edit_message.call_args_list:
shown[call.kwargs.get("message_id")] = call.kwargs.get("content", "")
for call in adapter.delete_message.call_args_list:
shown.pop(call.args[1], None)
return list(shown.values())
async def _stream_answer_then_marker(final_text):
adapter = _numbered_adapter()
consumer = GatewayStreamConsumer(
adapter, "chat_1", StreamConsumerConfig(edit_interval=0.01, buffer_threshold=1),
)
consumer.on_delta(ANSWER)
consumer.on_delta(None) # housekeeping tool boundary
consumer.on_delta("NO_REPLY")
consumer.finish(final_text)
await consumer.run()
return adapter, consumer
class TestMarkerAfterStreamedReply:
@pytest.mark.asyncio
async def test_reply_final_after_marker_shows_the_answer_once(self):
"""The gateway hands back the already-streamed reply line as the final: it stays
visible exactly once, and the held-back marker segment does not repeat it."""
adapter, consumer = await _stream_answer_then_marker(ANSWER)
assert _visible(adapter) == [ANSWER]
adapter.delete_message.assert_not_awaited()
assert not any("NO_REPLY" in text for text in _sent_and_edited(adapter))
# The gateway's dedupe sees the reply as delivered and sends nothing more.
assert consumer.has_durably_delivered_text(ANSWER) is True
@pytest.mark.asyncio
async def test_marker_final_still_retracts_the_turn(self):
"""A turn that stays silent (final is the marker) still retracts its streamed
preamble; the gateway's whole-response filter owns what goes out next."""
adapter, consumer = await _stream_answer_then_marker("NO_REPLY")
assert _visible(adapter) == []
assert consumer.final_content_delivered is False
def _draft_adapter():
from tests.gateway.test_stream_consumer_draft import _make_draft_capable_adapter
adapter = _make_draft_capable_adapter()
adapter.answer_copies = lambda: [c.kwargs.get("content") for c in adapter.send.call_args_list].count(ANSWER)
adapter.marker_shown = lambda: any(
"NO_REPLY" in (text or "") for text in
[c.kwargs.get("content") for c in adapter.send.call_args_list] + [d["content"] for d in adapter.draft_calls])
return adapter
def _native_adapter():
from tests.gateway.test_stream_consumer_wecom_native import _make_native_streaming_adapter
adapter = _make_native_streaming_adapter()
adapter.answer_copies = lambda: sum(1 for f in adapter.frames if f["finalize"] and f["text"] == ANSWER)
adapter.marker_shown = lambda: any("NO_REPLY" in f["text"] for f in adapter.frames)
return adapter
@pytest.mark.asyncio
@pytest.mark.parametrize("make_adapter", [_draft_adapter, _native_adapter], ids=["draft", "native"])
@pytest.mark.parametrize("final_text", [ANSWER, "NO_REPLY"], ids=["reply-final", "marker-final"])
async def test_marker_after_reply_on_draft_and_native_transports(make_adapter, final_text):
"""Draft and native transports: a reply final after the held-back marker is
delivered once; a marker final keeps today's silent-turn handling."""
adapter = make_adapter()
consumer = GatewayStreamConsumer(
adapter, "chat_1", StreamConsumerConfig(transport="auto", chat_type="dm", cursor="",
edit_interval=0.01, buffer_threshold=1),
)
consumer.on_delta(ANSWER)
consumer.on_delta(None) # housekeeping tool boundary
consumer.on_delta("NO_REPLY")
consumer.finish(final_text)
await consumer.run()
assert not adapter.marker_shown()
if final_text == ANSWER:
assert adapter.answer_copies() == 1
assert consumer.has_durably_delivered_text(ANSWER) is True
else:
assert consumer.final_content_delivered is False