You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(backend): semantic cache TTL never expires same-UTC-day entries (ISO vs datetime('now') text compare) #11559
semanticCache.ts writes expires_at in ISO-8601 format (new Date(...).toISOString() → "2026-08-26T14:00:00.000Z"), but reads it back by comparing against SQLite's datetime('now'), which produces a space-separated string ("2026-08-26 14:00:00").
Because expires_at is a TEXT column, the comparison is lexicographic — and at index 10 the stored value has 'T' (0x54) where datetime('now') has ' ' (0x20). 'T' > ' ', so every entry whose UTC calendar date equals "today" compares greater than datetime('now'), regardless of its actual time-of-day expiry. Entries therefore remain "valid" until 00:00 UTC of the day after their true TTL expires — up to ~24 hours stale.
This is a static analysis finding from reading src/lib/semanticCache.ts on the current default branch; I have not run a gateway instance.
Steps to Reproduce
Enable the semantic cache and send any request that gets cached with a short TTL (e.g. a few minutes), well before UTC midnight.
Let the TTL lapse.
Query the cache through the DB-backed path (getCachedResponse) after eviction/restart, or check /api/cache/stats.
Observed statically from source:
Write path (setCachedResponse, ~line 260):
constnow=newDate().toISOString();constexpiresAt=newDate(Date.now()+ttl).toISOString();db.prepare(`INSERT OR REPLACE INTO semantic_cache (... created_at, expires_at) VALUES (?, ?, ?, ?, ?, 0, ?, ?)`)...
Read path (getCachedResponse, ~line 206; same predicate in getCacheStats line 345):
"SELECT response, tokens_saved FROM semantic_cache WHERE signature = ? AND expires_at > datetime('now')"
Expected Behavior
A cached response should be considered expired once expires_at passes, i.e. comparisons should happen between values in the same format (either store space-separated SQLite format, or compare against an ISO string built in JS, or store epoch millis).
Actual Behavior
Same-UTC-day timestamps always satisfy expires_at > datetime('now') due to 'T' > ' ' in text ordering, so entries survive up to ~24h beyond their configured TTL. The in-memory LRU has correct per-entry TTLs, but getCachedResponse re-promotes DB rows into memory on hit (~line 220), so staleness survives eviction/restarts.
Note: other comparisons in the file are consistent-format (e.g. invalidateStale compares against an ISO cutoff) — only the expires_at vs datetime('now') pairs mismatch.
Test Impact
Needs a new unit test
Validation Plan
A unit test that seeds a row with expires_at = (now + small TTL, still today's UTC date), advances a mocked clock past the TTL, and asserts getCachedResponse misses.
node --import tsx --test tests/unit/semanticCache.test.ts (or the repo's equivalent suite for this module).
changed the title [-][BUG] Semantic cache entries stay valid up to ~24h past TTL: ISO-8601 expires_at compared against SQLite datetime('now')[/-][+]fix(backend): semantic cache TTL never expires same-UTC-day entries (ISO vs datetime('now') text compare)[/+]on Aug 25, 2026
Activity
[-][BUG] Semantic cache entries stay valid up to ~24h past TTL: ISO-8601 expires_at compared against SQLite datetime('now')[/-][+]fix(backend): semantic cache TTL never expires same-UTC-day entries (ISO vs datetime('now') text compare)[/+]fix(cache): compare expires_at against an ISO cutoff, not datetime('n…
fix(cache): expire a semantic-cache row on its TTL, not at UTC midnig…
fix(cache): expire a semantic-cache row on its TTL, not at UTC midnig…