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(action-center): score actions on nights the generator skips, and backfill unscored ones (#1145)
Cihaneb217f5ed2d2
diff --git a/server/src/lib/action-center/priority-data.js b/server/src/lib/action-center/priority-data.jsindex 31a3c089e..588ccf561 100644--- a/server/src/lib/action-center/priority-data.js+++ b/server/src/lib/action-center/priority-data.js@@ -119,6 +119,78 @@ export async function rescoreAction(action, now = new Date()) {return true;}+/**+ * A brand's actions scored again in one go, as the nightly pass's generator+ * does: their open signals and the prompts' demand are read once, not per+ * action. For the nights the generator doesn't run (a signal pass skipped+ * for no fresh results) and for the one-off backfill of actions opened+ * before scores existed (scripts/rescore-actions.js).+ *+ * @param {string} brandId+ * @param {{ now?: Date, statuses?: string[] | null, unscored?: boolean }} [options]+ * statuses: the actions' statuses, null for all. unscored: only actions with no score yet.+ * @returns {Promise<number>} how many actions were scored+ */+export async function rescoreBrandActions(+ brandId,+ { now = new Date(), statuses = ACTIVE_STATUSES, unscored = false } = {},+) {+ let query = supabaseAdmin+ .from('actions')+ .select('id, kind, category, impact, payload, created_at')+ .eq('brand_id', brandId);+ if (statuses) query = query.in('status', statuses);+ if (unscored) query = query.is('priority_score', null);+ const { data: actions, error } = await query.limit(1000);+ if (error) throw new Error(error.message);+ if ((actions ?? []).length === 0) return 0;++ const { data: signals, error: signalsErr } = await supabaseAdmin+ .from('signals')+ .select('id, kind, impact, payload, current_value, source, detected_at, action_id')+ .eq('brand_id', brandId)+ .in('status', OPEN_SIGNAL_STATUSES)+ .not('action_id', 'is', null)+ .limit(1000);+ if (signalsErr) throw new Error(signalsErr.message);++ let prompts = new Map();+ try {+ prompts = await loadPromptDemand(+ promptIdsOf([+ ...actions.map((action) => action.payload),+ ...(signals ?? []).map((signal) => signal.payload),+ ]),+ );+ } catch (err) {+ logger.warn({ brandId, err: err.message }, '[actions] prompt demand unavailable');+ }++ const evidence = new Map();+ for (const signal of signals ?? []) {+ if (!evidence.has(signal.action_id)) evidence.set(signal.action_id, []);+ evidence.get(signal.action_id).push(signal);+ }++ let scored = 0;+ for (const action of actions) {+ const priority = scorePriority(+ { action, signals: evidence.get(action.id) ?? [], prompts },+ now,+ );+ const { error: saveErr } = await supabaseAdmin+ .from('actions')+ .update(priorityColumns(priority, now))+ .eq('id', action.id);+ if (saveErr) {+ logger.warn({ actionId: action.id, err: saveErr.message }, '[actions] priority not saved');+ continue;+ }+ scored += 1;+ }+ return scored;+}+/*** Every active action a signal supports, scored again (AC-007): resolving,* dismissing or reopening a signal changes its actions' support anddiff --git a/server/src/lib/action-center/priority-data.test.js b/server/src/lib/action-center/priority-data.test.jsindex dba357337..96d53aa91 100644--- a/server/src/lib/action-center/priority-data.test.js+++ b/server/src/lib/action-center/priority-data.test.js@@ -2,32 +2,70 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';/*** What the priority score reads (AC-006): each prompt's demand and intent,- * the largest volume across its rows and the newest intent.+ * the largest volume across its rows and the newest intent. And a brand's+ * actions scored again in one go, from their own open signals.*/let rows = [];let reads = [];let pages = [];let fail = false;+let actionRows = [];+let signalRows = [];+let queries = [];+let saved = [];+let unsaved = new Set();vi.mock('../../config/supabase.js', () => ({default: {- from: () => {+ from: (table) => {let ids = [];+ const op = { table, filters: [] };+ queries.push(op);const chain = {select: () => chain,+ eq: (column, value) => {+ op.filters.push(['eq', column, value]);+ return chain;+ },in: (column, values) => {- ids = values;- reads.push(values);+ op.filters.push(['in', column, values]);+ if (table === 'prompt_volumes') {+ ids = values;+ reads.push(values);+ }+ return chain;+ },+ is: (column, value) => {+ op.filters.push(['is', column, value]);+ return chain;+ },+ not: (column, operator, value) => {+ op.filters.push(['not', column, value]);return chain;},order: () => chain,+ limit: async () => ({+ data: table === 'actions' ? actionRows : signalRows,+ error: null,+ }),range: async (from, to) => {pages.push([from, to]);if (fail) return { data: null, error: { message: 'timeout' } };const matching = rows.filter((r) => ids.includes(r.prompt_id));return { data: matching.slice(from, to + 1), error: null };},+ update: (row) => {+ op.update = row;+ return chain;+ },+ // An update, awaited after its .eq('id', …).+ then: (resolve) => {+ const id = op.filters.find(([, column]) => column === 'id')?.[2];+ if (unsaved.has(id)) return resolve({ error: { message: 'write failed' } });+ saved.push({ id, row: op.update });+ return resolve({ error: null });+ },};return chain;},@@ -35,12 +73,18 @@ vi.mock('../../config/supabase.js', () => ({}));vi.mock('../logger.js', () => ({ logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } }));-const { loadPromptDemand, priorityOnCreate } = await import('./priority-data.js');+const { loadPromptDemand, priorityOnCreate, rescoreBrandActions } =+ await import('./priority-data.js');beforeEach(() => {reads = [];pages = [];fail = false;+ actionRows = [];+ signalRows = [];+ queries = [];+ saved = [];+ unsaved = new Set();// Newest first, as the read orders them.rows = [{ prompt_id: 'p1', est_ai_volume: 300, intent: 'comparison', fetched_at: '2026-10-10' },@@ -108,3 +152,78 @@ describe('priorityOnCreate', () => {expect(columns.priority_score).toEqual(expect.any(Number));});});++describe('rescoreBrandActions', () => {+ const NOW = new Date('2026-10-18T03:00:00Z');+ const recover = (id, payload = {}) => ({+ id,+ kind: 'recover_lost_citations',+ category: 'recover',+ impact: 'high',+ payload,+ created_at: '2026-09-27T02:00:00Z',+ });+ const filtersOf = (table) => queries.find((q) => q.table === table).filters;++ it("scores a brand's active actions, each from its own open signals", async () => {+ actionRows = [recover('a1', { promptIds: ['p1'] }), recover('a2')];+ signalRows = [+ {+ id: 's1',+ action_id: 'a1',+ kind: 'citation_lost',+ impact: 'high',+ payload: {},+ detected_at: '2026-10-16T02:00:00Z',+ },+ ];++ expect(await rescoreBrandActions('b1', { now: NOW })).toBe(2);++ expect(filtersOf('actions')).toEqual([+ ['eq', 'brand_id', 'b1'],+ ['in', 'status', ['new', 'in_progress', 'on_hold']],+ ]);+ expect(filtersOf('signals')).toEqual([+ ['eq', 'brand_id', 'b1'],+ ['in', 'status', ['new', 'acknowledged']],+ ['not', 'action_id', null],+ ]);+ // The signals and the demand are read once for the brand, not per action.+ expect(queries.filter((q) => q.table === 'signals')).toHaveLength(1);+ expect(reads).toEqual([['p1']]);++ const byId = Object.fromEntries(saved.map(({ id, row }) => [id, row]));+ expect(byId.a1).toMatchObject({+ priority_score: expect.any(Number),+ priority_updated_at: NOW.toISOString(),+ });+ expect(byId.a1.priority_reason).toMatchObject({ days: 2, kinds: 1, demand: 'high' });+ // No open signal of its own: nothing says what changed or when.+ expect(byId.a2.priority_reason).toMatchObject({ days: null, kinds: 0 });+ });++ it('backfills only the actions with no score, in any status', async () => {+ actionRows = [recover('a1')];++ expect(await rescoreBrandActions('b1', { now: NOW, statuses: null, unscored: true })).toBe(1);+ expect(filtersOf('actions')).toEqual([+ ['eq', 'brand_id', 'b1'],+ ['is', 'priority_score', null],+ ]);+ });++ it('reads nothing more for a brand with no actions', async () => {+ expect(await rescoreBrandActions('b1', { now: NOW })).toBe(0);+ expect(queries.map((q) => q.table)).toEqual(['actions']);+ });++ it('carries on past a score it cannot save, and without demand', async () => {+ actionRows = [recover('a1', { promptIds: ['p1'] }), recover('a2')];+ unsaved.add('a1');+ fail = true;++ expect(await rescoreBrandActions('b1', { now: NOW })).toBe(1);+ expect(saved.map(({ id }) => id)).toEqual(['a2']);+ });+});diff --git a/server/src/lib/signals/pass.js b/server/src/lib/signals/pass.jsindex a74887184..cc89b4df8 100644--- a/server/src/lib/signals/pass.js+++ b/server/src/lib/signals/pass.js@@ -22,6 +22,7 @@ import supabaseAdmin from '../../config/supabase.js';import { logger } from '../logger.js';import { recordSignalsForBrand } from './record.js';import { generateActionsForBrand } from '../action-center/generate.js';+import { rescoreBrandActions } from '../action-center/priority-data.js';import { sweepActionTasks } from '../action-center/task-writer/index.js';/**@@ -47,6 +48,22 @@ async function latestStampedRun(brandId, { anySource = false } = {}) {return data?.[0]?.completed_at ?? null;}+/**+ * A night with no fresh results runs no generator, and the generator is what+ * scores the brand's active actions again. Without this, a brand whose+ * tracking is quiet keeps last night's scores, and an action opened before+ * scores existed keeps none. Best effort, as the generator's rescoring is: a+ * score that can't be worked out is no reason to fail the pass.+ */+async function rescoreSkippedNight(brandId, now) {+ try {+ return { rescored: await rescoreBrandActions(brandId, { now }) };+ } catch (err) {+ logger.warn({ brandId, err: err.message }, '[actions] rescore on a skipped night failed');+ return null;+ }+}+/*** Record signals and consolidate them into actions for one brand, then log* the pass against the run it covered.@@ -68,7 +85,9 @@ export async function runSignalPass(brandId, { runAt, now = new Date() } = {}) {}const recorded = await recordSignalsForBrand(brandId, { now });- const generated = recorded.skipped ? null : await generateActionsForBrand(brandId, { now });+ const generated = recorded.skipped+ ? await rescoreSkippedNight(brandId, now)+ : await generateActionsForBrand(brandId, { now });const { error } = await supabaseAdmin.from('signal_runs').upsert({diff --git a/server/src/lib/signals/pass.test.js b/server/src/lib/signals/pass.test.jsindex 88af4e74d..308a8ccb8 100644--- a/server/src/lib/signals/pass.test.js+++ b/server/src/lib/signals/pass.test.js@@ -12,6 +12,10 @@ vi.mock('./record.js', () => ({ recordSignalsForBrand: (...a) => recordSignalsFovi.mock('../action-center/generate.js', () => ({generateActionsForBrand: (...a) => generateActionsForBrand(...a),}));+const rescoreBrandActions = vi.fn();+vi.mock('../action-center/priority-data.js', () => ({+ rescoreBrandActions: (...a) => rescoreBrandActions(...a),+}));import { runFirstSignalPass, runSignalCatchUp, runSignalPass } from './pass.js';@@ -55,6 +59,8 @@ beforeEach(() => {from.mockReset();recordSignalsForBrand.mockReset();generateActionsForBrand.mockReset();+ rescoreBrandActions.mockReset();+ rescoreBrandActions.mockResolvedValue(3);recordSignalsForBrand.mockResolvedValue({ inserted: 2, refreshed: 1 });generateActionsForBrand.mockResolvedValue({ created: 1, refreshed: 0 });});@@ -111,6 +117,38 @@ describe('runSignalPass', () => {expect(writes[0].row.outcome).toBe('skipped');});+ /**+ * The generator is what scores the active actions again each night; with+ * no generator, the pass scores them itself.+ */+ it("still scores the brand's active actions on a skipped night", async () => {+ const writes = mockDb({ runs: [{ completed_at: RUN_AT }] });+ recordSignalsForBrand.mockResolvedValue({ skipped: 'no_fresh_results' });++ await runSignalPass(BRAND, { now: NOW });++ expect(rescoreBrandActions).toHaveBeenCalledWith(BRAND, { now: NOW });+ expect(writes[0].row.detail.actions).toEqual({ rescored: 3 });+ });++ it('logs a skipped night even when its scores fail', async () => {+ const writes = mockDb({ runs: [{ completed_at: RUN_AT }] });+ recordSignalsForBrand.mockResolvedValue({ skipped: 'no_fresh_results' });+ rescoreBrandActions.mockRejectedValue(new Error('timeout'));++ await runSignalPass(BRAND, { now: NOW });++ expect(writes[0].row).toMatchObject({ outcome: 'skipped', detail: { actions: null } });+ });++ it('leaves the scores to the generator on a recorded night', async () => {+ mockDb({ runs: [{ completed_at: RUN_AT }] });++ await runSignalPass(BRAND, { now: NOW });++ expect(rescoreBrandActions).not.toHaveBeenCalled();+ });+it('does nothing for a brand whose nightly run has never stamped', async () => {const writes = mockDb({ runs: [] });diff --git a/server/src/scripts/rescore-actions.js b/server/src/scripts/rescore-actions.jsnew file mode 100644index 000000000..9e5abd34c--- /dev/null+++ b/server/src/scripts/rescore-actions.js@@ -0,0 +1,46 @@+/**+ * One-off priority backfill (Action Center).+ *+ * Scores every action that has no priority score yet, for one brand or for+ * every brand. Scores arrived with migration 00144: an action is scored when+ * it is created and again every night while it is active, so the actions+ * opened before then show "—" until the next nightly pass, and the ones+ * already completed or dismissed never get one. This scores both now, from+ * their open signals as they stand today.+ *+ * Safe to re-run: only actions with no score are touched, so a repeat picks+ * up where a failed or capped run (1,000 actions per brand) stopped.+ *+ * `--all` takes paused brands too, unlike backfill-signals.js: their actions+ * are still listed in the Action Center, and scoring them costs no tracking.+ *+ * Run: node src/scripts/rescore-actions.js [brandId|--all]+ */+import 'dotenv/config';+import supabaseAdmin from '../config/supabase.js';+import { rescoreBrandActions } from '../lib/action-center/priority-data.js';++const arg = process.argv[2];+if (!arg) {+ console.error('Usage: node src/scripts/rescore-actions.js <brandId|--all>');+ process.exit(1);+}++let brandIds;+if (arg === '--all') {+ const { data, error } = await supabaseAdmin.from('brands').select('id').limit(1000);+ if (error) throw new Error(error.message);+ brandIds = (data ?? []).map((b) => b.id);+} else {+ brandIds = [arg];+}++for (const brandId of brandIds) {+ try {+ const scored = await rescoreBrandActions(brandId, { statuses: null, unscored: true });+ console.log(brandId, JSON.stringify({ scored }));+ } catch (err) {+ console.error(brandId, 'FAILED:', err.message);+ }+}+process.exit(0);