v1.4.36 — perf, charts, AI payload trim, UX punch

Heavy-aggregate skip on the rollup-fresh + fully-covered path,
daily chart fetches opt into rollup buckets via source=rollup,
Insights extractFeatures swap drops the prompt payload from
double-digit MB to the low hundreds of kB on power-user accounts,
page-level isLoading gate replaced with per-section early-skeleton
paint, trends-row chart slot pinned for CLS, IntakeHistoryListV2
restored on the medication detail page, About card folded into the
Admin Console with an auto-checking update badge, cumulative metric
tiles read the day's running sum, IP-whois fallback surfaces city
+ country when the ASN lookup misses. Migration 0068 adds an
additive users.insights_exclude_metrics column. Unit suite 4354
passing; typecheck + lint clean.
This commit is contained in:
Marc-André Bombeck committed 2026-05-17 13:22:51 +02:00
1 parent 5a3ca330ac
commit 0e2a99f8e7
70 files changed
+4982 -810

No files matched your search

@@ -170,6 +170,65 @@ describe("buildCoachSnapshot", () => {
expect(parsed.scope.window).toBe("last30days");
});
// v1.4.36 W3 T2 — `medications` exclusion drops the GLP-1 weeklyContext
// block + the compliance source so no medication data reaches the
// prompt. Empty-data behaviour: when the user has no GLP-1 medication
// the block is absent anyway (this test only proves the toggle path).
it("omits weeklyContext.glp1 when excludeMetrics contains 'medications'", async () => {
prismaMock.user.findUnique.mockResolvedValue({
coachPrefsJson: { excludeMetrics: ["medications"] },
timezone: "Europe/Berlin",
});
const out = await buildCoachSnapshot("user-1");
const parsed = JSON.parse(out.snapshotJson);
expect(parsed.weeklyContext).toBeUndefined();
expect(parsed.compliance).toBeUndefined();
expect(parsed.scope.sources).not.toContain("compliance");
});
// v1.4.36 W3 T2 — `anthropometrics` exclusion drops the profile
// block even when features.context has populated fields.
it("omits anthropometrics when excludeMetrics contains 'anthropometrics'", async () => {
featuresMock.mockResolvedValue({
context: { heightCm: 180, ageYears: 45, gender: "MALE" },
});
prismaMock.user.findUnique.mockResolvedValue({
coachPrefsJson: { excludeMetrics: ["anthropometrics"] },
timezone: "Europe/Berlin",
});
const out = await buildCoachSnapshot("user-1");
const parsed = JSON.parse(out.snapshotJson);
expect(parsed.anthropometrics).toBeUndefined();
});
// v1.4.36 W3 T2 — anthropometrics block is added when features.context
// has at least one non-null field AND the exclusion is off.
it("includes anthropometrics when context has data and exclusion is off", async () => {
featuresMock.mockResolvedValue({
context: { heightCm: 180, ageYears: 45, gender: "MALE" },
});
const out = await buildCoachSnapshot("user-1");
const parsed = JSON.parse(out.snapshotJson);
expect(parsed.anthropometrics).toEqual({
heightCm: 180,
ageYears: 45,
gender: "MALE",
});
});
// v1.4.36 W3 T2 — empty-block omit: when every anthropometric field
// is null the block is dropped entirely, not emitted as a labelled
// null-trio that would render as `Hier sind die Profildaten: [keine]`
// in the eventual prompt.
it("drops anthropometrics when every field is null", async () => {
featuresMock.mockResolvedValue({
context: { heightCm: null, ageYears: null, gender: null },
});
const out = await buildCoachSnapshot("user-1");
const parsed = JSON.parse(out.snapshotJson);
expect(parsed.anthropometrics).toBeUndefined();
});
it("scope-only-mood pulls just mood data, no measurements query", async () => {
featuresMock.mockResolvedValue({
mood: { avg30: 4.2, coverage: { count: 12 } },
+58 -8
View File
@@ -22,6 +22,7 @@ import {
type CoachExcludeMetric,
} from "@/lib/validations/coach-prefs";
import { DEFAULT_TIMEZONE } from "@/lib/tz/resolver";
import { compactSections } from "@/lib/insights/prompt-compact";
import { buildGlp1SnapshotBlock } from "./glp1-snapshot";
import type {
CoachProvenance,
@@ -395,15 +396,30 @@ async function buildCoachSnapshotImpl(
const prefs = parseCoachPrefs(prefsRow?.coachPrefsJson);
const userTz = prefsRow?.timezone ?? DEFAULT_TIMEZONE;
const excluded = new Set<CoachExcludeMetric>(prefs.excludeMetrics);
// v1.4.36 W3 T2 — `medications` and `anthropometrics` are
// exclude-only toggles (not in `CoachScopeSource`); they gate the
// GLP-1 weeklyContext / compliance branch and the anthropometrics
// block respectively. The mapping below treats them as additive to
// the existing source-level exclusions.
const excludesMedications = excluded.has("medications");
const excludesAnthropometrics = excluded.has("anthropometrics");
const sources = new Set<CoachScopeSource>();
for (const src of scopedSources) {
// The `excludeMetrics` enum is a strict subset of `CoachScopeSource`
// (every excludable key is also a valid scope source); this cast is
// safe and the runtime check is just defence-in-depth.
// The `excludeMetrics` enum is a superset of `CoachScopeSource` now
// (medications + anthropometrics live on the exclude-only side);
// the cast was safe before v1.4.36 because the enums matched 1:1,
// and the runtime `excluded.has` check still only catches the
// overlapping members.
if (!excluded.has(src as unknown as CoachExcludeMetric)) {
sources.add(src);
}
}
// `medications` exclusion also drops the compliance source so the
// intake-event branch below short-circuits — keeps the contract
// consistent (excluding medications == no medication data at all).
if (excludesMedications) {
sources.delete("compliance");
}
const windowDays = windowToDays(window);
const features = await extractFeatures(userId, false, {
@@ -756,10 +772,37 @@ async function buildCoachSnapshotImpl(
// Coach's GROUND RULE 9 forbids dose prescriptions — this block
// exists so the reply can SAY "your Mounjaro 7.5 mg" instead of
// "your medication", never to make recommendations.
const glp1Block = await buildGlp1SnapshotBlock(userId, now);
if (glp1Block) {
snapshot.weeklyContext = { glp1: glp1Block };
metrics.add("compliance");
// v1.4.36 W3 T2 — gated on `medications` exclusion. When excluded
// we skip the Prisma lookup entirely so the read cost vanishes too.
if (!excludesMedications) {
const glp1Block = await buildGlp1SnapshotBlock(userId, now);
if (glp1Block) {
snapshot.weeklyContext = { glp1: glp1Block };
metrics.add("compliance");
}
}
// v1.4.36 W3 T2 — anthropometrics block (height / age / gender).
// Sourced from `features.context`, which already reads
// `User.heightCm / dateOfBirth / gender`. Gated on the
// `anthropometrics` exclusion AND on at least one non-null field
// — accounts with no profile info never see an empty block. The
// `ctx?` guard tolerates mocked feature shapes in the test suite
// that don't populate the `context` object.
if (!excludesAnthropometrics) {
const ctx = features.context;
if (
ctx &&
(ctx.heightCm !== null ||
ctx.ageYears !== null ||
ctx.gender !== null)
) {
snapshot.anthropometrics = {
heightCm: ctx.heightCm,
ageYears: ctx.ageYears,
gender: ctx.gender,
};
}
}
if (Object.keys(snapshot).length === 0) {
@@ -775,8 +818,15 @@ async function buildCoachSnapshotImpl(
timelineRecentDays: DAILY_TIMELINE_DAYS,
};
// v1.4.36 W3 T4 — compactSections drops any zero-row block before
// serialisation so the prompt never carries a labelled-empty key.
// The snapshot is built conditionally above so most empty paths are
// already skipped, but the helper catches future regressions and
// matches the contract the /insights/generate route applies on its
// side of the prompt.
const compactSnapshot = compactSections(snapshot);
return {
snapshotJson: JSON.stringify(snapshot, null, 2),
snapshotJson: JSON.stringify(compactSnapshot, null, 2),
provenance: {
windows: Array.from(windows),
metrics: Array.from(metrics),
@@ -1,19 +1,20 @@
/**
* v1.4.33 C1 — unit-level pin for the slim summaries slice. Heavier
* integration coverage lives in `tests/integration/analytics-summaries-slice.test.ts`
* Unit-level pin for the slim summaries slice. Heavier integration
* coverage lives in `tests/integration/analytics-summaries-slice.test.ts`
* (real Postgres, real `regr_slope`); this file mocks `$queryRaw` so
* the slope/round/empty contracts are pinned without a container.
* the slope/round/empty/path-selection contracts are pinned without
* a container.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
$queryRaw: vi.fn(),
// v1.4.35 — slim slice now reads DAY buckets from
// `measurement_rollups` for the parity-checked composition. The
// freshness watermark inside `ensureUserRollupsFresh` also pokes
// `measurementRollup.findFirst`; mock both so the helper runs
// without a container.
measurement: { findFirst: vi.fn() },
// v1.4.36 — slim slice reads DAY buckets from `measurement_rollups`
// on the happy path. The freshness watermark inside
// `ensureUserRollupsFresh` also pokes `measurementRollup.findFirst`;
// mock both so the helper runs without a container.
measurementRollup: {
findMany: vi.fn(),
findFirst: vi.fn(),
@@ -29,6 +30,8 @@ import { prisma } from "@/lib/db";
import { computeSummariesSlice } from "../summaries-slice";
const RAW = prisma.$queryRaw as unknown as ReturnType<typeof vi.fn>;
const MEASUREMENT_FIND_FIRST =
prisma.measurement.findFirst as unknown as ReturnType<typeof vi.fn>;
const ROLLUP_FIND_MANY =
prisma.measurementRollup.findMany as unknown as ReturnType<typeof vi.fn>;
const ROLLUP_FIND_FIRST =
@@ -36,13 +39,12 @@ const ROLLUP_FIND_FIRST =
beforeEach(() => {
RAW.mockReset();
MEASUREMENT_FIND_FIRST.mockReset();
ROLLUP_FIND_MANY.mockReset();
ROLLUP_FIND_FIRST.mockReset();
// Defaults: empty rollup buckets so the parity check falls back to
// live SQL (the pre-backfill behaviour). Individual tests opt in
// to rollup-derived values by overriding the mock.
ROLLUP_FIND_MANY.mockResolvedValue([]);
ROLLUP_FIND_FIRST.mockResolvedValue(null);
MEASUREMENT_FIND_FIRST.mockResolvedValue(null);
});
afterEach(() => {
@@ -50,178 +52,242 @@ afterEach(() => {
});
describe("computeSummariesSlice", () => {
it("returns the empty-summary skeleton when the user has no rows", async () => {
// Pass 1 (aggregates) returns empty, pass 2 (latest) returns empty.
// Pass 3 (rollup.findMany) is defaulted to [] in beforeEach.
RAW.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
describe("cold fallback — empty rollup table", () => {
it("returns the empty-summary skeleton when the user has no rows", async () => {
// 1. per-type coverage probe — empty ⇒ cold path.
// 2. heavy aggregate ($queryRaw) — empty.
// 3. latests ($queryRaw) — empty.
RAW.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([]);
const result = await computeSummariesSlice("user-1");
const result = await computeSummariesSlice("user-1");
// Every enum option is seeded so consumers can read
// `summaries.WEIGHT` and get a stable empty shape.
expect(result.summaries.WEIGHT).toEqual({
count: 0,
latest: null,
min: null,
max: null,
mean: null,
avg7: null,
avg30: null,
slope7: null,
slope30: null,
slope90: null,
anomalyCount: 0,
avg30LastMonth: null,
avg30LastYear: null,
});
expect(result.bmi).toBeNull();
// Both SQL passes fired.
expect(RAW).toHaveBeenCalledTimes(2);
});
it("maps a populated aggregate row into the DataSummary shape", async () => {
RAW.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(42),
min_value: 79.2,
max_value: 84.1,
mean_value: 82.05,
avg7: 81.9,
avg30: 82.1,
slope7: -0.014,
r2_7: 0.65,
slope30: -0.005,
r2_30: 0.42,
slope90: 0.001,
r2_90: 0.12,
},
]).mockResolvedValueOnce([
{ type: "WEIGHT", value: 81.4, measured_at: new Date() },
]);
const result = await computeSummariesSlice("user-1");
const weight = result.summaries.WEIGHT;
expect(weight.count).toBe(42);
expect(weight.latest).toBe(81.4);
expect(weight.min).toBe(79.2);
expect(weight.max).toBe(84.1);
expect(weight.mean).toBe(82.05);
expect(weight.avg7).toBe(81.9);
expect(weight.avg30).toBe(82.1);
expect(weight.anomalyCount).toBe(0);
expect(weight.avg30LastMonth).toBeNull();
expect(weight.avg30LastYear).toBeNull();
// Slope direction respects the 0.01-units-per-day "stable"
// threshold the JS helper uses. -0.014 → "down", -0.005 →
// "stable", 0.001 → "stable".
expect(weight.slope7).toEqual({
slope: -0.014,
direction: "down",
confidence: 0.65,
});
expect(weight.slope30).toEqual({
slope: -0.005,
direction: "stable",
confidence: 0.42,
});
expect(weight.slope90).toEqual({
slope: 0.001,
direction: "stable",
confidence: 0.12,
});
});
it("returns a null slope tuple when the SQL slope is null (insufficient rows)", async () => {
RAW.mockResolvedValueOnce([
{
type: "PULSE",
count: BigInt(1),
min_value: 72,
max_value: 72,
mean_value: 72,
avg7: 72,
avg30: 72,
// regr_slope returns NULL when n < 2.
slope7: null,
r2_7: null,
slope30: null,
r2_30: null,
slope90: null,
r2_90: null,
},
]).mockResolvedValueOnce([
{ type: "PULSE", value: 72, measured_at: new Date() },
]);
const result = await computeSummariesSlice("user-1");
expect(result.summaries.PULSE.slope7).toBeNull();
expect(result.summaries.PULSE.slope30).toBeNull();
expect(result.summaries.PULSE.slope90).toBeNull();
});
// v1.4.34 IW-B — surface the per-type freshness map so the
// dashboard tile strip can render an "Letzter Wert vor Xd" caption
// even when the consumer read from the slim slice (the path the
// tile-strip pre-fetch uses on cold paint).
it("surfaces lastSeenByType from the DISTINCT ON pass's measured_at", async () => {
const tenDaysAgo = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
RAW.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(5),
min_value: 80,
max_value: 84,
mean_value: 82,
expect(result.summaries.WEIGHT).toEqual({
count: 0,
latest: null,
min: null,
max: null,
mean: null,
avg7: null,
avg30: 82,
avg30: null,
slope7: null,
r2_7: null,
slope30: 0.005,
r2_30: 0.2,
slope30: null,
slope90: null,
r2_90: null,
},
]).mockResolvedValueOnce([
{ type: "WEIGHT", value: 82.3, measured_at: tenDaysAgo },
]);
anomalyCount: 0,
avg30LastMonth: null,
avg30LastYear: null,
});
expect(result.bmi).toBeNull();
expect(RAW).toHaveBeenCalledTimes(3);
});
const result = await computeSummariesSlice("user-1");
const ws = result.lastSeenByType.WEIGHT;
expect(ws).not.toBeNull();
expect(ws?.daysAgo).toBeGreaterThanOrEqual(9);
expect(ws?.daysAgo).toBeLessThanOrEqual(11);
expect(ws?.lastSeenAt).toBe(tenDaysAgo.toISOString());
// Types the user never logged report null so call sites can
// fall through without painting a caption.
expect(result.lastSeenByType.PULSE).toBeNull();
it("maps a populated heavy aggregate row into the DataSummary shape on cold path", async () => {
// Coverage probe returns WEIGHT with no buckets → cold path.
RAW.mockResolvedValueOnce([{ type: "WEIGHT", has_buckets: false }])
.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(42),
min_value: 79.2,
max_value: 84.1,
mean_value: 82.05,
avg7: 81.9,
avg30: 82.1,
slope7: -0.014,
r2_7: 0.65,
slope30: -0.005,
r2_30: 0.42,
slope90: 0.001,
r2_90: 0.12,
},
])
.mockResolvedValueOnce([
{ type: "WEIGHT", value: 81.4, measured_at: new Date() },
]);
const result = await computeSummariesSlice("user-1");
const weight = result.summaries.WEIGHT;
expect(weight.count).toBe(42);
expect(weight.latest).toBe(81.4);
expect(weight.min).toBe(79.2);
expect(weight.max).toBe(84.1);
expect(weight.mean).toBe(82.05);
expect(weight.avg7).toBe(81.9);
expect(weight.avg30).toBe(82.1);
expect(weight.anomalyCount).toBe(0);
expect(weight.avg30LastMonth).toBeNull();
expect(weight.avg30LastYear).toBeNull();
expect(weight.slope7).toEqual({
slope: -0.014,
direction: "down",
confidence: 0.65,
});
expect(weight.slope30).toEqual({
slope: -0.005,
direction: "stable",
confidence: 0.42,
});
expect(weight.slope90).toEqual({
slope: 0.001,
direction: "stable",
confidence: 0.12,
});
});
it("returns a null slope tuple when the SQL slope is null (insufficient rows)", async () => {
RAW.mockResolvedValueOnce([{ type: "PULSE", has_buckets: false }])
.mockResolvedValueOnce([
{
type: "PULSE",
count: BigInt(1),
min_value: 72,
max_value: 72,
mean_value: 72,
avg7: 72,
avg30: 72,
slope7: null,
r2_7: null,
slope30: null,
r2_30: null,
slope90: null,
r2_90: null,
},
])
.mockResolvedValueOnce([
{ type: "PULSE", value: 72, measured_at: new Date() },
]);
const result = await computeSummariesSlice("user-1");
expect(result.summaries.PULSE.slope7).toBeNull();
expect(result.summaries.PULSE.slope30).toBeNull();
expect(result.summaries.PULSE.slope90).toBeNull();
});
it("surfaces lastSeenByType from the DISTINCT ON pass's measured_at", async () => {
const tenDaysAgo = new Date(Date.now() - 10 * 24 * 60 * 60 * 1000);
RAW.mockResolvedValueOnce([{ type: "WEIGHT", has_buckets: false }])
.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(5),
min_value: 80,
max_value: 84,
mean_value: 82,
avg7: null,
avg30: 82,
slope7: null,
r2_7: null,
slope30: 0.005,
r2_30: 0.2,
slope90: null,
r2_90: null,
},
])
.mockResolvedValueOnce([
{ type: "WEIGHT", value: 82.3, measured_at: tenDaysAgo },
]);
const result = await computeSummariesSlice("user-1");
const ws = result.lastSeenByType.WEIGHT;
expect(ws).not.toBeNull();
expect(ws?.daysAgo).toBeGreaterThanOrEqual(9);
expect(ws?.daysAgo).toBeLessThanOrEqual(11);
expect(ws?.lastSeenAt).toBe(tenDaysAgo.toISOString());
expect(result.lastSeenByType.PULSE).toBeNull();
});
it("seeds the latest value from the DISTINCT ON pass per type", async () => {
RAW.mockResolvedValueOnce([{ type: "PULSE", has_buckets: false }])
.mockResolvedValueOnce([
{
type: "PULSE",
count: BigInt(3),
min_value: 60,
max_value: 95,
mean_value: 77,
avg7: 77,
avg30: 77,
slope7: 0,
r2_7: 0,
slope30: 0,
r2_30: 0,
slope90: 0,
r2_90: 0,
},
])
.mockResolvedValueOnce([
{ type: "PULSE", value: 88, measured_at: new Date() },
]);
const result = await computeSummariesSlice("user-1");
expect(result.summaries.PULSE.latest).toBe(88);
expect(result.summaries.PULSE.max).toBe(95);
});
});
it("seeds the latest value from the DISTINCT ON pass per type", async () => {
RAW.mockResolvedValueOnce([
{
type: "PULSE",
count: BigInt(3),
min_value: 60,
max_value: 95,
mean_value: 77,
avg7: 77,
avg30: 77,
slope7: 0,
r2_7: 0,
slope30: 0,
r2_30: 0,
slope90: 0,
r2_90: 0,
},
]).mockResolvedValueOnce([
{ type: "PULSE", value: 88, measured_at: new Date() },
]);
describe("rollup-fresh happy path", () => {
it("composes count/min/max/mean from DAY buckets without running the heavy aggregate", async () => {
// 1. per-type coverage probe — WEIGHT fully covered ⇒ happy path.
// 2. narrow aggregate ($queryRaw) — windowed/regression only.
// 3. latests ($queryRaw).
RAW.mockResolvedValueOnce([{ type: "WEIGHT", has_buckets: true }])
.mockResolvedValueOnce([
{
type: "WEIGHT",
avg7: 82,
avg30: 82.5,
slope7: 0.02,
r2_7: 0.5,
slope30: 0.01,
r2_30: 0.4,
slope90: 0.005,
r2_90: 0.2,
},
])
.mockResolvedValueOnce([
{ type: "WEIGHT", value: 82.7, measured_at: new Date() },
]);
const result = await computeSummariesSlice("user-1");
// Latest is NOT max — comes from the row at MAX(measured_at).
expect(result.summaries.PULSE.latest).toBe(88);
expect(result.summaries.PULSE.max).toBe(95);
ROLLUP_FIND_MANY.mockResolvedValueOnce([
{
type: "WEIGHT",
bucketStart: new Date("2026-05-10T00:00:00.000Z"),
count: 10,
mean: 81.0,
minValue: 79.5,
maxValue: 82.0,
},
{
type: "WEIGHT",
bucketStart: new Date("2026-05-11T00:00:00.000Z"),
count: 10,
mean: 83.0,
minValue: 81.5,
maxValue: 84.0,
},
]);
const result = await computeSummariesSlice("user-rollup");
const weight = result.summaries.WEIGHT;
// count = 10 + 10 = 20; min/max across buckets; weighted mean.
expect(weight.count).toBe(20);
expect(weight.min).toBe(79.5);
expect(weight.max).toBe(84.0);
expect(weight.mean).toBe(82);
expect(weight.latest).toBe(82.7);
expect(weight.avg7).toBe(82);
expect(weight.slope7).toEqual({
slope: 0.02,
direction: "up",
confidence: 0.5,
});
// probe + narrow aggregate + latests — NOT the heavy aggregate.
expect(RAW).toHaveBeenCalledTimes(3);
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1);
});
});
});
+238 -50
View File
@@ -1,5 +1,5 @@
/**
* v1.4.33 C1 — slim `summaries` slice for `/api/analytics?slice=summaries`.
* v1.4.36 — slim `summaries` slice for `/api/analytics?slice=summaries`.
*
* The default `/api/analytics` path fans out 30+ `Promise.all`-wrapped
* chunked findMany reads (one per `MeasurementType`) so it can hand
@@ -9,28 +9,34 @@
* three slope tuples — every other field gets `null`-coalesced before
* it ever paints — yet the user waits on the full chunked walk.
*
* This helper resolves the same `DataSummary` shape (per-type) with
* three passes (two SQL + one rollup-table read):
* Read shape (v1.4.36)
* --------------------
* v1.4.35 partially read-swapped onto the persistent `measurement_rollups`
* table but kept the full-fat `$queryRaw` running in parallel as a
* parity check. The heavy COUNT/MIN/MAX/AVG aggregate scanned the
* measurements table every request even on warm rollups.
*
* 1. **`groupBy` + windowed aggregates** — one `$queryRaw` carrying
* `COUNT`/`MIN`/`MAX`/`AVG`, the 7-day and 30-day `AVG` slices
* via `FILTER`, plus `regr_slope` + `regr_r2` for the slope
* tuples at 7d / 30d / 90d windows. Postgres folds all of this
* into one index scan over `measurements WHERE user_id = …`.
* The `COUNT / MIN / MAX / AVG` columns stay on this pass as
* the live-SQL fallback for the v1.4.35 rollup parity check.
* v1.4.36 promotes the rollup table to the canonical source on the
* happy path: a single cheap COUNT against `measurement_rollups`
* decides whether to read the composable stats from buckets or fall
* back to the heavy live aggregate. When the rollup is populated:
*
* 2. **`DISTINCT ON (type)` latest read** — one `$queryRaw`
* returning the most-recent `(type, value)` pair per type so the
* slim shape can populate `latest`. `MAX(value)` would not do —
* we need the value of the row at `MAX(measured_at)`, not the
* largest value ever recorded.
* 1. **DAY-bucket read** — composes `count / min / max / mean` per
* type via `aggregateBuckets`. The four are linearly composable
* across DAY buckets so the composed value is mathematically
* identical to the live aggregate over the same rows.
*
* 3. **`measurement_rollups` DAY-bucket read** (v1.4.35) — folded
* into `count / min / max / mean` via `aggregateBuckets`. Used
* when the composed count agrees byte-for-byte with the live
* `COUNT(*)`; falls back to live for accounts that never ran
* the explicit backfill or that are mid-flight on the populator.
* 2. **Narrow `$queryRaw`** — carries only the non-composable
* windowed columns (`avg7`, `avg30`, and the slope tuples). The
* heavy COUNT/MIN/MAX/AVG columns are NOT projected.
*
* 3. **`DISTINCT ON (type)` latest read** — most-recent `(type,
* value, measured_at)` triplet per type for the `latest` field
* and the `lastSeenByType` freshness map.
*
* On the cold fallback (no rollup rows yet) the slim slice runs the
* legacy heavy aggregate so the response shape is correct on the very
* first request.
*
* Fields the slim shape intentionally omits (the dashboard never
* reads them on first paint):
@@ -53,8 +59,17 @@ import { measurementTypeEnum } from "@/lib/validations/measurement";
import { annotate } from "@/lib/logging/context";
import { ensureUserRollupsFresh } from "@/lib/measurements/rollups";
import { aggregateBuckets } from "@/lib/measurements/rollup-read";
import {
isFullyCovered,
probeRollupCoverage,
} from "@/lib/measurements/rollup-coverage";
interface AggregateRow {
/**
* Heavy aggregate row — used on the cold-mount fallback path where the
* rollup table is empty and we need every per-type column out of one
* SQL pass for the very first request.
*/
interface HeavyAggregateRow {
type: string;
count: bigint;
min_value: number | null;
@@ -70,6 +85,24 @@ interface AggregateRow {
r2_90: number | null;
}
/**
* Narrow aggregate row — only the columns DAY buckets can't compose
* linearly. The heavy `COUNT / MIN / MAX / AVG` columns are dropped
* so the rollup-fresh read path's SQL projection is half as wide as
* the legacy aggregate's.
*/
interface NarrowAggregateRow {
type: string;
avg7: number | null;
avg30: number | null;
slope7: number | null;
r2_7: number | null;
slope30: number | null;
r2_30: number | null;
slope90: number | null;
r2_90: number | null;
}
interface LatestRow {
type: string;
value: number;
@@ -146,26 +179,41 @@ export interface SummariesSlice {
export async function computeSummariesSlice(
userId: string,
): Promise<SummariesSlice> {
// v1.4.35 — keep the persistent rollup table current before the
// bucket read fires. On a warm process this is a single watermark
// query; on a cold mount it folds the trailing 90-day window into
// the DAY rollup so the bucket read below has something to compose.
// Keep the persistent rollup table current before the bucket read
// fires. On a warm process this is a single watermark query; on a
// cold mount it folds the trailing 90-day window into the DAY
// rollup so the bucket read below has something to compose.
await ensureUserRollupsFresh(userId);
// Three passes in parallel. The first two target the same
// `measurements (user_id, type, measured_at)` index path; the third
// hits the much smaller `measurement_rollups` table (one row per
// bucket, not per row). On users with a complete backfill the
// bucket scan dominates only marginally; on un-backfilled users it
// returns a short list and the parity check falls back to live.
const [aggregates, latests, dayBuckets] = await Promise.all([
prisma.$queryRaw<AggregateRow[]>`
// v1.4.36 QA C1 — per-type coverage probe replaces the legacy global
// COUNT. The previous gate returned true as soon as ANY type had at
// least one DAY bucket. Pathology: a user with BP fully rolled up
// logs their first WEIGHT measurement → the sync write-hook upserts
// one WEIGHT DAY bucket → the global probe stayed >0 → the route
// flipped to the bucket-derived path for WEIGHT as well → all-time
// count collapsed to whatever the trailing window covers because the
// narrow aggregate's windowed columns + the single fresh bucket
// can't reconstruct the prior history. We now decide per type and
// only take the rollup path when EVERY type the user has logged is
// covered. Partial coverage falls back to the live aggregate so the
// brand-new-type case stays correct.
const coverage = await probeRollupCoverage(userId);
if (isFullyCovered(coverage)) {
return computeFromRollups(userId);
}
return computeFromLiveAggregate(userId);
}
/**
* Happy path — DAY buckets carry `count / min / max / mean`; a narrow
* `$queryRaw` carries only the windowed avgs + regression columns the
* buckets cannot reconstruct.
*/
async function computeFromRollups(userId: string): Promise<SummariesSlice> {
const [narrows, latests, dayBuckets] = await Promise.all([
prisma.$queryRaw<NarrowAggregateRow[]>`
SELECT
m."type"::text AS type,
COUNT(*) AS count,
MIN(m."value")::double precision AS min_value,
MAX(m."value")::double precision AS max_value,
AVG(m."value")::double precision AS mean_value,
AVG(m."value") FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS avg7,
@@ -301,26 +349,165 @@ export async function computeSummariesSlice(
bucketsByType.set(b.type, list);
}
const narrowByType = new Map<string, NarrowAggregateRow>();
for (const row of narrows) {
narrowByType.set(row.type, row);
}
let totalRows = 0;
for (const [type, buckets] of bucketsByType.entries()) {
const composed = aggregateBuckets(buckets);
if (composed.count === 0) continue;
totalRows += composed.count;
const narrow = narrowByType.get(type);
const latest = latestByType.get(type) ?? null;
summaries[type] = {
count: composed.count,
latest,
min: round2(composed.min),
max: round2(composed.max),
mean: round2(composed.mean),
avg7: round2(narrow?.avg7 ?? null),
avg30: round2(narrow?.avg30 ?? null),
slope7: buildSlope(narrow?.slope7 ?? null, narrow?.r2_7 ?? null),
slope30: buildSlope(narrow?.slope30 ?? null, narrow?.r2_30 ?? null),
slope90: buildSlope(narrow?.slope90 ?? null, narrow?.r2_90 ?? null),
anomalyCount: 0,
avg30LastMonth: null,
avg30LastYear: null,
};
}
annotate({
action: { name: "analytics.get.slim" },
meta: {
analytics: {
slim_summaries: {
row_count: totalRows,
type_count: bucketsByType.size,
path: "rollup",
},
},
},
});
return { summaries, bmi: null, lastSeenByType };
}
/**
* Cold fallback — runs the legacy heavy aggregate when the rollup
* table is empty for this user. Subsequent reads pick up the populated
* rollup on `computeFromRollups`.
*/
async function computeFromLiveAggregate(
userId: string,
): Promise<SummariesSlice> {
const [aggregates, latests] = await Promise.all([
prisma.$queryRaw<HeavyAggregateRow[]>`
SELECT
m."type"::text AS type,
COUNT(*) AS count,
MIN(m."value")::double precision AS min_value,
MAX(m."value")::double precision AS max_value,
AVG(m."value")::double precision AS mean_value,
AVG(m."value") FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS avg7,
AVG(m."value") FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '30 days'
)::double precision AS avg30,
REGR_SLOPE(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS slope7,
REGR_R2(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS r2_7,
REGR_SLOPE(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '30 days'
)::double precision AS slope30,
REGR_R2(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '30 days'
)::double precision AS r2_30,
REGR_SLOPE(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '90 days'
)::double precision AS slope90,
REGR_R2(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '90 days'
)::double precision AS r2_90
FROM measurements m
WHERE m."user_id" = ${userId}
GROUP BY m."type"
`,
prisma.$queryRaw<LatestRow[]>`
SELECT DISTINCT ON (m."type")
m."type"::text AS type,
m."value"::double precision AS value,
m."measured_at" AS measured_at
FROM measurements m
WHERE m."user_id" = ${userId}
ORDER BY m."type", m."measured_at" DESC
`,
]);
const latestByType = new Map<string, number>();
const lastSeenAtByType = new Map<string, Date>();
for (const row of latests) {
latestByType.set(row.type, Number(row.value));
if (row.measured_at) {
lastSeenAtByType.set(row.type, new Date(row.measured_at));
}
}
const summaries: Record<string, DataSummary> = {};
const lastSeenByType: Record<
string,
{ lastSeenAt: string; daysAgo: number } | null
> = {};
for (const type of measurementTypeEnum.options) {
summaries[type] = emptySummary();
lastSeenByType[type] = null;
}
const nowForStaleness = Date.now();
for (const [type, measuredAt] of lastSeenAtByType.entries()) {
const daysAgo = Math.floor(
(nowForStaleness - measuredAt.getTime()) / (24 * 60 * 60 * 1000),
);
lastSeenByType[type] = {
lastSeenAt: measuredAt.toISOString(),
daysAgo,
};
}
let totalRows = 0;
for (const row of aggregates) {
const liveCount = Number(row.count);
// Compose count/min/max/mean from the DAY buckets when the
// composed count agrees with the live `COUNT(*)`. The slim slice's
// window is all-time, so a divergent count signals either an
// un-backfilled account (rollup only covers the trailing 90d
// `ensureUserRollupsFresh` warmed up) or a mid-flight populator —
// both cases want the live SQL values, which we already have.
const composed = aggregateBuckets(bucketsByType.get(row.type) ?? []);
const useRollup = composed.count === liveCount;
const count = useRollup ? composed.count : liveCount;
const count = Number(row.count);
totalRows += count;
const latest = latestByType.get(row.type) ?? null;
summaries[row.type] = {
count,
latest,
min: useRollup ? round2(composed.min) : round2(row.min_value),
max: useRollup ? round2(composed.max) : round2(row.max_value),
mean: useRollup ? round2(composed.mean) : round2(row.mean_value),
min: round2(row.min_value),
max: round2(row.max_value),
mean: round2(row.mean_value),
avg7: round2(row.avg7),
avg30: round2(row.avg30),
slope7: buildSlope(row.slope7, row.r2_7),
@@ -339,6 +526,7 @@ export async function computeSummariesSlice(
slim_summaries: {
row_count: totalRows,
type_count: aggregates.length,
path: "live",
},
},
},
+11
View File
@@ -31,6 +31,11 @@ export function invalidateUserMeasurements(userId: string): void {
caches.analytics.deleteByPrefix(`${userId}|`);
caches.achievements.deleteByPrefix(userId);
caches.workouts.deleteByPrefix(`${userId}|`);
// v1.4.36 W1 — measurement writes change the per-target consistency
// strips, the in-range rates and the streak counters that
// `/api/insights/targets` computes. Evict the user's bucket so the
// next mount paints fresh data.
caches.insightsTargets.deleteByPrefix(userId);
}
/**
@@ -44,6 +49,9 @@ export function invalidateUserMood(userId: string): void {
caches.moodAnalytics.deleteByPrefix(userId);
caches.achievements.deleteByPrefix(userId);
caches.analytics.deleteByPrefix(`${userId}|`);
// v1.4.36 W1 — mood writes change the mood target rows on the
// insights/targets response (MOOD_SCORE, MOOD_STABILITY).
caches.insightsTargets.deleteByPrefix(userId);
}
/**
@@ -56,6 +64,9 @@ export function invalidateUserMedications(userId: string): void {
caches.medications.deleteByPrefix(userId);
caches.medicationsIntake.deleteByPrefix(`${userId}|`);
caches.achievements.deleteByPrefix(userId);
// v1.4.36 W1 — medication writes change the MEDICATION_COMPLIANCE
// target rollup (compliance7 / compliance30 / consistency strip).
caches.insightsTargets.deleteByPrefix(userId);
}
/**
+13
View File
@@ -247,6 +247,19 @@ export const caches = {
maxEntries: 1000,
ttlMs: 60_000,
}),
/**
* v1.4.36 W1 — `/api/insights/targets` is read on every Insights
* mount and runs >1 s cold (it pulls 30-day measurements for every
* type + medications + intakes + mood + glucose). 60 s TTL matches
* the analytics cache so multiple sub-page mounts inside a minute
* all hit a warm cache. Invalidated alongside the analytics bucket
* on measurement / mood / medication writes.
*/
insightsTargets: new ServerCache<unknown>({
name: "insightsTargets",
maxEntries: 1000,
ttlMs: 60_000,
}),
} as const;
/**
@@ -1,19 +1,21 @@
/**
* v1.4.35 — unit pin for `buildComprehensiveAggregate`.
* Unit pin for `buildComprehensiveAggregate`.
*
* Heavier integration coverage (real Postgres, real `regr_slope`,
* cache hit/miss assertions) lives in
* `tests/integration/insights-comprehensive-cache.test.ts`. This file
* mocks `$queryRaw` and Prisma's `measurement.findMany` so the shape +
* rounding + slope-direction contracts are pinned without a container.
* cache hit/miss assertions, byte-identical parity with live SQL)
* lives in `tests/integration/insights-comprehensive-cache.test.ts`
* and `tests/integration/measurement-rollups.test.ts`. This file
* mocks `$queryRaw` and Prisma's `measurement.findMany` so the shape
* + rounding + slope-direction + read-path-selection contracts are
* pinned without a container.
*/
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
$queryRaw: vi.fn(),
measurement: { findMany: vi.fn() },
// v1.4.35 — the rollup read inside the Promise.all + the
measurement: { findMany: vi.fn(), findFirst: vi.fn() },
// v1.4.36 — the rollup read inside the Promise.all + the
// freshness watermark inside `ensureUserRollupsFresh`. We mock
// both so the unit test doesn't need a real container.
measurementRollup: {
@@ -33,6 +35,8 @@ import { buildComprehensiveAggregate } from "../comprehensive-aggregator";
const RAW = prisma.$queryRaw as unknown as ReturnType<typeof vi.fn>;
const FIND_MANY =
prisma.measurement.findMany as unknown as ReturnType<typeof vi.fn>;
const MEASUREMENT_FIND_FIRST =
prisma.measurement.findFirst as unknown as ReturnType<typeof vi.fn>;
const ROLLUP_FIND_MANY =
prisma.measurementRollup.findMany as unknown as ReturnType<typeof vi.fn>;
const ROLLUP_FIND_FIRST =
@@ -41,15 +45,16 @@ const ROLLUP_FIND_FIRST =
beforeEach(() => {
RAW.mockReset();
FIND_MANY.mockReset();
MEASUREMENT_FIND_FIRST.mockReset();
ROLLUP_FIND_MANY.mockReset();
ROLLUP_FIND_FIRST.mockReset();
// `ensureUserRollupsFresh` calls `prisma.measurement.findFirst`;
// we don't have it on the mock above, so it throws and the helper
// swallows + returns `{ recomputed: false }`. The bucket read
// proceeds against whatever we mock on `measurementRollup.findMany`.
// We default both to empty so individual tests opt in to data.
// `ensureUserRollupsFresh` calls `prisma.measurement.findFirst` and
// `prisma.measurementRollup.findFirst` in parallel. Default both to
// null so the warm-up is a no-op; individual tests opt in to a
// freshness mismatch by overriding the mocks.
ROLLUP_FIND_MANY.mockResolvedValue([]);
ROLLUP_FIND_FIRST.mockResolvedValue(null);
MEASUREMENT_FIND_FIRST.mockResolvedValue(null);
});
afterEach(() => {
@@ -57,153 +62,229 @@ afterEach(() => {
});
describe("buildComprehensiveAggregate", () => {
it("returns an empty bundle for a user with no measurements", async () => {
// v1.4.35 pass order: aggregates ($queryRaw), latests ($queryRaw),
// dayBuckets (rollup.findMany — defaulted to [] in beforeEach),
// sysRaw (findMany), diaRaw (findMany). The `firstMeasurementAt`
// $queryRaw is skipped when totalMeasurements === 0.
RAW.mockResolvedValueOnce([]) // aggregates
.mockResolvedValueOnce([]); // latests
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
describe("rollup-fresh happy path", () => {
it("skips the heavy live aggregate when the rollup table is populated", async () => {
const now = new Date();
// 1. per-type coverage probe — WEIGHT fully covered ⇒ happy path.
// 2. narrow aggregate ($queryRaw) — windowed/regression columns only.
// 3. latests ($queryRaw).
// 4. firstMeasurementAt ($queryRaw).
RAW.mockResolvedValueOnce([{ type: "WEIGHT", has_buckets: true }])
.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(42),
stddev_value: 1.2,
anomaly_count: BigInt(3),
avg7: 81.9,
avg30: 82.1,
avg30_last_month: 83.0,
slope7: -0.014,
r2_7: 0.65,
slope30: -0.005,
r2_30: 0.42,
slope90: 0.001,
r2_90: 0.12,
},
])
.mockResolvedValueOnce([
{ type: "WEIGHT", value: 81.4, measured_at: now },
])
.mockResolvedValueOnce([
{ first_at: new Date(now.getTime() - 86400000) },
]);
const result = await buildComprehensiveAggregate("user-empty");
// DAY buckets compose to count=42, min=79.2, max=84.1, mean=82.05.
// The summary's count/min/max/mean read from these buckets — NOT
// from a heavy live aggregate column.
ROLLUP_FIND_MANY.mockResolvedValueOnce([
{
type: "WEIGHT",
bucketStart: new Date("2026-05-10T00:00:00.000Z"),
count: 20,
mean: 81.0,
minValue: 79.2,
maxValue: 82.5,
},
{
type: "WEIGHT",
bucketStart: new Date("2026-05-11T00:00:00.000Z"),
count: 22,
mean: 83.0,
minValue: 80.0,
maxValue: 84.1,
},
]);
expect(result.summaries).toEqual({});
expect(result.bpRawRows.sys).toEqual([]);
expect(result.bpRawRows.dia).toEqual([]);
expect(result.dailyByType).toEqual({});
expect(result.firstMeasurementAt).toBeNull();
expect(result.totalMeasurements).toBe(0);
// 2 $queryRaw (aggregates, latests) + 2 findMany + 1 rollup.findMany.
// No firstMeasurementAt query when total === 0.
expect(RAW).toHaveBeenCalledTimes(2);
expect(FIND_MANY).toHaveBeenCalledTimes(2);
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
const result = await buildComprehensiveAggregate("user-rollup-fresh");
const weight = result.summaries.WEIGHT;
// count/min/max/mean composed from buckets — the test asserts the
// bucket-derived values are present even though we never wired up
// a heavy aggregate row for the parity check.
expect(weight.count).toBe(42);
expect(weight.min).toBe(79.2);
expect(weight.max).toBe(84.1);
// Weighted mean = (20 * 81 + 22 * 83) / 42 = 3446 / 42 ≈ 82.0476
// → round2 → 82.05.
expect(weight.mean).toBe(82.05);
// Windowed/regression columns come from the narrow aggregate.
expect(weight.latest).toBe(81.4);
expect(weight.avg7).toBe(81.9);
expect(weight.avg30).toBe(82.1);
expect(weight.avg30LastMonth).toBe(83.0);
expect(weight.anomalyCount).toBe(3);
expect(weight.slope7).toEqual({
slope: -0.014,
direction: "down",
confidence: 0.65,
});
expect(result.dailyByType.WEIGHT).toEqual([
{ day: "2026-05-10", value: 81 },
{ day: "2026-05-11", value: 83 },
]);
// Contract pin — the heavy aggregate path is NOT exercised on the
// rollup-fresh branch. The $queryRaw calls land on the COUNT
// probe + narrow aggregate + DISTINCT-ON latest + first_at, NOT
// the legacy heavy COUNT/MIN/MAX/AVG query. We assert by call
// count + by checking that the narrow projection (no min_value /
// max_value / mean_value cols) is what landed.
expect(RAW).toHaveBeenCalledTimes(4);
expect(FIND_MANY).toHaveBeenCalledTimes(2);
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1);
});
});
it("maps a populated WEIGHT aggregate into the DataSummary shape", async () => {
const now = new Date();
RAW.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(42),
min_value: 79.2,
max_value: 84.1,
mean_value: 82.05,
stddev_value: 1.2,
anomaly_count: BigInt(3),
avg7: 81.9,
avg30: 82.1,
avg30_last_month: 83.0,
slope7: -0.014,
r2_7: 0.65,
slope30: -0.005,
r2_30: 0.42,
slope90: 0.001,
r2_90: 0.12,
},
])
.mockResolvedValueOnce([{ type: "WEIGHT", value: 81.4, measured_at: now }])
.mockResolvedValueOnce([{ first_at: new Date(now.getTime() - 86400000) }]);
// v1.4.35 — DAY buckets feed both the per-type count/min/max/mean
// composition AND `dailyByType`. The two buckets compose to a
// count of 42 → matches the live aggregate's BigInt(42) → the
// parity check elects the rollup-derived values, which equal the
// live values byte-for-byte (chosen on purpose so the assertions
// exercise the rollup path while keeping the previous expected
// numbers stable).
ROLLUP_FIND_MANY.mockResolvedValueOnce([
{
type: "WEIGHT",
bucketStart: new Date("2026-05-10T00:00:00.000Z"),
count: 20,
mean: 81.0,
minValue: 79.2,
maxValue: 82.5,
},
{
type: "WEIGHT",
bucketStart: new Date("2026-05-11T00:00:00.000Z"),
count: 22,
mean: 83.0,
minValue: 80.0,
maxValue: 84.1,
},
]);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
describe("cold fallback when no rollup buckets exist", () => {
it("returns an empty bundle for a user with no measurements and no rollups", async () => {
// 1. per-type coverage probe — empty (no measurements) ⇒ cold path.
// 2. heavy aggregate ($queryRaw) — empty.
// 3. latests ($queryRaw) — empty.
// No firstMeasurementAt query when totalMeasurements === 0.
RAW.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([]);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
const result = await buildComprehensiveAggregate("user-pop");
const weight = result.summaries.WEIGHT;
const result = await buildComprehensiveAggregate("user-empty");
expect(weight.count).toBe(42);
expect(weight.latest).toBe(81.4);
// Composed from buckets: min(79.2, 80.0) = 79.2; max(82.5, 84.1) = 84.1.
expect(weight.min).toBe(79.2);
expect(weight.max).toBe(84.1);
// Weighted mean = (20 * 81 + 22 * 83) / 42 = 3446 / 42 = 82.0476…,
// round2 → 82.05. Matches the live `AVG` value of 82.05.
expect(weight.mean).toBe(82.05);
expect(weight.avg7).toBe(81.9);
expect(weight.avg30).toBe(82.1);
expect(weight.avg30LastMonth).toBe(83.0);
// Always null in the 90-day window — preserved legacy semantics.
expect(weight.avg30LastYear).toBeNull();
expect(weight.anomalyCount).toBe(3);
// Slope direction matches the JS threshold rules:
// -0.014 → "down" (|s| >= 0.01)
// -0.005 → "stable" (|s| < 0.01)
// 0.001 → "stable"
expect(weight.slope7).toEqual({
slope: -0.014,
direction: "down",
confidence: 0.65,
});
expect(weight.slope30).toEqual({
slope: -0.005,
direction: "stable",
confidence: 0.42,
});
expect(weight.slope90).toEqual({
slope: 0.001,
direction: "stable",
confidence: 0.12,
expect(result.summaries).toEqual({});
expect(result.bpRawRows.sys).toEqual([]);
expect(result.bpRawRows.dia).toEqual([]);
expect(result.dailyByType).toEqual({});
expect(result.firstMeasurementAt).toBeNull();
expect(result.totalMeasurements).toBe(0);
expect(RAW).toHaveBeenCalledTimes(3);
expect(FIND_MANY).toHaveBeenCalledTimes(2);
// The cold path's rollup.findMany still fires (in case some
// buckets exist for a subset of types post-race), but returns [].
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1);
});
expect(result.totalMeasurements).toBe(42);
expect(result.firstMeasurementAt).toBeInstanceOf(Date);
// `dailyByType` is sourced from the same DAY buckets; the entries
// are the bucket means rounded to 2 decimals, keyed on the UTC
// bucket-start date.
expect(result.dailyByType.WEIGHT).toEqual([
{ day: "2026-05-10", value: 81 },
{ day: "2026-05-11", value: 83 },
]);
it("runs the heavy aggregate when no rollup rows exist yet", async () => {
const now = new Date();
// 1. per-type coverage probe — WEIGHT measured but no buckets ⇒ cold path.
// 2. heavy aggregate ($queryRaw) — populated.
// 3. latests ($queryRaw) — populated.
// 4. firstMeasurementAt ($queryRaw).
RAW.mockResolvedValueOnce([{ type: "WEIGHT", has_buckets: false }])
.mockResolvedValueOnce([
{
type: "WEIGHT",
count: BigInt(42),
min_value: 79.2,
max_value: 84.1,
mean_value: 82.05,
stddev_value: 1.2,
anomaly_count: BigInt(3),
avg7: 81.9,
avg30: 82.1,
avg30_last_month: 83.0,
slope7: -0.014,
r2_7: 0.65,
slope30: -0.005,
r2_30: 0.42,
slope90: 0.001,
r2_90: 0.12,
},
])
.mockResolvedValueOnce([
{ type: "WEIGHT", value: 81.4, measured_at: now },
])
.mockResolvedValueOnce([
{ first_at: new Date(now.getTime() - 86400000) },
]);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
const result = await buildComprehensiveAggregate("user-cold");
const weight = result.summaries.WEIGHT;
expect(weight.count).toBe(42);
expect(weight.latest).toBe(81.4);
expect(weight.min).toBe(79.2);
expect(weight.max).toBe(84.1);
expect(weight.mean).toBe(82.05);
expect(weight.avg7).toBe(81.9);
expect(weight.avg30).toBe(82.1);
expect(weight.avg30LastMonth).toBe(83.0);
expect(weight.avg30LastYear).toBeNull();
expect(weight.anomalyCount).toBe(3);
expect(weight.slope7).toEqual({
slope: -0.014,
direction: "down",
confidence: 0.65,
});
expect(weight.slope30).toEqual({
slope: -0.005,
direction: "stable",
confidence: 0.42,
});
expect(weight.slope90).toEqual({
slope: 0.001,
direction: "stable",
confidence: 0.12,
});
expect(result.totalMeasurements).toBe(42);
expect(result.firstMeasurementAt).toBeInstanceOf(Date);
// The cold path's `dailyByType` is sourced from the same rollup
// findMany call (returns [] here since no buckets exist). The
// contract is: even on cold mount, if buckets do exist for some
// type, they feed `dailyByType` so the v1.4.35 shape is stable.
expect(result.dailyByType.WEIGHT).toBeUndefined();
});
});
it("threads sys/dia raw rows through bpRawRows so 5-min pairing survives", async () => {
const measuredAt = new Date("2026-05-10T08:00:00Z");
RAW.mockResolvedValueOnce([
{
type: "BLOOD_PRESSURE_SYS",
count: BigInt(1),
min_value: 120,
max_value: 120,
mean_value: 120,
stddev_value: 0,
anomaly_count: BigInt(0),
avg7: 120,
avg30: 120,
avg30_last_month: null,
slope7: null,
r2_7: null,
slope30: null,
r2_30: null,
slope90: null,
r2_90: null,
},
])
// Cold path so the heavy aggregate fires (BP type lacks coverage).
RAW.mockResolvedValueOnce([{ type: "BLOOD_PRESSURE_SYS", has_buckets: false }])
.mockResolvedValueOnce([
{
type: "BLOOD_PRESSURE_SYS",
count: BigInt(1),
min_value: 120,
max_value: 120,
mean_value: 120,
stddev_value: 0,
anomaly_count: BigInt(0),
avg7: 120,
avg30: 120,
avg30_last_month: null,
slope7: null,
r2_7: null,
slope30: null,
r2_30: null,
slope90: null,
r2_90: null,
},
])
.mockResolvedValueOnce([
{
type: "BLOOD_PRESSURE_SYS",
@@ -0,0 +1,144 @@
import { describe, expect, it } from "vitest";
import { applyInsightsExcludeFilter } from "../exclude-filter";
import type { AggregatedFeatures } from "../features";
function baseFeatures(): AggregatedFeatures {
return {
weight: {
latest: 82,
avg7: 82,
avg30: 82,
avg90: 82,
allTimeAvg: 82,
allTimeMin: 80,
allTimeMax: 84,
slope30: 0,
outlierCount: 0,
bmi: 25,
coverage: {
count: 10,
spanDays: 30,
avgDaysBetween: 3,
oldestDaysAgo: 30,
newestDaysAgo: 1,
},
},
bloodPressure: {
avgSys30: 128,
avgDia30: 82,
avgSys90: 128,
avgDia90: 82,
allTimeAvgSys: 128,
allTimeAvgDia: 82,
allTimeMinSys: 110,
allTimeMaxSys: 145,
allTimeMinDia: 70,
allTimeMaxDia: 90,
slopeSys30: 0,
slopeDia30: 0,
sdSys30: 5,
sdDia30: 3,
pulsePressure30: 46,
pctInTarget: 70,
coverage: {
count: 10,
spanDays: 30,
avgDaysBetween: 3,
oldestDaysAgo: 30,
newestDaysAgo: 1,
},
},
medications: [
{
name: "Mounjaro",
dose: "7.5mg",
category: "GLP1",
compliance7: 100,
compliance30: 95,
compliance90: 92,
streak: 30,
missedLast7: 0,
},
],
context: {
heightCm: 180,
hasBpTargets: true,
totalMeasurements: 100,
dataSpanDays: 365,
oldestMeasurementDaysAgo: 365,
newestMeasurementDaysAgo: 1,
ageYears: 45,
gender: "MALE",
},
};
}
describe("applyInsightsExcludeFilter", () => {
it("returns the original payload unchanged when excludeList is empty", () => {
const f = baseFeatures();
const out = applyInsightsExcludeFilter(f, []);
expect(out).toBe(f);
});
it("drops weight when 'weight' is excluded", () => {
const f = baseFeatures();
const out = applyInsightsExcludeFilter(f, ["weight"]);
expect(out.weight).toBeUndefined();
expect(out.bloodPressure).toBeDefined();
});
it("drops blood pressure when 'bp' is excluded", () => {
const f = baseFeatures();
const out = applyInsightsExcludeFilter(f, ["bp"]);
expect(out.bloodPressure).toBeUndefined();
expect(out.weight).toBeDefined();
});
it("drops medications when 'medications' or 'compliance' is excluded", () => {
expect(
applyInsightsExcludeFilter(baseFeatures(), ["medications"]).medications,
).toBeUndefined();
expect(
applyInsightsExcludeFilter(baseFeatures(), ["compliance"]).medications,
).toBeUndefined();
});
it("strips anthropometrics from context without nuking the whole block", () => {
const out = applyInsightsExcludeFilter(baseFeatures(), ["anthropometrics"]);
expect(out.context.heightCm).toBeNull();
expect(out.context.ageYears).toBeNull();
expect(out.context.gender).toBeNull();
// The aggregate totals stay — those aren't PII.
expect(out.context.dataSpanDays).toBe(365);
expect(out.context.totalMeasurements).toBe(100);
});
it("applies multiple exclusions in one pass", () => {
const out = applyInsightsExcludeFilter(baseFeatures(), [
"weight",
"bp",
"anthropometrics",
]);
expect(out.weight).toBeUndefined();
expect(out.bloodPressure).toBeUndefined();
expect(out.context.heightCm).toBeNull();
});
it("does not mutate the input", () => {
const f = baseFeatures();
applyInsightsExcludeFilter(f, ["weight", "bp", "anthropometrics"]);
expect(f.weight).toBeDefined();
expect(f.bloodPressure).toBeDefined();
expect(f.context.heightCm).toBe(180);
});
it("is a no-op for tokens with no mapped feature keys (hrv / resting_hr)", () => {
const out = applyInsightsExcludeFilter(baseFeatures(), [
"hrv",
"resting_hr",
]);
expect(out.weight).toBeDefined();
expect(out.bloodPressure).toBeDefined();
});
});
+153
View File
@@ -0,0 +1,153 @@
import { describe, expect, it, vi, beforeEach } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
user: { findUnique: vi.fn() },
measurement: { findMany: vi.fn() },
measurementRollup: { findMany: vi.fn() },
moodEntry: { findMany: vi.fn() },
medication: { findMany: vi.fn() },
medicationIntakeEvent: { findMany: vi.fn() },
},
}));
vi.mock("@/lib/medication-category", () => ({
getMedicationCategories: vi.fn(async () => ({})),
}));
import { prisma } from "@/lib/db";
import {
extractFeatures,
FeaturesPayloadTooLargeError,
FEATURES_MAX_BYTES,
type RawFeatures,
} from "../features";
const prismaMock = prisma as unknown as {
user: { findUnique: ReturnType<typeof vi.fn> };
measurement: { findMany: ReturnType<typeof vi.fn> };
measurementRollup: { findMany: ReturnType<typeof vi.fn> };
moodEntry: { findMany: ReturnType<typeof vi.fn> };
medication: { findMany: ReturnType<typeof vi.fn> };
medicationIntakeEvent: { findMany: ReturnType<typeof vi.fn> };
};
const dayMs = 24 * 60 * 60 * 1000;
function rollupRow(daysAgo: number, mean: number, count: number) {
return {
bucketStart: new Date(Date.now() - daysAgo * dayMs),
count,
mean,
minValue: mean,
maxValue: mean,
sd: 0,
slope: 0,
r2: 0,
computedAt: new Date(),
};
}
beforeEach(() => {
vi.resetAllMocks();
prismaMock.user.findUnique.mockResolvedValue({
heightCm: 180,
dateOfBirth: new Date("1980-01-01"),
gender: "MALE",
});
prismaMock.measurement.findMany.mockResolvedValue([]);
prismaMock.measurementRollup.findMany.mockResolvedValue([]);
prismaMock.moodEntry.findMany.mockResolvedValue([]);
prismaMock.medication.findMany.mockResolvedValue([]);
prismaMock.medicationIntakeEvent.findMany.mockResolvedValue([]);
});
describe("extractFeatures — v1.4.36 W3 bucketed payload", () => {
it("does not attach bucketedMeasurements when includeRaw=false", async () => {
const features = await extractFeatures("user-1", false);
const asRecord = features as unknown as Record<string, unknown>;
expect(asRecord.bucketedMeasurements).toBeUndefined();
expect(asRecord.rawMeasurements).toBeUndefined();
});
it("attaches DAY / WEEK / MONTH buckets from measurement_rollups when includeRaw=true", async () => {
// Two WEIGHT DAY buckets in the 0-90d window and one MONTH bucket
// in the 365-1825d window. The reader is called once per
// (type, granularity) combination — return data only for the two
// we care about, empty for the rest.
prismaMock.measurementRollup.findMany.mockImplementation(
async (args: { where: { type: string; granularity: string } }) => {
if (args.where.type === "WEIGHT" && args.where.granularity === "DAY") {
return [rollupRow(10, 82.5, 2), rollupRow(20, 82.7, 1)];
}
if (
args.where.type === "WEIGHT" &&
args.where.granularity === "MONTH"
) {
return [rollupRow(400, 85.1, 28)];
}
return [];
},
);
const features = (await extractFeatures("user-1", true)) as RawFeatures;
expect(features.bucketedMeasurements).toBeDefined();
const weightDay = features.bucketedMeasurements.find(
(s) => s.type === "WEIGHT" && s.granularity === "DAY",
);
expect(weightDay).toBeDefined();
expect(weightDay?.buckets).toHaveLength(2);
expect(weightDay?.buckets[0]).toMatchObject({
mean: 82.5,
count: 2,
});
expect(weightDay?.buckets[0].bucketStart).toMatch(
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
);
const weightMonth = features.bucketedMeasurements.find(
(s) => s.type === "WEIGHT" && s.granularity === "MONTH",
);
expect(weightMonth?.buckets).toHaveLength(1);
expect(weightMonth?.buckets[0].count).toBe(28);
});
it("drops empty (type, granularity) series so the payload never carries labelled-but-empty arrays", async () => {
prismaMock.measurementRollup.findMany.mockResolvedValue([]);
const features = (await extractFeatures("user-1", true)) as RawFeatures;
expect(features.bucketedMeasurements).toEqual([]);
});
it("keeps anthropometrics (heightCm / ageYears / gender) on context", async () => {
const features = await extractFeatures("user-1", false);
expect(features.context.heightCm).toBe(180);
expect(features.context.gender).toBe("MALE");
expect(features.context.ageYears).toBeGreaterThan(40);
});
it("throws FeaturesPayloadTooLargeError when the serialised payload exceeds the 5 MB cap", async () => {
// Fabricate an absurdly long bucket list so the JSON dump crosses
// the ceiling. ~200 KB per series × 28 series ≈ ~5.6 MB.
const giant = new Array(200_000).fill(null).map((_, i) => ({
bucketStart: new Date(Date.now() - i * dayMs),
count: i,
mean: i,
minValue: i,
maxValue: i,
sd: 0,
slope: 0,
r2: 0,
computedAt: new Date(),
}));
prismaMock.measurementRollup.findMany.mockResolvedValue(giant);
await expect(extractFeatures("user-1", true)).rejects.toThrow(
FeaturesPayloadTooLargeError,
);
});
it("exposes the size cap as a public constant", () => {
expect(FEATURES_MAX_BYTES).toBe(5 * 1024 * 1024);
});
});
@@ -0,0 +1,74 @@
import { describe, expect, it } from "vitest";
import { compactSections } from "../prompt-compact";
describe("compactSections", () => {
it("drops empty arrays", () => {
const out = compactSections({ sleep: [], weight: [{ v: 1 }] });
expect(out).toEqual({ weight: [{ v: 1 }] });
});
it("drops empty plain objects", () => {
const out = compactSections({ medications: {}, weight: { latest: 82 } });
expect(out).toEqual({ weight: { latest: 82 } });
});
it("drops undefined values", () => {
const out = compactSections({ weight: undefined, bp: { avg: 128 } });
expect(out).toEqual({ bp: { avg: 128 } });
});
it("preserves null (null is a meaningful 'unknown' marker, not 'empty')", () => {
const out = compactSections({ avg30: null, avg7: 82 });
expect(out).toEqual({ avg30: null, avg7: 82 });
});
it("preserves nested objects shallow by default (count: 0 stays)", () => {
const out = compactSections({
coverage: { count: 0, spanDays: 0 },
bp: { avg30: 128 },
});
expect(out.coverage).toEqual({ count: 0, spanDays: 0 });
});
it("deep mode recurses into nested plain objects", () => {
const out = compactSections(
{
bp: { avg30: 128, history: [] },
ghost: { medications: [] },
},
{ deep: true },
);
expect(out).toEqual({ bp: { avg30: 128 } });
});
it("preserves arrays of objects with content", () => {
const out = compactSections({
medications: [{ name: "Mounjaro" }],
bp: [],
});
expect(out).toEqual({ medications: [{ name: "Mounjaro" }] });
});
it("returns a new object — does not mutate the input", () => {
const input = { sleep: [], weight: 82 } as Record<string, unknown>;
const out = compactSections(input);
expect(input.sleep).toEqual([]);
expect(out).not.toBe(input);
});
it("renders empty input as empty output", () => {
expect(compactSections({})).toEqual({});
});
it("does not drop primitive zeros, false, or empty strings", () => {
// These are legitimate data signals — only structural emptiness
// (empty arrays/objects/undefined) is filtered.
const out = compactSections({
count: 0,
enabled: false,
label: "",
});
expect(out).toEqual({ count: 0, enabled: false, label: "" });
});
});
+394 -124
View File
@@ -1,6 +1,6 @@
/**
* v1.4.35 — SQL-driven aggregator for `/api/insights/comprehensive`,
* with the partial read-swap onto `measurement_rollups` landed.
* v1.4.36 — SQL-driven aggregator for `/api/insights/comprehensive`,
* rollup-first read with live-SQL fallback on cold mounts.
*
* Background
* ----------
@@ -21,39 +21,42 @@
* slim slice: the comprehensive endpoint also needs `anomalyCount`,
* `avg30LastMonth`, `avg30LastYear`, and the 90-day window cap.
*
* Read-swap (v1.4.35)
* -------------------
* Two surfaces now read from the persistent `measurement_rollups`
* table instead of recomputing per request:
* Read-swap (v1.4.35 → v1.4.36)
* -----------------------------
* v1.4.35 landed a partial read-swap that ran both the live aggregate
* AND the rollup read in parallel, then preferred the rollup-derived
* values when the count matched. That left the regression intact: the
* heavy `$queryRaw` over `measurements` still ran on every request,
* still scanning the 311k-row table on a power-user account.
*
* - **`count / min / max / mean`** per type — composed from the DAY
* rollup buckets via `aggregateBuckets` (sum of counts; min of
* mins; max of maxes; count-weighted mean). These four are
* linearly composable across DAY buckets, so the composed value
* is mathematically identical to the live `AVG` / `MIN` / `MAX`
* / `COUNT(*)` over the same rows. A defensive parity check
* against the live aggregate's `COUNT(*)` falls back to live SQL
* when divergence is detected — covers the cold-mount edge case
* where the rollup populator is mid-flight, or a sub-second race
* between the watermark check and the bucket read.
* v1.4.36 promotes the rollup table to the canonical source on the
* happy path:
*
* - **`dailyByType`** — read directly from the DAY rollup buckets
* keyed on (userId, type, granularity=DAY, bucketStart). The
* bucket's stored `mean` is the same `AVG(value)` per day the
* legacy SQL emitted via `date_trunc('day', measured_at)`, so
* downstream correlation pairings remain byte-shape stable.
* - On entry we call `ensureUserRollupsFresh` (warm-on-read populator
* for the trailing 90-day DAY window) and then probe the rollup
* table with a single COUNT.
*
* Everything else stays on live SQL:
* - `slope7 / r2_7`, `slope30 / r2_30`, `slope90 / r2_90` — slope
* and R² do **not** compose linearly across DAY buckets.
* - `stddev`, `anomalyCount` — same reason.
* - `avg7 / avg30 / avg30LastMonth` — these windows aren't aligned
* to DAY-bucket boundaries (the 7-day window slides relative to
* `NOW()`), so the live `FILTER (WHERE measured_at >= NOW() -
* INTERVAL '7 days')` clause is canonical.
* - `latest / latestMeasuredAt` — the `DISTINCT ON (type)` pass
* resolves the most recent raw row, which the bucket can't
* surface (it stores the day's mean, not the day's last value).
* - When the DAY rollup table has rows for this user the **heavy**
* `$queryRaw` over `measurements` is skipped. `count / min / max /
* mean` per type compose from the persisted DAY buckets via
* `aggregateBuckets`; `dailyByType` reads the same buckets keyed
* on `(type, bucketStart)`.
*
* - A **narrow** `$queryRaw` still fires for the non-composable
* fields: `stddev`, `anomalyCount`, `avg7 / avg30 / avg30LastMonth`,
* and the `slope7 / slope30 / slope90` tuples. These don't compose
* linearly across DAY buckets so live SQL stays canonical, but the
* query no longer carries the redundant `COUNT(*)` / `MIN` / `MAX`
* / `AVG` columns that duplicated the bucket-derived values.
*
* - `latest / latestMeasuredAt` keep their dedicated `DISTINCT ON
* (type)` pass; the bucket stores the day's mean rather than the
* last reading.
*
* - Fallback path — when the COUNT probe returns zero rows
* (post-restart cold mount or a brand-new account whose boot-time
* backfill is mid-flight) we run the legacy heavy aggregate so the
* response shape is correct on the very first request.
*
* Scope notes
* -----------
@@ -98,14 +101,18 @@ import { prisma } from "@/lib/db";
import type { DataSummary } from "@/lib/analytics/trends";
import { ensureUserRollupsFresh } from "@/lib/measurements/rollups";
import { aggregateBuckets } from "@/lib/measurements/rollup-read";
import {
isFullyCovered,
probeRollupCoverage,
} from "@/lib/measurements/rollup-coverage";
/**
* Raw row from the aggregate query. Keyed on `MeasurementType`, with
* windowed AVG/SLOPE/R2 expressions plus anomaly-detection inputs
* (window-wide mean + stddev so we can compute the |z| > 2 count
* client-side without a second pass over the raw rows).
* Heavy aggregate row — count/min/max/mean alongside the non-composable
* windowed columns. Used on the cold-mount fallback path where the
* rollup table is empty (the populator hasn't caught up yet) so the
* response shape is correct on the very first request.
*/
interface AggregateRow {
interface HeavyAggregateRow {
type: string;
count: bigint;
min_value: number | null;
@@ -124,6 +131,29 @@ interface AggregateRow {
r2_90: number | null;
}
/**
* Narrow aggregate row — only the columns the DAY rollup buckets cannot
* compose linearly. `count` rides along so the anomaly z-score loop
* knows the divisor for the window-wide mean / stddev; the per-type
* count exposed on `DataSummary` still comes from the bucket
* composition.
*/
interface NarrowAggregateRow {
type: string;
count: bigint;
stddev_value: number | null;
anomaly_count: bigint;
avg7: number | null;
avg30: number | null;
avg30_last_month: number | null;
slope7: number | null;
r2_7: number | null;
slope30: number | null;
r2_30: number | null;
slope90: number | null;
r2_90: number | null;
}
/** Latest raw value per type (DISTINCT ON over the 90-day window). */
interface LatestRow {
type: string;
@@ -183,35 +213,61 @@ function round2(value: number | null): number | null {
/**
* Build the per-type `DataSummary` bundle for the comprehensive route.
*
* Five queries against the trailing 90 days, in parallel:
* Two read shapes:
*
* 1. Per-type aggregate (count + min/max/mean/stddev + windowed
* avgs + regr_slope/r2 for the 7/30/90-day slopes). The
* `count / min / max / mean` columns are kept on this query as
* the live-SQL fallback for the v1.4.35 read-swap parity check.
* 2. `DISTINCT ON (type)` for the most-recent raw value per type.
* 3. DAY rollup buckets from `measurement_rollups` (v1.4.35) —
* composed into `count / min / max / mean` for the per-type
* summary and replayed as the `dailyByType` correlation feed.
* 4. Bounded raw-row sys query for BP target pairing (sub-day
* timestamps, can't be replaced by a daily mean).
* 5. Bounded raw-row dia query for BP target pairing.
* - **Rollup-fresh path** (the happy path on every warm request):
* `count / min / max / mean` + `dailyByType` compose from the
* persisted DAY buckets; a narrow `$queryRaw` carries only the
* non-composable windowed columns. The heavy COUNT/MIN/MAX/AVG
* aggregate is **not** executed.
*
* - **Live fallback path** (cold mount before the populator catches
* up, or a brand-new account whose backfill is mid-flight): the
* legacy heavy aggregate runs so the response shape stays correct
* on the very first request. Subsequent requests fall back to the
* rollup-fresh path once `ensureUserRollupsFresh` has written
* buckets.
*/
export async function buildComprehensiveAggregate(
userId: string,
): Promise<ComprehensiveAggregate> {
const ninetyDaysAgo = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// v1.4.35 — keep the persistent rollup table current before any
// read fires. After this returns the DAY-granularity buckets for
// the trailing 90 days reflect every measurement that landed before
// this call (the per-write hook is synchronous on the DAY bucket,
// and `ensureUserRollupsFresh` covers cold-mount / process-restart
// cases). The bucket reads below depend on that guarantee.
// Keep the persistent rollup table current before any read fires.
// After this returns the DAY-granularity buckets for the trailing
// 90 days reflect every measurement that landed before this call
// (the per-write hook is synchronous on the DAY bucket, and
// `ensureUserRollupsFresh` covers cold-mount / process-restart).
await ensureUserRollupsFresh(userId);
const [aggregates, latests, dayBuckets, sysRaw, diaRaw] = await Promise.all([
prisma.$queryRaw<AggregateRow[]>`
// v1.4.36 QA C1 — per-type coverage probe. The legacy global COUNT
// returned true as soon as any one type had a DAY bucket, which made
// the bucket-derived path swallow a brand-new type's all-time history
// (the per-write hook upserts a single bucket for the new type, the
// probe flips to true, the comprehensive response then iterates only
// bucket-bearing types and the trailing-90d narrow aggregate becomes
// the new "all-time" total for that type). We now decide per type
// and only take the rollup path when EVERY type the user has logged
// is covered. Partial coverage falls back to the live aggregate so
// a fresh-type measurement doesn't break its summary card.
const coverage = await probeRollupCoverage(userId);
if (isFullyCovered(coverage)) {
return buildFromRollups(userId, ninetyDaysAgo);
}
return buildFromLiveAggregate(userId, ninetyDaysAgo);
}
/**
* Happy path — DAY buckets carry the composable stats; a narrow
* `$queryRaw` carries only the windowed / regression columns the
* buckets cannot reconstruct.
*/
async function buildFromRollups(
userId: string,
ninetyDaysAgo: Date,
): Promise<ComprehensiveAggregate> {
const [narrows, latests, dayBuckets, sysRaw, diaRaw, firstRows] = await Promise.all([
prisma.$queryRaw<NarrowAggregateRow[]>`
WITH window_stats AS (
SELECT
m."type",
@@ -225,9 +281,6 @@ export async function buildComprehensiveAggregate(
SELECT
m."type"::text AS type,
COUNT(*) AS count,
MIN(m."value")::double precision AS min_value,
MAX(m."value")::double precision AS max_value,
AVG(m."value")::double precision AS mean_value,
ws.stddev_value::double precision AS stddev_value,
-- |z| > 2 anomaly count over the 90-day window. STDDEV_POP
-- matches the JS helper which divides by n (not n-1).
@@ -333,12 +386,14 @@ export async function buildComprehensiveAggregate(
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
prisma.$queryRaw<Array<{ first_at: Date | null }>>`
SELECT MIN(m."measured_at") AS first_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
`,
]);
// ── avg30LastYear ─────────────────────────────────────────
// The 90-day window guarantees this is null (no rows >= 365 days
// old will appear) — preserves legacy semantics.
const latestByType = new Map<string, { value: number; measuredAt: Date }>();
for (const row of latests) {
latestByType.set(row.type, {
@@ -347,53 +402,214 @@ export async function buildComprehensiveAggregate(
});
}
// v1.4.35 — partition DAY buckets by type so the per-type aggregate
// loop below can compose `count / min / max / mean` from them in O(1).
const bucketsByType = new Map<
string,
Array<{
day: Date;
count: number;
mean: number;
minValue: number;
maxValue: number;
}>
>();
for (const b of dayBuckets) {
const list = bucketsByType.get(b.type) ?? [];
list.push({
day: b.bucketStart,
count: b.count,
mean: b.mean,
minValue: b.minValue,
maxValue: b.maxValue,
});
bucketsByType.set(b.type, list);
const bucketsByType = partitionBucketsByType(dayBuckets);
const narrowByType = new Map<string, NarrowAggregateRow>();
for (const row of narrows) {
narrowByType.set(row.type, row);
}
// Seed only types that actually have rows — the legacy route only
// populated `summaries[t]` when `data.length > 0`.
const summaries: Record<string, DataSummary> = {};
let totalMeasurements = 0;
// Seed every type that has buckets — the bucket set is the source of
// truth on this path. The narrow aggregate provides the non-composable
// windowed columns alongside.
for (const [type, buckets] of bucketsByType.entries()) {
const composed = aggregateBuckets(buckets);
if (composed.count === 0) continue;
totalMeasurements += composed.count;
const latest = latestByType.get(type);
const narrow = narrowByType.get(type);
summaries[type] = {
count: composed.count,
latest: latest?.value ?? null,
min: round2(composed.min),
max: round2(composed.max),
mean: round2(composed.mean),
avg7: round2(narrow?.avg7 ?? null),
avg30: round2(narrow?.avg30 ?? null),
slope7: buildSlope(narrow?.slope7 ?? null, narrow?.r2_7 ?? null),
slope30: buildSlope(narrow?.slope30 ?? null, narrow?.r2_30 ?? null),
slope90: buildSlope(narrow?.slope90 ?? null, narrow?.r2_90 ?? null),
anomalyCount: Number(narrow?.anomaly_count ?? 0),
avg30LastMonth: round2(narrow?.avg30_last_month ?? null),
// Legacy semantics: the 90-day window guarantees no rows from a
// year ago. Always null.
avg30LastYear: null,
};
}
const firstMeasurementAt = firstRows[0]?.first_at
? new Date(firstRows[0].first_at)
: null;
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
dailyByType: buildDailyByType(bucketsByType),
firstMeasurementAt,
totalMeasurements,
};
}
/**
* Cold fallback — the rollup table is empty for this user (e.g. brand-
* new account, or post-restart before the boot-time backfill landed).
* Runs the legacy heavy aggregate so the response shape is correct on
* the very first request; subsequent requests will see the rollup
* populator catch up and route through `buildFromRollups`.
*/
async function buildFromLiveAggregate(
userId: string,
ninetyDaysAgo: Date,
): Promise<ComprehensiveAggregate> {
const [aggregates, latests, dayBuckets, sysRaw, diaRaw] = await Promise.all([
prisma.$queryRaw<HeavyAggregateRow[]>`
WITH window_stats AS (
SELECT
m."type",
AVG(m."value") AS mean_value,
STDDEV_POP(m."value") AS stddev_value
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
GROUP BY m."type"
)
SELECT
m."type"::text AS type,
COUNT(*) AS count,
MIN(m."value")::double precision AS min_value,
MAX(m."value")::double precision AS max_value,
AVG(m."value")::double precision AS mean_value,
ws.stddev_value::double precision AS stddev_value,
COUNT(*) FILTER (
WHERE ws.stddev_value IS NOT NULL
AND ws.stddev_value > 0
AND ABS(
(ROUND(((m."value" - ws.mean_value) / ws.stddev_value)::numeric, 2))
) > 2
) AS anomaly_count,
AVG(m."value") FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS avg7,
AVG(m."value") FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '30 days'
)::double precision AS avg30,
AVG(m."value") FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '60 days'
AND m."measured_at" < NOW() - INTERVAL '30 days'
)::double precision AS avg30_last_month,
REGR_SLOPE(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS slope7,
REGR_R2(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '7 days'
)::double precision AS r2_7,
REGR_SLOPE(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '30 days'
)::double precision AS slope30,
REGR_R2(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '30 days'
)::double precision AS r2_30,
REGR_SLOPE(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '90 days'
)::double precision AS slope90,
REGR_R2(
m."value",
EXTRACT(EPOCH FROM m."measured_at") / 86400.0
) FILTER (
WHERE m."measured_at" >= NOW() - INTERVAL '90 days'
)::double precision AS r2_90
FROM measurements m
JOIN window_stats ws ON ws."type" = m."type"
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
GROUP BY m."type", ws.stddev_value
`,
prisma.$queryRaw<LatestRow[]>`
SELECT DISTINCT ON (m."type")
m."type"::text AS type,
m."value"::double precision AS value,
m."measured_at" AS measured_at
FROM measurements m
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
ORDER BY m."type", m."measured_at" DESC
`,
// Buckets may exist for some types even when the COUNT probe came
// back zero (race window between the probe and a sibling
// populator); the cold-path read still honours them for the
// `dailyByType` feed.
prisma.measurementRollup.findMany({
where: {
userId,
granularity: "DAY",
bucketStart: { gte: ninetyDaysAgo },
},
orderBy: [{ type: "asc" }, { bucketStart: "asc" }],
}),
prisma.measurement.findMany({
where: {
userId,
type: "BLOOD_PRESSURE_SYS",
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
prisma.measurement.findMany({
where: {
userId,
type: "BLOOD_PRESSURE_DIA",
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { measuredAt: true, value: true },
}),
]);
const latestByType = new Map<string, { value: number; measuredAt: Date }>();
for (const row of latests) {
latestByType.set(row.type, {
value: Number(row.value),
measuredAt: new Date(row.measured_at),
});
}
const bucketsByType = partitionBucketsByType(dayBuckets);
const summaries: Record<string, DataSummary> = {};
let totalMeasurements = 0;
let firstMeasurementAt: Date | null = null;
for (const row of aggregates) {
const liveCount = Number(row.count);
// Compose count/min/max/mean from the DAY buckets when they agree
// with the live `COUNT(*)`. Parity divergence (cold mount, rollup
// populator mid-flight, watermark race) falls back to live SQL so
// the response shape never serves a wrong value.
const composed = aggregateBuckets(bucketsByType.get(row.type) ?? []);
const useRollup = composed.count === liveCount;
const count = useRollup ? composed.count : liveCount;
const count = Number(row.count);
totalMeasurements += count;
const latest = latestByType.get(row.type);
summaries[row.type] = {
count,
latest: latest?.value ?? null,
min: useRollup ? round2(composed.min) : round2(row.min_value),
max: useRollup ? round2(composed.max) : round2(row.max_value),
mean: useRollup ? round2(composed.mean) : round2(row.mean_value),
min: round2(row.min_value),
max: round2(row.max_value),
mean: round2(row.mean_value),
avg7: round2(row.avg7),
avg30: round2(row.avg30),
slope7: buildSlope(row.slope7, row.r2_7),
@@ -401,17 +617,10 @@ export async function buildComprehensiveAggregate(
slope90: buildSlope(row.slope90, row.r2_90),
anomalyCount: Number(row.anomaly_count),
avg30LastMonth: round2(row.avg30_last_month),
// Legacy semantics: the 90-day findMany window guarantees no
// rows from a year ago. Always null.
avg30LastYear: null,
};
}
// ── firstMeasurementAt ───────────────────────────────────
// Take the earliest `measuredAt` across the user's 90-day rows.
// One extra cheap aggregate query keeps the route's `dataSpanDays`
// truthful. Run lazily: skip when no aggregate rows came back
// (totalMeasurements === 0).
if (totalMeasurements > 0) {
const firstRows = await prisma.$queryRaw<Array<{ first_at: Date | null }>>`
SELECT MIN(m."measured_at") AS first_at
@@ -424,13 +633,84 @@ export async function buildComprehensiveAggregate(
: null;
}
// ── daily-by-type ────────────────────────────────────────
// v1.4.35 — sourced from the DAY rollup buckets. The legacy
// implementation ran a dedicated `date_trunc('day', m."measured_at")`
// GROUP BY scan and only returned the three types the correlation
// pairings consume. We filter to the same three types here so
// downstream consumers see a byte-identical shape, but the data
// origin is now the persisted bucket — no extra SQL pass needed.
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
dailyByType: buildDailyByType(bucketsByType),
firstMeasurementAt,
totalMeasurements,
};
}
/**
* Partition DAY buckets by type so the per-type aggregator can compose
* `count / min / max / mean` from them in O(1) and the daily-by-type
* builder can replay them as the correlation feed.
*/
function partitionBucketsByType(
rows: ReadonlyArray<{
type: string;
bucketStart: Date;
count: number;
mean: number;
minValue: number;
maxValue: number;
}>,
): Map<
string,
Array<{
day: Date;
count: number;
mean: number;
minValue: number;
maxValue: number;
}>
> {
const map = new Map<
string,
Array<{
day: Date;
count: number;
mean: number;
minValue: number;
maxValue: number;
}>
>();
for (const b of rows) {
const list = map.get(b.type) ?? [];
list.push({
day: b.bucketStart,
count: b.count,
mean: b.mean,
minValue: b.minValue,
maxValue: b.maxValue,
});
map.set(b.type, list);
}
return map;
}
/**
* `dailyByType` is sourced from DAY rollup buckets, filtered to the
* three types the correlation pairings consume so downstream consumers
* see a byte-identical shape with the legacy
* `date_trunc('day', m."measured_at")` GROUP BY query.
*/
function buildDailyByType(
bucketsByType: Map<
string,
Array<{
day: Date;
count: number;
mean: number;
minValue: number;
maxValue: number;
}>
>,
): Record<string, Array<{ day: string; value: number }>> {
const dailyByType: Record<string, Array<{ day: string; value: number }>> = {};
const DAILY_TYPES = ["WEIGHT", "BLOOD_PRESSURE_SYS", "PULSE"] as const;
for (const type of DAILY_TYPES) {
@@ -447,16 +727,6 @@ export async function buildComprehensiveAggregate(
value: round2(b.mean) ?? 0,
}));
}
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
dailyByType,
firstMeasurementAt,
totalMeasurements,
};
return dailyByType;
}
+76
View File
@@ -0,0 +1,76 @@
/**
* v1.4.36 W3 T3 — exclude-metrics filter for the AI Insights payload.
*
* Mirrors the Coach snapshot's `excludeMetrics` contract so the user
* has a single privacy contract across both surfaces: what's hidden
* from the Coach turn stays hidden from the Insights generation. The
* filter drops matching keys off the features object BEFORE
* `JSON.stringify` so the LLM never sees the excluded data.
*
* The token vocabulary is the same `coachExcludeMetricEnum` from
* `src/lib/validations/coach-prefs.ts` — see the comment there for
* the canonical list. The mapping below pairs each token with the
* features.ts field keys it gates; tokens with no mapped fields
* (e.g. Apple Health metrics that don't have a dedicated features
* branch yet) are no-ops on this surface but keep the contract
* symmetric so adding a future field is one-line.
*
* Anthropometrics is special-cased: rather than dropping the whole
* `context` field (which carries non-PII totals like `dataSpanDays`),
* we strip just `heightCm` / `ageYears` / `gender` from it.
*/
import type { AggregatedFeatures, RawFeatures } from "./features";
/**
* Map exclude-token → features.ts top-level keys to remove. `null`
* means the token gates a substructure inside `context` and is
* handled inline below.
*/
const EXCLUDE_TO_FEATURE_KEYS: Record<string, ReadonlyArray<keyof AggregatedFeatures>> = {
bp: ["bloodPressure"],
weight: ["weight"],
pulse: ["pulse"],
mood: ["mood"],
compliance: ["medications"],
medications: ["medications"],
sleep: ["sleep"],
steps: ["activity"],
// hrv / resting_hr — no dedicated features branch yet (carried only
// by the Coach Apple-Health blocks). Keep the entry for symmetry +
// future-proofing.
hrv: [],
resting_hr: [],
};
/**
* Return a shallow copy of `features` with every key gated by an
* active exclude-token removed. Idempotent. Original payload is not
* mutated — same posture as the snapshot filter so call sites can
* keep both the filtered + unfiltered shape if they need to.
*/
export function applyInsightsExcludeFilter<
T extends AggregatedFeatures | RawFeatures,
>(features: T, excludeMetrics: ReadonlyArray<string>): T {
if (excludeMetrics.length === 0) return features;
const next = { ...features } as Record<string, unknown>;
for (const token of excludeMetrics) {
const keys = EXCLUDE_TO_FEATURE_KEYS[token];
if (keys) {
for (const key of keys) {
delete next[key as string];
}
}
if (token === "anthropometrics" && next.context && typeof next.context === "object") {
// Anthropometrics gates the profile sub-fields inside `context`.
// The rest of context (totals, span days) stays — those are
// aggregate counts the model needs for narrative coverage even
// when the profile is hidden.
const ctx = { ...(next.context as Record<string, unknown>) };
ctx.heightCm = null;
ctx.ageYears = null;
ctx.gender = null;
next.context = ctx;
}
}
return next as T;
}
+159 -13
View File
@@ -15,6 +15,11 @@ import {
type CorrelationResult,
} from "@/lib/analytics/correlations";
import { getMedicationCategories } from "@/lib/medication-category";
import { readRollupBuckets } from "@/lib/measurements/rollups";
import type {
MeasurementType,
RollupGranularity,
} from "@/generated/prisma/client";
interface DataCoverage {
count: number;
@@ -166,14 +171,100 @@ export interface AggregatedFeatures {
};
}
export interface RawFeatures extends AggregatedFeatures {
rawMeasurements: Array<{
type: string;
value: number;
dayOffset: number; // days ago (anonymized — no exact date)
/**
* One bucketed series. `granularity` decides the bucket width (DAY for
* the trailing 90-day window, WEEK for 90→365 days, MONTH for the
* 365→1825-day deep history). `bucketStart` is an ISO-8601 UTC
* timestamp anchored to the start of the bucket — Postgres
* `date_trunc()` semantics, same as the rollup populator.
*
* Replaces v1.4.35.x `rawMeasurements` (which appended every
* measurement row and blew the 10 MB Codex prompt ceiling on power
* users with multi-year imports — 25.9 MB observed on Marc's account
* with 311 775 rows). Reading from `measurement_rollups` instead caps
* the payload at O(types × buckets) regardless of underlying row
* count.
*/
export interface BucketedSeries {
type: string;
granularity: RollupGranularity;
buckets: Array<{
bucketStart: string;
mean: number;
count: number;
}>;
}
export interface RawFeatures extends AggregatedFeatures {
bucketedMeasurements: BucketedSeries[];
}
/**
* v1.4.36 W3 T1 — payload size ceiling. The Codex provider rejects
* any prompt-string above 10 MB; the features payload is roughly half
* the eventual prompt (the JSON dump plus the system + comparison
* blocks), so a 5 MB feature payload sits comfortably below the
* ceiling even after locale-prefix expansion. Throwing here lets the
* route handler downgrade to the non-raw payload + annotate so we
* catch the regression instead of blowing the upstream call.
*/
export const FEATURES_MAX_BYTES = 5 * 1024 * 1024;
export class FeaturesPayloadTooLargeError extends Error {
readonly code = "FEATURES_PAYLOAD_TOO_LARGE";
readonly sizeBytes: number;
readonly limitBytes: number;
constructor(sizeBytes: number, limitBytes: number) {
super(
`Features payload too large: ${sizeBytes} bytes exceeds ${limitBytes} byte cap`,
);
this.name = "FeaturesPayloadTooLargeError";
this.sizeBytes = sizeBytes;
this.limitBytes = limitBytes;
}
}
/**
* v1.4.36 W3 T1 — bucket-window definitions for the
* `bucketedMeasurements` payload. Mirrors the rollup populator's
* granularity ladder so the read-side picks up whatever the persistent
* table holds without a recompute round-trip.
*
* The 90 / 365 / 1825-day windows are non-overlapping: each row of
* `measurement_rollups` lives at exactly one granularity, and the
* downstream model reads the union of the three series per type.
* Total volume per type for a Marc-scale power user (5 years of daily
* data): 90 DAY + 39 WEEK + 50 MONTH = 179 buckets × ~40 bytes JSON
* each ≈ 7 KB. Eight metric types lands at ~56 KB — well under the
* 5 MB cap above, vs 25.9 MB for the v1.4.35 rawMeasurements shape.
*/
const BUCKET_WINDOWS: Array<{
granularity: RollupGranularity;
fromDays: number;
toDays: number;
}> = [
{ granularity: "DAY", fromDays: 0, toDays: 90 },
{ granularity: "WEEK", fromDays: 90, toDays: 365 },
{ granularity: "MONTH", fromDays: 365, toDays: 1825 },
];
/**
* Types the bucketed payload covers. Mirrors the aggregate branches
* above so the model never sees a bucket for a metric whose aggregate
* block was suppressed. New `MeasurementType` enum values flow in by
* adding one row; the rollup populator already covers every type.
*/
const BUCKETED_TYPES: MeasurementType[] = [
"WEIGHT",
"BLOOD_PRESSURE_SYS",
"BLOOD_PRESSURE_DIA",
"PULSE",
"BODY_FAT",
"SLEEP_DURATION",
"ACTIVITY_STEPS",
];
function stdDev(values: number[]): number | null {
if (values.length < 2) return null;
const mean = values.reduce((s, v) => s + v, 0) / values.length;
@@ -764,20 +855,75 @@ export async function extractFeatures(
});
}
// Raw mode: add anonymized raw data points
// v1.4.36 W3 T1 — "raw" mode no longer dumps every measurement row
// into the prompt. Instead we attach DAY / WEEK / MONTH bucket means
// from `measurement_rollups` so the model gets the same temporal
// shape (granular near-term, coarser deep history) at O(buckets)
// instead of O(rows). The v1.4.35 shape blew past Codex's 10 MB
// string limit on power users (~25.9 MB observed); the bucketed
// shape lands at ~50–500 KB per account.
if (includeRaw) {
const bucketedMeasurements = await readBucketedSeries(userId, now);
const rawFeatures: RawFeatures = {
...features,
rawMeasurements: measurements.map((m) => ({
type: m.type,
value: m.value,
dayOffset: Math.round(
(now - m.measuredAt.getTime()) / (24 * 60 * 60 * 1000),
),
})),
bucketedMeasurements,
};
enforceSizeGuard(rawFeatures);
return rawFeatures;
}
enforceSizeGuard(features);
return features;
}
/**
* Read every BUCKETED_TYPES × BUCKET_WINDOWS combination from the
* persistent rollup table and project to the wire shape. Empty
* (type, granularity) combinations are dropped so the payload never
* carries a labelled-but-empty series.
*/
async function readBucketedSeries(
userId: string,
now: number,
): Promise<BucketedSeries[]> {
const series: BucketedSeries[] = [];
for (const type of BUCKETED_TYPES) {
for (const window of BUCKET_WINDOWS) {
const from = new Date(now - window.toDays * 24 * 60 * 60 * 1000);
const to = new Date(now - window.fromDays * 24 * 60 * 60 * 1000);
const rows = await readRollupBuckets(
userId,
type,
window.granularity,
from,
to,
);
if (rows.length === 0) continue;
series.push({
type,
granularity: window.granularity,
buckets: rows.map((r) => ({
bucketStart: r.bucketStart.toISOString(),
mean: Math.round(r.mean * 100) / 100,
count: r.count,
})),
});
}
}
return series;
}
/**
* Hard ceiling on the serialised payload. Throws
* `FeaturesPayloadTooLargeError` (tagged with `code` so the route
* handler can pattern-match) when the JSON dump crosses
* `FEATURES_MAX_BYTES`. The route handler downgrades to the non-raw
* shape and annotates so the regression is observable; the model
* call still succeeds.
*/
function enforceSizeGuard(payload: AggregatedFeatures | RawFeatures): void {
const sizeBytes = Buffer.byteLength(JSON.stringify(payload), "utf8");
if (sizeBytes > FEATURES_MAX_BYTES) {
throw new FeaturesPayloadTooLargeError(sizeBytes, FEATURES_MAX_BYTES);
}
}
+70
View File
@@ -0,0 +1,70 @@
/**
* v1.4.36 W3 T4 — `compactSections` helper for the AI prompt body.
*
* The features payload (and the Coach snapshot, transitively) carries
* conditionally-populated blocks: `medications`, `bucketedMeasurements`,
* `historicalComparison`, the per-metric aggregates, etc. Empty blocks
* have two failure modes once they hit the prompt:
*
* 1. The serialised JSON includes a labelled-empty key (`"sleep": []`
* or `"medications": []`) which the model occasionally narrates as
* "there are no medications in your data" even when the user
* explicitly excluded them — wasted prompt budget and a misleading
* framing.
* 2. Some downstream prose composers render the block label without
* checking row count, producing the v1.4.35 "Hier sind die
* Schlafdaten: [keine]" anti-pattern Marc flagged in W3.
*
* `compactSections` walks the object once and drops any property whose
* value is:
* - an empty array
* - an empty object (no own enumerable keys)
* - undefined
*
* It runs a single shallow pass at the top level — that's enough for
* the features + snapshot shapes today. We do NOT recurse into nested
* objects because the legitimate empty values inside e.g.
* `coverage: { count: 0, … }` should stay readable (count 0 IS the
* signal). A future call site that needs deep-compaction can opt in
* via the `deep` flag.
*/
type PlainObject = Record<string, unknown>;
function isPlainObject(value: unknown): value is PlainObject {
return (
typeof value === "object" &&
value !== null &&
!Array.isArray(value) &&
Object.getPrototypeOf(value) === Object.prototype
);
}
function isEmpty(value: unknown): boolean {
if (value === undefined) return true;
if (Array.isArray(value)) return value.length === 0;
if (isPlainObject(value)) return Object.keys(value).length === 0;
return false;
}
/**
* Return a shallow copy of `blocks` with every empty key dropped.
* `null` values are preserved — null carries the semantic "field
* exists but is unknown", which is different from "block doesn't
* exist". Set `deep: true` to recurse into nested plain objects.
*/
export function compactSections<T extends PlainObject>(
blocks: T,
options: { deep?: boolean } = {},
): T {
const out: PlainObject = {};
for (const [key, value] of Object.entries(blocks)) {
let folded = value;
if (options.deep && isPlainObject(value)) {
folded = compactSections(value, options);
}
if (isEmpty(folded)) continue;
out[key] = folded;
}
return out as T;
}
@@ -0,0 +1,107 @@
import { describe, expect, it } from "vitest";
import {
CUMULATIVE_DAY_SUM_TYPES,
isCumulativeDaySumType,
pickCumulativeDaySum,
} from "../cumulative-day-sum";
/**
* v1.4.36 W4c — pickCumulativeDaySum unit tests.
*
* The helper bucket-and-sums a series of cumulative samples per
* `dayKey(measuredAt)`. Tested in pure ISO-date space so the
* tz-aware caller (analytics route) can layer its own `userDayKey`
* on top without dragging a TZ runtime into the helper.
*/
// Use an ISO-date key so the test stays deterministic without a TZ.
const isoDay = (d: Date) => d.toISOString().slice(0, 10);
describe("pickCumulativeDaySum", () => {
it("returns an empty array when the input is empty", () => {
expect(pickCumulativeDaySum([], isoDay)).toEqual([]);
});
it("sums a single source within one day to one point", () => {
const result = pickCumulativeDaySum(
[
{ measuredAt: new Date("2026-05-17T08:00:00Z"), value: 1200 },
{ measuredAt: new Date("2026-05-17T12:30:00Z"), value: 2500 },
{ measuredAt: new Date("2026-05-17T19:45:00Z"), value: 4300 },
],
isoDay,
);
expect(result).toHaveLength(1);
expect(result[0].value).toBe(8000);
// The bucket's date is the latest measuredAt in that bucket.
expect(result[0].date.toISOString()).toBe("2026-05-17T19:45:00.000Z");
});
it("preserves source-priority ordering by ignoring it (caller's job)", () => {
// The helper does not look at `source`. Two sources collapsed in
// ONE bucket would double-count — the analytics route prevents
// that by running `pickCanonicalSourceRows` first. The helper's
// contract is: trust the input order, just bucket-and-sum.
const result = pickCumulativeDaySum(
[
{ measuredAt: new Date("2026-05-17T08:00:00Z"), value: 1000 },
{ measuredAt: new Date("2026-05-17T08:00:00Z"), value: 1500 },
],
isoDay,
);
expect(result).toHaveLength(1);
expect(result[0].value).toBe(2500);
});
it("buckets multiple days and only sums within each day", () => {
const result = pickCumulativeDaySum(
[
// Day 1 — total 5000
{ measuredAt: new Date("2026-05-15T08:00:00Z"), value: 2000 },
{ measuredAt: new Date("2026-05-15T20:00:00Z"), value: 3000 },
// Day 2 — total 1200
{ measuredAt: new Date("2026-05-16T09:00:00Z"), value: 700 },
{ measuredAt: new Date("2026-05-16T11:00:00Z"), value: 500 },
// Day 3 — total 9000 (today)
{ measuredAt: new Date("2026-05-17T07:00:00Z"), value: 4000 },
{ measuredAt: new Date("2026-05-17T13:00:00Z"), value: 5000 },
],
isoDay,
);
expect(result).toHaveLength(3);
expect(result.map((p) => p.value)).toEqual([5000, 1200, 9000]);
// Output is sorted ascending by date.
expect(result[0].date.toISOString()).toBe("2026-05-15T20:00:00.000Z");
expect(result[2].date.toISOString()).toBe("2026-05-17T13:00:00.000Z");
});
it("each bucket's date is the latest measuredAt in that bucket", () => {
const result = pickCumulativeDaySum(
[
{ measuredAt: new Date("2026-05-17T07:00:00Z"), value: 100 },
{ measuredAt: new Date("2026-05-17T15:00:00Z"), value: 200 },
{ measuredAt: new Date("2026-05-17T10:00:00Z"), value: 50 },
],
isoDay,
);
expect(result).toHaveLength(1);
expect(result[0].date.toISOString()).toBe("2026-05-17T15:00:00.000Z");
expect(result[0].value).toBe(350);
});
});
describe("isCumulativeDaySumType", () => {
it("returns true for every metric in the canonical list", () => {
for (const t of CUMULATIVE_DAY_SUM_TYPES) {
expect(isCumulativeDaySumType(t)).toBe(true);
}
});
it("returns false for non-cumulative types", () => {
expect(isCumulativeDaySumType("BLOOD_PRESSURE_SYSTOLIC")).toBe(false);
expect(isCumulativeDaySumType("WEIGHT")).toBe(false);
expect(isCumulativeDaySumType("SLEEP_DURATION")).toBe(false);
expect(isCumulativeDaySumType("MOOD")).toBe(false);
});
});
@@ -52,6 +52,15 @@ vi.mock("@/lib/jobs/boss-instance", () => ({
getGlobalBoss: () => mocks.getGlobalBossMock(),
}));
// v1.4.36 QA H3 — `ensureUserRollupsFresh` now annotates + console-
// errors on populator failures so silent regressions show up in ops.
// Mock the annotate boundary so the test can assert the meta payload.
vi.mock("@/lib/logging/context", () => ({
annotate: vi.fn(),
}));
import { annotate } from "@/lib/logging/context";
const {
queryRaw,
queryRawUnsafe,
@@ -281,6 +290,40 @@ describe("ensureUserRollupsFresh", () => {
const result = await ensureUserRollupsFresh("user-1");
expect(result.recomputed).toBe(false);
});
it("annotates the failure when the inner recompute throws (H3)", async () => {
// Stale watermark — rollup older than the newest measurement so
// the inner recompute branch fires.
const rollupAt = new Date("2026-05-10T10:00:00.000Z");
const measurementAt = new Date("2026-05-10T12:00:00.000Z");
findFirst.mockResolvedValueOnce({ computedAt: rollupAt });
findFirstMeasurement.mockResolvedValueOnce({
updatedAt: measurementAt,
measuredAt: measurementAt,
});
// The recompute aggregate query throws — simulates a populator
// regression (pool exhausted, deadlock, etc).
queryRawUnsafe.mockRejectedValueOnce(new Error("deadlock detected"));
const consoleSpy = vi.spyOn(console, "error").mockImplementation(() => {});
const result = await ensureUserRollupsFresh("user-1");
// The read path still gets a clean `{ recomputed: false }`.
expect(result.recomputed).toBe(false);
// The annotate event fires with the documented shape so ops can
// spot the silent populator regression in the wide-event pipeline.
expect(annotate).toHaveBeenCalledWith({
meta: {
rollup_refresh_failed: true,
rollup_refresh_error: "deadlock detected",
},
});
// And the console.error fallback fires so the worker log path
// surfaces the failure even when there's no request context.
expect(consoleSpy).toHaveBeenCalled();
consoleSpy.mockRestore();
});
});
describe("enqueueBootTimeRollupBackfill", () => {
@@ -0,0 +1,91 @@
/**
* v1.4.36 W4c — Pure day-bucket sum picker for cumulative metrics.
*
* SLEEP_DURATION already runs this shape inline in
* `src/app/api/analytics/route.ts` lines 198-225: pick a single
* source per day (source-priority + device-type) then sum the
* day's slices into one datapoint per day. The dashboard tile
* surfaces the resulting `latest` as "X minutes asleep".
*
* Steps / active energy / walking distance / flights climbed /
* time-in-daylight share the same physical shape — every iOS
* HealthKit ingest writes minute-level slices that the dashboard
* tile was rendering as the *latest slice* instead of the *day's
* total*. Marc reported "Steps tile shows last-measurement-not-day-sum".
*
* This helper extracts the per-day-sum reduction so the route can
* call it for every cumulative metric without copy-pasting the
* SLEEP_DURATION branch. The source-priority pick stays in the
* caller (it already imports `pickCanonicalSourceRows`); this
* helper only handles the bucket-and-sum once that picker has
* narrowed each day to its canonical source.
*/
export interface CumulativeRow {
measuredAt: Date;
value: number;
}
export interface DaySumPoint {
date: Date;
value: number;
}
/**
* Bucket `rows` by `dayKey(measuredAt)` and sum each bucket's `value`.
* The returned points are sorted ascending by date, with each
* bucket's `date` set to the latest `measuredAt` seen in that
* bucket (matches the SLEEP_DURATION branch's contract).
*
* Empty input returns an empty array.
*/
export function pickCumulativeDaySum<T extends CumulativeRow>(
rows: readonly T[],
dayKey: (d: Date) => string,
): DaySumPoint[] {
if (rows.length === 0) return [];
const byDay = new Map<string, { total: number; date: Date }>();
for (const row of rows) {
const key = dayKey(row.measuredAt);
const slot = byDay.get(key) ?? { total: 0, date: row.measuredAt };
slot.total += row.value;
if (row.measuredAt > slot.date) slot.date = row.measuredAt;
byDay.set(key, slot);
}
const out: DaySumPoint[] = Array.from(byDay.values()).map((s) => ({
date: s.date,
value: s.total,
}));
out.sort((a, b) => a.date.getTime() - b.date.getTime());
return out;
}
/**
* v1.4.36 W4c — Canonical set of cumulative metric types whose
* dashboard tile should read the *day's sum* and not the latest
* slice. Kept here so the analytics route and any other consumer
* (Coach snapshot, exports) share the same source of truth.
*
* Note: SLEEP_DURATION is also cumulative but the route handles
* it inline because the picker is the canonical sleep source ladder
* ("sleep" metricKey vs "default"). Once Apple-Health passthrough
* lands and the sleep ladder needs the same shared treatment it can
* fold into this list.
*/
export const CUMULATIVE_DAY_SUM_TYPES = [
"ACTIVITY_STEPS",
"ACTIVE_ENERGY_BURNED",
"WALKING_RUNNING_DISTANCE",
"FLIGHTS_CLIMBED",
"TIME_IN_DAYLIGHT",
] as const;
export type CumulativeDaySumType = (typeof CUMULATIVE_DAY_SUM_TYPES)[number];
export function isCumulativeDaySumType(
type: string,
): type is CumulativeDaySumType {
return (CUMULATIVE_DAY_SUM_TYPES as readonly string[]).includes(type);
}
+96
View File
@@ -0,0 +1,96 @@
/**
* v1.4.36 — per-type rollup coverage probe.
*
* The v1.4.35/v1.4.36 read-swap gates on whether the DAY rollup table
* carries buckets for a user. The first iteration checked a single
* `COUNT(*) > 0` against `measurement_rollups`. That returns `true` as
* soon as *any* type has buckets, which breaks the "first measurement
* for a brand-new type" case: the per-write hook upserts one DAY bucket
* for the new type → the global probe stays >0 → the read path picks
* the bucket-derived branch for *every* type → the brand-new type's
* narrow-aggregate windowed columns come back fine but the seed loop
* only iterates bucket-bearing types, so a type that has live
* measurements but no buckets falls out of the response entirely.
*
* The probe below joins `measurements`-distinct-types against
* `measurement_rollups` so the caller learns, per type, whether the
* bucket table covers it. Types with `hasBuckets=false` fall back to
* the live aggregator branch; types with `hasBuckets=true` ride the
* cheap composed branch. The result is a single round-trip indexed
* read regardless of how many types the user has logged.
*/
import { prisma } from "@/lib/db";
/**
* Per-type coverage map. `true` means at least one DAY rollup row
* exists for this `(user, type)` pair; the caller can safely compose
* `count / min / max / mean` from the bucket table for this type.
* `false` means the bucket table is empty for this type and the caller
* must fall back to the live aggregate.
*
* Types the user has never logged are absent from the map — callers
* never need to ask about a type with zero measurements.
*/
export type RollupCoverageMap = Map<string, boolean>;
/**
* Probe DAY-bucket coverage for every type the user has measurements
* for. The join is anchored on the smaller `DISTINCT type FROM
* measurements` set so the planner picks the per-type
* `(user_id, type, measured_at)` index path; the LEFT JOIN onto the
* rollup table uses the `(user_id, type, granularity, bucket_start)`
* composite primary key. A `COUNT > 0` per partition keeps the result
* shape stable even when a type has zero buckets.
*/
export async function probeRollupCoverage(
userId: string,
): Promise<RollupCoverageMap> {
const rows = await prisma.$queryRaw<
Array<{ type: string; has_buckets: boolean }>
>`
SELECT
m."type"::text AS type,
COUNT(r.*) > 0 AS has_buckets
FROM (
SELECT DISTINCT "type"
FROM measurements
WHERE user_id = ${userId}
) m
LEFT JOIN measurement_rollups r
ON r.user_id = ${userId}
AND r."type" = m."type"
AND r.granularity = 'DAY'
GROUP BY m."type"
`;
const coverage: RollupCoverageMap = new Map();
for (const row of rows) {
coverage.set(row.type, Boolean(row.has_buckets));
}
return coverage;
}
/**
* Returns the set of types the user has logged that do NOT yet have
* DAY-rollup coverage. The bucket-fresh read paths use this to decide
* which types still need the live aggregator branch.
*/
export function typesMissingCoverage(coverage: RollupCoverageMap): string[] {
const missing: string[] = [];
for (const [type, hasBuckets] of coverage.entries()) {
if (!hasBuckets) missing.push(type);
}
return missing;
}
/**
* Convenience — `true` when the user has at least one measurement and
* every type with measurements also has DAY-bucket coverage. The read
* path can skip the live aggregate entirely.
*/
export function isFullyCovered(coverage: RollupCoverageMap): boolean {
if (coverage.size === 0) return false;
for (const hasBuckets of coverage.values()) {
if (!hasBuckets) return false;
}
return true;
}
+15 -1
View File
@@ -30,6 +30,7 @@
*/
import { prisma } from "@/lib/db";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { annotate } from "@/lib/logging/context";
import type {
MeasurementType,
RollupGranularity,
@@ -616,8 +617,21 @@ export async function ensureUserRollupsFresh(
to: new Date(),
});
return { recomputed: true };
} catch {
} catch (err) {
// Best-effort — never block the read path on a populator failure.
// v1.4.36 QA H3 — surface the failure so ops can spot a silent
// populator regression. `annotate` is a no-op when there's no
// request context (worker calls, tests); `console.error` carries
// the same shape through the worker log pipeline so the regression
// is visible regardless of caller surface.
const message = err instanceof Error ? err.message : String(err);
annotate({
meta: {
rollup_refresh_failed: true,
rollup_refresh_error: message,
},
});
console.error("[rollups] ensureUserRollupsFresh failed:", message);
return { recomputed: false };
}
}
@@ -56,7 +56,9 @@ describe("coachPrefsSchema", () => {
expect(result.success).toBe(false);
});
it("caps excludeMetrics at 9 entries", () => {
it("caps excludeMetrics at 11 entries", () => {
// v1.4.36 W3 T2 — cap raised from 9 to 11 to admit the two new
// optional-context toggles (medications, anthropometrics).
const result = coachPrefsSchema.safeParse({
excludeMetrics: [
"bp",
@@ -68,11 +70,20 @@ describe("coachPrefsSchema", () => {
"sleep",
"resting_hr",
"steps",
"medications",
"anthropometrics",
"bp",
],
});
expect(result.success).toBe(false);
});
it("accepts the new medications + anthropometrics tokens", () => {
const out = coachPrefsSchema.parse({
excludeMetrics: ["medications", "anthropometrics"],
});
expect(out.excludeMetrics).toEqual(["medications", "anthropometrics"]);
});
});
describe("parseCoachPrefs", () => {
+9 -1
View File
@@ -55,6 +55,14 @@ export const coachExcludeMetricEnum = z.enum([
"sleep",
"resting_hr",
"steps",
// v1.4.36 W3 T2 — optional context blocks the user can opt out of.
// `medications` covers the snapshot's compliance + GLP-1 weeklyContext;
// `anthropometrics` covers height / age / gender on `context`. Each
// gates a single labelled block at the snapshot/feature layer, so an
// excluded block never lands in the prompt at all (not even as a
// labelled-empty key).
"medications",
"anthropometrics",
]);
export type CoachExcludeMetric = z.infer<typeof coachExcludeMetricEnum>;
@@ -83,7 +91,7 @@ export type CoachDefaultWindow = z.infer<typeof coachDefaultWindowEnum>;
export const coachPrefsSchema = z.object({
tone: coachToneEnum.default("warm"),
verbosity: coachVerbosityEnum.default("default"),
excludeMetrics: z.array(coachExcludeMetricEnum).max(9).default([]),
excludeMetrics: z.array(coachExcludeMetricEnum).max(11).default([]),
showEvidenceByDefault: z.boolean().default(false),
defaultWindow: coachDefaultWindowEnum.default("allTime"),
});
+10
View File
@@ -262,6 +262,16 @@ export const listMeasurementsSchema = z.object({
// Omitting `aggregate` keeps the raw wire shape (iOS contract); the
// chart-data client must opt in explicitly.
aggregate: z.enum(["raw", "daily", "weekly", "monthly"]).optional(),
// v1.4.36 W1 — opt-in source switch for daily-aggregate reads. When
// `source=rollup` + `aggregate=daily`, the route reads from the
// persistent `measurement_rollups` DAY buckets instead of running a
// live `date_trunc` GROUP BY scan over the raw `measurements` table.
// The chart-data client opts in for the trends-row strip + every
// sub-page chart so the three parallel daily-aggregate requests stop
// burning a full table scan each. The route falls back to live SQL
// when the rollup bucket set is empty for the requested window so
// brand-new accounts still see correct data on their first chart.
source: z.enum(["rollup"]).optional(),
});
export const createBatchMeasurementSchema = z.object({