// A VIDEO'S READING NOTES — `channels//data//reading-notes.json` // (lane/lookout slice 2). What a model found reading the video's transcript, // chunk by chunk: per chunk a line on what it covers and at most three items, // each a verbatim quote CODE located in the cues (start and end stamped from // them), a kind, one line of why, whose voice the model thinks it is and the // people named. Written by a lookout run (controller/lookoutRun.ts), its ONE // writer; read back by later runs, so a recording already read is never read // again while its notes are fresh. // // BESIDE THE CUES because it is derived from them and must go stale with them. // ONE READING PER MODEL, in one file: `{ version: 2, readings: { "|p|c": notes } }`, so two lookouts on two models share // the file without writing over each other's reading on every run (at most // READING_NOTES_MAX_READINGS kept, the most recently written). A reading is // FRESH iff its provenance is today's: that key and the same cues file (size // and mtime) — the digest's rule. A stale reading is read again from the start // and replaces its key; a damaged file (not JSON, not notes) is said by name in // the run's log, read again and written over: it is a cache of model calls, // never the operator's data. // // RESUMABLE: the notes are written after every chunk, with the chunks read so // far and `complete: false`; a cancelled or interrupted run leaves them so, // and the next run reads only the chunks that are not there. // // PRIVATE. Never composed, indexed, exported, archived or served by the MCP: // every consumer of a video dir reads its files by name (the cues, the // metadata, subtitle tracks named transcript..), and the privacy tests // (lane/lookout slice 2, 2b) plant this file and look for it in their output. // // SERVER-ONLY. import { readJsonFile, writeJsonAtomic } from "./jsonFile-server"; import { sidecar, sidecarField } from "./sidecar-server"; export const READING_NOTES_FILENAME = "reading-notes.json"; /** One reading's shape. */ export const READING_NOTES_VERSION = 1; /** The file's shape: a reading per model (and prompt and chunk size). */ export const READING_NOTES_FILE_VERSION = 2; /** Readings kept in one file, the most recently written first. */ export const READING_NOTES_MAX_READINGS = 4; export const READING_VOICES = ["own", "reading", "clip", "unsure"] as const; export type ReadingVoice = (typeof READING_VOICES)[number]; /** One located moment the model found. */ export type ReadingItem = { kind: string; /** Verbatim, as the model copied it and code found it in the cues. */ quote: string; /** Seconds, from the cues: the cue the quote starts in, the end of the one it ends in. */ start: number; end: number; why: string; /** The model's guess whose words they are — a caveat on a card, never a guarantee. */ voice: ReadingVoice; people: string[]; }; export type ReadingChunk = { /** The chunk's place in the video's chunks (lookoutPlan's chunking), from 0. */ index: number; /** Its first and last line's second. */ from: number; to: number; covers: string; items: ReadingItem[]; /** Items the model gave that code dropped (not found verbatim, too short or long, not a kind). */ dropped: number; }; export type ReadingProvenance = { model: string; promptVersion: number; chunkChars: number; cues: { size: number; mtimeMs: number }; }; export type ReadingNotes = { version: number; provenance: ReadingProvenance; /** How many chunks the video has. */ chunkCount: number; /** The chunks read, in the order they were. */ chunks: ReadingChunk[]; /** Every chunk read. */ complete: boolean; updatedAt: string; }; const isObj = (v: unknown): v is Record => typeof v === "object" && v !== null && !Array.isArray(v); const isNum = (v: unknown): v is number => typeof v === "number" && Number.isFinite(v); function coerceProvenance(v: unknown): ReadingProvenance | null { if (!isObj(v) || typeof v.model !== "string" || !isNum(v.promptVersion) || !isNum(v.chunkChars) || !isObj(v.cues)) return null; if (!isNum(v.cues.size) || !isNum(v.cues.mtimeMs)) return null; return { model: v.model, promptVersion: v.promptVersion, chunkChars: v.chunkChars, cues: { size: v.cues.size, mtimeMs: v.cues.mtimeMs } }; } function coerceItem(v: unknown): ReadingItem | null { if (!isObj(v) || typeof v.kind !== "string" || typeof v.quote !== "string" || !isNum(v.start) || !isNum(v.end)) return null; const voice = (READING_VOICES as readonly string[]).includes(v.voice as string) ? (v.voice as ReadingVoice) : "unsure"; return { kind: v.kind, quote: v.quote, start: v.start, end: v.end, why: typeof v.why === "string" ? v.why : "", voice, people: Array.isArray(v.people) ? v.people.filter((p): p is string => typeof p === "string") : [], }; } function coerceChunk(v: unknown): ReadingChunk | null { if (!isObj(v) || !Number.isInteger(v.index) || !isNum(v.from) || !isNum(v.to) || !Array.isArray(v.items)) return null; const items: ReadingItem[] = []; for (const raw of v.items) { const i = coerceItem(raw); if (!i) return null; items.push(i); } return { index: v.index as number, from: v.from, to: v.to, covers: typeof v.covers === "string" ? v.covers : "", items, dropped: isNum(v.dropped) ? v.dropped : 0 }; } /** A notes file coerced; null (read again) when any part of it is not notes. */ export function coerceReadingNotes(value: unknown): ReadingNotes | null { if (!isObj(value) || value.version !== READING_NOTES_VERSION || !Number.isInteger(value.chunkCount) || !Array.isArray(value.chunks)) return null; const provenance = coerceProvenance(value.provenance); if (!provenance) return null; const chunks: ReadingChunk[] = []; const seen = new Set(); for (const raw of value.chunks) { const c = coerceChunk(raw); if (!c || seen.has(c.index) || c.index < 0 || c.index >= (value.chunkCount as number)) return null; seen.add(c.index); chunks.push(c); } return { version: READING_NOTES_VERSION, provenance, chunkCount: value.chunkCount as number, chunks, complete: chunks.length === value.chunkCount, updatedAt: typeof value.updatedAt === "string" ? value.updatedAt : "", }; } export type ReadingNotesFile = { version: number; readings: Record }; /** The key a reading is kept under: its model, prompt version and chunk size. */ export function readingKeyOf(p: Pick): string { return `${p.model}|p${p.promptVersion}|c${p.chunkChars}`; } /** * A notes file coerced: `{file, damaged}` — every reading that is one, and * whether anything in it was not (the whole file, or one reading). A version-1 * file (one reading, slice 2's first form) is a file of that one reading. */ export function coerceReadingNotesFile(value: unknown): { file: ReadingNotesFile; damaged: boolean } { const file: ReadingNotesFile = { version: READING_NOTES_FILE_VERSION, readings: {} }; if (isObj(value) && value.version === READING_NOTES_VERSION) { const one = coerceReadingNotes(value); if (!one) return { file, damaged: true }; file.readings[readingKeyOf(one.provenance)] = one; return { file, damaged: false }; } if (!isObj(value) || value.version !== READING_NOTES_FILE_VERSION || !isObj(value.readings)) return { file, damaged: true }; let damaged = false; for (const [key, raw] of Object.entries(value.readings)) { const notes = coerceReadingNotes(raw); if (!notes || readingKeyOf(notes.provenance) !== key) { damaged = true; continue; } file.readings[key] = notes; } return { file, damaged }; } // Declared for the per-video sidecar registry (its name, its removal); the // run reads and writes through loadReadingNotes / writeReadingNotes. export const readingNotesSidecar = sidecar(READING_NOTES_FILENAME, sidecarField((v) => coerceReadingNotesFile(v).file), { indent: 2 }); export type LoadedReadingNotes = { /** The reading under this provenance's key, or null (none yet, or none whole). */ notes: ReadingNotes | null; /** "absent": no file; "damaged": a file that is not JSON or holds what is not notes. */ state: "absent" | "damaged" | "ok"; }; /** This model's reading of a video (any freshness: the caller compares the provenance). */ export async function loadReadingNotes(videoDir: string, key: Pick): Promise { const read = await readJsonFile(readingNotesSidecar.path(videoDir)); if (!read.ok) return { notes: null, state: read.reason === "absent" ? "absent" : "damaged" }; const { file, damaged } = coerceReadingNotesFile(read.value); return { notes: file.readings[readingKeyOf(key)] ?? null, state: damaged ? "damaged" : "ok" }; } /** * THE writer: this reading put under its key, the file's other readings kept * (what of them is whole; a damaged file is written over), at most * READING_NOTES_MAX_READINGS, the most recently written kept. Atomic. */ export async function writeReadingNotes(videoDir: string, notes: ReadingNotes): Promise { const at = readingNotesSidecar.path(videoDir); const read = await readJsonFile(at); const file = read.ok ? coerceReadingNotesFile(read.value).file : { version: READING_NOTES_FILE_VERSION, readings: {} }; const key = readingKeyOf(notes.provenance); const others = Object.entries(file.readings) .filter(([k]) => k !== key) .sort((a, b) => (a[1].updatedAt < b[1].updatedAt ? 1 : a[1].updatedAt > b[1].updatedAt ? -1 : 0)) .slice(0, READING_NOTES_MAX_READINGS - 1); await writeJsonAtomic(at, { version: READING_NOTES_FILE_VERSION, readings: { [key]: notes, ...Object.fromEntries(others) } }, { indent: 2 }); } export function sameReadingProvenance(a: ReadingProvenance, b: ReadingProvenance): boolean { return ( a.model === b.model && a.promptVersion === b.promptVersion && a.chunkChars === b.chunkChars && a.cues.size === b.cues.size && a.cues.mtimeMs === b.cues.mtimeMs ); } /** Empty notes for a video about to be read. */ export function newReadingNotes(provenance: ReadingProvenance, chunkCount: number, now: Date): ReadingNotes { return { version: READING_NOTES_VERSION, provenance, chunkCount, chunks: [], complete: chunkCount === 0, updatedAt: now.toISOString() }; } /** The notes with one more chunk read (replacing one of the same index), sorted by index. */ export function withReadingChunk(notes: ReadingNotes, chunk: ReadingChunk, now: Date): ReadingNotes { const chunks = [...notes.chunks.filter((c) => c.index !== chunk.index), chunk].sort((a, b) => a.index - b.index); return { ...notes, chunks, complete: chunks.length === notes.chunkCount, updatedAt: now.toISOString() }; }