mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 23:52:24 +00:00
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:
1 parent
45da8ae971
commit
9941db1549
49 files changed
+381
-497
No files matched your search
@@ -17,8 +17,8 @@ import type { Prisma } from "@/generated/prisma/client";
|
||||
* flags, the small preference documents). Anything else is read by the one
|
||||
* route that needs it, with its own `select`, at the moment it needs it:
|
||||
*
|
||||
* - bytes and large text: `avatarBytes`, `insightsCachedText`,
|
||||
* `insightsCachedTextEncrypted`, `homeLocationEncrypted`,
|
||||
* - bytes and large text: `avatarBytes`, `insightsCachedTextEncrypted`,
|
||||
* `homeLocationEncrypted`,
|
||||
* `environmentAqHistoryJson`, `dawarichPrefsEncrypted`;
|
||||
* - credentials and secrets: `passwordHash`, `totpSecretEncrypted`,
|
||||
* `totpLastStep`, `telegramBotToken`, every `*Encrypted` integration and
|
||||
|
||||
@@ -66,7 +66,7 @@ export async function purgeRegenerableAiText(
|
||||
where: {
|
||||
id: userId,
|
||||
OR: [
|
||||
...INSIGHTS_CACHE_PRESENT_WHERE.OR,
|
||||
INSIGHTS_CACHE_PRESENT_WHERE,
|
||||
{ insightsCachedAt: { not: null } },
|
||||
{ insightsSnapshotHash: { not: null } },
|
||||
],
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
*
|
||||
* A pure, deterministic composer that assembles the day's read from data that
|
||||
* ALREADY EXISTS — the nightly `insight-pregenerate` output lifted read-only
|
||||
* from `User.insightsCachedText`, the dashboard-snapshot ingredients (health
|
||||
* from `User.insightsCachedTextEncrypted`, the dashboard-snapshot ingredients (health
|
||||
* score, meds-today, sleep last-seen), plus two light deterministic reads
|
||||
* (integration status, due Vorsorge reminders). It computes no analytics and
|
||||
* NEVER triggers an AI/provider call — the warm-on-mount ban extends here. The
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
* briefing lift) rides `readDashboardSnapshotCached` — the SAME SWR cell the
|
||||
* dashboard already serves, so the digest never re-runs the snapshot builder
|
||||
* within its TTL and never reaches an AI provider (the briefing is lifted
|
||||
* read-only from `User.insightsCachedText`). Two light deterministic reads add
|
||||
* read-only from `User.insightsCachedTextEncrypted`). Two light deterministic reads add
|
||||
* the rail's non-snapshot inputs: broken integrations and overdue Vorsorge
|
||||
* reminders. Module gating is inherited from the snapshot (already
|
||||
* module-gated at the build layer) plus the resolved module map the item
|
||||
|
||||
@@ -16,6 +16,10 @@ import {
|
||||
AI_AVAILABLE,
|
||||
aiUnavailable,
|
||||
} from "@/__tests__/helpers/ai-capability-fixtures";
|
||||
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so seeding it needs a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
|
||||
const briefingBody = {
|
||||
tiles: {},
|
||||
@@ -35,9 +39,11 @@ vi.mock("@/lib/db", () => ({
|
||||
prisma: {
|
||||
user: {
|
||||
findUnique: async () => ({
|
||||
insightsCachedText: JSON.stringify({
|
||||
briefingGeneratedAt: new Date().toISOString(),
|
||||
}),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({
|
||||
briefingGeneratedAt: new Date().toISOString(),
|
||||
}),
|
||||
),
|
||||
}),
|
||||
},
|
||||
},
|
||||
|
||||
@@ -10,6 +10,10 @@ import {
|
||||
AI_AVAILABLE,
|
||||
aiUnavailable,
|
||||
} from "@/__tests__/helpers/ai-capability-fixtures";
|
||||
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so seeding it needs a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
|
||||
const BRIEFING = {
|
||||
paragraph: "Your pulse is well above its usual level today.",
|
||||
@@ -49,7 +53,7 @@ const buildDashboardSnapshot = vi.fn(async () => current);
|
||||
|
||||
// The cached briefing text is read off its own row, not the session user.
|
||||
const cacheRow = vi.hoisted(() => ({
|
||||
current: null as { insightsCachedText: string | null } | null,
|
||||
current: null as { insightsCachedTextEncrypted: Uint8Array | null } | null,
|
||||
}));
|
||||
vi.mock("@/lib/db", () => ({
|
||||
prisma: { user: { findUnique: async () => cacheRow.current } },
|
||||
@@ -80,10 +84,12 @@ const NOW = new Date("2026-10-05T19:00:00.000Z");
|
||||
|
||||
function user(generatedAt: string | null): SessionUser {
|
||||
cacheRow.current = {
|
||||
insightsCachedText: JSON.stringify({
|
||||
dailyBriefing: BRIEFING,
|
||||
...(generatedAt ? { briefingGeneratedAt: generatedAt } : {}),
|
||||
}),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({
|
||||
dailyBriefing: BRIEFING,
|
||||
...(generatedAt ? { briefingGeneratedAt: generatedAt } : {}),
|
||||
}),
|
||||
),
|
||||
};
|
||||
return {
|
||||
id: "user-briefing-today",
|
||||
|
||||
@@ -27,7 +27,7 @@
|
||||
* inside the same fast window.
|
||||
*
|
||||
* No LLM is reachable from this builder. The briefing is lifted
|
||||
* read-only from `User.insightsCachedText`; a cache miss / stale row
|
||||
* read-only from `User.insightsCachedTextEncrypted`; a cache miss / stale row
|
||||
* yields `briefingState: "preparing"` (with the last good briefing
|
||||
* delivered under `briefingStale: true` when one exists) and the
|
||||
* `insight-pregenerate` cron refills it — or `"no-provider"` when no
|
||||
@@ -943,7 +943,7 @@ export function applyBriefingCapability(
|
||||
}
|
||||
|
||||
/**
|
||||
* Read-only briefing lift. Parses `User.insightsCachedText`, validates
|
||||
* Read-only briefing lift. Parses `User.insightsCachedTextEncrypted`, validates
|
||||
* the `dailyBriefing` block, and reports a four-state. NEVER calls the
|
||||
* provider chain — `hasProvider` is a credential-PRESENCE thunk (two
|
||||
* narrow reads, no decrypt, no network) evaluated only on the
|
||||
|
||||
@@ -400,7 +400,6 @@ export const USER_RESET = {
|
||||
// Derived AI output cached on the row
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedLocale: null,
|
||||
insightsSnapshotHash: null,
|
||||
|
||||
@@ -536,6 +536,38 @@ describe("backup round trip — export, wire schema, restore", () => {
|
||||
expect(res.status).toBe(200);
|
||||
});
|
||||
|
||||
// Files written before v1.44.0 can name the readable insights cache the
|
||||
// release dropped (migration 0430), at the top level or among the account
|
||||
// settings. The column is gone, so the restore must neither fail on the name
|
||||
// nor try to write it.
|
||||
it("restores a file that still names the dropped readable insights cache", async () => {
|
||||
const { payload } = await buildFullBackupPayload(
|
||||
sourceClient() as never,
|
||||
OWNER,
|
||||
{ purpose: "disaster-recovery" },
|
||||
);
|
||||
const legacy: Record<string, unknown> = {
|
||||
...payload,
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
accountSettings: {
|
||||
...((payload.accountSettings as Record<string, unknown> | null) ?? {}),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
},
|
||||
};
|
||||
|
||||
const { res, written } = await roundTrip({}, legacy);
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
for (const w of written) {
|
||||
expect(
|
||||
Object.keys(w.data),
|
||||
`${w.model}.${w.op} must not write the dropped column`,
|
||||
).not.toContain("insightsCachedText");
|
||||
}
|
||||
// The rest of the file still lands.
|
||||
expect(written.some((w) => w.model === "userHealthProfile")).toBe(true);
|
||||
});
|
||||
|
||||
it("writes the durable self-context back with its ciphertext intact", async () => {
|
||||
const { written } = await roundTrip();
|
||||
|
||||
@@ -785,7 +817,7 @@ describe("backup round trip — export, wire schema, restore", () => {
|
||||
(w) =>
|
||||
w.model === "user" &&
|
||||
w.op === "update" &&
|
||||
"insightsCachedText" in w.data,
|
||||
"insightsCachedTextEncrypted" in w.data,
|
||||
);
|
||||
expect(
|
||||
userUpdate,
|
||||
@@ -794,7 +826,6 @@ describe("backup round trip — export, wire schema, restore", () => {
|
||||
// The language tag is cleared with the text: an empty slot carries
|
||||
// no language, and the next write tags it afresh.
|
||||
expect(userUpdate!.data).toEqual({
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedAt: null,
|
||||
insightsCachedLocale: null,
|
||||
@@ -831,7 +862,7 @@ describe("backup round trip — export, wire schema, restore", () => {
|
||||
(w) =>
|
||||
w.model === "user" &&
|
||||
w.op === "update" &&
|
||||
"insightsCachedText" in w.data,
|
||||
"insightsCachedTextEncrypted" in w.data,
|
||||
);
|
||||
expect(
|
||||
userUpdate,
|
||||
|
||||
@@ -948,7 +948,6 @@ export const USER_COLUMN_BACKUP_CLASS = {
|
||||
|
||||
// ── Operational ───────────────────────────────────────────────────────────
|
||||
insightsCachedAt: "OPERATIONAL",
|
||||
insightsCachedText: "OPERATIONAL",
|
||||
insightsCachedTextEncrypted: "OPERATIONAL",
|
||||
insightsCachedLocale: "OPERATIONAL",
|
||||
insightsSnapshotHash: "OPERATIONAL",
|
||||
|
||||
@@ -23,50 +23,27 @@ beforeEach(() => {
|
||||
afterEach(() => vi.unstubAllEnvs());
|
||||
|
||||
describe("cached insights payload at rest", () => {
|
||||
it("writes ciphertext and nulls the readable column in the same write", () => {
|
||||
it("writes ciphertext only", () => {
|
||||
const data = insightsCacheWrite(PAYLOAD);
|
||||
expect(data.insightsCachedText).toBeNull();
|
||||
expect(Object.keys(data)).toEqual(["insightsCachedTextEncrypted"]);
|
||||
expect(
|
||||
Buffer.from(data.insightsCachedTextEncrypted).toString("utf8"),
|
||||
).not.toContain("Sleep held");
|
||||
expect(openInsightsCachedText(data)).toBe(PAYLOAD);
|
||||
});
|
||||
|
||||
it("falls back to the readable column for a row the backfill has not reached", () => {
|
||||
it("reads a sealed value", () => {
|
||||
expect(
|
||||
openInsightsCachedText({
|
||||
insightsCachedText: PAYLOAD,
|
||||
insightsCachedTextEncrypted: null,
|
||||
}),
|
||||
).toBe(PAYLOAD);
|
||||
});
|
||||
|
||||
// This used to prefer the readable column, the one sealed column read
|
||||
// that way. Every sealed column now reads the ciphertext first.
|
||||
it("reads a sealed value before a readable one beside it", () => {
|
||||
expect(
|
||||
openInsightsCachedText({
|
||||
insightsCachedText: "readable",
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText("sealed"),
|
||||
}),
|
||||
).toBe("sealed");
|
||||
});
|
||||
|
||||
it("never serves the readable value in place of a sealed one that does not open", () => {
|
||||
const foreign = encryptBytes(Buffer.from(PAYLOAD, "utf8"), "other/v1");
|
||||
expect(
|
||||
openInsightsCachedText({
|
||||
insightsCachedText: "readable",
|
||||
insightsCachedTextEncrypted: new Uint8Array(foreign),
|
||||
}),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("does not open a value sealed for another purpose", () => {
|
||||
const foreign = encryptBytes(Buffer.from(PAYLOAD, "utf8"), "other/v1");
|
||||
expect(
|
||||
openInsightsCachedText({
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: new Uint8Array(foreign),
|
||||
}),
|
||||
).toBeNull();
|
||||
@@ -75,6 +52,9 @@ describe("cached insights payload at rest", () => {
|
||||
it("reads an empty row as no cache", () => {
|
||||
expect(openInsightsCachedText(null)).toBeNull();
|
||||
expect(openInsightsCachedText({ ...INSIGHTS_CACHE_CLEARED })).toBeNull();
|
||||
expect(
|
||||
openInsightsCachedText({ insightsCachedTextEncrypted: new Uint8Array() }),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("is fail-closed on write: no key, no payload", () => {
|
||||
@@ -82,13 +62,11 @@ describe("cached insights payload at rest", () => {
|
||||
expect(() => insightsCacheWrite(PAYLOAD)).toThrow();
|
||||
});
|
||||
|
||||
it("clears and selects both columns", () => {
|
||||
it("clears and selects the sealed column", () => {
|
||||
expect(INSIGHTS_CACHE_CLEARED).toEqual({
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
});
|
||||
expect(Object.keys(INSIGHTS_CACHE_SELECT).sort()).toEqual([
|
||||
"insightsCachedText",
|
||||
expect(Object.keys(INSIGHTS_CACHE_SELECT)).toEqual([
|
||||
"insightsCachedTextEncrypted",
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -92,7 +92,10 @@ vi.mock("@/lib/cache/invalidate", () => ({
|
||||
}));
|
||||
|
||||
import { generateComprehensiveInsight } from "../comprehensive-generate";
|
||||
import { openInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
import {
|
||||
openInsightsCachedText,
|
||||
sealInsightsCachedText,
|
||||
} from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so its writes need a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
@@ -148,7 +151,7 @@ function cachedUser(over: Record<string, unknown> = {}) {
|
||||
return {
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
// Never equal to the freshly computed hash, so a full generation runs.
|
||||
insightsSnapshotHash: null,
|
||||
@@ -189,7 +192,7 @@ describe("briefing grounding — disposal", () => {
|
||||
findUnique.mockResolvedValue(
|
||||
cachedUser({
|
||||
insightsCachedAt: new Date("2026-07-15T03:00:00.000Z"),
|
||||
insightsCachedText: PREVIOUS_PAYLOAD,
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(PREVIOUS_PAYLOAD),
|
||||
}),
|
||||
);
|
||||
// Both passes carry the same fabricated figure: a content failure.
|
||||
|
||||
@@ -98,7 +98,10 @@ import { hashInsightSnapshot } from "../snapshot-hash";
|
||||
import { compactSections } from "@/lib/ai/prompts/compact-sections";
|
||||
import { featuresInReaderUnits } from "../features-units";
|
||||
import { DEFAULT_UNIT_PREFERENCES } from "@/lib/measurements/display-transform";
|
||||
import { openInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
import {
|
||||
openInsightsCachedText,
|
||||
sealInsightsCachedText,
|
||||
} from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so its writes need a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
@@ -143,7 +146,7 @@ describe("daily-briefing re-roll on unchanged data", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: CACHED,
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(CACHED),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
insightsBriefingRerollDate: null, // never re-rolled today
|
||||
@@ -198,7 +201,7 @@ describe("daily-briefing re-roll on unchanged data", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: CACHED,
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(CACHED),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
insightsBriefingRerollDate: todayKey, // already re-rolled today
|
||||
@@ -214,7 +217,7 @@ describe("daily-briefing re-roll on unchanged data", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: CACHED,
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(CACHED),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -150,7 +150,7 @@ beforeEach(() => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -96,7 +96,10 @@ import { hashInsightSnapshot } from "../snapshot-hash";
|
||||
import { compactSections } from "@/lib/ai/prompts/compact-sections";
|
||||
import { featuresInReaderUnits } from "../features-units";
|
||||
import { DEFAULT_UNIT_PREFERENCES } from "@/lib/measurements/display-transform";
|
||||
import { openInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
import {
|
||||
openInsightsCachedText,
|
||||
sealInsightsCachedText,
|
||||
} from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so its writes need a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
@@ -143,7 +146,7 @@ describe("generateComprehensiveInsight — the briefing capability at the wire",
|
||||
const STALE_USER = {
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
};
|
||||
@@ -181,7 +184,7 @@ describe("generateComprehensiveInsight — the briefing capability at the wire",
|
||||
findUnique.mockResolvedValue({
|
||||
...STALE_USER,
|
||||
insightsCachedAt: new Date(Date.now() - 60 * 60 * 1000),
|
||||
insightsCachedText: "{}",
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText("{}"),
|
||||
});
|
||||
aiCapabilityForRecord.mockResolvedValue({
|
||||
available: false,
|
||||
@@ -201,7 +204,7 @@ describe("generateComprehensiveInsight — cache short-circuit", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 21 * 60 * 60 * 1000),
|
||||
insightsCachedText: "{}",
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText("{}"),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
});
|
||||
@@ -220,7 +223,7 @@ describe("generateComprehensiveInsight — cache short-circuit", () => {
|
||||
insightsPrivacyMode: "aggregated",
|
||||
// 21h old — inside the 24h TTL, so the un-forced path would cache.
|
||||
insightsCachedAt: new Date(Date.now() - 21 * 60 * 60 * 1000),
|
||||
insightsCachedText: "{}",
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText("{}"),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
});
|
||||
@@ -251,7 +254,9 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
});
|
||||
@@ -280,7 +285,9 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
// Stored fingerprint from an older data state.
|
||||
insightsSnapshotHash: "0".repeat(64),
|
||||
@@ -329,7 +336,9 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
unitPreference: "imperial",
|
||||
@@ -383,7 +392,9 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 1 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "english" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "english" } }),
|
||||
),
|
||||
insightsCachedLocale: "en",
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
@@ -403,7 +414,9 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
});
|
||||
@@ -436,10 +449,12 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({
|
||||
dailyBriefing: null,
|
||||
recommendations: [],
|
||||
}),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({
|
||||
dailyBriefing: null,
|
||||
recommendations: [],
|
||||
}),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
});
|
||||
@@ -475,7 +490,7 @@ describe("generateComprehensiveInsight — content-hash gate (v1.16.8)", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
});
|
||||
@@ -522,7 +537,9 @@ describe("generateComprehensiveInsight — usage level", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH,
|
||||
aiUsageLevel,
|
||||
|
||||
@@ -110,7 +110,7 @@ beforeEach(() => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -116,7 +116,7 @@ beforeEach(() => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -114,7 +114,7 @@ beforeEach(() => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -97,6 +97,7 @@ vi.mock("@/lib/logging/context", async (importOriginal) => {
|
||||
import { generateComprehensiveInsight } from "../comprehensive-generate";
|
||||
import { hashInsightSnapshot } from "../snapshot-hash";
|
||||
import { compactSections } from "@/lib/ai/prompts/compact-sections";
|
||||
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// The cached payload is sealed at rest, so its writes need a key.
|
||||
vi.stubEnv("ENCRYPTION_KEY", "c".repeat(64));
|
||||
@@ -151,7 +152,9 @@ describe("generateComprehensiveInsight — consent withdrawn in flight", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: "0".repeat(64),
|
||||
});
|
||||
@@ -191,7 +194,9 @@ describe("generateComprehensiveInsight — scope/version commit guard", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: "0".repeat(64), // changed → full generation
|
||||
});
|
||||
@@ -236,7 +241,9 @@ describe("generateComprehensiveInsight — scope/version commit guard", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: ["sleep"],
|
||||
insightsSnapshotHash: "0".repeat(64),
|
||||
gender: "FEMALE",
|
||||
@@ -280,7 +287,9 @@ describe("generateComprehensiveInsight — scope/version commit guard", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: ["sleep"],
|
||||
insightsSnapshotHash: "0".repeat(64),
|
||||
gender: "FEMALE",
|
||||
@@ -322,7 +331,9 @@ describe("generateComprehensiveInsight — scope/version commit guard", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
JSON.stringify({ dailyBriefing: { p: "old" } }),
|
||||
),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: "0".repeat(64),
|
||||
});
|
||||
@@ -361,7 +372,7 @@ describe("generateComprehensiveInsight — scope/version commit guard", () => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: new Date(Date.now() - 26 * 60 * 60 * 1000),
|
||||
insightsCachedText: CACHED_WITH_BRIEFING,
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(CACHED_WITH_BRIEFING),
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: FEATURES_HASH, // unchanged → reroll path
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -108,7 +108,7 @@ function mockUser(overrides: Record<string, unknown> = {}) {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
insightsBriefingRerollDate: null,
|
||||
@@ -165,7 +165,7 @@ describe("comprehensive timeout honouring", () => {
|
||||
const outcome = await generateComprehensiveInsight("u1", { locale: "de" });
|
||||
|
||||
expect(outcome.status).toBe("failed");
|
||||
// No cache row written → the prior `insightsCachedText` (last good) survives.
|
||||
// No cache row written → the prior `insightsCachedTextEncrypted` (last good) survives.
|
||||
expect(userUpdate).not.toHaveBeenCalled();
|
||||
// A dated failure marker is recorded so the read path can surface a hint.
|
||||
expect(recordBriefingFailure).toHaveBeenCalledWith(
|
||||
|
||||
@@ -129,7 +129,7 @@ beforeEach(() => {
|
||||
findUnique.mockResolvedValue({
|
||||
insightsPrivacyMode: "aggregated",
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsExcludeMetrics: [],
|
||||
insightsSnapshotHash: null,
|
||||
insightsBriefingRerollDate: null,
|
||||
|
||||
@@ -4,7 +4,7 @@ import { prisma } from "@/lib/db";
|
||||
* Dated failure marker for the daily-briefing generation.
|
||||
*
|
||||
* The briefing keeps its last good payload on a failed refresh: the generator
|
||||
* writes NO `User.insightsCachedText` row when a provider times out or errors,
|
||||
* writes NO `User.insightsCachedTextEncrypted` row when a provider times out or errors,
|
||||
* so the previous briefing stays intact and the surface never blanks. What was
|
||||
* missing is a signal the READ path can use to tell the difference between "no
|
||||
* refresh was due" and "the last refresh attempt failed" — without that, a
|
||||
|
||||
@@ -1,35 +1,30 @@
|
||||
/**
|
||||
* The cached AI insights payload, sealed at rest.
|
||||
*
|
||||
* `User.insightsCachedText` held the model's last full reading of the account
|
||||
* in the clear: the daily briefing, the per-domain status text and the
|
||||
* recommendations, every one of them a sentence about the person's health.
|
||||
* It was the last readable column of that kind on the user row. Since v1.43.7
|
||||
* the payload lives in `insightsCachedTextEncrypted` (AES-256-GCM, binary
|
||||
* layout, purpose-bound by {@link INSIGHTS_CACHED_TEXT_AAD}), and the free-text
|
||||
* encryption backfill seals the rows written before.
|
||||
* The model's last full reading of the account (the daily briefing, the
|
||||
* per-domain status text and the recommendations, every one of them a
|
||||
* sentence about the person's health) lives in `insightsCachedTextEncrypted`
|
||||
* (AES-256-GCM, binary layout, purpose-bound by
|
||||
* {@link INSIGHTS_CACHED_TEXT_AAD}). The readable column it replaced in
|
||||
* v1.43.7 was dropped in v1.44.0 (migration 0430).
|
||||
*
|
||||
* Every reader and writer goes through this module, which is what keeps the
|
||||
* two columns in step. A clear that nulls one column and not the other would
|
||||
* leave the briefing behind after a consent withdrawal, so the clearing value
|
||||
* is exported here and nowhere else spells the column names
|
||||
* Every reader and writer goes through this module, so the column name is
|
||||
* spelled here and in the classification tables only
|
||||
* (`insights-cached-text-access-guard.test.ts`).
|
||||
*/
|
||||
import { Buffer } from "node:buffer";
|
||||
|
||||
import { decryptBytes, encryptBytes } from "@/lib/crypto";
|
||||
import { INSIGHTS_CACHED_TEXT_AAD } from "@/lib/crypto/encrypted-columns";
|
||||
import { readSealedOrLegacy, warnAndDrop } from "@/lib/crypto/sealed-or-legacy";
|
||||
import { warnAndDrop } from "@/lib/crypto/sealed-or-legacy";
|
||||
|
||||
/** The columns a reader selects to open the cache. */
|
||||
export const INSIGHTS_CACHE_SELECT = {
|
||||
insightsCachedText: true,
|
||||
insightsCachedTextEncrypted: true,
|
||||
} as const;
|
||||
|
||||
/** The row shape {@link openInsightsCachedText} reads. */
|
||||
export interface InsightsCacheColumns {
|
||||
insightsCachedText?: string | null;
|
||||
insightsCachedTextEncrypted?: Uint8Array | null;
|
||||
}
|
||||
|
||||
@@ -44,47 +39,28 @@ export function sealInsightsCachedText(text: string): Uint8Array<ArrayBuffer> {
|
||||
return out;
|
||||
}
|
||||
|
||||
/**
|
||||
* The `data` fields that store a payload: the ciphertext, and the legacy
|
||||
* readable column nulled in the same write so a row never holds both a new
|
||||
* sealed payload and an older readable one.
|
||||
*/
|
||||
/** The `data` fields that store a payload. */
|
||||
export function insightsCacheWrite(text: string): {
|
||||
insightsCachedTextEncrypted: Uint8Array<ArrayBuffer>;
|
||||
insightsCachedText: null;
|
||||
} {
|
||||
return {
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(text),
|
||||
insightsCachedText: null,
|
||||
};
|
||||
return { insightsCachedTextEncrypted: sealInsightsCachedText(text) };
|
||||
}
|
||||
|
||||
/** The `data` fields that empty the cache. Both columns, always. */
|
||||
/** The `data` fields that empty the cache. */
|
||||
export const INSIGHTS_CACHE_CLEARED = {
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
} as const;
|
||||
|
||||
/** A `where` that finds rows holding a cached payload in either column. */
|
||||
/** A `where` that finds rows holding a cached payload. */
|
||||
export const INSIGHTS_CACHE_PRESENT_WHERE = {
|
||||
OR: [
|
||||
{ insightsCachedText: { not: null } },
|
||||
{ insightsCachedTextEncrypted: { not: null } },
|
||||
],
|
||||
insightsCachedTextEncrypted: { not: null },
|
||||
} as const;
|
||||
|
||||
const dropUndecryptable = warnAndDrop("insights cache did not open");
|
||||
|
||||
/**
|
||||
* The cached payload as text, or null when there is none.
|
||||
*
|
||||
* The ciphertext is read first, as every sealed column is read
|
||||
* (`readSealedOrLegacy`); the readable column only when the row holds no
|
||||
* ciphertext. A row holding both can come from a writer of the previous
|
||||
* release still running after the upgrade; its readable value may be the
|
||||
* newer one, and the free-text backfill seals it over the old ciphertext on
|
||||
* its next pass. Until then the reader serves the sealed one: an older
|
||||
* briefing for a while, never a readable value preferred over a protected
|
||||
* one.
|
||||
*
|
||||
* A sealed value that does not open reads as an empty cache with a warning on
|
||||
* the event. It is a cache: the next warm overwrites it under the active key,
|
||||
* whereas failing the read would take the dashboard down with it.
|
||||
@@ -92,14 +68,13 @@ export const INSIGHTS_CACHE_PRESENT_WHERE = {
|
||||
export function openInsightsCachedText(
|
||||
row: InsightsCacheColumns | null | undefined,
|
||||
): string | null {
|
||||
if (!row) return null;
|
||||
return readSealedOrLegacy({
|
||||
sealed: row.insightsCachedTextEncrypted,
|
||||
legacy: row.insightsCachedText,
|
||||
open: (sealed) =>
|
||||
decryptBytes(Buffer.from(sealed), INSIGHTS_CACHED_TEXT_AAD).toString(
|
||||
"utf8",
|
||||
),
|
||||
onUndecryptable: warnAndDrop("insights cache did not open"),
|
||||
});
|
||||
const sealed = row?.insightsCachedTextEncrypted;
|
||||
if (!sealed || sealed.byteLength === 0) return null;
|
||||
try {
|
||||
return decryptBytes(Buffer.from(sealed), INSIGHTS_CACHED_TEXT_AAD).toString(
|
||||
"utf8",
|
||||
);
|
||||
} catch (error) {
|
||||
return dropUndecryptable(error);
|
||||
}
|
||||
}
|
||||
@@ -1279,7 +1279,7 @@ export async function generateComprehensiveInsight(
|
||||
});
|
||||
// v1.25 — record a dated failure marker so the read path can surface a
|
||||
// discreet "couldn't refresh" hint alongside the preserved last-good
|
||||
// briefing. No cache row is written here, so `insightsCachedText` (the
|
||||
// briefing. No cache row is written here, so `insightsCachedTextEncrypted` (the
|
||||
// last good payload) stays intact and the surface never blanks.
|
||||
void recordBriefingFailure({
|
||||
userId,
|
||||
|
||||
@@ -5,10 +5,6 @@ import {
|
||||
decryptRouteGeometry,
|
||||
encryptRouteGeometry,
|
||||
} from "@/lib/workouts/route-geometry-cipher";
|
||||
import {
|
||||
openInsightsCachedText,
|
||||
sealInsightsCachedText,
|
||||
} from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
// ── in-memory store backing a minimal prisma mock ──────────────────────────
|
||||
interface ConversationRow {
|
||||
@@ -33,14 +29,6 @@ interface PractitionerRow {
|
||||
locationEncrypted: Uint8Array | null;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
interface UserRow {
|
||||
id: string;
|
||||
/** The row is its own owner; mirrors the other delegates' owner field. */
|
||||
userId: string;
|
||||
insightsCachedText: string | null;
|
||||
insightsCachedTextEncrypted: Uint8Array | null;
|
||||
updatedAt?: Date;
|
||||
}
|
||||
interface RouteRow {
|
||||
id: string;
|
||||
/** The owning workout's user; the real table reaches it through the join. */
|
||||
@@ -54,7 +42,6 @@ const store = vi.hoisted(() => ({
|
||||
entries: [] as EntryRow[],
|
||||
practitioners: [] as PractitionerRow[],
|
||||
routes: [] as RouteRow[],
|
||||
users: [] as UserRow[],
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/jobs/boss-instance", () => ({ getGlobalBoss: () => null }));
|
||||
@@ -131,33 +118,6 @@ vi.mock("@/lib/db", () => {
|
||||
customMetricEntry: delegate(() => store.entries, ["note"]),
|
||||
practitioner: delegate(() => store.practitioners, ["phone", "location"]),
|
||||
workoutRoute: delegate(() => store.routes, ["geometry"]),
|
||||
user: (() => {
|
||||
const base = delegate(() => store.users, ["insightsCachedText"]);
|
||||
return {
|
||||
...base,
|
||||
// The insights cache move also guards on the readable value it read,
|
||||
// so a warm that sealed a newer payload meanwhile is not overwritten.
|
||||
updateMany: async (args: {
|
||||
where: { id: string; updatedAt?: Date; insightsCachedText?: string };
|
||||
data: Partial<UserRow>;
|
||||
}) => {
|
||||
const row = store.users.find((x) => x.id === args.where.id);
|
||||
if (
|
||||
row &&
|
||||
args.where.insightsCachedText !== undefined &&
|
||||
row.insightsCachedText !== args.where.insightsCachedText
|
||||
) {
|
||||
return { count: 0 };
|
||||
}
|
||||
// Prisma stamps `@updatedAt` on every write that does not set it.
|
||||
const data =
|
||||
"updatedAt" in args.data
|
||||
? args.data
|
||||
: { ...args.data, updatedAt: new Date() };
|
||||
return base.updateMany({ ...args, data });
|
||||
},
|
||||
};
|
||||
})(),
|
||||
};
|
||||
return {
|
||||
prisma: {
|
||||
@@ -178,9 +138,6 @@ import {
|
||||
} from "@/lib/jobs/free-text-encryption-backfill";
|
||||
|
||||
const KEY = "a".repeat(64);
|
||||
const CACHE = JSON.stringify({
|
||||
dailyBriefing: { paragraph: "Your pressure eased after the new tablets." },
|
||||
});
|
||||
const TRACK = {
|
||||
type: "LineString",
|
||||
coordinates: [
|
||||
@@ -268,21 +225,6 @@ beforeEach(() => {
|
||||
},
|
||||
{ id: "r3", userId: "u2", geometry: TRACK, geometryEncrypted: null },
|
||||
];
|
||||
store.users = [
|
||||
{
|
||||
id: "u1",
|
||||
userId: "u1",
|
||||
insightsCachedText: CACHE,
|
||||
insightsCachedTextEncrypted: null,
|
||||
updatedAt: new Date("2026-03-01T07:00:00.000Z"),
|
||||
},
|
||||
{
|
||||
id: "u2",
|
||||
userId: "u2",
|
||||
insightsCachedText: "other user",
|
||||
insightsCachedTextEncrypted: null,
|
||||
},
|
||||
];
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
@@ -300,7 +242,6 @@ describe("runFreeTextEncryptionBackfillForUser", () => {
|
||||
routeGeometriesMigrated: 1,
|
||||
appointmentAddressesCleared: 0,
|
||||
contactAuditRowsScrubbed: 0,
|
||||
insightsCachesMigrated: 1,
|
||||
});
|
||||
|
||||
const c1 = store.conversations.find((r) => r.id === "c1")!;
|
||||
@@ -404,52 +345,12 @@ describe("runFreeTextEncryptionBackfillForUser", () => {
|
||||
routeGeometriesMigrated: 0,
|
||||
appointmentAddressesCleared: 0,
|
||||
contactAuditRowsScrubbed: 0,
|
||||
insightsCachesMigrated: 0,
|
||||
});
|
||||
expect(store.conversations.find((r) => r.id === "c1")!.titleEncrypted).toBe(
|
||||
sealed,
|
||||
);
|
||||
});
|
||||
|
||||
it("seals the cached insights payload, keeps updatedAt, and leaves another account's alone", async () => {
|
||||
await runFreeTextEncryptionBackfillForUser("u1");
|
||||
const u1 = store.users.find((r) => r.id === "u1")!;
|
||||
expect(u1.insightsCachedText).toBeNull();
|
||||
expect(u1.insightsCachedTextEncrypted).not.toBeNull();
|
||||
// Sealed, not merely encoded: the bytes do not carry the prose.
|
||||
expect(
|
||||
Buffer.from(u1.insightsCachedTextEncrypted!).toString("utf8"),
|
||||
).not.toContain("pressure");
|
||||
expect(openInsightsCachedText(u1)).toBe(CACHE);
|
||||
// Profile forms use updatedAt as their concurrency token; a storage move
|
||||
// must not invalidate an open form.
|
||||
expect(u1.updatedAt).toEqual(new Date("2026-03-01T07:00:00.000Z"));
|
||||
expect(store.users.find((r) => r.id === "u2")).toMatchObject({
|
||||
insightsCachedText: "other user",
|
||||
insightsCachedTextEncrypted: null,
|
||||
});
|
||||
});
|
||||
|
||||
it("does not overwrite a payload a warm sealed while the move was reading", async () => {
|
||||
const newer = sealInsightsCachedText("newer");
|
||||
const u1 = store.users.find((r) => r.id === "u1")!;
|
||||
// The warm lands between the backfill's read and its write: the read
|
||||
// returns the old readable value, the row then holds the newer sealed one.
|
||||
const { prisma } = await import("@/lib/db");
|
||||
const snapshot = { ...u1 };
|
||||
const spy = vi.spyOn(prisma.user, "findUnique").mockImplementationOnce(((
|
||||
_args: unknown,
|
||||
) => {
|
||||
u1.insightsCachedText = null;
|
||||
u1.insightsCachedTextEncrypted = newer;
|
||||
return Promise.resolve(snapshot);
|
||||
}) as never);
|
||||
await runFreeTextEncryptionBackfillForUser("u1");
|
||||
spy.mockRestore();
|
||||
expect(u1.insightsCachedTextEncrypted).toBe(newer);
|
||||
expect(openInsightsCachedText(u1)).toBe("newer");
|
||||
});
|
||||
|
||||
it("is fail-closed: without a key every readable row stays intact", async () => {
|
||||
vi.stubEnv("ENCRYPTION_KEY", "");
|
||||
vi.stubEnv("ENCRYPTION_KEYS", "");
|
||||
|
||||
@@ -22,9 +22,9 @@
|
||||
* column holds a JSON null rather than SQL NULL has nothing to seal; it is set
|
||||
* to SQL NULL so discovery stops finding it.
|
||||
*
|
||||
* v1.43.7 adds the cached AI insights payload on the user row:
|
||||
* `User.insightsCachedText` -> `insightsCachedTextEncrypted` (the model's
|
||||
* briefing, per-domain readings and recommendations about the account).
|
||||
* v1.43.7 sealed the cached AI insights payload on the user row the same
|
||||
* way; v1.44.0 dropped its readable column (migration 0430), so that part of
|
||||
* the pass is gone too.
|
||||
*
|
||||
* v1.42 (#615) added the environment module's coarse locations (home, dated
|
||||
* location periods, stored days). v1.43 dropped their readable columns
|
||||
@@ -71,7 +71,6 @@ import { getGlobalBoss } from "@/lib/jobs/boss-instance";
|
||||
import { encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { encryptNote } from "@/lib/crypto/note-cipher";
|
||||
import { encryptRouteGeometry } from "@/lib/workouts/route-geometry-cipher";
|
||||
import { sealInsightsCachedText } from "@/lib/insights/cached-text-cipher";
|
||||
|
||||
export const FREE_TEXT_ENCRYPTION_BACKFILL_QUEUE =
|
||||
"free-text-encryption-backfill";
|
||||
@@ -99,8 +98,6 @@ export interface FreeTextEncryptionBackfillSummary {
|
||||
appointmentAddressesCleared: number;
|
||||
/** v1.39.4 — edit audit rows whose old phone number / address was removed. */
|
||||
contactAuditRowsScrubbed: number;
|
||||
/** v1.43.7 — cached AI insights payloads sealed (0 or 1 per account). */
|
||||
insightsCachesMigrated: number;
|
||||
}
|
||||
|
||||
/** The audit action whose older rows carried contact values. */
|
||||
@@ -298,42 +295,6 @@ async function migrateRouteGeometry(id: string): Promise<boolean> {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Seal one account's cached AI insights payload into
|
||||
* `insightsCachedTextEncrypted` and null the readable column, atomically and
|
||||
* idempotently. The update is guarded on the readable value it read: a warm
|
||||
* that lands meanwhile writes the ciphertext and nulls the readable column, so
|
||||
* the guard misses and the newer payload is left as it is.
|
||||
*/
|
||||
async function migrateInsightsCache(id: string): Promise<boolean> {
|
||||
return prisma.$transaction(async (tx) => {
|
||||
const fresh = await tx.user.findUnique({
|
||||
where: { id },
|
||||
select: { insightsCachedText: true, updatedAt: true },
|
||||
});
|
||||
if (!fresh || fresh.insightsCachedText === null) return false;
|
||||
// FAIL-CLOSED: a key error throws here and rolls the tx back. A cache
|
||||
// move is storage, not activity, so `updatedAt` is carried: the profile
|
||||
// forms use it as their optimistic-concurrency token. Matching on it too
|
||||
// means an edit that lands meanwhile wins; the row is retried next pass.
|
||||
const { count } = await tx.user.updateMany({
|
||||
where: {
|
||||
id,
|
||||
updatedAt: fresh.updatedAt,
|
||||
insightsCachedText: fresh.insightsCachedText,
|
||||
},
|
||||
data: {
|
||||
insightsCachedTextEncrypted: sealInsightsCachedText(
|
||||
fresh.insightsCachedText,
|
||||
),
|
||||
insightsCachedText: null,
|
||||
updatedAt: fresh.updatedAt,
|
||||
},
|
||||
});
|
||||
return count === 1;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Walk one kind of row page by page, migrating each. `page` returns the ids
|
||||
* still holding a readable value; each migration clears it, so a migrated row
|
||||
@@ -428,18 +389,6 @@ export async function runFreeTextEncryptionBackfillForUser(
|
||||
once,
|
||||
);
|
||||
|
||||
// One row at most: the cache is a single slot on the user row.
|
||||
const insightsCachesMigrated = await drain(
|
||||
() =>
|
||||
prisma.user.findMany({
|
||||
where: { id: userId, insightsCachedText: { not: null } },
|
||||
select: { id: true },
|
||||
take: 1,
|
||||
}),
|
||||
migrateInsightsCache,
|
||||
once,
|
||||
);
|
||||
|
||||
// An appointment reminder's address is resolved from the practitioner when
|
||||
// it fires; the readable copy earlier releases wrote here goes.
|
||||
const appointmentAddressesCleared = (
|
||||
@@ -463,7 +412,6 @@ export async function runFreeTextEncryptionBackfillForUser(
|
||||
route_geometries_migrated: routeGeometriesMigrated,
|
||||
appointment_addresses_cleared: appointmentAddressesCleared,
|
||||
contact_audit_rows_scrubbed: contactAuditRowsScrubbed,
|
||||
insights_caches_migrated: insightsCachesMigrated,
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -475,7 +423,6 @@ export async function runFreeTextEncryptionBackfillForUser(
|
||||
routeGeometriesMigrated,
|
||||
appointmentAddressesCleared,
|
||||
contactAuditRowsScrubbed,
|
||||
insightsCachesMigrated,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -530,10 +477,6 @@ export async function enqueueBootTimeFreeTextEncryptionBackfill(
|
||||
WHERE action = ${CONTACT_AUDIT_ACTION}
|
||||
AND user_id IS NOT NULL
|
||||
AND (details LIKE '%"location":%' OR details LIKE '%"phone":%')`,
|
||||
// The 0411 partial index matches this predicate.
|
||||
prisma.$queryRaw<{ user_id: string }[]>`
|
||||
SELECT id AS user_id FROM users
|
||||
WHERE insights_cached_text IS NOT NULL`,
|
||||
]);
|
||||
|
||||
const userIds = new Set<string>();
|
||||
|
||||
@@ -93,7 +93,6 @@ describe("encrypted health profile fact revisions", () => {
|
||||
where: { id: "user-1" },
|
||||
data: {
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
@@ -133,7 +132,6 @@ describe("encrypted health profile fact revisions", () => {
|
||||
where: { id: "user-1" },
|
||||
data: {
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
@@ -199,7 +197,6 @@ describe("encrypted health profile fact revisions", () => {
|
||||
where: { id: "user-1" },
|
||||
data: {
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
@@ -247,7 +244,6 @@ describe("encrypted health profile fact revisions", () => {
|
||||
where: { id: "user-1" },
|
||||
data: {
|
||||
insightsCachedAt: null,
|
||||
insightsCachedText: null,
|
||||
insightsCachedTextEncrypted: null,
|
||||
insightsCachedLocale: null,
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user