Archilyzer · Source

archilyzer

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

commit 73c8bc2beeb6730ecff6d7c4a498953f985f077b
parent 719c7fe9448862847717e9b7217f13358a59e6b6
Author: I Mean I'm Just Saying <imeanimjustsaying@kiwifarms.st>
Date:   Fri, 16 Oct 2026 02:40:58 -0400

mcp: a video's copies from the mirror scans; enumerate_matches copies: "only-here" (release 31 D)

On a local source the MCP reads transcripts/mirrors/*.json (the archive's
own transcripts/): get_video_metadata gains a `- copies:` line — a mirror
video's status and the creator's copies it matched, or a creator video a
mirror holds and whether it is gone — and enumerate_matches takes
`copies: "only-here"`, the scan narrowed by id to the mirror videos that are
the only copy (to the mirror channels unless the call scopes itself). A
remote or hub source has no mirror data and says it is not available.
instructions.ts names the filter.

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

Diffstat:
Mmcp/src/instructions.ts | 5++++-
Amcp/src/mirrorCopies.test.ts | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Amcp/src/mirrorCopies.ts | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmcp/src/server.ts | 29+++++++++++++++++++++++++++++
4 files changed, 199 insertions(+), 1 deletion(-)

diff --git a/mcp/src/instructions.ts b/mcp/src/instructions.ts @@ -502,7 +502,10 @@ export function buildSweepInstructions( `implies them: a filtered scan reads only the shard pages that can hold ` + `a match, which is the difference between seconds and a minute per ` + `query — and it makes the answer narrower and more honest at the same ` + - `time.`, + `time. \`enumerate_matches\` alone also takes \`copies: "only-here"\`: ` + + `a MIRROR channel's videos that no creator platform still has public ` + + `(the mirror scan's only-copy status) — a local archive only; a ` + + `published site has no mirror data and refuses it.`, ); steps.push( diff --git a/mcp/src/mirrorCopies.test.ts b/mcp/src/mirrorCopies.test.ts @@ -0,0 +1,109 @@ +// The mirror scans in the MCP (release 31, lane D): get_video_metadata's +// `copies` line and enumerate_matches' `copies: "only-here"`, read from a +// local archive's transcripts/mirrors/; a published site has none and says so. +// Synthetic only: the draft corpus, with `synth-other` scanned as a mirror of +// `synth-kitchen`. + +import { after, before, test } from "node:test"; +import assert from "node:assert/strict"; +import { mkdtemp, rm } from "node:fs/promises"; +import os from "node:os"; +import path from "node:path"; +import { writeDraftCorpus } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; +import { matchMirror } from "yt-dlp-transcript-common/lib/mirrors/match"; +import { MIRROR_RULES, MIRROR_SCAN_FORMAT, MIRROR_SCAN_VERSION, type MirrorScanFile } from "yt-dlp-transcript-common/lib/mirrors/scanFile"; +import { writeMirrorScan } from "yt-dlp-transcript-common/lib/mirrors/scanFile-server"; +import { createCorpusTools, type CorpusTools } from "./callTool"; +import { COPIES_NOT_AVAILABLE, describeCopies, loadMirrorScans, onlyHereIds } from "./mirrorCopies"; +import { LocalSource, RemoteSource } from "./source"; + +let dir: string; +let transcripts: string; +let corpus: CorpusTools; +const savedEnv = process.env.TRANSCRIPTS_DIR; + +const rec = (id: string, title: string, uploadDate: string, gone: "deleted" | null = null) => ({ + id, + title, + uploadDate, + duration: 3600, + wasLive: true, + gone, +}); + +function scanFile(): MirrorScanFile { + const r = matchMirror({ + mirror: { + slug: "synth-other", + platform: "youtube", + records: [rec("flat-loaf-01", "The flat loaf", "20260112"), rec("mirror-pie-09", "Pie stream", "20260210")], + }, + creators: [ + { + slug: "synth-kitchen", + platform: "youtube", + records: [rec("bread-day-01", "Bread day", "20260105"), rec("pie-stream-02", "Pie stream", "20260210", "deleted")], + }, + ], + }); + return { + format: MIRROR_SCAN_FORMAT, + version: MIRROR_SCAN_VERSION, + mirror: "synth-other", + of: ["synth-kitchen"], + generatedAt: "2026-10-16T00:00:00.000Z", + rules: { ...MIRROR_RULES }, + ...r, + }; +} + +before(async () => { + dir = await writeDraftCorpus(await mkdtemp(path.join(os.tmpdir(), "mcp-mirrors-"))); + transcripts = await mkdtemp(path.join(os.tmpdir(), "mcp-mirrors-transcripts-")); + process.env.TRANSCRIPTS_DIR = transcripts; + await writeMirrorScan(transcripts, scanFile()); + corpus = await createCorpusTools({ source: `local:${dir}` }); +}); + +after(async () => { + if (savedEnv === undefined) delete process.env.TRANSCRIPTS_DIR; + else process.env.TRANSCRIPTS_DIR = savedEnv; + await corpus.close(); + await rm(dir, { recursive: true, force: true }); + await rm(transcripts, { recursive: true, force: true }); +}); + +test("the scans: read for a local source only", async () => { + const local = await loadMirrorScans(new LocalSource(dir), transcripts); + assert.equal(local?.length, 1); + assert.equal(await loadMirrorScans(new RemoteSource("https://example.invalid"), transcripts), null); + assert.deepEqual(onlyHereIds(local!), { ids: ["flat-loaf-01", "mirror-pie-09"], channels: ["synth-other"] }); +}); + +test("the copies line: a mirror video, a creator's video held, nothing known, remote", () => { + const scans = [scanFile()]; + assert.equal(describeCopies(scans, "synth-other", "flat-loaf-01"), "- copies: a MIRROR copy — only copy; the creator's copies: none found"); + assert.equal(describeCopies(scans, "synth-other", "mirror-pie-09"), "- copies: a MIRROR copy — only copy; the creator's copies: pie-stream-02 (synth-kitchen, deleted)"); + assert.equal(describeCopies(scans, "synth-kitchen", "pie-stream-02"), "- copies: this video is deleted on its platform; a mirror holds it: mirror-pie-09 (synth-other)"); + assert.equal(describeCopies(scans, "synth-kitchen", "bread-day-01"), ""); + assert.equal(describeCopies(null, "synth-kitchen", "bread-day-01"), `- copies: ${COPIES_NOT_AVAILABLE}`); +}); + +test("get_video_metadata carries the copies line", async () => { + const text = await corpus.call("get_video_metadata", { video_id: "flat-loaf-01" }); + assert.match(text, /- copies: a MIRROR copy — only copy; the creator's copies: none found/); + const other = await corpus.call("get_video_metadata", { video_id: "bread-day-01" }); + assert.doesNotMatch(other, /copies:/); +}); + +test("enumerate_matches copies: only-here keeps the only-copy mirror videos", async () => { + const all = await corpus.call("enumerate_matches", { query: ".", regex: true }); + assert.match(all, /bread-day-01/); + const only = await corpus.call("enumerate_matches", { query: ".", regex: true, copies: "only-here" }); + assert.deepEqual([...only.matchAll(/^- ([^ |]+) \| /gm)].map((m) => m[1]), ["flat-loaf-01"]); + assert.match(only, /copies: only-here — 2 mirror video\(s\) that are the only copy, from 1 mirror scan\(s\)/); + const none = await corpus.call("enumerate_matches", { query: ".", regex: true, copies: "only-here", ids: ["bread-day-01"] }); + assert.match(none, /^No matches/); + const bad = await corpus.call("enumerate_matches", { query: ".", copies: "everywhere" }); + assert.match(bad, /copies must be "only-here"/); +}); diff --git a/mcp/src/mirrorCopies.ts b/mcp/src/mirrorCopies.ts @@ -0,0 +1,57 @@ +// A video's COPIES as the mirror scans see them (release 31, lane D): +// `transcripts/mirrors/<slug>.json` (lib/mirrors/scanFile.ts), written by the +// editor's mirror scan. Mirror data is PRIVATE — no published site carries it — +// so only a LOCAL source reads it (the archive's own `transcripts/`, i.e. +// TRANSCRIPTS_DIR or the checkout's); a remote or hub source answers "not +// available" rather than guessing. + +import { getPaths } from "yt-dlp-transcript-common/lib/paths"; +import { listMirrorScans } from "yt-dlp-transcript-common/lib/mirrors/scanFile-server"; +import { copiesIndex, type MirrorScanFile, type VideoCopies } from "yt-dlp-transcript-common/lib/mirrors/scanFile"; +import { MIRROR_STATUS_LABELS } from "yt-dlp-transcript-common/lib/mirrors/match"; +import { LocalSource, type ShardSource } from "./source"; + +export const COPIES_NOT_AVAILABLE = + "not available: mirror scans are local-only (no published site carries them) — ask a local archive's MCP (--local)"; + +// The scans, or null when this source cannot have them (not local). +export async function loadMirrorScans( + source: ShardSource, + transcriptsDir: string = getPaths().transcriptsDir, +): Promise<MirrorScanFile[] | null> { + if (!(source instanceof LocalSource)) return null; + return listMirrorScans(transcriptsDir).catch(() => []); +} + +// The `copies` line of get_video_metadata, or "" when the scans say nothing of +// this video. +export function describeCopies(scans: MirrorScanFile[] | null, channel: string, id: string): string { + if (scans === null) return `- copies: ${COPIES_NOT_AVAILABLE}`; + const c: VideoCopies | undefined = copiesIndex(scans).get(`${channel}/${id}`); + if (!c) return ""; + if (c.role === "mirror") { + const theirs = c.matches.length + ? c.matches.map((m) => `${m.id} (${m.channel}${m.gone ? `, ${m.gone}` : ""})`).join(", ") + : "none found"; + return `- copies: a MIRROR copy — ${MIRROR_STATUS_LABELS[c.status].toLowerCase()}; the creator's copies: ${theirs}`; + } + const held = c.copies.map((x) => `${x.id} (${x.mirror})`).join(", "); + return c.gone + ? `- copies: this video is ${c.gone} on its platform; a mirror holds it: ${held}` + : `- copies: a mirror holds a copy: ${held}`; +} + +// The record ids of mirror videos that are the only copy (status only-copy), +// with the mirror channels they are on. +export function onlyHereIds(scans: MirrorScanFile[]): { ids: string[]; channels: string[] } { + const ids: string[] = []; + const channels = new Set<string>(); + for (const f of scans) { + for (const v of f.videos) { + if (v.status !== "only-copy") continue; + ids.push(v.id); + channels.add(f.mirror); + } + } + return { ids, channels: [...channels].sort() }; +} diff --git a/mcp/src/server.ts b/mcp/src/server.ts @@ -5,6 +5,7 @@ import { type Tool, } from "@modelcontextprotocol/server"; import { salvageCaveat } from "yt-dlp-transcript-common/lib/salvageMark"; +import { COPIES_NOT_AVAILABLE, describeCopies, loadMirrorScans, onlyHereIds } from "./mirrorCopies"; import { transcriptToMarkdown } from "yt-dlp-transcript-common/lib/transcriptToMarkdown"; import { formatDate, formatDuration } from "yt-dlp-transcript-common/lib/format"; import { manifestHasDigest } from "yt-dlp-transcript-common/lib/digests"; @@ -588,6 +589,15 @@ export const TOOLS: Tool[] = [ "holding them are read, so the scan is bounded by the ids rather " + "than the scope. The footer says how many of them the scope holds.", }, + copies: { + type: "string", + enum: ["only-here"], + description: + "Optional: \"only-here\" keeps only a MIRROR channel's videos that no " + + "creator platform still has public (the mirror scan's only-copy " + + "status): what survives nowhere else. A local archive only — a " + + "published site carries no mirror data and refuses it.", + }, }, required: ["query"], additionalProperties: false, @@ -1771,6 +1781,21 @@ async function handleEnumerateMatches( source: ShardSource, args: Record<string, unknown>, ): Promise<ToolResult> { + // `copies: "only-here"` (release 31, mirrorCopies.ts): the scan narrowed to + // the mirror videos that are the only copy, by id. + let copiesNote = ""; + if (args.copies !== undefined) { + if (args.copies !== "only-here") return errorText('copies must be "only-here"'); + const scans = await loadMirrorScans(source); + if (scans === null) return errorText(`copies: "only-here" is ${COPIES_NOT_AVAILABLE}`); + const only = onlyHereIds(scans); + const asked = strArray(args.ids); + const ids = asked ? only.ids.filter((id) => asked.includes(id)) : only.ids; + copiesNote = `copies: only-here — ${only.ids.length} mirror video(s) that are the only copy, from ${scans.length} mirror scan(s)`; + if (ids.length === 0) return text(`No matches for "${String(args.query ?? "").trim()}" (${copiesNote}).`); + const { copies: _copies, ...rest } = args; + args = { ...rest, ids, ...(rest.channels || rest.groups || rest.channel || rest.group ? {} : { channels: only.channels }) }; + } // The scan is enumerate.ts's, shared with umtool's Research page: this tool // only renders it. const e = await enumerate(source, args); @@ -1827,6 +1852,7 @@ async function handleEnumerateMatches( (prunedNote ? `; ${prunedNote}` : "") + (dupNote ? `; ${dupNote}` : "") + (idsNote ? `; ${idsNote}` : "") + + (copiesNote ? `; ${copiesNote}` : "") + (scopeNote ? `; ${scopeNote}` : "") + (filterNote ? `; ${filterNote}` : "") + (postsNote ? `; ${postsNote}` : "") + @@ -2408,6 +2434,9 @@ async function handleGetMetadata( // as the caveat it is whatever the stats layer says. if (found.record.salvaged) lines.push(`- ${salvageCaveat(found.record.salvaged)}`); + // ── transcripts/mirrors/ (local only) ── + lines.push(describeCopies(await loadMirrorScans(source), found.ch.slug, found.record.id)); + // ── duplicates.json ── lines.push(await describeOtherCopies(source, slug));