mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-16 15:23:29 +00:00
The nightly warm, the forced warm and the morning refresh bound a comprehensive generation and abort it when the bound fires, but the abort only reached the generator's final check before the cache write. The provider calls never saw it: the call in flight ran on detached, and a reply that landed after the bound could still start the JSON-shape, grounding or screen retry, each a full generation nobody was waiting for. runBriefingCompletion now takes the caller's signal, hands it to the provider call, and reserves and sends nothing once it has fired. The generator passes it at all five of its calls (the generation, its three retries and the phrasing re-roll). The on-demand route passes none on purpose: it finishes and writes its briefing after the browser stopped waiting. Refs #1279
261 lines
10 KiB
TypeScript
261 lines
10 KiB
TypeScript
/**
|
|
* Metered provider chokepoint for the comprehensive-briefing tier.
|
|
*
|
|
* The status/reference tier funnels every generation through
|
|
* `runStatusCompletion`, which reserves against the day's token ledger before
|
|
* egress and reconciles after. The comprehensive briefing did not: both the
|
|
* shared generator (`comprehensive-generate.ts`) and the on-demand
|
|
* `POST /api/insights/generate` route called `runRawCompletionWithFallback`
|
|
* directly, so none of that spend reached `coach_usage` and no daily ceiling
|
|
* applied to it. The briefing is the single most expensive generation in the
|
|
* product — a full feature snapshot in, a multi-section JSON payload out — and
|
|
* it retries up to twice per run (JSON-shape retry, grounding-correction
|
|
* retry), so an unmetered run could cost three full generations.
|
|
*
|
|
* This is the same accounting the status tier uses, not a second mechanism:
|
|
* `reserveBudget` (one atomic upsert-increment, no read-then-write window) +
|
|
* `resolveDailyCapFor("job", chain)` for the cost owner + `reconcileSpend` against the
|
|
* provider's reported count. Every provider call on the briefing path routes
|
|
* through here, so a retry is reserved and charged like any other call — a
|
|
* user at the ceiling does not get a free correction pass.
|
|
*
|
|
* The cap follows the COST OWNER. `resolveDailyCapFor` charges
|
|
* the operator ceiling only when the chain's primary is the operator's own
|
|
* credential (`admin-openai` / `admin-codex`); a self-hoster on their own key,
|
|
* their own ChatGPT plan, or a local model is measured against the generous
|
|
* user-plan ceiling, so their hardware is never rationed by the operator's
|
|
* bill.
|
|
*
|
|
* Callers own the consent gate and MUST apply it before calling in — a
|
|
* consent-blocked user must never have budget reserved against them, because
|
|
* nothing is ever sent upstream on their behalf.
|
|
*/
|
|
import {
|
|
runRawCompletionWithFallback,
|
|
type ProviderChainResolved,
|
|
type RunRawWithFallbackResult,
|
|
} from "@/lib/ai/provider-runner";
|
|
import { spentOnFailure } from "@/lib/ai/reasoning/answerless";
|
|
import {
|
|
buildDateKey,
|
|
reconcileSpend,
|
|
reserveBudget,
|
|
resolveCostOwner,
|
|
resolveDailyCapFor,
|
|
} from "@/lib/ai/coach/budget";
|
|
import { singleUserTurn } from "@/lib/ai/types";
|
|
import { annotate } from "@/lib/logging/context";
|
|
import {
|
|
REASONING_THINKING_BUDGET,
|
|
type BackgroundReasoningJob,
|
|
} from "@/lib/ai/reasoning/levels";
|
|
import { resolveJobReasoning } from "@/lib/ai/reasoning/controls";
|
|
|
|
/**
|
|
* Which call on the briefing path this was. Threaded onto the refusal
|
|
* annotation so an operator can tell a first-pass refusal (the whole briefing
|
|
* is missing) apart from a retry refusal (the first pass landed, the
|
|
* correction did not).
|
|
*/
|
|
export type BriefingCompletionStage =
|
|
"generate" | "json-retry" | "grounding-retry" | "screen-retry" | "reroll";
|
|
|
|
/**
|
|
* Thrown when the reservation would push the user past the day's ceiling.
|
|
*
|
|
* Deliberately a distinct type rather than a generic provider error: the
|
|
* callers map it to their own honest outcome (a typed `skipped` for the
|
|
* off-request generator, a 429 for the on-demand route) instead of reporting
|
|
* an upstream failure that never happened. No provider was contacted.
|
|
*/
|
|
export class BriefingBudgetExceededError extends Error {
|
|
constructor(
|
|
readonly stage: BriefingCompletionStage,
|
|
readonly totalAfter: number,
|
|
) {
|
|
super("insights.briefing.budget_exceeded");
|
|
this.name = "BriefingBudgetExceededError";
|
|
}
|
|
}
|
|
|
|
export interface RunBriefingCompletionArgs {
|
|
userId: string;
|
|
/** The resolved provider chain. Its primary decides the daily cap. */
|
|
chain: ProviderChainResolved[];
|
|
systemPrompt: string;
|
|
userPrompt: string;
|
|
temperature: number;
|
|
maxTokens: number;
|
|
/** Upstream per-request timeout, already resolved from the user's setting. */
|
|
timeoutMs: number;
|
|
stage: BriefingCompletionStage;
|
|
/**
|
|
* Deterministic seed. Omitted by the daily-briefing re-roll on purpose —
|
|
* a pinned seed would reproduce the phrasing the re-roll exists to vary.
|
|
*/
|
|
seed?: number;
|
|
/** Budget day-key override; defaults to the account's today. Tests pin it. */
|
|
dateKey?: string;
|
|
/**
|
|
* v1.41 — `daily_briefing` on the calls that may reason (the generation and
|
|
* its phrasing re-roll). Absent on the JSON-shape and grounding retries,
|
|
* which repair a reply rather than think it through again, and on every
|
|
* caller outside the background generator. `maxTokens` stays the answer
|
|
* budget; the client adds the thinking budget itself.
|
|
*/
|
|
reasoningJob?: BackgroundReasoningJob;
|
|
/**
|
|
* #1279 — a person is waiting on this call: the on-demand "Re-run
|
|
* analysis". It keeps the background share of the day's ceiling, and walks
|
|
* a provider the health ledger benched at the tail of the chain instead of
|
|
* leaving it out, so the person gets that provider's own answer or refusal.
|
|
* Absent for the scheduled generator.
|
|
*/
|
|
interactive?: boolean;
|
|
/**
|
|
* #1279 — the caller's outer bound (the nightly and forced warms, the
|
|
* morning refresh). It reaches every provider call, so a call the bound
|
|
* gave up on is torn down, and once it fired no further call is reserved
|
|
* or sent: a briefing that outlives its bound does not keep running
|
|
* detached. The on-demand route passes none on purpose: it finishes and
|
|
* writes its briefing after the browser stopped waiting (#786).
|
|
*/
|
|
signal?: AbortSignal;
|
|
}
|
|
|
|
/**
|
|
* Reserve, run the chain, reconcile. Returns the runner's result untouched so
|
|
* every existing caller keeps its own parsing, grounding, and error mapping.
|
|
*
|
|
* Failure semantics:
|
|
* - over the ceiling → `BriefingBudgetExceededError`, nothing reserved
|
|
* (the reservation is refunded inside `reserveBudget`), no egress.
|
|
* - provider threw → the reservation is refunded IN FULL and the original
|
|
* error re-thrown, so `AllProvidersFailedError` still reaches the callers
|
|
* that map it to a user-facing status.
|
|
* - provider returned → charged the reported count, net of cached input
|
|
* tokens; falls back to the reservation when the provider reports no
|
|
* count, so an unreported generation is never billed as free.
|
|
*/
|
|
export async function runBriefingCompletion(
|
|
args: RunBriefingCompletionArgs,
|
|
): Promise<RunRawWithFallbackResult> {
|
|
args.signal?.throwIfAborted();
|
|
const dateKey = args.dateKey ?? (await buildDateKey(args.userId));
|
|
|
|
// v1.41 — one resolution per call: the operator's switch and cap, the payer
|
|
// from the chain's primary. `undefined` is today's wire.
|
|
const reasoning = args.reasoningJob
|
|
? await resolveJobReasoning(args.reasoningJob, args.chain, args.userId)
|
|
: undefined;
|
|
const thinkingTokens =
|
|
reasoning && reasoning.effort !== "off"
|
|
? REASONING_THINKING_BUDGET[reasoning.effort]
|
|
: 0;
|
|
|
|
// Reserve an ESTIMATE up front — the output ceiling plus a ~4-chars-per-token
|
|
// approximation of the prompt about to be sent — and reconcile against the
|
|
// provider's reported count afterwards. Same shape as the status tier.
|
|
// Thinking is billed as output, so its budget is reserved too.
|
|
const estimatedTokens =
|
|
args.maxTokens +
|
|
thinkingTokens +
|
|
Math.ceil((args.systemPrompt.length + args.userPrompt.length) / 4);
|
|
|
|
// v1.38.19 — a background surface: half the day's ceiling when the
|
|
// operator funds the chain.
|
|
const jobCap = resolveDailyCapFor("job", args.chain);
|
|
const reservation = await reserveBudget(
|
|
args.userId,
|
|
estimatedTokens,
|
|
dateKey,
|
|
jobCap,
|
|
resolveCostOwner(args.chain),
|
|
"job",
|
|
);
|
|
if (!reservation.allowed) {
|
|
annotate({
|
|
action: { name: "insights.briefing.budget_exceeded" },
|
|
meta: {
|
|
stage: args.stage,
|
|
owner: reservation.owner,
|
|
surface: "job",
|
|
limit: reservation.limit,
|
|
cap: jobCap,
|
|
totalAfter: reservation.totalAfter,
|
|
operatorAfter: reservation.operatorAfter,
|
|
},
|
|
});
|
|
throw new BriefingBudgetExceededError(args.stage, reservation.totalAfter);
|
|
}
|
|
|
|
let outcome: RunRawWithFallbackResult;
|
|
try {
|
|
outcome = await runRawCompletionWithFallback({
|
|
userId: args.userId,
|
|
// Rationed at the job share, exactly as the reservation above was.
|
|
surface: "job",
|
|
...(args.interactive ? { interactive: true } : {}),
|
|
providers: args.chain,
|
|
params: {
|
|
...singleUserTurn({
|
|
system: args.systemPrompt,
|
|
user: args.userPrompt,
|
|
temperature: args.temperature,
|
|
maxTokens: args.maxTokens,
|
|
seed: args.seed,
|
|
timeoutMs: args.timeoutMs,
|
|
signal: args.signal,
|
|
// Every briefing call parses its reply with `JSON.parse`, so opt the
|
|
// chains with a native JSON mode into it.
|
|
responseFormat: "json",
|
|
}),
|
|
// v1.41 — only a call that names its job carries this.
|
|
...(reasoning ? { reasoning } : {}),
|
|
},
|
|
});
|
|
} catch (err) {
|
|
// No usable reply: refund the reservation rather than bill an invented
|
|
// figure. A partially-burned upstream call is possible here, but without
|
|
// a reported count there is nothing to charge, and over-charging a
|
|
// failed generation would ration the retry the user is entitled to.
|
|
// #1279 — what the hops did report spending is charged: a model that
|
|
// reasons without answering bills every call.
|
|
await reconcileSpend(
|
|
args.userId,
|
|
reservation.reserved,
|
|
spentOnFailure(err),
|
|
dateKey,
|
|
0,
|
|
{
|
|
servedBy: null,
|
|
reservedOwner: reservation.owner,
|
|
},
|
|
).catch(() => {
|
|
// Best-effort: a failed refund leaves the conservative reservation in
|
|
// place (never an undercount) and must not mask the provider error.
|
|
});
|
|
throw err;
|
|
}
|
|
|
|
// #1279 — plus what the hops that failed before it spent.
|
|
const actualTokens =
|
|
(outcome.result.tokensUsed ?? reservation.reserved) + outcome.tokensSpent;
|
|
await reconcileSpend(
|
|
args.userId,
|
|
reservation.reserved,
|
|
actualTokens,
|
|
dateKey,
|
|
outcome.result.cachedInputTokens ?? 0,
|
|
{
|
|
servedBy: outcome.workingProvider.providerType,
|
|
reservedOwner: reservation.owner,
|
|
},
|
|
).catch(() => {
|
|
// Ledger reconcile is best-effort; a failure leaves the reservation in
|
|
// place and never breaks a generation that already succeeded.
|
|
});
|
|
|
|
return outcome;
|
|
}
|