Skip to content

Your lightweight open-source personal agent.

fix(telegram): hard-cut when fence body cut lands on the prefix

santhreal7e9426d9bd7e
diff --git a/nanobot/channels/telegram/runtime.py b/nanobot/channels/telegram/runtime.py
index 39c28619b..3a8020740 100644
--- a/nanobot/channels/telegram/runtime.py
+++ b/nanobot/channels/telegram/runtime.py
@@ -102,7 +102,7 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
adjusted = recut.rfind("\n", min_code_pos)
if adjusted < min_code_pos:
adjusted = recut.rfind(" ", min_code_pos)
- pos = adjusted if adjusted >= min_code_pos else budget
+ pos = adjusted if adjusted > min_code_pos else budget
elif pos + len(closing) > max_len:
budget = max_len - len(closing)
if budget <= min_code_pos:
@@ -113,7 +113,11 @@ def _split_telegram_markdown(content: str, max_len: int) -> list[str]:
adjusted = recut.rfind("\n", min_code_pos)
if adjusted < min_code_pos:
adjusted = recut.rfind(" ", min_code_pos)
- pos = adjusted if adjusted >= min_code_pos else budget
+ pos = adjusted if adjusted > min_code_pos else budget
+ if pos <= min_code_pos:
+ chunks.append(content[:max_len])
+ content = content[max_len:].lstrip()
+ continue
chunks.append(content[:pos] + closing)
remainder = content[pos:]
if remainder.startswith("\n"):
diff --git a/nanobot/channels/telegram/tests/test_telegram_channel.py b/nanobot/channels/telegram/tests/test_telegram_channel.py
index 5a37c5823..ff62713a4 100644
--- a/nanobot/channels/telegram/tests/test_telegram_channel.py
+++ b/nanobot/channels/telegram/tests/test_telegram_channel.py
@@ -293,6 +293,19 @@ def test_split_telegram_markdown_tiny_limit_with_early_body_newline() -> None:
assert plain.count("a") >= 100
+def test_split_telegram_markdown_leading_space_in_fence_body() -> None:
+ body = "a" * 4500
+ content = f"```\n {body}"
+
+ chunks = _split_telegram_markdown(content, TELEGRAM_MAX_MESSAGE_LEN)
+
+ assert chunks
+ assert all(len(chunk) <= TELEGRAM_MAX_MESSAGE_LEN for chunk in chunks)
+ plain = "".join(chunks).replace("```", "").replace("\n", "")
+ assert plain.count("a") == 4500
+
+
+
@pytest.mark.asyncio
async def test_start_creates_separate_pools_with_proxy(monkeypatch) -> None: