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
feat(action-center): check every night that each action's count and its list agree (AC-009, 3/3) (#1390)
Cihan24efbd720ebf
diff --git a/server/src/lib/health/report.js b/server/src/lib/health/report.jsindex 8184daac6..586b7cd68 100644--- a/server/src/lib/health/report.js+++ b/server/src/lib/health/report.js@@ -234,11 +234,23 @@ async function section(name, fn) {}}+/**+ * Open actions whose saved count and kept list disagree (AC-009,+ * action_count_drift, 00245): a list missing, a list of another size, or a+ * task naming another count. Each should be 0.+ */+async function checkActionCounts() {+ const { data, error } = await supabaseAdmin.rpc('action_count_drift', { p_examples: 5 });+ if (error) throw new Error(error.message);+ return data ?? {};+}+export async function collectHealthReport(now = Date.now()) {const probes = await section('probes', runProbes);const nightly = await section('nightly', () => checkNightlyRuns(now));const cloro = await section('cloro', () => checkCloro(now));const fanout = await section('fanout', () => checkFanout(now));+ const actionCounts = await section('actionCounts', checkActionCounts);return {generatedAt: new Date(now).toISOString(),serverUptimeHours: Math.round((process.uptime() / 3600) * 10) / 10,@@ -246,9 +258,25 @@ export async function collectHealthReport(now = Date.now()) {nightly,cloro,fanout,+ actionCounts,};}+/** What each kind of count drift reads as in the report (AC-009). */+const COUNT_DRIFT = {+ missing: 'kept no list of what they are about',+ mismatch: 'kept a list of a different size from their count',+ tasks: "have a task that names a different count from the action's",+};++/** "(AC-40, AC-12)", or "(e.g. AC-40, AC-12)" when there are more: the newest that drifted. */+const actionNumbers = (examples, count) => {+ const list = examples ?? [];+ if (list.length === 0) return '';+ const names = list.map((no) => `AC-${no}`).join(', ');+ return count > list.length ? ` (e.g. ${names})` : ` (${names})`;+};+/** Problems worth a person's attention, as short lines. Empty means all clear. */export function findIssues(report) {const issues = [];@@ -289,6 +317,18 @@ export function findIssues(report) {`(${percent(p.baselineRate)} the days before) — check what Cloro returns`,);}++ // Reports from before the action count check have none.+ const counts = report.actionCounts ?? {};+ if (counts.error) issues.push(`Action counts could not be checked: ${counts.error}`);+ for (const [kind, words] of Object.entries(COUNT_DRIFT)) {+ const found = counts[kind];+ if (found?.count > 0) {+ issues.push(+ `${found.count} open action(s) ${words}${actionNumbers(found.examples, found.count)}`,+ );+ }+ }return issues;}@@ -318,6 +358,18 @@ function fanoutTable(fanout) {: '<p>No answers.</p>';}+function actionCountsBlock(counts) {+ if (!counts) return '<p>Not checked.</p>';+ if (counts.error) return `<p>Could not check: ${escapeHtml(counts.error)}</p>`;+ const lines = Object.entries(COUNT_DRIFT).map(+ ([kind, words]) =>+ `<li>${counts[kind]?.count ?? 0} ${escapeHtml(words)}${escapeHtml(+ actionNumbers(counts[kind]?.examples, counts[kind]?.count ?? 0),+ )}</li>`,+ );+ return `<p>${counts.checked ?? 0} open actions checked.</p><ul>${lines.join('')}</ul>`;+}+export function renderHealthEmail(report) {const issues = findIssues(report);const date = report.generatedAt.slice(0, 10);@@ -359,6 +411,8 @@ export function renderHealthEmail(report) {<p>Runs failed by a server restart: ${n.restartKills ?? 0}</p><h3>Query fan-out (answers with observed sub-queries)</h3>${fanoutTable(report.fanout)}+ <h3>Action counts (one count per action)</h3>+ ${actionCountsBlock(report.actionCounts)}<h3>Server</h3><p>Uptime: ${report.serverUptimeHours}h. Cloro tasks pending over an hour: ${report.cloro.pendingOverAnHour ?? 'unknown'diff --git a/server/src/lib/health/report.test.js b/server/src/lib/health/report.test.jsindex a3d083d9e..27d2ba3c5 100644--- a/server/src/lib/health/report.test.js+++ b/server/src/lib/health/report.test.js@@ -184,6 +184,9 @@ describe('collectHealthReport', () => {const report = await collectHealthReport(NOW);expect(report.probes.results[0]).toMatchObject({ status: 'red', error: 'statement timeout' });expect(report.nightly.counts).toEqual({});+ // The action count check (AC-009) asks for five examples, and says when it can't run.+ expect(db.rpc).toHaveBeenCalledWith('action_count_drift', { p_examples: 5 });+ expect(report.actionCounts).toEqual({ error: 'statement timeout' });});});@@ -309,4 +312,30 @@ describe('renderHealthEmail', () => {'Nightly runs could not be checked: boom',]);});++ it('raises actions whose count and kept list disagree, naming a few (AC-009)', () => {+ const actionCounts = {+ checked: 40,+ missing: { count: 0, examples: [] },+ mismatch: { count: 7, examples: [40, 12] },+ tasks: { count: 1, examples: [7] },+ };+ const { issues, html } = renderHealthEmail({ ...clean, actionCounts });+ expect(issues).toEqual([+ '7 open action(s) kept a list of a different size from their count (e.g. AC-40, AC-12)',+ "1 open action(s) have a task that names a different count from the action's (AC-7)",+ ]);+ expect(html).toContain('40 open actions checked.');+ // All zero: nothing to raise.+ const zero = {+ checked: 40,+ missing: { count: 0 },+ mismatch: { count: 0 },+ tasks: { count: 0 },+ };+ expect(findIssues({ ...clean, actionCounts: zero })).toEqual([]);+ expect(findIssues({ ...clean, actionCounts: { error: 'missing function' } })).toEqual([+ 'Action counts could not be checked: missing function',+ ]);+ });});diff --git a/supabase/migrations/00245_action_count_drift.sql b/supabase/migrations/00245_action_count_drift.sqlnew file mode 100644index 000000000..2bd989c48--- /dev/null+++ b/supabase/migrations/00245_action_count_drift.sql@@ -0,0 +1,104 @@+-- AC-009, part 3: the nightly check that every action's count and its list agree.+--+-- An action keeps its list once, when it is created (action_entities, 00244),+-- and everything it shows counts from it. The daily health e-mail now says+-- how many open actions disagree with their own list, and names a few:+--+-- * missing: an action about something (a targetCount) made since lists+-- are kept that kept none;+-- * mismatch: one whose list holds another number than the count it saved.+-- A new action keeps up to 1000 (MAX_TARGET_IDS): a list cut there is+-- the cap, not a mismatch;+-- * tasks: one with a planned task naming another count of the same+-- thing than the action saved ("across 13 prompts" under 12).+--+-- The last two judge only lists kept as their action was created+-- (action_entities.kept_at): an action from before 00244 got the list its+-- payload still named, and an older count may never have matched it.+-- Each should be 0; the server's health report raises any that isn't.++create or replace function public.action_count_drift(p_examples integer default 5)+returns jsonb+language sql+stable+security definer+set search_path = public+as $$+ with open_actions as (+ select a.id, a.action_no, a.created_at, a.payload->>'targetEntity' as entity, x.target_count+ from public.actions a+ -- A number only: anything else in the payload is no count to check.+ cross join lateral (+ select case when jsonb_typeof(a.payload->'targetCount') = 'number'+ then (a.payload #>> '{targetCount}')::numeric end as target_count+ ) x+ where a.deleted_at is null+ and a.status not in ('completed', 'dismissed')+ and x.target_count > 0+ and coalesce(a.payload->>'targetEntity', '') <> ''+ ),+ -- Lists started being kept here: actions before had theirs written by+ -- 00244's backfill, from what their payload still named, if anything.+ since as (+ select coalesce(min(kept_at), '-infinity'::timestamptz) as at from public.action_entities+ ),+ kept as (+ select o.id, o.action_no, o.created_at, o.target_count, l.total, l.at_creation+ from open_actions o+ -- Each action's own rows, through the primary key.+ cross join lateral (+ select count(*) as total,+ -- Kept as it was created (the trigger), not by the backfill.+ coalesce(bool_or(e.kept_at <= o.created_at + interval '1 minute'), false) as at_creation+ from public.action_entities e+ where e.action_id = o.id and e.entity_type = o.entity+ ) l+ ),+ missing as (+ select k.action_no from kept k, since+ where k.total = 0 and k.created_at >= since.at+ ),+ mismatch as (+ select action_no from kept+ where at_creation+ and total > 0+ and total <> target_count+ -- The cap a list was cut at, not a mismatch.+ and not (total = 1000 and target_count > total)+ ),+ tasks as (+ select distinct k.action_no+ from kept k+ join open_actions o on o.id = k.id+ join public.action_tasks t on t.action_id = o.id+ cross join lateral (+ select case when jsonb_typeof(t.title_params->'count') = 'number'+ then (t.title_params #>> '{count}')::numeric end as task_count+ ) c+ where k.at_creation+ and t.title_params->>'entity' = o.entity+ and c.task_count <> o.target_count+ )+ -- The newest first: a regression shows before the long-open ones.+ select jsonb_build_object(+ 'checked', (select count(*) from open_actions),+ 'missing', jsonb_build_object(+ 'count', (select count(*) from missing),+ 'examples', coalesce((select jsonb_agg(action_no order by action_no desc)+ from (select action_no from missing order by action_no desc limit p_examples) x), '[]'::jsonb)),+ 'mismatch', jsonb_build_object(+ 'count', (select count(*) from mismatch),+ 'examples', coalesce((select jsonb_agg(action_no order by action_no desc)+ from (select action_no from mismatch order by action_no desc limit p_examples) x), '[]'::jsonb)),+ 'tasks', jsonb_build_object(+ 'count', (select count(*) from tasks),+ 'examples', coalesce((select jsonb_agg(action_no order by action_no desc)+ from (select action_no from tasks order by action_no desc limit p_examples) x), '[]'::jsonb))+ );+$$;++comment on function public.action_count_drift(integer) is+ 'AC-009: open actions whose saved count and kept list (action_entities) disagree, by kind (missing list, list ≠ count, a task naming another count), with a few action numbers each; read by the daily health report. Each should be 0.';++revoke all on function public.action_count_drift(integer) from public, anon, authenticated;+grant execute on function public.action_count_drift(integer) to service_role;diff --git a/supabase/schema.sql b/supabase/schema.sqlindex e9c5ffd86..d3608206a 100644--- a/supabase/schema.sql+++ b/supabase/schema.sql@@ -36832,3 +36832,111 @@ select public.snapshot_action_entities(from public.actions awhere a.deleted_at is null;+-- ─────────────────────────────────────────────────────────────────────────+-- migrations/00245_action_count_drift.sql+-- ─────────────────────────────────────────────────────────────────────────+-- AC-009, part 3: the nightly check that every action's count and its list agree.+--+-- An action keeps its list once, when it is created (action_entities, 00244),+-- and everything it shows counts from it. The daily health e-mail now says+-- how many open actions disagree with their own list, and names a few:+--+-- * missing: an action about something (a targetCount) made since lists+-- are kept that kept none;+-- * mismatch: one whose list holds another number than the count it saved.+-- A new action keeps up to 1000 (MAX_TARGET_IDS): a list cut there is+-- the cap, not a mismatch;+-- * tasks: one with a planned task naming another count of the same+-- thing than the action saved ("across 13 prompts" under 12).+--+-- The last two judge only lists kept as their action was created+-- (action_entities.kept_at): an action from before 00244 got the list its+-- payload still named, and an older count may never have matched it.+-- Each should be 0; the server's health report raises any that isn't.++create or replace function public.action_count_drift(p_examples integer default 5)+returns jsonb+language sql+stable+security definer+set search_path = public+as $$+ with open_actions as (+ select a.id, a.action_no, a.created_at, a.payload->>'targetEntity' as entity, x.target_count+ from public.actions a+ -- A number only: anything else in the payload is no count to check.+ cross join lateral (+ select case when jsonb_typeof(a.payload->'targetCount') = 'number'+ then (a.payload #>> '{targetCount}')::numeric end as target_count+ ) x+ where a.deleted_at is null+ and a.status not in ('completed', 'dismissed')+ and x.target_count > 0+ and coalesce(a.payload->>'targetEntity', '') <> ''+ ),+ -- Lists started being kept here: actions before had theirs written by+ -- 00244's backfill, from what their payload still named, if anything.+ since as (+ select coalesce(min(kept_at), '-infinity'::timestamptz) as at from public.action_entities+ ),+ kept as (+ select o.id, o.action_no, o.created_at, o.target_count, l.total, l.at_creation+ from open_actions o+ -- Each action's own rows, through the primary key.+ cross join lateral (+ select count(*) as total,+ -- Kept as it was created (the trigger), not by the backfill.+ coalesce(bool_or(e.kept_at <= o.created_at + interval '1 minute'), false) as at_creation+ from public.action_entities e+ where e.action_id = o.id and e.entity_type = o.entity+ ) l+ ),+ missing as (+ select k.action_no from kept k, since+ where k.total = 0 and k.created_at >= since.at+ ),+ mismatch as (+ select action_no from kept+ where at_creation+ and total > 0+ and total <> target_count+ -- The cap a list was cut at, not a mismatch.+ and not (total = 1000 and target_count > total)+ ),+ tasks as (+ select distinct k.action_no+ from kept k+ join open_actions o on o.id = k.id+ join public.action_tasks t on t.action_id = o.id+ cross join lateral (+ select case when jsonb_typeof(t.title_params->'count') = 'number'+ then (t.title_params #>> '{count}')::numeric end as task_count+ ) c+ where k.at_creation+ and t.title_params->>'entity' = o.entity+ and c.task_count <> o.target_count+ )+ -- The newest first: a regression shows before the long-open ones.+ select jsonb_build_object(+ 'checked', (select count(*) from open_actions),+ 'missing', jsonb_build_object(+ 'count', (select count(*) from missing),+ 'examples', coalesce((select jsonb_agg(action_no order by action_no desc)+ from (select action_no from missing order by action_no desc limit p_examples) x), '[]'::jsonb)),+ 'mismatch', jsonb_build_object(+ 'count', (select count(*) from mismatch),+ 'examples', coalesce((select jsonb_agg(action_no order by action_no desc)+ from (select action_no from mismatch order by action_no desc limit p_examples) x), '[]'::jsonb)),+ 'tasks', jsonb_build_object(+ 'count', (select count(*) from tasks),+ 'examples', coalesce((select jsonb_agg(action_no order by action_no desc)+ from (select action_no from tasks order by action_no desc limit p_examples) x), '[]'::jsonb))+ );+$$;++comment on function public.action_count_drift(integer) is+ 'AC-009: open actions whose saved count and kept list (action_entities) disagree, by kind (missing list, list ≠ count, a task naming another count), with a few action numbers each; read by the daily health report. Each should be 0.';++revoke all on function public.action_count_drift(integer) from public, anon, authenticated;+grant execute on function public.action_count_drift(integer) to service_role;+