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(retention): sweep expired idempotency claims and cache rows (#1529)
tashdroid7cb839a3578c
diff --git a/server/src/__tests__/services/request-retention.test.ts b/server/src/__tests__/services/request-retention.test.tsindex 46398933..750d17e3 100644--- a/server/src/__tests__/services/request-retention.test.ts+++ b/server/src/__tests__/services/request-retention.test.ts@@ -123,4 +123,30 @@ describe('request analytics retention', () => {const remainingAfter = db.prepare(`SELECT hour FROM request_hourly ORDER BY hour`).all() as Array<{ hour: string }>;expect(remainingAfter.map(r => r.hour)).toEqual(['2026-05-15 00:00:00', '2026-05-31 00:00:00']);});++ it('sweeps expired idempotency claims and cache rows on the prune pass', () => {+ // Both tables ship with the migration set; seed one expired and one live+ // row in each so a prune must keep the live half.+ const db = getDb();+ const now = new Date('2026-05-31T00:00:00Z').getTime();+ const claim = db.prepare(`+ INSERT INTO idempotency_claims (key_hash, request_fingerprint, response_status, response_body, created_at_ms, expires_at_ms)+ VALUES (?, 'fp', 200, '{}', ?, ?)+ `);+ claim.run('hash-expired', now - 48 * 3600_000, now - 3600_000);+ claim.run('hash-live', now - 3600_000, now + 3600_000);+ const cache = db.prepare(`+ INSERT INTO response_cache (cache_key, body_json, platform, model_id, prompt_tokens, completion_tokens, created_at_ms, expires_at_ms)+ VALUES (?, '{}', 'groq', 'grok-test', 1, 1, ?, ?)+ `);+ cache.run('cache-expired', now - 48 * 3600_000, now - 1000);+ cache.run('cache-live', now - 1000, now + 60_000);++ const result = pruneRequestAnalytics({ db, force: true, now: new Date(now) });+ expect(result.deleted).toBe(2);+ expect((db.prepare("SELECT COUNT(*) AS n FROM idempotency_claims WHERE key_hash = 'hash-expired'").get() as { n: number }).n).toBe(0);+ expect((db.prepare("SELECT COUNT(*) AS n FROM idempotency_claims WHERE key_hash = 'hash-live'").get() as { n: number }).n).toBe(1);+ expect((db.prepare("SELECT COUNT(*) AS n FROM response_cache WHERE cache_key = 'cache-expired'").get() as { n: number }).n).toBe(0);+ expect((db.prepare("SELECT COUNT(*) AS n FROM response_cache WHERE cache_key = 'cache-live'").get() as { n: number }).n).toBe(1);+ });});diff --git a/server/src/services/request-retention.ts b/server/src/services/request-retention.tsindex d767bb6e..1c0cebac 100644--- a/server/src/services/request-retention.ts+++ b/server/src/services/request-retention.ts@@ -199,6 +199,35 @@ export function pruneServerLogs(db: RetentionDb, nowMs: number): number {return deleted;}+/**+ * Sweep expired rows from the two short-lived replay stores.+ *+ * Both tables are write-once with a TTL that readers already filter on+ * (`expires_at_ms > now`), and both are indexed on `expires_at_ms`. Neither+ * had a sweeper: `storeIdempotencyResult` only deletes expired rows for the+ * SAME key hash it is about to overwrite, so a claim whose hash never repeats+ * — the common case, since clients send a fresh UUID per request — sat in the+ * DB forever with its full response JSON. A busy install replaying+ * Idempotency-Key traffic grew this table monotonically. `response_cache`+ * swept expired rows only at startup, so a long-running server accumulated+ * them between restarts too.+ *+ * The deletes are plain index-range walks over `expires_at_ms`, so cost scales+ * with what is actually expired. Guarded against the tables being absent the+ * same way every other prune here is.+ */+export function pruneExpiredClaims(db: RetentionDb, nowMs: number): number {+ let deleted = 0;+ for (const table of ['idempotency_claims', 'response_cache'] as const) {+ const present = db+ .prepare("SELECT name FROM sqlite_master WHERE type='table' AND name = ?")+ .get(table);+ if (!present) continue;+ deleted += db.prepare(`DELETE FROM ${table} WHERE expires_at_ms <= ?`).run(nowMs).changes;+ }+ return deleted;+}+export function pruneRequestAnalytics(options: {db?: RetentionDb;force?: boolean;@@ -236,6 +265,10 @@ export function pruneRequestAnalytics(options: {// Persisted server logs, on the same 60s gate as the requests prune above.deleted += pruneServerLogs(db, nowMs);+ // Expired idempotency claims and cache rows, on the same 60s gate. Both are+ // TTL'd replay stores with no reader past expiry and no other sweeper.+ deleted += pruneExpiredClaims(db, nowMs);+// Hourly aggregate prune (gated once per day). The UI's widest window is// 30d, so we keep at most 30 days of hourly buckets (~720 rows). The table// is the source of truth for analytics totals — never prune more aggressively