mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-08-18 13:14:56 +03:00
Compare commits
2 Commits
fix/10078-
...
fix/10314-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
db321435b5 | ||
|
|
3eebf985ed |
@@ -1,2 +0,0 @@
|
||||
- Fix: wire AgentRouter's existing console balance fetcher into the Dashboard Quota UI (visibility gate + provider-limits data path + background sync) so its wallet balance renders instead of falling back to "Usage API not implemented" (#10078)
|
||||
- Fix: AgentRouter's dollar balance now renders as a currency-formatted "$X.XX" credits row in the Dashboard Quota UI instead of a bare percentage, and an exhausted wallet always shows exactly $0.00 (#10078)
|
||||
1
changelog.d/fixes/10314-combo-error-aggregation.md
Normal file
1
changelog.d/fixes/10314-combo-error-aggregation.md
Normal file
@@ -0,0 +1 @@
|
||||
- fix(resilience): keep combo quality and auth failure reasons separate and redact connection labels in terminal errors (#10314)
|
||||
@@ -93,6 +93,14 @@ import {
|
||||
expandPromptCacheAffinityTargetsFromConnections,
|
||||
resolvePromptCacheAffinityKey,
|
||||
} from "./combo/promptCacheAffinity.ts";
|
||||
import {
|
||||
classifyComboOutcome,
|
||||
formatComboOutcomes,
|
||||
redactConnectionLabel,
|
||||
buildRedactedSummary,
|
||||
resolveComboTerminalStatus,
|
||||
} from "./combo/comboErrorAggregation.ts";
|
||||
import type { ComboErrorEntry } from "./combo/comboErrorAggregation.ts";
|
||||
import type { CompressionMode } from "./compression/types.ts";
|
||||
import { getCachedProviderConnections } from "../../src/lib/db/readCache";
|
||||
import { isProviderInCooldown, recordProviderCooldown } from "./providerCooldownTracker.ts";
|
||||
@@ -853,7 +861,7 @@ export async function handleComboChat({
|
||||
let comboExpired = false;
|
||||
// Accumulator for per-model error details across targets in the current set try.
|
||||
// Reset at the start of each set retry (same lifecycle as lastError/recordedAttempts).
|
||||
let comboErrors: Array<{ model: string; status: number; error: string }> = [];
|
||||
let comboErrors: Array<ComboErrorEntry> = [];
|
||||
// Quota trust spans set retries and recursive cooldown re-dispatches. Once any
|
||||
// failure is non-quota, a nested caller must never treat this dispatch as quota-only.
|
||||
let observedFailure = false;
|
||||
@@ -1343,6 +1351,15 @@ export async function handleComboChat({
|
||||
// misleading ALL_ACCOUNTS_INACTIVE when the real issue is quality.
|
||||
lastError = `Upstream response failed quality validation: ${quality.reason}`;
|
||||
lastStatus = 502;
|
||||
// #10314: record quality failures as a FIRST-CLASS per-target outcome
|
||||
// so a quality reason is never silently dropped from the aggregated
|
||||
// terminal message when a later sibling overwrites lastError.
|
||||
comboErrors.push({
|
||||
model: modelStr,
|
||||
status: 502,
|
||||
error: quality.reason || "upstream response failed quality validation",
|
||||
kind: "quality",
|
||||
});
|
||||
if (i > 0) fallbackCount++;
|
||||
if (provider && rawModel) {
|
||||
const mlSettings = resolveModelLockoutSettings(settings);
|
||||
@@ -1850,6 +1867,7 @@ export async function handleComboChat({
|
||||
model: modelStr,
|
||||
status: result.status,
|
||||
error: errorText || String(result.status),
|
||||
kind: classifyComboOutcome(result.status, errorText),
|
||||
});
|
||||
lastStatus = result.status;
|
||||
if (i > 0) fallbackCount++;
|
||||
@@ -2043,6 +2061,7 @@ export async function handleComboChat({
|
||||
model: modelStr,
|
||||
status: result.status,
|
||||
error: errorText || String(result.status),
|
||||
kind: classifyComboOutcome(result.status, errorText),
|
||||
});
|
||||
lastStatus = result.status;
|
||||
if (i > 0) fallbackCount++;
|
||||
@@ -2197,15 +2216,10 @@ export async function handleComboChat({
|
||||
|
||||
// Global combo timeout: return aggregated error immediately, skipping set retries.
|
||||
if (comboExpired) {
|
||||
const summary = comboErrors
|
||||
.slice(0, 5)
|
||||
.map((e) => `${e.model} (${e.status})`)
|
||||
.join(", ");
|
||||
const summary = buildRedactedSummary(comboErrors);
|
||||
const msg =
|
||||
`Combo global timeout (${comboTimeoutMs}ms) after ${recordedAttempts}/${orderedTargets.length} targets` +
|
||||
(comboErrors.length > 0
|
||||
? ` | tried: ${summary}${comboErrors.length > 5 ? `... (+${comboErrors.length - 5})` : ""}`
|
||||
: "");
|
||||
(comboErrors.length > 0 ? ` | tried: ${summary}` : "");
|
||||
const latencyMs = Date.now() - startTime;
|
||||
if (recordedAttempts === 0) {
|
||||
recordComboRequest(combo.name, null, {
|
||||
@@ -2275,19 +2289,20 @@ export async function handleComboChat({
|
||||
);
|
||||
}
|
||||
|
||||
const status = lastStatus;
|
||||
// Build aggregated error message with per-model failure details for diagnostics.
|
||||
const comboErrorSummary =
|
||||
comboErrors.length > 0
|
||||
? " [" +
|
||||
comboErrors
|
||||
.slice(0, 5)
|
||||
.map((e) => `${e.model} (${e.status})`)
|
||||
.join(", ") +
|
||||
(comboErrors.length > 5 ? `... (+${comboErrors.length - 5})` : "") +
|
||||
"]"
|
||||
: "";
|
||||
const msg = (lastError || "All combo models unavailable") + comboErrorSummary;
|
||||
// #10501: derive the terminal HTTP status from the structured per-target
|
||||
// outcomes instead of `lastStatus` (whichever target happened to fail
|
||||
// LAST). A 4xx is preserved only when the request itself is genuinely
|
||||
// invalid across every eligible target; a heterogeneous mix of failure
|
||||
// classes (e.g. a quality failure + a sibling's 401) normalizes to a
|
||||
// 5xx-class status reflecting an infra/provider problem, not a client
|
||||
// error. See comboErrorAggregation.ts::resolveComboTerminalStatus.
|
||||
const status = resolveComboTerminalStatus(comboErrors, lastStatus);
|
||||
// #10314: build the terminal message from the structured per-target
|
||||
// outcomes (each distinct class+reason listed separately) instead of
|
||||
// mashing a single lastError with raw `[model (status)]` markers. Connection
|
||||
// identifiers are redacted. Falls back to lastError when no target recorded
|
||||
// a structured outcome.
|
||||
const msg = formatComboOutcomes(comboErrors) || lastError || "All combo models unavailable";
|
||||
|
||||
// Cooldown-aware retry: instead of crystallizing a transient failure, wait
|
||||
// out a SHORT cooldown and re-run the whole set loop. Guarded by the helper
|
||||
@@ -2715,6 +2730,10 @@ async function handleRoundRobinCombo({
|
||||
let globalAttempts = 0;
|
||||
let fallbackCount = 0;
|
||||
let recordedAttempts = 0;
|
||||
// #10314: per-target outcome accumulator for the round-robin twin so the
|
||||
// terminal message lists each distinct reason separately (see the quality path
|
||||
// and the "Done with this model" path below), mirroring handleComboChat.
|
||||
const rrOutcomes: Array<ComboErrorEntry> = [];
|
||||
|
||||
// #1731: Per-request in-memory set of providers whose quota is fully exhausted.
|
||||
// When a target returns a quota-exhausted 429, remaining targets from the same
|
||||
@@ -2911,6 +2930,12 @@ async function handleRoundRobinCombo({
|
||||
// misleading ALL_ACCOUNTS_INACTIVE when the real issue is quality.
|
||||
lastError = `Upstream response failed quality validation: ${quality.reason}`;
|
||||
lastStatus = 502;
|
||||
rrOutcomes.push({
|
||||
model: modelStr,
|
||||
status: 502,
|
||||
error: quality.reason || "upstream response failed quality validation",
|
||||
kind: "quality",
|
||||
});
|
||||
if (offset > 0) fallbackCount++;
|
||||
break; // move to next model
|
||||
}
|
||||
@@ -3217,6 +3242,12 @@ async function handleRoundRobinCombo({
|
||||
recordedAttempts++;
|
||||
lastError = errorText || String(result.status);
|
||||
lastStatus = result.status;
|
||||
rrOutcomes.push({
|
||||
model: modelStr,
|
||||
status: result.status,
|
||||
error: errorText || String(result.status),
|
||||
kind: classifyComboOutcome(result.status, errorText),
|
||||
});
|
||||
if (offset > 0) fallbackCount++;
|
||||
log.warn("COMBO-RR", `${modelStr} failed, trying next model`, { status: result.status });
|
||||
|
||||
@@ -3336,8 +3367,13 @@ async function handleRoundRobinCombo({
|
||||
);
|
||||
}
|
||||
|
||||
const status = lastStatus;
|
||||
const msg = lastError || "All round-robin combo models unavailable";
|
||||
// #10501: same terminal-status policy as handleComboChat — see
|
||||
// comboErrorAggregation.ts::resolveComboTerminalStatus.
|
||||
const status = resolveComboTerminalStatus(rrOutcomes, lastStatus);
|
||||
// #10314: same structured per-target aggregation as handleComboChat — list each
|
||||
// distinct reason separately (redacted), fall back to lastError when no outcome.
|
||||
const msg =
|
||||
formatComboOutcomes(rrOutcomes) || lastError || "All round-robin combo models unavailable";
|
||||
|
||||
if (earliestRetryAfter && isRetryAfterEligibleStatus(status)) {
|
||||
const retryHuman = formatRetryAfter(toRetryAfterDisplayValue(earliestRetryAfter));
|
||||
|
||||
179
open-sse/services/combo/comboErrorAggregation.ts
Normal file
179
open-sse/services/combo/comboErrorAggregation.ts
Normal file
@@ -0,0 +1,179 @@
|
||||
/**
|
||||
* Shared combo terminal-error aggregation.
|
||||
*
|
||||
* #10314 — combo error aggregation mixes quality and auth. Prior to this module
|
||||
* the combo terminal message was built as a single `lastError` string (last
|
||||
* writer wins — it can only ever represent ONE target's reason) concatenated
|
||||
* with a raw `[model (status)]` suffix. A quality-failure reason from one
|
||||
* target and a sibling's 401 were collapsed into one client-facing sentence
|
||||
* (`invalid_api_key [openai/proxy-account-b (401)]`) and a quality reason that
|
||||
* was not the final failing target was dropped entirely.
|
||||
*
|
||||
* This module gives each per-target failure a structured {model, status, error,
|
||||
* kind} entry, so the terminal message can list every distinct reason
|
||||
* separately (and classification-labelled) instead of mashing them, and it
|
||||
* redacts connection/account identifiers that, on openai-compatible proxy
|
||||
* connections, used to surface verbatim in client-visible and shared-warn
|
||||
* strings (ops/PII leak).
|
||||
*/
|
||||
|
||||
export type ComboOutcomeKind =
|
||||
| "quality"
|
||||
| "auth"
|
||||
| "rate_limit"
|
||||
| "model"
|
||||
| "provider"
|
||||
| "timeout"
|
||||
| "skipped"
|
||||
| "upstream";
|
||||
|
||||
export interface ComboErrorEntry {
|
||||
model: string;
|
||||
status: number;
|
||||
error: string;
|
||||
kind: ComboOutcomeKind;
|
||||
}
|
||||
|
||||
const KIND_LABELS: Record<ComboOutcomeKind, string> = {
|
||||
quality: "quality validation",
|
||||
auth: "auth",
|
||||
rate_limit: "rate limit",
|
||||
model: "model",
|
||||
provider: "provider",
|
||||
timeout: "timeout",
|
||||
skipped: "skipped",
|
||||
upstream: "upstream",
|
||||
};
|
||||
|
||||
/**
|
||||
* Classify a single target's terminal outcome for the client-facing message.
|
||||
* Auth-class errors (401/403 or auth-sounding text) are kept distinct from
|
||||
* model-class (400/422) and provider-class (5xx) so a sibling's 401 is never
|
||||
* presented as "quality failed". Fall through to `model` for everything else.
|
||||
*
|
||||
* #10501: the ordering below is deliberate and load-bearing — the timeout
|
||||
* check MUST use an exact match (408 / 499), never `status >= 499`. A `>=`
|
||||
* comparison there swallows every 5xx status too (500 >= 499), which made the
|
||||
* `status >= 500` branch permanently unreachable and silently mislabeled every
|
||||
* real provider outage (500/502/503/504) as a client-side "timeout". 429 is
|
||||
* also given its own explicit branch: a rate-limit/quota signal is neither a
|
||||
* "the client's request is invalid" (`model`) nor a hard provider outage, and
|
||||
* lumping it into `model` would make `resolveComboTerminalStatus` treat a
|
||||
* heterogeneous 429 mix as a genuinely-invalid-request case by accident.
|
||||
*/
|
||||
export function classifyComboOutcome(status: number, errorText: string): ComboOutcomeKind {
|
||||
const text = typeof errorText === "string" ? errorText : "";
|
||||
if (
|
||||
status === 401 ||
|
||||
status === 403 ||
|
||||
/(invalid.?api.?key|unauthorized|not.?authorized|auth(entication|orization)?)/i.test(text)
|
||||
) {
|
||||
return "auth";
|
||||
}
|
||||
if (status === 429) return "rate_limit";
|
||||
if (status === 408 || status === 499) return "timeout";
|
||||
if (status >= 500) return "provider";
|
||||
return "model";
|
||||
}
|
||||
|
||||
/**
|
||||
* Redact connection/account identifiers that can ride inside a proxy target's
|
||||
* model string (openai-compatible proxy model names often carry a connection
|
||||
* label). UUIDs and long hex hashes are truncated to a short `conn:` prefix.
|
||||
* Provider/model names operators need for debugging are left intact.
|
||||
*/
|
||||
export function redactConnectionLabel(modelStr: string | null | undefined): string {
|
||||
const label = typeof modelStr === "string" && modelStr ? modelStr : "unknown";
|
||||
return label
|
||||
.replace(
|
||||
/\b[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\b/g,
|
||||
(m) => `conn:${m.slice(0, 8)}`
|
||||
)
|
||||
.replace(/\b[0-9a-fA-F]{16,}\b/g, (m) => `conn:${m.slice(0, 8)}`);
|
||||
}
|
||||
|
||||
/** Build the redacted, collision-free `model (status)` summary used by the
|
||||
* global-combo-timeout diagnostics path. */
|
||||
export function buildRedactedSummary(
|
||||
entries: Array<{ model: string; status: number }> | ReadonlyArray<{ model: string; status: number }>
|
||||
): string {
|
||||
const slice = entries.slice(0, 5);
|
||||
const parts = slice.map((e) => `${redactConnectionLabel(e.model)} (${e.status})`).join(", ");
|
||||
return entries.length > 5 ? `${parts}... (+${entries.length - 5})` : parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format per-target terminal outcomes into one client-facing sentence that keeps
|
||||
* every distinct reason separate (and classification-labelled) instead of
|
||||
* mashing a single `lastError` with raw status markers. Always redacts
|
||||
* connection identifiers unless `{ redact: false }` is explicitly passed.
|
||||
*/
|
||||
export function formatComboOutcomes(
|
||||
entries: ReadonlyArray<{ model: string; status: number; error: string; kind?: ComboOutcomeKind }>,
|
||||
opts?: { redact?: boolean }
|
||||
): string {
|
||||
if (!entries.length) return "";
|
||||
const redact = opts?.redact !== false;
|
||||
const slice = entries.slice(0, 5);
|
||||
const parts = slice.map((e) => {
|
||||
const label = redact ? redactConnectionLabel(e.model) : e.model;
|
||||
const kind = e.kind ? KIND_LABELS[e.kind] ?? e.kind : null;
|
||||
// #10501: the raw upstream error TEXT can itself carry a connection/account
|
||||
// identifier (some openai-compatible proxies echo it back in the error body,
|
||||
// e.g. "invalid key for connection <uuid>") — redact it here too, not just
|
||||
// the model label above, or the identifier leaks into the client-facing
|
||||
// terminal message regardless of the label redaction.
|
||||
const rawReason = e.error || `HTTP ${e.status}`;
|
||||
const reason = redact ? redactConnectionLabel(rawReason) : rawReason;
|
||||
const statusTxt = ` (HTTP ${e.status})`;
|
||||
return kind ? `${label}: ${kind} — ${reason}${statusTxt}` : `${label}: ${reason}${statusTxt}`;
|
||||
});
|
||||
return entries.length > 5
|
||||
? `${parts.join("; ")}... (+${entries.length - 5} more)`
|
||||
: parts.join("; ");
|
||||
}
|
||||
|
||||
/**
|
||||
* #10501: explicit terminal-status policy for heterogeneous combo target
|
||||
* exhaustion. Prior behavior returned `lastStatus` — whichever target
|
||||
* happened to fail LAST, independent of what the other targets failed with.
|
||||
* That let an unrelated target's config-class 4xx (or a target's own auth
|
||||
* failure) masquerade as the combo's overall verdict, and vice versa.
|
||||
*
|
||||
* Policy:
|
||||
* - No structured entries: keep the caller's fallback status unchanged.
|
||||
* - Every entry is `model`-class AND a genuine 4xx (the request itself is
|
||||
* invalid on EVERY eligible target, homogeneous or not): preserve that
|
||||
* 4xx — this is a real client-request error, not an infra problem.
|
||||
* - All entries share the SAME kind (any kind, e.g. every target failed
|
||||
* with `auth`, or every target was `rate_limit`): preserve that shared
|
||||
* class's own status — a uniform reason across all targets is still a
|
||||
* single, well-defined verdict.
|
||||
* - Otherwise (a genuine MIX of different failure classes — e.g. a quality
|
||||
* failure on one target and a 401 on a sibling): this is heterogeneous by
|
||||
* definition, so it is normalized to a 5xx-class infra/provider status
|
||||
* instead of surfacing whichever target's status happened to be recorded
|
||||
* last. `timeout` present anywhere in the mix maps to 504 (Gateway
|
||||
* Timeout); otherwise 502 (Bad Gateway) — combo routing itself is the
|
||||
* "gateway" that could not complete the request via any target.
|
||||
*/
|
||||
export function resolveComboTerminalStatus(
|
||||
entries: ReadonlyArray<ComboErrorEntry>,
|
||||
fallbackStatus: number
|
||||
): number {
|
||||
if (!entries.length) return fallbackStatus;
|
||||
|
||||
const allGenuinelyInvalidRequest = entries.every(
|
||||
(e) => e.kind === "model" && e.status >= 400 && e.status < 500
|
||||
);
|
||||
if (allGenuinelyInvalidRequest) {
|
||||
return entries[entries.length - 1].status;
|
||||
}
|
||||
|
||||
const distinctKinds = new Set(entries.map((e) => e.kind));
|
||||
if (distinctKinds.size === 1) {
|
||||
return entries[entries.length - 1].status;
|
||||
}
|
||||
|
||||
return entries.some((e) => e.kind === "timeout") ? 504 : 502;
|
||||
}
|
||||
@@ -71,7 +71,6 @@ import { getFirecrawlUsage } from "./usage/firecrawl.ts";
|
||||
import { getCommandCodeUsage } from "./usage/command-code.ts";
|
||||
import { getQwenTokenPlanUsage } from "./usage/qwen-token-plan.ts";
|
||||
import { getConolUsage } from "./conolUsage.ts";
|
||||
import { getAgentrouterUsage } from "./usage/agentrouter.ts";
|
||||
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
type UsageProviderConnection = JsonRecord & {
|
||||
@@ -139,8 +138,6 @@ export const USAGE_FETCHER_PROVIDERS = [
|
||||
"command-code",
|
||||
"conol-web",
|
||||
"cnl",
|
||||
// AgentRouter (New-API) console balance (GET /api/user/self)
|
||||
"agentrouter",
|
||||
] as const;
|
||||
|
||||
export type UsageFetcherProvider = (typeof USAGE_FETCHER_PROVIDERS)[number];
|
||||
@@ -247,8 +244,6 @@ export async function getUsageForProvider(
|
||||
case "conol-web":
|
||||
case "cnl":
|
||||
return await getConolUsage(apiKey || accessToken, providerSpecificData);
|
||||
case "agentrouter":
|
||||
return await getAgentrouterUsage(id, connection);
|
||||
default:
|
||||
return { message: `Usage API not implemented for ${provider}` };
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
/**
|
||||
* usage/agentrouter.ts — AgentRouter (New-API) balance quota shapes the Provider
|
||||
* Limits dashboard expects.
|
||||
*
|
||||
* Reuses the already-registered preflight/monitor fetcher (OpenAI-style routing
|
||||
* apiKey vs console System Access Token + New-Api-User id) instead of re-implementing
|
||||
* the HTTP call, so the 60s in-memory cache in agentrouterQuotaFetcher.ts is shared.
|
||||
*
|
||||
* AgentRouter exposes a raw New-API credit balance, not a real grant to divide by —
|
||||
* so, following the DeepSeek boolean-availability precedent, `remainingPercentage` is
|
||||
* only a two-state signal (100 = has balance, 0 = exhausted) used for the quota-card
|
||||
* bar color. The human-meaningful number — the actual USD balance (rawQuota /
|
||||
* QUOTA_PER_UNIT) — MUST travel inside `quotas.balance.remaining` so the Dashboard
|
||||
* Quota UI's credits-row renderer (quotaParsing.ts::parseAgentrouterQuota, which reads
|
||||
* `quota.remaining`/`quota.currency`) can format it with a currency symbol instead of
|
||||
* dropping it: `getUsageForProvider()`'s top-level `remainingUsd`/`availableUsd`/
|
||||
* `balance` sibling fields exist for API/CLI consumers only — parseQuotaData() (the
|
||||
* Dashboard renderer) never reads them, only `data.quotas` (#10078 follow-up).
|
||||
*/
|
||||
import { fetchAgentrouterQuota, type AgentrouterQuota } from "../agentrouterQuotaFetcher.ts";
|
||||
import { type UsageQuota } from "./quota.ts";
|
||||
|
||||
type JsonRecord = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* AgentRouter balance → dashboard usage shape.
|
||||
*
|
||||
* Returns `{ message }` when the fetch returns null (no console credentials, an
|
||||
* upstream error, or a rejected token), which the Provider Limits UI renders as a
|
||||
* graceful per-row status instead of crashing the whole page. Otherwise shapes the
|
||||
* balance into a single USD `quotas.balance` entry whose `remaining` field carries
|
||||
* the exact dollar amount (never negative, exactly 0 when the wallet is exhausted).
|
||||
*/
|
||||
export async function getAgentrouterUsage(
|
||||
connectionId: string | undefined,
|
||||
connection: JsonRecord
|
||||
) {
|
||||
const quota = (await fetchAgentrouterQuota(
|
||||
connectionId || "",
|
||||
connection
|
||||
)) as AgentrouterQuota | null;
|
||||
|
||||
if (!quota) {
|
||||
return {
|
||||
message:
|
||||
"AgentRouter balance not available. Add the Console API Key + New-API User ID to the connection to view usage.",
|
||||
};
|
||||
}
|
||||
|
||||
// `dollarBalance` is already `rawQuota / QUOTA_PER_UNIT` (agentrouterQuotaFetcher.ts);
|
||||
// clamp defensively so an exhausted/mis-parsed wallet never surfaces as negative.
|
||||
const remainingUsd = Math.max(0, quota.dollarBalance);
|
||||
const remainingPercentage = quota.limitReached ? 0 : 100;
|
||||
|
||||
const balance: UsageQuota = {
|
||||
used: 0,
|
||||
total: 0,
|
||||
remaining: remainingUsd,
|
||||
remainingPercentage,
|
||||
resetAt: quota.resetAt ?? null,
|
||||
unlimited: true,
|
||||
currency: "USD",
|
||||
displayName: "Wallet Balance (USD)",
|
||||
};
|
||||
|
||||
return {
|
||||
plan: "AgentRouter",
|
||||
quotas: { balance },
|
||||
remainingUsd,
|
||||
availableUsd: remainingUsd,
|
||||
balance: remainingUsd,
|
||||
};
|
||||
}
|
||||
@@ -217,27 +217,6 @@ function parseDeepseek(data: any) {
|
||||
return quotaEntries(data).map(([quotaKey, quota]) => parseDeepseekQuota(quotaKey, quota));
|
||||
}
|
||||
|
||||
// #10078 follow-up: AgentRouter's `quotas.balance` entry (open-sse/services/usage/agentrouter.ts)
|
||||
// carries a real USD amount in `remaining` + `currency: "USD"`. The generic path
|
||||
// (normalizeQuotaEntry via parseGeneric) drops `currency` entirely and never sets
|
||||
// `isCredits`/`creditCount`, so QuotaCardBody/QuotaCardExpanded's dollar-formatted
|
||||
// renderer (which only activates on `q.isCredits`) never triggers — the balance was
|
||||
// rendered as a bare "100%/0% left" percentage instead of "$X.XX". Route it through
|
||||
// buildCreditsQuota() (same shape DeepSeek/Claude-extra-usage credits rows use) so the
|
||||
// dollar figure — and an exhausted ($0.00) balance — render unambiguously as USD.
|
||||
function parseAgentrouterQuota(quotaKey: string, quota: any) {
|
||||
if (quotaKey !== "balance") return normalizeQuotaEntry(quotaKey, quota);
|
||||
const remaining = Math.max(0, Number(quota?.remaining ?? 0));
|
||||
const currency = quota?.currency || "USD";
|
||||
const remainingPercentage =
|
||||
safePercentage(quota?.remainingPercentage) ?? (remaining > 0 ? 100 : 0);
|
||||
return buildCreditsQuota(currency, remaining, remainingPercentage, { currency });
|
||||
}
|
||||
|
||||
function parseAgentrouter(data: any) {
|
||||
return quotaEntries(data).map(([quotaKey, quota]) => parseAgentrouterQuota(quotaKey, quota));
|
||||
}
|
||||
|
||||
function parseProviderQuotas(providerId: string, data: any) {
|
||||
if (providerId === "github") return parseGithub(data);
|
||||
if (["glm", "glm-cn", "glmt", "opencode-go"].includes(providerId)) return parseGlmFamily(data);
|
||||
@@ -245,7 +224,6 @@ function parseProviderQuotas(providerId: string, data: any) {
|
||||
if (providerId === "codex") return parseCodex(data);
|
||||
if (providerId === "claude") return parseClaude(data);
|
||||
if (providerId === "deepseek") return parseDeepseek(data);
|
||||
if (providerId === "agentrouter") return parseAgentrouter(data);
|
||||
return parseGeneric(data);
|
||||
}
|
||||
|
||||
|
||||
@@ -100,8 +100,6 @@ const PROVIDER_LIMITS_APIKEY_PROVIDERS = new Set([
|
||||
// Alibaba Coding Plan (console API key) + Qwen personal Token Plan (console cookie) — #9603
|
||||
"bailian-coding-plan",
|
||||
"qwen-cloud-token-plan",
|
||||
// AgentRouter (New-API) console System Access Token + New-Api-User id (providerSpecificData)
|
||||
"agentrouter",
|
||||
]);
|
||||
const DEFAULT_PROVIDER_LIMITS_SYNC_INTERVAL_MINUTES = 70;
|
||||
const PROVIDER_LIMITS_AUTO_SYNC_SETTING_KEY = "provider_limits_auto_sync_last_run";
|
||||
|
||||
@@ -504,8 +504,6 @@ export const USAGE_SUPPORTED_PROVIDERS = [
|
||||
"bailian-coding-plan",
|
||||
// Qwen Cloud / Model Studio personal Token Plan (cookie-authenticated console gateway)
|
||||
"qwen-cloud-token-plan",
|
||||
// AgentRouter (New-API) console balance quota (consoleApiKey + newApiUserId)
|
||||
"agentrouter",
|
||||
];
|
||||
|
||||
// ── Zod validation at module load (Phase 7.2) ──
|
||||
|
||||
@@ -1,117 +0,0 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { getAgentrouterUsage } from "../../open-sse/services/usage/agentrouter.ts";
|
||||
import { parseQuotaData } from "../../src/app/(dashboard)/dashboard/usage/components/ProviderLimits/quotaParsing.ts";
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
test.afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
function mockAgentrouterFetch(rawQuota: number) {
|
||||
globalThis.fetch = (async () =>
|
||||
new Response(JSON.stringify({ data: { quota: rawQuota } }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
})) as typeof fetch;
|
||||
}
|
||||
|
||||
/**
|
||||
* #10078 follow-up — the original fix wired AgentRouter's balance into
|
||||
* getUsageForProvider()/USAGE_SUPPORTED_PROVIDERS (visibility + data path), but the
|
||||
* Dashboard Quota UI *renderer* (QuotaCardBody / QuotaCardExpanded under
|
||||
* src/app/(dashboard)/dashboard/usage/components/ProviderLimits/) only formats a
|
||||
* quota row as a dollar amount ("$X.XX") when the row carries `isCredits: true` +
|
||||
* `currency` + `creditCount` — fields the generic quota-parsing path
|
||||
* (parseGeneric -> normalizeQuotaEntry in quotaParsing.ts) never sets, and never
|
||||
* copies `currency` through at all. Because "agentrouter" wasn't special-cased in
|
||||
* parseProviderQuotas(), a configured balance rendered as a bare "100% left"
|
||||
* percentage (not USD), and the real dollar figure (`dollarBalance`) was only
|
||||
* exposed as a top-level `remainingUsd`/`availableUsd`/`balance` sibling field that
|
||||
* parseQuotaData() never reads (it only walks `data.quotas`).
|
||||
*
|
||||
* This test drives the real producer (getAgentrouterUsage) through the real
|
||||
* Dashboard adapter (parseQuotaData) end-to-end — the same path the Provider
|
||||
* Limits UI takes — and asserts the row the renderer actually consumes
|
||||
* (`isCredits`, `currency`, `creditCount`) instead of just the wire-shape fields
|
||||
* asserted by tests/unit/agentrouter-quota-visibility.test.ts.
|
||||
*/
|
||||
test("#10078: a configured AgentRouter balance renders as a USD credits row in the Dashboard Quota UI", async () => {
|
||||
const connectionId = `agentrouter-dash-configured-${Date.now()}`;
|
||||
mockAgentrouterFetch(250_000); // 250_000 / 500_000 QUOTA_PER_UNIT = $0.50
|
||||
|
||||
const usage = await getAgentrouterUsage(connectionId, {
|
||||
provider: "agentrouter",
|
||||
providerSpecificData: { consoleApiKey: "system-access-token", newApiUserId: "42" },
|
||||
});
|
||||
|
||||
const rows = parseQuotaData("agentrouter", usage) as Array<{
|
||||
isCredits?: boolean;
|
||||
currency?: string;
|
||||
creditCount?: number;
|
||||
remainingPercentage?: number;
|
||||
}>;
|
||||
|
||||
assert.equal(rows.length, 1, `expected exactly one quota row, got: ${JSON.stringify(rows)}`);
|
||||
const [row] = rows;
|
||||
|
||||
// These are exactly the fields QuotaCardBody.tsx / QuotaCardExpanded.tsx branch on
|
||||
// to render a dollar-formatted amount ("$0.50") instead of a bare percentage.
|
||||
assert.equal(row.isCredits, true, "renderer only formats USD when isCredits is true");
|
||||
assert.equal(row.currency, "USD", "renderer looks up CURRENCY_SYMBOLS[q.currency]");
|
||||
assert.equal(row.creditCount, 0.5, "renderer displays q.creditCount as the dollar amount");
|
||||
assert.equal(row.remainingPercentage, 100, "a funded wallet must not read as exhausted");
|
||||
});
|
||||
|
||||
test("#10078: an exhausted AgentRouter balance renders as exactly $0, not negative or NaN", async () => {
|
||||
const connectionId = `agentrouter-dash-exhausted-${Date.now()}`;
|
||||
mockAgentrouterFetch(0);
|
||||
|
||||
const usage = await getAgentrouterUsage(connectionId, {
|
||||
provider: "agentrouter",
|
||||
providerSpecificData: { consoleApiKey: "system-access-token", newApiUserId: "42" },
|
||||
});
|
||||
|
||||
const rows = parseQuotaData("agentrouter", usage) as Array<{
|
||||
isCredits?: boolean;
|
||||
currency?: string;
|
||||
creditCount?: number;
|
||||
remainingPercentage?: number;
|
||||
}>;
|
||||
|
||||
assert.equal(rows.length, 1);
|
||||
const [row] = rows;
|
||||
|
||||
assert.equal(row.isCredits, true);
|
||||
assert.equal(row.currency, "USD");
|
||||
assert.equal(row.creditCount, 0, "exhausted balance must render as exactly zero");
|
||||
assert.equal(Number.isFinite(row.creditCount), true, "must never render NaN");
|
||||
assert.ok((row.creditCount ?? -1) >= 0, "must never render negative");
|
||||
assert.equal(row.remainingPercentage, 0, "exhausted wallet must read as 0% remaining (critical color)");
|
||||
});
|
||||
|
||||
test("#10078: parseQuotaData never drops a raw negative/garbage remaining as -$X — clamps to 0", () => {
|
||||
// Defends the Math.max(0, ...) clamp in both getAgentrouterUsage() and
|
||||
// parseAgentrouterQuota() against a malformed/negative upstream `remaining`.
|
||||
const data = {
|
||||
plan: "AgentRouter",
|
||||
quotas: {
|
||||
balance: {
|
||||
used: 0,
|
||||
total: 0,
|
||||
remaining: -5,
|
||||
remainingPercentage: 0,
|
||||
resetAt: null,
|
||||
unlimited: true,
|
||||
currency: "USD",
|
||||
displayName: "Wallet Balance (USD)",
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
const rows = parseQuotaData("agentrouter", data) as Array<{ creditCount?: number }>;
|
||||
assert.equal(rows.length, 1);
|
||||
assert.equal(rows[0].creditCount, 0);
|
||||
});
|
||||
@@ -1,86 +0,0 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
import { USAGE_SUPPORTED_PROVIDERS } from "../../src/shared/constants/providers.ts";
|
||||
import { supportsProviderQuota } from "../../src/shared/utils/providerQuotaVisibility.ts";
|
||||
import {
|
||||
USAGE_FETCHER_PROVIDERS,
|
||||
getUsageForProvider,
|
||||
} from "../../open-sse/services/usage.ts";
|
||||
import {
|
||||
getAgentrouterUsage,
|
||||
} from "../../open-sse/services/usage/agentrouter.ts";
|
||||
import {
|
||||
invalidateAgentrouterQuotaCache,
|
||||
type AgentrouterQuota,
|
||||
} from "../../open-sse/services/agentrouterQuotaFetcher.ts";
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
test.afterEach(() => {
|
||||
globalThis.fetch = originalFetch;
|
||||
});
|
||||
|
||||
/**
|
||||
* #10078 — AgentRouter quota was missing from the dashboard:
|
||||
* - USAGE_SUPPORTED_PROVIDERS (visibility gate) omitted "agentrouter", and
|
||||
* - USAGE_FETCHER_PROVIDERS + getUsageForProvider (the provider-limits data
|
||||
* path) had no "agentrouter" case, so /api/usage/provider-limits fell back
|
||||
* to the generic "Usage API not implemented" message.
|
||||
* These three assertions are the permanent regression guard (RED before the
|
||||
* fix, GREEN after).
|
||||
*/
|
||||
test("#10078: agentrouter is present in USAGE_SUPPORTED_PROVIDERS", () => {
|
||||
assert.equal(
|
||||
USAGE_SUPPORTED_PROVIDERS.includes("agentrouter" as (typeof USAGE_SUPPORTED_PROVIDERS)[number]),
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
test("#10078: supportsProviderQuota('agentrouter') is true", () => {
|
||||
assert.equal(supportsProviderQuota("agentrouter"), true);
|
||||
});
|
||||
|
||||
test("#10078: agentrouter is present in USAGE_FETCHER_PROVIDERS", () => {
|
||||
assert.equal(
|
||||
USAGE_FETCHER_PROVIDERS.includes("agentrouter" as (typeof USAGE_FETCHER_PROVIDERS)[number]),
|
||||
true
|
||||
);
|
||||
});
|
||||
|
||||
test("#10078: getUsageForProvider shapes the AgentRouter balance into a USD quota", async () => {
|
||||
const connectionId = `agentrouter-vis-${Date.now()}`;
|
||||
globalThis.fetch = (async () => {
|
||||
return new Response(JSON.stringify({ data: { quota: 250_000 } }), {
|
||||
status: 200,
|
||||
headers: { "content-type": "application/json" },
|
||||
});
|
||||
}) as typeof fetch;
|
||||
|
||||
const usage = (await getUsageForProvider({
|
||||
id: connectionId,
|
||||
provider: "agentrouter",
|
||||
providerSpecificData: { consoleApiKey: "system-access-token", newApiUserId: "42" },
|
||||
})) as {
|
||||
plan?: string;
|
||||
quotas?: Record<string, { displayName?: string; remainingPercentage?: number }>;
|
||||
remainingUsd?: number;
|
||||
};
|
||||
|
||||
assert.equal(usage.plan, "AgentRouter");
|
||||
assert.ok(usage.quotas);
|
||||
const balance = usage.quotas.balance;
|
||||
assert.ok(balance, "expected a `balance` quota entry");
|
||||
assert.equal(balance.displayName, "Wallet Balance (USD)");
|
||||
assert.equal(usage.remainingUsd, 0.5);
|
||||
});
|
||||
|
||||
test("#10078: getAgentrouterUsage returns a graceful message when console credentials are missing", async () => {
|
||||
const usage = (await getAgentrouterUsage(`missing-${Date.now()}`, {
|
||||
provider: "agentrouter",
|
||||
})) as { message?: string; quotas?: unknown };
|
||||
|
||||
assert.equal(typeof usage.message, "string");
|
||||
assert.ok(/not available/i.test(usage.message || ""));
|
||||
assert.equal(usage.quotas, undefined);
|
||||
});
|
||||
@@ -66,7 +66,18 @@ async function runScenario(models: string[]) {
|
||||
return { result, modelsCalled };
|
||||
}
|
||||
|
||||
test("#8486 Part B: combo unavailableResponse must not attach an unrelated target's long retryAfter to the antigravity missing-projectId 422", async () => {
|
||||
// #10314/#10501 superseded the original "one target's message wins, silently
|
||||
// drops the sibling's reason" contract these two tests pinned: combo terminal
|
||||
// aggregation now DELIBERATELY lists every distinct per-target reason (#10314)
|
||||
// and normalizes a heterogeneous failure mix to a 5xx-class status instead of a
|
||||
// bare `lastStatus` (#10501 — see comboErrorAggregation.ts::resolveComboTerminalStatus).
|
||||
// The underlying #8486 concern — a config-class error getting the WRONG target's
|
||||
// long retry-after window stitched onto it — is still the thing under test, just
|
||||
// verified against the new contract: the response's `Retry-After` HEADER (the
|
||||
// actual out-of-band decoration #8486 was about) must never carry the unrelated
|
||||
// 21h47m window, in EITHER attempt order, regardless of which reasons appear in
|
||||
// the (now intentionally multi-reason) message body.
|
||||
test("#8486 Part B: heterogeneous rate_limit+config-class antigravity failure never attaches the unrelated 21h47m retryAfter as a response header", async () => {
|
||||
const { result, modelsCalled } = await runScenario([
|
||||
"antigravity/account-a-model",
|
||||
"antigravity/account-b-model",
|
||||
@@ -78,17 +89,24 @@ test("#8486 Part B: combo unavailableResponse must not attach an unrelated targe
|
||||
`expected both targets to be tried, got: ${JSON.stringify(modelsCalled)}`
|
||||
);
|
||||
|
||||
const text = await result.clone().text();
|
||||
|
||||
assert.ok(
|
||||
!/reset after/i.test(text) || !/missing google projectid/i.test(text),
|
||||
"a config-class antigravity error (missing_project_id, no retryAfter of its own) " +
|
||||
"must not be decorated with an unrelated target's long retry-after window — " +
|
||||
`got body: ${text}`
|
||||
// #10501: neither target's failure alone proves the CLIENT's request was
|
||||
// invalid (one is a rate limit, the other a config/auth problem) — the
|
||||
// heterogeneous mix must normalize to a 5xx infra/provider status.
|
||||
assert.equal(result.status, 502);
|
||||
assert.equal(
|
||||
result.headers.get("Retry-After"),
|
||||
null,
|
||||
"the config-class 422 (no retryAfter of its own) must never end up decorated " +
|
||||
"with account-a's unrelated 21h47m retry-after header"
|
||||
);
|
||||
|
||||
// #10314: both distinct reasons are now surfaced (never silently dropped).
|
||||
const text = await result.clone().text();
|
||||
assert.match(text, /reset after 21h47m32s/i);
|
||||
assert.match(text, /missing google projectid/i);
|
||||
});
|
||||
|
||||
test("#8486 Part B (reverse order): the config-class 422 must not swallow a genuinely rate-limited sibling's message either", async () => {
|
||||
test("#8486 Part B (reverse order): same result independent of which target failed first — both reasons present, no bogus Retry-After header", async () => {
|
||||
const { result, modelsCalled } = await runScenario([
|
||||
"antigravity/account-b-model",
|
||||
"antigravity/account-a-model",
|
||||
@@ -100,14 +118,10 @@ test("#8486 Part B (reverse order): the config-class 422 must not swallow a genu
|
||||
`expected both targets to be tried, got: ${JSON.stringify(modelsCalled)}`
|
||||
);
|
||||
|
||||
const text = await result.clone().text();
|
||||
assert.equal(result.status, 502, "attempt order must not change the terminal status");
|
||||
assert.equal(result.headers.get("Retry-After"), null);
|
||||
|
||||
// The surfaced status/message pair must always originate from the SAME
|
||||
// (last-attempted) target: here that's account-a (429, real retryAfter),
|
||||
// so the response must carry ITS message and MAY carry its own retry-after
|
||||
// — but must never resurrect the unrelated account-b 422 text alongside it.
|
||||
assert.ok(
|
||||
!/missing google projectid/i.test(text),
|
||||
`expected the last target's (account-a, 429) own message, not the unrelated account-b 422 text — got body: ${text}`
|
||||
);
|
||||
const text = await result.clone().text();
|
||||
assert.match(text, /reset after 21h47m32s/i);
|
||||
assert.match(text, /missing google projectid/i);
|
||||
});
|
||||
|
||||
165
tests/unit/combo-error-aggregation.test.ts
Normal file
165
tests/unit/combo-error-aggregation.test.ts
Normal file
@@ -0,0 +1,165 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import {
|
||||
classifyComboOutcome,
|
||||
formatComboOutcomes,
|
||||
redactConnectionLabel,
|
||||
buildRedactedSummary,
|
||||
resolveComboTerminalStatus,
|
||||
} from "../../open-sse/services/combo/comboErrorAggregation.ts";
|
||||
|
||||
// #10314 — combo error aggregation mixes quality and auth.
|
||||
// Regression guard for the pure aggregation helpers: a quality-failure reason from one
|
||||
// target and a sibling's 401 must be presented as SEPARATE classified outcomes (never
|
||||
// mashed into a single lastError), and account/connection identifiers must be redacted
|
||||
// from client-visible and shared-warn strings.
|
||||
|
||||
test("#10314: classifyComboOutcome keeps auth distinct from quality/model", () => {
|
||||
assert.equal(classifyComboOutcome(401, "invalid_api_key"), "auth");
|
||||
assert.equal(classifyComboOutcome(403, "not authorized"), "auth");
|
||||
assert.equal(classifyComboOutcome(408, "timeout"), "timeout");
|
||||
assert.equal(classifyComboOutcome(400, "bad request"), "model");
|
||||
});
|
||||
|
||||
// #10501: the classifier's ordering used to have `status === 408 || status >= 499`
|
||||
// checked BEFORE `status >= 500` — since 500 >= 499, that made the `provider`
|
||||
// branch unreachable for every real 5xx status (500/502/503/504), silently
|
||||
// mislabeling every provider outage as a client-side "timeout". These cases
|
||||
// pin the corrected, intentional mapping for the exact statuses call out in
|
||||
// the fix: 499 (client-abort convention), 408 (request timeout), 429 (rate
|
||||
// limit/quota — its own class, not lumped into `model`), and the 5xx family.
|
||||
test("#10501: classifyComboOutcome — 499/408 are timeout, 429 is rate_limit, 5xx is provider (not timeout)", () => {
|
||||
assert.equal(classifyComboOutcome(499, "client closed request"), "timeout");
|
||||
assert.equal(classifyComboOutcome(408, "request timeout"), "timeout");
|
||||
assert.equal(classifyComboOutcome(429, "rate limited"), "rate_limit");
|
||||
assert.equal(classifyComboOutcome(500, "internal server error"), "provider");
|
||||
assert.equal(classifyComboOutcome(502, "bad gateway"), "provider");
|
||||
assert.equal(classifyComboOutcome(503, "upstream unavailable"), "provider");
|
||||
assert.equal(classifyComboOutcome(504, "gateway timeout"), "provider");
|
||||
});
|
||||
|
||||
test("#10314: formatComboOutcomes lists quality and auth reasons SEPARATELY (both visible)", () => {
|
||||
const msg = formatComboOutcomes([
|
||||
{ model: "openai/model-quality", status: 502, error: "response failed quality validation", kind: "quality" },
|
||||
{ model: "openai/proxy-account-b", status: 401, error: "invalid_api_key", kind: "auth" },
|
||||
]);
|
||||
assert.match(msg, /quality validation/);
|
||||
assert.match(msg, /invalid_api_key/);
|
||||
assert.match(msg, /auth/);
|
||||
assert.ok(msg.indexOf("quality validation") < msg.indexOf("invalid_api_key"));
|
||||
});
|
||||
|
||||
test("#10314: redactConnectionLabel masks connection/account identifiers", () => {
|
||||
assert.equal(
|
||||
redactConnectionLabel("openai/proxy-account-b"),
|
||||
"openai/proxy-account-b"
|
||||
);
|
||||
const withUuid = redactConnectionLabel("openai/8a4f0c6e-3b27-4c51-9d88-1f2a3b4c5d6e");
|
||||
assert.equal(withUuid, "openai/conn:8a4f0c6e");
|
||||
const withHex = redactConnectionLabel("openai/0f1e2d3c4b5a69788796170a1b2c3d4e5f607182");
|
||||
assert.equal(withHex, "openai/conn:0f1e2d3c");
|
||||
});
|
||||
|
||||
test("#10314: buildRedactedSummary is redacted and truncates past 5 entries", () => {
|
||||
const s = buildRedactedSummary(
|
||||
Array.from({ length: 6 }, (_, i) => ({ model: `openai/8a4f0c6e-3b27-4c51-9d88-1f2a3b4c5d6e-${i}`, status: 401 + i }))
|
||||
);
|
||||
assert.ok(!s.includes("8a4f0c6e-3b27"), "summary must not leak a full UUID");
|
||||
assert.match(s, /conn:8a4f0c6e/);
|
||||
assert.match(s, /\(\+1\)/);
|
||||
});
|
||||
|
||||
// #10501: identifiers can ride inside the raw upstream ERROR TEXT too (some
|
||||
// openai-compatible proxies echo the connection/account id back in the error
|
||||
// body), not just the model label. formatComboOutcomes must redact BOTH.
|
||||
test("#10501: formatComboOutcomes redacts a UUID embedded in the error TEXT, not just the model label", () => {
|
||||
const msg = formatComboOutcomes([
|
||||
{
|
||||
model: "openai/proxy-account-b",
|
||||
status: 401,
|
||||
error: "invalid key for connection 8a4f0c6e-3b27-4c51-9d88-1f2a3b4c5d6e",
|
||||
kind: "auth",
|
||||
},
|
||||
]);
|
||||
assert.ok(!msg.includes("8a4f0c6e-3b27-4c51-9d88-1f2a3b4c5d6e"), "must not leak the full UUID");
|
||||
assert.match(msg, /conn:8a4f0c6e/, "must redact the UUID inside the error reason text");
|
||||
});
|
||||
|
||||
test("#10501: formatComboOutcomes({redact:false}) intentionally leaves identifiers intact (internal/debug callers only)", () => {
|
||||
const msg = formatComboOutcomes(
|
||||
[
|
||||
{
|
||||
model: "openai/proxy-account-b",
|
||||
status: 401,
|
||||
error: "invalid key for connection 8a4f0c6e-3b27-4c51-9d88-1f2a3b4c5d6e",
|
||||
kind: "auth",
|
||||
},
|
||||
],
|
||||
{ redact: false }
|
||||
);
|
||||
assert.ok(msg.includes("8a4f0c6e-3b27-4c51-9d88-1f2a3b4c5d6e"));
|
||||
});
|
||||
|
||||
// #10501: explicit terminal-status policy for heterogeneous combo target
|
||||
// exhaustion — see comboErrorAggregation.ts::resolveComboTerminalStatus header.
|
||||
test("#10501: resolveComboTerminalStatus preserves 4xx only when EVERY target is a genuine request-invalid (model) failure", () => {
|
||||
assert.equal(
|
||||
resolveComboTerminalStatus(
|
||||
[
|
||||
{ model: "a", status: 400, error: "bad request", kind: "model" },
|
||||
{ model: "b", status: 422, error: "unprocessable", kind: "model" },
|
||||
],
|
||||
500
|
||||
),
|
||||
422,
|
||||
"all-model-class 4xx across every target must be preserved (the request really is invalid)"
|
||||
);
|
||||
});
|
||||
|
||||
test("#10501: resolveComboTerminalStatus preserves a homogeneous non-model status (every target failed the SAME way)", () => {
|
||||
assert.equal(
|
||||
resolveComboTerminalStatus(
|
||||
[
|
||||
{ model: "a", status: 401, error: "invalid_api_key", kind: "auth" },
|
||||
{ model: "b", status: 401, error: "invalid_api_key", kind: "auth" },
|
||||
],
|
||||
500
|
||||
),
|
||||
401,
|
||||
"every target failing with the identical auth reason is still a well-defined single verdict"
|
||||
);
|
||||
});
|
||||
|
||||
test("#10501: resolveComboTerminalStatus normalizes a heterogeneous mix (quality + auth) to 5xx, never a bare lastStatus 401", () => {
|
||||
const status = resolveComboTerminalStatus(
|
||||
[
|
||||
{
|
||||
model: "a",
|
||||
status: 502,
|
||||
error: "response failed quality validation",
|
||||
kind: "quality",
|
||||
},
|
||||
{ model: "b", status: 401, error: "invalid_api_key", kind: "auth" },
|
||||
],
|
||||
401 // lastStatus — the OLD behavior would have surfaced this bare 401
|
||||
);
|
||||
assert.ok(
|
||||
status >= 500,
|
||||
`heterogeneous quality+auth exhaustion must surface an infra/provider 5xx, got ${status}`
|
||||
);
|
||||
});
|
||||
|
||||
test("#10501: resolveComboTerminalStatus maps a heterogeneous mix containing a timeout to 504", () => {
|
||||
const status = resolveComboTerminalStatus(
|
||||
[
|
||||
{ model: "a", status: 408, error: "request timeout", kind: "timeout" },
|
||||
{ model: "b", status: 400, error: "bad request", kind: "model" },
|
||||
],
|
||||
400
|
||||
);
|
||||
assert.equal(status, 504);
|
||||
});
|
||||
|
||||
test("#10501: resolveComboTerminalStatus falls back to the caller's status when there are no structured entries", () => {
|
||||
assert.equal(resolveComboTerminalStatus([], 503), 503);
|
||||
});
|
||||
@@ -458,6 +458,15 @@ test("opted-in combo ref remains a black box and only quota exhaustion advances
|
||||
}
|
||||
});
|
||||
|
||||
// #10501: the child combo's terminal status is now derived from
|
||||
// resolveComboTerminalStatus instead of a bare `lastStatus`. A quality failure
|
||||
// (openai/quality-invalid) mixed with a genuine quota-exhaustion 429
|
||||
// (anthropic/quota) is a heterogeneous, non-"the request itself is invalid"
|
||||
// mix, so it normalizes to a 5xx — the `fallbackOnlyOnQuotaExhaustion` STOP
|
||||
// decision itself is untouched (it is driven by internal quota-observation
|
||||
// tracking, not by re-reading the final HTTP status — asserted below via
|
||||
// `calls`, which must still show the parent stopping instead of falling back
|
||||
// to paid/backup).
|
||||
test("protected parent combo-ref stops after normal child quality rejection then quota exhaustion", async () => {
|
||||
const calls: string[] = [];
|
||||
const child = {
|
||||
@@ -496,8 +505,16 @@ test("protected parent combo-ref stops after normal child quality rejection then
|
||||
: ok(modelStr);
|
||||
},
|
||||
});
|
||||
assert.equal(result.status, 429);
|
||||
assert.deepEqual(calls, ["openai/quality-invalid", "anthropic/quota"]);
|
||||
assert.ok(
|
||||
result.status >= 500,
|
||||
`heterogeneous quality+quota-exhaustion mix must normalize to a 5xx, got ${result.status}`
|
||||
);
|
||||
assert.deepEqual(
|
||||
calls,
|
||||
["openai/quality-invalid", "anthropic/quota"],
|
||||
"the parent must still STOP at the child (not fall back to paid/backup) — the " +
|
||||
"fallbackOnlyOnQuotaExhaustion decision is unaffected by the status-code change"
|
||||
);
|
||||
});
|
||||
|
||||
test("protected parent combo-ref stops when a child has mixed quota and non-quota failures", async () => {
|
||||
|
||||
@@ -897,7 +897,15 @@ test("handleComboChat records per-target metrics separately when the same model
|
||||
assert.equal(metrics.byTarget[secondStep.id].connectionId, "conn-openai-b");
|
||||
});
|
||||
|
||||
test("handleComboChat surfaces the last failing target's status AND error message together, not a cross-target mismatch (#8486)", async () => {
|
||||
// #10314/#10501: superseded the original "last writer wins" contract (a single
|
||||
// `lastError` + raw `[model (status), ...]` suffix). Combo terminal aggregation
|
||||
// now lists every distinct per-target reason separately (comboErrorAggregation.ts
|
||||
// ::formatComboOutcomes) and derives the terminal status from an explicit policy
|
||||
// instead of whichever target happened to fail LAST — a provider 500 mixed with a
|
||||
// rate_limit 429 is a heterogeneous, non-client-fault outcome, so it normalizes to
|
||||
// a 5xx (::resolveComboTerminalStatus), never a bare 429 that would misrepresent
|
||||
// model-a's real 500 as "the client should retry the rate limit".
|
||||
test("handleComboChat surfaces EVERY failing target's reason (never drops one) and normalizes a heterogeneous 500+429 mix to 5xx (#8486/#10314/#10501)", async () => {
|
||||
const result = await handleComboChat({
|
||||
body: {},
|
||||
combo: {
|
||||
@@ -918,11 +926,12 @@ test("handleComboChat surfaces the last failing target's status AND error messag
|
||||
|
||||
const payload = (await result.json()) as any;
|
||||
|
||||
assert.equal(result.status, 429); // #8486: status/message from the SAME (last) failing target
|
||||
// The last error message is preserved and now carries an aggregated
|
||||
// per-model diagnostics suffix (status codes for every target attempted
|
||||
// in this set try), added alongside the global comboTimeoutMs feature.
|
||||
assert.equal(payload.error.message, "fail:model-b [model-a (500), model-b (429)]");
|
||||
assert.ok(
|
||||
result.status >= 500,
|
||||
`heterogeneous provider(500)+rate_limit(429) must normalize to a 5xx status, got ${result.status}`
|
||||
);
|
||||
assert.match(payload.error.message, /model-a.*fail:model-a.*HTTP 500/);
|
||||
assert.match(payload.error.message, /model-b.*fail:model-b.*HTTP 429/);
|
||||
});
|
||||
|
||||
interface ComboErrorPayload {
|
||||
@@ -1679,7 +1688,12 @@ test("handleComboChat round-robin falls through generic 400s when a later model
|
||||
assert.deepEqual(calls, ["model-a", "model-b"]);
|
||||
});
|
||||
|
||||
test("handleComboChat round-robin falls through 400s and returns the LAST target's status+message together, not a cross-target mismatch (#8486)", async () => {
|
||||
// #10314/#10501: same policy update as the priority-strategy test above, applied
|
||||
// to the round-robin twin. model-a's 400 is a genuine request-shape/model-class
|
||||
// error, but model-b's 500 is an infra/provider failure — since NOT every target
|
||||
// failed with a "model" (request-is-invalid) reason, this is a heterogeneous mix
|
||||
// and must normalize to a 5xx, never a bare "trust the last target's status" 500.
|
||||
test("handleComboChat round-robin surfaces EVERY target's reason and normalizes a heterogeneous 400+500 mix to 5xx (#8486/#10314/#10501)", async () => {
|
||||
const calls: any[] = [];
|
||||
|
||||
const result = await handleComboChat({
|
||||
@@ -1717,8 +1731,12 @@ test("handleComboChat round-robin falls through 400s and returns the LAST target
|
||||
});
|
||||
|
||||
const payload = (await result.json()) as any;
|
||||
assert.equal(result.status, 500); // #8486: status/message from the SAME (last) failing target
|
||||
assert.equal(payload.error.message, "rr-final-fail");
|
||||
assert.ok(
|
||||
result.status >= 500,
|
||||
`heterogeneous model(400)+provider(500) mix must normalize to a 5xx status, got ${result.status}`
|
||||
);
|
||||
assert.match(payload.error.message, /model-a.*unsupported message role.*HTTP 400/);
|
||||
assert.match(payload.error.message, /model-b.*rr-final-fail.*HTTP 500/);
|
||||
assert.deepEqual(calls, ["model-a", "model-b"]);
|
||||
});
|
||||
|
||||
|
||||
123
tests/unit/combo-terminal-status-policy-10501.test.ts
Normal file
123
tests/unit/combo-terminal-status-policy-10501.test.ts
Normal file
@@ -0,0 +1,123 @@
|
||||
/**
|
||||
* #10314 / #10501 — integration-level regression for the combo terminal-error
|
||||
* aggregation + status policy. Drives the REAL `handleComboChat` wiring (via
|
||||
* an injected `handleSingleModel`, the same seam `combo-body-specific-400-
|
||||
* stop-4279.test.ts` uses) end-to-end, asserting the actual HTTP `Response`
|
||||
* the client receives — not just the pure `comboErrorAggregation.ts` helpers
|
||||
* in isolation (those are covered by `combo-error-aggregation.test.ts`).
|
||||
*
|
||||
* Scenario: a priority combo where target #1 returns a 200 that FAILS
|
||||
* response-quality validation (empty body — see validateQuality.ts) and
|
||||
* target #2 returns a real 401 (auth). Before #10314/#10501 this surfaced a
|
||||
* bare 401 ("last writer wins") and dropped the quality reason entirely; now
|
||||
* it must list BOTH reasons and surface a 5xx-class infra status instead of
|
||||
* the sibling's 401 (resolveComboTerminalStatus — the request itself was
|
||||
* never proven invalid on every target).
|
||||
*/
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
const TEST_DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), "omniroute-combo-10501-"));
|
||||
process.env.DATA_DIR = TEST_DATA_DIR;
|
||||
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "combo-10501-test-secret";
|
||||
|
||||
const { handleComboChat } = await import("../../open-sse/services/combo.ts");
|
||||
|
||||
const noop = () => {};
|
||||
const log = { info: noop, warn: noop, debug: noop, error: noop };
|
||||
|
||||
function makeCombo(models: string[]) {
|
||||
return {
|
||||
name: "test-combo-10501",
|
||||
strategy: "priority",
|
||||
models: models.map((m) => ({ model: m })),
|
||||
};
|
||||
}
|
||||
|
||||
// Empty body + application/json content-type trips validateResponseQuality's
|
||||
// "empty response body" case for a non-streaming response.
|
||||
function qualityFailingResponse() {
|
||||
return new Response("", { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
}
|
||||
|
||||
function authFailureResponse() {
|
||||
return new Response(JSON.stringify({ error: { message: "invalid_api_key" } }), {
|
||||
status: 401,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
function successResponse() {
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
id: "chatcmpl-1",
|
||||
choices: [{ index: 0, message: { role: "assistant", content: "hi there" }, finish_reason: "stop" }],
|
||||
}),
|
||||
{ status: 200, headers: { "Content-Type": "application/json" } }
|
||||
);
|
||||
}
|
||||
|
||||
test("#10314/#10501: quality failure on target 1 + auth 401 on target 2 → 5xx terminal status, BOTH reasons listed", async () => {
|
||||
const modelsCalled: string[] = [];
|
||||
const handleSingleModel = async (_body: unknown, modelStr: string) => {
|
||||
modelsCalled.push(modelStr);
|
||||
if (modelStr.includes("model-a")) return qualityFailingResponse();
|
||||
return authFailureResponse();
|
||||
};
|
||||
|
||||
const result = await handleComboChat({
|
||||
body: { model: "test", messages: [{ role: "user", content: "hi" }], stream: false },
|
||||
combo: makeCombo(["openai/model-a", "openai/model-b"]),
|
||||
handleSingleModel,
|
||||
log,
|
||||
settings: {},
|
||||
allCombos: [],
|
||||
});
|
||||
|
||||
assert.deepEqual(modelsCalled, ["openai/model-a", "openai/model-b"]);
|
||||
|
||||
// #10501: a heterogeneous quality+auth mix must NOT surface the sibling's
|
||||
// bare 401 (the old `lastStatus` behavior) — it is an infra/provider-class
|
||||
// outcome (neither target proved the CLIENT's request itself was invalid).
|
||||
assert.ok(
|
||||
result.status >= 500,
|
||||
`expected a 5xx terminal status for a heterogeneous quality+auth mix, got ${result.status}`
|
||||
);
|
||||
assert.notEqual(result.status, 401, "must not regress to surfacing the sibling target's bare 401");
|
||||
|
||||
const body = (await result.json()) as { error?: { message?: string } };
|
||||
const message = body.error?.message ?? "";
|
||||
// #10314: both distinct reasons must be listed — neither silently dropped.
|
||||
assert.match(message, /quality/i, "quality-validation reason must be present in the message");
|
||||
assert.match(message, /invalid_api_key|auth/i, "auth reason must be present in the message");
|
||||
});
|
||||
|
||||
test("#10314: success on target 2 after a quality failure on target 1 returns the real success response", async () => {
|
||||
const modelsCalled: string[] = [];
|
||||
const handleSingleModel = async (_body: unknown, modelStr: string) => {
|
||||
modelsCalled.push(modelStr);
|
||||
if (modelStr.includes("model-a")) return qualityFailingResponse();
|
||||
return successResponse();
|
||||
};
|
||||
|
||||
const result = await handleComboChat({
|
||||
body: { model: "test", messages: [{ role: "user", content: "hi" }], stream: false },
|
||||
combo: makeCombo(["openai/model-a", "openai/model-b"]),
|
||||
handleSingleModel,
|
||||
log,
|
||||
settings: {},
|
||||
allCombos: [],
|
||||
});
|
||||
|
||||
assert.deepEqual(
|
||||
modelsCalled,
|
||||
["openai/model-a", "openai/model-b"],
|
||||
"must fail over from the quality-rejected target 1 to target 2"
|
||||
);
|
||||
assert.equal(result.status, 200, "combo must return the successful target's response");
|
||||
const body = (await result.json()) as { choices?: Array<{ message?: { content?: string } }> };
|
||||
assert.equal(body.choices?.[0]?.message?.content, "hi there");
|
||||
});
|
||||
Reference in New Issue
Block a user