// THE RUNNING JOB, as an async context (cookie refresh alerts, // plans/cookie-refresh-alerts.md). runManagedFunction runs every job's `fn` // inside `runInJobContext`, so code deep under it — the one yt-dlp runner, a // forum fetch — can say which job met a stale service and write the sentence // into THAT job's log, without a jobId threaded through forty signatures. // // Read-only for its consumers: what the job is, its log, and a set the cookie // recorder marks so the job's failure line can name the cause. Outside a job // (a test calling a controller directly, a CLI run) there is no context and // nothing is attributed. // // SERVER-ONLY (node:async_hooks). One storage per process, on globalThis — the // house pattern for state Next may load twice (jobs/registry.ts). import { AsyncLocalStorage } from "node:async_hooks"; export type JobContext = { jobId: string; kind: string; channelSlug?: string; // The queue the job runs on (its key in the registry). Code that must run // on ONE queue — an Instagram fetch, on platform:instagram — asks it here. queueKey?: string; // The job's log (the same sink its `fn` writes to). onLog: (line: string) => void; // Services whose cookie alert this job met while it ran (serviceCookies). staleServices: Set; // X'S STOP met by this job (jobs/downloadBackoff.ts recordXStop): a rate // limit or a bot check from X. The job ends deferred, saying so. xStop?: { until: number; why: string; at: number }; // AN X JOB'S ACCOUNT MARK (review 1 F5): set on every job of X's queues // (jobs/streamCommand.ts). True while the operator has marked the X account // suspended — then no writer records a post or a video as gone from what X // answered (xAnswersUntrusted below). Read at each write, so a mark made // while the job runs counts. xSuspended?: () => Promise; }; const KEY = Symbol.for("archilyzer.jobContext"); function storage(): AsyncLocalStorage { const g = globalThis as unknown as Record | undefined>; return (g[KEY] ??= new AsyncLocalStorage()); } export function runInJobContext(ctx: JobContext, fn: () => T): T { return storage().run(ctx, fn); } export function currentJobContext(): JobContext | undefined { return storage().getStore(); } // Is this write inside an X job whose account is marked suspended? Then what X // answered is thinned, and nothing may be recorded deleted, unavailable or // private from it (review 1 F5). False outside a job. A read that throws is // taken as suspended (fail closed). export async function xAnswersUntrusted(): Promise { const ask = currentJobContext()?.xSuspended; if (!ask) return false; try { return await ask(); } catch { return true; } }