From a3e5a8c1b9267dc84ab934df5487e6a577579004 Mon Sep 17 00:00:00 2001 From: asepharyana Date: Sun, 16 Aug 2026 09:06:17 +0700 Subject: [PATCH] perf(gateway): hoist correctedExamples query out of retry closure MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit buildCorrectedFewShotExamples() (a getRecentCorrectedModerations(5) DB hit) was called inside the per-sub-batch buildContent closure in textBatchProcessor.ts — re-queried for every sub-batch (≈10× for a 200-msg burst) AND re-fired on each parse-error retry. mediaBatchProcessor already hoisted it once. Mirror that: fetch once per runTextOnlyBatch, reuse the cached string inside the closure. No behavior change — identical content, fewer identical DB reads. tsc + 129 tests + biome green. Co-Authored-By: Claude Opus 5 (Nous Research) --- .../src/modules/ai-moderation/textBatchProcessor.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/services/discord-gateway/src/modules/ai-moderation/textBatchProcessor.ts b/services/discord-gateway/src/modules/ai-moderation/textBatchProcessor.ts index 490ca15..7277e80 100644 --- a/services/discord-gateway/src/modules/ai-moderation/textBatchProcessor.ts +++ b/services/discord-gateway/src/modules/ai-moderation/textBatchProcessor.ts @@ -195,6 +195,10 @@ export async function runTextOnlyBatch( ? await getChannelCulture(channelId) : null; const channelCulture = channelCultureObj?.culture_summary; + // Corrected false-positive examples are static per batch — fetch ONCE + // here instead of inside the per-sub-batch retry closure (which would + // re-query the DB on every sub-batch and every parse-error retry). + const correctedExamples = await buildCorrectedFewShotExamples(); for (let i = 0; i < subBatches.length; i++) { const batch = subBatches[i]; @@ -296,7 +300,6 @@ export async function runTextOnlyBatch( preview: state.lastInvalidContent?.slice(0, 800) ?? "", } : undefined; - const correctedExamples = await buildCorrectedFewShotExamples(); const systemText = buildSystemPromptModular({ mode: batchHasImageEvidence ? "mixed" : "text", correction,