Skip to content

DeepTutor: Lifelong Personalized Tutoring. https://deeptutor.info/.

fix(partners): merge queued outbound messages before delivery

zhujia1c1dc61fea29
diff --git a/deeptutor/partners/bus/queue.py b/deeptutor/partners/bus/queue.py
index 6e534da95..a1ea2940d 100644
--- a/deeptutor/partners/bus/queue.py
+++ b/deeptutor/partners/bus/queue.py
@@ -1,6 +1,7 @@
"""Async message queue for decoupled channel-agent communication."""
import asyncio
+import time
from deeptutor.partners.bus.events import InboundMessage, OutboundMessage
@@ -26,13 +27,31 @@ class MessageBus:
return await self.inbound.get()
async def publish_outbound(self, msg: OutboundMessage) -> None:
- """Publish a response from the agent to channels."""
+ """Publish a response from the agent to channels.
+
+ Stamps the enqueue time so the outbound lane can report how long a
+ message waited behind other work (the delivery lag a reader sees).
+ """
+ metadata = msg.metadata
+ if isinstance(metadata, dict):
+ metadata.setdefault("_enqueued_at", time.monotonic())
await self.outbound.put(msg)
async def consume_outbound(self) -> OutboundMessage:
"""Consume the next outbound message (blocks until available)."""
return await self.outbound.get()
+ def try_consume_outbound(self) -> OutboundMessage | None:
+ """Return the next queued outbound message without waiting, else ``None``.
+
+ Lets the single partner router look one message ahead (e.g. to merge
+ consecutive stream deltas) without blocking or racing another consumer.
+ """
+ try:
+ return self.outbound.get_nowait()
+ except asyncio.QueueEmpty:
+ return None
+
@property
def inbound_size(self) -> int:
"""Number of pending inbound messages."""
diff --git a/deeptutor/partners/channels/manager.py b/deeptutor/partners/channels/manager.py
index f4da05101..7fd2d2f79 100644
--- a/deeptutor/partners/channels/manager.py
+++ b/deeptutor/partners/channels/manager.py
@@ -28,6 +28,168 @@ _BOOL_CAMEL_ALIASES: dict[str, str] = {
}
+def _drain_outbound(bus: Any) -> OutboundMessage | None:
+ """Pop one queued outbound message without waiting, or ``None`` when empty.
+
+ Tolerates buses that only expose the blocking ``consume_outbound`` (tests
+ and embedders): those simply never coalesce.
+ """
+ try_consume = getattr(bus, "try_consume_outbound", None)
+ if callable(try_consume):
+ return try_consume()
+ try:
+ return bus.outbound.get_nowait()
+ except (asyncio.QueueEmpty, AttributeError, NotImplementedError):
+ return None
+
+
+def coalesce_stream_deltas(
+ bus: Any, first_msg: OutboundMessage
+) -> tuple[OutboundMessage, list[OutboundMessage]]:
+ """Merge consecutive ``_stream_delta`` messages for the same stream segment.
+
+ Shared by the channel dispatcher and the partner outbound router so both
+ keep the same delivery contract: an LLM that generates faster than the
+ channel can post must not turn every token into its own API call.
+
+ Returns:
+ tuple of (merged_message, list_of_non_matching_messages)
+ """
+ target_key = (first_msg.channel, first_msg.chat_id)
+ target_stream = (first_msg.metadata or {}).get("_stream_id")
+ combined_content = first_msg.content
+ final_metadata = dict(first_msg.metadata or {})
+ non_matching: list[OutboundMessage] = []
+
+ # Only merge consecutive deltas of the same stream segment. As soon
+ # as we hit any other message, stop and hand that boundary back to
+ # the dispatcher via `pending`.
+ while True:
+ next_msg = _drain_outbound(bus)
+ if next_msg is None:
+ break
+
+ next_meta = next_msg.metadata or {}
+ same_target = (next_msg.channel, next_msg.chat_id) == target_key
+ same_stream = next_meta.get("_stream_id") == target_stream
+ is_delta = bool(next_meta.get("_stream_delta"))
+ is_end = bool(next_meta.get("_stream_end"))
+
+ if same_target and same_stream and is_delta:
+ combined_content += next_msg.content
+ if is_end:
+ final_metadata["_stream_end"] = True
+ if next_meta.get("_stream_final"):
+ final_metadata["_stream_final"] = True
+ break
+ else:
+ # First non-matching message defines the coalescing boundary.
+ non_matching.append(next_msg)
+ break
+
+ merged = OutboundMessage(
+ channel=first_msg.channel,
+ chat_id=first_msg.chat_id,
+ content=combined_content,
+ # Routing hints belong to the segment, not to one delta: keep the first
+ # message's so a merged frame still replies/threads where it started.
+ reply_to=first_msg.reply_to,
+ media=list(first_msg.media),
+ metadata=final_metadata,
+ )
+ return merged, non_matching
+
+
+def coalesce_progress_messages(
+ bus: Any, first_msg: OutboundMessage
+) -> tuple[OutboundMessage, list[OutboundMessage]]:
+ """Merge consecutive progress messages that are already queued together.
+
+ Narration rounds and tool hints become separate messages, which is what a
+ reader wants while the channel keeps up. When the model outruns the channel
+ they instead pile up in the queue, and every one of them costs a blocking
+ API round trip — post the backlog as one message rather than dribbling it
+ out after the turn has already finished. Nothing waits for a merge: only
+ messages already waiting in the queue are combined.
+ """
+ target_key = (first_msg.channel, first_msg.chat_id)
+ is_tool_hint = bool((first_msg.metadata or {}).get("_tool_hint"))
+ joiner = "\n" if is_tool_hint else "\n\n"
+ combined_content = first_msg.content
+ final_metadata = dict(first_msg.metadata or {})
+ non_matching: list[OutboundMessage] = []
+
+ while True:
+ next_msg = _drain_outbound(bus)
+ if next_msg is None:
+ break
+
+ next_meta = next_msg.metadata or {}
+ mergeable = (
+ (next_msg.channel, next_msg.chat_id) == target_key
+ and bool(next_meta.get("_progress"))
+ and not next_meta.get("_stream_delta")
+ and not next_meta.get("_stream_end")
+ # The "thinking…" notice carries its own lifecycle (it is retracted
+ # once output starts), so it must stay a message of its own.
+ and not next_meta.get("_thinking_notice")
+ and not (first_msg.metadata or {}).get("_thinking_notice")
+ and bool(next_meta.get("_tool_hint")) == is_tool_hint
+ )
+ if not mergeable:
+ non_matching.append(next_msg)
+ break
+ combined_content = f"{combined_content}{joiner}{next_msg.content}"
+
+ merged = OutboundMessage(
+ channel=first_msg.channel,
+ chat_id=first_msg.chat_id,
+ content=combined_content,
+ reply_to=first_msg.reply_to,
+ media=list(first_msg.media),
+ metadata=final_metadata,
+ )
+ return merged, non_matching
+
+
+async def send_with_retry(
+ channel: BaseChannel,
+ msg: OutboundMessage,
+ *,
+ max_attempts: int = 3,
+ retry_delays: tuple[float, ...] = _SEND_RETRY_DELAYS,
+) -> None:
+ """Send a message with retry on failure using exponential backoff.
+
+ Note: CancelledError is re-raised to allow graceful shutdown.
+ """
+ attempts = max(max_attempts, 1)
+
+ for attempt in range(attempts):
+ try:
+ await deliver_outbound(channel, msg)
+ return
+ except asyncio.CancelledError:
+ raise
+ except Exception as e:
+ if attempt == attempts - 1:
+ _logger().exception("Failed to send to {} after {} attempts", msg.channel, attempts)
+ return
+ delay = retry_delays[min(attempt, len(retry_delays) - 1)]
+ _logger().warning(
+ "Send to {} failed (attempt {}/{}): {}, retrying in {}s",
+ msg.channel,
+ attempt + 1,
+ attempts,
+ type(e).__name__,
+ delay,
+ )
+ try:
+ await asyncio.sleep(delay)
+ except asyncio.CancelledError:
+ raise
+
+
class ChannelManager:
"""
Manages chat channels and coordinates message routing.
@@ -296,11 +458,6 @@ class ChannelManager:
except asyncio.CancelledError:
break
- @staticmethod
- async def _send_once(channel: BaseChannel, msg: OutboundMessage) -> None:
- """Send one outbound message without retry policy."""
- await deliver_outbound(channel, msg)
-
def _coalesce_stream_deltas(
self, first_msg: OutboundMessage
) -> tuple[OutboundMessage, list[OutboundMessage]]:
@@ -309,79 +466,18 @@ class ChannelManager:
Returns:
tuple of (merged_message, list_of_non_matching_messages)
"""
- target_key = (first_msg.channel, first_msg.chat_id)
- target_stream = (first_msg.metadata or {}).get("_stream_id")
- combined_content = first_msg.content
- final_metadata = dict(first_msg.metadata or {})
- non_matching: list[OutboundMessage] = []
-
- # Only merge consecutive deltas of the same stream segment. As soon
- # as we hit any other message, stop and hand that boundary back to
- # the dispatcher via `pending`.
- while True:
- try:
- next_msg = self.bus.outbound.get_nowait()
- except asyncio.QueueEmpty:
- break
-
- next_meta = next_msg.metadata or {}
- same_target = (next_msg.channel, next_msg.chat_id) == target_key
- same_stream = next_meta.get("_stream_id") == target_stream
- is_delta = bool(next_meta.get("_stream_delta"))
- is_end = bool(next_meta.get("_stream_end"))
-
- if same_target and same_stream and is_delta:
- combined_content += next_msg.content
- if is_end:
- final_metadata["_stream_end"] = True
- if next_meta.get("_stream_final"):
- final_metadata["_stream_final"] = True
- break
- else:
- # First non-matching message defines the coalescing boundary.
- non_matching.append(next_msg)
- break
-
- merged = OutboundMessage(
- channel=first_msg.channel,
- chat_id=first_msg.chat_id,
- content=combined_content,
- metadata=final_metadata,
- )
- return merged, non_matching
+ return coalesce_stream_deltas(self.bus, first_msg)
async def _send_with_retry(self, channel: BaseChannel, msg: OutboundMessage) -> None:
"""Send a message with retry on failure using exponential backoff.
Note: CancelledError is re-raised to allow graceful shutdown.
"""
- max_attempts = max(getattr(self.channels_config, "send_max_retries", 3), 1)
-
- for attempt in range(max_attempts):
- try:
- await self._send_once(channel, msg)
- return
- except asyncio.CancelledError:
- raise
- except Exception as e:
- if attempt == max_attempts - 1:
- _logger().exception(
- "Failed to send to {} after {} attempts", msg.channel, max_attempts
- )
- return
- delay = _SEND_RETRY_DELAYS[min(attempt, len(_SEND_RETRY_DELAYS) - 1)]
- _logger().warning(
- "Send to {} failed (attempt {}/{}): {}, retrying in {}s",
- msg.channel,
- attempt + 1,
- max_attempts,
- type(e).__name__,
- delay,
- )
- try:
- await asyncio.sleep(delay)
- except asyncio.CancelledError:
- raise
+ await send_with_retry(
+ channel,
+ msg,
+ max_attempts=max(getattr(self.channels_config, "send_max_retries", 3), 1),
+ )
def get_channel(self, name: str) -> BaseChannel | None:
return self.channels.get(name)
diff --git a/deeptutor/services/partners/manager.py b/deeptutor/services/partners/manager.py
index bfd3051ea..efa0ee5f8 100644
--- a/deeptutor/services/partners/manager.py
+++ b/deeptutor/services/partners/manager.py
@@ -9,6 +9,7 @@ isolated chat-format workspace under ``data/partners/{partner_id}/``.
from __future__ import annotations
import asyncio
+from collections import deque
from contextlib import contextmanager
from dataclasses import dataclass, field
from datetime import datetime
@@ -20,13 +21,13 @@ from pathlib import Path
import re
import shutil
import sys
+import time
from typing import Any, Awaitable, BinaryIO, Callable, Iterator
import yaml
from deeptutor.core.stream import StreamEventType
from deeptutor.multi_user.models import CurrentUser
-from deeptutor.partners.channels.base import deliver_outbound
from deeptutor.partners.config.paths import (
get_data_dir,
get_partner_dir,
@@ -48,6 +49,20 @@ from deeptutor.services.partners.workspace import (
logger = logging.getLogger(__name__)
+# The outbound lane is serial, so a message that waited this long behind others
+# is what a reader experiences as "the answer arrives long after the turn".
+_OUTBOUND_LAG_WARN_SECONDS = 2.0
+_OUTBOUND_SEND_WARN_SECONDS = 5.0
+
+
+def _send_max_retries(channel_manager: Any) -> int:
+ """Retry budget for ordinary outbound messages on this partner's channels."""
+ configured = getattr(getattr(channel_manager, "channels_config", None), "send_max_retries", 3)
+ try:
+ return max(int(configured), 1)
+ except (TypeError, ValueError):
+ return 3
+
class PartnerTurnBusyError(RuntimeError):
"""A new message cannot take over an in-flight web conversation."""
@@ -743,58 +758,132 @@ class PartnerManager:
return instance
async def _outbound_router(self, partner_id: str, bus: Any, instance: PartnerInstance) -> None:
- """Route outbound messages to channels, the WebUI feed, and EventBus."""
- try:
- from deeptutor.events.event_bus import Event, EventType, get_event_bus
- from deeptutor.partners.bus.events import OutboundMessage as _OMsg
+ """Route outbound messages to channels, the WebUI feed, and EventBus.
+
+ This is the partner's only outbound lane, and every channel delivery is
+ one blocking HTTP round trip. Left unmerged, a fast model fills the
+ queue faster than Feishu/Telegram can post, so the reply kept arriving
+ long after the turn had finished. Consecutive stream deltas of one
+ segment (and queued narration/tool-hint progress) are therefore merged
+ before delivery — the same contract the channel dispatcher applies — and
+ ordinary messages retry with backoff.
+ """
+ from deeptutor.events.event_bus import Event, EventType, get_event_bus
+ from deeptutor.partners.bus.events import OutboundMessage as _OMsg
+ from deeptutor.partners.channels.manager import (
+ coalesce_progress_messages,
+ coalesce_stream_deltas,
+ send_with_retry,
+ )
+
+ pending: deque[_OMsg] = deque()
+ try:
event_bus = get_event_bus()
while True:
- msg: _OMsg = await bus.consume_outbound()
- metadata = msg.metadata or {}
- is_progress = bool(
- metadata.get("_progress")
- or metadata.get("_stream_delta")
- or metadata.get("_stream_end")
- )
+ if pending:
+ msg = pending.popleft()
+ else:
+ msg: _OMsg = await bus.consume_outbound()
- if instance.channel_manager:
- channel = instance.channel_manager.get_channel(msg.channel)
- if channel:
- try:
- await deliver_outbound(channel, msg)
- except Exception:
- logger.exception(
- "Failed to send to channel %s for partner %s",
- msg.channel,
- partner_id,
+ metadata = msg.metadata or {}
+ if metadata.get("_stream_delta") and not metadata.get("_stream_end"):
+ msg, extra = coalesce_stream_deltas(bus, msg)
+ pending.extend(extra)
+ metadata = msg.metadata or {}
+ elif metadata.get("_progress"):
+ msg, extra = coalesce_progress_messages(bus, msg)
+ pending.extend(extra)
+ metadata = msg.metadata or {}
+ is_stream = bool(metadata.get("_stream_delta") or metadata.get("_stream_end"))
+ is_progress = bool(metadata.get("_progress") or is_stream)
+
+ # One bad message must not kill the lane for the whole partner.
+ try:
+ # A channel reload swaps the manager, so re-read it per
+ # message instead of pinning the one this task started with.
+ channel_manager = instance.channel_manager
+ if channel_manager:
+ channel = channel_manager.get_channel(msg.channel)
+ if channel:
+ # Stream frames self-heal (the next update carries
+ # the full text), so a failed one must not hold the
+ # lane behind backoff sleeps.
+ queued_at = metadata.get("_enqueued_at")
+ started = time.monotonic()
+ await send_with_retry(
+ channel,
+ msg,
+ max_attempts=(
+ 1 if is_stream else _send_max_retries(channel_manager)
+ ),
+ )
+ waited = started - queued_at if isinstance(queued_at, float) else 0.0
+ delivered_in = time.monotonic() - started
+ if waited >= _OUTBOUND_LAG_WARN_SECONDS:
+ logger.warning(
+ "Outbound backlog for partner %s on %s: message waited %.1fs "
+ "in the queue (%s, %d chars)",
+ partner_id,
+ msg.channel,
+ waited,
+ "stream" if is_stream else "message",
+ len(msg.content or ""),
+ )
+ elif delivered_in >= _OUTBOUND_SEND_WARN_SECONDS:
+ logger.warning(
+ "Slow outbound send for partner %s on %s: %.1fs (%s, %d chars)",
+ partner_id,
+ msg.channel,
+ delivered_in,
+ "stream" if is_stream else "message",
+ len(msg.content or ""),
+ )
+ else:
+ logger.debug(
+ "Outbound %s to %s for partner %s delivered in %.2fs "
+ "(queue %.2fs, %d chars)",
+ "stream" if is_stream else "message",
+ msg.channel,
+ partner_id,
+ delivered_in,
+ waited,
+ len(msg.content or ""),
+ )
+ if not is_progress and msg.chat_id:
+ instance.channel_bindings[msg.channel] = msg.chat_id
+
+ if not is_progress:
+ # Normal channel turns are already mirrored with their
+ # user bubble and full StreamEvent trace by PartnerRunner.
+ # Keep a final-only proactive frame only for direct
+ # producers such as cron jobs that bypass the inbound loop.
+ if not (msg.metadata or {}).get("_web_activity_mirrored"):
+ instance.activity_feed.publish(
+ None,
+ {"type": "proactive", "content": msg.content or ""},
+ )
+ await event_bus.publish(
+ Event(
+ type=EventType.CAPABILITY_COMPLETE,
+ task_id=f"partner:{partner_id}:{msg.channel}:{msg.chat_id}",
+ user_input="",
+ agent_output=msg.content or "",
+ metadata={
+ "source": "partner",
+ "partner_id": partner_id,
+ "channel": msg.channel,
+ "chat_id": msg.chat_id,
+ },
)
- if not is_progress and msg.chat_id:
- instance.channel_bindings[msg.channel] = msg.chat_id
-
- if not is_progress:
- # Normal channel turns are already mirrored with their user
- # bubble and full StreamEvent trace by PartnerRunner. Keep a
- # final-only proactive frame only for direct producers such
- # as cron jobs that bypass the inbound channel loop.
- if not (msg.metadata or {}).get("_web_activity_mirrored"):
- instance.activity_feed.publish(
- None,
- {"type": "proactive", "content": msg.content or ""},
- )
- await event_bus.publish(
- Event(
- type=EventType.CAPABILITY_COMPLETE,
- task_id=f"partner:{partner_id}:{msg.channel}:{msg.chat_id}",
- user_input="",
- agent_output=msg.content or "",
- metadata={
- "source": "partner",
- "partner_id": partner_id,
- "channel": msg.channel,
- "chat_id": msg.chat_id,
- },
)
+ except asyncio.CancelledError:
+ raise
+ except Exception:
+ logger.exception(
+ "Failed to deliver outbound message to %s for partner %s",
+ msg.channel,
+ partner_id,
)
except asyncio.CancelledError:
return
diff --git a/tests/services/partners/test_partner_outbound_delivery.py b/tests/services/partners/test_partner_outbound_delivery.py
index 84b8412d5..885a4c4e4 100644
--- a/tests/services/partners/test_partner_outbound_delivery.py
+++ b/tests/services/partners/test_partner_outbound_delivery.py
@@ -9,6 +9,7 @@ from unittest.mock import AsyncMock, MagicMock
import pytest
from deeptutor.partners.bus.events import OutboundMessage
+from deeptutor.partners.bus.queue import MessageBus
from deeptutor.services.partners.manager import PartnerManager
@@ -22,6 +23,24 @@ class _FiniteBus:
raise asyncio.CancelledError
+class _QueueBus:
+ """A real outbound queue, drained by a finite ``consume_outbound``."""
+
+ def __init__(self, messages: list[OutboundMessage]) -> None:
+ self._bus = MessageBus()
+ for message in messages:
+ self._bus.outbound.put_nowait(message)
+
+ async def consume_outbound(self) -> OutboundMessage:
+ message = self.try_consume_outbound()
+ if message is None:
+ raise asyncio.CancelledError
+ return message
+
+ def try_consume_outbound(self) -> OutboundMessage | None:
+ return self._bus.try_consume_outbound()
+
+
@pytest.mark.asyncio
async def test_live_router_dispatches_delta_end_and_final_once(monkeypatch) -> None:
channel = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
@@ -63,3 +82,231 @@ async def test_live_router_dispatches_delta_end_and_final_once(monkeypatch) -> N
assert channel.send.await_args.args[0].content == "ordinary reply"
assert event_bus.publish.await_count == 2 # streamed final and ordinary final
assert instance.activity_feed.publish.call_count == 2
+
+
+async def test_live_router_merges_a_backlog_of_deltas_before_delivering(monkeypatch) -> None:
+ """One queued burst must reach the channel as one edit, in order."""
+ channel = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
+ event_bus = SimpleNamespace(publish=AsyncMock())
+ monkeypatch.setattr("deeptutor.events.event_bus.get_event_bus", lambda: event_bus)
+ instance = SimpleNamespace(
+ channel_manager=SimpleNamespace(get_channel=lambda _: channel),
+ channel_bindings={},
+ activity_feed=SimpleNamespace(publish=MagicMock()),
+ )
+ delivered: list[tuple[str, str]] = []
+ channel.send_delta.side_effect = lambda _chat, content, _meta: delivered.append(
+ ("delta", content)
+ )
+ channel.send.side_effect = lambda msg: delivered.append(("send", msg.content))
+ bus = _QueueBus(
+ [
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="Hel",
+ metadata={"_stream_delta": True, "_stream_id": "turn:1"},
+ ),
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="lo ",
+ metadata={"_stream_delta": True, "_stream_id": "turn:1"},
+ ),
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="world",
+ metadata={"_stream_delta": True, "_stream_id": "turn:1"},
+ ),
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="tool hint",
+ metadata={"_progress": True, "_tool_hint": True},
+ ),
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="",
+ metadata={"_stream_end": True, "_stream_final": True, "_stream_id": "turn:1"},
+ ),
+ ]
+ )
+
+ await PartnerManager._outbound_router(None, "partner", bus, instance)
+
+ # Three deltas collapsed into one stream edit; the boundary message and the
+ # stream end that followed kept their order.
+ assert delivered == [
+ ("delta", "Hello world"),
+ ("send", "tool hint"),
+ ("delta", ""),
+ ]
+ assert channel.send_delta.await_args_list[1].args[2]["_stream_final"] is True
+ # Nothing here is a final answer, so nothing fans out to the web feed.
+ assert event_bus.publish.await_count == 0
+
+
+async def test_live_router_retries_finals_but_never_stream_frames(monkeypatch) -> None:
+ """A failed stream frame self-heals; a final answer deserves retries."""
+ channel = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
+ monkeypatch.setattr(
+ "deeptutor.events.event_bus.get_event_bus", lambda: SimpleNamespace(publish=AsyncMock())
+ )
+ captured: list[tuple[str, int]] = []
+
+ async def spy(_channel, msg, *, max_attempts=3, retry_delays=None):
+ captured.append((msg.content, max_attempts))
+
+ monkeypatch.setattr("deeptutor.partners.channels.manager.send_with_retry", spy)
+ instance = SimpleNamespace(
+ channel_manager=SimpleNamespace(
+ get_channel=lambda _: channel,
+ channels_config=SimpleNamespace(send_max_retries=5),
+ ),
+ channel_bindings={},
+ activity_feed=SimpleNamespace(publish=MagicMock()),
+ )
+
+ await PartnerManager._outbound_router(
+ None,
+ "partner",
+ _FiniteBus(
+ [
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="part",
+ metadata={"_stream_delta": True, "_stream_id": "turn:1"},
+ ),
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="",
+ metadata={"_stream_end": True, "_stream_id": "turn:1"},
+ ),
+ OutboundMessage(channel="feishu", chat_id="oc_group", content="final answer"),
+ ]
+ ),
+ instance,
+ )
+
+ assert captured == [("part", 1), ("", 1), ("final answer", 5)]
+
+
+async def test_live_router_merges_queued_progress_messages(monkeypatch) -> None:
+ """Buffered mode must not post one Feishu message per narration round."""
+ channel = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
+ monkeypatch.setattr(
+ "deeptutor.events.event_bus.get_event_bus", lambda: SimpleNamespace(publish=AsyncMock())
+ )
+ instance = SimpleNamespace(
+ channel_manager=SimpleNamespace(get_channel=lambda _: channel),
+ channel_bindings={},
+ activity_feed=SimpleNamespace(publish=MagicMock()),
+ )
+ bus = _QueueBus(
+ [
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="先看你的场景…",
+ metadata={"_progress": True},
+ ),
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="再给三句例句",
+ metadata={"_progress": True},
+ ),
+ # Tool hints are their own kind: never folded into narration.
+ OutboundMessage(
+ channel="feishu",
+ chat_id="oc_group",
+ content="→ web_search",
+ metadata={"_progress": True, "_tool_hint": True},
+ ),
+ OutboundMessage(channel="feishu", chat_id="oc_group", content="final answer"),
+ ]
+ )
+
+ await PartnerManager._outbound_router(None, "partner", bus, instance)
+
+ assert [call.args[0].content for call in channel.send.await_args_list] == [
+ "先看你的场景…\n\n再给三句例句",
+ "→ web_search",
+ "final answer",
+ ]
+
+
+async def test_live_router_survives_a_failing_message(monkeypatch) -> None:
+ """A poison message must not kill the partner's only outbound lane."""
+ channel = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
+ channel.send.side_effect = [RuntimeError("boom"), None]
+ monkeypatch.setattr(
+ "deeptutor.events.event_bus.get_event_bus", lambda: SimpleNamespace(publish=AsyncMock())
+ )
+ instance = SimpleNamespace(
+ channel_manager=SimpleNamespace(
+ get_channel=lambda _: channel,
+ channels_config=SimpleNamespace(send_max_retries=1),
+ ),
+ channel_bindings={},
+ activity_feed=SimpleNamespace(publish=MagicMock()),
+ )
+
+ await PartnerManager._outbound_router(
+ None,
+ "partner",
+ _FiniteBus(
+ [
+ OutboundMessage(channel="feishu", chat_id="oc_group", content="first"),
+ OutboundMessage(channel="feishu", chat_id="oc_group", content="second"),
+ ]
+ ),
+ instance,
+ )
+
+ assert channel.send.await_count == 2
+ assert channel.send.await_args.args[0].content == "second"
+ assert instance.channel_bindings["feishu"] == "oc_group"
+
+
+async def test_live_router_follows_a_channel_reload(monkeypatch) -> None:
+ """A reloaded channel manager must take over without restarting the lane."""
+ reloaded = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
+ channel = SimpleNamespace(send=AsyncMock(), send_delta=AsyncMock())
+ monkeypatch.setattr(
+ "deeptutor.events.event_bus.get_event_bus", lambda: SimpleNamespace(publish=AsyncMock())
+ )
+ instance = SimpleNamespace(
+ channel_manager=SimpleNamespace(get_channel=lambda _: channel),
+ channel_bindings={},
+ activity_feed=SimpleNamespace(publish=MagicMock()),
+ )
+
+ def reload(_msg) -> None:
+ instance.channel_manager = SimpleNamespace(get_channel=lambda _: reloaded)
+
+ channel.send.side_effect = reload
+
+ await PartnerManager._outbound_router(
+ None,
+ "partner",
+ _FiniteBus(
+ [
+ OutboundMessage(channel="feishu", chat_id="oc_group", content="before reload"),
+ OutboundMessage(channel="feishu", chat_id="oc_group", content="after reload"),
+ ]
+ ),
+ instance,
+ )
+
+ assert channel.send.await_count == 1
+ assert reloaded.send.await_args.args[0].content == "after reload"