Skip to content

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.

fix(insights): keep a failed same-prompt comparison out of the 24h cache

Cihan9fec1c7f4abd
diff --git a/web/src/lib/actions/tracking-insights-cache.test.ts b/web/src/lib/actions/tracking-insights-cache.test.ts
new file mode 100644
index 000000000..a76c72031
--- /dev/null
+++ b/web/src/lib/actions/tracking-insights-cache.test.ts
@@ -0,0 +1,157 @@
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+/**
+ * The anchored 24h view is computed in strict mode and cached until the next
+ * tracking run (cachedAnchoredInsights). A section that fails there must
+ * fail the cached read, so the page falls back to a live read and nothing
+ * degraded is stored. The same-prompt comparison (VIS-002) used to swallow
+ * its own errors: one timeout then cached the all-prompt changes, with no
+ * new-prompts note, until the next run.
+ */
+
+let sameError: { message: string } | null = null;
+
+const visibility = (answers: number, mentions: number) => ({
+ answers,
+ mention_answers: mentions,
+ citation_answers: 50,
+ position_factor: 0.5,
+ by_competitor: [],
+});
+const sameTotals = (answers: number, mentions: number) => ({
+ ...visibility(answers, mentions),
+ total_mentions: mentions,
+ total_citations: 50,
+ positive_count: 20,
+ mentioning_answers: mentions,
+});
+const SAME = {
+ complete: true,
+ same_prompts: 200,
+ new_prompts: 509,
+ dropped_prompts: 3,
+ cur: sameTotals(800, 220),
+ prev: sameTotals(780, 200),
+};
+
+function rpc(name: string) {
+ let data: unknown = null;
+ if (name === 'insights_same_prompt_compare') {
+ if (sameError) return Promise.resolve({ data: null, error: sameError });
+ data = SAME;
+ } else if (name === 'ai_visibility_aggregates') data = visibility(1800, 540);
+ else if (name === 'insights_aggregates') {
+ data = {
+ total_results: 1800,
+ sum_visibility: 0,
+ total_mentions: 710,
+ total_citations: 180,
+ positive_count: 0,
+ mentioning_results: 0,
+ last_checked_at: null,
+ by_model: [],
+ };
+ } else if (name === 'competitor_aggregates') {
+ data = {
+ brand_row_count: 1800,
+ brand_sum_visibility: 0,
+ brand_total_mentions: 710,
+ brand_total_citations: 180,
+ brand_prompt_count: 2,
+ brand_visible_prompts: 2,
+ by_competitor: [],
+ by_brand_provider: [],
+ by_competitor_provider: [],
+ };
+ } else if (name === 'share_of_voice_aggregates') {
+ data = { total_brand_mentions: 0, total_competitor_mentions: 0, by_platform: [] };
+ } else if (name === 'visibility_rate_trend') data = [];
+ else if (name === 'insights_filter_options') data = [{ regions: [], models: [] }];
+ else if (name === 'tracked_prompt_count') data = 2;
+ return Promise.resolve({ data, error: null });
+}
+
+// The last two completed tracking runs anchor the 24h window.
+const RUNS = [
+ { completed_at: '2026-10-14T06:00:00.000Z' },
+ { completed_at: '2026-10-13T06:00:00.000Z' },
+];
+
+function table(name: string) {
+ let activeRunQuery = false;
+ const builder: Record<string, unknown> = {
+ is: () => {
+ activeRunQuery = true;
+ return builder;
+ },
+ maybeSingle: async () => ({ data: { id: 'b1' }, error: null }),
+ single: async () => ({ data: { organization_id: 'org-1' }, error: null }),
+ then: (resolve: (v: { data: unknown[]; error: null }) => unknown) =>
+ resolve({ data: name === 'tracking_runs' && !activeRunQuery ? RUNS : [], error: null }),
+ };
+ for (const method of ['select', 'eq', 'neq', 'not', 'gte', 'order', 'limit']) {
+ builder[method] = () => builder;
+ }
+ return builder;
+}
+
+const client = { from: table, rpc };
+
+vi.mock('@/lib/supabase/server', () => ({ createClient: async () => client }));
+vi.mock('@/lib/supabase/admin', () => ({ supabaseAdmin: client }));
+vi.mock('@/lib/actions/topic', () => ({ getTopicById: vi.fn() }));
+vi.mock('@/lib/guards/plan-guard', () => ({
+ getOrgPlan: async () => ({ limits: { maxPrompts: 100 } }),
+}));
+vi.mock('@/lib/prompt-locations', () => ({ getOrgLocationUsage: async () => ({ total: 2 }) }));
+vi.mock('@/lib/actions/volumes', () => ({ getPromptVolumes: async () => ({ volumes: [] }) }));
+vi.mock('@/lib/actions/prompt-suggestions', () => ({
+ getPromptSuggestions: async () => ({ suggestions: [] }),
+}));
+
+// unstable_cache as Next runs it: a result is stored once its function
+// resolves, and a thrown error stores nothing.
+const cache = new Map<string, unknown>();
+vi.mock('next/cache', () => ({
+ unstable_cache: (fn: () => Promise<unknown>, keyParts: string[]) => async () => {
+ const key = keyParts.join('|');
+ if (!cache.has(key)) cache.set(key, await fn());
+ return cache.get(key);
+ },
+}));
+
+const { getInsightsData } = await import('./tracking');
+
+describe('the cached 24h Insights view (VIS-002)', () => {
+ beforeEach(() => {
+ cache.clear();
+ sameError = null;
+ });
+
+ it('keeps a failed same-prompt comparison out of the cache', async () => {
+ const errorLog = vi.spyOn(console, 'error').mockImplementation(() => {});
+ sameError = { message: 'canceling statement due to statement timeout' };
+
+ // The live fallback still shows the page, comparing all prompts.
+ const degraded = await getInsightsData('b1', { anchor24h: true });
+ expect(degraded.comparison).toBeNull();
+ expect(cache.size).toBe(0);
+ expect(errorLog).toHaveBeenCalled();
+ errorLog.mockRestore();
+
+ // The next open reads it again instead of serving the degraded result.
+ sameError = null;
+ const fresh = await getInsightsData('b1', { anchor24h: true });
+ expect(fresh.comparison).toMatchObject({ basis: 'same', newPrompts: 509 });
+ expect(cache.size).toBe(1);
+ });
+
+ it('caches the view once the comparison reads', async () => {
+ const first = await getInsightsData('b1', { anchor24h: true });
+ sameError = { message: 'canceling statement due to statement timeout' };
+ const second = await getInsightsData('b1', { anchor24h: true });
+
+ expect(second.comparison).toEqual(first.comparison);
+ expect(second.comparison?.samePrompts).toBe(200);
+ });
+});
diff --git a/web/src/lib/actions/tracking-visibility-parity.test.ts b/web/src/lib/actions/tracking-visibility-parity.test.ts
index 80cac9955..e6d843fba 100644
--- a/web/src/lib/actions/tracking-visibility-parity.test.ts
+++ b/web/src/lib/actions/tracking-visibility-parity.test.ts
@@ -63,6 +63,7 @@ const SAME_PAYLOAD = {
prev: sameTotals(780, 200, 260),
};
let SAME: unknown = null;
+let SAME_ERROR: { message: string } | null = null;
const groups = {
groups: [
@@ -117,7 +118,10 @@ function rpc(name: string, args: Record<string, unknown>) {
else if (name.startsWith('visibility_rate_trend')) data = [];
else if (name.startsWith('competitor_aggregates')) data = competitorAgg;
else if (name.startsWith('insights_aggregates')) data = isPrev(args) ? SUMMARY_PREV : SUMMARY_CUR;
- else if (name.startsWith('insights_same_prompt_compare')) data = SAME;
+ else if (name.startsWith('insights_same_prompt_compare')) {
+ if (SAME_ERROR) return Promise.resolve({ data: null, error: SAME_ERROR });
+ data = SAME;
+ }
return Promise.resolve({ data, error: null });
}
@@ -253,6 +257,7 @@ describe('Same prompts parity (VIS-002)', () => {
PREV = visibilityPayload(1600, 400, 176);
SUMMARY_PREV = summaryPayload(1600, 500);
SAME = SAME_PAYLOAD;
+ SAME_ERROR = null;
});
/** What the page gets: the cards, re-measured on the shared prompts. */
@@ -314,6 +319,19 @@ describe('Same prompts parity (VIS-002)', () => {
expect(panel.delta).toBe(card.trend!.summary.change);
});
+ it('compares all prompts in the panel when the shared-prompt read fails', async () => {
+ SAME_ERROR = { message: 'canceling statement due to statement timeout' };
+ const errorLog = vi.spyOn(console, 'error').mockImplementation(() => {});
+ const card = (await getVisibilityRateTrend('b1', thirtyDays)).summary;
+ const panel = await getInsightsBreakdown('b1', 'visibility', thirtyDays);
+
+ expect(panel.comparison).toBeNull();
+ expect(panel.delta).toBe(card.change);
+ expect(panel.comparedCur).toBe(panel.curTotal);
+ expect(errorLog).toHaveBeenCalled();
+ errorLog.mockRestore();
+ });
+
it('reads the shared prompts over the card\u2019s whole days', async () => {
await getInsightsBreakdown('b1', 'visibility', thirtyDays);
diff --git a/web/src/lib/actions/tracking.ts b/web/src/lib/actions/tracking.ts
index 661f861c4..079629ed8 100644
--- a/web/src/lib/actions/tracking.ts
+++ b/web/src/lib/actions/tracking.ts
@@ -832,7 +832,7 @@ async function loadWindowedInsights(
null,
'visibility trend',
),
- getSamePromptComparison(brandId, filterOpts),
+ lenient(getSamePromptComparison(brandId, filterOpts), null, 'same-prompt comparison'),
],
);
const windowed = { summary, competitors, sov, visibilityRate, filterOptions, trend };
@@ -842,9 +842,9 @@ async function loadWindowedInsights(
/**
* The comparison windows' totals over the prompts answered in both
* (VIS-002): the daily rollups for whole days, the raw rows for the 24h and
- * topic views. Null without a previous window, while the rollups behind it
- * are not yet backfilled (00130), or when the read fails — the page then
- * compares all prompts, as it always did.
+ * topic views. Null without a previous window, or while the rollups behind it
+ * are not yet backfilled (00130) — the page then compares all prompts, as it
+ * always did. A failed read throws, so strict mode keeps it out of the cache.
*/
async function getSamePromptComparison(
brandId: string,
@@ -883,10 +883,7 @@ async function getSamePromptComparison(
});
}
const { data, error } = res;
- if (error) {
- console.error('[insights] same-prompt comparison failed', error);
- return null;
- }
+ if (error) throw new Error(error.message);
return parseSamePromptComparison(data);
}
@@ -2998,7 +2995,11 @@ export async function getInsightsBreakdown(
windows.prev ? groupsFor(windows.prev) : Promise.resolve(null),
totalFor(windows.cur),
windows.prev ? totalFor(windows.prev) : Promise.resolve(null),
- getSamePromptComparison(brandId, opts, windows),
+ // Without it the panel compares all prompts, as the cards then do.
+ getSamePromptComparison(brandId, opts, windows).catch((err) => {
+ console.error('[insights] same-prompt comparison failed', err);
+ return null;
+ }),
]);
if (curGroupsRes.error) throw new Error(curGroupsRes.error.message);
if (prevGroupsRes?.error) throw new Error(prevGroupsRes.error.message);