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): enforce string truncation size limits (#14363)

QiuLG68b118f53a2e
diff --git a/autogpt_platform/backend/backend/copilot/tools/expert_chats_test.py b/autogpt_platform/backend/backend/copilot/tools/expert_chats_test.py
index 69286f6bcd..045eaeef7e 100644
--- a/autogpt_platform/backend/backend/copilot/tools/expert_chats_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/expert_chats_test.py
@@ -343,8 +343,10 @@ class TestReadRendering:
class TestReadPaging:
+ # Each row is over the row cap, so it truncates to exactly _MAX_MESSAGE_CHARS
+ # and _MAX_PAGE_CHARS // _MAX_MESSAGE_CHARS = 4 of the 6 fit on a page.
def _long_chat(self) -> _FakeChatDB:
- return _FakeChatDB(messages=[_msg(i, content="x" * 2_500) for i in range(1, 6)])
+ return _FakeChatDB(messages=[_msg(i, content="x" * 2_500) for i in range(1, 7)])
@pytest.mark.asyncio
async def test_the_cap_drops_the_oldest_rows_and_reports_them_as_more(
@@ -352,7 +354,7 @@ class TestReadPaging:
) -> None:
result = await _read(self._long_chat())
assert sum(len(m.content) for m in result.messages) <= _MAX_PAGE_CHARS
- assert [m.sequence for m in result.messages] == [3, 4, 5]
+ assert [m.sequence for m in result.messages] == [3, 4, 5, 6]
assert result.has_more is True
assert result.next_before_sequence == 3
diff --git a/autogpt_platform/backend/backend/util/truncate.py b/autogpt_platform/backend/backend/util/truncate.py
index 5d28868d37..e6ec3710f5 100644
--- a/autogpt_platform/backend/backend/util/truncate.py
+++ b/autogpt_platform/backend/backend/util/truncate.py
@@ -12,10 +12,29 @@ def _truncate_string_middle(value: str, limit: int) -> str:
if len(value) <= limit:
return value
- head_len = max(1, limit // 2)
- tail_len = limit - head_len # ensures total == limit
- omitted = len(value) - (head_len + tail_len)
- return f"{value[:head_len]}… (omitted {omitted} chars)…{value[-tail_len:]}"
+ if limit == 0:
+ return ""
+
+ omitted = len(value)
+ marker = f"… (omitted {omitted} chars)…"
+ while len(marker) <= limit:
+ retained = limit - len(marker)
+ if retained < 2:
+ break
+ actual_omitted = len(value) - retained
+ if actual_omitted == omitted:
+ head_len = (retained + 1) // 2
+ tail_len = retained - head_len
+ tail = value[-tail_len:] if tail_len else ""
+ return f"{value[:head_len]}{marker}{tail}"
+ omitted = actual_omitted
+ marker = f"… (omitted {omitted} chars)…"
+
+ retained = limit - 1
+ head_len = (retained + 1) // 2
+ tail_len = retained - head_len
+ tail = value[-tail_len:] if tail_len else ""
+ return f"{value[:head_len]}…{tail}"
# ---------------------------------------------------------------------------
@@ -79,6 +98,9 @@ def truncate(value: Any, size_limit: int) -> Any:
largest str_limit and list_limit that fit.
"""
+ if size_limit < 0:
+ raise ValueError("size_limit must be non-negative")
+
# Fast path: plain strings don't need the binary search machinery.
if isinstance(value, str):
return _truncate_string_middle(value, size_limit)
diff --git a/autogpt_platform/backend/backend/util/truncate_test.py b/autogpt_platform/backend/backend/util/truncate_test.py
new file mode 100644
index 0000000000..aaa05b49ed
--- /dev/null
+++ b/autogpt_platform/backend/backend/util/truncate_test.py
@@ -0,0 +1,43 @@
+import pytest
+
+from backend.util.truncate import truncate
+
+
+def test_truncate_string_respects_size_limit():
+ value = "0123456789" * 10
+
+ result = truncate(value, 30)
+
+ assert len(result) <= 30
+ assert result.startswith(value[0])
+ assert result.endswith(value[-1])
+ assert "omitted 91 chars" in result
+
+
[email protected]("size_limit", [0, 1, 5, 20])
+def test_truncate_string_respects_short_size_limits(size_limit: int):
+ result = truncate("0123456789" * 10, size_limit)
+
+ assert len(result) <= size_limit
+
+
+def test_truncate_string_returns_value_within_limit_unchanged():
+ value = "short value"
+
+ assert truncate(value, len(value)) == value
+
+
+def test_truncate_string_preserves_ends_when_full_marker_has_no_context():
+ value = "0123456789" * 10
+
+ result = truncate(value, 22)
+
+ assert len(result) == 22
+ assert result.startswith(value[0])
+ assert result.endswith(value[-1])
+
+
[email protected]("value", ["value", {"key": "value"}, 42])
+def test_truncate_rejects_negative_size_limit(value: object):
+ with pytest.raises(ValueError, match="size_limit must be non-negative"):
+ truncate(value, -1)