refactor(insights): drop the readable cached-text column

Every write has gone to insights_cached_text_encrypted since 0411, and
the boot-time free-text backfill sealed the rows written before it.
Migration 0430 drops insights_cached_text and the backfill index that
served it. A host that skipped the intervening releases loses only a
cache of model output; the next warm writes a fresh, sealed payload.

The cipher module now reads and writes the sealed column only, the
backfill job loses its insights step, and consent withdrawal, the wipe
plan, the backup plan and the session projection follow the schema.
Backups never carried the column, and restore ignores unknown account
settings; a round-trip test now proves an old backup naming the field
still restores cleanly. The access guard pins exact counts for where
the sealed name may appear. Fixtures, the demo seed and the demo
migration script seed the sealed column instead of plaintext.
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 19:24:21 +02:00
1 parent 45da8ae971
commit 9941db1549
49 files changed
+381 -497

No files matched your search

@@ -24,6 +24,7 @@ import {
type StateName,
} from "./ai-optional-fixtures";
import { getPrismaClient } from "./setup";
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
vi.mock("next/headers", async () => {
const { cookieJar, headerJar } = await import("./mock-next-headers");
@@ -172,9 +173,11 @@ describe("insights/generate refuses by the briefing capability", () => {
data: {
// The read resolves (never calls) the chain, so the key must decrypt.
aiAnthropicKeyEncrypted: encrypt("sk-ant-integration"),
insightsCachedText: JSON.stringify({
dailyBriefing: { paragraph: "Held text.", keyFindings: [] },
}),
insightsCachedTextEncrypted: sealInsightsCachedText(
JSON.stringify({
dailyBriefing: { paragraph: "Held text.", keyFindings: [] },
}),
),
insightsCachedAt: new Date(),
insightsCachedLocale: "en",
},
@@ -23,6 +23,7 @@ import {
type StateName,
} from "./ai-optional-fixtures";
import { getPrismaClient } from "./setup";
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
vi.mock("next/headers", async () => {
const { cookieJar, headerJar } = await import("./mock-next-headers");
@@ -94,10 +95,12 @@ async function seed(recordId: string): Promise<void> {
where: { id: recordId },
data: {
// Generated now: only text written today is served as today's read.
insightsCachedText: JSON.stringify({
...BRIEFING,
briefingGeneratedAt: new Date().toISOString(),
}),
insightsCachedTextEncrypted: sealInsightsCachedText(
JSON.stringify({
...BRIEFING,
briefingGeneratedAt: new Date().toISOString(),
}),
),
insightsCachedAt: new Date(),
insightsCachedLocale: "en",
},
@@ -13,6 +13,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { cookieJar, headerJar } from "./mock-next-headers";
import { getPrismaClient, truncateAllTables } from "./setup";
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
vi.mock("next/headers", async () => {
const { cookieJar, headerJar } = await import("./mock-next-headers");
@@ -67,10 +68,12 @@ async function seedUserWithCachedBriefing(cachedLocale: string | null) {
locale: "de",
insightsCachedAt: new Date(),
// Generated now: only text written today is served as today's read.
insightsCachedText: JSON.stringify({
dailyBriefing: BRIEFING,
briefingGeneratedAt: new Date().toISOString(),
}),
insightsCachedTextEncrypted: sealInsightsCachedText(
JSON.stringify({
dailyBriefing: BRIEFING,
briefingGeneratedAt: new Date().toISOString(),
}),
),
insightsCachedLocale: cachedLocale,
// The briefing is shown only while its AI capability is available, so
// the account has its own provider (presence only) and a receipt.
@@ -20,6 +20,7 @@ import {
purgeRegenerableAiText,
withdrawConsent,
} from "@/lib/consent/withdrawal";
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
const BYTES = Buffer.from("ciphertext-stand-in", "utf8");
@@ -33,7 +34,9 @@ async function seed(username: string, kinds: string[]) {
data: {
username,
email: `${username}@example.test`,
insightsCachedText: JSON.stringify({ briefing: "model prose" }),
insightsCachedTextEncrypted: sealInsightsCachedText(
JSON.stringify({ briefing: "model prose" }),
),
insightsCachedAt: new Date(),
insightsSnapshotHash: "hash",
},
@@ -153,7 +156,7 @@ async function snapshot(userId: string) {
prisma.user.findUniqueOrThrow({
where: { id: userId },
select: {
insightsCachedText: true,
insightsCachedTextEncrypted: true,
insightsCachedAt: true,
insightsSnapshotHash: true,
},
@@ -207,7 +210,7 @@ describe("withdrawing the analysis consent", () => {
const after = await snapshot(user.id);
expect(after.user).toEqual({
insightsCachedText: null,
insightsCachedTextEncrypted: null,
insightsCachedAt: null,
insightsSnapshotHash: null,
});
@@ -30,6 +30,7 @@ import {
runFreeTextEncryptionBackfillForUser,
} from "@/lib/jobs/free-text-encryption-backfill";
import {
INSIGHTS_CACHE_SELECT,
insightsCacheWrite,
openInsightsCachedText,
} from "@/lib/insights/cached-text-cipher";
@@ -498,73 +499,31 @@ describe("free-text encryption backfill (real Postgres)", () => {
expect(second.sent).toEqual([]);
});
it("seals the cached insights payload, keeps updatedAt, and converges", async () => {
it("has no readable insights cache left after migration 0430", async () => {
const prisma = getPrismaClient();
await Promise.all([seedUser("ic-a"), seedUser("ic-b"), seedUser("ic-c")]);
const payload = JSON.stringify({
dailyBriefing: { paragraph: "Pressure eased this week." },
});
// ic-a: written by the previous release, in the clear.
const columns = await prisma.$queryRaw<{ column_name: string }[]>`
SELECT column_name FROM information_schema.columns
WHERE table_name = 'users' AND column_name LIKE 'insights_cached_text%'
ORDER BY column_name`;
expect(columns.map((c) => c.column_name)).toEqual([
"insights_cached_text_encrypted",
]);
const indexes = await prisma.$queryRaw<{ indexname: string }[]>`
SELECT indexname FROM pg_indexes
WHERE indexname = 'users_insights_cached_text_backfill_idx'`;
expect(indexes).toEqual([]);
// The sealed column still round-trips through the one module.
await seedUser("ic-a");
await prisma.user.update({
where: { id: "ic-a" },
data: { insightsCachedText: payload },
});
// ic-b: written the current way.
await prisma.user.update({
where: { id: "ic-b" },
data: insightsCacheWrite("current"),
});
const before = await prisma.user.findUniqueOrThrow({
const row = await prisma.user.findUniqueOrThrow({
where: { id: "ic-a" },
select: { updatedAt: true },
select: INSIGHTS_CACHE_SELECT,
});
const { boss, sent } = makeCapturingBoss();
setGlobalBoss(boss as never);
const discovered = await enqueueBootTimeFreeTextEncryptionBackfill();
expect(discovered.error).toBeNull();
expect(sent.map((s) => s.userId)).toEqual(["ic-a"]);
const summary = await runFreeTextEncryptionBackfillForUser("ic-a");
expect(summary.insightsCachesMigrated).toBe(1);
const rowA = await prisma.user.findUniqueOrThrow({
where: { id: "ic-a" },
select: {
insightsCachedText: true,
insightsCachedTextEncrypted: true,
updatedAt: true,
},
});
expect(rowA.insightsCachedText).toBeNull();
expect(rowA.insightsCachedTextEncrypted).not.toBeNull();
expect(openInsightsCachedText(rowA)).toBe(payload);
expect(rowA.updatedAt).toEqual(before.updatedAt);
const rowB = await prisma.user.findUniqueOrThrow({
where: { id: "ic-b" },
select: { insightsCachedText: true, insightsCachedTextEncrypted: true },
});
expect(openInsightsCachedText(rowB)).toBe("current");
const again = await runFreeTextEncryptionBackfillForUser("ic-a");
expect(again.insightsCachesMigrated).toBe(0);
const second = makeCapturingBoss();
setGlobalBoss(second.boss as never);
await enqueueBootTimeFreeTextEncryptionBackfill();
expect(second.sent).toEqual([]);
});
it("has the partial index migration 0411 adds", async () => {
const indexes = await getPrismaClient().$queryRaw<
{ indexname: string; indexdef: string }[]
>`
SELECT indexname, indexdef FROM pg_indexes
WHERE indexname = 'users_insights_cached_text_backfill_idx'`;
expect(indexes).toHaveLength(1);
expect(indexes[0].indexdef).toContain(
"WHERE (insights_cached_text IS NOT NULL)",
);
expect(openInsightsCachedText(row)).toBe("current");
});
it("has the partial indexes migration 0361 adds", async () => {
@@ -288,7 +288,7 @@ describe("POST /api/insights/generate — chain fallback", () => {
const prisma = getPrismaClient();
await prisma.user.update({
where: { username: "chain-user" },
data: { insightsCachedAt: null, insightsCachedText: null },
data: { insightsCachedAt: null, insightsCachedTextEncrypted: null },
});
// Second call: cache should reorder so Anthropic is the first
+4 -1
View File
@@ -33,6 +33,7 @@ import { NextRequest } from "next/server";
import { cookieJar, headerJar } from "./mock-next-headers";
import { getPrismaClient, truncateAllTables, switchSessionTo } from "./setup";
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
vi.mock("next/headers", async () => {
const { cookieJar, headerJar } = await import("./mock-next-headers");
@@ -292,7 +293,9 @@ frontDoorRead("GET /api/daily/digest", {
await getPrismaClient().user.update({
where: { id: userId },
data: {
insightsCachedText: cachedBriefing(marker),
insightsCachedTextEncrypted: sealInsightsCachedText(
cachedBriefing(marker),
),
insightsCachedAt: new Date(),
...ownProvider(),
},