fix(insights): hand the briefing its figures in the reader's units

The daily briefing serialised the aggregated feature set as computed:
weight, grip strength and the weight target in kilograms, waist in
centimetres, glucose and the signals of the day in mg/dL (the glucose
signal carried no unit at all), workout distances in kilometres. A
reader on pounds or mmol/L got a briefing in units they do not use.

One step now converts the feature set after all canonical arithmetic,
on both the nightly and the on-demand path: absolute values through the
full transform, differences and slopes through the factor alone, every
converted block labelled, workout distances under unit-neutral keys.
The prompt, the stored fingerprint and the grounding check all read the
converted set, so a unit switch moves the fingerprint and the grounding
check compares the reply against the figures the model was shown. The
weight target sentence, the comparison block and the GLP-1 plateau
context quote weight in the reader's unit; the comparison block also
labelled sleep as hours while carrying minutes.

A unit switch now also re-warms a cached briefing alongside the status
notes and narratives.

Refs #1067
This commit is contained in:
Marc-André Bombeck committed 2026-10-01 13:23:17 +02:00
1 parent d1b2539b1f
commit a7e6b89672
15 files changed
+794 -21

No files matched your search

@@ -111,3 +111,30 @@ describe("buildWeightTargetFeature", () => {
).toBeNull();
});
});
describe("buildWeightTargetFeature — the reader's mass unit", () => {
it("quotes the band in pounds for an imperial reader and judges it in kilograms", () => {
const feature = buildWeightTargetFeature(
{ min: 65, max: 70 },
{ avg7: 66, latest: 66 },
"imperial",
);
expect(feature).toMatchObject({
min: 143.3,
max: 154.3,
unit: "lb",
position: "inside",
});
expect(feature?.reading).toContain("143.3–154.3 lb");
expect(feature?.reading).not.toContain("kg");
});
it("keeps kilograms for a metric reader", () => {
const feature = buildWeightTargetFeature(
{ min: 65, max: 70 },
{ avg7: 72, latest: 72 },
);
expect(feature).toMatchObject({ min: 65, max: 70, unit: "kg" });
expect(feature?.reading).toContain("65–70 kg");
});
});
+24 -5
View File
@@ -28,6 +28,12 @@
* Pure and client-safe: no DB read, no clock.
*/
import type { TrendDirectionSentiment } from "@/lib/insights/trend-sentiment";
import {
applyDisplayTransform,
DEFAULT_UNIT_PREFERENCE,
getDisplayTransform,
type UnitPreference,
} from "@/lib/measurements/display-transform";
/** Where a weight sits against the person's own target band. */
export type WeightTargetPosition = "below" | "inside" | "above";
@@ -99,8 +105,12 @@ export function resolveWeightTrend(
* colour, the prose makes no promise about which way is better).
*/
export interface WeightTargetFeature {
minKg: number;
maxKg: number;
/** The band's lower edge, in `unit`. */
min: number;
/** The band's upper edge, in `unit`. */
max: number;
/** The reader's mass unit; the band and the reading sentence are in it. */
unit: string;
position: WeightTargetPosition;
progress: "gaining" | "losing" | "holding steady";
reading: string;
@@ -118,13 +128,21 @@ const PROGRESS_BY_POSITION: Record<
export function buildWeightTargetFeature(
target: { min: number; max: number } | null,
summary: { avg7: number | null; latest: number | null } | null | undefined,
/**
* The reader's metric/imperial choice. The position is judged on the
* canonical kilograms either way; only the band the text quotes converts.
*/
system: UnitPreference = DEFAULT_UNIT_PREFERENCE,
): WeightTargetFeature | null {
const position = weightTargetPosition(
target,
weightTrendReferenceKg(summary),
);
if (!target || !position) return null;
const band = `${target.min}–${target.max} kg`;
const transform = getDisplayTransform("WEIGHT", system);
const min = applyDisplayTransform(target.min, transform);
const max = applyDisplayTransform(target.max, transform);
const band = `${min}–${max} ${transform.displayUnit}`;
const reading =
position === "below"
? `The person set their own weight target at ${band} and is currently below it, so gaining weight is progress and losing weight moves away from the goal. Judge weight changes against this target; never describe a gain as a setback or a regression.`
@@ -132,8 +150,9 @@ export function buildWeightTargetFeature(
? `The person set their own weight target at ${band} and is currently above it, so losing weight is progress and gaining moves away from the goal.`
: `The person set their own weight target at ${band} and is currently inside it, so holding steady is progress; a small move either way within the band is not a setback.`;
return {
minKg: target.min,
maxKg: target.max,
min,
max,
unit: transform.displayUnit,
position,
progress: PROGRESS_BY_POSITION[position],
reading,