Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): keep feishu tool hints on the active streaming card (#1957)

Lanre Shittu118f06a1e52f
diff --git a/agent/src/channels/feishu.py b/agent/src/channels/feishu.py
index bbc86be0d..8d641af10 100644
--- a/agent/src/channels/feishu.py
+++ b/agent/src/channels/feishu.py
@@ -2054,9 +2054,13 @@ class FeishuChannel(BaseChannel):
if buf and buf.card_id:
# Delegate to send_delta so tool hints get the same
# throttling (and card creation) as regular text deltas.
+ # Metadata must be forwarded too, otherwise send_delta
+ # resolves a different stream key than the one just
+ # checked above and writes to an orphaned buffer/card.
await self.send_delta(
msg.chat_id,
"\n\n" + self._format_tool_hint_delta(hint) + "\n\n",
+ msg.metadata,
)
return
# No active streaming card — send as a regular interactive card
diff --git a/agent/tests/test_feishu_tool_hint_stream_key.py b/agent/tests/test_feishu_tool_hint_stream_key.py
new file mode 100644
index 000000000..3dcd32a7c
--- /dev/null
+++ b/agent/tests/test_feishu_tool_hint_stream_key.py
@@ -0,0 +1,70 @@
+"""Regression: a tool hint delegated to send_delta must land on the same
+streaming card the caller just confirmed is active, not an orphaned one.
+"""
+
+from __future__ import annotations
+
+import asyncio
+
+from src.channels.bus.events import OutboundMessage
+from src.channels.feishu import FeishuChannel, _FeishuStreamBuf
+
+
+def _make_channel() -> FeishuChannel:
+ channel = FeishuChannel({}, bus=None)
+ channel._client = object()
+ channel._create_streaming_card_sync = lambda *args, **kwargs: "card_new"
+ channel._stream_update_text_with_reopen_sync = lambda card_id, text, seq: (True, seq)
+ return channel
+
+
+class TestToolHintStreamKeyContinuity:
+ def test_tool_hint_appends_to_the_active_card_instead_of_a_new_one(self) -> None:
+ channel = _make_channel()
+ chat_id = "oc_testchat"
+ message_id = "om_original_msg"
+ stream_key = FeishuChannel._stream_key(chat_id, {"message_id": message_id})
+ channel._stream_bufs[stream_key] = _FeishuStreamBuf(
+ text="Hello so far", card_id="card_main", sequence=3
+ )
+
+ asyncio.run(
+ channel.send(
+ OutboundMessage(
+ channel="feishu", chat_id=chat_id, content="Using calculator...",
+ metadata={"_tool_hint": True, "_progress": True, "message_id": message_id},
+ )
+ )
+ )
+
+ assert list(channel._stream_bufs.keys()) == [stream_key]
+ buf = channel._stream_bufs[stream_key]
+ assert buf.card_id == "card_main"
+ assert "Hello so far" in buf.text
+ assert "Using calculator..." in buf.text
+
+ def test_finalizing_the_stream_leaves_no_stray_buffer(self) -> None:
+ channel = _make_channel()
+ chat_id = "oc_testchat"
+ message_id = "om_original_msg"
+ stream_key = FeishuChannel._stream_key(chat_id, {"message_id": message_id})
+ channel._stream_bufs[stream_key] = _FeishuStreamBuf(
+ text="Hello so far", card_id="card_main", sequence=3
+ )
+ channel._stream_update_text_with_reopen_sync = lambda card_id, text, seq: (True, seq)
+ channel._close_streaming_mode_sync = lambda card_id, seq: True
+
+ async def run() -> None:
+ await channel.send(
+ OutboundMessage(
+ channel="feishu", chat_id=chat_id, content="Using calculator...",
+ metadata={"_tool_hint": True, "_progress": True, "message_id": message_id},
+ )
+ )
+ await channel.send_delta(
+ chat_id, "", {"_stream_end": True, "message_id": message_id}
+ )
+
+ asyncio.run(run())
+
+ assert channel._stream_bufs == {}