// AN X TRENDING LIBRARY IS ON NO SITE AND IN NO BUILD (lane x-trending, hard // rule 1). Three layers, each pinned here over a temp corpus with a marker in // every private file: // // 1. the site writer refuses a library as a member or a cited channel // (lib/site.ts writeSite — the one writer every site edit goes through); // 2. the index build skips it: no export index file and no LMDB record // carries its posts — EVEN WHEN a site.json written by hand names it // (the case layer 1 cannot catch), and even on a PRIVATE site, the most // permissive build there is; // 3. the site compose then has nothing of it to copy: the public dir, // site.json, corpus.json and llms.txt never name it. // A positive control (an ordinary X account channel's post, on the same // private site) proves each consumer did read posts. The MCP's corpus tools // over this kind of composed dir: mcp/src/xTrendingPrivacy.test.ts. // // Run with: node_modules/.bin/tsx --test common/bin/xTrendingPrivacy.test.ts import { after, test } from "node:test"; import assert from "node:assert/strict"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; const ROOT = mkdtempSync(path.join(tmpdir(), "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("../lib/paths"); const { buildIndex } = await import("../controller/buildIndex"); const { main: composeSite } = await import("./compose-site"); const { getSite, writeSite } = await import("../lib/site"); const { writePosts } = await import("../lib/posts-server"); const { recordXTrendingRead, writeXTrendingPosts, trendingReadDir, listXTrending, readXTrendingTopics: readXTrendingTopicsOf } = await import("../lib/xTrending/store"); const { X_TRENDING_NOT_ON_A_SITE } = await import("../lib/xTrending/config"); const { open } = await import("lmdb"); const paths = getPaths(); const LIB = "trending-lib"; const PUBLIC_X = "kettle-org-x"; const PUBLIC_POST = "an ordinary archived post about kettles"; const POST_MARK = "trendpostmarkerq7x"; const HANDLE_MARK = "trendhandlemarkerz4k"; const TOPIC_MARK = "trendtopicmarkerw2p"; const RAW_MARK = "trendrawmarkerj9m"; // Lane x-trending-2: the judgement's note, the captured media's bytes and // address, the capture's context and the shot — planted below, and looked for // in everything a build writes, like the four above. const { plantSlice2Library, SLICE2_CAPTURES_DIRNAME, SLICE2_FILE_NAMES, SLICE2_MARK_LIST } = await import("../lib/xTrending/__fixtures__/slice2Library"); const MARKS = [POST_MARK, HANDLE_MARK, TOPIC_MARK, RAW_MARK, ...SLICE2_MARK_LIST]; const READ = "tr-01JA0000000000000000000000"; const quiet = () => {}; 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, "1900000000000005001", "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, "1900000000000006001", HANDLE_MARK, `a trending post ${POST_MARK}`)]; // 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 ${TOPIC_MARK}`, postIds: ["1900000000000006001"], 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: RAW_MARK } })}\n`); const planted = (await readXTrendingTopicsOf(libRoot))[0]; await plantSlice2Library(libRoot, { postId: "1900000000000006001", topicId: planted.id }); const site = (channels: string[]) => ({ siteId: "priv", siteTitle: "priv", siteDescription: "fixture", headerTitle: "priv", homeTagline: "", socialLinks: [], groups: [{ id: "default", name: "All channels", selectedByDefault: true }], defaultGroupId: "default", channels: channels.map((slug) => ({ slug, groupId: "default" })), siteUrl: "https://priv.example.test", archives: false, audience: "private", }); // Written BY HAND, naming the library: what the site writer would refuse. writeJson(path.join(paths.sitesDir, "priv", "site.json"), site([PUBLIC_X, LIB])); function filesUnder(dir: string): { rel: string; text: string }[] { const out: { rel: string; text: string }[] = []; const walk = (d: string) => { for (const e of readdirSync(d, { withFileTypes: true })) { const p = path.join(d, e.name); if (e.isDirectory()) walk(p); else if (e.isFile() || (e.isSymbolicLink() && statSync(p).isFile())) out.push({ rel: path.relative(dir, p), text: readFileSync(p).toString("latin1") }); } }; if (existsSync(dir)) walk(dir); return out; } // No file is named for the library, and none holds a marker or its slug. function assertNoLeak(files: { rel: string; text: string }[], where: string, opts: { slugInText?: boolean } = {}) { for (const f of files) { assert.ok(!f.rel.split(path.sep).includes(LIB), `${where}: a file under ${f.rel}`); for (const mark of MARKS) assert.ok(!f.text.includes(mark), `${where}: ${f.rel} holds ${mark}`); if (opts.slugInText !== false) assert.ok(!f.text.includes(LIB), `${where}: ${f.rel} names ${LIB}`); } } const hasPublicPost = (files: { text: string }[]) => files.some((f) => f.text.includes(PUBLIC_POST)); test("layer 1: the site writer refuses a library as a member and as a cited channel", async () => { const stored = getSite("priv", paths); await assert.rejects(writeSite({ ...stored, channels: [{ slug: LIB, groupId: "default" }] }, paths), { message: X_TRENDING_NOT_ON_A_SITE(LIB), }); await assert.rejects( writeSite({ ...stored, channels: [{ slug: PUBLIC_X, groupId: "default" }], citedChannels: [{ slug: LIB }] } as never, paths), { message: `${LIB} is an X trending library: it is private, and never put on a site.` }, ); // The same site without it is written. await writeSite({ ...stored, channels: [{ slug: PUBLIC_X, groupId: "default" }] }, paths); // And the hand-written one is put back for the layers below. writeJson(path.join(paths.sitesDir, "priv", "site.json"), site([PUBLIC_X, LIB])); }); test("layer 2: the index build skips the library — no export index file, no LMDB record — though a site.json names it", async () => { const log: string[] = []; await buildIndex({ paths, onLog: (l: string) => log.push(l) }); assert.ok(log.some((l) => l.includes(`Skipping channel ${LIB}: an X trending library`)), "the build says it skipped it"); const files = filesUnder(paths.exportIndexDir); assert.ok(hasPublicPost(files), "control: the account channel's post is in the index"); // The site's own manifest may list the slug the hand-written site.json // names; no file is the library's and none holds its words. assertNoLeak(files, "export index", { slugInText: false }); const root = open({ path: paths.lmdbPath, maxDbs: 18, compression: true }); try { let seenPublic = false; for (const name of ["sums", "cues", "subs", "alts", "mtimes", "byChannel", "pageHashes", "subPageHashes", "channelStats", "posts", "postPageHashes", "channelPostsStats", "digests", "digestPageHashes", "channelDigestStats", "videoState", "meta"]) { const db = root.openDB({ name, encoding: "msgpack" }); for (const { key, value } of db.getRange()) { const text = JSON.stringify([key, value]); if (text.includes(PUBLIC_POST)) seenPublic = true; for (const mark of MARKS) assert.ok(!text.includes(mark), `LMDB ${name}: a record holds ${mark}`); // `meta` keeps each site's membership fingerprint, which repeats the // slug the hand-written site.json names; no other record may. if (name !== "meta") assert.ok(!text.includes(LIB), `LMDB ${name}: a record names ${LIB}`); } } assert.ok(seenPublic, "control: the account channel's post is in LMDB"); } finally { await root.close(); } }); test("layer 3: the composed site — pages, site.json, corpus.json, llms.txt — carries nothing of the library", async () => { await buildIndex({ paths, onLog: quiet }); const log = console.log; const warn = console.warn; console.log = quiet; console.warn = quiet; try { await composeSite({ siteId: "priv", paths }); } finally { console.log = log; console.warn = warn; } const files = filesUnder(paths.exportPublicDir); assert.ok(hasPublicPost(files), "control: the account channel's post is composed"); assertNoLeak(files, "public dir"); for (const name of ["corpus.json", "llms.txt", "site.json"]) { const f = files.find((x) => x.rel === name); assert.ok(f, `${name} was composed`); assert.ok(f!.text.includes(PUBLIC_X), `control: ${name} names the account channel`); } }); test("the library's own listing needs no index: it reads its files", async () => { const listing = await listXTrending(libRoot); assert.equal(listing.total, 1); assert.equal(listing.posts[0].author, HANDLE_MARK); assert.ok(listing.posts[0].text.includes(POST_MARK)); // And never the raw capture. assert.ok(!JSON.stringify(listing).includes(RAW_MARK)); }); // Lane x-trending-2. test("what a keep, a capture and a watch leave in a library is in no build: not its words, not its bytes, not a file of its name", async () => { // Planted for real: the files exist, where the editor leaves them. for (const rel of ["judgements.json", "watch.json", "media-index.json", "posts-availability.json", ".gitignore", path.join(SLICE2_CAPTURES_DIRNAME, "1900000000000006001", "capture.json")]) { assert.ok(existsSync(path.join(libRoot, "trending", rel)), `${rel} was planted`); } const built = [...filesUnder(paths.exportIndexDir), ...filesUnder(paths.exportPublicDir)]; assert.ok(built.length > 0, "the builds above wrote files"); assertNoLeak(built, "index and public dirs"); for (const f of built) { for (const name of SLICE2_FILE_NAMES) { assert.ok(!f.rel.split(path.sep).includes(name), `a build wrote ${f.rel}`); } } // The library's own listing does carry the judgement — and that is the only door. const listing = await listXTrending(libRoot); assert.equal(listing.posts[0].judgement?.state, "keep"); assert.equal(listing.topics[0].watch?.on, true); }); test("the search itself catches a leak", () => { assert.throws(() => assertNoLeak([{ rel: path.join("posts", LIB, "page-0001.json"), text: "" }], "x")); for (const mark of MARKS) assert.throws(() => assertNoLeak([{ rel: "page-0001.json", text: `{"q":"${mark}"}` }], "x")); assert.throws(() => assertNoLeak([{ rel: "corpus.json", text: `{"slug":"${LIB}"}` }], "x")); assert.doesNotThrow(() => assertNoLeak([{ rel: "page-0001.json", text: PUBLIC_POST }], "x")); }); // ── lane posts-library: what the judge view added stays behind the same doors ── const JUDGE_VIEW_ONLY = ["xTrending/purge", "purgeXTrendingDropped", "removeXTrendingPostCapture", "summarizeXTrending", "xTrendingCaptureFiles", "purge-trending"]; // Every call of `fn` in a source, with its WHOLE argument text (parentheses balanced, so a call whose // first argument is itself a call — `listXTrending(path.join(a, b), { detail: true })` — is read to its end). function callsOf(text: string, fn: string): string[] { const out: string[] = []; for (let at = text.indexOf(`${fn}(`); at >= 0; at = text.indexOf(`${fn}(`, at + 1)) { let depth = 0; let end = -1; for (let i = at + fn.length; i < text.length; i++) { if (text[i] === "(") depth++; else if (text[i] === ")" && --depth === 0) { end = i; break; } } out.push(text.slice(at + fn.length + 1, end < 0 ? text.length : end)); } return out; } // The judge view's asks of the listing: no publishing source may make one. const JUDGE_VIEW_ASKS = /\b(detail|ids|filter|offset|seen)\b\s*[:,}]/; function judgeViewAsk(text: string): string | null { for (const args of callsOf(text, "listXTrending")) { // The options are everything after the first argument (the library's root). if (JUDGE_VIEW_ASKS.test(args)) return `listXTrending(${args.replace(/\s+/g, " ").slice(0, 120)})`; } return null; } function sourcesUnder(dir: string): { rel: string; text: string }[] { const out: { rel: string; text: string }[] = []; const walk = (d: string) => { for (const e of readdirSync(d, { withFileTypes: true })) { if (e.name === "node_modules" || e.name.startsWith(".")) continue; const p = path.join(d, e.name); if (e.isDirectory()) walk(p); else if (/\.(ts|tsx|mjs)$/.test(e.name) && !/\.test\.|\.unit\./.test(e.name)) out.push({ rel: p, text: readFileSync(p, "utf8") }); } }; if (existsSync(dir)) walk(dir); return out; } test("the judge view's listing carries the kept post's note and its capture's context; no build wrote them, and it gives no address or path", async () => { const { SLICE2_MARKS } = await import("../lib/xTrending/__fixtures__/slice2Library"); const { purgeXTrendingDropped } = await import("../lib/xTrending/purge"); const { summarizeXTrending } = await import("../lib/xTrending/store"); const detail = JSON.stringify(await listXTrending(libRoot, { detail: true })); // It IS there for the one door that asks (so the search below watches the right words). assert.ok(detail.includes(SLICE2_MARKS.note), "the detail listing carries the judgement's note"); assert.ok(detail.includes(SLICE2_MARKS.context), "the detail listing carries the capture's context"); // Names only: no media address, no bytes, no path on disk. assert.ok(!detail.includes(SLICE2_MARKS.address) && !detail.includes(SLICE2_MARKS.media) && !detail.includes(SLICE2_MARKS.shot)); assert.ok(!detail.includes(libRoot) && !detail.includes("responses.jsonl")); // The ids listing, the one-line summary and a purge's dry run say nothing of a post's words. const idsOnly = await listXTrending(libRoot, { ids: true }); assert.deepEqual(idsOnly.posts, []); for (const answer of [idsOnly.ids, await summarizeXTrending(libRoot), await purgeXTrendingDropped(libRoot, { slug: LIB, dryRun: true, captureInFlight: () => false })]) { const text = JSON.stringify(answer); for (const mark of [...MARKS, ...SLICE2_MARK_LIST]) assert.ok(!text.includes(mark), `an answer carries ${mark}`); assert.ok(!text.includes(libRoot)); } // And still nothing of it in what the builds above wrote. assertNoLeak([...filesUnder(paths.exportIndexDir), ...filesUnder(paths.exportPublicDir)], "index and public dirs, after the judge view's reads"); }); test("no build, publisher, export, homepage or MCP module reaches the judge view's reads or the purge", () => { const repo = path.resolve(path.dirname(new URL(import.meta.url).pathname), "..", ".."); const dirs = ["common/bin", "common/publish", "common/views", "export/app", "export/lib", "homepage/app", "homepage/lib", "mcp/src"].map((d) => path.join(repo, d)); const files = [...dirs.flatMap(sourcesUnder), ...sourcesUnder(path.join(repo, "common", "controller")).filter((f) => /buildIndex|compose|report/i.test(path.basename(f.rel)))]; assert.ok(files.length > 50, "the sweep read the publishing sources"); for (const f of files) { for (const word of JUDGE_VIEW_ONLY) assert.ok(!f.text.includes(word), `${path.relative(repo, f.rel)} names ${word}`); // The listing is asked there with a topic and a limit at most: never the judge view's detail, ids, filter or marks. assert.equal(judgeViewAsk(f.text), null, `${path.relative(repo, f.rel)} asks the listing for the judge view's fields`); } }); test("the sweep catches the call shapes it guards against", () => { // The shape a real caller has: the root built by a call, the options after it, over several lines. assert.match(judgeViewAsk('const l = await listXTrending(path.join(paths.channelsDir, slug), { detail: true });') ?? "", /detail: true/); assert.ok(judgeViewAsk("await listXTrending(\n path.join(a, b),\n {\n ...(topic ? { topic } : {}),\n ids: true,\n },\n);")); assert.ok(judgeViewAsk("listXTrending(root(x), { filter: { state: \"drop\" } })")); assert.ok(judgeViewAsk("listXTrending(root(x), { limit: 5, offset })")); assert.ok(judgeViewAsk("listXTrending(root(x), { topic, seen })")); // What a publishing source may ask: a root, a topic, a limit. assert.equal(judgeViewAsk("listXTrending(path.join(a, b), { ...(topic ? { topic } : {}), ...(limit !== undefined ? { limit } : {}) })"), null); assert.equal(judgeViewAsk("listXTrending(libRoot)"), null); assert.equal(judgeViewAsk("// a comment about detail and ids\nfoo(detail)"), null); });