Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions src/lib/modelsDevSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,29 @@ function mapCapabilityRecord(record: Record<string, unknown>): ModelCapabilityEn
};
}

/**
* Process-local memo for models.dev pricing.
*
* `resolveCatalogPricing` used to call `getModelsDevPricing()` once per model
* while building `/v1/models` (~10k+ times). Each call re-ran the full SQL scan
* and `JSON.parse`d every pricing row, pegging the event loop for minutes
* (see #9685 / #10052). Memoize until the next save/clear write.
*/
let modelsDevPricingCache: PricingByProvider | null = null;

function invalidateModelsDevPricingCache(): void {
modelsDevPricingCache = null;
}
Comment on lines +205 to +209

/**
* Read synced pricing from `models_dev_pricing` namespace.
* Results are memoized until `saveModelsDevPricing` / `clearModelsDevPricing`.
*/
export function getModelsDevPricing(): PricingByProvider {
if (modelsDevPricingCache) {
return modelsDevPricingCache;
}

const db = getDbInstance();
const rows = db
.prepare("SELECT key, value FROM key_value WHERE namespace = 'models_dev_pricing'")
Expand All @@ -213,6 +232,7 @@ export function getModelsDevPricing(): PricingByProvider {
console.warn(`[MODELS_DEV] Corrupted pricing data for provider "${key}", skipping`);
}
}
modelsDevPricingCache = synced;
return synced;
}

Expand All @@ -233,6 +253,7 @@ export function saveModelsDevPricing(data: PricingByProvider): void {
});
tx();
backupDbFile("pre-write");
invalidateModelsDevPricingCache();
invalidateDbCache("pricing");
}

Expand All @@ -243,6 +264,7 @@ export function clearModelsDevPricing(): void {
const db = getDbInstance();
db.prepare("DELETE FROM key_value WHERE namespace = 'models_dev_pricing'").run();
backupDbFile("pre-write");
invalidateModelsDevPricingCache();
invalidateDbCache("pricing");
}

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/modelsDevSync-extended.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,33 @@ test("modelsDev pricing helpers persist records, skip corrupted rows, and clear
assert.deepEqual(modelsDev.getModelsDevPricing(), {});
});

test("getModelsDevPricing memoizes until save/clear (#9685)", async () => {
const modelsDev = await importFresh("pricing-memo");
const pricing = modelsDev.transformModelsDevToPricing(MOCK_MODELS_DEV_DATA);
modelsDev.saveModelsDevPricing(pricing);

const first = modelsDev.getModelsDevPricing();
const second = modelsDev.getModelsDevPricing();
assert.equal(first, second, "repeated reads must return the same memoized object");

// Mutating DB under the cache must not be visible until invalidation.
const db = core.getDbInstance();
db.prepare("DELETE FROM key_value WHERE namespace = 'models_dev_pricing'").run();
assert.equal(
modelsDev.getModelsDevPricing(),
first,
"raw SQL without save/clear must not bypass the memo"
);
Comment on lines +272 to +279

modelsDev.clearModelsDevPricing();
assert.deepEqual(modelsDev.getModelsDevPricing(), {});

modelsDev.saveModelsDevPricing(pricing);
const afterSave = modelsDev.getModelsDevPricing();
assert.notEqual(afterSave, first, "save must invalidate the memo");
assert.equal(afterSave.openai["gpt-4o"].input, 2.5);
});

test("modelsDev capabilities helpers create the table, persist rows, filter by provider/model, and expose context limits", async () => {
const modelsDev = await importFresh("capabilities-storage");
const capabilities = modelsDev.transformModelsDevToCapabilities(MOCK_MODELS_DEV_DATA);
Expand Down
Loading