Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/production-incident-baseline.md
Original file line number Diff line number Diff line change
Expand Up @@ -8796,3 +8796,40 @@ new incremental tables are populated.
- Follow-Up Work: Deploy the prepared bounded RHR and activity summary model
update; add freshness monitoring comparing latest sleep/heart-rate inputs to
latest active RHR output date.

### Healthspan activity and steps undercount

- Date: 2026-05-26.
- Symptoms: The Healthspan Score card showed `Aerobic Activity` as
`0 min/week` and `Daily Steps` around `1117 steps/day` despite the user
reporting regular activity and more walking than that.
- User Impact: The Healthspan score penalized activity and steps using inputs
that did not match the user's actual recent behavior.
- Evidence: Production `fitness.v_daily_metrics` averaged `1115` steps and
`16` exercise minutes over the 35-day Healthspan window. Recent
`fitness.daily_metrics` rows from Apple Health `HealthKit` showed many
overwritten low partial-day step totals, while the Healthspan aerobic query
used only HR/power-linked `analytics.activity_summary` activity data and
ignored device-reported `exercise_minutes`.
- Root Cause: Incremental mobile HealthKit sync used `now - 24h` as the start
time for daily cumulative statistics, then upserted those partial-day
statistics over whole-day `fitness.daily_metrics` rows. Separately,
Healthspan treated missing HR/power-linked aerobic activity as zero instead
of using the device-reported full-day exercise minutes already stored in
`fitness.v_daily_metrics`.
- Fix/Mitigation: Updated mobile HealthKit sync to start incremental sync
windows at the local calendar-day boundary, preventing future partial-day
overwrites. Updated Healthspan to use weekly device-reported exercise minutes
as the aerobic activity floor when HR-zone activity minutes are lower or
missing.
- Validation: Added regression coverage for day-boundary HealthKit sync and
Healthspan exercise-minute fallback. Focused Healthspan unit tests, mobile
Vitest project, Biome checks, and TypeScript checks passed locally.
- Remaining Risk: Existing corrupted historical Apple Health daily metric rows
remain in production until the iOS app runs a corrected manual/full HealthKit
sync from the user's device; the server cannot reconstruct those all-day
HealthKit totals without the device.
- Follow-Up Work: After deploying the fix, run a full Apple Health sync from
the iOS app to repair historical daily step and exercise-minute rows. Consider
adding a server-side diagnostic for suspicious step drops after partial
HealthKit sync windows.
23 changes: 23 additions & 0 deletions packages/mobile/lib/health-kit-sync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ describe("syncHealthKitToServer", () => {
expect(startDate.getFullYear()).toBeLessThanOrEqual(1970);
});

it("starts incremental daily statistics at the local day boundary", async () => {
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-05-26T15:30:00-07:00"));
const client = createMockClient();
const healthKit = createMockHealthKit();
const expectedStartDate = new Date();
expectedStartDate.setDate(expectedStartDate.getDate() - 1);
expectedStartDate.setHours(0, 0, 0, 0);

try {
await syncHealthKitToServer({
trpcClient: client,
healthKit,
syncRangeDays: 1,
});
} finally {
vi.useRealTimers();
}

const firstCall = healthKit.queryDailyStatistics.mock.calls[0];
expect(firstCall[1]).toBe(expectedStartDate.toISOString());
});

