Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/agent_env/agents/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ mostly the lxml and Pillow they depend on, and their pins match the Codex and Ge
task's documents read the same way in each. A `deploy_agent` `builtin_tools`
selection picks the CLI's own tools: `{"disable": "*"}` and `{"only": [...]}` become its `--tools` list, a list to
turn off becomes `--disallowedTools`, which keeps the CLI's defaults for the rest, and any selection adds
`--strict-mcp-config`, so its MCP servers are only the task env's. Tool search is off unless the agent's environment sets
`--strict-mcp-config`, so its MCP servers are only the task env's. A `deploy_agent` `peer_tools: false` also leaves out the peer_agents MCP
server, so the agent can't message the task's other agents; an agent whose agent-config takes no `peer_tools` fails
the deploy. Tool search is off unless the agent's environment sets
`ENABLE_TOOL_SEARCH`, so the CLI sends the env's tools and its own up front. The names are `ClaudeCodeTool`'s in
`claude_bridge.py`: every tool the pinned CLI has, though which it offers by default depends on the model. A test fails
when the Dockerfile's `CLAUDE_CODE_VERSION` moves, until `ClaudeCodeTool` and `TOOLS_CLI_VERSION` move with it.
Expand Down
2 changes: 1 addition & 1 deletion src/agent_env/task_step/task_steps/always_on.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ async def _configure_agents(self) -> None:
"max_thinking_tokens": overrides.get("agent_max_thinking_tokens") or step.max_thinking_tokens,
"output_format": step.output_format, "timeout_seconds": step.timeout_seconds,
"task_id": ctx.metadata.get("task_id"), "agentenv_tools": step.agentenv_tools,
"builtin_tools": card.get("builtin_tools")}
"builtin_tools": card.get("builtin_tools"), "peer_tools": card.get("peer_tools")}
desired = {k: v for k, v in desired.items() if v is not None}
model_params = get_config().get_model_params(step.model_params)
if model_params:
Expand Down
27 changes: 26 additions & 1 deletion src/agent_env/task_step/task_steps/deploy_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,15 @@ def _skill_fields(skill: dict) -> dict:



def _config_request(agent_config: dict | None) -> dict:
"""The ``set`` request description of a card's agent-config extension."""
return ((agent_config or {}).get("params") or {}).get("methods", {}).get("set", {}).get("request", {})


