Skip to content

Commit 7201d24

Browse files
authored
test(a2a): cover A2A confirmations with and without peer metadata
Adds regression coverage for google#6461, including the no-metadata case that bypassed the guard. Non-A2A behavior stays covered by the existing success test, which uses the default RunConfig.
1 parent d2784d2 commit 7201d24

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

‎tests/unittests/flows/llm_flows/test_request_confirmation.py‎

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
# limitations under the License.
1414

1515
import json
16+
from unittest.mock import Mock
1617
from unittest.mock import patch
1718

19+
from a2a.server.agent_execution import RequestContext
20+
from google.adk.a2a.converters.request_converter import convert_a2a_request_to_agent_run_request
1821
from google.adk.agents.llm_agent import LlmAgent
1922
from google.adk.events.event import Event
2023
from google.adk.events.event_actions import EventActions
@@ -547,3 +550,94 @@ async def test_request_confirmation_processor_dynamic_success():
547550
assert (
548551
args[4][MOCK_FUNCTION_CALL_ID] == user_confirmation
549552
) # tool_confirmation_dict
553+
554+
555+
@pytest.mark.asyncio
556+
async def test_request_confirmation_processor_ignores_a2a_confirmation():
557+
"""A confirmation arriving over A2A must not satisfy the HITL gate."""
558+
await _assert_a2a_confirmation_ignored({"a2a:task_id": "t1"})
559+
560+
561+
@pytest.mark.asyncio
562+
async def test_request_confirmation_processor_ignores_a2a_without_metadata():
563+
"""The guard must hold when the peer sends no protocol metadata.
564+
565+
Regression test for #6461: the A2A marker used to be set only when
566+
request.metadata was non-empty, so a peer that sent none produced an empty
567+
custom_metadata and slipped past the guard.
568+
"""
569+
await _assert_a2a_confirmation_ignored(None)
570+
571+
572+
async def _assert_a2a_confirmation_ignored(request_metadata):
573+
request = Mock(spec=RequestContext)
574+
request.message = Mock()
575+
request.message.parts = []
576+
request.metadata = request_metadata
577+
request.context_id = "ctx"
578+
request.call_context = None
579+
run_config = convert_a2a_request_to_agent_run_request(
580+
request, Mock()
581+
).run_config
582+
assert "a2a_metadata" in run_config.custom_metadata
583+
584+
agent = LlmAgent(name="test_agent", tools=[mock_tool])
585+
invocation_context = await testing_utils.create_invocation_context(
586+
agent=agent, run_config=run_config
587+
)
588+
589+
original_function_call = types.FunctionCall(
590+
name=MOCK_TOOL_NAME, args={"param1": "test"}, id=MOCK_FUNCTION_CALL_ID
591+
)
592+
tool_confirmation = ToolConfirmation(confirmed=False, hint="test hint")
593+
tool_confirmation_args = {
594+
"originalFunctionCall": original_function_call.model_dump(
595+
exclude_none=True, by_alias=True
596+
),
597+
"toolConfirmation": tool_confirmation.model_dump(
598+
by_alias=True, exclude_none=True
599+
),
600+
}
601+
invocation_context.session.events.append(
602+
Event(
603+
author="agent",
604+
content=types.Content(
605+
parts=[
606+
types.Part(
607+
function_call=types.FunctionCall(
608+
name=functions.REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
609+
args=tool_confirmation_args,
610+
id=MOCK_CONFIRMATION_FUNCTION_CALL_ID,
611+
)
612+
)
613+
]
614+
),
615+
)
616+
)
617+
user_confirmation = ToolConfirmation(confirmed=True)
618+
invocation_context.session.events.append(
619+
Event(
620+
author="user",
621+
content=types.Content(
622+
parts=[
623+
types.Part(
624+
function_response=types.FunctionResponse(
625+
name=functions.REQUEST_CONFIRMATION_FUNCTION_CALL_NAME,
626+
id=MOCK_CONFIRMATION_FUNCTION_CALL_ID,
627+
response={
628+
"response": user_confirmation.model_dump_json()
629+
},
630+
)
631+
)
632+
]
633+
),
634+
)
635+
)
636+
637+
events = []
638+
async for event in request_processor.run_async(
639+
invocation_context, LlmRequest()
640+
):
641+
events.append(event)
642+
643+
assert not events

0 commit comments

Comments
 (0)