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(pulse): stop the catch-up sweep from pulsing manual and backfill runs (#774)
Gökhan Geyiked94396bd015
diff --git a/server/src/lib/pulse/engine.js b/server/src/lib/pulse/engine.jsindex b5ea7a799..bd9c32b4f 100644--- a/server/src/lib/pulse/engine.js+++ b/server/src/lib/pulse/engine.js@@ -322,6 +322,11 @@ export async function generatePulseForBrand(brandId, { pulseDate: pulseDateOverr* (brand_id, pulse_date) key stops the same day being sent twice, and the* window key (#701) stops the recovery pulse repeating a run that today's own* pulse is about to report — the two dates disagree, the data does not.+ *+ * Cron runs only: the pulse trigger never fires for manual/immediate runs, so+ * those are not "missed" pulses to recover. Sweeping them mailed every new+ * brand twice on its first morning — once at cycle start for yesterday's+ * onboarding run, once when its own daily run completed an hour later.*/export async function runPulseCatchUp() {try {@@ -329,6 +334,7 @@ export async function runPulseCatchUp() {const { data: runs, error } = await supabaseAdmin.from('tracking_runs').select('brand_id, completed_at')+ .eq('source', 'cron').not('completed_at', 'is', null).gte('completed_at', since).order('completed_at', { ascending: false });diff --git a/server/src/lib/pulse/engine.test.js b/server/src/lib/pulse/engine.test.jsindex 8d1c55b4d..e3279d248 100644--- a/server/src/lib/pulse/engine.test.js+++ b/server/src/lib/pulse/engine.test.js@@ -1,10 +1,11 @@-import { describe, expect, it, vi } from 'vitest';+import { beforeEach, describe, expect, it, vi } from 'vitest';// The module imports the supabase admin client, whose config hard-exits when// SUPABASE_* env is absent (as in CI) — stub it before the import chain.-vi.mock('../../config/supabase.js', () => ({ default: {} }));+const from = vi.fn();+vi.mock('../../config/supabase.js', () => ({ default: { from: (...a) => from(...a) } }));-import { pulseWindowKey } from './engine.js';+import { pulseWindowKey, runPulseCatchUp } from './engine.js';/*** Pulse window dedupe (#701).@@ -55,3 +56,77 @@ describe('pulseWindowKey', () => {expect(pulseWindowKey({ window: { runAnchored: true } })).toBeNull();});});++/**+ * Catch-up sweep scope: only cron runs can have a "missed" pulse, because the+ * pulse trigger never fires for manual or backfill runs in the first place.+ * Sweeping those mailed every new brand twice on its first morning — the+ * onboarding run (deliberately unpulsed) was "recovered" at cycle start, then+ * the brand's own daily run pulsed an hour later.+ */+describe('runPulseCatchUp', () => {+ // Minimal filtering query fake: applies eq/not/gte predicates to the given+ // rows and resolves like postgrest ({ data, count, error: null }).+ function fakeTable(rows) {+ const filters = [];+ const matches = () => rows.filter((r) => filters.every((f) => f(r)));+ const builder = {+ select: () => builder,+ eq: (col, val) => (filters.push((r) => r[col] === val), builder),+ gte: (col, val) => (filters.push((r) => r[col] >= val), builder),+ not: (col, op, val) => {+ if (op === 'is' && val === null) filters.push((r) => r[col] !== null);+ return builder;+ },+ order: () => builder,+ then: (resolve) => {+ const data = matches();+ return Promise.resolve({ data, count: data.length, error: null }).then(resolve);+ },+ };+ return builder;+ }++ let queriedTables;++ beforeEach(() => {+ from.mockReset();+ queriedTables = [];+ });++ function tables(fixtures) {+ from.mockImplementation((table) => {+ queriedTables.push(table);+ return fakeTable(fixtures[table] ?? []);+ });+ }++ const recent = new Date(Date.now() - 60 * 60 * 1000).toISOString();++ it('ignores manual and backfill runs — they were never owed a pulse', async () => {+ tables({+ tracking_runs: [+ { brand_id: 'new-brand', completed_at: recent, source: 'manual' },+ { brand_id: 'old-brand', completed_at: recent, source: 'backfill' },+ ],+ });++ const res = await runPulseCatchUp();++ expect(res).toEqual({ sent: 0, skipped: 0 });+ expect(queriedTables).not.toContain('sent_pulses');+ });++ it('still considers a cron run with no pulse row after it', async () => {+ tables({+ tracking_runs: [{ brand_id: 'brand-1', completed_at: recent, source: 'cron' }],+ // A pulse already recorded after the run: the sweep looks, then skips.+ sent_pulses: [{ brand_id: 'brand-1', created_at: new Date().toISOString() }],+ });++ const res = await runPulseCatchUp();++ expect(res).toEqual({ sent: 0, skipped: 0 });+ expect(queriedTables).toContain('sent_pulses');+ });+});