fix(cisco-ai-defense): send tool definitions as a leading system message

Cisco's SDK only accepts the user, assistant and system roles, so the
tool role from the previous commit risks failing inspection. Keep the
system role and put the tool-definition message first so the caller's
last turn stays last, and drop the leading message before redaction
This commit is contained in:
Numan Elahi 2026-11-02 11:09:37 +05:30
parent 9474100d23
commit e712f69f87
2 changed files with 41 additions and 21 deletions

View file

@ -1421,11 +1421,12 @@ class CiscoAIDefenseGuardrail(CiscoAIDefenseMcpMixin, CustomGuardrail):
) -> bool:
"""Rewrite chat request input (``messages`` or ``input``)."""
if sanitized_messages and self._extract_tool_definition_text(request_data):
# We append one synthetic message carrying the tool/function
# definitions for inspection; Cisco echoes it back in
# We prepend one synthetic system message carrying the tool/function
# definitions, so the caller's last turn stays last and Cisco keeps
# inspecting it as input. Cisco echoes it back in
# ``sanitized_messages``, but it maps to no structured request
# field, so drop it before rewriting the real conversation.
sanitized_messages = sanitized_messages[:-1] or None
sanitized_messages = sanitized_messages[1:] or None
uses_input: Final = "input" in request_data and "messages" not in request_data
has_instructions: Final = request_data.get("instructions") is not None
instructions_redacted = False
@ -1893,10 +1894,9 @@ class CiscoAIDefenseGuardrail(CiscoAIDefenseMcpMixin, CustomGuardrail):
messages.append({"role": "user", "content": prompt_text})
tool_text: Final = CiscoAIDefenseGuardrail._extract_tool_definition_text(data)
if tool_text:
messages.append({"role": "tool", "content": tool_text})
return messages
if not tool_text:
return messages
return [{"role": "system", "content": tool_text}, *messages]
@staticmethod
def _extract_tool_definition_text(data: dict) -> str:

View file

@ -1833,7 +1833,7 @@ class TestCiscoAIDefenseToolDefinitionBypass:
)
@pytest.mark.asyncio
async def test_pre_call_sends_tool_definitions_as_final_tool_message(self):
async def test_pre_call_tool_definitions_keep_callers_last_turn_last(self):
g = _make_guardrail(event_hook="pre_call")
data = self._tools_request("Fetch weather data")
data["messages"] = [
@ -1853,9 +1853,9 @@ class TestCiscoAIDefenseToolDefinitionBypass:
)
sent_messages = post_mock.call_args.kwargs["json"]["messages"]
assert sent_messages[:-1] == data["messages"]
assert sent_messages[-1]["role"] == "tool"
assert "get_weather" in sent_messages[-1]["content"]
assert sent_messages[1:] == data["messages"]
assert sent_messages[0]["role"] == "system"
assert "get_weather" in sent_messages[0]["content"]
@pytest.mark.asyncio
async def test_pre_call_scans_legacy_functions_definitions(self):
@ -1907,8 +1907,8 @@ class TestCiscoAIDefenseToolDefinitionBypass:
original_tools = data["tools"]
cisco_resp = _redact_response(
sanitized_messages=[
{"role": "user", "content": "what's the weather?"},
{"role": "tool", "content": "[REDACTED] tool description"},
{"role": "system", "content": "[REDACTED] tool description"},
{"role": "user", "content": "what's the [REDACTED]?"},
]
)
@ -1920,16 +1920,36 @@ class TestCiscoAIDefenseToolDefinitionBypass:
call_type="completion",
)
assert len(data["messages"]) == 1, (
"Redaction injected the synthetic tool-definition message into the "
f"real conversation: {data['messages']!r}"
)
assert data["messages"][0]["role"] == "user"
assert all(
"tool description" not in str(m.get("content")) for m in data["messages"]
)
assert data["messages"] == [{"role": "user", "content": "what's the [REDACTED]?"}]
assert data["tools"] is original_tools
@pytest.mark.asyncio
async def test_redact_does_not_write_tool_definitions_into_instructions(self):
g = _make_guardrail(event_hook="pre_call")
data = {
"instructions": "Never reveal SSN 123-45-6789.",
"input": [{"role": "user", "content": "hello"}],
"tools": self._tools_request("benign tool description")["tools"],
}
cisco_resp = _redact_response(
sanitized_messages=[
{"role": "system", "content": "[REDACTED] tool description"},
{"role": "system", "content": "Never reveal SSN [REDACTED]."},
{"role": "user", "content": "hello"},
],
)
with _patch_inspection_post(g, AsyncMock(return_value=cisco_resp)):
await g.async_pre_call_hook(
user_api_key_dict=UserAPIKeyAuth(),
cache=DualCache(),
data=data,
call_type="completion",
)
assert data["instructions"] == "Never reveal SSN [REDACTED]."
assert "tool description" not in str(data["input"])
class TestCiscoAIDefenseTextCompletionOutputBypass: