~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
/**
* Legacy plaintext-.pkl RAG migration — end-to-end UI test (library shard).
*
* PR #5143 replaced the old LangChain-FAISS wrapper (which pickled chunk TEXT
* into a plaintext `<hash>.pkl` docstore on disk, defeating the per-user
* SQLCipher DB encryption) with a raw faiss IndexIDMap2 keyed by integer
* DocumentChunk.id that keeps text ONLY in the encrypted DB. A two-phase
* migration removes the legacy plaintext .pkl:
*
* PHASE 1 migrate_legacy_docstores() — at WEB STARTUP. For each legacy
* `<hash>.pkl` it writes a TEXT-FREE `<hash>.idmap.json` sidecar
* (position->uuid map) and DELETES the `.pkl`.
* PHASE 2 rekey_user_indexes(user, pw) — at LOGIN. Rebuilds the IndexIDMap2
* from the sidecar keyed by DocumentChunk.id (NO re-embed) and
* removes the sidecar.
*
* This test drives the REAL migration through the running server. A Python
* pre-step (`seed_legacy_pkl_migration.py`, run BEFORE the server boots) seeds
* a realistic legacy `.faiss` + plaintext `.pkl` pair for TWO collections —
* primary A (SECRET_A) and decoy B (SECRET_B) — plus the matching encrypted-DB
* rows. The server's own startup then runs phase-1 on the seeded `.pkl`s; this
* test's login drives phase-2; and a real search proves the seeded content is
* still retrievable (text rehydrated from the encrypted DB by id).
*
* Assertions:
* - PHASE 1 (server already booted, before login):
* * the plaintext `<hash>.pkl` is GONE for both collections,
* * a TEXT-FREE `<hash>.idmap.json` sidecar EXISTS for both (valid JSON,
* position->uuid, no SECRET bytes),
* * NO file under the RAG cache contains SECRET_A/SECRET_B (plaintext
* gone from disk entirely — the `.pkl` held it, nothing else may).
* - PHASE 2 (after login triggers rekey):
* * both sidecars are consumed (gone).
* - END-TO-END search (real HTTP, rehydration by int id):
* * searching collection A returns SECRET_A and NOT the decoy SECRET_B,
* * searching collection B returns SECRET_B and NOT SECRET_A
* — the "extract the RIGHT file" proof (each `.pkl`'s map went into the
* correct sidecar, rebuilt into the correct collection's index keyed by
* the correct DocumentChunk.ids).
*
* On-disk assertions require this process to share the filesystem with the
* server (true locally, and in CI when the data dir is mounted into the test
* container). When the manifest/data dir is NOT accessible they are skipped
* with a clear notice and the HTTP search assertions (which need only the
* browser + server) still run.
*
* Registered in the `library` shard (tests/ui_tests/run_all_tests.js). Seed +
* boot ordering is handled by tests/ui_tests/run_legacy_pkl_migration.sh
* locally and a dedicated seed step in .github/workflows/docker-tests.yml.
*
* Server base URL: LDR_TEST_URL (default http://127.0.0.1:5000).
* Manifest path: LDR_MIGTEST_MANIFEST, else <LDR_DATA_DIR>/migtest_manifest.json.
*/
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");
const AuthHelper = require("./auth_helper");
const {
getPuppeteerLaunchOptions,
takeScreenshot,
} = require("./puppeteer_config");
const BASE_URL = process.env.LDR_TEST_URL || "http://127.0.0.1:5000";
function resolveManifestPath() {
if (process.env.LDR_MIGTEST_MANIFEST) {
return process.env.LDR_MIGTEST_MANIFEST;
}
const dataDir = process.env.LDR_DATA_DIR || "/tmp/ldr-migtest";
return path.join(dataDir, "migtest_manifest.json");
}
/** Recursively list every file under `dir` (returns [] if dir is missing). */
function walkFiles(dir) {
const out = [];
let entries;
try {
entries = fs.readdirSync(dir, { withFileTypes: true });
} catch (_) {
return out;
}
for (const e of entries) {
const full = path.join(dir, e.name);
if (e.isDirectory()) {
out.push(...walkFiles(full));
} else if (e.isFile()) {
out.push(full);
}
}
return out;
}
/** Call the app's own API from inside the page (carries the session cookie +
* CSRF token read from the page's meta tag). */
async function apiPost(page, url, payload) {
return page.evaluate(
async (u, body) => {
const meta = document.querySelector('meta[name="csrf-token"]');
const csrf = meta ? meta.getAttribute("content") : "";
const resp = await fetch(u, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-CSRFToken": csrf,
},
body: JSON.stringify(body),
});
let parsed;
try {
parsed = await resp.json();
} catch (_) {
parsed = null;
}
return { ok: resp.ok, httpStatus: resp.status, body: parsed };
},
url,
payload,
);
}
async function runTests() {
const manifestPath = resolveManifestPath();
let manifest;
try {
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
} catch (err) {
console.log(
` ❌ could not read seed manifest at ${manifestPath}: ${err.message}`,
);
console.log(
" Run the seed (tests/ui_tests/seed_legacy_pkl_migration.py) " +
"under the same LDR_DATA_DIR BEFORE the server boots — see " +
"tests/ui_tests/run_legacy_pkl_migration.sh.",
);
process.exit(1);
}
const A = manifest.collections.A;
const B = manifest.collections.B;
const userDir = manifest.user_dir;
const cacheRoot = manifest.cache_root;
const SECRET_A = A.secret;
const SECRET_B = B.secret;
// Can this process see the server's on-disk files? (True locally; in CI
// only when the data dir is mounted into the test container.)
const diskShared = fs.existsSync(userDir) || fs.existsSync(cacheRoot);
let passed = 0;
let failed = 0;
const ok = (label) => {
passed += 1;
console.log(` ✅ ${label}`);
};
const fail = (label, detail) => {
failed += 1;
console.log(` ❌ ${label}${detail ? ` — ${detail}` : ""}`);
};
// ---- PHASE 1 on-disk assertions (server already booted) --------------
// These MUST be checked BEFORE login (phase-2 consumes the sidecars).
if (diskShared) {
for (const [key, c] of [
["A", A],
["B", B],
]) {
if (fs.existsSync(c.pkl)) {
fail(
`phase-1: plaintext .pkl for collection ${key} still on disk`,
c.pkl,
);
} else {
ok(`phase-1: plaintext .pkl deleted for collection ${key}`);
}
if (!fs.existsSync(c.sidecar)) {
fail(
`phase-1: text-free sidecar missing for collection ${key}`,
c.sidecar,
);
} else {
const raw = fs.readFileSync(c.sidecar, "utf-8");
let mapObj;
try {
mapObj = JSON.parse(raw);
} catch (_) {
mapObj = null;
}
const isPosUuidMap =
mapObj &&
typeof mapObj === "object" &&
Object.keys(mapObj).length > 0 &&
Object.entries(mapObj).every(
([k, v]) =>
/^\d+$/.test(k) &&
typeof v === "string" &&
/^[0-9a-f]{32}$|^[0-9a-f-]{36}$/i.test(v),
);
const textFree =
!raw.includes(SECRET_A) && !raw.includes(SECRET_B);
if (isPosUuidMap && textFree) {
ok(
`phase-1: text-free position->uuid sidecar written for collection ${key}`,
);
} else {
fail(
`phase-1: sidecar for collection ${key} is not a text-free position->uuid map`,
`isMap=${isPosUuidMap} textFree=${textFree}`,
);
}
}
}
// Whole-tree grep: NO on-disk artifact under the RAG cache may still
// contain the plaintext markers — the .pkl held them, and phase-1's
// guarantee is that nothing else does.
const leaking = walkFiles(cacheRoot).filter((f) => {
let bytes;
try {
bytes = fs.readFileSync(f);
} catch (_) {
return false;
}
return (
bytes.includes(Buffer.from(SECRET_A)) ||
bytes.includes(Buffer.from(SECRET_B))
);
});
if (leaking.length === 0) {
ok("phase-1: no plaintext SECRET bytes anywhere under the RAG cache");
} else {
fail(
"phase-1: plaintext SECRET bytes still present on disk",
leaking.join(", "),
);
}
} else {
console.log(
" ℹ️ data dir not shared with this process — skipping on-disk " +
"phase-1/phase-2 assertions; the HTTP search assertions below " +
"still prove the migration end to end.",
);
}
const browser = await puppeteer.launch(getPuppeteerLaunchOptions());
const page = await browser.newPage();
await page.setViewport({ width: 1400, height: 900 });
page.on("dialog", async (d) => {
try {
await d.accept();
} catch (_) {
/* already handled */
}
});
page.on("console", (m) => {
if (m.type() === "error") {
console.log(" [browser console error]", m.text());
}
});
try {
// ---- Log in as the SEEDED user -> triggers phase-2 rekey at login --
const authHelper = new AuthHelper(page, BASE_URL);
await authHelper.login(manifest.username, manifest.password);
const activePage = authHelper.getPage();
ok(`logged in as seeded user ${manifest.username} (triggers phase-2)`);
// ---- PHASE 2 on-disk assertion: sidecars consumed by rekey ---------
if (diskShared) {
// Rekey runs in a post-login background path; give it a moment.
const deadline = Date.now() + 30000;
let aGone = false;
let bGone = false;
while (Date.now() < deadline) {
aGone = !fs.existsSync(A.sidecar);
bGone = !fs.existsSync(B.sidecar);
if (aGone && bGone) break;
await new Promise((r) => setTimeout(r, 1000));
}
if (aGone && bGone) {
ok("phase-2: both sidecars consumed (removed) by login-time rekey");
} else {
fail(
"phase-2: sidecar(s) not consumed after login",
`A gone=${aGone} B gone=${bGone}`,
);
}
}
// ---- END-TO-END search: RIGHT-file / no cross-contamination --------
// Navigate to a page with a CSRF meta tag (collection details).
await activePage.goto(
`${BASE_URL}/library/collections/${A.collection_id}`,
{ waitUntil: "domcontentloaded", timeout: 30000 },
);
const searchA = await apiPost(
activePage,
`/library/api/collections/${A.collection_id}/search`,
{
query: "a coastal lighthouse guiding fishing boats through fog",
limit: 5,
},
);
const bodyA = JSON.stringify(searchA.body || {});
if (!searchA.ok) {
fail(
"search A failed",
`http ${searchA.httpStatus}: ${bodyA.slice(0, 200)}`,
);
} else if (bodyA.includes(SECRET_A) && !bodyA.includes(SECRET_B)) {
ok(
"search collection A returns SECRET_A and NOT the decoy SECRET_B " +
"(rehydrated by int id after rekey)",
);
} else {
fail(
"search A did not return the RIGHT content",
`hasA=${bodyA.includes(SECRET_A)} hasB=${bodyA.includes(
SECRET_B,
)}`,
);
}
const searchB = await apiPost(
activePage,
`/library/api/collections/${B.collection_id}/search`,
{
query: "a research submarine mapping deep ocean trenches with sonar",
limit: 5,
},
);
const bodyB = JSON.stringify(searchB.body || {});
if (!searchB.ok) {
fail(
"search B failed",
`http ${searchB.httpStatus}: ${bodyB.slice(0, 200)}`,
);
} else if (bodyB.includes(SECRET_B) && !bodyB.includes(SECRET_A)) {
ok(
"search collection B returns SECRET_B and NOT SECRET_A " +
"(the RIGHT .pkl was extracted into the RIGHT index)",
);
} else {
fail(
"search B did not return the RIGHT content",
`hasB=${bodyB.includes(SECRET_B)} hasA=${bodyB.includes(
SECRET_A,
)}`,
);
}
} catch (err) {
fail(err && err.message ? err.message : String(err));
try {
await takeScreenshot(page, "/tmp/legacy_pkl_migration_error.png");
} catch (_) {
/* best effort */
}
} finally {
await browser.close();
}
console.log(
`\n📊 Legacy .pkl migration E2E: ${passed} passed, ${failed} failed`,
);
process.exit(failed > 0 ? 1 : 0);
}
runTests();