fix(insights): correct medication compliance and per-user insight bucketing

A cluster of related correctness gaps in the compliance denominator and the
insight surfaces that read it:

- Paused medications. A doctor-ordered pause flipped `active` off and stamped
  `pausedAt`, but resume nulled `pausedAt`, so the interval was irrecoverable
  and every paused day became a missed dose after resume — collapsing
  adherence across the Health Score, the BP gate, the Coach snapshot and the
  report. Persist pause intervals as durable eras (new `MedicationPauseEra`,
  migration 0211), close the open era on resume, thread the eras through the
  compliance context and drop slots inside a paused interval from the
  denominator. Every compliance call site now fetches and passes the eras.

- Health Score eras. The Health-Score fast path was the only compliance
  caller that skipped the shared schedule select and schedule revisions, so
  after a schedule edit it scored past days against the current schedule
  while every other surface used the historical era. Switch it to the shared
  select and thread revisions (and pause eras) like the canonical path.

- Insight bucketing timezone. The per-metric status series bucketed every
  user in Europe/Berlin, so a near-midnight reading landed in the wrong day
  and the relative-day phrasing was off for non-Berlin self-hosters. Thread
  the resolved user timezone and key off `userDayKey`.

- Correlation significance. User-facing correlations surfaced from five
  paired points with strength read straight off the coefficient. Route them
  through the rigorous engine (at least twenty pairs, Student-t p below 0.05)
  so small-N coincidences no longer render as confident findings.

- Resting-pulse input. The mood-versus-"resting pulse" correlation fed the
  raw PULSE day-mean, which folds in workout HR, under a "Resting pulse"
  label. Feed `resolveRestingPulseSeries` (clean RESTING_HEART_RATE, else a
  daily low-percentile proxy) so the label matches the input.
This commit is contained in:
Marc-André Bombeck committed 2026-06-28 03:28:37 +02:00
1 parent 9e6a58b186
commit a827fe2ca7
33 files changed
+1007 -182

No files matched your search

