From bd8a4e7990d9371f230af6835de11be5c4199f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Bombeck?= Date: Tue, 2 Jun 2026 13:49:52 +0200 Subject: [PATCH] refactor(insights): type-check the generic metric previous-context scope MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The generic metric-status generator read its previous-context row through an `as never` cast, because `getPreviousInsightContext` was typed to the seven specialised scopes only. Widen the helper's `scope` param to a `PreviousContextScope` union that also accepts the `metric:-status` scope form, so the generic call type-checks honestly instead of silently disabling type checking on the argument. No behaviour change — the helper only interpolates the scope into the audit action. Co-Authored-By: Claude Opus 4.8 (1M context) (cherry picked from commit 899d59b6fa219a14ba39eacded2e042f0f95c2c5) --- src/lib/insights/memory.ts | 12 +++++++++++- src/lib/insights/metric-status.ts | 6 ++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/lib/insights/memory.ts b/src/lib/insights/memory.ts index b9277dd69..450c74df4 100644 --- a/src/lib/insights/memory.ts +++ b/src/lib/insights/memory.ts @@ -33,6 +33,16 @@ export type InsightScope = | "medication-compliance-status" | "mood-status"; +/** + * v1.8.7.1 — the generic per-HealthKit-metric assessments key their + * previous-context rows under `insights.metric:-status.`, the + * same `-status` action shape the seven specialised scopes use. Accept + * that scope form alongside the seven so the generic generator can read + * its comparison row without an `as never` cast that would silently + * disable type checking on the argument. + */ +export type PreviousContextScope = InsightScope | `metric:${string}-status`; + export interface PreviousInsightContext { /** ISO timestamp the previous analysis was rendered. */ generatedAt: string; @@ -59,7 +69,7 @@ export interface PreviousInsightContext { */ export async function getPreviousInsightContext( userId: string, - scope: InsightScope, + scope: PreviousContextScope, locale: "de" | "en", minAgeHours: number = 12, ): Promise { diff --git a/src/lib/insights/metric-status.ts b/src/lib/insights/metric-status.ts index 88a8d30dc..855d4a18c 100644 --- a/src/lib/insights/metric-status.ts +++ b/src/lib/insights/metric-status.ts @@ -280,8 +280,10 @@ export async function generateMetricStatus(args: { args.userId, // memory.ts keys previous context by the `insights..` // action; the generic scope (`metric:-status`) carries the same - // `-status` suffix so the comparison row reads back correctly. - `${scope}-status` as never, + // `-status` suffix so the comparison row reads back correctly. The + // `getPreviousInsightContext` param is typed to accept this scope form + // (`PreviousContextScope`), so the argument type-checks honestly. + `${scope}-status`, locale, 12, );