Skip to content

Commit fa2b97d

Browse files
committed
fix: record pricing refresh health failures
1 parent 02d3fe3 commit fa2b97d

3 files changed

Lines changed: 85 additions & 2 deletions

File tree

‎src/gateway/model-pricing-cache-state.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export type CachedModelPricing = {
2020
tieredPricing?: CachedPricingTier[];
2121
};
2222

23-
export type GatewayModelPricingHealthSource = "openrouter" | "litellm" | "bootstrap";
23+
export type GatewayModelPricingHealthSource = "openrouter" | "litellm" | "bootstrap" | "refresh";
2424

2525
export type GatewayModelPricingHealth = {
2626
state: "ok" | "degraded" | "disabled";

‎src/gateway/model-pricing-cache.test.ts‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,84 @@ describe("model-pricing-cache", () => {
456456
});
457457
});
458458

459+
it("records and clears scheduled refresh rejections for health surfaces", async () => {
460+
vi.useFakeTimers();
461+
try {
462+
const manifestRegistry: PluginManifestRegistry = { diagnostics: [], plugins: [] };
463+
let failManifestRead = false;
464+
const pluginMetadataSnapshot = {
465+
index: { plugins: [] } as never,
466+
get manifestRegistry() {
467+
if (failManifestRead) {
468+
throw new Error("manifest metadata failed");
469+
}
470+
return manifestRegistry;
471+
},
472+
};
473+
const config = {
474+
agents: {
475+
defaults: {
476+
model: { primary: "custom/gpt-remote" },
477+
},
478+
},
479+
models: {
480+
providers: {
481+
custom: {
482+
baseUrl: "https://models.example/v1",
483+
api: "openai-completions",
484+
models: [{ id: "gpt-remote" }],
485+
},
486+
},
487+
},
488+
} as unknown as OpenClawConfig;
489+
const fetchImpl = withFetchPreconnect(async (input: RequestInfo | URL) => {
490+
const url =
491+
typeof input === "string" ? input : input instanceof URL ? input.href : input.url;
492+
return new Response(JSON.stringify(url.includes("openrouter.ai") ? { data: [] } : {}), {
493+
status: 200,
494+
headers: { "Content-Type": "application/json" },
495+
});
496+
});
497+
498+
await refreshGatewayModelPricingCache({
499+
config,
500+
fetchImpl,
501+
pluginMetadataSnapshot,
502+
});
503+
expect(getGatewayModelPricingHealth()).toEqual({
504+
state: "ok",
505+
sources: [],
506+
});
507+
508+
failManifestRead = true;
509+
await vi.runOnlyPendingTimersAsync();
510+
511+
expect(getGatewayModelPricingHealth()).toMatchObject({
512+
state: "degraded",
513+
sources: [
514+
{
515+
source: "refresh",
516+
state: "degraded",
517+
detail: "pricing refresh failed: Error: manifest metadata failed",
518+
},
519+
],
520+
});
521+
522+
failManifestRead = false;
523+
await refreshGatewayModelPricingCache({
524+
config,
525+
fetchImpl,
526+
pluginMetadataSnapshot,
527+
});
528+
expect(getGatewayModelPricingHealth()).toEqual({
529+
state: "ok",
530+
sources: [],
531+
});
532+
} finally {
533+
vi.useRealTimers();
534+
}
535+
});
536+
459537
it("seeds pricing from explicit configured model cost without external catalog fetches", async () => {
460538
const config = {
461539
agents: {

‎src/gateway/model-pricing-cache.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,11 @@ function scheduleRefresh(
11251125
return;
11261126
}
11271127
void refreshGatewayModelPricingCache(params).catch((error: unknown) => {
1128-
log.warn(`pricing refresh failed: ${String(error)}`);
1128+
const message = `pricing refresh failed: ${String(error)}`;
1129+
log.warn(message);
1130+
if (!params.signal?.aborted) {
1131+
recordGatewayModelPricingSourceFailure("refresh", message);
1132+
}
11291133
});
11301134
}, CACHE_TTL_MS);
11311135
refreshTimer.unref?.();
@@ -1340,6 +1344,7 @@ export async function refreshGatewayModelPricingCache(
13401344
return;
13411345
}
13421346
clearGatewayModelPricingSourceFailure("bootstrap");
1347+
clearGatewayModelPricingSourceFailure("refresh");
13431348
replaceGatewayModelPricingCache(nextPricing);
13441349
scheduleRefresh({ ...params, fetchImpl });
13451350
})();

0 commit comments

Comments
 (0)