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(content): keep a missed brand facts page under the hourly cap and its status through a failed weekly read (CE-047 follow-ups) (#1466)
Cihan34aa8be1f516
diff --git a/server/src/lib/brand-facts-sweep.js b/server/src/lib/brand-facts-sweep.jsindex 918886861..e5bda702f 100644--- a/server/src/lib/brand-facts-sweep.js+++ b/server/src/lib/brand-facts-sweep.js@@ -19,6 +19,8 @@ export const REFRESH_DAYS = 7;export const SWEEP_LIMIT = 25;/** How long an approved fact gone from the site is kept, shown as gone. */export const GONE_KEEP_DAYS = 30;+/** How long one run reads: 25 slow sites in a row could keep it busy half an hour. */+export const SWEEP_DEADLINE_MS = 10 * 60_000;const DAY_MS = 86_400_000;@@ -75,8 +77,10 @@ export async function sweepBrandFacts({const markRead = [];const markFailed = [];let tried = 0;+ const started = Date.now();for (const brand of brands) {- if (tried >= limit) break;+ // The brands left stay first in line for the next run.+ if (tried >= limit || Date.now() - started > SWEEP_DEADLINE_MS) break;if (!active(brand.orgId)) {outcome.skipped += 1;markRead.push(brand.id);@@ -119,11 +123,19 @@ export async function sweepBrandFacts({);}if (markFailed.length) {+ // A page whose answer missed the schema stays missed: failed, it would+ // keep its hash and be taken as read, its facts never read.writes.push(db.from('brand_fact_pages').update({ checked_at: nowIso, status: 'failed' })- .in('brand_id', markFailed),+ .in('brand_id', markFailed)+ .neq('status', 'missed'),+ db+ .from('brand_fact_pages')+ .update({ checked_at: nowIso })+ .in('brand_id', markFailed)+ .eq('status', 'missed'),);}for (const res of await Promise.all(writes)) {diff --git a/server/src/lib/brand-facts-sweep.test.js b/server/src/lib/brand-facts-sweep.test.jsindex abcff9364..b109ff734 100644--- a/server/src/lib/brand-facts-sweep.test.js+++ b/server/src/lib/brand-facts-sweep.test.js@@ -13,7 +13,8 @@ vi.mock('../config/plans.js', () => ({}));vi.mock('./brand-facts.js', () => ({ refreshOnce: vi.fn() }));-const { GONE_KEEP_DAYS, REFRESH_DAYS, sweepBrandFacts } = await import('./brand-facts-sweep.js');+const { GONE_KEEP_DAYS, REFRESH_DAYS, SWEEP_DEADLINE_MS, sweepBrandFacts } =+ await import('./brand-facts-sweep.js');const NOW = Date.parse('2026-10-24T03:00:00Z');const DAY = 86_400_000;@@ -28,6 +29,7 @@ function fakeDb(tables) {const chain = {select: (columns) => (reads.push({ table, columns }), chain),eq: (col, value) => (q.filters.push(['eq', col, value]), chain),+ neq: (col, value) => (q.filters.push(['neq', col, value]), chain),in: (col, value) => (q.filters.push(['in', col, value]), chain),lt: (col, value) => (q.filters.push(['lt', col, value]), chain),order: () => chain,@@ -130,9 +132,20 @@ describe('sweepBrandFacts', () => {payload: { checked_at: new Date(NOW).toISOString() },filters: [['in', 'brand_id', ['b1', 'b2']]],}),+ // A missed page stays missed, its retry after a day kept.expect.objectContaining({payload: { checked_at: new Date(NOW).toISOString(), status: 'failed' },- filters: [['in', 'brand_id', ['b5']]],+ filters: [+ ['in', 'brand_id', ['b5']],+ ['neq', 'status', 'missed'],+ ],+ }),+ expect.objectContaining({+ payload: { checked_at: new Date(NOW).toISOString() },+ filters: [+ ['in', 'brand_id', ['b5']],+ ['eq', 'status', 'missed'],+ ],}),]);});@@ -182,4 +195,21 @@ describe('sweepBrandFacts', () => {expect(await sweepBrandFacts({ now: NOW, db, refresh })).toEqual({ skipped: 'off' });expect(refresh).not.toHaveBeenCalled();});++ it('stops reading at its deadline, the brands left first in line next run', async () => {+ const { db } = fakeDb({+ brand_fact_pages: [row('b1', 'o1', 9), row('b2', 'o1', 9)],+ organizations: [{ id: 'o1', subscription_status: 'active' }],+ });+ const clock = vi.spyOn(Date, 'now');+ let t = NOW;+ clock.mockImplementation(() => t);+ const refresh = vi.fn(async () => {+ t += SWEEP_DEADLINE_MS + 1;+ return { noDomain: false, paused: false };+ });+ await sweepBrandFacts({ now: NOW, db, refresh });+ expect(refresh.mock.calls.map(([id]) => id)).toEqual(['b1']);+ clock.mockRestore();+ });});diff --git a/server/src/lib/brand-facts.js b/server/src/lib/brand-facts.jsindex 3c0321119..f58423aec 100644--- a/server/src/lib/brand-facts.js+++ b/server/src/lib/brand-facts.js@@ -760,9 +760,18 @@ export async function refreshBrandFacts(},};}- // Changed again within the hour (a counter, a date): read next time.- if (extracted > now - MODEL_EVERY_MS && !missed) {- return { page, text: words, row: { status: 'ok', ...keep } };+ // Changed again within the hour (a counter, a date): read next time,+ // a page whose answer missed the schema too (it stays missed).+ if (extracted > now - MODEL_EVERY_MS) {+ return {+ page,+ text: words,+ row: {+ status: missed ? 'missed' : 'ok',+ text_hash: prev?.text_hash ?? null,+ extracted_at: keep.extracted_at,+ },+ };}if (!words) {return { page, row: { status: 'ok', text_hash: hash, extracted_at: keep.extracted_at } };diff --git a/server/src/lib/brand-facts.test.js b/server/src/lib/brand-facts.test.jsindex 2fc93433f..026be4fef 100644--- a/server/src/lib/brand-facts.test.js+++ b/server/src/lib/brand-facts.test.js@@ -575,6 +575,33 @@ describe('refreshBrandFacts', () => {expect(writesOf(writes, 'brand_facts', 'upsert')).toEqual([]);});+ it('reads a missed page whose text changes by the model once an hour at most too', async () => {+ const recent = new Date(NOW - 10 * 60_000).toISOString();+ const { db, writes } = fakeDb(+ base({+ brand_fact_pages: [+ {+ kind: 'pricing',+ text_hash: 'aaaaaaaaaaaaaaaa',+ status: 'missed',+ extracted_at: recent,+ },+ ],+ }),+ );+ await refresh(db);+ expect(generateObject.mock.calls.map(([c]) => c.prompt.slice(0, 13))).toEqual([+ 'Page: about p',+ ]);+ const [pages] = writesOf(writes, 'brand_fact_pages', 'upsert');+ expect(pages.payload[0]).toMatchObject({+ kind: 'pricing',+ status: 'missed',+ text_hash: 'aaaaaaaaaaaaaaaa',+ extracted_at: recent,+ });+ });+it('charges an answer that missed the schema and reads that page again after a day; a timeout keeps its hold', async () => {generateObject.mockImplementation(async ({ prompt }) => {if (prompt.startsWith('Page: pricing')) {