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.

refactor(copilot): stop declaring tools an automation session cannot call (#14629)

Reinier van der Leerd028684ccea8
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index 9c9955d6e0..4a6d78b209 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -130,6 +130,7 @@ from backend.copilot.tools import (
expert_tool_disabled_groups,
get_available_tools,
kickoff_turn_disabled_tools,
+ origin_disabled_tools,
)
from backend.copilot.tools.e2b_sandbox import (
count_expert_turn,
@@ -2273,10 +2274,11 @@ async def stream_chat_completion_baseline(
)
# A hire's kickoff turn is a server-sent control message, so nothing on
# it was asked for: narrow it to the onboarding card and nothing else.
+ # Otherwise hide what the origin gate would refuse anyway.
disabled_tools = (
kickoff_turn_disabled_tools()
if is_expert_kickoff_turn(session)
- else frozenset()
+ else origin_disabled_tools(session.metadata.origin)
)
tools = get_available_tools(
disabled_groups=disabled_tool_groups, disabled_tools=disabled_tools
diff --git a/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py b/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
index f741ef77b5..1af1e62b85 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
@@ -32,7 +32,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from claude_agent_sdk import ResultMessage
-from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.model import ChatMessage, ChatSession, ChatSessionMetadata
from backend.copilot.response_model import StreamStart
_SVC = "backend.copilot.sdk.service"
@@ -60,6 +60,10 @@ def _make_session() -> ChatSession:
started_at=datetime.now(UTC),
updated_at=datetime.now(UTC),
messages=[ChatMessage(role="user", content="hello")],
+ # Stated, not defaulted: the staffing tools this file asserts on are
+ # also hidden off an interactive origin, so a session that left it
+ # unset would pass the flag-off case for the wrong reason.
+ metadata=ChatSessionMetadata(origin="interactive"),
)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 8226574f29..4e8180c3e7 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -173,6 +173,7 @@ from ..tools import (
ToolGroup,
expert_tool_disabled_groups,
kickoff_turn_disabled_tools,
+ origin_disabled_tools,
tool_names_in_groups,
)
from ..tools.e2b_sandbox import get_or_create_sandbox, pause_sandbox_direct
@@ -4958,11 +4959,14 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
if is_expert_kickoff_turn(session)
else frozenset()
)
+ # A machine-authored session cannot staff the team, so it is not
+ # offered the proposal tools its own guard would refuse.
hidden_tools = (
_hidden_short_names_for_permissions(permissions)
| tool_names_in_groups(disabled_tool_groups)
| {"get_agent_building_guide"}
| kickoff_hidden
+ | origin_disabled_tools(session.metadata.origin)
)
mcp_server = create_copilot_mcp_server(
use_e2b=use_e2b,
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
index ad5da9395a..fcdd3f7b8c 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
@@ -878,7 +878,14 @@ def create_copilot_mcp_server(
# excluded from ``allowed_tools`` — advertising an MCP copy the CLI
# can never approve makes the model call it, receive a permission
# denial, and silently abandon the feature (e.g. the task checklist).
- if tool_name in hidden or tool_name in BASELINE_ONLY_MCP_TOOLS:
+ # ``is_available`` is the env check the baseline path applies in
+ # ``get_available_tools``; without it this engine offers browser
+ # tools on a box with no agent-browser binary.
+ if (
+ tool_name in hidden
+ or tool_name in BASELINE_ONLY_MCP_TOOLS
+ or not base_tool.is_available
+ ):
continue
handler = create_tool_handler(base_tool)
schema = _build_input_schema(base_tool)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
index 298d774f9d..62f4118008 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
@@ -3,7 +3,7 @@
import asyncio
import json
from typing import Any
-from unittest.mock import AsyncMock, MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from mcp.types import ListToolsRequest, ToolAnnotations
@@ -1264,15 +1264,13 @@ class TestCreateCopilotMcpServerHidden:
registered = await self._registered_tool_names(server)
assert hidden_name not in registered
# Other tools still register.
- assert len(registered) >= len(TOOL_REGISTRY) - 1
+ assert self._expected_registry_names() - {hidden_name} <= registered
@pytest.mark.asyncio
- async def test_no_hidden_tools_registers_all(self):
+ async def test_no_hidden_tools_registers_every_available_tool(self):
server = create_copilot_mcp_server()
registered = await self._registered_tool_names(server)
- for short in TOOL_REGISTRY:
- if short in BASELINE_ONLY_MCP_TOOLS:
- continue
+ for short in self._expected_registry_names():
assert short in registered
@pytest.mark.asyncio
@@ -1308,11 +1306,80 @@ class TestCreateCopilotMcpServerHidden:
)
registered = await self._registered_tool_names(server)
# All real tools still register.
- for short in TOOL_REGISTRY:
- if short in BASELINE_ONLY_MCP_TOOLS:
- continue
+ for short in self._expected_registry_names():
assert short in registered
+ @pytest.mark.asyncio
+ async def test_automation_origin_tools_not_registered(self):
+ """The origin gate reaches the MCP server, not just the schema list.
+
+ ``origin_disabled_tools`` is what both engines feed in; on this one
+ hiding IS the enforcement, since an unregistered tool does not exist
+ for the CLI. A legacy ``origin=None`` counts as automation.
+ """
+ from backend.copilot.tools import (
+ INTERACTIVE_ORIGIN_TOOLS,
+ origin_disabled_tools,
+ )
+
+ for origin in ("automation", None):
+ server = create_copilot_mcp_server(
+ hidden_tool_names=origin_disabled_tools(origin)
+ )
+ registered = await self._registered_tool_names(server)
+ assert not (INTERACTIVE_ORIGIN_TOOLS & registered), (
+ f"origin={origin!r} registered "
+ f"{sorted(INTERACTIVE_ORIGIN_TOOLS & registered)}"
+ )
+ # Narrow by design: the work an automation exists to do stays.
+ assert {"run_agent", "run_block", "run_sub_session"} <= registered
+
+ interactive = await self._registered_tool_names(
+ create_copilot_mcp_server(
+ hidden_tool_names=origin_disabled_tools("interactive")
+ )
+ )
+ assert INTERACTIVE_ORIGIN_TOOLS <= interactive, (
+ "an interactive session lost "
+ f"{sorted(INTERACTIVE_ORIGIN_TOOLS - interactive)}"
+ )
+
+ @pytest.mark.asyncio
+ async def test_env_unavailable_tools_not_registered(self):
+ """``is_available`` is honoured here as it is on the baseline path.
+
+ Without it the model is offered browser tools on a box with no
+ ``agent-browser`` binary, and they fail on first use.
+ """
+ browser_tools = {"browser_navigate", "browser_act", "browser_screenshot"}
+
+ with patch(
+ "backend.copilot.tools.agent_browser.shutil.which", return_value="/x"
+ ):
+ registered = await self._registered_tool_names(create_copilot_mcp_server())
+ assert browser_tools <= registered
+
+ with patch(
+ "backend.copilot.tools.agent_browser.shutil.which", return_value=None
+ ):
+ registered = await self._registered_tool_names(create_copilot_mcp_server())
+ assert not (browser_tools & registered)
+
+ @staticmethod
+ def _expected_registry_names() -> set[str]:
+ """Registry tools the SDK server should register in this environment.
+
+ ``is_available`` is read here rather than asserted over the whole
+ registry: the chat-platform, browser and E2B tools depend on env the
+ test box may not have, and registering one the environment cannot
+ serve is the bug, not the invariant.
+ """
+ return {
+ name
+ for name, tool in TOOL_REGISTRY.items()
+ if name not in BASELINE_ONLY_MCP_TOOLS and tool.is_available
+ }
+
@staticmethod
async def _registered_tool_names(server) -> set[str]:
instance = server["instance"]
diff --git a/autogpt_platform/backend/backend/copilot/tools/__init__.py b/autogpt_platform/backend/backend/copilot/tools/__init__.py
index f250f6ae19..e84cc1c660 100644
--- a/autogpt_platform/backend/backend/copilot/tools/__init__.py
+++ b/autogpt_platform/backend/backend/copilot/tools/__init__.py
@@ -97,7 +97,7 @@ from .workspace_files import (
)
if TYPE_CHECKING:
- from backend.copilot.model import ChatSession
+ from backend.copilot.model import ChatSession, ChatSessionOrigin
logger = logging.getLogger(__name__)
@@ -290,6 +290,35 @@ def expert_tool_disabled_groups(
return ["expert_admin"] if expert_id else ["experts"]
+# The tools ``autopilot_session_guard`` refuses off an interactive origin:
+# hidden there rather than declared and then refused. Not a ``ToolGroup`` —
+# that says what a tool does, and each of these already holds ``expert_admin``
+# — and not a wider "needs a person" set either, because ``automation`` marks
+# a machine-authored PROMPT, not an empty chat: a dream pass and a scheduled
+# brief both carry it and both expect the user to read and reply. So what is
+# safe to withhold on this seam is what the runtime already withholds, no
+# more. ``tool_schema_test`` asserts the two stay equal.
+INTERACTIVE_ORIGIN_TOOLS: frozenset[str] = frozenset(
+ {
+ "hire_expert",
+ "raise_expert",
+ "update_expert",
+ "confirm_expert_change",
+ }
+)
+
+
+def origin_disabled_tools(origin: "ChatSessionOrigin | None") -> frozenset[str]:
+ """Tools to hide from a session *origin* no person is driving.
+
+ Positive match, so a legacy ``None`` is hidden from too — an unknown
+ origin cannot prove a human is here, and ``autopilot_session_guard``
+ takes the same side of the same unknown, which is the point: whatever
+ it would refuse, this stops us declaring.
+ """
+ return frozenset() if origin == "interactive" else INTERACTIVE_ORIGIN_TOOLS
+
+
def tool_names_in_groups(groups: Iterable[ToolGroup]) -> frozenset[str]:
"""Return the set of tool short-names belonging to any of *groups*."""
group_set = frozenset(groups)
diff --git a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py
index 568fe1cef5..d9042897d2 100644
--- a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py
+++ b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py
@@ -283,6 +283,91 @@ def test_total_schema_char_budget() -> None:
)
+# The other half of the brake: what the LARGEST session actually declares, in
+# the shape the SDK path sends (``mcp__copilot__`` name, no ``required``,
+# compact separators). ``_CHAR_BUDGET`` above sums the registry, which no
+# session ever gets — so hiding a tool behind a context, as the interactive
+# gate does, is worth nothing to it, and the SDK-only file tools it never
+# counted are ones the user pays for on every turn. This line is that number.
+#
+# The largest is a plain Otto chat with both flags on and E2B: it hides the
+# five ``experts`` tools where an expert chat hides the eight ``expert_admin``
+# ones. ``is_available`` is deliberately NOT applied — the environment
+# decides it, so applying it would make the ceiling differ between CI and a
+# laptop; every tool the session's gates admit counts, which is the upper
+# bound a ceiling wants.
+#
+# Set at the measured 62,003 plus one on 2026-09-16, the first time this line
+# existed. No margin, for the reason _CHAR_BUDGET carries none.
+# Raised 62_004 -> 63_610 the same day, on the dev merge that brought
+# #14436's edit_chat_platform_message: the tool measures 1,351 here and the
+# line added to post_to_chat_platform's description 255, so the largest
+# session moves 62,003 -> 63,609. Measured on the branch merged with dev,
+# which is what CI builds — the branch tip still read 62,003 and would have
+# been ejected from the queue.
+# ON CONFLICT, KEEP THE HIGHER VALUE — same rule, same reason: each branch's
+# CI measures only its own delta while the ceiling has to cover every in-flight
+# PR together. MEASURE ON THE PR'S MERGE REF, never the branch tip.
+_SESSION_WIRE_BUDGET = 63_610
+
+
+def test_largest_declared_session_wire_budget() -> None:
+ """Assert what one session declares stays under the wire budget.
+
+ ``test_total_schema_char_budget`` measures the registry; this measures a
+ turn. The two move independently: a tool added behind a context raises the
+ first and not the second, and an SDK-only file tool raises the second and
+ not the first.
+ """
+ assert _largest_session_wire_chars() < _SESSION_WIRE_BUDGET, (
+ f"The largest session declares {_largest_session_wire_chars():,} chars "
+ f"of tool schema, over the {_SESSION_WIRE_BUDGET:,} budget. Hide the "
+ f"tool behind a context, trim it, or raise the budget intentionally."
+ )
+
+
+def _largest_session_wire_chars() -> int:
+ """Wire chars an Otto chat declares with both flags on, E2B, interactive."""
+ from backend.copilot.sdk.e2b_file_tools import E2B_FILE_TOOLS
+ from backend.copilot.sdk.tool_adapter import (
+ _READ_TOOL_DESCRIPTION,
+ _READ_TOOL_NAME,
+ _READ_TOOL_SCHEMA,
+ BASELINE_ONLY_MCP_TOOLS,
+ MCP_TOOL_PREFIX,
+ _build_input_schema,
+ )
+ from backend.copilot.tools import (
+ expert_tool_disabled_groups,
+ origin_disabled_tools,
+ tool_names_in_groups,
+ )
+
+ def wire(name: str, description: str, schema: dict) -> int:
+ entry = {
+ "name": f"{MCP_TOOL_PREFIX}{name}",
+ "description": description,
+ "input_schema": schema,
+ }
+ return len(json.dumps(entry, separators=(",", ":")))
+
+ hidden = set(
+ tool_names_in_groups(
+ expert_tool_disabled_groups(experts_enabled=True, expert_id=None)
+ )
+ )
+ hidden |= set(BASELINE_ONLY_MCP_TOOLS) | {"get_agent_building_guide"}
+ hidden |= origin_disabled_tools("interactive")
+
+ total = sum(
+ wire(name, tool.description, _build_input_schema(tool))
+ for name, tool in TOOL_REGISTRY.items()
+ if name not in hidden
+ )
+ total += sum(wire(name, desc, schema) for name, desc, schema, _ in E2B_FILE_TOOLS)
+ return total + wire(_READ_TOOL_NAME, _READ_TOOL_DESCRIPTION, _READ_TOOL_SCHEMA)
+
+
# ── Capability-group filtering (ToolGroup / disabled_groups) ───────────
@@ -338,3 +423,89 @@ def test_get_copilot_tool_names_hides_graphiti_when_disabled() -> None:
get_copilot_tool_names(use_e2b=True, disabled_groups=["graphiti"])
)
assert not (memory_mcp_names & filtered_e2b)
+
+
+# ── Origin filtering (INTERACTIVE_ORIGIN_TOOLS / origin_disabled_tools) ──
+
+
+def test_automation_origin_declares_no_interactive_origin_tools() -> None:
+ """A machine-authored session is not offered what its guard would refuse.
+
+ The baseline path passes the set as ``disabled_tools``; the SDK path
+ unions it into the names it never registers, covered against the real
+ MCP server in ``sdk/tool_adapter_test.py``. A legacy ``origin=None``
+ is treated as automation, as ``autopilot_session_guard`` treats it.
+ """
+ from backend.copilot.tools import (
+ INTERACTIVE_ORIGIN_TOOLS,
+ get_available_tools,
+ origin_disabled_tools,
+ )
+
+ for origin in ("automation", None):
+ hidden = origin_disabled_tools(origin)
+ assert hidden == INTERACTIVE_ORIGIN_TOOLS
+
+ declared = {
+ t["function"]["name"] for t in get_available_tools(disabled_tools=hidden)
+ }
+ assert not (INTERACTIVE_ORIGIN_TOOLS & declared), (
+ f"origin={origin!r} still declares "
+ f"{sorted(INTERACTIVE_ORIGIN_TOOLS & declared)}"
+ )
+ # The gate is narrow on purpose: an automation still does its work,
+ # still reports through a chat platform, still wakes itself up.
+ assert {
+ "run_agent",
+ "run_block",
+ "run_sub_session",
+ "schedule_followup",
+ "ask_question",
+ } <= declared
+
+
+def test_interactive_origin_declares_every_tool_it_did_before() -> None:
+ """An interactive session declares exactly what it declared before.
+
+ The counterpart to the test above, and what fails if the gate ever widens
+ past ``origin`` into the sessions a person really is driving.
+ """
+ from backend.copilot.tools import (
+ INTERACTIVE_ORIGIN_TOOLS,
+ get_available_tools,
+ origin_disabled_tools,
+ )
+
+ hidden = origin_disabled_tools("interactive")
+ assert hidden == frozenset()
+
+ declared = {
+ t["function"]["name"] for t in get_available_tools(disabled_tools=hidden)
+ }
+ assert (
+ INTERACTIVE_ORIGIN_TOOLS <= declared
+ ), f"interactive session lost {sorted(INTERACTIVE_ORIGIN_TOOLS - declared)}"
+
+
+def test_set_matches_the_tools_that_call_the_origin_guard() -> None:
+ """The set is only sound while it equals what the runtime refuses.
+
+ Hiding a tool the guard does not refuse takes a capability away from
+ automations; declaring one it does refuse is the waste this PR removes.
+ A new staffing tool adds ``autopilot_session_guard`` and this fails until
+ the name is listed — the check no other test in the tree performs.
+ """
+ import inspect
+
+ from backend.copilot.tools import INTERACTIVE_ORIGIN_TOOLS
+
+ guarded = {
+ name
+ for name, tool in TOOL_REGISTRY.items()
+ if "autopilot_session_guard(" in inspect.getsource(type(tool))
+ }
+ assert guarded == INTERACTIVE_ORIGIN_TOOLS, (
+ "INTERACTIVE_ORIGIN_TOOLS is out of step with the runtime guard: "
+ f"guarded but declared {sorted(guarded - INTERACTIVE_ORIGIN_TOOLS)}, "
+ f"hidden but unguarded {sorted(INTERACTIVE_ORIGIN_TOOLS - guarded)}"
+ )