Compare commits

..

1 Commits

Author SHA1 Message Date
diegosouzapw
4711f22c17 fix(usage): fail closed on budget enforcement for unpriced auto alias (#12341) 2026-09-10 15:00:23 -03:00
8 changed files with 245 additions and 158 deletions

View File

@@ -35,24 +35,16 @@ export function resolveOpencodeTarget(opts = {}) {
baseUrl = `http://localhost:${Number(opts.port ?? process.env.PORT ?? 20128) || 20128}`;
}
// Precedence: explicit --api-key flag > OMNIROUTE_API_KEY env var > active
// context's management token. A context's accessToken/apiKey is a CLI
// management credential (oma_live_...) with no /v1/* inference scope — it
// must never silently outrank a real inference key the caller supplied
// either as a flag or via the ambient env var (mirrors the explicit >
// ambient-env > context precedence documented in bin/cli/api.mjs's
// buildHeaders()). Only fall back to the context token when neither an
// explicit flag nor the env var is set.
let apiKey = opts.apiKey ?? opts["api-key"];
if (!apiKey) apiKey = process.env.OMNIROUTE_API_KEY || "";
if (!apiKey) {
try {
const c = resolveActiveContext(opts.context ?? process.env.OMNIROUTE_CONTEXT);
apiKey = c?.accessToken || c?.apiKey || "";
apiKey = c?.accessToken || c?.apiKey;
} catch {
/* no context auth */
}
}
if (!apiKey) apiKey = process.env.OMNIROUTE_API_KEY || "";
return { baseUrl: baseUrl.replace(/\/+$/, ""), apiKey };
}
@@ -185,17 +177,8 @@ export function registerSetupOpencode(program) {
"--allow-container-write",
"Write even when the target is inside a container and not mounted from the host"
)
.action(async (opts, cmd) => {
// Commander parses the ancestor program's own global --api-key option
// (bin/cli/program.mjs, bound to .env("OMNIROUTE_API_KEY")) against any
// occurrence of the flag in argv, so it wins the value even when the
// user typed --api-key AFTER `setup-opencode` — this local option's own
// `opts.apiKey` never sees it. cmd.optsWithGlobals() resolves to the
// correct value either way ("globals overwrite locals" is exactly the
// outcome we want here, since the global option is where the value
// always actually lands).
const resolvedOpts = { ...opts, apiKey: cmd.optsWithGlobals().apiKey ?? opts.apiKey };
const code = await runSetupOpencodeCommand(resolvedOpts);
.action(async (opts) => {
const code = await runSetupOpencodeCommand(opts);
if (code !== 0) process.exit(code);
});
}

View File

@@ -0,0 +1 @@
- fix(usage): fail closed on API-key budget enforcement when a provider's `auto` routing alias has no pricing row, instead of silently counting it as $0 (#12341)

View File

@@ -1 +0,0 @@
- fix(cli): setup-opencode no longer sends an active context's management token to `/v1/models` when `--api-key`/`OMNIROUTE_API_KEY` is supplied — an explicit flag or the env var now always outranks the context's token, and the flag itself is no longer swallowed by the parent program's global `--api-key` option (#12783)

View File

@@ -1,7 +1,7 @@
import { getDbInstance } from "@/lib/db/core";
import type { ProviderLimitsCacheEntry } from "@/lib/db/providerLimits";
import { getProviderQuotaWindowStartIso } from "@/lib/db/quotaResetEvents";
import { calculateCost } from "./costCalculator";
import { calculateCostDetailed } from "./costCalculator";
import { buildErrorBody, sanitizeErrorMessage } from "@omniroute/open-sse/utils/error.ts";
const FORTALEZA_UTC_OFFSET_MS = 3 * 60 * 60 * 1000;
@@ -29,6 +29,15 @@ export interface ApiKeyUsageLimitStatus {
weeklyResetAtIso: string | null;
dailyExceeded: boolean;
weeklyExceeded: boolean;
/**
* True when at least one usage_history row in the daily/weekly window could not
* be priced at all (no pricing row for the provider+model — e.g. a routing
* alias such as `auto`, #12341). Enforcement fails closed on this: an unpriced
* row forces `*Exceeded = true` rather than silently contributing $0 to spend,
* since a real cost may be hiding behind the alias.
*/
dailyHasUnpricedUsage?: boolean;
weeklyHasUnpricedUsage?: boolean;
}
export interface ApiKeyUsageLimitDeps {
@@ -373,8 +382,14 @@ async function getProviderWeeklyWindow(
};
}
async function getApiKeyUsdSpendSince(apiKeyId: string, sinceIso: string): Promise<number> {
if (!apiKeyId) return 0;
interface ApiKeyUsdSpend {
totalUsd: number;
/** True when at least one (provider, model) group had no pricing row at all (#12341). */
hasUnpricedUsage: boolean;
}
async function getApiKeyUsdSpendSince(apiKeyId: string, sinceIso: string): Promise<ApiKeyUsdSpend> {
if (!apiKeyId) return { totalUsd: 0, hasUnpricedUsage: false };
const db = getDbInstance();
const rows = db
.prepare(
@@ -398,12 +413,13 @@ async function getApiKeyUsdSpendSince(apiKeyId: string, sinceIso: string): Promi
.all({ apiKeyId, sinceIso }) as UsageCostRow[];
let total = 0;
let hasUnpricedUsage = false;
for (const row of rows) {
const provider = typeof row.provider === "string" ? row.provider : "";
const model = typeof row.model === "string" ? row.model : "";
if (!provider || !model) continue;
total += await calculateCost(
const { costUsd, priced } = await calculateCostDetailed(
provider,
model,
{
@@ -419,9 +435,17 @@ async function getApiKeyUsdSpendSince(apiKeyId: string, sinceIso: string): Promi
serviceTier: row.serviceTier || "standard",
}
);
if (!priced) {
hasUnpricedUsage = true;
console.warn(
`[apiKeyUsageLimits] no pricing found for ${provider}/${model} — usage counted as $0 ` +
"and enforcement is failing closed for this window (#12341)"
);
}
total += costUsd;
}
return roundUsd(total);
return { totalUsd: roundUsd(total), hasUnpricedUsage };
}
export async function getApiKeyUsageLimitStatus(
@@ -443,10 +467,27 @@ export async function getApiKeyUsageLimitStatus(
const weeklyLimitUsd = normalizeLimitUsd(metadata.weeklyUsageLimitUsd);
const enabled = metadata.usageLimitEnabled === true;
const [dailySpentUsd, weeklySpentUsd] = await Promise.all([
const [dailySpend, weeklySpend] = await Promise.all([
getApiKeyUsdSpendSince(metadata.id, dailyWindowStartIso),
getApiKeyUsdSpendSince(metadata.id, weeklyWindowStartIso),
]);
const dailySpentUsd = dailySpend.totalUsd;
const weeklySpentUsd = weeklySpend.totalUsd;
// Fail closed (#12341): a window with a configured limit that also contains
// usage which could not be priced at all (e.g. a provider's `auto` routing
// alias with no catalog price) must not let that usage silently pass the cap
// as an invisible $0 — treat the limit as exceeded rather than trust an
// undercounted spend total. A window with no configured limit was never
// enforced, so unpriced usage there is only logged, not blocking.
const dailyExceeded =
enabled &&
dailyLimitUsd !== null &&
(dailySpentUsd >= dailyLimitUsd || dailySpend.hasUnpricedUsage);
const weeklyExceeded =
enabled &&
weeklyLimitUsd !== null &&
(weeklySpentUsd >= weeklyLimitUsd || weeklySpend.hasUnpricedUsage);
return {
enabled,
@@ -458,8 +499,10 @@ export async function getApiKeyUsageLimitStatus(
dailyResetAtIso,
weeklyWindowStartIso,
weeklyResetAtIso,
dailyExceeded: enabled && dailyLimitUsd !== null && dailySpentUsd >= dailyLimitUsd,
weeklyExceeded: enabled && weeklyLimitUsd !== null && weeklySpentUsd >= weeklyLimitUsd,
dailyExceeded,
weeklyExceeded,
dailyHasUnpricedUsage: dailySpend.hasUnpricedUsage,
weeklyHasUnpricedUsage: weeklySpend.hasUnpricedUsage,
};
}

View File

@@ -173,18 +173,31 @@ export function computeCostFromPricing(
return cost * getCodexFastCostMultiplier(options.provider, options.model, options.serviceTier);
}
export async function calculateCost(
/**
* Result of a cost calculation that also reports whether the number is backed by
* a real pricing row. Budget-enforcement callers (#12341) must be able to tell
* "$0, priced" (a genuinely free/flat-rate model) apart from "$0, unpriced" (no
* pricing row was ever found — e.g. a routing alias like `auto`) so they can fail
* closed on the latter instead of letting it silently pass a hard budget cap.
*/
export interface CostCalculationResult {
costUsd: number;
/** false when no pricing row (direct, normalized, or codex-effortless) was found. */
priced: boolean;
}
export async function calculateCostDetailed(
provider: string,
model: string,
tokens: Record<string, number | undefined> | null | undefined,
options: CostCalculationOptions = {}
): Promise<number> {
if (!tokens || !provider || !model) return 0;
): Promise<CostCalculationResult> {
if (!tokens || !provider || !model) return { costUsd: 0, priced: true };
// Short-circuit before any pricing DB lookup when an exact, provider-reported
// cost is present (currently xAI's `cost_in_usd_ticks` — see extractExactCostUsd).
const exactCostUsd = extractExactCostUsd(tokens);
if (exactCostUsd !== null) return exactCostUsd;
if (exactCostUsd !== null) return { costUsd: exactCostUsd, priced: true };
try {
const { getPricingForModel } = await import("@/lib/db/settings");
@@ -204,23 +217,37 @@ export async function calculateCost(
}
}
}
if (!pricing) return 0;
// No pricing row anywhere — this is the #12341 case (e.g. a provider's own
// routing alias such as "auto" that has no catalog price). Report it as
// unpriced rather than a bare $0 so budget enforcement can fail closed.
if (!pricing) return { costUsd: 0, priced: false };
const pricingRecord =
pricing && typeof pricing === "object" && !Array.isArray(pricing)
? (pricing as Record<string, unknown>)
: {};
return computeCostFromPricing(pricingRecord, tokens, {
const costUsd = computeCostFromPricing(pricingRecord, tokens, {
provider,
model,
...options,
});
return { costUsd, priced: true };
} catch (error) {
console.error("Error calculating cost:", error);
return 0;
return { costUsd: 0, priced: false };
}
}
export async function calculateCost(
provider: string,
model: string,
tokens: Record<string, number | undefined> | null | undefined,
options: CostCalculationOptions = {}
): Promise<number> {
const result = await calculateCostDetailed(provider, model, tokens, options);
return result.costUsd;
}
type ModalPricing = Record<string, unknown>;
/** Per-image cost: flat per-image × n. 0 when pricing/usage absent. */

View File

@@ -0,0 +1,113 @@
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-api-key-budget-alias-"));
process.env.DATA_DIR = TEST_DATA_DIR;
process.env.API_KEY_SECRET = process.env.API_KEY_SECRET || "budget-alias-test-secret";
const core = await import("../../src/lib/db/core.ts");
const apiKeysDb = await import("../../src/lib/db/apiKeys.ts");
const usageHistory = await import("../../src/lib/usage/usageHistory.ts");
const usageLimits = await import("../../src/lib/usage/apiKeyUsageLimits.ts");
const NOW = Date.parse("2026-06-19T20:00:00.000Z");
async function resetStorage() {
core.resetDbInstance();
apiKeysDb.resetApiKeyState();
usageHistory.clearPendingRequests();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
fs.mkdirSync(TEST_DATA_DIR, { recursive: true });
}
test.beforeEach(async () => {
await resetStorage();
});
test.after(() => {
core.resetDbInstance();
apiKeysDb.resetApiKeyState();
fs.rmSync(TEST_DATA_DIR, { recursive: true, force: true, maxRetries: 5, retryDelay: 100 });
});
async function makeMeteredKey() {
const created = await apiKeysDb.createApiKey("Budget Alias Key", "machine-budget-01");
await apiKeysDb.updateApiKeyPermissions(created.id, {
usageLimitEnabled: true,
dailyUsageLimitUsd: 10,
weeklyUsageLimitUsd: 50,
});
apiKeysDb.clearApiKeyCaches();
const metadata = await apiKeysDb.getApiKeyMetadata(created.key);
assert.ok(metadata);
return { created, metadata: metadata! };
}
test("BUG #12341: a real, billable completion routed through cursor/auto (unpriced) must not silently pass the daily budget cap as $0", async () => {
const { created, metadata } = await makeMeteredKey();
// Cursor's own default routing alias ("Auto (current, default)") has no
// pricing row anywhere — this is real, mainstream billable traffic, not an
// edge case.
await usageHistory.saveRequestUsage({
provider: "cursor",
model: "auto",
apiKeyId: created.id,
apiKeyName: "Budget Alias Key",
tokens: { input: 1_000_000, output: 1_000_000 },
success: true,
timestamp: "2026-06-19T12:00:00.000Z",
});
const status = await usageLimits.getApiKeyUsageLimitStatus(
{ ...metadata, allowedConnections: null },
{ now: () => NOW }
);
// Fail closed (#12341): unpriced usage in a window with a configured limit
// must flip the window to exceeded, even though the naive USD total is $0.
assert.equal(status.dailySpentUsd, 0, "cost stays $0 — no pricing row exists for cursor/auto");
assert.equal(
status.dailyHasUnpricedUsage,
true,
"status must flag that unpriced usage was seen in the daily window"
);
assert.equal(
status.dailyExceeded,
true,
"enforcement must fail closed instead of silently allowing unlimited unpriced usage"
);
});
test("control: a priced model routed at the same tokens does NOT trip fail-closed enforcement", async () => {
const { updatePricing } = await import("@/lib/db/settings");
await updatePricing({
openai: {
"gpt-4o": { input: 1, cached: 1, output: 1, reasoning: 1, cache_creation: 1 },
},
});
const { created, metadata } = await makeMeteredKey();
await usageHistory.saveRequestUsage({
provider: "openai",
model: "gpt-4o",
apiKeyId: created.id,
apiKeyName: "Budget Alias Key",
tokens: { input: 1_000_000, output: 0 },
success: true,
timestamp: "2026-06-19T12:00:00.000Z",
});
const status = await usageLimits.getApiKeyUsageLimitStatus(
{ ...metadata, allowedConnections: null },
{ now: () => NOW }
);
assert.equal(status.dailySpentUsd, 1);
assert.equal(status.dailyHasUnpricedUsage, false);
assert.equal(status.dailyExceeded, false);
});

View File

@@ -0,0 +1,42 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
calculateCost,
calculateCostDetailed,
normalizeModelName,
} from "../../src/lib/usage/costCalculator.ts";
import { getDefaultPricing } from "../../src/shared/constants/pricing.ts";
const BILLABLE_TOKENS = { input: 1_000_000, output: 1_000_000 };
const PROVIDERS_WITH_UNPRICED_AUTO_MODEL = ["cursor", "factory", "trae", "dify", "llm-kiwi"];
test('normalizeModelName is a no-op for a bare alias like "auto"', () => {
assert.equal(normalizeModelName("auto"), "auto");
});
test('no pricing source carries an entry for the literal "auto" model id, for providers whose registry offers it as a real model', () => {
const pricing = getDefaultPricing() as Record<string, Record<string, unknown>>;
for (const provider of PROVIDERS_WITH_UNPRICED_AUTO_MODEL) {
const providerPricing = pricing[provider];
assert.ok(!providerPricing || !providerPricing["auto"], `expected no DEFAULT_PRICING entry for ${provider}/auto`);
}
});
test('calculateCost() still returns $0 for a real, billable completion routed through the unpriced "auto" alias (unchanged legacy contract)', async () => {
const cost = await calculateCost("cursor", "auto", BILLABLE_TOKENS);
assert.equal(cost, 0, "calculateCost's numeric contract is unchanged — $0 for unpriced usage");
});
test('#12341 fix: calculateCostDetailed() flags the "auto" alias as unpriced instead of a bare, indistinguishable $0', async () => {
for (const provider of PROVIDERS_WITH_UNPRICED_AUTO_MODEL) {
const result = await calculateCostDetailed(provider, "auto", BILLABLE_TOKENS);
assert.equal(result.costUsd, 0, `expected $0 for ${provider}/auto`);
assert.equal(result.priced, false, `expected ${provider}/auto to be reported as unpriced`);
}
});
test("control: calculateCostDetailed() DOES price a normal, non-alias model correctly and reports it as priced", async () => {
const result = await calculateCostDetailed("openai", "gpt-4o", BILLABLE_TOKENS);
assert.ok(result.costUsd > 0, `expected a known model to price above $0, got ${result.costUsd}`);
assert.equal(result.priced, true);
});

View File

@@ -1,121 +0,0 @@
import assert from "node:assert/strict";
import { mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { test } from "node:test";
import { resolveOpencodeTarget } from "../../bin/cli/commands/setup-opencode.mjs";
/** Point OMNIROUTE_CONTEXT config resolution at an isolated, throwaway DATA_DIR. */
function withIsolatedContext(contextConfig, fn) {
const dir = mkdtempSync(join(tmpdir(), "omniroute-setup-opencode-test-"));
const originalDataDir = process.env.DATA_DIR;
process.env.DATA_DIR = dir;
writeFileSync(
join(dir, "config.json"),
JSON.stringify({
version: 1,
currentContext: "remote",
contexts: { remote: contextConfig },
})
);
try {
return fn();
} finally {
if (originalDataDir === undefined) delete process.env.DATA_DIR;
else process.env.DATA_DIR = originalDataDir;
rmSync(dir, { recursive: true, force: true });
}
}
function withEnvApiKey(value, fn) {
const original = process.env.OMNIROUTE_API_KEY;
if (value === undefined) delete process.env.OMNIROUTE_API_KEY;
else process.env.OMNIROUTE_API_KEY = value;
try {
return fn();
} finally {
if (original === undefined) delete process.env.OMNIROUTE_API_KEY;
else process.env.OMNIROUTE_API_KEY = original;
}
}
test("setup-opencode: --api-key typed AFTER the subcommand name is not stolen by the parent program's global option", async () => {
const { createProgram } = await import("../../bin/cli/program.mjs");
const program = createProgram();
const setupOpencode = program.commands.find((c) => c.name() === "setup-opencode");
assert.ok(setupOpencode, "setup-opencode subcommand must be registered");
let capturedApiKey;
setupOpencode._actionHandler = null; // avoid the real network-calling action
setupOpencode.action((opts, cmd) => {
capturedApiKey = cmd.optsWithGlobals().apiKey ?? opts.apiKey;
});
await program.parseAsync(
[
"node",
"omniroute",
"setup-opencode",
"--remote",
"http://100.64.0.1:20128",
"--api-key",
"sk-TESTKEY123",
],
{ from: "node" }
);
assert.equal(
capturedApiKey,
"sk-TESTKEY123",
"the CLI-supplied --api-key value must reach the setup-opencode action handler"
);
});
test("resolveOpencodeTarget: (a) explicit --api-key flag wins over an active context's management token", () => {
withEnvApiKey(undefined, () => {
withIsolatedContext(
{ baseUrl: "http://100.64.0.1:20128", accessToken: "oma_live_CONTEXT_TOKEN" },
() => {
const { apiKey } = resolveOpencodeTarget({ apiKey: "sk-FLAG", context: "remote" });
assert.equal(apiKey, "sk-FLAG");
}
);
});
});
test("resolveOpencodeTarget: (b) OMNIROUTE_API_KEY env wins over an active context's management token when no flag is passed", () => {
withEnvApiKey("sk-ENVKEY", () => {
withIsolatedContext(
{ baseUrl: "http://100.64.0.1:20128", accessToken: "oma_live_CONTEXT_TOKEN" },
() => {
const { apiKey } = resolveOpencodeTarget({ context: "remote" });
assert.equal(apiKey, "sk-ENVKEY");
}
);
});
});
test("resolveOpencodeTarget: (c) the context's token is used only when neither a flag nor the env var is set", () => {
withEnvApiKey(undefined, () => {
withIsolatedContext(
{ baseUrl: "http://100.64.0.1:20128", accessToken: "oma_live_CONTEXT_TOKEN" },
() => {
const { apiKey } = resolveOpencodeTarget({ context: "remote" });
assert.equal(apiKey, "oma_live_CONTEXT_TOKEN");
}
);
});
});
test("resolveOpencodeTarget: falls back to '' when neither a flag, env var, nor a resolvable context is present", () => {
withEnvApiKey(undefined, () => {
withIsolatedContext({ baseUrl: "http://100.64.0.1:20128" }, () => {
const { apiKey } = resolveOpencodeTarget({
remote: "http://100.64.0.1:20128",
context: "__no-such-context__",
});
assert.equal(apiKey, "");
});
});
});