mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 15:42:24 +00:00
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:
1 parent
7df260d62e
commit
2143445da2
49 files changed
+306
-93
No files matched your search
@@ -419,3 +419,72 @@ describe("the status family", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("inside somebody else's record (S8, a MANAGE delegate)", () => {
|
||||
async function seedOwnerText(recordId: string): Promise<void> {
|
||||
await seed(recordId);
|
||||
const prisma = getPrismaClient();
|
||||
// The narrative is stale, so an owner's read would warm it.
|
||||
await prisma.insightNarrative.updateMany({
|
||||
where: { userId: recordId },
|
||||
data: { updatedAt: new Date(Date.now() - 48 * 60 * 60 * 1000) },
|
||||
});
|
||||
const { writeStatusNote, statusCacheAction } =
|
||||
await import("@/lib/insights/status-cache");
|
||||
const { userDayKey } = await import("@/lib/tz/format");
|
||||
await writeStatusNote({
|
||||
userId: recordId,
|
||||
cacheAction: statusCacheAction("weight", "en"),
|
||||
todayKey: userDayKey(new Date(), "UTC"),
|
||||
text: "The owner's weight note.",
|
||||
});
|
||||
}
|
||||
|
||||
it("reads the owner's stored model text exactly as the owner would", async () => {
|
||||
await enterState("S8", seedOwnerText);
|
||||
|
||||
const narrative = await readNarrative();
|
||||
expect(narrative.data.narrative?.text).toBe(
|
||||
"A model wrote this retrospective.",
|
||||
);
|
||||
expect(narrative.data.ai.available).toBe(true);
|
||||
|
||||
const snapshot = await readSnapshot();
|
||||
expect(snapshot.data.briefing).not.toBeNull();
|
||||
|
||||
const digest = await readDigest();
|
||||
expect(digest.data.briefingLead).toBe("Model prose about your week.");
|
||||
|
||||
const { GET } = await import("@/app/api/insights/weight-status/route");
|
||||
const status = await json<{ text: string | null }>(
|
||||
await GET(new NextRequest("http://localhost/api/insights/weight-status")),
|
||||
);
|
||||
expect(status.status).toBe(200);
|
||||
expect(status.data.text).toBe("The owner's weight note.");
|
||||
});
|
||||
|
||||
it("starts no model work on the owner's record", async () => {
|
||||
await enterState("S8", seedOwnerText);
|
||||
|
||||
// A stale narrative the owner's read would warm: the delegate's does not.
|
||||
const narrative = await readNarrative();
|
||||
expect(narrative.data.revalidating).toBe(false);
|
||||
expect(enqueueNarrativeWarm).not.toHaveBeenCalled();
|
||||
|
||||
// Every generating route refuses a delegate before any work.
|
||||
const { POST: chat } = await import("@/app/api/insights/chat/route");
|
||||
const chatRes = await chat(
|
||||
new NextRequest("http://localhost/api/insights/chat", {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ message: "How was my week?" }),
|
||||
}),
|
||||
);
|
||||
expect(chatRes.status).toBe(403);
|
||||
|
||||
const warm = await pregenerate();
|
||||
expect(warm.status).toBe(403);
|
||||
expect(enqueueForceWarm).not.toHaveBeenCalled();
|
||||
expect(await getPrismaClient().coachConversation.count()).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -261,6 +261,17 @@ frontDoorRead("GET /api/dashboard/snapshot", {
|
||||
read: (data: { user: { username: string } }) => data.user.username,
|
||||
});
|
||||
|
||||
/** The person's own provider (presence only) and an AI consent receipt. */
|
||||
function ownProvider() {
|
||||
return {
|
||||
aiProvider: "ANTHROPIC",
|
||||
aiAnthropicKeyEncrypted: "v1:presence-only",
|
||||
consentReceipts: {
|
||||
create: { kind: "ai_full", artefact: "test", signedAt: new Date() },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/** A parseable cached briefing — the only thing the digest lifts prose from. */
|
||||
function cachedBriefing(marker: string): string {
|
||||
return JSON.stringify({
|
||||
@@ -272,12 +283,16 @@ function cachedBriefing(marker: string): string {
|
||||
}
|
||||
|
||||
frontDoorRead("GET /api/daily/digest", {
|
||||
// The briefing lead is stored model text: shown whenever the record's own
|
||||
// `briefing` capability allows it, whoever reads. Each account therefore
|
||||
// has its own provider (presence only) and consent.
|
||||
seed: async (userId, marker) => {
|
||||
await getPrismaClient().user.update({
|
||||
where: { id: userId },
|
||||
data: {
|
||||
insightsCachedText: cachedBriefing(marker),
|
||||
insightsCachedAt: new Date(),
|
||||
...ownProvider(),
|
||||
},
|
||||
});
|
||||
},
|
||||
@@ -342,6 +357,10 @@ async function seedAssistantMessage(userId: string, at: string) {
|
||||
|
||||
frontDoorRead("GET /api/insights/coach/nudge-status", {
|
||||
seed: async (userId, marker) => {
|
||||
await getPrismaClient().user.update({
|
||||
where: { id: userId },
|
||||
data: ownProvider(),
|
||||
});
|
||||
await seedAssistantMessage(
|
||||
userId,
|
||||
marker === "owner" ? "2026-07-01T09:00:00Z" : "2026-07-02T09:00:00Z",
|
||||
|
||||
Reference in new issue
Block a user