~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
#!/usr/bin/env python3
"""Seed a REALISTIC legacy plaintext ``.pkl`` RAG docstore (plus the matching
encrypted-DB rows) so the running server's two-phase migration can be exercised
end to end by ``test_legacy_pkl_migration_ci.js``.
WHY A PYTHON SEED IS MANDATORY (a pure-Puppeteer test is impossible here):
1. A browser cannot build a genuine pre-cutover LangChain-FAISS
``<hash>.faiss`` + plaintext ``<hash>.pkl`` pair — that needs
``langchain_community.vectorstores.FAISS`` + real all-MiniLM-L6-v2
embeddings (exactly the ``_make_legacy_index`` recipe the vector_stores
unit tests use).
2. A browser cannot seed the per-user encrypted SQLCipher rows
(Collection / SourceType / Document / DocumentCollection /
RagDocumentStatus / DocumentChunk / RAGIndex) that post-migration search
rehydrates chunk text from — that needs the password + the DB manager.
The migration itself is then driven by the REAL server:
* PHASE 1 — ``migrate_legacy_docstores()`` at web startup
(web/app.py). It scans ``<LDR_DATA_DIR>/cache/rag_indices`` and, for each
legacy ``<hash>.pkl``, writes a TEXT-FREE ``<hash>.idmap.json`` sidecar and
DELETES the ``.pkl``. This is why the seed MUST run BEFORE the server boots.
* PHASE 2 — ``rekey_user_indexes(username, password)`` at login. It rebuilds
the ``IndexIDMap2`` keyed by ``DocumentChunk.id`` from the sidecar (no
re-embed) and removes the sidecar.
This seeds TWO collections so the node test can prove the RIGHT ``.pkl`` was
extracted into the RIGHT sidecar / index (no cross-contamination):
* collection A carries the distinctive token SECRET_A,
* collection B (the decoy) carries SECRET_B.
Prepare that model first with ``scripts/ci/prepare_library_embeddings.py``
under the same data-volume cache. This seed deliberately remains cache-only.
Both indexes are built with the SAME real all-MiniLM-L6-v2 (384-dim) model the
server re-embeds the query with at search time, so cosine similarity is
meaningful and the rekey dimension guard (``old.d != embedding_dimension``)
passes.
Env (all optional, sensible defaults):
LDR_DATA_DIR disposable data dir shared with the server (REQUIRED in
practice; the run wrapper / CI step exports it).
LDR_MIGTEST_USER seeded username (default: migtest_user)
LDR_MIGTEST_PASS seeded password (default: MigTest!Secure#2026$LDR)
Writes a private manifest JSON to ``<LDR_DATA_DIR>/migtest_manifest.json`` for
the node test to consume — it shares the filesystem with the server, so no path
recomputation in JS is needed. Credentials stay in that file, out of stdout.
"""
import hashlib
import json
import os
import sys
import tempfile
import uuid
from pathlib import Path
# --------------------------------------------------------------------------
# Distinctive, unambiguous per-collection content. The tokens are what the
# node test greps the on-disk sidecars/cache for (must be ABSENT — text-free)
# and what the post-migration search must return (rehydrated from the DB).
# --------------------------------------------------------------------------
SECRET_A = "ZZZ_MIGTEST_ALPHA_LIGHTHOUSE_7a19f0"
SECRET_B = "ZZZ_MIGTEST_BRAVO_SUBMARINE_3c82e1"
EMBEDDING_MODEL = "all-MiniLM-L6-v2"
EMBEDDING_PROVIDER = "sentence_transformers" # EmbeddingProvider enum .value
EMBEDDING_DIM = 384
def _make_legacy_index(dir_path: Path, ids, texts, embeddings, index_name):
"""Write a REAL old-format ``<index_name>.faiss`` + plaintext
``<index_name>.pkl`` pair, exactly as the pre-cutover LangChain-FAISS
wrapper did (mirrors tests/vector_stores' ``_make_legacy_index``)."""
from langchain_community.vectorstores import FAISS
dir_path.mkdir(parents=True, exist_ok=True)
metadatas = [{"chunk": i} for i in range(len(texts))]
vs = FAISS.from_texts(texts, embeddings, metadatas=metadatas, ids=ids)
vs.save_local(str(dir_path), index_name=index_name)
def _index_hash(collection_name: str) -> str:
"""The EXACT hash LibraryRAGService._get_index_hash computes."""
hash_input = f"{collection_name}:{EMBEDDING_MODEL}:{EMBEDDING_PROVIDER}"
return hashlib.sha256(hash_input.encode()).hexdigest()
def _user_scope(username: str) -> str:
"""The EXACT per-user subdir LibraryRAGService._get_index_path derives."""
return hashlib.sha256(username.encode("utf-8")).hexdigest()[:16]
def _write_manifest(data_dir: Path, manifest: dict) -> Path:
"""Keep fixture credentials private, including when replacing an old file."""
manifest_path = data_dir / "migtest_manifest.json"
temporary_path = None
try:
# NamedTemporaryFile creates the file with 0600 regardless of umask.
# Replacing the destination also repairs an existing permissive mode
# and avoids following a destination symlink.
with tempfile.NamedTemporaryFile(
mode="w",
encoding="utf-8",
dir=data_dir,
prefix=".migtest_manifest-",
delete=False,
) as output:
temporary_path = Path(output.name)
json.dump(manifest, output, indent=2)
temporary_path.replace(manifest_path)
finally:
if temporary_path is not None:
temporary_path.unlink(missing_ok=True)
print("Legacy migration fixture manifest is ready")
return manifest_path
def main() -> int:
data_dir = os.environ.get("LDR_DATA_DIR")
if not data_dir:
print(
"FATAL: LDR_DATA_DIR must be set (a disposable dir shared with the "
"throwaway server) so phase-1 runs on the seeded .pkl.",
file=sys.stderr,
)
return 2
data_dir = Path(data_dir)
data_dir.mkdir(parents=True, exist_ok=True)
(data_dir / "encrypted_databases").mkdir(parents=True, exist_ok=True)
username = os.environ.get("LDR_MIGTEST_USER", "migtest_user")
password = os.environ.get(
"LDR_MIGTEST_PASS", "MigTest!Secure#2026$LDR"
) # pragma: allowlist secret
# Import after LDR_DATA_DIR is settled so path resolution is correct.
from local_deep_research.config.paths import get_cache_directory
from local_deep_research.database.auth_db import (
get_auth_db_session,
init_auth_database,
)
from local_deep_research.database.encrypted_db import db_manager
from local_deep_research.database.models.auth import User
from local_deep_research.database.models.library import (
Collection,
Document,
DocumentChunk,
DocumentCollection,
DocumentStatus,
EmbeddingProvider,
RAGIndex,
RagDocumentStatus,
SourceType,
)
from local_deep_research.database.session_context import (
get_user_db_session,
)
from local_deep_research.embeddings.embeddings_config import get_embeddings
# ---------------------------------------------------------------------
# 1. Auth user + encrypted per-user database (mirrors init_test_database).
# MUST run under the same KDF env as the server or login 401s.
# ---------------------------------------------------------------------
init_auth_database()
auth_session = get_auth_db_session()
try:
existing = auth_session.query(User).filter_by(username=username).first()
if existing is None:
auth_session.add(User(username=username))
auth_session.commit()
finally:
auth_session.close()
# Idempotent for a fresh disposable dir; create_user_database is a no-op /
# returns the existing DB if already present.
db_manager.create_user_database(username, password)
# Real all-MiniLM-L6-v2 embeddings — same model the server re-embeds the
# query with at search time. The setup process must have prepared the
# shared HF cache already; this snapshot-less load cannot download it.
embeddings = get_embeddings(
provider=EMBEDDING_PROVIDER, model=EMBEDDING_MODEL
)
cache_root = get_cache_directory() / "rag_indices"
user_dir = cache_root / _user_scope(username)
manifest = {
"username": username,
"password": password,
"user_scope": _user_scope(username),
"cache_root": str(cache_root),
"user_dir": str(user_dir),
"collections": {},
}
# Two collections: A (primary, SECRET_A) and B (decoy, SECRET_B).
plan = [
{
"key": "A",
"name": "MigTest Alpha",
"secret": SECRET_A,
"texts": [
f"The alpha coastal lighthouse guided fishing vessels through "
f"fog for a century. Marker token {SECRET_A} appears here.",
f"A second alpha passage about the rotating lens and the "
f"keeper's leather-bound storm journal. {SECRET_A}",
],
},
{
"key": "B",
"name": "MigTest Bravo",
"secret": SECRET_B,
"texts": [
f"The bravo research submarine mapped deep ocean trenches for "
f"decades. Marker token {SECRET_B} appears here.",
f"A second bravo passage about sonar arrays and the pressure "
f"hull's titanium plating. {SECRET_B}",
],
},
]
with get_user_db_session(username, password) as session:
# SourceType is FK'd by Document; get-or-create by unique name.
source_type = (
session.query(SourceType).filter_by(name="document").first()
)
if source_type is None:
source_type = SourceType(
id=uuid.uuid4().hex,
name="document",
display_name="Document",
)
session.add(source_type)
session.flush()
for spec in plan:
collection_id = uuid.uuid4().hex
collection_name = f"collection_{collection_id}"
index_hash = _index_hash(collection_name)
faiss_path = user_dir / f"{index_hash}.faiss"
pkl_path = user_dir / f"{index_hash}.pkl"
sidecar_path = user_dir / f"{index_hash}.idmap.json"
texts = spec["texts"]
uuids = [uuid.uuid4().hex for _ in texts]
# -- encrypted-DB rows --------------------------------------
session.add(
Collection(
id=collection_id,
name=spec["name"],
collection_type="user_collection",
# Search uses collection settings before consulting the
# RAGIndex. Keep the legacy model explicit even when the
# application's default model changes.
embedding_model=EMBEDDING_MODEL,
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=EMBEDDING_DIM,
)
)
doc_id = uuid.uuid4().hex
body = "\n\n".join(texts)
session.add(
Document(
id=doc_id,
source_type_id=source_type.id,
document_hash=hashlib.sha256(
f"{collection_id}:{body}".encode()
).hexdigest(),
title=f"{spec['name']} document",
text_content=body,
filename=f"{spec['key'].lower()}_doc.txt",
file_size=len(body),
file_type="txt",
status=DocumentStatus.COMPLETED,
)
)
for i, (u, text) in enumerate(zip(uuids, texts)):
session.add(
DocumentChunk(
chunk_hash=hashlib.sha256(
f"{u}:{text}".encode()
).hexdigest(),
source_type="document",
source_id=doc_id,
collection_name=collection_name,
chunk_text=text,
chunk_index=i,
start_char=0,
end_char=len(text),
word_count=len(text.split()),
embedding_id=u, # MUST equal the .pkl id for rekey
embedding_model=EMBEDDING_MODEL,
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=EMBEDDING_DIM,
)
)
rag_index = RAGIndex(
collection_name=collection_name,
embedding_model=EMBEDDING_MODEL,
embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
embedding_dimension=EMBEDDING_DIM,
index_path=str(faiss_path),
index_hash=index_hash,
chunk_size=1000,
chunk_overlap=200,
# Pin the physical-build config to the LibraryRAGService
# defaults so rekey/search never fall back to settings lookups.
index_type="flat",
distance_metric="cosine",
normalize_vectors=True,
is_current=True,
)
session.add(rag_index)
session.flush() # need rag_index.id for RagDocumentStatus
# DocumentCollection + RagDocumentStatus: without these the real
# HTTP search path returns [] before ever touching FAISS
# (get_rag_stats()['indexed_documents'] counts RagDocumentStatus).
session.add(
DocumentCollection(
document_id=doc_id,
collection_id=collection_id,
indexed=True,
chunk_count=len(texts),
)
)
session.add(
RagDocumentStatus(
document_id=doc_id,
collection_id=collection_id,
rag_index_id=rag_index.id,
chunk_count=len(texts),
)
)
# -- legacy on-disk .faiss + plaintext .pkl -----------------
_make_legacy_index(
user_dir, uuids, texts, embeddings, index_name=index_hash
)
# Control: the raw legacy pickle really DOES hold the plaintext
# marker (proves the node test's "text is gone" grep is not
# vacuously passing on a fixture that never had it).
pkl_bytes = pkl_path.read_bytes()
assert spec["secret"].encode() in pkl_bytes, (
f"fixture control failed: {spec['secret']} not in {pkl_path}"
)
assert faiss_path.is_file(), f"missing {faiss_path}"
manifest["collections"][spec["key"]] = {
"collection_id": collection_id,
"collection_name": collection_name,
"index_hash": index_hash,
"secret": spec["secret"],
"faiss": str(faiss_path),
"pkl": str(pkl_path),
"sidecar": str(sidecar_path),
"document_id": doc_id,
"embedding_ids": uuids,
}
session.commit()
manifest_path = _write_manifest(data_dir, manifest)
print(f"\nSeed complete. Manifest: {manifest_path}", file=sys.stderr)
print(
f"Legacy fixtures seeded under: {user_dir}\n"
f" A (SECRET_A) index_hash={manifest['collections']['A']['index_hash']}\n"
f" B (SECRET_B) index_hash={manifest['collections']['B']['index_hash']}",
file=sys.stderr,
)
return 0
if __name__ == "__main__":
sys.exit(main())