Files
HealthLog/src/app/api/insights/derived/route.ts
T
Marc-André Bombeck 27342ee734 fix(ai): name the real cause of failed briefings and stop retrying what cannot heal
The nightly briefing warm completed every night and read "all-failed",
because one run event held every account's annotations and the last
account won. Each candidate now runs in its own wide event and the run
reports every outcome through `reportJobRun`, so ok, dead sign-in,
withheld and failed accounts show side by side.

A dead Codex refresh token threw a bare error with no status, so the chain
read it as a network failure: the health ledger recorded hard_failed, no
"reconnect" hint appeared, and every night tried the same token. The
refresh now throws `CodexRefreshError` with a 401 for a dead grant
(invalid_grant / invalid_refresh_token on a 400 included), as do a missing
or undecryptable stored credential. The ledger records auth_failed, the
chain marks the primary credential expired, and the nightly pass reports
the account as auth_failed without queuing a retry.

A briefing the outbound screen withheld (a causal claim) failed outright,
and the 45-minute provider retry then asked the same question eight times
a night. The generator now makes one bounded repair that names the broken
rule and keeps the repaired payload only if it passes the same parse,
grounding and screen; a withheld outcome queues no retry.

A provider that keeps failing is paused: from five consecutive hard
failures the skip window starts at 30 minutes and doubles per further
failure, capped at a day. A paused provider is walked last and tried again
when the window lifts; one success resumes it. The operator card shows how
many accounts it is paused for and until when. After a 5xx from a
non-canonical OpenAI-compatible endpoint the client asks the free model
listing, and a configured model the endpoint no longer offers is named in
the error, on the hop line and on the card.

Also:
- The tiered context's live day band read `measured_at AT TIME ZONE tz`,
  which treats the stored UTC as local time; a far-from-UTC reader's
  evening readings landed on the next day. It converts from UTC first now.
