Skip to content

The agent that grows with you

[verified] fix(gateway): preserve complete resolved reply context

Glucksberg6efca62c20e9
diff --git a/gateway/run_inbound.py b/gateway/run_inbound.py
index 989ccf794e6..b1e223a632b 100644
--- a/gateway/run_inbound.py
+++ b/gateway/run_inbound.py
@@ -1497,9 +1497,11 @@ class GatewayInboundMixin:
if getattr(event, "reply_to_text", None) and event.reply_to_message_id:
# Always inject the reply-to pointer even when the quoted text is already in history:
# it's disambiguation (*which* prior message), not deduplication.
- reply_snippet = event.reply_to_text[:500]
+ # Adapters resolve the original message (or the user's native partial quote).
+ # A preview here silently loses later list items and code; keep that context intact.
+ reply_text = event.reply_to_text
_who = " your previous message" if getattr(event, "reply_to_is_own_message", False) else ""
- message_text = f'[Replying to{_who}: "{reply_snippet}"]\n\n{message_text}'
+ message_text = f'[Replying to{_who}: "{reply_text}"]\n\n{message_text}'
return message_text
async def _inbound_model_context_length(self, source: SessionSource, session_key: str) -> int:
diff --git a/tests/gateway/test_reply_to_injection.py b/tests/gateway/test_reply_to_injection.py
index d92e6a53e01..b536c8a24dd 100644
--- a/tests/gateway/test_reply_to_injection.py
+++ b/tests/gateway/test_reply_to_injection.py
@@ -60,6 +60,32 @@ async def test_reply_prefix_injected_when_text_absent_from_history():
assert result.endswith("What's the best time to go?")
+async def test_telegram_long_reply_reaches_prompt_without_losing_later_items():
+ """The native reply already has the full message; preparation must not trim it."""
+ from gateway.platforms.base import MessageType
+ from tests.gateway.test_telegram_reply_quote import _make_adapter, _make_message
+
+ quoted = "\n".join(
+ f"{index}. {company}: " + "Evidence from the supplied list. " * 12
+ for index, company in enumerate(
+ ["GoCar", "Urban Drive", "DubCar", "GRPS", "Halucar"], 1
+ )
+ )
+ event = _make_adapter()._build_message_event(
+ _make_message(text="Review all five companies.", reply_to_text=quoted),
+ MessageType.TEXT,
+ )
+ history = [{"role": "user", "content": "Previous request"}]
+ result = await _make_runner()._prepare_inbound_message_text(
+ event=event, source=event.source, history=history,
+ )
+ assert result is not None
+ assert quoted in result
+ assert result.endswith("Review all five companies.")
+ assert history == [{"role": "user", "content": "Previous request"}]
+
+
@pytest.mark.asyncio
async def test_reply_prefix_still_injected_when_text_in_history():
"""Regression test: the pointer must survive even when the quoted text