Skip to content

The agent that grows with you

fix(hold-stream): rows a held user never got stay hidden in history

Teknium90bc28af984d
diff --git a/agent/reasoning_params.py b/agent/reasoning_params.py
index 38d881a4127..b3e9dc95d25 100644
--- a/agent/reasoning_params.py
+++ b/agent/reasoning_params.py
@@ -153,7 +153,13 @@ class ReasoningParamsMixin:
return None
return {"effort": clamp_github_reasoning_effort(cfg.get("effort"), supported)}
- _build_assistant_message = _forward("agent.chat_completion_helpers", "build_assistant_message")
+ def _build_assistant_message(self, assistant_message, finish_reason: str) -> dict:
+ """``build_assistant_message``; a held turn's row with visible text comes back typed hidden
+ (``withhold_held_row``) until ``finish_text_response`` publishes it as the final."""
+ from agent.chat_completion_helpers import build_assistant_message
+ from agent.stream_delivery import withhold_held_row
+
+ return withhold_held_row(self, build_assistant_message(self, assistant_message, finish_reason))
def _reasoning_replay_route(self):
"""Reasoning replay decision for the active route (``message_sanitization.reasoning_replay_route``).
diff --git a/agent/stream_delivery.py b/agent/stream_delivery.py
index 6811c6e1ccd..5ecc3939a37 100644
--- a/agent/stream_delivery.py
+++ b/agent/stream_delivery.py
@@ -16,6 +16,32 @@ from agent.history_commentary import visible_commentary
logger = logging.getLogger("run_agent")
+def withhold_held_row(agent: Any, assistant_msg: dict[str, Any]) -> dict[str, Any]:
+ """Type a held turn's model row ``display_kind="hidden"`` when it has visible text; returns it.
+
+ Every row ``_build_assistant_message`` makes passes here before its first flush, so commentary,
+ continuation fragments, codex incomplete partials and an answer a stop gate sent back never reach
+ a history load; ``finish_text_response`` publishes the one row the user gets
+ (``publish_held_final_row``). The key never reaches the wire: the model's context and its cached
+ prefix are unchanged."""
+ if (getattr(agent, "_hold_stream", False) is True and not assistant_msg.get("display_kind")
+ and agent._interim_assistant_visible_text(assistant_msg)):
+ assistant_msg["display_kind"] = "hidden"
+ return assistant_msg
+
+
+def publish_held_final_row(agent: Any, final_msg: dict[str, Any], final_response: Any, joined: bool) -> None:
+ """The held turn's final row shows what the user got: visible again, and carrying the whole
+ stitched reply when length-continuation fragments (hidden above) were joined into it."""
+ if getattr(agent, "_hold_stream", False) is not True:
+ return
+ if final_msg.get("display_kind") == "hidden":
+ del final_msg["display_kind"]
+ if joined and final_msg.get("content") and final_msg["content"] != final_response:
+ final_msg.setdefault("api_content", final_msg["content"]) # the model keeps the piece it wrote
+ final_msg["content"] = final_response
+
+
class StreamDeliveryMixin:
"""Stream ownership, delta/reasoning hook fan-out and interim-text dedup (see module docstring)."""
diff --git a/agent/turn_empty_response.py b/agent/turn_empty_response.py
index 7a76d297cc6..680b18980ef 100644
--- a/agent/turn_empty_response.py
+++ b/agent/turn_empty_response.py
@@ -189,10 +189,12 @@ def recover_empty_response(
agent._empty_content_retries = 0
# Do NOT modify the assistant message content (injected text poisoned history).
final_response = agent._strip_think_blocks(fallback).strip()
- agent._response_was_previewed = True
# 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.
- agent._reused_response_text = final_response
+ # 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 _verdict("break")
# Post-tool-call empty (no prior content, or only mid-task narration): nudge once.
diff --git a/agent/turn_final_response.py b/agent/turn_final_response.py
index 8b0c9b14930..a75d4d1f9d3 100644
--- a/agent/turn_final_response.py
+++ b/agent/turn_final_response.py
@@ -14,6 +14,7 @@ from typing import Any, 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_stop_gates import apply_stop_gates
@@ -53,6 +54,20 @@ class FinalResponseVerdict:
result: Optional[dict[str, Any]] = None
+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."""
+ if not parts:
+ return final_response, False
+ from agent.conversation_loop import _join_truncated_parts
+
+ for frag in messages:
+ if isinstance(frag, dict):
+ frag.pop("_length_continuation_fragment", None)
+ frag.pop("_length_continuation_nudge", None)
+ return _join_truncated_parts([*parts, (final_response, False)]), True
+
+
def finish_text_response(
agent: Any, *, assistant_message: Any, response: Any, finish_reason: Any, messages: Any,
api_messages: Any, conversation_history: Any, api_call_count: Any, user_message: Any,
@@ -68,7 +83,6 @@ def finish_text_response(
stop gates accept it."""
from agent.conversation_loop import (
_CODEX_ACK_CONTINUATION_NUDGE, _DEGENERATE_FINAL_NUDGE, _DROPPED_TOOLCALL_NUDGE_CONTENT,
- _join_truncated_parts
)
def _verdict(action: str, result: Optional[dict[str, Any]] = None) -> FinalResponseVerdict:
@@ -250,15 +264,9 @@ def finish_text_response(
codex_ack_continuations = 0
- if truncated_response_parts:
- final_response = _join_truncated_parts([*truncated_response_parts, (final_response, False)])
- truncated_response_parts = []
- length_continue_retries = 0
- # The continuation recovered, so the fragments stay in the transcript.
- for _frag in messages:
- if isinstance(_frag, dict):
- _frag.pop("_length_continuation_fragment", None)
- _frag.pop("_length_continuation_nudge", None)
+ final_response, _joined = _absorb_continuation_fragments(final_response, truncated_response_parts, messages)
+ if _joined:
+ truncated_response_parts, length_continue_retries = [], 0
final_response = agent._strip_think_blocks(final_response).strip()
@@ -356,6 +364,7 @@ def finish_text_response(
final_msg["api_content"] = final_response
else:
final_msg["content"] = final_response
+ publish_held_final_row(agent, final_msg, final_response, _joined)
append_message(messages, final_msg)
# Make the answer durable before leaving the loop (_DB_PERSISTED_MARKER keeps
diff --git a/agent/turn_tool_round.py b/agent/turn_tool_round.py
index 13d9cee114f..86ab0ad4e94 100644
--- a/agent/turn_tool_round.py
+++ b/agent/turn_tool_round.py
@@ -110,7 +110,6 @@ def run_tool_round(
assistant_msg, duplicate_previous_interim = stage_tool_call_message(
agent, assistant_message=assistant_message, finish_reason=finish_reason, messages=messages
)
- withhold_held_commentary(agent, assistant_msg)
append_message(messages, assistant_msg)
# Mixed batch: error-result invalid calls and drop them from execution.
@@ -234,15 +233,6 @@ def run_tool_round(
return _verdict("continue")
-def withhold_held_commentary(agent: Any, assistant_msg: dict[str, Any]) -> None:
- """A held turn never shows commentary live (``_deliver_interim``), and no history load may
- show it later: type the row ``display_kind="hidden"`` before its first flush. The key never
- reaches the wire, so the model's context (and its cached prefix) is unchanged."""
- if (getattr(agent, "_hold_stream", False) and not assistant_msg.get("display_kind")
- and agent._interim_assistant_visible_text(assistant_msg)):
- assistant_msg["display_kind"] = "hidden"
-
-
def stage_tool_call_message(
agent: Any, *, assistant_message: Any, finish_reason: Any, messages: Any
) -> tuple[dict[str, Any], bool]:
diff --git a/tests/agent/test_hold_stream_exits.py b/tests/agent/test_hold_stream_exits.py
index 7f98e69af6b..249a4c4f5a3 100644
--- a/tests/agent/test_hold_stream_exits.py
+++ b/tests/agent/test_hold_stream_exits.py
@@ -1,9 +1,11 @@
"""Every exit of a held turn (``hold_stream=True`` transform) goes through the turn-start transform
-snapshot.
+snapshot, and every assistant row the user did not get stays out of history.
Through a real ``AIAgent`` + ``SessionDB`` against the loopback model: a reply capped by the output
-limit on every continuation, an iteration-budget summary, and a provider error that ends the turn
-reach the user (and the stored row) only as the rewrite.
+limit on every continuation, a continuation that recovers, an iteration-budget summary, and a
+provider error that ends the turn all reach the user (and the stored row) only as the rewrite; an
+answer a late language-server verdict sends back for another try, and commentary on a call to an
+unknown tool, are hidden in every history load while the model keeps them byte-for-byte.
"""
import os
@@ -12,7 +14,9 @@ from pathlib import Path
import pytest
from tests.fakes.fake_llm_provider import Error, FakeLLMServer, Text, ToolCall
-from tests.fakes.redactor_plugin import RAW, REWRITE, redactor_home
+from tests.fakes.redactor_plugin import COMMENTARY, RAW, REWRITE, redactor_home
+
+ATTEMPT = "Done, the deploy key SECRET-321 is in place."
def _agent(server, db, session_id: str, **kwargs):
@@ -72,6 +76,15 @@ def test_reply_capped_on_every_continuation_is_still_rewritten(held):
assert "SECRET-" not in str(displayed) and _assistant_texts(displayed) == [result["final_response"]]
+def test_recovered_continuation_shows_only_the_rewritten_whole(held):
+ cut = RAW.index("the backup") # at a space: the fragments join back byte-for-byte
+ first, rest = RAW[:cut], RAW[cut:]
+ result, shown, displayed, _, _ = held([Text(first, finish_reason="length"), Text(rest)])
+ assert result["final_response"] == REWRITE
+ assert shown == []
+ assert _assistant_texts(displayed) == [REWRITE]
+
+
def test_iteration_budget_summary_is_stored_as_the_rewrite(held):
result, shown, displayed, _, _ = held([ToolCall("todo_list", {}), Text(RAW)], max_iterations=1)
assert result["final_response"] == REWRITE
@@ -97,3 +110,38 @@ def test_provider_error_after_a_capped_fragment_delivers_only_the_rewrite(held):
assert result.get("failed") and REWRITE in result["final_response"]
assert "SECRET-" not in result["final_response"] and shown == []
assert "SECRET-" not in str(displayed) and REWRITE in _assistant_texts(displayed)
+
+
+def test_answer_sent_back_by_a_late_lsp_verdict_stays_out_of_history(held, monkeypatch):
+ from agent.lsp import deferred
+
+ verdicts = [[("app.py", "app.py:3: error: name 'deploy' is not defined")]]
+ monkeypatch.setattr(deferred, "settle_for_agent", lambda agent, **_: verdicts.pop() if verdicts else [])
+ result, shown, displayed, server, db = held([Text(ATTEMPT), Text(RAW)])
+ assert result["final_response"] == REWRITE
+ assert shown == []
+ assert "SECRET-" not in str(displayed) and _assistant_texts(displayed) == [REWRITE]
+
+ # The model saw its attempted answer, and gets it byte-for-byte again on resume (cached prefix).
+ live = [m for m in server.main_requests()[1]["messages"] if m["role"] == "assistant"]
+ assert [m["content"] for m in live] == [ATTEMPT]
+ resumed = _agent(server, db, "held-exit")
+ resumed.run_conversation("thanks", conversation_history=db.get_messages_as_conversation("held-exit"))
+ replayed = [m for m in server.main_requests()[2]["messages"] if m["role"] == "assistant"]
+ assert replayed[0] == live[0]
+
+
+def test_reply_reused_from_held_commentary_is_not_reported_as_already_shown(held):
+ # Text beside a housekeeping call becomes the reply when the follow-up is empty. Held, that text
+ # was never shown, so the rewrite must not be flagged as previewed (the CLI would print nothing).
+ result, shown, displayed, _, _ = held([ToolCall("todo_list", {}, text=RAW), Text("")])
+ assert result["final_response"] == REWRITE and shown == []
+ assert not result["response_previewed"] and not result["response_reused"]
+ assert "SECRET-" not in str(displayed) and REWRITE in _assistant_texts(displayed)
+
+
+def test_commentary_on_an_unknown_tool_call_stays_out_of_history(held):
+ result, shown, displayed, _, _ = held([ToolCall("no_such_tool", {}, text=COMMENTARY), Text(RAW)])
+ assert result["final_response"] == REWRITE
+ assert shown == []
+ assert COMMENTARY not in str(displayed) and _assistant_texts(displayed) == [REWRITE]
diff --git a/website/docs/user-guide/features/hooks.md b/website/docs/user-guide/features/hooks.md
index 67dfd724447..dfb103090c5 100644
--- a/website/docs/user-guide/features/hooks.md
+++ b/website/docs/user-guide/features/hooks.md
@@ -1816,7 +1816,8 @@ While a plugin loaded in a profile holds the stream, that profile's sessions sho
What else is held:
-- **Commentary** (model text sent between tool calls, `display.interim_assistant_messages`) never passes through `transform_llm_output`, so a held turn drops it. It stays in the model's context but is stored hidden, so resuming or reopening the session (TUI, desktop app, dashboard, classic CLI) does not show it either.
+- **Commentary** (model text sent between tool calls, `display.interim_assistant_messages`) never passes through `transform_llm_output`, so a held turn drops it. It stays in the model's context but is stored hidden, so resuming or reopening the session (TUI, desktop app, dashboard, classic CLI) does not show it either. The same goes for every other model text the user never got: an answer Hermes sent back for another try (late language-server errors, verify-on-stop), a reply cut by the output limit before its continuation, a call to an unknown tool.
+- **Every way a turn ends passes through the hook.** A reply cut by the output limit on every continuation, an iteration-budget summary, a provider or compression error, an interrupt: whatever text the turn ends with reaches the user only after `transform_llm_output` saw it, and model text is stored as the rewrite.
- **A failing hook withholds the reply.** If no hook rewrote the reply and one raised or timed out, the user gets a short "reply withheld" notice, never the original.
- **The hook set is fixed for the turn.** A held turn runs the `transform_llm_output` hooks that were loaded when it started, even if plugins are re-discovered mid-turn.
- **Tool progress and reasoning** are not the reply and keep their own settings (`display.tool_progress`, `display.show_reasoning`). Turn them off as well if model output the plugin never saw must not reach the user.