fix(coach): tell a history outside the window apart from a metric never recorded

A Coach retrieval tool that read the window and found nothing returned
`reason: "no_data"` — the same code for two situations that have nothing in
common: the record holds nothing for that metric, and the record holds a
substantial history for it, all of it older than the window that was searched.
Nothing downstream could tell them apart, so the Coach said the honest-sounding
sentence for the wrong case, and then, having been told there was nothing there,
changed the subject. Someone with a year of imported glucose readings was told
the readings did not exist, on the same account whose glucose page was showing
them.

The window cap stays. It exists because a multi-year history does not fit a
prompt budget, and widening it would only move the same failure. What was
missing is the ability to say a different true sentence.

- `tools/availability.ts` — one grouped aggregate per miss, no rows
  materialised, resolving four states: `no_data` (probe confirmed empty),
  `outside_window` (rows exist, older than the searched window; carries the
  count, the date range in the user's own timezone, and a bounded per-series
  mean/min/max), `unavailable_in_scope` (rows exist inside the window and the
  domain still produced none — a module is off), and `no_data_unconfirmed` (the
  probe itself failed; absence was never established, so it is not asserted).
- Every empty read in the executor routes through it. A bare `no_data` can no
  longer be returned without the record having been checked.
- The DATA INVENTORY carries the same distinction, so a domain the window
  cannot reach is advertised with what it holds instead of as absent — the model
  no longer skips the fetch, and a vague follow-up keeps the subject.
- Both instructions that told the model to report absence and move on now
  branch on the reason code, and forbid answering about one metric by reporting
  another.
- The out-of-window aggregate joins the authoritative set for the prose
  verifier, so the sentence the fix exists to make sayable is not elided.
- A cross-cutting narration block that throws is now counted rather than
  vanishing into a `null` that looks like honest absence.
- The medication-compliance page tells a failed read apart from an account with
  no medications, instead of inviting someone to re-enter what it already holds.

Refs #648
This commit is contained in:
Marc-André Bombeck committed 2026-07-26 18:34:42 +02:00
1 parent 24d699f252
commit a402eaaf15
10 files changed
+917 -112

No files matched your search

+3 -1
View File
@@ -695,7 +695,9 @@ async function handleChatRequest(request: NextRequest): Promise<Response> {
// ("you've logged 42 BP readings") stays grounded.
const presentToolPayloads = [
...(workoutEvidence === null ? [] : [workoutEvidence]),
...(loop.toolResults ?? []).map((r) => r.data),
// A miss carries no `data` but may carry `available` — the bounded
// out-of-window aggregate rule 3 lets the model cite. Ground it.
...(loop.toolResults ?? []).map((r) => r.data ?? r.available),
];
toolResultPayloads =
presentToolPayloads.length > 0
+26 -1
View File
@@ -13,6 +13,7 @@ import { Button } from "@/components/ui/button";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { EmptyState } from "@/components/ui/empty-state";
import { Progress } from "@/components/ui/progress";
import { QueryErrorCard } from "@/components/ui/query-error-card";
import { ComplianceHeatmap } from "@/components/charts/compliance-heatmap";
import { CoachLaunchButton } from "@/components/insights/coach-launch-button";
import { InsightStatusCard } from "@/components/insights/insight-status-card";
@@ -93,7 +94,12 @@ export default function InsightsMedikamentePage() {
const { isAuthenticated } = useAuth();
const { t, locale } = useTranslations();
const { data: comprehensive, isLoading: isComprehensiveLoading } = useQuery({
const {
data: comprehensive,
isLoading: isComprehensiveLoading,
isError: isComprehensiveError,
refetch: refetchComprehensive,
} = useQuery({
queryKey: queryKeys.insightsComprehensive(),
queryFn: async () => {
return apiGet<ComprehensiveMedicationData>("/api/insights/comprehensive");
@@ -153,6 +159,25 @@ export default function InsightsMedikamentePage() {
);
}
// A failed comprehensive read leaves `medications` empty, which is
// indistinguishable from an account that tracks none — and the empty state
// then invites someone on five daily medications to add the medications the
// app already holds. A read failure gets its own branch, before the length
// check, mirroring the sibling overview at `/insights`.
if (isComprehensiveError) {
return (
<SubPageShell
title={t("insights.medicationCompliance")}
explainerMetric="medications"
>
<QueryErrorCard
description={t("insights.loadError")}
onRetry={() => refetchComprehensive()}
/>
</SubPageShell>
);
}
if (medications.length === 0) {
// v1.4.27 F17 — medication compliance is event-driven so the
// gate reads `medications.length > 0`. CTA targets `/medications`