- Google Health intraday heart rate re-reads two hours per sync instead of
  a day; daily summaries and sleep keep the day (#1023).
- The first filtered daily read that answers 400 and triggers the
  snake_case retry is recorded as a note, not an error (#1023).
- The single derived-score read goes through the derived cache with
  stale-while-revalidate, like the batch beside it.
2026-10-08 09:06:09 +02:00

206 lines
9.0 KiB
TypeScript

/**
* v1.10.0 — generic derived-wellness-metric route.
*
* `GET /api/insights/derived?metric=<DERIVED_METRIC_ID>[&type=<MeasurementType>][&windowDays=N]`
* serves the compute-once `Derived<T>` value for any metric registered
* in `derived/registry.ts`. Mirrors the v1.8.7.1 `metric-status` route
* precedent: `apiHandler` wrapper, Zod `safeParse` on the query, a closed
* registry enum (unknown id → 422 via `returnAllZodIssues`), cookie OR
* Bearer auth, `userId` narrowed from the session/Bearer (never a query
* field).
*
* The `data` payload is the flat `Derived<T>` union so iOS can decode +
* combine values across metrics: `{ metric, status, value?, coverage,
* confidence?, provenance, reason? }`. Numbers are pure compute over the
* rollup tier (Tier-1, no narrative) — no provider call, no cache table;
* since v1.42 the in-process derived cache, as the batch route uses.
*
* Wave 1 implements `VITALS_BASELINE` end-to-end; other registered ids
* return a `not_implemented` insufficient until their compute lands in a
* later wave (never a 500, never a fabricated value).
*/
import { NextRequest } from "next/server";
import { z } from "zod/v4";
import { apiError, apiSuccess, returnAllZodIssues } from "@/lib/api-response";
import { apiHandler, requireRecordAuth } from "@/lib/api-handler";
import { annotate } from "@/lib/logging/context";
import { checkAnalyticsReadRateLimit } from "@/lib/rate-limit";
import { requireModuleEnabled, type ModuleKey } from "@/lib/modules/gate";
import { prisma } from "@/lib/db";
import {
computeDerivedMetric,
loadBaselineProfile,
DERIVED_METRIC_IDS,
type DerivedMetricId,
} from "@/lib/insights/derived";
import { DERIVED_MAX_WINDOW_DAYS } from "@/lib/insights/derived/types";
import { resolveDerivedAssessment } from "@/lib/insights/derived/derived-assessment-ai";
import { resolveServerLocale } from "@/lib/i18n/server-locale";
import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
import { cachedSwr, caches, type ServerCache } from "@/lib/cache/server-cache";
export const dynamic = "force-dynamic";
/**
* Derived scores that are NATIVE to a toggleable module rather than a core
* vital. SLEEP_SCORE is the sleep module's; the strain / recovery / stress
* trio belongs to the WHOOP/Polar recovery module. READINESS equals the
* computed recovery score (same number, recovery-native), so it is gated on
* the recovery module too. The remaining composite baselines
* (VITALS_BASELINE, HRV_BALANCE, …) read off core vitals and stay open — a
* metric absent from this map carries no module gate. Exported so the batch
* route gates on the SAME map and the two routes cannot drift.
*/
export const DERIVED_MODULE: Partial<Record<DerivedMetricId, ModuleKey>> = {
SLEEP_SCORE: "sleep",
RECOVERY_SCORE: "recovery",
STRAIN_SCORE: "recovery",
STRESS_SCORE: "recovery",
READINESS: "recovery",
};
// Closed enum derived from the registry so the route + registry cannot
// drift — an id added to the registry is accepted automatically, an
// unknown id 422s rather than silently resolving to no metric.
const derivedQuerySchema = z.object({
metric: z.enum(DERIVED_METRIC_IDS as [string, ...string[]]),
// Optional sub-target for metrics that baseline a single chosen vital
// (VITALS_BASELINE). Validated against the engine's supported set in
// the dispatcher; an unsupported value yields an `insufficient`, not a
// 422, so the contract stays forgiving for iOS combinations.
type: z.string().optional(),
// Trailing window override, in days. Absent means "engine default" — each
// compute keeps its own (14 for the wellness trend, 30 for the baselines, a
// year for vascular age), so omitting the parameter changes nothing. The
// ceiling is the point past which the derived engines stop resolving against
// DAY rollup buckets and degrade to an uncapped raw read; the reasoning is
// written out at `DERIVED_MAX_WINDOW_DAYS`. Out of range is a 422 rather
// than a silent clamp: a caller that asked for a year should learn that it
// is not on offer, not read back a number it did not choose.
windowDays: z.coerce
.number()
.int()
.min(1)
.max(DERIVED_MAX_WINDOW_DAYS)
.optional(),
});
export const GET = apiHandler(async (request: NextRequest) => {
// v1.37.0 — MANAGE-level read: a generated assessment over the whole
// record, which is not a section a scoped grant can name. The miss behind it
// enqueues nothing while a delegate is holding the request.
const { user } = await requireRecordAuth("manage", "record");
// v1.15.20 — shared analytics-read budget (generous; caps runaway loops).
const rl = await checkAnalyticsReadRateLimit(user.id);
if (!rl.allowed) {
return apiError("Too many analytics requests. Please retry later.", 429);
}
// Pure compute over the rollup tier, so no AI gate and no `insights`
// module gate (the AI analysis opt-out): switching AI off does not blank the
// derived tiles. Only the model-written assessment below depends on AI.
const parsed = derivedQuerySchema.safeParse({
metric: request.nextUrl.searchParams.get("metric"),
type: request.nextUrl.searchParams.get("type") ?? undefined,
windowDays: request.nextUrl.searchParams.get("windowDays") ?? undefined,
});
if (!parsed.success) {
annotate({
action: { name: "insights.derived.invalid-metric" },
meta: { issue_count: parsed.error.issues.length },
});
return returnAllZodIssues(parsed.error, 422);
}
const metric = parsed.data.metric as DerivedMetricId;
// Per-domain gate: a derived score owned by a toggleable module
// (sleep / recovery) is refused with a 403 module.disabled envelope when
// the account has that module turned off. Core-vital composites stay open.
const moduleKey = DERIVED_MODULE[metric];
if (moduleKey) {
const gate = await requireModuleEnabled(user.id, moduleKey);
if (!gate.enabled) return gate.response;
}
// v1.42 — read-through the per-user derived cache with
// stale-while-revalidate, like the batch grid beside it. This single-score
// read was the one derived surface that recomputed on every request: the
// score detail sheet asks for it on each open, and a composite walks the
// rollup tier once per input. Same bucket, so the invalidation the batch
// relies on covers it unchanged (measurement writes evict or mark the
// `${userId}|` prefix stale, mood writes mark it stale). The module gate
// above and the AI assessment below stay per request.
const type = parsed.data.type ?? null;
const windowDays = parsed.data.windowDays;
const derived = await cachedSwr(
caches.insightsDerived as ServerCache<
Awaited<ReturnType<typeof computeDerivedMetric>>
>,
`${user.id}|single|${metric}|${type ?? ""}|${windowDays ?? ""}`,
async () =>
computeDerivedMetric({
metric,
userId: user.id,
profile: await loadBaselineProfile(prisma, user.id),
type,
windowDays,
}),
annotate,
);
annotate({
action: { name: "insights.derived" },
meta: {
metric,
status: derived.status,
// Both halves of the window story: what the caller asked for (null when
// they asked for nothing) and what the engine actually read. They differ
// for a metric that composes fixed per-pillar windows, and a dashboard
// that sees them drift apart is looking at a real answer, not a bug.
window_days_requested: parsed.data.windowDays ?? null,
window_days_effective: derived.provenance.windowDays,
},
});
// v1.13.2 — additive per-score assessment: a short "why is this score what
// it is" explanation, keyed to the SAME id the caller passed (READINESS,
// SLEEP_SCORE, RECOVERY_SCORE, STRAIN_SCORE, STRESS_SCORE). Null for any
// other id, and null when status !== "ok". Always a non-empty deterministic
// text when present (the demo + provider-less accounts fill); warmer AI
// prose overrides it once cached. The warm enqueue is fire-and-forget — this
// never blocks on an LLM round-trip.
const localeParam = request.nextUrl.searchParams.get("locale");
const resolvedLocale = await resolveServerLocale({
request,
userLocale: user.locale ?? null,
override: localeParam,
});
// The model-written override is served, and warmed, only while
// `statusText` is available. Otherwise the deterministic text stands and
// `ai` says why there is no warmer prose.
const ai = await aiCapabilityToServe(user.id, "statusText");
const assessment = await resolveDerivedAssessment({
metric,
userId: user.id,
derived,
locale: resolvedLocale,
aiAvailable: ai.available,
});
// Flatten the discriminated union for the wire: `metric` tags it,
// `value`/`reason` are nullable so iOS decodes one stable shape.
return apiSuccess({
metric,
status: derived.status,
value: derived.status === "ok" ? derived.value : null,
coverage: derived.coverage,
confidence: derived.status === "ok" ? derived.confidence : null,
provenance: derived.provenance,
reason: derived.status === "insufficient" ? derived.reason : null,
assessment,
ai,
});
});