Archilyzer · Source

archilyzer

Archilyzer
git clone https://archilyzer.pages.dev/source/archilyzer.git
Log | Files | Refs | README | LICENSE

commit bd65959d892aba4ef7ade26c763d88fd8c6023d7
parent 97f9c437330a2ec7edf2477a135bc8f5b5c5c9cb
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Sat, 10 Oct 2026 12:40:41 -0400

common: release 23 F4 — the stats cache sees the caption-track pass

buildIndex's per-video mtimes record carries a cue fingerprint (the count
and the last cue's end — what a stat is made of), written on every
(re)process, so a pass that re-reads a record's cues with no input file
moving — the caption-track pass — moves it. buildStats' indexSignature
appends it only when present: a record written before it keeps its old
signature exactly, so shipping this recomputes nothing by itself, and a
record the pass changed recomputes once.

A stat a pass left stale before the fingerprint (no transcript in the cache
while the index holds cues for it) is healed too: buildStats recomputes such
a stat, asked by the stored value's length only, for stats that say no
transcript.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Diffstat:
Mcommon/controller/buildIndex.ts | 18++++++++++++++++++
Mcommon/controller/buildStats.test.ts | 114+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcommon/controller/buildStats.ts | 27+++++++++++++++++++++++++--
3 files changed, 157 insertions(+), 2 deletions(-)

