Skip to content

Commit 2ebdb70

Browse files
author
Xinwei Gao
committed
fix(catalog): distinguish cache eviction from authority changes
1 parent f5f0f43 commit 2ebdb70

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎src/codex/catalog/sync.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,7 +466,7 @@ export function resetCatalogRuntimeStateForTests(): void {
466466
comboUnrestorableShadowWarnings.clear();
467467
accountSelectorShadowCollisionWarnings.clear();
468468
clearLastComboCatalogOmissions();
469-
clearModelCache();
469+
clearModelCache(undefined, "eviction");
470470
clearGatherRoutedModelsInflight();
471471
}
472472

‎src/codex/model-cache.ts‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ export type ProviderModelDiscoveryFailure = ProviderModelDiscoveryStatus extends
4242
: never
4343
: never;
4444

45+
/** Whether clearing cache rows also revokes in-flight discovery authority. */
46+
export type ModelCacheClearReason = "authority" | "eviction";
47+
4548
const cache = new Map<string, CacheEntry>();
4649
let globalCacheGeneration = 0;
4750
const providerCacheGenerations = new Map<string, number>();
@@ -194,15 +197,21 @@ export function setCached(
194197
}
195198

196199
/** Drop one provider's cache (or all) so the next resolve forces a live re-fetch. */
197-
export function clearModelCache(provider?: string): void {
200+
export function clearModelCache(
201+
provider?: string,
202+
reason: ModelCacheClearReason = "authority",
203+
): void {
204+
const revokesInFlightDiscovery = reason === "authority";
198205
if (provider) {
199-
providerCacheGenerations.set(provider, (providerCacheGenerations.get(provider) ?? 0) + 1);
206+
if (revokesInFlightDiscovery) {
207+
providerCacheGenerations.set(provider, (providerCacheGenerations.get(provider) ?? 0) + 1);
208+
}
200209
deleteCachedProvider(provider);
201210
failureAt.delete(provider);
202211
discoveryStatus.delete(provider);
203212
liveModelCounts.delete(provider);
204213
} else {
205-
globalCacheGeneration += 1;
214+
if (revokesInFlightDiscovery) globalCacheGeneration += 1;
206215
cache.clear();
207216
cacheBytes = 0;
208217
oldestCachedProvider = undefined;

‎tests/helpers/provider-registry-discovery.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export async function withRegistryDiscovery<T>(
1616
if (!entry) throw new Error(`missing ${providerId} registry entry`);
1717
const originalDiscovery = entry.modelDiscovery;
1818
const originalPreserveCustomDestination = entry.preserveCustomDestination;
19-
clearModelCache(providerId);
19+
clearModelCache(providerId, "eviction");
2020
entry.modelDiscovery = spec;
2121
if (overrides.preserveCustomDestination !== undefined) {
2222
entry.preserveCustomDestination = overrides.preserveCustomDestination;
@@ -28,6 +28,6 @@ export async function withRegistryDiscovery<T>(
2828
else entry.modelDiscovery = originalDiscovery;
2929
if (originalPreserveCustomDestination === undefined) delete entry.preserveCustomDestination;
3030
else entry.preserveCustomDestination = originalPreserveCustomDestination;
31-
clearModelCache(providerId);
31+
clearModelCache(providerId, "eviction");
3232
}
3333
}

‎tests/model-cache.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,30 @@ const provider = "removed-provider-generation";
1212
afterEach(() => clearModelCache(provider));
1313

1414
describe("model-cache provider reconciliation", () => {
15+
test.each([
16+
["provider", () => clearModelCache(provider, "eviction")],
17+
["global", () => clearModelCache(undefined, "eviction")],
18+
])("%s eviction keeps an in-flight discovery authorized", (_scope, evict) => {
19+
const captured = captureModelCacheGeneration(provider);
20+
21+
evict();
22+
23+
expect(setCached(provider, [{ provider, id: "late-model" }], Date.now(), captured)).toBe(true);
24+
expect(getStaleCached(provider)).toEqual([{ provider, id: "late-model" }]);
25+
});
26+
27+
test.each([
28+
["provider", () => clearModelCache(provider)],
29+
["global", () => clearModelCache()],
30+
])("%s authority change rejects an in-flight discovery", (_scope, revokeAuthority) => {
31+
const captured = captureModelCacheGeneration(provider);
32+
33+
revokeAuthority();
34+
35+
expect(setCached(provider, [{ provider, id: "late-model" }], Date.now(), captured)).toBe(false);
36+
expect(getStaleCached(provider)).toBeNull();
37+
});
38+
1539
test("rejects an in-flight write for a provider removed before it has a cache entry", () => {
1640
const captured = captureModelCacheGeneration(provider);
1741

0 commit comments

Comments
 (0)