fix(hrv): keep RMSSD on the display surfaces for an account with no SDNN

Fitbit and Google Health HRV is filed as RMSSD since migration 0420, so
an account whose only HRV comes from them lost it wherever a display
surface read SDNN alone. The dashboard HRV tile keeps its RMSSD fallback
with Recovery off while the account has no SDNN, and its toggle stays
offered for either estimator. A briefing HRV signal counts as measured
today when either series has a reading. The doctor report already files
RMSSD as its own ungated leaf; a guard now holds that. Readiness, HRV
balance and stress stay on SDNN; a test pins that an RMSSD-only account
renormalises over the other components.
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 14:32:56 +02:00
1 parent 8d16799479
commit bf3830203f
18 files changed
+232 -66

No files matched your search

@@ -24,9 +24,9 @@ import type { DataSummary } from "@/lib/analytics/trends";
* The dashboard HRV tile must accept either HRV type.
*
* HRV is stored under two measurement types that are not the same measure:
* SDNN (`HEART_RATE_VARIABILITY`, Apple / Fitbit / Google Health) and nightly
* RMSSD (`HRV_RMSSD`, Oura / Polar / WHOOP, and a bridge pushing a Garmin-style
* feed). Every other HRV surface already unions them — `/insights/hrv` via
* SDNN (`HEART_RATE_VARIABILITY`, Apple Health / Health Connect) and nightly
* RMSSD (`HRV_RMSSD`, Oura / Polar / WHOOP / Fitbit / Google Health, and a
* bridge pushing a Garmin-style feed). Every other HRV surface already unions them — `/insights/hrv` via
* `fallbackMeasurementType`, the Coach via `COACH_SOURCE_MEASUREMENT_TYPES.hrv`,
* the MCP reads via `withHrvFallback` — and `sub-page-metric.ts` states that
* listing both is what keeps "the tab, pill, and dashboard tile lit whichever
@@ -141,16 +141,16 @@ describe("pickHrvSummary", () => {
});
describe("the RMSSD fallback cannot leak recovery-module data", () => {
it("drops HRV_RMSSD from the snapshot summaries when recovery is off", () => {
it("lists HRV_RMSSD among the recovery-off summary types", () => {
// The half of this fix that is easy to miss. `HRV_RMSSD` is recovery-owned
// in `METRIC_STATUS_MODULE_OWNERS`, which governs the metric-status and
// MCP reads — but the dashboard snapshot gates off `SUMMARY_TYPE_MODULE`,
// a DIFFERENT map, and that one did not list it. Inert while the tile read
// SDNN alone; the moment the tile falls back to RMSSD it would show a
// recovery-off account a tile built from recovery-owned rows.
// SDNN alone. The one exception, the tile's fallback for an account with
// no SDNN, is made in `gateSummariesByModules` (tested below).
expect(disabledSummaryTypes({ recovery: false })).toContain("HRV_RMSSD");
// SDNN is a plain vital and stays ungated — turning recovery off must not
// take the ordinary HRV tile away from an Apple / Fitbit account.
// take the ordinary HRV tile away from an Apple Health account.
expect(disabledSummaryTypes({ recovery: false })).not.toContain(
"HEART_RATE_VARIABILITY",
);
@@ -290,35 +290,74 @@ describe("the gate reaches every feed, not just the default one", () => {
expect(on.summaries.HRV_RMSSD).toBeDefined();
expect(summaries.HRV_RMSSD).toBeDefined();
});
it("keeps the RMSSD fallback for an account with no SDNN", () => {
// A Fitbit or Google Health account files its nightly HRV as RMSSD since
// migration 0420. On this tile RMSSD stands in for SDNN, a plain vital
// that paints with Recovery off, so the fallback must not vanish for the
// estimator the device reports.
const summaries = {
HEART_RATE_VARIABILITY: summary(0, 0),
HRV_RMSSD: summary(30, 42),
};
const lastSeen = {
HRV_RMSSD: { lastSeenAt: "2026-09-02T00:00:00.000Z" },
};
const off = gateSummariesByModules(summaries, lastSeen, {
recovery: false,
});
expect(off.summaries.HRV_RMSSD).toBeDefined();
expect(off.lastSeenByType.HRV_RMSSD).toBeDefined();
expect(pickHrvSummary(off.summaries).type).toBe("HRV_RMSSD");
// Another recovery-owned series is still stripped.
const withResting = gateSummariesByModules(
{ ...summaries, RESTING_HEART_RATE: summary(5, 58) },
lastSeen,
{ recovery: false },
);
expect(
(withResting.summaries as Record<string, unknown>).RESTING_HEART_RATE,
).toBeUndefined();
});
});
describe("a switch that cannot do anything is not offered", () => {
// The symptom one step on from the original bug: with recovery off, a
// ring / strap account's only HRV series is withheld, so the tile can
// never paint — and before this, the toggle was still offered, still
// turned on, and still explained nothing. Same experience as the defect
// this change exists to remove, so it does not get to survive here.
it("drops the HRV toggle for a ring account with recovery off", () => {
it("drops the HRV toggle for an account with recovery off and no HRV", () => {
expect(
unavailableWidgetIds({ recovery: false }, { hasSdnn: false }),
unavailableWidgetIds(
{ recovery: false },
{ hasSdnn: false, hasRmssd: false },
),
).toEqual(["hrv"]);
});
it("keeps the toggle for an account that has SDNN", () => {
// SDNN is a plain vital that no module owns, so the tile still works
// with recovery off. Taking this row away would be a new bug.
it("keeps the toggle for an account that has either estimator", () => {
expect(
unavailableWidgetIds({ recovery: false }, { hasSdnn: true }),
unavailableWidgetIds(
{ recovery: false },
{ hasSdnn: true, hasRmssd: false },
),
).toEqual([]);
expect(
unavailableWidgetIds(
{ recovery: false },
{ hasSdnn: false, hasRmssd: true },
),
).toEqual([]);
});
it("keeps the toggle whenever recovery is on", () => {
expect(
unavailableWidgetIds({ recovery: true }, { hasSdnn: false }),
unavailableWidgetIds(
{ recovery: true },
{ hasSdnn: false, hasRmssd: false },
),
).toEqual([]);
// Fail-open on an unresolved map, like the module gate beside it: only
// an explicit `false` takes a row away.
expect(unavailableWidgetIds({}, { hasSdnn: false })).toEqual([]);
expect(
unavailableWidgetIds({}, { hasSdnn: false, hasRmssd: false }),
).toEqual([]);
});
});
+12 -10
View File
@@ -235,7 +235,7 @@ export const GET = apiHandler(async () => {
// linger for minutes after Recovery went off, which is the confusion this
// is removing.
//
// The SDNN probe is an index-only existence check
// The HRV probes are index-only existence checks
// (`@@index([userId, type, measuredAt])`), not a summaries read; it runs
// only when Recovery is off, so the common account pays nothing.
// One flag, read the way this codebase reads one flag — not the whole
@@ -243,20 +243,22 @@ export const GET = apiHandler(async () => {
// is the only module that decides it.
const recoveryEnabled = await isModuleEnabled(user.id, "recovery");
let hasSdnn = true;
let hasRmssd = true;
if (!recoveryEnabled) {
const sdnnRow = await prisma.measurement.findFirst({
where: {
userId: user.id,
type: "HEART_RATE_VARIABILITY",
deletedAt: null,
},
select: { id: true },
});
const [sdnnRow, rmssdRow] = await Promise.all(
(["HEART_RATE_VARIABILITY", "HRV_RMSSD"] as const).map((type) =>
prisma.measurement.findFirst({
where: { userId: user.id, type, deletedAt: null },
select: { id: true },
}),
),
);
hasSdnn = sdnnRow !== null;
hasRmssd = rmssdRow !== null;
}
const unavailable = unavailableWidgetIds(
{ recovery: recoveryEnabled },
{ hasSdnn },
{ hasSdnn, hasRmssd },
);
return apiSuccess(
unavailable.length > 0
+3 -3
View File
@@ -11,9 +11,9 @@ import { EcgCrossLink } from "@/components/insights/ecg-cross-link";
* Heart-rate variability sub-page. Pairs with the iOS HKQuantityType
* `heartRateVariabilitySDNN` ingest path; the user typically logs one
* sample per night during sleep, so the chart is sparse but the
* weekly trend is the load-bearing signal. Apple Health / Fitbit store
* SDNN as millisecond `HEART_RATE_VARIABILITY` rows; Oura / Polar / WHOOP
* store nightly RMSSD as `HRV_RMSSD`. The page renders SDNN when present
* weekly trend is the load-bearing signal. Apple Health and Health Connect
* store SDNN as millisecond `HEART_RATE_VARIABILITY` rows; Oura / Polar /
* WHOOP / Fitbit / Google Health store nightly RMSSD as `HRV_RMSSD`. The page renders SDNN when present
* and falls back to the RMSSD series (labelled "RMSSD") otherwise, so a
* ring / strap user's stored HRV always surfaces. Since iOS 27 an Apple
* Watch writes both (#1110); with rows of both the page charts each on its
+3 -2
View File
@@ -491,8 +491,9 @@ export default function DashboardPageClient({
// measurement-type enum), so no backend change was needed; the strip
// tiles below self-gate on `count > 0`.
// HRV is stored under two types and the tile has to accept either: SDNN
// (`HEART_RATE_VARIABILITY`, Apple / Fitbit / Google Health) or nightly
// RMSSD (`HRV_RMSSD`, Oura / Polar / WHOOP and bridge pushes). Reading
// (`HEART_RATE_VARIABILITY`, Apple Health / Health Connect) or nightly
// RMSSD (`HRV_RMSSD`, Oura / Polar / WHOOP / Fitbit / Google Health and
// bridge pushes). Reading
// SDNN alone left a ring / strap account with a Settings toggle that
// rendered nothing while `/insights/hrv` charted the same data. The
// helper keeps the union in one place and hands back which type won, so