|
| 1 | +/** |
| 2 | + * #10360 — the executor-result contract guard must not hot-loop the router. |
| 3 | + * |
| 4 | + * Two defects, one symptom (`tests/unit/batch_api.test.ts` hanging forever): |
| 5 | + * |
| 6 | + * 1. CROSS-REALM FALSE POSITIVE. The guard added in #10256 used a bare |
| 7 | + * `result.response instanceof Response`. OmniRoute's default egress |
| 8 | + * (`open-sse/utils/proxyFetch.ts`) is the npm `undici` package's `fetch`, |
| 9 | + * whose `Response` class is NOT `globalThis.Response` — so every ordinary |
| 10 | + * upstream response arrived as a "contract violation". The guard must |
| 11 | + * recognize a structurally valid Response from any realm. |
| 12 | + * |
| 13 | + * 2. TRANSIENT MISCLASSIFICATION. A genuine contract violation is an INTERNAL |
| 14 | + * bug, not a flaky upstream. It carried no `.status`, so chatCore's default |
| 15 | + * mapped it to 502 → the connection got cooled down as "rate limited", the |
| 16 | + * provider breaker counted it, and `processSingleItemWithRetry` (which |
| 17 | + * retries 429/502/504 up to 200×/24h) span forever. It must surface as a |
| 18 | + * terminal internal 500 carrying a stable error code, and every resilience |
| 19 | + * layer must treat that code as request-scoped: no cooldown, no breaker. |
| 20 | + */ |
| 21 | +import test from "node:test"; |
| 22 | +import assert from "node:assert/strict"; |
| 23 | +import { Response as UndiciResponse } from "undici"; |
| 24 | + |
| 25 | +import { normalizeExecutorResult } from "../../open-sse/handlers/chatCore/upstreamTimeouts.ts"; |
| 26 | +import { EXECUTOR_CONTRACT_VIOLATION_CODE } from "../../open-sse/config/constants.ts"; |
| 27 | +import { |
| 28 | + isRequestScopedUpstreamFailure, |
| 29 | + shouldSkipConnDisable, |
| 30 | +} from "../../open-sse/services/combo/comboPredicates.ts"; |
| 31 | +import { shouldTripProviderBreakerForResult } from "../../src/sse/handlers/chatPredicates.ts"; |
| 32 | +import { checkFallbackError } from "../../open-sse/services/accountFallback.ts"; |
| 33 | + |
| 34 | +// ─── 1. Cross-realm Response acceptance ────────────────────────────────────── |
| 35 | + |
| 36 | +test("undici's Response is a different class than the global one (premise)", () => { |
| 37 | + assert.notEqual( |
| 38 | + UndiciResponse as unknown, |
| 39 | + globalThis.Response as unknown, |
| 40 | + "if these ever become the same class the cross-realm guard below is moot" |
| 41 | + ); |
| 42 | + assert.equal( |
| 43 | + new UndiciResponse("x", { status: 200 }) instanceof globalThis.Response, |
| 44 | + false, |
| 45 | + "premise: an undici Response fails a bare `instanceof Response`" |
| 46 | + ); |
| 47 | +}); |
| 48 | + |
| 49 | +test("normalizeExecutorResult accepts a cross-realm Response in the capture-object arm", () => { |
| 50 | + const response = new UndiciResponse(JSON.stringify({ ok: true }), { status: 401 }); |
| 51 | + |
| 52 | + const normalized = normalizeExecutorResult({ |
| 53 | + response, |
| 54 | + url: "https://api.openai.com/v1/chat/completions", |
| 55 | + headers: { "x-req": "1" }, |
| 56 | + transformedBody: { a: 1 }, |
| 57 | + }); |
| 58 | + |
| 59 | + assert.equal(normalized.response, response as unknown); |
| 60 | + assert.equal(normalized.response.status, 401); |
| 61 | + assert.equal(normalized.url, "https://api.openai.com/v1/chat/completions"); |
| 62 | + assert.deepEqual(normalized.headers, { "x-req": "1" }); |
| 63 | + assert.deepEqual(normalized.transformedBody, { a: 1 }); |
| 64 | +}); |
| 65 | + |
| 66 | +test("normalizeExecutorResult accepts a bare cross-realm Response", () => { |
| 67 | + const response = new UndiciResponse("body", { status: 503 }); |
| 68 | + |
| 69 | + const normalized = normalizeExecutorResult(response); |
| 70 | + |
| 71 | + assert.equal(normalized.response, response as unknown); |
| 72 | + assert.equal(normalized.response.status, 503); |
| 73 | + assert.equal(normalized.url, ""); |
| 74 | + assert.deepEqual(normalized.headers, {}); |
| 75 | + assert.equal(normalized.transformedBody, null); |
| 76 | +}); |
| 77 | + |
| 78 | +// ─── 2. A genuine violation is terminal, not a transient provider failure ──── |
| 79 | + |
| 80 | +function captureThrow(run: () => unknown): Error & { status?: unknown; code?: unknown } { |
| 81 | + try { |
| 82 | + run(); |
| 83 | + } catch (err) { |
| 84 | + return err as Error & { status?: unknown; code?: unknown }; |
| 85 | + } |
| 86 | + throw new assert.AssertionError({ message: "expected normalizeExecutorResult to throw" }); |
| 87 | +} |
| 88 | + |
| 89 | +test("a genuinely malformed executor result still throws", () => { |
| 90 | + assert.throws(() => normalizeExecutorResult({}), /must contain a Response/); |
| 91 | + assert.throws(() => normalizeExecutorResult(undefined), /must contain a Response/); |
| 92 | + assert.throws(() => normalizeExecutorResult({ response: "not-a-response" }), /must contain a/); |
| 93 | + // A partial look-alike (no body readers) must NOT slip past the duck-type. |
| 94 | + assert.throws( |
| 95 | + () => normalizeExecutorResult({ response: { status: 200, ok: true } }), |
| 96 | + /must contain a Response/ |
| 97 | + ); |
| 98 | +}); |
| 99 | + |
| 100 | +test("the contract-violation error carries an internal-terminal status + stable code", () => { |
| 101 | + const err = captureThrow(() => normalizeExecutorResult({ response: "not-a-response" })); |
| 102 | + |
| 103 | + assert.equal(err.status, 500, "an internal contract violation is a 500, never a provider 502"); |
| 104 | + assert.equal( |
| 105 | + err.code, |
| 106 | + EXECUTOR_CONTRACT_VIOLATION_CODE, |
| 107 | + "chatCore reads `.code` (getUpstreamErrorIdentifier) to tag the surfaced error" |
| 108 | + ); |
| 109 | + assert.equal(EXECUTOR_CONTRACT_VIOLATION_CODE, "executor_contract_violation"); |
| 110 | +}); |
| 111 | + |
| 112 | +test("the contract-violation code is classified as a request-scoped failure", () => { |
| 113 | + assert.equal(isRequestScopedUpstreamFailure({ code: EXECUTOR_CONTRACT_VIOLATION_CODE }), true); |
| 114 | +}); |
| 115 | + |
| 116 | +test("a contract violation must not cool the connection down", () => { |
| 117 | + assert.equal( |
| 118 | + shouldSkipConnDisable( |
| 119 | + { |
| 120 | + status: 500, |
| 121 | + errorCode: EXECUTOR_CONTRACT_VIOLATION_CODE, |
| 122 | + errorType: null, |
| 123 | + error: "Executor result must contain a Response", |
| 124 | + }, |
| 125 | + false, |
| 126 | + false, |
| 127 | + "openai" |
| 128 | + ), |
| 129 | + true, |
| 130 | + "our own bug must never mark the operator's account as rate-limited/unavailable" |
| 131 | + ); |
| 132 | +}); |
| 133 | + |
| 134 | +test("a contract violation must not trip the provider circuit breaker", () => { |
| 135 | + assert.equal( |
| 136 | + shouldTripProviderBreakerForResult( |
| 137 | + { |
| 138 | + status: 500, |
| 139 | + errorCode: EXECUTOR_CONTRACT_VIOLATION_CODE, |
| 140 | + errorType: null, |
| 141 | + error: "Executor result must contain a Response", |
| 142 | + }, |
| 143 | + false, |
| 144 | + false |
| 145 | + ), |
| 146 | + false, |
| 147 | + "500 is a breaker-failure status, but this one never reached the provider" |
| 148 | + ); |
| 149 | +}); |
| 150 | + |
| 151 | +test("checkFallbackError treats the contract violation as terminal — no retry, no cooldown", () => { |
| 152 | + const decision = checkFallbackError( |
| 153 | + 500, |
| 154 | + "[500]: Executor result must contain a Response", |
| 155 | + 0, |
| 156 | + "gpt-4o-mini", |
| 157 | + "openai", |
| 158 | + null, |
| 159 | + null, |
| 160 | + { code: EXECUTOR_CONTRACT_VIOLATION_CODE } |
| 161 | + ); |
| 162 | + |
| 163 | + assert.equal(decision.shouldFallback, false, "retrying our own bug just reproduces it"); |
| 164 | + assert.equal(decision.cooldownMs, 0, "no connection cooldown for an internal defect"); |
| 165 | + assert.equal(decision.skipProviderBreaker, true); |
| 166 | +}); |
| 167 | + |
| 168 | +test("a real provider 500 is still retryable (the terminal branch is not over-broad)", () => { |
| 169 | + const decision = checkFallbackError(500, "Internal server error", 0, null, "openai"); |
| 170 | + |
| 171 | + assert.equal(decision.shouldFallback, true); |
| 172 | + assert.ok(decision.cooldownMs > 0, "a genuine upstream 500 keeps its backoff cooldown"); |
| 173 | +}); |
0 commit comments