feat(insights): persist correlation patterns

This commit is contained in:
Marc-André Bombeck committed 2026-07-28 14:01:11 +02:00
1 parent 8ece6f39cc
commit 666a85d311
53 files changed
+2129 -55

No files matched your search

+7
View File
@@ -261,6 +261,13 @@ export function invalidateUserMedicationListLayout(userId: string): void {
caches.medicationListLayout.deleteByPrefix(userId);
}
/** Invalidate surfaces whose server decision includes pattern dismissal. */
export function invalidateUserCorrelationPatterns(userId: string): void {
caches.analytics.deleteByPrefix(`${userId}|`);
caches.moodInsights.deleteByPrefix(userId);
caches.insightsDerived.deleteByPrefix(`${userId}|`);
}
/**
* v1.16.9 — full per-user cache eviction for the wipe / restore paths.
* A data wipe or a backup restore replaces (or removes) every row the
@@ -19,6 +19,7 @@ export interface CustomMetricRow {
targetHigh: number | null;
decimals: number | null;
description: string | null;
correlationEnabled: boolean;
createdAt: Date;
updatedAt: Date;
}
@@ -49,6 +50,7 @@ export function serialiseCustomMetric(row: CustomMetricRow) {
targetHigh: row.targetHigh,
decimals: row.decimals,
description: row.description,
correlationEnabled: row.correlationEnabled,
createdAt: row.createdAt.toISOString(),
updatedAt: row.updatedAt.toISOString(),
};
@@ -230,6 +230,7 @@ function makePrisma() {
targetHigh: null,
decimals: 1,
description: "Right hand, before breakfast",
correlationEnabled: true,
createdAt: new Date("2026-07-01T00:00:00.000Z"),
updatedAt: new Date("2026-07-19T00:00:00.000Z"),
deletedAt: null,
@@ -246,6 +247,32 @@ function makePrisma() {
},
]),
},
correlationPattern: {
findMany: vi.fn().mockResolvedValue([
{
id: "pattern-1",
userId: "user-1",
canonicalKey: `p1:${"a".repeat(64)}`,
family: "DISCOVERY_RETROSPECTIVE",
factorKey: "CUSTOM_METRIC:metric-1",
outcomeKey: "RESTING_HEART_RATE",
lagDays: 1,
sampleSize: 42,
effectSize: 0.34,
pValue: 0.01,
qValue: 0.04,
evidenceHash: "b".repeat(64),
isCurrent: true,
lastComputedAt: new Date("2026-07-19T08:00:00.000Z"),
dismissedAt: new Date("2026-07-19T09:00:00.000Z"),
dismissedEvidenceHash: "b".repeat(64),
dismissedEffectSize: 0.34,
dismissedSampleSize: 42,
createdAt: new Date("2026-07-19T08:00:00.000Z"),
updatedAt: new Date("2026-07-19T09:00:00.000Z"),
},
]),
},
};
}
@@ -551,6 +578,7 @@ describe("buildFullBackupPayload — section keys reach the payload", () => {
// from the builders.
"healthProfile",
"customMetrics",
"correlationPatterns",
"intradayProfiles",
];
@@ -662,6 +690,7 @@ describe("buildFullBackupPayload — profile and custom metrics", () => {
targetHigh: null,
decimals: 1,
description: "Right hand, before breakfast",
correlationEnabled: true,
deletedAt: null,
entries: [
expect.objectContaining({
@@ -676,6 +705,17 @@ describe("buildFullBackupPayload — profile and custom metrics", () => {
]);
expect(counts.customMetrics).toBe(1);
expect(counts.customMetricEntries).toBe(1);
expect(payload.correlationPatterns).toEqual([
expect.objectContaining({
id: "pattern-1",
canonicalKey: `p1:${"a".repeat(64)}`,
factorKey: "CUSTOM_METRIC:metric-1",
outcomeKey: "RESTING_HEART_RATE",
dismissedAt: "2026-07-19T09:00:00.000Z",
dismissedEvidenceHash: "b".repeat(64),
}),
]);
expect(counts.correlationPatterns).toBe(1);
});
it("excludes deleted metrics from a portable export", async () => {
+2
View File
@@ -101,6 +101,7 @@ export const BACKED_UP_MODELS = [
// ── Custom metrics ────────────────────────────────────────────────────────
"CustomMetric",
"CustomMetricEntry",
"CorrelationPattern",
// ── Activity and nutrition ────────────────────────────────────────────────
"Workout",
@@ -202,6 +203,7 @@ export const TWO_ENDED_MODELS: readonly string[] = [
"CycleSymptomLink",
"CustomMetric",
"CustomMetricEntry",
"CorrelationPattern",
"Workout",
"NutrientIntakeDay",
"InboundDocument",
+3 -4
View File
@@ -213,10 +213,9 @@ export async function buildFullBackupPayload(
buildRecordsBackupSection(prisma, userId, {
purpose: disasterRecovery ? "disaster-recovery" : "portable-export",
}),
// Durable self-context + user-defined metrics. Both were classified as
// carried when the backup plan was written and carried by nothing; the
// metrics are the live loss, since a series the person defined themselves
// is the one kind no integration can re-sync.
// Durable self-context, user-defined metrics, and persisted pattern
// decisions. These account-owned rows cannot be reconstructed from an
// integration after restore.
buildProfileBackupSection(prisma, userId, {
purpose: disasterRecovery ? "disaster-recovery" : "portable-export",
}),
+117 -13
View File
@@ -1,12 +1,11 @@
/**
* Durable self-context and user-defined metrics — both ends in one file.
* Durable self-context, user-defined metrics, and persisted pattern decisions,
* with both backup ends in one file.
*
* Two models classified `BACKED_UP` since `backup-plan.ts` was written and
* carried by nothing: `UserHealthProfile` (what a person told the app is
* durably true of them) and `CustomMetric` / `CustomMetricEntry` (a series the
* person defined themselves, which by definition no integration can re-sync).
* The custom-metric gap is live data loss today: a restore rebuilt the account
* without the metric or a single one of its readings and reported success.
* These account-owned models cannot be reconstructed from an integration:
* `UserHealthProfile`, `CustomMetric` / `CustomMetricEntry`, and
* `CorrelationPattern`. The last model carries stable finding identity and the
* evidence baseline that makes a dismissal survive recomputation and restore.
*
* The builder and the restore live together deliberately, the way
* `src/lib/cycle/backup.ts` does. A reader asking "is this carried at both
@@ -98,6 +97,7 @@ export interface CustomMetricBackupEntry {
targetHigh: number | null;
decimals: number | null;
description: string | null;
correlationEnabled: boolean;
createdAt?: string;
updatedAt?: string;
/** Tombstone, DR payloads only — a portable export never carries deleted rows. */
@@ -105,15 +105,40 @@ export interface CustomMetricBackupEntry {
entries: CustomMetricEntryBackupEntry[];
}
/** Persisted accepted correlation evidence and its dismissal decision. */
export interface CorrelationPatternBackupEntry {
id?: string;
canonicalKey: string;
family: string;
factorKey: string;
outcomeKey: string;
lagDays: number;
sampleSize: number;
effectSize: number;
pValue: number;
qValue: number | null;
evidenceHash: string;
isCurrent: boolean;
lastComputedAt: string;
dismissedAt: string | null;
dismissedEvidenceHash: string | null;
dismissedEffectSize: number | null;
dismissedSampleSize: number | null;
createdAt?: string;
updatedAt?: string;
}
export interface ProfileBackupSection {
healthProfile: HealthProfileBackupEntry | null;
customMetrics: CustomMetricBackupEntry[];
correlationPatterns: CorrelationPatternBackupEntry[];
}
export interface ProfileBackupCounts {
healthProfile: number;
customMetrics: number;
customMetricEntries: number;
correlationPatterns: number;
}
/**
@@ -155,13 +180,16 @@ function toBase64(buf: Uint8Array | null): string | null {
* `buildCycleBackupSection` and `buildRecordsBackupSection`.
*/
export async function buildProfileBackupSection(
prisma: Pick<PrismaClient, "userHealthProfile" | "customMetric">,
prisma: Pick<
PrismaClient,
"userHealthProfile" | "customMetric" | "correlationPattern"
>,
userId: string,
options: ProfileBackupOptions = {},
): Promise<ProfileBackupSection> {
const disasterRecovery = options.purpose === "disaster-recovery";
const [profileRow, metricRows] = await Promise.all([
const [profileRow, metricRows, patternRows] = await Promise.all([
prisma.userHealthProfile.findUnique({ where: { userId } }),
prisma.customMetric.findMany({
// A portable export never resurrects a tombstone; a DR payload restores
@@ -169,12 +197,13 @@ export async function buildProfileBackupSection(
where: disasterRecovery ? { userId } : { userId, deletedAt: null },
orderBy: { name: "asc" },
include: {
// `CustomMetricEntry` carries no tombstone of its own — a deleted
// metric cascades its readings away — so both purposes read the same
// set and only the parent filter differs.
entries: { orderBy: { measuredAt: "asc" } },
},
}),
prisma.correlationPattern.findMany({
where: { userId },
orderBy: { canonicalKey: "asc" },
}),
]);
const healthProfile: HealthProfileBackupEntry | null = profileRow
@@ -230,6 +259,7 @@ export async function buildProfileBackupSection(
targetHigh: metric.targetHigh,
decimals: metric.decimals,
description: metric.description,
correlationEnabled: metric.correlationEnabled,
entries: metric.entries.map((entry) => ({
...(disasterRecovery
? {
@@ -244,7 +274,35 @@ export async function buildProfileBackupSection(
})),
}));
return { healthProfile, customMetrics };
const correlationPatterns: CorrelationPatternBackupEntry[] = patternRows.map(
(pattern) => ({
...(disasterRecovery
? {
id: pattern.id,
createdAt: pattern.createdAt.toISOString(),
updatedAt: pattern.updatedAt.toISOString(),
}
: {}),
canonicalKey: pattern.canonicalKey,
family: pattern.family,
factorKey: pattern.factorKey,
outcomeKey: pattern.outcomeKey,
lagDays: pattern.lagDays,
sampleSize: pattern.sampleSize,
effectSize: pattern.effectSize,
pValue: pattern.pValue,
qValue: pattern.qValue,
evidenceHash: pattern.evidenceHash,
isCurrent: pattern.isCurrent,
lastComputedAt: pattern.lastComputedAt.toISOString(),
dismissedAt: pattern.dismissedAt?.toISOString() ?? null,
dismissedEvidenceHash: pattern.dismissedEvidenceHash,
dismissedEffectSize: pattern.dismissedEffectSize,
dismissedSampleSize: pattern.dismissedSampleSize,
}),
);
return { healthProfile, customMetrics, correlationPatterns };
}
/** Row counts for the audit trail, mirroring the other section counters. */
@@ -258,6 +316,7 @@ export function countProfileBackupSection(
(sum, metric) => sum + metric.entries.length,
0,
),
correlationPatterns: section.correlationPatterns.length,
};
}
@@ -265,6 +324,7 @@ export function countProfileBackupSection(
export interface ProfileRestoreCleared {
healthProfile: number;
customMetrics: number;
correlationPatterns: number;
}
/**
@@ -278,6 +338,7 @@ export interface ProfileRestoreCleared {
export interface ProfileRestoreInput {
healthProfile: HealthProfileBackupEntry | null;
customMetrics: CustomMetricBackupEntry[];
correlationPatterns: CorrelationPatternBackupEntry[];
}
/**
@@ -337,6 +398,9 @@ export async function restoreProfileData(
const metricsCleared = await tx.customMetric.deleteMany({
where: { userId: ownerId },
});
const patternsCleared = await tx.correlationPattern.deleteMany({
where: { userId: ownerId },
});
if (payload.healthProfile) {
const p = payload.healthProfile;
@@ -406,6 +470,7 @@ export async function restoreProfileData(
targetHigh: metric.targetHigh,
decimals: metric.decimals,
description: metric.description,
correlationEnabled: metric.correlationEnabled,
...(metric.createdAt ? { createdAt: new Date(metric.createdAt) } : {}),
...(metric.updatedAt ? { updatedAt: new Date(metric.updatedAt) } : {}),
deletedAt: metric.deletedAt ? new Date(metric.deletedAt) : null,
@@ -426,8 +491,47 @@ export async function restoreProfileData(
});
}
const seenPatternKeys = new Set<string>();
for (const pattern of payload.correlationPatterns) {
if (seenPatternKeys.has(pattern.canonicalKey)) {
throw new Error(
`Duplicate correlation pattern key: ${pattern.canonicalKey}`,
);
}
seenPatternKeys.add(pattern.canonicalKey);
await tx.correlationPattern.create({
data: {
...(pattern.id ? { id: pattern.id } : {}),
userId: ownerId,
canonicalKey: pattern.canonicalKey,
family: pattern.family,
factorKey: pattern.factorKey,
outcomeKey: pattern.outcomeKey,
lagDays: pattern.lagDays,
sampleSize: pattern.sampleSize,
effectSize: pattern.effectSize,
pValue: pattern.pValue,
qValue: pattern.qValue,
evidenceHash: pattern.evidenceHash,
isCurrent: pattern.isCurrent,
lastComputedAt: new Date(pattern.lastComputedAt),
dismissedAt: pattern.dismissedAt ? new Date(pattern.dismissedAt) : null,
dismissedEvidenceHash: pattern.dismissedEvidenceHash,
dismissedEffectSize: pattern.dismissedEffectSize,
dismissedSampleSize: pattern.dismissedSampleSize,
...(pattern.createdAt
? { createdAt: new Date(pattern.createdAt) }
: {}),
...(pattern.updatedAt
? { updatedAt: new Date(pattern.updatedAt) }
: {}),
},
});
}
return {
healthProfile: profileCleared.count,
customMetrics: metricsCleared.count,
correlationPatterns: patternsCleared.count,
};
}
@@ -3,16 +3,20 @@ import type { SleepStage } from "@/generated/prisma/client";
const measurementFindMany = vi.fn();
const moodFindMany = vi.fn();
const customMetricFindMany = vi.fn();
vi.mock("@/lib/db", () => ({
prisma: {
measurement: { findMany: (a: unknown) => measurementFindMany(a) },
moodEntry: { findMany: (a: unknown) => moodFindMany(a) },
customMetric: { findMany: (a: unknown) => customMetricFindMany(a) },
},
}));
import {
buildMeasurementDailySeries,
fetchMeasurementWindowSeries,
fetchCustomMetricBehaviourSeries,
MAX_CUSTOM_CORRELATION_CHANNELS,
fetchMoodWindowSeries,
toDailyMeans,
type MeasurementSeriesRow,
@@ -238,3 +242,73 @@ describe("fetchMoodWindowSeries — desc+cap+resort", () => {
);
});
});
describe("fetchCustomMetricBehaviourSeries", () => {
beforeEach(() => {
customMetricFindMany.mockReset();
});
it("reads only active opted-in owner metrics with a deterministic cap", async () => {
customMetricFindMany.mockResolvedValue([]);
await fetchCustomMetricBehaviourSeries(
"owner-1",
"Europe/Berlin",
new Date("2026-01-01T00:00:00.000Z"),
);
expect(customMetricFindMany).toHaveBeenCalledWith(
expect.objectContaining({
where: {
userId: "owner-1",
deletedAt: null,
correlationEnabled: true,
},
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
take: MAX_CUSTOM_CORRELATION_CHANNELS,
}),
);
});
it("keeps metric identity, drops historical units, and averages same-local-day values", async () => {
customMetricFindMany.mockResolvedValue([
{
id: "grip-id",
name: "Grip strength",
unit: "kg",
entries: [
{
value: 40,
unit: "kg",
measuredAt: new Date("2026-07-01T08:00:00.000Z"),
},
{
value: 44,
unit: "kg",
measuredAt: new Date("2026-07-01T18:00:00.000Z"),
},
{
value: 100,
unit: "lb",
measuredAt: new Date("2026-07-01T20:00:00.000Z"),
},
],
},
]);
const result = await fetchCustomMetricBehaviourSeries(
"owner-1",
"UTC",
new Date("2026-01-01T00:00:00.000Z"),
);
expect(result).toEqual([
{
key: "CUSTOM_METRIC:grip-id",
label: "Grip strength",
role: "behaviour",
points: [{ day: "2026-07-01", value: 42 }],
},
]);
});
});
@@ -0,0 +1,203 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
interface StoredPattern {
id: string;
userId: string;
canonicalKey: string;
family: string;
factorKey: string;
outcomeKey: string;
lagDays: number;
sampleSize: number;
effectSize: number;
pValue: number;
qValue: number | null;
evidenceHash: string;
isCurrent: boolean;
lastComputedAt: Date;
dismissedAt: Date | null;
dismissedEvidenceHash: string | null;
dismissedEffectSize: number | null;
dismissedSampleSize: number | null;
}
const rows = new Map<string, StoredPattern>();
const correlationPattern = {
findMany: vi.fn(
async ({
where,
}: {
where: { userId: string; canonicalKey: { in: string[] } };
}) =>
[...rows.values()].filter(
(row) =>
row.userId === where.userId &&
where.canonicalKey.in.includes(row.canonicalKey),
),
),
updateMany: vi.fn(
async ({
where,
data,
}: {
where: Record<string, unknown>;
data: { isCurrent: boolean };
}) => {
let count = 0;
for (const row of rows.values()) {
const notIn = (where.canonicalKey as { notIn?: string[] } | undefined)
?.notIn;
if (
row.userId === where.userId &&
row.family === where.family &&
row.isCurrent === where.isCurrent &&
(!notIn || !notIn.includes(row.canonicalKey))
) {
row.isCurrent = data.isCurrent;
count += 1;
}
}
return { count };
},
),
upsert: vi.fn(
async ({
where,
create,
update,
}: {
where: { userId_canonicalKey: { userId: string; canonicalKey: string } };
create: Omit<
StoredPattern,
| "id"
| "dismissedAt"
| "dismissedEvidenceHash"
| "dismissedEffectSize"
| "dismissedSampleSize"
>;
update: Partial<StoredPattern>;
}) => {
const key = `${where.userId_canonicalKey.userId}:${where.userId_canonicalKey.canonicalKey}`;
const prior = rows.get(key);
if (prior) {
Object.assign(prior, update);
return prior;
}
const created: StoredPattern = {
id: `pattern-${rows.size + 1}`,
...create,
dismissedAt: null,
dismissedEvidenceHash: null,
dismissedEffectSize: null,
dismissedSampleSize: null,
};
rows.set(key, created);
return created;
},
),
};
vi.mock("@/lib/db", () => ({
prisma: {
$transaction: (
fn: (tx: { correlationPattern: typeof correlationPattern }) => unknown,
) => fn({ correlationPattern }),
},
}));
import {
canonicalPatternKey,
isMaterialEvidenceChange,
PATTERN_FAMILIES,
syncAcceptedPatterns,
} from "@/lib/insights/correlation-patterns";
const baseEvidence = {
factorKey: "TAG:alcohol",
outcomeKey: "CROSSTAB:RESTING_HEART_RATE",
lagDays: 1,
sampleSize: 40,
effectSize: 0.3,
pValue: 0.01,
qValue: 0.04,
};
describe("correlation pattern identity and dismissal", () => {
beforeEach(() => {
rows.clear();
vi.clearAllMocks();
});
it("uses the same canonical identity across recomputation", () => {
expect(canonicalPatternKey("factor", "outcome", 1)).toBe(
canonicalPatternKey("factor", "outcome", 1),
);
expect(canonicalPatternKey("factor", "outcome", 0)).not.toBe(
canonicalPatternKey("factor", "outcome", 1),
);
});
it("retains an unchanged dismissal, resurfaces material evidence, and retires stale findings", async () => {
const first = await syncAcceptedPatterns({
userId: "owner-1",
family: PATTERN_FAMILIES.moodTagCrosstab,
accepted: [baseEvidence],
computedAt: new Date("2026-07-01T00:00:00.000Z"),
});
const decision = [...first.values()][0];
const row = [...rows.values()][0];
row.dismissedAt = new Date("2026-07-02T00:00:00.000Z");
row.dismissedEvidenceHash = row.evidenceHash;
row.dismissedEffectSize = row.effectSize;
row.dismissedSampleSize = row.sampleSize;
const unchanged = await syncAcceptedPatterns({
userId: "owner-1",
family: PATTERN_FAMILIES.moodTagCrosstab,
accepted: [{ ...baseEvidence, sampleSize: 45, effectSize: 0.35 }],
});
expect(unchanged.get(decision.canonicalKey)?.dismissed).toBe(true);
const changed = await syncAcceptedPatterns({
userId: "owner-1",
family: PATTERN_FAMILIES.moodTagCrosstab,
accepted: [{ ...baseEvidence, sampleSize: 50, effectSize: -0.2 }],
});
expect(changed.get(decision.canonicalKey)?.dismissed).toBe(false);
expect(row.dismissedAt).toBeNull();
await syncAcceptedPatterns({
userId: "owner-1",
family: PATTERN_FAMILIES.moodTagCrosstab,
accepted: [],
});
expect(row.isCurrent).toBe(false);
});
it("defines material movement deterministically", () => {
expect(
isMaterialEvidenceChange({
currentEffectSize: 0.39,
currentSampleSize: 49,
dismissedEffectSize: 0.3,
dismissedSampleSize: 40,
}),
).toBe(false);
expect(
isMaterialEvidenceChange({
currentEffectSize: 0.4,
currentSampleSize: 40,
dismissedEffectSize: 0.3,
dismissedSampleSize: 40,
}),
).toBe(true);
expect(
isMaterialEvidenceChange({
currentEffectSize: 0.3,
currentSampleSize: 50,
dismissedEffectSize: 0.3,
dismissedSampleSize: 40,
}),
).toBe(true);
});
});
@@ -5,9 +5,21 @@ vi.mock("@/lib/db", () => ({
user: { findUnique: vi.fn() },
measurement: { findMany: vi.fn() },
moodEntry: { findMany: vi.fn() },
medication: { findMany: vi.fn() },
medicationIntakeEvent: { findMany: vi.fn() },
illnessEpisode: { findMany: vi.fn() },
illnessDayLog: { findMany: vi.fn() },
environmentContext: { findMany: vi.fn() },
customMetric: { findMany: vi.fn() },
},
}));
vi.mock("@/lib/insights/correlation-patterns", () => ({
PATTERN_FAMILIES: { discoveryRetrospective: "DISCOVERY_RETROSPECTIVE" },
syncAcceptedPatterns: vi.fn().mockResolvedValue(new Map()),
decisionForEvidence: vi.fn().mockReturnValue(null),
}));
import { prisma } from "@/lib/db";
import { getRelevantCorrelationsForMetric } from "../metric-correlation-context";
@@ -17,6 +29,14 @@ beforeEach(() => {
timezone: "Europe/Berlin",
} as never);
vi.mocked(prisma.moodEntry.findMany).mockResolvedValue([] as never);
vi.mocked(prisma.medication.findMany).mockResolvedValue([] as never);
vi.mocked(prisma.medicationIntakeEvent.findMany).mockResolvedValue(
[] as never,
);
vi.mocked(prisma.illnessEpisode.findMany).mockResolvedValue([] as never);
vi.mocked(prisma.illnessDayLog.findMany).mockResolvedValue([] as never);
vi.mocked(prisma.environmentContext.findMany).mockResolvedValue([] as never);
vi.mocked(prisma.customMetric.findMany).mockResolvedValue([] as never);
});
describe("getRelevantCorrelationsForMetric", () => {
@@ -1090,11 +1090,13 @@ describe("computeTagMetricCrosstab", () => {
type: string,
presentValue: number,
absentValue: number,
tag: StructuredTagRef = ALCOHOL_TAG,
) {
const entries: MoodAggregateEntry[] = [];
const measurements: CrossMetricMeasurement[] = [];
for (let i = 1; i <= 12; i++) {
entries.push(structuredEntry(i, 3, [ALCOHOL_TAG]));
entries.push(structuredEntry(i, 3, [tag]));
// All built-in binary tags use this same branch and FDR family.
measurements.push(
meas(i - 1, type, presentValue + (i % 2 === 0 ? 2 : -2)),
);
@@ -1149,6 +1151,35 @@ describe("computeTagMetricCrosstab", () => {
expect(row!.delta).toBeCloseTo(-17, 0);
});
it.each([
["alcohol", "mood.tag.alcohol", "Wine"],
["caffeine", "mood.tag.caffeine", "Coffee"],
["nicotine", "mood.tag.nicotine", "Cigarette"],
])(
"runs the built-in %s tag through the same next-day crosstab and FDR family",
(key, labelKey, icon) => {
const { entries, measurements } = nextDayFixture(
"RESTING_HEART_RATE",
62,
54,
{ key, categoryKey: "health", labelKey, icon },
);
const rows = computeTagMetricCrosstab({
entries,
measurements,
now: NOW,
});
const row = rows.find(
(candidate) =>
candidate.tag === key &&
candidate.metricKey === "nextDayRestingHeartRate",
);
expect(row).toBeDefined();
expect(row!.mode).toBe("nextDay");
expect(row!.qValue).toBeLessThanOrEqual(CROSSTAB_FDR_Q);
},
);
it("reads both overnight vitals from the fetched channel set", () => {
// The fetch filter is derived from the map, so a metric added to
// CROSSTAB_METRICS without a channel to read it from is impossible.
@@ -6,6 +6,7 @@ vi.mock("@/lib/db", () => ({
// v1.18.11 (P6) — the input gate / fingerprint probe salient inputs.
measurement: { groupBy: vi.fn() },
moodEntry: { aggregate: vi.fn() },
customMetric: { findMany: vi.fn() },
},
}));
@@ -40,6 +41,7 @@ function cacheRow(details: Record<string, unknown>, createdAt = new Date()) {
beforeEach(() => {
vi.resetAllMocks();
vi.mocked(prisma.customMetric.findMany).mockResolvedValue([] as never);
});
describe("isTimeoutStub", () => {
@@ -493,6 +495,44 @@ describe("computeStatusInputFingerprint (v1.18.11 P6)", () => {
expect(after).not.toBe(before);
});
it("folds opted-in custom metric configuration and entries into the fingerprint", async () => {
const t0 = new Date("2026-05-30T08:00:00.000Z");
vi.mocked(prisma.measurement.groupBy).mockResolvedValue([] as never);
vi.mocked(prisma.customMetric.findMany)
.mockResolvedValueOnce([] as never)
.mockResolvedValueOnce([
{
id: "custom-1",
unit: "kg",
updatedAt: t0,
_count: { entries: 1 },
entries: [{ measuredAt: t0, unit: "kg" }],
},
] as never);
const before = await computeStatusInputFingerprint({
userId: "u1",
types: ["WEIGHT"],
includeCorrelationChannels: true,
});
const after = await computeStatusInputFingerprint({
userId: "u1",
types: ["WEIGHT"],
includeCorrelationChannels: true,
});
expect(after).not.toBe(before);
expect(prisma.customMetric.findMany).toHaveBeenLastCalledWith(
expect.objectContaining({
where: {
userId: "u1",
deletedAt: null,
correlationEnabled: true,
},
}),
);
});
it("widens the grouped query by the discovery channels only when asked", async () => {
vi.mocked(prisma.measurement.groupBy).mockResolvedValue([] as never);
await computeStatusInputFingerprint({
@@ -250,6 +250,56 @@ export async function fetchEnvironmentSeries(
});
}
/** Explicit deterministic ceiling on opt-in custom behaviour channels. */
export const MAX_CUSTOM_CORRELATION_CHANNELS = 8;
export const CUSTOM_METRIC_CHANNEL_PREFIX = "CUSTOM_METRIC:";
/**
* Read only active, owner-scoped custom metrics that explicitly opted into
* discovery. Metrics are selected oldest-first with an id tie-break and capped
* before entries are read, bounding both channel count and pair-space growth.
* Historical rows whose unit snapshot differs from the definition's current
* unit are excluded so a rename/conversion can never mix units.
*/
export async function fetchCustomMetricBehaviourSeries(
userId: string,
tz: string,
since: Date,
): Promise<NamedSeries[]> {
const metrics = await prisma.customMetric.findMany({
where: {
userId,
deletedAt: null,
correlationEnabled: true,
},
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
take: MAX_CUSTOM_CORRELATION_CHANNELS,
select: {
id: true,
name: true,
unit: true,
entries: {
where: { measuredAt: { gte: since } },
orderBy: [{ measuredAt: "desc" }, { id: "desc" }],
take: 2000,
select: { value: true, unit: true, measuredAt: true },
},
},
});
return metrics.map((metric) => ({
key: `${CUSTOM_METRIC_CHANNEL_PREFIX}${metric.id}`,
label: metric.name,
role: "behaviour" as const,
points: toDailyMeans(
metric.entries
.filter((entry) => entry.unit === metric.unit)
.map((entry) => ({ value: entry.value, at: entry.measuredAt })),
tz,
),
}));
}
/** Day key (YYYY-MM-DD) for an instant in the user's display timezone. */
function tzDayKey(at: Date, tz: string): string {
const { year, month, day } = wallClockInTz(at, tz);
+28 -3
View File
@@ -121,6 +121,7 @@ export function metricFamily(key: string): string {
// near-tautology). They stay free to pair cross-domain against mood / sleep /
// vitals — the whole point of the module.
if (key.startsWith("ENV_")) return "ENVIRONMENT";
if (key.startsWith("CUSTOM_METRIC:")) return key;
// v1.21.0 (FDREXTEND) — medication compliance and symptom severity each form
// their OWN single-channel family, so the loop's same-family guard collapses
// only the self-lag (compliance→compliance, symptom→symptom). The returned
@@ -168,6 +169,8 @@ export interface NamedSeries {
key: string;
/** Whether the channel is a behaviour (lag source) or an outcome (lag target). */
role: "behaviour" | "outcome";
/** User-facing label for dynamic channels such as custom metrics. */
label?: string;
points: DailySeriesPoint[];
}
@@ -175,7 +178,9 @@ export interface NamedSeries {
export interface DiscoveredCorrelation {
behaviour: string;
outcome: string;
behaviourLabel?: string;
/** Paired-day count after the day+1 lag join. */
outcomeLabel?: string;
n: number;
/** Pearson r (lag-joined). */
r: number;
@@ -214,6 +219,10 @@ export interface DiscoveredCorrelation {
* provisional rather than established.
*/
provisional?: boolean;
/** Persisted server decision for dismissal across recomputation. */
patternId?: string;
canonicalKey?: string;
dismissed?: boolean;
}
export interface CorrelationDiscoveryResult {
@@ -263,6 +272,8 @@ interface RawPair {
n: number;
r: number;
pValue: number;
behaviourLabel?: string;
outcomeLabel?: string;
}
/**
@@ -309,10 +320,12 @@ function interpret(
r: number,
tier: ConfidenceTier,
t: Translate,
behaviourLabel?: string,
outcomeLabel?: string,
): string {
const params = {
behaviour: humanise(behaviour, t),
outcome: humanise(outcome, t),
behaviour: behaviourLabel ?? humanise(behaviour, t),
outcome: outcomeLabel ?? humanise(outcome, t),
direction: t(
`insights.correlation.direction.${r < 0 ? "lower" : "higher"}`,
),
@@ -471,6 +484,8 @@ export function discoverCorrelations(
n: result.n,
r: result.r,
pValue: result.pValue,
behaviourLabel: b.label,
outcomeLabel: o.label,
});
}
}
@@ -505,13 +520,23 @@ export function discoverCorrelations(
.map((t) => ({
behaviour: t.behaviour,
outcome: t.outcome,
behaviourLabel: t.behaviourLabel,
outcomeLabel: t.outcomeLabel,
n: t.n,
r: t.r,
pValue: t.pValue,
qValue: Math.round(t.qValue * 1000) / 1000,
shrunkR: Math.round(t.shrunkR * 1000) / 1000,
tier: t.tier,
interpretation: interpret(t.behaviour, t.outcome, t.r, t.tier, translate),
interpretation: interpret(
t.behaviour,
t.outcome,
t.r,
t.tier,
translate,
t.behaviourLabel,
t.outcomeLabel,
),
lagDays,
}))
// RECON1 (D2-2 / D4) — rank by the SHRUNK effect magnitude (a deep, strong
+208
View File
@@ -0,0 +1,208 @@
import { createHash } from "node:crypto";
import { prisma } from "@/lib/db";
export const PATTERN_FAMILIES = {
discoveryRetrospective: "DISCOVERY_RETROSPECTIVE",
discoveryRecent: "DISCOVERY_RECENT",
fixed: "FIXED",
moodTagCrosstab: "MOOD_TAG_CROSSTAB",
moodFactorCrosstab: "MOOD_FACTOR_CROSSTAB",
} as const;
export type PatternFamily =
(typeof PATTERN_FAMILIES)[keyof typeof PATTERN_FAMILIES];
export interface AcceptedPatternEvidence {
factorKey: string;
outcomeKey: string;
lagDays: number;
sampleSize: number;
effectSize: number;
pValue: number;
qValue?: number | null;
}
export interface PatternDecision {
patternId: string;
canonicalKey: string;
dismissed: boolean;
}
/** Stable identity derived only from the user-independent pair definition. */
export function canonicalPatternKey(
factorKey: string,
outcomeKey: string,
lagDays: number,
): string {
const source = JSON.stringify([factorKey, outcomeKey, lagDays]);
return `p1:${createHash("sha256").update(source).digest("hex")}`;
}
function evidenceHash(evidence: AcceptedPatternEvidence): string {
const source = JSON.stringify({
factorKey: evidence.factorKey,
outcomeKey: evidence.outcomeKey,
lagDays: evidence.lagDays,
sampleSize: evidence.sampleSize,
effectSize: evidence.effectSize,
pValue: evidence.pValue,
qValue: evidence.qValue ?? null,
});
return createHash("sha256").update(source).digest("hex");
}
/**
* A dismissal survives normal sampling noise. It is released only when the
* effect reverses direction, moves by at least 0.10, or the accepted sample
* grows by at least 25% (with a ten-observation floor).
*/
export function isMaterialEvidenceChange(args: {
currentEffectSize: number;
currentSampleSize: number;
dismissedEffectSize: number;
dismissedSampleSize: number;
}): boolean {
const signChanged =
Math.sign(args.currentEffectSize) !== Math.sign(args.dismissedEffectSize);
const effectMoved =
Math.abs(args.currentEffectSize - args.dismissedEffectSize) >= 0.1;
const sampleGrowth = Math.max(10, Math.ceil(args.dismissedSampleSize * 0.25));
const sampleDeepened =
args.currentSampleSize >= args.dismissedSampleSize + sampleGrowth;
return signChanged || effectMoved || sampleDeepened;
}
/**
* Persist one accepted family run, retire findings no longer accepted, and
* return the server's presentation decision for every accepted finding.
*/
export async function syncAcceptedPatterns(args: {
userId: string;
family: PatternFamily;
accepted: AcceptedPatternEvidence[];
computedAt?: Date;
}): Promise<Map<string, PatternDecision>> {
const computedAt = args.computedAt ?? new Date();
const prepared = args.accepted.map((evidence) => ({
evidence,
canonicalKey: canonicalPatternKey(
evidence.factorKey,
evidence.outcomeKey,
evidence.lagDays,
),
evidenceHash: evidenceHash(evidence),
}));
const keys = prepared.map((item) => item.canonicalKey);
return prisma.$transaction(async (tx) => {
const existing = await tx.correlationPattern.findMany({
where: { userId: args.userId, canonicalKey: { in: keys } },
});
const existingByKey = new Map(
existing.map((row) => [row.canonicalKey, row]),
);
if (keys.length === 0) {
await tx.correlationPattern.updateMany({
where: { userId: args.userId, family: args.family, isCurrent: true },
data: { isCurrent: false },
});
} else {
await tx.correlationPattern.updateMany({
where: {
userId: args.userId,
family: args.family,
isCurrent: true,
canonicalKey: { notIn: keys },
},
data: { isCurrent: false },
});
}
const decisions = new Map<string, PatternDecision>();
for (const item of prepared) {
const prior = existingByKey.get(item.canonicalKey);
const shouldResurface =
prior?.dismissedAt != null &&
prior.dismissedEffectSize != null &&
prior.dismissedSampleSize != null &&
isMaterialEvidenceChange({
currentEffectSize: item.evidence.effectSize,
currentSampleSize: item.evidence.sampleSize,
dismissedEffectSize: prior.dismissedEffectSize,
dismissedSampleSize: prior.dismissedSampleSize,
});
const row = await tx.correlationPattern.upsert({
where: {
userId_canonicalKey: {
userId: args.userId,
canonicalKey: item.canonicalKey,
},
},
create: {
userId: args.userId,
canonicalKey: item.canonicalKey,
family: args.family,
factorKey: item.evidence.factorKey,
outcomeKey: item.evidence.outcomeKey,
lagDays: item.evidence.lagDays,
sampleSize: item.evidence.sampleSize,
effectSize: item.evidence.effectSize,
pValue: item.evidence.pValue,
qValue: item.evidence.qValue ?? null,
evidenceHash: item.evidenceHash,
isCurrent: true,
lastComputedAt: computedAt,
},
update: {
family: args.family,
factorKey: item.evidence.factorKey,
outcomeKey: item.evidence.outcomeKey,
lagDays: item.evidence.lagDays,
sampleSize: item.evidence.sampleSize,
effectSize: item.evidence.effectSize,
pValue: item.evidence.pValue,
qValue: item.evidence.qValue ?? null,
evidenceHash: item.evidenceHash,
isCurrent: true,
lastComputedAt: computedAt,
...(shouldResurface
? {
dismissedAt: null,
dismissedEvidenceHash: null,
dismissedEffectSize: null,
dismissedSampleSize: null,
}
: {}),
},
});
decisions.set(item.canonicalKey, {
patternId: row.id,
canonicalKey: row.canonicalKey,
dismissed: row.dismissedAt != null,
});
}
return decisions;
});
}
export function decisionForEvidence(
decisions: Map<string, PatternDecision>,
evidence: Pick<
AcceptedPatternEvidence,
"factorKey" | "outcomeKey" | "lagDays"
>,
): PatternDecision | null {
return (
decisions.get(
canonicalPatternKey(
evidence.factorKey,
evidence.outcomeKey,
evidence.lagDays,
),
) ?? null
);
}
+3
View File
@@ -106,6 +106,9 @@ export interface CorrelationOk {
xLabel: string;
/** Free-form key the UI can use to label the y-axis. */
yLabel: string;
patternId?: string;
canonicalKey?: string;
dismissed?: boolean;
}
export interface CorrelationInsufficient {
+58 -4
View File
@@ -28,13 +28,24 @@ import {
DISCOVERY_BEHAVIOURS,
DISCOVERY_OUTCOMES,
type NamedSeries,
MEDICATION_COMPLIANCE_CHANNEL_KEY,
SYMPTOM_SEVERITY_CHANNEL_KEY,
} from "@/lib/insights/correlation-discovery";
import {
buildMeasurementDailySeries,
fetchComplianceSeries,
fetchCustomMetricBehaviourSeries,
fetchEnvironmentSeries,
fetchMeasurementWindowSeries,
fetchMoodWindowSeries,
fetchSymptomSeries,
} from "@/lib/insights/correlation-channel-series";
import { loadUserSourcePriority } from "@/lib/rollups/measurement-read";
import {
decisionForEvidence,
PATTERN_FAMILIES,
syncAcceptedPatterns,
} from "@/lib/insights/correlation-patterns";
import type { RelevantCorrelation } from "@/lib/insights/assessment-context";
const MS_PER_DAY = 24 * 60 * 60 * 1000;
@@ -101,13 +112,25 @@ export async function getRelevantCorrelationsForMetric(
// lives in `fetchMeasurementWindowSeries` / `fetchMoodWindowSeries`
// (`correlation-channel-series.ts`), shared with the route, the Coach
// tool, and the period narrative.
const [{ byType }, { moodDaily }, priorityJson] = await Promise.all([
const [
{ byType },
{ moodDaily },
priorityJson,
complianceSeries,
symptomSeries,
environmentSeries,
customMetricSeries,
] = await Promise.all([
fetchMeasurementWindowSeries(userId, since, [
...behaviourTypes,
...outcomeTypes,
]),
fetchMoodWindowSeries(userId, tz, since),
loadUserSourcePriority(userId),
fetchComplianceSeries(userId, tz, since),
fetchSymptomSeries(userId, tz, since),
fetchEnvironmentSeries(userId, since),
fetchCustomMetricBehaviourSeries(userId, tz, since),
]);
const points = (key: string) =>
@@ -122,15 +145,46 @@ export async function getRelevantCorrelationsForMetric(
const series: NamedSeries[] = [];
for (const key of DISCOVERY_BEHAVIOURS) {
series.push({ key, role: "behaviour", points: points(key) });
if (key === MEDICATION_COMPLIANCE_CHANNEL_KEY) {
series.push(complianceSeries);
} else if (key === SYMPTOM_SEVERITY_CHANNEL_KEY) {
series.push({ ...symptomSeries, role: "behaviour" });
} else {
series.push({ key, role: "behaviour", points: points(key) });
}
}
for (const key of DISCOVERY_OUTCOMES) {
series.push({ key, role: "outcome", points: points(key) });
if (key === SYMPTOM_SEVERITY_CHANNEL_KEY) {
series.push({ ...symptomSeries, role: "outcome" });
} else {
series.push({ key, role: "outcome", points: points(key) });
}
}
series.push(...environmentSeries, ...customMetricSeries);
const result = discoverCorrelations(series);
const decisions = await syncAcceptedPatterns({
userId,
family: PATTERN_FAMILIES.discoveryRetrospective,
accepted: result.discovered.map((pattern) => ({
factorKey: pattern.behaviour,
outcomeKey: pattern.outcome,
lagDays: pattern.lagDays,
sampleSize: pattern.n,
effectSize: pattern.r,
pValue: pattern.pValue,
qValue: pattern.qValue,
})),
});
return result.discovered
.filter((d) => d.behaviour === channel || d.outcome === channel)
.filter((d) => {
const decision = decisionForEvidence(decisions, {
factorKey: d.behaviour,
outcomeKey: d.outcome,
lagDays: d.lagDays,
});
return decision?.dismissed !== true;
})
.map((d) => ({ interpretation: d.interpretation, n: d.n, r: d.r }));
} catch {
return [];
+51 -1
View File
@@ -70,6 +70,11 @@ import {
type TagInfluence,
type TagSummaryRow,
} from "@/lib/insights/mood-tag-influence";
import {
decisionForEvidence,
PATTERN_FAMILIES,
syncAcceptedPatterns,
} from "@/lib/insights/correlation-patterns";
import type { MeasurementType } from "@/generated/prisma/enums";
import type { MeasurementSource } from "@/generated/prisma/client";
@@ -760,11 +765,56 @@ export async function fetchMoodAggregates(
resolveUserTimezone(userId),
]);
return computeMoodAggregates({
const aggregate = computeMoodAggregates({
entries,
measurements,
now,
userPriorityJson,
tz,
});
const tagEvidence = aggregate.tagMetricCrosstab.map((row) => ({
factorKey: `TAG:${row.tag}`,
outcomeKey: `CROSSTAB:${row.metricKey}`,
lagDays: row.mode === "nextDay" ? 1 : 0,
sampleSize: row.withDays + row.withoutDays,
effectSize: row.delta,
pValue: row.pValue,
qValue: row.qValue,
}));
const factorEvidence = aggregate.factorCrosstab.map((row) => ({
factorKey: `RATED:${row.factor}`,
outcomeKey: `CROSSTAB:${row.metricKey}`,
lagDays: row.mode === "nextDay" ? 1 : 0,
sampleSize: row.lowDays + row.highDays,
effectSize: row.delta,
pValue: row.pValue,
qValue: row.qValue,
}));
const [tagDecisions, factorDecisions] = await Promise.all([
syncAcceptedPatterns({
userId,
family: PATTERN_FAMILIES.moodTagCrosstab,
accepted: tagEvidence,
computedAt: now,
}),
syncAcceptedPatterns({
userId,
family: PATTERN_FAMILIES.moodFactorCrosstab,
accepted: factorEvidence,
computedAt: now,
}),
]);
return {
...aggregate,
tagMetricCrosstab: aggregate.tagMetricCrosstab.map((row, index) => ({
...row,
...(decisionForEvidence(tagDecisions, tagEvidence[index]) ?? {}),
})),
factorCrosstab: aggregate.factorCrosstab.map((row, index) => ({
...row,
...(decisionForEvidence(factorDecisions, factorEvidence[index]) ?? {}),
})),
};
}
+6
View File
@@ -170,6 +170,9 @@ export interface TagMetricCrosstabRow {
qValue: number;
/** Discrete confidence band (p + min per-group day count). */
confidence: InfluenceConfidence;
patternId?: string;
canonicalKey?: string;
dismissed?: boolean;
}
const CROSSTAB_SUM_TYPES = new Set<string>([
@@ -497,6 +500,9 @@ export interface FactorMetricCrosstabRow {
qValue: number;
/** Discrete confidence band (p + min per-group day count). */
confidence: InfluenceConfidence;
patternId?: string;
canonicalKey?: string;
dismissed?: boolean;
}
type FactorMetricDisplay =
+32 -1
View File
@@ -14,6 +14,7 @@ import {
DISCOVERY_BEHAVIOURS,
DISCOVERY_OUTCOMES,
} from "@/lib/insights/correlation-discovery";
import { MAX_CUSTOM_CORRELATION_CHANNELS } from "@/lib/insights/correlation-channel-series";
import { annotate } from "@/lib/logging/context";
import type { SupportedLocale } from "@/lib/insights/status-shared";
@@ -289,7 +290,7 @@ export async function computeStatusInputFingerprint(args: {
? Array.from(new Set<string>([...args.types, ...CORRELATION_CHANNEL_TYPES]))
: [...args.types];
const [grouped, mood] = await Promise.all([
const [grouped, mood, customMetrics] = await Promise.all([
prisma.measurement.groupBy({
by: ["type"],
where: {
@@ -307,6 +308,28 @@ export async function computeStatusInputFingerprint(args: {
_max: { moodLoggedAt: true },
})
: Promise.resolve(null),
args.includeCorrelationChannels
? prisma.customMetric.findMany({
where: {
userId: args.userId,
deletedAt: null,
correlationEnabled: true,
},
orderBy: [{ createdAt: "asc" }, { id: "asc" }],
take: MAX_CUSTOM_CORRELATION_CHANNELS,
select: {
id: true,
unit: true,
updatedAt: true,
_count: { select: { entries: true } },
entries: {
orderBy: [{ measuredAt: "desc" }, { id: "desc" }],
take: 1,
select: { measuredAt: true, unit: true },
},
},
})
: Promise.resolve([]),
]);
// Deterministic shape regardless of group order: sort by type, project a
// stable `{ type, count, newest }` triple. `hashInsightSnapshot` sorts keys
@@ -330,6 +353,14 @@ export async function computeStatusInputFingerprint(args: {
},
}
: {}),
customMetrics: customMetrics.map((metric) => ({
id: metric.id,
unit: metric.unit,
updatedAt: metric.updatedAt.toISOString(),
count: metric._count.entries,
newest: metric.entries[0]?.measuredAt.toISOString() ?? null,
newestUnit: metric.entries[0]?.unit ?? null,
})),
...(args.extra ? { extra: args.extra } : {}),
});
}
+6 -5
View File
@@ -7,9 +7,9 @@
* so the wire contract stays single-source. The response shapes are declared
* here.
*
* Custom metrics are a SEPARATE generic store from the closed measurement
* system: not synced, not in FHIR, not in AI insights — log + chart only. All
* fields are plaintext.
* Custom metrics are a separate generic store from the closed measurement
* system. A definition can explicitly opt into the bounded daily correlation
* behaviour-channel scan. All fields are plaintext.
*/
import type { ZodOpenApiObject } from "zod-openapi";
import { z } from "zod/v4";
@@ -26,13 +26,13 @@ import { dataEnvelope, errorEnvelope, stdResponses } from "./shared";
createCustomMetricSchema.meta({
id: "CreateCustomMetricRequest",
description:
"Define a user-scoped custom metric ONCE: free-text `name` + `unit`, an optional target window (`targetLow` / `targetHigh`; when both present `targetLow` must not exceed `targetHigh`), optional display `decimals`, and an optional `description`. The name is unique per user. Logging a value later just picks this metric — its unit is snapshotted onto the value at write time. Isolated from the closed measurement system: not synced, not in FHIR, not in insights.",
"Define a user-scoped custom metric ONCE: free-text `name` + `unit`, an optional target window (`targetLow` / `targetHigh`; when both present `targetLow` must not exceed `targetHigh`), optional display `decimals`, an optional `description`, and explicit `correlationEnabled` opt-in. The name is unique per user. Logging a value later snapshots its unit.",
});
updateCustomMetricSchema.meta({
id: "UpdateCustomMetricRequest",
description:
"Partial edit of a custom metric. An omitted key leaves the column untouched; an explicit `null` on a target bound / `decimals` / `description` clears it. A rename that collides with another of the caller's live metrics is rejected 409.",
"Partial edit of a custom metric. An omitted key leaves the column untouched; an explicit `null` on a target bound / `decimals` / `description` clears it. `correlationEnabled` explicitly controls use as a bounded behaviour channel. A conflicting rename is rejected 409.",
});
createCustomMetricEntrySchema.meta({
@@ -64,6 +64,7 @@ const customMetricRow = z
targetHigh: z.number().nullable(),
decimals: z.number().nullable(),
description: z.string().nullable(),
correlationEnabled: z.boolean(),
latest: latestValue,
entryCount: z.number(),
createdAt: z.string(),
+58
View File
@@ -24,6 +24,9 @@ import {
derivedBatchQuery,
derivedBatchResponse,
correlationDiscoveryResponse,
correlationPatternListResponse,
updateCorrelationPatternRequest,
updateCorrelationPatternResponse,
glp1PlateauResponse,
insightStatusQuery,
insightStatusResponse,
@@ -511,4 +514,59 @@ export const insightsPaths: NonNullable<ZodOpenApiObject["paths"]> = {
},
},
},
"/api/insights/patterns": {
get: {
tags: ["Insights"],
summary: "List current correlation patterns",
description:
"Returns the authenticated account's currently accepted persisted correlation identities, evidence, and dismissal timestamps.",
responses: {
"200": {
description: "Current persisted correlation patterns.",
content: {
"application/json": {
schema: dataEnvelope(
correlationPatternListResponse,
"CorrelationPatternListResponseEnvelope",
),
},
},
},
...stdResponses,
},
},
},
"/api/insights/patterns/{id}": {
patch: {
tags: ["Insights"],
summary: "Update correlation pattern dismissal",
description:
"Dismisses or restores one current account-owned pattern. A dismissal remains effective until the accepted evidence changes materially.",
requestParams: { path: z.object({ id: z.string() }) },
requestBody: {
required: true,
content: {
"application/json": { schema: updateCorrelationPatternRequest },
},
},
responses: {
"200": {
description: "Updated dismissal decision.",
content: {
"application/json": {
schema: dataEnvelope(
updateCorrelationPatternResponse,
"UpdateCorrelationPatternResponseEnvelope",
),
},
},
},
"404": {
description: "No current pattern with that id for this account.",
content: { "application/json": { schema: errorEnvelope } },
},
...stdResponses,
},
},
},
};
@@ -281,6 +281,14 @@ export const discoveredCorrelation = z
.describe(
"Outcome channel (lag target), e.g. SLEEP_DURATION, HEART_RATE_VARIABILITY.",
),
behaviourLabel: z
.string()
.optional()
.describe("Display label for a dynamic behaviour channel."),
outcomeLabel: z
.string()
.optional()
.describe("Display label for a dynamic outcome channel."),
n: z
.number()
.int()
@@ -308,9 +316,57 @@ export const discoveredCorrelation = z
.describe(
"v1.22 — true for an emerging recent-window pair: fewer days, hedged as provisional rather than established.",
),
patternId: z
.string()
.optional()
.describe("Account-scoped persisted identity for dismissal."),
canonicalKey: z
.string()
.optional()
.describe("Stable factor + outcome + lag identity."),
dismissed: z
.boolean()
.optional()
.describe("Server decision to suppress unchanged evidence."),
})
.meta({ id: "DiscoveredCorrelation" });
export const correlationPattern = z
.object({
id: z.string(),
canonicalKey: z.string(),
family: z.string(),
factorKey: z.string(),
outcomeKey: z.string(),
lagDays: z.number().int(),
sampleSize: z.number().int(),
effectSize: z.number(),
pValue: z.number(),
qValue: z.number().nullable(),
evidenceHash: z.string(),
lastComputedAt: z.string(),
dismissedAt: z.string().nullable(),
})
.meta({ id: "CorrelationPattern" });
export const correlationPatternListResponse = z
.object({ patterns: z.array(correlationPattern) })
.meta({ id: "CorrelationPatternListResponse" });
export const updateCorrelationPatternRequest = z
.strictObject({ dismissed: z.boolean() })
.meta({ id: "UpdateCorrelationPatternRequest" });
export const updateCorrelationPatternResponse = z
.object({
id: z.string(),
canonicalKey: z.string(),
dismissed: z.boolean(),
dismissedAt: z.string().nullable(),
evidenceHash: z.string(),
})
.meta({ id: "UpdateCorrelationPatternResponse" });
// v1.22 — one labs ↔ outcome association (point-vs-window over sparse draws).
export const discoveredLabCorrelation = z
.object({
+33
View File
@@ -447,6 +447,7 @@ const customMetricBackupSchema = z
targetHigh: z.number().nullable().default(null),
decimals: z.number().int().nullable().default(null),
description: z.string().nullable().default(null),
correlationEnabled: z.boolean().default(false),
createdAt: isoDateTime.optional(),
updatedAt: isoDateTime.optional(),
deletedAt: isoDateTime.nullable().optional(),
@@ -454,6 +455,34 @@ const customMetricBackupSchema = z
})
.passthrough();
const correlationPatternBackupSchema = z
.object({
id: z.string().min(1).optional(),
canonicalKey: z.string().regex(/^p1:[a-f0-9]{64}$/),
family: z.string().min(1),
factorKey: z.string().min(1),
outcomeKey: z.string().min(1),
lagDays: z.number().int().min(0),
sampleSize: z.number().int().positive(),
effectSize: z.number().finite(),
pValue: z.number().min(0).max(1),
qValue: z.number().min(0).max(1).nullable().default(null),
evidenceHash: z.string().regex(/^[a-f0-9]{64}$/),
isCurrent: z.boolean(),
lastComputedAt: isoDateTime,
dismissedAt: isoDateTime.nullable().default(null),
dismissedEvidenceHash: z
.string()
.regex(/^[a-f0-9]{64}$/)
.nullable()
.default(null),
dismissedEffectSize: z.number().finite().nullable().default(null),
dismissedSampleSize: z.number().int().positive().nullable().default(null),
createdAt: isoDateTime.optional(),
updatedAt: isoDateTime.optional(),
})
.passthrough();
/**
* One local day's cumulative curve for one metric.
*
@@ -749,6 +778,7 @@ export const backupPayloadSchema = z
// `null` / `[]`.
healthProfile: healthProfileBackupSchema.nullable().default(null),
customMetrics: z.array(customMetricBackupSchema).default([]),
correlationPatterns: z.array(correlationPatternBackupSchema).default([]),
// The hourly shape of a cumulative day. Defaulted for the same reason as
// the pair above: a file written before the table existed carries no key.
intradayProfiles: z.array(intradayProfileBackupSchema).default([]),
@@ -810,6 +840,8 @@ export interface BackupSummary {
customMetrics: number;
/** Readings across every user-defined metric. */
customMetricEntries: number;
/** Persisted accepted correlation identities and dismissal decisions. */
correlationPatterns: number;
/** Stored day curves for the cumulative metrics, across every metric. */
intradayProfiles: number;
}
@@ -843,6 +875,7 @@ export function summarizeBackup(payload: BackupPayload): BackupSummary {
(sum, metric) => sum + metric.entries.length,
0,
),
correlationPatterns: payload.correlationPatterns.length,
intradayProfiles: payload.intradayProfiles.length,
};
}
@@ -0,0 +1,9 @@
import { z } from "zod/v4";
export const updateCorrelationPatternSchema = z.strictObject({
dismissed: z.boolean(),
});
export type UpdateCorrelationPatternInput = z.infer<
typeof updateCorrelationPatternSchema
>;
+2
View File
@@ -44,6 +44,7 @@ export const createCustomMetricSchema = z
targetHigh,
decimals,
description: optionalDescription,
correlationEnabled: z.boolean().default(false),
})
.refine(
(d) =>
@@ -71,6 +72,7 @@ export const updateCustomMetricSchema = z
targetHigh: targetHigh.or(z.null()),
decimals: decimals.or(z.null()),
description: optionalDescription.or(z.null()),
correlationEnabled: z.boolean().optional(),
})
.refine(
(d) =>