fix(insights): screen the medication-adherence card and background lines uniformly

Route the medication-compliance status card through finalizeStatusSummary so
it enforces the same dose / risk / causal outbound contracts its seven sibling
status cards do, not only the unparseable-envelope withhold it already had. A
dose-change imperative or a fabricated figure is now withheld to the controlled
fallback on this surface too.

Add the status cards' structured-envelope detection to the two background hero
lines (arrival reaction, coach nudge): a brace-led or fenced JSON reply, or a
truncated envelope under the char cap, now resolves to the deterministic lead
rather than rendering raw.

Fold in the residuals from the same sweep: strip a truncated SUGGEST-ACTION
marker from the Coach prose tail, drop an unreachable zero-rate branch in the
adherence fold, and document why the internal conversation summary stays off
the render-boundary screen.
This commit is contained in:
Marc-André Bombeck committed 2026-07-24 14:32:21 +02:00
1 parent 1d3963bebf
commit 0baffe04cd
6 files changed
+73 -10

No files matched your search

+6 -1
View File
@@ -284,7 +284,12 @@ export async function prepareGeneralStatusForUser(
.sort(([a], [b]) => a.localeCompare(b))
.map(([day, value]) => ({
measuredAt: new Date(`${day}T12:00:00.000Z`),
value: value.total > 0 ? round((value.taken / value.total) * 100, 1) : 0,
// A map entry is only ever created after `bucket.total += 1`, so
// `value.total` is always >= 1 here — the former `: 0` guard branch was
// unreachable. v1.32.20 removed it: a real zero-dose day never reaches
// this fold, so a synthesised `0` would have been a fabricated rate, not
// an honest absence (the #582 silent-wrong-absence class).
value: round((value.taken / value.total) * 100, 1),
}));
const adherenceSeries = applyPayloadBudget(adherenceRecords, {
now,
@@ -30,7 +30,7 @@ import { buildMetricSignal } from "@/lib/insights/metric-signal";
import { degradeStatusSnapshotToBudget } from "@/lib/insights/graded-series";
import {
type SupportedLocale,
extractAssessmentSummary,
finalizeStatusSummary,
normalizeLocale,
normalizeSummaryText,
round,
@@ -577,15 +577,22 @@ export async function prepareMedicationComplianceStatusForUser(
// does not emit per-medication text. The per-medication cards carry a
// placeholder so the UI surfaces a row per active medication; the
// overall `summary` is the model-authored assessment.
// A malformed / truncated / summary-less structured envelope must never
// persist as the day's assessment (the raw-JSON class of bug). WITHHOLD:
//
// v1.32.20 — route the completion through the shared status chokepoint,
// so this card enforces the exact contracts its seven siblings do rather
// than only withholding unparseable envelopes. `finalizeStatusSummary`
// parses the `{ summary }` envelope, scrubs chart tokens + whitespace,
// then SCREENS the prose against `INSIGHTS_CONTRACTS` (dose / risk /
// causal). This is the highest-leverage surface for the outbound screen —
// a medication card is exactly where a dose-change imperative or a
// fabricated figure lands. WITHHOLD on any block or unparseable envelope:
// serve the deterministic no-key line, write a short negative stub, and
// persist no model text. The per-medication rows still render.
const extracted = extractAssessmentSummary(outcome.content);
if (extracted.kind === "unparseable") {
const finalized = finalizeStatusSummary(outcome.content, locale);
if (!finalized.ok) {
annotate({
action: { name: "insights.status.outbound_blocked" },
meta: { cacheAction, reason: "unparseable_output" },
meta: { cacheAction, reason: finalized.reason },
});
returnTimeoutFallback({
cacheAction,
@@ -602,7 +609,7 @@ export async function prepareMedicationComplianceStatusForUser(
updatedAt: null,
};
}
const summary = normalizeSummaryText(extracted.text);
const summary = finalized.text;
if (!summary) {
throw new Error(
"Medication-compliance-status summary was empty after normalization",