Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

59.6 KB
Raw Blame
"""
Unit tests for Tool Permission Guardrail (OpenAI tool_calls semantics)
"""

import json
import logging
import re
from typing import Final
from unittest.mock import patch

import pytest

from litellm.caching.dual_cache import DualCache


from fastapi import HTTPException

from litellm.exceptions import GuardrailRaisedException
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.guardrails.guardrail_hooks.tool_permission import (
    ToolPermissionGuardrail,
)
from litellm.types.guardrails import GuardrailEventHooks, LitellmParams
from litellm.types.proxy.guardrails.guardrail_hooks.tool_permission import (
    PermissionError,
)
from litellm.types.utils import (
    ChatCompletionMessageToolCall,
    Choices,
    ModelResponse,
    ModelResponseStream,
)


class TestToolPermissionGuardrail:
    """Test class for Tool Permission Guardrail functionality"""

    def setup_method(self):
        """Set up test fixtures"""
        self.test_rules = [
            {"id": "allow_bash", "tool_name": r"^Bash$", "decision": "allow"},
            {
                "id": "allow_github",
                "tool_name": r"^mcp__github_.*$",
                "decision": "allow",
            },
            {
                "id": "allow_documentation",
                "tool_name": r"^mcp__aws-documentation_.*_documentation$",
                "decision": "allow",
            },
            {"id": "deny_read", "tool_name": r"^Read$", "decision": "deny"},
            {"id": "deny_get", "tool_name": r".*_get$", "decision": "deny"},
        ]

        self.guardrail = ToolPermissionGuardrail(
            guardrail_name="test-tool-permission",
            rules=self.test_rules,
            default_action="deny",
            on_disallowed_action="block",
        )

    def test_initialization(self):
        """Test guardrail initialization"""
        assert self.guardrail.guardrail_name == "test-tool-permission"
        assert len(self.guardrail.rules) == 5
        assert self.guardrail.default_action == "deny"
        assert self.guardrail.on_disallowed_action == "block"
        assert GuardrailEventHooks.post_call in (
            self.guardrail.supported_event_hooks or []
        )

    def test_matches_regex_helper(self):
        pattern = re.compile(r"^Read$")
        assert self.guardrail._matches_regex(pattern, "Read") is True
        assert self.guardrail._matches_regex(pattern, "Write") is False
        assert self.guardrail._matches_regex(None, "Any") is True
        assert self.guardrail._matches_regex(pattern, None) is False

    def test_rule_matches_tool_with_type_only(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="type-only",
            rules=[
                {
                    "id": "allow_functions",
                    "tool_type": r"^function$",
                    "decision": "allow",
                }
            ],
            default_action="deny",
            on_disallowed_action="block",
        )

        is_allowed, rule_id, _ = guardrail._check_tool_permission("AnyTool", "function")
        assert is_allowed is True
        assert rule_id == "allow_functions"

        is_allowed, rule_id, _ = guardrail._check_tool_permission("AnyTool", "custom")
        assert is_allowed is False
        assert rule_id is None

    def test_rule_matches_tool_with_name_and_type(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="name-type",
            rules=[
                {
                    "id": "allow_specific",
                    "tool_name": r"^Bash$",
                    "tool_type": r"^function$",
                    "decision": "allow",
                }
            ],
            default_action="deny",
            on_disallowed_action="block",
        )

        is_allowed, rule_id, _ = guardrail._check_tool_permission("Bash", "function")
        assert is_allowed is True
        assert rule_id == "allow_specific"

        is_allowed, rule_id, _ = guardrail._check_tool_permission("Bash", "custom")
        assert is_allowed is False
        assert rule_id is None

    def test_rule_requires_name_or_type(self):
        with pytest.raises(ValueError, match='validation error for ToolPermissionRule'):
            ToolPermissionGuardrail(
                guardrail_name="invalid-rule",
                rules=[{"id": "no_target", "decision": "allow"}],
                default_action="deny",
                on_disallowed_action="block",
            )

    def test_type_only_rule_skips_param_patterns(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="type-param",
            rules=[
                {
                    "id": "allow_type_only",
                    "tool_type": r"^function$",
                    "decision": "allow",
                    "allowed_param_patterns": {"foo": r"^bar$"},
                }
            ],
            default_action="deny",
            on_disallowed_action="block",
        )

        tool_call = ChatCompletionMessageToolCall(
            function={"name": "AnyTool", "arguments": "{}"},
            type="function",
        )

        is_allowed, rule_id, _ = guardrail._get_permission_for_tool_call(tool_call)
        assert is_allowed is True
        assert rule_id == "allow_type_only"

    def test_check_tool_permission_allow(self):
        is_allowed, rule_id, msg = self.guardrail._check_tool_permission("Bash")
        assert is_allowed is True
        assert rule_id == "allow_bash"
        assert "allowed" in (msg or "")

        is_allowed, rule_id, _ = self.guardrail._check_tool_permission(
            "mcp__github_add_issue_comment"
        )
        assert is_allowed is True
        assert rule_id == "allow_github"

    def test_check_tool_permission_deny(self):
        is_allowed, rule_id, msg = self.guardrail._check_tool_permission("Read")
        assert is_allowed is False
        assert rule_id == "deny_read"
        assert "denied" in (msg or "")

        is_allowed, rule_id, msg = self.guardrail._check_tool_permission("UnknownTool")
        assert is_allowed is False
        assert rule_id is None
        assert "default" in (msg or "")

    def test_check_tool_permission_custom_template(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="custom-template",
            rules=self.test_rules,
            default_action="deny",
            violation_message_template="custom {tool_name} {rule_id} :: {default_message}",
        )

        _, rule_id, message = guardrail._check_tool_permission("Read")
        assert rule_id == "deny_read"
        assert message.startswith("custom Read deny_read")
        assert "Tool 'Read' denied" in message

        _, rule_id, message = guardrail._check_tool_permission("UnknownTool")
        assert rule_id is None
        assert message.startswith("custom UnknownTool None")
        assert "Tool 'UnknownTool' denied by default action" in message

    def test_extract_tool_calls_openai_format(self):
        tool_call = {
            "id": "call_123",
            "function": {
                "name": "Read",
                "arguments": '{"file_path": "/test/file.txt"}',
            },
            "type": "function",
        }
        response = ModelResponse(
            choices=[
                Choices(
                    message={
                        "tool_calls": [tool_call],
                    }
                )
            ]
        )

        tool_calls = self.guardrail._extract_tool_calls_from_response(response)
        assert len(tool_calls) == 1
        assert isinstance(tool_calls[0], ChatCompletionMessageToolCall)
        assert tool_calls[0].id == "call_123"
        assert tool_calls[0].function.name == "Read"

    def test_extract_tool_calls_legacy_function_call_format(self):
        response = ModelResponse(
            choices=[
                Choices(
                    message={
                        "function_call": {
                            "name": "Read",
                            "arguments": '{"file_path": "/test/file.txt"}',
                        },
                    }
                )
            ]
        )

        tool_calls = self.guardrail._extract_tool_calls_from_response(response)
        assert len(tool_calls) == 1
        assert isinstance(tool_calls[0], ChatCompletionMessageToolCall)
        assert tool_calls[0].id == "legacy_function_call_0"
        assert tool_calls[0].function.name == "Read"
        assert tool_calls[0].function.arguments == '{"file_path": "/test/file.txt"}'

    def test_extract_tool_calls_empty_response(self):
        response = ModelResponse(choices=[])
        tool_calls = self.guardrail._extract_tool_calls_from_response(response)
        assert len(tool_calls) == 0

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_no_tools(self):
        """Test that async_post_call_success_hook returns response when no tool calls are present."""
        response = ModelResponse(choices=[Choices(message={})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["test-tool-permission"]}

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            result = await self.guardrail.async_post_call_success_hook(
                data=data, user_api_key_dict=user_api_key_dict, response=response
            )
        assert result is response

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_with_allowed_tools(self):
        """Test that async_post_call_success_hook returns response when tool calls are allowed."""
        tool_call = {
            "function": {"name": "Bash", "arguments": "{}"},
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["test-tool-permission"]}

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            result = await self.guardrail.async_post_call_success_hook(
                data=data, user_api_key_dict=user_api_key_dict, response=response
            )
        assert result is response

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_with_denied_tools_raises(self):
        tool_call = {
            "function": {"name": "Read", "arguments": "{}"},
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["test-tool-permission"]}

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self.guardrail.async_post_call_success_hook(
                    data=data, user_api_key_dict=user_api_key_dict, response=response
                )

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_with_denied_legacy_function_call_raises(
        self,
    ):
        response = ModelResponse(
            choices=[
                Choices(
                    message={
                        "function_call": {
                            "name": "Read",
                            "arguments": "{}",
                        },
                    }
                )
            ]
        )
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["test-tool-permission"]}

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self.guardrail.async_post_call_success_hook(
                    data=data, user_api_key_dict=user_api_key_dict, response=response
                )

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_param_patterns_allow(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="mail-guardrail",
            rules=[
                {
                    "id": "allow_mail",
                    "tool_name": r"^mail_mcp-send_email$",
                    "decision": "allow",
                    "allowed_param_patterns": {
                        "to[]": r"^.+@berri\.ai$",
                        "subject": r"^.{1,120}$",
                    },
                }
            ],
            default_action="deny",
            on_disallowed_action="block",
        )

        tool_call = {
            "function": {
                "name": "mail_mcp-send_email",
                "arguments": '{"to": ["[email protected]"], "subject": "Hi"}',
            },
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["mail-guardrail"]}

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            await guardrail.async_post_call_success_hook(
                data=data, user_api_key_dict=user_api_key_dict, response=response
            )

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_param_patterns_block(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="mail-guardrail",
            rules=[
                {
                    "id": "allow_mail",
                    "tool_name": r"^mail_mcp-send_email$",
                    "decision": "allow",
                    "allowed_param_patterns": {"to[]": r"^.+@berri\.ai$"},
                }
            ],
            default_action="deny",
            on_disallowed_action="block",
        )

        tool_call = {
            "function": {
                "name": "mail_mcp-send_email",
                "arguments": '{"to": ["[email protected]"]}',
            },
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["mail-guardrail"]}

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await guardrail.async_post_call_success_hook(
                    data=data, user_api_key_dict=user_api_key_dict, response=response
                )

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_param_patterns_rewrite(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="mail-guardrail",
            rules=[
                {
                    "id": "allow_mail",
                    "tool_name": r"^mail_mcp-send_email$",
                    "decision": "allow",
                    "allowed_param_patterns": {"to[]": r"^.+@berri\.ai$"},
                }
            ],
            default_action="deny",
            on_disallowed_action="rewrite",
        )

        tool_call = {
            "id": "call_berri",
            "function": {
                "name": "mail_mcp-send_email",
                "arguments": '{"to": ["[email protected]"]}',
            },
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["mail-guardrail"]}

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            await guardrail.async_post_call_success_hook(
                data=data, user_api_key_dict=user_api_key_dict, response=response
            )

        choice = response.choices[0]
        assert isinstance(choice, Choices)
        assert not choice.message.tool_calls
        assert isinstance(choice.message.content, str)
        assert "berri" in choice.message.content

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_missing_arguments_blocks_param_rule(
        self,
    ):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="mail-guardrail",
            rules=[
                {
                    "id": "deny_gmail",
                    "tool_name": r"^mail_mcp-send_email$",
                    "decision": "deny",
                    "allowed_param_patterns": {"to[]": r"^.+@gmail\.com$"},
                }
            ],
            default_action="allow",
            on_disallowed_action="block",
        )

        tool_call = {
            "function": {
                "name": "mail_mcp-send_email",
            },
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["mail-guardrail"]}

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await guardrail.async_post_call_success_hook(
                    data=data, user_api_key_dict=user_api_key_dict, response=response
                )

    @pytest.mark.asyncio
    @pytest.mark.parametrize(
        "arguments",
        [
            "{not-json",
            '["[email protected]"]',
        ],
    )
    async def test_async_post_call_success_hook_malformed_arguments_blocks_param_rule(
        self, arguments
    ):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="mail-guardrail",
            rules=[
                {
                    "id": "deny_gmail",
                    "tool_name": r"^mail_mcp-send_email$",
                    "decision": "deny",
                    "allowed_param_patterns": {"to[]": r"^.+@gmail\.com$"},
                }
            ],
            default_action="allow",
            on_disallowed_action="block",
        )

        tool_call = {
            "function": {
                "name": "mail_mcp-send_email",
                "arguments": arguments,
            },
            "type": "function",
        }
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])
        user_api_key_dict = UserAPIKeyAuth()
        data = {"guardrails": ["mail-guardrail"]}

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await guardrail.async_post_call_success_hook(
                    data=data, user_api_key_dict=user_api_key_dict, response=response
                )

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_block_mode(self):
        data = {
            "tools": [
                {"type": "function", "function": {"name": "Bash"}},
                {"type": "function", "function": {"name": "Read"}},
            ]
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(HTTPException) as excinfo:
                await self.guardrail.async_pre_call_hook(
                    user_api_key_dict=user_api_key_dict,
                    cache=cache,
                    data=data,
                    call_type="completion",
                )
        assert excinfo.value.status_code == 400

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_without_tools_logs_skip_at_debug(self, caplog):
        data = {"model": "gpt-4", "messages": [{"role": "user", "content": "hi"}]}

        with caplog.at_level(logging.DEBUG, logger="LiteLLM Proxy"):
            with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
                result = await self.guardrail.async_pre_call_hook(
                    user_api_key_dict=UserAPIKeyAuth(),
                    cache=DualCache(default_in_memory_ttl=1),
                    data=data,
                    call_type="completion",
                )

        assert result is data
        skip_levels = [r.levelno for r in caplog.records if "No tools or functions in data" in r.getMessage()]
        assert skip_levels == [logging.DEBUG]
        assert [r.getMessage() for r in caplog.records if r.levelno >= logging.WARNING] == []

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_denied_tool_logs_at_info(self, caplog):
        data = {"tools": [{"type": "function", "function": {"name": "Read"}}]}

        with caplog.at_level(logging.DEBUG, logger="LiteLLM Proxy"):
            with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
                with pytest.raises(HTTPException):
                    await self.guardrail.async_pre_call_hook(
                        user_api_key_dict=UserAPIKeyAuth(),
                        cache=DualCache(default_in_memory_ttl=1),
                        data=data,
                        call_type="completion",
                    )

        denied_levels = [
            r.levelno
            for r in caplog.records
            if r.getMessage() == "Tool Permission Guardrail: Tool 'Read' denied by rule 'deny_read'"
        ]
        assert denied_levels == [logging.INFO]
        assert [r.getMessage() for r in caplog.records if r.levelno >= logging.WARNING] == []

    @pytest.mark.asyncio
    async def test_async_post_call_success_hook_denied_tool_logs_at_info(self, caplog):
        tool_call = {"function": {"name": "Read", "arguments": "{}"}, "type": "function"}
        response = ModelResponse(choices=[Choices(message={"tool_calls": [tool_call]})])

        with caplog.at_level(logging.DEBUG, logger="LiteLLM Proxy"):
            with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
                with pytest.raises(GuardrailRaisedException):
                    await self.guardrail.async_post_call_success_hook(
                        data={"guardrails": ["test-tool-permission"]},
                        user_api_key_dict=UserAPIKeyAuth(),
                        response=response,
                    )

        denied_levels = [
            r.levelno
            for r in caplog.records
            if r.getMessage() == "Tool Permission Guardrail: Tool 'Read' denied by rule 'deny_read'"
        ]
        assert denied_levels == [logging.INFO]
        assert [r.getMessage() for r in caplog.records if r.levelno >= logging.WARNING] == []

    def test_parse_tool_call_arguments_malformed_json_logs_warning(self, caplog):
        tool_call = ChatCompletionMessageToolCall(function={"name": "Bash", "arguments": "{not json"}, id="call_1")

        with caplog.at_level(logging.DEBUG, logger="LiteLLM Proxy"):
            parsed, error = self.guardrail._parse_tool_call_arguments(tool_call)

        assert parsed is None
        assert error == "arguments could not be parsed"
        warning_messages = [r.getMessage() for r in caplog.records if r.levelno == logging.WARNING]
        assert len(warning_messages) == 1
        assert warning_messages[0].startswith("Tool Permission Guardrail: Failed to decode arguments for tool Bash")

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_blocks_legacy_functions(self):
        data = {
            "functions": [
                {"name": "Bash", "description": "allowed"},
                {"name": "Read", "description": "denied"},
            ]
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(HTTPException) as excinfo:
                await self.guardrail.async_pre_call_hook(
                    user_api_key_dict=user_api_key_dict,
                    cache=cache,
                    data=data,
                    call_type="completion",
                )
        assert excinfo.value.status_code == 400

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_blocks_named_legacy_function_call(self):
        data = {
            "functions": [{"name": "Bash"}],
            "function_call": {"name": "Read"},
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(HTTPException) as excinfo:
                await self.guardrail.async_pre_call_hook(
                    user_api_key_dict=user_api_key_dict,
                    cache=cache,
                    data=data,
                    call_type="completion",
                )
        assert excinfo.value.status_code == 400

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_blocks_named_tool_choice(self):
        data = {
            "tools": [{"type": "function", "function": {"name": "Bash"}}],
            "tool_choice": {"type": "function", "function": {"name": "Read"}},
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(self.guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(HTTPException) as excinfo:
                await self.guardrail.async_pre_call_hook(
                    user_api_key_dict=user_api_key_dict,
                    cache=cache,
                    data=data,
                    call_type="completion",
                )
        assert excinfo.value.status_code == 400

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_uses_custom_template(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="custom-template",
            rules=self.test_rules,
            default_action="deny",
            on_disallowed_action="block",
            violation_message_template="blocked {tool_name} by policy",
        )

        data = {
            "tools": [
                {"type": "function", "function": {"name": "Read"}},
            ]
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(HTTPException) as excinfo:
                await guardrail.async_pre_call_hook(
                    user_api_key_dict=user_api_key_dict,
                    cache=cache,
                    data=data,
                    call_type="completion",
                )

        assert excinfo.value.status_code == 400
        assert excinfo.value.detail.get("detection_message") == "blocked Read by policy"

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_rewrite_mode(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-tool-permission",
            rules=self.test_rules,
            default_action="deny",
            on_disallowed_action="rewrite",
        )
        data = {
            "tools": [
                {"type": "function", "function": {"name": "Bash"}},
                {"type": "function", "function": {"name": "Read"}},
            ]
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            new_data = await guardrail.async_pre_call_hook(
                user_api_key_dict=user_api_key_dict,
                cache=cache,
                data=data,
                call_type="completion",
            )

        assert isinstance(new_data, dict)
        assert "tools" in new_data
        tool_names = [t["function"]["name"] for t in new_data["tools"]]
        assert "Bash" in tool_names
        assert "Read" not in tool_names

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_rewrite_mode_filters_legacy_functions(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-tool-permission",
            rules=self.test_rules,
            default_action="deny",
            on_disallowed_action="rewrite",
        )
        data = {
            "functions": [
                {"name": "Bash", "description": "allowed"},
                {"name": "Read", "description": "denied"},
            ],
            "function_call": {"name": "Read"},
            "tools": [
                {"type": "function", "function": {"name": "Bash"}},
            ],
            "tool_choice": {"type": "function", "function": {"name": "Read"}},
        }
        user_api_key_dict = UserAPIKeyAuth()
        cache = DualCache(default_in_memory_ttl=1)

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            new_data = await guardrail.async_pre_call_hook(
                user_api_key_dict=user_api_key_dict,
                cache=cache,
                data=data,
                call_type="completion",
            )

        assert isinstance(new_data, dict)
        assert [function["name"] for function in new_data["functions"]] == ["Bash"]
        assert new_data["function_call"] == "none"
        assert new_data["tool_choice"] == "none"

    @staticmethod
    def _tool_search_turn(*loaded_names: str) -> dict:
        return {
            "role": "assistant",
            "content": None,
            "provider_specific_fields": {
                "tool_search_items": [
                    {"type": "tool_search_call", "call_id": "ts_1", "execution": "client"},
                    {
                        "type": "tool_search_output",
                        "call_id": "ts_1",
                        "tools": [{"type": "function", "name": name} for name in loaded_names],
                    },
                ]
            },
        }

    def _replayed_run_shell_request(self) -> dict:
        return {
            "tools": [{"type": "function", "function": {"name": "get_weather"}}, {"type": "tool_search"}],
            "messages": [
                {"role": "user", "content": "check the weather, then clean up the disk"},
                self._tool_search_turn("get_weather"),
                self._tool_search_turn("run_shell"),
            ],
        }

    @staticmethod
    def _deny_run_shell(on_disallowed_action: str) -> ToolPermissionGuardrail:
        return ToolPermissionGuardrail(
            guardrail_name="test-tool-permission",
            rules=[{"id": "deny_shell", "tool_name": r"^run_shell$", "decision": "deny"}],
            default_action="allow",
            on_disallowed_action=on_disallowed_action,
        )

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_blocks_tool_loaded_by_replayed_tool_search(self):
        guardrail = self._deny_run_shell("block")

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            with pytest.raises(HTTPException) as excinfo:
                await guardrail.async_pre_call_hook(
                    user_api_key_dict=UserAPIKeyAuth(),
                    cache=DualCache(default_in_memory_ttl=1),
                    data=self._replayed_run_shell_request(),
                    call_type="completion",
                )

        assert excinfo.value.status_code == 400
        assert "run_shell" in json.dumps(excinfo.value.detail)

    @pytest.mark.asyncio
    async def test_async_pre_call_hook_rewrite_drops_replayed_tool_search_loading_denied_tool(self):
        guardrail = self._deny_run_shell("rewrite")
        data = self._replayed_run_shell_request()

        with patch.object(guardrail, "should_run_guardrail", return_value=True):
            new_data = await guardrail.async_pre_call_hook(
                user_api_key_dict=UserAPIKeyAuth(),
                cache=DualCache(default_in_memory_ttl=1),
                data=data,
                call_type="completion",
            )

        assert isinstance(new_data, dict)
        user_turn, weather_search, shell_search = new_data["messages"]
        assert user_turn == {"role": "user", "content": "check the weather, then clean up the disk"}
        assert weather_search == self._tool_search_turn("get_weather")
        assert shell_search == {"role": "assistant", "content": None, "provider_specific_fields": {}}
        assert [tool.get("function", {}).get("name") for tool in new_data["tools"]] == ["get_weather", None]

    @pytest.mark.asyncio
    async def test_async_post_call_streaming_iterator_hook_plain_text_yields_chunks(
        self,
    ):
        """Regression test: hook must re-emit chunks when LLM replies with plain text.

        Before the fix, the `if not tool_calls:` branch did a bare `return` inside
        the async generator, which yielded nothing.  Clients received only
        `data: [DONE]` with no content.
        """
        text_chunk = ModelResponseStream(
            id="chatcmpl-plain-text",
            created=1700000000,
            model="gpt-4",
            object="chat.completion.chunk",
            choices=[],
        )

        async def _fake_stream():
            yield text_chunk

        assembled = ModelResponse(
            choices=[Choices(message={"content": "Hello, world!"})]
        )

        with patch("litellm.main.stream_chunk_builder", return_value=assembled):
            chunks = []
            async for chunk in self.guardrail.async_post_call_streaming_iterator_hook(
                user_api_key_dict=UserAPIKeyAuth(),
                response=_fake_stream(),
                request_data={},
            ):
                chunks.append(chunk)

        assert len(chunks) >= 1, (
            "Hook must yield at least one chunk for plain-text responses; "
            "got none — bare return bug"
        )
        assert chunks[0].choices[0].delta.content == "Hello, world!", (
            "Hook must preserve the original response content; "
            f"got: {chunks[0].choices[0].delta.content!r}"
        )

    def test_modify_response_with_permission_errors(self):
        # Setup a response with one tool_call
        tool_call = ChatCompletionMessageToolCall(
            function={"name": "Read", "arguments": "{}"}, id="call_123"
        )
        response = ModelResponse(
            choices=[Choices(message={"tool_calls": [tool_call], "content": ""})]
        )

        # Denied tools tuple of (tool_call, PermissionError)
        denied_tools = [
            (
                tool_call,
                PermissionError(
                    tool_name="Read",
                    rule_id="deny_read",
                    message="Tool 'Bash' denied by rule 'deny_read'",
                ),
            )
        ]

        # Apply modifications
        self.guardrail._modify_response_with_permission_errors(response, denied_tools)

        # Verify: tool_calls removed and content contains error message
        choice = response.choices[0]
        assert isinstance(choice, Choices)
        assert choice.message.tool_calls is None or choice.message.tool_calls == []
        assert isinstance(choice.message.content, str)
        assert "Permission denied" in choice.message.content

    def test_modify_response_resets_finish_reason_when_every_tool_call_is_denied(self):
        tool_call = ChatCompletionMessageToolCall(function={"name": "Read", "arguments": "{}"}, id="call_123")
        response = ModelResponse(
            choices=[Choices(finish_reason="tool_calls", message={"tool_calls": [tool_call], "content": ""})]
        )
        denied_tools = [
            (
                tool_call,
                PermissionError(tool_name="Read", rule_id="deny_read", message="Tool 'Read' denied by rule 'deny_read'"),
            )
        ]

        self.guardrail._modify_response_with_permission_errors(response, denied_tools)

        choice = response.choices[0]
        assert isinstance(choice, Choices)
        assert choice.finish_reason == "stop", (
            "keeping finish_reason tool_calls with no surviving tool calls leaves the client waiting on a tool"
        )

    def test_modify_response_with_permission_errors_filters_legacy_function_call(self):
        response = ModelResponse(
            choices=[
                Choices(
                    message={
                        "function_call": {
                            "name": "Read",
                            "arguments": "{}",
                        },
                        "content": "",
                    }
                )
            ]
        )
        tool_call = self.guardrail._extract_tool_calls_from_response(response)[0]
        denied_tools = [
            (
                tool_call,
                PermissionError(
                    tool_name="Read",
                    rule_id="deny_read",
                    message="Tool 'Read' denied by rule 'deny_read'",
                ),
            )
        ]

        self.guardrail._modify_response_with_permission_errors(response, denied_tools)

        choice = response.choices[0]
        assert isinstance(choice, Choices)
        assert choice.message.function_call is None
        assert isinstance(choice.message.content, str)
        assert "Permission denied" in choice.message.content


class TestToolPermissionGuardrailIntegration:
    """Integration tests for Tool Permission Guardrail"""

    def test_default_action_allow(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-allow-default",
            rules=[{"id": "deny_read", "tool_name": r"^Read$", "decision": "deny"}],
            default_action="allow",
        )

        is_allowed, rule_id, message = guardrail._check_tool_permission("UnknownTool")
        assert is_allowed is True
        assert rule_id is None
        assert "default" in (message or "")

        is_allowed, rule_id, _ = guardrail._check_tool_permission("Read")
        assert is_allowed is False
        assert rule_id == "deny_read"

    def test_empty_rules(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-no-rules",
            rules=[],
            default_action="allow",
        )

        is_allowed, rule_id, message = guardrail._check_tool_permission("AnyTool")
        assert is_allowed is True
        assert rule_id is None
        assert "default" in (message or "")

    def test_case_insensitive_default_action(self):
        """Test that default_action accepts capitalized values and normalizes them"""
        # Test capitalized 'Deny'
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-case-insensitive",
            rules=[],
            default_action="Deny",  # Should be normalized to 'deny'
        )
        assert guardrail.default_action == "deny"

        # Test capitalized 'Allow'
        guardrail2 = ToolPermissionGuardrail(
            guardrail_name="test-case-insensitive2",
            rules=[],
            default_action="Allow",  # Should be normalized to 'allow'
        )
        assert guardrail2.default_action == "allow"

        # Test uppercase 'DENY'
        guardrail3 = ToolPermissionGuardrail(
            guardrail_name="test-case-insensitive3",
            rules=[],
            default_action="DENY",  # Should be normalized to 'deny'
        )
        assert guardrail3.default_action == "deny"

    def test_case_insensitive_on_disallowed_action(self):
        """Test that on_disallowed_action accepts capitalized values and normalizes them"""
        # Test capitalized 'Block'
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-on-disallowed",
            rules=[],
            default_action="deny",
            on_disallowed_action="Block",  # Should be normalized to 'block'
        )
        assert guardrail.on_disallowed_action == "block"

        # Test capitalized 'Rewrite'
        guardrail2 = ToolPermissionGuardrail(
            guardrail_name="test-on-disallowed2",
            rules=[],
            default_action="deny",
            on_disallowed_action="Rewrite",  # Should be normalized to 'rewrite'
        )
        assert guardrail2.on_disallowed_action == "rewrite"

    def test_case_insensitive_decision_in_rules(self):
        """Test that decision field in rules accepts capitalized values and normalizes them"""
        guardrail = ToolPermissionGuardrail(
            guardrail_name="test-decision-case",
            rules=[
                {
                    "id": "allow_bash",
                    "tool_name": r"^Bash$",
                    "decision": "Allow",
                },  # Capitalized
                {
                    "id": "deny_read",
                    "tool_name": r"^Read$",
                    "decision": "DENY",
                },  # Uppercase
            ],
            default_action="deny",
        )

        # Verify rules are normalized
        assert guardrail.rules[0].decision == "allow"
        assert guardrail.rules[1].decision == "deny"

        # Verify functionality still works
        is_allowed, rule_id, _ = guardrail._check_tool_permission("Bash")
        assert is_allowed is True
        assert rule_id == "allow_bash"

        is_allowed, rule_id, _ = guardrail._check_tool_permission("Read")
        assert is_allowed is False
        assert rule_id == "deny_read"


class TestToolPermissionGuardrailInMemoryUpdate:
    """Regression: an in-memory params update (PUT /guardrails path) must rebuild
    the compiled rule maps, not just self.rules, so the new rules are enforced
    without reinitializing the guardrail."""

    def _bash(self, command):
        return ChatCompletionMessageToolCall(
            function={"name": "Bash", "arguments": json.dumps({"command": command})},
            type="function",
        )

    def test_update_in_memory_recompiles_added_param_pattern(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="tp",
            rules=[{"id": "native-bash", "tool_name": r"^Bash$", "decision": "allow"}],
            default_action="deny",
            on_disallowed_action="block",
        )
        # No pattern yet: any Bash command is allowed.
        assert (
            guardrail._get_permission_for_tool_call(self._bash("echo blockme"))[0]
            is True
        )

        guardrail.update_in_memory_litellm_params(
            LitellmParams(
                guardrail="tool_permission",
                mode=["pre_call", "post_call"],
                default_action="deny",
                on_disallowed_action="block",
                rules=[
                    {
                        "id": "native-bash",
                        "tool_name": r"^Bash$",
                        "decision": "allow",
                        "allowed_param_patterns": {
                            "command": r"^(?!(echo blockme)$).*$"
                        },
                    }
                ],
            )
        )

        # The compiled map must be rebuilt, and enforcement must reflect it.
        assert "command" in guardrail._compiled_rule_patterns.get("native-bash", {})
        assert (
            guardrail._get_permission_for_tool_call(self._bash("echo blockme"))[0]
            is False
        )
        assert (
            guardrail._get_permission_for_tool_call(self._bash("echo hello"))[0] is True
        )

    def test_update_in_memory_recompiles_tool_name_target(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="tp",
            rules=[],
            default_action="allow",
            on_disallowed_action="block",
        )
        # No rules: default_action allow lets Bash through.
        assert guardrail._get_permission_for_tool_call(self._bash("echo x"))[0] is True

        guardrail.update_in_memory_litellm_params(
            LitellmParams(
                guardrail="tool_permission",
                mode=["pre_call", "post_call"],
                default_action="allow",
                on_disallowed_action="block",
                rules=[{"id": "deny-bash", "tool_name": r"^Bash$", "decision": "deny"}],
            )
        )

        # A newly added deny rule (new id) must match -> its compiled target was rebuilt.
        assert "deny-bash" in guardrail._compiled_rule_targets
        assert guardrail._get_permission_for_tool_call(self._bash("echo x"))[0] is False

    def test_update_in_memory_preserves_rules_when_rules_absent(self):
        guardrail = ToolPermissionGuardrail(
            guardrail_name="tp",
            rules=[
                {
                    "id": "native-bash",
                    "tool_name": r"^Bash$",
                    "decision": "allow",
                    "allowed_param_patterns": {"command": r"^(?!(echo blockme)$).*$"},
                }
            ],
            default_action="deny",
            on_disallowed_action="block",
        )
        assert "command" in guardrail._compiled_rule_patterns.get("native-bash", {})

        # A partial update that does not carry `rules` must NOT wipe the existing
        # ruleset / compiled maps.
        guardrail.update_in_memory_litellm_params(
            LitellmParams(
                guardrail="tool_permission",
                mode=["pre_call", "post_call"],
                default_action="deny",
                on_disallowed_action="block",
            )
        )

        assert len(guardrail.rules) == 1
        assert "command" in guardrail._compiled_rule_patterns.get("native-bash", {})
        assert (
            guardrail._get_permission_for_tool_call(self._bash("echo blockme"))[0]
            is False
        )

    def test_update_in_memory_rejects_invalid_regex_and_keeps_previous_rules(self):
        """Regression: a live update whose rules contain an invalid regex must be
        rejected atomically. The bad rule must not leak in as a compiled-target
        wildcard (match-all), and the previously enforced ruleset must survive."""
        guardrail = ToolPermissionGuardrail(
            guardrail_name="tp",
            rules=[{"id": "deny-secret", "tool_name": r"^Secret$", "decision": "deny"}],
            default_action="allow",
            on_disallowed_action="block",
        )
        # Baseline: only "Secret" is denied; any other tool is allowed.
        assert guardrail._check_tool_permission("Secret")[0] is False
        assert guardrail._check_tool_permission("Other")[0] is True

        with pytest.raises(ValueError, match="Invalid regex for tool_name in rule 'bad': unterminated"):
            guardrail.update_in_memory_litellm_params(
                LitellmParams(
                    guardrail="tool_permission",
                    mode=["pre_call", "post_call"],
                    default_action="allow",
                    on_disallowed_action="block",
                    rules=[
                        {
                            "id": "deny-secret",
                            "tool_name": r"^Secret$",
                            "decision": "deny",
                        },
                        {"id": "bad", "tool_name": "[unclosed", "decision": "deny"},
                    ],
                )
            )

        # The bad rule must not have leaked in, and the prior ruleset must hold.
        assert "bad" not in guardrail._compiled_rule_targets
        assert all(rule.id != "bad" for rule in guardrail.rules)
        assert guardrail._check_tool_permission("Other")[0] is True
        assert guardrail._check_tool_permission("Secret")[0] is False


class TestToolPermissionGuardrailAnthropicMessages:
    """LIT-5250: /v1/messages responses arrive as Anthropic content blocks, not a
    ModelResponse. Before the fix the hooks early-returned on that shape, so every
    tool call an Anthropic-native client made bypassed the rules entirely.
    """

    def setup_method(self):
        self.rules = [
            {"id": "allow_bash", "tool_name": r"^Bash$", "decision": "allow"},
            {"id": "deny_read", "tool_name": r"^Read$", "decision": "deny"},
        ]
        self.blocking = ToolPermissionGuardrail(
            guardrail_name="anthropic-block",
            rules=self.rules,
            default_action="deny",
            on_disallowed_action="block",
        )
        self.rewriting = ToolPermissionGuardrail(
            guardrail_name="anthropic-rewrite",
            rules=self.rules,
            default_action="deny",
            on_disallowed_action="rewrite",
        )

    def _response(self, *blocks):
        return {
            "id": "msg_1",
            "type": "message",
            "role": "assistant",
            "model": "claude-sonnet-4-5",
            "content": list(blocks),
            "stop_reason": "tool_use",
            "usage": {"input_tokens": 10, "output_tokens": 5},
        }

    def _tool_use(self, name, tool_id="tu_1"):
        return {"type": "tool_use", "id": tool_id, "name": name, "input": {"command": "ls"}}

    @pytest.mark.asyncio
    async def test_denied_anthropic_tool_use_is_blocked(self):
        response = self._response({"type": "text", "text": "reading"}, self._tool_use("Read"))

        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self.blocking.async_post_call_success_hook(
                    data={}, user_api_key_dict=UserAPIKeyAuth(), response=response
                )

    @pytest.mark.asyncio
    async def test_allowed_anthropic_tool_use_passes_through_untouched(self):
        response = self._response({"type": "text", "text": "listing"}, self._tool_use("Bash"))

        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            result = await self.blocking.async_post_call_success_hook(
                data={}, user_api_key_dict=UserAPIKeyAuth(), response=response
            )

        assert [b["type"] for b in result["content"]] == ["text", "tool_use"]
        assert result["stop_reason"] == "tool_use"

    @pytest.mark.asyncio
    async def test_rewrite_mode_strips_the_denied_anthropic_tool_use(self):
        response = self._response({"type": "text", "text": "reading"}, self._tool_use("Read"))

        with patch.object(self.rewriting, "should_run_guardrail", return_value=True):
            result = await self.rewriting.async_post_call_success_hook(
                data={}, user_api_key_dict=UserAPIKeyAuth(), response=response
            )

        assert all(b["type"] != "tool_use" for b in result["content"]), (
            "denied tool_use must not reach the client in rewrite mode"
        )
        assert any("Permission denied" in b.get("text", "") for b in result["content"])
        assert result["stop_reason"] == "end_turn", (
            "leaving stop_reason as tool_use makes the client wait for a tool result that will never come"
        )

    @pytest.mark.asyncio
    async def test_rewrite_mode_keeps_allowed_tool_use_when_only_one_is_denied(self):
        response = self._response(self._tool_use("Bash", "tu_ok"), self._tool_use("Read", "tu_bad"))

        with patch.object(self.rewriting, "should_run_guardrail", return_value=True):
            result = await self.rewriting.async_post_call_success_hook(
                data={}, user_api_key_dict=UserAPIKeyAuth(), response=response
            )

        tool_ids = [b["id"] for b in result["content"] if b["type"] == "tool_use"]
        assert tool_ids == ["tu_ok"]
        assert result["stop_reason"] == "tool_use"

    def _sse_chunks(self, tool_name, tool_id="tu_1"):
        events = [
            {"type": "message_start", "message": {"id": "msg_1", "type": "message", "role": "assistant",
                                                  "model": "claude-sonnet-4-5", "content": [], "stop_reason": None,
                                                  "usage": {"input_tokens": 10, "output_tokens": 0}}},
            {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
            {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "working"}},
            {"type": "content_block_stop", "index": 0},
            {"type": "content_block_start", "index": 1,
             "content_block": {"type": "tool_use", "id": tool_id, "name": tool_name, "input": {}}},
            {"type": "content_block_delta", "index": 1,
             "delta": {"type": "input_json_delta", "partial_json": '{"command": "ls"}'}},
            {"type": "content_block_stop", "index": 1},
            {"type": "message_delta", "delta": {"stop_reason": "tool_use"}, "usage": {"output_tokens": 5}},
            {"type": "message_stop"},
        ]
        return [f"event: {e['type']}\ndata: {json.dumps(e)}\n\n".encode() for e in events]

    async def _drain(self, guardrail, chunks):
        async def _stream():
            for chunk in chunks:
                yield chunk

        return [
            c
            async for c in guardrail.async_post_call_streaming_iterator_hook(
                user_api_key_dict=UserAPIKeyAuth(), response=_stream(), request_data={}
            )
        ]

    @pytest.mark.asyncio
    async def test_denied_tool_use_in_anthropic_sse_stream_is_blocked(self):
        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self._drain(self.blocking, self._sse_chunks("Read"))

    @pytest.mark.asyncio
    async def test_allowed_tool_use_in_anthropic_sse_stream_is_passed_through_verbatim(self):
        chunks = self._sse_chunks("Bash")

        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            out = await self._drain(self.blocking, chunks)

        assert out == chunks, "an allowed stream must not be re-serialized"

    @pytest.mark.asyncio
    async def test_rewrite_mode_removes_denied_tool_use_from_anthropic_sse_stream(self):
        with patch.object(self.rewriting, "should_run_guardrail", return_value=True):
            out = await self._drain(self.rewriting, self._sse_chunks("Read"))

        body = b"".join(c if isinstance(c, bytes) else str(c).encode() for c in out).decode()
        assert '"type": "tool_use"' not in body, "denied tool_use must not survive into the rewritten stream"
        assert "Permission denied" in body
        assert '"stop_reason": "end_turn"' in body, (
            "dropping every tool_use must end the turn, or the client waits for a tool result that never comes"
        )
        assert '"stop_reason": "tool_use"' not in body

    @pytest.mark.asyncio
    async def test_rewrite_mode_keeps_the_stream_identity_it_had_before_the_shared_helper(self):
        """Well-formed SSE must round-trip exactly as it did before the helpers were shared.

        The shared module can stamp the upstream message id onto the assembled response for
        callers that ask for it; this path never did, and a client reads those bytes. The model,
        though, is now the upstream's, matching what the untouched passthrough shows clients.
        """
        with patch.object(self.rewriting, "should_run_guardrail", return_value=True):
            out = await self._drain(self.rewriting, self._sse_chunks("Read"))

        body = b"".join(c if isinstance(c, bytes) else str(c).encode() for c in out).decode()
        message_start = next(
            json.loads(line[6:])
            for line in body.splitlines()
            if line.startswith("data: ") and json.loads(line[6:]).get("type") == "message_start"
        )["message"]
        assert message_start["id"].startswith("chatcmpl-"), "the rewritten stream must not adopt the upstream message id"
        assert message_start["model"] == "claude-sonnet-4-5", "the rewritten stream reports the model the upstream served"

    @pytest.mark.asyncio
    async def test_rewrite_mode_keeps_the_allowed_tool_use_verdict_with_its_explanation(self):
        """Claude Code auto mode reads the classifier verdict from message_delta. The rewrite changes
        no text the explanation could repeat, so the allowed call keeps its verdict whole."""
        default_on_rewriting: Final = ToolPermissionGuardrail(
            guardrail_name="anthropic-rewrite-default-on",
            rules=self.rules,
            default_action="deny",
            on_disallowed_action="rewrite",
            event_hook=GuardrailEventHooks.post_call,
            default_on=True,
        )
        bash_verdict: Final = {"type": "evaluated", "outcome": "flagged", "explanation": "lists the home directory"}
        safeguard_results: Final = [
            {
                "type": "dangerous_tool_use",
                "status": {
                    "type": "available",
                    "tool_uses": {"tu_ok": bash_verdict, "tu_bad": {"type": "evaluated", "outcome": "not_flagged"}},
                },
            }
        ]
        bash_chunks: Final = self._sse_chunks("Bash", "tu_ok")
        read_chunks: Final = self._sse_chunks("Read", "tu_bad")
        read_tool_use: Final = [chunk.replace(b'"index": 1', b'"index": 2') for chunk in read_chunks[4:7]]
        message_delta: Final = {
            "type": "message_delta",
            "delta": {"stop_reason": "tool_use", "safeguard_results": safeguard_results},
            "usage": {"output_tokens": 5},
        }
        chunks: Final = [
            *bash_chunks[:7],
            *read_tool_use,
            f"event: message_delta\ndata: {json.dumps(message_delta)}\n\n".encode(),
            bash_chunks[-1],
        ]

        body: Final = b"".join(await self._drain(default_on_rewriting, chunks)).decode()
        delta: Final = next(
            json.loads(line[6:])["delta"]
            for line in body.splitlines()
            if line.startswith("data: ") and json.loads(line[6:]).get("type") == "message_delta"
        )
        assert "Permission denied" in body
        assert delta["safeguard_results"] == [
            {"type": "dangerous_tool_use", "status": {"type": "available", "tool_uses": {"tu_ok": bash_verdict}}}
        ]

    @pytest.mark.asyncio
    async def test_message_start_without_a_dict_message_fails_closed(self):
        """Malformed SSE must not be forwarded unscanned.

        The shared assembler requires message_start.message to be a dict; the private helper it
        replaced accepted anything, and assembled a response from it.
        """
        events = [
            {"type": "message_start", "message": "not-a-dict"},
            {"type": "content_block_start", "index": 0, "content_block": {"type": "text", "text": ""}},
            {"type": "content_block_delta", "index": 0, "delta": {"type": "text_delta", "text": "hi"}},
            {"type": "message_stop"},
        ]
        chunks = [f"event: {e['type']}\ndata: {json.dumps(e)}\n\n".encode() for e in events]

        with patch.object(self.rewriting, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self._drain(self.rewriting, chunks)

    def _resplit(self, chunks, size=7):
        joined = b"".join(chunks)
        return [joined[i : i + size] for i in range(0, len(joined), size)]

    @pytest.mark.asyncio
    async def test_denied_tool_use_is_caught_when_sse_events_are_split_across_chunk_boundaries(self):
        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException) as exc_info:
                await self._drain(self.blocking, self._resplit(self._sse_chunks("Read")))

        assert "deny_read" in str(exc_info.value), (
            "a stream split mid-event must still assemble and hit the rule, not fail as unparseable"
        )

    @pytest.mark.asyncio
    async def test_allowed_stream_split_across_chunk_boundaries_is_passed_through_verbatim(self):
        chunks = self._resplit(self._sse_chunks("Bash"))

        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            out = await self._drain(self.blocking, chunks)

        assert out == chunks

    @pytest.mark.asyncio
    async def test_non_anthropic_sse_stream_fails_closed(self):
        gemini_chunks = [
            b'data: {"candidates": [{"content": {"parts": [{"functionCall": '
            b'{"name": "run_shell", "args": {"command": "ls"}}}], "role": "model"}}]}\n\n',
            b'data: {"candidates": [{"content": {"parts": [{"text": "done"}]}, "finishReason": "STOP"}]}\n\n',
        ]

        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self._drain(self.blocking, gemini_chunks)

    @pytest.mark.asyncio
    async def test_unparseable_sse_stream_fails_closed(self):
        with patch.object(self.blocking, "should_run_guardrail", return_value=True):
            with pytest.raises(GuardrailRaisedException):
                await self._drain(self.blocking, [b"data: not-json\n\n", b"event: weird\n\n"])