From 353db7ba10a879c83ef62be6e1f5431ddee86474 Mon Sep 17 00:00:00 2001 From: OpenBot Date: Sat, 24 Oct 2026 10:12:19 +0800 Subject: [PATCH] fix(wecom): proactive fallback must not spam one message per delta The 59c44e9 fallback replied every coalesced delta (~0.8s) as a separate proactive message once the callback req_id went stale - a wall of repeating snapshots. Now: intermediate deltas are dropped after a single 'long reply, result will follow' notice; only the final text goes out. --- src/agenthub/channels/wecom.py | 23 ++++++++++++++++------- tests/test_channels.py | 12 +++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/agenthub/channels/wecom.py b/src/agenthub/channels/wecom.py index 677bee9..1420b42 100644 --- a/src/agenthub/channels/wecom.py +++ b/src/agenthub/channels/wecom.py @@ -501,13 +501,22 @@ class WecomAibotChannel: with self._stream_lock: self._req_ids.pop(chat_id, None) return - # no live callback req_id: fall back to a proactive message - self.reply(chat_id, text) if text else None - if finished: - st["sent"] = True - with self._stream_lock: - self._req_ids.pop(chat_id, None) - self._logger.info(f"[wecom] stream finished via proactive message (callback req_id stale or absent) chat={chat_id}") + # No live callback req_id (stale or absent): proactive aibot_send_msg + # territory. Intermediate deltas are DROPPED — replying each would spam + # one full-snapshot message every 0.8s. Instead: a single notice the + # first time we notice staleness, then silence until the final text, + # which always goes out (possibly in overflow chunks). + if not finished: + if not st.get("proactive_noticed"): + st["proactive_noticed"] = True + self.reply(chat_id, "⏳ 回复时间较长,处理完成后将直接发送结果…") + return + st["sent"] = True + with self._stream_lock: + self._req_ids.pop(chat_id, None) + if text: + self.reply(chat_id, text) + self._logger.info(f"[wecom] stream finished via proactive message (callback req_id stale or absent) chat={chat_id}") def _send(self, obj: dict) -> None: raw = json.dumps(obj, ensure_ascii=False) diff --git a/tests/test_channels.py b/tests/test_channels.py index d540f27..168b79b 100644 --- a/tests/test_channels.py +++ b/tests/test_channels.py @@ -178,8 +178,14 @@ def test_wecom_stream_falls_back_when_callback_req_stale(): ch, ws = _make_wecom() ch._ws = ws ch._req_ids["c1"] = ("req-old", __import__("time").time() - 600) # beyond TTL - ch.push_event("c1", "final", {"text": "late answer"}) - respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"] + # intermediate deltas: only ONE proactive notice, no full-snapshot spam + for _ in range(5): + ch.push_event("c1", "delta", {"text": "more words "}) send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"] + assert len(send) == 1 and "处理完成后" in json.dumps(send[0], ensure_ascii=False) + respond = [m for m in ws.sent if m["cmd"] == "aibot_respond_msg"] assert respond == [] # stale req_id must NOT be reused - assert send and "late answer" in json.dumps(send[-1]) + # final: exactly one proactive message with the full text + ch.push_event("c1", "final", {"text": "late answer"}) + send = [m for m in ws.sent if m["cmd"] == "aibot_send_msg"] + assert len(send) == 2 and "late answer" in json.dumps(send[-1], ensure_ascii=False)