diff --git a/scripts/test-layout/layout-combos.json b/scripts/test-layout/layout-combos.json index 0863446effc..66408dea339 100644 --- a/scripts/test-layout/layout-combos.json +++ b/scripts/test-layout/layout-combos.json @@ -5,6 +5,7 @@ "server-combo-quota-code-provenance.test.ts": "server", "combo-codex-ws-overload-failover.test.ts": "responses", "combo-cooldown-persistence.test.ts": "routing", + "combo-unoffered-tool-refusal.test.ts": "routing", "server-combo-cooldown-recording.test.ts": "server", "combo-selection-keepalive.test.ts": "routing", "server-combo-selection-keepalive.test.ts": "server" diff --git a/src/combos/failover.ts b/src/combos/failover.ts index 12e06ef9abd..b59b34ab209 100644 --- a/src/combos/failover.ts +++ b/src/combos/failover.ts @@ -998,6 +998,162 @@ function isResponseFormatCapabilityRefusal( return false; } +/** + * Codes, types and params an unoffered-tool-call refusal may carry. The reported carrier uses + * `type: "upstream_error"` with `code: null` -- an aggregator re-wrapping the model backend's + * 400 -- while request-validation spellings stay accepted because gateways disagree on which + * they emit. Anything outside these closed sets fails closed. + */ +const UNOFFERED_TOOL_CALL_CODES = new Set(["", "invalid_request_error", "invalid_argument", "upstream_error"]); +const UNOFFERED_TOOL_CALL_TYPES = new Set(["", "invalid_request_error", "upstream_error", "ai_apicallerror"]); +const UNOFFERED_TOOL_CALL_PARAMS = new Set(["tools", "tool_choice"]); +/** + * The provider's own verdict sentences for target-local model failures, each anchored so a + * validation message that merely quotes or reflects one cannot impersonate the verdict -- the + * same failure mode moderation-refusal matching guards against. Both observed carriers arrive + * as `type: "upstream_error"` with `code: null` behind aggregator re-wraps. + */ +const UNOFFERED_TOOL_CALL_MESSAGES = [ + /^the model called a function that was not offered in tools\.?$/i, + /^the model returned an unexpected parser control marker; no output was published\.?$/i, +] as const; +/** Sibling records that could smuggle a competing verdict this classifier does not inspect. */ +const UNOFFERED_TOOL_CALL_CARRIERS = ["response", "last_error", "details", "detail", "errors"] as const; + +function isUnofferedToolCallVerdict(text: string): boolean { + const value = text.trim().replace(/^Provider error \d{3}: /, "").trim(); + return UNOFFERED_TOOL_CALL_MESSAGES.some(pattern => pattern.test(value)); +} + +/** + * A root message that carries its own envelope prefix is peeled and its contents must still + * support the verdict: `Provider error 400: Invalid value for tools` contradicts a nested + * refusal even though the prefix itself is recognized. + */ +function isUnofferedToolCallCarrierVerdict(text: string): boolean { + let value = text.trim(); + for (let depth = 0; depth < 3; depth += 1) { + const proxyPrefix = /^Provider error \d{3}: /.exec(value); + if (proxyPrefix) { + value = value.slice(proxyPrefix[0].length).trim(); + continue; + } + if (value.startsWith("data:") && !value.includes("\n")) { + value = value.slice("data:".length).trim(); + continue; + } + break; + } + return isUnofferedToolCallVerdict(value); +} + +function hasUnofferedToolCallCarrierPrefix(text: string): boolean { + return /^Provider error \d{3}: /.test(text) || (text.startsWith("data:") && !text.includes("\n")); +} + +function unofferedToolCallRecordShape(record: Record): boolean { + if (record.code !== undefined && record.code !== null && typeof record.code !== "string" && typeof record.code !== "number") return false; + if (record.type !== undefined && record.type !== null && typeof record.type !== "string") return false; + if (record.param !== undefined && record.param !== null) { + if (typeof record.param === "string") { + if (!UNOFFERED_TOOL_CALL_PARAMS.has(record.param.trim().toLowerCase())) return false; + } else if (typeof record.param === "object" && !Array.isArray(record.param)) { + // An AI-SDK gateway wraps the verdict in an error object under `param`; accept it only + // when a nested string field independently carries the same anchored verdict, so the + // object cannot smuggle a different claim. + const nested = record.param as Record; + const diagnostics = [nested.error, nested.message].filter((v) => v !== undefined && v !== null); + if (diagnostics.length === 0) return false; + for (const diagnostic of diagnostics) { + if (typeof diagnostic !== "string" || !isUnofferedToolCallVerdict(diagnostic)) return false; + } + } else { + return false; + } + } + if (!UNOFFERED_TOOL_CALL_CODES.has(normalizedFailureCode(record.code as string | number | undefined))) return false; + if (!UNOFFERED_TOOL_CALL_TYPES.has(normalizedFailureCode(record.type as string | undefined))) return false; + for (const carrier of UNOFFERED_TOOL_CALL_CARRIERS) { + if (record[carrier] !== undefined && record[carrier] !== null) return false; + } + return true; +} + +/** + * A target-local model verdict -- an unoffered tool call, or output the transport could not + * publish -- is evidence a later combo target may not repeat: the provider answered, the + * request fit, and the failure arrives before any output commits, so advancing is replay-safe. + * + * The envelope is bounded like its siblings: a depth budget, one `Provider error NNN:` or + * single-line `data:` unwrap per level, closed code/type/param sets, and fail-closed treatment + * of any record carrying a competing diagnostic carrier or a contradicting root message. + * Bare verdict text is also a carrier: a chat gateway can surface the sentence with no JSON + * envelope at all. + */ +export function isUnofferedToolCallRefusal( + status: number, + message: string, + code?: string | number | null, +): boolean { + if (status !== 400 || message.length > 16_384) return false; + if (code !== undefined && code !== null && typeof code !== "string" && typeof code !== "number") return false; + if (!UNOFFERED_TOOL_CALL_CODES.has(normalizedFailureCode(code))) return false; + let text = message.trim(); + for (let depth = 0; depth < 4; depth += 1) { + const proxyPrefix = /^Provider error \d{3}: /.exec(text); + if (proxyPrefix) text = text.slice(proxyPrefix[0].length).trim(); + if (text.startsWith("data:") && !text.includes("\n")) text = text.slice("data:".length).trim(); + let payload: unknown; + try { + payload = JSON.parse(text); + } catch { + return isUnofferedToolCallVerdict(text); + } + if (!payload || typeof payload !== "object" || Array.isArray(payload)) return false; + const root = payload as Record; + if (!unofferedToolCallRecordShape(root)) return false; + const error = root.error; + // A nested verdict must agree with the root's own message. When the root carries no error + // record its message IS the payload carrier and is validated by the flat path below. + if (error !== undefined && error !== null + && typeof root.message === "string" && root.message.trim() !== "" + && !isUnofferedToolCallCarrierVerdict(root.message)) return false; + if (error === undefined || error === null) { + if (typeof root.message !== "string") return false; + if (hasUnofferedToolCallCarrierPrefix(root.message.trim())) { + text = root.message.trim(); + continue; + } + return isUnofferedToolCallVerdict(root.message); + } + if (typeof error === "string") { + if (hasUnofferedToolCallCarrierPrefix(error.trim())) { + text = error.trim(); + continue; + } + return isUnofferedToolCallVerdict(error); + } + if (typeof error !== "object" || Array.isArray(error)) return false; + const record = error as Record; + if (!unofferedToolCallRecordShape(record)) return false; + if (record.error !== undefined && record.error !== null) { + if (typeof record.error !== "string") return false; + if (record.error.trim() !== "" && !isUnofferedToolCallVerdict(record.error)) return false; + } + if (root.param !== undefined && root.param !== null && record.param !== undefined && record.param !== null + && String(root.param).trim().toLowerCase() !== String(record.param).trim().toLowerCase()) return false; + if (typeof record.message !== "string") return false; + const inner = record.message.trim(); + if (hasUnofferedToolCallCarrierPrefix(inner)) { + // Our own wrapper, re-wrapped by a downstream hop. Peel it and look again, within budget. + text = inner; + continue; + } + return isUnofferedToolCallVerdict(record.message); + } + return false; +} + /** The classification text the combo path bounds a body to (src/server/responses/core-errors.ts). */ const MODERATION_EVIDENCE_LIMIT = 500; @@ -1324,6 +1480,7 @@ export function comboFailureCooldownScope( code?: string | number | null; malformedResponseFormat?: boolean; openRouterOpaque400?: boolean; + unofferedToolCallRefusal?: boolean; /** Whole-body moderation verdict from a reader that saw the complete bounded body. */ providerModerationRefusal?: boolean; }, @@ -1345,6 +1502,13 @@ export function comboFailureCooldownScope( // A capability gap says the target is healthy and the request did not fit it, which is the // same reason every other entry here refuses to cool a target. || isResponseFormatCapabilityRefusal(status, message, rawCode) + // The provider answered and its model simply called a function it was never offered: the + // request fit and the target is healthy, so this must not cool it either. A caller that + // classified the intact bounded body supplies the verdict directly; the display-truncated + // text here would fail the envelope parse on a long body. + || (options?.unofferedToolCallRefusal !== undefined + ? options.unofferedToolCallRefusal + : isUnofferedToolCallRefusal(status, message, rawCode)) ) return "none"; if (isProviderScopedQuotaCap(status, message, rawCode)) return "provider"; // A rejected or unpaid credential is provider-wide evidence: every target that routes @@ -1491,6 +1655,7 @@ export function comboFailureDecision( verifiedPlanRefusal?: boolean; malformedResponseFormat?: boolean; openRouterOpaque400?: boolean; + unofferedToolCallRefusal?: boolean; /** Whole-body moderation verdict from a reader that saw the complete bounded body. */ providerModerationRefusal?: boolean; }, @@ -1519,6 +1684,7 @@ function decideComboFailure( verifiedPlanRefusal?: boolean; malformedResponseFormat?: boolean; openRouterOpaque400?: boolean; + unofferedToolCallRefusal?: boolean; /** Whole-body moderation verdict from a reader that saw the complete bounded body. */ providerModerationRefusal?: boolean; }, @@ -1610,6 +1776,14 @@ function decideComboFailure( // could have served the request (#4903). if (isResponseFormatCapabilityRefusal(status, message, rawCode)) return "hop"; if (providerModerationHop(status, message, rawCode, options?.providerModerationRefusal, options?.malformedResponseFormat)) return "hop"; + // A model that calls a function the request never offered hallucinated the call: target-local + // behavior on a healthy target, arriving as a pre-output 400. Must precede the generic + // invalid_request_error stop, which is where this envelope ended the chain. A caller that + // classified the intact bounded body supplies the verdict directly; the display-truncated + // text here would fail the envelope parse on a long body. + if (options?.unofferedToolCallRefusal !== undefined + ? options.unofferedToolCallRefusal + : isUnofferedToolCallRefusal(status, message, rawCode)) return "hop"; if (isAnthropicBillingFailure(status, message, rawCode)) return "hop"; if (isAnthropicExtraUsageFailure(status, message, rawCode)) return "hop"; // Kiro's body validator rejects the request for the identity it was routed under. The account diff --git a/src/server/responses/core-combo-failure.ts b/src/server/responses/core-combo-failure.ts index b6753b16b1c..2fae14e4e8e 100644 --- a/src/server/responses/core-combo-failure.ts +++ b/src/server/responses/core-combo-failure.ts @@ -8,6 +8,7 @@ import { codexAccountModelRefusalHardStopCode, isMalformedResponseFormatRequest, isOpenRouterOpaque400Envelope, + isUnofferedToolCallRefusal, isProviderModerationRefusal, upstreamEnvelopeRootCode, type CodexAccountModelRefusal, @@ -67,6 +68,7 @@ export async function consumeComboFailure( let codexModelRefusal: CodexAccountModelRefusal | undefined; let malformedResponseFormat: boolean | undefined; let openRouterOpaque400: boolean | undefined; + let unofferedToolCallRefusal: boolean | undefined; let providerModerationRefusal: boolean | undefined; let usage: OcxUsage | undefined; let upstreamCode: string | undefined; @@ -108,6 +110,9 @@ export async function consumeComboFailure( codexModelRefusal = codexAccountModelRefusal(response.status, body.text); malformedResponseFormat = isMalformedResponseFormatRequest(response.status, body.text); openRouterOpaque400 = isOpenRouterOpaque400Envelope(body.text); + // The intact bounded body, not the 500-char display prefix: a long refusal envelope + // would fail the classifier's JSON parse on truncated text and stop the combo. + unofferedToolCallRefusal = isUnofferedToolCallRefusal(response.status, body.text); } // Judged on the whole body: the classification text below is cut at 500 characters, and a // hard refusal after the cut must still veto a moderation hop. @@ -196,6 +201,7 @@ export async function consumeComboFailure( ...(codexModelRefusal !== undefined ? { codexModelRefusal } : {}), ...(malformedResponseFormat !== undefined ? { malformedResponseFormat } : {}), ...(openRouterOpaque400 !== undefined ? { openRouterOpaque400 } : {}), + ...(unofferedToolCallRefusal !== undefined ? { unofferedToolCallRefusal } : {}), ...(providerModerationRefusal !== undefined ? { providerModerationRefusal } : {}), ...(normalizedUpstreamCode !== undefined ? { upstreamCode: normalizedUpstreamCode } : {}), // A rewritten identity (policy, replay refusal) is the whole verdict; the root's own code diff --git a/src/server/responses/core-combo.ts b/src/server/responses/core-combo.ts index 74a5d4f918b..5a330449309 100644 --- a/src/server/responses/core-combo.ts +++ b/src/server/responses/core-combo.ts @@ -1478,6 +1478,7 @@ export async function executeComboResponses( verifiedPlanRefusal: failure.verifiedPlanRefusal, malformedResponseFormat: failure.malformedResponseFormat, openRouterOpaque400: failure.openRouterOpaque400, + unofferedToolCallRefusal: failure.unofferedToolCallRefusal, providerModerationRefusal: failure.providerModerationRefusal, // The adapter the failed leg actually resolved: a configured `kiro` provider can route a // model through another wire via `modelAdapters`, and an unresolved leg grants no hop. @@ -1557,6 +1558,7 @@ export async function executeComboResponses( code: failure.upstreamCode, malformedResponseFormat: failure.malformedResponseFormat, openRouterOpaque400: failure.openRouterOpaque400, + unofferedToolCallRefusal: failure.unofferedToolCallRefusal, providerModerationRefusal: failure.providerModerationRefusal, }); const failedTargetKey = targetKey(pick.target); diff --git a/src/server/responses/core-options.ts b/src/server/responses/core-options.ts index 4b4fa216b18..a6cd0dcc860 100644 --- a/src/server/responses/core-options.ts +++ b/src/server/responses/core-options.ts @@ -37,6 +37,8 @@ export interface ConsumedComboFailure { malformedResponseFormat?: boolean; /** Complete bounded-envelope OpenRouter opaque-400 verdict captured before display truncation. */ openRouterOpaque400?: boolean; + /** Complete bounded-envelope unoffered-tool-call verdict captured before display truncation. */ + unofferedToolCallRefusal?: boolean; /** Complete bounded-envelope provider moderation verdict captured before display truncation. */ providerModerationRefusal?: boolean; /** Structured upstream `error.code` when present in the failure body. */ diff --git a/structure/transports/responses-combo-boundaries.md b/structure/transports/responses-combo-boundaries.md index ba8ce5b2f87..cf2dc5f4e52 100644 --- a/structure/transports/responses-combo-boundaries.md +++ b/structure/transports/responses-combo-boundaries.md @@ -28,6 +28,20 @@ Every physical attempt retains its own send count and usage. Coverage: `tests/he (registered by `tests/server/server-combo-failover-e2e.test.ts`), `tests/server/server-combo-reasoning-replay-eligibility.test.ts` and `tests/responses/responses-combo-exhausted-error.test.ts`. +## Unoffered tool-call refusals + +`isUnofferedToolCallRefusal` in `src/combos/failover.ts` recognizes the provider verdicts for +target-local model failures: that a model called a function the request never offered (`The model +called a function that was not offered in tools.`) alongside the parser control-marker refusal +(`The model returned an unexpected parser control marker; no output was published.`). The match is +anchored to each sentence inside a bounded envelope walk — closed code/type/param sets, competing +diagnostic carriers and a contradicting root message all fail closed — so quoted or reflected +occurrences cannot impersonate the verdict. `consumeComboFailure` classifies the intact bounded body +and passes the verdict through `unofferedToolCallRefusal`, because the 500-character display prefix +would leave a long envelope unparseable. Like the other pre-output 400 hops it advances the combo +without cooling the target: the model hallucinated the call or control marker on this target, the +request itself fit, and the refusal arrives before any output commits. Coverage: `tests/routing/combo-unoffered-tool-refusal.test.ts`. + ## Buffered required-tool contract Before selection, `src/server/responses/core-combo.ts` excludes resolved Meta Responses destinations diff --git a/tests/fixtures/test-layout-expected.json b/tests/fixtures/test-layout-expected.json index 1e5efac52dd..1be6f7c27a3 100644 --- a/tests/fixtures/test-layout-expected.json +++ b/tests/fixtures/test-layout-expected.json @@ -165,6 +165,7 @@ "responses-undeclared-tool-guard-inbound.test.ts": "responses", "messages-request-id-endpoint.test.ts": "claude-integration", "combo-cooldown-persistence.test.ts": "routing", + "combo-unoffered-tool-refusal.test.ts": "routing", "anthropic-revoked-token.test.ts": "adapters/anthropic", "messages-revoked-token.test.ts": "claude-integration", "anthropic-revoked-token-continuation.test.ts": "adapters/anthropic", diff --git a/tests/routing/combo-unoffered-tool-refusal.test.ts b/tests/routing/combo-unoffered-tool-refusal.test.ts new file mode 100644 index 00000000000..528cf5ba7f1 --- /dev/null +++ b/tests/routing/combo-unoffered-tool-refusal.test.ts @@ -0,0 +1,156 @@ +import { describe, expect, test } from "bun:test"; +import { + comboFailureCooldownScope, + comboFailureDecision, +} from "../../src/combos/failover"; +import { consumeComboFailure } from "../../src/server/responses/core-combo-failure"; + +describe("unoffered tool-call refusal", () => { + const verdict = "The model called a function that was not offered in tools."; + // The reported carrier: an aggregator re-wraps the model backend's 400 as + // `type: "upstream_error"` with `code: null`, then the proxy wraps that envelope again. + const reported = JSON.stringify({ + error: { message: `Provider error 400: ${verdict}`, type: "upstream_error", code: null }, + }); + + test("hops without cooling through every carrier the report shows", () => { + for (const message of [ + reported, + `Provider error 400: ${reported}`, + `data: ${reported}`, + `Provider error 400: data: ${reported}`, + verdict, + `Provider error 400: ${verdict}`, + JSON.stringify({ message: verdict, type: "upstream_error", code: null }), + JSON.stringify({ error: verdict, type: "upstream_error", code: null }), + ]) { + expect(comboFailureDecision(400, message)).toBe("hop"); + expect(comboFailureCooldownScope(400, message)).toBe("none"); + } + expect(comboFailureDecision(400, reported, { code: "upstream_error" })).toBe("hop"); + expect(comboFailureCooldownScope(400, reported, { code: "upstream_error" })).toBe("none"); + }); + + test("request-validation spellings of the same verdict still hop", () => { + for (const type of ["invalid_request_error", "upstream_error"]) { + for (const param of [undefined, "tools", "tool_choice"]) { + const body = JSON.stringify({ + error: { message: verdict, type, code: null, ...(param ? { param } : {}) }, + }); + expect(comboFailureDecision(400, body)).toBe("hop"); + expect(comboFailureCooldownScope(400, body)).toBe("none"); + } + } + }); + + test("echoed or competing evidence stays terminal", () => { + for (const message of [ + // The verdict quoted inside a validation message is reflected text, not a provider verdict. + JSON.stringify({ error: { message: `Invalid value: "${verdict}"`, type: "invalid_request_error", code: null } }), + // Trailing content means the sentence is not the whole verdict. + JSON.stringify({ error: { message: `${verdict} Request id: abc`, type: "upstream_error", code: null } }), + // A param naming an unrelated field contradicts the refusal. + JSON.stringify({ error: { message: verdict, type: "invalid_request_error", code: null, param: "messages" } }), + // A competing diagnostic carrier could hide a hard refusal; fail closed. + JSON.stringify({ error: { message: verdict, type: "upstream_error", code: null }, details: { code: "cyber_policy" } }), + // A contradicting root message keeps the root authoritative. + JSON.stringify({ message: "different failure", type: "upstream_error", error: { message: verdict, type: "upstream_error", code: null } }), + // A carrier-prefixed root message is peeled and its contents must still support the + // verdict; `Provider error 400:` alone does not launder a contradiction. + JSON.stringify({ message: "Provider error 400: Invalid value for tools", type: "upstream_error", error: { message: verdict, type: "upstream_error", code: null } }), + JSON.stringify({ message: "data: {\"error\":{\"message\":\"different failure\"}}", type: "upstream_error", error: { message: verdict, type: "upstream_error", code: null } }), + // Codes and types outside the closed sets stay terminal. (`permission_denied` and + // `rate_limit_exceeded` would hop through their own structured-code rules regardless of + // the message, so they cannot pin this classifier shut.) + JSON.stringify({ error: { message: verdict, type: "made_up_type", code: null } }), + JSON.stringify({ error: { message: verdict, type: "upstream_error", code: "future_unknown_code" } }), + ]) { + expect(comboFailureDecision(400, message)).toBe("stop"); + } + }); + + test("non-400 statuses and foreign outer codes stay on the existing rules", () => { + expect(comboFailureDecision(422, reported)).toBe("stop"); + expect(comboFailureDecision(400, verdict, { code: "unsupported_value" })).toBe("stop"); + expect(comboFailureDecision(400, reported, { code: "rate_limit_exceeded" })).toBe("stop"); + }); + + test("the parser control-marker verdict hops the same way", () => { + const markerVerdict = "The model returned an unexpected parser control marker; no output was published."; + const markerBody = JSON.stringify({ + error: { message: `Provider error 400: ${markerVerdict}`, type: "upstream_error", code: null }, + }); + for (const message of [markerBody, `Provider error 400: ${markerBody}`, markerVerdict]) { + expect(comboFailureDecision(400, message)).toBe("hop"); + expect(comboFailureCooldownScope(400, message)).toBe("none"); + } + // Reflected inside a validation message it stays terminal, same as the tool-call verdict. + expect(comboFailureDecision(400, JSON.stringify({ + error: { message: `Invalid value: "${markerVerdict}"`, type: "invalid_request_error", code: null }, + }))).toBe("stop"); + }); + + test("a long refusal envelope still hops: the intact bounded body is classified, not the display prefix", async () => { + // Padding past the 500-char display limit must not turn a valid verdict into truncated, + // unparseable JSON. consumeComboFailure classifies body.text separately and hands the + // verdict to both decisions while classificationText stays the display prefix. + const padding = "x".repeat(600); + const longBody = JSON.stringify({ + id: `chatcmpl-${padding}`, + error: { message: `Provider error 400: ${verdict}`, type: "upstream_error", code: null }, + }); + expect(longBody.length).toBeGreaterThan(500); + const failure = await consumeComboFailure(new Response(longBody, { + status: 400, + headers: { "content-type": "application/json" }, + })); + expect(failure.classificationText.length).toBeLessThanOrEqual(500); + expect(failure.unofferedToolCallRefusal).toBe(true); + expect(comboFailureDecision(400, failure.classificationText, { + code: failure.upstreamCode, + unofferedToolCallRefusal: failure.unofferedToolCallRefusal, + })).toBe("hop"); + expect(comboFailureCooldownScope(400, failure.classificationText, { + code: failure.upstreamCode, + unofferedToolCallRefusal: failure.unofferedToolCallRefusal, + })).toBe("none"); + // Without the intact-body verdict the truncated text fails closed, as before. + expect(comboFailureDecision(400, failure.classificationText, { + code: failure.upstreamCode, + })).toBe("stop"); + }); + + test("AI-SDK gateway carrier with object param hops", () => { + const verdict = "The model returned an unexpected parser control marker; no output was published."; + const body = JSON.stringify({ + error: { + message: verdict, + type: "AI_APICallError", + param: { + error: verdict, statusCode: 400, name: "AI_APICallError", + message: verdict, isRetryable: false, type: "AI_APICallError", + }, + }, + providerMetadata: { gateway: { routing: { originalModelId: "humansand/ampersand-v0.1" } } }, + }); + expect(comboFailureDecision(400, body)).toBe("hop"); + expect(comboFailureCooldownScope(400, body)).toBe("none"); + // A param object carrying a different claim stays terminal. + const contradicted = JSON.stringify({ + error: { + message: verdict, type: "AI_APICallError", + param: { error: "different failure", statusCode: 400, type: "AI_APICallError" }, + }, + }); + expect(comboFailureDecision(400, contradicted)).toBe("stop"); + // A matching param.error cannot mask a contradicting param.message. + const mixed = JSON.stringify({ + error: { + message: verdict, type: "AI_APICallError", + param: { error: verdict, message: "Invalid value for tools", statusCode: 400 }, + }, + }); + expect(comboFailureDecision(400, mixed)).toBe("stop"); + expect(comboFailureCooldownScope(400, mixed)).toBe("target"); + }); +});