// AN X TRENDING LIBRARY NEVER REACHES THE MCP'S CORPUS TOOLS (lane x-trending, // hard rule 1). The MCP reads a COMPOSED site (`--local`, TRANSCRIPT_LOCAL_DIR, // export/public) or a published one, never the corpus. So the corpus is made // for real — an ordinary X account channel with one post (the control) and a // library with a post, a topic and a raw capture, each carrying a marker — on // a PRIVATE site whose site.json, written by hand, even NAMES the library; // then indexed and composed with the real build, and every read-only corpus // tool is asked over the composed dir. None answers with a marker, the // library's slug or its post; each does answer with the control. // common/bin/xTrendingPrivacy.test.ts holds the index, compose and site-writer // halves. (The library's own listing is the editor's: x_trending_topics and // x_trending_posts, xTrending.test.ts.) import { after, test } from "node:test"; import assert from "node:assert/strict"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "mcp-x-trending-privacy-")); Object.assign(process.env, { TRANSCRIPTS_DIR: path.join(ROOT, "transcripts"), SAVED_VIDEOS_DIR: path.join(ROOT, "saved-videos"), SITES_DIR: path.join(ROOT, "transcripts", "sites"), SETTINGS_FILE: path.join(ROOT, "settings.json"), EXPORT_PUBLIC_DIR: path.join(ROOT, "public"), EXPORT_INDEX_DIR: path.join(ROOT, ".export-index"), EXPORT_BUILDS_DIR: path.join(ROOT, ".export-builds"), EDITOR_CHANGELOG_FILE: path.join(ROOT, "editor-CHANGELOG.md"), EXPORT_CHANGELOG_FILE: path.join(ROOT, "export-CHANGELOG.md"), CHARTS_CONFIG_FILE: path.join(ROOT, "chart-templates.json"), SEARCH_ALIASES_FILE: path.join(ROOT, "transcripts", "search-aliases.json"), CURATED_TAGS_FILE: path.join(ROOT, "transcripts", "tags.json"), ARCHILYZER_CONFIG_DIR: path.join(ROOT, "config"), ARCHILYZER_SOURCE_SCRATCH: path.join(ROOT, "source-scratch"), }); delete process.env.ARCHILYZER_INDEX_ALLOW_HELD; after(() => rmSync(ROOT, { recursive: true, force: true })); const { getPaths } = await import("yt-dlp-transcript-common/lib/paths"); const { buildIndex } = await import("yt-dlp-transcript-common/controller/buildIndex"); const { main: composeSite } = await import("yt-dlp-transcript-common/bin/compose-site"); const { writePosts } = await import("yt-dlp-transcript-common/lib/posts-server"); const { recordXTrendingRead, writeXTrendingPosts, trendingReadDir, readXTrendingTopics } = await import("yt-dlp-transcript-common/lib/xTrending/store"); const { createCorpusTools } = await import("./callTool"); const paths = getPaths(); const LIB = "trending-lib"; const PUBLIC_X = "kettle-org-x"; const PUBLIC_ID = "1900000000000005001"; const PUBLIC_POST = "an ordinary archived post about kettles"; const LIB_ID = "1900000000000006001"; const { plantSlice2Library, SLICE2_MARK_LIST } = await import("yt-dlp-transcript-common/lib/xTrending/__fixtures__/slice2Library"); // The last five are lane x-trending-2's: a judgement's note, a captured media // file's bytes and address, the capture's context, the shot. const MARKS = ["trendpostmarkerq7x", "trendhandlemarkerz4k", "trendtopicmarkerw2p", "trendrawmarkerj9m", ...SLICE2_MARK_LIST]; const READ = "tr-01JA0000000000000000000000"; const writeJson = (file: string, value: unknown) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, JSON.stringify(value, null, 2)); }; const post = (channelSlug: string, id: string, author: string, text: string) => ({ id, slug: `${channelSlug}/${id}`, channelSlug, author, createdAt: "2026-11-16T12:00:00.000Z", uploadDate: "20261116", text, url: `https://x.com/${author}/status/${id}`, platform: "twitter" as const, isReply: false, isRepost: false, links: [], }); writeJson(paths.settingsFile, {}); writeJson(path.join(paths.channelsDir, PUBLIC_X, "config.json"), { handling: "transcribe", sourceKind: "social", platform: "twitter", name: "Kettle Org (X)", url: "https://x.com/kettle_org", socialHandle: "kettle_org", }); await writePosts(path.join(paths.channelsDir, PUBLIC_X), [post(PUBLIC_X, PUBLIC_ID, "kettle_org", PUBLIC_POST)]); const libRoot = path.join(paths.channelsDir, LIB); writeJson(path.join(libRoot, "config.json"), { handling: "transcribe", sourceKind: "social", platform: "twitter", name: "Trending", xTrending: {}, }); const libPosts = [post(LIB, LIB_ID, MARKS[1], `kettles ${MARKS[0]}`)]; // Where a library keeps them, AND where an ordinary posts channel would (a // hand copy): neither may reach a build. await writeXTrendingPosts(libRoot, libPosts); await writePosts(libRoot, libPosts); await recordXTrendingRead(libRoot, { readId: READ, at: "2026-11-17T10:00:00.000Z", ref: { trendId: "1900000000000000001", url: "https://x.com/i/trending/1900000000000000001" }, topic: `Topic ${MARKS[2]}`, postIds: [LIB_ID], added: 1, }); mkdirSync(trendingReadDir(libRoot, READ), { recursive: true }); writeFileSync(path.join(trendingReadDir(libRoot, READ), "responses.jsonl"), `${JSON.stringify({ n: 1, op: "Op", status: 200, body: { t: `kettles ${MARKS[3]}` } })}\n`); // What a keep, a capture and a watch leave (lane x-trending-2). await plantSlice2Library(libRoot, { postId: LIB_ID, topicId: (await readXTrendingTopics(libRoot))[0].id }); // Written by hand, naming the library (the site writer would refuse it). writeJson(path.join(paths.sitesDir, "priv", "site.json"), { siteId: "priv", siteTitle: "priv", siteDescription: "fixture", headerTitle: "priv", homeTagline: "", socialLinks: [], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", channels: [{ slug: PUBLIC_X, groupId: "default" }, { slug: LIB, groupId: "default" }], siteUrl: "https://priv.example.test", archives: false, audience: "private", }); test("every read-only corpus tool over the composed site: the account's post, never the library's", async () => { await buildIndex({ paths, onLog: () => {} }); const log = console.log; const warn = console.warn; console.log = () => {}; console.warn = () => {}; try { await composeSite({ siteId: "priv", paths }); } finally { console.log = log; console.warn = warn; } const corpus = await createCorpusTools({ source: `local:${paths.exportPublicDir}` }); const ask = (tool: string, args?: Record) => corpus.call(tool as never, args as never).catch((e: Error) => `refused: ${e.message}`); try { const answers: Record = { list_channels: await ask("list_channels"), search_control: await ask("search_transcripts", { query: "kettles" }), enumerate: await ask("enumerate_matches", { query: "kettles" }), get_post_control: await ask("get_post", { post_id: PUBLIC_ID }), get_post_library: await ask("get_post", { post_id: LIB_ID }), get_post_library_named: await ask("get_post", { post_id: LIB_ID, channel: LIB }), get_thread_library: await ask("get_thread", { post_id: LIB_ID }), channel_coverage: await ask("channel_coverage", { channel: LIB }), }; for (const mark of MARKS) answers[`search ${mark}`] = await ask("search_transcripts", { query: mark }); assert.match(answers.list_channels!, new RegExp(PUBLIC_X), "control: the account channel is listed"); assert.match(answers.get_post_control!, new RegExp(PUBLIC_POST), "control: the account's post is read"); for (const [tool, text] of Object.entries(answers)) { // A refusal may echo the id or slug it was asked for; nothing else may // name the library, and nothing at all may carry its words. const asked = tool.startsWith("get_post_library") || tool.startsWith("get_thread") || tool === "channel_coverage"; if (!asked) { assert.ok(!text.includes(LIB), `${tool}: names ${LIB}`); assert.ok(!text.includes(LIB_ID), `${tool}: names the library's post`); } for (const mark of MARKS) { if (tool === `search ${mark}`) continue; // the search echoes its own query assert.ok(!text.includes(mark), `${tool}: answers with ${mark}`); } assert.ok(!text.includes("responses.jsonl") && !text.includes(READ), `${tool}: names a raw capture`); for (const name of ["judgements.json", "media-index.json", "media-store", "watch.json", "posts-media"]) { assert.ok(!text.includes(name), `${tool}: names ${name}`); } } // Asked for by id, the library's post is not found. for (const tool of ["get_post_library", "get_post_library_named", "get_thread_library"]) { assert.ok(!answers[tool]!.includes(`https://x.com/${MARKS[1]}`), `${tool}: answered the post`); assert.match(answers[tool]!, /not found|no post|No post|refused|unknown|not in|No channel|no channel/i, `${tool}: ${answers[tool]!.slice(0, 200)}`); } // A search for a marker finds no post. for (const mark of MARKS) assert.ok(!answers[`search ${mark}`]!.includes("x.com/"), `search ${mark}: a hit`); } finally { await corpus.close(); } }); // Lane posts-library: the judge view's asks of the editor's listing (detail, // ids, the filters, the seen marks) and the purge are umtool's and `pnpm // ops`'s. The MCP's two trending tools ask for a slug, a topic and a limit, and // gain no purge. // // THE MCP BUILDS ITS QUERY WITH `new URLSearchParams({…})` AND `.set("…")`, so // that is what is read: every key given to either, in a source that asks the // listing, is one of the three. // `accounts` (lane x-trending-publish): the library's accounts, a read like // the other two. Still never a judge view's key. const LISTING_KEYS = new Set(["slug", "topic", "limit", "accounts"]); function queryKeys(text: string): string[] { const keys: string[] = []; // new URLSearchParams({ a: …, "b": …, c }) for (const m of text.matchAll(/new URLSearchParams\(\s*\{([^}]*)\}/g)) { for (const part of m[1].split(",")) { const key = /^\s*(?:\.\.\.)?["'`]?([A-Za-z_$][\w$]*)["'`]?\s*(?::|$)/.exec(part); if (key) keys.push(key[1]); } } // q.set("a", …) / q.append("a", …) for (const m of text.matchAll(/\.(?:set|append)\(\s*["'`]([^"'`]+)["'`]/g)) keys.push(m[1]); // …and a key written into the address by hand: ?a=… &a=… for (const m of text.matchAll(/\/api\/ops\/trending\?[^"'`\s]*/g)) for (const k of m[0].matchAll(/[?&]([A-Za-z_]+)=/g)) keys.push(k[1]); return keys; } test("posts-library: no MCP source asks the listing for the judge view's fields, or names the purge", async () => { const { readdirSync, readFileSync } = await import("node:fs"); const here = path.dirname(new URL(import.meta.url).pathname); const sources = readdirSync(here).filter((n) => /\.ts$/.test(n) && !n.includes(".test.")); assert.ok(sources.length > 5, "the MCP's sources were read"); let asking = 0; for (const name of sources) { const text = readFileSync(path.join(here, name), "utf8"); // Lane x-trending-publish: nor a way to organise a library or publish a // topic — those are asks of the editor, and a publish is the operator's. for (const word of ["/api/ops/organise-trending", "/api/ops/publish-trending", "publishXTrending", "organiseXTrending", "mergeXTrendingTopics", "published.json"]) { assert.ok(!text.includes(word), `mcp/src/${name} names ${word}`); } for (const word of ["purge-trending", "purgeXTrending", "removeXTrendingPostCapture", "/api/library", "judge-trending\"", "watch-trending\""]) { assert.ok(!text.includes(word), `mcp/src/${name} names ${word}`); } if (!text.includes("/api/ops/trending")) continue; asking++; const keys = queryKeys(text); assert.ok(keys.includes("slug"), `mcp/src/${name}: the query's keys were read (${keys.join(", ")})`); for (const key of keys) assert.ok(LISTING_KEYS.has(key), `mcp/src/${name} asks the listing for "${key}"`); } assert.ok(asking >= 1, "a source that asks the listing was read"); }); test("posts-library: the query sweep catches the shapes it guards against", () => { assert.deepEqual(queryKeys('const q = new URLSearchParams({ slug: channel, limit: "1" });\nq.set("topic", topic);'), ["slug", "limit", "topic"]); assert.deepEqual(queryKeys('new URLSearchParams({ slug, detail: "1" })'), ["slug", "detail"]); assert.deepEqual(queryKeys('q.set("ids", "1"); q.append("seen", marks);'), ["ids", "seen"]); assert.deepEqual(queryKeys("`/api/ops/trending?slug=${s}&state=drop&by=operator`"), ["slug", "state", "by"]); });