Skip to content
Closed
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
33 changes: 30 additions & 3 deletions src/google/adk/agents/remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@


# Function call names whose pause is resolved locally (ADK request-* tools or a
# workflow HITL node); their response is flattened to text before forwarding.
# workflow HITL node). Flatten those answers to text before forwarding, unless
# the matching function call was raised by the remote peer itself.
_HUMAN_INPUT_FUNCTION_CALL_NAMES = frozenset({
MOCK_FUNCTION_CALL_FOR_REQUIRED_USER_INPUT,
MOCK_FUNCTION_CALL_FOR_REQUIRED_USER_AUTH,
Expand Down Expand Up @@ -176,8 +177,14 @@ def _sanitize_user_function_response_event(
event: Event,
trusted_call_names_by_id: dict[Optional[str], set[str]],
id_less_call_is_ambiguous: bool,
flatten_human_input: bool = True,
) -> Event:
"""Returns a copy of ``event`` with its parts sanitized for forwarding."""
"""Returns a copy of ``event`` with its parts sanitized for forwarding.

When ``flatten_human_input`` is false the matching pause was raised by the
remote peer, so confirmation and request-input answers stay function
responses and the peer can resume. Credential answers are still dropped.
"""
if event.content is None:
return event
new_event = event.model_copy(deep=True)
Expand All @@ -188,6 +195,8 @@ def _sanitize_user_function_response_event(
parts = new_content.parts or []

def _is_human_input(fr: genai_types.FunctionResponse) -> bool:
if not flatten_human_input:
return False
names = trusted_call_names_by_id.get(fr.id)
if not names or names.isdisjoint(_HUMAN_INPUT_FUNCTION_CALL_NAMES):
return False
Expand Down Expand Up @@ -783,8 +792,14 @@ def _create_a2a_request_for_user_function_response(
if fc.id is None and fc.name not in _HUMAN_INPUT_FUNCTION_CALL_NAMES:
id_less_call_is_ambiguous = True

# Relayed HITL uses the same adk_request_* names as a local workflow
# pause. Flatten by origin so the remote peer still receives a
# FunctionResponse and can resume. Credentials still drop (aec7aa3).
event = _sanitize_user_function_response_event(
event, trusted_call_names_by_id, id_less_call_is_ambiguous
event,
trusted_call_names_by_id,
id_less_call_is_ambiguous,
flatten_human_input=not self._is_relayed_pause(function_call_event),
)

a2a_message = convert_event_to_a2a_message(
Expand Down Expand Up @@ -824,6 +839,18 @@ def _is_remote_response(self, event: Event) -> bool:

return False

def _is_relayed_pause(self, event: Event) -> bool:
"""Whether ``event`` is a pause this remote peer raised.

Production events carry ``a2a:response`` and match
``_is_remote_response``. Nested A2A relays also author the pause as
this agent, which is enough to tell them apart from a local workflow
HITL pause (those are authored by the caller, not ``self.name``).
"""
if self._is_remote_response(event):
return True
return event.author == self.name

def _construct_message_parts_from_session(
self, ctx: InvocationContext
) -> tuple[list[A2APart], Optional[str]]:
Expand Down
109 changes: 104 additions & 5 deletions tests/unittests/agents/test_remote_a2a_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5852,6 +5852,8 @@ def _resume_events(
responses,
user_text=None,
task_id="task-123",
pause_author="agent",
extra_call_metadata=None,
):
"""Builds the ``[pause_event, user_response_event]`` sequence seen on resume.

Expand All @@ -5863,6 +5865,10 @@ def _resume_events(
parallel real-tool + human-input case.
user_text: optional sibling text part appended to the response event.
task_id: value stamped into the pausing event's a2a metadata.
pause_author: author of the pausing function-call event. Local workflow
pauses use the caller (default ``agent``). Relayed A2A pauses use the
remote peer's agent name.
extra_call_metadata: extra keys merged into the pausing event metadata.

Returns:
``[pause_event, user_response_event]``.
Expand All @@ -5873,16 +5879,19 @@ def _resume_events(
)
for name, cid in calls
]
call_metadata = {
A2A_METADATA_PREFIX + "task_id": task_id,
A2A_METADATA_PREFIX + "context_id": "context-123",
}
if extra_call_metadata:
call_metadata.update(extra_call_metadata)
call_event = Event(
invocation_id="inv-1",
author="agent",
author=pause_author,
id="e_call",
content=genai_types.Content(role="model", parts=call_parts),
long_running_tool_ids={cid for _, cid in calls if cid},
custom_metadata={
A2A_METADATA_PREFIX + "task_id": task_id,
A2A_METADATA_PREFIX + "context_id": "context-123",
},
custom_metadata=call_metadata,
)
response_parts = [
genai_types.Part(
Expand Down Expand Up @@ -6009,6 +6018,96 @@ def test_request_confirmation_is_flattened(self):
assert parts
assert "data" not in _kinds(parts)

def test_relayed_request_confirmation_stays_a_function_response(self):
"""A confirmation the remote peer raised is forwarded as data, not text."""
agent = _make_agent()
parts = _forwarded_parts(
agent,
_resume_events(
calls=[("adk_request_confirmation", "fc-1")],
responses=[(
"adk_request_confirmation",
"fc-1",
{
"confirmed": True,
"hint": "Approve publish_result()?",
},
)],
pause_author=agent.name,
extra_call_metadata={
A2A_METADATA_PREFIX + "response": True,
},
),
)
assert _kinds(parts) == ["data"]
dumped = _data(parts[0])
assert dumped.get("id") == "fc-1"
assert dumped.get("name") == "adk_request_confirmation"

def test_relayed_confirmation_without_response_metadata_still_resumes(self):
"""Origin is the peer author even when a2a:response is not stamped."""
agent = _make_agent()
parts = _forwarded_parts(
agent,
_resume_events(
calls=[("adk_request_confirmation", "fc-1")],
responses=[
("adk_request_confirmation", "fc-1", {"confirmed": True})
],
pause_author=agent.name,
),
)
assert _kinds(parts) == ["data"]
assert _data(parts[0]).get("name") == "adk_request_confirmation"

def test_relayed_request_input_stays_a_function_response(self):
"""A request-input pause the remote peer raised is forwarded as data."""
agent = _make_agent()
parts = _forwarded_parts(
agent,
_resume_events(
calls=[("adk_request_input", "fc-1")],
responses=[("adk_request_input", "fc-1", {"company_name": "Okta"})],
pause_author=agent.name,
extra_call_metadata={
A2A_METADATA_PREFIX + "response": True,
},
),
)
assert _kinds(parts) == ["data"]
assert _data(parts[0]).get("id") == "fc-1"
assert _data(parts[0]).get("name") == "adk_request_input"

def test_relayed_confirmation_still_drops_credentials(self):
"""Credential material is dropped even on a relayed confirmation resume."""
agent = _make_agent()
parts = _forwarded_parts(
agent,
_resume_events(
calls=[
("adk_request_confirmation", "fc-1"),
("adk_request_credential", "fc-auth"),
],
responses=[
("adk_request_confirmation", "fc-1", {"confirmed": True}),
("adk_request_credential", "fc-auth", _AUTH_PAYLOAD),
],
pause_author=agent.name,
extra_call_metadata={
A2A_METADATA_PREFIX + "response": True,
},
),
)
assert _SECRET not in _dump(parts)
assert any(
_kind(part) == "data" and _data(part).get("id") == "fc-1"
for part in parts
)
assert not any(
_kind(part) == "data" and _data(part).get("id") == "fc-auth"
for part in parts
)

def test_real_long_running_tool_response_is_preserved(self):
"""A real remote long-running tool response is preserved id-for-id."""
parts = _forwarded_parts(
Expand Down