fix(usage): preserve distinct physical image attempt accounting

This commit is contained in:
Qiancheng Yan
2026-10-11 22:15:02 +08:00
committed by diegosouzapw
parent 9b4ecacf95
commit f83f69f0f1
7 changed files with 184 additions and 68 deletions

View File

@@ -1 +1 @@
- **fix(usage):** `/v1/images/generations` and `/v1/images/edits` (direct, provider-scoped and combo) now record successful calls in `usage_history` against the API key and the connection that served them when the upstream reports token usage **and the model has a pricing row**, so per-key USD limits and per-key usage reports include priced image calls; successful genuinely unpriced/no-usage image calls remain in attributed `call_logs` only to avoid the fail-closed unpriced-usage guard; pricing-store failures preserve reported token evidence rather than treating an unknown price as missing, and Codex dedicated Images usage is accounted without changing its existing response envelope; image `call_logs` rows on the combo and edit paths now carry the API key and connection ([#14619](https://github.com/diegosouzapw/OmniRoute/pull/14619), refs [#14618](https://github.com/diegosouzapw/OmniRoute/issues/14618))
- **fix(usage):** covered normalized-token image generation/edit paths now record successful priced calls in `usage_history` against the API key and serving connection; each physical image attempt keeps its own priced usage row even when completion timestamps and token counts match; genuinely unpriced/no-usage calls remain in attributed `call_logs` only to avoid the fail-closed unpriced-usage guard; pricing-store failures preserve reported token evidence, Codex dedicated Images usage keeps its existing response envelope, and covered combo/edit `call_logs` rows carry the API key and connection. Direct SYNTX edits, Antigravity-native edits, and native provider usage that is not normalized into the wrapper remain outside this change ([#14619](https://github.com/diegosouzapw/OmniRoute/pull/14619), refs [#14618](https://github.com/diegosouzapw/OmniRoute/issues/14618))

View File

@@ -13,8 +13,8 @@
* provider handler writes (request-scoped, see callLogApiKeyContext);
* - on success, when the handler result carries the upstream `usage` object
* (OpenAI-compatible image APIs return one), writes one `usage_history` row
* attributed to the same key and connection, so the existing token
* extractors, pricing and per-key USD limits apply unchanged.
* for this physical attempt, attributed to the same key and connection, so
* the existing token extractors, pricing and per-key USD limits apply unchanged.
*
* Successful calls whose upstream reports no usage are deliberately NOT
* written. Calls that report usage but have no pricing row are also skipped: a
@@ -93,20 +93,23 @@ export async function runImageRequestWithAccounting<T>(
if (!priced && !failed) return result;
// saveRequestUsage never throws (it logs and swallows DB errors).
await saveRequestUsage({
provider: accounting.provider,
model: localModel,
tokens: usage,
status: "200",
success: true,
latencyMs,
timeToFirstTokenMs: latencyMs,
connectionId,
apiKeyId,
apiKeyName,
comboStrategy: accounting.comboStrategy || null,
endpoint: accounting.endpoint,
});
await saveRequestUsage(
{
provider: accounting.provider,
model: localModel,
tokens: usage,
status: "200",
success: true,
latencyMs,
timeToFirstTokenMs: latencyMs,
connectionId,
apiKeyId,
apiKeyName,
comboStrategy: accounting.comboStrategy || null,
endpoint: accounting.endpoint,
},
{ deduplicate: false }
);
}
return result;

View File

@@ -857,10 +857,22 @@ export interface UsageEntry {
cpaAuthIndex?: string | null;
}
/**
* Controls the legacy natural-key guard for one usage write.
*
* Normal callers keep the default so repeated callbacks for one request remain
* one row. A writer that already owns a distinct physical-attempt boundary may
* explicitly disable that guard without changing the row, cache, event, or
* account-attribution paths below.
*/
export interface SaveRequestUsageOptions {
deduplicate?: boolean;
}
/**
* Save request usage entry to SQLite.
*/
export async function saveRequestUsage(entry: UsageEntry) {
export async function saveRequestUsage(entry: UsageEntry, options: SaveRequestUsageOptions = {}) {
if (!shouldPersistToDisk) return;
// Freeze the timestamp on the first attempt so the deferred retry replays
@@ -868,13 +880,13 @@ export async function saveRequestUsage(entry: UsageEntry) {
const frozenEntry = { ...entry, timestamp: entry.timestamp || new Date().toISOString() };
try {
attemptWrite(frozenEntry);
attemptWrite(frozenEntry, options);
} catch (error) {
if (!isWriteLockError(error)) {
console.error("Failed to save usage stats:", error);
return;
}
scheduleWriteRetry(frozenEntry);
scheduleWriteRetry(frozenEntry, options);
}
}

View File

@@ -0,0 +1,51 @@
import type { getDbInstance } from "../../db/core";
import { resolveProviderId } from "@/shared/constants/providers";
import type { UsageEntry } from "../usageHistory";
/** Look up and backfill a legacy natural-key duplicate inside the caller's transaction. */
export function backfillDuplicateUsageEntry(
db: ReturnType<typeof getDbInstance>,
entry: UsageEntry,
timestamp: string,
tokensInput: number,
tokensOutput: number
): boolean {
const existing = db
.prepare(
`SELECT id, endpoint, cpa_auth_index FROM usage_history
WHERE timestamp = ?
AND COALESCE(provider, '') = COALESCE(?, '')
AND COALESCE(model, '') = COALESCE(?, '')
AND COALESCE(connection_id, '') = COALESCE(?, '')
AND COALESCE(api_key_id, '') = COALESCE(?, '')
AND tokens_input = ?
AND tokens_output = ?
ORDER BY id DESC LIMIT 1`
)
.get(
timestamp,
entry.provider ? resolveProviderId(entry.provider) : null,
entry.model || null,
entry.connectionId || null,
entry.apiKeyId || null,
tokensInput,
tokensOutput
) as { id: number; endpoint: string | null; cpa_auth_index: string | null } | undefined;
if (!existing) return false;
// Back-fill endpoint if the original row missed it.
if (!existing.endpoint && entry.endpoint) {
db.prepare(`UPDATE usage_history SET endpoint = ? WHERE id = ?`).run(
entry.endpoint,
existing.id
);
}
// A later completed attempt can carry the trace the first write missed.
if (!existing.cpa_auth_index && entry.cpaAuthIndex) {
db.prepare(`UPDATE usage_history SET cpa_auth_index = ? WHERE id = ?`).run(
entry.cpaAuthIndex,
existing.id
);
}
return true;
}

View File

@@ -29,6 +29,7 @@ import {
getPromptCacheReadTokens,
getReasoningTokens,
} from "./tokenAccounting";
import { backfillDuplicateUsageEntry } from "./usageHistory/duplicateEntry";
import type { UsageEntry } from "./usageHistory";
/** Delay before the single deferred attempt. Internal, not user-facing. */
@@ -64,16 +65,30 @@ function isLockMessage(message: string): boolean {
return /database(?: table| schema)? is (?:locked|busy)|SQLITE_BUSY|SQLITE_LOCKED/i.test(message);
}
/** Legacy natural-key dedup stays on unless the caller owns a physical attempt. */
type UsageWriteOptions = {
deduplicate?: boolean;
};
/**
* Single write attempt. Throws on failure so the caller decides between a
* deferred retry (lock) and an immediate log (anything else).
*
* `deduplicate: false` is the image-attempt opt-out: two real upstream calls
* can share every natural-key field, including the millisecond timestamp.
*/
export function attemptWrite(entry: UsageEntry): void {
export function attemptWrite(entry: UsageEntry, options: UsageWriteOptions = {}): void {
const db = getDbInstance();
const prepared = prepareEntry(db, entry);
let inserted = false;
const deduplicate = options.deduplicate !== false;
db.transaction(() => {
if (!deduplicate) {
insertEntry(db, prepared);
inserted = true;
return;
}
inserted = insertUnlessDuplicate(db, prepared);
})();
@@ -128,30 +143,7 @@ function insertUnlessDuplicate(
prepared: PreparedEntry
): boolean {
const { entry, timestamp, tokensInput, tokensOutput } = prepared;
const existing = db
.prepare(
`SELECT id, endpoint, cpa_auth_index FROM usage_history
WHERE timestamp = ?
AND COALESCE(provider, '') = COALESCE(?, '')
AND COALESCE(model, '') = COALESCE(?, '')
AND COALESCE(connection_id, '') = COALESCE(?, '')
AND COALESCE(api_key_id, '') = COALESCE(?, '')
AND tokens_input = ?
AND tokens_output = ?
ORDER BY id DESC LIMIT 1`
)
.get(
timestamp,
entry.provider ? resolveProviderId(entry.provider) : null,
entry.model || null,
entry.connectionId || null,
entry.apiKeyId || null,
tokensInput,
tokensOutput
) as { id: number; endpoint: string | null; cpa_auth_index: string | null } | undefined;
if (existing) {
backfillDuplicate(db, existing, entry);
if (backfillDuplicateUsageEntry(db, entry, timestamp, tokensInput, tokensOutput)) {
return false;
}
@@ -159,27 +151,6 @@ function insertUnlessDuplicate(
return true;
}
function backfillDuplicate(
db: ReturnType<typeof getDbInstance>,
existing: { id: number; endpoint: string | null; cpa_auth_index: string | null },
entry: UsageEntry
): void {
// Back-fill endpoint if the original row missed it.
if (!existing.endpoint && entry.endpoint) {
db.prepare(`UPDATE usage_history SET endpoint = ? WHERE id = ?`).run(
entry.endpoint,
existing.id
);
}
// A later completed attempt can carry the trace the first write missed.
if (!existing.cpa_auth_index && entry.cpaAuthIndex) {
db.prepare(`UPDATE usage_history SET cpa_auth_index = ? WHERE id = ?`).run(
entry.cpaAuthIndex,
existing.id
);
}
}
function insertEntry(db: ReturnType<typeof getDbInstance>, prepared: PreparedEntry): void {
const { entry, timestamp, serviceTier, tokensInput, tokensOutput, accountIdentity } = prepared;
db.prepare(
@@ -231,10 +202,10 @@ function toFirstToken(entry: UsageEntry): number {
* the retry attempts the write once and, on failure, writes the existing log
* line and stops.
*/
export function scheduleWriteRetry(entry: UsageEntry): void {
export function scheduleWriteRetry(entry: UsageEntry, options: UsageWriteOptions = {}): void {
const timer = setTimeout(() => {
try {
attemptWrite(entry);
attemptWrite(entry, options);
} catch (retryError) {
console.error("Failed to save usage stats:", retryError);
}

View File

@@ -694,3 +694,44 @@ test("parallel image requests do not cross key or connection attribution", async
assert.ok(logs.some((row) => row.api_key_id === keyA.id && row.connection_id === a.id));
assert.ok(logs.some((row) => row.api_key_id === keyB.id && row.connection_id === b.id));
});
test("same-millisecond priced image attempts each persist usage and spend", async (t) => {
await updatePricing({ openai: { "gpt-image-2": IMAGE_PRICE } });
const connection = await seedConnection("openai");
const key = await createLimitedKey(100);
const hits = mockOpenAiCompatibleImageUpstream("https://api.openai.com/v1/images/generations");
t.mock.timers.enable({ apis: ["Date"], now: 1_791_705_600_000 });
try {
const first = await imageRoute.POST(generationRequest(key.key, "openai/gpt-image-2"));
const second = await imageRoute.POST(generationRequest(key.key, "openai/gpt-image-2"));
assert.equal(first.status, 200);
assert.equal(second.status, 200);
assert.equal(hits.length, 2);
assert.equal((await callLogRows("/v1/images/generations")).length, 2);
assert.equal(usageRows().length, 2);
assert.ok(usageRows().every((row) => row.connection_id === connection.id));
assert.equal((await spendOf(key.key)).weeklySpentUsd, EXPECTED_CALL_USD * 2);
} finally {
t.mock.timers.reset();
}
});
test("real-clock parallel priced image burst persists every physical attempt", async () => {
await updatePricing({ openai: { "gpt-image-2": IMAGE_PRICE } });
await seedConnection("openai");
const key = await createLimitedKey(100);
const hits = mockOpenAiCompatibleImageUpstream("https://api.openai.com/v1/images/generations");
const responses = await Promise.all(
Array.from({ length: 20 }, () =>
imageRoute.POST(generationRequest(key.key, "openai/gpt-image-2"))
)
);
assert.ok(responses.every((response) => response.status === 200));
assert.equal(hits.length, 20);
assert.equal((await callLogRows("/v1/images/generations")).length, 20);
assert.equal(usageRows().length, 20);
// The quota API rounds the Decimal total; avoid binary multiplication residue.
assert.equal((await spendOf(key.key)).weeklySpentUsd, 20.005);
});

View File

@@ -140,3 +140,41 @@ test("saveRequestUsage: two entries with different providers are both inserted",
"Two entries with different providers should both be inserted"
);
});
test("saveRequestUsage: legacy dedup retains endpoint and cpa auth backfills", async () => {
const db = getDbInstance();
const before = countRows(db);
const ts = "2026-10-11T12:34:56.789Z";
const entry = makeEntry({ timestamp: ts, endpoint: null, cpaAuthIndex: null });
await saveRequestUsage(entry);
await saveRequestUsage({
...entry,
endpoint: "/v1/chat/completions",
cpaAuthIndex: "auth-index-fixture",
});
assert.equal(countRows(db), before + 1);
const row = db
.prepare("SELECT endpoint, cpa_auth_index FROM usage_history WHERE timestamp = ?")
.get(ts) as { endpoint: string | null; cpa_auth_index: string | null };
assert.deepEqual(row, {
endpoint: "/v1/chat/completions",
cpa_auth_index: "auth-index-fixture",
});
});
test("saveRequestUsage: opt-in physical attempts insert same natural key twice", async () => {
const db = getDbInstance();
const before = countRows(db);
const entry = makeEntry({ timestamp: new Date().toISOString() });
await saveRequestUsage(entry, { deduplicate: false });
await saveRequestUsage(entry, { deduplicate: false });
assert.equal(
countRows(db),
before + 2,
"Distinct physical attempts must not share the legacy natural-key dedup"
);
});