mirror of
https://github.com/diegosouzapw/OmniRoute.git
synced 2026-09-02 12:52:17 +03:00
Compare commits
2 Commits
dependabot
...
fix/11500-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2ce428e926 | ||
|
|
1fcd5e4d1f |
1
changelog.d/fixes/11500-arena-elo-log-dedup.md
Normal file
1
changelog.d/fixes/11500-arena-elo-log-dedup.md
Normal file
@@ -0,0 +1 @@
|
||||
- fix(db): rate-limit repeated Arena ELO leaderboard fetch-failure warnings instead of logging one per sync attempt (#11500)
|
||||
@@ -212,6 +212,25 @@ export function normalizeModelName(rawName: string): string {
|
||||
|
||||
// ─── Core: Fetch ─────────────────────────────────────────
|
||||
|
||||
/**
|
||||
* How many consecutive per-category fetch failures to let through to
|
||||
* `console.warn` before going quiet again. Timeouts against the Arena API
|
||||
* repeat every sync cycle (see `startPeriodicSync()`), so logging every
|
||||
* single one turns into log spam within a few hours (#11500). Mirrors the
|
||||
* once-per-label dedup pattern used by `warnEmptyAutoPoolOnce()`
|
||||
* (`open-sse/services/autoCombo/virtualFactory.ts`), except here the streak
|
||||
* resets on the next successful fetch so a genuinely new outage warns again.
|
||||
*/
|
||||
const ARENA_ELO_FETCH_WARN_STREAK_INTERVAL = 10;
|
||||
|
||||
/** Consecutive fetch-failure count per leaderboard category, since the last success. */
|
||||
const categoryFetchFailureStreak = new Map<string, number>();
|
||||
|
||||
/** Test-only: reset the per-category fetch-failure streak dedup state. */
|
||||
export function resetArenaEloFetchFailureStreaksForTests(): void {
|
||||
categoryFetchFailureStreak.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch leaderboards from the Arena AI API for all configured categories.
|
||||
*
|
||||
@@ -244,9 +263,18 @@ export async function fetchArenaLeaderboards(): Promise<ArenaLeaderboardMap> {
|
||||
`Arena API returned invalid JSON for "${category}" (${text.slice(0, 100)}...)`
|
||||
);
|
||||
}
|
||||
// Recovered: let the next failure streak warn from scratch again.
|
||||
categoryFetchFailureStreak.delete(category);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
console.warn(`[ARENA_ELO_SYNC] Failed to fetch "${category}" leaderboard: ${message}`);
|
||||
const streak = (categoryFetchFailureStreak.get(category) ?? 0) + 1;
|
||||
categoryFetchFailureStreak.set(category, streak);
|
||||
if (streak === 1 || streak % ARENA_ELO_FETCH_WARN_STREAK_INTERVAL === 0) {
|
||||
console.warn(
|
||||
`[ARENA_ELO_SYNC] Failed to fetch "${category}" leaderboard: ${message}` +
|
||||
(streak > 1 ? ` (${streak} consecutive failures; further warnings rate-limited)` : "")
|
||||
);
|
||||
}
|
||||
errors.push(message);
|
||||
}
|
||||
});
|
||||
|
||||
154
tests/unit/arena-elo-fetch-failure-log-dedup.test.ts
Normal file
154
tests/unit/arena-elo-fetch-failure-log-dedup.test.ts
Normal file
@@ -0,0 +1,154 @@
|
||||
/**
|
||||
* Unit tests for the fetchArenaLeaderboards() consecutive-failure log-dedup
|
||||
* added in src/lib/arenaEloSync.ts (#11500 sub-issue 3).
|
||||
*
|
||||
* Split out of arena-elo-sync.test.ts (#11500) — that file needs a full
|
||||
* SQLite/DB fixture the sync path requires, which this fetch-only surface
|
||||
* does not; keeping these two tests self-contained keeps the split honest
|
||||
* and avoids pushing arena-elo-sync.test.ts's own (pre-existing) size past
|
||||
* the file-size gate's new-test-file cap.
|
||||
*/
|
||||
|
||||
import { describe, it, afterEach } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
const { fetchArenaLeaderboards, resetArenaEloFetchFailureStreaksForTests } = await import(
|
||||
"../../src/lib/arenaEloSync.ts"
|
||||
);
|
||||
import type { ArenaLeaderboardData, ArenaModelEntry } from "../../src/lib/arenaEloSync.ts";
|
||||
|
||||
const originalFetch = globalThis.fetch;
|
||||
|
||||
function mockFetch(impl: (url: string, opts?: RequestInit) => Promise<Response>): void {
|
||||
globalThis.fetch = impl as typeof fetch;
|
||||
}
|
||||
|
||||
function restoreFetch(): void {
|
||||
globalThis.fetch = originalFetch;
|
||||
}
|
||||
|
||||
function jsonResponse(data: unknown, status = 200): Response {
|
||||
return new Response(JSON.stringify(data), {
|
||||
status,
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
}
|
||||
|
||||
function makeModelEntry(overrides: Partial<ArenaModelEntry> = {}): ArenaModelEntry {
|
||||
return {
|
||||
rank: 1,
|
||||
model: "anthropic/claude-sonnet",
|
||||
vendor: "Anthropic",
|
||||
score: 1350,
|
||||
ci: 10,
|
||||
votes: 5000,
|
||||
license: "proprietary",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
function makeLeaderboardData(
|
||||
models: ArenaModelEntry[] = [],
|
||||
category = "text"
|
||||
): ArenaLeaderboardData {
|
||||
return {
|
||||
meta: { leaderboard: category, model_count: models.length },
|
||||
models,
|
||||
};
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
restoreFetch();
|
||||
resetArenaEloFetchFailureStreaksForTests();
|
||||
});
|
||||
|
||||
describe("fetchArenaLeaderboards() — consecutive-failure log dedup (#11500)", () => {
|
||||
it("rate-limits the per-category fetch-failure warning across many consecutive timeouts", async () => {
|
||||
resetArenaEloFetchFailureStreaksForTests();
|
||||
const originalWarn = console.warn;
|
||||
const warnCalls: string[] = [];
|
||||
console.warn = ((...args: unknown[]) => {
|
||||
warnCalls.push(args.map(String).join(" "));
|
||||
}) as typeof console.warn;
|
||||
|
||||
try {
|
||||
mockFetch(async () => {
|
||||
throw new Error("The operation was aborted due to timeout");
|
||||
});
|
||||
|
||||
const ATTEMPTS = 25;
|
||||
for (let i = 0; i < ATTEMPTS; i++) {
|
||||
await assert.rejects(() => fetchArenaLeaderboards());
|
||||
}
|
||||
|
||||
const fetchFailureWarnings = warnCalls.filter((line) =>
|
||||
line.includes('Failed to fetch "text" leaderboard')
|
||||
);
|
||||
|
||||
// One category × 25 consecutive-failure attempts would be 25 raw warns —
|
||||
// the rate limiter must keep the emitted count far below that.
|
||||
assert.ok(
|
||||
fetchFailureWarnings.length < ATTEMPTS,
|
||||
`expected fewer than ${ATTEMPTS} warnings, got ${fetchFailureWarnings.length}`
|
||||
);
|
||||
assert.ok(
|
||||
fetchFailureWarnings.length <= 5,
|
||||
`expected the streak-gated warning to stay tightly bounded, got ${fetchFailureWarnings.length}`
|
||||
);
|
||||
assert.ok(fetchFailureWarnings.length >= 1, "the first failure must still be logged");
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
resetArenaEloFetchFailureStreaksForTests();
|
||||
}
|
||||
});
|
||||
|
||||
it("resets the fetch-failure streak after a successful fetch so the next outage warns again", async () => {
|
||||
resetArenaEloFetchFailureStreaksForTests();
|
||||
const originalWarn = console.warn;
|
||||
const warnCalls: string[] = [];
|
||||
console.warn = ((...args: unknown[]) => {
|
||||
warnCalls.push(args.map(String).join(" "));
|
||||
}) as typeof console.warn;
|
||||
|
||||
try {
|
||||
mockFetch(async () => {
|
||||
throw new Error("timeout");
|
||||
});
|
||||
await assert.rejects(() => fetchArenaLeaderboards());
|
||||
await assert.rejects(() => fetchArenaLeaderboards());
|
||||
|
||||
const textData = makeLeaderboardData(
|
||||
[makeModelEntry({ model: "recovered-model", score: 1200, votes: 5000, rank: 1 })],
|
||||
"text"
|
||||
);
|
||||
const codeData = makeLeaderboardData(
|
||||
[makeModelEntry({ model: "recovered-code", score: 1200, votes: 5000, rank: 1 })],
|
||||
"code"
|
||||
);
|
||||
mockFetch(async (url: string) => {
|
||||
if (url.includes("name=text")) return jsonResponse(textData);
|
||||
if (url.includes("name=code")) return jsonResponse(codeData);
|
||||
return new Response("Not found", { status: 404 });
|
||||
});
|
||||
await fetchArenaLeaderboards();
|
||||
|
||||
mockFetch(async () => {
|
||||
throw new Error("timeout again");
|
||||
});
|
||||
warnCalls.length = 0;
|
||||
await assert.rejects(() => fetchArenaLeaderboards());
|
||||
|
||||
const fetchFailureWarnings = warnCalls.filter((line) =>
|
||||
line.includes('Failed to fetch "text" leaderboard')
|
||||
);
|
||||
assert.strictEqual(
|
||||
fetchFailureWarnings.length,
|
||||
1,
|
||||
"streak reset by the success must re-arm the first-failure warning"
|
||||
);
|
||||
} finally {
|
||||
console.warn = originalWarn;
|
||||
resetArenaEloFetchFailureStreaksForTests();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user