diff --git a/python/packages/core/agent_framework/security.py b/python/packages/core/agent_framework/security.py index 8665a6de35a..396f83bd3d3 100644 --- a/python/packages/core/agent_framework/security.py +++ b/python/packages/core/agent_framework/security.py @@ -4533,7 +4533,9 @@ async def before_run( if configured_labeler is not None: raw_providers = getattr(agent, "context_providers", None) if not isinstance(raw_providers, Sequence): - raise ValueError("SecureAgentConfig with message_labeler requires an ordered context provider sequence.") + raise ValueError( + "SecureAgentConfig with message_labeler requires an ordered context provider sequence." + ) providers = cast(Sequence[Any], raw_providers) try: config_index = next(index for index, provider in enumerate(providers) if provider is self) @@ -4542,7 +4544,9 @@ async def before_run( "SecureAgentConfig with message_labeler must be registered as a context provider." ) from exc if any(not isinstance(provider, HistoryProvider) for provider in providers[config_index + 1 :]): - raise ValueError("SecureAgentConfig with message_labeler must follow every non-history context provider.") + raise ValueError( + "SecureAgentConfig with message_labeler must follow every non-history context provider." + ) provider_source_ids = set(context.context_messages) message_labels: dict[int, tuple[Message, ContentLabel]] = {} diff --git a/python/packages/declarative/agent_framework_declarative/_loader.py b/python/packages/declarative/agent_framework_declarative/_loader.py index 5009e8abbde..98f9d2736c4 100644 --- a/python/packages/declarative/agent_framework_declarative/_loader.py +++ b/python/packages/declarative/agent_framework_declarative/_loader.py @@ -5,11 +5,12 @@ import asyncio from collections.abc import Callable, Mapping from pathlib import Path -from typing import Any, TypedDict, cast +from typing import Any, Literal, TypedDict, cast import yaml from agent_framework import ( Agent, + Content, SupportsChatGetResponse, ) from agent_framework import ( @@ -757,11 +758,34 @@ def _parse_tool(self, tool_resource: Tool) -> AFFunctionTool | dict[str, Any]: for binding in tool_resource.bindings: if binding.name and (func := self.bindings.get(binding.name)): break + registered_tool = func if isinstance(func, AFFunctionTool) else None + approval_mode: Literal["always_require", "never_require"] | None = None + if registered_tool: + if registered_tool.approval_mode == "always_require": + approval_mode = "always_require" + elif registered_tool.approval_mode == "never_require": + approval_mode = "never_require" + result_parser = registered_tool.result_parser if registered_tool else None + if registered_tool is not None and callable(result_parser): + registered_result_parser = result_parser + + def preserve_parser_exception_count(result: Any) -> str | list[Content]: + try: + return registered_result_parser(result) + except Exception: + registered_tool.invocation_exception_count += 1 + raise + + result_parser = preserve_parser_exception_count return AFFunctionTool( name=tool_resource.name, # type: ignore description=tool_resource.description, # type: ignore input_model=tool_resource.parameters.to_json_schema() if tool_resource.parameters else None, func=func, + approval_mode=approval_mode, + kind=registered_tool.kind if registered_tool else None, + additional_properties=registered_tool.additional_properties if registered_tool else None, + result_parser=result_parser, ) case WebSearchTool(): result: dict[str, Any] = {"type": "web_search_preview"} diff --git a/python/packages/declarative/tests/conftest.py b/python/packages/declarative/tests/conftest.py index d386789879a..a315379b146 100644 --- a/python/packages/declarative/tests/conftest.py +++ b/python/packages/declarative/tests/conftest.py @@ -5,12 +5,21 @@ from __future__ import annotations import sys -from collections.abc import Callable +from collections.abc import Awaitable, Callable, Mapping, Sequence from typing import TYPE_CHECKING, Any from unittest.mock import AsyncMock, MagicMock import pytest -from agent_framework import _feature_stage +from agent_framework import ( + BaseChatClient, + ChatOptions, + ChatResponse, + ChatResponseUpdate, + FunctionInvocationLayer, + Message, + ResponseStream, + _feature_stage, +) if TYPE_CHECKING: import httpx2 as httpx @@ -21,6 +30,36 @@ collect_ignore_glob = ["test_*.py"] +class _FunctionCallingTestClient(FunctionInvocationLayer[ChatOptions], BaseChatClient[ChatOptions]): + """Minimal client that exercises the real function-invocation layer.""" + + def __init__(self) -> None: + super().__init__() + self.responses: list[ChatResponse] = [] + + def _inner_get_response( + self, + *, + messages: Sequence[Message], + stream: bool, + options: Mapping[str, Any], + **kwargs: Any, + ) -> Awaitable[ChatResponse] | ResponseStream[ChatResponseUpdate, ChatResponse]: + if stream: + raise AssertionError("Streaming is not supported by this test client.") + + async def get_response() -> ChatResponse: + return self.responses.pop(0) + + return get_response() + + +@pytest.fixture +def function_call_client() -> _FunctionCallingTestClient: + """Create a client backed by the framework function-invocation layer.""" + return _FunctionCallingTestClient() + + @pytest.fixture def fresh_feature_warnings(monkeypatch: pytest.MonkeyPatch) -> None: """Isolate once-per-feature warnings from other tests.""" diff --git a/python/packages/declarative/tests/test_declarative_loader.py b/python/packages/declarative/tests/test_declarative_loader.py index 423d3df6376..76c1c8ffb38 100644 --- a/python/packages/declarative/tests/test_declarative_loader.py +++ b/python/packages/declarative/tests/test_declarative_loader.py @@ -19,6 +19,7 @@ AnonymousConnection, ApiKeyConnection, ArrayProperty, + Binding, CodeInterpreterTool, Connection, CustomTool, @@ -1552,6 +1553,166 @@ def my_function(): tools = agent.default_options.get("tools", []) assert len(tools) == 1 + @pytest.mark.parametrize("safe_mode", [True, False]) + async def test_parse_registered_function_tool_preserves_policy( + self, function_call_client: Any, safe_mode: bool + ) -> None: + """Registered tool policy remains authoritative after declarative wrapping.""" + from agent_framework import ChatResponse, Content, Message + from agent_framework import FunctionTool as AFFunctionTool + + from agent_framework_declarative import AgentFactory + + executions: list[str] = [] + + def sensitive_action() -> str: + executions.append("executed") + return "done" + + def parse_result(result: Any) -> str: + return f"parsed: {result}" + + registered_tool = AFFunctionTool( + name="registered_action", + description="Registered description", + func=sensitive_action, + approval_mode="always_require", + kind="sensitive", + max_invocations=1, + max_invocation_exceptions=1, + additional_properties={"security": {"integrity": "trusted"}}, + result_parser=parse_result, + ) + factory = AgentFactory(bindings={"registered_action": registered_tool}, safe_mode=safe_mode) + wrapped_tool = factory._parse_tool( + FunctionTool( + name="declarative_action", + description="Declarative description", + bindings=[Binding(name="registered_action")], + ) + ) + + assert isinstance(wrapped_tool, AFFunctionTool) + assert wrapped_tool.name == "declarative_action" + assert wrapped_tool.description == "Declarative description" + assert wrapped_tool.func is registered_tool + assert wrapped_tool.approval_mode == "always_require" + assert wrapped_tool.kind == registered_tool.kind + assert wrapped_tool.additional_properties == registered_tool.additional_properties + assert wrapped_tool.result_parser is not None + assert wrapped_tool.max_invocations is None + assert wrapped_tool.max_invocation_exceptions is None + + function_call_client.responses.append( + ChatResponse( + messages=Message( + role="assistant", + contents=[ + Content.from_function_call( + call_id="call-1", + name="declarative_action", + arguments={}, + ) + ], + ) + ) + ) + response = await function_call_client.get_response( + [Message(role="user", contents=["Run the sensitive action."])], + options={"tools": [wrapped_tool]}, + ) + + assert executions == [] + approval_requests = [ + content + for message in response.messages + for content in message.contents + if content.type == "function_approval_request" + ] + assert len(approval_requests) == 1 + + def test_parse_registered_function_tool_keeps_invocation_limit(self) -> None: + """The original registered tool remains the invocation-limit authority.""" + from agent_framework import FunctionTool as AFFunctionTool + from agent_framework.exceptions import ToolException + + from agent_framework_declarative import AgentFactory + + registered_tool = AFFunctionTool(name="registered_action", func=lambda: "done", max_invocations=1) + wrapped_tool = AgentFactory(bindings={"registered_action": registered_tool})._parse_tool( + FunctionTool(name="declarative_action", bindings=[Binding(name="registered_action")]) + ) + + assert isinstance(wrapped_tool, AFFunctionTool) + assert wrapped_tool() == "done" + with pytest.raises(ToolException, match="maximum invocation limit"): + wrapped_tool() + + assert registered_tool.invocation_count == 1 + assert wrapped_tool.max_invocations is None + + def test_parse_registered_function_tool_keeps_exception_limit(self) -> None: + """The original registered tool remains the exception-limit authority.""" + from agent_framework import FunctionTool as AFFunctionTool + from agent_framework.exceptions import ToolException + + from agent_framework_declarative import AgentFactory + + def fail() -> None: + raise RuntimeError("boom") + + registered_tool = AFFunctionTool(name="registered_action", func=fail, max_invocation_exceptions=1) + wrapped_tool = AgentFactory(bindings={"registered_action": registered_tool})._parse_tool( + FunctionTool(name="declarative_action", bindings=[Binding(name="registered_action")]) + ) + + assert isinstance(wrapped_tool, AFFunctionTool) + with pytest.raises(RuntimeError, match="boom"): + wrapped_tool() + with pytest.raises(ToolException, match="maximum exception limit"): + wrapped_tool() + + assert registered_tool.invocation_exception_count == 1 + assert wrapped_tool.max_invocation_exceptions is None + + async def test_parse_registered_function_tool_keeps_parser_exception_limit(self) -> None: + """Parser failures consume the original registered tool's exception limit.""" + from agent_framework import FunctionTool as AFFunctionTool + from agent_framework.exceptions import ToolException + + from agent_framework_declarative import AgentFactory + + executions: list[str] = [] + parser_calls: list[str] = [] + + def execute() -> str: + executions.append("executed") + return "done" + + def fail_parser(result: Any) -> str: + parser_calls.append(result) + raise ValueError("parser failed") + + registered_tool = AFFunctionTool( + name="registered_action", + func=execute, + max_invocation_exceptions=1, + result_parser=fail_parser, + ) + wrapped_tool = AgentFactory(bindings={"registered_action": registered_tool})._parse_tool( + FunctionTool(name="declarative_action", bindings=[Binding(name="registered_action")]) + ) + + assert isinstance(wrapped_tool, AFFunctionTool) + with pytest.raises(ValueError, match="parser failed"): + await wrapped_tool.invoke() + with pytest.raises(ToolException, match="maximum exception limit"): + await wrapped_tool.invoke() + + assert executions == ["executed"] + assert parser_calls == ["done"] + assert registered_tool.invocation_exception_count == 1 + def test_parse_file_search_tool_with_all_options(self): """Test parsing FileSearchTool with ranker and filters.""" from unittest.mock import MagicMock