feat(integrations): resolve sync health on the server

The ledger records what the last attempt did. It has no way to say what is
no longer being attempted: a row frozen at `error_transient` for a month
reads exactly like one retrying every hour, because nothing anywhere read
the age of `lastAttemptAt`.

`sync-verdict.ts` derives the missing reading as a pure read-side
projection: eight verdicts down one ladder, no writer, no cron, nothing that
can desynchronise from the row it reads. `stalled` is the new one, and it
deliberately outranks `failing` — an integration nobody is calling is not
failing, it is not running.

Every entry on `/api/integrations/status` carries the verdict, computed
inline from data the route already holds. That placement is the point: a
card-level verdict misses a card-less provider by construction, which is how
a dead sync stayed invisible. Nightscout joins the envelope so its card can
stop fetching its own status.

The freshness module gains the workout leg (Strava writes no measurements at
all) and a source-parameterised core so the Apple Health route can reuse it.
Its per-metric `stale` flag fires only while the provider itself reads
healthy: that combination is the dead-pipe signature, and when the whole
connection has stopped every metric ages together anyway.

MCP reports the verdict beside `connected`, which only ever meant "not
explicitly disconnected".
This commit is contained in:
Marc-André Bombeck committed 2026-07-25 09:41:20 +02:00
1 parent a1a978f5e6
commit de47bfa5ba
6 files changed
+736 -86

No files matched your search

+20 -1
View File
@@ -62,6 +62,10 @@ import {
getIntegrationStatus,
type IntegrationKey,
} from "@/lib/integrations/status";
import {
INTEGRATION_CADENCE,
resolveSyncVerdict,
} from "@/lib/integrations/sync-verdict";
import { toMeasurementReminderDto } from "@/lib/measurement-reminders/dto";
import { fenceUserText, scrubFenceMarkers } from "@/lib/ai/coach/data-fence";
import type { McpAuthContext } from "./auth";
@@ -860,6 +864,8 @@ const getIntegrationStatusOutput: z.ZodRawShape = {
reauthRequired: z.boolean(),
lastSuccessAt: z.string().nullable(),
lastAttemptAt: z.string().nullable(),
verdict: z.string(),
since: z.string().nullable(),
}),
)
.optional(),
@@ -1478,7 +1484,7 @@ export const MCP_TOOLS: McpToolDefinition[] = [
name: "get_integration_status",
title: "Get device & service sync status",
description:
"Fetch the sync health of the user's connected devices and services (Withings, WHOOP, Fitbit, Nightscout, Polar, Oura, moodLog) — which are connected, when each last synced, and whether one needs reconnecting or is failing. Answers 'why is my data stale?'. Reuses the integration-status ledger; carries no secrets or tokens. Returns { present: false } when no integration has ever attempted a sync.",
"Fetch the sync health of the user's connected devices and services (Withings, WHOOP, Fitbit, Nightscout, Polar, Oura, moodLog) — which are connected, when each last synced, and whether one needs reconnecting or is failing. Answers 'why is my data stale?'. Reuses the integration-status ledger; carries no secrets or tokens. `connected` means only 'not explicitly disconnected'; `verdict` is the liveness truth (fresh / stale / stalled / failing / reauth_required / parked / pending_first_sync / disconnected), where `stalled` means the sync has stopped even attempting. Returns { present: false } when no integration has ever attempted a sync.",
inputShape: {},
annotations: READ_ONLY_ANNOTATIONS,
outputShape: getIntegrationStatusOutput,
@@ -1503,6 +1509,17 @@ export const MCP_TOOLS: McpToolDefinition[] = [
ctx.userId,
r.integration as IntegrationKey,
);
// `connected` means "not explicitly disconnected" and says nothing
// about liveness — a month-dead pull still reads true. `verdict` is
// the liveness truth, resolved by the same server-side rule the
// Settings page renders.
const health = resolveSyncVerdict({
configured: true,
state: s.state,
lastSuccessAt: s.lastSuccessAt,
lastAttemptAt: s.lastAttemptAt,
cadence: INTEGRATION_CADENCE[s.integration],
});
return {
provider: s.integration,
state: s.state,
@@ -1510,6 +1527,8 @@ export const MCP_TOOLS: McpToolDefinition[] = [
reauthRequired: s.state === "error_reauth" || s.state === "parked",
lastSuccessAt: s.lastSuccessAt,
lastAttemptAt: s.lastAttemptAt,
verdict: health.verdict,
since: health.since,
};
}),
);