diff --git a/common/controller/buildIndex.ts b/common/controller/buildIndex.ts @@ -334,8 +334,25 @@ type MtimeRecord = { isDeleted: boolean; isUnlisted: boolean; indexKey: IndexKey; + // THE CUES THIS RECORD WAS LAST WRITTEN WITH (release 23 F4): cueFingerprint + // over the list stored under `indexKey`. Every input above can stand still + // while the cues move — the caption-track pass re-reads a record under a new + // rule and nothing on disk changed — and buildStats keys a video's stat on + // this record (indexSignature), so without it such a record kept its old + // `hasTranscript` / `cueCount` until something else moved. Absent on a record + // written before it, which buildStats reads as the old signature exactly. + cueFingerprint?: string; }; +// The cue facts a stat is made of — the count, and the last cue's end (the +// coverage) — as one comparable string. +export function cueFingerprint(list: readonly Cue[] | undefined): string { + if (!list || list.length === 0) return "0"; + let lastEnd = 0; + for (const c of list) if ((c.end ?? 0) > lastEnd) lastEnd = c.end ?? 0; + return `${list.length}:${lastEnd}`; +} + // The probe result an MtimeRecord carries, preferring the exact availability // string and falling back to the pre-VideoState booleans. function stateFromRecord(rec: MtimeRecord): VideoState { @@ -1312,6 +1329,7 @@ export async function buildIndex({ isDeleted: availability === "deleted", isUnlisted: availability === "unlisted", indexKey, + cueFingerprint: cueFingerprint(cueList), }); } catch (err) { log(`Failed to process ${s.channelSlug}/${s.videoDir}: ${String(err)}`); diff --git a/common/controller/buildStats.test.ts b/common/controller/buildStats.test.ts @@ -691,6 +691,120 @@ test("(k) the whole-pool bundle leaves out a channel only an unlisted site expos assert.deepEqual(own.map((s) => s.id).sort(), ["s1", "u1", "u2"]); }); +// The index's own sub-DBs, for the caption-track cases: the state an index +// built under the old caption-track rule left (cues stored empty), and its +// records as written before the cue fingerprint. +function withIndex<T>(fn: (db: (name: string) => ReturnType<ReturnType<typeof open>["openDB"]>) => T): T { + const root = open({ path: paths.lmdbPath, maxDbs: 18, compression: true }); + try { + return fn((name) => root.openDB({ name, encoding: "msgpack" })); + } finally { + root.close(); + } +} +type IdxRec = { indexKey: [string, string, string]; cueFingerprint?: string }; + +test("(l) release 23 F4: a record the caption-track pass takes from no text to text recomputes its stat, once", async () => { + resetCorpus(); + seedVideo("blocks"); + addCaptions("blocks", "2026-07-11T12:00:00Z"); + seedVideo("other"); + addCaptions("other", "2026-07-11T12:00:00Z"); + await runIndex(); + // The index as an older build left it: "blocks" read to no cues under the + // old rule, no record carrying a fingerprint, the rule's pass not yet run. + withIndex((db) => { + const mtimes = db("mtimes"); + for (const { key, value } of mtimes.getRange()) { + const { cueFingerprint: _gone, ...old } = value as IdxRec; + mtimes.putSync(key, old); + if ((key as string[])[1] === "blocks") db("cues").putSync((old as IdxRec).indexKey, []); + } + db("meta").removeSync("captionTrackRule"); + }); + const before = await runStats(); + assert.equal(statOf(before.byId, "blocks").hasTranscript, false); + assert.equal(statOf(before.byId, "blocks").cueCount, 0); + + // The pass re-reads "blocks" (its stored cues are empty); no input moved. + await runIndex(); + const after = await runStats(); + assert.equal(after.res.changed, 1, "only the record the pass changed"); + assert.equal(statOf(after.byId, "blocks").hasTranscript, true); + assert.equal(statOf(after.byId, "blocks").cueCount, 2); + assert.equal(statOf(after.byId, "other").cueCount, 2); + + const again = await runStats(); + assert.equal(again.res.changed, 0, "once"); +}); + +test("(l2) release 23 F4: a pass that changes a record's cue count (text to other text) recomputes its stat — the fingerprint alone", async () => { + resetCorpus(); + seedVideo("tracks"); + addCaptions("tracks", "2026-07-11T12:00:00Z"); + // A second English track: a record the caption-track pass reaches. + const en = path.join(videoDir("tracks"), "transcript.en.vtt"); + const orig = path.join(videoDir("tracks"), "transcript.en-orig.vtt"); + writeFileSync(orig, readFileSync(en, "utf8")); + touch(orig, "2026-07-11T12:00:00Z"); + await runIndex(); + // As an older rule left it: one cue of the two, no fingerprint, pass due. + withIndex((db) => { + const mtimes = db("mtimes"); + for (const { key, value } of mtimes.getRange()) { + const { cueFingerprint: _gone, ...old } = value as IdxRec; + mtimes.putSync(key, old); + const list = db("cues").get((old as IdxRec).indexKey) as unknown[]; + db("cues").putSync((old as IdxRec).indexKey, list.slice(0, 1)); + } + db("meta").removeSync("captionTrackRule"); + }); + const before = await runStats(); + assert.equal(statOf(before.byId, "tracks").cueCount, 1); + assert.equal(statOf(before.byId, "tracks").hasTranscript, true); + + await runIndex(); + const after = await runStats(); + assert.equal(after.res.changed, 1); + assert.equal(statOf(after.byId, "tracks").cueCount, 2); + assert.equal((await runStats()).res.changed, 0, "once"); +}); + +test("(m) release 23 F4: a stat a pass left stale before the fingerprint (no text, cues held) is healed, once", async () => { + resetCorpus(); + seedVideo("stale"); + addCaptions("stale", "2026-07-11T12:00:00Z"); + seedVideo("bare"); + await runIndex(); + await runStats(); + // What a caption-track pass that ran before this fix left: the index holds + // the cues, the cached stat still says no transcript, and its key matches. + withDb(({ stats }) => { + for (const { key, value } of stats.getRange()) { + if ((key as string[])[1] !== "stale") continue; + const v = value as { stat: Record<string, unknown> }; + stats.putSync(key, { ...v, stat: { ...v.stat, hasTranscript: false, cueCount: 0 } }); + } + }); + const healed = await runStats(); + assert.equal(healed.res.changed, 1, "the stale stat alone; a video with no cues is not asked again"); + assert.equal(statOf(healed.byId, "stale").hasTranscript, true); + assert.equal(statOf(healed.byId, "stale").cueCount, 2); + assert.equal(statOf(healed.byId, "bare").hasTranscript, false); + assert.equal((await runStats()).res.changed, 0); +}); + +test("(n) release 23 F4: shipping the fingerprint recomputes nothing — an old record's signature is unchanged", async () => { + const { indexSignature } = await import("./buildStats"); + const { cueFingerprint } = await import("./buildIndex"); + const old = { metaMs: 1, transcriptMs: 2, subsMs: null, availabilityMs: null, digestMs: null, indexKey: ["20260601", "c", "v"] as [string, string, string] }; + assert.equal(indexSignature(old), ["1", "2", "", "", "", "20260601", "c", "v"].join("\u0000")); + assert.notEqual(indexSignature({ ...old, cueFingerprint: "2:110" }), indexSignature(old)); + assert.equal(cueFingerprint(undefined), "0"); + assert.equal(cueFingerprint([]), "0"); + assert.equal(cueFingerprint([{ start: 0, end: 5, text: "a" }, { start: 60, end: 110, text: "b" }]), "2:110"); +}); + test("(z) no write this file caused landed outside its temp root", () => { // LMDB writes natively, past the spy: its file must be under the root too. assert.ok(paths.lmdbPath.startsWith(ROOT + path.sep), paths.lmdbPath); diff --git a/common/controller/buildStats.ts b/common/controller/buildStats.ts @@ -106,6 +106,9 @@ type IndexRecord = { availabilityMs?: number | null; digestMs?: number | null; indexKey: IndexKey; + // buildIndex's cueFingerprint (release 23 F4): it moves when a pass re-reads + // a record's cues with no input file moving (the caption-track pass). + cueFingerprint?: string; }; // buildIndex has no `mtimes` record for this video. See the two counts below. @@ -116,7 +119,12 @@ const NOT_INDEXED = "-"; // for another reason can read a fresher transcript.cues.json and change the cue // count, and a moved index key moves the cues. Numbers print as their shortest // round-trip form, so an unchanged record gives an identical string. -function indexSignature(r: IndexRecord | undefined): string { +// +// The cue fingerprint (release 23 F4) is appended only when the record has +// one: a record written before it gives the old string exactly, so shipping it +// recomputes nothing by itself; a record the index (re)writes from then on — +// the caption-track pass's included — moves once. +export function indexSignature(r: IndexRecord | undefined): string { if (!r) return NOT_INDEXED; const n = (v: number | null | undefined) => (v == null ? "" : String(v)); return [ @@ -126,9 +134,24 @@ function indexSignature(r: IndexRecord | undefined): string { n(r.availabilityMs), n(r.digestMs), ...r.indexKey, + ...(r.cueFingerprint !== undefined ? [`cues=${r.cueFingerprint}`] : []), ].join("\u0000"); } +// A cached stat that says NO TRANSCRIPT while the index holds cues for it: +// the stale shape a caption-track pass that ran before the fingerprint left +// (a record taken from no text to text with no input moving). Asked only of +// such stats, by the stored value's length — no decode. +function statMissesStoredCues( + prev: StatsRecord, + rec: IndexRecord | undefined, + cues: { getBinaryFast(key: IndexKey): Buffer | undefined }, +): boolean { + if (!rec || prev.stat.hasTranscript) return false; + const raw = cues.getBinaryFast(rec.indexKey); + return raw !== undefined && raw.length > 4; +} + // Set to 1 (or true/yes/on) to let an OLDER build clear a stats cache a newer // one wrote — a deliberate rollback. Declared in lib/envVars.ts. export const STATS_DOWNGRADE_ENV = "ARCHILYZER_STATS_ALLOW_DOWNGRADE"; @@ -471,7 +494,7 @@ export async function buildStats({ if (!prev) { added++; toProcess.push({ e, idx, indexKey: rec?.indexKey }); - } else if (prev.metaMs !== e.metaMs || prev.idx !== idx) { + } else if (prev.metaMs !== e.metaMs || prev.idx !== idx || statMissesStoredCues(prev, rec, cues)) { // The second half is the fix for stats frozen at first sight: a // transcript that arrives later makes buildIndex re-process the video // (and a video first seen before the index had it moves off