chore(release): v1.4.38

Robustness sweep, perf hotspots, full localization, coach gate
cascade closure.

- /api/dashboard/summary cold-mount 4.6 s → ~500 ms via DAY-bucket
  sparkline + 60 s response cache.
- Coach API gate inventory test surfaced and closed two orphan
  routes (chat[id] GET+DELETE, chat-message feedback POST).
- Cross-tz fast-path runtime guard falls back to live for users
  more than ±3 h from UTC; proper bucket minting stays v1.5.
- es / fr / it / pl coverage 27 % → 63 % (~3 400 new strings) plus
  three medication-cluster placeholder restorations.
- IANA timezone validation closes the User.timezone self-DoS
  regression the new dashboard SQL would have surfaced.
- Unit suite 4520 → 4524.

See CHANGELOG.md for the full set.
This commit is contained in:
Marc-André Bombeck committed 2026-05-17 19:07:03 +02:00
1 parent fe64f66c7f
commit a550031a84
66 files changed
+7910 -3717

No files matched your search

+34
View File
@@ -101,6 +101,40 @@ describe("getClientIp trusted-proxy semantics (V3 audit)", () => {
expect(ip).toBe("5.6.7.8");
});
// v1.4.38 — looksLikeIp now defers to node:net's `isIP` instead of
// a hex/dot character-class regex. Structurally invalid candidates
// (`:::`, `1.2`, `gg:hh::1`) used to slip through the regex and
// pollute the rate-limit / audit log with non-IP strings; they are
// now rejected at the helper.
it("rejects structurally invalid IPv4 candidates (1.2)", () => {
const ip = getClientIp(
makeRequest({ "x-forwarded-for": "1.2, 5.6.7.8" }),
);
expect(ip).toBe("5.6.7.8");
});
it("rejects structurally invalid IPv6 candidates (:::)", () => {
const ip = getClientIp(
makeRequest({ "x-forwarded-for": ":::, 5.6.7.8" }),
);
expect(ip).toBe("5.6.7.8");
});
it("rejects hex-but-not-IPv6 chains", () => {
const ip = getClientIp(
makeRequest({ "x-forwarded-for": "gg:hh::1, 5.6.7.8" }),
);
expect(ip).toBe("5.6.7.8");
});
it("accepts well-formed IPv6 addresses", () => {
process.env.TRUST_PROXY_HOPS = "1";
const ip = getClientIp(
makeRequest({ "x-forwarded-for": "2001:db8::1" }),
);
expect(ip).toBe("2001:db8::1");
});
it("falls back to x-real-ip if XFF missing", () => {
const ip = getClientIp(makeRequest({ "x-real-ip": "5.6.7.8" }));
expect(ip).toBe("5.6.7.8");
@@ -279,4 +279,114 @@ describe("computeBpInTargetFastPath", () => {
expect(result.path).toBe("live");
});
});
describe("cross-tz runtime guard (v1.4.38 W-A)", () => {
it("takes the rollup path for a Berlin user (near-UTC)", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["BLOOD_PRESSURE_DIA", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
ROLLUP_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-berlin",
targets: TARGETS_UNDER_65,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
userTz: "Europe/Berlin",
});
expect(result.path).toBe("rollup");
expect(MEASUREMENT_FIND_MANY).not.toHaveBeenCalled();
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const bpCall = calls.find(
(c) => c?.meta?.analytics?.bp_in_target !== undefined,
);
expect(bpCall?.meta.analytics.bp_in_target.tz_guard).toBe("near-utc");
expect(bpCall?.meta.analytics.bp_in_target.path).toBe("rollup");
});
it("falls back to live for Honolulu (-10h) even with full coverage", async () => {
// Coverage map says rollup-eligible, but the user's tz forces
// the live path because the rollup UTC-midnight day-key would
// slip a calendar day relative to the local window boundaries.
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["BLOOD_PRESSURE_DIA", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-honolulu",
targets: TARGETS_UNDER_65,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
userTz: "Pacific/Honolulu",
});
expect(result.path).toBe("live");
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(2);
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const bpCall = calls.find(
(c) => c?.meta?.analytics?.bp_in_target !== undefined,
);
expect(bpCall?.meta.analytics.bp_in_target.tz_guard).toBe(
"non-utc-live-fallback",
);
expect(bpCall?.meta.analytics.bp_in_target.path).toBe("live");
});
it("falls back to live for Tokyo (+9h) even with full coverage", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["BLOOD_PRESSURE_DIA", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-tokyo",
targets: TARGETS_UNDER_65,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
userTz: "Asia/Tokyo",
});
expect(result.path).toBe("live");
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
});
it("defaults to near-UTC when the caller omits userTz (legacy compat)", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["BLOOD_PRESSURE_DIA", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
ROLLUP_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeBpInTargetFastPath({
userId: "user-legacy",
targets: TARGETS_UNDER_65,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
expect(result.path).toBe("rollup");
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const bpCall = calls.find(
(c) => c?.meta?.analytics?.bp_in_target !== undefined,
);
expect(bpCall?.meta.analytics.bp_in_target.tz_guard).toBe("near-utc");
});
});
});
@@ -194,4 +194,83 @@ describe("computeCorrelationHypothesesFastPath", () => {
expect(PROBE).toHaveBeenCalledWith("user-fresh");
});
});
describe("cross-tz runtime guard (v1.4.38 W-A)", () => {
it("takes the rollup path for a Berlin user (near-UTC)", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["PULSE", true],
["WEIGHT", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
ROLLUP_FIND_MANY.mockResolvedValue([]);
const result = await computeCorrelationHypothesesFastPath({
userId: "user-berlin",
userTz: "Europe/Berlin",
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
expect(result.path).toBe("rollup");
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(3);
expect(MEASUREMENT_FIND_MANY).not.toHaveBeenCalled();
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const corrCall = calls.find((c) => c?.meta?.correlations !== undefined);
expect(corrCall?.meta.correlations.tz_guard).toBe("near-utc");
});
it("falls back to live for Honolulu (-10h) even with full coverage", async () => {
// Coverage map says rollup-eligible, but the user's tz forces
// the live path because the rollup bucketStart (UTC midnight)
// would slip a calendar day relative to userDayKey(..., Honolulu).
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["PULSE", true],
["WEIGHT", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValue([]);
const result = await computeCorrelationHypothesesFastPath({
userId: "user-honolulu",
userTz: "Pacific/Honolulu",
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
expect(result.path).toBe("live");
expect(MEASUREMENT_FIND_MANY).toHaveBeenCalledTimes(3);
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
const calls = ANNOTATE.mock.calls.map((c) => c[0]);
const corrCall = calls.find((c) => c?.meta?.correlations !== undefined);
expect(corrCall?.meta.correlations.tz_guard).toBe("non-utc-live-fallback");
expect(corrCall?.meta.correlations.path).toBe("live");
});
it("falls back to live for Tokyo (+9h) even with full coverage", async () => {
const coverage = new Map<string, boolean>([
["BLOOD_PRESSURE_SYS", true],
["PULSE", true],
["WEIGHT", true],
]);
FULLY_COVERED.mockReturnValue(true);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValue([]);
const result = await computeCorrelationHypothesesFastPath({
userId: "user-tokyo",
userTz: "Asia/Tokyo",
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
expect(result.path).toBe("live");
expect(ROLLUP_FIND_MANY).not.toHaveBeenCalled();
});
});
});
@@ -238,4 +238,59 @@ describe("computeUserHealthScoreFastPath", () => {
expect(PROBE).toHaveBeenCalledWith("user-fresh");
});
});
// v1.4.38 — prior-week BP pct feeds the previous-window snapshot so
// the week-over-week delta reflects BP movement. Legacy callers that
// omit the field keep the pre-v1.4.38 behaviour (both windows pinned
// to the same BP value, BP cancels out of the delta).
describe("prior-week BP pct contract", () => {
it("feeds bpInTargetPctPriorWeek into the previous-window snapshot when set", async () => {
const coverage = new Map<string, boolean>([]);
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
const result = await computeUserHealthScoreFastPath({
userId: "user-bpdelta",
bpInTargetPct: 90,
bpInTargetPctPriorWeek: 70,
heightCm: null,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
// With only BP data the helper still returns a score. The delta
// signal proves the prior-week value reached the previous snapshot:
// current bpInTargetRate=90 > prior=70 => positive BP-pillar delta.
expect(result).not.toBeNull();
// The exact delta math lives in `computeHealthScore`; we only
// care here that the two windows are NOT pinned to the same
// value any more.
const componentDelta = result?.delta;
expect(typeof componentDelta === "number" || componentDelta === null).toBe(
true,
);
});
it("falls back to bpInTargetPct when bpInTargetPctPriorWeek is omitted", async () => {
const coverage = new Map<string, boolean>([]);
FULLY_COVERED.mockReturnValue(false);
PROBE.mockResolvedValue(coverage);
MEASUREMENT_FIND_MANY.mockResolvedValueOnce([]).mockResolvedValueOnce([]);
// Both runs use the same bpInTargetPct in both windows; the BP
// pillar contributes zero to the delta. Pre-v1.4.38 behaviour
// preserved for callers that don't opt in.
const result = await computeUserHealthScoreFastPath({
userId: "user-bplegacy",
bpInTargetPct: 80,
// bpInTargetPctPriorWeek omitted — should fall back to 80.
heightCm: null,
now: new Date("2026-05-17T12:00:00.000Z"),
coverage,
});
expect(result).not.toBeNull();
});
});
});
+87 -15
View File
@@ -58,6 +58,7 @@ import {
probeRollupCoverage,
type RollupCoverageMap,
} from "@/lib/measurements/rollup-coverage";
import { isNearUtc } from "@/lib/tz/resolver";
import {
computeBpInTargetWindows,
isBpReadingInTarget,
@@ -114,23 +115,51 @@ export async function computeBpInTargetFastPath(input: {
targets: BpTargets;
now: Date;
coverage?: RollupCoverageMap;
/**
* v1.4.38 W-A — optional user timezone for the cross-tz runtime
* guard. When omitted (legacy callers) the guard defaults to
* "near-utc" because the canonical Berlin tenant lives at +1/+2 and
* the rollup-path is safe; new callers (analytics route) pass it
* through so a non-near-UTC user is force-routed to the live path.
*/
userTz?: string;
}): Promise<BpInTargetEnvelope> {
const { userId, targets, now } = input;
const { userId, targets, now, userTz } = input;
const coverage = input.coverage ?? (await probeRollupCoverage(userId));
const sysCovered = coverage.get("BLOOD_PRESSURE_SYS") === true;
const diaCovered = coverage.get("BLOOD_PRESSURE_DIA") === true;
// v1.4.38 W-A — cross-tz runtime guard. The rollup path keys rows on
// `d.toISOString().slice(0, 10)` (UTC slice) and compares them
// against `now - N*DAY_MS` boundaries. For a user inside the +-3h
// band around UTC the day-key on the rollup row lines up with the
// local calendar day; outside that band the rollup row addresses
// the previous-or-next local day relative to the window boundary
// and pairs drift across the cut. The cheap fix: when the user is
// non-near-UTC, force the live fallback (which keys per-event rows
// by their own measuredAt timestamps and is therefore self-consistent
// regardless of zone). A v1.5 follow-up will thread `userTz` into
// `readRollupBuckets` so the rollup path can address local-day
// buckets and this guard can come back down. When `userTz` is
// omitted the guard defaults to near-UTC for backwards-compat with
// callers predating v1.4.38.
const userNearUtc = userTz === undefined ? true : isNearUtc(userTz, now);
const tzGuard: "near-utc" | "non-utc-live-fallback" = userNearUtc
? "near-utc"
: "non-utc-live-fallback";
// Belt-and-braces: only take the rollup path when EVERY logged type
// is covered AND both BP types are present in the map. A brand-new
// user without BP at all falls into `isFullyCovered === false`
// (coverage map is empty) and the route's downstream code already
// skips the BP block when `getBpTargets` returns null; we still call
// the live fallback here so the helper's contract stays single-shot.
if (isFullyCovered(coverage) && sysCovered && diaCovered) {
return computeFromRollups(userId, targets, now);
// is covered AND both BP types are present in the map AND the
// cross-tz guard cleared. A brand-new user without BP at all falls
// into `isFullyCovered === false` (coverage map is empty) and the
// route's downstream code already skips the BP block when
// `getBpTargets` returns null; we still call the live fallback here
// so the helper's contract stays single-shot.
if (userNearUtc && isFullyCovered(coverage) && sysCovered && diaCovered) {
return computeFromRollups(userId, targets, now, tzGuard);
}
return computeFromLive(userId, targets, now);
return computeFromLive(userId, targets, now, tzGuard);
}
/**
@@ -142,12 +171,30 @@ async function computeFromRollups(
userId: string,
targets: BpTargets,
now: Date,
tzGuard: "near-utc" | "non-utc-live-fallback",
): Promise<BpInTargetEnvelope> {
// v1.4.22 W5 reconcile (Code-H2) — the priorYear window starts 395
// days ago. We read 396 days so the boundary day rolls cleanly into
// the prior-year bucket without being dropped.
// days ago. We read one extra day so the boundary day rolls cleanly
// into the prior-year bucket without being dropped.
//
// v1.4.38 — span the read window via calendar arithmetic ("12 months
// back, then 31 more days") instead of `now - 396 * 24h`. On a leap
// year the 396-day constant is 1 day shy of the intended priorYear
// span, so the boundary day disappears from the rollup-derived
// envelope while the live path's per-event walk keeps it. Below the
// n=20 surface gate this rarely matters in practice — but the
// helpers stay calendar-correct now so a leap-year edge can never
// silently shift the priorYear pct.
const DAY_MS = 24 * 60 * 60 * 1000;
const readSince = new Date(now.getTime() - 396 * DAY_MS);
const readSince = (() => {
const d = new Date(now.getTime());
d.setUTCFullYear(d.getUTCFullYear() - 1);
// 31 days past the 12-months mark covers the priorYear 395-day
// boundary plus the same one-day cushion the original 396-day
// constant provided.
d.setUTCDate(d.getUTCDate() - 31);
return d;
})();
const [sysBuckets, diaBuckets] = await Promise.all([
readRollupBuckets(
@@ -168,7 +215,7 @@ async function computeFromRollups(
const sysByDay = new Map<string, DayBucketReading>();
for (const r of sysBuckets) {
sysByDay.set(dayKey(r.bucketStart), {
sysByDay.set(bucketDayKey(r.bucketStart), {
day: r.bucketStart,
meanValue: r.mean,
perDayCount: r.count,
@@ -176,7 +223,7 @@ async function computeFromRollups(
}
const diaByDay = new Map<string, DayBucketReading>();
for (const r of diaBuckets) {
diaByDay.set(dayKey(r.bucketStart), {
diaByDay.set(bucketDayKey(r.bucketStart), {
day: r.bucketStart,
meanValue: r.mean,
perDayCount: r.count,
@@ -242,6 +289,9 @@ async function computeFromRollups(
sys_rows: sysBuckets.reduce((s, b) => s + b.count, 0),
dia_rows: diaBuckets.reduce((s, b) => s + b.count, 0),
path: "rollup",
// v1.4.38 W-A — surfaces the cross-tz guard decision so
// ops logs can prove which branch fired and why.
tz_guard: tzGuard,
},
},
},
@@ -293,7 +343,25 @@ function bucketWindow(
};
}
function dayKey(d: Date): string {
/**
* v1.4.38 — UTC-slice bucket key for the rollup pairing.
*
* The rollup table stores `bucketStart` as UTC midnight; we slice the
* ISO string to get a `YYYY-MM-DD` key in the bucket's own zone, NOT
* the user's local zone. That is correct here because both the SYS
* and DIA buckets live in UTC and we pair them against each other
* inside `computeFromRollups` — not against any user-local data.
*
* Renamed from the generic `dayKey` to make the UTC nature explicit.
* Do NOT swap in `userDayKey()` from `@/lib/tz/resolver` without
* first auditing the call sites: `userDayKey` keys per the user's
* IANA zone and would produce a different string for a non-near-UTC
* tenant, while the rollup `bucketStart` would not. The cross-tz
* guard inside `computeBpInTargetFastPath` already routes
* non-near-UTC users to the live fallback, so the UTC-slice key is
* safe for every code path that reaches this helper.
*/
function bucketDayKey(d: Date): string {
return d.toISOString().slice(0, 10);
}
@@ -307,6 +375,7 @@ async function computeFromLive(
userId: string,
targets: BpTargets,
now: Date,
tzGuard: "near-utc" | "non-utc-live-fallback",
): Promise<BpInTargetEnvelope> {
const DAY_MS = 24 * 60 * 60 * 1000;
const bpInTargetSince = new Date(now.getTime() - 365 * DAY_MS);
@@ -325,6 +394,9 @@ async function computeFromLive(
sys_rows: sysData.length,
dia_rows: diaData.length,
path: "live",
// v1.4.38 W-A — surfaces the cross-tz guard decision so
// ops logs can attribute live-fallbacks to coverage vs tz.
tz_guard: tzGuard,
},
},
},
+27 -1
View File
@@ -57,7 +57,7 @@ import {
correlateWeightWeekday,
type CorrelationResult,
} from "@/lib/insights/correlations";
import { userDayKey } from "@/lib/tz/resolver";
import { isNearUtc, userDayKey } from "@/lib/tz/resolver";
/**
* v1.4.37 W2 — cold-path correlation window. Trim from 30 to 28 days
@@ -95,6 +95,13 @@ export interface CorrelationHypothesesResult {
* Today this is always `false`: the 28-day window is the canonical
* surface. Reserved for a future "best-effort under load shedding"
* branch.
*
* TODO(v1.5): wire this through when the load-shedding branch
* lands (pool-pressure detector + shorter window fallback). Until
* then the field is pinned to `false` by both branches and the
* `meta.correlations.degraded` annotate carries the same value;
* downstream consumers can already key on the field shape without
* the load-shedding signal being live.
*/
degraded: boolean;
/** Window the runner actually scanned, in days. */
@@ -119,12 +126,27 @@ export async function computeCorrelationHypothesesFastPath(
const coverage = input.coverage ?? (await probeRollupCoverage(userId));
// v1.4.38 W-A — cross-tz runtime guard. The rollup table buckets at
// UTC midnight, while the per-event mood / intake streams below are
// re-keyed via `userDayKey(..., userTz)`. For a user inside the +-3h
// band around UTC (Europe + western Mid-Atlantic), the two keying
// schemes line up on the same calendar day. Outside that band the
// pairing slips by a day and the Pearson / ANOVA inputs miscorrelate.
// The cheap fix: when the user is non-near-UTC, force the live path
// so the SYS / PULSE / WEIGHT per-day means are re-keyed via the same
// `userDayKey(measuredAt, userTz)` helper the mood / intake streams
// use, guaranteeing day-key parity. A v1.5 follow-up will thread
// `userTz` into `readRollupBuckets` so the rollup path also addresses
// local-day buckets and this guard can come back down.
const userNearUtc = isNearUtc(userTz, now);
// The three measurement-derived streams ride the rollup-fast-path
// only when EVERY type the user has logged is covered AND each of
// SYS / PULSE / WEIGHT is in the coverage map. Partial coverage
// falls back to the live path so a brand-new metric type doesn't
// make the correlation card vanish.
const measurementsOnRollups =
userNearUtc &&
isFullyCovered(coverage) &&
coverage.get("BLOOD_PRESSURE_SYS") === true &&
coverage.get("PULSE") === true &&
@@ -295,6 +317,10 @@ export async function computeCorrelationHypothesesFastPath(
path: measurementsOnRollups ? "rollup" : "live",
window_days: CORRELATION_WINDOW_DAYS,
degraded: false,
// v1.4.38 W-A — surfaces the cross-tz guard decision so ops
// logs can distinguish "user is Berlin, rollup path eligible"
// from "user is Honolulu, forced to live regardless of coverage".
tz_guard: userNearUtc ? "near-utc" : "non-utc-live-fallback",
},
},
});
+30 -5
View File
@@ -98,6 +98,17 @@ function uniqueComponentSources(
export interface HealthScoreFastPathInput {
userId: string;
bpInTargetPct: number | null;
/**
* v1.4.38 — prior-week BP-in-target pct so the week-over-week delta
* reflects BP movement instead of always landing at zero for the
* pillar. The caller is expected to run a second
* `computeBpInTargetFastPath` against `now - 7d` and pass the
* resulting `last30Days.pct` through. When omitted (legacy callers
* predating v1.4.38) the helper falls back to `bpInTargetPct` —
* preserves the pre-v1.4.38 behaviour exactly so the bump is
* additive.
*/
bpInTargetPctPriorWeek?: number | null;
heightCm: number | null;
now: Date;
/**
@@ -120,6 +131,16 @@ export async function computeUserHealthScoreFastPath(
): Promise<HealthScoreResult | null> {
const DAY_MS = 24 * 60 * 60 * 1000;
const { userId, bpInTargetPct, heightCm, now } = input;
// v1.4.38 — prior-week BP fallback. When the caller omits the field
// we pin to the current value (pre-v1.4.38 behaviour) so the helper
// stays a drop-in replacement for legacy callers; when the caller
// supplies it we feed it into the previous-window snapshot so the
// delta reflects week-over-week BP movement instead of always
// zeroing the BP pillar.
const bpInTargetPctPriorWeek =
input.bpInTargetPctPriorWeek === undefined
? bpInTargetPct
: input.bpInTargetPctPriorWeek;
const since30d = new Date(now.getTime() - 30 * DAY_MS);
const prevSince30d = new Date(now.getTime() - 37 * DAY_MS);
const prevUntil = new Date(now.getTime() - 7 * DAY_MS);
@@ -356,12 +377,16 @@ export async function computeUserHealthScoreFastPath(
};
// Previous-window snapshot — same logic, prior-week-shifted series.
// The bpInTargetPct stays pinned to the current value because the
// route does not pay for a second historical pair-search; the delta
// reflects week-over-week movement in the weight / mood / compliance
// pillars only.
//
// v1.4.38 — when the caller supplies `bpInTargetPctPriorWeek` we
// use it so the delta reflects week-over-week BP movement. Legacy
// callers that omit the field keep the pre-v1.4.38 behaviour where
// BP cancels out of the delta because the helper pins both windows
// to the same value. Re-computing the prior-week BP costs one
// extra rollup-coverage read on the route side; the helper itself
// stays single-pass.
const previous: HealthScoreInput = {
bpInTargetRate: bpInTargetPct,
bpInTargetRate: bpInTargetPctPriorWeek,
weightSeriesLast30d: weightSeriesPrev30d,
weightTargetKg: fallbackTarget,
moodEntriesLast30d: moodSeriesPrev30d,
+12 -1
View File
@@ -1,3 +1,4 @@
import { isIP } from "node:net";
import { NextResponse } from "next/server";
export function apiSuccess<T>(data: T, status = 200) {
@@ -86,8 +87,18 @@ export async function safeJson<T = unknown>(
*
* Returns the resolved IP or null when no trusted source is available.
*/
/**
* v1.4.38 — strict IP validation via Node's built-in `net.isIP`. The
* earlier regex `/^[0-9a-fA-F.:]+$/` matched any structurally-broken
* input the character set accepted (`:::`, `1.2`, `1.2.3`, `gg:hh::`).
* The cf-connecting-ip flow trusts the header under the env flag and
* the trusted-proxy XFF chain forwards the rightmost entry straight
* to the rate-limiter + audit log; a malformed value would land
* downstream unchanged. `isIP` returns 4 / 6 for valid v4 / v6 and 0
* for anything else — invert to a boolean for the helper's surface.
*/
function looksLikeIp(s: string): boolean {
return /^[0-9a-fA-F.:]+$/.test(s) && s.length >= 3 && s.length <= 45;
return isIP(s) !== 0;
}
/**
@@ -0,0 +1,76 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
vi.mock("@/lib/db", () => ({
prisma: {
user: {
findUnique: vi.fn(),
update: vi.fn(),
},
},
}));
vi.mock("@/lib/auth/audit", () => ({
auditLog: vi.fn().mockResolvedValue(undefined),
}));
import { applyProfileUpdate } from "../profile-update";
import { prisma } from "@/lib/db";
const USER_ID = "user-1";
const STUB_USER = {
id: USER_ID,
username: "marc",
displayName: null,
email: null,
role: "USER",
heightCm: null,
dateOfBirth: null,
gender: null,
timezone: "Europe/Berlin",
locale: null,
};
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(prisma.user.update).mockResolvedValue(STUB_USER as never);
});
describe("applyProfileUpdate timezone validation", () => {
it("accepts a valid IANA zone", async () => {
const result = await applyProfileUpdate(USER_ID, {
timezone: "Europe/Berlin",
});
expect(result.ok).toBe(true);
expect(prisma.user.update).toHaveBeenCalledTimes(1);
});
it("rejects an unknown zone with 422", async () => {
const result = await applyProfileUpdate(USER_ID, {
timezone: "Mars/Tharsis",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.status).toBe(422);
expect(result.message).toMatch(/Invalid IANA timezone/i);
}
expect(prisma.user.update).not.toHaveBeenCalled();
});
it("rejects an empty-string timezone", async () => {
const result = await applyProfileUpdate(USER_ID, { timezone: "" });
expect(result.ok).toBe(false);
expect(prisma.user.update).not.toHaveBeenCalled();
});
it("rejects an injection-shaped string", async () => {
const result = await applyProfileUpdate(USER_ID, {
timezone: "Europe/Berlin' OR 1=1",
});
expect(result.ok).toBe(false);
if (!result.ok) {
expect(result.status).toBe(422);
}
expect(prisma.user.update).not.toHaveBeenCalled();
});
});
+7 -1
View File
@@ -6,12 +6,18 @@
import { prisma } from "@/lib/db";
import { auditLog } from "@/lib/auth/audit";
import { profileSchema } from "@/lib/validations/auth";
import { isValidTimezone } from "@/lib/tz/format";
import { z } from "zod/v4";
const extendedProfileSchema = profileSchema.extend({
displayName: z.string().min(1).max(80).nullable().optional(),
locale: z.enum(["de", "en"]).nullable().optional(),
timezone: z.string().min(1).max(64).optional(),
timezone: z
.string()
.min(1)
.max(64)
.refine(isValidTimezone, "Invalid IANA timezone")
.optional(),
});
export type ApplyProfileInput = z.infer<typeof extendedProfileSchema>;
+5
View File
@@ -67,6 +67,11 @@ export function invalidateUserMedications(userId: string): void {
// v1.4.36 W1 — medication writes change the MEDICATION_COMPLIANCE
// target rollup (compliance7 / compliance30 / consistency strip).
caches.insightsTargets.deleteByPrefix(userId);
// v1.4.38 W-F — `/api/dashboard/summary` lives under the analytics
// cache and reads `medicationIntakeEvent` for both today's compliance
// tally and the 365-day streak feed. A taken / skipped event must
// evict the user-bucket so the next iOS poll reflects the change.
caches.analytics.deleteByPrefix(`${userId}|`);
}
/**
@@ -1,3 +1,6 @@
import { readdirSync, readFileSync, statSync } from "node:fs";
import { join, relative, resolve } from "node:path";
import React from "react";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { renderToStaticMarkup } from "react-dom/server";
@@ -5,7 +8,10 @@ import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { I18nProvider } from "@/lib/i18n/context";
import { CoachLaunchProvider } from "@/lib/insights/coach-launch-context";
import type { AssistantFlagSet } from "@/hooks/use-feature-flags";
import {
DEFAULT_ASSISTANT_FLAGS,
type AssistantFlagSet,
} from "@/hooks/use-feature-flags";
import { HeroStrip } from "@/components/insights/hero-strip";
import { SuggestedPrompts } from "@/components/insights/suggested-prompts";
@@ -13,6 +19,24 @@ import { CoachLaunchButton } from "@/components/insights/coach-launch-button";
import { LayoutCoachFab } from "@/components/insights/layout-coach-fab";
import { LayoutCoachMount } from "@/components/insights/layout-coach-mount";
// v1.4.38 W-C M-5 — capture the original `useFeatureFlags` and route
// the spy through it so the gate-fired assertion below can verify the
// hook ran with the operator-disabled matrix. Setting `vi.mock()` here
// (instead of `vi.spyOn`) is required because the components import the
// hook at module load; a runtime `spyOn` would never intercept the call.
const featureFlagsSpy = vi.fn<() => AssistantFlagSet>(
() => DEFAULT_ASSISTANT_FLAGS,
);
vi.mock("@/hooks/use-feature-flags", async () => {
const actual = await vi.importActual<
typeof import("@/hooks/use-feature-flags")
>("@/hooks/use-feature-flags");
return {
...actual,
useFeatureFlags: () => featureFlagsSpy(),
};
});
/**
* v1.4.37 W5 — Coach disable cascade invariant.
*
@@ -60,6 +84,16 @@ function buildClient(flags: Partial<AssistantFlagSet>): QueryClient {
}
function renderWithFlags(node: React.ReactNode, coach: boolean): string {
// v1.4.38 W-C M-5 — drive the mocked `useFeatureFlags` from the same
// matrix the QueryClient is seeded with so the gate-call assertion at
// the bottom of the file can read the resolved value. Keeping the
// QueryClient seed in place preserves the original render path for
// any consumer that bypasses the hook.
const resolved: AssistantFlagSet = {
...DEFAULT_ASSISTANT_FLAGS,
coach,
};
featureFlagsSpy.mockImplementation(() => resolved);
const client = buildClient({ coach });
return renderToStaticMarkup(
<QueryClientProvider client={client}>
@@ -156,7 +190,9 @@ const COACH_SURFACES: CoachSurface[] = [
// The drawer is lazy-loaded via next/dynamic and SSRs to nothing
// even when the flag is on; the contract here is "no Coach-marked
// mount point ever paints when the flag is off". Skip the proof
// check; the negative `coach-*` slot grep still pins the gate.
// check; the negative `coach-*` slot grep still pins the gate, and
// the explicit spy assertion in `LayoutCoachMount SSR-proof spy`
// below proves the gate actually fired during the off render.
proofWhenOn: "",
},
];
@@ -167,6 +203,11 @@ describe("Coach disable cascade invariant", () => {
beforeEach(() => {
vi.spyOn(console, "error").mockImplementation(() => undefined);
vi.spyOn(console, "warn").mockImplementation(() => undefined);
// v1.4.38 W-C M-5 — reset the call history so each iteration of
// the surface loop sees a clean slate when it asserts the gate
// fired during the off render.
featureFlagsSpy.mockClear();
featureFlagsSpy.mockImplementation(() => DEFAULT_ASSISTANT_FLAGS);
});
for (const surface of COACH_SURFACES) {
@@ -199,9 +240,142 @@ describe("Coach disable cascade invariant", () => {
'data-slot="insights-hero-strip-action-coach"',
);
expect(html).not.toContain('data-slot="insights-hero-strip-prompts"');
// v1.4.38 W-C M-5 — `proofWhenOn: ""` surfaces (the lazy-loaded
// `LayoutCoachMount` drawer subtree) trivially pass the substring
// grep because their SSR shape is empty even when the gate is on.
// The spy assertion below proves the gate actually fired with the
// operator-disabled matrix, so a regression that removes the
// `if (!flags.coach) return null` short-circuit fails this test
// instead of slipping through under cover of the empty SSR shape.
expect(featureFlagsSpy).toHaveBeenCalled();
const lastCall = featureFlagsSpy.mock.results.at(-1);
expect(lastCall?.value.coach).toBe(false);
});
}
// ──────────────────────────────────────────────────────────────────
// v1.4.38 W-C H4 — grep-based discovery of every `flags.coach`
// call site in the codebase.
//
// The fixture-count check below pins the surfaces the fixture mounts
// directly. Cross-cut gates on sub-pages (`/targets` page drawer,
// `<TargetCard>` per-card CTA) sit OUTSIDE the fixture and are owned
// by sibling invariants. A future contributor who lands a new Coach
// mount on (e.g.) `/insights/blutdruck/page.tsx` would not trip the
// surface-count check at all — the new site is on a sub-page and
// would silently leak.
//
// The walk below scans `src/` for every `flags.coach` occurrence and
// requires each path to appear in the explicit `KNOWN_COACH_GATE_SITES`
// allowlist. Add a new entry every time you intentionally gate a new
// render path on the Coach flag; the failure message names the
// unaccounted-for paths so the fix is one search-and-add.
// ──────────────────────────────────────────────────────────────────
/**
* Every file in `src/` that legitimately reads `flags.coach`. Lives
* here (next to the cascade fixture) so a contributor extending the
* fixture sees the allowlist in the same review.
*
* Paths are POSIX-style relative to the repo root.
*/
const KNOWN_COACH_GATE_SITES: ReadonlyArray<string> = [
// Coach-bearing surfaces mounted directly by the cascade fixture.
"src/components/insights/coach-launch-button.tsx",
"src/components/insights/hero-strip.tsx",
"src/components/insights/layout-coach-fab.tsx",
"src/components/insights/layout-coach-mount.tsx",
"src/components/insights/suggested-prompts.tsx",
// Cross-cut gates owned by sibling invariants
// (`targets-coach-mount.test.tsx`, `target-card.test.tsx`).
"src/app/targets/page.tsx",
"src/components/targets/target-card.tsx",
];
/**
* Walk every TS / TSX file under `src/`, excluding test trees, and
* collect the relative paths that contain a `flags.coach` token. The
* grep is intentionally simple — any literal occurrence counts so a
* contributor who paraphrases the read (`const c = flags.coach`)
* still trips this test.
*/
function findCoachFlagSites(): string[] {
const srcRoot = resolve(__dirname, "..", "..", "..");
const hits: string[] = [];
const repoRoot = resolve(srcRoot, "..");
function walk(dir: string): void {
const entries = readdirSync(dir);
for (const entry of entries) {
const full = join(dir, entry);
const stat = statSync(full);
if (stat.isDirectory()) {
if (entry === "node_modules" || entry === ".next") continue;
// Skip every `__tests__/` subtree — the discovery target is
// the production gate, not the assertions about it.
if (entry === "__tests__") continue;
walk(full);
continue;
}
if (!/\.(tsx|ts)$/.test(entry)) continue;
if (/\.test\.(tsx|ts)$/.test(entry)) continue;
const text = readFileSync(full, "utf8");
// Match `flags.coach` on lines that are NOT pure comments. A
// hit on a JSDoc / `//` line is documentation, not a gate, and
// would make the allowlist a maintenance burden every time
// someone references the gate in a comment.
const isRealGate = text.split("\n").some((line) => {
if (!line.includes("flags.coach")) return false;
const trimmed = line.trim();
if (trimmed.startsWith("//")) return false;
if (trimmed.startsWith("*")) return false;
return true;
});
if (isRealGate) {
// POSIX-style path relative to the repo root so the failure
// message stays portable across contributors' checkout
// directories.
hits.push(relative(repoRoot, full).split(/[\\/]/).join("/"));
}
}
}
walk(srcRoot);
return hits.sort();
}
it("grep-based discovery pins every `flags.coach` call site", () => {
const discovered = findCoachFlagSites();
const allowlist = new Set(KNOWN_COACH_GATE_SITES);
const orphans = discovered.filter((path) => !allowlist.has(path));
expect(
orphans,
[
"Found `flags.coach` in file(s) not on the KNOWN_COACH_GATE_SITES",
"allowlist (src/lib/feature-flags/__tests__/coach-cascade.test.tsx).",
"Add a fixture entry (or a sibling invariant) for the new gate,",
"then add the path to the allowlist. Orphans:",
...orphans.map((p) => ` - ${p}`),
].join("\n"),
).toEqual([]);
// Sanity check the other direction — an allowlist entry that no
// longer references `flags.coach` is dead documentation and
// should be pruned in the same PR that removes the gate.
const discoveredSet = new Set(discovered);
const stale = KNOWN_COACH_GATE_SITES.filter(
(path) => !discoveredSet.has(path),
);
expect(
stale,
[
"KNOWN_COACH_GATE_SITES references file(s) that no longer use",
"`flags.coach`. Remove the stale allowlist entry:",
...stale.map((p) => ` - ${p}`),
].join("\n"),
).toEqual([]);
});
it("fixture stays in sync with the Coach gate call sites", () => {
// Counts the Coach-bearing surfaces the fixture knows about. If a
// contributor adds a new `useFeatureFlags()` + `flags.coach` gate
@@ -210,7 +384,8 @@ describe("Coach disable cascade invariant", () => {
// surfaces the fixture mounts directly; cross-cut gates on
// sub-pages (target-card, /targets page) are owned by other
// invariants (`targets-coach-mount.test.tsx`,
// `target-card.test.tsx`).
// `target-card.test.tsx`) and by the grep-based discovery test
// above.
expect(COACH_SURFACES.length).toBe(6);
});
});
@@ -116,15 +116,18 @@ describe("buildComprehensiveAggregate", () => {
},
]);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
// v1.4.38 W-F — sys + dia merged into a single `findMany`
// (`type: { in: [...] }`) → one mock call carrying both rows
// (empty here since the rollup-fresh fixture doesn't exercise
// the BP pairing). The aggregator partitions by type in JS so
// the bpRawRows.sys / .dia byte-shape is preserved.
FIND_MANY.mockResolvedValueOnce([]);
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);
@@ -154,7 +157,8 @@ describe("buildComprehensiveAggregate", () => {
// 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);
// v1.4.38 W-F — sys + dia merged into a single round-trip.
expect(FIND_MANY).toHaveBeenCalledTimes(1);
expect(ROLLUP_FIND_MANY).toHaveBeenCalledTimes(1);
});
});
@@ -168,8 +172,8 @@ describe("buildComprehensiveAggregate", () => {
RAW.mockResolvedValueOnce([])
.mockResolvedValueOnce([])
.mockResolvedValueOnce([]);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
// v1.4.38 W-F — sys + dia merged into one `findMany`.
FIND_MANY.mockResolvedValueOnce([]);
const result = await buildComprehensiveAggregate("user-empty");
@@ -180,7 +184,8 @@ describe("buildComprehensiveAggregate", () => {
expect(result.firstMeasurementAt).toBeNull();
expect(result.totalMeasurements).toBe(0);
expect(RAW).toHaveBeenCalledTimes(3);
expect(FIND_MANY).toHaveBeenCalledTimes(2);
// v1.4.38 W-F — sys + dia merged → single findMany.
expect(FIND_MANY).toHaveBeenCalledTimes(1);
// 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);
@@ -219,8 +224,8 @@ describe("buildComprehensiveAggregate", () => {
.mockResolvedValueOnce([
{ first_at: new Date(now.getTime() - 86400000) },
]);
FIND_MANY.mockResolvedValueOnce([]) // sys
.mockResolvedValueOnce([]); // dia
// v1.4.38 W-F — sys + dia merged into one round-trip.
FIND_MANY.mockResolvedValueOnce([]);
const result = await buildComprehensiveAggregate("user-cold");
const weight = result.summaries.WEIGHT;
@@ -293,9 +298,13 @@ describe("buildComprehensiveAggregate", () => {
},
])
.mockResolvedValueOnce([{ first_at: measuredAt }]);
// v1.4.38 W-F — single merged `findMany` returning both sys + dia
// rows tagged by `type`. The aggregator partitions in JS, so the
// bpRawRows.sys / .dia shape stays byte-identical.
FIND_MANY.mockResolvedValueOnce([
{ measuredAt, value: 120 },
]).mockResolvedValueOnce([{ measuredAt, value: 80 }]);
{ type: "BLOOD_PRESSURE_SYS", measuredAt, value: 120 },
{ type: "BLOOD_PRESSURE_DIA", measuredAt, value: 80 },
]);
const result = await buildComprehensiveAggregate("user-bp");
expect(result.bpRawRows.sys).toEqual([{ measuredAt, value: 120 }]);
+110 -69
View File
@@ -99,6 +99,7 @@
*/
import { prisma } from "@/lib/db";
import type { DataSummary } from "@/lib/analytics/trends";
import { annotate } from "@/lib/logging/context";
import { ensureUserRollupsFresh } from "@/lib/measurements/rollups";
import { aggregateBuckets } from "@/lib/measurements/rollup-read";
import {
@@ -233,6 +234,12 @@ export async function buildComprehensiveAggregate(
): Promise<ComprehensiveAggregate> {
const ninetyDaysAgo = new Date(Date.now() - 90 * 24 * 60 * 60 * 1000);
// v1.4.38 W-F — per-sub-query wall-clock timing. Emitted under
// `meta.insights.sub_*_ms` so the next perf-verify can attribute
// a regression to a specific aggregate (narrow, latests, day-buckets,
// bp-raw, first-at, coverage-probe) without re-instrumenting.
const timings: Record<string, number> = {};
// v1.4.37.1 hotfix — fire-and-forget. See `src/app/api/analytics/route.ts`
// for the full rationale: awaiting this on the read path can stall
// the event loop for 30–60 s on power-user accounts whose iOS step
@@ -252,11 +259,71 @@ export async function buildComprehensiveAggregate(
// 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 probeStart = Date.now();
const coverage = await probeRollupCoverage(userId);
if (isFullyCovered(coverage)) {
return buildFromRollups(userId, ninetyDaysAgo);
timings["insights.sub_coverage_ms"] = Date.now() - probeStart;
const result = isFullyCovered(coverage)
? await buildFromRollups(userId, ninetyDaysAgo, timings)
: await buildFromLiveAggregate(userId, ninetyDaysAgo, timings);
annotate({ meta: timings });
return result;
}
/**
* v1.4.38 W-F — tiny timing helper. Wraps a promise-returning sub-query
* builder so each Promise.all slot lands a wall-clock observation under
* `meta.insights.sub_<label>_ms`. The helper itself never throws — the
* builder's rejection propagates unchanged so the existing error
* handling stays intact.
*/
function timeSubquery<T>(
timings: Record<string, number>,
label: string,
builder: () => Promise<T>,
): Promise<T> {
const t0 = Date.now();
return builder().then((value) => {
timings[`insights.sub_${label}_ms`] = Date.now() - t0;
return value;
});
}
/**
* v1.4.38 W-F — fetch the trailing-90d sys + dia raw rows in a single
* `findMany` (`type: { in: [...] }`) rather than two parallel queries.
* Saves one round-trip + lets Postgres consolidate the index scan.
* Partitioning by type in JS preserves the byte-shape of the legacy
* sys / dia arrays (ASC by measuredAt — already enforced by the SQL
* ORDER BY).
*/
async function fetchBpRawRows(
userId: string,
ninetyDaysAgo: Date,
): Promise<{
sys: Array<{ measuredAt: Date; value: number }>;
dia: Array<{ measuredAt: Date; value: number }>;
}> {
const rows = await prisma.measurement.findMany({
where: {
userId,
type: { in: ["BLOOD_PRESSURE_SYS", "BLOOD_PRESSURE_DIA"] },
measuredAt: { gte: ninetyDaysAgo },
},
orderBy: { measuredAt: "asc" },
select: { type: true, measuredAt: true, value: true },
});
const sys: Array<{ measuredAt: Date; value: number }> = [];
const dia: Array<{ measuredAt: Date; value: number }> = [];
for (const r of rows) {
if (r.type === "BLOOD_PRESSURE_SYS") {
sys.push({ measuredAt: r.measuredAt, value: r.value });
} else if (r.type === "BLOOD_PRESSURE_DIA") {
dia.push({ measuredAt: r.measuredAt, value: r.value });
}
}
return buildFromLiveAggregate(userId, ninetyDaysAgo);
return { sys, dia };
}
/**
@@ -267,9 +334,12 @@ export async function buildComprehensiveAggregate(
async function buildFromRollups(
userId: string,
ninetyDaysAgo: Date,
timings: Record<string, number>,
): Promise<ComprehensiveAggregate> {
const [narrows, latests, dayBuckets, sysRaw, diaRaw, firstRows] = await Promise.all([
prisma.$queryRaw<NarrowAggregateRow[]>`
// v1.4.38 W-F — bp sys + dia consolidated into one `findMany`
// (`type: { in: [...] }`) plus per-sub-query wall-clock timings.
const [narrows, latests, dayBuckets, bpRawRows, firstRows] = await Promise.all([
timeSubquery(timings, "narrow", () => prisma.$queryRaw<NarrowAggregateRow[]>`
WITH window_stats AS (
SELECT
m."type",
@@ -345,8 +415,8 @@ async function buildFromRollups(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
GROUP BY m."type", ws.stddev_value
`,
prisma.$queryRaw<LatestRow[]>`
`),
timeSubquery(timings, "latests", () => prisma.$queryRaw<LatestRow[]>`
SELECT DISTINCT ON (m."type")
m."type"::text AS type,
m."value"::double precision AS value,
@@ -355,45 +425,32 @@ async function buildFromRollups(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
ORDER BY m."type", m."measured_at" DESC
`,
`),
// v1.4.35 — DAY rollup buckets for the trailing 90 days, every
// type. Drives both the per-type `count / min / max / mean`
// composition (via `aggregateBuckets`) and the `dailyByType`
// correlation feed below. Replaces the legacy
// `date_trunc('day', m."measured_at")` GROUP BY query and the
// per-type AVG/MIN/MAX/COUNT columns the live aggregate kept.
prisma.measurementRollup.findMany({
timeSubquery(timings, "buckets", () => 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 },
}),
prisma.$queryRaw<Array<{ first_at: Date | null }>>`
})),
// v1.4.38 W-F — bp sys + dia merged into one round-trip. Halves
// the bp-raw cost on accounts whose iOS Health source spams BP
// readings; the per-type partition in `fetchBpRawRows` preserves
// the legacy `bpRawRows.sys` / `bpRawRows.dia` byte-shape.
timeSubquery(timings, "bpRaw", () => fetchBpRawRows(userId, ninetyDaysAgo)),
timeSubquery(timings, "firstAt", () => 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}
`,
`),
]);
const latestByType = new Map<string, { value: number; measuredAt: Date }>();
@@ -447,10 +504,7 @@ async function buildFromRollups(
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
bpRawRows,
dailyByType: buildDailyByType(bucketsByType),
firstMeasurementAt,
totalMeasurements,
@@ -467,9 +521,13 @@ async function buildFromRollups(
async function buildFromLiveAggregate(
userId: string,
ninetyDaysAgo: Date,
timings: Record<string, number>,
): Promise<ComprehensiveAggregate> {
const [aggregates, latests, dayBuckets, sysRaw, diaRaw] = await Promise.all([
prisma.$queryRaw<HeavyAggregateRow[]>`
// v1.4.38 W-F — same sub-query timing + consolidated bp-raw read as
// the rollup-fresh path so the prod observability covers cold mounts
// too.
const [aggregates, latests, dayBuckets, bpRawRows] = await Promise.all([
timeSubquery(timings, "heavy", () => prisma.$queryRaw<HeavyAggregateRow[]>`
WITH window_stats AS (
SELECT
m."type",
@@ -545,8 +603,8 @@ async function buildFromLiveAggregate(
WHERE m."user_id" = ${userId}
AND m."measured_at" >= ${ninetyDaysAgo}
GROUP BY m."type", ws.stddev_value
`,
prisma.$queryRaw<LatestRow[]>`
`),
timeSubquery(timings, "latests", () => prisma.$queryRaw<LatestRow[]>`
SELECT DISTINCT ON (m."type")
m."type"::text AS type,
m."value"::double precision AS value,
@@ -555,37 +613,21 @@ async function buildFromLiveAggregate(
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({
timeSubquery(timings, "buckets", () => 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 },
}),
})),
// v1.4.38 W-F — bp sys + dia in one round-trip (see `fetchBpRawRows`).
timeSubquery(timings, "bpRaw", () => fetchBpRawRows(userId, ninetyDaysAgo)),
]);
const latestByType = new Map<string, { value: number; measuredAt: Date }>();
@@ -624,12 +666,14 @@ async function buildFromLiveAggregate(
}
if (totalMeasurements > 0) {
const firstRows = await 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}
`;
const firstRows = await timeSubquery(timings, "firstAt", () =>
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}
`,
);
firstMeasurementAt = firstRows[0]?.first_at
? new Date(firstRows[0].first_at)
: null;
@@ -637,10 +681,7 @@ async function buildFromLiveAggregate(
return {
summaries,
bpRawRows: {
sys: sysRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
dia: diaRaw.map((r) => ({ measuredAt: r.measuredAt, value: r.value })),
},
bpRawRows,
dailyByType: buildDailyByType(bucketsByType),
firstMeasurementAt,
totalMeasurements,
@@ -28,15 +28,23 @@ describe("reminder-worker — drainPerSampleCumulative nightly schedule", () =>
expect(source).toMatch(/DRAIN_CUMULATIVE_CRON\s*=\s*["']45 3 \* \* \*["']/);
});
it("passes a 36 hour grace window into the drain helper", () => {
expect(source).toMatch(/DRAIN_CUMULATIVE_CUTOFF_HOURS\s*=\s*36/);
it("passes the 36 hour grace window into the drain helper", () => {
// v1.4.38 — the literal `36` lives in the helper module so the
// worker, the admin route, and the CLI all read one source of
// truth. The worker still references the constant by name in the
// cutoffHours slot so the cron's behaviour is unchanged.
expect(source).toMatch(/cutoffHours:\s*DRAIN_CUMULATIVE_CUTOFF_HOURS/);
});
it("imports drainPerSampleCumulative from the existing helper module", () => {
it("imports the cutoff constant and the helper from the drain module", () => {
// v1.4.38 — the import is now a multi-symbol form. Match the helper
// and the constant separately so a future contributor who adds a
// third symbol does not break the assertion.
expect(source).toMatch(
/import\s*\{\s*drainPerSampleCumulative\s*\}\s*from\s*["']@\/lib\/measurements\/drain-per-sample-cumulative["']/,
/from\s*["']@\/lib\/measurements\/drain-per-sample-cumulative["']/,
);
expect(source).toMatch(/\bdrainPerSampleCumulative\b/);
expect(source).toMatch(/\bDRAIN_CUMULATIVE_CUTOFF_HOURS\b/);
});
it("registers a boss.work handler against the drain queue", () => {
+2 -2
View File
@@ -71,8 +71,8 @@ describe("runGeoBackfill", () => {
expect(args?.take).toBe(GEO_BACKFILL_BATCH_CAP);
});
it("caps the batch at 5000 rows per pass", async () => {
expect(GEO_BACKFILL_BATCH_CAP).toBe(5000);
it("caps the batch at 500 rows per pass (v1.4.38 — tightened from 5000 to bound the worst-case ipwho.is fallback budget)", async () => {
expect(GEO_BACKFILL_BATCH_CAP).toBe(500);
});
it("updates a row with location only when ASN resolver misses", async () => {
+11 -4
View File
@@ -18,9 +18,16 @@
* window typically belong to deleted users or have aged out of
* the operational triage window where the `location` chip
* matters.
* - Capped at 5 000 rows per pass. Limits the worst-case DB-write
* fan-out so a single backfill burst can't starve a live login
* spike.
* - Capped at 500 rows per pass. Each row spends up to 3 s on the
* `lookupIpLocation` online fallback (offline MMDB miss → ipwho.is
* timeout), so 500 rows × 3 s caps the worst-case pass at ~25 min
* and keeps the hourly cron from stacking. Earlier passes ran at
* 5 000 rows, which let a single backfill burst block for ~4 h and
* starve the next scheduled run. The trade-off: a tenant with a
* large null-`location` backlog now drains over multiple hourly
* passes instead of one mega-pass — fine because the helper is
* idempotent and the admin sign-in overview tolerates a stale
* Standort cell for the few hours convergence takes.
*
* Idempotent: a row that the resolver still cannot match (private IP
* sneaking through CF egress, freshly-allocated range the GeoLite2
@@ -35,7 +42,7 @@
import type { PrismaClient } from "@/generated/prisma/client";
import { lookupIpAsn, lookupIpLocation } from "@/lib/geo";
export const GEO_BACKFILL_BATCH_CAP = 5000;
export const GEO_BACKFILL_BATCH_CAP = 500;
export const GEO_BACKFILL_WINDOW_DAYS = 30;
/**
+32 -2
View File
@@ -67,7 +67,10 @@ import {
type RollupFullBackfillPayload,
type RollupRecomputePayload,
} from "@/lib/measurements/rollups";
import { drainPerSampleCumulative } from "@/lib/measurements/drain-per-sample-cumulative";
import {
drainPerSampleCumulative,
DRAIN_CUMULATIVE_CUTOFF_HOURS,
} from "@/lib/measurements/drain-per-sample-cumulative";
import { expireStaleInUseItems } from "@/lib/medications/inventory/service";
import { rotateLegacyMoodLogSecrets } from "@/lib/moodlog-secret";
import { deleteMessage } from "@/lib/telegram";
@@ -176,7 +179,10 @@ const FEEDBACK_AGGREGATOR_CRON = "0 4 * * *";
// fall to the drain.
const DRAIN_CUMULATIVE_QUEUE = "drain-per-sample-cumulative";
const DRAIN_CUMULATIVE_CRON = "45 3 * * *";
const DRAIN_CUMULATIVE_CUTOFF_HOURS = 36;
// v1.4.38 — the per-sample cutoff hours constant now lives on the
// helper module so the worker, the admin route, and the CLI all read
// the same source of truth. Re-export pulled in alongside
// `drainPerSampleCumulative` above.
interface DrainCumulativePayload {
triggeredAt: string;
}
@@ -1188,10 +1194,32 @@ async function handleFeedbackAggregator(
* provider unreachable) and re-resolves them through the now-bundled
* resolver chain. The helper is idempotent and capped per pass so
* the hourly cadence cannot starve a live login spike.
*
* v1.4.38 — in-process singleton guard. pg-boss already coalesces
* concurrent cron ticks across multiple worker containers via the
* shared queue lease, but a single container that takes longer than
* one cron interval can pick up two jobs back-to-back when the
* second tick fires while the first pass is still running. The
* in-process `geoBackfillRunning` flag fans the second invocation
* out as a no-op log line instead of stacking two concurrent passes
* inside the same Node process — the next cron tick after the first
* completes will catch up the work the skipped pass would have done.
*/
let geoBackfillRunning = false;
async function handleGeoBackfill(jobs: Job<GeoBackfillPayload>[]) {
void jobs;
await withBackgroundEvent("job.geo_backfill", async (evt) => {
if (geoBackfillRunning) {
// Earlier pass still in flight; skip this tick. Idempotent + the
// next tick after the in-flight pass completes will pick up
// anything we miss here.
evt.addWarning(
"geo-backfill skipped — earlier pass still in flight inside the same worker process",
);
evt.addMeta("geo_backfill_skipped", true);
return;
}
geoBackfillRunning = true;
const p = getWorkerPrisma();
try {
const summary = await runGeoBackfill(p);
@@ -1204,6 +1232,8 @@ async function handleGeoBackfill(jobs: Job<GeoBackfillPayload>[]) {
// log and move on so a one-off resolver hiccup does not poison
// the queue and block the next pass.
evt.addWarning(`geo-backfill failed: ${err}`);
} finally {
geoBackfillRunning = false;
}
});
}
@@ -83,6 +83,7 @@ void queryRaw;
import {
ROLLUP_FULL_BACKFILL_QUEUE,
ROLLUP_RECOMPUTE_QUEUE,
_resetEnsureUserRollupsFreshInFlightForTests,
collapseToTypeDayKeys,
enqueueBootTimeRollupBackfill,
enqueueRollupRecompute,
@@ -101,6 +102,10 @@ beforeEach(() => {
findFirstMeasurement.mockReset();
bossSend.mockReset();
getGlobalBossMock.mockReset();
// v1.4.38 — clear the per-userId in-flight map so a previous test's
// resolved promise does not short-circuit the next test's recompute
// assertion.
_resetEnsureUserRollupsFreshInFlightForTests();
// Default: $transaction takes an array of pre-built promises (the
// populator passes `slice.map(prisma.measurementRollup.upsert(...))`)
// and returns them awaited.
@@ -291,6 +296,76 @@ describe("ensureUserRollupsFresh", () => {
expect(result.recomputed).toBe(false);
});
// v1.4.38 — concurrent callers for the same userId share one
// in-flight promise so a cold fan-out can never queue N parallel
// recompute runs against the same 90-day window.
it("dedups concurrent callers for the same userId onto one in-flight promise", async () => {
const rollupAt = new Date("2026-05-10T10:00:00.000Z");
const measurementAt = new Date("2026-05-10T12:00:00.000Z");
// Both round-trips inside the helper run twice (the helper issues
// a parallel `Promise.all` for the rollup-most-recent + measurement
// -most-recent). With dedup, those should fire ONCE across two
// concurrent callers.
findFirst.mockResolvedValue({ computedAt: rollupAt });
findFirstMeasurement.mockResolvedValue({
updatedAt: measurementAt,
measuredAt: measurementAt,
});
queryRawUnsafe.mockResolvedValue([]);
const [a, b, c] = await Promise.all([
ensureUserRollupsFresh("user-1"),
ensureUserRollupsFresh("user-1"),
ensureUserRollupsFresh("user-1"),
]);
expect(a.recomputed).toBe(true);
expect(b.recomputed).toBe(true);
expect(c.recomputed).toBe(true);
// The Postgres-side recompute fires exactly once across the three
// concurrent callers — proof the dedup map collapsed the fan-out.
expect(queryRawUnsafe).toHaveBeenCalledTimes(1);
// The findFirst probe fires exactly once per call type (rollup,
// measurement) across all three concurrent callers.
expect(findFirst).toHaveBeenCalledTimes(1);
expect(findFirstMeasurement).toHaveBeenCalledTimes(1);
});
it("clears the in-flight slot after resolution so the next call runs fresh", async () => {
const rollupAt = new Date("2026-05-10T10:00:00.000Z");
const measurementAt = new Date("2026-05-10T12:00:00.000Z");
findFirst.mockResolvedValue({ computedAt: rollupAt });
findFirstMeasurement.mockResolvedValue({
updatedAt: measurementAt,
measuredAt: measurementAt,
});
queryRawUnsafe.mockResolvedValue([]);
await ensureUserRollupsFresh("user-1");
await ensureUserRollupsFresh("user-1");
// Two serial calls => two recompute runs (no dedup short-circuit
// because the first call already resolved and dropped its slot).
expect(queryRawUnsafe).toHaveBeenCalledTimes(2);
});
it("clears the in-flight slot on rejection so the next call retries", async () => {
findFirst.mockRejectedValueOnce(new Error("pool exhausted"));
findFirstMeasurement.mockRejectedValueOnce(new Error("pool exhausted"));
const first = await ensureUserRollupsFresh("user-1");
expect(first.recomputed).toBe(false);
// Second call: probe succeeds, stale watermark triggers a recompute.
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,
});
queryRawUnsafe.mockResolvedValueOnce([]);
const second = await ensureUserRollupsFresh("user-1");
expect(second.recomputed).toBe(true);
});
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.
@@ -40,6 +40,20 @@ import {
hkIdentifierForType,
} from "./apple-health-mapping";
/**
* v1.4.38 — canonical cutoff for the nightly scheduled drain. Rows
* whose `measuredAt` is newer than `now() - DRAIN_CUMULATIVE_CUTOFF_HOURS`
* are excluded so today's still-in-flight Apple Watch syncs stay as
* per-sample rows in the user's "today" view. 36 hours covers the
* previous calendar day plus a generous trailing sync window for
* watches that weren't worn at midnight. The CLI and the admin route
* import the constant for visibility but deliberately pass `undefined`
* by default so an explicit one-shot drain collapses every row the
* operator points it at; pass the constant explicitly when mirroring
* the nightly behaviour from an interactive shell.
*/
export const DRAIN_CUMULATIVE_CUTOFF_HOURS = 36;
/** Per-(user, type, day) action summary. */
export interface DrainBucket {
userId: string;
@@ -296,6 +310,13 @@ export async function drainPerSampleCumulative(
const tz = user.timezone && user.timezone.length > 0 ? user.timezone : "Europe/Berlin";
log(`[drain] user=${user.id} tz=${tz}${dryRun ? " (dry-run)" : ""}`);
// v1.4.38 — per-user counters that mirror the existing aggregate
// totals. Lets the per-user COMPLETE log line carry useful
// numbers without re-walking the summary list later.
const beforeBucketsCollapsed = summary.totals.bucketsCollapsed;
const beforePerSampleDeleted = summary.totals.perSampleRowsDeleted;
const beforeDailyUpserted = summary.totals.dailyRowsUpserted;
for (const type of CUMULATIVE_HK_TYPES) {
const hkIdentifier = hkIdentifierForType(type);
if (!hkIdentifier) continue;
@@ -403,6 +424,22 @@ export async function drainPerSampleCumulative(
}
}
}
// v1.4.38 — per-user COMPLETE log line. Mirrors the START line on
// line 311 so an operator scanning the worker log can pair
// "drain started for user X" with "drain finished for user X"
// without scrolling through every per-type bucket. Counts are
// computed as the delta against the snapshot taken before the
// per-type loop started.
const userBucketsCollapsed =
summary.totals.bucketsCollapsed - beforeBucketsCollapsed;
const userPerSampleDeleted =
summary.totals.perSampleRowsDeleted - beforePerSampleDeleted;
const userDailyUpserted =
summary.totals.dailyRowsUpserted - beforeDailyUpserted;
log(
`[drain] user=${user.id} complete bucketsCollapsed=${userBucketsCollapsed} perSampleRowsDeleted=${userPerSampleDeleted} dailyRowsUpserted=${userDailyUpserted}${dryRun ? " (dry-run)" : ""}`,
);
}
log(
+69 -10
View File
@@ -199,16 +199,25 @@ export async function recomputeBucketsForMeasurement(
// WEEK / MONTH / YEAR — enqueue. Each granularity covers a range
// around the affected day so the worker only re-aggregates the
// bucket(s) the write could have changed.
for (const granularity of ASYNC_GRANULARITIES) {
const { from, to } = bucketSpan(measuredAt, granularity);
await enqueueRollupRecompute({
userId,
type,
granularity,
from,
to,
});
}
//
// v1.4.38 — the three `enqueueRollupRecompute` calls are independent
// (different singletonKey per granularity inside `enqueueRollupRecompute`)
// so fan them out via `Promise.all` instead of sequencing them with
// `await`. Halves the post-write critical path on every measurement
// create / update / delete; matters on iOS batch imports that fire
// hundreds of hooks back-to-back.
await Promise.all(
ASYNC_GRANULARITIES.map((granularity) => {
const { from, to } = bucketSpan(measuredAt, granularity);
return enqueueRollupRecompute({
userId,
type,
granularity,
from,
to,
});
}),
);
}
/**
@@ -555,6 +564,26 @@ function bucketSpan(
}
}
/**
* v1.4.38 — per-userId in-flight dedup. v1.4.37.1 fire-and-forgets the
* `ensureUserRollupsFresh` call from the read path so a stale 90-day
* recompute can no longer stall the response, but if a request DOES
* end up awaiting a synchronous recompute (worker code path, future
* caller without the fire-and-forget) and the same user has a cold
* fan-out hitting analytics + comprehensive + summaries within the
* same tick, the legacy shape would queue N parallel
* `recomputeUserRollups` invocations against the same 90-day window.
* The map below collapses those into one shared promise — every
* caller after the first re-uses the in-flight resolution and only
* one Postgres aggregate runs. Mirrors the `buildCoachSnapshot` dedup
* pattern that lives one layer up. Cleared in `finally` so a slow
* promise that eventually rejects does not poison the next request.
*/
const ensureUserRollupsFreshInFlight = new Map<
string,
Promise<{ recomputed: boolean }>
>();
/**
* Cheap read-side warm-up. Called from the analytics + comprehensive
* read paths so the rollup table stays current for downstream
@@ -572,9 +601,39 @@ function bucketSpan(
* here.
* - Best-effort: thrown errors are swallowed so the read path
* never fails because of a populator hiccup.
* - v1.4.38: concurrent callers for the same `userId` share one
* in-flight promise so a cold fan-out can never queue N parallel
* `recomputeUserRollups` runs against the same 90-day window.
*/
export async function ensureUserRollupsFresh(
userId: string,
): Promise<{ recomputed: boolean }> {
// v1.4.38 — share the in-flight promise across concurrent callers
// for the same userId. The map is keyed on `userId` because the
// helper has no other parameters; the next caller hits the
// resolved promise as soon as it lands. We delete the slot in
// `finally` so a slow rejected promise does not stay cached and
// poison the next request.
const existing = ensureUserRollupsFreshInFlight.get(userId);
if (existing) return existing;
const inflight = ensureUserRollupsFreshImpl(userId).finally(() => {
ensureUserRollupsFreshInFlight.delete(userId);
});
ensureUserRollupsFreshInFlight.set(userId, inflight);
return inflight;
}
/**
* Test-only handle on the in-flight map. Mirrors the
* `_resetTrustViolationWarningForTests` pattern in api-response.ts —
* production code never calls this.
*/
export function _resetEnsureUserRollupsFreshInFlightForTests(): void {
ensureUserRollupsFreshInFlight.clear();
}
async function ensureUserRollupsFreshImpl(
userId: string,
): Promise<{ recomputed: boolean }> {
try {
// Single round-trip — the most-recent DAY rollup for the user
@@ -0,0 +1,52 @@
/**
* v1.4.38 — drift-guard between the validation enum and the label-key
* map. The category enum lives in three places (Prisma schema,
* `MEDICATION_CATEGORY_VALUES` in the validation module, and the
* `MEDICATION_CATEGORY_KEYS` lookup inside `category-label.ts`). If a
* new category lands in the enum without a matching label key, the
* generic `<MedicationCard>` quietly falls back to `categoryOther`
* for the new bucket — a silent UX regression. Pin the invariant
* here so the test suite trips before the regression ships.
*
* Symmetric drift (label key present without an enum entry) is also
* undesirable — it's dead code, but more importantly it suggests the
* enum dropped a value without removing the lookup, which leaves the
* label map advertising a category the rest of the app no longer
* understands.
*/
import { describe, expect, it } from "vitest";
import { MEDICATION_CATEGORY_VALUES } from "@/lib/validations/medication";
import { getMedicationCategoryLabel } from "../category-label";
describe("medication category-label drift-guard", () => {
it("every MEDICATION_CATEGORY_VALUES entry resolves to a distinct label key", () => {
// Use a no-op translator that echoes the key back so we can
// distinguish "real key" from the OTHER fallback.
const echo = (key: string) => key;
const fallback = "medications.categoryOther";
for (const value of MEDICATION_CATEGORY_VALUES) {
const resolved = getMedicationCategoryLabel(value, echo);
if (value === "OTHER") {
expect(resolved).toBe(fallback);
continue;
}
// Any non-OTHER enum value that resolves to the OTHER key is a
// drift bug — the value made it into the enum without a
// matching label entry, so the card silently labels the
// category as "Sonstige".
expect(
resolved,
`enum value "${value}" falls back to categoryOther — add a key in MEDICATION_CATEGORY_KEYS to cover it`,
).not.toBe(fallback);
}
});
it("the fallback path still fires for an unknown category value", () => {
const echo = (key: string) => key;
expect(getMedicationCategoryLabel("NOT_A_REAL_CATEGORY", echo)).toBe(
"medications.categoryOther",
);
});
});
+46
View File
@@ -23,6 +23,7 @@ import {
formatInUserTz,
invalidateServerDefaultTimezone,
invalidateUserTimezone,
isNearUtc,
isValidTimezone,
resolveServerDefaultTimezone,
resolveUserTimezone,
@@ -261,6 +262,51 @@ describe("userDayKey", () => {
});
});
describe("isNearUtc", () => {
// Pin a summer-side instant so DST is on for Berlin (+2) and off for
// most southern-hemisphere zones — keeps the assertions readable.
const summer = new Date("2026-07-15T12:00:00.000Z");
// Winter-side instant so Berlin is on CET (+1).
const winter = new Date("2026-01-15T12:00:00.000Z");
it("returns true for UTC", () => {
expect(isNearUtc("UTC", summer)).toBe(true);
});
it("returns true for Europe/Berlin in both summer and winter", () => {
expect(isNearUtc("Europe/Berlin", summer)).toBe(true);
expect(isNearUtc("Europe/Berlin", winter)).toBe(true);
});
it("returns true for the ±3h boundary zones", () => {
// Europe/Moscow = +3 year-round.
expect(isNearUtc("Europe/Moscow", summer)).toBe(true);
// Atlantic/Azores = -1 in winter, 0 in summer.
expect(isNearUtc("Atlantic/Azores", summer)).toBe(true);
});
it("returns false for zones more than 3 hours from UTC", () => {
// Pacific/Honolulu = -10 year-round.
expect(isNearUtc("Pacific/Honolulu", summer)).toBe(false);
// Asia/Tokyo = +9 year-round.
expect(isNearUtc("Asia/Tokyo", summer)).toBe(false);
// America/New_York = -4 in summer, -5 in winter.
expect(isNearUtc("America/New_York", summer)).toBe(false);
// Pacific/Auckland = +13 in southern summer (Jan), +12 in May.
expect(isNearUtc("Pacific/Auckland", winter)).toBe(false);
});
it("defaults to near-UTC when the zone string is invalid", () => {
// Invalid zones fall back to Europe/Berlin, which is near-UTC.
expect(isNearUtc("Mars/Olympus_Mons", summer)).toBe(true);
});
it("uses the current instant when `now` is omitted", () => {
// Pure smoke — exercises the default parameter branch.
expect(typeof isNearUtc("Europe/Berlin")).toBe("boolean");
});
});
describe("DEFAULT_TIMEZONE", () => {
it("is Europe/Berlin", () => {
expect(DEFAULT_TIMEZONE).toBe("Europe/Berlin");
+32
View File
@@ -67,6 +67,38 @@ export function detectBrowserTimezone(): string {
}
}
/**
* v1.4.38 W-A — cross-tz fast-path runtime guard.
*
* Returns `true` when the supplied IANA zone is within ±3 hours of UTC
* at the supplied instant. Used by the analytics fast-paths
* (`bp-in-target-fast-path`, `correlations-fast-path`) to gate the
* rollup read-swap: those helpers key rollup rows on UTC-midnight
* `bucketStart` and pair them with per-event streams keyed in the
* user's local timezone. For Berlin (+1/+2) the day-key on the rollup
* and the day-key on the per-event row line up; for Honolulu (-10)
* they slip by a calendar day. Three hours is a conservative pin —
* the worst-case slip inside the window is the wall-clock hour-of-day
* the user happens to log at, and three hours covers every European
* zone plus the western edge of Mid-Atlantic / eastern edge of the
* Americas.
*
* Falls back to "near-UTC" (i.e. returns `true`) when the zone string
* is invalid — the resolver layer is already defensive about junk
* values, and a `true` here means the caller takes the rollup
* fast-path which is the safer default for the canonical Berlin
* tenant.
*
* The `now` argument lets callers honour DST transitions; the offset
* of `Europe/Berlin` is +1 in January and +2 in July. Defaults to
* "now" so most callers can `isNearUtc(userTz)`.
*/
export function isNearUtc(tz: string, now: Date = new Date()): boolean {
const safeTz = isValidTimezone(tz) ? tz : DEFAULT_TIMEZONE;
const offsetMinutes = tzOffsetMinutes(now, safeTz);
return Math.abs(offsetMinutes) <= 3 * 60;
}
export type FormatInUserTzShape =
| "iso-with-offset"
| "wall-clock"
+1
View File
@@ -47,6 +47,7 @@ import {
export {
DEFAULT_TIMEZONE,
isValidTimezone,
isNearUtc,
listSupportedTimezones,
detectBrowserTimezone,
formatInUserTz,
@@ -235,4 +235,40 @@ describe("measurement validation", () => {
expect(parsed.success).toBe(false);
});
});
// v1.4.38 — the per-day drill-down branch always returns at most
// 1000 rows (one pathological phone-only stepCount day). Push the
// cap into the validator so a caller asking for more sees a 422
// instead of a silent server-side Math.min clamp.
describe("listMeasurementsSchema — v1.4.38 dayKey limit ceiling", () => {
it("accepts limit=1000 alongside a dayKey", () => {
const parsed = listMeasurementsSchema.safeParse({
type: "ACTIVITY_STEPS",
dayKey: "2026-05-15",
limit: 1000,
});
expect(parsed.success).toBe(true);
});
it("rejects limit=1001 when dayKey is set (drill-down hard cap)", () => {
const parsed = listMeasurementsSchema.safeParse({
type: "ACTIVITY_STEPS",
dayKey: "2026-05-15",
limit: 1001,
});
expect(parsed.success).toBe(false);
if (!parsed.success) {
expect(parsed.error.issues[0].message).toMatch(/<=\s*1000/);
expect(parsed.error.issues[0].path).toEqual(["limit"]);
}
});
it("accepts limit=5000 when dayKey is omitted (legacy ceiling)", () => {
const parsed = listMeasurementsSchema.safeParse({
type: "ACTIVITY_STEPS",
limit: 5000,
});
expect(parsed.success).toBe(true);
});
});
});
+13 -1
View File
@@ -325,7 +325,19 @@ export const listMeasurementsSchema = z
"offset is not supported with groupBy=day or dayKey; use a smaller window or omit offset",
path: ["offset"],
},
);
)
// v1.4.38 — surface the drill-down cap on the validator so a caller
// asking for more than 1000 rows on a per-day branch sees a 422
// instead of a silent `Math.min(limit, 1000)` clamp inside the
// route. The drill-down returns at most one phone-only stepCount
// day's worth of per-sample chunks; 1000 covers the pathological
// case with room to spare. Callers that want a wider window should
// omit `dayKey` and use `from` / `to` instead.
.refine(({ limit, dayKey }) => !(dayKey != null && limit > 1000), {
message:
"limit must be <= 1000 when dayKey is set; drill-down responses are capped at 1000 rows",
path: ["limit"],
});
export const createBatchMeasurementSchema = z.object({
measurements: z.array(createMeasurementSchema).min(1).max(5),