@@ -0,0 +1,250 @@
/**
* v1.25 correctness fixes — two compliance-denominator regressions.
*
* H-MED1: a paused medication's gap must NOT count as missed once the
* medication resumes. The pause interval lives in `MedicationPauseEra`
* (threaded onto `ComplianceMedicationContext.pauseEras`); the ledger drops
* expected slots whose anchor falls inside `[pausedAt, resumedAt ?? now)` so
* the denominator never inflates with the paused days.
*
* M-MED3: the compliance rate must honour archived schedule eras
* (`scheduleRevisions`). A past day scores against the schedule that was live
* THEN, not the current one. This pins that threading `scheduleRevisions`
* through `buildComplianceMedicationContext` changes the rate — the exact
* behaviour the Health-Score fast-path was missing (it never threaded eras).
*
* All instants are UTC; run under `TZ=UTC` so the day grid is deterministic.
*/
import { describe, it, expect } from "vitest";
import {
buildComplianceMedicationContext,
tallyComplianceFromLedger,
type ComplianceSchedule,
type MedicationPauseEraLike,
} from "../compliance";
import type { ScheduleRevisionLike } from "@/lib/medications/scheduling/schedule-eras";
const TZ = "UTC";
/** A taken intake for the 08:00 slot of `day` (08:15 → on-time). */
function taken08(day: Date) {
const scheduledFor = new Date(day);
scheduledFor.setUTCHours(8, 0, 0, 0);
const takenAt = new Date(scheduledFor.getTime() + 15 * 60_000);
return { scheduledFor, takenAt, skipped: false };
}
function dayUTC(y: number, m: number, d: number): Date {
return new Date(Date.UTC(y, m - 1, d, 0, 0, 0, 0));
}
describe("H-MED1 — paused days drop out of the compliance denominator", () => {
// Daily 08:00 medication. Window June 1 – June 28; the user takes every
// day EXCEPT June 10–19 (the pause window) where nothing is logged.
const createdAt = dayUTC(2026, 6, 1);
const endsOn = dayUTC(2026, 6, 28);
const from = dayUTC(2026, 6, 1);
const to = dayUTC(2026, 6, 29);
const now = dayUTC(2026, 6, 29);
const schedules: ComplianceSchedule[] = [
{
windowStart: "08:00",
windowEnd: "09:00",
daysOfWeek: null,
rrule: "FREQ=DAILY",
rollingIntervalDays: null,
timesOfDay: ["08:00"],
scheduleType: "SCHEDULED",
cyclicOnWeeks: null,
cyclicOffWeeks: null,
doseWindows: null,
},
];
// Taken June 1–9 and June 20–28; nothing logged June 10–19 (paused).
const events = [
...[1, 2, 3, 4, 5, 6, 7, 8, 9].map((d) => taken08(dayUTC(2026, 6, d))),
...[20, 21, 22, 23, 24, 25, 26, 27, 28].map((d) =>
taken08(dayUTC(2026, 6, d)),
),
];
const pauseEras: MedicationPauseEraLike[] = [
{ pausedAt: dayUTC(2026, 6, 10), resumedAt: dayUTC(2026, 6, 20) },
];
const medBase = {
startsOn: createdAt,
endsOn,
oneShot: false,
createdAt,
};
it("WITHOUT pauseEras the paused days collapse to missed", () => {
const ctx = buildComplianceMedicationContext(medBase, null, TZ);
const tally = tallyComplianceFromLedger(
events,
schedules,
ctx,
from,
to,
now,
);
// June 10–19 = 10 expected slots with no intake → 10 misses.
expect(tally.missed).toBe(10);
expect(tally.taken).toBe(18);
expect(tally.denominator).toBe(28);
});
it("WITH pauseEras the paused days are excluded → no missed", () => {
const ctx = buildComplianceMedicationContext(
{ ...medBase, pauseEras },
null,
TZ,
);
const tally = tallyComplianceFromLedger(
events,
schedules,
ctx,
from,
to,
now,
);
// The 10 paused slots are dropped from the ledger entirely.
expect(tally.missed).toBe(0);
expect(tally.taken).toBe(18);
expect(tally.denominator).toBe(18);
expect(tally.rate).toBe(100);
});
it("the resumed day (resumedAt boundary) still counts", () => {
// No intake on June 20 (the resume day) → it must count as missed: the
// exclusion is half-open `[pausedAt, resumedAt)`, so June 20 is live.
const eventsMissResumeDay = [
...[1, 2, 3, 4, 5, 6, 7, 8, 9].map((d) => taken08(dayUTC(2026, 6, d))),
...[21, 22, 23, 24, 25, 26, 27, 28].map((d) =>
taken08(dayUTC(2026, 6, d)),
),
];
const ctx = buildComplianceMedicationContext(
{ ...medBase, pauseEras },
null,
TZ,
);
const tally = tallyComplianceFromLedger(
eventsMissResumeDay,
schedules,
ctx,
from,
to,
now,
);
// June 20 is past the pause window and was not taken → exactly 1 miss.
expect(tally.missed).toBe(1);
});
});
describe("M-MED3 — the compliance rate honours archived schedule eras", () => {
// The user ran a TWICE-daily schedule (08:00 + 20:00) for the first half of
// the window, then edited it down to ONCE daily (08:00). Throughout, only
// the 08:00 dose is ever taken. With the era threaded, the old-era days
// expect two doses (one missed each); without it, every day is scored
// against the current one-dose schedule and the rate is a misleading 100 %.
const createdAt = dayUTC(2026, 6, 1);
const replaceAt = dayUTC(2026, 6, 15); // the edit instant
const endsOn = dayUTC(2026, 6, 28);
const from = dayUTC(2026, 6, 1);
const to = dayUTC(2026, 6, 29);
const now = dayUTC(2026, 6, 29);
// Current (live) schedule: one dose at 08:00.
const liveSchedules: ComplianceSchedule[] = [
{
windowStart: "08:00",
windowEnd: "09:00",
daysOfWeek: null,
rrule: "FREQ=DAILY",
rollingIntervalDays: null,
timesOfDay: ["08:00"],
scheduleType: "SCHEDULED",
cyclicOnWeeks: null,
cyclicOffWeeks: null,
doseWindows: null,
},
];
// Archived era: the superseded twice-daily schedule (08:00 + 20:00).
const oldEraRevision: ScheduleRevisionLike = {
id: "rev-twice-daily",
validFrom: createdAt,
validUntil: replaceAt,
payload: [
{
timesOfDay: ["08:00", "20:00"],
windowStart: "08:00",
windowEnd: "20:00",
daysOfWeek: null,
rrule: "FREQ=DAILY",
rollingIntervalDays: null,
scheduleType: "SCHEDULED",
cyclicOnWeeks: null,
cyclicOffWeeks: null,
doseWindows: null,
label: null,
dose: null,
reminderGraceMinutes: null,
},
],
};
// Only the 08:00 dose is taken, every day June 1–28. The 20:00 dose that
// the old era expected on June 1–14 is never taken.
const events = [
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28,
].map((d) => taken08(dayUTC(2026, 6, d)));
const medBase = {
startsOn: createdAt,
endsOn,
oneShot: false,
createdAt,
};
it("WITHOUT scheduleRevisions every day scores against the current one-dose schedule → 100 %", () => {
const ctx = buildComplianceMedicationContext(medBase, null, TZ);
const tally = tallyComplianceFromLedger(
events,
liveSchedules,
ctx,
from,
to,
now,
);
expect(tally.missed).toBe(0);
expect(tally.rate).toBe(100);
});
it("WITH scheduleRevisions the old-era days expect the second dose → rate drops", () => {
const ctx = buildComplianceMedicationContext(
{ ...medBase, scheduleRevisions: [oldEraRevision] },
null,
TZ,
);
const tally = tallyComplianceFromLedger(
events,
liveSchedules,
ctx,
from,
to,
now,
);
// June 1–14 expected the 20:00 dose too; it was never taken → 14 misses.
expect(tally.missed).toBe(14);
expect(tally.rate).toBeLessThan(100);
// The era path strictly raises the denominator vs the no-era path.
expect(tally.denominator).toBeGreaterThan(28);
});
});
@@ -77,8 +77,8 @@ describe("computeCorrelationHypothesesFastPath", () => {
expect(CORRELATION_WINDOW_DAYS).toBe(28);
});
describe("rollup-fast-path — SYS / PULSE / WEIGHT covered", () => {
it("reads per-day means from measurement_rollups, never from measurements", async () => {
describe("rollup-fast-path — SYS / WEIGHT covered", () => {
it("reads SYS + WEIGHT per-day means from measurement_rollups (PULSE no longer)", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["PULSE", true],
@@ -88,7 +88,6 @@ describe("computeCorrelationHypothesesFastPath", () => {
PROBE.mockResolvedValue(coverage);
ROLLUP_FIND_MANY.mockResolvedValueOnce([]) // SYS buckets
.mockResolvedValueOnce([]) // PULSE buckets
.mockResolvedValueOnce([]); // WEIGHT buckets
const result = await computeCorrelationHypothesesFastPath({
@@ -101,9 +100,14 @@ describe("computeCorrelationHypothesesFastPath", () => {
expect(result.path).toBe("rollup");
expect(result.windowDays).toBe(28);
expect(result.degraded).toBe(false);
// No raw measurement reads when on rollup-fast-path.
expect(MEASUREMENT_FIND_MANY).not.toHaveBeenCalled();
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(3);
// v1.2.5 (M-CS2) — SYS + WEIGHT ride the rollup tier; PULSE no
// longer does. The mood × resting-pulse hypothesis instead reads the
// RESTING_HEART_RATE rows (and, only when absent, RAW PULSE for the
// low-percentile proxy) from `measurements`, so the rollup count
// drops to 2 and the resting reads land on `measurements`.
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(2);
// RESTING read returns [] (default mock) → RAW PULSE proxy read too.
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(2);
// Mood + intake reads always live.
expect(MOOD_FIND_MANY).toHaveBeenCalledTimes(1);
expect(INTAKE_FIND_MANY).toHaveBeenCalledTimes(1);
@@ -145,9 +149,7 @@ describe("computeCorrelationHypothesesFastPath", () => {
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]) // SYS rows
.mockResolvedValueOnce([]) // PULSE rows
.mockResolvedValueOnce([]); // WEIGHT rows
MEASUREMENT_FIND_MANY.mockResolvedValue([]); // SYS / WEIGHT / RESTING / PULSE
const result = await computeCorrelationHypothesesFastPath({
userId: "user-partial",
@@ -157,7 +159,9 @@ describe("computeCorrelationHypothesesFastPath", () => {
});
expect(result.path).toBe("live");
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(3);
// SYS + WEIGHT (live series) + RESTING + RAW-PULSE proxy (resting
// series, since RESTING returns [] under the default mock) = 4 reads.
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(4);
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
});
@@ -219,8 +223,9 @@ describe("computeCorrelationHypothesesFastPath", () => {
});
expect(result.path).toBe("rollup");
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(3);
expect(MEASUREMENT_FIND_MANY).not.toHaveBeenCalled();
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(2);
// Resting-pulse reads (RESTING + RAW-PULSE proxy) land on measurements.
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(2);
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const corrCall = calls.find((c) => c?.meta?.correlations !== undefined);
@@ -248,7 +253,7 @@ describe("computeCorrelationHypothesesFastPath", () => {
});
expect(result.path).toBe("live");
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(3);
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(4);
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
@@ -280,4 +285,58 @@ describe("computeCorrelationHypothesesFastPath", () => {
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
});
});
describe("mood × resting pulse uses the RESTING series, not the raw PULSE mean (M-CS2)", () => {
it("scores the low-percentile resting proxy, so workout HR can't inflate the pair", async () => {
// One day with a handful of resting-low readings buried under a dense
// workout burst. The OLD code paired mood against the day's raw PULSE
// MEAN (~120 bpm — workout-inflated). The fix routes through
// `resolveRestingPulseSeries`, whose proxy is the day's 20th-percentile
// (the resting floor), so the paired value sits in the ~50s, not ~120.
const day = new Date("2026-05-16T10:00:00.000Z"); // Berlin 12:00 → 05-16
const pulseValues = [55, 57, 59, 150, 151, 152, 153, 154, 155, 156];
const rawMean =
pulseValues.reduce((s, v) => s + v, 0) / pulseValues.length; // ~124.2
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", false], // force the live path
]);
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockImplementation(
async (args: {
where?: { type?: string };
}): Promise<Array<{ id: string; measuredAt: Date; value: number }>> => {
const type = args?.where?.type;
if (type === "PULSE") {
// No RESTING_HEART_RATE rows exist, so the proxy reads RAW PULSE.
return pulseValues.map((value, i) => ({
id: `pulse-${i}`,
measuredAt: day,
value,
}));
}
// SYS / WEIGHT / RESTING_HEART_RATE → empty.
return [];
},
);
MOOD_FIND_MANY.mockResolvedValue([{ score: 4, moodLoggedAt: day }]);
const result = await computeCorrelationHypothesesFastPath({
userId: "user-resting",
userTz: "Europe/Berlin",
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
// n = 1 day → below the n>=20 surfacing gate, but the points preview
// still carries the paired resting value the runner computed.
expect(result.moodPulse.points).toHaveLength(1);
const y = result.moodPulse.points[0].y;
// The paired pulse is the resting floor (~50s), NOT the raw day-mean.
expect(y).toBeLessThan(70);
expect(y).toBeLessThan(rawMean - 40);
});
});
});
@@ -1,6 +1,11 @@
import { describe, it, expect } from "vitest";
import type { DataPoint } from "../trends";
import { pairByTimestamp, pearsonCorrelation } from "../correlations";
import {
pairByTimestamp,
pearsonCorrelation,
significantPearsonCorrelation,
} from "../correlations";
import type { PairedPoint } from "../correlations";
function makePoints(values: number[], startDaysAgo = 30): DataPoint[] {
const now = Date.now();
@@ -102,3 +107,42 @@ describe("pearsonCorrelation", () => {
expect(result.strength).toBe("keine");
});
});
describe("significantPearsonCorrelation (M-CS3 — n>=20 AND p<0.05)", () => {
const mk = (a: number, b: number): PairedPoint => ({
a,
b,
date: new Date(),
});
it("suppresses a 5-pair r≈0.7 dataset (below the n>=20 floor)", () => {
// Five points with a strong-looking slope — exactly the small-n fluke the
// plain `pearsonCorrelation` would surface as a "stark" correlation.
const pairs = [mk(1, 1), mk(2, 2), mk(3, 2), mk(4, 4), mk(5, 3)];
// Sanity-check the legacy path WOULD have surfaced it (non-null, n=5).
const legacy = pearsonCorrelation(pairs);
expect(legacy).not.toBeNull();
expect(legacy!.n).toBe(5);
// The gated path refuses it — too few pairs.
expect(significantPearsonCorrelation(pairs)).toBeNull();
});
it("surfaces a >=20-pair significant dataset", () => {
// 20 points on a near-perfect line → high r, vanishingly small p.
const pairs = Array.from({ length: 20 }, (_, i) =>
mk(i, i * 2 + (i % 2 === 0 ? 0.3 : -0.3)),
);
const result = significantPearsonCorrelation(pairs);
expect(result).not.toBeNull();
expect(result!.n).toBe(20);
expect(result!.strength).toBe("stark");
expect(result!.r).toBeGreaterThan(0.9);
});
it("suppresses a 20-pair NON-significant (noisy) dataset", () => {
// 20 points with no real relationship → p well above 0.05.
const ys = [5, 2, 7, 1, 6, 3, 8, 2, 5, 4, 6, 1, 7, 3, 5, 2, 6, 4, 5, 3];
const pairs = ys.map((y, i) => mk(i, y));
expect(significantPearsonCorrelation(pairs)).toBeNull();
});
});
+56 -1
View File
@@ -436,6 +436,24 @@ export interface ComplianceMedicationContext {
* callers without revisions keep the live-only expansion.
*/
scheduleRevisions?: ScheduleRevisionLike[];
/**
* v1.25 H-MED1 — durable pause intervals (`MedicationPauseEra`). When
* present, expected dose slots whose anchor falls inside any
* `[pausedAt, resumedAt ?? now)` interval are dropped from the ledger so a
* resumed medication never counts the paused days as missed. Optional:
* callers without eras keep counting every expected slot.
*/
pauseEras?: MedicationPauseEraLike[];
}
/**
* v1.25 H-MED1 — the pause-interval projection the compliance engine reads.
* A Prisma `MedicationPauseEra` row (or a `{ pausedAt, resumedAt }` literal)
* drops in. An open era (`resumedAt === null`) runs to `now`.
*/
export interface MedicationPauseEraLike {
pausedAt: Date;
resumedAt: Date | null;
}
/**
@@ -452,6 +470,8 @@ export function buildComplianceMedicationContext(
createdAt: Date;
/** v1.16.3 — thread the archived eras when the bundle carries them. */
scheduleRevisions?: ScheduleRevisionLike[];
/** v1.25 H-MED1 — thread the pause eras when the caller carries them. */
pauseEras?: MedicationPauseEraLike[];
},
lastIntakeAt: Date | null,
timeZone: string,
@@ -464,6 +484,7 @@ export function buildComplianceMedicationContext(
lastIntakeAt,
timeZone,
...(med.scheduleRevisions && { scheduleRevisions: med.scheduleRevisions }),
...(med.pauseEras && { pauseEras: med.pauseEras }),
};
}
@@ -1348,7 +1369,41 @@ export function buildComplianceLedgerRows(
pinned: e.attributionSource === "USER_PIN",
}));
return reconstructDoseHistory(bands, intakes, now);
const rows = reconstructDoseHistory(bands, intakes, now);
// v1.25 H-MED1 — drop expected dose slots whose anchor falls inside a
// pause interval. While a medication is paused no dose is expected, so a
// slot minted across the paused window must never count as "missed" (the
// denominator-inflating status). Only `slot` rows are dropped, and only
// those whose status feeds the tally (taken / missed) — skip / ad-hoc /
// upcoming rows are already excluded from the denominator, so dropping
// them here would be a redundant double-exclusion. An open era
// (`resumedAt === null`) runs to `now`.
const pauseEras = ctx.pauseEras;
if (pauseEras && pauseEras.length > 0) {
const isInPause = (at: Date): boolean => {
const t = at.getTime();
for (const era of pauseEras) {
const start = era.pausedAt.getTime();
const end = (era.resumedAt ?? now).getTime();
if (t >= start && t < end) return true;
}
return false;
};
return rows.filter((row) => {
if (row.kind !== "slot") return true;
if (
row.status !== "taken_on_time" &&
row.status !== "taken_late" &&
row.status !== "missed"
) {
return true;
}
return !isInPause(row.at);
});
}
return rows;
}
/**
+57 -36
View File
@@ -23,14 +23,17 @@
* The meta-dict annotate carries `window_days: 28` as a sentinel
* so the UI / docs can quote the truthful window.
*
* 2. **Probe-gated SYS / PULSE / WEIGHT** — when DAY-bucket coverage
* exists for all three types the runner hydrates the per-day-mean
* maps from `measurement_rollups`. Each rollup row already
* 2. **Probe-gated SYS / WEIGHT** — when DAY-bucket coverage exists
* for both types the runner hydrates the per-day-mean / per-day
* weight maps from `measurement_rollups`. Each rollup row already
* carries the day's `mean` value computed by Postgres' `AVG`, so
* the pairing logic is byte-for-byte identical to the live path's
* "average the day's raw values" step.
* "average the day's raw values" step. PULSE is intentionally NOT
* hydrated from the rollup tier — the mood × resting-pulse
* hypothesis scores the RESTING series (RESTING_HEART_RATE rows,
* else a low-percentile RAW-PULSE proxy), never the day-mean.
*
* 3. **Mood + medication intake** — no rollup equivalent today; both
* 3. **Mood + medication intake + resting pulse** — no rollup equivalent today; both
* reads stay live regardless of coverage. The mood read is small
* (per-day entries, bounded by the 28-day window) and the
* medication intake read scales with active medications × 28 days
@@ -57,6 +60,7 @@ import {
correlateWeightWeekday,
type CorrelationResult,
} from "@/lib/insights/correlations";
import { resolveRestingPulseSeries } from "@/lib/analytics/resting-pulse";
import { isNearUtc, userDayKey } from "@/lib/tz/resolver";
import { wallClockInTz } from "@/lib/tz/wall-clock";
@@ -141,19 +145,24 @@ export async function computeCorrelationHypothesesFastPath(
// local-day buckets and this guard can come back down.
const userNearUtc = isNearUtc(userTz, now);
// v1.4.38.8 — gate only on the three types this helper actually
// reads (SYS / PULSE / WEIGHT). The prior `isFullyCovered &&` AND
// poisoned the correlations fast-path with any unrelated brand-new
// type the user had logged, even when SYS + PULSE + WEIGHT were
// fully covered. Per-type coverage is the correct gate.
// v1.4.38.8 — gate only on the types this helper reads from the rollup
// tier (SYS / WEIGHT). The prior `isFullyCovered &&` AND poisoned the
// correlations fast-path with any unrelated brand-new type the user had
// logged, even when those were fully covered. Per-type coverage is the
// correct gate.
//
// v1.2.5 (M-CS2) — PULSE is no longer hydrated from the rollup tier:
// the mood × "resting pulse" hypothesis now scores the RESTING series
// (RESTING_HEART_RATE rows, else a low-percentile PULSE proxy from RAW
// samples), not the day-mean PULSE the rollup carries. A day-mean folds
// in workout HR and inflates the "resting" figure for Apple-Health
// users, so the rollup pulse-mean is structurally the wrong input here.
const measurementsOnRollups =
userNearUtc &&
coverage.get("BLOOD_PRESSURE_SYS") === true &&
coverage.get("PULSE") === true &&
coverage.get("WEIGHT") === true;
let dailySysMean: Map<string, number>;
let dailyPulseMean: Map<string, number>;
let weightRows: ChunkedRow[];
if (measurementsOnRollups) {
@@ -173,7 +182,7 @@ export async function computeCorrelationHypothesesFastPath(
// v1.11.2 — load the source-priority blob once and thread it into every
// reader so the per-type fan-out doesn't re-query the user 3×.
const priority = await loadUserSourcePriority(userId);
const [sysBuckets, pulseBuckets, weightBuckets] = await Promise.all([
const [sysBuckets, weightBuckets] = await Promise.all([
readRollupBuckets(
userId,
"BLOOD_PRESSURE_SYS",
@@ -182,17 +191,12 @@ export async function computeCorrelationHypothesesFastPath(
now,
priority,
),
readRollupBuckets(userId, "PULSE", "DAY", since, now, priority),
readRollupBuckets(userId, "WEIGHT", "DAY", since, now, priority),
]);
dailySysMean = new Map();
for (const b of sysBuckets) {
dailySysMean.set(userDayKey(b.bucketStart, userTz), b.mean);
}
dailyPulseMean = new Map();
for (const b of pulseBuckets) {
dailyPulseMean.set(userDayKey(b.bucketStart, userTz), b.mean);
}
// The weekday hypothesis needs the per-event weight series for
// ANOVA — the runner expects one row per measurement so the group
// counts reflect reality. Hydrate from buckets by emitting one
@@ -207,9 +211,8 @@ export async function computeCorrelationHypothesesFastPath(
}));
} else {
// Live fallback — chunked reads against the measurements table.
const [sysRows, pulseRows, weightRowsLive] = await Promise.all([
const [sysRows, weightRowsLive] = await Promise.all([
fetchSeriesChunked(userId, "BLOOD_PRESSURE_SYS", since),
fetchSeriesChunked(userId, "PULSE", since),
fetchSeriesChunked(userId, "WEIGHT", since),
]);
dailySysMean = new Map();
@@ -224,21 +227,39 @@ export async function computeCorrelationHypothesesFastPath(
const sum = list.reduce((s, v) => s + v, 0);
dailySysMean.set(key, sum / list.length);
}
dailyPulseMean = new Map();
const pulseBuckets = new Map<string, number[]>();
for (const r of pulseRows) {
const key = userDayKey(r.measuredAt, userTz);
const list = pulseBuckets.get(key) ?? [];
list.push(r.value);
pulseBuckets.set(key, list);
}
for (const [key, list] of pulseBuckets.entries()) {
const sum = list.reduce((s, v) => s + v, 0);
dailyPulseMean.set(key, sum / list.length);
}
weightRows = weightRowsLive;
}
// v1.2.5 (M-CS2) — resolve the RESTING-pulse series the mood × pulse
// hypothesis scores against. Prefer the clean RESTING_HEART_RATE rows;
// only when the user has none do we pull RAW PULSE samples for the
// low-percentile daily proxy (the proxy needs the per-day distribution,
// not the day-mean — a day-mean folds in workout HR). Both reads are
// raw because the proxy floor is a percentile, not an AVG. Keying the
// proxy on `userDayKey(d, userTz)` aligns its buckets with the mood /
// intake day keys used below.
const restingSamples = await fetchSeriesChunked(
userId,
"RESTING_HEART_RATE",
since,
);
const pulseSamplesForResting =
restingSamples.length === 0
? await fetchSeriesChunked(userId, "PULSE", since)
: [];
const { series: restingPulseSeries } = resolveRestingPulseSeries({
restingSamples,
pulseSamples: pulseSamplesForResting,
dayKeyOf: (d) => userDayKey(d, userTz),
});
// One resting figure per user-local day. RESTING_HEART_RATE can carry
// more than one row/day (rare); last-write-wins is fine for a single
// daily resting figure, and the proxy already emits one row per day.
const dailyRestingPulse = new Map<string, number>();
for (const s of restingPulseSeries) {
dailyRestingPulse.set(userDayKey(s.measuredAt, userTz), s.value);
}
// Mood + intake — always live. Both reads are small (28-day window,
// narrow projections) and neither has a rollup equivalent today.
const [moodRows, intakeRows] = await Promise.all([
@@ -298,13 +319,13 @@ export async function computeCorrelationHypothesesFastPath(
restingPulse: number;
}> = [];
for (const [key, moodScores] of dailyMood.entries()) {
const meanPulse = dailyPulseMean.get(key);
if (meanPulse === undefined) continue;
const restingPulse = dailyRestingPulse.get(key);
if (restingPulse === undefined) continue;
const meanMood = moodScores.reduce((s, v) => s + v, 0) / moodScores.length;
moodPulsePairs.push({
date: dateFromDayKey(key),
mood: meanMood,
restingPulse: meanPulse,
restingPulse,
});
}
const moodPulse = correlateMoodPulse({ daily: moodPulsePairs });
@@ -349,7 +370,7 @@ export async function computeCorrelationHypothesesFastPath(
async function fetchSeriesChunked(
userId: string,
type: "BLOOD_PRESSURE_SYS" | "PULSE" | "WEIGHT",
type: "BLOOD_PRESSURE_SYS" | "PULSE" | "WEIGHT" | "RESTING_HEART_RATE",
since: Date,
): Promise<ChunkedRow[]> {
const out: ChunkedRow[] = [];
+47
View File
@@ -4,6 +4,7 @@
*/
import type { DataPoint } from "./trends";
import { pearson, MAX_P_VALUE } from "@/lib/insights/correlations";
export interface PairedPoint {
a: number;
@@ -104,3 +105,49 @@ export function pearsonCorrelation(
return { r, strength, n };
}
/**
* v1.2.5 (M-CS3) — significance-gated Pearson for USER-FACING surfaces.
*
* The plain `pearsonCorrelation` above assigns a strength band from |r|
* alone with no significance test and a default floor of only 5 pairs —
* so a 5-point r ≈ 0.7 fluke surfaces as a "stark" correlation. Every
* surfaced insight-card / status-snapshot correlation must instead clear
* the same rigorous bar the `/insights` correlation engine enforces:
*
* - n >= 20 paired points (the engine's `MIN_PAIRED_N`), AND
* - a two-sided Student-t p-value < 0.05 (`MAX_P_VALUE`).
*
* Below either bar this returns `null` so the caller suppresses the
* correlation exactly as it already does for the < minPairs case — no
* call-site shape change. The returned `{ r, strength, n }` keeps the
* legacy shape (German strength band) so the snapshot byte-shape is
* unchanged on the happy path.
*
* This routes through the engine's `pearson` (exact regularised-
* incomplete-beta p-value) rather than re-deriving the t-test, so the
* surface decision shares ONE definition with the dedicated cards.
*/
export function significantPearsonCorrelation(
pairs: PairedPoint[],
): CorrelationResult | null {
const result = pearson({
xs: pairs.map((p) => p.a),
ys: pairs.map((p) => p.b),
});
// `pearson` defaults `minPairs` to MIN_PAIRED_N (20); below it (or with
// zero variance) it reports `insufficient` and we suppress the card.
if (result.status !== "ok") return null;
// Significance gate — a non-significant coefficient never surfaces.
if (result.pValue >= MAX_P_VALUE) return null;
const r = Math.round(result.r * 1000) / 1000;
const absR = Math.abs(r);
let strength: CorrelationResult["strength"];
if (absR >= 0.7) strength = "stark";
else if (absR >= 0.4) strength = "moderat";
else if (absR >= 0.2) strength = "schwach";
else strength = "keine";
return { r, strength, n: result.n };
}
+28 -20
View File
@@ -50,6 +50,7 @@ import {
buildComplianceMedicationContext,
calculateCompliance,
lastNonSkippedTakenAt,
SCHEDULE_COMPLIANCE_SELECT,
} from "./compliance";
import { resolveUserTimezone } from "@/lib/tz/resolver";
import {
@@ -345,29 +346,26 @@ export async function computeUserHealthScoreFastPath(
startsOn: true,
endsOn: true,
oneShot: true,
schedules: {
// v1.25 M-MED3 — route through the SHARED compliance select like every
// other caller, so the Health-Score pillar honours the same per-dose
// on-time windows + cadence fields the dashboard / detail surfaces use.
schedules: { select: SCHEDULE_COMPLIANCE_SELECT },
// v1.25 M-MED3 — archived schedule eras so a past day scores against
// the schedule that was live THEN, not the current one (every other
// compliance caller already threads these). The builder copies them
// through into the context.
scheduleRevisions: {
orderBy: { validFrom: "asc" },
select: {
windowStart: true,
windowEnd: true,
// v1.5.0 — cadence-aware compliance reads daysOfWeek so a
// weekly med (Mondays only) doesn't get a 30-day denominator
// that depresses the score by ~85 percentage points. Closes #214.
daysOfWeek: true,
// v1.7.0 SB-SCHED-2 — widen the select so the engine reads
// RRULE / rolling / PRN / cyclic, not just the legacy
// daysOfWeek string.
rrule: true,
rollingIntervalDays: true,
timesOfDay: true,
reminderGraceMinutes: true,
scheduleType: true,
cyclicOnWeeks: true,
cyclicOffWeeks: true,
// v1.15.18 — per-dose window so the score's compliance honours the
// same on-time band the detail % + history use.
doseWindows: true,
id: true,
validFrom: true,
validUntil: true,
payload: true,
supersededByRevisionId: true,
},
},
// v1.25 H-MED1 — pause eras so paused days drop out of the denominator.
pauseEras: { select: { pausedAt: true, resumedAt: true } },
},
}),
]);
@@ -428,6 +426,16 @@ export async function computeUserHealthScoreFastPath(
takenAt: e.takenAt ? new Date(e.takenAt.getTime() + 7 * DAY_MS) : null,
skipped: e.skipped,
}));
// v1.25 M-MED3 — `med` (and so its scheduleRevisions + pauseEras) is
// passed UNshifted while the intake events shift +7d (the pre-existing
// approximation that lets the pinned `now` anchor still capture the prior
// logical 30 days). The revisions / pause eras are absolute historical
// records; no canonical previous-window caller shifts them, so they stay
// as-is here for consistency with the rest of the engine. Trade-off: in
// the trailing-30-day DELTA the era / pause segmentation can be up to 7
// days out of phase with the shifted slots. The primary (current-window)
// score — `medicationCompliance30` above — is exact; only the secondary
// trend delta carries this best-effort approximation.
const medicationContext = buildComplianceMedicationContext(
med,
lastNonSkippedTakenAt(shifted),