fix(panw_prisma_airs): resolve latest-only scope from whichever message source aligns

Devin flagged that an explicit `true` still scanned full history on
/v1/responses. Correct: those requests carry `input` rather than `messages`,
so request_data.get("messages") was empty and the code fell through to the
role filter. The config text claiming "every request shape" overstated it.

Neither source is right everywhere, so the fix is to try both and let the
existing count check in _get_latest_user_text_indices select the aligned one:

- request_data["messages"] is the original, unfiltered list, and is the
  aligned source for Anthropic /v1/messages, whose structured_messages carries
  a translation-injected system entry with no counterpart in `texts`.
- structured_messages is built alongside `texts` by the translation handler,
  and is the aligned source when a skip flag scoped a message out upstream -
  and the only source at all for /v1/responses.

Preferring structured_messages outright (as bedrock does) regresses the
Anthropic case; preferring request_data["messages"] outright misses Responses.
Trying both fixes Responses without giving up Anthropic, and when neither
walks to the same text count the role filter engages exactly as before.

This also improves the scoped-out-upstream case: previously a mismatch on
request_data["messages"] degraded to a full-history scan even though
structured_messages aligned. That test now asserts latest-only instead.

Adds Responses-shape coverage (flag true scans latest only, flag unset keeps
full history) and a both-sources-mismatch test for the safety fallback that
the updated test no longer exercises. Suite 224 -> 227.
This commit is contained in:
scthornton 2026-09-10 22:17:45 -04:00
parent 6d0e2eed1a
commit 7a1cf6509f
2 changed files with 139 additions and 11 deletions

View file

@ -1779,12 +1779,27 @@ class PanwPrismaAirsHandler(CustomGuardrail):
if input_type == "request":
structured_messages: Final = inputs.get("structured_messages")
if structured_messages:
# Uses request_data["messages"] (original format), NOT structured_messages
# (which has injected system content from adapter translation).
# Neither message source aligns with `texts` on every request shape,
# so try both and let the count check in
# _get_latest_user_text_indices pick the one that does:
# - request_data["messages"] is the original, unfiltered list. It
# is the aligned source for Anthropic /v1/messages, whose
# structured_messages carries a system entry injected by
# translation that has no counterpart in `texts`.
# - structured_messages is built alongside `texts` by the
# translation handler. It is the aligned source when a skip flag
# scoped a message out upstream, and the only source at all for
# /v1/responses, whose request_data carries `input` rather than
# `messages`.
# If neither walks to the same text count, scannable_indices stays
# None and the existing role filter below engages unchanged.
if self._use_latest_user_only(request_data, logging_obj):
original_messages: Final = request_data.get("messages")
if original_messages:
scannable_indices = self._get_latest_user_text_indices(texts, original_messages)
for candidate in (request_data.get("messages"), structured_messages):
if not candidate:
continue
scannable_indices = self._get_latest_user_text_indices(texts, candidate)
if scannable_indices is not None:
break
# Fall through to existing role filtering if:
# - latest-only not enabled for this request, OR
# - no original messages, OR

View file

