commit 0cf2db56caac4fc876371d7de7c558870579b2fb
parent 9f5a2912e712065b54f570be6aea1b52911ceee9
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Wed, 18 Nov 2026 23:10:37 -0500
scheduler: review 3 N9 -- no more X syncs are queued than X's job cap has starts left
At the hour cap with one start left, the tick queued every due X channel,
started one and deferred the rest at their start. It now looks at the cap
(xCapRoom: the starts left, nothing counted), takes away the X jobs already
waiting on X's queue, queues that many X syncs and holds the others for a
later tick with the reason; every other channel is queued as before.
Co-Authored-By: Claude <noreply@anthropic.com>
Diffstat:
4 files changed, 76 insertions(+), 2 deletions(-)
diff --git a/common/jobs/networkGuard.ts b/common/jobs/networkGuard.ts
@@ -357,6 +357,15 @@ export async function xCapHeldReason(paths: Pick<Paths, "transcriptsDir" | "jobs
return status.ok ? null : (status.sentence ?? "X's job cap is reached.");
}
+// HOW MANY X JOBS MAY START NOW under X's job cap (0 when it is reached or
+// its count cannot be read): for the scheduler, which queues no more X syncs
+// than that (review 3 N9). A look: nothing is counted.
+export async function xCapRoom(paths: Pick<Paths, "transcriptsDir" | "jobsDir">, now?: number): Promise<number> {
+ const status = await xJobCapStatus(paths, now ?? queueGapClock().now());
+ if (!status.ok) return 0;
+ return Math.max(0, Math.min(status.perHour - status.lastHour, status.perDay - status.lastDay));
+}
+
export async function xJobGuard(opts: {
paths: Paths;
channelSlug?: string;
diff --git a/common/jobs/syncScheduler.test.ts b/common/jobs/syncScheduler.test.ts
@@ -329,3 +329,24 @@ test("R-b: a sync deferred by X's job cap is not a failure, and the channel wait
assert.equal(foldScheduledSyncOutcome(running, { status: "running" }, now, sched, 0), false);
assert.equal(running.lastQueuedJobId, "j");
});
+
+// Review 3 N9: at X's hour cap with one start left, queueing every due X
+// channel would start one and defer the rest, each a job for nothing. The
+// tick queues at most the starts the cap has left — less the X jobs already
+// waiting on X's queue — and holds the rest for a later tick. A look: no
+// start is recorded.
+test("review 3 N9: no more X syncs are queued than the cap has starts left; the others wait, and the rest are untouched", async () => {
+ const { limitXSyncs } = await import("./syncScheduler");
+ const isX = (slug: string) => slug.startsWith("x-");
+ const due = ["x-a", "yt-a", "x-b", "x-c", "yt-b"];
+ // One start left, none waiting: one X channel, every other channel.
+ const one = limitXSyncs(due, isX, { room: 1, waiting: 0 });
+ assert.deepEqual(one.queue, ["x-a", "yt-a", "yt-b"]);
+ assert.deepEqual(one.held.map((h) => h.slug), ["x-b", "x-c"]);
+ assert.match(one.held[0].reason, /^X's job cap: 1 X job may start now and 0 are waiting on X's queue — queued on a later tick/);
+ // The start left is taken by an X job already waiting: none.
+ assert.deepEqual(limitXSyncs(due, isX, { room: 1, waiting: 1 }).queue, ["yt-a", "yt-b"]);
+ // Room enough: all.
+ assert.deepEqual(limitXSyncs(due, isX, { room: 12, waiting: 2 }).queue, due);
+ assert.deepEqual(limitXSyncs(due, isX, { room: 12, waiting: 2 }).held, []);
+});
diff --git a/common/jobs/syncScheduler.ts b/common/jobs/syncScheduler.ts
@@ -70,6 +70,34 @@ export function xSyncHold(
return (config.platform ?? detectPlatform(config.url)) === "twitter" ? held : null;
}
+// NO MORE X SYNCS THAN X'S CAP HAS STARTS LEFT (review 3 N9). With one start
+// left in the hour, queueing every due X channel would start one and defer
+// the rest at their start — a job each for nothing. `room`: the starts the
+// cap has left now (jobs/networkGuard.ts xCapRoom, a look: nothing counted);
+// `waiting`: X jobs already queued on X's queue, which take that room first.
+// The X channels past it are held for a later tick; every other channel is
+// queued as it was.
+export function limitXSyncs(
+ toQueue: readonly string[],
+ isX: (slug: string) => boolean,
+ cap: { room: number; waiting: number },
+): { queue: string[]; held: Array<{ slug: string; reason: string }> } {
+ let left = Math.max(0, cap.room - cap.waiting);
+ const queue: string[] = [];
+ const held: Array<{ slug: string; reason: string }> = [];
+ const reason =
+ `X's job cap: ${cap.room} X job${cap.room === 1 ? "" : "s"} may start now and ${cap.waiting} ${cap.waiting === 1 ? "is" : "are"} waiting on X's queue — ` +
+ "queued on a later tick, not deferred at its start";
+ for (const slug of toQueue) {
+ if (!isX(slug)) queue.push(slug);
+ else if (left > 0) {
+ queue.push(slug);
+ left--;
+ } else held.push({ slug, reason });
+ }
+ return { queue, held };
+}
+
// WHY AN INSTAGRAM CHANNEL IS NOT SYNCED NOW, or null (review 1, S10). A
// channel with no cookie source of its own would be refused by its fetch
// before anything is asked; one while Instagram is held, by the guard. Either
diff --git a/editor/app/scheduler/runTick.ts b/editor/app/scheduler/runTick.ts
@@ -5,7 +5,8 @@
// row and two forms import from it.
import { getPaths } from "yt-dlp-transcript-common/lib/paths";
import { instagramHeldReason } from "yt-dlp-transcript-common/jobs/instagramGuard";
-import { xCapHeldReason } from "yt-dlp-transcript-common/jobs/networkGuard";
+import { xCapHeldReason, xCapRoom } from "yt-dlp-transcript-common/jobs/networkGuard";
+import { X_QUEUE_KEY } from "yt-dlp-transcript-common/lib/xJobRule";
import { listChannelConfigs } from "yt-dlp-transcript-common/controller/channels";
import { getSettings } from "yt-dlp-transcript-common/lib/settings";
import { getRegistry } from "yt-dlp-transcript-common/jobs/registry";
@@ -16,6 +17,7 @@ import {
foldScheduledSyncOutcome,
selectDueChannels,
isInstagramPostsConfig,
+ limitXSyncs,
xSyncHold,
type ChannelEntry,
} from "yt-dlp-transcript-common/jobs/syncScheduler";
@@ -155,10 +157,24 @@ export async function runSchedulerTick(): Promise<SchedulerTickResult> {
// Concurrency cap doubles as the stagger: queue at most (cap - running)
// channels; the rest roll to the next tick instead of fanning out at once.
const slots = Math.max(0, scheduler.maxConcurrentSyncs - running);
- const toQueue = due.slice(0, slots);
+ let toQueue = due.slice(0, slots);
for (const slug of due.slice(slots)) {
skipped.push({ slug, reason: "concurrency cap (deferred to next tick)" });
}
+ // NO MORE X SYNCS THAN X'S CAP HAS STARTS LEFT (review 3 N9): the rest
+ // would each be deferred at its start. A look at the count, no start.
+ const isXSync = (slug: string) => {
+ const config = channels.find((c) => c.slug === slug)?.config;
+ return config ? xSyncHold(config, "x") !== null : false;
+ };
+ if (toQueue.some(isXSync)) {
+ const waiting = getRegistry()
+ .list()
+ .filter((r) => r.status === "queued" && r.queueKey === X_QUEUE_KEY).length;
+ const limited = limitXSyncs(toQueue, isXSync, { room: await xCapRoom(paths), waiting });
+ toQueue = limited.queue;
+ skipped.push(...limited.held);
+ }
const queued: string[] = [];
const bySlug = new Map(channels.map((c) => [c.slug, c.config]));