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.

feat(backend/copilot): shrink the schemas and tool results AutoPilot reads (#14336)

Reinier van der Leer839120d6377d
diff --git a/autogpt_platform/backend/backend/copilot/tools/agent_output.py b/autogpt_platform/backend/backend/copilot/tools/agent_output.py
index 8f89c169fd..38bc400ec6 100644
--- a/autogpt_platform/backend/backend/copilot/tools/agent_output.py
+++ b/autogpt_platform/backend/backend/copilot/tools/agent_output.py
@@ -115,6 +115,8 @@ def parse_time_expression(
class AgentOutputTool(BaseTool):
"""Tool for retrieving execution outputs from user's library agents."""
+ digest_large_output = True
+
@property
def name(self) -> str:
return "view_agent_output"
diff --git a/autogpt_platform/backend/backend/copilot/tools/base.py b/autogpt_platform/backend/backend/copilot/tools/base.py
index 0eaa839469..ceb6406a77 100644
--- a/autogpt_platform/backend/backend/copilot/tools/base.py
+++ b/autogpt_platform/backend/backend/copilot/tools/base.py
@@ -2,6 +2,7 @@
import json
import logging
+from collections import deque
from typing import Any
from openai.types.chat import ChatCompletionToolParam
@@ -10,6 +11,7 @@ from backend.copilot.model import ChatSession
from backend.copilot.response_model import StreamToolOutputAvailable
from backend.data.activity_event import ActivityEventDraft
from backend.data.db_accessors import activity_event_db, workspace_db
+from backend.util.feature_flag import Flag, is_feature_enabled
from backend.util.truncate import truncate
from backend.util.workspace import WorkspaceManager
@@ -29,9 +31,18 @@ _LARGE_OUTPUT_THRESHOLD = 80_000
# to avoid double truncation/spilling. 95K + ~300 wrapper = ~95.3K, under both.
_PREVIEW_CHARS = 95_000
+# Threshold and budget for the digest, which fires when AUTOPILOT_CONTEXT_TRIMMING
+# is on and the tool opts in. The budget is derived from the trigger so a digest
+# can never be larger than the output it replaces.
+_DIGEST_THRESHOLD = 8_000
+_DIGEST_PREVIEW_CHARS = _DIGEST_THRESHOLD // 4
+_OUTLINE_SCALAR_CHARS = 120
+
# Fields whose values are binary/base64 data — truncating them produces
# garbage, so we replace them with a human-readable size summary instead.
+# Top-level only: the outline reads offsets built from the unsummarised text,
+# so a nested field added here would make the two drift.
_BINARY_FIELD_NAMES = {"content_base64"}
@@ -61,18 +72,33 @@ async def _persist_and_summarize(
user_id: str,
session_id: str,
tool_call_id: str,
+ digest: bool = False,
) -> str:
- """Persist full output to workspace and return a middle-out preview with retrieval instructions.
+ """Persist full output to workspace and return a preview with retrieval
+ instructions — a structural outline when *digest*, else a middle-out slice.
On failure, returns the original ``raw_output`` unchanged so that the
existing ``model_post_init`` middle-out truncation handles it as before.
"""
file_path = f"tool-outputs/{tool_call_id}.json"
+
+ # The outline quotes offsets into the persisted file, so the file has to be
+ # the text the index was built from, not the original serialisation.
+ body, outline = raw_output, ""
+ if digest:
+ indexed = _index_json(raw_output)
+ if indexed is not None:
+ body, _, offsets = indexed
+ # Summarised for display only: the file keeps the real bytes, so a
+ # window read still yields them.
+ summarized = json.loads(_summarize_binary_fields(body))
+ outline = _outline(summarized, offsets, _DIGEST_PREVIEW_CHARS)
+
try:
workspace = await workspace_db().get_or_create_workspace(user_id)
manager = WorkspaceManager(user_id, workspace.id, session_id)
await manager.write_file(
- content=raw_output.encode("utf-8"),
+ content=body.encode("utf-8"),
filename=f"{tool_call_id}.json",
path=file_path,
mime_type="application/json",
@@ -86,28 +112,166 @@ async def _persist_and_summarize(
)
return raw_output # fall back to normal truncation
- total = len(raw_output)
- preview = truncate(_summarize_binary_fields(raw_output), _PREVIEW_CHARS)
- retrieval = (
- f"\nFull output ({total:,} chars) saved to workspace. "
- f"Use read_workspace_file("
- f'path="{file_path}", offset=<char_offset>, length=50000) '
- f"to read any section. "
+ total = len(body)
+ sandbox = (
f"To process the file in the sandbox/working dir, use "
f"read_workspace_file("
f'path="{file_path}", save_to_path="<working_dir>/{tool_call_id}.json") '
f"first, then use bash_exec to work with the local copy."
)
- # Use workspace:// prefix so the model doesn't confuse the workspace path
- # with a local filesystem path (e.g. ~/.claude/projects/.../tool-outputs/).
+ if outline:
+ fmt, preview = ' format="outline"', outline
+ # A flat length= larger than the file makes one "narrow" read pull
+ # everything back, which is how a digest ends up costing more.
+ retrieval = (
+ f"\nThe preview above is a structural outline of the {total:,}-char "
+ f"output, not a literal prefix of it. Each `@offset+length` is that "
+ f"node's exact window in the file: "
+ f'read_workspace_file(path="{file_path}", offset=<offset>, '
+ f"length=<length>) returns it and nothing else."
+ f"\n{sandbox}"
+ )
+ else:
+ fmt, preview = "", truncate(_summarize_binary_fields(body), _PREVIEW_CHARS)
+ retrieval = (
+ f"\nFull output ({total:,} chars) saved to workspace. "
+ f"Use read_workspace_file("
+ f'path="{file_path}", offset=<char_offset>, length=50000) '
+ f"to read any section. "
+ f"{sandbox}"
+ )
return (
- f'<tool-output-truncated total_chars={total} workspace_path="{file_path}">\n'
+ f"<tool-output-truncated total_chars={total} "
+ f'workspace_path="{file_path}"{fmt}>\n'
f"{preview}\n"
f"{retrieval}\n"
f"</tool-output-truncated>"
)
+def _index_json(raw_output: str) -> tuple[str, Any, dict[str, tuple[int, int]]] | None:
+ """``(text, data, path -> (offset, length))``, or ``None`` for non-JSON.
+
+ Serialising it ourselves is what makes the offsets exact: they are recorded
+ as the text is built, so no re-parsing can drift from what was persisted.
+ """
+ try:
+ data = json.loads(raw_output)
+ except (json.JSONDecodeError, TypeError):
+ return None
+ if not isinstance(data, (dict, list)):
+ return None
+ chunks: list[str] = []
+ offsets: dict[str, tuple[int, int]] = {}
+ _write_indexed(data, "$", chunks, offsets, 0)
+ return "".join(chunks), data, offsets
+
+
+def _write_indexed(
+ node: Any,
+ path: str,
+ chunks: list[str],
+ offsets: dict[str, tuple[int, int]],
+ pos: int,
+) -> int:
+ start = pos
+ if isinstance(node, (dict, list)):
+ pairs = node.items() if isinstance(node, dict) else enumerate(node)
+ opening, closing = ("{", "}") if isinstance(node, dict) else ("[", "]")
+ chunks.append(opening)
+ pos += 1
+ for index, (key, value) in enumerate(pairs):
+ if index:
+ chunks.append(",")
+ pos += 1
+ if isinstance(node, dict):
+ label = json.dumps(key, ensure_ascii=False) + ":"
+ chunks.append(label)
+ pos += len(label)
+ child = f"{path}.{key}"
+ else:
+ child = f"{path}[{key}]"
+ pos = _write_indexed(value, child, chunks, offsets, pos)
+ chunks.append(closing)
+ pos += 1
+ else:
+ text = json.dumps(node, ensure_ascii=False)
+ chunks.append(text)
+ pos += len(text)
+ offsets[path] = (start, pos - start)
+ return pos
+
+
+def _outline(data: Any, offsets: dict[str, tuple[int, int]], budget: int) -> str:
+ """Structural outline of *data* within *budget* characters.
+
+ A middle-out slice of a schema shows one arbitrary window; an outline names
+ every property first and carries each node's window in the file, so the
+ model can both judge whether it needs more and fetch only that.
+ """
+ # Widen the per-scalar cap when the structure alone leaves most of the
+ # budget unspent, so a payload that is one long string still says something.
+ tight = _render_outline(data, offsets, budget, _OUTLINE_SCALAR_CHARS)
+ if len(tight) >= budget // 2:
+ return tight
+ wide = _render_outline(data, offsets, budget, budget // 2)
+ return max((tight, wide), key=lambda text: (text.count("\n"), len(text)))
+
+
+def _render_outline(
+ root: Any, offsets: dict[str, tuple[int, int]], budget: int, scalar_chars: int
+) -> str:
+ """Breadth-first so shallow facts outlive the budget: one line per
+ container, its scalar children inline and its container children named
+ with the window that holds them."""
+ lines: list[str] = []
+ used = 0
+ # read_workspace_file base64-encodes its slice, so a window past three
+ # quarters of the file costs more to read back than the whole output did.
+ widest = offsets.get("$", (0, 0))[1] * 3 // 4
+ queue: deque[tuple[str, Any]] = deque([("$", root)])
+ while queue:
+ path, node = queue.popleft()
+ is_map = isinstance(node, dict)
+ parts: list[str] = []
+ for key, value in node.items() if is_map else enumerate(node):
+ child = f"{path}.{key}" if is_map else f"{path}[{key}]"
+ if isinstance(value, (dict, list)):
+ braces = "{}" if isinstance(value, dict) else "[]"
+ unit = "keys" if isinstance(value, dict) else "items"
+ start, length = offsets.get(child, (0, 0))
+ window = f" @{start}+{length}" if length < widest else ""
+ parts.append(
+ f"{key}={braces[0]}…{len(value)} {unit}{window}{braces[1]}"
+ )
+ queue.append((child, value))
+ else:
+ # `message` is the tool's own instruction to the model and the
+ # 120-char cut lands mid-sentence; half the budget is as much
+ # as it can have without crowding out the field names.
+ cap = budget // 2 if key == "message" else scalar_chars
+ parts.append(f"{key}={_scalar(value, cap)}")
+ braces = "{}" if is_map else "[]"
+ line = f"{path}: {braces[0]}{', '.join(parts)}{braces[1]}"
+ if len(line) + 1 > budget - used:
+ marker = f"… {len(queue) + 1} more nodes not shown; read their windows"
+ while lines and used + len(marker) > budget:
+ used -= len(lines.pop()) + 1
+ room = budget - used - len(marker) - 1
+ if room > _OUTLINE_SCALAR_CHARS:
+ lines.append(line[: room - 1] + "…")
+ lines.append(marker)
+ break
+ lines.append(line)
+ used += len(line) + 1
+ return "\n".join(lines)
+
+
+def _scalar(value: Any, limit: int | None) -> str:
+ text = json.dumps(value, ensure_ascii=False)
+ return text if limit is None or len(text) <= limit else text[: limit - 1] + "…"
+
+
async def _record_activity(
tool: "BaseTool",
user_id: str,
@@ -137,6 +301,10 @@ async def _record_activity(
class BaseTool:
"""Base class for all chat tools."""
+ # Opt-in for the digest: an outline is only readable back in windows, so a
+ # tool whose bulk is one long text (a guide, a docs page) must not set it.
+ digest_large_output: bool = False
+
@property
def name(self) -> str:
"""Tool name for OpenAI function calling."""
@@ -233,13 +401,20 @@ class BaseTool:
await _record_activity(self, user_id, session, result, kwargs)
raw_output = result.model_dump_json(exclude_none=True)
- if (
- len(raw_output) > _LARGE_OUTPUT_THRESHOLD
- and user_id
- and session.session_id
- ):
+ # Consult the flag only once the output could plausibly be digested,
+ # so the common small-output path stays a pure local check.
+ digest = (
+ self.digest_large_output
+ and len(raw_output) > _DIGEST_THRESHOLD
+ and user_id is not None
+ and await is_feature_enabled(
+ Flag.AUTOPILOT_CONTEXT_TRIMMING, user_id, default=False
+ )
+ )
+ threshold = _DIGEST_THRESHOLD if digest else _LARGE_OUTPUT_THRESHOLD
+ if len(raw_output) > threshold and user_id and session.session_id:
raw_output = await _persist_and_summarize(
- raw_output, user_id, session.session_id, tool_call_id
+ raw_output, user_id, session.session_id, tool_call_id, digest
)
return StreamToolOutputAvailable(
diff --git a/autogpt_platform/backend/backend/copilot/tools/base_test.py b/autogpt_platform/backend/backend/copilot/tools/base_test.py
index 101481a48e..e9a9e96283 100644
--- a/autogpt_platform/backend/backend/copilot/tools/base_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/base_test.py
@@ -1,16 +1,32 @@
"""Tests for BaseTool large-output persistence in execute()."""
+import base64
+import json
+import re
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from backend.copilot.tools.base import (
+ _DIGEST_PREVIEW_CHARS,
+ _DIGEST_THRESHOLD,
_LARGE_OUTPUT_THRESHOLD,
BaseTool,
+ _index_json,
+ _outline,
_persist_and_summarize,
_summarize_binary_fields,
)
-from backend.copilot.tools.models import ResponseType, ToolResponseBase
+from backend.copilot.tools.models import (
+ BlockDetails,
+ BlockDetailsResponse,
+ ResponseType,
+ ToolResponseBase,
+)
+from backend.copilot.tools.workspace_files import (
+ ReadWorkspaceFileTool,
+ WorkspaceFileContentResponse,
+)
class _HugeOutputTool(BaseTool):
@@ -92,6 +108,20 @@ class TestPersistAndSummarize:
assert result == raw # unchanged — fallback to normal truncation
+ @pytest.mark.asyncio
+ async def test_the_excerpt_envelope_is_unchanged_from_before_the_digest(self):
+ """Flag off has to be byte-identical above 80K too, which is what the
+ "safe to merge dark" claim rests on."""
+ db_patch, mgr_patch = _workspace_patches(AsyncMock())
+ with db_patch, mgr_patch:
+ result = await _persist_and_summarize(
+ "A" * 200_000, "user-1", "session-1", "tc-123"
+ )
+
+ assert 'workspace_path="tool-outputs/tc-123.json">' in result
+ assert "format=" not in result
+ assert "to read any section. To process the file" in result
+
# ---------------------------------------------------------------------------
# BaseTool.execute — integration with persistence
@@ -192,3 +222,288 @@ class TestSummarizeBinaryFields:
data = {"message": "hello", "type": "info"}
raw = json.dumps(data)
assert _summarize_binary_fields(raw) == raw
+
+
+# ---------------------------------------------------------------------------
+# AUTOPILOT_CONTEXT_TRIMMING digest
+# ---------------------------------------------------------------------------
+
+
+class _SchemaOutputTool(_HugeOutputTool):
+ """Returns a block-details payload, which is what the digest exists for."""
+
+ digest_large_output = True
+
+ def __init__(self, output_size: int, message: str = "Block details.", **field):
+ super().__init__(output_size)
+ self._message = message
+ self._field = field
+
+ async def _execute(self, user_id, session, **kwargs) -> ToolResponseBase:
+ return BlockDetailsResponse(
+ message=self._message,
+ block=BlockDetails(
+ id="b-1",
+ name="Wide Block",
+ description="A block with many fields",
+ inputs={
+ "properties": {
+ f"field_{i}": {
+ "type": "string",
+ "description": "d" * (self._output_size // 40),
+ **self._field,
+ }
+ for i in range(40)
+ },
+ "required": ["field_0"],
+ },
+ ),
+ )
+
+
+class _BinaryOutputTool(_HugeOutputTool):
+ """Returns base64 payload: 1K of it tells the model nothing its size doesn't."""
+
+ digest_large_output = True
+
+ async def _execute(self, user_id, session, **kwargs) -> ToolResponseBase:
+ return WorkspaceFileContentResponse(
+ file_id="f-1",
+ name="shot.png",
+ path="tool-outputs/shot.png",
+ mime_type="image/png",
+ content_base64="A" * self._output_size,
+ message="Screenshot captured.",
+ )
+
+
+class _RetrievalTool(ReadWorkspaceFileTool):
+ """The real retrieval tool with only the workspace read stubbed out."""
+
+ def __init__(self, text: str) -> None:
+ self._text = text
+
+ async def _execute(self, user_id, session, **kwargs) -> ToolResponseBase:
+ return WorkspaceFileContentResponse(
+ file_id="f-1",
+ name="tc-digest.json",
+ path="tool-outputs/tc-digest.json",
+ mime_type="text/plain",
+ content_base64=base64.b64encode(self._text.encode()).decode(),
+ message=f"Read chars 0-{len(self._text)} of {len(self._text):,} total",
+ )
+
+
+def _workspace_patches(manager):
+ workspace = MagicMock()
+ workspace.id = "ws-1"
+ db = AsyncMock()
+ db.get_or_create_workspace = AsyncMock(return_value=workspace)
+ return (
+ patch("backend.copilot.tools.base.workspace_db", return_value=db),
+ patch("backend.copilot.tools.base.WorkspaceManager", return_value=manager),
+ )
+
+
+async def _execute_with_flag(tool, flag_on: bool, manager=None):
+ session = MagicMock()
+ session.session_id = "s-1"
+ db_patch, mgr_patch = _workspace_patches(manager or AsyncMock())
+ with (
+ db_patch,
+ mgr_patch,
+ patch(
+ "backend.copilot.tools.base.is_feature_enabled",
+ new_callable=AsyncMock,
+ return_value=flag_on,
+ ),
+ ):
+ return await tool.execute("user-1", session, "tc-digest")
+
+
+class TestDigestThreshold:
+ def test_the_budget_cannot_exceed_its_own_trigger(self):
+ """Between 80K and 95K the legacy preview makes the context bigger;
+ deriving the budget from the trigger makes that unrepresentable."""
+ assert _DIGEST_PREVIEW_CHARS < _DIGEST_THRESHOLD
+
+ @pytest.mark.asyncio
+ async def test_flag_off_leaves_a_mid_sized_output_byte_identical(self):
+ tool = _SchemaOutputTool(output_size=_DIGEST_THRESHOLD * 3)
+ result = await _execute_with_flag(tool, flag_on=False)
+ expected = (await tool._execute("user-1", MagicMock())).model_dump_json(
+ exclude_none=True
+ )
+ assert result.output == expected
+
+ @pytest.mark.asyncio
+ async def test_flag_on_digests_a_mid_sized_output(self):
+ manager = AsyncMock()
+ tool = _SchemaOutputTool(output_size=_DIGEST_THRESHOLD * 3)
+ result = await _execute_with_flag(tool, flag_on=True, manager=manager)
+ output = str(result.output)
+
+ assert 'format="outline"' in output
+ assert len(output) < _DIGEST_THRESHOLD
+ # Decision-grade: every property name survives, so the model can tell
+ # from the digest alone whether it needs the file.
+ assert all(f"field_{i}" in output for i in range(40))
+ # And the way back names the tool and its arguments.
+ assert 'read_workspace_file(path="tool-outputs/tc-digest.json"' in output
+ assert "offset=<offset>, length=<length>" in output
+ # Without this the model reads the outline as a prefix of the output.
+ assert "not a literal prefix" in output
+ # A flat length= wider than the file turns one "narrow" read into a
+ # full one, which is how a digest ends up costing more than it saves.
+ assert "length=50000" not in output
+ assert "@" in output
+
+ @pytest.mark.asyncio
+ async def test_flag_on_writes_the_full_text_to_the_workspace(self):
+ manager = AsyncMock()
+ tool = _SchemaOutputTool(output_size=_DIGEST_THRESHOLD * 3)
+ result = await _execute_with_flag(tool, flag_on=True, manager=manager)
+ written = manager.write_file.await_args.kwargs["content"].decode()
+ assert len(written) > _DIGEST_THRESHOLD
+ assert written == (await tool._execute("user-1", MagicMock())).model_dump_json(
+ exclude_none=True
+ )
+ assert len(str(result.output)) < len(written)
+
+ @pytest.mark.asyncio
+ async def test_a_tool_that_does_not_opt_in_is_never_digested(self):
+ """An outline is only readable back in windows, so a tool whose bulk is
+ one long text (the building guide, a docs page) must not be digested."""
+ tool = _HugeOutputTool(output_size=_DIGEST_THRESHOLD * 4)
+ assert tool.digest_large_output is False
+ result = await _execute_with_flag(tool, flag_on=True)
+ expected = (await tool._execute("user-1", MagicMock())).model_dump_json(
+ exclude_none=True
+ )
+ assert result.output == expected
+
+ @pytest.mark.asyncio
+ async def test_a_retrieved_window_is_never_digested(self):
+ """The way back must fit through the door: base64 pushes any window
+ over ~5.8K past the trigger, and digesting it returns base64, not text."""
+ text = "R" * (_DIGEST_THRESHOLD * 2)
+ result = await _execute_with_flag(_RetrievalTool(text), flag_on=True)
+ payload = json.loads(str(result.output))
+ assert base64.b64decode(payload["content_base64"]).decode() == text
+
+ @pytest.mark.asyncio
+ async def test_the_outline_summarizes_binary_fields_instead_of_quoting_them(self):
+ """1K of base64 is worth nothing to the model; its size is."""
+ tool = _BinaryOutputTool(output_size=_DIGEST_THRESHOLD * 2)
+ result = await _execute_with_flag(tool, flag_on=True)
+ output = str(result.output)
+ assert "<binary, ~12,000 bytes>" in output
+ assert "AAAAAAAA" not in output
+
+ @pytest.mark.asyncio
+ async def test_the_outline_keeps_the_message_whole(self):
+ """`message` carries the tool's own instruction to the model — on
+ run_block, that credentials must not go in input_data."""
+ message = "Block 'Wide Block' details. " + "Do not fabricate credentials. " * 8
+ tool = _SchemaOutputTool(output_size=_DIGEST_THRESHOLD * 3, message=message)
+ result = await _execute_with_flag(tool, flag_on=True)
+ assert message in str(result.output)
+
+ @pytest.mark.asyncio
+ async def test_a_long_message_cannot_crowd_out_the_field_names(self):
+ """`view_agent_output` interpolates raw node errors into its message,
+ so an unbounded one would spend the whole outline on a stack trace."""
+ tool = _SchemaOutputTool(
+ output_size=_DIGEST_THRESHOLD * 3, message="E" * (_DIGEST_THRESHOLD // 2)
+ )
+ output = str((await _execute_with_flag(tool, flag_on=True)).output)
+ # Uncapped this is 0: the message alone consumes the whole budget.
+ assert sum(f"field_{i}" in output for i in range(40)) >= 10
+
+ @pytest.mark.asyncio
+ async def test_the_persisted_text_is_the_text_the_offsets_index(self):
+ """pydantic and json.dumps disagree on float exponents, so persisting
+ the pydantic serialisation would shift every later window by a char."""
+ manager = AsyncMock()
+ tool = _SchemaOutputTool(output_size=_DIGEST_THRESHOLD * 3, default=1e-7)
+ result = await _execute_with_flag(tool, flag_on=True, manager=manager)
+ written = manager.write_file.await_args.kwargs["content"].decode()
+ window = re.search(r"required=\[…1 items @(\d+)\+(\d+)\]", str(result.output))
+ assert window, result.output
+ start, length = int(window[1]), int(window[2])
+ assert json.loads(written[start : start + length]) == ["field_0"]
+
+ @pytest.mark.asyncio
+ async def test_a_small_output_never_consults_the_flag(self):
+ tool = _SchemaOutputTool(output_size=200)
+ with patch(
+ "backend.copilot.tools.base.is_feature_enabled",
+ new_callable=AsyncMock,
+ ) as flag:
+ session = MagicMock()
+ session.session_id = "s-1"
+ await tool.execute("user-1", session, "tc-small")
+ flag.assert_not_awaited()
+
+
+def _outline_of(data, budget: int) -> tuple[str, str]:
+ body, parsed, offsets = _index_json(json.dumps(data))
+ return body, _outline(parsed, offsets, budget)
+
+
+class TestOutline:
+ def test_non_json_is_not_indexed(self):
+ assert _index_json("not json at all") is None
+ assert _index_json(json.dumps("a bare string")) is None
+
+ def test_names_every_key_before_spending_the_budget_on_values(self):
+ data = {"a": {"deep": {"x": "v" * 4_000}}, "b": 1, "c": [1, 2, 3]}
+ _, outline = _outline_of(data, 400)
+ assert "b=1" in outline
+ assert "a={…1 keys" in outline
+ assert "c=[…3 items @" in outline
+ assert len(outline) <= 400
+
+ def test_every_indexed_window_slices_out_exactly_that_node(self):
+ """The whole point of the index: one narrow read, not the whole file."""
+ data = {"block": {"inputs": {"q": {"type": "string"}}}, "n": [1, {"a": 2}]}
+ body, parsed, offsets = _index_json(json.dumps(data))
+ assert parsed == data
+ assert json.loads(body) == data
+ assert offsets["$"] == (0, len(body))
+ for path, (start, length) in offsets.items():
+ node = data
+ for step in path[1:].replace("[", ".").replace("]", "").split(".")[1:]:
+ node = node[int(step)] if isinstance(node, list) else node[step]
+ assert json.loads(body[start : start + length]) == node, path
+
+ def test_the_outline_quotes_each_node_its_real_window(self):
+ """An index nothing quotes is no index: the window in the outline has
+ to slice that node out of the file the model will read."""
+ data = {"block": {"inputs": {"q": {"type": "string", "title": "Q"}}}}
+ body, outline = _outline_of(data, 600)
+ window = re.search(r"inputs=\{…1 keys @(\d+)\+(\d+)\}", outline)
+ assert window, outline
+ start, length = int(window[1]), int(window[2])
+ assert json.loads(body[start : start + length]) == data["block"]["inputs"]
+
+ def test_no_window_costs_more_to_read_back_than_the_whole_output(self):
+ """read_workspace_file base64-encodes its slice, so a window spanning
+ most of the file is a read the model must never be offered."""
+ data = {"tag": "x", "record": {f"k{i}": {"v": "v" * 200} for i in range(10)}}
+ body, outline = _outline_of(data, 900)
+ assert "record={…10 keys}" in outline # spans the file: no window
+ widest = max(int(m) for m in re.findall(r"@\d+\+(\d+)", outline))
+ assert widest * 4 // 3 < len(body)
+
+ def test_a_single_long_string_still_says_something(self):
+ """Structure alone leaves the budget unspent, so the scalar cap widens."""
+ _, outline = _outline_of({"content": "abcdef" * 1_000}, 800)
+ assert len(outline) > 400
+ assert len(outline) <= 800
+
+ def test_says_how_much_it_left_out(self):
+ data = {f"k{i}": {"v": "x" * 200} for i in range(30)}
+ _, outline = _outline_of(data, 600)
+ assert "more nodes not shown" in outline
+ assert len(outline) <= 600
diff --git a/autogpt_platform/backend/backend/copilot/tools/continue_run_block.py b/autogpt_platform/backend/backend/copilot/tools/continue_run_block.py
index 7deb42c88c..f83c445383 100644
--- a/autogpt_platform/backend/backend/copilot/tools/continue_run_block.py
+++ b/autogpt_platform/backend/backend/copilot/tools/continue_run_block.py
@@ -25,6 +25,9 @@ logger = logging.getLogger(__name__)
class ContinueRunBlockTool(BaseTool):
"""Tool for continuing a block execution after human review approval."""
+ # Returns execute_block's result, same as run_block.
+ digest_large_output = True
+
@property
def name(self) -> str:
return "continue_run_block"
diff --git a/autogpt_platform/backend/backend/copilot/tools/run_block.py b/autogpt_platform/backend/backend/copilot/tools/run_block.py
index 54726984d2..f0b6c3c8aa 100644
--- a/autogpt_platform/backend/backend/copilot/tools/run_block.py
+++ b/autogpt_platform/backend/backend/copilot/tools/run_block.py
@@ -9,6 +9,7 @@ from backend.copilot.constants import COPILOT_NODE_EXEC_ID_SEPARATOR
from backend.copilot.context import get_current_permissions
from backend.copilot.model import ChatSession
from backend.data.activity_event import ActivityEventDraft
+from backend.util.feature_flag import Flag, is_feature_enabled
from .base import BaseTool
from .helpers import (
@@ -31,6 +32,8 @@ logger = logging.getLogger(__name__)
class RunBlockTool(BaseTool):
"""Tool for executing a block and returning its outputs."""
+ digest_large_output = True
+
@property
def name(self) -> str:
return "run_block"
@@ -242,6 +245,11 @@ class RunBlockTool(BaseTool):
llm_input_schema = _strip_credentials_from_schema(
prep.input_schema, prep.credentials_fields
)
+ if await is_feature_enabled(
+ Flag.AUTOPILOT_CONTEXT_TRIMMING, user_id, default=False
+ ):
+ llm_input_schema = _strip_presentation_annotations(llm_input_schema)
+ output_schema = _strip_presentation_annotations(output_schema)
if validate_only and not missing:
detail_msg = (
f"Block '{prep.block.name}' — all required inputs "
@@ -299,6 +307,39 @@ class RunBlockTool(BaseTool):
)
+# Builder-UI render hints: none of them can appear in a graph, and the copilot's
+# own block-details card reads only title/type/description/required.
+_PRESENTATION_ONLY_KEYS = frozenset(
+ {"advanced", "llm_model", "llm_model_metadata", "secret"}
+)
+
+# Keys whose sub-dicts are named by the *author*, so a field called "secret"
+# must survive even though the annotation of the same name must not.
+_SCHEMA_MAP_KEYS = frozenset(
+ {"$defs", "definitions", "patternProperties", "properties"}
+)
+
+
+def _strip_presentation_annotations(node: Any) -> Any:
+ """Return *node* with the builder-UI annotation keys removed, recursively."""
+ if isinstance(node, dict):
+ cleaned: dict[str, Any] = {}
+ for key, value in node.items():
+ if key in _PRESENTATION_ONLY_KEYS:
+ continue
+ if key in _SCHEMA_MAP_KEYS and isinstance(value, dict):
+ cleaned[key] = {
+ name: _strip_presentation_annotations(sub)
+ for name, sub in value.items()
+ }
+ else:
+ cleaned[key] = _strip_presentation_annotations(value)
+ return cleaned
+ if isinstance(node, list):
+ return [_strip_presentation_annotations(item) for item in node]
+ return node
+
+
def _strip_credentials_from_schema(
input_schema: dict[str, Any], credentials_fields: set[str]
) -> dict[str, Any]:
diff --git a/autogpt_platform/backend/backend/copilot/tools/test_run_block_details.py b/autogpt_platform/backend/backend/copilot/tools/test_run_block_details.py
index 8216ec45d8..d4fbc19155 100644
--- a/autogpt_platform/backend/backend/copilot/tools/test_run_block_details.py
+++ b/autogpt_platform/backend/backend/copilot/tools/test_run_block_details.py
@@ -1,5 +1,6 @@
"""Tests for BlockDetailsResponse in RunBlockTool."""
+from copy import deepcopy
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -151,3 +152,90 @@ async def test_run_block_returns_details_when_only_credentials_provided():
assert isinstance(response, BlockDetailsResponse)
assert response.block.id == "api-block-id"
assert response.block.name == "API Call"
+
+
+def make_annotated_block(block_id: str = "annotated-block-id"):
+ """A block whose schema carries builder-UI annotations, plus a property
+ literally named ``secret`` so the strip can't be a blind key filter."""
+ mock = make_mock_block_with_inputs(block_id, "Annotated Block")
+ mock.input_schema.jsonschema.return_value = {
+ "properties": {
+ "model": {
+ "type": "string",
+ "enum": ["gpt-5", "claude-opus-5"],
+ "llm_model": True,
+ "llm_model_metadata": {
+ "gpt-5": {"price_tier": "high", "creator": "OpenAI"},
+ },
+ "advanced": False,
+ "secret": False,
+ },
+ "secret": {"type": "string", "description": "A field named secret"},
+ },
+ "required": ["model"],
+ }
+ mock.output_schema.jsonschema.return_value = {
+ "properties": {"result": {"type": "string", "advanced": True}},
+ }
+ return mock
+
+
+async def _details_for(block, flag_on: bool) -> BlockDetailsResponse:
+ session = make_session(user_id=_TEST_USER_ID)
+ with (
+ patch("backend.copilot.tools.helpers.get_block", return_value=block),
+ patch(
+ "backend.copilot.tools.helpers.resolve_block_credentials",
+ new_callable=AsyncMock,
+ return_value=({}, []),
+ ),
+ patch(
+ "backend.copilot.tools.run_block.is_feature_enabled",
+ new_callable=AsyncMock,
+ return_value=flag_on,
+ ),
+ ):
+ response = await RunBlockTool()._execute(
+ user_id=_TEST_USER_ID,
+ session=session,
+ block_id=block.id,
+ input_data={},
+ dry_run=False,
+ )
+ assert isinstance(response, BlockDetailsResponse)
+ return response
+
+
[email protected](loop_scope="session")
+async def test_flag_off_leaves_the_schema_byte_identical():
+ block = make_annotated_block()
+ response = await _details_for(block, flag_on=False)
+ assert response.block.inputs == block.input_schema.jsonschema.return_value
+ assert response.block.outputs == block.output_schema.jsonschema.return_value
+
+
[email protected](loop_scope="session")
+async def test_flag_on_strips_presentation_annotations_from_both_schemas():
+ block = make_annotated_block()
+ response = await _details_for(block, flag_on=True)
+ model = response.block.inputs["properties"]["model"]
+ assert set(model) == {"type", "enum"}
+ assert model["enum"] == ["gpt-5", "claude-opus-5"]
+ assert "advanced" not in response.block.outputs["properties"]["result"]
+
+
[email protected](loop_scope="session")
+async def test_flag_on_keeps_a_property_that_shares_an_annotation_name():
+ """``secret`` is both a UI annotation and a legal field name."""
+ response = await _details_for(make_annotated_block(), flag_on=True)
+ assert "secret" in response.block.inputs["properties"]
+ assert response.block.inputs["required"] == ["model"]
+
+
[email protected](loop_scope="session")
+async def test_the_source_schema_is_not_mutated_by_the_strip():
+ """The frontend reads the same registry object through other endpoints."""
+ block = make_annotated_block()
+ original = deepcopy(block.input_schema.jsonschema.return_value)
+ await _details_for(block, flag_on=True)
+ assert block.input_schema.jsonschema.return_value == original
diff --git a/autogpt_platform/backend/backend/util/feature_flag.py b/autogpt_platform/backend/backend/util/feature_flag.py
index 8ebeeaa0b7..3120d758ce 100644
--- a/autogpt_platform/backend/backend/util/feature_flag.py
+++ b/autogpt_platform/backend/backend/util/feature_flag.py
@@ -181,6 +181,10 @@ class Flag(str, Enum):
# a cohort before it reaches everyone.
CHAT_CONNECTION_UPSELL = "chat-connection-upsell"
+ # Shrinks what AutoPilot reads: strips builder-UI annotations from the
+ # block schemas, and digests oversized tool results to the workspace.
+ AUTOPILOT_CONTEXT_TRIMMING = "autopilot-context-trimming"
+
def is_configured() -> bool:
"""Check if LaunchDarkly is configured with an SDK key."""