Merge branch 'worktree-agent-acf4680fc308d77c8' into release/v1.30.25

This commit is contained in:
Marc-André Bombeck committed 2026-07-19 04:24:13 +02:00
commit b1370626fc
9 files changed
+583 -21

No files matched your search

@@ -2,11 +2,18 @@
* v1.11.1 — worker pipeline for the combined Coach memory-refresh queue.
*
* Runs both background generators for one conversation, sequentially so they
* share the wake-up but each pays its own budget check inside
* `runStatusCompletion`: the rolling conversation summary first, then durable
* fact extraction. Each step is fault-isolated — a failure or no-provider in
* one never sinks the other or the job. Kept out of the route bundle (the
* route imports only `enqueueCoachMemoryRefresh` from `coach-memory-shared`).
* share the wake-up: the rolling conversation summary first, then durable fact
* extraction. Each step is fault-isolated — a failure or no-provider in one
* never sinks the other or the job. Kept out of the route bundle (the route
* imports only `enqueueCoachMemoryRefresh` from `coach-memory-shared`).
*
* Both steps reserve and reconcile against the caller's daily token ledger
* inside `runStatusCompletion`, so this off-request work is metered on the same
* `coach_usage` row as the chat turn that triggered it, against the ceiling
* that matches the chain's cost owner. This comment previously asserted the
* same guarantee while no such accounting existed anywhere on the path — the
* chokepoint did no budget work at all, so every generator behind it, not just
* these two, spent unmetered.
*/
import { annotate } from "@/lib/logging/context";
@@ -0,0 +1,196 @@
/**
* Cost accounting for the status/reference provider chokepoint.
*
* `runStatusCompletion` is the single provider entry for every status and
* reference generator — the specialised status cards, the generic metric
* cards, biomarker cards, the batched assessment, the derived scores, the
* period narrative, and the off-request Coach memory workers. It previously
* ran with NO budget accounting whatsoever: nothing reserved, nothing
* recorded, no ceiling. Everything behind it spent invisibly, and on an
* operator-key account that was unmetered operator spend.
*
* These tests drive the REAL budget module (`reserveBudget` / `reconcileSpend`
* / `resolveDailyCap`) against a stateful in-memory stand-in for the
* `coach_usage` row, so they exercise the actual atomic-upsert path rather
* than merely asserting that a mock was called.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
/** In-memory stand-in for the day's `coach_usage.total_tokens`. */
let ledgerTotal = 0;
vi.mock("@/lib/db", () => ({
prisma: {
// `reserveBudget`'s atomic upsert-increment. Tagged-template call shape:
// (strings, userId, dateKey, reserved, reserved).
$queryRaw: vi.fn(
async (_strings: TemplateStringsArray, ...v: unknown[]) => {
ledgerTotal += Number(v[2] ?? 0);
return [{ total_tokens: ledgerTotal }];
},
),
// `reconcileSpend` (+delta) and `refundReservation` (-reserved).
$executeRaw: vi.fn(
async (strings: TemplateStringsArray, ...v: unknown[]) => {
const sql = strings.join("?");
const amount = Number(v[0] ?? 0);
if (sql.includes("total_tokens + ")) ledgerTotal += amount;
else if (sql.includes("total_tokens - ")) ledgerTotal -= amount;
ledgerTotal = Math.max(0, ledgerTotal);
return 1;
},
),
user: {
findUnique: vi.fn(async () => ({ aiResponseTimeoutSeconds: null })),
},
},
}));
vi.mock("@/lib/logging/context", () => ({ annotate: vi.fn() }));
const { resolveProviderChain, resolveProvider } = vi.hoisted(() => ({
resolveProviderChain: vi.fn(),
resolveProvider: vi.fn(),
}));
vi.mock("@/lib/ai/provider", () => ({ resolveProviderChain, resolveProvider }));
const { runRawCompletionWithFallback } = vi.hoisted(() => ({
runRawCompletionWithFallback: vi.fn(),
}));
vi.mock("@/lib/ai/provider-runner", () => ({
AllProvidersFailedError: class extends Error {},
runRawCompletionWithFallback,
}));
// Consent gate is exercised by its own suite; keep it open here so the budget
// behaviour is what these tests isolate.
vi.mock("@/lib/ai/consent-guard", () => ({
chainRequiresServerManagedConsent: vi.fn(() => false),
hasActiveConsentForSurface: vi.fn(async () => true),
}));
import { runStatusCompletion } from "../status-provider";
import { annotate } from "@/lib/logging/context";
import { OPERATOR_COST_CAP, USER_PLAN_CAP } from "@/lib/ai/coach/budget";
const OPERATOR_CHAIN = [{ providerType: "admin-openai", instance: {} }];
const BYOK_CHAIN = [{ providerType: "anthropic", instance: {} }];
function completionArgs(overrides: Record<string, unknown> = {}) {
return {
userId: "u1",
cacheAction: "status:test",
systemPrompt: "system",
userPrompt: "user",
consentSurface: "insights" as const,
...overrides,
};
}
/** A successful provider reply reporting `tokensUsed`. */
function mockProviderReply(tokensUsed: number | null) {
runRawCompletionWithFallback.mockResolvedValue({
result: { content: '{"summary":"ok"}', model: "m", tokensUsed },
workingProvider: { providerType: "admin-openai" },
});
}
beforeEach(() => {
vi.clearAllMocks();
ledgerTotal = 0;
resolveProviderChain.mockResolvedValue(OPERATOR_CHAIN);
resolveProvider.mockResolvedValue({ type: "none" });
});
describe("runStatusCompletion — ledger accounting", () => {
it("lands a successful generation on the day's ledger", async () => {
mockProviderReply(1234);
const result = await runStatusCompletion(completionArgs());
expect(result.kind).toBe("ok");
// Reserved an estimate, then reconciled down to what the provider
// actually reported. Either way the spend is now ON the ledger — before
// this wiring existed the total stayed at zero forever.
expect(ledgerTotal).toBe(1234);
});
it("charges the reservation when the provider reports no token count", async () => {
mockProviderReply(null);
await runStatusCompletion(completionArgs());
// An unreported generation must not bill as free.
expect(ledgerTotal).toBeGreaterThan(0);
});
it("refuses a generation once the day's cap is already spent", async () => {
ledgerTotal = OPERATOR_COST_CAP;
mockProviderReply(500);
const result = await runStatusCompletion(completionArgs());
// Refused BEFORE any provider egress, and reported as a transient miss
// (`error`) rather than `none` — callers cache `none` as the settled
// "no provider configured" assessment, which a budget refusal is not.
expect(runRawCompletionWithFallback).not.toHaveBeenCalled();
expect(result.kind).toBe("error");
expect(annotate).toHaveBeenCalledWith(
expect.objectContaining({
action: { name: "insights.status.budget_exceeded" },
}),
);
// The refused reservation was refunded, not left on the row.
expect(ledgerTotal).toBe(OPERATOR_COST_CAP);
});
it("refunds the reservation when the provider errors", async () => {
runRawCompletionWithFallback.mockRejectedValue(new Error("upstream down"));
const result = await runStatusCompletion(completionArgs());
expect(result.kind).toBe("error");
expect(ledgerTotal).toBe(0);
});
});
describe("runStatusCompletion — cost owner decides the ceiling", () => {
it("measures an operator-key chain against the operator ceiling", async () => {
// Just under the operator ceiling: an operator-key user is refused here.
ledgerTotal = OPERATOR_COST_CAP;
mockProviderReply(100);
const result = await runStatusCompletion(completionArgs());
expect(result.kind).toBe("error");
expect(runRawCompletionWithFallback).not.toHaveBeenCalled();
});
it("measures a BYOK chain against the user-plan ceiling, not the operator's", async () => {
// Same spend that locked the operator-key user out above. A self-hoster on
// their own key pays their own bill, so the operator's ceiling is a
// category error for them — they must still be served.
resolveProviderChain.mockResolvedValue(BYOK_CHAIN);
ledgerTotal = OPERATOR_COST_CAP;
runRawCompletionWithFallback.mockResolvedValue({
result: { content: '{"summary":"ok"}', model: "m", tokensUsed: 100 },
workingProvider: { providerType: "anthropic" },
});
const result = await runStatusCompletion(completionArgs());
expect(result.kind).toBe("ok");
expect(runRawCompletionWithFallback).toHaveBeenCalled();
});
it("still bounds a BYOK chain at the user-plan ceiling", async () => {
resolveProviderChain.mockResolvedValue(BYOK_CHAIN);
ledgerTotal = USER_PLAN_CAP;
mockProviderReply(100);
const result = await runStatusCompletion(completionArgs());
expect(result.kind).toBe("error");
expect(runRawCompletionWithFallback).not.toHaveBeenCalled();
});
});
+63 -1
View File
@@ -10,6 +10,12 @@ import {
type ConsentSurface,
} from "@/lib/ai/consent-guard";
import { annotate } from "@/lib/logging/context";
import {
buildDateKey,
reconcileSpend,
reserveBudget,
resolveDailyCap,
} from "@/lib/ai/coach/budget";
import { AI_BUDGETS, REFERENCE_AI_SEED } from "@/lib/ai/ai-budgets";
import { singleUserTurn } from "@/lib/ai/types";
import { STATUS_PROVIDER_TIMEOUT_MS, withTimeout } from "./with-timeout";
@@ -189,6 +195,49 @@ export async function runStatusCompletion(
STATUS_PROVIDER_TIMEOUT_MS,
);
const maxTokens = args.maxTokens ?? AI_BUDGETS.status.maxTokens;
// The day's token ledger. Until now this chokepoint — the provider entry for
// EVERY status/reference family (the specialised cards, the generic metric
// cards, biomarker cards, the batched assessment, the derived scores, the
// period narrative, and the off-request Coach memory workers: rolling
// summary, fact extraction, plan proposals) — ran with no accounting at all,
// so none of that spend appeared in `coach_usage` and no ceiling applied.
//
// The reservation is atomic (single upsert-increment), matching the Coach and
// document paths: a read-then-write check would let concurrent generations
// each observe a sub-cap total and all proceed. We reserve an ESTIMATE up
// front — the output ceiling plus a ~4-chars-per-token approximation of the
// prompt we are about to send — and reconcile against the provider's reported
// count afterwards, refunding in full when nothing was generated.
//
// The cap follows the COST OWNER, not the surface: `resolveDailyCap` 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
// or a local model is measured against the generous user-plan ceiling, so
// their own hardware/plan is never rationed by the operator's bill.
const dateKey = buildDateKey();
const estimatedTokens =
maxTokens + Math.ceil((systemPrompt.length + userPrompt.length) / 4);
const reservation = await reserveBudget(
userId,
estimatedTokens,
dateKey,
resolveDailyCap(chain),
);
if (!reservation.allowed) {
// Over the day's ceiling. Reported as `error` — a TRANSIENT miss the caller
// serves the fallback for without persisting it — deliberately NOT `none`,
// which callers cache as the settled "no provider configured" assessment.
// The distinct annotation keeps the refusal observable even though the
// result shape is shared.
annotate({
action: { name: "insights.status.budget_exceeded" },
meta: { cacheAction, totalAfter: reservation.totalAfter },
});
return { kind: "error" };
}
const raced = await withTimeout(
() =>
runRawCompletionWithFallback({
@@ -198,7 +247,7 @@ export async function runStatusCompletion(
system: systemPrompt,
user: userPrompt,
temperature: args.temperature ?? AI_BUDGETS.status.temperature,
maxTokens: args.maxTokens ?? AI_BUDGETS.status.maxTokens,
maxTokens,
// v1.18.7 — status/reference output is reproducible: pin the
// deterministic seed unless a caller overrides it.
seed: args.seed ?? REFERENCE_AI_SEED,
@@ -213,13 +262,26 @@ export async function runStatusCompletion(
);
if (raced.timedOut) {
// A timed-out generation may still have burned upstream tokens, but we have
// no reported count to charge — refund the reservation rather than bill an
// invented figure.
await reconcileSpend(userId, reservation.reserved, 0, dateKey);
return { kind: "timeout" };
}
if (raced.errored || raced.value === null) {
await reconcileSpend(userId, reservation.reserved, 0, dateKey);
return { kind: "error" };
}
const { result, workingProvider } = raced.value;
// Reconcile against what the provider actually reported. This runs for the
// empty-content branch too: those tokens were burned upstream even though the
// reply was unusable, so they stay on the ledger rather than being refunded
// into a free retry loop. Falls back to the reservation when the provider
// reports no count, so an unreported generation is never billed as zero.
const actualTokens = result.tokensUsed ?? reservation.reserved;
await reconcileSpend(userId, reservation.reserved, actualTokens, dateKey);
const content = result.content;
if (typeof content !== "string" || content.trim().length === 0) {
annotate({
@@ -108,7 +108,7 @@ describe("enqueueStatusGeneration", () => {
).resolves.toBeUndefined();
});
it("sends with a per-(user,metric,locale) singletonKey when boss exists", async () => {
it("sends with a per-(user,metric) singletonKey when boss exists", async () => {
const send = vi.fn().mockResolvedValue("job-id");
getGlobalBoss.mockReturnValue({ send });
await enqueueStatusGeneration({
@@ -119,10 +119,34 @@ describe("enqueueStatusGeneration", () => {
expect(send).toHaveBeenCalledWith(
INSIGHT_STATUS_GENERATE_QUEUE,
{ userId: "u1", metric: "mood", locale: "en" },
expect.objectContaining({ singletonKey: "u1:mood:en" }),
expect.objectContaining({ singletonKey: "u1:mood" }),
);
});
/**
* Cost guard. The locale on a status read is client-chosen (`?locale=`, or
* the `healthlog-locale` cookie), so when it was part of the singleton key
* each of the six supported locales opened its own de-dupe slot and one
* legitimate generation pass could be multiplied into six. The key must
* collapse them: same user, same metric, one queued generation regardless of
* how many locales ask.
*/
it("collapses every locale for one metric onto a single singletonKey", async () => {
const send = vi.fn().mockResolvedValue("job-id");
getGlobalBoss.mockReturnValue({ send });
for (const locale of ["de", "en", "fr", "es", "it", "pl"] as const) {
await enqueueStatusGeneration({ userId: "u1", metric: "mood", locale });
}
const keys = new Set(
send.mock.calls.map(
(call) => (call[2] as { singletonKey: string }).singletonKey,
),
);
expect(keys).toEqual(new Set(["u1:mood"]));
});
it("swallows a boss.send rejection (best-effort enqueue)", async () => {
const send = vi.fn().mockRejectedValue(new Error("boom"));
getGlobalBoss.mockReturnValue({ send });
+24 -7
View File
@@ -95,12 +95,29 @@ export interface InsightStatusGeneratePayload {
/**
* Fire-and-forget enqueue used by the read-only status route on a cache
* miss. A `singletonKey` per `(user, metric, locale)` collapses repeated
* polls into one queued job, so a client polling every few seconds while
* the provider works can't pile up duplicate generations. No-ops cleanly
* when the global boss instance is not available (e.g. the web process
* runs without an embedded worker) — the nightly pre-generate cron and
* the daily status crons remain the catch-net.
* miss. A `singletonKey` per `(user, metric)` collapses repeated polls into
* one queued job, so a client polling every few seconds while the provider
* works can't pile up duplicate generations. No-ops cleanly when the global
* boss instance is not available (e.g. the web process runs without an
* embedded worker) — the nightly pre-generate cron and the daily status
* crons remain the catch-net.
*
* The key deliberately EXCLUDES `locale`. It used to be
* `(user, metric, locale)`, and the locale on a status read is client-chosen
* — the `?locale=` query param, or the `healthlog-locale` cookie, both of
* which a caller sets freely. Each distinct locale therefore opened its own
* singleton slot, so walking the metric registry across the six supported
* locales multiplied one legitimate generation pass into six, none of which
* the de-dupe could collapse. Keying on `(user, metric)` makes the locale a
* property of the queued payload instead of part of its identity: one
* generation per metric per window, whichever locale asked first.
*
* A legitimate language switch still works. The cache stays keyed by
* `(user, metric, locale)`, so the new locale is a genuine cache miss and
* enqueues normally; the only change is that a switch made WHILE a generation
* for the same metric is already in flight waits out the 120 s window instead
* of racing a second one. The next poll re-enqueues and the card converges to
* the requested language.
*/
export async function enqueueStatusGeneration(
payload: InsightStatusGeneratePayload,
@@ -115,7 +132,7 @@ export async function enqueueStatusGeneration(
}
try {
await boss.send(INSIGHT_STATUS_GENERATE_QUEUE, payload, {
singletonKey: `${payload.userId}:${payload.metric}:${payload.locale}`,
singletonKey: `${payload.userId}:${payload.metric}`,
// De-dupe within a short window so a polling client doesn't enqueue
// a fresh job on every tick while a generation is already running.
singletonSeconds: 120,