Skip to content

The agent that grows with you

refactor(gateway): apply shutdown-notice interim marker once in _send_notice_logged

kshitijk4poor6979cb9bab68
diff --git a/agent/conversation_loop.py b/agent/conversation_loop.py
index ad696c9b29b..2fc119eb1ea 100644
--- a/agent/conversation_loop.py
+++ b/agent/conversation_loop.py
@@ -288,16 +288,14 @@ def _continuation_overlap_length(previous: str, continuation: str) -> int:
return matched if matched >= _MIN_CONTINUATION_OVERLAP else 0
-def _join_truncated_parts(parts: List[str | tuple[str, bool]]) -> str:
+def _join_truncated_parts(parts: List[tuple[str, bool]]) -> str:
"""Join continuation fragments, deduping only interrupted-stream seams."""
joined = ""
previous_was_partial_stub = False
- for fragment in parts:
- part, is_partial_stub = (
- fragment if isinstance(fragment, tuple) else (fragment, False)
- )
+ for part, is_partial_stub in parts:
if previous_was_partial_stub and joined and part:
- part = part[_continuation_overlap_length(joined, part):]
+ # Overlap can't exceed len(part): scan only that tail of ``joined``.
+ part = part[_continuation_overlap_length(joined[-len(part):], part):]
if joined and not joined[-1].isspace() and part and not part[0].isspace():
joined += "\n"
joined += part
diff --git a/agent/turn_truncation.py b/agent/turn_truncation.py
index 0197d9bbf49..b689b4692fb 100644
--- a/agent/turn_truncation.py
+++ b/agent/turn_truncation.py
@@ -131,7 +131,7 @@ class TruncationVerdict:
result: Optional[Dict[str, Any]]
messages: List[Dict[str, Any]]
length_continue_retries: int
- truncated_response_parts: List[Any]
+ truncated_response_parts: List[Tuple[str, bool]]
truncated_tool_call_retries: int
retry_count: int
compression_attempts: int
@@ -360,7 +360,7 @@ def recover_from_truncation(
agent: Any, response: Any, finish_reason: str, _retry: TurnRetryState, *,
messages: List[Dict[str, Any]], conversation_history: Any, api_kwargs: Any, api_call_count: int,
effective_task_id: Any, current_turn_user_idx: Any, length_continue_retries: int,
- truncated_response_parts: List[Any], truncated_tool_call_retries: int, retry_count: int,
+ truncated_response_parts: List[Tuple[str, bool]], truncated_tool_call_retries: int, retry_count: int,
compression_attempts: int,
) -> TruncationVerdict:
"""Recover from a truncated response. Order is load-bearing: thinking exhaustion and
diff --git a/gateway/run_shutdown.py b/gateway/run_shutdown.py
index 402f50091d7..1b14760e90a 100644
--- a/gateway/run_shutdown.py
+++ b/gateway/run_shutdown.py
@@ -944,10 +944,9 @@ class GatewayShutdownMixin:
with _log_suppressed(logging.DEBUG, "Cron interrupt notice to %s:%s raised: %s", platform.value, chat_id):
metadata = self._thread_metadata_for_target(platform, chat_id, thread_id, adapter=adapter)
async def send_notice():
- from gateway.run import _interim_metadata # races live turns too (#98432)
if await self._send_notice_logged(
adapter, chat_id, msg, platform.value, "Cron interrupt notice to %s:%s failed: %s",
- "Cron interrupt notice to %s:%s raised: %s", metadata=_interim_metadata(metadata),
+ "Cron interrupt notice to %s:%s raised: %s", metadata=metadata,
):
notified.add(dedup_key)
from gateway.warning_notifications import present_notification
@@ -994,7 +993,10 @@ class GatewayShutdownMixin:
adapter, chat_id: str, msg: str, platform_str: str, fail_fmt: str, raise_fmt: Optional[str] = None, **kw
) -> bool:
"""``adapter.send`` whose failure is debug-logged as ``fmt % (platform, chat, error)`` — ``fail_fmt``
- for success=False, ``raise_fmt`` (default ``fail_fmt``) for a raise; True only on a delivered send."""
+ for success=False, ``raise_fmt`` (default ``fail_fmt``) for a raise; True only on a delivered send.
+ Every shutdown notice races live turns, so it always carries the interim marker (#98432)."""
+ from gateway.run import _interim_metadata
+ kw["metadata"] = _interim_metadata(kw.get("metadata"))
try:
result = await adapter.send(chat_id, msg, **kw)
except Exception as e:
@@ -1062,11 +1064,8 @@ class GatewayShutdownMixin:
# requested outcome of that command and is never suppressed.
async def _send_active(adapter=adapter, chat_id=chat_id, platform_str=platform_str,
metadata=metadata, dedup_key=dedup_key):
- # Fires while a turn may still be streaming: the interim marker keeps a
- # stream-is-the-message adapter from sealing the in-flight answer (#98432).
- from gateway.run import _interim_metadata
if await self._send_shutdown_notice(
- adapter, chat_id, msg, "active chat", platform_str, metadata=_interim_metadata(metadata)
+ adapter, chat_id, msg, "active chat", platform_str, metadata=metadata
):
notified.add(dedup_key)
from gateway.warning_notifications import present_notification
@@ -1107,12 +1106,9 @@ class GatewayShutdownMixin:
"Failed to send shutdown notification to home channel %s:%s: %s", platform.value, home.chat_id, e,
)
continue
- # The broadcast races live turns too, so it always carries the interim marker (#98432).
async def _send_home(adapter=adapter, home=home, platform=platform, metadata=metadata):
- from gateway.run import _interim_metadata
if await self._send_shutdown_notice(
- adapter, str(home.chat_id), msg, "home channel", platform.value,
- metadata=_interim_metadata(metadata),
+ adapter, str(home.chat_id), msg, "home channel", platform.value, metadata=metadata,
):
notified.add(dedup_key)
from gateway.warning_notifications import present_notification
diff --git a/tests/agent/test_continuation_ceiling_wedge.py b/tests/agent/test_continuation_ceiling_wedge.py
index 8e40efe513a..e13aa6a43cc 100644
--- a/tests/agent/test_continuation_ceiling_wedge.py
+++ b/tests/agent/test_continuation_ceiling_wedge.py
@@ -216,16 +216,16 @@ class TestTruncatedPartJoining:
def test_glued_parts_get_a_newline(self):
from agent.conversation_loop import _join_truncated_parts
assert _join_truncated_parts(
- ["Edited index.html", "Review the 5 changes"]
+ [("Edited index.html", False), ("Review the 5 changes", False)]
) == "Edited index.html\nReview the 5 changes"
def test_existing_whitespace_is_not_doubled(self):
from agent.conversation_loop import _join_truncated_parts
- assert _join_truncated_parts(["line one\n", "line two"]) == "line one\nline two"
- assert _join_truncated_parts(["word", " next"]) == "word next"
+ assert _join_truncated_parts([("line one\n", False), ("line two", False)]) == "line one\nline two"
+ assert _join_truncated_parts([("word", False), (" next", False)]) == "word next"
def test_degenerate_inputs(self):
from agent.conversation_loop import _join_truncated_parts
assert _join_truncated_parts([]) == ""
- assert _join_truncated_parts(["only"]) == "only"
- assert _join_truncated_parts(["a", "", "b"]) == "a\nb"
+ assert _join_truncated_parts([("only", False)]) == "only"
+ assert _join_truncated_parts([("a", False), ("", False), ("b", False)]) == "a\nb"