From 4d87643a0fc53ddbedf6d5d13e675f89ea420d50 Mon Sep 17 00:00:00 2001 From: mailuminatti <46273278+mailuminatti@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:39:56 -0500 Subject: [PATCH 1/3] fix(insights): update scoped feature read comment for type-restricted bulk read --- src/lib/insights/features.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib/insights/features.ts b/src/lib/insights/features.ts index 6c360850e..9c6a7e8b7 100644 --- a/src/lib/insights/features.ts +++ b/src/lib/insights/features.ts @@ -1072,7 +1072,8 @@ export async function extractFeatures( /** * Build only these blocks (plus `context`) and return. Each block is * identical to the one the full extraction builds: the bulk read keeps its - * all-type newest-first cap, so the rows a block sees do not change. Used + * newest-first cap, restricted to the requested blocks' own measurement + * types, so a dense type cannot starve a sparse one out of the read. Used * by the single-metric Coach read, which reads one block and nothing else. */ only?: ReadonlySet; From dc947a5c2bb3307cf6774806c0088ff5aa8a1d32 Mon Sep 17 00:00:00 2001 From: mailuminatti <46273278+mailuminatti@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:42:41 -0500 Subject: [PATCH 2/3] fix(insights): restrict scoped feature bulk read to requested blocks' measurement types --- src/lib/insights/features.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/lib/insights/features.ts b/src/lib/insights/features.ts index 9c6a7e8b7..0f83674a3 100644 --- a/src/lib/insights/features.ts +++ b/src/lib/insights/features.ts @@ -1127,13 +1127,27 @@ export async function extractFeatures( // cap is safe even unbounded: the genuine all-time extremes are sourced // separately via `readAllTimeExtremes` on the bounded branch, so capping only // sheds the deepest rows of the bulk read and never relabels an extreme. + // A scoped read (`only`) builds its blocks from their own measurement + // types, so its bulk read is restricted to those types. Under the + // all-type cap a dense type fills the whole cap within hours — PULSE + // from a wearable is a 200k-row-class type — and starves every sparse + // type out of the read, so the scoped block then reports no data for a + // metric that demonstrably exists. + const scopedTypes = only ? [...allTimeTypes] : []; + const typeFilter = + scopedTypes.length > 0 ? { type: { in: scopedTypes } } : {}; const measurements = !needsMeasurementRows ? [] : await prisma.measurement .findMany({ where: sinceCutoff - ? { userId, measuredAt: { gte: sinceCutoff }, deletedAt: null } - : { userId, deletedAt: null }, + ? { + userId, + measuredAt: { gte: sinceCutoff }, + deletedAt: null, + ...typeFilter, + } + : { userId, deletedAt: null, ...typeFilter }, orderBy: { measuredAt: "desc" }, take: FEATURE_MEASUREMENT_ROW_CAP, // Project only the columns every downstream consumer reads (`byType`, From 383500b5fa59b12bba8e0d2b3efbb149cc149f14 Mon Sep 17 00:00:00 2001 From: mailuminatti <46273278+mailuminatti@users.noreply.github.com> Date: Sun, 15 Nov 2026 19:44:38 -0500 Subject: [PATCH 3/3] fix(insights): update Coach source snapshot comment for type-restricted scoped read --- src/lib/ai/coach/source-snapshot.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib/ai/coach/source-snapshot.ts b/src/lib/ai/coach/source-snapshot.ts index ce9ae0bda..40890dcdc 100644 --- a/src/lib/ai/coach/source-snapshot.ts +++ b/src/lib/ai/coach/source-snapshot.ts @@ -14,9 +14,9 @@ * source's own reads only, and hands back the same section and the same * reference grounding. Each block reads exactly what the full build reads for * it: the core clinical aggregates come from `extractFeatures` scoped to the - * one block (whose bulk read keeps its all-type cap, so the rows a block sees - * are unchanged), the timelines from the same windowed read, the coarse tail - * from the same rollup reader. + * one block (whose bulk read is restricted to that block's own measurement + * types under the same newest-first cap), the timelines from the same + * windowed read, the coarse tail from the same rollup reader. * * Two cases go to the full builder instead: *