// THE GAP BETWEEN TWO JOBS ON ONE PLATFORM QUEUE — `pacing.jobGapSeconds` // (plans/pacing-guard.md). Pure and client-safe: the settings schema // sanitizes with it, the job funnel (jobs/queueGap.ts) waits by it, and the // /settings form shows the gaps in force. // The built-in gaps: every platform queue waits `default` seconds after the // previous network job on it ended; X five minutes, a forum and Bluesky one, // Instagram ten (it checkpoints the logged-in account that reads too often). export const JOB_GAP_DEFAULT_SECONDS: Readonly> = Object.freeze({ default: 20, twitter: 300, xenforo: 60, bluesky: 60, instagram: 600, }); export const JOB_GAP_MAX_SECONDS = 24 * 60 * 60; export function sanitizeJobGapSeconds(v: unknown): Partial> { const out: Partial> = {}; if (!v || typeof v !== "object" || Array.isArray(v)) return out; for (const [k, raw] of Object.entries(v as Record)) { if (!/^[a-z0-9][a-z0-9.-]{0,99}$/.test(k)) continue; if (typeof raw !== "number" || !Number.isFinite(raw) || raw < 0) continue; out[k] = Math.min(JOB_GAP_MAX_SECONDS, Math.floor(raw)); } return out; } // The gap (seconds) a job on `platform`'s queue waits after the previous one // ended: the operator's value for the platform, else the built-in one for it, // else the operator's `default`, else the built-in default. `builtIn` lets the // e2e server shrink the BUILT-IN values (E2E_JOB_GAP_SECONDS) while a value a // spec sets explicitly still applies. export function jobGapSecondsFor( pacing: { jobGapSeconds?: Partial> } | undefined, platform: string, builtIn: Readonly> = JOB_GAP_DEFAULT_SECONDS, ): { seconds: number; key: string } { const set = pacing?.jobGapSeconds ?? {}; if (set[platform] !== undefined) return { seconds: set[platform]!, key: platform }; if (builtIn[platform] !== undefined) return { seconds: builtIn[platform]!, key: platform }; if (set.default !== undefined) return { seconds: set.default, key: "default" }; return { seconds: builtIn.default ?? JOB_GAP_DEFAULT_SECONDS.default, key: "default" }; }