|
18 | 18 |
|
19 | 19 | from unittest import mock |
20 | 20 |
|
| 21 | +from google.adk.agents._caller_principal import CallerPrincipal |
21 | 22 | from google.adk.agents.base_agent import BaseAgent |
22 | 23 | from google.adk.agents.llm_agent import LlmAgent |
23 | 24 | from google.adk.apps import App |
24 | 25 | from google.adk.events._interrupts import extract_event_interrupt_ids |
| 26 | +from google.adk.events.event import Event |
25 | 27 | from google.adk.runners import Runner |
26 | 28 | from google.adk.sessions.in_memory_session_service import InMemorySessionService |
27 | 29 | from google.adk.tools.base_tool import BaseTool |
@@ -576,6 +578,124 @@ def after_node(node_input): |
576 | 578 | ) |
577 | 579 |
|
578 | 580 |
|
| 581 | +class _ConfirmedFunctionNodeWorkflow: |
| 582 | + """A Workflow with one `@node(require_confirmation=True)` node. |
| 583 | +
|
| 584 | + Mirrors `_ConfirmationWorkflow` in test_tool_node.py for the FunctionNode |
| 585 | + confirmation path, which stores the resumed answer the same way. |
| 586 | + """ |
| 587 | + |
| 588 | + def __init__(self) -> None: |
| 589 | + from google.adk.apps.app import ResumabilityConfig |
| 590 | + |
| 591 | + self.calls: list[str] = [] |
| 592 | + self.downstream_seen: list[object] = [] |
| 593 | + |
| 594 | + class RemoveResult(BaseModel): |
| 595 | + removed: bool |
| 596 | + |
| 597 | + @node(require_confirmation=True) |
| 598 | + def remove_file() -> RemoveResult: |
| 599 | + self.calls.append("removed") |
| 600 | + return RemoveResult(removed=True) |
| 601 | + |
| 602 | + def after_node(node_input): |
| 603 | + self.downstream_seen.append(node_input) |
| 604 | + return node_input |
| 605 | + |
| 606 | + wf = Workflow( |
| 607 | + name="confirm_wf", |
| 608 | + edges=[(START, remove_file), (remove_file, after_node)], |
| 609 | + ) |
| 610 | + self.runner = testing_utils.InMemoryRunner( |
| 611 | + app=App( |
| 612 | + name="test_confirm_wf", |
| 613 | + root_agent=wf, |
| 614 | + resumability_config=ResumabilityConfig(is_resumable=True), |
| 615 | + ) |
| 616 | + ) |
| 617 | + |
| 618 | + async def start(self) -> Event: |
| 619 | + """Runs the workflow and returns its confirmation request.""" |
| 620 | + from google.adk.utils._function_call_names import REQUEST_INPUT_FUNCTION_CALL_NAME |
| 621 | + |
| 622 | + from . import workflow_testing_utils |
| 623 | + |
| 624 | + events = await self.runner.run_async("start") |
| 625 | + request = workflow_testing_utils.find_function_call_event( |
| 626 | + events, REQUEST_INPUT_FUNCTION_CALL_NAME |
| 627 | + ) |
| 628 | + assert request is not None |
| 629 | + assert not self.calls |
| 630 | + return request |
| 631 | + |
| 632 | + async def answer( |
| 633 | + self, request: Event, *, caller_principal: CallerPrincipal |
| 634 | + ) -> None: |
| 635 | + """Approves the request on an invocation a serving layer attributed. |
| 636 | +
|
| 637 | + `caller_principal` stands in for what the serving layer would set on the |
| 638 | + invocation that carries the answer. |
| 639 | + """ |
| 640 | + from google.adk.workflow.utils._workflow_hitl_utils import create_request_input_response |
| 641 | + |
| 642 | + [interrupt_id] = extract_event_interrupt_ids(request) |
| 643 | + session = self.runner.session |
| 644 | + async for _ in self.runner.runner.run_async( |
| 645 | + user_id=session.user_id, |
| 646 | + session_id=session.id, |
| 647 | + invocation_id=request.invocation_id, |
| 648 | + new_message=testing_utils.UserContent( |
| 649 | + create_request_input_response(interrupt_id, {"confirmed": True}) |
| 650 | + ), |
| 651 | + caller_principal=caller_principal, |
| 652 | + ): |
| 653 | + pass |
| 654 | + |
| 655 | + |
| 656 | +@pytest.mark.asyncio |
| 657 | +async def test_node_require_confirmation_from_unauthenticated_caller_refused_in_strict_mode( |
| 658 | + monkeypatch, |
| 659 | +): |
| 660 | + """An approval the serving layer could not attribute is stored as a rejection. |
| 661 | +
|
| 662 | + Same decision as the agent confirmation flow and the tool node: with strict |
| 663 | + mode on, a resume that arrives with an unauthenticated principal must not |
| 664 | + run the function, and the node answers with the rejection output. |
| 665 | + """ |
| 666 | + monkeypatch.setenv("ADK_ENABLE_STRICT_CALLER_PRINCIPAL", "1") |
| 667 | + wf = _ConfirmedFunctionNodeWorkflow() |
| 668 | + request = await wf.start() |
| 669 | + |
| 670 | + await wf.answer( |
| 671 | + request, |
| 672 | + caller_principal=CallerPrincipal(authenticated=False, source="a2a"), |
| 673 | + ) |
| 674 | + |
| 675 | + assert not wf.calls |
| 676 | + assert wf.downstream_seen == [{"error": "This tool call is rejected."}] |
| 677 | + |
| 678 | + |
| 679 | +@pytest.mark.asyncio |
| 680 | +async def test_node_require_confirmation_from_authenticated_caller_runs_function( |
| 681 | + monkeypatch, |
| 682 | +): |
| 683 | + """An approval from a caller the serving layer verified is honored.""" |
| 684 | + monkeypatch.setenv("ADK_ENABLE_STRICT_CALLER_PRINCIPAL", "1") |
| 685 | + wf = _ConfirmedFunctionNodeWorkflow() |
| 686 | + request = await wf.start() |
| 687 | + |
| 688 | + await wf.answer( |
| 689 | + request, |
| 690 | + caller_principal=CallerPrincipal( |
| 691 | + authenticated=True, user_name="operator", source="a2a" |
| 692 | + ), |
| 693 | + ) |
| 694 | + |
| 695 | + assert wf.calls == ["removed"] |
| 696 | + assert wf.downstream_seen == [{"removed": True}] |
| 697 | + |
| 698 | + |
579 | 699 | @pytest.mark.parametrize( |
580 | 700 | "target_path,should_pause", [("/dev/tmp", False), ("/prod/db", True)] |
581 | 701 | ) |
|
0 commit comments