// WHAT BECAME OF A RECORDING'S SOURCE, AND WHAT IT WAS AT FIRST (lane source-status). // // A report video clips streams a streamer later privates, deletes or moves behind a // membership. Its source line says so ("… · now private (public when streamed)"), and // the line may only say what the archive's own records prove. This is the one // reading of those records: pure, plain JS (umtool's bins run under bare node), fed // the parsed files of one video directory by sourceStatus-server.mjs. // // NOW is the effective availability every other reader uses // (availability-server.ts resolveEffectiveAvailabilityObserved, re-stated here // because umtool cannot import TypeScript, and kept to its rules: the // download-outcome's last class wins only when its finishedAt is LATER than the // record's checkedAt, both parseable, and only an outcome of // coerceDownloadOutcome's shape counts). `since` is the first time the archive // saw that state (the trailing run of history entries that agree with it), // `asOf` the newest observation behind it. Two rules on top, both of which draw // NOTHING (`now: null`, the reason kept as an evidence item) rather than a claim // the records do not make: // // DELETED only from the channel's Check availability: availability.json's own // top level written by a check (`checkedBy: "check"`, or a legacy record with // no checkedBy whose trailing run holds a check). A download's class is // parseUnavailableFromStderr over its stderr, and any bare "Video unavailable" // (a region block, a transient answer) reads "deleted" there. // THE RECORDS DISAGREE: a first-hand public or unlisted reading NEWER than the // state's asOf -- the metadata's own field at its epoch (a successful persist // rewrites it and writes no availability entry), a check/fetch history entry, // or a download that succeeded without cookies -- means the source may be // back. `asOf` is then that newer reading's time. // // The published archive's "deleted" is the same effective reading // (buildIndex), so umtool source-status does not draw it either. // // WAS PUBLIC is said only with EVIDENCE, each item kept with its time: // // metadata metadata.info.json's OWN `availability: "public"` field, at the time // the file was saved (`epoch`), and every earlier state of it that // metadata.history.json lets us rebuild. The field must be there. // fetch an availability.json history entry of "public" written by a // one-video fetch: a metadata refresh records only a field that is // there (refreshVideoMetadata), so it is the platform's own word. // check the same written by a real probe -- but checkAvailability reads a // MISSING field (an extractor that sets none) or unparseable output // as "public", and the record does not say which it was. So a check's // public counts only when this video's metadata shows its extractor // reports the field (some snapshot carries a string `availability`). // The limit left: a check whose own output had no parseable JSON, // on a video whose metadata has the field, still reads public. // // "When streamed": a metadata snapshot that was itself taken while the stream was // live (`live_status: "is_live"`), or a check/fetch observation inside the stream's // span (release_timestamp .. + duration, else the live recording's end). // // NOT EVIDENCE, and recorded as such (`counts: false`): // // backfill a copy of metadata's field that turns a MISSING field into // "public" (availabilityFromJsonField): the metadata is the // evidence, not the copy. // download a download attempt's class in the history: stderr read // loosely, not the platform's field. // live recording the recorder passes the channel's cookies when its policy says // so and does not record whether it did: a member's login records a // members-only stream as well as a public one. // a download without cookies watchable without a login: public OR unlisted. // // Client-safe: no node: imports. /** The states a viewer cannot watch at the platform (availability.ts NOT_PUBLIC_STATES, by availability). */ export const NOT_PUBLIC = Object.freeze(["private", "members_only", "deleted"]); /** availability.ts AVAILABILITY_VALUES: every value a stamp's `now` may hold, beside null. */ export const AVAILABILITIES = Object.freeze(["public", "unlisted", "private", "members_only", "needs_auth", "deleted", "error"]); const AVAILABLE = new Set(AVAILABILITIES); /** downloadOutcome.ts DOWNLOAD_OUTCOME_STATUS_VALUES (coerceDownloadOutcome's shape check). */ const OUTCOME_STATUSES = new Set([ "ok", "ok-with-cookies", "ok-auto-transcribed", "ok-audio-checked", "failed", "failed-corrupt-source", "corrupt-full-source", "failed-short-audio", "skipped-filtered", "chat-only", ]); /** The metadata field's words that mean anyone could watch it (yt-dlp's, availabilityFromJsonField). */ const OPEN_FIELD = new Map([["public", "public"], ["unlisted", "unlisted"], ["public_unlisted", "unlisted"]]); const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const msOf = (iso) => { const t = typeof iso === "string" ? Date.parse(iso) : NaN; return Number.isFinite(t) ? t : null; }; const isoOf = (ms) => (ms === null ? null : new Date(ms).toISOString()); /** `a` strictly after `b`, both parseable (a Date comparison with NaN is false: availability-server.ts). */ const later = (a, b) => { const x = msOf(a); const y = msOf(b); return x !== null && y !== null && x > y; }; /** availability.json as availability-server.ts coerceAvailability accepts it, else null. */ function explicitOf(rec) { return isObj(rec) && AVAILABLE.has(rec.availability) && typeof rec.checkedAt === "string" ? rec : null; } /** download-outcome.json as downloadOutcome-server.ts coerceDownloadOutcome accepts it, else null. */ function outcomeRecordOf(rec) { return isObj(rec) && OUTCOME_STATUSES.has(rec.status) && typeof rec.videoId === "string" && typeof rec.startedAt === "string" && typeof rec.finishedAt === "string" && Array.isArray(rec.attempts) ? rec : null; } /** The outcome's last attempt's class, as loadDownloadOutcomeAvailability reads it. */ function outcomeOf(rec) { const cls = rec?.attempts.at(-1)?.availabilityClass; return AVAILABLE.has(cls) ? { availability: cls, finishedAt: rec.finishedAt } : null; } /** A download that succeeded on its last attempt without cookies: anyone could watch it then. */ function cookielessSuccess(rec) { return Boolean(rec && rec.status.startsWith("ok") && rec.status !== "ok-with-cookies" && rec.attempts.at(-1)?.usedCookies === false); } function firstOfTrailingRun(record, availability) { const history = Array.isArray(record.history) ? record.history : []; let since = null; for (let i = history.length - 1; i >= 0; i--) { if (history[i]?.availability !== availability) break; since = history[i].observedAt ?? null; } return since; } /** * The metadata's states over time, oldest first: `{ at, availability, liveStatus }`, * `availability` the file's own field (undefined when it was absent). The current * file is the last; metadata.history.json's entries, walked back, give the ones * before (an entry's `changed[k].from`, `removed[k]`, or absent for `added[k]`). */ export function metadataSnapshots(metadata, history) { if (!isObj(metadata)) return []; const KEYS = ["availability", "live_status"]; const cur = Object.fromEntries(KEYS.map((k) => [k, metadata[k]])); const epochMs = Number.isFinite(metadata.epoch) ? metadata.epoch * 1000 : null; const entries = isObj(history) && Array.isArray(history.entries) ? history.entries.filter(isObj) : []; const shots = []; let values = { ...cur }; let at = epochMs ?? msOf(entries.at(-1)?.at); shots.push({ at, ...values }); for (let i = entries.length - 1; i >= 0; i--) { const e = entries[i]; const before = { ...values }; for (const k of KEYS) { if (isObj(e.changed) && isObj(e.changed[k]) && "from" in e.changed[k]) before[k] = e.changed[k].from; else if (isObj(e.added) && k in e.added) before[k] = undefined; else if (isObj(e.removed) && k in e.removed) before[k] = e.removed[k]; } values = before; at = msOf(e.from?.at); shots.push({ at, ...values }); } return shots .reverse() .map((s) => ({ at: isoOf(s.at), availability: s.availability, liveStatus: s.live_status })); } /** The stream's span, ms, when the records say it was a live: start, end (either may be null). */ function liveSpan(metadata, liveRecording) { const start = isObj(metadata) && Number.isFinite(metadata.release_timestamp) ? metadata.release_timestamp * 1000 : null; let end = start !== null && Number.isFinite(metadata?.duration) ? start + metadata.duration * 1000 : null; if (end === null && isObj(liveRecording)) end = msOf(liveRecording.endedAt); return { start, end }; } /** Was this deletion read by the channel's Check availability (see the header)? */ function removalByCheck(explicit) { if (explicit.availability !== "deleted") return false; if (explicit.checkedBy === "check") return true; if (explicit.checkedBy !== undefined) return false; const history = Array.isArray(explicit.history) ? explicit.history : []; for (let i = history.length - 1; i >= 0 && history[i]?.availability === "deleted"; i--) { if (history[i].source === "check") return true; } return false; } /** * One video's source status from its parsed records, or null when the archive holds * no availability reading for it at all (no record is no state: never "gone"). * `now` is null when the records hold a state the line may not say (a removal not * read by a check, or a newer reading that disagrees): the reason is an evidence * item with `counts: false`. * * @param {{ availability?: any, outcome?: any, metadata?: any, metadataHistory?: any, liveRecording?: any }} records * @returns {null | { now: string|null, since: string|null, asOf: string|null, first: "public"|null, firstAt: string|null, * firstWhen: "live"|"other"|null, evidence: Array> }} */ export function deriveSourceStatus({ availability = null, outcome = null, metadata = null, metadataHistory = null, liveRecording = null } = {}) { const explicit = explicitOf(availability); const outRec = outcomeRecordOf(outcome); const out = outcomeOf(outRec); const fromOutcome = Boolean(out && (!explicit || later(out.finishedAt, explicit.checkedAt))); /** @type {string|null} */ let now; let since; let asOf; if (fromOutcome) { now = out.availability; since = explicit ? firstOfTrailingRun(explicit, now) : null; asOf = out.finishedAt; } else if (explicit) { now = explicit.availability; since = firstOfTrailingRun(explicit, now); asOf = explicit.checkedAt; } else { return null; } const evidence = []; const span = liveSpan(metadata, liveRecording); const inLive = (ms) => ms !== null && span.start !== null && span.end !== null && ms >= span.start && ms <= span.end; const shots = metadataSnapshots(metadata, metadataHistory); // Does this video's extractor report the field at all (a check's "public" is then its word, not a default)? const fieldReported = shots.some((x) => typeof x.availability === "string"); // A REMOVAL only from a check. if (now === "deleted" && (fromOutcome || !removalByCheck(explicit))) { evidence.push({ kind: fromOutcome ? "download" : (explicit.checkedBy ?? "record"), at: asOf, availability: "deleted", counts: false, why: fromOutcome ? "a download's class: any \"Video unavailable\" (a region block, a transient answer) reads deleted -- a removal is read only by the channel's Check availability" : "not written by the channel's Check availability, the one reader of a removal", }); now = null; since = null; } // THE RECORDS DISAGREE: a first-hand open reading newer than the state. if (NOT_PUBLIC.includes(now)) { const open = []; for (const x of shots) { const word = typeof x.availability === "string" ? OPEN_FIELD.get(x.availability) : undefined; if (word && msOf(x.at) !== null) open.push({ from: "metadata", at: x.at, availability: word }); } for (const h of Array.isArray(explicit?.history) ? explicit.history : []) { if (isObj(h) && (h.source === "check" || h.source === "fetch") && (h.availability === "public" || h.availability === "unlisted") && msOf(h.observedAt) !== null) { open.push({ from: h.source, at: h.observedAt, availability: h.availability }); } } if (cookielessSuccess(outRec) && msOf(outRec.finishedAt) !== null) open.push({ from: "download", at: outRec.finishedAt, availability: "public or unlisted" }); const asOfMs = msOf(asOf); const newer = open.filter((o) => asOfMs === null || msOf(o.at) > asOfMs).sort((p, q) => msOf(q.at) - msOf(p.at))[0]; if (newer) { evidence.push({ kind: "disagree", at: newer.at, availability: newer.availability, from: newer.from, counts: false, why: `a ${newer.from} reading of ${newer.availability} ${asOfMs === null ? "beside an undated" : "newer than the"} ${now} one: the records disagree, so nothing is drawn`, }); now = null; since = null; asOf = isoOf(msOf(newer.at)); } } for (const x of shots) { if (x.availability !== "public") continue; evidence.push({ kind: "metadata", at: x.at, availability: "public", ...(x.liveStatus ? { liveStatus: x.liveStatus } : {}), live: x.liveStatus === "is_live", counts: true }); } for (const h of Array.isArray(explicit?.history) ? explicit.history : []) { if (!isObj(h) || h.availability !== "public") continue; const ms = msOf(h.observedAt); const at = h.observedAt ?? null; if (h.source === "fetch" || (h.source === "check" && fieldReported)) { evidence.push({ kind: h.source, at, availability: "public", live: inLive(ms), counts: true }); } else if (h.source === "check") { evidence.push({ kind: "check", at, availability: "public", counts: false, why: "a check reads a missing field as public, and this video's metadata carries no availability field" }); } else if (h.source === "download") { evidence.push({ kind: "download", at, availability: "public", counts: false, why: "a download attempt's class (its stderr read loosely), not the platform's field" }); } else { evidence.push({ kind: h.source ?? "backfill", at, availability: "public", counts: false, why: "a copy of the metadata's field (a missing field reads public)" }); } } if (isObj(liveRecording)) { evidence.push({ kind: "live-recording", at: typeof liveRecording.startedAt === "string" ? liveRecording.startedAt : null, counts: false, why: "recorded live, possibly with the channel's cookies (not recorded): a members-only stream records too" }); } if (cookielessSuccess(outRec)) { evidence.push({ kind: "download", at: outRec.finishedAt, counts: false, why: "downloaded without cookies: public or unlisted" }); } // Evidence of the FIRST state precedes the state it is first to: an observation // after `since` (when both are dated) says nothing about how it started. const sinceMs = msOf(since); const counting = evidence.filter((e) => e.counts && (sinceMs === null || msOf(e.at) === null || msOf(e.at) <= sinceMs)); const first = NOT_PUBLIC.includes(now) && counting.length ? "public" : null; const dated = counting.map((e) => msOf(e.at)).filter((t) => t !== null); return { now, since: since ?? null, asOf: asOf ?? null, first, firstAt: first && dated.length ? isoOf(Math.min(...dated)) : null, firstWhen: first ? (counting.some((e) => e.live) ? "live" : "other") : null, evidence, }; }