def _check_builtin_tools(agent_config: dict | None, agent_name: str, selection: dict) -> None:
"""Refuse a selection of the agent's own tools that its card's agent-config extension can't take: it has no
``builtin_tools``, or its schema doesn't name a tool the selection does."""
request = ((agent_config or {}).get("params") or {}).get("methods", {}).get("set", {}).get("request", {})
request = _config_request(agent_config)
if "builtin_tools" not in request.get("supported", []):
raise RuntimeError(f"Agent '{agent_name}' cannot have its own tools selected: its card's "
f"{AGENT_CONFIG_V1.uri} takes no 'builtin_tools'")
Expand All @@ -165,6 +170,14 @@ def _check_builtin_tools(agent_config: dict | None, agent_name: str, selection:
if error is not None:
raise RuntimeError(f"Agent '{agent_name}' can't take builtin_tools {selection}: {error.message}")


def _check_peer_tools(agent_config: dict | None, agent_name: str) -> None:
"""Refuse to turn off the peer_agents tools of an agent whose card's agent-config extension can't take
``peer_tools``: it would keep them."""
if "peer_tools" not in _config_request(agent_config).get("supported", []):
raise RuntimeError(f"Agent '{agent_name}' cannot have its peer_agents tools turned off: its card's "
f"{AGENT_CONFIG_V1.uri} takes no 'peer_tools'")

class DeployAgentTaskStep(TaskStep):
type: ClassVar[str] = "deploy_agent"
entity_refs = (
Expand Down Expand Up @@ -208,6 +221,7 @@ def __init__(
network_policy: Optional[dict] = None,
role: Optional[str] = None,
builtin_tools: Optional[dict] = None,
peer_tools: Optional[bool] = None,
enable_agent_changelog: bool = False,
agent_changelog_toolcall_position_exclusive: Optional[int] = None,
sandbox_name: Optional[str] = None,
Expand Down Expand Up @@ -241,6 +255,10 @@ def __init__(
self.role = role or None
self.builtin_tools = (None if builtin_tools is None
else BuiltinTools[str].model_validate(builtin_tools).model_dump(exclude_none=True))
# False: the agent gets no peer_agents MCP tools, so it can't message the task's other agents.
if peer_tools is not None and not isinstance(peer_tools, bool):
raise ValueError(f"peer_tools must be true or false, not {peer_tools!r}")
self.peer_tools = peer_tools
self.enable_agent_changelog = enable_agent_changelog
self.agent_changelog_object_url = agent_changelog_object_url
self.agent_changelog_toolcall_position_exclusive = agent_changelog_toolcall_position_exclusive
Expand Down Expand Up @@ -279,6 +297,7 @@ def to_dict(self) -> dict:
base["network_policy"] = self.network_policy
base["role"] = self.role
base["builtin_tools"] = self.builtin_tools
base["peer_tools"] = self.peer_tools
base["enable_agent_changelog"] = self.enable_agent_changelog
base["agent_changelog_object_url"] = self.agent_changelog_object_url
base["agent_changelog_toolcall_position_exclusive"] = self.agent_changelog_toolcall_position_exclusive
Expand Down Expand Up @@ -312,6 +331,7 @@ def from_dict(cls, data: dict) -> DeployAgentTaskStep:
network_policy=data.get("network_policy"),
role=data.get("role"),
builtin_tools=data.get("builtin_tools"),
peer_tools=data.get("peer_tools"),
enable_agent_changelog=data.get("enable_agent_changelog", False),
agent_changelog_object_url=data.get("agent_changelog_object_url"),
agent_changelog_toolcall_position_exclusive=data.get("agent_changelog_toolcall_position_exclusive"),
Expand Down Expand Up @@ -464,6 +484,11 @@ async def _execute(self, context: TaskStepContext, deployed_agents: list) -> Tas
_check_builtin_tools(A2AAgent.find_extension(card, A2AAgent.EXT_AGENT_CONFIG), self.agent_name,
self.builtin_tools)
identity_payload["builtin_tools"] = self.builtin_tools
if self.peer_tools is not None:
if not self.peer_tools:
# Before the first request to the agent, as for builtin_tools.
_check_peer_tools(A2AAgent.find_extension(card, A2AAgent.EXT_AGENT_CONFIG), self.agent_name)
identity_payload["peer_tools"] = self.peer_tools
identity = A2AAgent.negotiate_agent_config(card, identity_payload)
if self.role is not None:
carried_by = [via for via, carried in (
Expand Down
10 changes: 7 additions & 3 deletions src/agent_env/task_step/task_steps/prompt_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ async def _converse(
"task_id": context.metadata.get("task_id"),
"agentenv_tools": self.agentenv_tools,
# Again each turn, so an agent that restarted since deploy_agent sent it doesn't get its own tools back.
"builtin_tools": card.get("builtin_tools"),
"builtin_tools": card.get("builtin_tools"), "peer_tools": card.get("peer_tools"),
}
desired_config = {k: v for k, v in desired_config.items() if v is not None}
model_params = get_config().get_model_params(self.model_params)
Expand All @@ -568,8 +568,12 @@ async def _converse(
elif is_user_sim:
user_output_format = self.user_output_format or _DEFAULT_USER_SIM_OUTPUT_FORMAT
user_model = self._resolve_user_model(context)
user_config = {k: v for k, v in {"output_format": user_output_format, "model": user_model}.items() if v is not None}
negotiated = A2AAgent.negotiate_agent_config(user_sim.a2a_card or {}, user_config)
user_card = user_sim.a2a_card or {}
# Its own tools and peer tools as deploy_agent set them, again, as for the agent above.
user_config = {k: v for k, v in {"output_format": user_output_format, "model": user_model,
"builtin_tools": user_card.get("builtin_tools"),
"peer_tools": user_card.get("peer_tools")}.items() if v is not None}
Comment on lines +571 to +575

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Built-in users regain peer tools. The new resend runs only in elif is_user_sim, so it skips users created by deploy_user_sim_agent. If that agent restarts between deployment and prompt_agent, its saved peer_tools: false is never sent again. Claude Code defaults to True, so the user simulator gets the peer tools back. always_on also skips this resend for its user simulator. Send the saved tool settings independently of the branches that skip model and prompt setup, and cover the built-in user path in a test.

Knowledge Base Used: Task-step context and execution

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agent_env/task_step/task_steps/prompt_agent.py
Line: 571-575

Comment:
**Built-in users regain peer tools.** The new resend runs only in `elif is_user_sim`, so it skips users created by `deploy_user_sim_agent`. If that agent restarts between deployment and `prompt_agent`, its saved `peer_tools: false` is never sent again. Claude Code defaults to `True`, so the user simulator gets the peer tools back. `always_on` also skips this resend for its user simulator. Send the saved tool settings independently of the branches that skip model and prompt setup, and cover the built-in user path in a test.

**Knowledge Base Used:** [Task-step context and execution](https://app.greptile.com/-/switch-tenant?tenant=scale-ai&next=%2Fcustom-context%2Fknowledge-base%2Fscaleapi%2Fagentenv-framework%2F-%2Fdocs%2Ftask-step-execution.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Fix in Cursor Fix in Claude Code Fix in Codex

negotiated = A2AAgent.negotiate_agent_config(user_card, user_config)
if negotiated:
await protocol.post_agent_config(user_url + negotiated.endpoint, negotiated.fields,
headers=user_sim.control_headers)
Expand Down
38 changes: 38 additions & 0 deletions tst/unit/task_step/test_deploy_agent_role_delivery.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,44 @@ async def test_a_selection_the_agent_cannot_take_fails_before_any_request(wiring
assert wiring.agent.close.await_count == 1


PEER_CONFIG = _agent_config(["name", "description", "peer_tools"])


@pytest.mark.asyncio
async def test_peer_tools_off_is_sent_once_with_its_identity_and_kept_on_the_card(wiring):
context = await wiring.deploy(_card(HEADERS_MCP, PEER_CONFIG), role=None, peer_tools=False)

assert wiring.bodies("/ext/agent-config") == [{"name": "solver", "peer_tools": False}]
assert context.deployed_agents[0].a2a_card["peer_tools"] is False


@pytest.mark.asyncio
async def test_peer_tools_left_unset_sends_none(wiring):
await wiring.deploy(_card(HEADERS_MCP, PEER_CONFIG), role=None)

assert wiring.bodies("/ext/agent-config") == [{"name": "solver"}]


@pytest.mark.asyncio
@pytest.mark.parametrize("card", [_card(HEADERS_MCP, ROLE_CONFIG), _card(HEADERS_MCP)],
ids=["config-without-it", "no-agent-config"])
async def test_peer_tools_off_on_an_agent_that_cannot_take_it_fails_before_any_request(wiring, card):
with pytest.raises(RuntimeError, match="takes no 'peer_tools'"):
await wiring.deploy(card, role=None, peer_tools=False)

assert wiring.requests == []
assert wiring.agent.close.await_count == 1


def test_peer_tools_is_a_bool_and_round_trips():
with pytest.raises(ValueError):
DeployAgentTaskStep(id="ag", version=None, peer_tools="no")
step = DeployAgentTaskStep(id="ag", version=None, peer_tools=False)
assert DeployAgentTaskStep.from_dict({**step.to_dict(), "version": 1}).peer_tools is False
assert DeployAgentTaskStep.from_dict({**DeployAgentTaskStep(id="ag", version=None).to_dict(),
"version": 1}).peer_tools is None


def test_a_selection_takes_exactly_one_of_disable_and_only():
for selection in ({}, {"disable": "*", "only": ["Read"]}, {"enable": ["Read"]}):
with pytest.raises(ValueError):
Expand Down
39 changes: 35 additions & 4 deletions tst/unit/task_step/test_prompt_agent_builtin_tools.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""prompt_agent sends the agent's selection of its own tools again on every turn, as deploy_agent recorded it on the
agent's card, so an agent that restarted since its deploy doesn't get its own tools back; an agent deployed without
one is sent none."""
"""prompt_agent sends the agent's selection of its own tools, and peer_tools, again on every turn, as deploy_agent
recorded them on the agent's card, so an agent that restarted since its deploy doesn't get its tools back; an agent
deployed without them is sent none. A user sim gets its own the same way."""

from unittest.mock import MagicMock

Expand All @@ -12,7 +12,7 @@
from agent_env.task_step.task_steps import prompt_agent as pa_mod
from agent_env.task_step.task_steps.prompt_agent import PromptAgentTaskStep

SUPPORTED = ["model", "builtin_tools"]
SUPPORTED = ["model", "builtin_tools", "peer_tools"]


@pytest.fixture
Expand Down Expand Up @@ -81,3 +81,34 @@ async def post_agent_config(url, fields, headers=None):
await PromptAgentTaskStep(id="one", version=None, prompt="one", agent_name="solver").execute(context)

assert sent == [{"AgentEnv-Control-Token": "tok"}]


@pytest.mark.asyncio
async def test_every_turn_sends_peer_tools_off_as_deploy_agent_recorded(posted):
context = _context(peer_tools=False)

for turn in ("one", "two"):
await PromptAgentTaskStep(id=turn, version=None, prompt=turn, agent_name="solver").execute(context)

assert [fields.get("peer_tools") for fields in posted] == [False, False]


@pytest.mark.asyncio
async def test_a_user_sim_is_sent_its_own_tools_and_peer_tools_each_turn(posted, monkeypatch):
context = _context()
user_card = {**context.deployed_agents[0].a2a_card, "builtin_tools": {"disable": "*"}, "peer_tools": False}
context.deployed_agents.append(DeployedAgent(agent_name="usersim", api_url="http://usersim",
a2a_url="http://usersim", a2a_card=user_card))
step = PromptAgentTaskStep(id="one", version=None, prompt="one", agent_name="solver", user_agent_name="usersim")
calls = []

async def post_agent_config(url, fields, headers=None):
calls.append((url, fields))

monkeypatch.setattr(pa_mod.protocol, "post_agent_config", post_agent_config)

await step.execute(context)

user_fields = [fields for url, fields in calls if url.startswith("http://usersim")]
assert user_fields and all(f.get("builtin_tools") == {"disable": "*"} and f.get("peer_tools") is False
for f in user_fields)
Loading