fix(coach): keep updatedAt through storage-only rewrites

The title encryption backfill moved each Coach conversation's title into
ciphertext with a plain Prisma update, which stamps updatedAt. The Coach
panel orders and groups conversations by that column, so threads untouched
for months showed up under Today. The memory summary refresh did the same
in the background.

Storage-only rewrites now carry the row's own updatedAt and match on it, so
a turn that lands meanwhile wins and the row is retried on the next pass:
conversation titles, practitioner contacts, measurement and mood notes
(where updatedAt is the sync cursor), inventory notes, and the mood-note
script. The summary refresh writes through raw SQL. A guard freezes both
rules: every backfill write on a model with @updatedAt names the column or
is listed with the reason the stamp is wanted, and every conversation
write outside the person's own actions keeps it.
This commit is contained in:
Marc-André Bombeck committed 2026-10-05 12:40:55 +02:00
1 parent 3381026acb
commit 7d42cf7122
13 files changed
+504 -77

No files matched your search

@@ -53,7 +53,8 @@ function makePrisma(conversation: unknown) {
update,
findFirst,
client: {
coachConversation: { findFirst, update },
coachConversation: { findFirst },
$executeRaw: update,
} as never,
};
}
@@ -161,7 +162,7 @@ describe("refreshConversationSummary", () => {
}
});
it("(e) success path encrypts and persists summaryEncrypted + turn count + updatedAt", async () => {
it("(e) success path persists the summary and turn count, and leaves updatedAt alone", async () => {
const now = new Date("2026-06-04T08:00:00.000Z");
const { client, update } = makePrisma(
makeConversation(30, { summaryTurnCount: 0 }),
@@ -177,12 +178,19 @@ describe("refreshConversationSummary", () => {
expect(result.status).toBe("generated");
expect(encryptToBytes).toHaveBeenCalledWith(okCompletion.content);
expect(update).toHaveBeenCalledTimes(1);
const updateArg = update.mock.calls[0]![0];
expect(updateArg.where).toEqual({ id: "conv-1" });
expect(updateArg.data.summaryEncrypted).toBeDefined();
expect(updateArg.data.summaryUpdatedAt).toBe(now);
const [strings, ...values] = update.mock.calls[0]! as [
TemplateStringsArray,
...unknown[],
];
const sql = strings.join("?");
expect(sql).toContain('UPDATE "coach_conversations"');
expect(sql).toContain('"summary_encrypted"');
// A memory rewrite is not activity: the panel orders by updated_at.
expect(sql).not.toMatch(/"updated_at"/);
expect(values[1]).toBe(now);
// foldHighWater = 30 - 18 = 12.
expect(updateArg.data.summaryTurnCount).toBe(12);
expect(values[2]).toBe(12);
expect(values[3]).toBe("conv-1");
});
});
+11 -9
View File
@@ -56,7 +56,7 @@ type RunCompletion = typeof runStatusCompletion;
export interface RefreshConversationSummaryOptions {
now?: Date;
runCompletion?: RunCompletion;
prisma?: Pick<PrismaClient, "coachConversation">;
prisma?: Pick<PrismaClient, "coachConversation" | "$executeRaw">;
locale?: "de" | "en";
}
@@ -227,14 +227,16 @@ export async function refreshConversationSummary(
// 6. Encrypt + persist. Field-by-field data object (no spread).
const summaryEncrypted = encryptToBytes(text);
await prisma.coachConversation.update({
where: { id: conversationId },
data: {
summaryEncrypted,
summaryUpdatedAt: now,
summaryTurnCount: foldHighWater,
},
});
// Raw SQL on purpose: a Prisma `update` stamps `updatedAt`, and the Coach
// panel orders and groups conversations by it. A memory rewrite is
// bookkeeping, not activity, so it must not move the thread to "Today".
// Every value is a bound parameter.
await prisma.$executeRaw`
UPDATE "coach_conversations"
SET "summary_encrypted" = ${summaryEncrypted},
"summary_updated_at" = ${now},
"summary_turn_count" = ${foldHighWater}
WHERE "id" = ${conversationId}`;
// 7. Counts + ids only — never the summary text.
annotate({
@@ -12,6 +12,7 @@ interface ConversationRow {
userId: string;
title: string | null;
titleEncrypted: Uint8Array | null;
updatedAt?: Date;
}
interface EntryRow {
id: string;
@@ -26,6 +27,7 @@ interface PractitionerRow {
location: string | null;
phoneEncrypted: Uint8Array | null;
locationEncrypted: Uint8Array | null;
updatedAt?: Date;
}
interface RouteRow {
id: string;
@@ -67,22 +69,49 @@ vi.mock("@/lib/db", () => {
},
findUnique: async (args: { where: { id: string } }) =>
rows().find((x) => x.id === args.where.id) ?? null,
update: async (args: { where: { id: string }; data: Partial<R> }) => {
const r = rows().find((x) => x.id === args.where.id)!;
// Prisma's DbNull sentinel stores SQL NULL.
const data = Object.fromEntries(
Object.entries(args.data).map(([k, v]) => [
k,
v !== null && typeof v === "object" && !(v instanceof Uint8Array)
? null
: v,
]),
);
Object.assign(r, data);
return r;
update: async (args: { where: { id: string }; data: Partial<R> }) =>
write(rows, args.where.id, args.data),
// The storage-only rewrites match on the row's own `updatedAt` and
// carry it forward; a row whose stamp moved meanwhile is not written.
updateMany: async (args: {
where: { id: string; updatedAt?: Date };
data: Partial<R>;
}) => {
const r = rows().find((x) => x.id === args.where.id) as
(R & { updatedAt?: Date }) | undefined;
if (!r) return { count: 0 };
if (
args.where.updatedAt !== undefined &&
r.updatedAt?.getTime() !== args.where.updatedAt?.getTime()
) {
return { count: 0 };
}
write(rows, args.where.id, args.data);
return { count: 1 };
},
};
}
function write<R extends { id: string }>(
rows: () => R[],
id: string,
patch: Partial<R>,
): R {
const r = rows().find((x) => x.id === id)!;
// Prisma's DbNull sentinel stores SQL NULL; a Date is a real value.
const data = Object.fromEntries(
Object.entries(patch).map(([k, v]) => [
k,
v !== null &&
typeof v === "object" &&
!(v instanceof Uint8Array) &&
!(v instanceof Date)
? null
: v,
]),
);
Object.assign(r, data);
return r;
}
const delegates = {
coachConversation: delegate(() => store.conversations, ["title"]),
customMetricEntry: delegate(() => store.entries, ["note"]),
@@ -126,6 +155,7 @@ beforeEach(() => {
userId: "u1",
title: "Why is my pressure up after the new tablets?",
titleEncrypted: null,
updatedAt: new Date("2026-03-02T09:15:00.000Z"),
},
// Already migrated: left alone.
{
@@ -218,6 +248,9 @@ describe("runFreeTextEncryptionBackfillForUser", () => {
expect(readNote(c1.titleEncrypted, null)).toBe(
"Why is my pressure up after the new tablets?",
);
// Sealing a title is not activity: the Coach panel orders and groups by
// updatedAt, so the conversation keeps the stamp it had.
expect(c1.updatedAt).toEqual(new Date("2026-03-02T09:15:00.000Z"));
const e1 = store.entries.find((r) => r.id === "e1")!;
expect(e1.note).toBeNull();
@@ -20,6 +20,7 @@ interface InventoryRow {
userId: string;
notes: string | null;
notesEncrypted: Uint8Array | null;
updatedAt?: Date;
}
const store = vi.hoisted(() => ({
@@ -99,16 +100,31 @@ vi.mock("@/lib/db", () => {
.map((r) => ({ id: r.id })),
findUnique: async (args: { where: { id: string } }) => {
const r = store.inventory.find((x) => x.id === args.where.id);
return r ? { notes: r.notes, notesEncrypted: r.notesEncrypted } : null;
return r
? {
notes: r.notes,
notesEncrypted: r.notesEncrypted,
updatedAt: r.updatedAt,
}
: null;
},
update: async (args: {
where: { id: string };
data: { notes: string | null; notesEncrypted: Uint8Array | null };
updateMany: async (args: {
where: { id: string; updatedAt?: Date };
data: {
notes: string | null;
notesEncrypted: Uint8Array | null;
updatedAt?: Date;
};
}) => {
const r = store.inventory.find((x) => x.id === args.where.id)!;
const r = store.inventory.find((x) => x.id === args.where.id);
if (!r || r.updatedAt?.getTime() !== args.where.updatedAt?.getTime()) {
return { count: 0 };
}
r.notes = args.data.notes;
r.notesEncrypted = args.data.notesEncrypted;
return r;
// Prisma stamps now unless the write names the column.
r.updatedAt = args.data.updatedAt ?? new Date();
return { count: 1 };
},
},
};
@@ -148,7 +164,13 @@ beforeEach(() => {
},
];
store.inventory = [
{ id: "i1", userId: "u1", notes: "opened pen #2", notesEncrypted: null },
{
id: "i1",
userId: "u1",
notes: "opened pen #2",
notesEncrypted: null,
updatedAt: new Date("2026-01-12T10:00:00.000Z"),
},
{
id: "i2",
userId: "u1",
@@ -181,6 +203,8 @@ describe("runMedNotesEncryptionBackfillForUser", () => {
const i1 = store.inventory.find((r) => r.id === "i1")!;
expect(i1.notes).toBeNull();
expect(readNote(i1.notesEncrypted, null)).toBe("opened pen #2");
// A storage-only rewrite keeps the row's own updatedAt.
expect(i1.updatedAt).toEqual(new Date("2026-01-12T10:00:00.000Z"));
});
it("leaves note-less rows untouched and never writes a both-null content row", async () => {
@@ -7,12 +7,14 @@ interface MeasRow {
userId: string;
notes: string | null;
notesEncrypted: Uint8Array | null;
updatedAt?: Date;
}
interface MoodRow {
id: string;
userId: string;
note: string | null;
noteEncrypted: Uint8Array | null;
updatedAt?: Date;
}
const store = vi.hoisted(() => ({
@@ -44,16 +46,31 @@ vi.mock("@/lib/db", () => {
.map((r) => ({ id: r.id })),
findUnique: async (args: { where: { id: string } }) => {
const r = store.measurements.find((x) => x.id === args.where.id);
return r ? { notes: r.notes, notesEncrypted: r.notesEncrypted } : null;
return r
? {
notes: r.notes,
notesEncrypted: r.notesEncrypted,
updatedAt: r.updatedAt,
}
: null;
},
update: async (args: {
where: { id: string };
data: { notes: string | null; notesEncrypted: Uint8Array | null };
updateMany: async (args: {
where: { id: string; updatedAt?: Date };
data: {
notes: string | null;
notesEncrypted: Uint8Array | null;
updatedAt?: Date;
};
}) => {
const r = store.measurements.find((x) => x.id === args.where.id)!;
const r = store.measurements.find((x) => x.id === args.where.id);
if (!r || r.updatedAt?.getTime() !== args.where.updatedAt?.getTime()) {
return { count: 0 };
}
r.notes = args.data.notes;
r.notesEncrypted = args.data.notesEncrypted;
return r;
// Prisma stamps now unless the write names the column.
r.updatedAt = args.data.updatedAt ?? new Date();
return { count: 1 };
},
},
moodEntry: {
@@ -69,16 +86,30 @@ vi.mock("@/lib/db", () => {
.map((r) => ({ id: r.id })),
findUnique: async (args: { where: { id: string } }) => {
const r = store.mood.find((x) => x.id === args.where.id);
return r ? { note: r.note, noteEncrypted: r.noteEncrypted } : null;
return r
? {
note: r.note,
noteEncrypted: r.noteEncrypted,
updatedAt: r.updatedAt,
}
: null;
},
update: async (args: {
where: { id: string };
data: { note: string | null; noteEncrypted: Uint8Array | null };
updateMany: async (args: {
where: { id: string; updatedAt?: Date };
data: {
note: string | null;
noteEncrypted: Uint8Array | null;
updatedAt?: Date;
};
}) => {
const r = store.mood.find((x) => x.id === args.where.id)!;
const r = store.mood.find((x) => x.id === args.where.id);
if (!r || r.updatedAt?.getTime() !== args.where.updatedAt?.getTime()) {
return { count: 0 };
}
r.note = args.data.note;
r.noteEncrypted = args.data.noteEncrypted;
return r;
r.updatedAt = args.data.updatedAt ?? new Date();
return { count: 1 };
},
},
};
@@ -99,7 +130,13 @@ beforeEach(() => {
vi.stubEnv("ENCRYPTION_ACTIVE_KEY_ID", "");
vi.stubEnv("ENCRYPTION_KEY", KEY);
store.measurements = [
{ id: "m1", userId: "u1", notes: "felt dizzy", notesEncrypted: null },
{
id: "m1",
userId: "u1",
notes: "felt dizzy",
notesEncrypted: null,
updatedAt: new Date("2026-02-01T07:00:00.000Z"),
},
{ id: "m2", userId: "u1", notes: null, notesEncrypted: null }, // no note
{
id: "m3",
@@ -109,7 +146,13 @@ beforeEach(() => {
},
];
store.mood = [
{ id: "x1", userId: "u1", note: "rough night", noteEncrypted: null },
{
id: "x1",
userId: "u1",
note: "rough night",
noteEncrypted: null,
updatedAt: new Date("2026-02-03T22:00:00.000Z"),
},
{ id: "x2", userId: "u2", note: "other user", noteEncrypted: null },
];
fail.encryptThrows = false;
@@ -135,6 +178,10 @@ describe("runNoteEncryptionBackfillForUser", () => {
const x1 = store.mood.find((r) => r.id === "x1")!;
expect(x1.note).toBeNull();
expect(readNote(x1.noteEncrypted, null)).toBe("rough night");
// updatedAt is the sync cursor: a storage-only rewrite keeps it.
expect(m1.updatedAt).toEqual(new Date("2026-02-01T07:00:00.000Z"));
expect(x1.updatedAt).toEqual(new Date("2026-02-03T22:00:00.000Z"));
});
it("leaves note-less rows untouched and never writes a both-null content row", async () => {
+19 -9
View File
@@ -183,15 +183,23 @@ async function migrateConversationTitle(id: string): Promise<boolean> {
return prisma.$transaction(async (tx) => {
const fresh = await tx.coachConversation.findUnique({
where: { id },
select: { title: true },
select: { title: true, updatedAt: true },
});
if (!fresh || fresh.title === null) return false;
// FAIL-CLOSED: a key error throws here and rolls the tx back.
await tx.coachConversation.update({
where: { id },
data: { titleEncrypted: encryptToBytes(fresh.title), title: null },
// `updatedAt` is carried, not stamped: the Coach panel orders and groups
// by it, and moving a title into ciphertext is not activity. Matching on
// it too means a turn that lands meanwhile wins; the row is retried on
// the next pass.
const { count } = await tx.coachConversation.updateMany({
where: { id, updatedAt: fresh.updatedAt },
data: {
titleEncrypted: encryptToBytes(fresh.title),
title: null,
updatedAt: fresh.updatedAt,
},
});
return true;
return count === 1;
});
}
@@ -224,14 +232,16 @@ async function migratePractitionerContact(id: string): Promise<boolean> {
return prisma.$transaction(async (tx) => {
const fresh = await tx.practitioner.findUnique({
where: { id },
select: { phone: true, location: true },
select: { phone: true, location: true, updatedAt: true },
});
if (!fresh || (fresh.phone === null && fresh.location === null)) {
return false;
}
await tx.practitioner.update({
where: { id },
// Storage-only rewrite: the row keeps its own `updatedAt`.
const { count } = await tx.practitioner.updateMany({
where: { id, updatedAt: fresh.updatedAt },
data: {
updatedAt: fresh.updatedAt,
...(fresh.phone !== null
? { phoneEncrypted: encryptNote(fresh.phone), phone: null }
: {}),
@@ -240,7 +250,7 @@ async function migratePractitionerContact(id: string): Promise<boolean> {
: {}),
},
});
return true;
return count === 1;
});
}
+10 -5
View File
@@ -120,15 +120,20 @@ async function migrateInventoryItemRow(id: string): Promise<boolean> {
return prisma.$transaction(async (tx) => {
const fresh = await tx.medicationInventoryItem.findUnique({
where: { id },
select: { notes: true, notesEncrypted: true },
select: { notes: true, notesEncrypted: true, updatedAt: true },
});
if (!fresh || fresh.notesEncrypted || fresh.notes === null) return false;
const encrypted = encryptNote(fresh.notes);
await tx.medicationInventoryItem.update({
where: { id },
data: { notesEncrypted: encrypted, notes: null },
// Storage-only rewrite: the row keeps its own `updatedAt`.
const { count } = await tx.medicationInventoryItem.updateMany({
where: { id, updatedAt: fresh.updatedAt },
data: {
notesEncrypted: encrypted,
notes: null,
updatedAt: fresh.updatedAt,
},
});
return true;
return count === 1;
});
}
+20 -10
View File
@@ -68,18 +68,24 @@ async function migrateMeasurementRow(id: string): Promise<boolean> {
return prisma.$transaction(async (tx) => {
const fresh = await tx.measurement.findUnique({
where: { id },
select: { notes: true, notesEncrypted: true },
select: { notes: true, notesEncrypted: true, updatedAt: true },
});
// Already migrated (ciphertext present) or nothing to migrate.
if (!fresh || fresh.notesEncrypted || fresh.notes === null) return false;
// FAIL-CLOSED: a key error throws here and rolls the tx back, leaving the
// plaintext row untouched.
const encrypted = encryptNote(fresh.notes);
await tx.measurement.update({
where: { id },
data: { notesEncrypted: encrypted, notes: null },
// `updatedAt` is the sync cursor; a storage-only rewrite carries it so no
// client re-pulls a reading that did not change.
const { count } = await tx.measurement.updateMany({
where: { id, updatedAt: fresh.updatedAt },
data: {
notesEncrypted: encrypted,
notes: null,
updatedAt: fresh.updatedAt,
},
});
return true;
return count === 1;
});
}
@@ -91,15 +97,19 @@ async function migrateMoodRow(id: string): Promise<boolean> {
return prisma.$transaction(async (tx) => {
const fresh = await tx.moodEntry.findUnique({
where: { id },
select: { note: true, noteEncrypted: true },
select: { note: true, noteEncrypted: true, updatedAt: true },
});
if (!fresh || fresh.noteEncrypted || fresh.note === null) return false;
const encrypted = encryptNote(fresh.note);
await tx.moodEntry.update({
where: { id },
data: { noteEncrypted: encrypted, note: null },
const { count } = await tx.moodEntry.updateMany({
where: { id, updatedAt: fresh.updatedAt },
data: {
noteEncrypted: encrypted,
note: null,
updatedAt: fresh.updatedAt,
},
});
return true;
return count === 1;
});
}