diff --git a/python/packages/core/agent_framework/_tools.py b/python/packages/core/agent_framework/_tools.py index eb40fec7ee7..06d304e54b5 100644 --- a/python/packages/core/agent_framework/_tools.py +++ b/python/packages/core/agent_framework/_tools.py @@ -136,6 +136,7 @@ def _has_authoritative_approval_session(invocation_session: AgentSession | None) _APPROVED_ARGUMENTS_CONTEXT_KEY: Final[str] = "_approved_function_arguments" _SECURITY_ARGUMENTS_SNAPSHOT_CONTEXT_KEY: Final[str] = "_security_function_arguments" _PREPARED_ARGUMENTS_CONTEXT_KEY: Final[str] = "_prepared_function_arguments" +_MALFORMED_WRAPPER_CONTEXT_KEY: Final[str] = "_malformed_wrapper_provenance" _AUTO_ARGUMENT_PREPARATION_CONTEXT_KEY: Final[str] = "_auto_prepare_function_arguments" _FUNCTION_INVOCATION_LIMIT_FALLBACK_TEXT: Final[str] = ( "Function invocation limit reached before a final answer could be produced." @@ -1020,7 +1021,20 @@ def _prepare_context_arguments( context.arguments = prepared_state.arguments return prepared_state.arguments + # A malformed payload provenance marker keeps the synthetic raw entry + # alive across schema validation: validation drops the undeclared key, + # which would otherwise erase the signal that the call is still the + # wrapped malformed payload rather than a middleware repair. Presence + # is tracked separately from the value so a wrapped JSON null - a + # legitimate malformed payload - is not mistaken for no wrapper. + preserved_wrapper: dict[str, Any] = {} + malformed_wrapper = context.metadata.get(_MALFORMED_WRAPPER_CONTEXT_KEY) + if malformed_wrapper is not None and isinstance(current_arguments, Mapping) and "raw" in current_arguments: + preserved_wrapper["raw"] = current_arguments["raw"] + validated_arguments = self._prepare_arguments(arguments) + if preserved_wrapper: + validated_arguments = {**validated_arguments, **preserved_wrapper} context.arguments = validated_arguments context.metadata[_PREPARED_ARGUMENTS_CONTEXT_KEY] = _PreparedArgumentsState( arguments=validated_arguments, @@ -2164,6 +2178,40 @@ def _finalize_function_result( return function_result +def _tool_declares_parameter(tool: FunctionTool, name: str) -> bool: + """Return whether the tool's own schema declares a parameter with this name. + + Schema-supplied tools declare through their JSON ``properties``; decorated + tools through their resolved input model. A ``**kwargs`` catch-all does not + declare anything: the model never agreed to such a parameter by name. + """ + if tool._schema_supplied: # pyright: ignore[reportPrivateUsage] + properties = tool._input_schema_cached.get("properties") if tool._input_schema_cached else None # pyright: ignore[reportPrivateUsage] + return isinstance(properties, Mapping) and name in properties + return tool.input_model is not None and name in tool.input_model.model_fields + + +def _function_call_payload_is_not_an_object(function_call: Content) -> bool: + """Return whether the model sent a payload that is not a JSON object. + + `Content.parse_arguments()` wraps such a payload under a single "raw" key. + That is the right answer for a caller that wants to see what the model sent, + but not for a function call: the tool would run with an argument it never + declared, and the model would not learn that its call was malformed. The + wrapped mapping still reaches the function middleware, which can repair the + arguments the way it repairs any other invalid arguments; when it stays + unrepaired, the innermost handler answers the call instead of running it. + """ + arguments = function_call.arguments + if isinstance(arguments, str) and arguments: + try: + decoded = json.loads(arguments) + except (json.JSONDecodeError, TypeError): + decoded = None + return not isinstance(decoded, dict) + return False + + async def _auto_invoke_function( function_call_content: Content, custom_args: dict[str, Any] | None = None, @@ -2239,7 +2287,29 @@ async def _auto_invoke_function( approval_response = function_call_content function_call_content = approved_function_call - parsed_args: dict[str, Any] = dict(function_call_content.parse_arguments() or {}) + # A payload that is not a JSON object still reaches middleware as the raw + # parsed mapping it wraps, so middleware has the same chance to repair it + # that it has for any other invalid arguments. If it is still unrepaired, + # the innermost handler answers it before the tool runs. + payload_is_not_an_object = _function_call_payload_is_not_an_object(function_call_content) + parsed_args = dict(function_call_content.parse_arguments() or {}) + wrapped_raw_present = payload_is_not_an_object and set(parsed_args) == {"raw"} + + def _unrepaired_wrapper(arguments: Any) -> bool: + """Return whether the arguments are still the wrapped malformed payload. + + Provenance is structural, not value-based. The synthetic ``raw`` entry + the framework created for the malformed payload is the wrapper however + its contents are rewritten: hidden-reference expansion rebuilds the + wrapped value without repairing the structure, so value inequality + must not be read as a middleware repair. Keeping the ``raw`` entry - + which an argument-injecting middleware does - leaves the call + malformed, while arguments that no longer carry the entry - or a + ``raw`` entry the tool itself declares - are ordinary arguments. + """ + if not wrapped_raw_present or _tool_declares_parameter(tool, "raw"): + return False + return isinstance(arguments, Mapping) and "raw" in arguments # Filter out internal framework kwargs before passing to tools. # conversation_id is an internal tracking ID that should not be forwarded to tools. @@ -2278,6 +2348,15 @@ async def _auto_invoke_function( # No middleware - execute directly direct_context = None try: + if _unrepaired_wrapper(args): + # Without middleware there is nothing that could repair the + # wrapped payload, so answer it before the tool runs. A tool + # that declares ``raw`` receives the payload as ordinary + # arguments instead. + raise _FunctionArgumentValidationError( + f"Invalid arguments for '{tool.name}': arguments are not a JSON object", + redacted_message=f"Invalid arguments for '{tool.name}'.", + ) if getattr(tool, "_context_parameter_name", None): direct_context = FunctionInvocationContext( function=tool, @@ -2312,13 +2391,18 @@ async def _auto_invoke_function( return _function_execution_error_result(function_call_content, tool.name, exc, config, direct_context) # Execute through middleware pipeline if available arguments_prepared = False - try: - args = tool._prepare_arguments(args) # pyright: ignore[reportPrivateUsage] - arguments_prepared = True - except _FunctionArgumentValidationError: - # Invalid provider arguments are intentionally exposed to middleware so - # it has a supported opportunity to repair them before final validation. - pass + if not payload_is_not_an_object or _tool_declares_parameter(tool, "raw"): + try: + args = tool._prepare_arguments(args) # pyright: ignore[reportPrivateUsage] + arguments_prepared = True + except _FunctionArgumentValidationError: + # Invalid provider arguments are intentionally exposed to middleware so + # it has a supported opportunity to repair them before final validation. + pass + # Only a wrapped malformed payload that the tool does not accept as a + # declared ``raw`` argument skips preparation: preparation would silently + # drop that wrapper as an extra field, and middleware would never see what + # the model actually sent. middleware_context = FunctionInvocationContext( function=tool, @@ -2332,6 +2416,11 @@ async def _auto_invoke_function( if host_payload_budget is not None: middleware_context.metadata[_FUNCTION_RESULT_PAYLOAD_BUDGET_CONTEXT_KEY] = host_payload_budget middleware_context.metadata[_AUTO_ARGUMENT_PREPARATION_CONTEXT_KEY] = True + if wrapped_raw_present and not _tool_declares_parameter(tool, "raw"): + # A tool that declares raw keeps the value validation produced, so the + # wrapper marker must not turn a middleware-rewritten declared raw into + # an unrepaired malformed payload. + middleware_context.metadata[_MALFORMED_WRAPPER_CONTEXT_KEY] = dict(parsed_args) if arguments_prepared: middleware_context.metadata[_PREPARED_ARGUMENTS_CONTEXT_KEY] = _PreparedArgumentsState( arguments=args, @@ -2361,6 +2450,15 @@ async def _auto_invoke_function( async def final_function_handler(context_obj: Any) -> Any: nonlocal final_handler_started final_handler_started = True + if _unrepaired_wrapper(context_obj.arguments): + # Middleware left the wrapped payload in place - an argument-injecting + # middleware keeps its ``raw`` entry with the original value - so the + # arguments are still not arguments: answer it here, immediately + # before the tool runs. + raise _FunctionArgumentValidationError( + f"Invalid arguments for '{tool.name}': arguments are not a JSON object", + redacted_message=f"Invalid arguments for '{tool.name}'.", + ) return await tool.invoke( arguments=context_obj.arguments, context=context_obj, diff --git a/python/packages/core/tests/core/test_function_invocation_logic.py b/python/packages/core/tests/core/test_function_invocation_logic.py index f37f6a8848f..b0bb2828a27 100644 --- a/python/packages/core/tests/core/test_function_invocation_logic.py +++ b/python/packages/core/tests/core/test_function_invocation_logic.py @@ -971,6 +971,225 @@ def _finalize(stream_updates: Sequence[ChatResponseUpdate]) -> ChatResponse: chat_client_base._get_streaming_response = _get_streaming_response +@pytest.mark.parametrize("streaming", [False, True], ids=["non-streaming", "streaming"]) +@pytest.mark.parametrize("requires_approval", [False, True], ids=["immediate", "approved"]) +async def test_null_arguments_with_label_tracking_never_execute_the_tool( + chat_client_base: SupportsChatGetResponse, streaming: bool, requires_approval: bool +) -> None: + """A null wrapper survives normalization and approval resume without executing the tool.""" + from agent_framework.security import LabelTrackingFunctionMiddleware + + calls: list[int] = [] + + @tool(name="null_target", approval_mode="always_require" if requires_approval else "never_require") + def null_target(limit: int = 0) -> str: + calls.append(limit) + return "unexpected execution" + + function_call = Content.from_function_call("null-call", "null_target", arguments="null", id="null-occurrence") + if streaming: + chat_client_base.streaming_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute] + [ChatResponseUpdate(role="assistant", contents=[function_call])], + [ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")])], + ] + else: + chat_client_base.run_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute] + ChatResponse(messages=Message("assistant", [function_call])), + ChatResponse(messages=Message("assistant", ["done"])), + ] + + agent = Agent(client=chat_client_base, tools=[null_target], middleware=[LabelTrackingFunctionMiddleware()]) + session = agent.create_session() + + async def run(messages: str | list[Message]) -> list[Message]: + if streaming: + response_stream = agent.run(messages, session=session, stream=True) + updates = [update async for update in response_stream] + response = await response_stream.get_final_response() + assert [ + (content.call_id, content.result) + for update in updates + for content in update.contents + if content.type == "function_result" + ] == [ + (content.call_id, content.result) + for message in response.messages + for content in message.contents + if content.type == "function_result" + ] + else: + response = await agent.run(messages, session=session) + return response.messages + + messages = await run("go") + if requires_approval: + requests = [ + content + for message in messages + for content in message.contents + if content.type == "function_approval_request" + ] + assert len(requests) == 1 + assert calls == [] + session = AgentSession.from_dict(json.loads(json.dumps(session.to_dict()))) + messages = await run([Message("user", [requests[0].to_function_approval_response(approved=True)])]) + + assert calls == [] + results = [content for message in messages for content in message.contents if content.type == "function_result"] + assert len(results) == 1 + assert results[0].call_id == "null-call" + assert results[0].result == "Error: Argument parsing failed." + assert results[0].exception is not None + + +@pytest.mark.parametrize("streaming", [False, True], ids=["non-streaming", "streaming"]) +@pytest.mark.parametrize("requires_approval", [False, True], ids=["immediate", "approved"]) +async def test_hidden_reference_expansion_inside_a_wrapper_never_executes_the_tool( + chat_client_base: SupportsChatGetResponse, streaming: bool, requires_approval: bool +) -> None: + """A wrapper whose raw value is rewritten by hidden-reference expansion stays malformed. + + The label-tracking middleware expands a variable reference inside the + synthetic ``raw`` entry, which rebuilds the value without repairing the + malformed structure. Provenance must survive that expansion - through the + streaming and non-streaming loops and through a serialized approval resume + - so the permissive tool never runs with the synthetic argument. + """ + from agent_framework.security import ContentLabel, IntegrityLabel, LabelTrackingFunctionMiddleware + + calls: list[dict[str, Any]] = [] + + @tool(name="expansion_target", approval_mode="always_require" if requires_approval else "never_require") + def expansion_target(**kwargs: Any) -> str: + calls.append(kwargs) + return "unexpected execution" + + tracker = LabelTrackingFunctionMiddleware(auto_hide_untrusted=False) + + agent = Agent(client=chat_client_base, tools=[expansion_target], middleware=[tracker]) + session = agent.create_session() + # Seed the hidden variable in the session-bound security scope: the + # middleware resolves references through the scope of the live session. + variable_id = tracker.get_variable_store(session).store( + "expanded external content", + ContentLabel(integrity=IntegrityLabel.UNTRUSTED), + ) + function_call = Content.from_function_call( + "expansion-call", + "expansion_target", + arguments=f'["[{variable_id}]", "tail"]', + id="expansion-occurrence", + ) + if streaming: + chat_client_base.streaming_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute] + [ChatResponseUpdate(role="assistant", contents=[function_call])], + [ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")])], + ] + else: + chat_client_base.run_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute] + ChatResponse(messages=Message("assistant", [function_call])), + ChatResponse(messages=Message("assistant", ["done"])), + ] + + async def run(messages: str | list[Message]) -> list[Message]: + if streaming: + response_stream = agent.run(messages, session=session, stream=True) + updates = [update async for update in response_stream] + response = await response_stream.get_final_response() + assert [ + (content.call_id, content.result) + for update in updates + for content in update.contents + if content.type == "function_result" + ] == [ + (content.call_id, content.result) + for message in response.messages + for content in message.contents + if content.type == "function_result" + ] + else: + response = await agent.run(messages, session=session) + return response.messages + + messages = await run("go") + if requires_approval: + requests = [ + content + for message in messages + for content in message.contents + if content.type == "function_approval_request" + ] + assert len(requests) == 1 + assert calls == [] + session = AgentSession.from_dict(json.loads(json.dumps(session.to_dict()))) + # The variable must survive the session round-trip, otherwise the + # resumed replay would keep the original value and could not exercise + # the expansion bypass this regression guards against. + assert tracker.get_variable_store(session).exists(variable_id) + messages = await run([Message("user", [requests[0].to_function_approval_response(approved=True)])]) + + assert calls == [] + results = [content for message in messages for content in message.contents if content.type == "function_result"] + assert len(results) == 1 + assert results[0].call_id == "expansion-call" + assert "Invalid arguments for 'expansion_target'" in str(results[0].exception) + + +@pytest.mark.parametrize("streaming", [False, True], ids=["non-streaming", "streaming"]) +@pytest.mark.parametrize("object_payload", [False, True], ids=["scalar", "object"]) +async def test_declared_raw_rewritten_by_middleware_reaches_the_tool_with_its_declared_type( + chat_client_base: SupportsChatGetResponse, streaming: bool, object_payload: bool +) -> None: + """Both payload forms retain ordinary type preparation after middleware rewrites a value.""" + observed_values: list[datetime] = [] + + class RewriteRaw(FunctionMiddleware): + async def process(self, context: FunctionInvocationContext, call_next: Callable[[], Awaitable[None]]) -> None: + context.arguments = {"raw": "2025-02-03T00:00:00+00:00"} + await call_next() + + @tool(name="raw_target", approval_mode="never_require") + def raw_target(raw: datetime) -> str: + observed_values.append(raw) + return raw.isoformat() + + arguments = '{"raw":"2024-01-01T00:00:00+00:00"}' if object_payload else '"2024-01-01T00:00:00+00:00"' + function_call = Content.from_function_call("raw-call", "raw_target", arguments=arguments) + if streaming: + chat_client_base.streaming_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute] + [ChatResponseUpdate(role="assistant", contents=[function_call])], + [ChatResponseUpdate(role="assistant", contents=[Content.from_text("done")])], + ] + else: + chat_client_base.run_responses = [ # type: ignore[attr-defined] # ty: ignore[unresolved-attribute] + ChatResponse(messages=Message("assistant", [function_call])), + ChatResponse(messages=Message("assistant", ["done"])), + ] + agent = Agent(client=chat_client_base, tools=[raw_target], middleware=[RewriteRaw()]) + if streaming: + response_stream = agent.run("go", stream=True) + updates = [update async for update in response_stream] + response = await response_stream.get_final_response() + assert [ + content.result for update in updates for content in update.contents if content.type == "function_result" + ] == [ + content.result + for message in response.messages + for content in message.contents + if content.type == "function_result" + ] + else: + response = await agent.run("go") + + assert observed_values == [datetime(2025, 2, 3, tzinfo=timezone.utc)] + results = [ + content for message in response.messages for content in message.contents if content.type == "function_result" + ] + assert len(results) == 1 + assert results[0].exception is None + assert results[0].result == "2025-02-03T00:00:00+00:00" + + async def test_base_client_with_function_calling(chat_client_base: SupportsChatGetResponse): exec_counter = 0 diff --git a/python/packages/core/tests/core/test_tools.py b/python/packages/core/tests/core/test_tools.py index 8dd1da6f053..172c6a4e533 100644 --- a/python/packages/core/tests/core/test_tools.py +++ b/python/packages/core/tests/core/test_tools.py @@ -3,9 +3,10 @@ import copy import logging import threading +from collections.abc import Awaitable, Callable from contextvars import ContextVar from datetime import date, datetime -from typing import Annotated, Any, Literal, get_args, get_origin +from typing import Annotated, Any, Literal, cast, get_args, get_origin from unittest.mock import Mock import pytest @@ -20,7 +21,11 @@ FunctionTool, tool, ) -from agent_framework._middleware import FunctionInvocationContext +from agent_framework._middleware import ( + FunctionInvocationContext, + FunctionMiddleware, + FunctionMiddlewarePipeline, +) from agent_framework._tools import ( _auto_invoke_function, _format_tool_parameters, @@ -431,6 +436,291 @@ def search(query: str) -> str: await search.invoke(arguments={}) +@pytest.mark.parametrize("arguments", ["not {json", "[1, 2]", '"text"', " "]) +async def test_function_call_arguments_that_are_not_a_json_object_are_answered_with_an_error( + arguments: str, +) -> None: + """A payload that is not a JSON object is not arguments, and must not be called as one. + + Wrapping it under "raw" would call the tool with an argument it never + declared, and the model would not learn that its call was malformed. + """ + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments=arguments), + ], + tools=[account_tool], + config={}, + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "not a JSON object" in str(result.exception) + + +async def test_function_call_with_object_arguments_still_runs_the_tool() -> None: + """A well-formed call is unaffected by the guard.""" + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments='{"limit": 5}'), + ], + tools=[account_tool], + config={}, + ) + + assert not should_terminate + assert calls == [{"limit": 5}] + assert result_groups[0][0].result == "ok" + + +async def test_malformed_arguments_reach_approval_like_any_other_call() -> None: + """A malformed call is not answered before approval. + + Middleware may still repair the arguments once the call is approved, so the + request goes to approval the way every other call does; the innermost + handler answers the call only if the payload is still unrepaired there. + """ + calls: list[dict[str, Any]] = [] + + @tool(name="guarded_write", approval_mode="always_require") + def guarded_write(value: str = "") -> str: + calls.append({"value": value}) + return "ok" + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="guarded_write", arguments="not {json"), + ], + tools=[guarded_write], + config={}, + ) + + assert not should_terminate + assert calls == [] + assert len(result_groups) == 1 + assert result_groups[0][0].type == "function_approval_request" + + +async def test_middleware_can_repair_a_payload_that_is_not_a_json_object() -> None: + """Middleware sees the raw parsed mapping and may repair it into real arguments. + + This is the function-loop contract: middleware receives the arguments before + any final validation, and a repaired call executes like any other. + """ + + calls: list[dict[str, Any]] = [] + + @tool(name="list_accounts") + def list_accounts(limit: int = 0) -> str: + calls.append({"limit": limit}) + return "ok" + + class RepairRawArguments(FunctionMiddleware): + async def process( + self, + context: FunctionInvocationContext, + call_next: Callable[[], Awaitable[None]], + ) -> None: + arguments = cast("dict[str, Any]", context.arguments) + if "raw" in arguments: + context.arguments = {"limit": 5} + await call_next() + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments="[1, 2]"), + ], + tools=[list_accounts], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(RepairRawArguments()), + ) + + assert not should_terminate + assert calls == [{"limit": 5}] + assert result_groups[0][0].result == "ok" + + +@pytest.mark.parametrize("arguments", ["not {json", "[1, 2]"]) +async def test_malformed_arguments_left_unrepaired_are_answered_before_the_tool_runs( + arguments: str, +) -> None: + """When middleware does not repair the wrapped payload, the call is answered. + + The error is the argument-validation result, raised immediately before the + tool would have executed, so the tool never runs on undeclared arguments. + """ + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + class Passthrough(FunctionMiddleware): + async def process( + self, + context: FunctionInvocationContext, + call_next: Callable[[], Awaitable[None]], + ) -> None: + await call_next() + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments=arguments), + ], + tools=[account_tool], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(Passthrough()), + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "not a JSON object" in str(result.exception) + + +class _InjectUser(FunctionMiddleware): + """Middleware that appends a key to every call's arguments.""" + + async def process( + self, + context: FunctionInvocationContext, + call_next: Callable[[], Awaitable[None]], + ) -> None: + context.arguments = {**cast("dict[str, Any]", context.arguments), "user": "u1"} + await call_next() + + +async def test_injecting_middleware_does_not_make_a_malformed_payload_count_as_repaired() -> None: + """A middleware that appends a key must not un-mark the wrapped payload. + + The injection preserves the original ``raw`` entry, so the arguments are + still the wrapper plus an undeclared key - the tool must not run. + """ + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments="[1, 2]"), + ], + tools=[account_tool], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(_InjectUser()), + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "not a JSON object" in str(result.exception) + + +async def test_injecting_middleware_does_not_silence_a_malformed_payload_for_a_typed_tool() -> None: + """The same holds for a typed tool: the wrapper must not reach it either. + + The input model would silently drop the undeclared ``raw`` key, so the tool + would run on injected values alone without the model ever learning that its + call was malformed. + """ + calls: list[dict[str, Any]] = [] + + @tool(name="typed_target") + def typed_target(user: str = "", limit: int = 0) -> str: + calls.append({"user": user, "limit": limit}) + return "ok" + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="typed_target", arguments="[1, 2]"), + ], + tools=[typed_target], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(_InjectUser()), + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "not a JSON object" in str(result.exception) + + +async def test_a_tool_that_declares_raw_receives_a_non_object_payload_as_arguments() -> None: + """A tool that declares ``raw`` gets the wrapped payload as ordinary arguments. + + For such a tool the wrapper coincides with a valid call, which is the + behaviour the loop had before the malformed-argument guard existed. + """ + calls: list[dict[str, Any]] = [] + + @tool(name="raw_writer") + def raw_writer(raw: str = "") -> str: + calls.append({"raw": raw}) + return "ok" + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="raw_writer", arguments="not {json"), + ], + tools=[raw_writer], + config={}, + ) + + assert not should_terminate + assert calls == [{"raw": "not {json"}] + assert result_groups[0][0].result == "ok" + + async def test_invoke_preserves_explicit_null_argument(): """A required nullable argument the model sets to null must reach the function. @@ -2168,3 +2458,255 @@ def test_skip_parsing_is_singleton() -> None: # endregion + + +@pytest.mark.parametrize("arguments", ["null", "NaN"]) +async def test_malformed_null_and_nan_payloads_are_answered_without_middleware(arguments: str) -> None: + """A wrapper whose raw entry is None or NaN is still a wrapper. + + ``parse_arguments()`` wraps ``"null"`` as ``{"raw": None}`` and ``"NaN"`` + as ``{"raw": }``; neither the absence of a value nor the NaN + inequality may un-mark the wrapper. + """ + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments=arguments), + ], + tools=[account_tool], + config={}, + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "not a JSON object" in str(result.exception) + + +@pytest.mark.parametrize("arguments", ["null", "NaN"]) +async def test_injecting_middleware_does_not_unmark_null_or_nan_wrappers(arguments: str) -> None: + """An injecting middleware keeps the raw entry, so the call stays malformed. + + The injected key changes the mapping but the wrapper survives with its + original None/NaN value, and the wrapper-presence check is value-safe for + both. + """ + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments=arguments), + ], + tools=[account_tool], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(_InjectUser()), + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "not a JSON object" in str(result.exception) + + +class _ReplaceWithValidArguments(FunctionMiddleware): + """Middleware that replaces the arguments with a valid mapping. + + The replacement drops any raw wrapper and supplies declared arguments, + which is a full repair. + """ + + async def process( + self, + context: FunctionInvocationContext, + call_next: Callable[[], Awaitable[None]], + ) -> None: + context.arguments = {"limit": 5} + await call_next() + + +async def test_middleware_that_replaces_null_wrapper_with_valid_arguments_repairs_the_call() -> None: + """A full-argument replacement is a repair, not an unrepaired wrapper. + + The replacement no longer contains the ``raw`` entry, so the + ``None``-valued wrapper of ``"null"`` must not be misread from the + absent key, and the repaired call executes. + """ + calls: list[dict[str, Any]] = [] + + @tool(name="list_accounts") + def list_accounts(limit: int = 0) -> str: + calls.append({"limit": limit}) + return "ok" + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="list_accounts", arguments="null"), + ], + tools=[list_accounts], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(_ReplaceWithValidArguments()), + ) + + assert not should_terminate + assert calls == [{"limit": 5}] + assert result_groups[0][0].result == "ok" + + +@pytest.mark.parametrize("arguments", ["not {json", "[1, 2]", "null", "NaN", "false", "0", '""', " "]) +async def test_builtin_label_tracking_middleware_preserves_malformed_wrapper_provenance(arguments: str) -> None: + """The built-in label-tracking middleware must not erase the wrapper. + + Its argument normalization validates against the declared model and drops + the undeclared ``raw`` entry for a typed tool; the provenance marker keeps + the malformed payload rejected instead of executing with defaults. + """ + from agent_framework.security import LabelTrackingFunctionMiddleware + + calls: list[dict[str, Any]] = [] + + @tool(name="typed_label_target") + def typed_label_target(user: str = "", limit: int = 0) -> str: + calls.append({"user": user, "limit": limit}) + return "ok" + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call(call_id="call-1", name="typed_label_target", arguments=arguments), + ], + tools=[typed_label_target], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(LabelTrackingFunctionMiddleware()), + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert result.type == "function_result" + assert "Invalid arguments for 'typed_label_target'" in str(result.exception) + + +async def test_hidden_reference_expansion_inside_a_wrapper_never_executes_a_permissive_tool() -> None: + """Expanding a hidden reference inside the wrapper is not a repair. + + The label-tracking middleware rebuilds the wrapped value when it expands a + variable reference inside the synthetic ``raw`` entry. The wrapper is still + the wrapped malformed payload - only its value changed - so provenance has + to survive the expansion: a permissive tool must not run with the synthetic + argument just because the value is no longer the one that was parsed. + """ + from agent_framework.security import ContentLabel, IntegrityLabel, LabelTrackingFunctionMiddleware + + calls: list[dict[str, Any]] = [] + + async def list_accounts(**kwargs: Any) -> str: + calls.append(kwargs) + return "ok" + + account_tool = FunctionTool( + name="list_accounts", + description="List the accounts", + func=list_accounts, + input_model={"type": "object", "properties": {}}, + ) + + tracker = LabelTrackingFunctionMiddleware(auto_hide_untrusted=False) + variable_id = tracker.get_variable_store().store( + "expanded external content", + ContentLabel(integrity=IntegrityLabel.UNTRUSTED), + ) + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call( + call_id="call-1", + name="list_accounts", + arguments=f'["[{variable_id}]", "tail"]', + ), + ], + tools=[account_tool], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(tracker), + ) + + assert not should_terminate + assert calls == [] + result = result_groups[0][0] + assert "Invalid arguments for 'list_accounts'" in str(result.exception) + + +@tool(name="raw_reader", description="Read a raw value") +def raw_reader_tool(raw: datetime) -> str: + return f"raw:{raw.isoformat()}" + + +@pytest.mark.parametrize( + "arguments", ['"2024-01-01T00:00:00+00:00"', '{"raw":"2024-01-01T00:00:00+00:00"}'], ids=["scalar", "object"] +) +@pytest.mark.parametrize("rewrite_raw", [False, True], ids=["unchanged", "rewritten"]) +async def test_declared_raw_tool_keeps_normal_preparation_types(arguments: str, rewrite_raw: bool) -> None: + """A declared ``raw: datetime`` reaches middleware as a datetime, not a string. + + Preparation runs normally for tools that declare ``raw``, so middleware + sees the declared types instead of the raw JSON string. + """ + seen_types: list[str] = [] + + class TrackTypes(FunctionMiddleware): + async def process( + self, + context: FunctionInvocationContext, + call_next: Callable[[], Awaitable[None]], + ) -> None: + raw_value = cast("dict[str, Any]", context.arguments).get("raw") + seen_types.append(type(raw_value).__name__) + if rewrite_raw: + context.arguments = {"raw": "2025-02-03T00:00:00+00:00"} + await call_next() + + result_groups, should_terminate = await _try_execute_function_call_groups( + custom_args={}, + function_calls=[ + Content.from_function_call( + call_id="call-1", + name="raw_reader", + arguments=arguments, + ), + ], + tools=[raw_reader_tool], + config={}, + middleware_pipeline=FunctionMiddlewarePipeline(TrackTypes()), + ) + + assert not should_terminate + assert seen_types == ["datetime"] + expected_timestamp = "2025-02-03T00:00:00+00:00" if rewrite_raw else "2024-01-01T00:00:00+00:00" + assert result_groups[0][0].exception is None + assert result_groups[0][0].result == f"raw:{expected_timestamp}"