diff --git a/scripts/test-layout/layout.json b/scripts/test-layout/layout.json index 0a69a2e9fba..dd0bd035a27 100644 --- a/scripts/test-layout/layout.json +++ b/scripts/test-layout/layout.json @@ -1622,7 +1622,7 @@ "route-explainability.test.ts": "cli", "routed-agent-messages.test.ts": "adapters", "router-blocked-cross-provider.test.ts": "routing", - "router-combo-failover-classification.test.ts": "routing", "combo-failure-transport-parity.test.ts": "routing", + "router-combo-failover-classification.test.ts": "routing", "combo-failover-moderation-prose.test.ts": "routing", "combo-failure-transport-parity.test.ts": "routing", "router-discarded-baseurl-warning.test.ts": "routing", "router-template-baseurl.test.ts": "routing", "router.test.ts": "routing", diff --git a/src/combos/failover.ts b/src/combos/failover.ts index a687b4b821e..d72baa837ad 100644 --- a/src/combos/failover.ts +++ b/src/combos/failover.ts @@ -1226,23 +1226,56 @@ const RESPONSE_REQUEST_ECHO_KEYS = new Set([ * Any other wording (an account, model, parameter or billing problem, in any phrasing) does not * match, so the body keeps dev's decision; only known shapes are admitted. */ -const MODERATION_REFUSAL_PROSE = new RegExp("^(?:" - + "(?:content )?(?:refused|flagged|blocked|rejected|filtered)(?: by (?:the )?(?:moderation|content filter))?\\.?" +// The anchored scan commits to the first verdict alternative that matches, so alternatives run +// longest-first: Azure's fixed wording leads ("the response was filtered" is a strict prefix of it) +// and the noun-phrased verdict precedes the bare one (its `by ... system` clause reaches further). +const MODERATION_REFUSAL_VERDICT = new RegExp("^(?:" + + "the response was filtered due to the prompt triggering azure openai'?s content management policy\\.?" + "|(?:(?:the|your) )?(?:input|request|prompt|content|message|response|output)(?: (?:was|has been|is))?" + " (?:refused|flagged|blocked|rejected|filtered)(?: by (?:the )?(?:moderation|content filter)(?: system)?)?\\.?" - + "|the response was filtered due to the prompt triggering azure openai'?s content management policy\\.?" + + "|(?:content )?(?:refused|flagged|blocked|rejected|filtered)(?: by (?:the )?(?:moderation|content filter))?\\.?" + "|(?:your )?(?:input|request|prompt|content) violated (?:the )?content (?:safety|management|filtering) polic(?:y|ies)\\.?" - + ")(?:" - + " categor(?:y|ies): [a-z][a-z/_-]*(?:(?:,? and |, )[a-z][a-z/_-]*)*\\.?" + + ")", "i"); +const MODERATION_REFUSAL_TAIL = new RegExp("(?:" + + "( categor(?:y|ies): )" + "| please (?:modify|revise|rephrase|change|adjust|edit) (?:your|the) (?:prompt|input|request|message)(?: and (?:retry|try again))?\\.?" + "| (?:please )?(?:retry|try again)(?: later)?\\.?" + "| to learn more about our content filtering policies please read our documentation: https://[^ ]+" - + ")*$", "i"); + + ")", "iy"); +const MODERATION_CATEGORY_WORD = /[a-z][a-z/_-]*/iy; +const MODERATION_CATEGORY_AND = / and /iy; -/** Whether a prose string is a known moderation refusal copy and nothing else (bounded, linear). */ -function isModerationRefusalProse(text: string): boolean { +/** Whether the whole string is known moderation refusal copy. */ +export function isModerationRefusalProse(text: string): boolean { if (text.length > 2_000) return false; - return MODERATION_REFUSAL_PROSE.test(text.replace(/\s+/g, " ").trim()); + const prose = text.replace(/\s+/g, " ").trim(); + const verdict = MODERATION_REFUSAL_VERDICT.exec(prose); + if (!verdict) return false; + + // A comma can introduce the category "and" or the separator ", and ". Keep both + // interpretations until the rest of the sentence distinguishes them. Each token + // boundary is visited once, even when several interpretations reach it. + const tailStarts = new Set([verdict[0].length]); + const categoryStarts = new Set(); + for (let index = verdict[0].length; index < prose.length; index++) { + if (tailStarts.has(index)) { + MODERATION_REFUSAL_TAIL.lastIndex = index; + const tail = MODERATION_REFUSAL_TAIL.exec(prose); + if (tail) { + const next = tail[1] ? categoryStarts : tailStarts; + next.add(MODERATION_REFUSAL_TAIL.lastIndex); + } + } + if (!categoryStarts.has(index)) continue; + MODERATION_CATEGORY_WORD.lastIndex = index; + if (!MODERATION_CATEGORY_WORD.test(prose)) continue; + const end = MODERATION_CATEGORY_WORD.lastIndex; + tailStarts.add(prose[end] === "." ? end + 1 : end); + if (prose.startsWith(", ", end)) categoryStarts.add(end + 2); + MODERATION_CATEGORY_AND.lastIndex = prose[end] === "," ? end + 1 : end; + if (MODERATION_CATEGORY_AND.test(prose)) categoryStarts.add(MODERATION_CATEGORY_AND.lastIndex); + } + return tailStarts.has(prose.length); } /** diff --git a/structure/providers-and-adapters.md b/structure/providers-and-adapters.md index 1c312121cdc..a41948c29a2 100644 --- a/structure/providers-and-adapters.md +++ b/structure/providers-and-adapters.md @@ -588,7 +588,7 @@ Account-row projection is loaded with `import()` after each account probe in `sr ## Combo model refusal evidence -`src/combos/failover.ts` recognizes a bounded, exact HTTP 400 plan/model refusal, not credential or entitlement evidence. It also permits OpenRouter's exact opaque numeric-400 wrapper (`Provider returned error` with `metadata.raw: "ERROR"`, without `type`) to try another combo target. Typed uncoded 400s, malformed `response_format` schema errors and other terminal `invalid_request_error` cases stop and cool the target. An envelope root that states a terminal code of its own stops too (`origin_rejected`, `content_policy_violation`, `context_length_exceeded`, `invalid_request_error`, the shared terminal-refusal set and the non-replayable post-send codes, not a blanket inequality); a root that states only an ordinary hop signal adds no verdict, and an empty code counts as absent. The root code and the record it wraps are both consulted, so a hop needs each to authorize one and a root hard stop is never hidden behind a retryable nested record. Request-local target incompatibilities recognized by `isRequestLocalTargetIncompatibility` and `isResponseFormatCapabilityRefusal` may advance without cooling. So may a provider or gateway moderation verdict on HTTP 400/422 (`isProviderModerationRefusal`), granted on structured evidence only and only for the known shape of a refusal: a `moderation_refusal` or `content_filter` code (the caller's, or a `code`/`error_code` in the body; a `type` or an incomplete `reason` of `content_filter` alone is a model's own output filter and stays terminal) and nothing else. The walk (`moderationVerdictFields`) admits a closed set of fields: verdict codes and types on a short compatible list, a stated status of 400/422 (or a Responses `failed`/`incomplete` state), a `param` naming the prompt or input (or an AI SDK error object read as a record), bare identifiers (`id`, `request_id`, `model`, `name`...), `innererror`, Azure's per-category filter results in their own shape only (category objects of flags and numbers, a `severity` of safe/low/medium/high, bare `id`/`url`/`license` tokens and compatible codes; any other text, or a key naming a state, status, error or message, stops), and a gateway's `providerMetadata` holding plain routing tokens under keys that name no state. Any other field stops. Prose (`message`, a string `error`, plain text with a caller's code) must be known refusal copy as a whole, after whitespace is collapsed: one verdict sentence (OpenAI's "The input was refused by moderation", Azure's content-management sentence, a bare "filtered"...) optionally followed by the category list, the advice to change the prompt and retry, and Azure's documentation pointer; anything else, an account, model, parameter or billing problem in any phrasing, keeps dev's decision. A Responses event's own `response.*` type is read as the carrier, whose response object is read without the fields that echo the caller's request (`instructions`, `tools`, `text`, `reasoning`, `input`, `output`, `usage`...); its `metadata` must be empty. Prose alone never hops (owner decision on #8428). It never outranks a stop: an envelope root verdict code, a hard-refusal token in any spelling (JSON-escaped, camelCase, or with hyphens, dots, slashes or spaces for underscores), safety or terminal-refusal copy, a malformed `response_format` diagnostic, subscription-gate wording, or any text the shared `classifyError` files more specifically than a generic invalid request keeps the terminal decision and its cooldown, and quota, billing or account-state evidence (including such wording beside a moderation code) still cools the provider. The verdict needs the whole body: `consumeComboFailure`, the responses stream preflight, the Messages native preflight and `policy-fallback.ts` judge the complete bounded body or event and pass `providerModerationRefusal`; a preflight that rebuilds a slim envelope attaches the whole event as combo failure evidence whenever either reads as a moderation verdict, and an event that states a non-400/422 status of its own is never a moderation refusal; and without it a classification text of 500 characters or more (possibly cut) never hops. +`src/combos/failover.ts` recognizes a bounded, exact HTTP 400 plan/model refusal, not credential or entitlement evidence. It also permits OpenRouter's exact opaque numeric-400 wrapper (`Provider returned error` with `metadata.raw: "ERROR"`, without `type`) to try another combo target. Typed uncoded 400s, malformed `response_format` schema errors and other terminal `invalid_request_error` cases stop and cool the target. An envelope root that states a terminal code of its own stops too (`origin_rejected`, `content_policy_violation`, `context_length_exceeded`, `invalid_request_error`, the shared terminal-refusal set and the non-replayable post-send codes, not a blanket inequality); a root that states only an ordinary hop signal adds no verdict, and an empty code counts as absent. The root code and the record it wraps are both consulted, so a hop needs each to authorize one and a root hard stop is never hidden behind a retryable nested record. Request-local target incompatibilities recognized by `isRequestLocalTargetIncompatibility` and `isResponseFormatCapabilityRefusal` may advance without cooling. So may a provider or gateway moderation verdict on HTTP 400/422 (`isProviderModerationRefusal`), granted on structured evidence only and only for the known shape of a refusal: a `moderation_refusal` or `content_filter` code (the caller's, or a `code`/`error_code` in the body; a `type` or an incomplete `reason` of `content_filter` alone is a model's own output filter and stays terminal) and nothing else. The walk (`moderationVerdictFields`) admits a closed set of fields: verdict codes and types on a short compatible list, a stated status of 400/422 (or a Responses `failed`/`incomplete` state), a `param` naming the prompt or input (or an AI SDK error object read as a record), bare identifiers (`id`, `request_id`, `model`, `name`...), `innererror`, Azure's per-category filter results in their own shape only (category objects of flags and numbers, a `severity` of safe/low/medium/high, bare `id`/`url`/`license` tokens and compatible codes; any other text, or a key naming a state, status, error or message, stops), and a gateway's `providerMetadata` holding plain routing tokens under keys that name no state. Any other field stops. Prose (`message`, a string `error`, plain text with a caller's code) must be known refusal copy as a whole, after whitespace is collapsed: one verdict sentence (OpenAI's "The input was refused by moderation", Azure's content-management sentence, a bare "filtered"...) optionally followed by the category list (where `and` can be a name or a separator), the advice to change the prompt and retry, and Azure's documentation pointer; anything else, an account, model, parameter or billing problem in any phrasing, keeps dev's decision. A Responses event's own `response.*` type is read as the carrier, whose response object is read without the fields that echo the caller's request (`instructions`, `tools`, `text`, `reasoning`, `input`, `output`, `usage`...); its `metadata` must be empty. Prose alone never hops (owner decision on #8428). It never outranks a stop: an envelope root verdict code, a hard-refusal token in any spelling (JSON-escaped, camelCase, or with hyphens, dots, slashes or spaces for underscores), safety or terminal-refusal copy, a malformed `response_format` diagnostic, subscription-gate wording, or any text the shared `classifyError` files more specifically than a generic invalid request keeps the terminal decision and its cooldown, and quota, billing or account-state evidence (including such wording beside a moderation code) still cools the provider. The verdict needs the whole body: `consumeComboFailure`, the responses stream preflight, the Messages native preflight and `policy-fallback.ts` judge the complete bounded body or event and pass `providerModerationRefusal`; a preflight that rebuilds a slim envelope attaches the whole event as combo failure evidence whenever either reads as a moderation verdict, and an event that states a non-400/422 status of its own is never a moderation refusal; and without it a classification text of 500 characters or more (possibly cut) never hops. `src/server/responses/core-combo-failure.ts` captures its finite classification before display truncation; `src/server/responses/combo-stream-preflight.ts` preserves it before rebuilding terminal SSE errors. `src/server/responses/core-options.ts` carries the value to the combo decision. An inspected non-match diff --git a/tests/fixtures/test-layout-expected.json b/tests/fixtures/test-layout-expected.json index 3691aaa6489..fb3e1bfbfba 100644 --- a/tests/fixtures/test-layout-expected.json +++ b/tests/fixtures/test-layout-expected.json @@ -1391,7 +1391,7 @@ "restore-completes-shared-teardown.test.ts": "cli", "retry-after-429.test.ts": "server", "route-decision-trace.test.ts": "server", "route-explainability.test.ts": "cli", "routed-agent-messages.test.ts": "adapters", "router-blocked-cross-provider.test.ts": "routing", - "router-combo-failover-classification.test.ts": "routing", "combo-failure-transport-parity.test.ts": "routing", "router-discarded-baseurl-warning.test.ts": "routing", + "router-combo-failover-classification.test.ts": "routing", "combo-failover-moderation-prose.test.ts": "routing", "combo-failure-transport-parity.test.ts": "routing", "router-discarded-baseurl-warning.test.ts": "routing", "router-template-baseurl.test.ts": "routing", "router.test.ts": "routing", "routing-analytics.test.ts": "routing", "routing-capability-catalog.test.ts": "routing", "routing-capability-model-matching.test.ts": "routing", "routing-compatibility-auth-identity.test.ts": "routing", diff --git a/tests/routing/combo-failover-moderation-prose.test.ts b/tests/routing/combo-failover-moderation-prose.test.ts new file mode 100644 index 00000000000..8b4e0c263c5 --- /dev/null +++ b/tests/routing/combo-failover-moderation-prose.test.ts @@ -0,0 +1,70 @@ +import { describe, expect, test } from "bun:test"; +import { isModerationRefusalProse } from "../../src/combos/failover"; + +/** + * Direct coverage of the moderation-refusal prose matcher. The pipeline-level + * classification lives in router-combo-failover-classification.test.ts; this + * file pins the matcher's accepted language and its linear bound. + */ +describe("moderation refusal prose matcher", () => { + const accepted = [ + "refused", + "Content refused.", + "The input was refused by moderation.", + "Prompt was blocked by moderation.", + "Input was flagged by moderation.", + "Your request was rejected by the content filter system.", + "the response was filtered due to the prompt triggering azure openai's content management policy.", + "the response was filtered due to the prompt triggering azure openais content management policy.", + // A shorter verdict alternative must not win over the noun-phrased one that + // reaches `by ... system` — the scan commits to the first verdict match. + "content refused by moderation system.", + "The request was blocked by the content filter system.", + // `, and ` must not swallow retry-advice openings as category names. + "refused category: harassment, and retry later.", + "refused category: harassment, and please change your prompt.", + "content refused. category: spam, and please", + "refused category: a, and and b", + "Your input violated the content safety policies.", + "input violated content filtering policy", + "refused category: a, b and c. please change your prompt and retry.", + "refused categories: a, and b, c. retry later.", + "refused please modify the prompt", + "refused try again", + "refused. to learn more about our content filtering policies please read our documentation: https://example.com/x", + "flagged by content filter. category: harassment, hate. please revise your input.", + ]; + test.each(accepted)("admits %s", prose => { + expect(isModerationRefusalProse(prose)).toBe(true); + }); + + const rejected = [ + "your account is suspended", + "quota exceeded: billing required", + "model not found", + "refused category: a!", + "refused and retried", + "please retry", + "category: harassment", + "", + ]; + test.each(rejected)("keeps dev's decision on %s", prose => { + expect(isModerationRefusalProse(prose)).toBe(false); + }); + + test("rejects a crafted category list in bounded time", () => { + // CodeQL js/redos: the old single-regex shape backtracked exponentially on + // "refused category: a" + ", and retry category: a" repeated before failing. + const attack = "refused category: a" + ", and retry category: a".repeat(80) + "!"; + const started = performance.now(); + expect(isModerationRefusalProse(attack)).toBe(false); + expect(performance.now() - started).toBeLessThan(1_000); + }); + + test("matches a crafted category list that happens to be accepted in bounded time", () => { + const attack = "refused category: a" + ", and retry category: a".repeat(80); + const started = performance.now(); + expect(isModerationRefusalProse(attack)).toBe(true); + expect(performance.now() - started).toBeLessThan(1_000); + }); +});