Repository navigation
feat(body): add measurement decision context #2417
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 9 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
04518eb
fix(ci): mask Infisical secret values
Asherlc 4d2b769
feat(body): add measurement decision context
Asherlc f9709bb
fix(body): address review blockers
Asherlc 390d717
fix(server): harden body decision context
Asherlc 2ea1ddd
test(server): consolidate mobile dashboard suites
Asherlc 14c6eb2
test(server): reconcile mainline contracts
Asherlc dfbf904
fix(ci): resolve body context checks
Asherlc c8fd9b6
fix(ci): align body context fixtures
Asherlc e9e22eb
docs(body): clarify residual threshold
Asherlc 639a9eb
fix(contract): validate decision context dates
Asherlc ebd6582
test(contract): cover nullable decision context
Asherlc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,6 +273,9 @@ | |
| "acwr", | ||
| "drhr", | ||
| "ewma", | ||
| "Tukey", | ||
| "tukey", | ||
| "interquartile", | ||
| "trimp", | ||
| "trimps", | ||
| "tdee", | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Body metrics decision context | ||
|
|
||
| The Body page and mobile Recovery tab show the server-authored context needed to | ||
| interpret Trend Weight and recent scale readings. The context is derived from | ||
| the existing ClickHouse body-measurement view; it does not create a second | ||
| source of body data. | ||
|
|
||
| ## Measurement provenance | ||
|
|
||
| The latest positive body measurement selected for the user’s local calendar day | ||
| is shown with its provider, optional device/source name, weight, and recorded | ||
| clock time in the configured user timezone. The display encourages comparable | ||
| measurements: [NHS England recommends recording weight at the same time of day | ||
| and using the same scales](https://www.england.nhs.uk/long-read/how-to-record-your-weight/). | ||
|
|
||
| ## Trend Weight contract | ||
|
|
||
| Trend Weight uses the existing server-side EWMA with alpha `0.1`: | ||
|
|
||
| - each selected daily scale reading moves the trend 10% toward that reading; | ||
| - missing days between readings are linearly interpolated; | ||
| - non-positive weights are excluded from the trend input; | ||
| - readings are not removed as outliers. | ||
|
|
||
| The exact contract is returned with the body analytics response so web and | ||
| mobile render the same explanation. | ||
|
|
||
| ## Personalized typical measurement variation | ||
|
|
||
| The server computes scale-to-Trend-Weight residuals for the latest 30 selected | ||
| daily readings. It reports an informational residual band after at least eight | ||
| readings using Tukey inner fences (`Q1 - 1.5 × IQR` and `Q3 + 1.5 × IQR`). The | ||
| band is not a clinical threshold, and outlying readings remain included in the | ||
| underlying data. This follows the standard boxplot inner-fence definition | ||
| described by [NIST’s outlier guidance](https://www.itl.nist.gov/div898/handbook/prc/section1/prc16.htm). | ||
|
|
||
| When fewer than eight residual observations are available, the response explicitly | ||
| returns an insufficient-data state instead of inventing a range. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { | ||
| BODY_SOURCE_GUIDANCE, | ||
| BODY_TREND_WEIGHT_DECISION_COPY, | ||
| formatBodyDecisionProvenance, | ||
| formatBodyDecisionVariation, | ||
| formatSignedBodyResidual, | ||
| } from "./body-decision-context.ts"; | ||
|
|
||
| const formatWeight = (value: number) => `${value.toFixed(1)} kg`; | ||
|
|
||
| describe("body decision context formatting", () => { | ||
| it("returns no provenance when there is no latest measurement", () => { | ||
| expect( | ||
| formatBodyDecisionProvenance(null, { | ||
| formatWeight, | ||
| providerLabel: (providerId) => providerId, | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| it("uses the same provenance copy for the measurement source and local time", () => { | ||
| expect( | ||
| formatBodyDecisionProvenance( | ||
| { | ||
| recordedAtLocal: "2026-07-25 08:00:00", | ||
| weightKg: 80, | ||
| providerId: "withings", | ||
| sourceName: "Body+", | ||
| }, | ||
| { | ||
| formatWeight, | ||
| providerLabel: (providerId) => (providerId === "withings" ? "Withings" : providerId), | ||
| }, | ||
| ), | ||
| ).toBe( | ||
| "Latest scale reading: 80.0 kg · Provider: Withings · Source: Body+ · Recorded: 2026-07-25 08:00:00", | ||
| ); | ||
| }); | ||
|
|
||
| it("omits provenance copy when the source name is absent", () => { | ||
| expect( | ||
| formatBodyDecisionProvenance( | ||
| { | ||
| recordedAtLocal: "2026-07-25 08:00:00", | ||
| weightKg: 80, | ||
| providerId: "withings", | ||
| sourceName: null, | ||
| }, | ||
| { | ||
| formatWeight, | ||
| providerLabel: () => "Withings", | ||
| }, | ||
| ), | ||
| ).toBe("Latest scale reading: 80.0 kg · Provider: Withings · Recorded: 2026-07-25 08:00:00"); | ||
| }); | ||
|
|
||
| it("formats positive, negative, and zero residuals with absolute magnitudes", () => { | ||
| expect(formatSignedBodyResidual(0.4, formatWeight)).toBe("+0.4 kg"); | ||
| expect(formatSignedBodyResidual(-0.4, formatWeight)).toBe("-0.4 kg"); | ||
| expect(formatSignedBodyResidual(0, formatWeight)).toBe("0.0 kg"); | ||
| }); | ||
|
|
||
| it("describes available variation as an informational residual band", () => { | ||
| expect( | ||
| formatBodyDecisionVariation( | ||
| { | ||
| status: "available", | ||
| observations: 12, | ||
| minimumObservations: 8, | ||
| maximumObservations: 30, | ||
| lowerResidualKg: -0.4, | ||
| upperResidualKg: 0.6, | ||
| }, | ||
| (value) => formatSignedBodyResidual(value, formatWeight), | ||
| ), | ||
| ).toBe( | ||
| "Personalized typical measurement variation is -0.4 kg to +0.6 kg around Trend Weight, based on 12 of the latest 30 actual scale readings. This is informational, not a clinical threshold; outliers remain included.", | ||
| ); | ||
| }); | ||
|
|
||
| it("states when variation needs more readings", () => { | ||
| expect( | ||
| formatBodyDecisionVariation( | ||
| { | ||
| status: "insufficient_data", | ||
| observations: 7, | ||
| minimumObservations: 8, | ||
| maximumObservations: 30, | ||
| lowerResidualKg: null, | ||
| upperResidualKg: null, | ||
| }, | ||
| (value) => formatSignedBodyResidual(value, formatWeight), | ||
| ), | ||
| ).toBe( | ||
| "Personalized typical measurement variation is unavailable until at least 8 actual scale readings are available.", | ||
| ); | ||
| }); | ||
|
|
||
| it("treats either missing residual bound as unavailable", () => { | ||
| const formatResidual = (value: number) => formatSignedBodyResidual(value, formatWeight); | ||
| const baseVariation = { | ||
| status: "available" as const, | ||
| observations: 12, | ||
| minimumObservations: 8, | ||
| maximumObservations: 30, | ||
| }; | ||
|
|
||
| expect( | ||
| formatBodyDecisionVariation( | ||
| { ...baseVariation, lowerResidualKg: null, upperResidualKg: 0.6 }, | ||
| formatResidual, | ||
| ), | ||
| ).toContain("unavailable until at least 8 actual scale readings are available"); | ||
| expect( | ||
| formatBodyDecisionVariation( | ||
| { ...baseVariation, lowerResidualKg: -0.4, upperResidualKg: null }, | ||
| formatResidual, | ||
| ), | ||
| ).toContain("unavailable until at least 8 actual scale readings are available"); | ||
| }); | ||
|
|
||
| it("treats a non-available status as unavailable even when bounds are present", () => { | ||
| expect( | ||
| formatBodyDecisionVariation( | ||
| { | ||
| status: "insufficient_data", | ||
| observations: 8, | ||
| minimumObservations: 8, | ||
| maximumObservations: 30, | ||
| lowerResidualKg: -0.4, | ||
| upperResidualKg: 0.6, | ||
| }, | ||
| (value) => formatSignedBodyResidual(value, formatWeight), | ||
| ), | ||
| ).toBe( | ||
| "Personalized typical measurement variation is unavailable until at least 8 actual scale readings are available.", | ||
| ); | ||
| }); | ||
|
|
||
| it("keeps the Trend Weight method and source guidance as shared copy", () => { | ||
| expect(BODY_TREND_WEIGHT_DECISION_COPY).toContain("Missing days are linearly interpolated"); | ||
| expect(BODY_SOURCE_GUIDANCE).toBe( | ||
| "For comparable readings, use the same scale at a consistent time of day.", | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| export const BODY_TREND_WEIGHT_DECISION_COPY = | ||
| "Trend Weight moves 10% toward each selected daily scale reading. Missing days are linearly interpolated; non-positive values are excluded and outliers remain included."; | ||
|
|
||
| export const BODY_SOURCE_GUIDANCE = | ||
| "For comparable readings, use the same scale at a consistent time of day."; | ||
|
|
||
| export const BODY_DECISION_CONTEXT_UNAVAILABLE = | ||
| "Measurement decision context is temporarily unavailable. Refresh to try again."; | ||
|
|
||
| interface BodyDecisionLatestMeasurement { | ||
| recordedAtLocal: string; | ||
| weightKg: number; | ||
| providerId: string; | ||
| sourceName: string | null; | ||
| } | ||
|
|
||
| interface BodyDecisionVariation { | ||
| status: "available" | "insufficient_data"; | ||
| observations: number; | ||
| minimumObservations: number; | ||
| maximumObservations: number; | ||
| lowerResidualKg: number | null; | ||
| upperResidualKg: number | null; | ||
| } | ||
|
|
||
| export interface BodyDecisionContextView { | ||
| latestMeasurement: BodyDecisionLatestMeasurement | null; | ||
| variation: BodyDecisionVariation; | ||
| } | ||
|
|
||
| export function formatBodyDecisionProvenance( | ||
| measurement: BodyDecisionLatestMeasurement | null, | ||
| options: { | ||
| formatWeight: (weightKg: number) => string; | ||
| providerLabel: (providerId: string) => string; | ||
| }, | ||
| ): string | null { | ||
| if (measurement == null) return null; | ||
|
|
||
| const source = measurement.sourceName != null ? `Source: ${measurement.sourceName}` : null; | ||
| return [ | ||
| `Latest scale reading: ${options.formatWeight(measurement.weightKg)}`, | ||
| `Provider: ${options.providerLabel(measurement.providerId)}`, | ||
| source, | ||
| `Recorded: ${measurement.recordedAtLocal}`, | ||
| ] | ||
| .filter((part): part is string => part != null) | ||
| .join(" · "); | ||
| } | ||
|
|
||
| export function formatSignedBodyResidual( | ||
| residualKg: number, | ||
| formatWeight: (absoluteWeightKg: number) => string, | ||
| ): string { | ||
| const magnitude = formatWeight(Math.abs(residualKg)); | ||
| if (residualKg > 0) return `+${magnitude}`; | ||
| if (residualKg < 0) return `-${magnitude}`; | ||
| return magnitude; | ||
| } | ||
|
|
||
| export function formatBodyDecisionVariation( | ||
| variation: BodyDecisionVariation, | ||
| formatResidual: (residualKg: number) => string, | ||
| ): string { | ||
| if ( | ||
| variation.status !== "available" || | ||
| variation.lowerResidualKg == null || | ||
| variation.upperResidualKg == null | ||
| ) { | ||
| return `Personalized typical measurement variation is unavailable until at least ${variation.minimumObservations} actual scale readings are available.`; | ||
| } | ||
|
|
||
| return `Personalized typical measurement variation is ${formatResidual(variation.lowerResidualKg)} to ${formatResidual(variation.upperResidualKg)} around Trend Weight, based on ${variation.observations} of the latest ${variation.maximumObservations} actual scale readings. This is informational, not a clinical threshold; outliers remain included.`; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.