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(server): stop every automatic process for a workspace whose plan ended (#1608)

Cihan1591142f3c38
diff --git a/server/src/lib/action-center/task-writer/index.js b/server/src/lib/action-center/task-writer/index.js
index 741ec146c..21bc7e341 100644
--- a/server/src/lib/action-center/task-writer/index.js
+++ b/server/src/lib/action-center/task-writer/index.js
@@ -17,6 +17,8 @@
import { NoObjectGeneratedError, generateObject } from 'ai';
import supabaseAdmin from '../../../config/supabase.js';
+import { isCloud } from '../../../config/plans.js';
+import { ACTIVE_SUBSCRIPTION_STATUSES } from '../../active-subscriptions.js';
import { logger } from '../../logger.js';
import { resolveModel } from '../../ai-provider.js';
import { withRetry } from '../../retry.js';
@@ -479,14 +481,23 @@ const SWEEP_LIMIT = 20;
* Write the tasks of recent actions that have none written yet. Runs after
* a brand's signal pass and once a day for the ones it missed. An
* organization whose budget is spent is skipped for the rest of the sweep.
+ * On the cloud, only for organizations still on a plan (2026-10-27):
+ * filtered in the query, so the actions of one whose plan ended can't take
+ * the sweep's places, each one only to be refused its budget.
*
* @param {{ brandId?: string, now?: Date, limit?: number }} [options]
*/
export async function sweepActionTasks({ brandId, now = new Date(), limit = SWEEP_LIMIT } = {}) {
if (!isTaskWriterEnabled()) return { skipped: 'disabled' };
- let query = supabaseAdmin
- .from('actions')
- .select('id, brand_id, kind, status')
+ let query = supabaseAdmin.from('actions');
+ query = isCloud()
+ ? query
+ .select(
+ 'id, brand_id, kind, status, brands!inner(organizations!inner(subscription_status))',
+ )
+ .in('brands.organizations.subscription_status', ACTIVE_SUBSCRIPTION_STATUSES)
+ : query.select('id, brand_id, kind, status');
+ query = query
.is('tasks_generated_at', null)
.gte('created_at', new Date(now.getTime() - SWEEP_WINDOW_MS).toISOString())
.not('status', 'in', '("completed","dismissed")')
diff --git a/server/src/lib/action-center/task-writer/index.test.js b/server/src/lib/action-center/task-writer/index.test.js
index 781fdf2f5..2758e32d8 100644
--- a/server/src/lib/action-center/task-writer/index.test.js
+++ b/server/src/lib/action-center/task-writer/index.test.js
@@ -1,4 +1,4 @@
-import { beforeEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
/**
* The task writer end to end (AC-004), on an in-memory database and a
@@ -8,6 +8,11 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
let db;
let ops;
+// The columns each read asked for.
+let selects;
+
+/** A row's value at a column, or at an embed's (`brands.organizations.…`). */
+const valueAt = (row, col) => col.split('.').reduce((value, key) => value?.[key], row);
/** A query builder over `db`, recording every write. */
function table(name) {
@@ -28,8 +33,8 @@ function table(name) {
col === 'action_signals.action_id'
? true
: Array.isArray(value)
- ? value.includes(row[col])
- : row[col] === value,
+ ? value.includes(valueAt(row, col))
+ : valueAt(row, col) === value,
),
);
const run = () => {
@@ -55,7 +60,11 @@ function table(name) {
return { data: null, error: null };
};
const chain = {
- select: () => ((q.returning = q.op !== 'select'), chain),
+ select: (columns) => (
+ q.op === 'select' && selects.push({ table: name, columns }),
+ (q.returning = q.op !== 'select'),
+ chain
+ ),
// Only the claim uses or(): a free action, or one whose writer died.
or: () => ((q.free = true), chain),
eq: (col, value) => (q.filters.push([col, value]), chain),
@@ -121,6 +130,7 @@ const answer = (tasks) => ({ object: { tasks }, usage: { inputTokens: 1000, outp
beforeEach(() => {
ops = [];
+ selects = [];
generateObject.mockReset();
charges.length = 0;
release.mockReset();
@@ -442,4 +452,50 @@ describe('sweepActionTasks', () => {
expect(outcome).toEqual({ written: 0, skipped: 0, failed: 1 });
expect(reserveAiBudget).toHaveBeenCalledTimes(1);
});
+
+ /**
+ * Once the plan has ended (2026-10-27), on the cloud: its actions are left
+ * out by the query itself, so they never reach the model or the budget.
+ */
+ describe('on the cloud', () => {
+ beforeEach(() => vi.stubEnv('IS_CLOUD', 'true'));
+ afterEach(() => vi.unstubAllEnvs());
+
+ const onPlan = (status) => ({ organizations: { subscription_status: status } });
+
+ it("writes no task for an organization whose plan has ended, and the paying one's still", async () => {
+ db.actions = [
+ { ...db.actions[0], id: 'lapsed', brand_id: 'b-lapsed', brands: onPlan('canceled') },
+ { ...db.actions[0], id: 'paying', brand_id: 'b1', brands: onPlan('active') },
+ ];
+ db.action_tasks = [];
+ generateObject.mockResolvedValue(answer([task()]));
+
+ const outcome = await sweepActionTasks();
+
+ expect(outcome).toEqual({ written: 1, skipped: 0, failed: 0 });
+ expect(selects).toContainEqual({
+ table: 'actions',
+ columns:
+ 'id, brand_id, kind, status, brands!inner(organizations!inner(subscription_status))',
+ });
+ expect(reserveAiBudget).toHaveBeenCalledTimes(1);
+ expect(ops.filter((op) => op.table === 'actions' && op.op === 'update')).not.toContainEqual(
+ expect.objectContaining({ filters: expect.arrayContaining([['id', 'lapsed']]) }),
+ );
+ });
+ });
+
+ describe('self-hosted', () => {
+ beforeEach(() => vi.stubEnv('IS_CLOUD', 'false'));
+ afterEach(() => vi.unstubAllEnvs());
+
+ it('reads every action, unfiltered', async () => {
+ db.action_tasks = [];
+ generateObject.mockResolvedValue(answer([task()]));
+
+ expect(await sweepActionTasks()).toEqual({ written: 1, skipped: 0, failed: 0 });
+ expect(selects).toContainEqual({ table: 'actions', columns: 'id, brand_id, kind, status' });
+ });
+ });
});
diff --git a/server/src/lib/active-subscriptions.js b/server/src/lib/active-subscriptions.js
index 1c754778e..813b376b2 100644
--- a/server/src/lib/active-subscriptions.js
+++ b/server/src/lib/active-subscriptions.js
@@ -5,9 +5,11 @@
* cancelled is read-only: its people can look around, and the nightly
* database-only upkeep (rollups, counters, retention) still runs for it, but
* nothing that costs: no tracking, no AI, no agent, no scraping, no sync
- * with an outside service. Those sweeps, and the job recovery, keep to the
- * ids these let through, as webflow-status.js and brand-facts-sweep.js
- * already did.
+ * with an outside service, no new signal, action or suggestion. Those
+ * sweeps, the jobs and the job recovery keep to the ids these let through,
+ * as webflow-status.js and brand-facts-sweep.js already did; what outside
+ * sites and services send for it (AI-bot logs, traffic beacons, Cloro's
+ * answers, workflow callbacks) is answered as usual and not stored.
*
* Self-hosted, everything is let through (billing doesn't apply), and
* nothing is read.
@@ -19,7 +21,9 @@ import { selectInChunks } from './chunked-in.js';
/**
* The subscription statuses isSubscriptionActive takes as active, for a
- * filter in a query (`.in(...)`); kept equal to it by a test.
+ * filter in a query (`.in(...)`); kept equal to it by a test. past_due is
+ * not one: a failed payment locks the workspace at once (product decision,
+ * 2026-10-27), with no grace while Stripe retries.
*/
export const ACTIVE_SUBSCRIPTION_STATUSES = ['active', 'trialing'];
@@ -87,3 +91,91 @@ export async function activeBrandFilter(brandIds) {
export async function isOrgActive(orgId) {
return (await activeOrgFilter([orgId]))(orgId);
}
+
+/**
+ * Whether one brand's organization has an active or trialing subscription
+ * (always, self-hosted): for a job, a pass or a callback that starts work
+ * for one brand.
+ *
+ * @param {string} brandId
+ * @returns {Promise<boolean>}
+ */
+export async function isBrandActive(brandId) {
+ return (await activeBrandFilter([brandId]))(brandId);
+}
+
+/**
+ * How long an answer is kept for what outside sites and services send us
+ * (the AI-bot ingest, the traffic beacon, Cloro's answers): one read per
+ * organization or brand every few minutes, not one per event. A plan that
+ * ends now stops storing within these minutes.
+ */
+export const CACHED_ANSWER_MS = 5 * 60_000;
+/**
+ * An inactive answer is kept a minute at most: a plan bought now starts
+ * storing within it.
+ */
+export const CACHED_INACTIVE_MS = 60_000;
+/** Answers kept at most; the oldest goes first. */
+export const MAX_CACHED_ANSWERS = 10_000;
+
+/** `org:<id>` or `brand:<id>` → { until, active: Promise<boolean> } */
+const answers = new Map();
+
+/**
+ * The kept answer for `key`, or a new read kept for CACHED_ANSWER_MS
+ * (CACHED_INACTIVE_MS once it answers inactive). The read itself is kept,
+ * so a burst of events waits on one read; a read that fails is dropped, and
+ * the next event asks again.
+ */
+function keptAnswer(key, read, now) {
+ const kept = answers.get(key);
+ if (kept && kept.until > now) return kept.active;
+ answers.delete(key);
+ if (answers.size >= MAX_CACHED_ANSWERS) answers.delete(answers.keys().next().value);
+ const active = read();
+ const entry = { until: now + CACHED_ANSWER_MS, active };
+ answers.set(key, entry);
+ active.then(
+ (isActive) => {
+ if (!isActive) entry.until = Math.min(entry.until, now + CACHED_INACTIVE_MS);
+ },
+ () => {
+ if (answers.get(key) === entry) answers.delete(key);
+ },
+ );
+ return active;
+}
+
+/**
+ * isOrgActive, answered from memory for CACHED_ANSWER_MS (an inactive
+ * answer, CACHED_INACTIVE_MS). Self-hosted, always true, without a read.
+ *
+ * @param {string} orgId
+ * @param {{ now?: number }} [options]
+ * @returns {Promise<boolean>}
+ */
+export async function isOrgActiveCached(orgId, { now = Date.now() } = {}) {
+ if (!isCloud()) return true;
+ if (!orgId) return false;
+ return keptAnswer(`org:${orgId}`, () => isOrgActive(orgId), now);
+}
+
+/**
+ * isBrandActive, answered from memory for CACHED_ANSWER_MS (an inactive
+ * answer, CACHED_INACTIVE_MS). Self-hosted, always true, without a read.
+ *
+ * @param {string} brandId
+ * @param {{ now?: number }} [options]
+ * @returns {Promise<boolean>}
+ */
+export async function isBrandActiveCached(brandId, { now = Date.now() } = {}) {
+ if (!isCloud()) return true;
+ if (!brandId) return false;
+ return keptAnswer(`brand:${brandId}`, () => isBrandActive(brandId), now);
+}
+
+// Test hook — what is answered depends on what is kept.
+export function clearCachedAnswers() {
+ answers.clear();
+}
diff --git a/server/src/lib/active-subscriptions.test.js b/server/src/lib/active-subscriptions.test.js
index c6a062692..c6e5b3af0 100644
--- a/server/src/lib/active-subscriptions.test.js
+++ b/server/src/lib/active-subscriptions.test.js
@@ -40,13 +40,25 @@ vi.mock('../config/supabase.js', () => ({
const ids = (read) => read.in?.[1] ?? [];
-const { ACTIVE_SUBSCRIPTION_STATUSES, activeBrandFilter, activeOrgFilter, isOrgActive } =
- await import('./active-subscriptions.js');
+const {
+ ACTIVE_SUBSCRIPTION_STATUSES,
+ CACHED_ANSWER_MS,
+ CACHED_INACTIVE_MS,
+ MAX_CACHED_ANSWERS,
+ activeBrandFilter,
+ activeOrgFilter,
+ clearCachedAnswers,
+ isBrandActive,
+ isBrandActiveCached,
+ isOrgActive,
+ isOrgActiveCached,
+} = await import('./active-subscriptions.js');
const { isSubscriptionActive } = await import('../config/plans.js');
beforeEach(() => {
reads.length = 0;
failWith = null;
+ clearCachedAnswers();
rows = {
organizations: [
{ id: 'trialing', subscription_status: 'trialing' },
@@ -150,6 +162,104 @@ describe('isOrgActive', () => {
});
});
+describe('isBrandActive', () => {
+ it("tells one brand by its organization's subscription, self-hosted always", async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ expect(await isBrandActive('b-trialing')).toBe(true);
+ expect(await isBrandActive('b-canceled')).toBe(false);
+ expect(await isBrandActive('b-orphan')).toBe(false);
+ vi.stubEnv('IS_CLOUD', 'false');
+ reads.length = 0;
+ expect(await isBrandActive('b-canceled')).toBe(true);
+ expect(reads).toHaveLength(0);
+ });
+});
+
+/**
+ * What outside sites send us (2026-10-27): one read per organization or
+ * brand every CACHED_ANSWER_MS, not one per event.
+ */
+describe('isOrgActiveCached and isBrandActiveCached', () => {
+ const T0 = 1_800_000_000_000;
+
+ it('keeps an active answer for minutes, without a read, until they are up', async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ expect(await isOrgActiveCached('active', { now: T0 })).toBe(true);
+ expect(reads).toHaveLength(1);
+
+ // The plan ends meanwhile: still the kept answer, without a read.
+ rows.organizations.find((org) => org.id === 'active').subscription_status = 'canceled';
+ expect(await isOrgActiveCached('active', { now: T0 + CACHED_ANSWER_MS - 1 })).toBe(true);
+ expect(reads).toHaveLength(1);
+
+ // Once they are up, read again.
+ expect(await isOrgActiveCached('active', { now: T0 + CACHED_ANSWER_MS })).toBe(false);
+ expect(reads).toHaveLength(2);
+ });
+
+ it('keeps an inactive answer a minute at most, so a plan bought now counts soon', async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ expect(CACHED_INACTIVE_MS).toBeLessThanOrEqual(60_000);
+ expect(await isOrgActiveCached('canceled', { now: T0 })).toBe(false);
+ expect(await isBrandActiveCached('b-canceled', { now: T0 })).toBe(false);
+
+ rows.organizations.find((org) => org.id === 'canceled').subscription_status = 'active';
+ rows.brands.find((brand) => brand.id === 'b-canceled').organizations.subscription_status =
+ 'trialing';
+ expect(await isOrgActiveCached('canceled', { now: T0 + CACHED_INACTIVE_MS - 1 })).toBe(false);
+ expect(reads).toHaveLength(2);
+ expect(await isOrgActiveCached('canceled', { now: T0 + CACHED_INACTIVE_MS })).toBe(true);
+ expect(await isBrandActiveCached('b-canceled', { now: T0 + CACHED_INACTIVE_MS })).toBe(true);
+ expect(reads).toHaveLength(4);
+ });
+
+ it('keeps organizations and brands apart, and a burst waits on one read', async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ const answers = await Promise.all([
+ isBrandActiveCached('b-active', { now: T0 }),
+ isBrandActiveCached('b-active', { now: T0 }),
+ isBrandActiveCached('b-canceled', { now: T0 }),
+ isOrgActiveCached('active', { now: T0 }),
+ ]);
+ expect(answers).toEqual([true, true, false, true]);
+ expect(reads.map((read) => [read.table, read.in[1]])).toEqual([
+ ['brands', ['b-active']],
+ ['brands', ['b-canceled']],
+ ['organizations', ['active']],
+ ]);
+ });
+
+ it("doesn't keep a read that failed: the next event asks again", async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ failWith = 'statement timeout';
+ await expect(isOrgActiveCached('active', { now: T0 })).rejects.toThrow('statement timeout');
+ failWith = null;
+ expect(await isOrgActiveCached('active', { now: T0 + 1 })).toBe(true);
+ expect(reads).toHaveLength(2);
+ });
+
+ it('forgets the oldest answer first once full', async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ for (let i = 0; i < MAX_CACHED_ANSWERS; i++) await isOrgActiveCached(`org-${i}`, { now: T0 });
+ await isOrgActiveCached('one-more', { now: T0 });
+ reads.length = 0;
+ await isOrgActiveCached(`org-${MAX_CACHED_ANSWERS - 1}`, { now: T0 });
+ expect(reads).toHaveLength(0);
+ await isOrgActiveCached('org-0', { now: T0 });
+ expect(reads).toHaveLength(1);
+ });
+
+ it('lets nothing through without an id, and everything self-hosted, without a read', async () => {
+ vi.stubEnv('IS_CLOUD', 'true');
+ expect(await isOrgActiveCached(null, { now: T0 })).toBe(false);
+ expect(await isBrandActiveCached(undefined, { now: T0 })).toBe(false);
+ vi.stubEnv('IS_CLOUD', 'false');
+ expect(await isOrgActiveCached('canceled', { now: T0 })).toBe(true);
+ expect(await isBrandActiveCached('b-canceled', { now: T0 })).toBe(true);
+ expect(reads).toHaveLength(0);
+ });
+});
+
describe('ACTIVE_SUBSCRIPTION_STATUSES', () => {
it('is what isSubscriptionActive takes as active, for a filter in a query', () => {
vi.stubEnv('IS_CLOUD', 'true');
diff --git a/server/src/lib/job-manager.js b/server/src/lib/job-manager.js
index 9e13c4919..8cc45b366 100644
--- a/server/src/lib/job-manager.js
+++ b/server/src/lib/job-manager.js
@@ -164,12 +164,8 @@ export async function cancelJob(jobId) {
if (controller) controller.abort();
}
-/**
- * Cancel a job that is still waiting, saying why (`reason`, kept in
- * failed_reason): one already started, finished or cancelled is left as it
- * is. Returns whether it was cancelled.
- */
-export async function cancelWaitingJob(jobId, reason) {
+/** Cancel a job while its status is `status`, keeping why; whether it was. */
+async function cancelJobWhile(jobId, status, reason) {
const { data, error } = await supabaseAdmin
.from('jobs')
.update({
@@ -178,16 +174,34 @@ export async function cancelWaitingJob(jobId, reason) {
updated_at: new Date().toISOString(),
})
.eq('id', jobId)
- .eq('status', 'waiting')
+ .eq('status', status)
.select('id');
if (error) {
- logger.error({ err: error, jobId }, 'failed to cancel waiting job');
+ logger.error({ err: error, jobId, status }, 'failed to cancel job');
return false;
}
return (data ?? []).length > 0;
}
+/**
+ * Cancel a job that is still waiting, saying why (`reason`, kept in
+ * failed_reason): one already started, finished or cancelled is left as it
+ * is. Returns whether it was cancelled.
+ */
+export async function cancelWaitingJob(jobId, reason) {
+ return cancelJobWhile(jobId, 'waiting', reason);
+}
+
+/**
+ * Cancel a job this process has just claimed (claimWaitingJob), before its
+ * work starts, saying why: one cancelled meanwhile is left as it is.
+ * Returns whether it was cancelled.
+ */
+export async function cancelClaimedJob(jobId, reason) {
+ return cancelJobWhile(jobId, 'active', reason);
+}
+
/** Register an active job's AbortController. */
export function registerActiveJob(jobId, abortController) {
activeJobs.set(jobId, abortController);
diff --git a/server/src/lib/job-manager.test.js b/server/src/lib/job-manager.test.js
index 2fa5c6000..f3637e3b9 100644
--- a/server/src/lib/job-manager.test.js
+++ b/server/src/lib/job-manager.test.js
@@ -1,8 +1,8 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
/**
- * Cancelling a waiting job (2026-10-27): only while it still waits, with
- * why it was cancelled kept on the row.
+ * Cancelling a waiting job, or one just claimed (2026-10-27): only while it
+ * is still in that state, with why it was cancelled kept on the row.
*/
const updates = [];
@@ -30,7 +30,7 @@ vi.mock('../config/supabase.js', () => ({
}));
vi.mock('./logger.js', () => ({ default: { info: vi.fn(), warn: vi.fn(), error: vi.fn() } }));
-const { cancelWaitingJob } = await import('./job-manager.js');
+const { cancelClaimedJob, cancelWaitingJob } = await import('./job-manager.js');
beforeEach(() => {
updates.length = 0;
@@ -65,3 +65,29 @@ describe('cancelWaitingJob', () => {
expect(await cancelWaitingJob('j1', 'subscription_inactive')).toBe(false);
});
});
+
+describe('cancelClaimedJob', () => {
+ it('cancels a job this process has just claimed, and keeps why', async () => {
+ expect(await cancelClaimedJob('j1', 'subscription_inactive')).toBe(true);
+ expect(updates).toEqual([
+ {
+ table: 'jobs',
+ patch: expect.objectContaining({
+ status: 'cancelled',
+ failed_reason: 'subscription_inactive',
+ }),
+ filters: [
+ ['id', 'j1'],
+ ['status', 'active'],
+ ],
+ },
+ ]);
+ });
+
+ it('says so when the job was cancelled meanwhile, or the write fails', async () => {
+ answer = { data: [], error: null };
+ expect(await cancelClaimedJob('j1', 'subscription_inactive')).toBe(false);
+ answer = { data: null, error: { message: 'timeout' } };
+ expect(await cancelClaimedJob('j1', 'subscription_inactive')).toBe(false);
+ });
+});
diff --git a/server/src/lib/job-runner.js b/server/src/lib/job-runner.js
index 59d5a55c1..60ecc0bb6 100644
--- a/server/src/lib/job-runner.js
+++ b/server/src/lib/job-runner.js
@@ -6,6 +6,7 @@
import {
getJob,
+ cancelClaimedJob,
cancelWaitingJob,
claimWaitingJob,
listWaitingJobs,
@@ -15,7 +16,7 @@ import {
registerActiveJob,
unregisterActiveJob,
} from './job-manager.js';
-import { activeBrandFilter } from './active-subscriptions.js';
+import { activeBrandFilter, isBrandActive } from './active-subscriptions.js';
import { processTrackingJob } from '../workers/tracking-worker.js';
import { processContentJob } from '../workers/content-worker.js';
import { generatePulseForBrand } from './pulse/engine.js';
@@ -57,6 +58,27 @@ function createJobProxy(jobId, signal) {
};
}
+/**
+ * Whether a claimed job's organization has no active plan, cancelling the
+ * job if so (2026-10-27): every way a job starts (the nightly run, the
+ * internal trigger, a retry, the recovery) ends in a claim, so this is where
+ * an ended plan stops tracking and content generation, with no Cloro, AI or
+ * scraping spent. A read that fails throws, and the job retries as after any
+ * failure.
+ */
+async function skipUnpaid(jobId, brandId) {
+ if (await isBrandActive(brandId)) return false;
+ if (await cancelClaimedJob(jobId, 'subscription_inactive')) {
+ logger.info({ jobId, brandId }, 'cancelled a job of an organization without an active plan');
+ } else {
+ // Not cancelled (the write failed, or the row moved on): never left
+ // 'active', where nothing would end it.
+ logger.warn({ jobId, brandId }, 'could not cancel a job without an active plan; failing it');
+ await failJob(jobId, 'subscription_inactive');
+ }
+ return true;
+}
+
/**
* Run a tracking job. Call without await (fire-and-forget).
* Handles concurrency gating, retry, and Socket.IO emit.
@@ -90,6 +112,7 @@ async function attemptTrackingJob(jobId, io) {
try {
const { brandId, promptId, promptIds, immediate } = jobRow.data;
+ if (await skipUnpaid(jobId, brandId)) return;
const proxy = createJobProxy(jobId, abortController.signal);
const result = await processTrackingJob({
@@ -237,6 +260,7 @@ async function attemptContentJob(jobId, io) {
try {
const { brandId, model, request } = jobRow.data;
+ if (await skipUnpaid(jobId, brandId)) return;
const proxy = createJobProxy(jobId, abortController.signal);
const result = await processContentJob({ brandId, model, job: proxy, request });
diff --git a/server/src/lib/job-runner.test.js b/server/src/lib/job-runner.test.js
index 52524dfbf..0545a3133 100644
--- a/server/src/lib/job-runner.test.js
+++ b/server/src/lib/job-runner.test.js
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
const manager = vi.hoisted(() => ({
+ cancelClaimedJob: vi.fn(),
cancelWaitingJob: vi.fn(),
claimWaitingJob: vi.fn(),
listWaitingJobs: vi.fn(),
@@ -14,9 +15,14 @@ const manager = vi.hoisted(() => ({
const processTrackingJob = vi.hoisted(() => vi.fn());
const processContentJob = vi.hoisted(() => vi.fn());
const activeBrandFilter = vi.hoisted(() => vi.fn());
+const isBrandActive = vi.hoisted(() => vi.fn());
vi.mock('./job-manager.js', () => manager);
-vi.mock('./active-subscriptions.js', () => ({ activeBrandFilter }));
+// A retry sets its job back to waiting itself.
+vi.mock('../config/supabase.js', () => ({
+ default: { from: () => ({ update: () => ({ eq: async () => ({ error: null }) }) }) },
+}));
+vi.mock('./active-subscriptions.js', () => ({ activeBrandFilter, isBrandActive }));
vi.mock('../workers/tracking-worker.js', () => ({ processTrackingJob }));
vi.mock('../workers/content-worker.js', () => ({ processContentJob }));
vi.mock('./pulse/engine.js', () => ({ generatePulseForBrand: vi.fn() }));
@@ -25,7 +31,7 @@ vi.mock('./logger.js', () => ({
default: { info: vi.fn(), warn: vi.fn(), error: vi.fn() },
}));
-const { runTrackingJob, recoverWaitingJobs } = await import('./job-runner.js');
+const { runContentJob, runTrackingJob, recoverWaitingJobs } = await import('./job-runner.js');
// Default TRACKING_CONCURRENCY.
const MAX = 2;
@@ -59,6 +65,8 @@ beforeEach(() => {
processContentJob.mockReset();
// Every organization on a plan unless a test says otherwise.
activeBrandFilter.mockReset().mockResolvedValue(() => true);
+ isBrandActive.mockReset().mockResolvedValue(true);
+ manager.cancelClaimedJob.mockResolvedValue(true);
// The claim resolves a tick later, as a network call would.
manager.claimWaitingJob.mockImplementation(async (id) => {
await Promise.resolve();
@@ -110,6 +118,100 @@ describe('runTrackingJob', () => {
});
});
+/**
+ * Once the plan has ended (2026-10-27): a job is cancelled when it is
+ * claimed, however it was started, before anything is tracked or written.
+ */
+describe('a job of an organization whose plan has ended', () => {
+ it('is cancelled once claimed, not tracked, and gives its slot back', async () => {
+ const releases = heldRuns();
+ isBrandActive.mockImplementation(async (brandId) => brandId !== 'brand-lapsed');
+
+ for (const id of ['lapsed', 'a', 'b']) runTrackingJob(id);
+ await vi.advanceTimersByTimeAsync(5000);
+
+ expect(isBrandActive).toHaveBeenCalledWith('brand-lapsed');
+ expect(manager.cancelClaimedJob).toHaveBeenCalledWith('lapsed', 'subscription_inactive');
+ expect(processTrackingJob.mock.calls.map(([job]) => job.brandId)).toEqual([
+ 'brand-a',
+ 'brand-b',
+ ]);
+ expect(manager.completeJob).not.toHaveBeenCalledWith('lapsed', expect.anything());
+ expect(manager.failJob).not.toHaveBeenCalled();
+ await drain(releases);
+ });
+
+ it('is failed, never left active, when it cannot be cancelled', async () => {
+ isBrandActive.mockResolvedValue(false);
+ manager.cancelClaimedJob.mockResolvedValue(false);
+ const { default: logger } = await import('./logger.js');
+ vi.mocked(logger.warn).mockClear();
+
+ runTrackingJob('stuck');
+ await flush();
+
+ expect(manager.cancelClaimedJob).toHaveBeenCalledWith('stuck', 'subscription_inactive');
+ expect(manager.failJob).toHaveBeenCalledWith('stuck', 'subscription_inactive');
+ expect(logger.warn).toHaveBeenCalledTimes(1);
+ expect(processTrackingJob).not.toHaveBeenCalled();
+ });
+
+ it('is only cancelled, not failed, when the cancel is written', async () => {
+ isBrandActive.mockResolvedValue(false);
+ manager.cancelClaimedJob.mockResolvedValue(true);
+
+ runTrackingJob('cancelled');
+ await flush();
+
+ expect(manager.failJob).not.toHaveBeenCalled();
+ });
+
+ it('writes no content either', async () => {
+ isBrandActive.mockResolvedValue(false);
+
+ runContentJob('lapsed-content');
+ await flush();
+
+ expect(manager.cancelClaimedJob).toHaveBeenCalledWith(
+ 'lapsed-content',
+ 'subscription_inactive',
+ );
+ expect(processContentJob).not.toHaveBeenCalled();
+ });
+
+ it('is not retried once the plan ends between two attempts', async () => {
+ processTrackingJob.mockRejectedValueOnce(new Error('cloro down'));
+ manager.getJob.mockResolvedValue({ attempts: 1, max_attempts: 3 });
+ isBrandActive.mockResolvedValueOnce(true).mockResolvedValue(false);
+
+ runTrackingJob('retry');
+ await flush();
+ expect(processTrackingJob).toHaveBeenCalledTimes(1);
+
+ // The retry waits attempts × 30 s.
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(manager.claimWaitingJob).toHaveBeenCalledTimes(2);
+ expect(manager.cancelClaimedJob).toHaveBeenCalledWith('retry', 'subscription_inactive');
+ expect(processTrackingJob).toHaveBeenCalledTimes(1);
+ });
+
+ it('tracks nothing when it cannot tell, and retries as after any failure', async () => {
+ isBrandActive.mockRejectedValueOnce(new Error('brands read: timeout'));
+ manager.getJob.mockResolvedValue({ attempts: 1, max_attempts: 3 });
+ processTrackingJob.mockResolvedValue({ resultCount: 0, stamped: false });
+
+ runTrackingJob('unsure');
+ await flush();
+
+ expect(processTrackingJob).not.toHaveBeenCalled();
+ expect(manager.cancelClaimedJob).not.toHaveBeenCalled();
+ expect(manager.failJob).toHaveBeenCalledWith('unsure', 'brands read: timeout');
+
+ await vi.advanceTimersByTimeAsync(30_000);
+ expect(processTrackingJob).toHaveBeenCalledTimes(1);
+ });
+});
+
describe('recoverWaitingJobs', () => {
it('restarts waiting jobs with no run chain here, routed by type', async () => {
const releases = heldRuns();
diff --git a/server/src/lib/signals/pass.js b/server/src/lib/signals/pass.js
index 11ce9c68a..888fc4fbd 100644
--- a/server/src/lib/signals/pass.js
+++ b/server/src/lib/signals/pass.js
@@ -19,7 +19,7 @@
*/
import supabaseAdmin from '../../config/supabase.js';
-import { activeBrandFilter } from '../active-subscriptions.js';
+import { activeBrandFilter, isBrandActive } from '../active-subscriptions.js';
import { logger } from '../logger.js';
import { recordSignalsForBrand } from './record.js';
import { generateActionsForBrand } from '../action-center/generate.js';
@@ -79,6 +79,12 @@ async function rescoreSkippedNight(brandId, now) {
* run this pass covers; omitted, the brand's most recent stamped run.
*/
export async function runSignalPass(brandId, { runAt, now = new Date() } = {}) {
+ // Once the plan has ended, no signal is recorded and no action raised,
+ // whichever way the pass was started (a run's stamp, a late stamp, the
+ // first pass, the catch-up). No row either: should a plan be bought within
+ // the catch-up's window, the night is still there to catch up.
+ if (!(await isBrandActive(brandId))) return { skipped: 'subscription_inactive' };
+
const trackingRunAt = runAt ?? (await latestStampedRun(brandId));
if (!trackingRunAt) {
logger.info({ brandId }, '[signals] no stamped run to record against');
diff --git a/server/src/lib/signals/pass.test.js b/server/src/lib/signals/pass.test.js
index e68f2d004..ce420d325 100644
--- a/server/src/lib/signals/pass.test.js
+++ b/server/src/lib/signals/pass.test.js
@@ -17,8 +17,14 @@ vi.mock('../action-center/priority-data.js', () => ({
rescoreBrandActions: (...a) => rescoreBrandActions(...a),
}));
const activeBrandFilter = vi.fn();
+const isBrandActive = vi.fn();
vi.mock('../active-subscriptions.js', () => ({
activeBrandFilter: (...a) => activeBrandFilter(...a),
+ isBrandActive: (...a) => isBrandActive(...a),
+}));
+const sweepActionTasks = vi.fn();
+vi.mock('../action-center/task-writer/index.js', () => ({
+ sweepActionTasks: (...a) => sweepActionTasks(...a),
}));
import { runFirstSignalPass, runSignalCatchUp, runSignalPass } from './pass.js';
@@ -69,6 +75,8 @@ beforeEach(() => {
generateActionsForBrand.mockResolvedValue({ created: 1, refreshed: 0 });
// Every brand's organization on a plan unless a test says otherwise.
activeBrandFilter.mockReset().mockResolvedValue(() => true);
+ isBrandActive.mockReset().mockResolvedValue(true);
+ sweepActionTasks.mockReset().mockResolvedValue({ written: 0, skipped: 0, failed: 0 });
});
describe('runSignalPass', () => {
@@ -164,6 +172,45 @@ describe('runSignalPass', () => {
expect(recordSignalsForBrand).not.toHaveBeenCalled();
expect(writes).toHaveLength(0);
});
+
+ it("has tonight's new actions' tasks written", async () => {
+ mockDb({ runs: [{ completed_at: RUN_AT }] });
+
+ await runSignalPass(BRAND, { now: NOW });
+
+ expect(isBrandActive).toHaveBeenCalledWith(BRAND);
+ expect(sweepActionTasks).toHaveBeenCalledWith({ brandId: BRAND, now: NOW });
+ });
+
+ /**
+ * Once the plan has ended (2026-10-27): no signal, no action, no task
+ * written, no score, and no row, so a plan bought within the catch-up's
+ * window still gets the night.
+ */
+ it("records nothing and raises no action for a brand whose organization's plan has ended", async () => {
+ const writes = mockDb({ runs: [{ completed_at: RUN_AT }] });
+ isBrandActive.mockResolvedValue(false);
+
+ const result = await runSignalPass(BRAND, { now: NOW });
+
+ expect(result).toEqual({ skipped: 'subscription_inactive' });
+ expect(isBrandActive).toHaveBeenCalledWith(BRAND);
+ expect(recordSignalsForBrand).not.toHaveBeenCalled();
+ expect(generateActionsForBrand).not.toHaveBeenCalled();
+ expect(rescoreBrandActions).not.toHaveBeenCalled();
+ expect(sweepActionTasks).not.toHaveBeenCalled();
+ expect(writes).toHaveLength(0);
+ expect(from).not.toHaveBeenCalled();
+ });
+
+ it('throws, recording nothing, when it cannot tell whether the plan has ended', async () => {
+ const writes = mockDb({ runs: [{ completed_at: RUN_AT }] });
+ isBrandActive.mockRejectedValue(new Error('brands read: timeout'));
+
+ await expect(runSignalPass(BRAND, { now: NOW })).rejects.toThrow('brands read');
+ expect(recordSignalsForBrand).not.toHaveBeenCalled();
+ expect(writes).toHaveLength(0);
+ });
});
describe('runSignalCatchUp', () => {
@@ -286,6 +333,17 @@ describe('runFirstSignalPass', () => {
expect(recordSignalsForBrand).not.toHaveBeenCalled();
});
+ it("raises nothing for a new brand whose organization's plan has ended", async () => {
+ const writes = mockDb({ runs: [{ completed_at: RUN_AT }], passes: [] });
+ isBrandActive.mockResolvedValue(false);
+
+ expect(await runFirstSignalPass(BRAND, { now: NOW })).toEqual({
+ skipped: 'subscription_inactive',
+ });
+ expect(generateActionsForBrand).not.toHaveBeenCalled();
+ expect(writes).toHaveLength(0);
+ });
+
it('leaves the nightly pass reading nightly runs only', async () => {
const writes = mockDb({ runs: [{ completed_at: RUN_AT }] });
diff --git a/server/src/lib/volume-analysis.js b/server/src/lib/volume-analysis.js
index b0e1cec56..f11bb5c65 100644
--- a/server/src/lib/volume-analysis.js
+++ b/server/src/lib/volume-analysis.js
@@ -1,6 +1,7 @@
import { resolveModel } from '../lib/ai-provider.js';
import { regionToLocationCode, languageToCode } from '../lib/dataforseo-codes.js';
import supabaseAdmin from '../config/supabase.js';
+import { isOrgActive } from './active-subscriptions.js';
import { AI_VOLUME_MULTIPLIER, extractIntentKeywords } from '../lib/intent-extraction.js';
import { getSearchVolumes } from './dataforseo.js';
import { withRetry } from './retry.js';
@@ -339,7 +340,7 @@ export async function analyzeBrandVolumes(
) {
const { data: brand, error: brandError } = await supabaseAdmin
.from('brands')
- .select('region, language')
+ .select('region, language, organization_id')
.eq('id', brandId)
.maybeSingle();
@@ -349,6 +350,13 @@ export async function analyzeBrandVolumes(
if (!brand) {
throw new Error(`Brand ${brandId} not found`);
}
+ // Once the plan has ended (2026-10-27), no volume is looked up: every
+ // prompt is a model call and a DataForSEO request. The internal trigger
+ // (after a checkout) reaches here without the API's 402.
+ if (!(await isOrgActive(brand.organization_id))) {
+ logger.info({ brandId }, 'volume analysis skipped: no active plan');
+ return [];
+ }
const resolvedLocationCode =
locationCode ?? (brand.region ? regionToLocationCode(brand.region) : undefined);
diff --git a/server/src/lib/volume-analysis.test.js b/server/src/lib/volume-analysis.test.js
index 80eeebd77..798c92855 100644
--- a/server/src/lib/volume-analysis.test.js
+++ b/server/src/lib/volume-analysis.test.js
@@ -109,7 +109,7 @@ function seed({ activeCount = 0, inactiveCount = 0, withVolumes = [] } = {}) {
prompts.push({ id: `off${i}`, text: `off ${i}`, prompt_set_id: 's1', is_active: false });
}
return {
- brands: [{ id: 'b1', region: 'US', language: 'en' }],
+ brands: [{ id: 'b1', region: 'US', language: 'en', organization_id: 'o1' }],
prompt_sets: [{ id: 's1', brand_id: 'b1' }],
prompts,
prompt_volumes: withVolumes.map((id) => ({
@@ -122,12 +122,14 @@ function seed({ activeCount = 0, inactiveCount = 0, withVolumes = [] } = {}) {
const extractIntentKeywords = vi.fn();
const getSearchVolumes = vi.fn();
+const isOrgActive = vi.fn();
async function load(db) {
const { client, queries } = makeSupabase(db);
vi.resetModules();
vi.doMock('../config/supabase.js', () => ({ default: client }));
vi.doMock('./dataforseo.js', () => ({ getSearchVolumes }));
+ vi.doMock('./active-subscriptions.js', () => ({ isOrgActive }));
vi.doMock('./ai-provider.js', () => ({ resolveModel: () => 'model' }));
vi.doMock('./intent-extraction.js', () => ({
AI_VOLUME_MULTIPLIER: 0.15,
@@ -144,6 +146,8 @@ async function load(db) {
beforeEach(() => {
vi.clearAllMocks();
extractIntentKeywords.mockResolvedValue({ intent: 'informational', keywords: ['a', 'b'] });
+ // Every organization on a plan unless a test says otherwise.
+ isOrgActive.mockResolvedValue(true);
getSearchVolumes.mockResolvedValue({
a: { volume: 10, competitionIndex: 50, competition: 'MEDIUM' },
});
@@ -156,6 +160,30 @@ beforeEach(() => {
* reason to refuse it.
*/
describe('analyzeBrandVolumes', () => {
+ /**
+ * Once the plan has ended (2026-10-27): no model call, no DataForSEO
+ * request, nothing written, whoever asks (the internal trigger has no 402
+ * in front of it).
+ */
+ it("looks up nothing for a brand whose organization's plan has ended", async () => {
+ isOrgActive.mockResolvedValue(false);
+ const { analyzeBrandVolumes, queries } = await load(seed({ activeCount: 3 }));
+
+ expect(await analyzeBrandVolumes('b1', { onlyMissing: true })).toEqual([]);
+ expect(isOrgActive).toHaveBeenCalledWith('o1');
+ expect(extractIntentKeywords).not.toHaveBeenCalled();
+ expect(getSearchVolumes).not.toHaveBeenCalled();
+ expect(queries.map((q) => q.table)).toEqual(['brands']);
+ });
+
+ it("asks about the brand's own organization, and goes on when it is on a plan", async () => {
+ const { analyzeBrandVolumes } = await load(seed({ activeCount: 2 }));
+
+ expect(await analyzeBrandVolumes('b1', { onlyMissing: true })).toHaveLength(2);
+ expect(isOrgActive).toHaveBeenCalledWith('o1');
+ expect(getSearchVolumes).toHaveBeenCalled();
+ });
+
it('analyzes a brand far past the old 50-prompt refusal', async () => {
const { analyzeBrandVolumes } = await load(seed({ activeCount: 100 }));
diff --git a/server/src/routes/bot-ingest.js b/server/src/routes/bot-ingest.js
index ff43e6660..11913f039 100644
--- a/server/src/routes/bot-ingest.js
+++ b/server/src/routes/bot-ingest.js
@@ -9,9 +9,14 @@
* month's raw cap and written in one transaction, so a file it sends again
* (after timing out, say) is read once, and a read that fails leaves nothing
* for the retry to count twice.
+ *
+ * Once the organization's plan has ended (2026-10-27), a sender's requests
+ * are answered 200 and nothing is read or stored: the customer's edge keeps
+ * working and doesn't retry, and we pay for no read, verification or row.
*/
import express, { Router } from 'express';
+import { isOrgActive, isOrgActiveCached } from '../lib/active-subscriptions.js';
import { PlanLimitError } from '../lib/plan-guard.js';
import {
connectionForSecret,
@@ -32,7 +37,14 @@ const router = Router();
// Mounted before the app's JSON parser (server.js), so this reads it.
const body = express.raw({ type: () => true, limit: '20mb' });
-/** The live connection the secret names, before the body is read; 401 otherwise. */
+/** The answer to a sender whose organization's plan has ended: nothing read. */
+export const NOT_STORED = { received: 0, verified: 0, stored: 0, inactive: true };
+
+/**
+ * The live connection the secret names, before the body is read; 401
+ * otherwise. Once the organization's plan has ended, 200 with nothing read
+ * (whether it has is remembered a few minutes, active-subscriptions.js).
+ */
async function sender(req, res, next) {
try {
const secret = (req.headers.authorization || '').replace(/^Bearer\s+/i, '');
@@ -40,6 +52,7 @@ async function sender(req, res, next) {
if (!connection) return res.status(401).json({ error: 'unknown_connection' });
// Its next requests count against the connection's bucket, not the IP's.
markBotSenderVerified(req.headers.authorization, connection.id);
+ if (!(await isOrgActiveCached(connection.organizationId))) return res.json(NOT_STORED);
res.locals.connection = connection;
return next();
} catch (error) {
@@ -75,8 +88,18 @@ router.post('/bots/ingest', botIngestLimiter, sender, body, async (req, res) =>
: await ingestLogFile(connection, hash, parsed);
return res.json({ ...result, records: parsed.records });
} catch (error) {
+ // A plan that ended within the minutes `sender` remembers raises it here
+ // (plan-guard.js): asked again, unremembered, it is answered as `sender`
+ // would. Anything else, a failed read too, is a 500 the sender retries.
if (error instanceof PlanLimitError) {
- return res.status(error.statusCode).json({ error: 'plan_limit', message: error.message });
+ try {
+ if (!(await isOrgActive(connection.organizationId))) return res.json(NOT_STORED);
+ } catch (err) {
+ logger.error({ err }, 'bot ingest plan check failed');
+ return res.status(500).json({ error: 'ingest_failed' });
+ }
+ logger.error({ err: error }, 'bot ingest refused for an organization on a plan');
+ return res.status(500).json({ error: 'ingest_failed' });
}
// Mounted before the request-id middleware: there is no req.log here.
logger.error({ err: error }, 'bot ingest error');
diff --git a/server/src/routes/bot-ingest.test.js b/server/src/routes/bot-ingest.test.js
index 3a5058fcf..e329cf603 100644
--- a/server/src/routes/bot-ingest.test.js
+++ b/server/src/routes/bot-ingest.test.js
@@ -20,6 +20,12 @@ vi.mock('../lib/bot-ingest.js', () => ({
logFileRead: (...a) => logFileRead(...a),
}));
vi.mock('../lib/logger.js', () => ({ logger: { warn: vi.fn(), error: vi.fn() } }));
+const isOrgActiveCached = vi.fn();
+const isOrgActive = vi.fn();
+vi.mock('../lib/active-subscriptions.js', () => ({
+ isOrgActive: (...a) => isOrgActive(...a),
+ isOrgActiveCached: (...a) => isOrgActiveCached(...a),
+}));
vi.mock('../lib/plan-guard.js', () => ({
PlanLimitError: class PlanLimitError extends Error {
constructor(message, statusCode = 403) {
@@ -40,6 +46,9 @@ let url;
beforeEach(async () => {
clearVerifiedTokens();
connectionForSecret.mockReset();
+ // Every organization on a plan unless a test says otherwise.
+ isOrgActiveCached.mockReset().mockResolvedValue(true);
+ isOrgActive.mockReset().mockResolvedValue(true);
ingestBotRequests
.mockReset()
.mockResolvedValue({ received: 0, verified: 0, stored: 0, skipped: {} });
@@ -185,18 +194,92 @@ describe('POST /bots/ingest', () => {
expect(ingestLogFile).not.toHaveBeenCalled();
});
- it('refuses an unknown secret, and an organization without a subscription', async () => {
+ it('refuses an unknown secret', async () => {
connectionForSecret.mockResolvedValue(null);
expect((await post('abot_bad', { requests: [] })).status).toBe(401);
expect(ingestBotRequests).not.toHaveBeenCalled();
+ expect(isOrgActiveCached).not.toHaveBeenCalled();
// A made-up secret earns no bucket of its own.
expect(botIngestKey({ headers: { authorization: 'Bearer abot_bad' }, ip: '1.1.1.1' })).toBe(
'ip:1.1.1.1',
);
+ });
- connectionForSecret.mockResolvedValue({ id: 'c1', kind: 'cloudflare_worker' });
+ /**
+ * Once the plan has ended (2026-10-27): answered 200, so the customer's
+ * edge neither breaks nor retries, with nothing read or stored.
+ */
+ it("stores nothing for an organization whose plan has ended, and doesn't read the file", async () => {
+ isOrgActiveCached.mockResolvedValue(false);
+ connectionForSecret.mockResolvedValue({
+ id: 'c1',
+ organizationId: 'o1',
+ kind: 'cloudflare_worker',
+ });
+ const worker = await post('abot_x', {
+ requests: [{ userAgent: 'GPTBot', ip: '1.2.3.4', url: '/' }],
+ });
+ expect(worker.status).toBe(200);
+ expect(await worker.json()).toEqual({ received: 0, verified: 0, stored: 0, inactive: true });
+ expect(isOrgActiveCached).toHaveBeenCalledWith('o1');
+ expect(ingestBotRequests).not.toHaveBeenCalled();
+
+ connectionForSecret.mockResolvedValue({
+ id: 'c3',
+ organizationId: 'o1',
+ kind: 'cloudflare_logpush',
+ });
+ const file = await fetch(url, {
+ method: 'POST',
+ headers: { Authorization: 'Bearer abot_x' },
+ body: gzipSync('{"content":"tests"}'),
+ });
+ expect(file.status).toBe(200);
+ expect(await file.json()).toMatchObject({ stored: 0, inactive: true });
+ expect(logFileRead).not.toHaveBeenCalled();
+ expect(ingestLogFile).not.toHaveBeenCalled();
+ });
+
+ it('answers the same when the plan ended within the remembered minutes, asked afresh', async () => {
+ connectionForSecret.mockResolvedValue({
+ id: 'c1',
+ organizationId: 'o1',
+ kind: 'cloudflare_worker',
+ });
ingestBotRequests.mockRejectedValue(new PlanLimitError('choose a plan', 402));
- expect((await post('abot_x', { requests: [] })).status).toBe(402);
+ isOrgActive.mockResolvedValue(false);
+ const res = await post('abot_x', { requests: [] });
+ expect(res.status).toBe(200);
+ expect(await res.json()).toMatchObject({ stored: 0, inactive: true });
+ expect(isOrgActive).toHaveBeenCalledWith('o1');
+ });
+
+ it('answers 500, so the sender retries, when a plan that is on is refused', async () => {
+ connectionForSecret.mockResolvedValue({
+ id: 'c1',
+ organizationId: 'o1',
+ kind: 'cloudflare_worker',
+ });
+ ingestBotRequests.mockRejectedValue(new PlanLimitError('choose a plan', 402));
+ const res = await post('abot_x', { requests: [] });
+ expect(res.status).toBe(500);
+ expect(await res.json()).toEqual({ error: 'ingest_failed' });
+ expect(isOrgActive).toHaveBeenCalledWith('o1');
+
+ // A fresh read that fails too.
+ isOrgActive.mockRejectedValue(new Error('organizations read: timeout'));
+ expect((await post('abot_x', { requests: [] })).status).toBe(500);
+ });
+
+ it('answers 500, so the sender retries, when it cannot tell whether the plan has ended', async () => {
+ isOrgActiveCached.mockRejectedValue(new Error('organizations read: timeout'));
+ connectionForSecret.mockResolvedValue({
+ id: 'c1',
+ organizationId: 'o1',
+ kind: 'cloudflare_worker',
+ });
+ expect((await post('abot_x', { requests: [] })).status).toBe(500);
+ expect(ingestBotRequests).not.toHaveBeenCalled();
});
it("checks the secret before reading the body: an unknown one gets 401, not the body's 413", async () => {
diff --git a/server/src/routes/cloro-callback.js b/server/src/routes/cloro-callback.js
new file mode 100644
index 000000000..5ede69d3a
--- /dev/null
+++ b/server/src/routes/cloro-callback.js
@@ -0,0 +1,192 @@
+/**
+ * POST /cloro/callback — Cloro's answer to a submitted task.
+ *
+ * Signature verification (HMAC-SHA256, see lib/cloro-webhook-verify.js) runs when
+ * CLORO_WEBHOOK_SECRET is set. When it's unset (self-hosted installs, or signing not
+ * yet enabled in the Cloro dashboard) we fall back to the original defence:
+ * task_id entropy + cloro_pending_tasks lookup.
+ *
+ * Mounted in server.js after its raw body parser for this path, which keeps
+ * the bytes as sent for the signature.
+ */
+
+import { Router } from 'express';
+import supabaseAdmin from '../config/supabase.js';
+import { parseScraperResponse } from '../lib/cloro-scraper.js';
+import { handleScraperResult } from '../lib/cloro-result-handler.js';
+import { verifyCloroWebhook } from '../lib/cloro-webhook-verify.js';
+import { isBrandActive, isBrandActiveCached } from '../lib/active-subscriptions.js';
+
+const router = Router();
+
+/**
+ * Whether a brand's answer is dropped because its plan has ended. A kept
+ * "active" lets it through (a run's answers arrive in hundreds); dropping is
+ * decided on a fresh read only, as a kept "inactive" may be a minute old. A
+ * read that fails stores the answer: losing a paying brand's paid answer
+ * costs more than storing one more for a lapsed brand.
+ */
+async function droppedForPlan(brandId, log) {
+ try {
+ if (await isBrandActiveCached(brandId)) return false;
+ return !(await isBrandActive(brandId));
+ } catch (err) {
+ log.warn({ err, brandId }, 'cloro callback: plan check failed; stored as usual');
+ return false;
+ }
+}
+
+router.post('/', async (req, res) => {
+ const webhookSecret = process.env.CLORO_WEBHOOK_SECRET;
+ if (webhookSecret) {
+ const verdict = verifyCloroWebhook({
+ rawBody: Buffer.isBuffer(req.body) ? req.body : Buffer.from(''),
+ signatureHeader: req.headers['x-cloro-signature'],
+ timestampHeader: req.headers['x-cloro-timestamp'],
+ secret: webhookSecret,
+ });
+ if (!verdict.ok) {
+ req.log.warn(
+ {
+ reason: verdict.reason,
+ webhookId: req.headers['x-cloro-webhook-id'] || null,
+ ip: req.ip,
+ },
+ 'cloro callback rejected delivery',
+ );
+ return res.status(verdict.status).json({ error: verdict.reason });
+ }
+ }
+
+ // Parse only after the signature check has passed (raw body arrives as a Buffer).
+ let payload;
+ try {
+ payload = Buffer.isBuffer(req.body) ? JSON.parse(req.body.toString('utf8') || '{}') : req.body;
+ } catch {
+ req.log.warn({ ip: req.ip }, 'cloro callback rejected: malformed JSON body');
+ return res.status(400).json({ error: 'malformed JSON body' });
+ }
+
+ // Always ack quickly so Cloro doesn't retry on slow processing
+ res.status(200).send();
+
+ try {
+ const { task, response } = payload || {};
+ const taskId = task?.id;
+ const status = task?.status;
+
+ if (!taskId) {
+ req.log.warn('cloro callback missing task.id in payload');
+ return;
+ }
+
+ const { data: pending } = await supabaseAdmin
+ .from('cloro_pending_tasks')
+ .select('*')
+ .eq('task_id', taskId)
+ .maybeSingle();
+
+ if (!pending) {
+ req.log.info({ taskId }, 'cloro callback: no pending task (already processed?)');
+ return;
+ }
+
+ if (status === 'FAILED') {
+ req.log.error({ taskId, scraperId: pending.scraper_id }, 'cloro callback: task failed');
+ await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
+ return;
+ }
+
+ if (status !== 'COMPLETED') {
+ req.log.info({ taskId, status }, 'cloro callback: task status ignored');
+ return;
+ }
+
+ if (!response) {
+ req.log.error({ taskId }, 'cloro callback: task completed but missing response');
+ await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
+ return;
+ }
+
+ // Once the brand's plan has ended (2026-10-27), the answer is dropped,
+ // not stored: Cloro has been paid for it already, but storing it means a
+ // sentiment model call whenever the brand is named, for a read-only
+ // workspace. Dropping costs nothing more.
+ if (await droppedForPlan(pending.brand_id, req.log)) {
+ req.log.info(
+ { taskId, scraperId: pending.scraper_id, brandId: pending.brand_id },
+ 'cloro callback: dropped, no active plan',
+ );
+ await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
+ return;
+ }
+
+ // Fetch context for result handler
+ const [{ data: brand }, { data: domains }, { data: competitorRows }] = await Promise.all([
+ supabaseAdmin.from('brands').select('id, name').eq('id', pending.brand_id).single(),
+ supabaseAdmin.from('brand_domains').select('domain').eq('brand_id', pending.brand_id),
+ supabaseAdmin.from('competitors').select('id, name, domain').eq('brand_id', pending.brand_id),
+ ]);
+
+ if (!brand) {
+ req.log.error(
+ { taskId, brandId: pending.brand_id },
+ 'cloro callback: brand not found — dropping',
+ );
+ await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
+ return;
+ }
+
+ const brandInfo = {
+ brandName: brand.name,
+ domains: (domains || []).map((d) => d.domain),
+ };
+ const competitors = (competitorRows || []).map((c) => ({
+ id: c.id,
+ name: c.name,
+ domain: c.domain || '',
+ }));
+
+ // A completed task with nothing to store — google-aio for a query Google
+ // shows no AI Overview for — is a finished task, not a failed delivery.
+ // Leaving its pending row behind held every tracking run open: the worker
+ // waited out its stall window and the run's stamp its late deadline, for
+ // answers that had arrived seconds after submission.
+ let aiResponse;
+ try {
+ aiResponse = parseScraperResponse(response, pending.scraper_id);
+ } catch (err) {
+ req.log.info(
+ { taskId, scraperId: pending.scraper_id, reason: err.message },
+ 'cloro callback: task completed with no answer to store',
+ );
+ await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
+ return;
+ }
+
+ try {
+ await handleScraperResult({
+ aiResponse,
+ scraperId: pending.scraper_id,
+ promptId: pending.prompt_id,
+ brandId: pending.brand_id,
+ region: pending.region,
+ brandInfo,
+ competitors,
+ });
+ } finally {
+ // Cloro does not deliver again after our 200, so a task that failed to
+ // store will never come back; keeping its row only holds the run open.
+ await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
+ }
+
+ req.log.info(
+ { taskId, scraperId: pending.scraper_id },
+ 'cloro callback: task processed and inserted',
+ );
+ } catch (err) {
+ req.log.error({ err }, 'cloro callback: error processing webhook');
+ }
+});
+
+export default router;
diff --git a/server/src/routes/cloro-callback.test.js b/server/src/routes/cloro-callback.test.js
new file mode 100644
index 000000000..7590a257a
--- /dev/null
+++ b/server/src/routes/cloro-callback.test.js
@@ -0,0 +1,168 @@
+import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
+import express from 'express';
+
+/**
+ * POST /cloro/callback once the brand's plan has ended (2026-10-27): the
+ * answer is dropped on a fresh read only, and stored as usual when the plan
+ * can't be read, so a paying brand's paid answer is never lost to a failed
+ * read. Answered 200 at once either way.
+ */
+
+const PENDING = {
+ task_id: 'task-1',
+ scraper_id: 'chatgpt-web',
+ prompt_id: 'prompt-1',
+ brand_id: 'brand-1',
+ region: 'US',
+};
+
+// What happened, in order: the tables read, the pending rows deleted, the
+// answers handled.
+const events = [];
+
+vi.mock('../config/supabase.js', () => ({
+ default: {
+ from: (table) => {
+ events.push(['read', table]);
+ const chain = {
+ select: () => chain,
+ eq: () => chain,
+ delete: () => ({
+ eq: async (column, value) => {
+ events.push(['delete', table, value]);
+ return { error: null };
+ },
+ }),
+ maybeSingle: async () => ({ data: table === 'cloro_pending_tasks' ? PENDING : null }),
+ single: async () => ({ data: { id: 'brand-1', name: 'Acme' } }),
+ then: (resolve, reject) => Promise.resolve({ data: [], error: null }).then(resolve, reject),
+ };
+ return chain;
+ },
+ },
+}));
+vi.mock('../lib/cloro-scraper.js', () => ({
+ parseScraperResponse: () => ({ text: 'Acme is good.', citations: [], model: 'gpt' }),
+}));
+const handleScraperResult = vi.fn();
+vi.mock('../lib/cloro-result-handler.js', () => ({
+ handleScraperResult: (...a) => handleScraperResult(...a),
+}));
+vi.mock('../lib/cloro-webhook-verify.js', () => ({ verifyCloroWebhook: () => ({ ok: true }) }));
+const isBrandActive = vi.fn();
+const isBrandActiveCached = vi.fn();
+vi.mock('../lib/active-subscriptions.js', () => ({
+ isBrandActive: (...a) => isBrandActive(...a),
+ isBrandActiveCached: (...a) => isBrandActiveCached(...a),
+}));
+
+const log = { info: vi.fn(), warn: vi.fn(), error: vi.fn() };
+let server;
+let url;
+
+beforeAll(async () => {
+ vi.stubEnv('CLORO_WEBHOOK_SECRET', '');
+ const { default: router } = await import('./cloro-callback.js');
+ const app = express();
+ app.use('/cloro/callback', express.raw({ type: 'application/json' }));
+ app.use((req, _res, next) => {
+ req.log = log;
+ next();
+ });
+ app.use('/cloro/callback', router);
+ await new Promise((resolve) => {
+ server = app.listen(0, resolve);
+ });
+ url = `http://127.0.0.1:${server.address().port}/cloro/callback`;
+});
+
+afterAll(() => {
+ vi.unstubAllEnvs();
+ return new Promise((resolve) => server.close(resolve));
+});
+
+beforeEach(() => {
+ events.length = 0;
+ handleScraperResult.mockReset().mockImplementation(async () => {
+ events.push(['handled']);
+ return { inserted: true };
+ });
+ // On a plan unless a test says otherwise.
+ isBrandActiveCached.mockReset().mockResolvedValue(true);
+ isBrandActive.mockReset().mockResolvedValue(true);
+ for (const fn of Object.values(log)) fn.mockClear();
+});
+
+const deliver = () =>
+ fetch(url, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify({ task: { id: 'task-1', status: 'COMPLETED' }, response: { ok: 1 } }),
+ });
+
+/** The callback answers first and works after: wait for the pending row to go. */
+const settled = () =>
+ vi.waitFor(() => expect(events.some(([kind]) => kind === 'delete')).toBe(true));
+
+const deleted = () => events.filter(([kind]) => kind === 'delete');
+
+describe('POST /cloro/callback once the plan has ended', () => {
+ it('drops the answer on a fresh read, removing its pending row, and reads no more', async () => {
+ isBrandActiveCached.mockResolvedValue(false);
+ isBrandActive.mockResolvedValue(false);
+
+ expect((await deliver()).status).toBe(200);
+ await settled();
+
+ expect(isBrandActive).toHaveBeenCalledWith('brand-1');
+ expect(handleScraperResult).not.toHaveBeenCalled();
+ expect(deleted()).toEqual([['delete', 'cloro_pending_tasks', 'task-1']]);
+ // Not the brand, its domains or competitors: nothing to store them with.
+ expect(events.map(([, table]) => table)).not.toContain('competitors');
+ });
+
+ it('stores it when a kept "inactive" is out of date: a plan was bought since', async () => {
+ isBrandActiveCached.mockResolvedValue(false);
+ isBrandActive.mockResolvedValue(true);
+
+ await deliver();
+ await settled();
+
+ expect(handleScraperResult).toHaveBeenCalledTimes(1);
+ });
+
+ it('stores it on a kept "active", without a fresh read', async () => {
+ await deliver();
+ await settled();
+
+ expect(handleScraperResult).toHaveBeenCalledTimes(1);
+ expect(isBrandActive).not.toHaveBeenCalled();
+ });
+
+ it.each([
+ ['the kept answer', () => isBrandActiveCached.mockRejectedValue(new Error('read: timeout'))],
+ [
+ 'the fresh read',
+ () => {
+ isBrandActiveCached.mockResolvedValue(false);
+ isBrandActive.mockRejectedValue(new Error('read: timeout'));
+ },
+ ],
+ ])(
+ 'stores it as usual when %s fails: its pending row goes only once it is stored',
+ async (_, failing) => {
+ failing();
+
+ await deliver();
+ await settled();
+
+ expect(handleScraperResult).toHaveBeenCalledTimes(1);
+ const kinds = events.map(([kind]) => kind).filter((kind) => kind !== 'read');
+ expect(kinds).toEqual(['handled', 'delete']);
+ expect(log.warn).toHaveBeenCalledWith(
+ expect.objectContaining({ brandId: 'brand-1' }),
+ 'cloro callback: plan check failed; stored as usual',
+ );
+ },
+ );
+});
diff --git a/server/src/routes/traffic-track.test.js b/server/src/routes/traffic-track.test.js
index 8796de660..f4ea4c704 100644
--- a/server/src/routes/traffic-track.test.js
+++ b/server/src/routes/traffic-track.test.js
@@ -54,7 +54,7 @@ vi.mock('../config/supabase.js', () => ({
single: async () =>
chain.columns === 'conversions_enabled'
? conversionsRead
- : { data: { id: 'b1' }, error: null },
+ : { data: { id: 'b1', organization_id: 'o1' }, error: null },
maybeSingle: async () => ({ data: latestPageview, error: null }),
insert: async (row) => {
inserted.push({ table, row });
@@ -80,6 +80,10 @@ vi.mock('../lib/logger.js', () => {
return { default: logger, logger };
});
vi.mock('../lib/traffic-visits.js', () => ({ findVisitOf: (...args) => findVisitOf(...args) }));
+const isOrgActiveCached = vi.fn();
+vi.mock('../lib/active-subscriptions.js', () => ({
+ isOrgActiveCached: (...args) => isOrgActiveCached(...args),
+}));
// The visitor key stands in for the IP address (00250).
const visitorKey = vi.fn();
vi.mock('../lib/visitor-key.js', () => ({ visitorKey: (...args) => visitorKey(...args) }));
@@ -100,6 +104,8 @@ beforeAll(async () => {
afterAll(() => new Promise((resolve) => server.close(resolve)));
beforeEach(() => {
+ // Every organization on a plan unless a test says otherwise.
+ isOrgActiveCached.mockReset().mockResolvedValue(true);
inserted.length = 0;
insertError = null;
findVisitOf.mockReset();
@@ -370,6 +376,69 @@ describe('POST /track/:trackingCode', () => {
});
});
+/**
+ * Once the plan has ended (2026-10-27): a beacon is answered as a stored one
+ * is, so the site's snippet never errors, and nothing is written.
+ */
+describe('POST /track/:trackingCode once the plan has ended', () => {
+ it('stores no visit, no not-found mark and no journey event, and answers 204', async () => {
+ isOrgActiveCached.mockResolvedValue(false);
+ conversionsRead = { data: { conversions_enabled: true }, error: null };
+ latestPageview = { id: 'pv1' };
+
+ expect((await beacon()).status).toBe(204);
+ expect((await beacon({ k: 'nf', nf: 1 })).status).toBe(204);
+ expect((await beacon({ k: 'cv', id: 'Vq3xT0mZ8kLp2RwYbN5sQa', g: 'demo' })).status).toBe(204);
+
+ expect(isOrgActiveCached).toHaveBeenCalledWith('o1');
+ expect(inserted).toEqual([]);
+ expect(updated).toEqual([]);
+ expect(findVisitOf).not.toHaveBeenCalled();
+ expect(visitorKey).not.toHaveBeenCalled();
+ // Only the brand's own read: not its conversions setting.
+ expect(brandFilters.filter(([columns]) => columns === 'conversions_enabled')).toEqual([]);
+ expect(brandFilters).toContainEqual([
+ 'id, organization_id',
+ 'eq',
+ 'tracking_code',
+ 'abcdef123',
+ ]);
+ });
+
+ it('stores the visit for an organization on a plan', async () => {
+ expect((await beacon()).status).toBe(204);
+ expect(inserted.map(({ table }) => table)).toEqual(['ai_traffic_logs']);
+ });
+
+ it('stores the visit as usual when it cannot tell: a beacon is never sent again', async () => {
+ isOrgActiveCached.mockRejectedValue(new Error('organizations read: timeout'));
+ expect((await beacon()).status).toBe(204);
+ expect(inserted.map(({ table }) => table)).toEqual(['ai_traffic_logs']);
+ });
+
+ it('answers a bad body alike whether the plan is on or not, asking nothing', async () => {
+ const post = (body) =>
+ fetch(baseUrl, {
+ method: 'POST',
+ headers: { 'Content-Type': 'text/plain;charset=UTF-8' },
+ body,
+ });
+ const answers = async () => [
+ (await post('{not json')).status,
+ (await post(JSON.stringify({ s: 'chatgpt.com' }))).status,
+ ];
+
+ const onPlan = await answers();
+ isOrgActiveCached.mockResolvedValue(false);
+ const ended = await answers();
+
+ expect(onPlan).toEqual([400, 400]);
+ expect(ended).toEqual(onPlan);
+ expect(isOrgActiveCached).not.toHaveBeenCalled();
+ expect(inserted).toEqual([]);
+ });
+});
+
describe('POST /track/:trackingCode: a journey’s events (CE-017)', () => {
const ID = 'Vq3xT0mZ8kLp2RwYbN5sQa';
const event = (body) =>
diff --git a/server/src/routes/traffic.js b/server/src/routes/traffic.js
index 291e035a6..29164d5f6 100644
--- a/server/src/routes/traffic.js
+++ b/server/src/routes/traffic.js
@@ -2,6 +2,7 @@ import express, { Router } from 'express';
import supabaseAdmin from '../config/supabase.js';
import { journeyEventLimiter, trafficLimiter } from '../middleware/rate-limiter.js';
import logger from '../lib/logger.js';
+import { isOrgActiveCached } from '../lib/active-subscriptions.js';
import { findVisitOf } from '../lib/traffic-visits.js';
import { visitorKey as anonymousVisitorKey } from '../lib/visitor-key.js';
import {
@@ -86,7 +87,7 @@ router.post('/track/:trackingCode', trafficLimiter, trackBody, async (req, res)
const { data: brand, error: brandErr } = await supabaseAdmin
.from('brands')
- .select('id')
+ .select('id, organization_id')
.eq('tracking_code', trackingCode)
.single();
@@ -108,6 +109,12 @@ router.post('/track/:trackingCode', trafficLimiter, trackBody, async (req, res)
return res.status(400).json({ ok: false });
}
+ // Once the organization's plan has ended (2026-10-27), no visit or
+ // journey event is stored: answered as a stored one is, so the site's
+ // snippet never errors, and after the body is checked, so a bad one is
+ // answered alike either way.
+ if (!(await storesFor(brand.organization_id))) return res.status(204).end();
+
// A journey's pageview or conversion (CE-017), from a snippet installed
// with conversions on: stored only while the brand has them on.
if (body.k === 'pv' || body.k === 'cv') {
@@ -224,6 +231,21 @@ router.post('/track/:trackingCode', trafficLimiter, trackBody, async (req, res)
}
});
+/**
+ * Whether a beacon for the organization is stored: while its plan is on,
+ * as remembered a few minutes (active-subscriptions.js), not read per
+ * beacon. A read that fails stores it: a beacon can't be sent again, and a
+ * paying site's lost visit costs more than a lapsed one's stored visit.
+ */
+async function storesFor(orgId) {
+ try {
+ return await isOrgActiveCached(orgId);
+ } catch (err) {
+ logger.warn({ err }, 'traffic plan check failed; stored as usual');
+ return true;
+ }
+}
+
/**
* Stores a journey event (traffic-events.js) for a brand with conversions
* on. Read here, not with the brand above: a server deployed before 00286
diff --git a/server/src/routes/webhook-content-callback.js b/server/src/routes/webhook-content-callback.js
index f432f30d7..db239e992 100644
--- a/server/src/routes/webhook-content-callback.js
+++ b/server/src/routes/webhook-content-callback.js
@@ -6,9 +6,16 @@ import {
} from '../middleware/rate-limiter.js';
import { sourceDataForStatus, VALID_OPPORTUNITY_STATUSES } from '../lib/opportunity-status.js';
import { webhookSecretsMatch } from '../lib/webhook-secret.js';
+import { isBrandActive } from '../lib/active-subscriptions.js';
const router = Router();
+/**
+ * The answer once the brand's plan has ended (2026-10-27): a 2xx, so the
+ * workflow neither fails nor retries, and nothing written.
+ */
+const IGNORED = { success: false, ignored: true, code: 'subscription_inactive' };
+
// This endpoint is public by design. The supplied secret hash gives each
// webhook its own budget; the second limiter counts failed (401) attempts by IP.
router.use(webhookCallbackLimiter, webhookCallbackAuthFailureLimiter);
@@ -50,6 +57,8 @@ router.post('/:id/status', async (req, res) => {
if (!webhookSecretsMatch(webhookConfig?.webhook_secret, req.get('X-Webhook-Secret'))) {
return res.status(401).json({ error: 'Invalid webhook credentials' });
}
+ // Asked only once the secret matched, so the answer tells a stranger nothing.
+ if (!(await isBrandActive(current.brand_id))) return res.status(202).json(IGNORED);
const { status, reason } = req.body ?? {};
if (!VALID_OPPORTUNITY_STATUSES.includes(status)) {
diff --git a/server/src/routes/webhook-content-callback.test.js b/server/src/routes/webhook-content-callback.test.js
index ca83412f8..7cf0e95ec 100644
--- a/server/src/routes/webhook-content-callback.test.js
+++ b/server/src/routes/webhook-content-callback.test.js
@@ -13,6 +13,8 @@ vi.mock('../middleware/rate-limiter.js', () => ({
webhookCallbackLimiter: (_req, _res, next) => next(),
webhookCallbackAuthFailureLimiter: (_req, _res, next) => next(),
}));
+const isBrandActive = vi.hoisted(() => vi.fn());
+vi.mock('../lib/active-subscriptions.js', () => ({ isBrandActive }));
const { default: router } = await import('./webhook-content-callback.js');
@@ -74,6 +76,8 @@ beforeEach(async () => {
state.configs = [{ brand_id: 'brand-1', is_active: true, webhook_secret: 'shared-secret' }];
mockFrom.mockReset();
mockFrom.mockImplementation((name) => table(name));
+ // Every brand's organization on a plan unless a test says otherwise.
+ isBrandActive.mockReset().mockResolvedValue(true);
await startApp();
});
@@ -98,6 +102,29 @@ async function send({
}
describe('POST /webhooks/content/:id/status', () => {
+ /**
+ * Once the plan has ended (2026-10-27): a 2xx, so the workflow doesn't
+ * fail or retry, and the opportunity left as it is.
+ */
+ it("changes nothing for a brand whose organization's plan has ended, and answers 202", async () => {
+ isBrandActive.mockResolvedValue(false);
+ const response = await send({ secret: 'shared-secret', status: 'completed' });
+ expect(response.status).toBe(202);
+ await expect(response.json()).resolves.toEqual({
+ success: false,
+ ignored: true,
+ code: 'subscription_inactive',
+ });
+ expect(isBrandActive).toHaveBeenCalledWith('brand-1');
+ expect(state.opportunities[0].status).toBe('sent');
+ });
+
+ it('asks about the plan only once the secret matched', async () => {
+ isBrandActive.mockResolvedValue(false);
+ expect((await send({ secret: 'wrong-secret' })).status).toBe(401);
+ expect(isBrandActive).not.toHaveBeenCalled();
+ });
+
it('rejects a missing secret', async () => {
const response = await send();
expect(response.status).toBe(401);
diff --git a/server/src/routes/webhook-content-draft-callback.js b/server/src/routes/webhook-content-draft-callback.js
index 5181d6e78..9fbc31972 100644
--- a/server/src/routes/webhook-content-draft-callback.js
+++ b/server/src/routes/webhook-content-draft-callback.js
@@ -8,6 +8,7 @@ import {
} from '../middleware/rate-limiter.js';
import { webhookSecretsMatch } from '../lib/webhook-secret.js';
import { liveAddress, markDeliveryPublished } from '../lib/content-published.js';
+import { isBrandActive } from '../lib/active-subscriptions.js';
/**
* Send to CMS's callback (#1331): the workspace's automation tells how a sent
@@ -18,7 +19,9 @@ import { liveAddress, markDeliveryPublished } from '../lib/content-published.js'
* limiters (each secret's budget, failed attempts by IP) cover it too. A
* delivery id with its webhook's secret proves the package arrived, so the
* callback counts whatever the delivery reads: an automation may call back
- * before it answers the send, or after the send gave up waiting.
+ * before it answers the send, or after the send gave up waiting. Once the
+ * brand's plan has ended (2026-10-27), it is answered 202 and nothing is
+ * written, as /webhooks/content does.
*/
const router = Router();
@@ -27,6 +30,8 @@ router.use(webhookCallbackLimiter, webhookCallbackAuthFailureLimiter);
const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
const DENIED = { error: 'Invalid webhook credentials' };
+/** Once the plan has ended: a 2xx, so the workflow neither fails nor retries. */
+const IGNORED = { success: false, ignored: true, code: 'subscription_inactive' };
const MAX_URL = 2048;
/** A field a workflow left unmapped (Zapier and Make send "" or null) reads as left out. */
@@ -95,6 +100,8 @@ router.post('/:deliveryId/status', async (req, res) => {
(c) => c.events?.includes(DELIVERY_EVENT) && webhookSecretsMatch(c.webhook_secret, provided),
);
if (!matches) return res.status(401).json(DENIED);
+ // Asked only once the secret matched, so the answer tells a stranger nothing.
+ if (!(await isBrandActive(delivery.brand_id))) return res.status(202).json(IGNORED);
const parsed = statusBody.safeParse(req.body ?? {});
const url = parsed.success && parsed.data.url ? liveAddress(parsed.data.url) : null;
diff --git a/server/src/routes/webhook-content-draft-callback.test.js b/server/src/routes/webhook-content-draft-callback.test.js
index 81d3bc708..213cbdcfc 100644
--- a/server/src/routes/webhook-content-draft-callback.test.js
+++ b/server/src/routes/webhook-content-draft-callback.test.js
@@ -55,6 +55,8 @@ vi.mock('../middleware/rate-limiter.js', () => ({
webhookCallbackLimiter: (_req, _res, next) => next(),
webhookCallbackAuthFailureLimiter: (_req, _res, next) => next(),
}));
+const isBrandActive = vi.hoisted(() => vi.fn());
+vi.mock('../lib/active-subscriptions.js', () => ({ isBrandActive }));
const { default: router } = await import('./webhook-content-draft-callback.js');
@@ -63,6 +65,8 @@ let server;
let baseUrl;
beforeEach(async () => {
+ // Every brand's organization on a plan unless a test says otherwise.
+ isBrandActive.mockReset().mockResolvedValue(true);
state.deliveries = [
{
id: DELIVERY,
@@ -111,6 +115,35 @@ const call = (body, { id = DELIVERY, secret = 's3cret' } = {}) =>
const published = (url, more = {}) => call({ status: 'published', url, ...more });
describe('POST /webhooks/content-drafts/:deliveryId/status', () => {
+ /**
+ * Once the plan has ended (2026-10-27): a 2xx, so the workflow doesn't
+ * fail or retry, and the delivery, its page and History left as they are.
+ */
+ it("writes nothing for a brand whose organization's plan has ended, and answers 202", async () => {
+ isBrandActive.mockResolvedValue(false);
+ const res = await published('https://acme.example/live');
+ expect(res.status).toBe(202);
+ expect(await res.json()).toEqual({
+ success: false,
+ ignored: true,
+ code: 'subscription_inactive',
+ });
+ expect(isBrandActive).toHaveBeenCalledWith('b1');
+ expect(state.deliveries[0].status).toBe('sent');
+ expect(state.drafts[0].status).toBe('ready');
+ expect(state.rpcs).toEqual([]);
+
+ expect((await call({ status: 'failed', reason: 'CMS down' })).status).toBe(202);
+ expect(state.deliveries[0].status).toBe('sent');
+ });
+
+ it('asks about the plan only once the secret matched', async () => {
+ isBrandActive.mockResolvedValue(false);
+ const res = await call({ status: 'published', url: 'https://a.example' }, { secret: 'nope' });
+ expect(res.status).toBe(401);
+ expect(isBrandActive).not.toHaveBeenCalled();
+ });
+
it('marks the delivery and its page published at the address, keeping a publish version', async () => {
const res = await published('https://acme.example/blog/best-aeo-tools', { externalId: '812' });
expect(res.status).toBe(200);
diff --git a/server/src/server.js b/server/src/server.js
index b7c85b097..dd784c5ed 100644
--- a/server/src/server.js
+++ b/server/src/server.js
@@ -15,6 +15,7 @@ import { requireActiveSubscription } from './middleware/subscription.js';
import routes from './routes/index.js';
import trafficRoutes from './routes/traffic.js';
import botIngestRoutes from './routes/bot-ingest.js';
+import cloroCallbackRoutes from './routes/cloro-callback.js';
import { pruneBotEvents } from './lib/bot-ingest.js';
import { pruneContentVersions } from './lib/content-history.js';
import {
@@ -31,9 +32,6 @@ import { sweepImproveOutcomes } from './lib/action-center/improve-outcome.js';
import { sweepBrandFacts } from './lib/brand-facts-sweep.js';
import { sweepCatalogSources } from './lib/catalog/sources.js';
import { sweepWebflowPublished } from './lib/cms/webflow-status.js';
-import { parseScraperResponse } from './lib/cloro-scraper.js';
-import { handleScraperResult } from './lib/cloro-result-handler.js';
-import { verifyCloroWebhook } from './lib/cloro-webhook-verify.js';
import { generateBriefForOpportunity } from './routes/content.js';
import webhookContentCallbackRoutes from './routes/webhook-content-callback.js';
import webhookContentDraftCallbackRoutes from './routes/webhook-content-draft-callback.js';
@@ -595,150 +593,9 @@ app.post('/api/internal/analyze-volumes', async (req, res) => {
}
});
-// --- Cloro webhook callback ---
-// Signature verification (HMAC-SHA256, see lib/cloro-webhook-verify.js) runs when
-// CLORO_WEBHOOK_SECRET is set. When it's unset (self-hosted installs, or signing not
-// yet enabled in the Cloro dashboard) we fall back to the original defence:
-// task_id entropy + cloro_pending_tasks lookup.
-app.post('/cloro/callback', async (req, res) => {
- const webhookSecret = process.env.CLORO_WEBHOOK_SECRET;
- if (webhookSecret) {
- const verdict = verifyCloroWebhook({
- rawBody: Buffer.isBuffer(req.body) ? req.body : Buffer.from(''),
- signatureHeader: req.headers['x-cloro-signature'],
- timestampHeader: req.headers['x-cloro-timestamp'],
- secret: webhookSecret,
- });
- if (!verdict.ok) {
- req.log.warn(
- {
- reason: verdict.reason,
- webhookId: req.headers['x-cloro-webhook-id'] || null,
- ip: req.ip,
- },
- 'cloro callback rejected delivery',
- );
- return res.status(verdict.status).json({ error: verdict.reason });
- }
- }
-
- // Parse only after the signature check has passed (raw body arrives as a Buffer).
- let payload;
- try {
- payload = Buffer.isBuffer(req.body) ? JSON.parse(req.body.toString('utf8') || '{}') : req.body;
- } catch {
- req.log.warn({ ip: req.ip }, 'cloro callback rejected: malformed JSON body');
- return res.status(400).json({ error: 'malformed JSON body' });
- }
-
- // Always ack quickly so Cloro doesn't retry on slow processing
- res.status(200).send();
-
- try {
- const { task, response } = payload || {};
- const taskId = task?.id;
- const status = task?.status;
-
- if (!taskId) {
- req.log.warn('cloro callback missing task.id in payload');
- return;
- }
-
- const { data: pending } = await supabaseAdmin
- .from('cloro_pending_tasks')
- .select('*')
- .eq('task_id', taskId)
- .maybeSingle();
-
- if (!pending) {
- req.log.info({ taskId }, 'cloro callback: no pending task (already processed?)');
- return;
- }
-
- if (status === 'FAILED') {
- req.log.error({ taskId, scraperId: pending.scraper_id }, 'cloro callback: task failed');
- await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
- return;
- }
-
- if (status !== 'COMPLETED') {
- req.log.info({ taskId, status }, 'cloro callback: task status ignored');
- return;
- }
-
- if (!response) {
- req.log.error({ taskId }, 'cloro callback: task completed but missing response');
- await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
- return;
- }
-
- // Fetch context for result handler
- const [{ data: brand }, { data: domains }, { data: competitorRows }] = await Promise.all([
- supabaseAdmin.from('brands').select('id, name').eq('id', pending.brand_id).single(),
- supabaseAdmin.from('brand_domains').select('domain').eq('brand_id', pending.brand_id),
- supabaseAdmin.from('competitors').select('id, name, domain').eq('brand_id', pending.brand_id),
- ]);
-
- if (!brand) {
- req.log.error(
- { taskId, brandId: pending.brand_id },
- 'cloro callback: brand not found — dropping',
- );
- await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
- return;
- }
-
- const brandInfo = {
- brandName: brand.name,
- domains: (domains || []).map((d) => d.domain),
- };
- const competitors = (competitorRows || []).map((c) => ({
- id: c.id,
- name: c.name,
- domain: c.domain || '',
- }));
-
- // A completed task with nothing to store — google-aio for a query Google
- // shows no AI Overview for — is a finished task, not a failed delivery.
- // Leaving its pending row behind held every tracking run open: the worker
- // waited out its stall window and the run's stamp its late deadline, for
- // answers that had arrived seconds after submission.
- let aiResponse;
- try {
- aiResponse = parseScraperResponse(response, pending.scraper_id);
- } catch (err) {
- req.log.info(
- { taskId, scraperId: pending.scraper_id, reason: err.message },
- 'cloro callback: task completed with no answer to store',
- );
- await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
- return;
- }
-
- try {
- await handleScraperResult({
- aiResponse,
- scraperId: pending.scraper_id,
- promptId: pending.prompt_id,
- brandId: pending.brand_id,
- region: pending.region,
- brandInfo,
- competitors,
- });
- } finally {
- // Cloro does not deliver again after our 200, so a task that failed to
- // store will never come back; keeping its row only holds the run open.
- await supabaseAdmin.from('cloro_pending_tasks').delete().eq('task_id', taskId);
- }
-
- req.log.info(
- { taskId, scraperId: pending.scraper_id },
- 'cloro callback: task processed and inserted',
- );
- } catch (err) {
- req.log.error({ err }, 'cloro callback: error processing webhook');
- }
-});
+// --- Cloro webhook callback (routes/cloro-callback.js) ---
+// After the raw body parser mounted for this path at the top.
+app.use('/cloro/callback', cloroCallbackRoutes);
// Public, webhook-secret-authenticated status callback for external workflows.
// It is mounted outside /api so no dashboard JWT is required; the router has its own stricter limiter.