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): a sub-session hands back a bounded result, not its whole tool log (#14309)

Reinier van der Leerb10910d2de27
diff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.py
index 11aedfec78..3812b8f6ab 100644
--- a/autogpt_platform/backend/backend/copilot/tools/models.py
+++ b/autogpt_platform/backend/backend/copilot/tools/models.py
@@ -437,9 +437,13 @@ class SubSessionStatusResponse(ToolResponseBase):
"``delegate_to_expert`` runs; None for same-scope sub-AutoPilots."
),
)
- tool_calls: list[dict[str, Any]] | None = Field(
+ sub_tool_call_count: int | None = Field(
default=None,
- description="Tool calls made during the sub-AutoPilot run.",
+ description=(
+ "How many tool calls the sub made. The calls themselves are not "
+ "returned; read the sub's transcript at "
+ "``sub_autopilot_session_link``."
+ ),
)
sub_workspace_files: list[WorkspaceFileInfoData] | None = Field(
default=None,
diff --git a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
index e517534930..85902cbb58 100644
--- a/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
+++ b/autogpt_platform/backend/backend/copilot/tools/run_sub_session.py
@@ -98,7 +98,10 @@ class RunSubSessionTool(BaseTool):
},
"sub_autopilot_session_id": {
"type": "string",
- "description": ("Continue/queue-into a prior sub; empty = new."),
+ "description": (
+ "Continue or queue into a prior sub; empty = new. "
+ "Cheaper when the follow-up builds on what it read."
+ ),
"default": "",
},
"wait_for_result": {
@@ -405,11 +408,10 @@ def response_from_outcome(
message is built correctly once instead of via a post-hoc string
substitution against this function's own wording.
- ``completed`` surfaces the aggregated response text + tool calls, plus a
- manifest of any workspace files the sub wrote (SECRT-2377). Pass
- ``workspace_files`` to supply the authoritative listing from the sub's
- session; when omitted, the files are mined from ``result.tool_calls`` as a
- fallback.
+ ``completed`` surfaces the aggregated response text, plus a manifest of
+ any workspace files the sub wrote (SECRT-2377). Pass ``workspace_files``
+ to supply the authoritative listing from the sub's session; when omitted,
+ the files are mined from ``result.tool_calls`` as a fallback.
``failed`` returns the error marker with the same handles.
``running`` returns just the polling handles so the agent can resume.
``queued`` means the target session already had a turn in flight; the
@@ -497,7 +499,7 @@ def response_from_outcome(
sub_autopilot_session_id=inner_session_id,
sub_autopilot_session_link=link,
response=result.response_text,
- tool_calls=[tc.model_dump() for tc in result.tool_calls],
+ sub_tool_call_count=len(result.tool_calls),
sub_workspace_files=workspace_files or None,
elapsed_seconds=round(elapsed, 2),
)
diff --git a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
index 7b1be09425..b85db893da 100644
--- a/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/sub_session_test.py
@@ -419,8 +419,8 @@ class TestRunSubSession:
self, mock_queue, mock_waiter, mock_model
):
"""When the queue primitive returns 'completed' + a SessionResult,
- the tool surfaces response_text + tool_calls directly — no DB
- round-trip needed for the content."""
+ the tool surfaces response_text directly — no DB round-trip
+ needed for the content."""
res = SessionResult()
res.response_text = "the answer"
@@ -444,10 +444,64 @@ class TestRunSubSession:
assert isinstance(r, SubSessionStatusResponse)
assert r.status == "completed"
assert r.response == "the answer"
- assert r.tool_calls is not None and len(r.tool_calls) == 1
- assert r.tool_calls[0]["tool_name"] == "foo"
+ assert r.sub_tool_call_count == 1
mock_waiter.assert_awaited_once()
+ @pytest.mark.asyncio
+ async def test_a_large_sub_tool_output_does_not_come_back(
+ self, mock_queue, mock_waiter, mock_model
+ ):
+ """``BaseTool.execute`` serialises this whole model into the parent's
+ context, so the hand-back's serialised size is the thing to pin."""
+ res = SessionResult()
+ res.response_text = "read the schema, wrote the graph"
+ res.tool_calls = [
+ ToolCallEntry(
+ tool_call_id="tc-1",
+ tool_name="get_block_schema",
+ input={"block_id": "b-1"},
+ output="SCHEMA_BODY_" + ("x" * 200_000),
+ success=True,
+ )
+ ]
+ mock_waiter.return_value = ("completed", res)
+
+ r = await RunSubSessionTool()._execute(
+ user_id="alice",
+ session=_session("alice"),
+ prompt="build it",
+ wait_for_result=60,
+ )
+ assert isinstance(r, SubSessionStatusResponse)
+ assert r.status == "completed"
+ serialised = r.model_dump_json(exclude_none=True)
+ assert "SCHEMA_BODY_" not in serialised
+ assert len(serialised) < 2_000, f"hand-back grew to {len(serialised)} chars"
+ assert r.sub_tool_call_count == 1
+
+ @pytest.mark.asyncio
+ async def test_a_completed_run_with_no_tool_calls_reports_zero(
+ self, mock_queue, mock_waiter, mock_model
+ ):
+ """``BaseTool.execute`` serialises with ``exclude_none=True``, so a
+ None count would drop the field and read as "not applicable" rather
+ than "the sub used no tools"."""
+ res = SessionResult()
+ res.response_text = "answered from what I already knew"
+ res.tool_calls = []
+ mock_waiter.return_value = ("completed", res)
+
+ r = await RunSubSessionTool()._execute(
+ user_id="alice",
+ session=_session("alice"),
+ prompt="what is 2+2",
+ wait_for_result=60,
+ )
+ assert isinstance(r, SubSessionStatusResponse)
+ assert r.status == "completed"
+ assert r.sub_tool_call_count == 0
+ assert "sub_tool_call_count" in r.model_dump_json(exclude_none=True)
+
@pytest.mark.asyncio
async def test_completed_surfaces_workspace_file_manifest(
self, mock_queue, mock_waiter, mock_model, stub_workspace_listing