Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(signals): move the last raw aggregate call off the raw scan (#834)
Gökhan Geyika77eda506d19
diff --git a/server/src/lib/signals/nightly-reads.test.js b/server/src/lib/signals/nightly-reads.test.jsnew file mode 100644index 000000000..e6803021c--- /dev/null+++ b/server/src/lib/signals/nightly-reads.test.js@@ -0,0 +1,56 @@+import { readFileSync } from 'node:fs';+import { describe, expect, it } from 'vitest';++/**+ * Which RPCs the nightly path is allowed to call.+ *+ * `ai_visibility_aggregates`, `insights_aggregates` and `competitor_aggregates`+ * rescan everything a brand has ever collected, and past roughly 25k results+ * that crosses the database's 8s statement timeout. Each has a `_daily`+ * variant answered from the rollup tables, whose cost scales with days in the+ * window instead.+ *+ * This is a grep, and it exists because the alternative failed twice. #829+ * moved the pulse's calls to the rollups and missed the one in the signal+ * recorder, which then threw every night on the largest brand and took the+ * whole pass with it — the second silent multi-day outage from the same+ * cause. A convention nothing checks is a convention that holds until someone+ * adds a call site.+ */+const NIGHTLY_MODULES = [+ 'src/lib/signals/record.js',+ 'src/lib/pulse/metrics.js',+ 'src/lib/action-center/generate.js',+ 'src/lib/action-center/validate.js',+ 'src/lib/action-center/execution/tools.js',+];++const RAW_AGGREGATES = ['ai_visibility_aggregates', 'insights_aggregates', 'competitor_aggregates'];++describe('the nightly path', () => {+ it.each(NIGHTLY_MODULES)('%s reads wide windows from the rollups', (file) => {+ const source = readFileSync(new URL(`../../../${file}`, import.meta.url), 'utf8');++ for (const rpc of RAW_AGGREGATES) {+ // A direct call with the raw name as a literal: `rpc('name', …)`.+ // Deliberately not prose — these names appear in comments explaining+ // why they are not called — and deliberately not `rpc(daily('name',+ // window), …)`, which is the routed form that picks the rollup when the+ // window is expressed in whole days and the raw RPC only for the 24-hour+ // run-anchored window that no day window can express.+ const call = new RegExp(`rpc\\(\\s*['"\`]${rpc}(?!_daily)['"\`]`, 'g');+ const hits = [...source.matchAll(call)].map((match) => match[0]);+ expect({ file, rpc, hits }).toEqual({ file, rpc, hits: [] });+ }+ });++ /** A module joining the nightly path without joining this list would be+ * unchecked, so the list itself has to stay honest. */+ it('covers every module the list names', () => {+ for (const file of NIGHTLY_MODULES) {+ expect(() =>+ readFileSync(new URL(`../../../${file}`, import.meta.url), 'utf8'),+ ).not.toThrow();+ }+ });+});diff --git a/server/src/lib/signals/record.js b/server/src/lib/signals/record.jsindex 67d9e2f31..1e4cc954b 100644--- a/server/src/lib/signals/record.js+++ b/server/src/lib/signals/record.js@@ -33,6 +33,10 @@ import { resolve } from '../../config/action-engine.js';const DAY_MS = 86_400_000;+function utcDay(date) {+ return date.toISOString().slice(0, 10);+}+// What counts as a signal is decided in config/action-engine.js (#818 phase// 2.5), alongside every other threshold the engine judges by.const { detection } = resolve();@@ -361,15 +365,31 @@ export function classifyCitationGap({ brandCitations, leaderCitations }) {* One signal per brand, naming the weakest platform. A brand behind on three* engines has one condition to work on, not three rows.*/+/** Rows the platform comparison reads. Comfortably above the largest brand's+ * 7-day volume; reaching it means the comparison cannot be trusted. */+const PLATFORM_ROW_LIMIT = 20000;+async function platformGapCandidates(brandId) {const { data, error } = await supabaseAdmin.from('insights_prompt_daily').select('model_used, prompt_id, has_mention, has_citation').eq('brand_id', brandId).gte('day', new Date(Date.now() - detection.windowDays * DAY_MS).toISOString().slice(0, 10))- .limit(20000);+ .limit(PLATFORM_ROW_LIMIT);if (error) throw new Error(error.message);+ // The ceiling is generous — the largest brand uses 8k of it — but a brand+ // that reaches it would have its platform rates computed from an arbitrary+ // subset, and report a gap that is an artefact of where the read stopped.+ // Say so rather than answer confidently from truncated data.+ if ((data ?? []).length >= PLATFORM_ROW_LIMIT) {+ logger.warn(+ { brandId, rows: data.length, limit: PLATFORM_ROW_LIMIT },+ '[signals] platform gap read hit its row ceiling — skipping rather than judging a subset',+ );+ return [];+ }+const byPlatform = new Map();for (const row of data ?? []) {const entry = byPlatform.get(row.model_used) ?? { tracked: new Set(), visible: new Set() };@@ -414,11 +434,22 @@ async function platformGapCandidates(brandId) {* tiny numbers.*/async function competitorCitationGapCandidates(brandId, now) {- const from = new Date(now.getTime() - detection.windowDays * DAY_MS);- const { data, error } = await supabaseAdmin.rpc('ai_visibility_aggregates', {+ // Read from the daily rollups, not the raw aggregate.+ //+ // #829 moved the pulse's wide windows off `ai_visibility_aggregates`+ // because it rescans a brand's entire history and crosses the database's 8s+ // statement timeout once a brand is large enough. This call site was missed,+ // and it is worse placed than the ones that were fixed: the pulse's helper+ // retries a timeout, this called the client directly, so the first timeout+ // threw and took the whole nightly pass with it. On the largest brand it did+ // so every night — 8.2s to fail here against 0.13s to succeed from the+ // rollup.+ const to = utcDay(now);+ const from = utcDay(new Date(now.getTime() - (detection.windowDays - 1) * DAY_MS));+ const { data, error } = await supabaseAdmin.rpc('ai_visibility_aggregates_daily', {p_brand_id: brandId,- p_date_from: from.toISOString(),- p_date_to: now.toISOString(),+ p_day_from: from,+ p_day_to: to,});if (error) throw new Error(error.message);