|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import json |
| 16 | +from unittest.mock import Mock |
16 | 17 | from unittest.mock import patch |
17 | 18 |
|
| 19 | +from a2a.server.agent_execution import RequestContext |
| 20 | +from google.adk.a2a.converters.request_converter import convert_a2a_request_to_agent_run_request |
18 | 21 | from google.adk.agents.llm_agent import LlmAgent |
19 | 22 | from google.adk.events.event import Event |
20 | 23 | from google.adk.events.event_actions import EventActions |
@@ -547,3 +550,94 @@ async def test_request_confirmation_processor_dynamic_success(): |
547 | 550 | assert ( |
548 | 551 | args[4][MOCK_FUNCTION_CALL_ID] == user_confirmation |
549 | 552 | ) # 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