feat(ai): show stored model text inside a shared record as the owner would

Serving stored text and starting model work are separate questions. A
delegate may not start model work on somebody else's record, but reads
the owner's stored briefing, period narrative, status notes, workout
notes and Coach unread signal exactly when the owner would: those
readers now resolve the record's own capability state, not the
viewer's. Generation, warms and enqueues still ask as the viewer, so a
delegate's read of a stale narrative queues nothing. A hidden
model-written narrative is no longer overwritten by a deterministic
warm while AI is unavailable.

The sharing front door reads the digest's briefing lead again, and an
integration case pins both halves.

Refs #975
This commit is contained in:
Marc-André Bombeck committed 2026-09-24 10:17:00 +02:00
1 parent 7df260d62e
commit 2143445da2
49 files changed
+306 -93

No files matched your search

+2 -2
View File
@@ -30,7 +30,7 @@ import { apiError, apiSuccess } from "@/lib/api-response";
import { annotate } from "@/lib/logging/context";
import { pickCanonicalWorkoutRows } from "@/lib/measurements/pick-canonical-workout-rows";
import { requireModuleEnabled } from "@/lib/modules/gate";
import { getAiCapability } from "@/lib/ai/capabilities/gate";
import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
import { getAgeFromDateOfBirth } from "@/lib/analytics/pulse-targets";
import { decryptFromBytes } from "@/lib/ai/coach/bytes-codec";
import { buildWorkoutHrSeries } from "@/lib/workouts/hr-series";
@@ -65,7 +65,7 @@ export const GET = apiHandler(
// served only while `workoutInsights` is available (operator switch, the
// AI analysis opt-out, provider presence, consent). Otherwise the row is
// not even loaded, `aiInsight` is null, and `ai` says why.
const ai = await getAiCapability("workoutInsights");
const ai = await aiCapabilityToServe(user.id, "workoutInsights");
const row = await prisma.workout.findUnique({
where: { id },
@@ -64,13 +64,13 @@ vi.mock("@/lib/modules/gate", async (importOriginal) => {
});
// The Activity Insight paragraph follows the `workoutInsights` capability.
vi.mock("@/lib/ai/capabilities/gate", () => ({ getAiCapability: vi.fn() }));
vi.mock("@/lib/ai/capabilities/gate", () => ({ aiCapabilityToServe: vi.fn() }));
import { GET } from "../[id]/route";
import { prisma } from "@/lib/db";
import { getSession } from "@/lib/auth/session";
import { requireModuleEnabled } from "@/lib/modules/gate";
import { getAiCapability } from "@/lib/ai/capabilities/gate";
import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
import {
AI_AVAILABLE,
aiUnavailable,
@@ -146,7 +146,7 @@ describe("GET /api/workouts/{id}", () => {
vi.mocked(requireModuleEnabled).mockResolvedValue({
enabled: true,
} as never);
vi.mocked(getAiCapability).mockResolvedValue(AI_AVAILABLE);
vi.mocked(aiCapabilityToServe).mockResolvedValue(AI_AVAILABLE);
// The timezone resolver caches per user for 60 s in process; drop the
// entry so a test that pins a zone is not served the previous one.
invalidateUserTimezone(SESSION_OK.user.id);
@@ -572,7 +572,7 @@ describe("GET /api/workouts/{id} — aiInsight", () => {
vi.clearAllMocks();
vi.stubEnv("ENCRYPTION_KEY", TEST_KEY);
_resetCryptoCacheForTests();
vi.mocked(getAiCapability).mockResolvedValue(AI_AVAILABLE);
vi.mocked(aiCapabilityToServe).mockResolvedValue(AI_AVAILABLE);
vi.mocked(getSession).mockResolvedValue(SESSION_OK as never);
vi.mocked(prisma.user.findUnique).mockResolvedValue({
sourcePriorityJson: null,
@@ -624,7 +624,10 @@ describe("GET /api/workouts/{id} — aiInsight", () => {
paragraph: "A steady, aerobic-leaning run.",
generatedAt: generatedAt.toISOString(),
});
expect(getAiCapability).toHaveBeenCalledWith("workoutInsights");
expect(aiCapabilityToServe).toHaveBeenCalledWith(
"user-1",
"workoutInsights",
);
expect(body.data.ai).toEqual(AI_AVAILABLE);
});
@@ -637,7 +640,7 @@ describe("GET /api/workouts/{id} — aiInsight", () => {
] as const)(
"does not read or expose the stored paragraph while workoutInsights is %s",
async (reason) => {
vi.mocked(getAiCapability).mockResolvedValue(aiUnavailable(reason));
vi.mocked(aiCapabilityToServe).mockResolvedValue(aiUnavailable(reason));
vi.mocked(prisma.workout.findUnique).mockResolvedValue({
...BASE_ROW,
insight: {