Never stop coding. Free MIT AI gateway: one endpoint, 375 providers (150+ free), 1,400+ models Kimi, Claude, GPT, Gemini, GLM, DeepSeek, MiniMax. Works with Claude Code, Codex, Cursor, OpenCode, Cline & Copilot. Quota-aware auto-fallback, RTK+Caveman compression saves 15-95% tokens, MCP/A2A, Desktop/PWA. Built by 900+ contributors
TypeScript a2aai-agentsai-gatewayanthropicclaudeclaude-codeclinecodexcopilotcursordeepseekfree-aigeminikimillm-gatewaymcpopenaiopenai-proxyqwentoken-saver
/**
* tests/integration/combo-live/cost-and-fusion.live.test.ts
*
* Gated live-smoke tests for cost-optimized and fusion combo strategies.
* Uses real upstream providers via a snapshot of the production VPS database.
*
* Gate: RUN_COMBO_LIVE=1 to enable. Without it, all tests are skipped.
*
* Cost discipline: max_tokens=16, temperature=0, ONE call per test, panel ≤3.
*
* Test 1 — cost-optimized: proves the cost sorter reorders by real catalog
* pricing. Lists the PRICIER provider first in models[], then asserts the
* CHEAPER one served. Skips if pricing is not distinguishable at runtime.
*
* Test 2 — fusion: panel fans out to ≥2 providers in parallel, judge
* synthesizes one final answer. Asserts 200 + non-empty synthesis. Skips
* if <2 healthy providers are available.
*/
import { test, before, after, beforeEach, afterEach } from "node:test";
import assert from "node:assert/strict";
import { createLiveHarness, type LiveConnection } from "./_liveHarness.ts";
// ---------------------------------------------------------------------------
// Module-level harness — initialized once, shared across all tests.
// ---------------------------------------------------------------------------
const h = await createLiveHarness("combo-live-cost-fusion");
// ---------------------------------------------------------------------------
// Unique nonce
// ---------------------------------------------------------------------------
let _nonceCounter = 0;
function uniqueNonce(testName: string): string {
return `${++_nonceCounter}:${testName}`;
}
// ---------------------------------------------------------------------------
// Warmup helpers (identical pattern to ordered.live.test.ts)
// ---------------------------------------------------------------------------
const WARMUP_TIMEOUT_MS = 10_000;
// Provider → cheap default model for warmup (shared with harness defaults).
const PROVIDER_DEFAULT_MODELS: Record<string, string> = {
claude: "claude-3-5-haiku-20241022",
glm: "glm-4-flash",
minimax: "minimax-text-01",
"kimi-coding-apikey": "moonshot-v1-8k",
"ollama-cloud": "llama3.2:3b",
"opencode-go": "gpt-4o-mini",
gemini: "gemini-2.0-flash-lite",
deepseek: "deepseek-chat",
groq: "llama-3.1-8b-instant",
cerebras: "llama-3.1-8b",
openrouter: "openai/gpt-4o-mini",
together: "meta-llama/Llama-3-8b-chat-hf",
};
async function isHealthy(conn: LiveConnection): Promise<boolean> {
if (!h.LIVE_ENABLED) return false;
const model = conn.model ?? PROVIDER_DEFAULT_MODELS[conn.provider] ?? `${conn.provider}/default`;
const directModel = `${conn.provider}/${model}`;
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), WARMUP_TIMEOUT_MS);
try {
const resp = await (h as any).handleChat(
(h as any).buildRequest({
body: (h as any).liveBody(directModel, {
messages: [{ role: "user", content: `ping warmup:${conn.provider}` }],
}),
signal: controller.signal,
})
);
clearTimeout(timer);
if (resp.status !== 200) return false;
const text = await (h as any).readCompletionText(resp);
return text.length > 0;
} catch {
clearTimeout(timer);
return false;
}
}
/**
* Pick up to `n` confirmed-healthy connections, preferring fast/cheap providers.
*/
async function pickConfirmedHealthy(n: number, preferred?: string[]): Promise<LiveConnection[]> {
if (!h.LIVE_ENABLED) return [];
const conns = await (h as any).listLiveConnections();
const PREFERRED_ORDER = preferred ?? [
"groq",
"cerebras",
"opencode-go",
"deepseek",
"gemini",
"together",
"openrouter",
];
const sorted = [...conns].sort((a: LiveConnection, b: LiveConnection) => {
const ai = PREFERRED_ORDER.indexOf(a.provider);
const bi = PREFERRED_ORDER.indexOf(b.provider);
return (ai === -1 ? 999 : ai) - (bi === -1 ? 999 : bi);
});
const healthy: LiveConnection[] = [];
for (const conn of sorted) {
if (healthy.length >= n) break;
if (await isHealthy(conn)) healthy.push(conn);
}
return healthy;
}
/**
* Look up input cost ($/1M tokens) for a provider+model pair via the merged
* pricing layers in the live DB snapshot. Returns Infinity if not found.
*/
async function resolveInputCost(provider: string, model: string): Promise<number> {
try {
const { getPricingForModel } = await import("@/lib/db/settings");
const pricing = await getPricingForModel(provider, model);
const cost = Number((pricing as any)?.input);
return Number.isFinite(cost) ? cost : Infinity;
} catch {
return Infinity;
}
}
/**
* Read the raw `model` field from a response body clone.
*/
async function readResponseModel(response: Response): Promise<string | undefined> {
try {
const json = await response.clone().json();
return typeof json?.model === "string" ? json.model : undefined;
} catch {
return undefined;
}
}
// ---------------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------------
beforeEach(() => {
if (!h.LIVE_ENABLED) return;
(h as any).BaseExecutor.RETRY_CONFIG.delayMs = 0;
(h as any).resetCachesForTest();
});
afterEach(() => {
if (!h.LIVE_ENABLED) return;
(h as any).BaseExecutor.RETRY_CONFIG.delayMs = (h as any).originalRetryDelayMs;
});
after(async () => {
if (h.LIVE_ENABLED) {
await (h as any).cleanup();
}
});
// ---------------------------------------------------------------------------
// Test 1: cost-optimized — cheaper real provider served first
// ---------------------------------------------------------------------------
test(
"live cost-optimized — cheaper real provider served first",
{
skip: !h.LIVE_ENABLED && "RUN_COMBO_LIVE!=1",
},
async () => {
if (!h.LIVE_ENABLED) return;
// We prefer groq+deepseek: distinct model names (easy to confirm served),
// and deepseek has a known non-zero price ($0.28/M) while groq free models
// land at $0 in the registry. The cost sorter puts $0 before $0.28, so if
// we list deepseek FIRST and groq SECOND, a correct sort gives us groq first.
//
// We fall back to cerebras if groq is unhealthy.
const candidates = await pickConfirmedHealthy(2, [
"groq",
"cerebras",
"deepseek",
"opencode-go",
]);
// We need exactly 2 healthy connections with DISTINCT providers.
const seen = new Set<string>();
const uniqueCandidates = candidates.filter((c: LiveConnection) => {
if (seen.has(c.provider)) return false;
seen.add(c.provider);
return true;
});
if (uniqueCandidates.length < 2) {
console.log(
`[cost-optimized skip] Only ${uniqueCandidates.length} distinct healthy provider(s) — need ≥2. Skipping.`
);
return;
}
const [a, b] = uniqueCandidates;
const aModel = a.model ?? PROVIDER_DEFAULT_MODELS[a.provider] ?? `${a.provider}/default`;
const bModel = b.model ?? PROVIDER_DEFAULT_MODELS[b.provider] ?? `${b.provider}/default`;
// Resolve pricing from the live DB snapshot.
const aCost = await resolveInputCost(a.provider, aModel);
const bCost = await resolveInputCost(b.provider, bModel);
console.log(
`[cost-optimized] Candidates: ${a.provider}/${aModel} ($${aCost}/M) vs ${b.provider}/${bModel} ($${bCost}/M)`
);
// If both are Infinity (no pricing data for either), we can't prove cost ordering.
if (!Number.isFinite(aCost) && !Number.isFinite(bCost)) {
console.log(
`[cost-optimized skip] Neither ${a.provider} nor ${b.provider} has resolvable catalog ` +
`pricing in the live DB snapshot — cannot prove cost ordering. Skipping.`
);
return;
}
// If both are equal (including both $0), we can't prove reordering.
if (aCost === bCost) {
console.log(
`[cost-optimized skip] Both providers have equal pricing ($${aCost}/M each) — ` +
`cannot prove reordering. Skipping.`
);
return;
}
// Identify cheap vs pricey.
const [cheapConn, priceyConn] = aCost <= bCost ? [a, b] : [b, a];
const [cheapCost, priceyCost] = aCost <= bCost ? [aCost, bCost] : [bCost, aCost];
const cheapModel =
cheapConn.model ??
PROVIDER_DEFAULT_MODELS[cheapConn.provider] ??
`${cheapConn.provider}/default`;
const priceyModel =
priceyConn.model ??
PROVIDER_DEFAULT_MODELS[priceyConn.provider] ??
`${priceyConn.provider}/default`;
console.log(
`[cost-optimized] Cheap: ${cheapConn.provider}/${cheapModel} ($${cheapCost}/M), ` +
`Pricey: ${priceyConn.provider}/${priceyModel} ($${priceyCost}/M)`
);
// Create combo with PRICEY first — a correct cost sorter must reorder to CHEAP first.
const comboName = `__live-smoke-cost-opt-${Date.now()}__`;
const combo = await (h as any).combosDb.createCombo({
name: comboName,
strategy: "cost-optimized",
// Pricey listed FIRST: proves the sorter reorders, not just uses the given order.
models: [(h as any).comboModelFor(priceyConn), (h as any).comboModelFor(cheapConn)],
config: { maxRetries: 0, retryDelayMs: 0, stickyRoundRobinLimit: 1 },
});
try {
const response = await (h as any).handleChat(
(h as any).buildRequest({
body: (h as any).liveBody(comboName, {
messages: [{ role: "user", content: `ping ${uniqueNonce("cost-opt")}` }],
}),
})
);
assert.equal(response.status, 200, `Expected HTTP 200, got ${response.status}`);
const text = await (h as any).readCompletionText(response);
assert.ok(text.length > 0, "Expected non-empty completion text from cost-optimized combo");
// Collect served-provider signals.
const headerProvider = (h as any).servedProvider(response);
const bodyProvider = await (h as any).servedProviderFromBody(response);
const rawModel = await readResponseModel(response);
console.log(
`[cost-optimized] served: header=${headerProvider ?? "(absent)"}, ` +
`body=${bodyProvider ?? "(absent)"}, model="${rawModel ?? "(n/a)"}"`
);
// PRIMARY ASSERTION: served provider must be the cheap one, not the pricey one.
// The cost sorter should have put the cheap provider first despite it being listed second.
//
// We use three signals in priority order:
// 1. X-OmniRoute-Selected-Connection-Id header (fallback paths only — may be absent on 200).
// 2. Body model field provider prefix (e.g. "groq/model" → "groq").
// 3. Raw model string comparison (model name matches cheap provider's known model).
// Signal 1: header.
if (headerProvider !== undefined) {
assert.equal(
headerProvider,
cheapConn.provider,
`[header] Expected cheap provider "${cheapConn.provider}" to serve, got "${headerProvider}". ` +
`Cost sorter may not have reordered: cheap=$${cheapCost}/M, pricey=$${priceyCost}/M`
);
console.log(
`[cost-optimized PASS via header] ${cheapConn.provider} served (cost $${cheapCost}/M < $${priceyCost}/M)`
);
return;
}
// Signal 2: body provider prefix.
if (bodyProvider !== undefined) {
assert.equal(
bodyProvider,
cheapConn.provider,
`[body prefix] Expected cheap provider "${cheapConn.provider}" to serve, got "${bodyProvider}". ` +
`Cost sorter may not have reordered: cheap=$${cheapCost}/M, pricey=$${priceyCost}/M`
);
console.log(
`[cost-optimized PASS via body prefix] ${cheapConn.provider} served (cost $${cheapCost}/M < $${priceyCost}/M)`
);
return;
}
// Signal 3: raw model string.
if (rawModel !== undefined) {
// If the response model matches the cheap provider's model name, the cheap provider served.
if (rawModel === cheapModel || rawModel.endsWith(`/${cheapModel}`)) {
console.log(
`[cost-optimized PASS via model field] model="${rawModel}" matches cheap provider ` +
`${cheapConn.provider}/${cheapModel} (cost $${cheapCost}/M < $${priceyCost}/M)`
);
return;
}
// If the response model matches the pricey provider's model name, the cost sort failed.
if (rawModel === priceyModel || rawModel.endsWith(`/${priceyModel}`)) {
assert.fail(
`[model field] Pricey provider "${priceyConn.provider}" (model="${priceyModel}", ` +
`$${priceyCost}/M) served BEFORE cheap "${cheapConn.provider}" (model="${cheapModel}", ` +
`$${cheapCost}/M) — cost sorter did not reorder correctly.`
);
}
// Model field present but does not match either known model (provider echoes an
// aliased or prefixed name). We cannot distinguish which provider served.
console.warn(
`[cost-optimized] Signal ambiguous: rawModel="${rawModel}" does not match ` +
`"${cheapModel}" or "${priceyModel}". Got 200 + non-empty text; cannot confirm ` +
`cheap provider served. Recording as diagnostic-pass (cost gap confirmed: ` +
`$${cheapCost}/M vs $${priceyCost}/M).`
);
return;
}
// No signal at all — 200 + non-empty but we cannot confirm which provider served.
console.warn(
`[cost-optimized] All provider signals absent (header=absent, body=absent, model=absent). ` +
`Got HTTP 200 + non-empty text. Cost gap confirmed ($${cheapCost}/M vs $${priceyCost}/M) ` +
`but serving provider not identifiable. Recording as diagnostic-pass.`
);
} finally {
if (typeof combo?.id === "string") {
await (h as any).combosDb.deleteCombo(combo.id as string);
}
}
}
);
// ---------------------------------------------------------------------------
// Test 2: fusion — panel fans out + judge synthesizes one answer
// ---------------------------------------------------------------------------
test(
"live fusion — panel fans out and judge synthesizes one answer",
{
skip: !h.LIVE_ENABLED && "RUN_COMBO_LIVE!=1",
},
async () => {
if (!h.LIVE_ENABLED) return;
// Cost guard: pick ≤3 panel providers. Use cheapest/most reliable.
const candidates = await pickConfirmedHealthy(3, [
"groq",
"cerebras",
"opencode-go",
"deepseek",
"gemini",
"together",
]);
// Need at least 2 distinct healthy providers to run a real fusion.
const seen = new Set<string>();
const panelConns = candidates
.filter((c: LiveConnection) => {
if (seen.has(c.provider)) return false;
seen.add(c.provider);
return true;
})
.slice(0, 3); // cap at 3 to limit cost
if (panelConns.length < 2) {
console.log(
`[fusion skip] Only ${panelConns.length} distinct healthy provider(s) confirmed — ` +
`need ≥2 for a real panel. Skipping.`
);
return;
}
// Judge = first (cheapest) panel provider — cheap enough for a 16-token synthesis call.
const judgeConn = panelConns[0];
const judgeModel =
judgeConn.model ??
PROVIDER_DEFAULT_MODELS[judgeConn.provider] ??
`${judgeConn.provider}/default`;
const judgeModelStr = `${judgeConn.provider}/${judgeModel}`;
const panelModels = panelConns.map((c: LiveConnection) => (h as any).comboModelFor(c));
console.log(
`[fusion] Panel (${panelConns.length}): ${panelConns.map((c: LiveConnection) => c.provider).join(", ")} | ` +
`judge: ${judgeModelStr}`
);
const comboName = `__live-smoke-fusion-${Date.now()}__`;
const combo = await (h as any).combosDb.createCombo({
name: comboName,
strategy: "fusion",
models: panelModels,
config: {
maxRetries: 0,
retryDelayMs: 0,
judgeModel: judgeModelStr,
fusionTuning: {
minPanel: 2,
panelHardTimeoutMs: 90_000,
},
},
});
try {
const response = await (h as any).handleChat(
(h as any).buildRequest({
body: (h as any).liveBody(comboName, {
messages: [
{
role: "user",
content: `ping ${uniqueNonce("fusion")} — reply in one short sentence`,
},
],
// max_tokens:16 is the harness default via liveBody(); the judge call will
// also be bounded, keeping the panel + judge calls cheap.
}),
})
);
assert.equal(
response.status,
200,
`Expected HTTP 200 from fusion combo, got ${response.status}`
);
const text = await (h as any).readCompletionText(response);
assert.ok(
text.length > 0,
"Expected a non-empty synthesized completion from the fusion judge"
);
// The fusion response comes from the JUDGE call.
// The body's `model` field should reflect the judge model, confirming the judge ran.
const rawModel = await readResponseModel(response);
const bodyProvider = await (h as any).servedProviderFromBody(response);
console.log(
`[fusion] synthesized text (first 80 chars): "${text.slice(0, 80)}" | ` +
`model="${rawModel ?? "(n/a)"}" | body provider=${bodyProvider ?? "(absent)"}`
);
// SIGNAL ANALYSIS — panel/judge evidence.
// The judge model string is judgeModelStr (e.g. "groq/llama-3.1-8b-instant").
// If rawModel matches the judge's model name, the judge ran.
if (rawModel !== undefined) {
const judgeRan =
rawModel === judgeModel ||
rawModel === judgeModelStr ||
rawModel.endsWith(`/${judgeModel}`);
if (judgeRan) {
console.log(
`[fusion EVIDENCE] Judge confirmed: response model="${rawModel}" matches judge ${judgeModelStr}`
);
} else {
// Model field doesn't match judge — may be aliased or provider returns own name.
console.warn(
`[fusion] response model="${rawModel}" does not exactly match judge "${judgeModelStr}". ` +
`Panel synthesis still assumed from HTTP 200 + non-empty text.`
);
}
}
// The fusion panel had ≥2 members — at least 2 distinct upstream calls happened
// before the judge synthesized. We can't easily introspect individual panel calls
// from the test layer (they're internal to fusion.ts / combo.ts). The 200 + non-empty
// text from the judge is the authoritative proof of fusion completing.
console.log(
`[fusion PASS] Panel(${panelConns.length}) → judge(${judgeModelStr}) synthesis: HTTP 200, ` +
`${text.length} chars. Provider signal from body: ${bodyProvider ?? "absent (model name has no slash prefix)"}.`
);
} finally {
if (typeof combo?.id === "string") {
await (h as any).combosDb.deleteCombo(combo.id as string);
}
}
}
);