// settings.sourceGroups — channels that archive the SAME recordings from // different sources (lane cross-source-dupes, slice 1). A group is an ORDERED // list of channel slugs: the first is the preferred source, the rest fall back // in order. A recording held on two members is transcribed and given speakers // once; the other copy stays on disk as the fallback. // // EMPTY BY DEFAULT, and with no group the lanes are byte for byte what they // were: nothing here is read by a lane until a group names channels. No // channel is named in code; a group is a settings patch on the machine that // runs it (`pnpm ops settings`). // // Pure: the types, the defaults, the sanitizer settingsSchema.ts runs the // block through, and the per-field docs SETTINGS.md renders. import type { FieldDocs } from "../fieldDocs"; export type SourceGroup = { id: string; label: string; // ORDER IS PREFERENCE: the first slug is the preferred source. A channel is // in one group at most. channels: string[]; // Whether the transcription and Speakers lanes act on this group's pairs. // false = the scan and the report only. lanes: boolean; // How long a held copy waits (days, from when the scan first saw the pair), // after which it is released and its lane does the work, whatever the other // copy's state. 0 = never held. waitDays: number; // Whether the other copy's PLATFORM captions count as the transcription // done (a YouTube ASR track still does not when the transcription lane // would replace it). false: only a transcript of our own engine counts, so // a fallback copy holding the audio still gets one. captionsCountAsDone: boolean; // The metadata stage: recorded dates at most this many days apart … dateWindowDays: number; // … and runtimes within max(seconds, ratio × the longer runtime). durationToleranceSeconds: number; durationToleranceRatio: number; // Also pair copies inside one member (a channel that re-uploaded its own // streams). Report only: the lanes never act on a pair inside one channel. withinChannels: boolean; }; export const SOURCE_GROUP_DEFAULT_WAIT_DAYS = 7; export const SOURCE_GROUP_MAX_WAIT_DAYS = 365; export const SOURCE_GROUP_DEFAULT_DATE_WINDOW_DAYS = 1; export const SOURCE_GROUP_MAX_DATE_WINDOW_DAYS = 7; export const SOURCE_GROUP_DEFAULT_DURATION_SECONDS = 120; export const SOURCE_GROUP_DEFAULT_DURATION_RATIO = 0.02; // A group id is a file name (`transcripts/copies/.json`): a lowercase slug. export const SOURCE_GROUP_ID_RE = /^[a-z0-9][a-z0-9-]{0,63}$/; // A channel slug as the channels tree names it (the citations' segment rule). const CHANNEL_SLUG_RE = /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/; export const SOURCE_GROUP_FIELD_DOCS: FieldDocs = { id: "The group's id: a lowercase slug (`a-z`, `0-9`, `-`, at most 64), the name its scan is kept under " + "(`transcripts/copies/.json`, never published). Omitted: made from `label`. An entry with neither is " + "dropped, and so is a second entry with an id already used.", label: "What the group is called in the report. Omitted: the id.", channels: "The member channels' slugs, IN ORDER OF PREFERENCE: the first is the preferred source and gets new work; " + "the rest are fallbacks, kept, synced and downloaded exactly as before. A slug listed twice, or already in an " + "earlier group, is dropped; a group left with fewer than two channels is dropped. A slug this corpus does not " + "hold is ignored when the group is used.", lanes: "Whether the transcription and Speakers lanes act on the group's pairs: skip a CONFIRMED copy (by text and " + "runtime, or by `copies-confirm`) whose other copy has the work done, and hold a metadata-only one or one " + "waiting on its preferred copy for at most `waitDays`. A part-of or overlapping pair is reported, never acted " + "on. false = the scan and the report only. Default true.", waitDays: "How long a held copy waits, in days from when the scan FIRST SAW the pair (not from when a lane held it: a " + "pair older than this never holds; re-ordering the group makes new pairs and a new wait). After it the copy " + "is released and its lane does the work, whatever the other copy's state — a metadata-only match is never " + "held longer, even when the other copy is done (`copies-confirm` makes it a skip). 0 = no hold at all. " + "Clamped to [0, 365]; default 7.", captionsCountAsDone: "Whether the other copy's platform captions (a VTT track) count as its transcription done. Default false: " + "only a transcript of our own engine (`transcript.json`) counts, so the fallback copy still gets its own when " + "the preferred copy has captions only. With true a caption track counts, except a YouTube ASR track while the " + "transcription lane would replace it (`replaceAutoSubs`, or a rule on the auto-captions buckets). Either way " + "a transcript counts only with real words for its length (at least max(50, 10 per minute of runtime), " + "bracketed non-speech like [Music] left out).", dateWindowDays: "The metadata stage: two videos are candidates when their recorded dates (the channel's title rule, else the " + "upload date) are at most this many days apart. Clamped to [0, 7]; default 1.", durationToleranceSeconds: "The metadata stage: runtimes agree within max(this, `durationToleranceRatio` × the longer). Clamped to " + "[0, 3600]; default 120.", durationToleranceRatio: "See `durationToleranceSeconds`. Clamped to [0, 0.5]; default 0.02.", withinChannels: "Also pair copies inside one member channel (one channel holding two upload series of the same streams). " + "Report only: the lanes never act on a pair inside one channel. Default false.", }; function num(value: unknown, fallback: number, min: number, max: number, integer: boolean): number { const n = typeof value === "number" ? value : typeof value === "string" && value.trim() ? Number(value) : NaN; if (!Number.isFinite(n)) return fallback; const v = Math.min(max, Math.max(min, n)); return integer ? Math.round(v) : v; } /** A lowercase slug made from free text: "Stream copies!" → "stream-copies". */ export function sourceGroupIdFrom(text: string): string { return text .normalize("NFKD") .replace(/[̀-ͯ]/g, "") .toLowerCase() .replace(/[^a-z0-9]+/g, "-") .replace(/^-+|-+$/g, "") .slice(0, 64) .replace(/-+$/, ""); } /** * The block, coerced. Never throws. A channel is kept in the FIRST group that * names it; a duplicate slug inside a group is dropped; a group with fewer than * two channels left, or no usable id, or an id already used, is dropped. */ export function sanitizeSourceGroups(value: unknown): SourceGroup[] { if (!Array.isArray(value)) return []; const out: SourceGroup[] = []; const ids = new Set(); const claimed = new Set(); for (const raw of value) { if (!raw || typeof raw !== "object" || Array.isArray(raw)) continue; const r = raw as Record; const rawId = typeof r.id === "string" ? r.id.trim() : ""; const rawLabel = typeof r.label === "string" ? r.label.trim() : ""; const id = SOURCE_GROUP_ID_RE.test(rawId) ? rawId : sourceGroupIdFrom(rawId || rawLabel); if (!SOURCE_GROUP_ID_RE.test(id) || ids.has(id)) continue; const channels: string[] = []; if (Array.isArray(r.channels)) { for (const c of r.channels) { if (typeof c !== "string") continue; const slug = c.trim(); if (!slug || slug === ".." || !CHANNEL_SLUG_RE.test(slug)) continue; if (channels.includes(slug) || claimed.has(slug)) continue; channels.push(slug); } } if (channels.length < 2) continue; ids.add(id); for (const c of channels) claimed.add(c); out.push({ id, label: rawLabel || id, channels, lanes: r.lanes === false ? false : true, waitDays: num(r.waitDays, SOURCE_GROUP_DEFAULT_WAIT_DAYS, 0, SOURCE_GROUP_MAX_WAIT_DAYS, false), captionsCountAsDone: r.captionsCountAsDone === true, dateWindowDays: num(r.dateWindowDays, SOURCE_GROUP_DEFAULT_DATE_WINDOW_DAYS, 0, SOURCE_GROUP_MAX_DATE_WINDOW_DAYS, true), durationToleranceSeconds: num(r.durationToleranceSeconds, SOURCE_GROUP_DEFAULT_DURATION_SECONDS, 0, 3600, false), durationToleranceRatio: num(r.durationToleranceRatio, SOURCE_GROUP_DEFAULT_DURATION_RATIO, 0, 0.5, false), withinChannels: r.withinChannels === true, }); } return out; } /** The rank of `slug` in its group (0 = preferred), or -1 when it is not a member. */ export function preferenceRank(group: Pick, slug: string): number { return group.channels.indexOf(slug); }