Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/routing/analytics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* sample for the full history.
*/

import type { PersistedUsageEntry, PersistedUsageAttempt } from "../usage/log";
import type { PersistedUsageEntry } from "../usage/log";
import { estimateRequestCost, serviceTierContext } from "../usage/cost";
import { openRequestHistoryIndex, requestHistoryDb } from "./history/indexer";

Expand Down Expand Up @@ -144,14 +144,15 @@ function parseEntry(rowJson: string): PersistedUsageEntry | null {
}
}

function attemptsOf(entry: PersistedUsageEntry | null): PersistedUsageAttempt[] | undefined {
return entry?.attempts;
}

function cooldownTriggering(entry: PersistedUsageEntry | null, status: number): boolean {
if (status === 429) return true;
const attempts = attemptsOf(entry) ?? [];
return attempts.some(attempt => attempt.recoveryKinds.some(kind => COOLDOWN_RECOVERY_KINDS.has(kind)));
if (!Array.isArray(entry?.attempts)) return false;
return entry.attempts.some((attempt: unknown) => {
if (!attempt || typeof attempt !== "object") return false;
const recoveryKinds = (attempt as { recoveryKinds?: unknown }).recoveryKinds;
return Array.isArray(recoveryKinds)
&& recoveryKinds.some(kind => typeof kind === "string" && COOLDOWN_RECOVERY_KINDS.has(kind));
});
}

function successCostUsd(
Expand Down
30 changes: 29 additions & 1 deletion tests/routing-analytics.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
import { mkdtempSync, rmSync } from "node:fs";
import { appendFileSync, mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { handleManagementAPI } from "../src/server/management-api";
import { ManagementRequest } from "./helpers/management-auth";
import {
appendUsageEntry,
resetUsageReadCacheForTests,
usageLogPath,
type PersistedUsageEntry,
} from "../src/usage/log";
import { closeRequestHistoryIndex } from "../src/routing/history/indexer";
Expand Down Expand Up @@ -224,6 +225,33 @@ describe("routing analytics (RI-03)", () => {
expect(result.cooldownTriggeringFailures).toBe(1);
});

test("ignores malformed attempts while preserving explicit 429 classification", async () => {
appendUsageEntry(entry("baseline", { timestamp: 1, status: 200, durationMs: 10 }));
const historicalRows = [
{
...entry("missing-recovery-kinds", { timestamp: 2, status: 503, durationMs: 20 }),
attempts: [{ ordinal: 1 }],
},
{
...entry("malformed-attempts", { timestamp: 3, status: 503, durationMs: 30 }),
attempts: { recoveryKinds: ["rate-limit-429"] },
},
{
...entry("malformed-recovery-kinds", { timestamp: 4, status: 503, durationMs: 40 }),
attempts: [null, { recoveryKinds: "rate-limit-429" }, { recoveryKinds: [null, 42, "unknown"] }],
},
{
...entry("malformed-429", { timestamp: 5, status: 429, durationMs: 50 }),
attempts: { recoveryKinds: ["unknown"] },
},
];
appendFileSync(usageLogPath(), `${historicalRows.map(row => JSON.stringify(row)).join("\n")}\n`);

const result = await computeRoutingAnalytics({});
expect(result.totalRequests).toBe(5);
expect(result.cooldownTriggeringFailures).toBe(1);
});

test("routing analytics API returns 400 for invalid from/to/limit", async () => {
const cases = [
{ query: "from=abc", code: "invalid_from" },
Expand Down
Loading