// X IN SITTINGS (lane x-sittings; the operator, 21 Nov 2026: "Implement all // your suggestions"). The archive's X account was suspended after one sitting // of a trending read and 12 timeline fetches back to back, each 5-6 minutes // after the last. So X is now asked in SMALL, UNEVEN SITTINGS: // // 1. A SITTING begins at a cookie refresh (the jar's refresh time, // social/xSessionBroker.ts) and admits at most `social.x.sittingJobs` X // job starts (default 4, bounded 1-6 HERE, in code). A start beyond it is // refused: a new Refresh on /cookies starts the next sitting. // 2. ONE KIND PER SITTING: "timelines" (account post fetches, captures, // checks) or "browse" (trending reads, search walks). The first job fixes // it; a job of the other kind is refused until the next Refresh. // 3. THE GAP BEFORE AN X JOB IS DRAWN per job, uniform in a range (default // 5-12 min; the floor 5 min is in code), not a fixed beat. // // Pure and client-safe: the settings coercion, the guard (jobs/networkGuard.ts), // the count on disk (jobs/xSitting.ts), the queue gap (jobs/queueGap.ts) and // /cookies all read it. What counts is X's job cap's rule (lib/xJobRule.ts // xJobCounts): the same starts, counted at the same moment. export type XSittingKind = "timelines" | "browse"; export const X_SITTING_DEFAULT_JOBS = 4; export const X_SITTING_MIN_JOBS = 1; export const X_SITTING_MAX_JOBS = 6; // `social.x.sittingJobs` as stored: a whole number 1-6, else absent. export function parseXSittingJobs(v: unknown): number | undefined { if (typeof v !== "number" || !Number.isFinite(v)) return undefined; return Math.min(X_SITTING_MAX_JOBS, Math.max(X_SITTING_MIN_JOBS, Math.floor(v))); } // The limit in force: the setting, bounded again (a hand-edited file never // passes 6), else the default. export function xSittingJobs(x: { sittingJobs?: number } | undefined): number { return parseXSittingJobs(x?.sittingJobs) ?? X_SITTING_DEFAULT_JOBS; } // ── the kind of an X job ─────────────────────────────────────────────────── // The job as it will run: its kind, and what its channel is (read by the // guard from the channel's config, never from the caller). export type XSittingPlan = { kind: string; channel?: { library?: boolean; search?: boolean }; }; // "browse": reading what X shows — a trending page, a search walk, and the // capture of what a trending read found (a library's posts). "timelines": // everything else — an account's posts in any mode, an X run's channel, a // capture of an account's posts, an availability check, an import of one // post, an X video window. export function xSittingKindOf(plan: XSittingPlan): XSittingKind { if (plan.kind === "read-x-trending") return "browse"; if (plan.channel?.search === true) return "browse"; if (plan.channel?.library === true) return "browse"; return "timelines"; } export const X_SITTING_KIND_TEXT: Readonly> = Object.freeze({ timelines: "timelines (account post fetches, captures, checks)", browse: "browse (trending reads, search walks)", }); // ── the verdict ──────────────────────────────────────────────────────────── // The sitting as it stands: when it began (the jar's refresh time; null: no // refresh on record), its kind (null: no job yet; "unknown": its record was // lost and its kind cannot be told), the jobs it started, and its first job. export type XSittingState = { began: string | null; kind: XSittingKind | "unknown" | null; used: number; first?: { jobKind: string; at: number }; }; export type XSittingVerdict = | { ok: true; used: number; left: number; limit: number } | { ok: false; why: "used-up" | "kind" | "unknown"; used: number; left: number; limit: number; sentence: string }; const hhmm = (iso: string | number) => new Date(iso).toISOString().slice(11, 16); function beganText(began: string | null): string { return began ? `since the cookies were refreshed at ${hhmm(began)} UTC` : "with no cookie refresh on record"; } const NEXT = "A new Refresh cookies on /cookies starts the next sitting; nothing queues or retries by itself."; // May a job of `kind` start in this sitting? export function xSittingVerdict(state: XSittingState, kind: XSittingKind, limit: number): XSittingVerdict { const used = state.used; const left = Math.max(0, limit - used); const base = { used, left, limit }; if (used >= limit) { return { ok: false, why: "used-up", ...base, sentence: `This X sitting is used up: ${used} of ${limit} X job${limit === 1 ? "" : "s"} started ${beganText(state.began)} ` + `(social.x.sittingJobs, at most ${X_SITTING_MAX_JOBS}). ${NEXT}`, }; } if (state.kind === "unknown") { return { ok: false, why: "unknown", ...base, sentence: `This X sitting's kind cannot be told: its record is gone and the ${used} X job${used === 1 ? "" : "s"} it started left none. ` + `No X job starts in it. ${NEXT}`, }; } if (state.kind !== null && state.kind !== kind) { const first = state.first ? ` (its first job, ${state.first.jobKind} at ${hhmm(state.first.at)} UTC, fixed it)` : ""; return { ok: false, why: "kind", ...base, sentence: `This X sitting is a ${state.kind} sitting${first}: a ${kind} job does not start in it — ` + `one sitting is ${X_SITTING_KIND_TEXT.timelines} or ${X_SITTING_KIND_TEXT.browse}, never both. ${NEXT}`, }; } return { ok: true, ...base }; } // Is `next` a LATER refresh than `began`, so a new sitting begins? An equal or // earlier time (a jar restored from a backup, a clock set back) continues the // sitting on record: only a real refresh resets it. export function isLaterRefresh(next: string | null | undefined, began: string | null): boolean { if (!next) return false; const n = Date.parse(next); if (!Number.isFinite(n)) return false; if (began === null) return true; const b = Date.parse(began); return !Number.isFinite(b) || n > b; } // ── the gap before an X job ──────────────────────────────────────────────── // The floor of X's gap: never below the fixed five minutes it replaced. export const X_GAP_FLOOR_MINUTES = 5; export const X_GAP_DEFAULT_MIN_MINUTES = 5; export const X_GAP_DEFAULT_MAX_MINUTES = 12; export const X_GAP_CEILING_MINUTES = 120; // THE RANGE IS NEVER ONE VALUE (review 1 F4): the longest gap is at least // this much above the shortest, whatever is set — a range of one value is a // fixed beat again. export const X_GAP_MIN_SPREAD_MINUTES = 3; // `social.x.gapMinMinutes` / `gapMaxMinutes` as stored: whole minutes from // the floor to the ceiling, else absent. export function parseXGapMinutes(v: unknown): number | undefined { if (typeof v !== "number" || !Number.isFinite(v)) return undefined; return Math.min(X_GAP_CEILING_MINUTES, Math.max(X_GAP_FLOOR_MINUTES, Math.floor(v))); } export type XGapRange = { minSeconds: number; maxSeconds: number }; // The range in force, in seconds. The minimum is the setting (else 5 min), // never below the floor, and raised — never lowered — by an explicit // `pacing.jobGapSeconds.twitter` above it; the maximum is the setting (else // 12 min), never less than the minimum plus X_GAP_MIN_SPREAD_MINUTES. export function xGapRange( x: { gapMinMinutes?: number; gapMaxMinutes?: number } | undefined, explicitTwitterSeconds?: number, ): XGapRange { const minMinutes = parseXGapMinutes(x?.gapMinMinutes) ?? X_GAP_DEFAULT_MIN_MINUTES; let minSeconds = minMinutes * 60; if (typeof explicitTwitterSeconds === "number" && Number.isFinite(explicitTwitterSeconds) && explicitTwitterSeconds > minSeconds) { minSeconds = Math.floor(explicitTwitterSeconds); } const maxMinutes = parseXGapMinutes(x?.gapMaxMinutes) ?? X_GAP_DEFAULT_MAX_MINUTES; const maxSeconds = Math.max(minSeconds + X_GAP_MIN_SPREAD_MINUTES * 60, maxMinutes * 60); return { minSeconds, maxSeconds }; } // One draw from the range, uniform: `rand` in [0, 1). export function drawXGapSeconds(range: XGapRange, rand: number): number { const r = Number.isFinite(rand) ? Math.min(Math.max(rand, 0), 1) : 0; const s = range.minSeconds + Math.floor(r * (range.maxSeconds - range.minSeconds + 1)); return Math.min(range.maxSeconds, Math.max(range.minSeconds, s)); } // "5-12 min" (whole minutes), else "300-720 s". export function xGapRangeText(range: XGapRange): string { const whole = range.minSeconds % 60 === 0 && range.maxSeconds % 60 === 0; return whole ? `${range.minSeconds / 60}-${range.maxSeconds / 60} min` : `${range.minSeconds}-${range.maxSeconds} s`; } // ── the account's state (suspension) ────────────────────────────────────── // The operator's word on the account X answers as: "suspended" (read-only: // its answers are thinned, so a post it cannot see is NOT a deleted post) or // "ok". Marked by hand (`pnpm ops x-session`, /cookies). export type XAccountState = "ok" | "suspended"; export function isXAccountState(v: unknown): v is XAccountState { return v === "ok" || v === "suspended"; } // The kinds a suspended account refuses: they RECORD posts as deleted or // unavailable from what X answers. A capture with its screenshots off records // no availability (social/xPostCapture.ts records it with the shot only). // (review 1 F5) A capture refuses unless both halves that read a page's // verdict — the post's shot and its article — are off by name; the video // checks (check-availability and its kin) on an X channel refuse as the // post check does. Every other writer is covered at the write // (lib/jobContext.ts xAnswersUntrusted). const SUSPENDED_REFUSED_KINDS: ReadonlySet = new Set([ "check-post-availability", "check-availability", "quick-availability-check", "check-maybe-missing", "check-kept-deleted", ]); export function xSuspendedRefuses(plan: { kind: string; params?: Readonly> }): boolean { if (SUSPENDED_REFUSED_KINDS.has(plan.kind)) return true; if (plan.kind !== "capture-posts") return false; const p = plan.params; if (p?.avatarsOnly === true || p?.contextOnly === true) return false; return p?.shots !== false || p?.articles !== false; } export function xSuspendedSentence(what: string, since: string, note?: string): string { return ( `The X account is marked suspended (since ${since.slice(0, 16).replace("T", " ")} UTC${note ? `: ${note}` : ""}): ` + `${what} is refused, because a suspended account's answers are thinned and a post it cannot see would be recorded ` + `deleted or unavailable when it is not. Mark it ok (pnpm ops x-session --json '{"accountState":"ok"}', or /cookies) once it is restored.` ); } // ── what ONE X job may read (review 1 F8) ───────────────────────────────── // A walk of an account's timeline (sync new, backfill, full, older) or of an // X search reads at most `social.x.maxPostsPerJob` posts (default 300), and // never more than the ceiling in the code; then it saves its place and ends, // to be continued in a later sitting. One job read 634 on 20 Nov 2026. export const X_POSTS_PER_JOB_DEFAULT = 300; export const X_POSTS_PER_JOB_MIN = 20; export const X_POSTS_PER_JOB_CEILING = 400; export function parseXPostsPerJob(v: unknown): number | undefined { if (typeof v !== "number" || !Number.isFinite(v)) return undefined; return Math.min(X_POSTS_PER_JOB_CEILING, Math.max(X_POSTS_PER_JOB_MIN, Math.floor(v))); } // The posts this job may read: the caller's limit when lower, else the // setting, never above the ceiling. export function xJobPostLimit(x: { maxPostsPerJob?: number } | undefined, asked?: number): number { const cap = parseXPostsPerJob(x?.maxPostsPerJob) ?? X_POSTS_PER_JOB_DEFAULT; return typeof asked === "number" && Number.isFinite(asked) && asked > 0 ? Math.min(Math.floor(asked), cap) : cap; }