Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit 457d3eaf47e5e1e5eedd15a311dd2a2b13b54e1a
parent 835a05793a10a41a2a8d8aa3c64ee3e556dff30f
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Thu, 19 Nov 2026 00:00:19 -0500

x-jobs: review 4 B and C (addresses) -- an import by an X page's address is X's on any channel, t.co included; X's addresses read by their host alone

import-media decides X by isXUrl, not by detectPlatform: a t.co, x.com or
twitter.com page goes on X's queue, under the jar rule and the cap, whatever
the channel. xJobAsksX judges import-media by its url (the guards read it as
askParams: import-media has no replay descriptor, and a spec would offer a
Retry it does not have), and the metadata pass asks xJobContextRefusal before
it hands an X address to yt-dlp.

isXUrl: a trailing dot is dropped from the host; twimg.com and its
subdomains are X's; an address written without a scheme is X's when its first
segment is an X host. Userinfo, lookalikes, case and subdomains as before.

Co-Authored-By: Claude <noreply@anthropic.com>

Diffstat:
Mcommon/controller/importMedia.ts | 34++++++++++++++++++++++++++++------
Mcommon/controller/landMedia.test.ts | 33++++++++++++++++++++++++++++++++-
Mcommon/jobs/networkGuard.ts | 13+++++++++++--
Mcommon/jobs/pacingGuard.test.ts | 22++++++++++++++++++++++
Mcommon/jobs/streamCommand.ts | 3+++
Mcommon/lib/xJobRule.test.ts | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcommon/lib/xJobRule.ts | 59+++++++++++++++++++++++++++++++++++++----------------------
7 files changed, 191 insertions(+), 31 deletions(-)

