Skip to content

"Vibe-Trading: Your Personal Trading Agent"

Merge PR #2114: cap the initial Discord message and emit overflow once

Haozhe Wu548ba1e94fc7
diff --cc agent/src/channels/discord.py
index c12b3cbc7,ef14d755b..94d61aaa7
--- a/agent/src/channels/discord.py
+++ b/agent/src/channels/discord.py
@@@ -551,9 -551,14 +551,14 @@@ class DiscordChannel(BaseChannel)
now = time.monotonic()
if buf.message is None:
+ chunks = DiscordBotClient._build_chunks(buf.text, [], False)
+ if not chunks:
+ return
try:
- buf.message = await target.send(content=buf.text)
+ buf.message = await target.send(content=chunks[0])
buf.last_edit = now
- for extra_chunk in chunks[1:]:
- await target.send(content=extra_chunk)
++ # Overflow is sent once by _finalize_stream, after the
++ # accumulated text is complete. Sending it here duplicates it.
except Exception as e:
self.logger.warning("stream initial send failed: %s", e)
raise
diff --cc agent/tests/test_discord_stream_initial_chunking.py
index 000000000,c4a8ccfc5..3e07f93c2
mode 000000,100644..100644
--- a/agent/tests/test_discord_stream_initial_chunking.py
+++ b/agent/tests/test_discord_stream_initial_chunking.py
@@@ -1,0 -1,61 +1,78 @@@
+ """Regression: the first send of a Discord streaming reply must respect the
+ 2000-char message cap, same as the periodic edit and finalize paths below it.
+ """
+
+ from __future__ import annotations
+
+ import asyncio
+
+ import pytest
+
+ pytest.importorskip("discord")
+
+ from src.channels.discord import DiscordChannel, DiscordConfig, MAX_MESSAGE_LEN
+
+
+ class _FakeClient:
+ def is_ready(self) -> bool:
+ return True
+
+
+ class _FakeTarget:
+ def __init__(self) -> None:
+ self.sent: list[str] = []
+
++ async def edit(self, *, content: str) -> None:
++ assert len(content) <= MAX_MESSAGE_LEN
++ self.sent[0] = content
++
+ async def send(self, content: str | None = None, **kwargs) -> "_FakeTarget":
+ if content is not None and len(content) > MAX_MESSAGE_LEN:
+ raise ValueError(f"content length {len(content)} exceeds {MAX_MESSAGE_LEN}")
+ self.sent.append(content)
+ return self
+
+
+ def _make_channel() -> tuple[DiscordChannel, _FakeTarget]:
+ channel = DiscordChannel(DiscordConfig(enabled=True, token="x"), bus=None)
+ channel._client = _FakeClient()
+ target = _FakeTarget()
+
+ async def fake_resolve_channel(chat_id: str):
+ return target
+
+ channel._resolve_channel = fake_resolve_channel
+ return channel, target
+
+
+ class TestInitialStreamSendChunking:
+ def test_oversized_first_delta_is_split_across_sends(self) -> None:
+ channel, target = _make_channel()
+ oversized = "x" * (MAX_MESSAGE_LEN + 500)
+
+ asyncio.run(channel.send_delta("12345", oversized, {"_stream_id": "s1"}))
+
++ assert len(target.sent) == 1
++ asyncio.run(channel.send_delta("12345", "", {"_stream_id": "s1", "_stream_end": True}))
+ assert len(target.sent) == 2
+ assert len(target.sent[0]) == MAX_MESSAGE_LEN
+ assert len(target.sent[1]) == 500
+ assert target.sent[0] + target.sent[1] == oversized
+
+ def test_normal_sized_first_delta_is_sent_as_one_message(self) -> None:
+ channel, target = _make_channel()
+
+ asyncio.run(channel.send_delta("12345", "hello world", {"_stream_id": "s1"}))
+
+ assert target.sent == ["hello world"]
++
++
++ def test_later_deltas_and_repeated_end_do_not_duplicate_overflow(self):
++ channel, target = _make_channel()
++ text = "x" * (MAX_MESSAGE_LEN + 500)
++ asyncio.run(channel.send_delta("12345", text, {"_stream_id": "s1"}))
++ asyncio.run(channel.send_delta("12345", "tail", {"_stream_id": "s1"}))
++ for _ in range(2):
++ asyncio.run(channel.send_delta("12345", "", {"_stream_id": "s1", "_stream_end": True}))
++ assert "".join(target.sent) == text + "tail"
++ assert len(target.sent) == 2