perf(gateway): hoist correctedExamples query out of retry closure

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)
This commit is contained in:
asepharyana
2026-08-16 09:06:17 +07:00
co-authored by Claude Opus 5 (Nous Research)
parent f82b5caae4
commit a3e5a8c1b9
@@ -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) ?? "<empty>",
}
: undefined;
const correctedExamples = await buildCorrectedFewShotExamples();
const systemText = buildSystemPromptModular({
mode: batchHasImageEvidence ? "mixed" : "text",
correction,