Skip to content

Open source alternative to Semrush and Ahrefs

Stop AI visibility collect retries from re-saving every answer (#1079)

Ben Senescu992cd2512671
diff --git a/src/server/features/ai-visibility/repositories/AiVisibilityRepository.ts b/src/server/features/ai-visibility/repositories/AiVisibilityRepository.ts
index 06d83ba2..f62270c0 100644
--- a/src/server/features/ai-visibility/repositories/AiVisibilityRepository.ts
+++ b/src/server/features/ai-visibility/repositories/AiVisibilityRepository.ts
@@ -1,4 +1,5 @@
import { and, asc, desc, eq, inArray, isNull, lte, ne } from "drizzle-orm";
+import { chunk } from "remeda";
import { db } from "@/db";
import { runBatch } from "@/db/runBatch";
import {
@@ -195,8 +196,16 @@ async function persistAnswer(input: {
tx
.delete(aiMatches)
.where(eq(aiMatches.observationId, input.observationId)),
- ...input.sources.map((s) => tx.insert(aiSources).values(s)),
- ...input.matches.map((m) => tx.insert(aiMatches).values(m)),
+ // Multi-row inserts keep the transaction to a few round trips, so a
+ // collect step saving many answers does not hold pooled connections. D1
+ // allows 100 bound parameters per statement: 6 columns per source and 8
+ // per match.
+ ...chunk(input.sources, 16).map((rows) =>
+ tx.insert(aiSources).values(rows),
+ ),
+ ...chunk(input.matches, 12).map((rows) =>
+ tx.insert(aiMatches).values(rows),
+ ),
tx
.update(aiObservations)
.set(input.values)
@@ -306,6 +315,18 @@ export const AiVisibilityRepository = {
.from(aiObservations)
.where(inArray(aiObservations.runId, runIds))
: Promise.resolve([]),
+ listPendingObservationIds: async (runId: string) =>
+ (
+ await db
+ .select({ id: aiObservations.id })
+ .from(aiObservations)
+ .where(
+ and(
+ eq(aiObservations.runId, runId),
+ eq(aiObservations.status, "pending"),
+ ),
+ )
+ ).map((row) => row.id),
getEvidence,
persistAnswer,
failPendingObservations,
diff --git a/src/server/features/ai-visibility/services/aiVisibilityCollection.test.ts b/src/server/features/ai-visibility/services/aiVisibilityCollection.test.ts
index 2e51ffb0..e72386c3 100644
--- a/src/server/features/ai-visibility/services/aiVisibilityCollection.test.ts
+++ b/src/server/features/ai-visibility/services/aiVisibilityCollection.test.ts
@@ -151,6 +151,9 @@ describe("AI answer collection", () => {
}),
]),
);
+ // A retried step skips the answer an earlier attempt already saved.
+ await collectAiRound(runId, [pendingTask], true);
+ expect(dataforseo.fetchTaskResult).toHaveBeenCalledTimes(1);
});
it("fails the answers DataForSEO did not accept and collects the rest", async () => {
@@ -412,10 +415,7 @@ describe("AI answer collection", () => {
});
it("fails answers that never arrived and finishes the run as partial", async () => {
- dataforseo.fetchTaskResult.mockResolvedValue({
- status: "completed",
- result: { markdown: "OpenSEO", sources: [] },
- });
+ dataforseo.fetchTaskResult.mockResolvedValue(liveAnswer.value);
await collectAiRound(runId, [pendingTask], true);
await finalizeAiRun(runId);
diff --git a/src/server/features/ai-visibility/services/aiVisibilityCollection.ts b/src/server/features/ai-visibility/services/aiVisibilityCollection.ts
index 26c35e84..6f6ad1ec 100644
--- a/src/server/features/ai-visibility/services/aiVisibilityCollection.ts
+++ b/src/server/features/ai-visibility/services/aiVisibilityCollection.ts
@@ -328,10 +328,15 @@ export async function collectAiRound(
): Promise<{ pending: AiPendingTask[]; retry: AiTaskBatch[] }> {
const brands = await runBrands(runId);
if (!brands) return { pending: [], retry: [] };
+ // A retried or timed-out step hands back answers an earlier attempt already
+ // saved or failed. Skip them, so each attempt continues where the last one
+ // stopped instead of saving every answer again.
+ const stillPending = new Set(await repo.listPendingObservationIds(runId));
+ const unsaved = tasks.filter((task) => stillPending.has(task.tag));
const pending: AiPendingTask[] = [];
const retry: AiPendingTask[] = [];
- for (let i = 0; i < tasks.length; i += TASK_GET_CONCURRENCY) {
- const chunk = tasks.slice(i, i + TASK_GET_CONCURRENCY);
+ for (let i = 0; i < unsaved.length; i += TASK_GET_CONCURRENCY) {
+ const chunk = unsaved.slice(i, i + TASK_GET_CONCURRENCY);
const outcomes = await Promise.allSettled(
chunk.map((task) => fetchAiTrackingTaskResult(task)),
);