Tag the briefing cache with its language and refuse a foreign one

The comprehensive-briefing cache (User.insightsCachedText) is one slot
per account with no record of the language it was written in. Every
reader served whatever was there, so a German-language account whose
nightly warm had written English kept seeing English on the dashboard
lead and the Today briefing. The request path only replaced it once the
row was a day old and the provider answered, which with a provider at
its usage limit was never.

Add User.insightsCachedLocale (migration 0335). Both writers store the
locale they generated in; the timestamp-only refresh on an unchanged
snapshot tags the row too, since the fingerprint covers the generation
locale. Every reset that nulls the text nulls the tag.

The snapshot briefing lift and GET /api/insights/generate treat a tag
that differs from the reader's locale as an empty cache: no prose, no
timestamp, the preparing / no-provider state, and the out-of-band warm
enqueued in the reader's locale through the existing path. The
generator's own short-circuit and the POST short-circuit refuse to call
a foreign-language row fresh. A NULL tag (rows written before this
change) is served as before until the next write tags it. The Today
digest derives from the snapshot and follows.

Covered by unit tests on the lift, the route and the generator, and an
integration test that writes the row through Prisma and reads the
snapshot for a reader in another language.
This commit is contained in:
Marc-André Bombeck committed 2026-09-02 10:44:03 +02:00
1 parent d5cf82fb6e
commit ba4e34f09e
21 files changed
+404 -26

No files matched your search

@@ -0,0 +1,4 @@
-- Tag the comprehensive-briefing cache with the language it was written in.
-- One slot per user; readers in another language treat a differing tag as an
-- empty cache. NULL on existing rows: treated as matching until the next write.
ALTER TABLE "users" ADD COLUMN "insights_cached_locale" TEXT;
+7
View File
@@ -119,6 +119,13 @@ model User {
insightsPrivacyMode String @default("aggregated") @map("insights_privacy_mode") // "aggregated" | "raw"
insightsCachedAt DateTime? @map("insights_cached_at")
insightsCachedText String? @map("insights_cached_text") // cached JSON output
// Language the cached text was generated in. The cache is one slot per
// user, so a reader in another language must not be served it: the
// snapshot briefing lift and the advisor read treat a tag that differs
// from the reader's locale as an empty cache and re-warm in the reader's.
// NULL on rows written before the tag existed (treated as matching; the
// next write tags it).
insightsCachedLocale String? @map("insights_cached_locale")
// v1.16.8 — SHA-256 fingerprint of the compacted feature snapshot the
// cached comprehensive text was generated from. Nightly / forced / lazy
// regenerations compare the fresh snapshot's hash against this and skip