mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-18 08:02:12 +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
@@ -31,6 +31,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
import { prisma } from "@/lib/db";
|
||||
|
||||
@@ -41,6 +41,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -39,6 +39,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -53,6 +53,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -53,6 +53,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -78,6 +78,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -57,6 +57,8 @@ vi.mock("@/lib/ai/capabilities/egress", () => ({
|
||||
}));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord: (...a: unknown[]) => aiCapabilityForRecord(...a),
|
||||
aiCapabilityToServe: (...args: unknown[]) =>
|
||||
(aiCapabilityForRecord as (...a: unknown[]) => unknown)(...args),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -51,6 +51,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -50,6 +50,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -55,6 +55,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -57,6 +57,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
})),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", () => ({
|
||||
resolveProviderChain: (...a: unknown[]) => resolveProviderChain(...a),
|
||||
|
||||
@@ -31,6 +31,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -44,6 +44,8 @@ vi.mock("@/lib/jobs/insight-status-generate-shared", () => ({
|
||||
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord: (...a: unknown[]) => aiCapabilityForRecord(...a),
|
||||
aiCapabilityToServe: (...args: unknown[]) =>
|
||||
(aiCapabilityForRecord as (...a: unknown[]) => unknown)(...args),
|
||||
aiCapabilityForJob: vi.fn(async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
|
||||
@@ -98,6 +98,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -29,6 +29,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -38,6 +38,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -30,6 +30,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -54,6 +54,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -61,6 +61,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -42,6 +42,11 @@ vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
aiCapabilityToServe: async () => ({
|
||||
available: true,
|
||||
reason: null,
|
||||
onDeviceAllowed: true,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/insights/memory", () => ({
|
||||
|
||||
@@ -38,6 +38,8 @@ vi.mock("@/lib/ai/provider", () => ({
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord: (...args: unknown[]) =>
|
||||
aiCapabilityForRecord(...(args as [])),
|
||||
aiCapabilityToServe: (...args: unknown[]) =>
|
||||
(aiCapabilityForRecord as (...a: unknown[]) => unknown)(...args),
|
||||
}));
|
||||
|
||||
vi.mock("@/lib/sharing/delegated-generation", () => ({
|
||||
|
||||
@@ -30,6 +30,8 @@ vi.mock("@/lib/ai/coach/bytes-codec", () => ({
|
||||
const aiCapabilityForRecord = vi.fn();
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord: (...a: unknown[]) => aiCapabilityForRecord(...a),
|
||||
aiCapabilityToServe: (...args: unknown[]) =>
|
||||
(aiCapabilityForRecord as (...a: unknown[]) => unknown)(...args),
|
||||
}));
|
||||
|
||||
const probeProviderPresence = vi.fn();
|
||||
|
||||
@@ -75,7 +75,10 @@ vi.mock("@/lib/ai/provider", () => ({
|
||||
const { aiCapabilityForRecord } = vi.hoisted(() => ({
|
||||
aiCapabilityForRecord: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({ aiCapabilityForRecord }));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord,
|
||||
aiCapabilityToServe: aiCapabilityForRecord,
|
||||
}));
|
||||
|
||||
const { runRawCompletionWithFallback } = vi.hoisted(() => ({
|
||||
runRawCompletionWithFallback: vi.fn(),
|
||||
|
||||
@@ -48,7 +48,10 @@ const {
|
||||
probeProviderPresence: vi.fn(),
|
||||
enqueueStatusGeneration: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({ aiCapabilityForRecord }));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord,
|
||||
aiCapabilityToServe: aiCapabilityForRecord,
|
||||
}));
|
||||
vi.mock("@/lib/ai/provider", async (importOriginal) => ({
|
||||
...(await importOriginal<typeof import("@/lib/ai/provider")>()),
|
||||
probeProviderPresence,
|
||||
|
||||
@@ -20,7 +20,10 @@ vi.mock("@/lib/db", () => ({ prisma: {} }));
|
||||
const { aiCapabilityForRecord } = vi.hoisted(() => ({
|
||||
aiCapabilityForRecord: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({ aiCapabilityForRecord }));
|
||||
vi.mock("@/lib/ai/capabilities/gate", () => ({
|
||||
aiCapabilityForRecord,
|
||||
aiCapabilityToServe: aiCapabilityForRecord,
|
||||
}));
|
||||
|
||||
const UNAVAILABLE = {
|
||||
available: false,
|
||||
|
||||
@@ -32,7 +32,7 @@ import { annotate } from "@/lib/logging/context";
|
||||
import { AI_BUDGETS, REFERENCE_AI_SEED } from "@/lib/ai/ai-budgets";
|
||||
import { dayRotatedSeed } from "@/lib/ai/prompts/opener-archetype";
|
||||
import { runStatusCompletion } from "@/lib/insights/status-provider";
|
||||
import { aiCapabilityForRecord } from "@/lib/ai/capabilities/gate";
|
||||
import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
|
||||
import {
|
||||
buildPeriodNarrativeContext,
|
||||
type NarrativePeriod,
|
||||
@@ -565,14 +565,15 @@ export async function readPeriodNarrative(
|
||||
/**
|
||||
* Whether a stored narrative may be served: always for the deterministic
|
||||
* narrative, and for model-written prose only while `periodNarrative` is
|
||||
* available for the record.
|
||||
* available for the record, answered from the record's own state whoever is
|
||||
* reading.
|
||||
*/
|
||||
async function narrativeServable(
|
||||
userId: string,
|
||||
providerType: string | null,
|
||||
): Promise<boolean> {
|
||||
if (providerType === DETERMINISTIC_PROVIDER_TYPE) return true;
|
||||
return (await aiCapabilityForRecord(userId, "periodNarrative")).available;
|
||||
return (await aiCapabilityToServe(userId, "periodNarrative")).available;
|
||||
}
|
||||
|
||||
/** Prisma `Bytes` ↔ ciphertext, mirroring the CoachMessage helper. */
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import type { MeasurementType } from "@/generated/prisma/client";
|
||||
import { prisma } from "@/lib/db";
|
||||
import { decryptFromBytes, encryptToBytes } from "@/lib/ai/coach/bytes-codec";
|
||||
import { aiCapabilityForRecord } from "@/lib/ai/capabilities/gate";
|
||||
import { aiCapabilityToServe } from "@/lib/ai/capabilities/gate";
|
||||
import type { AiUnavailableReason } from "@/lib/ai/capabilities/types";
|
||||
import { probeProviderPresence } from "@/lib/ai/provider";
|
||||
import {
|
||||
@@ -161,10 +161,12 @@ async function readStatusNote(
|
||||
|
||||
/**
|
||||
* Whether stored status notes may be served, or re-dated, for this record.
|
||||
* Every reader and both unchanged-data gates ask this first.
|
||||
* Every reader and both unchanged-data gates ask this first. Answered from
|
||||
* the record's own state, so a delegate reads the owner's notes exactly when
|
||||
* the owner would; starting work is a separate question (see below).
|
||||
*/
|
||||
async function statusTextServable(userId: string): Promise<boolean> {
|
||||
return (await aiCapabilityForRecord(userId, "statusText")).available;
|
||||
return (await aiCapabilityToServe(userId, "statusText")).available;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -708,7 +710,9 @@ export async function resolveReadOnlyStatusMiss(args: {
|
||||
metric: InsightStatusScope;
|
||||
locale: SupportedLocale;
|
||||
}): Promise<ReadOnlyMissOutcome> {
|
||||
const capability = await aiCapabilityForRecord(args.userId, "statusText");
|
||||
// Serving view: whether the record's notes may be shown at all. Starting
|
||||
// work for a delegate is refused by the suppression branch below.
|
||||
const capability = await aiCapabilityToServe(args.userId, "statusText");
|
||||
if (!capability.available) {
|
||||
const reason = capability.reason ?? "check_failed";
|
||||
annotate({
|
||||
|
||||
Reference in new issue
Block a user