mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-17 07:42:23 +00:00
fix(insights): do not count a status run the bench sent nothing for
A status run whose whole chain was benched for background work sent no request, yet the caller opened the note's negative window as for any failure and raised its failure streak. The streak doubles the window up to two hours, so a note kept waiting long after its provider recovered. AllProvidersFailedError now says when no hop sent anything (sentNothing: every provider benched, or refused by the operator cost cap). The status chokepoint then holds the note for the first window without touching the streak (holdStatusNoteWhileBenched), and the caller's own failure write lands inside that window and neither moves nor counts it. A run that reached a provider counts as before. Refs #1279
This commit is contained in:
1 parent
b0ea3b1654
commit
f1163d5efb
8 files changed
+225
-53
No files matched your search
@@ -22,6 +22,7 @@ vi.mock("@/lib/db", () => ({
|
||||
import {
|
||||
STATUS_NEGATIVE_WINDOW_BASE_MS,
|
||||
clearStatusNegativeWindows,
|
||||
holdStatusNoteWhileBenched,
|
||||
statusNegativeWindowMs,
|
||||
statusNoteCoolingDown,
|
||||
writeStatusNegativeWindow,
|
||||
@@ -116,6 +117,57 @@ describe("writeStatusNegativeWindow", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("holdStatusNoteWhileBenched", () => {
|
||||
// A run that sent nothing because every provider was benched says nothing
|
||||
// about the note: counting it left the note waiting up to two hours after
|
||||
// the provider recovered.
|
||||
it("opens the first window and leaves the streak as it was", async () => {
|
||||
findUnique.mockResolvedValue({ retryAt: new Date(Date.now() - MINUTE) });
|
||||
const before = Date.now();
|
||||
await holdStatusNoteWhileBenched({
|
||||
userId: "u1",
|
||||
cacheAction: "insights.weight-status.en",
|
||||
todayKey: TODAY,
|
||||
});
|
||||
const args = written();
|
||||
expect(args.update).not.toHaveProperty("failureStreak");
|
||||
expect(args.create).toMatchObject({ failureStreak: 0, dateKey: TODAY });
|
||||
const window = args.update.retryAt.getTime() - before;
|
||||
expect(window).toBeGreaterThanOrEqual(STATUS_NEGATIVE_WINDOW_BASE_MS);
|
||||
expect(window).toBeLessThan(STATUS_NEGATIVE_WINDOW_BASE_MS + MINUTE);
|
||||
});
|
||||
|
||||
it("leaves an open window alone, and the failure after it uncounted", async () => {
|
||||
findUnique.mockResolvedValue({
|
||||
retryAt: new Date(Date.now() + MINUTE),
|
||||
failureStreak: 3,
|
||||
});
|
||||
await holdStatusNoteWhileBenched({
|
||||
userId: "u1",
|
||||
cacheAction: "insights.weight-status.en",
|
||||
todayKey: TODAY,
|
||||
});
|
||||
await writeStatusNegativeWindow({
|
||||
userId: "u1",
|
||||
cacheAction: "insights.weight-status.en",
|
||||
todayKey: TODAY,
|
||||
reason: "error",
|
||||
});
|
||||
expect(upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("never throws: a missed hold only lets the next failure count", async () => {
|
||||
findUnique.mockRejectedValue(new Error("pool exhausted"));
|
||||
await expect(
|
||||
holdStatusNoteWhileBenched({
|
||||
userId: "u1",
|
||||
cacheAction: "insights.weight-status.en",
|
||||
todayKey: TODAY,
|
||||
}),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("statusNegativeWindowMs", () => {
|
||||
it("doubles from five minutes to a two-hour ceiling", () => {
|
||||
expect([1, 2, 3, 4, 5, 6, 7, 30].map(statusNegativeWindowMs)).toEqual(
|
||||
|
||||
@@ -60,6 +60,7 @@ vi.mock("@/lib/db", () => ({
|
||||
// #1279 — the note's negative window, read before any provider work.
|
||||
insightStatusCache: {
|
||||
findUnique: vi.fn(async () => noteWindow),
|
||||
upsert: vi.fn(async () => ({})),
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -374,6 +375,36 @@ describe("runStatusCompletion — ledger accounting", () => {
|
||||
expect(again.kind).toBe("ok");
|
||||
});
|
||||
|
||||
it("holds the note without counting a run the bench sent nothing for (#1279)", async () => {
|
||||
runRawCompletionWithFallback.mockRejectedValue(
|
||||
Object.assign(new Error("All 1 configured providers failed"), {
|
||||
sentNothing: true,
|
||||
}),
|
||||
);
|
||||
const { prisma } = await import("@/lib/db");
|
||||
const result = await runStatusCompletion(
|
||||
completionArgs({ cacheAction: "insights.weight-status.en" }),
|
||||
);
|
||||
expect(result.kind).toBe("error");
|
||||
expect(prisma.insightStatusCache.upsert).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
create: expect.objectContaining({ failureStreak: 0 }),
|
||||
update: expect.not.objectContaining({
|
||||
failureStreak: expect.anything(),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it("leaves a run that reached a provider to the caller's counted window", async () => {
|
||||
runRawCompletionWithFallback.mockRejectedValue(new Error("upstream down"));
|
||||
const { prisma } = await import("@/lib/db");
|
||||
await runStatusCompletion(
|
||||
completionArgs({ cacheAction: "insights.weight-status.en" }),
|
||||
);
|
||||
expect(prisma.insightStatusCache.upsert).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("charges what a failed chain reported spending (#1279)", async () => {
|
||||
// A model that reasons and never answers is billed for every call; a
|
||||
// full refund let background work spend without the ceiling seeing it.
|
||||
|
||||
@@ -72,6 +72,48 @@ export async function writeStatusNegativeWindow(args: {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* #1279 — hold a note while its chain sent nothing: every provider was
|
||||
* benched for background work (or refused by the operator cost cap). Such a
|
||||
* run says nothing about the note, and counting it left the note waiting up
|
||||
* to two hours after the provider recovered. Opens the first window unless
|
||||
* one is open and leaves the streak as it was, so the caller's own failure
|
||||
* write lands inside it and neither moves nor counts. Best-effort: a missed
|
||||
* hold only lets that write count.
|
||||
*/
|
||||
export async function holdStatusNoteWhileBenched(args: {
|
||||
userId: string;
|
||||
cacheAction: string;
|
||||
todayKey: string;
|
||||
}): Promise<void> {
|
||||
const key = statusCacheKey(args.cacheAction);
|
||||
if (!key) return;
|
||||
const where = { userId_metric_locale: { userId: args.userId, ...key } };
|
||||
try {
|
||||
const row = await prisma.insightStatusCache.findUnique({
|
||||
where,
|
||||
select: { retryAt: true },
|
||||
});
|
||||
const now = Date.now();
|
||||
if (row?.retryAt && row.retryAt.getTime() > now) return;
|
||||
const retryAt = new Date(now + STATUS_NEGATIVE_WINDOW_BASE_MS);
|
||||
await prisma.insightStatusCache.upsert({
|
||||
where,
|
||||
create: {
|
||||
userId: args.userId,
|
||||
...key,
|
||||
dateKey: args.todayKey,
|
||||
retryAt,
|
||||
negativeReason: "error",
|
||||
failureStreak: 0,
|
||||
},
|
||||
update: { retryAt, negativeReason: "error" },
|
||||
});
|
||||
} catch {
|
||||
// The caller's own write still opens a window.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* True while the note's negative-cache window is open. #1279 — checked right
|
||||
* before a status generation reaches a provider (`runStatusCompletion`), so
|
||||
|
||||
@@ -21,6 +21,7 @@ import {
|
||||
reserveBudget,
|
||||
resolveCostOwner,
|
||||
resolveDailyCapFor,
|
||||
type BudgetCostOwner,
|
||||
} from "@/lib/ai/coach/budget";
|
||||
import {
|
||||
AI_BUDGETS,
|
||||
@@ -36,7 +37,10 @@ import {
|
||||
type BackgroundReasoningJob,
|
||||
} from "@/lib/ai/reasoning/levels";
|
||||
import { resolveJobReasoning } from "@/lib/ai/reasoning/controls";
|
||||
import { statusNoteCoolingDown } from "./status-negative-window";
|
||||
import {
|
||||
holdStatusNoteWhileBenched,
|
||||
statusNoteCoolingDown,
|
||||
} from "./status-negative-window";
|
||||
|
||||
/**
|
||||
* v1.41 — the fallback ceiling for a status-path call that reasons. Thinking
|
||||
@@ -332,16 +336,7 @@ export async function runStatusCompletion(
|
||||
);
|
||||
|
||||
if (raced.timedOut || raced.errored || raced.value === null) {
|
||||
// A timeout or failure: charge only what a reply reported spending, never
|
||||
// an invented figure. #1279 — a failed chain's report counts: a model
|
||||
// that reasons without answering bills every call, and a full refund let
|
||||
// background work spend without the day's ceiling ever seeing it.
|
||||
const spent = spentOnFailure(raced.error);
|
||||
await reconcileSpend(userId, reservation.reserved, spent, dateKey, 0, {
|
||||
servedBy: null,
|
||||
reservedOwner: reservation.owner,
|
||||
});
|
||||
return { kind: raced.timedOut ? "timeout" : "error" };
|
||||
return failedRun(raced, { userId, cacheAction, dateKey, reservation });
|
||||
}
|
||||
|
||||
const { result, workingProvider, tokensSpent } = raced.value;
|
||||
@@ -395,6 +390,44 @@ export async function runStatusCompletion(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* A run that timed out or failed. It charges only what a reply reported
|
||||
* spending, never an invented figure; #1279 — a failed chain's report counts,
|
||||
* since a model that reasons without answering bills every call, and a full
|
||||
* refund let background work spend without the day's ceiling seeing it. A
|
||||
* chain that sent nothing (the bench) holds the note without counting it.
|
||||
*/
|
||||
async function failedRun(
|
||||
raced: { timedOut: boolean; error?: unknown },
|
||||
run: {
|
||||
userId: string;
|
||||
cacheAction: string;
|
||||
dateKey: string;
|
||||
reservation: { reserved: number; owner: BudgetCostOwner };
|
||||
},
|
||||
): Promise<StatusProviderResult> {
|
||||
const spent = spentOnFailure(raced.error);
|
||||
await reconcileSpend(
|
||||
run.userId,
|
||||
run.reservation.reserved,
|
||||
spent,
|
||||
run.dateKey,
|
||||
0,
|
||||
{
|
||||
servedBy: null,
|
||||
reservedOwner: run.reservation.owner,
|
||||
},
|
||||
);
|
||||
if ((raced.error as { sentNothing?: unknown } | null)?.sentNothing === true) {
|
||||
await holdStatusNoteWhileBenched({
|
||||
userId: run.userId,
|
||||
cacheAction: run.cacheAction,
|
||||
todayKey: run.dateKey,
|
||||
});
|
||||
}
|
||||
return { kind: raced.timedOut ? "timeout" : "error" };
|
||||
}
|
||||
|
||||
/**
|
||||
* Over the day's ceiling. Reported as `error` — a TRANSIENT miss the caller
|
||||
* serves the fallback for without persisting it — deliberately NOT `none`,
|
||||
|
||||
Reference in new issue
Block a user