fix(ai): read AI switches, modules and consent afresh inside background jobs

A background job opens one wide event for its whole run, and the per-request
memo cached the operator switches, module availability, module preferences,
provider presence and consent kinds on it. The nightly status passes loop
over every candidate inside that one event, so a switch turned off mid-run
did not stop the remaining users, and the wire re-check read the same cached
answer.

memoizePerRequest takes a freshInBackground option; every input that decides
whether AI work may run sets it, so a background caller reads them on every
call while a request still reads them once.
This commit is contained in:
Marc-André Bombeck committed 2026-09-24 12:08:39 +02:00
1 parent 4ae231d757
commit 06251d7219
10 files changed
+236 -67

No files matched your search

@@ -16,7 +16,7 @@ vi.mock("@/lib/db", () => ({
// pre-v1.4.33 assertion semantics.
let mockEvent: object | null = null;
vi.mock("@/lib/logging/context", () => ({
getEvent: () => mockEvent ?? { addWarning: vi.fn() },
getEvent: () => mockEvent ?? { getKind: () => "http", addWarning: vi.fn() },
}));
import { prisma } from "@/lib/db";
@@ -154,7 +154,7 @@ describe("per-request memoisation", () => {
// Pin a stable stub object so every `getEvent()` inside this test
// returns the same reference — the memo keys on identity, so a
// shared reference == shared cache.
mockEvent = { addWarning: vi.fn() };
mockEvent = { getKind: () => "http", addWarning: vi.fn() };
const a = await getAssistantFlags();
const b = await getAssistantFlags();
@@ -174,9 +174,9 @@ describe("per-request memoisation", () => {
assistantDocumentAiEnabled: true,
});
mockEvent = { addWarning: vi.fn(), id: "req-1" };
mockEvent = { getKind: () => "http", addWarning: vi.fn(), id: "req-1" };
await getAssistantFlags();
mockEvent = { addWarning: vi.fn(), id: "req-2" };
mockEvent = { getKind: () => "http", addWarning: vi.fn(), id: "req-2" };
await getAssistantFlags();
expect(FIND).toHaveBeenCalledTimes(2);
+32 -27
View File
@@ -49,35 +49,40 @@ const ASSISTANT_FLAGS_OFF: AssistantFlagSet = Object.freeze({
* switch; failing open meant a database blip switched every AI egress on.
*
* Memoised per request, so a page that fires several gated reads at once reads
* the singleton row once.
* the singleton row once. Read afresh on every call inside a background job,
* whose one event spans a whole multi-user pass.
*/
export function loadAssistantSwitches(): Promise<AssistantFlagSet | null> {
return memoizePerRequest("assistant-flags", async () => {
try {
const settings = await prisma.appSettings.findUnique({
where: { id: "singleton" },
select: {
assistantEnabled: true,
assistantCoachEnabled: true,
assistantBriefingEnabled: true,
assistantInsightStatusEnabled: true,
assistantDocumentAiEnabled: true,
},
});
return resolveAssistantFlags({
enabled: settings?.assistantEnabled ?? true,
coach: settings?.assistantCoachEnabled ?? true,
briefing: settings?.assistantBriefingEnabled ?? true,
insightStatus: settings?.assistantInsightStatusEnabled ?? true,
documentAi: settings?.assistantDocumentAiEnabled ?? true,
});
} catch {
getEvent()?.addWarning(
"Failed to load assistant switches; every AI capability is off for this request",
);
return null;
}
});
return memoizePerRequest(
"assistant-flags",
async () => {
try {
const settings = await prisma.appSettings.findUnique({
where: { id: "singleton" },
select: {
assistantEnabled: true,
assistantCoachEnabled: true,
assistantBriefingEnabled: true,
assistantInsightStatusEnabled: true,
assistantDocumentAiEnabled: true,
},
});
return resolveAssistantFlags({
enabled: settings?.assistantEnabled ?? true,
coach: settings?.assistantCoachEnabled ?? true,
briefing: settings?.assistantBriefingEnabled ?? true,
insightStatus: settings?.assistantInsightStatusEnabled ?? true,
documentAi: settings?.assistantDocumentAiEnabled ?? true,
});
} catch {
getEvent()?.addWarning(
"Failed to load assistant switches; every AI capability is off for this request",
);
return null;
}
},
{ freshInBackground: true },
);
}
/**