@ -5051,12 +5051,14 @@ class TestPanwAirsLatestRoleMessageOnlyOpenAIShape:
@pytest.mark.asyncio
async def test_flag_true_count_mismatch_falls_back_to_role_filter(self):
"""OpenAI shape + flag True + texts/messages count mismatch: safety fallback.
"""OpenAI shape + flag True + request_data["messages"] mismatch:
structured_messages carries the scope.
When request_data["messages"] does not walk to the same text count as
the flattened texts (e.g. a message was scoped out upstream),
_get_latest_user_text_indices returns None and the existing role-filter
scan over structured_messages must engage.
structured_messages is the source that stays aligned with `texts`, so
latest-only scanning still engages through it rather than degrading to
a full-history scan.
"""
handler = make_handler(experimental_use_latest_role_message_only=True)
@ -5091,10 +5093,11 @@ class TestPanwAirsLatestRoleMessageOnlyOpenAIShape:
input_type="request",
)
# Fallback role-filter scan: all system/user texts scanned
assert mock_api.call_count == 3
# structured_messages aligns with texts, so only the latest user
# message is scanned - no degradation to a full-history scan.
assert mock_api.call_count == 1
scanned = [call.kwargs["content"] for call in mock_api.call_args_list]
assert scanned == ["system text", "user one", "user two"]
assert scanned == ["user two"]
@pytest.mark.asyncio
async def test_flag_true_no_user_message_falls_back(self):
@ -5244,6 +5247,116 @@ class TestPanwAirsLatestRoleMessageOnlyOpenAIShape:
assert mock_api.call_count == 2
class TestPanwAirsLatestRoleMessageOnlyResponsesShape:
"""/v1/responses requests carry `input`, not `messages`, so request_data has no
messages list. structured_messages is the only aligned source there."""
@pytest.mark.asyncio
async def test_responses_shape_scans_latest_user_only(self):
handler = make_handler(experimental_use_latest_role_message_only=True)
inputs: GenericGuardrailAPIInputs = {
"texts": ["first user turn", "first assistant turn", "latest user turn"],
"structured_messages": [
{"role": "user", "content": "first user turn"},
{"role": "assistant", "content": "first assistant turn"},
{"role": "user", "content": "latest user turn"},
],
}
# No "messages" key - this is what a Responses request looks like.
request_data = {
"litellm_call_id": "test-call-id",
"model": "gpt-4.1",
"input": "latest user turn",
"proxy_server_request": {"url": "http://localhost:4000/v1/responses"},
}
with patch.object(
handler, "_call_panw_api", new_callable=AsyncMock
) as mock_api:
mock_api.return_value = {"action": "allow", "category": "benign"}
await handler.apply_guardrail(
inputs=inputs, request_data=request_data, input_type="request"
)
assert mock_api.call_count == 1
scanned = [c.kwargs["content"] for c in mock_api.call_args_list]
assert scanned == ["latest user turn"]
@pytest.mark.asyncio
async def test_responses_shape_full_history_when_flag_unset(self):
handler = make_handler()
inputs: GenericGuardrailAPIInputs = {
"texts": ["first user turn", "latest user turn"],
"structured_messages": [
{"role": "user", "content": "first user turn"},
{"role": "user", "content": "latest user turn"},
],
}
request_data = {
"litellm_call_id": "test-call-id",
"model": "gpt-4.1",
"input": "latest user turn",
"proxy_server_request": {"url": "http://localhost:4000/v1/responses"},
}
with patch.object(
handler, "_call_panw_api", new_callable=AsyncMock
) as mock_api:
mock_api.return_value = {"action": "allow", "category": "benign"}
result = await handler.apply_guardrail(
inputs=inputs, request_data=request_data, input_type="request"
)
# Unset default is unchanged for non-Anthropic shapes: every user turn
# is sent for scanning, and all texts pass through on allow.
scanned = [c.kwargs["content"] for c in mock_api.call_args_list]
assert scanned == ["first user turn", "latest user turn"]
assert result["texts"] == ["first user turn", "latest user turn"]
@pytest.mark.asyncio
async def test_both_sources_mismatch_falls_back_to_role_filter(self):
"""When neither message source walks to the same text count, the existing
role-filter scan engages - the original safety fallback."""
handler = make_handler(experimental_use_latest_role_message_only=True)
inputs: GenericGuardrailAPIInputs = {
"texts": ["system text", "user one", "user two"],
"structured_messages": [
{"role": "system", "content": "system text"},
{"role": "user", "content": "user one"},
{"role": "user", "content": "user two"},
{"role": "user", "content": "extra, not in texts"},
],
}
request_data = {
"litellm_call_id": "test-call-id",
"model": "gemini-2.5-flash",
"messages": [
{"role": "system", "content": "system text"},
{"role": "user", "content": "user one"},
{"role": "user", "content": "user two"},
{"role": "user", "content": "also not in texts"},
],
}
with patch.object(
handler, "_call_panw_api", new_callable=AsyncMock
) as mock_api:
mock_api.return_value = {"action": "allow", "category": "benign"}
await handler.apply_guardrail(
inputs=inputs, request_data=request_data, input_type="request"
)
assert mock_api.call_count == 3
scanned = [c.kwargs["content"] for c in mock_api.call_args_list]
assert scanned == ["system text", "user one", "user two"]
class TestPanwAirsLatestRoleMessageOnlyStartupWarning:
"""Enabling latest-only scanning trusts caller-supplied history, so the operator
gets an explicit startup warning - same treatment as fallback_on_error='allow'."""