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: recover pulses lost to process death with a daily catch-up sweep (#654)
Gökhan Geyike1515add145e
diff --git a/server/src/lib/pulse/engine.js b/server/src/lib/pulse/engine.jsindex 98340e8dd..e7ecd6076 100644--- a/server/src/lib/pulse/engine.js+++ b/server/src/lib/pulse/engine.js@@ -100,10 +100,13 @@ async function recentWarningKeys(brandId, now) {return keys;}-export async function generatePulseForBrand(brandId) {+export async function generatePulseForBrand(brandId, { pulseDate: pulseDateOverride } = {}) {try {const now = new Date();- const pulseDate = now.toISOString().slice(0, 10);+ // The catch-up sweep passes the missed run's completion date so a+ // recovered pulse claims THAT day's dedupe slot — today's real pulse+ // (fired when today's run completes) keeps its own date.+ const pulseDate = pulseDateOverride ?? now.toISOString().slice(0, 10);const { data: brand } = await supabaseAdmin.from('brands')@@ -239,3 +242,59 @@ export async function generatePulseForBrand(brandId) {return { error: true };}}++/**+ * Catch-up sweep (#missed-pulse incident): the per-brand pulse trigger is+ * fire-and-forget at the end of a tracking job — a process death, deploy, or+ * OOM in that window silently eats the mail while the run's stamp survives.+ * This sweep runs at the start of the daily cycle: any brand whose latest+ * completed run (last 36h) has no pulse row created after it gets a recovery+ * pulse, dated to that run's completion day so today's real pulse keeps its+ * own dedupe slot. The sent_pulses unique key makes double-sends impossible.+ */+export async function runPulseCatchUp() {+ try {+ const since = new Date(Date.now() - 36 * 60 * 60 * 1000).toISOString();+ const { data: runs, error } = await supabaseAdmin+ .from('tracking_runs')+ .select('brand_id, completed_at')+ .not('completed_at', 'is', null)+ .gte('completed_at', since)+ .order('completed_at', { ascending: false });+ if (error) throw new Error(error.message);++ const latestByBrand = new Map();+ for (const r of runs || []) {+ if (!latestByBrand.has(r.brand_id)) latestByBrand.set(r.brand_id, r.completed_at);+ }++ let sent = 0;+ let skipped = 0;+ for (const [brandId, completedAt] of latestByBrand) {+ const { count, error: countErr } = await supabaseAdmin+ .from('sent_pulses')+ .select('*', { count: 'exact', head: true })+ .eq('brand_id', brandId)+ .gte('created_at', completedAt);+ if (countErr || (count ?? 0) > 0) continue; // run already pulsed (or unknowable)++ const result = await generatePulseForBrand(brandId, {+ pulseDate: completedAt.slice(0, 10),+ });+ if (result?.generated) {+ logger.info({ brandId, runCompletedAt: completedAt }, '[pulse] catch-up pulse sent');+ sent++;+ } else {+ skipped++;+ }+ }++ if (sent > 0 || skipped > 0) {+ logger.info({ sent, skipped }, '[pulse] catch-up sweep completed');+ }+ return { sent, skipped };+ } catch (err) {+ logger.error({ err }, '[pulse] catch-up sweep failed');+ return { error: true };+ }+}diff --git a/server/src/server.js b/server/src/server.jsindex 18e8d4c34..9540971e1 100644--- a/server/src/server.js+++ b/server/src/server.js@@ -31,6 +31,7 @@ import supabaseAdmin from './config/supabase.js';import { getPlan, hasFeature, isCloud, isSubscriptionActive } from './config/plans.js';import { analyzeBrandVolumes } from './lib/volume-analysis.js';import { runGscSync } from './lib/gsc-sync.js';+import { runPulseCatchUp } from './lib/pulse/engine.js';const app = express();app.set('trust proxy', 1);@@ -112,6 +113,13 @@ async function runDailyTracking() {// self-hosted, since both paths funnel through here).await cleanupStalePendingTasks();+ // Recover pulses whose fire-and-forget trigger died with the process+ // (OOM, deploy, crash) after the run had already stamped. Non-blocking —+ // today's runs and their own pulses are independent of this sweep.+ runPulseCatchUp().catch((err) => {+ logger.error({ err }, '[pulse] catch-up sweep crashed');+ });+// Skip paused brands (is_active = false): the user has explicitly suspended// tracking for them, so they should not spend Cloro / LLM credits daily.const { data: brands, error } = await supabaseAdmin