Skip to content

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.

feat(action-center): turn on automatic execution, with a trail people can read (#835)

Gökhan Geyikddfed8bd5d94
diff --git a/server/src/config/action-engine.js b/server/src/config/action-engine.js
index 470857e78..af479391e 100644
--- a/server/src/config/action-engine.js
+++ b/server/src/config/action-engine.js
@@ -153,6 +153,22 @@ export const ENGINE_THRESHOLDS = Object.freeze({
*/
maxNewActionsPerDay: 5,
+ /**
+ * How many tasks the engine may carry out for itself in one nightly sweep,
+ * across every brand.
+ *
+ * A ceiling on a night that goes wrong, not a target. Every tool reads —
+ * one from our own rollups, one from the brand's analytics property — so
+ * the cost of overshooting is API calls and time rather than damage, but a
+ * detector misfiring across the base should not turn into a thousand
+ * requests to a third party before anyone is awake to see it.
+ *
+ * Sized from what exists: seven brands have analytics connected and an
+ * action carries at most two tool-backed tasks, so a normal night is well
+ * under this. Reaching it is a signal in itself, and the sweep says so.
+ */
+ maxTaskRunsPerNight: 50,
+
/**
* Platform-wide result volume collapsing to this fraction of its trailing
* daily average is a collection incident on our side, not a visibility
diff --git a/server/src/lib/action-center/execution/run.js b/server/src/lib/action-center/execution/run.js
index 55db5ba5e..c5d634a60 100644
--- a/server/src/lib/action-center/execution/run.js
+++ b/server/src/lib/action-center/execution/run.js
@@ -16,15 +16,18 @@
* asking for one before every read teaches people to approve blindly.
* 3. **Every attempt is recorded** — inputs, tool, result, error. Never the
* model's reasoning: that is not evidence, and storing it invites people
- * to read it as explanation.
+ * to read it as explanation. Recorded twice, for two readers: the audit
+ * table for whoever is debugging the engine, and `action_events` for
+ * whoever owns the action, because a task that completes itself with no
+ * trail is indistinguishable from one a colleague did.
* 4. **A failed run fails its task, not its action.** A tool that throws
* costs one step. The action stays open, the other tasks stay as they
* were, and somebody can do that step by hand.
*
- * Deliberately not wired to the nightly cron. The engine underneath it spent
- * six days failing silently before anyone noticed (#829); an execution layer
- * that runs unattended belongs after the verification that the thing it
- * executes for is running at all, not before.
+ * `sweepTaskExecution` is the nightly entry point. It is bounded by a ceiling
+ * on attempts per night, because both tools read and the cost of a bad night
+ * is third-party requests rather than damage — but a detector misfiring across
+ * the base should not become a thousand of them before anyone is awake.
*/
import supabaseAdmin from '../../../config/supabase.js';
@@ -32,6 +35,9 @@ import { logger } from '../../logger.js';
import { getTask } from '../tasks/registry.js';
import { resolveBrandSources } from '../sources.js';
import { getTool } from './tools.js';
+import { resolve } from '../../../config/action-engine.js';
+
+const { noise } = resolve();
/** A task can only be carried out from the statuses that mean "not done". */
const RUNNABLE_STATUSES = ['todo', 'in_progress', 'waiting_approval'];
@@ -41,6 +47,32 @@ async function record(row) {
if (error) throw new Error(error.message);
}
+/**
+ * The trail the user sees.
+ *
+ * `action_task_runs` is the audit table and has no interface; the drawer's
+ * History tab reads `action_events`. Without an entry here, a task carried out
+ * automatically looked exactly like one a colleague had done — marked
+ * complete, with no trail and nobody to ask. That is worse than not
+ * automating it.
+ *
+ * `actor_id` stays null, which is already how the nightly engine's own events
+ * are recorded: null means the product did it, not a person.
+ *
+ * Only outcomes a user needs: it ran, it failed, it needs their approval. A
+ * task blocked because no tool implements it yet, or because it waits on
+ * another task, is operational detail — it belongs in the audit table and
+ * would be noise in a trail people read to understand their own work.
+ */
+async function logEvent(actionId, event, data) {
+ const { error } = await supabaseAdmin
+ .from('action_events')
+ .insert({ action_id: actionId, event, data, actor_id: null });
+ // The trail is not worth failing a completed run over: the audit row is
+ // already written, and the task's own status is the fact that matters.
+ if (error) logger.error({ err: error, actionId, event }, '[task-run] could not log the event');
+}
+
/**
* Why this task cannot be carried out right now, or null.
*
@@ -147,6 +179,10 @@ export async function runTask(taskId, { now = new Date(), sources } = {}) {
.from('action_tasks')
.update({ status: 'waiting_approval', updated_at: now.toISOString() })
.eq('id', task.id);
+ await logEvent(action.id, 'task_awaiting_approval', {
+ task: task.task_key,
+ tool: tool.id,
+ });
}
logger.info({ taskId, reason: refusal }, '[task-run] blocked');
return { status: 'blocked', reason: refusal };
@@ -177,6 +213,7 @@ export async function runTask(taskId, { now = new Date(), sources } = {}) {
.eq('id', task.id);
if (error) throw new Error(error.message);
+ await logEvent(action.id, 'task_ran', { task: task.task_key, tool: tool.id });
logger.info({ taskId, tool: tool.id }, '[task-run] completed');
return { status: 'succeeded', output };
} catch (err) {
@@ -200,7 +237,68 @@ export async function runTask(taskId, { now = new Date(), sources } = {}) {
.update({ status: 'failed', updated_at: new Date().toISOString() })
.eq('id', task.id);
+ await logEvent(action.id, 'task_run_failed', { task: task.task_key, tool: tool.id });
logger.error({ err, taskId, tool: tool.id }, '[task-run] failed');
return { status: 'failed', error: err.message };
}
}
+
+/**
+ * Carry out the tasks that can be carried out, once, across every brand.
+ *
+ * Only tasks on actions that are still open, only those a tool implements, and
+ * only as many as the nightly ceiling allows. Ordered oldest first so a brand
+ * with a long-standing action is not permanently behind one whose actions
+ * churn.
+ *
+ * Every refusal is already recorded by `runTask`, and one brand's failure
+ * cannot stop the sweep: the whole point of the layer is that a step failing
+ * costs a step.
+ */
+export async function sweepTaskExecution({ now = new Date() } = {}) {
+ const { maxTaskRunsPerNight } = noise;
+
+ // Candidate tasks: not finished, on an action nobody has closed. The tool
+ // and dependency checks belong to `runTask`, which records what it refused.
+ const { data: rows, error } = await supabaseAdmin
+ .from('action_tasks')
+ .select('id, task_key, action_id, actions!inner(status)')
+ .in('status', RUNNABLE_STATUSES)
+ .not('actions.status', 'in', '("completed","dismissed")')
+ .order('created_at', { ascending: true })
+ .limit(1000);
+ if (error) throw new Error(error.message);
+
+ // Filter here rather than in the query: which tasks a tool implements is
+ // code, not a column, and duplicating it into SQL is how the two drift.
+ const runnable = (rows ?? []).filter((row) => getTask(row.task_key)?.tool);
+ const attempts = runnable.slice(0, maxTaskRunsPerNight);
+
+ const summary = {
+ succeeded: 0,
+ failed: 0,
+ blocked: 0,
+ skippedForCeiling: runnable.length - attempts.length,
+ };
+
+ for (const row of attempts) {
+ try {
+ const result = await runTask(row.id, { now });
+ summary[result.status === 'succeeded' ? 'succeeded' : result.status] += 1;
+ } catch (err) {
+ // An unexpected throw — a task row that vanished mid-sweep, a database
+ // error. One task's problem, not the sweep's.
+ logger.error({ err, taskId: row.id }, '[task-run] sweep could not attempt a task');
+ summary.failed += 1;
+ }
+ }
+
+ if (summary.skippedForCeiling > 0) {
+ logger.warn(
+ { ...summary, ceiling: maxTaskRunsPerNight },
+ '[task-run] sweep hit its nightly ceiling — more tasks were runnable than allowed',
+ );
+ }
+ logger.info(summary, '[task-run] sweep completed');
+ return summary;
+}
diff --git a/server/src/lib/action-center/execution/run.test.js b/server/src/lib/action-center/execution/run.test.js
index 8743aecfb..8ee563deb 100644
--- a/server/src/lib/action-center/execution/run.test.js
+++ b/server/src/lib/action-center/execution/run.test.js
@@ -10,7 +10,10 @@ vi.mock('../../logger.js', () => ({
}));
vi.mock('../../composio.js', () => ({ runGaReport: vi.fn() }));
-import { blockedReason, runTask } from './run.js';
+import { blockedReason, runTask, sweepTaskExecution } from './run.js';
+import { ENGINE_THRESHOLDS } from '../../../config/action-engine.js';
+
+const { maxTaskRunsPerNight } = ENGINE_THRESHOLDS.noise;
import { TOOLS } from './tools.js';
const TASK = 'task-1';
@@ -23,7 +26,7 @@ const ALL = new Set(['tracking', 'competitors', 'site_audits', 'analytics']);
* Enough PostgREST to drive one run. Tables answer from fixtures; every write
* is recorded so a test can assert what the attempt actually left behind.
*/
-function mockDb({ task, action, siblings = [] }) {
+function mockDb({ task, action, siblings = [], eventsFail = false }) {
const writes = { inserted: [], updated: [] };
from.mockImplementation((table) => {
const builder = {
@@ -34,7 +37,9 @@ function mockDb({ task, action, siblings = [] }) {
single: () => Promise.resolve({ data: table === 'actions' ? action : task, error: null }),
insert: (row) => {
writes.inserted.push({ table, row });
- return Promise.resolve({ error: null });
+ return Promise.resolve({
+ error: eventsFail && table === 'action_events' ? { message: 'insert denied' } : null,
+ });
},
update: (patch) => {
writes.updated.push({ table, patch });
@@ -168,6 +173,73 @@ describe('runTask', () => {
});
});
+ /**
+ * Without this the layer is worse than not automating: a task marked
+ * complete that nobody remembers doing, with no trail and nobody to ask.
+ * The audit table has no interface — the drawer's History tab reads
+ * `action_events`.
+ */
+ describe('the trail the user sees', () => {
+ const events = (writes) => writes.inserted.filter((w) => w.table === 'action_events');
+
+ it('says the task was carried out, and with what', async () => {
+ const writes = mockDb({ task: task(), action });
+
+ await runTask(TASK, { now: NOW, sources: ALL });
+
+ expect(events(writes)).toHaveLength(1);
+ expect(events(writes)[0].row).toMatchObject({
+ action_id: ACTION,
+ event: 'task_ran',
+ data: { task: 'validate', tool: 'visibility_window' },
+ });
+ });
+
+ /** Null actor is already how the nightly engine records its own events:
+ * it means the product did this, not a person. */
+ it('records no person as the actor', async () => {
+ const writes = mockDb({ task: task(), action });
+
+ await runTask(TASK, { now: NOW, sources: ALL });
+
+ expect(events(writes)[0].row.actor_id).toBeNull();
+ });
+
+ it('says so when the attempt failed', async () => {
+ rpc.mockRejectedValue(new Error('statement timeout'));
+ const writes = mockDb({ task: task(), action });
+
+ await runTask(TASK, { now: NOW, sources: ALL });
+
+ expect(events(writes)[0].row).toMatchObject({ event: 'task_run_failed' });
+ });
+
+ /**
+ * A task with no tool yet, or one waiting on another task, is operational
+ * detail. It is in the audit table; putting it in a trail people read to
+ * understand their own work would be noise.
+ */
+ it('stays out of the trail when the reason is ours, not theirs', async () => {
+ const writes = mockDb({ task: task({ task_key: 'coverage_gaps' }), action });
+
+ const result = await runTask(TASK, { now: NOW, sources: ALL });
+
+ expect(result.status).toBe('blocked');
+ expect(events(writes)).toHaveLength(0);
+ expect(writes.inserted.some((w) => w.table === 'action_task_runs')).toBe(true);
+ });
+
+ /** A completed run must not be undone by a trail that failed to write. */
+ it('does not fail the run when the trail cannot be written', async () => {
+ const writes = mockDb({ task: task(), action, eventsFail: true });
+
+ const result = await runTask(TASK, { now: NOW, sources: ALL });
+
+ expect(result.status).toBe('succeeded');
+ expect(writes.updated.find((w) => w.table === 'action_tasks').patch.status).toBe('completed');
+ });
+ });
+
it('never stores the model’s reasoning, because nothing may put it there', async () => {
const writes = mockDb({ task: task(), action });
@@ -274,3 +346,83 @@ describe('blockedReason', () => {
).toMatch(/is completed/);
});
});
+
+/**
+ * The nightly sweep (#818 phase 7). What makes this safe to leave running is
+ * not that nothing fails — it is that a failure costs one step, and that the
+ * night has a ceiling.
+ */
+describe('sweepTaskExecution', () => {
+ /** Enough of the builder for the sweep's one query plus whatever runTask
+ * does per task, which is already covered above. */
+ function mockSweep(tasks) {
+ const attempted = [];
+ from.mockImplementation((table) => {
+ const builder = {
+ select: () => builder,
+ eq: () => builder,
+ in: () => builder,
+ not: () => builder,
+ order: () => builder,
+ limit: () => builder,
+ single: () =>
+ Promise.resolve({
+ data:
+ table === 'actions'
+ ? { id: ACTION, brand_id: BRAND, payload: {} }
+ : { ...task({ id: attempted.at(-1) }), task_key: 'validate' },
+ error: null,
+ }),
+ insert: () => Promise.resolve({ error: null }),
+ update: () => builder,
+ then: (resolve, reject) =>
+ Promise.resolve({ data: table === 'action_tasks' ? tasks : [], error: null }).then(
+ resolve,
+ reject,
+ ),
+ };
+ return builder;
+ });
+ return attempted;
+ }
+
+ it('does nothing when no task has a tool', async () => {
+ mockSweep([{ id: 't1', task_key: 'update_content', action_id: ACTION }]);
+
+ const summary = await sweepTaskExecution({ now: NOW });
+
+ expect(summary).toMatchObject({ succeeded: 0, failed: 0, blocked: 0 });
+ });
+
+ it('stops at the nightly ceiling and says it did', async () => {
+ const many = Array.from({ length: maxTaskRunsPerNight + 3 }, (_, i) => ({
+ id: `t${i}`,
+ task_key: 'validate',
+ action_id: ACTION,
+ }));
+ mockSweep(many);
+
+ const summary = await sweepTaskExecution({ now: NOW });
+
+ expect(summary.skippedForCeiling).toBe(3);
+ expect(summary.succeeded + summary.failed + summary.blocked).toBe(maxTaskRunsPerNight);
+ });
+
+ it('carries on past a task that throws unexpectedly', async () => {
+ mockSweep([
+ { id: 't1', task_key: 'validate', action_id: ACTION },
+ { id: 't2', task_key: 'validate', action_id: ACTION },
+ ]);
+ let calls = 0;
+ rpc.mockImplementation(() => {
+ calls += 1;
+ return calls <= 2
+ ? Promise.reject(new Error('boom'))
+ : Promise.resolve({ data: { answers: 1 }, error: null });
+ });
+
+ const summary = await sweepTaskExecution({ now: NOW });
+
+ expect(summary.succeeded + summary.failed).toBe(2);
+ });
+});
diff --git a/server/src/scripts/backfill-signals.js b/server/src/scripts/backfill-signals.js
index c80f7b174..cfed1fa47 100644
--- a/server/src/scripts/backfill-signals.js
+++ b/server/src/scripts/backfill-signals.js
@@ -1,16 +1,24 @@
/**
* One-off signal backfill (Action Center).
*
- * Runs the nightly signal recorder once for one brand or for every active
- * brand, so the Signals page has real rows before the next tracking cycle.
- * Safe to re-run: recording dedupes on (brand, dedup_key).
+ * Runs the nightly pass once for one brand or for every active brand, so the
+ * Signals page has real rows before the next tracking cycle.
+ *
+ * Goes through `runSignalPass` rather than calling the recorder and the
+ * generator itself, so a manual recovery logs itself in `signal_runs` like any
+ * other pass. Calling them directly left the ledger saying the pass had not
+ * run when it had, which made the catch-up sweep repeat work and made the one
+ * table that answers "did it run last night" unreliable — the exact question
+ * it was added to answer.
+ *
+ * Safe to re-run: recording dedupes on (brand, dedup_key) and the ledger row
+ * is keyed to the tracking run, so a repeat updates rather than duplicates.
*
* Run: node src/scripts/backfill-signals.js [brandId|--all]
*/
import 'dotenv/config';
import supabaseAdmin from '../config/supabase.js';
-import { recordSignalsForBrand } from '../lib/signals/record.js';
-import { generateActionsForBrand } from '../lib/action-center/generate.js';
+import { runSignalPass } from '../lib/signals/pass.js';
const arg = process.argv[2];
if (!arg) {
@@ -33,11 +41,7 @@ if (arg === '--all') {
for (const brandId of brandIds) {
try {
- const result = await recordSignalsForBrand(brandId);
- const actions = result.skipped
- ? { skipped: result.skipped }
- : await generateActionsForBrand(brandId);
- console.log(brandId, JSON.stringify({ signals: result, actions }));
+ console.log(brandId, JSON.stringify(await runSignalPass(brandId)));
} catch (err) {
console.error(brandId, 'FAILED:', err.message);
}
diff --git a/server/src/server.js b/server/src/server.js
index 3cabb2ae8..d28cc676a 100644
--- a/server/src/server.js
+++ b/server/src/server.js
@@ -37,6 +37,7 @@ import { runGaSync } from './lib/ga-sync.js';
import { runPageOpportunityDetection } from './lib/page-opportunities.js';
import { runPulseCatchUp } from './lib/pulse/engine.js';
import { runSignalCatchUp } from './lib/signals/pass.js';
+import { sweepTaskExecution } from './lib/action-center/execution/run.js';
const app = express();
app.set('trust proxy', 1);
@@ -134,6 +135,16 @@ async function runDailyTracking() {
logger.error({ err }, '[signals] catch-up sweep crashed');
});
+ // Carry out the tasks the engine can carry out itself (#818 phase 7). Runs
+ // after the catch-up above so tonight's actions exist before anything tries
+ // to work on them. Bounded by a nightly ceiling; every attempt is recorded
+ // in action_task_runs and, where the user needs to know, in the action's own
+ // trail. Fire-and-forget like the rest — an unexecuted task is a step
+ // somebody does by hand, not a reason to hold up tracking.
+ sweepTaskExecution().catch((err) => {
+ logger.error({ err }, '[task-run] sweep crashed');
+ });
+
// Insights rollup backstop (00066). Re-refreshes the trailing days for
// every brand, so days whose run never stamped (partial, crashed) and
// out-of-run writes (a prompt analyzed right after being added) appear the
diff --git a/web/messages/en.json b/web/messages/en.json
index d08fe0a99..caebb5550 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -485,7 +485,10 @@
"status_changed": "Status changed to {to}",
"assigned": "Assignee changed",
"due_date_set": "Due date updated",
- "task_status": "Task updated"
+ "task_status": "Task updated",
+ "task_ran": "{task} — carried out automatically ({tool})",
+ "task_run_failed": "{task} — the automatic attempt failed; it needs doing by hand",
+ "task_awaiting_approval": "{task} — waiting for your approval before it reaches outside Ansvisor"
},
"taskText": "Task text",
"renameTask": "Rename task",
diff --git a/web/src/components/action-center/action-drawer.tsx b/web/src/components/action-center/action-drawer.tsx
index 29f13552b..1a6d9fbb8 100644
--- a/web/src/components/action-center/action-drawer.tsx
+++ b/web/src/components/action-center/action-drawer.tsx
@@ -50,6 +50,7 @@ import {
actionContextTags,
actionGoal,
actionTexts,
+ eventTaskText,
memberLabel,
taskText,
} from '@/lib/action-center/display';
@@ -333,6 +334,12 @@ export function ActionDrawer({
{t(`drawer.events.${event.event}`, {
to: String(event.data.to ?? ''),
count: Number(event.data.count ?? 0),
+ // Work the product did itself names the task in the
+ // reader's language and the tool as it is recorded —
+ // the tool id is the audit fact, so it is not
+ // translated.
+ task: eventTaskText(event.data.task, tTasks),
+ tool: String(event.data.tool ?? ''),
})}
</p>
<p className="text-xs text-muted-foreground">
diff --git a/web/src/lib/action-center/display.test.ts b/web/src/lib/action-center/display.test.ts
index 0bf407a72..6bbd7810f 100644
--- a/web/src/lib/action-center/display.test.ts
+++ b/web/src/lib/action-center/display.test.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
-import { actionGoal, actionTexts, humanizeKind, taskText } from './display';
+import { actionGoal, actionTexts, eventTaskText, humanizeKind, taskText } from './display';
import { ACTION_KINDS } from './registry';
import type { ActionItem } from '@/lib/actions/action-center';
import messages from '../../../messages/en.json';
@@ -101,3 +101,28 @@ describe('taskText', () => {
expect(taskText({ taskKey: null, title: null }, t)).toBe('');
});
});
+
+/**
+ * Events carry a task's key, not its text: an event records what happened,
+ * and the text is a rendering choice that may change afterwards.
+ */
+describe('eventTaskText', () => {
+ const withHas = Object.assign((key: string) => t(key), {
+ has: (key: string) => key in messages.actionCenter.actionTasks,
+ });
+
+ it('renders the task in the reader’s language', () => {
+ expect(eventTaskText('validate', withHas)).toBe(messages.actionCenter.actionTasks.validate);
+ });
+
+ /** A task this build has no copy for still has to read as a sentence. */
+ it('humanises a key it has no copy for', () => {
+ expect(eventTaskText('seize_the_means', withHas)).toBe('seize the means');
+ });
+
+ it('renders nothing when the event named no task', () => {
+ expect(eventTaskText(undefined, withHas)).toBe('');
+ expect(eventTaskText('', withHas)).toBe('');
+ expect(eventTaskText(42, withHas)).toBe('');
+ });
+});
diff --git a/web/src/lib/action-center/display.ts b/web/src/lib/action-center/display.ts
index 07c8a24f7..e26388b4b 100644
--- a/web/src/lib/action-center/display.ts
+++ b/web/src/lib/action-center/display.ts
@@ -124,6 +124,23 @@ export function actionTexts(
}
}
+/**
+ * A task named inside an event message.
+ *
+ * Events carry the task's key, not its text, because an event is a record of
+ * what happened and the text is a rendering choice that may change. Falls back
+ * to a humanised key so an event about a task this build has no copy for still
+ * reads as a sentence.
+ */
+export function eventTaskText(
+ key: unknown,
+ t: Translator & { has?: (key: string) => boolean },
+): string {
+ if (typeof key !== 'string' || key.length === 0) return '';
+ if (t.has && !t.has(key)) return humanizeKind(key).toLowerCase();
+ return t(key);
+}
+
/**
* What the drawer shows under "Goal".
*