perf(ai): read the deletion lock with the usage level, not on its own

The capability gate's check for an account pending deletion added a
point read of users to every request that resolves AI capabilities, one
statement over the budget on /api/auth/me, the dashboard snapshot and the
batch ingest. The usage-level read already loads the record's row once
per request; it now carries the deletion lock too.
This commit is contained in:
Marc-André Bombeck committed 2026-10-25 18:54:33 +01:00
1 parent 52a80957f9
commit a42b29a160
2 files changed
+14 -7

No files matched your search

+7 -6
View File
@@ -41,7 +41,11 @@ import {
type AiRecordKind,
} from "./resolve";
import type { AiCapabilities } from "./types";
import { loadAiUsageState, loadRecordUsageLevel } from "./usage";
import {
loadAiUsageState,
loadRecordAiUsage,
loadRecordUsageLevel,
} from "./usage";
/** Whose record, seen by whom, through which grant. */
export interface AiCapabilityScope {
@@ -89,11 +93,8 @@ function loadActiveConsents(
* (`src/lib/account-deletion/active-account.ts`).
*/
async function loadRecordDeletionPending(recordId: string): Promise<boolean> {
const row = await prisma.user.findUnique({
where: { id: recordId },
select: { deletionRequestedAt: true },
});
return row?.deletionRequestedAt != null;
// The same memoised point read as the usage level.
return (await loadRecordAiUsage(recordId)).deletionPending;
}
function scopeKey(scope: AiCapabilityScope): string {
+7 -1
View File
@@ -106,6 +106,11 @@ export function loadAiUsageSettings(): Promise<AiUsageSettings> {
export interface RecordAiUsage extends AiUsageResolution {
preference: AiUsageLevel | null;
settings: AiUsageSettings;
/**
* The record is pending deletion. Read in the same point read as the level,
* so the capability gate's deletion check costs no statement of its own.
*/
deletionPending: boolean;
}
/**
@@ -118,11 +123,12 @@ export function loadRecordAiUsage(recordId: string): Promise<RecordAiUsage> {
loadAiUsageSettings(),
prisma.user.findUnique({
where: { id: recordId },
select: { aiUsageLevel: true },
select: { aiUsageLevel: true, deletionRequestedAt: true },
}),
]);
const preference = usageLevelFromDb(row?.aiUsageLevel);
return {
deletionPending: row?.deletionRequestedAt != null,
...resolveAiUsageLevel({
preference,
defaultLevel: settings.defaultLevel,