feat(ai): serve Insights and Coach data whatever AI says

Data never depends on AI. The Insights data reads no longer ask an AI
switch or the `insights` module (now only the AI analysis opt-out): ECG
list, detail and upload, rhythm events, derived scores, rule cards,
correlations and patterns, the baseline strip, health status, intraday
pulse, and the Today digest with its dismiss. Breathing screening moves
to the sleep module and lab changes to the labs module. Stored Coach
facts, conversations, message ratings and the seen stamp stay readable
and erasable while the Coach is unavailable. Cards now say
`provider: "rules"`.

Reads that mix data with model text answer 200, null the model text
and publish an `ai` state from the capability resolver, without warming
anything: comprehensive (`ai.briefing`, `ai.statusText`, `hasProvider`
from the presence probe), the derived assessment override, the period
narrative, nudge status, the seeded opener, about-me questions, the
workout paragraph, the pregenerate warm (`queued: false`), the Today
digest (lead, reaction line, check-in) and the dashboard briefing,
which is now applied per read after the snapshot cache. Hiding the
Coach no longer hides the briefing.

`insights/generate` GET and POST ask the `briefing` capability and
`insights/chat` POST the `coach` capability, with the typed refusal
envelope; a missing provider keeps the chat's `coach.provider.none`
frame and adds `reason`. The /coach page, the MCP ECG and intraday
tools and `aiAvailable` on the provider route follow the same answer.

An inventory of AI and data routes replaces the Coach gate inventory:
every route that asks a capability names it, and no data route asks.

Refs #975
This commit is contained in:
Marc-André Bombeck committed 2026-09-24 09:19:36 +02:00
1 parent f434ba7d29
commit 3309ac177c
104 files changed
+3299 -1636

No files matched your search

@@ -505,15 +505,15 @@ describe("GET /api/user/ai-provider — the shared-provider offer", () => {
},
);
it("never offers when the operator switched the assistant surfaces off", async () => {
it("never offers when the operator switched off everything the consent would unlock", async () => {
serverManaged();
vi.mocked(readServerProviderHealth).mockResolvedValue("healthy");
vi.mocked(getAssistantFlags).mockResolvedValue({
enabled: true,
coach: false,
briefing: true,
briefing: false,
insightStatus: true,
documentAi: true,
documentAi: false,
});
await expect(read()).resolves.toMatchObject({
serverProviderHealth: "healthy",
@@ -521,6 +521,41 @@ describe("GET /api/user/ai-provider — the shared-provider offer", () => {
});
});
it.each(["coach", "briefing", "documentAi"] as const)(
"still offers while the operator keeps %s on",
async (open) => {
serverManaged();
vi.mocked(readServerProviderHealth).mockResolvedValue("healthy");
vi.mocked(getAssistantFlags).mockResolvedValue({
enabled: true,
coach: false,
briefing: false,
insightStatus: false,
documentAi: false,
[open]: true,
});
await expect(read()).resolves.toMatchObject({
serverProviderOffer: true,
});
},
);
it("reports no AI available while the operator's master switch is off, whatever is configured", async () => {
serverManaged();
vi.mocked(getAssistantFlags).mockResolvedValue({
enabled: false,
coach: false,
briefing: false,
insightStatus: false,
documentAi: false,
});
await expect(read()).resolves.toMatchObject({
aiAvailable: false,
managedBy: "server",
serverProviderOffer: false,
});
});
it("never offers to a managed profile — a child's record does not consent for itself", async () => {
serverManaged();
vi.mocked(readServerProviderHealth).mockResolvedValue("healthy");
+20 -4
View File
@@ -53,7 +53,17 @@ export const GET = apiHandler(async () => {
// no personal provider. iOS keys its Coach visibility off `aiAvailable` so
// a server-managed provider is no longer invisible to the client.
// `managedBy` reports the origin only; no admin keys/endpoints are leaked.
const { aiAvailable, managedBy } = await resolveProviderAvailability(user.id);
//
// The operator's master switch is part of the answer: with every AI
// capability switched off there is no AI available, whatever is
// configured, so a native client that keys on this field hides its AI
// surfaces. The switches fail closed (a read error reads as all off).
const [presence, assistantFlags] = await Promise.all([
resolveProviderAvailability(user.id),
getAssistantFlags(),
]);
const aiAvailable = presence.aiAvailable && assistantFlags.enabled;
const { managedBy } = presence;
// ── v1.38.19 — the shared provider, honestly ────────────────
// `managedBy: "server"` says the operator configured a key. It has never
@@ -77,7 +87,6 @@ export const GET = apiHandler(async () => {
user.id,
"coach",
);
const assistantFlags = await getAssistantFlags();
// `personal`, not merely "not deny": a guardian's own record resolves to
// `personal`, while a managed profile resolves to `operator-default` —
// its provider is the operator's by definition, and a child's record does
@@ -93,13 +102,20 @@ export const GET = apiHandler(async () => {
// one visitor minted would turn the operator's provider on for every later
// one. A button that can only 403 into a generic toast is precisely the
// misleading failure this surface exists to remove, so there is no button.
//
// The offer is worth making while any capability it would unlock is
// switched on by the operator: the Coach, the briefing, or reading
// documents. With all three off there is nothing for the consent to open.
const demoInstance = process.env.DEMO_MODE === "true";
const offerUnlocksSomething =
assistantFlags.coach ||
assistantFlags.briefing ||
assistantFlags.documentAi;
const serverProviderOffer =
!demoInstance &&
managedBy === "server" &&
serverProviderHealth === "healthy" &&
assistantFlags.enabled &&
assistantFlags.coach &&
offerUnlocksSomething &&
credentialPolicy === "personal" &&
!serverProviderConsent;