From 699b34401bee85283ce77f9f5df5a74b92980783 Mon Sep 17 00:00:00 2001 From: Jan Leon Date: Mon, 29 Jun 2026 15:06:59 +0200 Subject: [PATCH] chore: remove unused semantic cache maintenance exports (#5334) Verified dead-code removal: merged result passes typecheck:core + 194 affected unit tests + ESLint clean. Integrated into release/v3.8.41. Thanks @JxnLexn for the cleanup! --- src/lib/semanticCache.ts | 50 +++++-------------------------- tests/unit/semantic-cache.test.ts | 14 ++++++++- 2 files changed, 20 insertions(+), 44 deletions(-) diff --git a/src/lib/semanticCache.ts b/src/lib/semanticCache.ts index 0410844678..356a76835f 100644 --- a/src/lib/semanticCache.ts +++ b/src/lib/semanticCache.ts @@ -116,7 +116,13 @@ function getMemoryCache() { * @param {string} [apiKeyId] - API key ID for per-key isolation (prevents cross-user cache hits) * @returns {string} hex signature */ -export function generateSignature(model, conversation, temperature = 0, topP = 1, apiKeyId?: string) { +export function generateSignature( + model, + conversation, + temperature = 0, + topP = 1, + apiKeyId?: string +) { const payload = JSON.stringify({ model, messages: normalizeConversation(conversation), @@ -315,48 +321,6 @@ export function invalidateStale(maxAgeMs: number): number { } } -// ── Auto-cleanup timer ── - -let _cleanupTimer: ReturnType | null = null; - -/** - * Start periodic auto-cleanup of expired entries. - * @param {number} intervalMs - Cleanup interval (default: 5 minutes) - */ -export function startAutoCleanup(intervalMs = 300_000): void { - stopAutoCleanup(); - _cleanupTimer = setInterval(() => { - const removed = cleanExpiredEntries(); - if (removed > 0) { - console.log(`[SemanticCache] Auto-cleaned ${removed} expired entries`); - } - }, intervalMs); - if (_cleanupTimer && typeof _cleanupTimer === "object" && "unref" in _cleanupTimer) { - (_cleanupTimer as { unref?: () => void }).unref?.(); - } -} - -/** - * Stop periodic auto-cleanup. - */ -export function stopAutoCleanup(): void { - if (_cleanupTimer) { - clearInterval(_cleanupTimer); - _cleanupTimer = null; - } -} - -export function cleanOldMetrics(retentionDays = 90): number { - try { - const db = getDbInstance(); - const cutoff = new Date(Date.now() - retentionDays * 86400000).toISOString(); - const result = db.prepare("DELETE FROM semantic_cache WHERE created_at < ?").run(cutoff); - return result.changes || 0; - } catch { - return 0; - } -} - /** * Clear all cache entries. */ diff --git a/tests/unit/semantic-cache.test.ts b/tests/unit/semantic-cache.test.ts index 2205594b17..a36394ef95 100644 --- a/tests/unit/semantic-cache.test.ts +++ b/tests/unit/semantic-cache.test.ts @@ -6,7 +6,15 @@ import { isCacheableForWrite, } from "../../src/lib/semanticCache.ts"; +const semanticCachePublicApi = await import("../../src/lib/semanticCache.ts"); + describe("Semantic Cache", () => { + it("public surface excludes unused maintenance timer helpers", () => { + assert.equal("startAutoCleanup" in semanticCachePublicApi, false); + assert.equal("stopAutoCleanup" in semanticCachePublicApi, false); + assert.equal("cleanOldMetrics" in semanticCachePublicApi, false); + }); + describe("generateSignature", () => { it("generates consistent signatures for same inputs", () => { const messages = [{ role: "user", content: "hello" }]; @@ -77,7 +85,11 @@ describe("Semantic Cache", () => { const messages = [{ role: "user", content: "what is 2+2?" }]; const sigKeyA = generateSignature("gpt-4o", messages, 0, 1, "key-id-alice"); const sigKeyB = generateSignature("gpt-4o", messages, 0, 1, "key-id-bob"); - assert.notEqual(sigKeyA, sigKeyB, "different API keys must produce different cache signatures"); + assert.notEqual( + sigKeyA, + sigKeyB, + "different API keys must produce different cache signatures" + ); }); it("generates consistent signatures for same API key ID (#3740)", () => {