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(keys): sweep key-bound model rows when a standard key is deleted (#1522)

Johandd2660800086
diff --git a/server/src/__tests__/routes/keys.test.ts b/server/src/__tests__/routes/keys.test.ts
index 41fb4ed5..c9eb5cf9 100644
--- a/server/src/__tests__/routes/keys.test.ts
+++ b/server/src/__tests__/routes/keys.test.ts
@@ -252,6 +252,40 @@ describe('Keys API', () => {
expect(status).toBe(404);
});
+ it('DELETE /api/keys/:id sweeps key-bound rows on standard platforms (#1516)', async () => {
+ const { body: created } = await request(app, 'POST', '/api/keys', {
+ platform: 'groq',
+ key: 'gsk_test123456789',
+ });
+ const db = getDb();
+ // A key-bound chat row plus its fallback row, embedding row and media row.
+ // Shared catalog rows (key_id NULL) must survive the delete.
+ const chat = db.prepare(`
+ INSERT INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, key_id, enabled, source)
+ VALUES ('groq', 'orphan-chat-1516', 'Orphan Chat', 50, 50, 'Large', ?, 1, 'user')
+ `).run(created.id);
+ db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, 9999, 1)').run(chat.lastInsertRowid);
+ db.prepare(`
+ INSERT INTO embedding_models (family, platform, model_id, display_name, dimensions, priority, enabled, quota_label, key_id)
+ VALUES ('fam-1516', 'groq', 'orphan-embed-1516', 'Orphan Embed', 768, 1, 1, 'test', ?)
+ `).run(created.id);
+ db.prepare(`
+ INSERT INTO media_models (platform, model_id, display_name, modality, priority, enabled, quota_label, key_id)
+ VALUES ('groq', 'orphan-tts-1516', 'Orphan TTS', 'audio', 1, 1, 'test', ?)
+ `).run(created.id);
+ const sharedBefore = (db.prepare("SELECT COUNT(*) AS n FROM models WHERE platform = 'groq' AND key_id IS NULL").get() as { n: number }).n;
+
+ const { status } = await request(app, 'DELETE', `/api/keys/${created.id}`);
+ expect(status).toBe(200);
+
+ expect(db.prepare('SELECT COUNT(*) AS n FROM models WHERE key_id = ?').get(created.id)).toEqual({ n: 0 });
+ expect(db.prepare('SELECT COUNT(*) AS n FROM embedding_models WHERE key_id = ?').get(created.id)).toEqual({ n: 0 });
+ expect(db.prepare('SELECT COUNT(*) AS n FROM media_models WHERE key_id = ?').get(created.id)).toEqual({ n: 0 });
+ expect(db.prepare('SELECT COUNT(*) AS n FROM fallback_config WHERE model_db_id = ?').get(chat.lastInsertRowid)).toEqual({ n: 0 });
+ // Shared catalog rows are untouched.
+ expect(db.prepare("SELECT COUNT(*) AS n FROM models WHERE platform = 'groq' AND key_id IS NULL").get()).toEqual({ n: sharedBefore });
+ });
+
it('PATCH /api/keys/:id updates label', async () => {
const { body: created } = await request(app, 'POST', '/api/keys', {
platform: 'groq',
diff --git a/server/src/__tests__/services/catalog-sync.test.ts b/server/src/__tests__/services/catalog-sync.test.ts
index 49c1d0a6..722c6049 100644
--- a/server/src/__tests__/services/catalog-sync.test.ts
+++ b/server/src/__tests__/services/catalog-sync.test.ts
@@ -177,6 +177,27 @@ describe('applyCatalog', () => {
expect(getDb().prepare('SELECT id FROM fallback_config WHERE model_db_id = ?').get(before.id)).toBeUndefined();
});
+ it('sweeps rows pinned to keys that no longer exist (#1516)', () => {
+ const db = getDb();
+ // 999999 names no api_keys row: exactly the orphan the delete cascade
+ // used to leave behind on standard platforms.
+ const chat = db.prepare(`
+ INSERT INTO models (platform, model_id, display_name, intelligence_rank, speed_rank, size_label, key_id, enabled, source)
+ VALUES ('groq', 'orphan-sync-chat-1516', 'Orphan Chat', 50, 50, 'Large', 999999, 1, 'user')
+ `).run();
+ db.prepare('INSERT INTO fallback_config (model_db_id, priority, enabled) VALUES (?, 9999, 1)').run(chat.lastInsertRowid);
+ db.prepare(`
+ INSERT INTO embedding_models (family, platform, model_id, display_name, dimensions, priority, enabled, quota_label, key_id)
+ VALUES ('fam-sync-1516', 'groq', 'orphan-sync-embed-1516', 'Orphan Embed', 768, 1, 1, 'test', 999999)
+ `).run();
+
+ const counts = applyCatalog(db, catalogOf(existingAsCatalogModels()));
+ expect(counts.removed).toBeGreaterThanOrEqual(2);
+ expect(db.prepare("SELECT id FROM models WHERE model_id = 'orphan-sync-chat-1516'").get()).toBeUndefined();
+ expect(db.prepare('SELECT id FROM fallback_config WHERE model_db_id = ?').get(chat.lastInsertRowid)).toBeUndefined();
+ expect(db.prepare("SELECT id FROM embedding_models WHERE model_id = 'orphan-sync-embed-1516'").get()).toBeUndefined();
+ });
+
it('never touches custom-provider models', () => {
getDb()
.prepare(
diff --git a/server/src/routes/keys.ts b/server/src/routes/keys.ts
index 4aa42846..d6d99b9c 100644
--- a/server/src/routes/keys.ts
+++ b/server/src/routes/keys.ts
@@ -1738,6 +1738,15 @@ keysRouter.delete('/:id', (req: Request, res: Response) => {
}
}
}
+ // #1516: key-bound rows on ANY platform die with their key. The custom
+ // branch above already removed this endpoint's rows (plus tombstones and
+ // the legacy sweep); this covers key_id-pinned rows on standard
+ // platforms, which the catalog prune (key_id IS NULL only) never touches.
+ // Shared catalog rows (key_id NULL) are untouched.
+ db.prepare('DELETE FROM fallback_config WHERE model_db_id IN (SELECT id FROM models WHERE key_id = ?)').run(id);
+ db.prepare('DELETE FROM models WHERE key_id = ?').run(id);
+ db.prepare('DELETE FROM embedding_models WHERE key_id = ?').run(id);
+ db.prepare('DELETE FROM media_models WHERE key_id = ?').run(id);
});
remove();
diff --git a/server/src/services/catalog-sync.ts b/server/src/services/catalog-sync.ts
index 62eefa8f..63cd7b60 100644
--- a/server/src/services/catalog-sync.ts
+++ b/server/src/services/catalog-sync.ts
@@ -758,6 +758,21 @@ function applyCatalogInner(db: Db, catalog: Catalog): NonNullable<SyncResult['co
counts.removed++;
}
+ // #1516: sweep rows pinned to keys that no longer exist. The DELETE-key
+ // cascade in routes/keys.ts removes these at delete time; this is the
+ // backstop for orphans from older builds or manual DB edits. Shared rows
+ // (key_id NULL) are never touched — only key-bound rows whose key is gone.
+ const orphanChat = db
+ .prepare('SELECT id FROM models WHERE key_id IS NOT NULL AND key_id NOT IN (SELECT id FROM api_keys)')
+ .all() as { id: number }[];
+ for (const o of orphanChat) {
+ deleteFb.run(o.id);
+ deleteModel.run(o.id);
+ counts.removed++;
+ }
+ counts.removed += (db.prepare('DELETE FROM embedding_models WHERE key_id IS NOT NULL AND key_id NOT IN (SELECT id FROM api_keys)').run().changes as number);
+ counts.removed += (db.prepare('DELETE FROM media_models WHERE key_id IS NOT NULL AND key_id NOT IN (SELECT id FROM api_keys)').run().changes as number);
+
// Remove media models the catalog no longer lists (own table, no
// fallback_config). Deliberately an ALLOWLIST of the two modalities that
// `models[]` maintains, not "everything except transcription": video and