Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(catalog): a cached boot re-apply must not lift an upstream model retirement (#634) (#1421)

suantea (asuan_suan)469ff8d82721
diff --git a/server/src/__tests__/services/catalog-sync.test.ts b/server/src/__tests__/services/catalog-sync.test.ts
index 1789e806..1fe563db 100644
--- a/server/src/__tests__/services/catalog-sync.test.ts
+++ b/server/src/__tests__/services/catalog-sync.test.ts
@@ -2,7 +2,12 @@ import { describe, it, expect, beforeAll } from 'vitest';
import { initDb, getDb, setSetting, getSetting } from '../../db/index.js';
import { applyCatalog, reapplyCachedCatalog, MIN_CATALOG_VERSION } from '../../services/catalog-sync.js';
import { runMigrationsSync } from '../../db/migrate/runner.js';
-import { recordCatalogModelTombstone, upsertModelOverrides } from '../../services/model-state.js';
+import {
+ getCatalogModelTombstone,
+ recordCatalogModelTombstone,
+ retireCatalogModelUpstream,
+ upsertModelOverrides,
+} from '../../services/model-state.js';
// applyCatalog is the write path between the published catalog and the live
// router DB. These tests lock its contract: catalog metadata always wins, the
@@ -389,6 +394,42 @@ describe('reapplyCachedCatalog', () => {
).toBeUndefined();
});
+ it('keeps an upstream retirement across a cached re-apply, and lifts it only for a newer catalog', () => {
+ // The re-apply replays the snapshot the retirement already post-dates, so
+ // it must not read that snapshot as a newer opinion — otherwise every 410
+ // (#634) is undone by the next restart. A genuinely newer catalog still
+ // wins, which is what the lift exists for.
+ const models = existingAsCatalogModels();
+ models.push(baseModel({ modelId: 'replay-retired', displayName: 'Replay Retired' }));
+ const catalog = catalogOf(models);
+ applyCatalog(getDb(), catalog);
+ cacheCatalog(catalog);
+
+ const row = getDb()
+ .prepare("SELECT id FROM models WHERE platform = 'groq' AND model_id = 'replay-retired'")
+ .get() as { id: number };
+ expect(retireCatalogModelUpstream(getDb(), row.id, 'groq', 'replay-retired', 'HTTP 410 gone')).toBe(true);
+ const chainOff = () =>
+ (getDb().prepare('SELECT enabled FROM fallback_config WHERE model_db_id = ?').get(row.id) as { enabled: number }).enabled;
+ expect(chainOff()).toBe(0);
+
+ expect(reapplyCachedCatalog().reapplied).toBe(true);
+ expect(getCatalogModelTombstone(getDb(), 'chat', 'groq', 'replay-retired')?.source).toBe('upstream_eol');
+ expect(chainOff()).toBe(0);
+ // Retirement disables the chain entries, never the row: the dashboard keeps
+ // showing it as retired upstream rather than losing it.
+ expect(
+ (getDb().prepare('SELECT enabled FROM models WHERE id = ?').get(row.id) as { enabled: number }).enabled,
+ ).toBe(1);
+
+ // Same model, still listed enabled, but a catalog the install has not seen.
+ const newer = catalogOf(existingAsCatalogModels());
+ newer.version = '2099.02.02';
+ applyCatalog(getDb(), newer);
+ expect(getCatalogModelTombstone(getDb(), 'chat', 'groq', 'replay-retired')).toBeUndefined();
+ expect(chainOff()).toBe(1);
+ });
+
it('clears the applied version when an older install has no cached document', () => {
getDb().prepare("DELETE FROM settings WHERE key = 'catalog_applied_json'").run();
setSetting('catalog_applied_version', '2099.01.01');
diff --git a/server/src/services/catalog-sync.ts b/server/src/services/catalog-sync.ts
index 97e2573b..2dab93e2 100644
--- a/server/src/services/catalog-sync.ts
+++ b/server/src/services/catalog-sync.ts
@@ -331,8 +331,25 @@ export function applyCatalog(db: Db, catalog: Catalog): NonNullable<SyncResult['
return db.transaction(() => applyCatalogInner(db, catalog))();
}
+/**
+ * True when this document can say anything we have not already acted on.
+ *
+ * `reapplyCachedCatalog` deliberately re-applies the very document that is
+ * already marked applied — its job is to undo the baseline drift that boot
+ * migrations reintroduce. A replay therefore re-presents stale data as if it
+ * were a fresh verdict. The fetched path reaches `applyCatalog` only behind
+ * `syncCatalog`'s own same-as-applied guard, so for that path the check is
+ * always true and nothing changes.
+ */
+function catalogBringsNewEvidence(catalog: Catalog): boolean {
+ return getSetting(SETTING_APPLIED_VERSION) !== catalog.version || getSetting(SETTING_APPLIED_TIER) !== catalog.tier;
+}
+
function applyCatalogInner(db: Db, catalog: Catalog): NonNullable<SyncResult['counts']> {
const counts = { updated: 0, inserted: 0, removed: 0, skippedUnknownPlatform: 0, quirks: 0 };
+ // Read once: the applied-version settings are only written after this
+ // transaction returns, so they cannot change underneath the loop.
+ const newEvidence = catalogBringsNewEvidence(catalog);
const selectModel = db.prepare('SELECT id, enabled, source FROM models WHERE platform = ? AND model_id = ?');
const updateModel = db.prepare(`
@@ -448,7 +465,11 @@ function applyCatalogInner(db: Db, catalog: Catalog): NonNullable<SyncResult['co
// A model auto-retired from a 410/end-of-life response (#634) is disabled,
// not deleted. A catalog that STILL lists it — and lists it enabled — is
// newer evidence than that one provider response, so lift the retirement.
- if (m.enabled) reinstateUpstreamRetiredCatalogModel(db, m.platform, m.modelId);
+ // Only for a catalog we have not applied yet: `reapplyCachedCatalog`
+ // replays the snapshot the retirement already came after, so replaying it
+ // back would clear every retirement on the next boot, however fresh the
+ // provider's 410 was.
+ if (m.enabled && newEvidence) reinstateUpstreamRetiredCatalogModel(db, m.platform, m.modelId);
inCatalog.add(`${m.platform}:${m.modelId}`);
const row = selectModel.get(m.platform, m.modelId) as