it("batches large sample sets into groups of 500", async () => {
const client = createMockClient();
const healthKit = createMockHealthKit();
Expand Down
15 changes: 10 additions & 5 deletions packages/mobile/lib/health-kit-sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,15 @@ const ALL_QUANTITY_TYPES = [...ADDITIVE_QUANTITY_TYPES, ...NON_ADDITIVE_QUANTITY

const BATCH_SIZE = 500;

function daysAgo(days: number): string {
const date = new Date();
date.setDate(date.getDate() - days);
return date.toISOString();
function syncWindowStart(syncRangeDays: number | null): string {
if (syncRangeDays === null) {
return new Date(0).toISOString();
}

const startDate = new Date();
startDate.setDate(startDate.getDate() - syncRangeDays);
startDate.setHours(0, 0, 0, 0);
return startDate.toISOString();
}

function normalizeWorkout(workout: WorkoutSample): WorkoutSample {
Expand Down Expand Up @@ -114,7 +119,7 @@ export interface SyncResult {
export async function syncHealthKitToServer(options: SyncOptions): Promise<SyncResult> {
const { trpcClient, healthKit, syncRangeDays, onProgress } = options;

const startDate = syncRangeDays === null ? new Date(0).toISOString() : daysAgo(syncRangeDays);
const startDate = syncWindowStart(syncRangeDays);
const endDate = new Date().toISOString();

const allSamples: HealthKitSample[] = [];
Expand Down
7 changes: 6 additions & 1 deletion packages/server/src/routers/healthspan-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ function makeFetchContext(
overrides: Partial<Parameters<typeof fetchHealthspanRawData>[0]>,
): Parameters<typeof fetchHealthspanRawData>[0] {
return {
db: { execute: vi.fn().mockResolvedValue([makeRawHealthspanRow()]) },
db: {
execute: vi
.fn()
.mockResolvedValueOnce([{ weekly_exercise_min: null }])
.mockResolvedValueOnce([makeRawHealthspanRow()]),
},
userId: "user-1",
timezone: "UTC",
accessWindow: fullAccessWindow,
Expand Down
31 changes: 29 additions & 2 deletions packages/server/src/routers/healthspan-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { averageVo2MaxEstimates } from "@dofek/training/derived-cardio";
import { ZONE_BOUNDARIES_FTP } from "@dofek/zones/zones";
import { sql } from "drizzle-orm";
import { z } from "zod";
import { dateWindowStart, timestampWindowStart } from "../lib/date-window.ts";
import { dateWindowEnd, dateWindowStart, timestampWindowStart } from "../lib/date-window.ts";
import { dateStringSchema, executeWithSchema } from "../lib/typed-sql.ts";
import { fetchBodyCompRows } from "../repositories/body-clickhouse.ts";
import { fetchSleepNights } from "../repositories/clickhouse-sleep-repository.ts";
Expand Down Expand Up @@ -36,6 +36,10 @@ const rawRowSchema = z.object({

export type HealthspanRawRow = z.infer<typeof rawRowSchema>;

const weeklyExerciseMinutesRowSchema = z.object({
weekly_exercise_min: z.coerce.number().nullable(),
});

type WeeklyHistoryRow = z.infer<typeof historyRowSchema>;
type HealthspanRawDataContext = Pick<
AuthenticatedContext,
Expand Down Expand Up @@ -178,6 +182,25 @@ async function fetchHrZoneTime(
return rows[0] ?? { aerobic_minutes: 0, high_intensity_minutes: 0 };
}

async function fetchWeeklyExerciseMinutes(
ctx: HealthspanRawDataContext,
endDate: string,
totalDays: number,
): Promise<number | null> {
const rows = await executeWithSchema(
ctx.db,
weeklyExerciseMinutesRowSchema,
sql`SELECT
(SUM(exercise_minutes)::real / GREATEST(${totalDays}::real / 7, 1)) AS weekly_exercise_min
FROM fitness.v_daily_metrics
WHERE user_id = ${ctx.userId}
AND date > ${dateWindowStart(endDate, totalDays)}
AND date <= ${dateWindowEnd(endDate)}`,
Comment thread
Asherlc marked this conversation as resolved.
Resolved
);

return rows[0]?.weekly_exercise_min ?? null;
}
Comment thread
Asherlc marked this conversation as resolved.
Resolved

/**
* Fetch the raw aggregates and weekly history needed to compute a Healthspan score.
*
Expand All @@ -202,8 +225,12 @@ export async function fetchHealthspanRawData(
: [];
const restingHeartRateCte = restingHeartRateValuesCte(restingHeartRateRows);
const hrZoneTime = await fetchHrZoneTime(ctx, endDate, totalDays, restingHeartRateRows);
const weeklyExerciseMin = await fetchWeeklyExerciseMinutes(ctx, endDate, totalDays);
const weeklyDivisor = Math.max(totalDays / 7, 1);
const weeklyAerobicMin = hrZoneTime.aerobic_minutes / weeklyDivisor;
const weeklyAerobicMin = Math.max(
hrZoneTime.aerobic_minutes / weeklyDivisor,
weeklyExerciseMin ?? 0,
);
const weeklyHighIntensityMin = hrZoneTime.high_intensity_minutes / weeklyDivisor;
const bodyMeasurements = ctx.sensorStore
? await fetchBodyCompRows(ctx.sensorStore, ctx.userId, endDate, totalDays)
Expand Down
34 changes: 31 additions & 3 deletions packages/server/src/routers/healthspan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,10 @@ function makeFetchContext(

describe("fetchHealthspanRawData", () => {
it("passes derived sleep, body, and zone metrics into the aggregate query", async () => {
const execute = vi.fn().mockResolvedValue([makeRawHealthspanRow()]);
const execute = vi
.fn()
.mockResolvedValueOnce([{ weekly_exercise_min: null }])
.mockResolvedValueOnce([makeRawHealthspanRow()]);
const query = vi.fn(async (_schema: unknown, queryText: string) => {
if (queryText.includes("analytics.resting_heart_rate_sleep_window")) {
return [{ date: "2026-03-02", resting_hr: 48 }];
Expand Down Expand Up @@ -534,7 +537,7 @@ describe("fetchHealthspanRawData", () => {
restingHeartRates: [48],
}),
);
const sqlValues = collectSqlParameterValues(execute.mock.calls[0]?.[0]);
const sqlValues = collectSqlParameterValues(execute.mock.calls[1]?.[0]);
Comment thread
Asherlc marked this conversation as resolved.
Resolved
expectSqlParamsToContainNumber(sqlValues, 500);
expectSqlParamsToContainNumber(sqlValues, Math.sqrt(115_800));
expectSqlParamsToContainNumber(sqlValues, 70);
Expand All @@ -543,6 +546,31 @@ describe("fetchHealthspanRawData", () => {
expectSqlParamsToContainNumber(sqlValues, 21);
});

it("uses device-reported exercise minutes as the aerobic activity floor", async () => {
const execute = vi
.fn()
.mockResolvedValueOnce([{ weekly_exercise_min: 112 }])
.mockResolvedValueOnce([makeRawHealthspanRow()]);
const query = vi.fn(async (_schema: unknown, queryText: string) => {
if (queryText.includes("activity_metadata")) {
return [{ aerobic_minutes: 0, high_intensity_minutes: 0 }];
}
return [];
});
const ctx = makeFetchContext({
db: { execute },
sensorStore: makeSensorStore({
query,
getVo2MaxEstimates: vi.fn().mockResolvedValue([]),
}),
});

await fetchHealthspanRawData(ctx, "2026-03-15", 14);

const sqlValues = collectSqlParameterValues(execute.mock.calls[1]?.[0]);
expectSqlParamsToContainNumber(sqlValues, 112);
});

it("keeps activity sensor bounds in the ClickHouse join", async () => {
const query = vi.fn().mockResolvedValue([]);
const ctx = makeFetchContext({
Expand Down Expand Up @@ -631,7 +659,7 @@ describe("fetchHealthspanRawData", () => {

await fetchHealthspanRawData(ctx, "2026-03-15", 14);

const sqlValues = collectSqlParameterValues(execute.mock.calls[0]?.[0]);
const sqlValues = collectSqlParameterValues(execute.mock.calls[1]?.[0]);
expect(sqlValues).not.toContain(undefined);
expect(countSqlNumberParams(sqlValues, 0)).toBeGreaterThanOrEqual(2);
});
Expand Down
Loading