Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

fix(backend/copilot-bot): clamp oversized bot prompts to the request cap (#13577)

Bently28178f2ff44a
diff --git a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
index 74fef46c27..da162815eb 100644
--- a/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
+++ b/autogpt_platform/backend/backend/copilot/bot/bot_backend.py
@@ -24,6 +24,7 @@ from backend.copilot.response_model import (
StreamToolOutputAvailable,
)
from backend.platform_linking.models import (
+ MAX_BOT_MESSAGE_CHARS,
BotChatRequest,
BotEventInput,
BotGuildInput,
@@ -44,6 +45,7 @@ from backend.util.exceptions import (
)
from .adapters.base import InboundAttachment
+from .prompt import clamp_prompt
# How long to wait for a single chunk from the copilot stream before giving
# up. Covers the case where the backend crashes mid-stream and never sends
@@ -436,7 +438,10 @@ class BotBackend:
request=BotChatRequest(
platform=Platform(platform.upper()),
platform_user_id=platform_user_id,
- message=message,
+ # A long conversation's history can push the assembled prompt
+ # past the request cap; clamp here so it never fails validation
+ # (which the turn streamer would surface as a generic error).
+ message=clamp_prompt(message, MAX_BOT_MESSAGE_CHARS),
session_id=session_id,
platform_server_id=platform_server_id,
file_ids=file_ids or [],
diff --git a/autogpt_platform/backend/backend/copilot/bot/prompt.py b/autogpt_platform/backend/backend/copilot/bot/prompt.py
index 172ed68125..72337a590a 100644
--- a/autogpt_platform/backend/backend/copilot/bot/prompt.py
+++ b/autogpt_platform/backend/backend/copilot/bot/prompt.py
@@ -9,6 +9,27 @@ from .adapters.base import MessageContext, MessageHistoryEntry
THREAD_NAME_PREFIX = "AutoGPT: "
+# Prepended when a prompt is clamped so the model knows context was elided.
+_TRUNCATION_NOTICE = "[Earlier context was truncated to fit the size limit.]\n\n"
+
+
+def clamp_prompt(message: str, max_chars: int) -> str:
+ """Clamp an assembled bot prompt to a hard character cap.
+
+ The current message sits at the *end* of the assembled prompt (after any
+ referenced-conversation content and thread history — see
+ ``build_message_text``), so we keep the tail and drop the older leading
+ context, marking the cut so the model doesn't treat the elided text as
+ missing. A no-op when the prompt already fits.
+ """
+ if len(message) <= max_chars:
+ return message
+ keep = max_chars - len(_TRUNCATION_NOTICE)
+ if keep <= 0:
+ # Pathologically tiny cap — can't even fit the notice; hard-cut.
+ return message[-max_chars:]
+ return _TRUNCATION_NOTICE + message[-keep:]
+
def build_message_text(
ctx: MessageContext,
diff --git a/autogpt_platform/backend/backend/copilot/bot/prompt_test.py b/autogpt_platform/backend/backend/copilot/bot/prompt_test.py
new file mode 100644
index 0000000000..d600d4db91
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/bot/prompt_test.py
@@ -0,0 +1,51 @@
+"""Tests for prompt assembly helpers."""
+
+from backend.platform_linking.models import (
+ MAX_BOT_MESSAGE_CHARS,
+ BotChatRequest,
+ Platform,
+)
+
+from .prompt import _TRUNCATION_NOTICE, clamp_prompt
+
+
+def test_clamp_prompt_leaves_a_fitting_prompt_untouched():
+ message = "a" * (MAX_BOT_MESSAGE_CHARS - 1)
+ assert clamp_prompt(message, MAX_BOT_MESSAGE_CHARS) == message
+
+
+def test_clamp_prompt_leaves_an_exactly_capped_prompt_untouched():
+ message = "a" * MAX_BOT_MESSAGE_CHARS
+ assert clamp_prompt(message, MAX_BOT_MESSAGE_CHARS) == message
+
+
+def test_clamp_prompt_keeps_the_tail_and_marks_the_cut():
+ # The current message sits at the END of the assembled prompt, so the tail
+ # (not the head) must survive.
+ body = "OLD-CONTEXT" * 5000 + "THE ACTUAL QUESTION"
+ clamped = clamp_prompt(body, MAX_BOT_MESSAGE_CHARS)
+ assert len(clamped) == MAX_BOT_MESSAGE_CHARS
+ assert clamped.startswith(_TRUNCATION_NOTICE)
+ assert clamped.endswith("THE ACTUAL QUESTION")
+ assert "OLD-CONTEXT" * 5000 not in clamped # older context was dropped
+
+
+def test_clamp_prompt_output_always_satisfies_bot_chat_request():
+ # Regression: a long conversation's assembled prompt used to exceed the
+ # request cap and raise a pydantic ValidationError mid-turn (surfaced to
+ # the user as a generic "Something went wrong").
+ oversized = "x" * (MAX_BOT_MESSAGE_CHARS * 3)
+ request = BotChatRequest(
+ platform=Platform.DISCORD,
+ platform_user_id="u-1",
+ message=clamp_prompt(oversized, MAX_BOT_MESSAGE_CHARS),
+ )
+ assert len(request.message) <= MAX_BOT_MESSAGE_CHARS
+
+
+def test_clamp_prompt_hard_cuts_when_cap_below_notice_width():
+ # Pathologically tiny cap can't fit the notice — still must respect it.
+ tiny = len(_TRUNCATION_NOTICE) - 5
+ clamped = clamp_prompt("z" * 100, tiny)
+ assert len(clamped) == tiny
+ assert clamped == "z" * tiny
diff --git a/autogpt_platform/backend/backend/platform_linking/models.py b/autogpt_platform/backend/backend/platform_linking/models.py
index 521aa67046..55ba224c1e 100644
--- a/autogpt_platform/backend/backend/platform_linking/models.py
+++ b/autogpt_platform/backend/backend/platform_linking/models.py
@@ -6,6 +6,12 @@ from typing import Literal
from pydantic import BaseModel, Field
+# Hard cap on the assembled bot prompt. The chat bots build a prompt from the
+# user's message plus channel history / referenced conversations, which on a
+# very long conversation can blow past this — callers must clamp to it before
+# constructing a BotChatRequest (see bot.prompt.clamp_prompt).
+MAX_BOT_MESSAGE_CHARS = 32000
+
class Platform(str, Enum):
"""Mirror of the Prisma PlatformType enum."""
@@ -106,7 +112,9 @@ class BotChatRequest(BaseModel):
max_length=255,
)
message: str = Field(
- description="The user's message", min_length=1, max_length=32000
+ description="The user's message",
+ min_length=1,
+ max_length=MAX_BOT_MESSAGE_CHARS,
)
session_id: str | None = Field(
default=None,