commit 8790ed5473e9a6bec685e7f38fccfcdd3380d76d
parent a24777f3764cbf1a12f6a3590829a0b45d0073f4
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date: Sun, 1 Nov 2026 23:12:47 -0500
common/lib/research: the release-38 case-pipeline contract (lane 0) -- caseTypes.ts: the case's pipeline.json definition beside umtool's board case.json (subjects, scope, eras with term families, reader groups, private names as sha256 only, reader prompts, status), the stage files, coverage rows, candidate windows, batches (no line over 200), finding rows with the quote check and verify statuses including `quarantined`, leads (archive | watch | never-archive), date checks, and the guards; privateNames-server.ts: the hash and a count-only hit check over every word and 2-3-word phrase. Lane E's video-assist ops are already lib/report/videoAssistOps.d.mts, so there is no videoAssistTypes.ts
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Diffstat:
3 files changed, 418 insertions(+), 0 deletions(-)
diff --git a/common/lib/research/caseTypes.test.ts b/common/lib/research/caseTypes.test.ts
@@ -0,0 +1,132 @@
+// The research-case pipeline contract (lib/research/caseTypes.ts): a whole case typed end to end, the guards, the
+// statuses the lanes rely on (`quarantined`), and the private-name gate's hashing (never the term in a file).
+//
+// Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/research/caseTypes.test.ts
+
+import test from "node:test";
+import assert from "node:assert/strict";
+import {
+ BATCH_LINE_MAX,
+ CASE_FILES,
+ CASE_PIPELINE_FORMAT,
+ CASE_PIPELINE_VERSION,
+ type CaseBatch,
+ type CaseDefinition,
+ type CaseLead,
+ type CandidateWindow,
+ type FindingRow,
+ isCaseStatus,
+ isFindingBy,
+ isLeadDisposition,
+ isRelevance,
+ isVerifyStatus,
+ privateNameCandidates,
+ VERIFY_STATUSES,
+} from "./caseTypes";
+import { privateNameHash, privateNameHits } from "./privateNames-server";
+
+const definition = {
+ format: CASE_PIPELINE_FORMAT,
+ version: CASE_PIPELINE_VERSION,
+ case: "example-case",
+ subjects: [{ name: "Example Subject", aliases: ["ex", "the subject"] }],
+ scope: { channels: ["example"], groups: [], sites: ["jeralyzer"] },
+ eras: [{ id: "early", from: "2014-01-01", to: "2016-12-31", families: { name: "\\bexample\\b" } }],
+ groups: [
+ { id: "own", label: "The subject's own", channels: "subject", era: "early" },
+ { id: "others", label: "Coverage", channels: "coverage" },
+ ],
+ privateNames: { sha256: [privateNameHash("Jane Q Private")], note: "a private person named in a forum thread" },
+ readerPrompts: { common: "Quote verbatim.", own: "The subject's own words." },
+ status: "reading",
+ createdAt: "2026-11-01T00:00:00Z",
+ updatedAt: "2026-11-01T00:00:00Z",
+} satisfies CaseDefinition;
+
+const window = {
+ key: "example/abc123@120",
+ slug: "example",
+ id: "abc123",
+ date: "20150301",
+ title: "A stream",
+ from: 120,
+ to: 180,
+ era: "early",
+ group: "own",
+ track: "en-orig",
+ hits: [{ t: 131.5, family: "name", match: "example" }],
+ text: "and then the example came up",
+} satisfies CandidateWindow;
+
+const batch = {
+ id: "own-1-001",
+ group: "own",
+ round: 1,
+ windows: [window.key],
+ chars: window.text.length,
+ file: `${CASE_FILES.batchDir}/own-1-001.txt`,
+ state: "read",
+} satisfies CaseBatch;
+
+const finding = {
+ row: "f1",
+ batch: batch.id,
+ slug: window.slug,
+ id: window.id,
+ date: window.date,
+ title: window.title,
+ t: 131.5,
+ url: "https://example.invalid/v/abc123?t=131",
+ category: "first mention",
+ relevance: 3,
+ quote: "the example came up",
+ summary: "The subject names it on stream.",
+ speaker: "Example Subject",
+ confidence: "high",
+ round: 1,
+ by: "local",
+ quoteCheck: "found",
+ verify: { status: "quarantined", at: "2026-11-01T01:00:00Z", how: "hand", note: "forum-only" },
+ evidence: [{ kind: "forum-attachment", post: "12", url: "https://forum.invalid/t/1#12" }],
+} satisfies FindingRow;
+
+const lead = {
+ key: "x:@someone",
+ url: "https://x.com/someone",
+ platform: "x",
+ count: 4,
+ disposition: "never-archive",
+} satisfies CaseLead;
+
+test("a whole case types end to end and round-trips through JSON", () => {
+ for (const value of [definition, window, batch, finding, lead]) {
+ assert.deepEqual(JSON.parse(JSON.stringify(value)), value);
+ }
+ assert.equal(batch.file.startsWith(`${CASE_FILES.batchDir}/`), true);
+ assert.ok(BATCH_LINE_MAX <= 200, "a batch line over 200 chars is truncated by readers' file tools");
+});
+
+test("the guards accept the contract's values and refuse others", () => {
+ assert.ok(VERIFY_STATUSES.includes("quarantined"));
+ for (const s of VERIFY_STATUSES) assert.ok(isVerifyStatus(s));
+ assert.equal(isVerifyStatus("ok"), false, "the board's QuoteVerify states are not a finding's");
+ assert.ok(isLeadDisposition("never-archive"));
+ assert.equal(isLeadDisposition("ignore"), false);
+ assert.ok(isFindingBy("hand") && isFindingBy("code"));
+ assert.equal(isFindingBy("agent"), false);
+ assert.ok(isCaseStatus(definition.status));
+ assert.ok(isRelevance(1) && isRelevance(3));
+ assert.equal(isRelevance(0) || isRelevance(4) || isRelevance("2"), false);
+});
+
+test("private names: stored as hashes, matched on words and 2–3-word phrases, reported as a count", () => {
+ const hash = privateNameHash("Jane Q Private");
+ assert.match(hash, /^[0-9a-f]{64}$/);
+ assert.equal(privateNameHash(" jane q PRIVATE "), hash, "case and spacing do not change the hash");
+ assert.equal(JSON.stringify(definition).toLowerCase().includes("jane"), false, "the definition never holds the term");
+
+ assert.deepEqual(privateNameCandidates("Hi, Jane Q."), ["hi", "hi jane", "hi jane q", "jane", "jane q", "q"]);
+ assert.equal(privateNameHits("then Jane Q Private, posted it", definition.privateNames.sha256), 1);
+ assert.equal(privateNameHits("then Jane posted it", definition.privateNames.sha256), 0);
+ assert.equal(privateNameHits("anything", []), 0);
+});
diff --git a/common/lib/research/caseTypes.ts b/common/lib/research/caseTypes.ts
@@ -0,0 +1,270 @@
+// RESEARCH CASES AS A PIPELINE -- the contract (plans/release-38/research-cases-and-assist.md, lane 0).
+// Every r38 lane codes against these shapes: A (coverage, candidates, batches, merge, the privacy gate, date
+// checks), B (the read: by hand, `case-read`, the quote check), C (verify, leads, evidence, polite retries) and D
+// (the MCP's case tools). Types, constants and guards only: no I/O, and safe for a client component. The one hash
+// the gate and the case page must agree on, privateNameHash, is in privateNames-server.ts.
+//
+// A case is umtool's board (umtool/lib/research/cases.mjs, `REPORTS_ROOT/.umtool/research/<case>/case.json`,
+// "umtool-research-case" v1). The pipeline lives BESIDE it in the same directory, never inside case.json: the
+// board's store has one writer and its own version, and every stage below writes a file of its own.
+//
+// <case>/pipeline.json CaseDefinition (subjects, scope, eras, reader groups, private-name hashes)
+// <case>/coverage.json CoverageRow[] (stage 1)
+// <case>/candidates.jsonl CandidateWindow, one per line (stage 2)
+// <case>/batches/index.json CaseBatch[] (stage 3)
+// <case>/batches/<batch>.txt the batch's WRAPPED text: no line over BATCH_LINE_MAX (stage 3)
+// <case>/findings.jsonl FindingRow, one per line, appended (stage 4; verify and merge rewrite whole)
+// <case>/leads.json CaseLead[] (stage 8)
+// <case>/date-checks.json DateCheck[] (stage 9)
+//
+// PRIVATE NAMES ARE NEVER TEXT. A case's private terms are stored as `privateNames.sha256` (privateNameHash of
+// each term, privateNames-server.ts), so no file holds them. The gate hashes every word and every 2- and 3-word phrase of what it checks
+// (privateNameCandidates) and blocks on a match; what it reports is counts and row ids, never the term.
+
+export const CASE_PIPELINE_FORMAT = "archilyzer-case-pipeline";
+export const CASE_PIPELINE_VERSION = 1;
+
+export const CASE_FILES = Object.freeze({
+ definition: "pipeline.json",
+ coverage: "coverage.json",
+ candidates: "candidates.jsonl",
+ batchDir: "batches",
+ batchIndex: "batches/index.json",
+ findings: "findings.jsonl",
+ leads: "leads.json",
+ dateChecks: "date-checks.json",
+} as const);
+
+/** A batch file's longest line. A reader's file tool truncates long lines and drops the densest windows silently. */
+export const BATCH_LINE_MAX = 200;
+/** A finding's quote: verbatim, at most this many words. */
+export const QUOTE_WORDS_MAX = 45;
+/** Merge: two rows on the same video closer than this (seconds) are one finding. */
+export const MERGE_DEDUPE_SECONDS = 20;
+/** Merge: the report's default floor. */
+export const MERGE_MIN_RELEVANCE = 2;
+
+/** What the naming rule replaces a private name with, in every reader prompt and every surface. */
+export const NAME_WITHHELD = "[name withheld]";
+
+// ---------------------------------------------------------------------------------------------------------------
+// The definition
+
+export type CaseSubject = { name: string; aliases: string[] };
+
+/** What the case reads: channel slugs, channel groups, sites. */
+export type CasePipelineScope = { channels: string[]; groups: string[]; sites: string[] };
+
+/** A period of the subject's life with its own vocabulary. `families` maps a family id to a regex SOURCE (flags "iu"). */
+export type CaseEra = { id: string; from: string; to: string; families: Record<string, string> };
+
+/**
+ * A reader group: who reads which windows, with which prompt. `channels` is a list of slugs, or "subject" (the
+ * subjects' own channels) or "coverage" (everyone else in scope, where a mention of a subject at all is the bar).
+ */
+export type ReaderGroup = { id: string; label: string; channels: string[] | "subject" | "coverage"; era?: string };
+
+export type PrivateNames = {
+ /** privateNameHash of each term, lowercase hex. The terms themselves are never stored. */
+ sha256: string[];
+ /** Why these names are withheld, in words that do not contain them. */
+ note: string;
+};
+
+export const CASE_STATUSES = ["defining", "gathering", "reading", "merging", "verifying", "done"] as const;
+export type CaseStatus = (typeof CASE_STATUSES)[number];
+
+export type CaseDefinition = {
+ format: typeof CASE_PIPELINE_FORMAT;
+ version: typeof CASE_PIPELINE_VERSION;
+ /** The umtool case id it sits beside. */
+ case: string;
+ subjects: CaseSubject[];
+ scope: CasePipelineScope;
+ eras: CaseEra[];
+ groups: ReaderGroup[];
+ privateNames: PrivateNames;
+ /** `common` goes before every group's own prompt; keyed by group id otherwise. */
+ readerPrompts: { common: string } & Record<string, string>;
+ status: CaseStatus;
+ createdAt: string;
+ updatedAt: string;
+};
+
+// ---------------------------------------------------------------------------------------------------------------
+// Stage 1: coverage, per channel × year
+
+export type CoverageRow = {
+ channel: string;
+ year: number;
+ listed: number;
+ held: number;
+ transcribed: number;
+ /** Held with audio but no transcript. */
+ untranscribedAudio: number;
+ /** The year contains one of the case's key periods (an era's span). */
+ key?: boolean;
+};
+
+// ---------------------------------------------------------------------------------------------------------------
+// Stage 2: candidate windows
+
+/** The text a window was read from; en-orig is preferred over en, then a whisper/parakeet transcript, then cues. */
+export const WINDOW_TRACKS = ["en-orig", "en", "transcript", "cues"] as const;
+export type WindowTrack = (typeof WINDOW_TRACKS)[number];
+
+export type CandidateHit = { t: number; family: string; match: string };
+
+export type CandidateWindow = {
+ /** `<slug>/<id>@<from>`: stable across rebuilds of the same scan. */
+ key: string;
+ slug: string;
+ id: string;
+ /** YYYYMMDD. */
+ date: string;
+ title: string;
+ from: number;
+ to: number;
+ era: string;
+ group: string;
+ track: WindowTrack;
+ hits: CandidateHit[];
+ /** A family matched the title rather than (or as well as) the words. */
+ titleHit?: boolean;
+ /** The window's caption text after rolling-caption dedupe: what a reader reads and the quote check checks. */
+ text: string;
+};
+
+// ---------------------------------------------------------------------------------------------------------------
+// Stage 3: batches
+
+export type CaseBatch = {
+ /** `<group>-<round>-<nnn>`. */
+ id: string;
+ group: string;
+ round: number;
+ /** CandidateWindow keys, in reading order. */
+ windows: string[];
+ chars: number;
+ /** Relative to the case dir: batches/<id>.txt. */
+ file: string;
+ state: "pending" | "reading" | "read" | "failed";
+};
+
+// ---------------------------------------------------------------------------------------------------------------
+// Stage 4: findings
+
+export type Relevance = 1 | 2 | 3;
+
+/** Who wrote a row: a person's hand on the candidate deck, or a `case-read` route. */
+export const FINDING_BY = ["hand", "api", "local", "code"] as const;
+export type FindingBy = (typeof FINDING_BY)[number];
+
+/**
+ * The quote check: the row's quote, normalised, is in its window's caption text, or it is not. A row whose quote is
+ * not found is KEPT and flagged, never dropped: a garbled caption is still evidence to check.
+ */
+export type QuoteCheck = "found" | "not-found" | "unchecked";
+
+/**
+ * Stage 7. `quarantined`: first seen on a forum and not corroborated by an independent source (operator,
+ * 2026-10-20: forum joke culture). It has its own report section.
+ */
+export const VERIFY_STATUSES = ["unverified", "verified", "corrected", "disputed", "quarantined"] as const;
+export type VerifyStatus = (typeof VERIFY_STATUSES)[number];
+
+export type FindingVerify = {
+ status: VerifyStatus;
+ at: string;
+ /** "audio" when an audio check (a window transcribed and diffed) decided it. */
+ how?: "captions" | "audio" | "hand";
+ note?: string;
+ /** "Corrected by hand" keeps what the reader wrote. */
+ original?: { quote: string; speaker?: string };
+};
+
+/** A forum post's attachment or screenshot, shown everywhere as "forum attachment, post #N, unverified". */
+export type FindingEvidence = { kind: "forum-attachment" | "wayback"; post?: string; url: string; capturedAt?: string };
+
+export type FindingRow = {
+ /** `f<n>`: unique in the case, never reused. */
+ row: string;
+ batch: string;
+ slug: string;
+ id: string;
+ date: string;
+ title: string;
+ t: number;
+ url: string;
+ category: string;
+ relevance: Relevance;
+ /** Verbatim, at most QUOTE_WORDS_MAX words. Private names already NAME_WITHHELD. */
+ quote: string;
+ summary: string;
+ speaker: string;
+ confidence: "high" | "medium" | "low";
+ round: number;
+ by: FindingBy;
+ quoteCheck: QuoteCheck;
+ verify?: FindingVerify;
+ evidence?: FindingEvidence[];
+};
+
+// ---------------------------------------------------------------------------------------------------------------
+// Stage 8: leads; stage 9: absence and date checks
+
+export const LEAD_DISPOSITIONS = ["archive", "watch", "never-archive"] as const;
+export type LeadDisposition = (typeof LEAD_DISPOSITIONS)[number];
+
+export type CaseLead = {
+ /** The account or URL, normalised (`x:@handle`, a host+path). */
+ key: string;
+ url: string;
+ platform: string;
+ /** How often the scope's posts link it. */
+ count: number;
+ role?: string;
+ /** Unset until a person decides. `never-archive` feeds the case's private names; `archive` creates the channel. */
+ disposition?: LeadDisposition;
+ /** The channel `archive` created. */
+ channel?: string;
+};
+
+export type DateCheck = {
+ /** The key date checked, YYYY-MM-DD. */
+ date: string;
+ label: string;
+ /** The subject's uploads in the window around it. */
+ uploads: { slug: string; id: string; date: string; title: string; captioned: boolean }[];
+ /** "no response found in captioned uploads through <date>", or what was found. */
+ sentence: string;
+};
+
+// ---------------------------------------------------------------------------------------------------------------
+// Guards, and what the private-name gate hashes
+
+const isOneOf =
+ <T extends string>(list: readonly T[]) =>
+ (value: unknown): value is T =>
+ typeof value === "string" && (list as readonly string[]).includes(value);
+
+export const isVerifyStatus = isOneOf(VERIFY_STATUSES);
+export const isLeadDisposition = isOneOf(LEAD_DISPOSITIONS);
+export const isFindingBy = isOneOf(FINDING_BY);
+export const isCaseStatus = isOneOf(CASE_STATUSES);
+export const isRelevance = (value: unknown): value is Relevance => value === 1 || value === 2 || value === 3;
+
+/** A term's words NFKC-normalised, lowercased and single-spaced: what privateNameHash hashes. */
+export const normalizeTerm = (s: string) => s.normalize("NFKC").toLowerCase().trim().split(/\s+/u).join(" ");
+
+/** What the gate hashes: every word of `text` and every 2- and 3-word phrase, each normalizeTerm'd. */
+export function privateNameCandidates(text: string): string[] {
+ const words = normalizeTerm(text.replace(/[^\p{L}\p{N}'’\-\s]/gu, " "))
+ .split(" ")
+ .filter(Boolean);
+ const out = new Set<string>();
+ for (let i = 0; i < words.length; i++) {
+ for (let n = 1; n <= 3 && i + n <= words.length; n++) out.add(words.slice(i, i + n).join(" "));
+ }
+ return [...out];
+}
diff --git a/common/lib/research/privateNames-server.ts b/common/lib/research/privateNames-server.ts
@@ -0,0 +1,16 @@
+// A case's private names as stored (lib/research/caseTypes.ts, "PRIVATE NAMES ARE NEVER TEXT"): the sha256 of each
+// term, never the term. The gate hashes privateNameCandidates of what it checks and compares.
+import { createHash } from "node:crypto";
+import { normalizeTerm, privateNameCandidates } from "./caseTypes";
+
+/** A term's stored form: sha256 of normalizeTerm(term), lowercase hex. */
+export function privateNameHash(term: string): string {
+ return createHash("sha256").update(normalizeTerm(term)).digest("hex");
+}
+
+/** How many of `text`'s words and 2–3-word phrases hash to one of `hashes`: a count, never which. */
+export function privateNameHits(text: string, hashes: readonly string[]): number {
+ if (!hashes.length) return 0;
+ const set = new Set(hashes);
+ return privateNameCandidates(text).filter((c) => set.has(privateNameHash(c))).length;
+}