Skip to content

Commit 70dfef8

Browse files
caohy1988claude
andcommitted
fix: classify TransferToAgentTool transfers to RemoteA2aAgent as TRANSFER_A2A
When a RemoteA2aAgent is registered via sub_agents, the BigQuery analytics plugin now correctly classifies transfers to it as "TRANSFER_A2A" instead of the generic "TRANSFER_AGENT". Classification is call-level: a single TransferToAgentTool with mixed local and A2A targets produces the correct label per invocation based on the selected agent_name. The fix is localized to the plugin — no changes to TransferToAgentTool or the ADK core. The plugin resolves the target agent at runtime via the invocation context's agent tree. Fixes google#5073 Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
1 parent 1a9df8f commit 70dfef8

2 files changed

Lines changed: 199 additions & 4 deletions

File tree

‎src/google/adk/plugins/bigquery_agent_analytics_plugin.py‎

Lines changed: 54 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,57 @@ def _format_content(
168168
return " | ".join(parts), truncated
169169

170170

171-
def _get_tool_origin(tool: "BaseTool") -> str:
171+
def _find_transfer_target(agent, agent_name: str):
172+
"""Find a transfer target agent by name in the accessible agent tree.
173+
174+
Searches the current agent's sub-agents, parent, and peer agents
175+
to locate the transfer target.
176+
177+
Args:
178+
agent: The current agent executing the transfer.
179+
agent_name: The name of the transfer target to find.
180+
181+
Returns:
182+
The matching agent object, or None if not found.
183+
"""
184+
for sub in getattr(agent, "sub_agents", []):
185+
if sub.name == agent_name:
186+
return sub
187+
parent = getattr(agent, "parent_agent", None)
188+
if parent is not None and parent.name == agent_name:
189+
return parent
190+
if parent is not None:
191+
for peer in getattr(parent, "sub_agents", []):
192+
if peer.name == agent_name and peer.name != agent.name:
193+
return peer
194+
return None
195+
196+
197+
def _get_tool_origin(
198+
tool: "BaseTool",
199+
tool_args: Optional[dict[str, Any]] = None,
200+
tool_context: Optional["ToolContext"] = None,
201+
) -> str:
172202
"""Returns the provenance category of a tool.
173203
174204
Uses lazy imports to avoid circular dependencies.
175205
206+
For ``TransferToAgentTool`` the classification is **call-level**: when
207+
*tool_args* and *tool_context* are supplied the selected
208+
``agent_name`` is resolved against the agent tree so that transfers
209+
to a ``RemoteA2aAgent`` are labelled ``TRANSFER_A2A`` rather than
210+
the generic ``TRANSFER_AGENT``.
211+
176212
Args:
177213
tool: The tool instance.
214+
tool_args: Optional tool arguments, used for call-level
215+
classification of TransferToAgentTool.
216+
tool_context: Optional tool context, used to access the agent
217+
tree for TransferToAgentTool classification.
178218
179219
Returns:
180-
One of LOCAL, MCP, A2A, SUB_AGENT, TRANSFER_AGENT, or UNKNOWN.
220+
One of LOCAL, MCP, A2A, SUB_AGENT, TRANSFER_AGENT,
221+
TRANSFER_A2A, or UNKNOWN.
181222
"""
182223
# Import lazily to avoid circular dependencies.
183224
# pylint: disable=g-import-not-at-top
@@ -199,6 +240,15 @@ def _get_tool_origin(tool: "BaseTool") -> str:
199240
if McpTool is not None and isinstance(tool, McpTool):
200241
return "MCP"
201242
if isinstance(tool, TransferToAgentTool):
243+
if RemoteA2aAgent is not None and tool_args and tool_context:
244+
agent_name = tool_args.get("agent_name")
245+
if agent_name:
246+
target = _find_transfer_target(
247+
tool_context._invocation_context.agent,
248+
agent_name,
249+
)
250+
if target is not None and isinstance(target, RemoteA2aAgent):
251+
return "TRANSFER_A2A"
202252
return "TRANSFER_AGENT"
203253
if isinstance(tool, AgentTool):
204254
if RemoteA2aAgent is not None and isinstance(tool.agent, RemoteA2aAgent):
@@ -3228,7 +3278,7 @@ async def before_tool_callback(
32283278
args_truncated, is_truncated = _recursive_smart_truncate(
32293279
tool_args, self.config.max_content_length
32303280
)
3231-
tool_origin = _get_tool_origin(tool)
3281+
tool_origin = _get_tool_origin(tool, tool_args, tool_context)
32323282
content_dict = {
32333283
"tool": tool.name,
32343284
"args": args_truncated,
@@ -3262,7 +3312,7 @@ async def after_tool_callback(
32623312
resp_truncated, is_truncated = _recursive_smart_truncate(
32633313
result, self.config.max_content_length
32643314
)
3265-
tool_origin = _get_tool_origin(tool)
3315+
tool_origin = _get_tool_origin(tool, tool_args, tool_context)
32663316
content_dict = {
32673317
"tool": tool.name,
32683318
"result": resp_truncated,

‎tests/unittests/plugins/test_bigquery_agent_analytics_plugin.py‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4486,6 +4486,151 @@ def test_transfer_tool_returns_transfer_agent(self):
44864486
result = bigquery_agent_analytics_plugin._get_tool_origin(tool)
44874487
assert result == "TRANSFER_AGENT"
44884488

4489+
def test_transfer_tool_without_args_returns_transfer_agent(self):
4490+
"""TransferToAgentTool without tool_args falls back to TRANSFER_AGENT."""
4491+
from google.adk.tools.transfer_to_agent_tool import TransferToAgentTool
4492+
4493+
tool = TransferToAgentTool(agent_names=["remote_a2a"])
4494+
result = bigquery_agent_analytics_plugin._get_tool_origin(
4495+
tool, tool_args=None, tool_context=None
4496+
)
4497+
assert result == "TRANSFER_AGENT"
4498+
4499+
def test_transfer_to_remote_a2a_sub_agent_returns_transfer_a2a(self):
4500+
"""Transfer to a RemoteA2aAgent sub-agent is classified TRANSFER_A2A."""
4501+
from google.adk.tools.transfer_to_agent_tool import TransferToAgentTool
4502+
4503+
try:
4504+
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
4505+
except ImportError:
4506+
pytest.skip("A2A agent not available")
4507+
4508+
remote_agent = mock.MagicMock(spec=RemoteA2aAgent)
4509+
remote_agent.name = "remote_a2a"
4510+
4511+
current_agent = mock.MagicMock()
4512+
current_agent.name = "root"
4513+
current_agent.sub_agents = [remote_agent]
4514+
current_agent.parent_agent = None
4515+
4516+
inv_ctx = mock.MagicMock()
4517+
inv_ctx.agent = current_agent
4518+
tool_context = mock.MagicMock()
4519+
tool_context._invocation_context = inv_ctx
4520+
4521+
tool = TransferToAgentTool(agent_names=["remote_a2a"])
4522+
result = bigquery_agent_analytics_plugin._get_tool_origin(
4523+
tool,
4524+
tool_args={"agent_name": "remote_a2a"},
4525+
tool_context=tool_context,
4526+
)
4527+
assert result == "TRANSFER_A2A"
4528+
4529+
def test_transfer_to_local_sub_agent_returns_transfer_agent(self):
4530+
"""Transfer to a local sub-agent is still classified TRANSFER_AGENT."""
4531+
from google.adk.tools.transfer_to_agent_tool import TransferToAgentTool
4532+
4533+
local_agent = mock.MagicMock()
4534+
local_agent.name = "local_sub"
4535+
4536+
current_agent = mock.MagicMock()
4537+
current_agent.name = "root"
4538+
current_agent.sub_agents = [local_agent]
4539+
current_agent.parent_agent = None
4540+
4541+
inv_ctx = mock.MagicMock()
4542+
inv_ctx.agent = current_agent
4543+
tool_context = mock.MagicMock()
4544+
tool_context._invocation_context = inv_ctx
4545+
4546+
tool = TransferToAgentTool(agent_names=["local_sub"])
4547+
result = bigquery_agent_analytics_plugin._get_tool_origin(
4548+
tool,
4549+
tool_args={"agent_name": "local_sub"},
4550+
tool_context=tool_context,
4551+
)
4552+
assert result == "TRANSFER_AGENT"
4553+
4554+
def test_transfer_to_a2a_peer_returns_transfer_a2a(self):
4555+
"""Transfer to a RemoteA2aAgent peer is classified TRANSFER_A2A."""
4556+
from google.adk.tools.transfer_to_agent_tool import TransferToAgentTool
4557+
4558+
try:
4559+
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
4560+
except ImportError:
4561+
pytest.skip("A2A agent not available")
4562+
4563+
remote_peer = mock.MagicMock(spec=RemoteA2aAgent)
4564+
remote_peer.name = "remote_peer"
4565+
4566+
current_agent = mock.MagicMock()
4567+
current_agent.name = "child"
4568+
current_agent.sub_agents = []
4569+
4570+
parent_agent = mock.MagicMock()
4571+
parent_agent.name = "parent"
4572+
parent_agent.sub_agents = [current_agent, remote_peer]
4573+
current_agent.parent_agent = parent_agent
4574+
4575+
inv_ctx = mock.MagicMock()
4576+
inv_ctx.agent = current_agent
4577+
tool_context = mock.MagicMock()
4578+
tool_context._invocation_context = inv_ctx
4579+
4580+
tool = TransferToAgentTool(
4581+
agent_names=["remote_peer"],
4582+
)
4583+
result = bigquery_agent_analytics_plugin._get_tool_origin(
4584+
tool,
4585+
tool_args={"agent_name": "remote_peer"},
4586+
tool_context=tool_context,
4587+
)
4588+
assert result == "TRANSFER_A2A"
4589+
4590+
def test_transfer_mixed_targets_classifies_per_call(self):
4591+
"""A single TransferToAgentTool with mixed targets classifies per call."""
4592+
from google.adk.tools.transfer_to_agent_tool import TransferToAgentTool
4593+
4594+
try:
4595+
from google.adk.agents.remote_a2a_agent import RemoteA2aAgent
4596+
except ImportError:
4597+
pytest.skip("A2A agent not available")
4598+
4599+
remote_agent = mock.MagicMock(spec=RemoteA2aAgent)
4600+
remote_agent.name = "remote_a2a"
4601+
local_agent = mock.MagicMock()
4602+
local_agent.name = "local_sub"
4603+
4604+
current_agent = mock.MagicMock()
4605+
current_agent.name = "root"
4606+
current_agent.sub_agents = [remote_agent, local_agent]
4607+
current_agent.parent_agent = None
4608+
4609+
inv_ctx = mock.MagicMock()
4610+
inv_ctx.agent = current_agent
4611+
tool_context = mock.MagicMock()
4612+
tool_context._invocation_context = inv_ctx
4613+
4614+
tool = TransferToAgentTool(
4615+
agent_names=["remote_a2a", "local_sub"],
4616+
)
4617+
4618+
# Transfer to remote target → TRANSFER_A2A
4619+
result = bigquery_agent_analytics_plugin._get_tool_origin(
4620+
tool,
4621+
tool_args={"agent_name": "remote_a2a"},
4622+
tool_context=tool_context,
4623+
)
4624+
assert result == "TRANSFER_A2A"
4625+
4626+
# Transfer to local target → TRANSFER_AGENT
4627+
result = bigquery_agent_analytics_plugin._get_tool_origin(
4628+
tool,
4629+
tool_args={"agent_name": "local_sub"},
4630+
tool_context=tool_context,
4631+
)
4632+
assert result == "TRANSFER_AGENT"
4633+
44894634
def test_mcp_tool_returns_mcp(self):
44904635
try:
44914636
from google.adk.tools.mcp_tool.mcp_tool import McpTool

0 commit comments

Comments
 (0)