Skip to content
Merged
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
9 changes: 1 addition & 8 deletions client/src/lib/routing-explanations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,11 @@ describe("ROUTING_EXPLANATIONS", () => {

it("documents routing score axes", () => {
expect(ROUTING_EXPLANATIONS.score).toContain("higher score routes first");
expect(ROUTING_EXPLANATIONS.reliability).toContain("successful requests");
expect(ROUTING_EXPLANATIONS.reliability).toContain("healthy");
expect(ROUTING_EXPLANATIONS.speed).toContain("tokens per second");
expect(ROUTING_EXPLANATIONS.intelligence).toContain("benchmark score");
});

it("documents heartbeat-toggled reliability as key-health share", () => {
expect(ROUTING_EXPLANATIONS.reliabilityWithHeartbeat).toContain(
"heartbeat",
);
expect(ROUTING_EXPLANATIONS.reliabilityWithHeartbeat).toContain("keys");
});

it("documents benchmark chart semantics", () => {
expect(ROUTING_EXPLANATIONS.benchmarkChart).toContain(
"benchmarked request latency",
Expand Down
2 changes: 0 additions & 2 deletions client/src/lib/routing-explanations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ export const ROUTING_EXPLANATIONS = {
score:
"Final weighted score across reliability, speed, intelligence, latency, boost, and penalties. A higher score routes first for score-based strategies.",
reliability:
"Reliability comes from successful requests versus errors, rate limits, cooldowns, and degradation state. More clean traffic raises this axis.",
reliabilityWithHeartbeat:
"Reliability is the share of API keys the heartbeat currently marks healthy for this model — cheap health-check pings cycled per key, independent of real traffic. A model with no live errors can still score low if its keys are down, and vice-versa.",
speed:
"Speed is output plus reasoning tokens per second from observed traffic. It answers how quickly useful tokens arrive after the first byte.",
Expand Down
4 changes: 2 additions & 2 deletions server/src/__tests__/integration/key-affinity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import { type RouteOptions, routeRequest } from "../../services/router.js";

// Mock heartbeat module to simplify testing
vi.mock("../../services/heartbeat.js", () => ({
heartbeatReliability: vi.fn(() => 100), // 100% healthy when not pre-configured
isKeyHealthy: vi.fn(() => true), // All keys healthy by default
isHeartbeatEnabled: vi.fn(() => false), // Heartbeat disabled by default
markKeyUnhealthy: vi.fn(),
recordActivity: vi.fn(),
getAllKeyHealth: vi.fn(() => new Map()),
getKeyStats: vi.fn(),
getKeyHealth: vi.fn(),
}));

Expand Down
76 changes: 0 additions & 76 deletions server/src/__tests__/services/heartbeat.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,15 +336,6 @@ describe("Provider Health Heartbeat", () => {
expect(isKeyHealthy(999)).toBe(false);
});

it("isKeyHealthy returns true for a cold key when heartbeat is disabled (backward compat)", () => {
// Disable heartbeat
setSetting("heartbeat_enabled", "false");
resetHeartbeatConfig();

// When heartbeat is off, cold keys are assumed healthy for backward compat
expect(isKeyHealthy(999)).toBe(true);
});

it("isKeyHealthy returns true after a successful warmup ping", async () => {
const { modelDbId, keyId } = setupProvider();

Expand Down Expand Up @@ -608,13 +599,6 @@ describe("Provider Health Heartbeat", () => {
// ── Lifecycle ──────────────────────────────────────────────────────────

describe("Lifecycle (start/stop)", () => {
it("startHeartbeat is a no-op when disabled", () => {
setSetting("heartbeat_enabled", "false");
startHeartbeat();
vi.advanceTimersByTime(30 * 60 * 1000);
expect(publishedEvents.length).toBe(0);
});

it("stopHeartbeat is safe to call even if never started", () => {
expect(() => stopHeartbeat()).not.toThrow();
});
Expand Down Expand Up @@ -754,14 +738,6 @@ describe("Provider Health Heartbeat", () => {
// ── resetHeartbeatConfig ─────────────────────────────────────────────

describe("pokeKey", () => {
it("returns true when heartbeat is disabled (backward compat)", async () => {
setSetting("heartbeat_enabled", "false");
resetHeartbeatConfig();

const result = await pokeKey(1);
expect(result).toBe(true);
});

it("pings a specific key and returns true when healthy", async () => {
const { keyId } = setupProvider();

Expand Down Expand Up @@ -1186,58 +1162,6 @@ describe("Provider Health Heartbeat", () => {
getPendingRechecks().get(healthKey(keyId, "test-model"))!.attempt,
).toBe(1);
});

it("disabled heartbeat → no recheck scheduled", async () => {
// Need a fresh module with heartbeat disabled
vi.resetModules();
vi.doMock("../../providers/index.js", async (importOriginal) => {
const actual = (await importOriginal()) as any;
return {
...actual,
buildProviderFor: () => ({ name: "fake", chatCompletion: vi.fn() }),
};
});
vi.doMock("../../services/events.js", () => ({
publish: vi.fn(),
}));
vi.doMock("../../lib/crypto.js", async (importOriginal) => {
const actual = (await importOriginal()) as any;
return { ...actual, decrypt: vi.fn(() => "mocked-api-key") };
});

process.env.ENCRYPTION_KEY = "0".repeat(64);
const freshDb = await import("../../db/index.js");
const freshHb = await import("../../services/heartbeat.js");
const freshDegr = await import("../../services/degradation.js");

freshDb.initDb(":memory:");
freshDegr.initDegradation();
freshDb.setSetting("heartbeat_enabled", "false");
freshHb.resetHeartbeatConfig();

// Setup provider in fresh DB
const db = freshDb.getDb();
db.prepare(
"INSERT INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, enabled) VALUES ('testprov', 'test-model', 'Test', 1, 1, 1)",
).run();
const id = (
db
.prepare("SELECT id FROM models WHERE platform = 'testprov'")
.get() as any
).id;
db.prepare(
"INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, 1, 1)",
).run(id);
db.prepare(
"INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, status, enabled) VALUES ('testprov', 'Key 1', 'enc', 'iv', 'tag', 'healthy', 1)",
).run();
const keyRow = db
.prepare("SELECT id FROM api_keys WHERE platform = ? AND enabled = 1")
.get("testprov") as any;

freshHb.markKeyUnhealthy(keyRow.id, "test-model", "429 rate limit");
expect(freshHb.getPendingRechecks().size).toBe(0);
});
});

// ── Recheck Execution ──────────────────────────────────────────────────
Expand Down
1 change: 0 additions & 1 deletion server/src/__tests__/services/racing-mode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ vi.mock("../../services/ratelimit.js", () => ({
}));

vi.mock("../../services/heartbeat.js", () => ({
isHeartbeatEnabled: () => false,
markKeyTrafficHealthy: () => {},
recordActivity: mocks.recordActivity,
}));
Expand Down
161 changes: 0 additions & 161 deletions server/src/__tests__/services/router-bandit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ describe("bandit router", () => {
getDb().exec(
"DELETE FROM fallback_config; DELETE FROM api_keys; DELETE FROM models; DELETE FROM requests;",
);
setSetting("heartbeat_enabled", "false");
resetHeartbeatConfig();
vi.clearAllMocks();
(crypto.decrypt as any).mockReturnValue("mocked-api-key");
Expand Down Expand Up @@ -221,34 +220,6 @@ describe("bandit router", () => {
});
});

it("applies configured coding-agent model priors before live observations", () => {
addModel({
platform: "nvidia",
modelId: "z-ai/glm-5.2",
name: "GLM-5.2",
intelligenceRank: 1,
sizeLabel: "Frontier",
budget: "~10M",
priority: 1,
});
addModel({
platform: "example",
modelId: "unlisted",
name: "Unlisted",
intelligenceRank: 1,
sizeLabel: "Frontier",
budget: "~10M",
priority: 2,
});

const scores = getRoutingScores().scores;
const configured = scores.find((s) => s.modelId === "z-ai/glm-5.2");
const unlisted = scores.find((s) => s.modelId === "unlisted");

expect(configured?.reliability).toBeCloseTo(0.3995, 4);
expect(unlisted?.reliability).toBeCloseTo(0.5, 4);
});

it("attaches aggressive per-arm timeouts to volatile StepFun routes", () => {
setRoutingStrategy("priority");
addModel({
Expand Down Expand Up @@ -289,33 +260,6 @@ describe("bandit router", () => {
expect(counts["a"]).toBe(50); // priority 1 always wins regardless of intelligence
});

it("balanced strategy favors the more reliable model", () => {
addModel({
platform: "google",
modelId: "good",
name: "Good",
intelligenceRank: 3,
sizeLabel: "Large",
budget: "~50M",
priority: 1,
});
addModel({
platform: "groq",
modelId: "flaky",
name: "Flaky",
intelligenceRank: 3,
sizeLabel: "Large",
budget: "~50M",
priority: 2,
});
addHistory("google", "good", { successes: 60, failures: 1 });
addHistory("groq", "flaky", { successes: 5, failures: 40 });
setRoutingStrategy("balanced");
refreshStatsCache(getDb(), true);
const counts = pickCounts(300);
expect(counts["good"] ?? 0).toBeGreaterThan((counts["flaky"] ?? 0) * 3);
});

it("uses heartbeat health proportion for flat-chain reliability when heartbeat is enabled", () => {
addModel({
platform: "healthy-provider",
Expand Down Expand Up @@ -344,7 +288,6 @@ describe("bandit router", () => {
failures: 0,
});

setSetting("heartbeat_enabled", "true");
resetHeartbeatConfig();
setHeartbeatState("healthy-provider", "historically-bad", true);
setHeartbeatState("sick-provider", "historically-good", false);
Expand All @@ -358,75 +301,6 @@ describe("bandit router", () => {
expect(byModel.get("historically-good")!.reliability).toBe(0);
});

it("falls back to historical reliability when heartbeat is disabled", () => {
addModel({
platform: "healthy-provider",
modelId: "historically-bad",
name: "Historically Bad",
intelligenceRank: 3,
sizeLabel: "Large",
budget: "~50M",
priority: 1,
});
addModel({
platform: "sick-provider",
modelId: "historically-good",
name: "Historically Good",
intelligenceRank: 3,
sizeLabel: "Large",
budget: "~50M",
priority: 2,
});
addHistory("healthy-provider", "historically-bad", {
successes: 1,
failures: 40,
});
addHistory("sick-provider", "historically-good", {
successes: 60,
failures: 0,
});

setSetting("heartbeat_enabled", "false");
resetHeartbeatConfig();
setHeartbeatState("healthy-provider", "historically-bad", true);
setHeartbeatState("sick-provider", "historically-good", false);
setRoutingStrategy("balanced");
refreshStatsCache(getDb(), true);

const byModel = new Map(
getRoutingScores().scores.map((score) => [score.modelId, score]),
);
expect(byModel.get("historically-good")!.reliability).toBeGreaterThan(
byModel.get("historically-bad")!.reliability,
);
});

it("explores unseen models — both get picked at least once", () => {
addModel({
platform: "google",
modelId: "x",
name: "X",
intelligenceRank: 3,
sizeLabel: "Large",
budget: "~50M",
priority: 1,
});
addModel({
platform: "groq",
modelId: "y",
name: "Y",
intelligenceRank: 3,
sizeLabel: "Large",
budget: "~50M",
priority: 2,
});
setRoutingStrategy("balanced");
refreshStatsCache(getDb(), true);
const counts = pickCounts(200);
expect(counts["x"] ?? 0).toBeGreaterThan(0);
expect(counts["y"] ?? 0).toBeGreaterThan(0);
});

it("smartest vs fastest flips which model wins, at equal reliability", () => {
// Smart: frontier tier, slow (high speed_rank = slow default). Fast: small tier, fast (low speed_rank).
// Both get 60+2=62 requests, well above REAL_SPEED_CONFIDENCE_THRESHOLD (50),
Expand Down Expand Up @@ -511,7 +385,6 @@ describe("bandit router", () => {
addBenchmarkSamples(nearModelId, "groq", "near-slow-work", 520, 60);
addPingSamples("groq", "near-slow-work", 20, 60);

setSetting("heartbeat_enabled", "true");
resetHeartbeatConfig();
setHeartbeatState("google", "far-fast-work", true);
setHeartbeatState("groq", "near-slow-work", true);
Expand Down Expand Up @@ -689,38 +562,4 @@ describe("bandit router", () => {
latency: 0.1,
});
});

it("getRoutingScores returns a per-axis breakdown ranked by score", () => {
addModel({
platform: "google",
modelId: "m1",
name: "M1",
intelligenceRank: 1,
sizeLabel: "Frontier",
budget: "~50M",
priority: 1,
});
addHistory("google", "m1", {
successes: 30,
failures: 0,
outTokens: 500,
latencyMs: 1000,
ttfbMs: 200,
});
setRoutingStrategy("balanced");
refreshStatsCache(getDb(), true);
const { strategy, weights, scores } = getRoutingScores();
expect(strategy).toBe("balanced");
expect(weights).toEqual({
reliability: 0.4,
speed: 0.2,
intelligence: 0.2,
latency: 0.2,
});
expect(scores).toHaveLength(1);
expect(scores[0]).toMatchObject({ modelId: "m1", enabled: true });
expect(scores[0].reliability).toBeGreaterThan(0.9);
expect(scores[0].score).toBeGreaterThan(0);
expect(scores[0].score).toBeLessThanOrEqual(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ describe("routeRequest(options.strategy) override", () => {
getDb().exec(
"DELETE FROM fallback_config; DELETE FROM api_keys; DELETE FROM models; DELETE FROM requests;",
);
setSetting("heartbeat_enabled", "false");
resetHeartbeatConfig();
vi.clearAllMocks();

// Deterministic disagreement across strategies:
Expand Down
Loading
Loading