mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 16:12:11 +00:00
fix(daily): write the briefing for the reader's own morning and readings
With signals of the day resting on today's readings only, the briefing had to be written after those readings, and it was only written at the 04:30 Berlin warm, the post-sleep morning refresh, or a page open on a day-old cache. A morning blood pressure or weigh-in never became a signal, and since an earlier day's briefing is no longer served, a reader in Los Angeles or Seoul had none for most of the day: the Berlin warm lands the previous evening there, or at noon. The snapshot read every surface goes through (dashboard, iOS, digest, morning push) now asks for a briefing for today at two moments: the first read of the reader's local day with no briefing written today, and the first read after a signal metric's first reading of the day that the briefing has not read. Each reason is claimed once per user per local day in a Postgres bucket and remembered in-process, so later reads cost no query; no write path needs a hook. A claim enqueues one today warm: immediately for a new day, after a five-minute window for readings, so a burst (weigh-in, then blood pressure) folds into one job. The warm writes the comprehensive briefing only, past the one-hour freshness gate but under the failure backoff, the daily forced-warm cap (two per 24 h) and the content-hash gate. Worst case per user per local day: one enqueue per reason, at most eight (the day plus seven signal reading types, measured over a day of reads every two minutes); at most two forced comprehensive attempts under the existing cap, each at most three completions (the answer, a JSON retry, a grounding retry), so six provider calls at the ceiling. A warm whose data has not changed costs at most the existing once-a-day paragraph re-roll, inside the same cap.
This commit is contained in:
1 parent
f6b0131731
commit
43361bd74e
8 files changed
+533
-10
No files matched your search
@@ -6,7 +6,10 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import type { User } from "@/generated/prisma/client";
|
||||
import { AI_AVAILABLE } from "@/__tests__/helpers/ai-capability-fixtures";
|
||||
import {
|
||||
AI_AVAILABLE,
|
||||
aiUnavailable,
|
||||
} from "@/__tests__/helpers/ai-capability-fixtures";
|
||||
|
||||
const BRIEFING = {
|
||||
paragraph: "Your pulse is well above its usual level today.",
|
||||
@@ -52,8 +55,15 @@ vi.mock("@/lib/dashboard/snapshot", async (importOriginal) => ({
|
||||
vi.mock("@/lib/i18n/server-locale", () => ({
|
||||
resolveServerLocale: async () => "en",
|
||||
}));
|
||||
const capability = vi.hoisted(() => ({ current: null as unknown }));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityToServe: async () => AI_AVAILABLE,
|
||||
aiCapabilityToServe: async () => capability.current,
|
||||
}));
|
||||
const requestTodayBriefingWarm = vi.hoisted(() =>
|
||||
vi.fn(async (_input: unknown) => undefined),
|
||||
);
|
||||
vi.mock("@/lib/daily/briefing-today-warm", () => ({
|
||||
requestTodayBriefingWarm,
|
||||
}));
|
||||
|
||||
const { readDashboardSnapshotCached } = await import("../snapshot-read");
|
||||
@@ -75,6 +85,8 @@ function user(generatedAt: string | null): User {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
capability.current = AI_AVAILABLE;
|
||||
requestTodayBriefingWarm.mockClear();
|
||||
__resetAllCachesForTests();
|
||||
current = body("2026-10-04T06:50:00.000Z");
|
||||
});
|
||||
@@ -123,3 +135,32 @@ describe("readDashboardSnapshotCached — the briefing's own day", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("readDashboardSnapshotCached — asking for today's briefing", () => {
|
||||
it("hands the read's view of the day to the today warm", async () => {
|
||||
await readDashboardSnapshotCached(
|
||||
user("2026-10-04T02:30:00.000Z"),
|
||||
undefined,
|
||||
{
|
||||
now: NOW,
|
||||
},
|
||||
);
|
||||
expect(requestTodayBriefingWarm).toHaveBeenCalledTimes(1);
|
||||
const arg = requestTodayBriefingWarm.mock.calls[0][0] as {
|
||||
generatedAt: string;
|
||||
timezone: string;
|
||||
now: Date;
|
||||
lastSeenAt: (type: string) => string | null;
|
||||
};
|
||||
expect(arg.generatedAt).toBe("2026-10-04T02:30:00.000Z");
|
||||
expect(arg.timezone).toBe("Europe/Berlin");
|
||||
expect(arg.now).toBe(NOW);
|
||||
expect(arg.lastSeenAt("PULSE")).toBe("2026-10-04T06:50:00.000Z");
|
||||
});
|
||||
|
||||
it("asks for nothing while the briefing capability is unavailable", async () => {
|
||||
capability.current = aiUnavailable("consent_required");
|
||||
await readDashboardSnapshotCached(user(null), undefined, { now: NOW });
|
||||
expect(requestTodayBriefingWarm).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -38,6 +38,7 @@ import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
|
||||
import { resolveServerLocale } from "@/lib/i18n/server-locale";
|
||||
import type { Locale } from "@/lib/i18n/config";
|
||||
import { briefingForToday } from "@/lib/daily/briefing-today";
|
||||
import { requestTodayBriefingWarm } from "@/lib/daily/briefing-today-warm";
|
||||
import { readBriefingGeneratedAt } from "@/lib/insights/briefing-generated-at";
|
||||
import { userDayKey } from "@/lib/tz/format";
|
||||
|
||||
@@ -175,15 +176,28 @@ export async function readDashboardSnapshotCached(
|
||||
// capability is available, decided on every read rather than baked into
|
||||
// the cached body. The record's own state decides, whoever is reading, so a
|
||||
// delegate sees the owner's briefing exactly when the owner would.
|
||||
const now = options.now ?? new Date();
|
||||
const generatedAt = readBriefingGeneratedAt(user.insightsCachedText);
|
||||
const body = applyBriefingForToday(
|
||||
applyBriefingCapability(cached, briefingAi),
|
||||
{
|
||||
generatedAt: readBriefingGeneratedAt(user.insightsCachedText),
|
||||
timezone: user.timezone,
|
||||
language: locale,
|
||||
now: options.now ?? new Date(),
|
||||
},
|
||||
{ generatedAt, timezone: user.timezone, language: locale, now },
|
||||
);
|
||||
|
||||
// The day's first read with no briefing written today, or the first read
|
||||
// after a signal metric's first reading of the day, asks for a briefing
|
||||
// for today (`briefing-today-warm.ts`). Never awaited by the read beyond
|
||||
// its claim, never thrown.
|
||||
if (briefingAi.available) {
|
||||
const lastSeen = cached.tiles?.lastSeenByType ?? {};
|
||||
void requestTodayBriefingWarm({
|
||||
userId: user.id,
|
||||
locale,
|
||||
timezone: user.timezone,
|
||||
generatedAt,
|
||||
lastSeenAt: (type) => lastSeen[type]?.lastSeenAt ?? null,
|
||||
now,
|
||||
}).catch(() => undefined);
|
||||
}
|
||||
|
||||
return { body, locale };
|
||||
}
|
||||
Reference in new issue
Block a user