mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 08:12:52 +00:00
fix(insights): make per-category status cards read-only so navigation never blocks on the LLM
Opening an Insights category page froze the whole UI: the per-metric status card fired a cold, uncapped LLM fetch on mount with no client abort, so the request hung up to the server's 60s ceiling and the pending/resolve re-renders thrashed the tab strip and ate touch events. Timeouts were never negatively cached, so every visit re-blocked. Fix the class holistically: - Bound every status GET on the client. The shared use-insight-status hook and the inline medication-compliance query now wrap fetch in an 8s AbortController and stop retrying inline; navigation can no longer await an uncapped round-trip. - Make the status routes read-only. On a cache miss the generator enqueues an out-of-band pg-boss job (insight-status-generate) and returns a preparing envelope; the client polls a bounded interval until the worker warms the cache. Models the dashboard snapshot's ready/preparing pattern. The queue is registered in the worker allQueues loop + a boss.work handler, with a guard test. - Fix the nightly warm-pass regression: warm every supported locale (the client reads against the active UI locale, not the persisted User.locale) and warm on a cached comprehensive outcome too (its write still evicts the per-status caches). Add a short-TTL negative cache on the timeout path so a transient provider stall no longer makes every subsequent visit re-block or re-enqueue. - Add loading.tsx for the insights segment + the root so any future RSC await streams a skeleton instead of blocking the transition. - Stabilise the advisor regenerate callback so a status-query flip can't re-render the memoised tab strip mid-gesture. New i18n key insights.assessmentPreparing across all six locales.
This commit is contained in:
1 parent
6a603ad587
commit
35800becbb
47 files changed
+1297
-75
No files matched your search
@@ -152,7 +152,19 @@ describe("generateBloodPressureStatusForUser — timeout/error never persists",
|
||||
expect(result.text).toBeTruthy();
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -121,7 +121,19 @@ describe("generateBmiStatusForUser — timeout/error never persists", () => {
|
||||
expect(result.text).toBeTruthy();
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it("serves the fallback without writing a cache row on provider error", async () => {
|
||||
@@ -140,7 +152,19 @@ describe("generateBmiStatusForUser — timeout/error never persists", () => {
|
||||
const result = await generateBmiStatusForUser("user-1", { locale: "en" });
|
||||
expect(result.text).toBeTruthy();
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -162,7 +162,19 @@ describe("generateGeneralStatusForUser — timeout/error never persists", () =>
|
||||
expect(result.text).toBeTruthy();
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -155,7 +155,19 @@ describe("generateMedicationComplianceStatusForUser — timeout never persists",
|
||||
expect(result.medications).toEqual([]);
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -118,7 +118,19 @@ describe("generateMoodStatusForUser — timeout/error never persists", () => {
|
||||
expect(result.text).toBeTruthy();
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -4,12 +4,15 @@ import { describe, expect, it, vi, beforeEach } from "vitest";
|
||||
* Provider-timeout fallback for the pulse-status route. When the
|
||||
* provider chain does not resolve inside `STATUS_PROVIDER_TIMEOUT_MS`
|
||||
* the route returns the deterministic no-key fallback so the
|
||||
* InsightStatusCard renders instead of spinning — but it must NOT
|
||||
* persist the fallback. The earlier behaviour cached a `timeout-stub`
|
||||
* row keyed to today; that stub stuck until midnight and hid the real
|
||||
* data-driven assessment. The fix treats a timeout as a transient miss:
|
||||
* serve the fallback for this render, write nothing, re-attempt next
|
||||
* mount.
|
||||
* InsightStatusCard renders instead of spinning. It must NOT persist the
|
||||
* fallback AS AN ASSESSMENT (the pre-v1.4.28 stick-until-midnight bug);
|
||||
* `updatedAt` stays null and the served text is never a real assessment.
|
||||
*
|
||||
* v1.8.3 — the timeout path now writes a *short-TTL negative stub*
|
||||
* (`{ timeout:true, model:"timeout-stub", retryAt }`). It is explicitly
|
||||
* rejected by `readFreshStatusText`, so it can never hide the real
|
||||
* assessment; its sole purpose is to stop the read-only route re-enqueuing
|
||||
* generation on every navigation while a provider is degraded.
|
||||
*/
|
||||
|
||||
vi.mock("@/lib/db", () => ({
|
||||
@@ -64,8 +67,23 @@ describe("generatePulseStatusForUser — provider timeout fallback", () => {
|
||||
expect(result.cached).toBe(true);
|
||||
expect(typeof result.text).toBe("string");
|
||||
expect(result.text?.length ?? 0).toBeGreaterThan(0);
|
||||
// No persisted row — the sticky-stub bug is gone.
|
||||
// No real assessment persisted — `updatedAt` stays null so the card
|
||||
// never mislabels the fallback as a fresh assessment.
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
|
||||
// v1.8.3 — a short-TTL negative stub IS persisted (fire-and-forget) so
|
||||
// the read-only route doesn't re-enqueue on every navigation while the
|
||||
// provider is degraded. It is marked as a timeout stub, which
|
||||
// `readFreshStatusText` rejects, so it never hides the real assessment.
|
||||
// The write is fire-and-forget (`void`), so flush the microtask queue.
|
||||
await Promise.resolve();
|
||||
expect(prisma.auditLog.create).toHaveBeenCalledTimes(1);
|
||||
const persisted = vi.mocked(prisma.auditLog.create).mock.calls[0][0] as {
|
||||
data: { details: string };
|
||||
};
|
||||
const stub = JSON.parse(persisted.data.details);
|
||||
expect(stub.timeout).toBe(true);
|
||||
expect(stub.model).toBe("timeout-stub");
|
||||
expect(typeof stub.retryAt).toBe("string");
|
||||
});
|
||||
});
|
||||
@@ -6,8 +6,22 @@ vi.mock("@/lib/db", () => ({
|
||||
},
|
||||
}));
|
||||
|
||||
const hasUsableStatusProvider = vi.fn();
|
||||
vi.mock("@/lib/insights/status-provider", () => ({
|
||||
hasUsableStatusProvider: (...a: unknown[]) => hasUsableStatusProvider(...a),
|
||||
}));
|
||||
|
||||
const enqueueStatusGeneration = vi.fn();
|
||||
vi.mock("@/lib/jobs/insight-status-generate-shared", () => ({
|
||||
enqueueStatusGeneration: (...a: unknown[]) => enqueueStatusGeneration(...a),
|
||||
}));
|
||||
|
||||
import { prisma } from "@/lib/db";
|
||||
import { isTimeoutStub, readFreshStatusText } from "../status-cache";
|
||||
import {
|
||||
isTimeoutStub,
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "../status-cache";
|
||||
|
||||
const TODAY = "2026-05-31";
|
||||
|
||||
@@ -121,3 +135,71 @@ describe("readFreshStatusText", () => {
|
||||
expect(hit).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveReadOnlyStatusMiss", () => {
|
||||
it("returns no-provider without enqueuing when the user has no provider", async () => {
|
||||
hasUsableStatusProvider.mockResolvedValue(false);
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId: "u1",
|
||||
metric: "weight",
|
||||
locale: "en",
|
||||
});
|
||||
expect(outcome).toBe("no-provider");
|
||||
expect(enqueueStatusGeneration).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("enqueues generation and returns preparing on a clean miss", async () => {
|
||||
hasUsableStatusProvider.mockResolvedValue(true);
|
||||
// No negative stub present.
|
||||
vi.mocked(prisma.auditLog.findFirst).mockResolvedValue(null as never);
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId: "u1",
|
||||
metric: "pulse",
|
||||
locale: "de",
|
||||
});
|
||||
expect(outcome).toBe("preparing");
|
||||
expect(enqueueStatusGeneration).toHaveBeenCalledWith({
|
||||
userId: "u1",
|
||||
metric: "pulse",
|
||||
locale: "de",
|
||||
});
|
||||
});
|
||||
|
||||
it("suppresses re-enqueue while a fresh timeout stub exists", async () => {
|
||||
hasUsableStatusProvider.mockResolvedValue(true);
|
||||
vi.mocked(prisma.auditLog.findFirst).mockResolvedValue(
|
||||
cacheRow({
|
||||
dateKey: TODAY,
|
||||
timeout: true,
|
||||
model: "timeout-stub",
|
||||
retryAt: new Date(Date.now() + 60_000).toISOString(),
|
||||
}) as never,
|
||||
);
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId: "u1",
|
||||
metric: "mood",
|
||||
locale: "en",
|
||||
});
|
||||
expect(outcome).toBe("preparing");
|
||||
expect(enqueueStatusGeneration).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("re-enqueues once the negative stub's retryAt has passed", async () => {
|
||||
hasUsableStatusProvider.mockResolvedValue(true);
|
||||
vi.mocked(prisma.auditLog.findFirst).mockResolvedValue(
|
||||
cacheRow({
|
||||
dateKey: TODAY,
|
||||
timeout: true,
|
||||
model: "timeout-stub",
|
||||
retryAt: new Date(Date.now() - 60_000).toISOString(),
|
||||
}) as never,
|
||||
);
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId: "u1",
|
||||
metric: "bmi",
|
||||
locale: "de",
|
||||
});
|
||||
expect(outcome).toBe("preparing");
|
||||
expect(enqueueStatusGeneration).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -126,7 +126,19 @@ describe("generateWeightStatusForUser — timeout/error never persists", () => {
|
||||
expect(result.text).toBeTruthy();
|
||||
expect(result.cached).toBe(true);
|
||||
expect(result.updatedAt).toBeNull();
|
||||
expect(prisma.auditLog.create).not.toHaveBeenCalled();
|
||||
// v1.8.3 — no real assessment persisted (updatedAt stays null above),
|
||||
// but a short-TTL negative stub IS written so the read-only route does
|
||||
// not re-enqueue on every navigation while the provider is degraded.
|
||||
// The stub is a timeout marker that `readFreshStatusText` rejects.
|
||||
await Promise.resolve();
|
||||
for (const call of vi.mocked(prisma.auditLog.create).mock.calls) {
|
||||
const details = JSON.parse(
|
||||
(call[0] as { data: { details: string } }).data.details,
|
||||
);
|
||||
expect(details.timeout === true || details.model === "timeout-stub").toBe(
|
||||
true,
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -42,7 +42,10 @@ import {
|
||||
summarizeSeries,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { readFreshStatusText } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -94,15 +97,19 @@ export async function generateBloodPressureStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/** v1.8.3 — read-only navigation path; see weight-status for the rationale. */
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
text: string | null;
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.blood-pressure-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -121,6 +128,29 @@ export async function generateBloodPressureStatusForUser(
|
||||
};
|
||||
}
|
||||
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "blood-pressure",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
text: getNoKeyBloodPressureStatusText(locale),
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: null,
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
@@ -523,6 +553,8 @@ export async function generateBloodPressureStatusForUser(
|
||||
return returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyBloodPressureStatusText(locale),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -22,7 +22,10 @@ import {
|
||||
summarizeSeries,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { readFreshStatusText } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -32,15 +35,19 @@ export async function generateBmiStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/** v1.8.3 — read-only navigation path; see weight-status for the rationale. */
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
text: string | null;
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.bmi-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -59,6 +66,29 @@ export async function generateBmiStatusForUser(
|
||||
};
|
||||
}
|
||||
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "bmi",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
text: getNoKeyBmiStatusText(locale),
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: null,
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
@@ -227,6 +257,8 @@ export async function generateBmiStatusForUser(
|
||||
return returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyBmiStatusText(locale),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -25,7 +25,10 @@ import {
|
||||
summarizeSeries,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { readFreshStatusText } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -47,15 +50,19 @@ export async function generateGeneralStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/** v1.8.3 — read-only navigation path; see weight-status for the rationale. */
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
text: string | null;
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.general-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -74,6 +81,29 @@ export async function generateGeneralStatusForUser(
|
||||
};
|
||||
}
|
||||
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "general",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
text: getNoKeyGeneralStatusText(locale),
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: null,
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
@@ -348,6 +378,8 @@ export async function generateGeneralStatusForUser(
|
||||
return returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyGeneralStatusText(locale),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -27,7 +27,10 @@ import {
|
||||
round,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { isTimeoutStub } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
isTimeoutStub,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -46,6 +49,8 @@ export async function generateMedicationComplianceStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/** v1.8.3 — read-only navigation path; see weight-status for the rationale. */
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
@@ -53,9 +58,11 @@ export async function generateMedicationComplianceStatusForUser(
|
||||
medications: MedicationSummaryItem[];
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.medication-compliance-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -106,6 +113,33 @@ export async function generateMedicationComplianceStatusForUser(
|
||||
}
|
||||
}
|
||||
|
||||
// v1.8.3 — read-only navigation path: never block on the provider.
|
||||
// Enqueue generation out of band and return preparing / no-provider.
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "medication-compliance",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
summary: getNoKeyMedicationComplianceStatusText(locale),
|
||||
medications: [],
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
summary: null,
|
||||
medications: [],
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
const medications = await prisma.medication.findMany({
|
||||
where: { userId, active: true },
|
||||
include: { schedules: true },
|
||||
@@ -338,6 +372,8 @@ export async function generateMedicationComplianceStatusForUser(
|
||||
returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyMedicationComplianceStatusText(locale),
|
||||
});
|
||||
return {
|
||||
|
||||
@@ -28,7 +28,10 @@ import {
|
||||
summarizeSeries,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { readFreshStatusText } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -70,15 +73,19 @@ export async function generateMoodStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/** v1.8.3 — read-only navigation path; see weight-status for the rationale. */
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
text: string | null;
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.mood-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -97,6 +104,29 @@ export async function generateMoodStatusForUser(
|
||||
};
|
||||
}
|
||||
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "mood",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
text: getNoKeyMoodStatusText(locale),
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: null,
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
// v1.4.28 FB-D2 — cap at 90 entries (~3 months) for the mood
|
||||
// surface. The downstream `applyPayloadBudget` slims further.
|
||||
// Order desc + reverse so the per-row consumers downstream (which
|
||||
@@ -382,6 +412,8 @@ export async function generateMoodStatusForUser(
|
||||
return returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyMoodStatusText(locale),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -28,7 +28,10 @@ import {
|
||||
summarizeSeries,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { readFreshStatusText } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -38,15 +41,19 @@ export async function generatePulseStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/** v1.8.3 — read-only navigation path; see weight-status for the rationale. */
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
text: string | null;
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.pulse-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -65,6 +72,29 @@ export async function generatePulseStatusForUser(
|
||||
};
|
||||
}
|
||||
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "pulse",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
text: getNoKeyPulseStatusText(locale),
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: null,
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: { id: userId },
|
||||
select: {
|
||||
@@ -270,6 +300,8 @@ export async function generatePulseStatusForUser(
|
||||
return returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyPulseStatusText(locale),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,10 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { hasUsableStatusProvider } from "@/lib/insights/status-provider";
|
||||
import {
|
||||
enqueueStatusGeneration,
|
||||
type InsightStatusMetric,
|
||||
} from "@/lib/jobs/insight-status-generate-shared";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
|
||||
/**
|
||||
* Shared cache-read for the seven `*-status.ts` insight generators.
|
||||
@@ -31,6 +37,8 @@ interface ParsedStatusCache {
|
||||
model?: string;
|
||||
tokensUsed?: number | null;
|
||||
timeout?: boolean;
|
||||
/** v1.8.3 — ISO timestamp before which a timeout stub suppresses re-enqueue. */
|
||||
retryAt?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,3 +100,91 @@ export async function readFreshStatusText(args: {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Outcome of the read-only cache-miss resolution. The generators map this
|
||||
* onto their public return shape:
|
||||
* - `no-provider` → `{ hasProvider: false, text: <no-key fallback> }`
|
||||
* - `preparing` → `{ hasProvider: true, text: null, preparing: true }`
|
||||
*/
|
||||
export type ReadOnlyMissOutcome = "no-provider" | "preparing";
|
||||
|
||||
/**
|
||||
* v1.8.3 — resolve what a read-only status generation should return on a
|
||||
* cache miss WITHOUT running the heavy SQL gather or the blocking LLM
|
||||
* round-trip. This is the core of the holistic freeze fix: a navigation
|
||||
* request must never await an uncapped provider call.
|
||||
*
|
||||
* On a miss the route either:
|
||||
* - finds no usable provider → returns `no-provider` (the card shows the
|
||||
* no-key fallback; nothing to generate), or
|
||||
* - finds a provider → fire-and-forget enqueues a generation job and
|
||||
* returns `preparing` (the card shows a preparing state and the client
|
||||
* polls until the worker warms the cache).
|
||||
*
|
||||
* The provider probe is a cheap chain-resolve (no completion), so the GET
|
||||
* stays sub-second even on a cold cache.
|
||||
*/
|
||||
export async function resolveReadOnlyStatusMiss(args: {
|
||||
userId: string;
|
||||
metric: InsightStatusMetric;
|
||||
locale: "de" | "en";
|
||||
}): Promise<ReadOnlyMissOutcome> {
|
||||
const hasProvider = await hasUsableStatusProvider(args.userId);
|
||||
if (!hasProvider) return "no-provider";
|
||||
|
||||
const cacheAction = `insights.${args.metric}-status.${args.locale}`;
|
||||
|
||||
// v1.8.3 — honour the short-TTL negative cache. If the worker recently
|
||||
// hit a provider stall it wrote a `retryAt` stub; re-enqueuing on every
|
||||
// navigation while the provider is still degraded would be a storm. While
|
||||
// the stub is fresh, stay in `preparing` without enqueuing; once it goes
|
||||
// stale (or never existed) enqueue a fresh generation.
|
||||
if (await hasFreshTimeoutStub({ userId: args.userId, cacheAction })) {
|
||||
annotate({
|
||||
action: { name: "insights.status.preparing" },
|
||||
meta: { metric: args.metric, suppressed_enqueue: true },
|
||||
});
|
||||
return "preparing";
|
||||
}
|
||||
|
||||
// Enqueue out of band — do NOT await an LLM here. The enqueue itself is
|
||||
// best-effort and de-duped per (user, metric, locale).
|
||||
await enqueueStatusGeneration({
|
||||
userId: args.userId,
|
||||
metric: args.metric,
|
||||
locale: args.locale,
|
||||
});
|
||||
annotate({
|
||||
action: { name: "insights.status.preparing" },
|
||||
meta: { metric: args.metric },
|
||||
});
|
||||
return "preparing";
|
||||
}
|
||||
|
||||
/**
|
||||
* True when the most recent cache row for `(userId, cacheAction)` is a
|
||||
* timeout stub whose `retryAt` is still in the future. Used by the
|
||||
* read-only resolver to suppress a re-enqueue storm while a provider is
|
||||
* degraded. A stub without `retryAt` (legacy) is treated as stale so the
|
||||
* resolver retries, matching the pre-v1.8.3 "transient miss" behaviour.
|
||||
*/
|
||||
async function hasFreshTimeoutStub(args: {
|
||||
userId: string;
|
||||
cacheAction: string;
|
||||
}): Promise<boolean> {
|
||||
const latest = await prisma.auditLog.findFirst({
|
||||
where: { userId: args.userId, action: args.cacheAction },
|
||||
orderBy: { createdAt: "desc" },
|
||||
select: { details: true },
|
||||
});
|
||||
if (!latest?.details) return false;
|
||||
try {
|
||||
const parsed = JSON.parse(latest.details) as ParsedStatusCache;
|
||||
if (!isTimeoutStub(parsed)) return false;
|
||||
if (typeof parsed.retryAt !== "string") return false;
|
||||
return new Date(parsed.retryAt).getTime() > Date.now();
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -64,6 +64,22 @@ async function resolveStatusChain(
|
||||
return [{ providerType: "admin-openai", instance: legacy }];
|
||||
}
|
||||
|
||||
/**
|
||||
* Cheap provider-availability probe for the read-only status path.
|
||||
*
|
||||
* v1.8.3 — when a status route runs in read-only mode (serve cache, never
|
||||
* block on the LLM) it still has to tell the difference between "no
|
||||
* provider — show the no-key fallback" and "provider configured but the
|
||||
* assessment isn't warm yet — show preparing + enqueue a generation". This
|
||||
* resolves the same chain `runStatusCompletion` would, but does NOT run a
|
||||
* completion, so the navigation request never awaits an LLM round-trip.
|
||||
*/
|
||||
export async function hasUsableStatusProvider(
|
||||
userId: string,
|
||||
): Promise<boolean> {
|
||||
return (await resolveStatusChain(userId)) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run a status generation across the user's provider chain, bounded by
|
||||
* `STATUS_PROVIDER_TIMEOUT_MS`. The result discriminates between
|
||||
|
||||
@@ -1,32 +1,79 @@
|
||||
import { prisma } from "@/lib/db";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
|
||||
/**
|
||||
* Shared timeout / provider-error fallback for the InsightStatus routes.
|
||||
*
|
||||
* When the upstream provider call exceeds `STATUS_PROVIDER_TIMEOUT_MS`
|
||||
* (or rejects outright) the route still has to return a deterministic
|
||||
* envelope so the InsightStatusCard renders the no-key fallback text
|
||||
* instead of spinning. It used to ALSO persist a sentinel `auditLog`
|
||||
* row keyed to today's Berlin day. That stub carried the generic
|
||||
* fallback under the same `text` field a real assessment uses, the
|
||||
* cache-read served it as valid, and it stuck until midnight — hiding
|
||||
* the real, data-driven assessment for the rest of the day on any
|
||||
* account that hit a single transient stall.
|
||||
* (or rejects outright) the generator still has to return a deterministic
|
||||
* envelope. It used to persist a sentinel row carrying the generic
|
||||
* fallback under the same `text` field a real assessment uses; the
|
||||
* cache-read served it as valid and it stuck until midnight — hiding the
|
||||
* real, data-driven assessment for the rest of the day on any account that
|
||||
* hit a single transient stall. v1.4.28 removed that persist entirely.
|
||||
*
|
||||
* The fix removes the persist entirely: a timeout / error is treated as
|
||||
* a transient miss. The current render still gets the fallback text, but
|
||||
* nothing is written, so the next mount re-attempts a real generation.
|
||||
* The shared cache-read (`status-cache.ts`) additionally rejects any
|
||||
* legacy stub rows still in the table by their `model:"timeout-stub"` /
|
||||
* `timeout:true` markers.
|
||||
*
|
||||
* `cached:true` is kept so the UI does not mislabel the fallback as a
|
||||
* fresh assessment, and `updatedAt:null` signals "no persisted row".
|
||||
* v1.8.3 — re-introduce a *negative* cache, but a strictly bounded one. The
|
||||
* status route is now read-only: a cache miss enqueues an out-of-band
|
||||
* generation. If the provider stalls inside the worker, nothing stops the
|
||||
* next navigation from enqueuing again, and again — a stalled provider
|
||||
* turns into a re-enqueue storm. So the timeout path writes a short-TTL
|
||||
* negative stub (`{ timeout:true, model:"timeout-stub", retryAt }`). The
|
||||
* read-only resolver honours it: while the stub is fresh it returns
|
||||
* `preparing` WITHOUT re-enqueuing; once `retryAt` passes the stub is stale
|
||||
* and the next visit re-attempts. The stub is NEVER served as assessment
|
||||
* text — `readFreshStatusText` still rejects every timeout stub by marker,
|
||||
* so a transient stall can't hide the real assessment for the day.
|
||||
*/
|
||||
|
||||
/** Short retry window after a provider stall before a re-enqueue is allowed. */
|
||||
export const TIMEOUT_NEGATIVE_CACHE_MS = 5 * 60 * 1000;
|
||||
|
||||
/**
|
||||
* Persist a short-TTL negative-cache stub for `(userId, cacheAction)`.
|
||||
* Best-effort: a failure here just means the read-only resolver may
|
||||
* re-enqueue sooner than the window intended.
|
||||
*/
|
||||
export async function persistTimeoutNegativeStub(args: {
|
||||
userId: string;
|
||||
cacheAction: string;
|
||||
todayKey: string;
|
||||
reason: "timeout" | "error";
|
||||
}): Promise<void> {
|
||||
try {
|
||||
await prisma.auditLog.create({
|
||||
data: {
|
||||
userId: args.userId,
|
||||
action: args.cacheAction,
|
||||
details: JSON.stringify({
|
||||
dateKey: args.todayKey,
|
||||
timeout: true,
|
||||
model: "timeout-stub",
|
||||
reason: args.reason,
|
||||
retryAt: new Date(
|
||||
Date.now() + TIMEOUT_NEGATIVE_CACHE_MS,
|
||||
).toISOString(),
|
||||
}),
|
||||
},
|
||||
});
|
||||
} catch {
|
||||
// Negative cache is best-effort — never throw out of the timeout path.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the deterministic fallback envelope for a timeout / error. The
|
||||
* current render still gets the fallback text, marked `cached:true` so the
|
||||
* UI does not mislabel it as a fresh assessment and `updatedAt:null` to
|
||||
* signal "no persisted assessment". When `userId` + `todayKey` are present
|
||||
* (the worker-side generation path) a short-TTL negative stub is persisted
|
||||
* so a stalled provider doesn't trigger a re-enqueue storm.
|
||||
*/
|
||||
export function returnTimeoutFallback(input: {
|
||||
cacheAction: string;
|
||||
reason: "timeout" | "error";
|
||||
stubText: string;
|
||||
userId?: string;
|
||||
todayKey?: string;
|
||||
}): {
|
||||
hasProvider: true;
|
||||
text: string;
|
||||
@@ -37,6 +84,15 @@ export function returnTimeoutFallback(input: {
|
||||
action: { name: "insights.status.fallback_served" },
|
||||
meta: { cacheAction: input.cacheAction, reason: input.reason },
|
||||
});
|
||||
if (input.userId && input.todayKey) {
|
||||
// Fire-and-forget — the caller's render must not wait on the write.
|
||||
void persistTimeoutNegativeStub({
|
||||
userId: input.userId,
|
||||
cacheAction: input.cacheAction,
|
||||
todayKey: input.todayKey,
|
||||
reason: input.reason,
|
||||
});
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: input.stubText,
|
||||
|
||||
@@ -32,7 +32,10 @@ import {
|
||||
summarizeSeries,
|
||||
} from "@/lib/insights/status-shared";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { readFreshStatusText } from "@/lib/insights/status-cache";
|
||||
import {
|
||||
readFreshStatusText,
|
||||
resolveReadOnlyStatusMiss,
|
||||
} from "@/lib/insights/status-cache";
|
||||
import { returnTimeoutFallback } from "@/lib/insights/timeout-fallback";
|
||||
import { annotate } from "@/lib/logging/context";
|
||||
import { toBerlinDayKey } from "@/lib/tz/resolver";
|
||||
@@ -81,15 +84,25 @@ export async function generateWeightStatusForUser(
|
||||
options?: {
|
||||
locale?: string | null;
|
||||
force?: boolean;
|
||||
/**
|
||||
* v1.8.3 — read-only mode for the navigation path. On a cache miss the
|
||||
* generator does NOT run the SQL gather + blocking LLM inline; it
|
||||
* enqueues an out-of-band generation and returns a `preparing` shape so
|
||||
* the request never awaits the provider. The worker (and the nightly
|
||||
* pre-generate cron) call with `readOnly: false` to actually generate.
|
||||
*/
|
||||
readOnly?: boolean;
|
||||
},
|
||||
): Promise<{
|
||||
hasProvider: boolean;
|
||||
text: string | null;
|
||||
cached: boolean;
|
||||
updatedAt: string | null;
|
||||
preparing?: boolean;
|
||||
}> {
|
||||
const locale = normalizeLocale(options?.locale);
|
||||
const force = options?.force === true;
|
||||
const readOnly = options?.readOnly === true;
|
||||
const cacheAction = `insights.weight-status.${locale}`;
|
||||
const todayKey = toBerlinDayKey(new Date());
|
||||
|
||||
@@ -111,6 +124,31 @@ export async function generateWeightStatusForUser(
|
||||
};
|
||||
}
|
||||
|
||||
// v1.8.3 — read-only navigation path: never block on the provider.
|
||||
// Enqueue generation out of band and return preparing / no-provider.
|
||||
if (readOnly) {
|
||||
const outcome = await resolveReadOnlyStatusMiss({
|
||||
userId,
|
||||
metric: "weight",
|
||||
locale,
|
||||
});
|
||||
if (outcome === "no-provider") {
|
||||
return {
|
||||
hasProvider: false,
|
||||
text: getNoKeyWeightStatusText(locale),
|
||||
cached: true,
|
||||
updatedAt: null,
|
||||
};
|
||||
}
|
||||
return {
|
||||
hasProvider: true,
|
||||
text: null,
|
||||
cached: false,
|
||||
updatedAt: null,
|
||||
preparing: true,
|
||||
};
|
||||
}
|
||||
|
||||
// v1.4.28 FB-D2 — cap the snapshot input. BP captures three types
|
||||
// per day so 1095 = 365 d × 3 channels. Order desc + reverse so we
|
||||
// keep the most recent year of rows.
|
||||
@@ -414,6 +452,8 @@ export async function generateWeightStatusForUser(
|
||||
return returnTimeoutFallback({
|
||||
cacheAction,
|
||||
reason: outcome.kind,
|
||||
userId,
|
||||
todayKey,
|
||||
stubText: getNoKeyWeightStatusText(locale),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user