Skip to content

Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.

fix(security): let due posts take turns by account in each publish run

spacesdrive52341f4cc530
diff --git a/backend/lib/scheduledPublishing.ts b/backend/lib/scheduledPublishing.ts
index 904273d..45f6cc8 100644
--- a/backend/lib/scheduledPublishing.ts
+++ b/backend/lib/scheduledPublishing.ts
@@ -41,3 +41,21 @@ export function failureMessage(reason: string, transient: boolean, attempts: num
if (!transient || attempts < MAX_PUBLISH_ATTEMPTS) return reason;
return `${reason.replace(/\.?$/, '.')} Publishing stopped after ${MAX_PUBLISH_ATTEMPTS} attempts; schedule the post again.`;
}
+
+// one account's backlog must not hold every other account's posts back for the whole run, so due posts take turns by
+// account, oldest first; five a run still clears a 50 post backlog in under three hours of quarter-hour runs
+export const MAX_POSTS_PER_ACCOUNT_PER_RUN = 5;
+
+export function fairPublishOrder<T extends { accountId: string; scheduledAt: string }>(due: T[], perAccount = MAX_POSTS_PER_ACCOUNT_PER_RUN): T[] {
+ const queues = new Map<string, T[]>();
+ for (const post of [...due].sort((first, second) => first.scheduledAt.localeCompare(second.scheduledAt))) {
+ const queue = queues.get(post.accountId) ?? [];
+ if (queue.length < perAccount) queue.push(post);
+ queues.set(post.accountId, queue);
+ }
+ const ordered: T[] = [];
+ for (let turn = 0; turn < perAccount; turn++) {
+ for (const queue of queues.values()) if (queue[turn]) ordered.push(queue[turn]);
+ }
+ return ordered;
+}
diff --git a/backend/services/scheduledPublisher.ts b/backend/services/scheduledPublisher.ts
index bb145c4..53830e9 100644
--- a/backend/services/scheduledPublisher.ts
+++ b/backend/services/scheduledPublisher.ts
@@ -3,7 +3,7 @@ import { publishableAccount } from './publishingService.ts';
import { DatabaseError, NotFoundError, UpstreamError, UpstreamTimeoutError, isAbortError } from '../lib/errors.ts';
import { errorFields } from '../lib/logger.ts';
import {
- MAX_PUBLISH_ATTEMPTS, containerStep, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
+ MAX_PUBLISH_ATTEMPTS, containerStep, fairPublishOrder, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
} from '../lib/scheduledPublishing.ts';
import type { AccountRepository } from '../repositories/accountRepository.ts';
import type { ScheduledPostRepository } from '../repositories/scheduledPostRepository.ts';
@@ -160,7 +160,8 @@ export function createScheduledPublisher({ accounts, posts, context }: Scheduled
let candidates: ScheduledPost[];
try {
const [due, stale] = await load();
- candidates = [...stale, ...due];
+ // a stale claim may already be on Instagram, so it is checked first; due posts take turns by account
+ candidates = [...stale, ...fairPublishOrder(due)];
} catch (err) {
log.error('scheduler could not load due posts', errorFields(err));
return { processed, errors };
diff --git a/backend/tests/fixtures/scheduledPublisher.process.golden.json b/backend/tests/fixtures/scheduledPublisher.process.golden.json
index 4785460..690e03f 100644
--- a/backend/tests/fixtures/scheduledPublisher.process.golden.json
+++ b/backend/tests/fixtures/scheduledPublisher.process.golden.json
@@ -25,9 +25,9 @@
3000,
3000,
3000,
+ 2000,
3000,
3000,
- 2000,
3000,
3000,
3000,
@@ -95,10 +95,6 @@
"log",
"{\"level\":\"info\",\"message\":\"scheduled post published\",\"requestId\":\"cron-test\",\"postId\":\"p-image\",\"mediaId\":\"m-c-IG1\"}"
],
- [
- "log",
- "{\"level\":\"info\",\"message\":\"scheduled post published\",\"requestId\":\"cron-test\",\"postId\":\"p-reel\",\"mediaId\":\"m-c-IG1\"}"
- ],
[
"error",
"{\"level\":\"error\",\"message\":\"scheduled post failed\",\"requestId\":\"cron-test\",\"postId\":\"p-story\",\"attempts\":1,\"error\":\"Processing failed: Bad video\",\"errorName\":\"UpstreamError\"}"
@@ -115,6 +111,10 @@
"error",
"{\"level\":\"error\",\"message\":\"scheduled post failed\",\"requestId\":\"cron-test\",\"postId\":\"p-publish\",\"attempts\":1,\"error\":\"Publish refused\",\"errorName\":\"UpstreamError\"}"
],
+ [
+ "log",
+ "{\"level\":\"info\",\"message\":\"scheduled post published\",\"requestId\":\"cron-test\",\"postId\":\"p-reel\",\"mediaId\":\"m-c-IG1\"}"
+ ],
[
"log",
"{\"level\":\"info\",\"message\":\"scheduled post published\",\"requestId\":\"cron-test\",\"postId\":\"p-dup\",\"mediaId\":\"m-c-IG1\"}"
@@ -129,22 +129,6 @@
"url": "https://graph.instagram.com/v25.0/IG1/media_publish?creation_id=c-IG1&access_token=T1",
"method": "POST"
},
- {
- "url": "https://graph.instagram.com/v25.0/IG1/media?access_token=T1&media_type=REELS&video_url=https%3A%2F%2Fcdn%2Fr.mp4&share_to_feed=true",
- "method": "POST"
- },
- {
- "url": "https://graph.instagram.com/v25.0/c-IG1?fields=status_code%2Cstatus&access_token=T1",
- "method": "GET"
- },
- {
- "url": "https://graph.instagram.com/v25.0/c-IG1?fields=status_code%2Cstatus&access_token=T1",
- "method": "GET"
- },
- {
- "url": "https://graph.instagram.com/v25.0/IG1/media_publish?creation_id=c-IG1&access_token=T1",
- "method": "POST"
- },
{
"url": "https://graph.instagram.com/v25.0/ERR/media?access_token=T2&media_type=STORIES&video_url=https%3A%2F%2Fcdn%2Fs.mp4",
"method": "POST"
@@ -437,6 +421,22 @@
"url": "https://graph.instagram.com/v25.0/c-SLOW?fields=status_code%2Cstatus&access_token=T6",
"method": "GET"
},
+ {
+ "url": "https://graph.instagram.com/v25.0/IG1/media?access_token=T1&media_type=REELS&video_url=https%3A%2F%2Fcdn%2Fr.mp4&share_to_feed=true",
+ "method": "POST"
+ },
+ {
+ "url": "https://graph.instagram.com/v25.0/c-IG1?fields=status_code%2Cstatus&access_token=T1",
+ "method": "GET"
+ },
+ {
+ "url": "https://graph.instagram.com/v25.0/c-IG1?fields=status_code%2Cstatus&access_token=T1",
+ "method": "GET"
+ },
+ {
+ "url": "https://graph.instagram.com/v25.0/IG1/media_publish?creation_id=c-IG1&access_token=T1",
+ "method": "POST"
+ },
{
"url": "https://graph.instagram.com/v25.0/IG1/media?access_token=T1&image_url=https%3A%2F%2Fcdn%2Fd.jpg",
"method": "POST"
@@ -564,7 +564,7 @@
"filters": [
[
"id",
- "p-reel"
+ "p-story"
],
[
"attempts",
@@ -583,7 +583,7 @@
"filters": [
[
"id",
- "ig1"
+ "igErr"
],
[
"platform",
@@ -599,10 +599,10 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-reel",
+ "p_id": "p-story",
"p_user_id": null,
"p_patch": {
- "containerId": "c-IG1"
+ "containerId": "c-ERR"
},
"p_status": null,
"p_scheduled_at": null,
@@ -614,14 +614,12 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-reel",
+ "p_id": "p-story",
"p_user_id": null,
"p_patch": {
- "publishedMediaId": "m-c-IG1",
- "publishedAt": "2026-09-01T12:00:00.000Z",
- "error": null
+ "error": "Processing failed: Bad video"
},
- "p_status": "published",
+ "p_status": "failed",
"p_scheduled_at": null,
"p_only_status": null
},
@@ -638,7 +636,7 @@
"filters": [
[
"id",
- "p-story"
+ "p-expired"
],
[
"attempts",
@@ -657,7 +655,7 @@
"filters": [
[
"id",
- "igErr"
+ "igExp"
],
[
"platform",
@@ -673,10 +671,10 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-story",
+ "p_id": "p-expired",
"p_user_id": null,
"p_patch": {
- "containerId": "c-ERR"
+ "containerId": "c-EXP"
},
"p_status": null,
"p_scheduled_at": null,
@@ -688,12 +686,13 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-story",
+ "p_id": "p-expired",
"p_user_id": null,
"p_patch": {
- "error": "Processing failed: Bad video"
+ "error": "Container expired",
+ "containerId": null
},
- "p_status": "failed",
+ "p_status": "pending",
"p_scheduled_at": null,
"p_only_status": null
},
@@ -710,7 +709,7 @@
"filters": [
[
"id",
- "p-expired"
+ "p-missing"
],
[
"attempts",
@@ -729,7 +728,7 @@
"filters": [
[
"id",
- "igExp"
+ "gone"
],
[
"platform",
@@ -745,28 +744,12 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-expired",
- "p_user_id": null,
- "p_patch": {
- "containerId": "c-EXP"
- },
- "p_status": null,
- "p_scheduled_at": null,
- "p_only_status": null
- },
- "filters": []
- },
- {
- "type": "rpc",
- "table": "merge_scheduled_post",
- "values": {
- "p_id": "p-expired",
+ "p_id": "p-missing",
"p_user_id": null,
"p_patch": {
- "error": "Container expired",
- "containerId": null
+ "error": "Account not found: gone"
},
- "p_status": "pending",
+ "p_status": "failed",
"p_scheduled_at": null,
"p_only_status": null
},
@@ -783,7 +766,7 @@
"filters": [
[
"id",
- "p-missing"
+ "p-create"
],
[
"attempts",
@@ -802,7 +785,7 @@
"filters": [
[
"id",
- "gone"
+ "igBad"
],
[
"platform",
@@ -818,10 +801,10 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-missing",
+ "p_id": "p-create",
"p_user_id": null,
"p_patch": {
- "error": "Account not found: gone"
+ "error": "Invalid image"
},
"p_status": "failed",
"p_scheduled_at": null,
@@ -840,7 +823,7 @@
"filters": [
[
"id",
- "p-create"
+ "p-publish"
],
[
"attempts",
@@ -859,7 +842,7 @@
"filters": [
[
"id",
- "igBad"
+ "igPub"
],
[
"platform",
@@ -875,10 +858,25 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-create",
+ "p_id": "p-publish",
"p_user_id": null,
"p_patch": {
- "error": "Invalid image"
+ "containerId": "c-PUB"
+ },
+ "p_status": null,
+ "p_scheduled_at": null,
+ "p_only_status": null
+ },
+ "filters": []
+ },
+ {
+ "type": "rpc",
+ "table": "merge_scheduled_post",
+ "values": {
+ "p_id": "p-publish",
+ "p_user_id": null,
+ "p_patch": {
+ "error": "Publish refused"
},
"p_status": "failed",
"p_scheduled_at": null,
@@ -897,7 +895,7 @@
"filters": [
[
"id",
- "p-publish"
+ "p-slow"
],
[
"attempts",
@@ -916,7 +914,7 @@
"filters": [
[
"id",
- "igPub"
+ "igSlow"
],
[
"platform",
@@ -932,10 +930,10 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-publish",
+ "p_id": "p-slow",
"p_user_id": null,
"p_patch": {
- "containerId": "c-PUB"
+ "containerId": "c-SLOW"
},
"p_status": null,
"p_scheduled_at": null,
@@ -947,12 +945,12 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-publish",
+ "p_id": "p-slow",
"p_user_id": null,
"p_patch": {
- "error": "Publish refused"
+ "error": "Instagram did not finish processing the media within 3 minutes"
},
- "p_status": "failed",
+ "p_status": "pending",
"p_scheduled_at": null,
"p_only_status": null
},
@@ -969,7 +967,7 @@
"filters": [
[
"id",
- "p-slow"
+ "p-reel"
],
[
"attempts",
@@ -988,7 +986,7 @@
"filters": [
[
"id",
- "igSlow"
+ "ig1"
],
[
"platform",
@@ -1004,10 +1002,10 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-slow",
+ "p_id": "p-reel",
"p_user_id": null,
"p_patch": {
- "containerId": "c-SLOW"
+ "containerId": "c-IG1"
},
"p_status": null,
"p_scheduled_at": null,
@@ -1019,12 +1017,14 @@
"type": "rpc",
"table": "merge_scheduled_post",
"values": {
- "p_id": "p-slow",
+ "p_id": "p-reel",
"p_user_id": null,
"p_patch": {
- "error": "Instagram did not finish processing the media within 3 minutes"
+ "publishedMediaId": "m-c-IG1",
+ "publishedAt": "2026-09-01T12:00:00.000Z",
+ "error": null
},
- "p_status": "pending",
+ "p_status": "published",
"p_scheduled_at": null,
"p_only_status": null
},
diff --git a/backend/tests/lib/scheduledPublishing.test.ts b/backend/tests/lib/scheduledPublishing.test.ts
index 4c9ca4c..c85c395 100644
--- a/backend/tests/lib/scheduledPublishing.test.ts
+++ b/backend/tests/lib/scheduledPublishing.test.ts
@@ -1,7 +1,7 @@
import { test, describe } from 'node:test';
import assert from 'node:assert/strict';
import {
- MAX_PUBLISH_ATTEMPTS, PUBLISH_LEASE_MS, containerStep, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
+ MAX_PUBLISH_ATTEMPTS, PUBLISH_LEASE_MS, containerStep, fairPublishOrder, MAX_POSTS_PER_ACCOUNT_PER_RUN, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
} from '../../lib/scheduledPublishing.ts';
describe('scheduled publishing rules', () => {
@@ -36,4 +36,14 @@ describe('scheduled publishing rules', () => {
assert.equal(failureMessage('Instagram API unreachable (reset)', true, MAX_PUBLISH_ATTEMPTS),
'Instagram API unreachable (reset). Publishing stopped after 3 attempts; schedule the post again.');
});
+
+ test('due posts take turns by account, oldest first, and one account gets a capped share of a run', () => {
+ const post = (id: string, accountId: string, minute: number) => ({ id, accountId, scheduledAt: `2026-11-05T10:${String(minute).padStart(2, '0')}:00.000Z` });
+ const busy = Array.from({ length: 20 }, (_, i) => post(`a${i}`, 'busy', i));
+ const ordered = fairPublishOrder([...busy, post('b1', 'quiet', 30), post('c1', 'other', 25)]);
+ assert.deepEqual(ordered.slice(0, 3).map(item => item.id), ['a0', 'c1', 'b1']);
+ assert.equal(ordered.filter(item => item.accountId === 'busy').length, MAX_POSTS_PER_ACCOUNT_PER_RUN);
+ assert.deepEqual(ordered.filter(item => item.accountId === 'busy').map(item => item.id), ['a0', 'a1', 'a2', 'a3', 'a4']);
+ assert.deepEqual(fairPublishOrder([]), []);
+ });
});
diff --git a/docs/architecture/data-flows/SCHEDULING.md b/docs/architecture/data-flows/SCHEDULING.md
index 380710f..daf1fad 100644
--- a/docs/architecture/data-flows/SCHEDULING.md
+++ b/docs/architecture/data-flows/SCHEDULING.md
@@ -36,6 +36,8 @@ Each run lists two kinds of candidates:
- Due posts: `status = 'pending'` and `scheduled_at <= now()` (`listDue()`)
- Stale posts: `status = 'publishing'` and `claimed_at` older than 20 minutes (`listStale(cutoff)`, cutoff from `staleClaimCutoff()`)
+Stale posts come first, since one may already be on Instagram. Due posts then take turns by account, oldest first, at most `MAX_POSTS_PER_ACCOUNT_PER_RUN` (5) per account a run (`fairPublishOrder()` in `backend/lib/scheduledPublishing.ts`, used by both schedulers), so one account's large backlog cannot hold every other account's posts back for a whole run (audit lead L8). The rest of a backlog goes out in the next runs.
+
It then claims each candidate with one UPDATE that only matches the post as it was listed:
```sql
diff --git a/scripts/publish-scheduled.ts b/scripts/publish-scheduled.ts
index abcd21d..abb558b 100644
--- a/scripts/publish-scheduled.ts
+++ b/scripts/publish-scheduled.ts
@@ -4,7 +4,7 @@
// SUPABASE_SERVICE_KEY (a sb_secret_ key or the legacy service_role key) as GitHub secrets.
import {
- MAX_PUBLISH_ATTEMPTS, containerStep, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
+ MAX_PUBLISH_ATTEMPTS, containerStep, fairPublishOrder, failureMessage, failureOutcome, isTransientGraphCode, staleClaimCutoff,
} from '../backend/lib/scheduledPublishing.ts';
import { redactSecrets } from '../backend/lib/redact.ts';
import type { Account, Platform, PostStatus, ScheduledPost } from '../shared/types/domain.ts';
@@ -324,7 +324,8 @@ async function main(): Promise<void> {
console.log(` Time: ${new Date().toISOString()}\n`);
const [stalePosts, duePosts] = await Promise.all([getStalePosts(), getDuePosts()]);
- const candidates = [...stalePosts, ...duePosts];
+ // the same order as the Worker: stale claims first, then due posts taking turns by account
+ const candidates = [...stalePosts, ...fairPublishOrder(duePosts)];
if (candidates.length === 0) {
console.log('No posts due for publishing');