perf(illness): compute the recovery gap lazily and gate it on signal density

The illness list load triggered the cross-episode correlation fan-out on
every mount, so the page waited on up to 24 per-episode reads before it
painted. Move that work behind an explicit opt-in: the insights endpoint
takes an includeRecoveryGap query param (default off), the list reads the
count breakdown on a single fast query, and the recovery gap is computed
only when the viewer opens the Analyse expansion.

Tighten the typical-gap gate against coincidental data. A gap now counts
only from an episode that cleared a per-episode measurement floor
(MIN_GAP_MEASUREMENT_DAYS), at least MIN_EPISODES_FOR_TYPICAL_GAP such
episodes must qualify, and the median magnitude must reach
MIN_TYPICAL_GAP_MAGNITUDE_DAYS — a 0/+-1-day gap is the coincidental
baseline and is withheld as noise.
This commit is contained in:
Marc-André Bombeck committed 2026-06-20 11:58:39 +02:00
1 parent a5b3715a46
commit 1de63f2b5c
15 files changed
+313 -84

No files matched your search

+10
View File
@@ -182,7 +182,17 @@ export type IllnessDayLogListQuery = z.infer<
* The retrospective-insight window query: `GET /api/illness/insights?
* windowDays=365`. Bounded 30..1095 days; defaults to a trailing year.
* Retrospective only — the engine summarises past episodes, never forecasts.
*
* `includeRecoveryGap` (default false) gates the EXPENSIVE per-episode
* correlation fan-out. The illness LIST loads with it off so the page paints
* on a single fast count query; only the explicit "Analyse" expansion sets it
* true and pays for the recovery-gap computation. Withholding it leaves the
* typical-gap null (the surface shows the calm "still learning" line).
*/
export const illnessInsightsQuerySchema = z.object({
windowDays: z.coerce.number().int().min(30).max(1095).optional(),
includeRecoveryGap: z
.enum(["true", "false"])
.transform((v) => v === "true")
.optional(),
});