import type { JobRecord } from "./registry"; // THE CLIP-WINDOW JOB ALREADY ON ITS WAY (release 19, A5). // // A tool that asks for a window and does not get it at once asks again — umtool's // fetch-via-editor after a timeout, an agent retrying, the same manifest sent // twice — and every ask used to queue another job for the same seconds of the // same video: the second waited behind the first, found nothing to fetch when // it ran, and meanwhile both sat in the platform's queue in front of real work. // The disk cache (`findContainingClipWindow`) only answers once a file exists; // this answers the window that is still being fetched. // // A request is IN FLIGHT when a queued or running job will produce a file that // covers it: a single `fetch-window` job for the same channel and video whose // window CONTAINS the asked one (the cache accepts a wider file, so this does // too), or a `fetch-windows` batch with such an item. The source height cap // must match: a window capped at 480 is not an answer to a request for 720. // Read off the job's replay spec — the request it was started with — so a // record without one (an old job) is never a match. export type WantedWindow = { slug: string; videoId: string; from: number; to: number; maxHeight?: number; }; export type WindowInFlight = { jobId: string; kind: "fetch-window" | "fetch-windows"; status: "queued" | "running"; // The window the job will write, which contains the asked one. from: number; to: number; }; const num = (v: unknown): number | undefined => typeof v === "number" && Number.isFinite(v) ? v : undefined; export function windowInFlight( records: readonly JobRecord[], want: WantedWindow, ): WindowInFlight | null { for (const r of records) { if (r.status !== "queued" && r.status !== "running") continue; const p = (r.spec?.params ?? {}) as Record; if (num(p.maxHeight) !== want.maxHeight) continue; const covers = (from: number | undefined, to: number | undefined) => from !== undefined && to !== undefined && from <= want.from && to >= want.to; if (r.kind === "fetch-window") { if (r.spec?.slug !== want.slug || p.videoId !== want.videoId) continue; const from = num(p.from); const to = num(p.to); if (covers(from, to)) { return { jobId: r.id, kind: "fetch-window", status: r.status, from: from!, to: to! }; } } else if (r.kind === "fetch-windows" && Array.isArray(p.items)) { for (const raw of p.items) { const it = (raw ?? {}) as Record; if (it.slug !== want.slug || it.id !== want.videoId) continue; const from = num(it.from); const to = num(it.to); if (covers(from, to)) { return { jobId: r.id, kind: "fetch-windows", status: r.status, from: from!, to: to! }; } } } } return null; }