@bitkyc08/opencodex 2.100.1 → 2.100.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/gui/dist/assets/App-BpJOzw4F.js +59 -0
- package/gui/dist/assets/Tray-BcaI-VCr.js +1 -0
- package/gui/dist/assets/{brand-mark-nr3ziR-T.js → brand-mark-DU-0Vdkr.js} +1 -1
- package/gui/dist/assets/index-Bbor_y80.js +94 -0
- package/gui/dist/assets/index-DJEkIDAi.css +2 -0
- package/gui/dist/assets/{segment-thumb-features-DKCVUUTi.js → segment-thumb-features-e9A_FogJ.js} +1 -1
- package/gui/dist/classic/assets/{App-CgPBKUP-.js → App-BrIlUiLc.js} +18 -18
- package/gui/dist/classic/assets/{Tray-BedmOsE7.js → Tray-DT146AhS.js} +1 -1
- package/gui/dist/classic/assets/index-DyJHagvT.js +94 -0
- package/gui/dist/classic/assets/quota-summary-CakPe9Ni.js +1 -0
- package/gui/dist/classic/index.html +1 -1
- package/gui/dist/index.html +2 -2
- package/package.json +2 -2
- package/src/adapters/client-fingerprint.ts +5 -3
- package/src/adapters/cursor/native-exec.ts +3 -2
- package/src/adapters/cursor/tool-guidance.ts +3 -3
- package/src/adapters/cursor/tool-wording.ts +10 -1
- package/src/adapters/devin/cloud-direct/tool-schema.ts +20 -2
- package/src/adapters/google-antigravity-wire.ts +13 -6
- package/src/adapters/google-truncation.ts +4 -2
- package/src/adapters/google.ts +8 -2
- package/src/adapters/openai-chat/tool-schema.ts +9 -0
- package/src/adapters/openai-responses/tool-schema.ts +18 -4
- package/src/adapters/run-turn-queue.ts +139 -5
- package/src/adapters/upstream-http-error.ts +4 -2
- package/src/bridge/sse.ts +8 -1
- package/src/claude/chat-tool-dispatch.ts +233 -0
- package/src/claude/inbound-tool-loading.ts +42 -17
- package/src/cli/account-api.ts +6 -0
- package/src/cli/account-extended.ts +60 -6
- package/src/cli/account-key-quota.ts +22 -2
- package/src/cli/account.ts +1 -1
- package/src/cli/capabilities-access-remote.ts +10 -0
- package/src/cli/capabilities-agents-routing.ts +2 -2
- package/src/cli/capabilities-base.ts +2 -2
- package/src/cli/claude.ts +19 -14
- package/src/cli/config-command.ts +24 -1
- package/src/cli/connect.ts +36 -3
- package/src/cli/gui-pair-client.ts +23 -2
- package/src/cli/help.ts +1 -1
- package/src/cli/hub.ts +18 -1
- package/src/cli/link-child-ops.ts +136 -0
- package/src/cli/link-status-child.ts +94 -0
- package/src/cli/link.ts +22 -9
- package/src/cli/registry.ts +3 -2
- package/src/cli/sync-command.ts +3 -1
- package/src/cli/sync-fanout.ts +8 -81
- package/src/client/link-readiness.ts +16 -6
- package/src/client/link-tunnel-status.ts +76 -0
- package/src/clients/config-export.ts +14 -1
- package/src/codex/account-resume-health.ts +41 -0
- package/src/codex/auth-api/login-flow.ts +6 -0
- package/src/codex/auth-api/routes.ts +35 -2
- package/src/codex/catalog/gather-capture.ts +2 -0
- package/src/codex/catalog/provider-models.ts +13 -8
- package/src/codex/convergence-types.ts +2 -0
- package/src/codex/quota.ts +111 -6
- package/src/combos/cooldown-store.ts +208 -0
- package/src/combos/failover.ts +432 -35
- package/src/config/salvage.ts +1 -0
- package/src/config/schema/config-schema.ts +6 -3
- package/src/config/schema/leaf-validators.ts +34 -12
- package/src/generated/compatibility-version.json +213 -121
- package/src/lib/catalog-etag.ts +10 -0
- package/src/lib/client-error-body.ts +15 -0
- package/src/lib/context-overflow-verdict.ts +266 -9
- package/src/lib/failure-detail.ts +72 -3
- package/src/lib/hard-refusal.ts +259 -0
- package/src/link/child-activity.ts +57 -0
- package/src/link/child-catalog-freshness.ts +149 -0
- package/src/link/child-gated-ocx.ts +117 -0
- package/src/link/child-observations.ts +205 -0
- package/src/link/child-observe.ts +121 -0
- package/src/link/child-ops-engine.ts +162 -0
- package/src/link/child-update-contract.ts +165 -0
- package/src/link/paths.ts +5 -0
- package/src/link/remote-windows.ts +42 -6
- package/src/link/ssh-argv.ts +126 -32
- package/src/link/status-projection.ts +73 -0
- package/src/link/sync-fanout-classify.ts +98 -0
- package/src/link/tunnel-state.ts +10 -4
- package/src/messaging/mcp-queue-rpc.ts +2 -1
- package/src/messaging/mcp-queue-server.ts +5 -0
- package/src/messaging/mcp-reply-tools.ts +24 -9
- package/src/messaging/mcp-reply-wait.ts +70 -0
- package/src/oauth/account-resume-health.ts +60 -0
- package/src/oauth/index.ts +1 -0
- package/src/oauth/store.ts +44 -1
- package/src/providers/api-key-pause-state.ts +23 -0
- package/src/providers/api-key-pause.ts +68 -10
- package/src/providers/api-key-selection-capture.ts +3 -1
- package/src/providers/api-key-selection.ts +3 -2
- package/src/providers/api-keys.ts +18 -5
- package/src/providers/claude-cli-usage-state.ts +11 -6
- package/src/providers/claude-cli-usage.ts +27 -9
- package/src/providers/codex-capacity.ts +11 -2
- package/src/providers/key-failover.ts +13 -6
- package/src/providers/model-discovery-config.ts +31 -0
- package/src/providers/model-discovery-diagnostic.ts +34 -3
- package/src/providers/model-discovery.ts +43 -7
- package/src/providers/model-rename-fields.ts +2 -0
- package/src/providers/openai-sidecar.ts +15 -2
- package/src/providers/provider-id-rewrite.ts +3 -0
- package/src/providers/quota/account-cache.ts +52 -34
- package/src/providers/quota/anthropic-usage-windows.ts +19 -9
- package/src/providers/quota/report-cache.ts +53 -5
- package/src/providers/quota/vendor-probes-key.ts +73 -0
- package/src/providers/quota/vendor-probes-oauth.ts +57 -3
- package/src/providers/quota-key-accounts.ts +12 -5
- package/src/providers/quota-mistral-budget.ts +18 -0
- package/src/providers/quota-types.ts +25 -0
- package/src/providers/quota.ts +22 -22
- package/src/providers/reasoning-metadata.ts +1051 -12
- package/src/quota/reset-observer.ts +4 -2
- package/src/quota/reset-seen-store.ts +9 -0
- package/src/quota/window-mapping.ts +1 -0
- package/src/quota-pool/assemble.ts +4 -1
- package/src/responses/hosted-tool-policy.ts +56 -12
- package/src/router.ts +2 -0
- package/src/server/auth-cors.ts +11 -8
- package/src/server/catalog-download.ts +3 -5
- package/src/server/chat-native.ts +11 -2
- package/src/server/claude-messages.ts +2 -1
- package/src/server/images.ts +41 -13
- package/src/server/index/link-listener.ts +16 -2
- package/src/server/index/link-relay-sessions.ts +43 -2
- package/src/server/index/optional-listeners.ts +18 -0
- package/src/server/management/config-routes.ts +7 -1
- package/src/server/management/context.ts +1 -1
- package/src/server/management/link-child-ops-routes.ts +116 -0
- package/src/server/management/link-route-state.ts +10 -0
- package/src/server/management/link-routes.ts +10 -0
- package/src/server/management/oauth-account-routes.ts +33 -4
- package/src/server/management/provider-overwrite-carry.ts +28 -3
- package/src/server/management/provider-routes.ts +16 -11
- package/src/server/management/route-registry.ts +3 -0
- package/src/server/messages-native.ts +5 -0
- package/src/server/request-log-failure-detail.ts +59 -1
- package/src/server/responses/adapter-dispatch.ts +35 -44
- package/src/server/responses/combo-stream-preflight.ts +146 -29
- package/src/server/responses/compact.ts +16 -5
- package/src/server/responses/context-overflow.ts +58 -2
- package/src/server/responses/core-combo-failure.ts +29 -4
- package/src/server/responses/core-combo-native.ts +3 -1
- package/src/server/responses/core-combo.ts +30 -3
- package/src/server/responses/core-errors.ts +3 -1
- package/src/server/responses/core-opaque-recovery.ts +45 -2
- package/src/server/responses/core-options.ts +12 -0
- package/src/server/responses/passthrough-dispatch.ts +30 -38
- package/src/server/responses/request-transport.ts +43 -14
- package/src/server/responses/reset-replay.ts +58 -2
- package/src/server/responses/run-turn-execution.ts +56 -30
- package/src/types/claude-tool-loading.ts +48 -0
- package/src/types/config.ts +5 -35
- package/src/types/provider.ts +21 -0
- package/src/types.ts +1 -0
- package/src/web-search/parse.ts +21 -2
- package/gui/dist/assets/App-g3Cm2xhZ.js +0 -59
- package/gui/dist/assets/Tray-BMDkAiXy.js +0 -1
- package/gui/dist/assets/index-Bziu8gTy.css +0 -2
- package/gui/dist/assets/index-OYzhQUNP.js +0 -94
- package/gui/dist/classic/assets/index-BR-HhNrp.js +0 -94
- package/gui/dist/classic/assets/quota-summary-CXKhvSmQ.js +0 -1
package/src/combos/failover.ts
CHANGED
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
import { parseResetCooldownMs } from "../codex/routing";
|
|
2
|
-
import { classifyError, isCyberPolicyCode } from "../lib/errors";
|
|
2
|
+
import { classifyError, isCyberPolicyCode, isTerminalRefusalCode } from "../lib/errors";
|
|
3
|
+
import { isHardPolicyOrOriginRefusal } from "../lib/hard-refusal";
|
|
3
4
|
import { isNonReplayableUpstreamCode } from "../lib/upstream-retry";
|
|
4
5
|
import { isAnthropicBillingFailure } from "../providers/anthropic-billing-refusal";
|
|
6
|
+
import { hasConflictingReasoningEffortCode } from "../providers/reasoning-metadata";
|
|
5
7
|
import { isKiroAccountProfileRefusal } from "../adapters/kiro-refusal";
|
|
6
8
|
import type { OcxComboTarget } from "../types";
|
|
7
9
|
import { targetKey } from "./types";
|
|
10
|
+
import { PersistedCooldownMap } from "./cooldown-store";
|
|
11
|
+
import { registerOptionalShutdownHook } from "../lib/optional-shutdown-hooks";
|
|
8
12
|
import { isDefiniteContextOverflow } from "../lib/context-overflow-verdict";
|
|
9
13
|
|
|
10
14
|
// The verdict lives in a dependency-free leaf so the presentation classifier can share it.
|
|
@@ -48,9 +52,11 @@ const HTTP_MONTH_INDEX: Record<string, number> = {
|
|
|
48
52
|
};
|
|
49
53
|
|
|
50
54
|
/** Map<`${comboId}\0${provider/model}`, TargetCooldown> */
|
|
51
|
-
const targetCooldowns = new Map<string, TargetCooldown>();
|
|
52
55
|
// Never reset: an in-flight reservation may still refer to an entry cleared from the map.
|
|
53
56
|
let nextCooldownRevision = 0;
|
|
57
|
+
// Disk-backed (combo-cooldowns.json): a restart keeps every live, reset-aware cooldown.
|
|
58
|
+
const targetCooldowns = new PersistedCooldownMap<TargetCooldown>(() => ++nextCooldownRevision);
|
|
59
|
+
registerOptionalShutdownHook("combo-target-cooldowns", () => targetCooldowns.flushForShutdown());
|
|
54
60
|
let lastReconciledGeneration = 0;
|
|
55
61
|
let liveComboTargets = new Set<string>();
|
|
56
62
|
|
|
@@ -164,7 +170,7 @@ export function isComboTargetInCooldown(
|
|
|
164
170
|
const entry = targetCooldowns.get(key);
|
|
165
171
|
if (!entry) return false;
|
|
166
172
|
if (entry.cooldownUntil <= now) {
|
|
167
|
-
targetCooldowns.
|
|
173
|
+
targetCooldowns.dropLocal(key);
|
|
168
174
|
return false;
|
|
169
175
|
}
|
|
170
176
|
return true;
|
|
@@ -180,7 +186,7 @@ export function comboTargetCooldownExpiry(
|
|
|
180
186
|
const entry = targetCooldowns.get(key);
|
|
181
187
|
if (!entry) return undefined;
|
|
182
188
|
if (entry.cooldownUntil <= now) {
|
|
183
|
-
targetCooldowns.
|
|
189
|
+
targetCooldowns.dropLocal(key);
|
|
184
190
|
return undefined;
|
|
185
191
|
}
|
|
186
192
|
return entry.cooldownUntil;
|
|
@@ -203,7 +209,7 @@ export function clearComboTargetCooldownIfUnchanged(
|
|
|
203
209
|
expectedRevision: number,
|
|
204
210
|
): void {
|
|
205
211
|
const key = cooldownMapKey(comboId, target);
|
|
206
|
-
if (targetCooldowns.get(key)?.revision === expectedRevision) targetCooldowns.
|
|
212
|
+
if (targetCooldowns.get(key)?.revision === expectedRevision) targetCooldowns.dropLocal(key);
|
|
207
213
|
}
|
|
208
214
|
|
|
209
215
|
export function isTransientRequestRateLimit(input: {
|
|
@@ -233,7 +239,7 @@ export function remainingComboCooldownMs(comboId: string, now = Date.now()): num
|
|
|
233
239
|
if (!key.startsWith(prefix)) continue;
|
|
234
240
|
const remaining = cooldown.cooldownUntil - now;
|
|
235
241
|
if (remaining <= 0) {
|
|
236
|
-
targetCooldowns.
|
|
242
|
+
targetCooldowns.dropLocal(key);
|
|
237
243
|
continue;
|
|
238
244
|
}
|
|
239
245
|
if (soonest === undefined || remaining < soonest) soonest = remaining;
|
|
@@ -351,7 +357,7 @@ export function sweepExpiredComboTargetCooldowns(now = Date.now()): number {
|
|
|
351
357
|
let removed = 0;
|
|
352
358
|
for (const [key, cooldown] of targetCooldowns) {
|
|
353
359
|
if (cooldown.cooldownUntil > now) continue;
|
|
354
|
-
targetCooldowns.
|
|
360
|
+
targetCooldowns.dropLocal(key);
|
|
355
361
|
removed += 1;
|
|
356
362
|
}
|
|
357
363
|
return removed;
|
|
@@ -360,28 +366,352 @@ export function sweepExpiredComboTargetCooldowns(now = Date.now()): number {
|
|
|
360
366
|
/** Clear all cooldowns, one combo, or only the supplied target keys within that combo. */
|
|
361
367
|
export function clearComboTargetCooldowns(comboId?: string, targetKeys?: Iterable<string>): void {
|
|
362
368
|
if (comboId === undefined) {
|
|
363
|
-
targetCooldowns.
|
|
369
|
+
targetCooldowns.clearAllPersisted();
|
|
364
370
|
liveComboTargets.clear();
|
|
365
371
|
lastReconciledGeneration = 0;
|
|
366
372
|
return;
|
|
367
373
|
}
|
|
368
374
|
const prefix = `${comboId}\0`;
|
|
369
375
|
if (targetKeys !== undefined) {
|
|
370
|
-
|
|
376
|
+
targetCooldowns.clearPersisted([...targetKeys].map(key => `${prefix}${key}`));
|
|
371
377
|
return;
|
|
372
378
|
}
|
|
373
|
-
|
|
374
|
-
if (key.startsWith(prefix)) targetCooldowns.delete(key);
|
|
375
|
-
}
|
|
379
|
+
targetCooldowns.clearPersisted([...targetCooldowns.keys()].filter(key => key.startsWith(prefix)));
|
|
376
380
|
}
|
|
377
381
|
|
|
378
382
|
export type ComboFailureDecision = "hop" | "stop";
|
|
379
383
|
export type ComboFailureCooldownScope = "none" | "target" | "provider";
|
|
380
384
|
|
|
381
|
-
function normalizedFailureCode(code?: string | null): string {
|
|
385
|
+
function normalizedFailureCode(code?: string | number | null): string {
|
|
386
|
+
if (typeof code === "number") return Number.isFinite(code) ? String(code) : "";
|
|
382
387
|
return code?.trim().toLowerCase().replaceAll("-", "_") ?? "";
|
|
383
388
|
}
|
|
384
389
|
|
|
390
|
+
/** Parse one bounded error body or single SSE data frame into its root record. */
|
|
391
|
+
function parseErrorEnvelope(message: string): Record<string, unknown> | undefined {
|
|
392
|
+
if (message.length > 16_384) return undefined;
|
|
393
|
+
let text = message.trim();
|
|
394
|
+
const proxyPrefix = /^Provider error \d{3}: /.exec(text);
|
|
395
|
+
if (proxyPrefix) text = text.slice(proxyPrefix[0].length).trim();
|
|
396
|
+
if (text.startsWith("data:") && !text.includes("\n")) text = text.slice("data:".length).trim();
|
|
397
|
+
try {
|
|
398
|
+
const payload: unknown = JSON.parse(text);
|
|
399
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return undefined;
|
|
400
|
+
return payload as Record<string, unknown>;
|
|
401
|
+
} catch {
|
|
402
|
+
return undefined;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* The one error record an envelope carries: its own `error`, else the root when that root states
|
|
408
|
+
* the failure itself. HTTP consumption and the SSE preflight both select through here so the two
|
|
409
|
+
* transports cannot disagree about which record speaks.
|
|
410
|
+
*
|
|
411
|
+
* A `response.error` alone is deliberately not a carrier here. Under a bare frame it is not an
|
|
412
|
+
* error event at all, so reading it on HTTP only made the same envelope hop on one transport and
|
|
413
|
+
* stop on the other; leaving it unread is the behavior both transports had.
|
|
414
|
+
*/
|
|
415
|
+
export function envelopeErrorRecord(root: Record<string, unknown>): Record<string, unknown> | undefined {
|
|
416
|
+
const error = root.error;
|
|
417
|
+
if (error !== null && typeof error === "object" && !Array.isArray(error)) {
|
|
418
|
+
const record = { ...(error as Record<string, unknown>) };
|
|
419
|
+
if (typeof record.detail !== "string" && typeof root.detail === "string") {
|
|
420
|
+
record.detail = root.detail;
|
|
421
|
+
}
|
|
422
|
+
return record;
|
|
423
|
+
}
|
|
424
|
+
if (typeof root.message === "string" || typeof root.detail === "string") {
|
|
425
|
+
return root;
|
|
426
|
+
}
|
|
427
|
+
return undefined;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Parse one bounded error record from an HTTP 400 body or a single SSE data frame. */
|
|
431
|
+
export function http400ErrorRecord(message: string): Record<string, unknown> | undefined {
|
|
432
|
+
const root = parseErrorEnvelope(message);
|
|
433
|
+
return root ? envelopeErrorRecord(root) : undefined;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
/** A record's own structured code as a string; blank, absent and non-finite values state none. */
|
|
437
|
+
export function envelopeRecordCode(record: Record<string, unknown> | undefined): string | undefined {
|
|
438
|
+
if (!record) return undefined;
|
|
439
|
+
const code = record.code;
|
|
440
|
+
if (typeof code === "number") {
|
|
441
|
+
return Number.isFinite(code) ? String(code) : undefined;
|
|
442
|
+
}
|
|
443
|
+
return typeof code === "string" && code.trim() !== "" ? code : undefined;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* The envelope root's own structured code, when it states one beside a nested record.
|
|
448
|
+
*
|
|
449
|
+
* The nested record is what normalization selects for display and classification, so this is
|
|
450
|
+
* the only place a root verdict survives. Numeric codes are stringified the way the decision
|
|
451
|
+
* normalizes them. An absent, empty or whitespace-only code is no verdict at all.
|
|
452
|
+
*/
|
|
453
|
+
export function upstreamEnvelopeRootCode(message: string): string | undefined {
|
|
454
|
+
return envelopeRecordCode(parseErrorEnvelope(message));
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** Check whether a parsed error record or raw diagnostic text represents a malformed response_format. */
|
|
458
|
+
export function isMalformedResponseFormatRecord(record?: Record<string, unknown>, rawText?: string): boolean {
|
|
459
|
+
const fields = record
|
|
460
|
+
? [record.message, record.detail].filter((value): value is string => typeof value === "string")
|
|
461
|
+
: [];
|
|
462
|
+
if (rawText) fields.push(rawText);
|
|
463
|
+
return fields.some(detail =>
|
|
464
|
+
/\binvalid schema(?: for)? response_format\b/i.test(detail)
|
|
465
|
+
|| /\bresponse_format(?:\.[a-z_]+)*\s+must be (?:one of|valid|a valid)\b/i.test(detail));
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/** Identify known malformed structured-output diagnostics that must remain terminal. */
|
|
469
|
+
export function isMalformedResponseFormatRequest(status: number, message: string): boolean {
|
|
470
|
+
if (status !== 400) return false;
|
|
471
|
+
return isMalformedResponseFormatRecord(http400ErrorRecord(message), message);
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Check whether a parsed error record matches OpenRouter's uncoded numeric-400 opaque wrapper. */
|
|
475
|
+
export function isOpenRouterOpaque400Record(record?: Record<string, unknown>): boolean {
|
|
476
|
+
if (!record) return false;
|
|
477
|
+
const metadata = record.metadata;
|
|
478
|
+
const errorType = record.type === "error" ? undefined : record.type;
|
|
479
|
+
return record.message === "Provider returned error"
|
|
480
|
+
&& (record.code === 400 || record.code === "400")
|
|
481
|
+
&& errorType === undefined
|
|
482
|
+
&& metadata !== null && typeof metadata === "object" && !Array.isArray(metadata)
|
|
483
|
+
&& (metadata as Record<string, unknown>).raw === "ERROR";
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
/** Recognize OpenRouter's uncoded numeric-400 wrapper for an opaque provider failure. */
|
|
487
|
+
export function isOpenRouterOpaque400Envelope(message: string): boolean {
|
|
488
|
+
const root = parseErrorEnvelope(message);
|
|
489
|
+
return !!root && isOpenRouterOpaque400EnvelopeRoot(root);
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* The same verdict for an already parsed envelope root, which is what the SSE preflight holds.
|
|
494
|
+
* Both transports entering here is what keeps their answers equal.
|
|
495
|
+
*/
|
|
496
|
+
export function isOpenRouterOpaque400EnvelopeRoot(root: Record<string, unknown>): boolean {
|
|
497
|
+
return isOpenRouterOpaque400Record(envelopeErrorRecord(root)) && !envelopeRootStatesVerdict(root);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* Root codes that state a request verdict of their own, so the record beside them does not
|
|
502
|
+
* speak for the envelope.
|
|
503
|
+
*
|
|
504
|
+
* Membership is a correctness question, not a formatting one: an equality test against `"400"`
|
|
505
|
+
* made every other root code -- including a blank one and the codes that are ordinary combo hop
|
|
506
|
+
* signals -- suppress the opaque verdict, so HTTP stopped on failures SSE advanced past. Each
|
|
507
|
+
* entry here is a code `comboFailureDecision` reaches its terminal arms with when read alone.
|
|
508
|
+
* Content refusals are the shared {@link isTerminalRefusalCode} set; the post-send codes are the
|
|
509
|
+
* ones {@link isNonReplayableUpstreamCode} owns; the rest are the generic request verdicts and the
|
|
510
|
+
* origin's own refusal.
|
|
511
|
+
*/
|
|
512
|
+
const ROOT_VERDICT_CODES = new Set([
|
|
513
|
+
"origin_rejected",
|
|
514
|
+
"content_policy_violation",
|
|
515
|
+
"context_length_exceeded",
|
|
516
|
+
"invalid_request_error",
|
|
517
|
+
"client_closed_request",
|
|
518
|
+
"client_cancelled",
|
|
519
|
+
]);
|
|
520
|
+
|
|
521
|
+
/** A root code that is itself a hard stop or terminal request verdict; blank means "no verdict". */
|
|
522
|
+
function rootCodeStatesVerdict(code: string): boolean {
|
|
523
|
+
const normalized = normalizedFailureCode(code);
|
|
524
|
+
return ROOT_VERDICT_CODES.has(normalized)
|
|
525
|
+
|| isTerminalRefusalCode(normalized)
|
|
526
|
+
|| isNonReplayableUpstreamCode(normalized);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Does the envelope root name its own verdict? A non-`"error"` type and any terminal root code do;
|
|
531
|
+
* a blank, absent, numeric-`400` or hop-signal code does not, so the nested record keeps speaking.
|
|
532
|
+
*/
|
|
533
|
+
function envelopeRootStatesVerdict(root: Record<string, unknown>): boolean {
|
|
534
|
+
const { type } = root;
|
|
535
|
+
if (type !== undefined && type !== null && type !== "error") return true;
|
|
536
|
+
const code = envelopeRecordCode(root);
|
|
537
|
+
return code !== undefined && rootCodeStatesVerdict(code);
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
/** Check whether an uncoded numeric 400 is an opaque OpenRouter failure that may safely advance without cooling. */
|
|
541
|
+
function isUncodedUpstream400(
|
|
542
|
+
status: number,
|
|
543
|
+
message: string,
|
|
544
|
+
code?: string | number | null,
|
|
545
|
+
options?: {
|
|
546
|
+
malformedResponseFormat?: boolean;
|
|
547
|
+
openRouterOpaque400?: boolean;
|
|
548
|
+
},
|
|
549
|
+
): boolean {
|
|
550
|
+
const normalized = normalizedFailureCode(code);
|
|
551
|
+
if (status !== 400 || (normalized !== "" && normalized !== "400")) return false;
|
|
552
|
+
const malformed = options?.malformedResponseFormat !== undefined
|
|
553
|
+
? options.malformedResponseFormat
|
|
554
|
+
: isMalformedResponseFormatRequest(status, message);
|
|
555
|
+
if (malformed) return false;
|
|
556
|
+
return options?.openRouterOpaque400 !== undefined
|
|
557
|
+
? options.openRouterOpaque400
|
|
558
|
+
: isOpenRouterOpaque400Envelope(message);
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* xAI can reject a model/account effort rung with this exact vendor envelope. It is target-local
|
|
563
|
+
* evidence for a combo: the same model may accept a lower rung, or a later target may accept the
|
|
564
|
+
* request. Keep the match exact so unrelated invalid arguments stay terminal.
|
|
565
|
+
*/
|
|
566
|
+
const EXACT_EFFORT_COMPATIBLE_CODES = new Set(["", "invalid_request_error", "invalid_argument"]);
|
|
567
|
+
const EXACT_EFFORT_COMPATIBLE_TYPES = new Set(["", "invalid_request_error", "upstream_error"]);
|
|
568
|
+
|
|
569
|
+
function hasIncompatibleDiagnosticCarrier(val: unknown, depth = 0): boolean {
|
|
570
|
+
if (val === undefined || val === null) return false;
|
|
571
|
+
if (depth > 8) return true;
|
|
572
|
+
if (Array.isArray(val)) {
|
|
573
|
+
if (val.length > 32) return true;
|
|
574
|
+
for (const item of val) {
|
|
575
|
+
if (hasIncompatibleDiagnosticCarrier(item, depth + 1)) return true;
|
|
576
|
+
}
|
|
577
|
+
return false;
|
|
578
|
+
}
|
|
579
|
+
if (typeof val === "string") {
|
|
580
|
+
try {
|
|
581
|
+
const parsed = JSON.parse(val);
|
|
582
|
+
if (parsed && typeof parsed === "object") {
|
|
583
|
+
return hasIncompatibleDiagnosticCarrier(parsed, depth + 1);
|
|
584
|
+
}
|
|
585
|
+
return true;
|
|
586
|
+
} catch {
|
|
587
|
+
return true;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (typeof val === "object") {
|
|
591
|
+
const rec = val as Record<string, unknown>;
|
|
592
|
+
if (rec.param !== undefined && rec.param !== null) {
|
|
593
|
+
if (typeof rec.param !== "string") return true;
|
|
594
|
+
const param = rec.param.trim().toLowerCase();
|
|
595
|
+
if (param.length > 0 && param !== "reasoning_effort" && param !== "reasoning.effort") return true;
|
|
596
|
+
}
|
|
597
|
+
if (rec.code !== undefined && rec.code !== null) {
|
|
598
|
+
if (typeof rec.code !== "string") return true;
|
|
599
|
+
if (!EXACT_EFFORT_COMPATIBLE_CODES.has(normalizedFailureCode(rec.code))) return true;
|
|
600
|
+
}
|
|
601
|
+
if (rec.type !== undefined && rec.type !== null) {
|
|
602
|
+
if (typeof rec.type !== "string") return true;
|
|
603
|
+
if (!EXACT_EFFORT_COMPATIBLE_TYPES.has(normalizedFailureCode(rec.type))) return true;
|
|
604
|
+
}
|
|
605
|
+
if (rec.error !== undefined && rec.error !== null) {
|
|
606
|
+
if (hasIncompatibleDiagnosticCarrier(rec.error, depth + 1)) return true;
|
|
607
|
+
}
|
|
608
|
+
if (rec.response !== undefined && rec.response !== null) {
|
|
609
|
+
if (hasIncompatibleDiagnosticCarrier(rec.response, depth + 1)) return true;
|
|
610
|
+
}
|
|
611
|
+
if (rec.last_error !== undefined && rec.last_error !== null) {
|
|
612
|
+
if (hasIncompatibleDiagnosticCarrier(rec.last_error, depth + 1)) return true;
|
|
613
|
+
}
|
|
614
|
+
if (rec.details !== undefined && rec.details !== null) {
|
|
615
|
+
if (hasIncompatibleDiagnosticCarrier(rec.details, depth + 1)) return true;
|
|
616
|
+
}
|
|
617
|
+
if (rec.detail !== undefined && rec.detail !== null) {
|
|
618
|
+
if (hasIncompatibleDiagnosticCarrier(rec.detail, depth + 1)) return true;
|
|
619
|
+
}
|
|
620
|
+
if (rec.errors !== undefined && rec.errors !== null) {
|
|
621
|
+
if (hasIncompatibleDiagnosticCarrier(rec.errors, depth + 1)) return true;
|
|
622
|
+
}
|
|
623
|
+
if (rec.message !== undefined && rec.message !== null) {
|
|
624
|
+
if (typeof rec.message !== "string" || rec.message !== "Invalid reasoning effort.") return true;
|
|
625
|
+
}
|
|
626
|
+
return false;
|
|
627
|
+
}
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function isExactInvalidReasoningEffortRefusal(status: number, message: string, code?: string | null): boolean {
|
|
632
|
+
if (status !== 400 || message.length > 16_384) return false;
|
|
633
|
+
if (code !== undefined && code !== null && typeof code !== "string") return false;
|
|
634
|
+
const outerCode = normalizedFailureCode(code);
|
|
635
|
+
if (!EXACT_EFFORT_COMPATIBLE_CODES.has(outerCode)) return false;
|
|
636
|
+
if (isHardPolicyOrOriginRefusal(message) || hasConflictingReasoningEffortCode(message)) return false;
|
|
637
|
+
let text = message.trim();
|
|
638
|
+
for (let depth = 0; depth < 3; depth += 1) {
|
|
639
|
+
if (text.startsWith("Provider error 400: ")) text = text.slice("Provider error 400: ".length).trim();
|
|
640
|
+
if (text.startsWith("data:") && !text.includes("\n")) text = text.slice("data:".length).trim();
|
|
641
|
+
let payload: unknown;
|
|
642
|
+
try { payload = JSON.parse(text); } catch { return false; };
|
|
643
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return false;
|
|
644
|
+
const root = payload as Record<string, unknown>;
|
|
645
|
+
if (root.code !== undefined && root.code !== null && typeof root.code !== "string") return false;
|
|
646
|
+
if (root.type !== undefined && root.type !== null && typeof root.type !== "string") return false;
|
|
647
|
+
if (root.message !== undefined && root.message !== null && typeof root.message !== "string") return false;
|
|
648
|
+
if (root.param !== undefined && root.param !== null) {
|
|
649
|
+
if (typeof root.param !== "string") return false;
|
|
650
|
+
const param = root.param.trim().toLowerCase();
|
|
651
|
+
if (param !== "reasoning_effort" && param !== "reasoning.effort") return false;
|
|
652
|
+
}
|
|
653
|
+
const rootCode = normalizedFailureCode(root.code as string | undefined);
|
|
654
|
+
const rootType = normalizedFailureCode(root.type as string | undefined);
|
|
655
|
+
if (!EXACT_EFFORT_COMPATIBLE_CODES.has(rootCode)) return false;
|
|
656
|
+
if (!EXACT_EFFORT_COMPATIBLE_TYPES.has(rootType)) return false;
|
|
657
|
+
if (root.response !== undefined && root.response !== null) return false;
|
|
658
|
+
if (root.last_error !== undefined && root.last_error !== null) return false;
|
|
659
|
+
if (hasIncompatibleDiagnosticCarrier(root.details)) return false;
|
|
660
|
+
if (hasIncompatibleDiagnosticCarrier(root.detail)) return false;
|
|
661
|
+
if (hasIncompatibleDiagnosticCarrier(root.errors)) return false;
|
|
662
|
+
if (rootCode === "invalid_argument"
|
|
663
|
+
&& root.error === "Invalid reasoning effort."
|
|
664
|
+
&& (root.type === undefined || root.type === "invalid_request_error")
|
|
665
|
+
&& (root.param === undefined || root.param === "reasoning_effort" || root.param === "reasoning.effort")) {
|
|
666
|
+
if (root.message !== undefined && root.message !== null && root.message !== "Invalid reasoning effort.") return false;
|
|
667
|
+
return true;
|
|
668
|
+
}
|
|
669
|
+
const error = root.error;
|
|
670
|
+
if (!error || typeof error !== "object" || Array.isArray(error)) return false;
|
|
671
|
+
const record = error as Record<string, unknown>;
|
|
672
|
+
if (record.code !== undefined && record.code !== null && typeof record.code !== "string") return false;
|
|
673
|
+
if (record.type !== undefined && record.type !== null && typeof record.type !== "string") return false;
|
|
674
|
+
if (record.message !== undefined && record.message !== null && typeof record.message !== "string") return false;
|
|
675
|
+
if (record.error !== undefined && record.error !== null && typeof record.error !== "string") return false;
|
|
676
|
+
if (record.param !== undefined && record.param !== null) {
|
|
677
|
+
if (typeof record.param !== "string") return false;
|
|
678
|
+
const param = record.param.trim().toLowerCase();
|
|
679
|
+
if (param !== "reasoning_effort" && param !== "reasoning.effort") return false;
|
|
680
|
+
}
|
|
681
|
+
if (root.param !== undefined && root.param !== null && record.param !== undefined && record.param !== null) {
|
|
682
|
+
if (root.param.trim().toLowerCase() !== record.param.trim().toLowerCase()) return false;
|
|
683
|
+
}
|
|
684
|
+
const errorCode = normalizedFailureCode(record.code as string | undefined);
|
|
685
|
+
const errorType = normalizedFailureCode(record.type as string | undefined);
|
|
686
|
+
if (!EXACT_EFFORT_COMPATIBLE_CODES.has(errorCode)) return false;
|
|
687
|
+
if (!EXACT_EFFORT_COMPATIBLE_TYPES.has(errorType)) return false;
|
|
688
|
+
if (record.response !== undefined && record.response !== null) return false;
|
|
689
|
+
if (record.last_error !== undefined && record.last_error !== null) return false;
|
|
690
|
+
if (hasIncompatibleDiagnosticCarrier(record.details)) return false;
|
|
691
|
+
if (hasIncompatibleDiagnosticCarrier(record.detail)) return false;
|
|
692
|
+
if (hasIncompatibleDiagnosticCarrier(record.errors)) return false;
|
|
693
|
+
if (record.type === "upstream_error" && typeof record.message === "string"
|
|
694
|
+
&& record.message.startsWith("Provider error 400: ")
|
|
695
|
+
&& EXACT_EFFORT_COMPATIBLE_CODES.has(errorCode)) {
|
|
696
|
+
text = record.message;
|
|
697
|
+
continue;
|
|
698
|
+
}
|
|
699
|
+
if (root.type === "upstream_error" && typeof root.message === "string"
|
|
700
|
+
&& root.message.startsWith("Provider error 400: ")
|
|
701
|
+
&& EXACT_EFFORT_COMPATIBLE_CODES.has(rootCode)) {
|
|
702
|
+
text = root.message;
|
|
703
|
+
continue;
|
|
704
|
+
}
|
|
705
|
+
if (root.message !== undefined && root.message !== null && root.message !== "Invalid reasoning effort.") return false;
|
|
706
|
+
if (record.error !== undefined && record.error !== null && record.error !== "Invalid reasoning effort.") return false;
|
|
707
|
+
return record.type === "invalid_request_error"
|
|
708
|
+
&& record.message === "Invalid reasoning effort."
|
|
709
|
+
&& EXACT_EFFORT_COMPATIBLE_CODES.has(errorCode)
|
|
710
|
+
&& (record.param === undefined || record.param === "reasoning_effort" || record.param === "reasoning.effort");
|
|
711
|
+
}
|
|
712
|
+
return false;
|
|
713
|
+
}
|
|
714
|
+
|
|
385
715
|
/**
|
|
386
716
|
* A spent account window, by structured code or upstream prose. Status is deliberately not
|
|
387
717
|
* consulted: the ChatGPT Codex backend reports a depleted plan window as HTTP 502
|
|
@@ -489,6 +819,9 @@ const REQUEST_SHAPE_FAILURE_CODES = new Set([
|
|
|
489
819
|
// A route that cannot read the request's media (image input to a text-only CLI route) is
|
|
490
820
|
// healthy for every other request, so cooling it would only block unrelated turns.
|
|
491
821
|
"unsupported_input_modality",
|
|
822
|
+
// A request whose token cap was reached before output was produced is request-specific;
|
|
823
|
+
// the candidate remains healthy for subsequent requests with larger limits.
|
|
824
|
+
"zero_output_token_limit",
|
|
492
825
|
]);
|
|
493
826
|
|
|
494
827
|
/** Credential/billing failures that every target sharing the provider inherits. */
|
|
@@ -509,6 +842,7 @@ const PROVIDER_SCOPED_FAILURE_CODES = new Set([
|
|
|
509
842
|
* Image rejection requires `param: input` and an exact model-scoped prefix.
|
|
510
843
|
*/
|
|
511
844
|
function isRequestLocalTargetIncompatibility(status: number, message: string, code?: string | null): boolean {
|
|
845
|
+
if (isExactInvalidReasoningEffortRefusal(status, message, code)) return true;
|
|
512
846
|
if (status !== 400 || message.length > 16_384) return false;
|
|
513
847
|
const genericCodes = new Set(["", "invalid_request_error", "unsupported_parameter", "unsupported_value"]);
|
|
514
848
|
if (!genericCodes.has(normalizedFailureCode(code))) return false;
|
|
@@ -518,10 +852,20 @@ function isRequestLocalTargetIncompatibility(status: number, message: string, co
|
|
|
518
852
|
let payload: unknown;
|
|
519
853
|
try { payload = JSON.parse(text); } catch { return false; }
|
|
520
854
|
if (!payload || typeof payload !== "object" || Array.isArray(payload)) return false;
|
|
521
|
-
const
|
|
855
|
+
const root = payload as Record<string, unknown>;
|
|
856
|
+
if (root.code !== undefined && root.code !== null && typeof root.code !== "string") return false;
|
|
857
|
+
if (root.type !== undefined && root.type !== null && typeof root.type !== "string") return false;
|
|
858
|
+
if (root.param !== undefined && root.param !== null && typeof root.param !== "string") return false;
|
|
859
|
+
const rootCode = normalizedFailureCode(root.code as string | undefined);
|
|
860
|
+
if (!genericCodes.has(rootCode)) return false;
|
|
861
|
+
if (root.type !== undefined && root.type !== null && root.type !== "invalid_request_error" && root.type !== "upstream_error") return false;
|
|
862
|
+
const error = root.error;
|
|
522
863
|
if (!error || typeof error !== "object" || Array.isArray(error)) return false;
|
|
523
864
|
const e = error as Record<string, unknown>;
|
|
524
865
|
if (e.code !== undefined && e.code !== null && typeof e.code !== "string") return false;
|
|
866
|
+
if (root.param !== undefined && root.param !== null && typeof e.param === "string") {
|
|
867
|
+
if (root.param.trim().toLowerCase() !== e.param.trim().toLowerCase()) return false;
|
|
868
|
+
}
|
|
525
869
|
const errorCode = normalizedFailureCode(typeof e.code === "string" ? e.code : undefined);
|
|
526
870
|
if (!genericCodes.has(errorCode) || typeof e.message !== "string") return false;
|
|
527
871
|
if (e.type !== "invalid_request_error" && e.type !== "upstream_error") return false;
|
|
@@ -644,13 +988,19 @@ function isResponseFormatCapabilityRefusal(
|
|
|
644
988
|
return false;
|
|
645
989
|
}
|
|
646
990
|
|
|
991
|
+
/** Return the narrowest cooldown scope supported by this target's failure evidence. */
|
|
647
992
|
export function comboFailureCooldownScope(
|
|
648
993
|
status: number,
|
|
649
994
|
message: string,
|
|
650
|
-
options?: {
|
|
995
|
+
options?: {
|
|
996
|
+
code?: string | number | null;
|
|
997
|
+
malformedResponseFormat?: boolean;
|
|
998
|
+
openRouterOpaque400?: boolean;
|
|
999
|
+
},
|
|
651
1000
|
): ComboFailureCooldownScope {
|
|
652
|
-
const
|
|
653
|
-
|
|
1001
|
+
const rawCode = typeof options?.code === "number" ? String(options.code) : options?.code;
|
|
1002
|
+
const code = normalizedFailureCode(rawCode);
|
|
1003
|
+
if (isAnthropicBillingFailure(status, message, rawCode)) return "provider";
|
|
654
1004
|
// Request-shape refusals first: an oversized request must not cool a healthy target.
|
|
655
1005
|
// A native transport can surface a zero-output model overflow as a generic
|
|
656
1006
|
// upstream_server_error carrying precise context-window prose, so consult the bounded
|
|
@@ -658,19 +1008,23 @@ export function comboFailureCooldownScope(
|
|
|
658
1008
|
if (
|
|
659
1009
|
status === 413
|
|
660
1010
|
|| REQUEST_SHAPE_FAILURE_CODES.has(code)
|
|
661
|
-
|| isRequestLocalFreePromptCap(status, message,
|
|
662
|
-
|| isProviderTargetContextOverflow(status, message,
|
|
1011
|
+
|| isRequestLocalFreePromptCap(status, message, rawCode)
|
|
1012
|
+
|| isProviderTargetContextOverflow(status, message, rawCode)
|
|
663
1013
|
|| isDefiniteContextOverflow(status, message)
|
|
664
|
-
|| isRequestLocalTargetIncompatibility(status, message,
|
|
1014
|
+
|| isRequestLocalTargetIncompatibility(status, message, rawCode)
|
|
665
1015
|
// A capability gap says the target is healthy and the request did not fit it, which is the
|
|
666
1016
|
// same reason every other entry here refuses to cool a target.
|
|
667
|
-
|| isResponseFormatCapabilityRefusal(status, message,
|
|
1017
|
+
|| isResponseFormatCapabilityRefusal(status, message, rawCode)
|
|
668
1018
|
) return "none";
|
|
669
|
-
if (isProviderScopedQuotaCap(status, message,
|
|
1019
|
+
if (isProviderScopedQuotaCap(status, message, rawCode)) return "provider";
|
|
670
1020
|
// A rejected or unpaid credential is provider-wide evidence: every target that routes
|
|
671
1021
|
// through the same provider row carries the same key and will fail identically.
|
|
672
1022
|
if (status === 401 || status === 402 || status === 403) return "provider";
|
|
673
1023
|
if (PROVIDER_SCOPED_FAILURE_CODES.has(code)) return "provider";
|
|
1024
|
+
// OpenRouter's opaque numeric-code wrapper may be target-specific (for example a gateway
|
|
1025
|
+
// rejecting a model's translated payload). It may advance the combo, but says nothing about
|
|
1026
|
+
// target health and must not quarantine the target.
|
|
1027
|
+
if (isUncodedUpstream400(status, message, rawCode, options)) return "none";
|
|
674
1028
|
return "target";
|
|
675
1029
|
}
|
|
676
1030
|
|
|
@@ -783,15 +1137,53 @@ export function codexAccountModelRefusal(
|
|
|
783
1137
|
return codexAccountModelRefusalPayload(status, payload);
|
|
784
1138
|
}
|
|
785
1139
|
|
|
1140
|
+
/**
|
|
1141
|
+
* Decide whether a zero-output combo failure can move to another eligible target.
|
|
1142
|
+
*
|
|
1143
|
+
* `rootCode` is the envelope root's own structured code when the record `code` came from is a
|
|
1144
|
+
* nested one. Told about both, this applies the same rule for every transport: each code must
|
|
1145
|
+
* authorize the hop on its own, so a root hard stop is never hidden behind a retryable record
|
|
1146
|
+
* and a retryable pair is not stopped merely because the envelope states them twice.
|
|
1147
|
+
*/
|
|
786
1148
|
export function comboFailureDecision(
|
|
787
1149
|
status: number,
|
|
788
1150
|
message: string,
|
|
789
1151
|
options?: {
|
|
790
|
-
code?: string | null;
|
|
1152
|
+
code?: string | number | null;
|
|
1153
|
+
/** The envelope root's own code when it differs from `code`; absent or blank states none. */
|
|
1154
|
+
rootCode?: string | number | null;
|
|
791
1155
|
codexModelRefusal?: CodexAccountModelRefusal;
|
|
792
1156
|
failedAdapter?: string;
|
|
793
1157
|
/** The caller proved the failure is an adapter-normalized plan refusal (`isModelNotInPlanRefusal`). */
|
|
794
1158
|
verifiedPlanRefusal?: boolean;
|
|
1159
|
+
malformedResponseFormat?: boolean;
|
|
1160
|
+
openRouterOpaque400?: boolean;
|
|
1161
|
+
},
|
|
1162
|
+
): ComboFailureDecision {
|
|
1163
|
+
const rootRawCode = typeof options?.rootCode === "number" ? String(options.rootCode) : options?.rootCode;
|
|
1164
|
+
const nestedRawCode = typeof options?.code === "number" ? String(options.code) : options?.code;
|
|
1165
|
+
const rootCode = normalizedFailureCode(rootRawCode);
|
|
1166
|
+
const nestedCode = normalizedFailureCode(nestedRawCode);
|
|
1167
|
+
if (rootCode !== "" && rootCode !== nestedCode) {
|
|
1168
|
+
if (nestedCode === "") return decideComboFailure(status, message, { ...options, code: rootRawCode });
|
|
1169
|
+
const rootDecision = decideComboFailure(status, message, { ...options, code: rootRawCode });
|
|
1170
|
+
if (rootDecision === "stop") return "stop";
|
|
1171
|
+
return decideComboFailure(status, message, { ...options, code: nestedRawCode }) === "hop"
|
|
1172
|
+
? "hop" : "stop";
|
|
1173
|
+
}
|
|
1174
|
+
return decideComboFailure(status, message, options);
|
|
1175
|
+
}
|
|
1176
|
+
|
|
1177
|
+
function decideComboFailure(
|
|
1178
|
+
status: number,
|
|
1179
|
+
message: string,
|
|
1180
|
+
options?: {
|
|
1181
|
+
code?: string | number | null;
|
|
1182
|
+
codexModelRefusal?: CodexAccountModelRefusal;
|
|
1183
|
+
failedAdapter?: string;
|
|
1184
|
+
verifiedPlanRefusal?: boolean;
|
|
1185
|
+
malformedResponseFormat?: boolean;
|
|
1186
|
+
openRouterOpaque400?: boolean;
|
|
795
1187
|
},
|
|
796
1188
|
): ComboFailureDecision {
|
|
797
1189
|
if (status === 499) return "stop";
|
|
@@ -799,14 +1191,15 @@ export function comboFailureDecision(
|
|
|
799
1191
|
// Structured form of the same hard refusal. The prose test above misses it when the origin
|
|
800
1192
|
// reports the code out of band, and every hop rule below -- including the context-overflow
|
|
801
1193
|
// one -- must stay subordinate to it.
|
|
802
|
-
|
|
1194
|
+
const rawCode = typeof options?.code === "number" ? String(options.code) : options?.code;
|
|
1195
|
+
if (normalizedFailureCode(rawCode) === "origin_rejected") return "stop";
|
|
803
1196
|
// The origin may already be executing this turn (the Codex WebSocket relay sent the create
|
|
804
1197
|
// frame and never saw a response event). Hopping would send the same request to a second
|
|
805
1198
|
// target while the first may still be generating; the honest status goes to the client.
|
|
806
|
-
if (isNonReplayableUpstreamCode(
|
|
1199
|
+
if (isNonReplayableUpstreamCode(rawCode)) return "stop";
|
|
807
1200
|
// Cyber policy is a hard non-retryable refusal — honor structured code even when
|
|
808
1201
|
// classificationText was truncated before the JSON code field.
|
|
809
|
-
if (isCyberPolicyCode(
|
|
1202
|
+
if (isCyberPolicyCode(rawCode)) return "stop";
|
|
810
1203
|
// HTTP consumers retain hard codes before truncation; SSE metadata owns its selected carrier.
|
|
811
1204
|
if (options?.codexModelRefusal === undefined && codexAccountModelRefusalHardStopCode(status, message)) return "stop";
|
|
812
1205
|
const modelRefusal = status === 400
|
|
@@ -817,13 +1210,13 @@ export function comboFailureDecision(
|
|
|
817
1210
|
// however: another provider/model in the declared combo can still serve the request.
|
|
818
1211
|
// Require structured lifecycle code or explicit model+lifecycle prose so unrelated
|
|
819
1212
|
// application-level 410 responses remain fail-closed.
|
|
820
|
-
if (isModelLifecycleGone(status, message,
|
|
1213
|
+
if (isModelLifecycleGone(status, message, rawCode)) return "hop";
|
|
821
1214
|
const error = classifyError(status, "upstream_error", message);
|
|
822
1215
|
if (isCyberPolicyCode(error.code)) return "stop";
|
|
823
1216
|
// A provider can expose its own target hard cap with a non-semantic vendor code
|
|
824
1217
|
// (for example 5059 + invalid_request_prompt_too_long). That is evidence that this
|
|
825
1218
|
// target is too small, not that every later combo target is incapable of serving it.
|
|
826
|
-
if (isProviderTargetContextOverflow(status, message,
|
|
1219
|
+
if (isProviderTargetContextOverflow(status, message, rawCode)) return "hop";
|
|
827
1220
|
// A definite context-window refusal is target-local inside a heterogeneous combo: this model
|
|
828
1221
|
// cannot hold the turn, but a later target may have a larger window. Two boundaries keep this
|
|
829
1222
|
// safe. It is reached only after cancellation, structured origin/cyber refusals and
|
|
@@ -850,16 +1243,16 @@ export function comboFailureDecision(
|
|
|
850
1243
|
// an upstream already controls other hop signals (429, 5xx), and traversal is finite: policy
|
|
851
1244
|
// tries each candidate once via `tried`, and combo excludes each attempted target. So this is
|
|
852
1245
|
// structured-code-only, not provably local.
|
|
853
|
-
if (
|
|
1246
|
+
if (rawCode === "input_admission_refused" || error.code === "input_admission_refused") {
|
|
854
1247
|
return "hop";
|
|
855
1248
|
}
|
|
856
|
-
if (isProviderScopedQuotaCap(status, message,
|
|
1249
|
+
if (isProviderScopedQuotaCap(status, message, rawCode || error.code)) {
|
|
857
1250
|
return "hop";
|
|
858
1251
|
}
|
|
859
1252
|
// A model-scoped rejection is target-local: this provider does not serve THIS model, which
|
|
860
1253
|
// says nothing about the next combo target. Structured code only, plus the explicit prose
|
|
861
1254
|
// form upstreams emit when they carry no code, so an unrelated 400 stays terminal.
|
|
862
|
-
const failureCode = normalizedFailureCode(
|
|
1255
|
+
const failureCode = normalizedFailureCode(rawCode || error.code);
|
|
863
1256
|
// The input-modality refusal is the same kind of verdict: this route cannot read the turn's
|
|
864
1257
|
// media, and a later target may. Its 400 would otherwise end the chain as invalid_request_error.
|
|
865
1258
|
if (["model_not_found", "model_unavailable", "unsupported_model", "unsupported_input_modality"].includes(failureCode)) {
|
|
@@ -873,13 +1266,13 @@ export function comboFailureDecision(
|
|
|
873
1266
|
// `free_rate_limited` no longer routes through `isProviderScopedQuotaCap` (it is a
|
|
874
1267
|
// per-request cap, not provider-wide evidence), so keep its hop verdict explicit here.
|
|
875
1268
|
if (failureCode === "free_rate_limited") return "hop";
|
|
876
|
-
if (isRequestLocalTargetIncompatibility(status, message,
|
|
1269
|
+
if (isRequestLocalTargetIncompatibility(status, message, rawCode)) return "hop";
|
|
877
1270
|
// Must precede the generic `invalid_request_error` stop below, which is where this refusal
|
|
878
1271
|
// ended the chain: the gateway reports `type: "invalid_request_error"`, so the classifier
|
|
879
1272
|
// reaches that list and returns terminal before anything can ask whether the next target
|
|
880
1273
|
// could have served the request (#4903).
|
|
881
|
-
if (isResponseFormatCapabilityRefusal(status, message,
|
|
882
|
-
if (isAnthropicBillingFailure(status, message,
|
|
1274
|
+
if (isResponseFormatCapabilityRefusal(status, message, rawCode)) return "hop";
|
|
1275
|
+
if (isAnthropicBillingFailure(status, message, rawCode)) return "hop";
|
|
883
1276
|
// Kiro's body validator rejects the request for the identity it was routed under. The account
|
|
884
1277
|
// layer rotates logins first; this is the no-alternate case, so the next combo target is the
|
|
885
1278
|
// only remaining chance to serve the turn. Only a caller that knows the failed target is a Kiro
|
|
@@ -889,7 +1282,11 @@ export function comboFailureDecision(
|
|
|
889
1282
|
// A structured account-window exhaustion code is provider-state evidence even inside a
|
|
890
1283
|
// 400 / invalid_request_error envelope, so it must precede that stop. Exhaustion prose alone
|
|
891
1284
|
// never reaches this arm: a validation error can quote quota wording back (#7625).
|
|
892
|
-
if (hasAccountExhaustionCode(
|
|
1285
|
+
if (hasAccountExhaustionCode(rawCode)) return "hop";
|
|
1286
|
+
// OpenRouter can wrap an opaque provider failure as a numeric 400 with `metadata.raw: "ERROR"`
|
|
1287
|
+
// and no type. Let a heterogeneous combo try another target in this case, while keeping
|
|
1288
|
+
// malformed response_format diagnostics terminal and preserving any structured code.
|
|
1289
|
+
if (isUncodedUpstream400(status, message, rawCode, options)) return "hop";
|
|
893
1290
|
if (["origin_rejected", "context_length_exceeded", "invalid_request_error"].includes(error.code ?? "")) {
|
|
894
1291
|
return "stop";
|
|
895
1292
|
}
|