mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-14 06:40:50 +00:00
Carry the Coach's memory through backup and restore
The transcript came back in the previous change; what the Coach keeps BETWEEN threads did not. A restored account had every word of every conversation and a Coach that has to be told its own history again — the register said as much for all three: "a restore makes the account re-tell its history", "keeps the talk and loses the commitment", "a restored account silently stops sending them". Tombstones ride only in a disaster-recovery file. All three soft-delete, and a deleted fact is hidden from injection while kept for audit — a distinction only the owning instance can act on. A portable file carrying them would hand a person rows their own app refuses to show them, and a restore that resurrected one would put a fact back into the Coach's mouth that the person had deliberately taken out of it. Neither reference here is a foreign key. sourceConversationId, on all three, and relatedPlanId on the reminder are bare id columns, so nothing in the database refuses a value pointing at nothing. That makes them more dangerous than a real key rather than less: a dangling reference costs no error, it just quietly stops meaning anything while the row goes on looking complete. The restore resolves both against what it actually wrote, nulls what it cannot resolve, and reports the count. Nulling rather than dropping is the point. A live reminder must not be lost because the plan it mentions was tombstoned out of a portable file. The reminder is still the person's and it still fires; what it loses is a pointer that was already going nowhere. Order follows from that: threads, then plans, then reminders. Running the memory before the transcript would cut every fact loose from the conversation it came out of and still report success. Verified by breaking it: making the conversation lookup always return null fails the round trip on the fact's own source field.
This commit is contained in:
1 parent
ad8325f18d
commit
1fbd3b628b
11 files changed
+735
-9
No files matched your search
@@ -297,6 +297,9 @@ function sourceClient() {
|
||||
measurementReminder: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
measurementReminderEvent: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
coachConversation: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
coachFact: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
coachPlan: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
coachReminder: { findMany: vi.fn().mockResolvedValue([]) },
|
||||
customMetric: {
|
||||
findMany: vi.fn().mockResolvedValue([
|
||||
{
|
||||
|
||||
@@ -50,7 +50,10 @@ import { restoreIntradayProfileData } from "@/lib/export/intraday-profile-backup
|
||||
import { restoreHealthScoreData } from "@/lib/export/health-score-backup";
|
||||
import { restoreVisitsData } from "@/lib/export/visits-backup";
|
||||
import { restoreVaccinationsData } from "@/lib/export/vaccinations-backup";
|
||||
import { restoreCoachData } from "@/lib/export/coach-backup";
|
||||
import {
|
||||
restoreCoachData,
|
||||
restoreCoachMemoryData,
|
||||
} from "@/lib/export/coach-backup";
|
||||
import { restoreRemindersData } from "@/lib/export/reminders-backup";
|
||||
import { invalidateUserData } from "@/lib/cache/invalidate";
|
||||
|
||||
@@ -102,6 +105,9 @@ interface RestoreResponse {
|
||||
coachConversations: number;
|
||||
coachMessages: number;
|
||||
coachConversationDocuments: number;
|
||||
coachFacts: number;
|
||||
coachPlans: number;
|
||||
coachReminders: number;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1433,6 +1439,19 @@ const handler = apiHandler(
|
||||
skips,
|
||||
);
|
||||
|
||||
// What the Coach keeps between threads. AFTER the transcript,
|
||||
// because all three carry a `sourceConversationId` that is resolved
|
||||
// against the threads the branch above has just written — resolving
|
||||
// earlier would cut every fact loose from the conversation it came
|
||||
// out of and still report success.
|
||||
const coachMemoryCleared = await restoreCoachMemoryData(
|
||||
tx,
|
||||
ownerId,
|
||||
payload,
|
||||
new Set(payload.coachConversations.map((c) => c.id)),
|
||||
skips,
|
||||
);
|
||||
|
||||
const cleared = {
|
||||
measurements: measurements.count,
|
||||
medications: meds.count,
|
||||
@@ -1470,6 +1489,9 @@ const handler = apiHandler(
|
||||
coachConversations: coachCleared.coachConversations,
|
||||
coachMessages: coachCleared.coachMessages,
|
||||
coachConversationDocuments: coachCleared.coachConversationDocuments,
|
||||
coachFacts: coachMemoryCleared.coachFacts,
|
||||
coachPlans: coachMemoryCleared.coachPlans,
|
||||
coachReminders: coachMemoryCleared.coachReminders,
|
||||
};
|
||||
return { cleared, skipped: summarizeRestoreSkips(skips) };
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user