fix(guardrails): judge a legacy functions entry as a function and relax a tool_choice in extra_body

This commit is contained in:
mateo-berri 2026-10-31 00:16:13 -07:00
parent 499015c671
commit 85817e64d8
2 changed files with 74 additions and 9 deletions

View file

@ -1062,7 +1062,7 @@ class ToolPermissionGuardrail(CustomGuardrail):
request_tools.extend(map(_mcp_server_target, _request_list(request, "mcp_servers") or ()))
for function in _request_list(request, "functions") or ():
request_tools.extend(self._get_request_tool_targets(function))
request_tools.extend(self._legacy_function_targets(function))
listed_names: Final = frozenset(name for name, _ in request_tools)
for forced_tool_name in (
@ -1079,6 +1079,24 @@ class ToolPermissionGuardrail(CustomGuardrail):
return request_tools
def _legacy_function_targets(self, function: object) -> tuple[_ToolTarget, ...]:
targets: Final = self._get_request_tool_targets(function)
as_functions: Final = tuple((name, "function") for name, _ in targets if name is not None)
return tuple(dict.fromkeys(targets + as_functions))
def _rewritten_extra_body(
self,
extra_body: Mapping[str, object],
kept_tools: tuple[object, ...] | None,
rewrite_choice: Callable[[object], object],
) -> dict[str, object]:
choice: Final = extra_body.get("tool_choice")
return {
**extra_body,
**({"tools": list(kept_tools)} if kept_tools is not None else {}),
**({"tool_choice": rewrite_choice(choice)} if choice is not None else {}),
}
def _tool_names_in(self, tools: Sequence[object]) -> frozenset[str]:
targets: Final = chain.from_iterable(self._get_request_tool_targets(tool) for tool in tools)
return frozenset(name for name, _ in targets if name is not None)
@ -1189,11 +1207,6 @@ class ToolPermissionGuardrail(CustomGuardrail):
extra_body: Final = _extra_body(request)
extra_body_tools: Final = _extra_body_tools(request)
kept_extra_body_tools: Final = None if extra_body_tools is None else kept_tool_list(extra_body_tools)
if extra_body is not None and kept_extra_body_tools is not None:
data["extra_body"] = { # rebind-ok: rewrite mode edits the request in place
**extra_body,
"tools": list(kept_extra_body_tools),
}
functions: Final = _request_list(request, "functions")
kept_functions: Final = None if functions is None else kept_tool_list(functions)
@ -1232,12 +1245,19 @@ class ToolPermissionGuardrail(CustomGuardrail):
removed_types: Final = self._tool_types_in(sent_tools) - kept_types
tools_remain: Final = bool(kept_sent_tools) or bool(kept_mcp_servers)
rewrite_choice: Final = partial(
self._rewritten_tool_choice,
removed_names=removed_names,
removed_types=removed_types,
tools_remain=tools_remain,
)
tool_choice: Final = _REQUEST_DATA.validate_python(data.get("tool_choice"))
if tool_choice is not None:
rewritten_choice: Final = self._rewritten_tool_choice(
tool_choice, removed_names, removed_types, tools_remain
data["tool_choice"] = rewrite_choice(tool_choice) # rebind-ok: rewrite mode edits the request in place
if extra_body is not None and (kept_extra_body_tools is not None or "tool_choice" in extra_body):
data["extra_body"] = self._rewritten_extra_body( # rebind-ok: rewrite mode edits the request in place
extra_body, kept_extra_body_tools, rewrite_choice
)
data["tool_choice"] = rewritten_choice # rebind-ok: rewrite mode edits the request in place
named_function_call: Final = self._get_named_function_call(data)
if named_function_call in removed_names:

View file

@ -2024,6 +2024,25 @@ class TestToolPermissionGuardrailAnthropicMessages:
assert message == "Tool 'Read' denied by rule 'deny_read'"
assert rewritten["extra_body"] == {"tools": [bash], "seed": 7}
@pytest.mark.asyncio
async def test_rewrite_relaxes_an_extra_body_tool_choice_that_names_a_dropped_tool(self) -> None:
guardrail = self._pre_call([{"id": "deny_delete", "tool_name": r"^delete_db$", "decision": "deny"}], "rewrite")
weather = {"type": "function", "function": {"name": "get_weather"}}
delete_db = {"type": "function", "function": {"name": "delete_db"}}
data = {
"model": "m",
"messages": [{"role": "user", "content": "hi"}],
"extra_body": {
"tools": [weather, delete_db],
"tool_choice": {"type": "function", "function": {"name": "delete_db"}},
"seed": 7,
},
}
result = await self._run_pre_call(guardrail, data, "acompletion")
assert result["extra_body"] == {"tools": [weather], "tool_choice": "none", "seed": 7}
@pytest.mark.asyncio
@pytest.mark.parametrize(
"rule",
@ -2574,6 +2593,32 @@ class TestToolPermissionGuardrailAnthropicMessages:
assert result["functions"] == [weather]
@pytest.mark.asyncio
@pytest.mark.parametrize("on_disallowed_action", ["block", "rewrite"])
async def test_pre_call_judges_a_legacy_functions_entry_as_a_function_whatever_its_type(
self, on_disallowed_action: Literal["block", "rewrite"]
) -> None:
guardrail = self._pre_call(
[{"id": "deny_delete", "tool_name": r"^delete_db$", "tool_type": r"^function$", "decision": "deny"}],
on_disallowed_action,
)
weather = {"name": "get_weather"}
data = {
"model": "gemini-3",
"messages": [{"role": "user", "content": "hi"}],
"functions": [weather, {"type": "custom", "name": "delete_db"}],
}
if on_disallowed_action == "block":
with pytest.raises(HTTPException) as excinfo:
await self._run_pre_call(guardrail, data, "acompletion")
assert excinfo.value.detail["detection_message"] == "Tool 'delete_db' denied by rule 'deny_delete'"
return
result = await self._run_pre_call(guardrail, data, "acompletion")
assert result["functions"] == [weather]
@pytest.mark.asyncio
async def test_rewrite_keeps_an_allowed_function_whose_declaration_list_is_denied(self) -> None:
guardrail = self._pre_call(