mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 07:13:29 +00:00
The outbound screen let a halving with no number through in every
language but Dutch ("Reduce your dose by half.", "Reduza a dose pela
metade.", "Tome meia dose."), let a bare change imperative through
("Zmniejsz dawkę.", "복용량을 늘리세요.") and did not know the Polish
spelling of the generic stems, so "Odstaw ramipryl." passed. Each
language's bank now carries a halving and fraction shape and a bare
change shape on a medication noun, and the stems include the Polish
-pryl, -statyn and -glutyd.
The continuation exemption trusted "as before" on any surface that
passed no schedule, so "Take 40 mg as before." went out to people who
had never taken 40 mg. The screen now takes the dose plan (each active
medication with its dose) instead of a bare list of numbers, and
exempts a stated amount only when it is the planned dose of the
medication the sentence names. Without a plan no stated amount is
exempt.
330 lines
10 KiB
TypeScript
330 lines
10 KiB
TypeScript
/**
|
|
* v1.41 — plans the Coach proposes in a turn, and the person's answer.
|
|
*
|
|
* When the person names a goal or agrees to a suggestion, the Coach calls
|
|
* `propose_plan`: one if-then plan tied to one metric, with an optional
|
|
* target and a review window of 7 to 56 days. Its three fields are model
|
|
* text, so each passes the outbound screen the answer passes (dose and risk,
|
|
* in the person's locale, with their scheduled doses and medication names)
|
|
* and the injection
|
|
* screen before anything is kept; a field that trips either declines the
|
|
* call as `unsafe`. The plan is then written as `proposed` (its text
|
|
* encrypted like every plan) with the answer that carries it
|
|
* (`turn-writes.ts`): an answer that is blocked, fails or is abandoned leaves
|
|
* no plan behind. It stays inert until the person taps "Take on this plan": a
|
|
* plan is a commitment, so only the person activates it. The answer arrives as `planDecision` on the chat request and
|
|
* flips the plan to `active` (with its review date counted from the tap) or
|
|
* `abandoned`.
|
|
*
|
|
* At most one proposal per answer and {@link MAX_OPEN_PLAN_PROPOSALS} open
|
|
* proposals per person; an unanswered proposal lapses after
|
|
* {@link PROPOSAL_EXPIRY_DAYS} days (`coach-reminder-sweep`).
|
|
*
|
|
* Plan text never reaches `annotate()`.
|
|
*
|
|
* Server-only.
|
|
*/
|
|
import { prisma } from "@/lib/db";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import type { Locale } from "@/lib/i18n/config";
|
|
import { getMedicationNames } from "@/lib/medications/medication-names";
|
|
import {
|
|
getScheduledDosePlan,
|
|
type ScheduledDose,
|
|
} from "@/lib/medications/scheduled-doses";
|
|
|
|
import { screenCoachReply } from "../outbound-guard";
|
|
import { detectRefusal } from "../refusal";
|
|
|
|
import { decryptFromBytes } from "../bytes-codec";
|
|
import { isNearDuplicate } from "../facts";
|
|
import { PLAN_FIELD_MAX_CHARS } from "../plans";
|
|
import type { CoachPlanProposal } from "../types";
|
|
import type {
|
|
DecidePlanProposalArgs,
|
|
DecidePlanProposalOutcome,
|
|
ProposePlanArgs,
|
|
ProposePlanOutcome,
|
|
} from "./contract";
|
|
import {
|
|
MAX_OPEN_PLAN_PROPOSALS,
|
|
PLAN_REVIEW_DAYS,
|
|
PROPOSAL_EXPIRY_DAYS,
|
|
} from "./shared";
|
|
import { stageTurnWrite } from "./turn-writes";
|
|
|
|
const MS_PER_DAY = 86_400_000;
|
|
|
|
/** A metric key as the plans store it: `WEIGHT`, `SLEEP`, `BLOOD_PRESSURE`. */
|
|
const METRIC_KEY = /^[A-Z][A-Z0-9_]{1,59}$/;
|
|
|
|
function clean(text: string | undefined): string | null {
|
|
if (typeof text !== "string") return null;
|
|
const flat = text.replace(/\s+/g, " ").trim();
|
|
return flat.length >= 2 && flat.length <= PLAN_FIELD_MAX_CHARS ? flat : null;
|
|
}
|
|
|
|
/** The review window, clamped into {@link PLAN_REVIEW_DAYS}. */
|
|
export function clampReviewDays(days: number): number {
|
|
const whole = Number.isFinite(days) ? Math.round(days) : PLAN_REVIEW_DAYS.min;
|
|
return Math.min(PLAN_REVIEW_DAYS.max, Math.max(PLAN_REVIEW_DAYS.min, whole));
|
|
}
|
|
|
|
function decryptOrNull(buf: Uint8Array | null): string | null {
|
|
if (!buf) return null;
|
|
try {
|
|
return decryptFromBytes(buf);
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Whether model-written plan text may be kept: no dose or risk the answer's
|
|
* outbound screen would block, and no instruction smuggled into it. The
|
|
* injection screen is the one half of `detectRefusal` that applies: its
|
|
* off-topic half reads requests, and "when it rains" or "after music
|
|
* practice" is an ordinary cue, not an off-topic question.
|
|
*/
|
|
export function planTextPasses(
|
|
texts: readonly string[],
|
|
locale: Locale,
|
|
screen: {
|
|
dosePlan: readonly ScheduledDose[];
|
|
medicationNames: readonly string[];
|
|
},
|
|
): boolean {
|
|
try {
|
|
for (const text of [...texts, texts.join(" ")]) {
|
|
if (
|
|
screenCoachReply(text, locale, screen.dosePlan, screen.medicationNames)
|
|
.block
|
|
) {
|
|
return false;
|
|
}
|
|
if (
|
|
detectRefusal({ message: text, locale }).reason === "prompt_injection"
|
|
) {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/** Runs a `propose_plan` call: stages the plan as `proposed`. */
|
|
export async function proposePlanFromTool(
|
|
args: ProposePlanArgs,
|
|
): Promise<ProposePlanOutcome> {
|
|
const outcome = await runPropose(args);
|
|
annotate({
|
|
action: { name: "coach.plans.proposed" },
|
|
meta:
|
|
outcome.kind === "proposed"
|
|
? {
|
|
outcome: "proposed",
|
|
metric: outcome.proposal.metric,
|
|
reviewInDays: outcome.proposal.reviewInDays,
|
|
}
|
|
: { outcome: "declined", reason: outcome.reason },
|
|
});
|
|
return outcome;
|
|
}
|
|
|
|
async function runPropose(args: ProposePlanArgs): Promise<ProposePlanOutcome> {
|
|
const { userId, conversationId, call } = args;
|
|
const metric =
|
|
typeof call.metric === "string"
|
|
? call.metric
|
|
.trim()
|
|
.toUpperCase()
|
|
.replace(/[\s-]+/g, "_")
|
|
: "";
|
|
const ifCue = clean(call.ifCue);
|
|
const thenAction = clean(call.thenAction);
|
|
const target = call.target === undefined ? null : clean(call.target);
|
|
if (
|
|
!METRIC_KEY.test(metric) ||
|
|
ifCue === null ||
|
|
thenAction === null ||
|
|
(call.target !== undefined && target === null)
|
|
) {
|
|
return { kind: "declined", reason: "invalid" };
|
|
}
|
|
const [dosePlan, medicationNames] = await Promise.all([
|
|
args.dosePlan ??
|
|
getScheduledDosePlan(userId).catch((): ScheduledDose[] => []),
|
|
args.medicationNames ??
|
|
getMedicationNames(userId).catch((): string[] => []),
|
|
]);
|
|
if (
|
|
!planTextPasses(
|
|
[ifCue, thenAction, ...(target ? [target] : [])],
|
|
args.locale,
|
|
{ dosePlan, medicationNames },
|
|
)
|
|
) {
|
|
return { kind: "declined", reason: "unsafe" };
|
|
}
|
|
const reviewInDays = clampReviewDays(call.reviewInDays);
|
|
const now = new Date();
|
|
|
|
const turnStart = await prisma.coachMessage.findFirst({
|
|
where: { conversationId, role: "user", conversation: { userId } },
|
|
orderBy: { createdAt: "desc" },
|
|
select: { createdAt: true },
|
|
});
|
|
if (!turnStart) return { kind: "declined", reason: "unavailable" };
|
|
|
|
const open = await prisma.coachPlan.findMany({
|
|
where: {
|
|
userId,
|
|
deletedAt: null,
|
|
status: { in: ["proposed", "active"] },
|
|
},
|
|
select: {
|
|
id: true,
|
|
status: true,
|
|
ifCueEncrypted: true,
|
|
thenActionEncrypted: true,
|
|
sourceConversationId: true,
|
|
createdAt: true,
|
|
},
|
|
});
|
|
|
|
// One proposal per answer: a plan this conversation proposed since the
|
|
// person's message the turn answers. (Within the turn, the loop allows one
|
|
// `propose_plan` per answer before this runs.)
|
|
if (
|
|
open.some(
|
|
(plan) =>
|
|
plan.status === "proposed" &&
|
|
plan.sourceConversationId === conversationId &&
|
|
plan.createdAt >= turnStart.createdAt,
|
|
)
|
|
) {
|
|
return { kind: "declined", reason: "one_per_answer" };
|
|
}
|
|
|
|
const signature = `${ifCue} ${thenAction}`;
|
|
const known = open.some((plan) => {
|
|
const cue = decryptOrNull(plan.ifCueEncrypted);
|
|
const action = decryptOrNull(plan.thenActionEncrypted);
|
|
return (
|
|
cue !== null &&
|
|
action !== null &&
|
|
isNearDuplicate(signature, [`${cue} ${action}`])
|
|
);
|
|
});
|
|
if (known) return { kind: "declined", reason: "invalid" };
|
|
|
|
const lapse = new Date(now.getTime() - PROPOSAL_EXPIRY_DAYS * MS_PER_DAY);
|
|
const openProposals = open.filter(
|
|
(plan) => plan.status === "proposed" && plan.createdAt >= lapse,
|
|
).length;
|
|
if (openProposals >= MAX_OPEN_PLAN_PROPOSALS) {
|
|
return { kind: "declined", reason: "too_many_open" };
|
|
}
|
|
|
|
// Written with the answer that carries the proposal, never before.
|
|
const planId = stageTurnWrite({
|
|
kind: "plan",
|
|
userId,
|
|
conversationId,
|
|
metric,
|
|
ifCue,
|
|
thenAction,
|
|
target,
|
|
reviewInDays,
|
|
});
|
|
const proposal: CoachPlanProposal = {
|
|
planId,
|
|
metric,
|
|
reviewInDays,
|
|
ifCue,
|
|
thenAction,
|
|
...(target ? { target } : {}),
|
|
};
|
|
return { kind: "proposed", proposal };
|
|
}
|
|
|
|
/** The `planProposal` an assistant message carries in its stored metadata. */
|
|
function proposedPlanId(metricSourceJson: string | null): string | null {
|
|
if (!metricSourceJson) return null;
|
|
try {
|
|
const parsed = JSON.parse(metricSourceJson) as {
|
|
planProposal?: { planId?: unknown };
|
|
};
|
|
const id = parsed?.planProposal?.planId;
|
|
return typeof id === "string" ? id : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Answers a plan proposal: `active` on accept, `abandoned` on decline. Only a
|
|
* plan the assistant message proposed, and only the person's own.
|
|
*/
|
|
export async function decidePlanProposal(
|
|
args: DecidePlanProposalArgs,
|
|
): Promise<DecidePlanProposalOutcome> {
|
|
const outcome = await runDecide(args).catch(
|
|
(): DecidePlanProposalOutcome => ({ kind: "stale" }),
|
|
);
|
|
annotate({
|
|
action: { name: "coach.plans.decided" },
|
|
meta: { accept: args.accept, outcome: outcome.kind },
|
|
});
|
|
return outcome;
|
|
}
|
|
|
|
async function runDecide(
|
|
args: DecidePlanProposalArgs,
|
|
): Promise<DecidePlanProposalOutcome> {
|
|
const { userId, conversationId, messageId, planId, accept } = args;
|
|
const message = await prisma.coachMessage.findFirst({
|
|
where: {
|
|
id: messageId,
|
|
conversationId,
|
|
role: "assistant",
|
|
conversation: { userId },
|
|
},
|
|
select: { metricSourceJson: true },
|
|
});
|
|
if (!message || proposedPlanId(message.metricSourceJson) !== planId) {
|
|
return { kind: "stale" };
|
|
}
|
|
const plan = await prisma.coachPlan.findFirst({
|
|
where: { id: planId, userId, deletedAt: null, status: "proposed" },
|
|
select: { createdAt: true, reviewDate: true },
|
|
});
|
|
if (!plan) return { kind: "stale" };
|
|
|
|
const now = new Date();
|
|
if (!accept) {
|
|
const { count } = await prisma.coachPlan.updateMany({
|
|
where: { id: planId, userId, deletedAt: null, status: "proposed" },
|
|
data: { status: "abandoned", reviewDate: null },
|
|
});
|
|
return count > 0 ? { kind: "abandoned" } : { kind: "stale" };
|
|
}
|
|
|
|
// The window the Coach proposed, counted from the tap.
|
|
const reviewInDays = clampReviewDays(
|
|
plan.reviewDate
|
|
? (plan.reviewDate.getTime() - plan.createdAt.getTime()) / MS_PER_DAY
|
|
: PLAN_REVIEW_DAYS.min,
|
|
);
|
|
const { count } = await prisma.coachPlan.updateMany({
|
|
where: { id: planId, userId, deletedAt: null, status: "proposed" },
|
|
data: {
|
|
status: "active",
|
|
reviewDate: new Date(now.getTime() + reviewInDays * MS_PER_DAY),
|
|
},
|
|
});
|
|
return count > 0 ? { kind: "activated", reviewInDays } : { kind: "stale" };
|
|
}
|