fix(ai-moderation): skip caching error results to prevent false positives from transient failures

This commit is contained in:
MythEclipse
2026-06-04 17:42:23 +07:00
parent 4996acfacf
commit 946343ec8c
@@ -1685,6 +1685,19 @@ export async function runModerationAnalysis(
try { try {
const cached = await getCachedUserModeration(cacheKey); const cached = await getCachedUserModeration(cacheKey);
if (cached) { if (cached) {
// Safety: skip cache entries that are artifacts of API/parse errors.
// A previous bug cached error results as "flagged", causing 24h false positives.
// This guards against both legacy corrupt entries and any future write-path bugs.
if (
cached.flags.some((f) =>
["analysis_api_failed", "analysis_parse_failed", "analysis_incomplete"].includes(f),
)
) {
log.warn(
{ messageId: target.id, cacheKey },
"Cache entry contains error artifact — treating as miss",
);
} else {
cacheHits.push({ cacheHits.push({
messageId: target.id, messageId: target.id,
status: cached.status, status: cached.status,
@@ -1704,6 +1717,7 @@ export async function runModerationAnalysis(
); );
continue; continue;
} }
}
} catch { } catch {
// Cache lookup failed — proceed with uncached path // Cache lookup failed — proceed with uncached path
} }
@@ -1767,6 +1781,10 @@ export async function runModerationAnalysis(
const rawContent = target.edited_content ?? target.content; const rawContent = target.edited_content ?? target.content;
if (!rawContent.trim()) continue; if (!rawContent.trim()) continue;
// Do NOT cache error results (API failures, parse failures, incomplete).
// Caching a transient error would turn it into a 24h false positive.
if (result.status === "error") continue;
const cacheKey = makeUserModerationCacheKey(target.user_id, rawContent); const cacheKey = makeUserModerationCacheKey(target.user_id, rawContent);
setCachedUserModeration(cacheKey, { setCachedUserModeration(cacheKey, {
flags: result.flags ?? [], flags: result.flags ?? [],
@@ -1776,7 +1794,7 @@ export async function runModerationAnalysis(
severity: result.severity ?? "none", severity: result.severity ?? "none",
confidence: result.confidence ?? result.score ?? 0, confidence: result.confidence ?? result.score ?? 0,
recommendedAction: result.recommendedAction ?? "none", recommendedAction: result.recommendedAction ?? "none",
status: result.status === "error" ? "flagged" : result.status, status: result.status,
}).catch(() => {}); }).catch(() => {});
} }