diff --git a/common/controller/importMedia.ts b/common/controller/importMedia.ts @@ -25,8 +25,10 @@ // attach-media's walk (controller/attachMedia.ts) instead. // // The job is `import-media` on its own queue (`import-media:<slug>`): a -// multi-GB copy never holds a platform's download queue. `needsMedia`: it -// writes the container and the audio. +// multi-GB copy never holds a platform's download queue — except an import by +// an X page's address (x.com, twitter.com, t.co), which asks X for its +// metadata and so runs on X's queue on any channel (review 4 B). `needsMedia`: +// it writes the container and the audio. import { localMediaWriteRefusal } from "./localMediaDoors"; import path from "node:path"; @@ -64,7 +66,7 @@ import { } from "./landMedia"; import { refreshVideoMetadata, type RefreshRunner } from "./refreshVideoMetadata"; -import { X_QUEUE_KEY } from "../lib/xJobRule"; +import { X_QUEUE_KEY, isXUrl, xJobContextRefusal } from "../lib/xJobRule"; export const IMPORT_MEDIA_JOB_KIND = "import-media"; export const IMPORT_MEDIA_RESULT_MARKER = "@@import-media "; @@ -73,6 +75,14 @@ export function importMediaQueueKey(slug: string): string { return `import-media:${slug}`; } +// THE JOB'S QUEUE (review 4 B): an import by an X page's address — x.com, +// twitter.com, t.co (lib/xJobRule.ts isXUrl) — asks X for its metadata, so it +// runs on X's queue, under the jar rule and X's job cap, on ANY channel. +// Everything else on its own queue. +export function importMediaJobQueueKey(slug: string, target: ImportTarget): string { + return target.kind === "url" && isXUrl(target.url) ? X_QUEUE_KEY : importMediaQueueKey(slug); +} + // A path the action hands to attach-media's walk rather than landing itself. export function isArchivePath(p: string, isDirectory: boolean): boolean { return isDirectory || /\.(zip|7z)$/i.test(p); @@ -103,7 +113,8 @@ export async function resolveImportTarget( if (req.id && req.id !== id) { return { error: `"id" ${req.id} is not the id ${req.url} is archived by (${id}); give one of them` }; } - const platform = detectPlatform(req.url) ?? "unknown"; + // X's addresses (t.co and X's media hosts too) are X's pace and holds. + const platform = isXUrl(req.url) ? "twitter" : (detectPlatform(req.url) ?? "unknown"); return { kind: "url", id, url: req.url, platform, exists: await hasInfo(path.join(dataDir, id)) }; } if (req.meta) { @@ -178,6 +189,15 @@ export async function importMediaRun( } else if (held || cooling > 0) { throw new Error(held ?? `${target.platform} is in a rate-limit cooldown; import once it lapses`); } else { + // THE BELT (review 4 B): an X address is asked of yt-dlp only inside + // a job on X's queue — nothing else spaces it, jars it or counts it. + if (isXUrl(target.url)) { + const outside = xJobContextRefusal( + `The metadata fetch of ${target.url}`, + `pnpm ops import-media --json '{"slug":"${req.slug}","path":"…","url":"…"}' (it queues an X page on X's queue)`, + ); + if (outside) throw new Error(outside); + } const created = !(await isDir(videoDir)); await mkdir(videoDir, { recursive: true }); try { @@ -331,8 +351,10 @@ export async function runImportMediaJob(opts: ImportMediaJobOpts): Promise<Strea } return runManagedFunction({ kind: IMPORT_MEDIA_JOB_KIND, - // An x.com URL asks X: X's queue (lib/xJobRule.ts), like every X job. - queueKey: target.kind === "url" && target.platform === "twitter" ? X_QUEUE_KEY : importMediaQueueKey(req.slug), + // An X page's address asks X: X's queue (importMediaJobQueueKey), and the + // guards judge it by that address (lib/xJobRule.ts xJobAsksX). + queueKey: importMediaJobQueueKey(req.slug, target), + ...(target.kind === "url" ? { askParams: { url: target.url } } : {}), paths, channelSlug: req.slug, videoId: target.id, diff --git a/common/controller/landMedia.test.ts b/common/controller/landMedia.test.ts @@ -28,7 +28,7 @@ await writeFile(process.env.SETTINGS_FILE, "{}"); const { getPaths } = await import("../lib/paths"); const { landMediaFile, transcriptionHandoffLines } = await import("./landMedia"); -const { importMediaRun, resolveImportTarget } = await import("./importMedia"); +const { importMediaJobQueueKey, importMediaRun, resolveImportTarget } = await import("./importMedia"); const { mediaImportSidecar } = await import("../lib/mediaImport-server"); const { loadSavedVideo } = await import("../lib/savedVideo-server"); const { isDoNotClean } = await import("../lib/doNotClean-server"); @@ -219,6 +219,37 @@ test("import by url: one metadata pass makes the record under the URL id", async assert.match(archive, /^RumbleEmbed v8embed$/m); }); +test("review 4 B: an import by an X page's address (t.co included) is X's: X's queue on any channel, and never asked of yt-dlp outside an X job", async () => { + const urlTarget = (url: string) => ({ kind: "url" as const, id: "1", url, platform: "unknown", exists: false }); + for (const url of ["https://t.co/AbC123", "https://x.com/u/status/1", "https://mobile.twitter.com/u/status/1"]) { + assert.equal(importMediaJobQueueKey(SLUG, urlTarget(url)), "platform:twitter", url); + } + assert.equal(importMediaJobQueueKey(SLUG, urlTarget("https://rumble.com/v9abcde-a-stream.html")), `import-media:${SLUG}`); + assert.equal(importMediaJobQueueKey(SLUG, { kind: "id", id: "1" }), `import-media:${SLUG}`); + // The belt: outside a job on X's queue, no metadata pass is asked of an X address. + const calls: string[][] = []; + const refresh = async (_cwd: string, args: string[]): Promise<AttemptOutcome> => { + calls.push(args); + return { exitCode: 0, stderrTail: "", archiveLine: null, formatLine: null }; + }; + const req = { slug: SLUG, path: await fake("x.mp4", "mov,mp4,m4a", 30), url: "https://t.co/AbC123" }; + await assert.rejects( + importMediaRun({ + paths, + request: req, + channelConfig: CONFIG, + cookiePolicy: { cookies: undefined, mode: "when-required" }, + target: { kind: "url", id: "AbC123", url: req.url, platform: "unknown", exists: false } as never, + onLog, + signal, + deps: { refresh, land: deps }, + }), + /asks X, and X is asked only by the editor's own jobs/, + ); + assert.equal(calls.length, 0, "yt-dlp was not asked"); + await assert.rejects(stat(path.join(CORPUS, "channels", SLUG, "data", "AbC123")), "no record dir was made"); +}); + test("the store's root: the corpus default is made when missing; an override or a dangling link is refused", async () => { const { ensureStoreRoot } = await import("./landMedia"); const { symlink } = await import("node:fs/promises"); diff --git a/common/jobs/networkGuard.ts b/common/jobs/networkGuard.ts @@ -69,8 +69,17 @@ export type NetworkJobOpts = { // The job's replay descriptor — what its function runs with. X's job cap // reads its PLAN from it (lib/xJobRule.ts xJobCounts): never a flag. spec?: JobSpec; + // What a job with NO replay descriptor will ask (import-media's page + // address, review 4 B): read by the guards in place of `spec.params`, and + // never recorded (a spec would offer a Retry the kind does not have). + askParams?: Readonly<Record<string, unknown>>; }; +// The params the guards judge a job by: its spec's, else what it will ask. +function planParams(opts: Pick<NetworkJobOpts, "spec" | "askParams">): Record<string, unknown> | undefined { + return (opts.spec?.params ?? opts.askParams) as Record<string, unknown> | undefined; +} + // ── 1. the empty queue ───────────────────────────────────────────────────── export function emptyQueueRefusal(kind: string, queueKey: string): string | null { @@ -124,7 +133,7 @@ async function channelPlatformOf(opts: NetworkJobOpts): Promise<string | null> { // names no URL is fetched from its channel's page, so each such item's // channel is read here (a batch may span channels). export async function jobAsksX(opts: NetworkJobOpts): Promise<boolean> { - const params = opts.spec?.params as Record<string, unknown> | undefined; + const params = planParams(opts); const itemChannelPlatforms: Record<string, string | null> = {}; if (opts.kind === "fetch-windows" && Array.isArray(params?.items)) { for (const raw of params.items as unknown[]) { @@ -389,7 +398,7 @@ export async function xJobGuard(opts: { // spec's params, and whether its channel is a trending library (read here, // never taken from the caller). async function xJobPlanOf(opts: NetworkJobOpts): Promise<XJobPlan> { - const params = opts.spec?.params as Record<string, unknown> | undefined; + const params = planParams(opts); let channel: XJobPlan["channel"]; if (opts.channelSlug) { const read = await readJsonFile(path.join(opts.paths.channelsDir, opts.channelSlug, "config.json")).catch(() => null); diff --git a/common/jobs/pacingGuard.test.ts b/common/jobs/pacingGuard.test.ts @@ -637,6 +637,28 @@ test("S5: an X channel's jobs that ask x.com nothing pass off X's queue; those t await reset(); }); +test("review 4 B: an import of a file by an X page's address is X's on any channel — refused off X's queue; a local file's or another page's passes", async () => { + await reset(); + await writeJar(5 * 60 * 60_000); + for (let i = 0; i < 12; i++) await recordXJobStart(paths, fakeNow); + const yt = "yt-chan-r4"; + await mkdir(path.join(transcripts, "channels", yt), { recursive: true }); + await writeFile(path.join(transcripts, "channels", yt, "config.json"), JSON.stringify({ handling: "transcribe", platform: "youtube", name: yt, url: "https://www.youtube.com/@example" })); + // import-media has no replay descriptor: what it will ask is given to the guards alone. + const g = (slug: string, url?: string) => + guardNetworkJob({ kind: "import-media", queueKey: `import-media:${slug}`, paths, channelSlug: slug, ...(url ? { askParams: { url } } : {}) }); + for (const [slug, url] of [[yt, "https://t.co/AbC"], [yt, "https://x.com/example/status/1"], [SLUGS[0], "https://t.co/AbC"]] as const) { + const r = await g(slug, url); + assert.equal(r.ok, false, `${url} on ${slug}`); + assert.match(r.ok ? "" : r.error, /X jobs run on X's queue only/, `${url} on ${slug}`); + } + for (const [slug, url] of [[yt, "https://www.youtube.com/watch?v=a"], [SLUGS[0], "https://www.youtube.com/watch?v=a"], [SLUGS[0], undefined]] as const) { + const r = await g(slug, url); + assert.equal(r.ok, true, `${url ?? "a local file"} on ${slug}: ${r.ok ? "" : r.error}`); + } + await reset(); +}); + test("review 3 M2/S5: a window job is X's by its URLs on any channel; a URL-less item by its own channel, in a batch across channels", async () => { await reset(); await writeJar(5 * 60 * 60_000); diff --git a/common/jobs/streamCommand.ts b/common/jobs/streamCommand.ts @@ -77,6 +77,9 @@ type CommonOpts = { // When set, recorded on the job (and its meta sidecar) so the job can be // re-launched (retried) later. See common/jobs/jobSpec.ts. spec?: JobSpec; + // What a job with no `spec` will ask, for the network guard alone + // (jobs/networkGuard.ts askParams): never recorded on the job. + askParams?: Readonly<Record<string, unknown>>; // Background jobs queue BEHIND any foreground (default) job on the same // queueKey: a manually-triggered job (sync, manual download) jumps ahead of // queued background work without interrupting a running one. The auto-download diff --git a/common/lib/xJobRule.test.ts b/common/lib/xJobRule.test.ts @@ -209,6 +209,64 @@ test("review 3 S6: a t.co short link is X's", () => { assert.equal(isXUrl("https://twitter.com/a/status/1"), true); }); +test("review 4 C: X's addresses — a trailing dot, X's media hosts, a schemeless address; never a lookalike or a userinfo", () => { + for (const url of [ + "https://x.com./u/status/1", + "https://twitter.com./u/status/1", + "https://t.co./AbC", + "https://video.twimg.com/ext_tw_video/1/pu/vid/720x1280/a.mp4", + "https://pbs.twimg.com/media/abc.jpg", + "https://twimg.com/x", + "x.com/u/status/1", + "twitter.com/u/status/1", + "mobile.twitter.com/u/status/1", + "t.co/AbC", + "video.twimg.com/a.mp4", + "X.COM/u/status/1", + "//x.com/u/status/1", + " https://x.com/u/status/1 ", + "HTTPS://X.COM/u/status/1", + "https://mobile.TWITTER.com/u/status/1", + "https://evil.example@x.com/u/status/1", + "https://x.com:443/u/status/1", + "x.com:443/u/status/1", + ]) { + assert.equal(isXUrl(url), true, url); + } + for (const url of [ + "https://x.com@evil.example/u/status/1", + "https://x.com.evil.example/u/status/1", + "https://evilx.com/u", + "https://nottwitter.com/u", + "https://twitter.com.evil.example/u", + "https://nottwimg.com/a.mp4", + "https://twimg.com.evil.example/a.mp4", + "https://t.co.evil.example/a", + "evil.example/x.com/u/status/1", + "x.com.evil.example/u", + "nottwitter.com/u", + "mailto:someone@example.org", + "javascript:alert(1)", + "/u/status/1", + "x", + ]) { + assert.equal(isXUrl(url), false, url); + } +}); + +test("review 4 B: an import of a file by its page is X's by that page's address, on any channel", () => { + const im = (url: string | undefined, channelPlatform: string, queueKey = "import-media:c") => + xJobAsksX({ kind: "import-media", queueKey, channelPlatform, ...(url !== undefined ? { params: { url } } : {}) }); + for (const url of ["https://x.com/u/status/1", "https://t.co/AbC", "https://twitter.com/u/status/1"]) { + assert.equal(im(url, "youtube"), true, `${url} on a YouTube channel`); + assert.equal(im(url, "twitter"), true, `${url} on an X channel`); + } + assert.equal(im("https://www.youtube.com/watch?v=a", "twitter"), false, "another platform's page, on an X channel"); + assert.equal(im("https://rumble.com/v-a.html", "rumble"), false); + assert.equal(im(undefined, "twitter"), false, "a local file with no page"); + assert.equal(im("https://x.com/u/status/1", "youtube", "platform:twitter"), true, "on X's queue"); +}); + test("review 3 S4: a start in the future counts in both windows, and never moves the windows off the real starts", () => { const real = Array.from({ length: 11 }, (_, i) => T0 + i * MIN); const future = T0 + 365 * 24 * 60 * MIN; diff --git a/common/lib/xJobRule.ts b/common/lib/xJobRule.ts @@ -33,7 +33,6 @@ // (lib/jobContext.ts). The count's file is jobs/xJobCap.ts. import { currentJobContext } from "./jobContext"; -import { detectPlatform } from "./platform"; export const X_QUEUE_KEY = "platform:twitter"; @@ -191,29 +190,42 @@ export const X_CLIPS_QUEUE_KEY = "clips:twitter"; // Windows cut from saved containers (lib/queueKeys.ts SAVED_VIDEO_CLIP_QUEUE). const SAVED_VIDEO_CLIP_QUEUE = "clips:saved-video"; -// IS THIS ADDRESS X'S? x.com and twitter.com (lib/platform.ts detectPlatform) -// and X's short links, t.co (review 3 S6): a t.co link is answered by X, which -// redirects to the post — asking it is asking X. -export function isXUrl(url: unknown): boolean { - if (typeof url !== "string" || !url.trim()) return false; - if (detectPlatform(url.trim()) === "twitter") { - // detectPlatform takes any host ending in "twitter.com": only twitter.com - // itself and its subdomains are X's. - try { - const host = new URL(url.trim()).hostname.toLowerCase(); - return host === "x.com" || host.endsWith(".x.com") || host === "twitter.com" || host.endsWith(".twitter.com"); - } catch { - return false; - } - } +// IS THIS ADDRESS X'S? x.com and twitter.com and their subdomains, X's short +// links (t.co: X answers and redirects to the post — asking it is asking X; +// review 3 S6), and X's media hosts (twimg.com: video., pbs. and the rest; +// review 4 C). By the HOST alone, lowercased and without a trailing dot: a +// host that merely ends in "twitter.com" (nottwitter.com) or begins with an X +// host (x.com.example.org) is not X's, and neither is a userinfo that reads +// like one (https://x.com@example.org/). An address written without a scheme +// is X's when its first segment is an X host ("x.com/u/status/1"). +const X_HOSTS: ReadonlyArray<string> = ["x.com", "twitter.com", "t.co", "twimg.com"]; + +function hostOf(raw: string): string | null { + const s = raw.trim(); + // A scheme with an authority (https://…); a scheme-relative //host/…; a + // scheme with none (mailto:, javascript:), which names no host; else no + // scheme at all — the first segment is the host (a port after it allowed). + const href = /^[a-z][a-z0-9+.-]*:\/\//i.test(s) + ? s + : s.startsWith("//") + ? `https:${s}` + : /^[a-z][a-z0-9+.-]*:(?!\d)/i.test(s) + ? s + : `https://${s}`; try { - const host = new URL(url.trim()).hostname.toLowerCase(); - return host === "t.co" || host.endsWith(".t.co"); + const host = new URL(href).hostname.toLowerCase().replace(/\.+$/, ""); + return host || null; } catch { - return false; + return null; } } +export function isXUrl(url: unknown): boolean { + if (typeof url !== "string" || !url.trim()) return false; + const host = hostOf(url); + return host !== null && X_HOSTS.some((h) => host === h || host.endsWith(`.${h}`)); +} + // The network job as queued: its kind, its queue, its channel's platform and // its spec's params. `itemChannelPlatforms`: for a batch of windows, the // platform of each item's channel (by slug), read by the caller — an item @@ -247,7 +259,7 @@ function windowAsks(plan: XAskPlan): Array<{ url?: string; slug?: string }> { // DOES THIS NETWORK JOB ASK x.com? It does on X's queue (or X's old clip // queue), whatever its kind. Then: -// import-one asks its URL's host (isXUrl: x.com, twitter.com, t.co); +// import-one asks its URL's host (isXUrl: x.com, twitter.com, t.co, twimg.com); // fetch-window(s) ask the URLs of their windows (review 3 M2), ON ANY // CHANNEL: an x.com page is X's wherever it is filed. A // window that names no URL is fetched from its @@ -259,14 +271,17 @@ function windowAsks(plan: XAskPlan): Array<{ url?: string; slug?: string }> { // controller/fetchWindows.ts); // fetch-copies archive.org (its controller refuses an X address); // transcribe-one the transcription worker, over a file on disk; -// import-media on its own queue (`import-media:<slug>`): a local -// file (an import of an x.com URL is queued on X's); +// import-media asks its page's address when it names one +// (`params.url`, isXUrl — review 4 B), on any channel +// and queue; with none, on its own queue +// (`import-media:<slug>`), a local file; // import-archive-org archive.org. // Everything else an X channel queues asks X, and is refused off X's queue. export function xJobAsksX(plan: XAskPlan): boolean { if (plan.queueKey === X_QUEUE_KEY || plan.queueKey === X_CLIPS_QUEUE_KEY) return true; if (plan.kind === "import-one" && typeof plan.params?.url === "string") return isXUrl(plan.params.url); if (plan.kind === "transcribe-one") return false; + if (plan.kind === "import-media" && typeof plan.params?.url === "string" && plan.params.url.trim()) return isXUrl(plan.params.url); if (isWindowKind(plan.kind)) { if (plan.queueKey === SAVED_VIDEO_CLIP_QUEUE) return false; return windowAsks(plan).some((w) => {