Skip to content

fix(insights): restrict the scoped feature bulk read to the requested blocks' types - #1399

Merged
MBombeck merged 5 commits into
MBombeck:mainfrom
mailuminatti:fix/scoped-feature-read
Nov 16, 2026
Merged

MBombeck merged 5 commits into
MBombeck:mainfrom
mailuminatti:fix/scoped-feature-read

Conversation

@mailuminatti

Copy link
Copy Markdown
Contributor

Fixes #1398.

Problem

get_metric_series (and the single-metric Coach read behind it) returned { present: false, reason: "unavailable_in_scope" } for sparse metrics on an account with a dense PULSE stream, on every window. The scoped snapshot builds its section via extractFeatures(userId, { only }), whose bulk measurement read is newest-first across all types with take: FEATURE_MEASUREMENT_ROW_CAP (6,000). On the reporting account (~824,000 PULSE rows in 30 days) the newest 6,000 rows span only a few hours, so byType("WEIGHT") saw zero rows, no weight block was built, and the availability probe — which uses uncapped aggregates and sees the data — classified the miss as unavailable_in_scope.

Change

When extractFeatures is called with only, the bulk read now also filters on the requested blocks' own measurement types (SCOPED_BLOCK_TYPES, via the already-computed allTimeTypes), under the same newest-first cap. The cap's purpose is unchanged — it still bounds the read at 6,000 rows — but the rows are the block's own, so a dense type can no longer starve a sparse one out of a scoped read. Unscoped reads (no only) are byte-for-byte the same query as before. The two comments that described the all-type behaviour are updated to match.

Testing

Deployed on a self-hosted v1.47.1 instance carrying this change (the affected code is identical between v1.47.1 and current main):

  • Before: get_metric_series for weight returned unavailable_in_scope for last7days, last30days, last90days, lastYear and allTime.
  • After: all windows return present: true (server-side durations 0.4–1.2 s), with aggregates matching the availability probe's figures (351 readings, 2021-01-23 to 2026-11-11).
  • get_metric_series for pulse and get_medication_schedule verified unchanged and working.

…cannot starve sparse ones

The bulk measurement reads behind the insight features, the Coach snapshot
timelines, the mood cross-metric pairing, the correlation windows and the
weight and blood pressure status cards read newest-first across all their
types under a single row cap. On an account with a wearable heart rate
stream the cap filled within hours, and every weight, blood pressure or
other sparse reading fell out of the read, so blocks reported no data for
metrics with years of it.

All of them now go through one read that descends the live partial index
once per type, each type stopping at its own cap. The feature extraction
and the Coach snapshot cap each type at 6000, so a block built from one
type's read equals the block the full read builds (worst case 72,000 rows
for the twelve feature types). The mood, correlation and status reads keep
their old totals and share them fairly: each type keeps at least its share,
a sparse one all of its window.

The feature context's data span now comes from index probes over every
type in the window rather than from the capped rows, and its measurement
count is the readings the blocks were built from.

Refs MBombeck#1398
@MBombeck

Copy link
Copy Markdown
Owner

@mailuminatti Thanks again for this, the diagnosis was spot on. I pushed one commit on top of your branch: the full build and the Coach snapshot used the same all-type cap, so on an account like yours they missed weight and blood pressure too. The bulk reads now cap per type, which also keeps the scoped block identical to the full build's, as the comments promise. Your commits stay as they are. There's an integration test with a dense pulse stream and sparse weight and BP now. I'll merge once CI is green.

@MBombeck
MBombeck merged commit d32036b into MBombeck:main Nov 16, 2026
31 checks passed
@MBombeck MBombeck mentioned this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_metric_series returns unavailable_in_scope for sparse metrics when a dense PULSE stream exhausts the 6,000-row bulk-read cap

2 participants