fix(daily): surface a due dose on Today and keep the last grounded briefing

The Today hero stayed on its all-clear line while a dose was actually due:
the dose-window item only fired once the slot was already overdue, so a
dose the user could take right now produced no rail item at all. Emit the
item from the moment the dose becomes takeable — the on-time window's lead
plus the early grace, the same instant logging it starts crediting the
slot — and give the two faces distinct wording and status: a due dose reads
calm and informational, an overdue one keeps the warning. A settled day
still emits nothing, and a cached block whose anchor has since passed keeps
rendering neither face. Push reminders are untouched; they still fire only
on overdue.

The briefing grounding gate discarded the whole block over one number it
could not verify, and it could not verify plenty of legitimate ones: the
allow-set was a hand-maintained list of feature blocks that had already
drifted from the prompt three times, and still omitted medication
compliance, the context counters, the correlation coefficients, every
coverage object and the comparison snapshot — all of which the prompt hands
the model verbatim. Derive the allow-set by walking the prompt payload
instead, so grounded means exactly what it says: a figure the server gave
the model. A fabricated number still matches nothing.

Fix the disposal to match. A content-failed retry left a hole while a
transport-failed one fell back to the previous cached briefing, though in
both cases the rejected text is discarded and the previous briefing passed
this same gate when it was written. Fall back on both paths, and only
report the briefing as withheld when there is genuinely nothing to show.
The on-demand route had no fallback at all; it now mirrors the background
generator that writes the same cached payload.
This commit is contained in:
Marc-André Bombeck committed 2026-07-19 09:11:57 +02:00
1 parent e6e2210314
commit f8bb8f4b9b
10 files changed
+252 -228

No files matched your search

+33 -5
View File
@@ -847,6 +847,7 @@ export const POST = apiHandler((request: NextRequest) =>
readBriefingBlock(insights),
signals,
features,
comparisonSnapshot,
);
if (ungrounded.length > 0) {
annotate({
@@ -879,6 +880,7 @@ export const POST = apiHandler((request: NextRequest) =>
readBriefingBlock(retryInsights),
signals,
features,
comparisonSnapshot,
);
if (ungrounded.length === 0) {
insights = retryInsights;
@@ -891,14 +893,40 @@ export const POST = apiHandler((request: NextRequest) =>
// strip. An ungrounded figure is never persisted.
}
}
// Still ungrounded after the retry: strip the briefing rather than ship a
// fabricated figure. The structured recommendations/citations stay.
// Still ungrounded after the retry: the freshly generated briefing is
// discarded rather than shipped — the anti-fabrication guarantee is
// absolute here too. But rather than leave a hole, stand the PREVIOUS
// cached briefing in its place: it passed this identical gate when it
// was written, so it carries no fabricated figure, and a day-old
// grounded briefing beats a vanished one. This mirrors the background
// generator, which is the path that writes the same cached payload;
// the two must not disagree on disposal.
//
// `briefingOmittedReason` is set ONLY when no previous briefing exists
// to stand in, so the card's honest "a figure couldn't be verified"
// state still fires exactly when the user really has no briefing.
if (ungrounded.length > 0 && insights && typeof insights === "object") {
(insights as Record<string, unknown>).dailyBriefing = null;
briefingOmittedReason = "ungrounded";
let fallbackBriefing: unknown = null;
if (dbUser?.insightsCachedText) {
try {
const prev = JSON.parse(dbUser.insightsCachedText) as Record<
string,
unknown
>;
fallbackBriefing = prev.dailyBriefing ?? null;
} catch {
// Unparseable previous payload — nothing safe to stand in.
}
}
(insights as Record<string, unknown>).dailyBriefing = fallbackBriefing;
briefingOmittedReason = fallbackBriefing == null ? "ungrounded" : null;
annotate({
action: { name: "insights.generate.briefing_grounding_stripped" },
meta: { ungroundedCount: ungrounded.length },
meta: {
ungroundedCount: ungrounded.length,
briefing_fallback:
fallbackBriefing != null ? "previous-cached" : "stripped",
},
});
}
}