~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
"""One-time migration of legacy RAG index docstore files off disk.
The previous RAG index format persisted a companion ``<hash>.pkl`` docstore next
to each ``<hash>.faiss``. That docstore held the chunk **text** (plaintext at
rest) plus the ``index_to_docstore_id`` map (FAISS position -> uuid) that the
index needs. The current format stores no text in the index — text lives only in
the encrypted DB and is rehydrated by id.
This runs at startup (pure filesystem, no login / DB access). For each legacy
``.pkl`` it:
1. extracts ONLY the ``index_to_docstore_id`` map (position -> uuid) into a
small, **text-free** ``<hash>.idmap.json`` sidecar (plain JSON — no pickle,
no text, no metadata), then
2. deletes the ``.pkl`` — removing all plaintext from disk.
The per-user, login-time re-key (elsewhere) later consumes the sidecar to
rebuild the index keyed by ``DocumentChunk.id`` (no re-embedding), then deletes
the sidecar. If a ``.pkl`` can't be read (corrupt), it is deleted anyway
(plaintext removed) and that collection falls back to a normal reindex.
IMPORTANT: this MUST ship in the same release as the vector-store cutover.
Before the cutover the old code still reads the ``.pkl``'s text for snippets, so
removing it early would break search — do NOT wire this into startup until the
old FAISS path is gone. It is exposed as a plain function so it can be called
from tests and from the cutover's startup hook.
"""
import json
import os
import re
import stat
import tempfile
import time
from pathlib import Path
from typing import Dict, List, Mapping, Tuple
import numpy as np
from faiss import (
IndexFlatIP,
IndexFlatL2,
IndexHNSWFlat,
IndexIDMap,
IndexIDMap2,
read_index,
vector_to_array,
)
from loguru import logger
from ..config.paths import get_cache_directory
from ..research_library.services.faiss_safe_load import (
load_index_to_docstore_id,
)
_RAG_CACHE_SUBDIR = "rag_indices"
_SIDECAR_SUFFIX = ".idmap.json"
# Only sweep sidecar ``.tmp`` files older than this — long enough that no single
# write is still in flight, so a concurrent (multi-process) phase-1 run's temp is
# never mistaken for an orphan. Mirrors faiss_store._STALE_TMP_AGE_SECONDS.
_STALE_TMP_AGE_SECONDS = 3600
# A legitimate docstore key is a ``uuid.uuid4().hex`` (32 hex chars, what
# _store_chunks_to_db writes); accept the dashed uuid form too. Anything else in
# a (tampered) ``.pkl`` — arbitrary text, whitespace — is rejected so it can
# never be smuggled into the "text-free" sidecar.
_ID_RE = re.compile(
r"[0-9a-f]{32}"
r"|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
re.IGNORECASE,
)
# Anchored so a crafted name like ``x.pkl.corrupt-1.idmap.json`` is NOT matched.
_QUARANTINE_RE = re.compile(r"\.pkl\.corrupt-[0-9-]+$")
def _rag_cache_root() -> Path:
return get_cache_directory() / _RAG_CACHE_SUBDIR
def _is_live_pkl(path: Path) -> bool:
"""A live legacy docstore ``<hash>.pkl`` (has a re-keyable sibling index)."""
return path.is_file() and path.name.endswith(".pkl")
def _is_quarantined_pkl(path: Path) -> bool:
"""A quarantined docstore ``<hash>.pkl.corrupt-<ns>`` (dead, no sidecar)."""
return path.is_file() and _QUARANTINE_RE.search(path.name) is not None
def _sidecar_for(pkl_path: Path) -> Path:
# <hash>.pkl -> <hash>.idmap.json
return pkl_path.with_name(pkl_path.stem + _SIDECAR_SUFFIX)
def _find_legacy_docstores(root: Path) -> Tuple[List[Path], List[str]]:
"""Return ``(found_docstores, unscannable_dirs)``.
A non-empty ``unscannable_dirs`` means ``os.walk`` could not read a
directory, so a plaintext ``.pkl`` inside it may survive UNDETECTED — the
caller must treat that as an INCOMPLETE migration, not clean success (a
re-scan is blind to the very same unreadable directory)."""
if not root.exists():
return [], []
scan_errors: List[str] = []
def _onerror(exc: OSError) -> None:
# os.walk (like rglob) SILENTLY skips a directory it can't scan. This
# migration's whole guarantee is "no plaintext .pkl left on disk", so a
# scan error must be LOUD and must fail the completeness check —
# otherwise an unreadable subdir could hide an un-deleted .pkl while the
# run reports clean success.
target = getattr(exc, "filename", "?")
scan_errors.append(str(target))
logger.error(
"RAG plaintext migration could not scan "
f"{target} ({exc}); a legacy .pkl there may "
"NOT have been removed — fix permissions and re-run, or delete it "
"manually."
)
found: List[Path] = []
for dirpath, dirs, files in os.walk(str(root), onerror=_onerror):
# os.walk does NOT descend into symlinked subdirectories
# (followlinks=False) and does NOT call onerror for them — they are
# silently skipped. A legacy .pkl inside one would survive undetected,
# so flag each symlinked subdir as unscanned (same as a permission
# error) rather than miss it. followlinks=True is avoided deliberately:
# a symlink cycle would hang the migration.
for d in dirs:
sub = Path(dirpath) / d
# A stat error (PermissionError, etc.) on a SINGLE entry must never
# abort the whole walk — that would leave EVERY .pkl on disk while
# the caller logs "migration failed". Flag the entry and keep going.
try:
is_link = sub.is_symlink()
except OSError:
scan_errors.append(str(sub))
logger.exception(
f"RAG plaintext migration could not stat directory {sub}; "
"a legacy .pkl there may NOT have been removed."
)
continue
if is_link:
scan_errors.append(str(sub))
logger.error(
"RAG plaintext migration did not descend into symlinked "
f"directory {sub}; a legacy .pkl inside may NOT have been "
"removed — replace the symlink with a real directory and "
"re-run, or delete the .pkl manually."
)
for name in files:
p = Path(dirpath) / name
try:
is_pkl = _is_live_pkl(p) or _is_quarantined_pkl(p)
except OSError:
scan_errors.append(str(p))
logger.exception(
f"RAG plaintext migration could not stat file {p}; "
"a legacy .pkl there may NOT have been removed."
)
continue
if is_pkl:
# A symlinked .pkl is a trap: _is_*_pkl / is_file() follow the
# link, so it looks migratable, but _delete_file's unlink()
# removes only the symlink — the real plaintext file it points
# to (potentially OUTSIDE the cache root) survives untouched
# while the run logs clean success. Don't follow it: flag it as
# unresolved (incomplete migration) so an operator removes the
# real file. We deliberately do NOT auto-delete the target — it
# can be an arbitrary path we must not unlink blindly. A stat
# error here is treated as "can't tell" -> unresolved.
try:
is_link = p.is_symlink()
except OSError:
is_link = True
if is_link:
scan_errors.append(str(p))
logger.error(
f"RAG plaintext migration found a SYMLINKED (or "
f"un-statable) docstore {p}; deleting the link would "
"leave the real plaintext file intact. Remove the real "
"target manually, then re-run."
)
continue
found.append(p)
return sorted(found), scan_errors
def _delete_file(path: Path) -> None:
"""Delete ``path`` cross-platform (Windows + POSIX).
Windows raises ``PermissionError`` when a file carries the read-only
attribute; clear it and retry once. On POSIX ``unlink`` succeeds even while a
file is open. A still-failing delete propagates to the caller, which records
it — the re-scan then reports it.
"""
try:
path.unlink()
return
except FileNotFoundError:
return
except PermissionError:
pass
try:
os.chmod(path, stat.S_IWRITE | stat.S_IREAD)
except OSError:
pass
path.unlink()
def _write_sidecar_atomic(sidecar: Path, id_map: Dict[str, str]) -> None:
"""Atomically write the text-free position->uuid map as JSON."""
# Lazy import: this module runs at startup, before the app is fully
# wired up, so avoid a top-level import cycle.
from ..security.directory_creation import create_directory
create_directory(sidecar.parent, context="RAG legacy sidecar directory")
try:
sidecar.parent.chmod(0o700)
except OSError:
logger.warning(f"Could not chmod {sidecar.parent} to 0o700")
# Sweep stale sidecar temps from a previously hard-killed migration. Phase-1
# normally runs single-threaded at startup, but under a multi-process server
# (gunicorn -w N) two workers can start it concurrently — only sweep temps
# OLDER than a generous threshold so a concurrent worker's just-created,
# still-in-flight temp for the same sidecar is never mistaken for an orphan
# and unlinked (which would crash its write). Mirrors FaissVectorStore.persist.
now = time.time()
for stale in sidecar.parent.glob(f"{sidecar.name}.*.tmp"):
try:
if now - stale.stat().st_mtime < _STALE_TMP_AGE_SECONDS:
continue
stale.unlink()
except OSError:
logger.warning(f"Could not remove stale sidecar temp {stale}")
# Safe: this writes only the text-free ``.idmap.json`` sidecar — a plain
# position->uuid JSON map (see module docstring / `_ID_RE`), never chunk
# text/metadata. Not sensitive data at rest.
fd, tmp_name = tempfile.mkstemp(
dir=str(sidecar.parent), prefix=f"{sidecar.name}.", suffix=".tmp"
)
tmp = Path(tmp_name)
try:
# Safe: same text-free id-map as above, written via the fd from
# mkstemp — no chunk text/metadata ever passes through this path.
with os.fdopen(fd, "w", encoding="utf-8") as f:
json.dump(id_map, f)
f.flush()
os.fsync(
f.fileno()
) # durable before the rename (crash-consistency)
# nosemgrep: semgrep.rules.path-traversal-risk, reason: tmp is created by this call's mkstemp beside the fixed-suffix sidecar derived from a non-symlink legacy pickle discovered under the operator cache root
os.replace(tmp, sidecar)
# fsync the directory so the rename itself survives a crash (mirrors
# FaissVectorStore.persist); unsupported on some platforms.
try:
dir_fd = os.open(str(sidecar.parent), os.O_RDONLY)
try:
os.fsync(dir_fd)
finally:
os.close(dir_fd)
except OSError:
pass
finally:
if tmp.exists():
try:
# nosemgrep: semgrep.rules.path-traversal-risk, reason: tmp is this sidecar write's mkstemp file; final cleanup removes only that generated temporary name
tmp.unlink()
except OSError:
logger.warning(f"Could not remove temp sidecar {tmp}")
def _extract_map(pkl_path: Path) -> bool:
"""Extract the text-free position->uuid map to a sidecar next to ``pkl_path``.
Returns True if the sidecar was written (so the login re-key can rebuild
without re-embedding). Returns False if the ``.pkl`` couldn't be read — the
caller still deletes the ``.pkl`` (plaintext removal is not optional) and
that collection will fall back to a full reindex.
"""
try:
raw = load_index_to_docstore_id(str(pkl_path))
except Exception as exc:
logger.warning(
f"Could not read legacy docstore {pkl_path} "
f"({type(exc).__name__}); that collection will reindex"
)
return False
# Validate shape + content. A malformed/tampered map (non-dict, non-int
# positions, or non-uuid values that could smuggle text into the "text-free"
# sidecar) falls to the reindex fallback — it must never crash the whole
# migration (a DoS leaving other users' plaintext on disk) nor leak content.
if not isinstance(raw, dict):
logger.warning(
f"Legacy docstore {pkl_path} has an unexpected id-map shape; "
"that collection will reindex"
)
return False
id_map: Dict[str, str] = {}
for pos, key in raw.items():
# Require key to be a real str and match it DIRECTLY (never str(key)).
# A tampered .pkl can put an arbitrary unpickled object here; calling
# str() on a maliciously deep/shared nested structure is a pickle
# "billion laughs" amplification that can hang/OOM the whole startup
# migration. A legitimate index_to_docstore_id value is always a uuid
# string, so a non-str (or non-uuid) value is simply rejected to the
# reindex fallback — it can never reach str().
# pos must be a plausible faiss array position. Bounding it also stops
# str(pos) below from raising ValueError on a maliciously huge int
# (Python's int->str digit limit), which would otherwise escape this
# loop uncaught and abort the whole startup migration — leaving other
# users' plaintext .pkl files on disk. bool is an int subclass; the
# range check harmlessly admits True/False (1/0), rejected downstream.
if (
not isinstance(pos, int)
or isinstance(pos, bool) # bool is an int subclass; True/False
# would serialize as "True"/"False" and never match a str(pos)
# lookup at rekey time, silently orphaning that chunk's vector.
or not (0 <= pos < 2**31)
or not isinstance(key, str)
or not _ID_RE.fullmatch(key)
):
logger.warning(
f"Legacy docstore {pkl_path} has an unexpected id-map entry; "
"that collection will reindex"
)
return False
id_map[str(pos)] = key
try:
_write_sidecar_atomic(_sidecar_for(pkl_path), id_map)
except OSError:
logger.exception(
f"Could not write id-map sidecar for {pkl_path}; that "
"collection will reindex"
)
return False
return True
def migrate_legacy_docstores() -> Dict[str, int]:
"""Extract each legacy ``.pkl``'s id-map to a text-free sidecar, then delete
the ``.pkl``, removing all plaintext docstores from disk.
Returns ``{"found", "extracted", "deleted", "reindex_fallback",
"remaining", "scan_errors", "hardening_errors"}``. Re-scans afterward and
logs a prominent error recommending manual deletion if any docstore
remains — plaintext must never pass silently. ``hardening_errors`` is a
0/1 flag (not a count): 1 when the cache root could not be restricted to
``0o700``, reported on its own ERROR line independent of the plaintext
outcome. Idempotent (a clean tree is a no-op).
"""
root = _rag_cache_root()
# SECURITY: refuse to operate on a SYMLINKED cache root. os.walk (and
# Path.chmod below) follow a symlinked TOP-LEVEL path unconditionally —
# followlinks=False only guards descent into symlinked CHILDREN found
# mid-walk (which _find_legacy_docstores already flags). A symlinked
# rag_indices/ would otherwise make this migration chmod 0o700 and
# pattern-delete .pkl files OUTSIDE the intended tree (an admin who
# symlinked the cache onto a bigger volume, or a hostile account that can
# write the cache parent), reporting clean success. Treat it exactly like a
# symlinked subdir: log loudly and refuse rather than following it. Uses
# is_symlink() (an lstat, does NOT follow) so it is safe to call first.
if root.is_symlink():
logger.error(
f"RAG cache root {root} is a symlink; refusing to migrate through "
"it (following it would chmod/delete files outside the intended "
"cache tree). Replace it with a real directory and restart."
)
return {
"found": 0,
"extracted": 0,
"deleted": 0,
"reindex_fallback": 0,
"remaining": 0,
"scan_errors": 1,
"hardening_errors": 0,
}
# Harden the RAG cache ROOT at the earliest point it is touched so another
# local OS account can't traverse into any user's vector caches. Both the
# leaf-only chmod in _write_sidecar_atomic and _get_index_path otherwise
# leave the root at the process umask (typically world-traversable) between
# startup and the first index op.
hardening_errors = 0
if root.exists():
try:
root.chmod(0o700)
except OSError:
hardening_errors = 1
# Logged here, at the point of failure and at ERROR, so the
# severity is the same whether or not any legacy docstore
# happens to exist below. Kept as its own line rather than
# folded into the plaintext-migration summary: a chmod failure
# (e.g. on a mount that doesn't support it) must not read as
# "plaintext may remain", which is a different incident.
logger.exception(
f"RAG cache: root {root} could not be restricted to 0o700 — "
"other local OS accounts may still traverse it. Fix the "
"directory permissions manually."
)
found, scan_errors = _find_legacy_docstores(root)
if not found and not scan_errors:
logger.debug(f"RAG cache: no legacy docstore files under {root}")
return {
"found": 0,
"extracted": 0,
"deleted": 0,
"reindex_fallback": 0,
"remaining": 0,
"scan_errors": 0,
"hardening_errors": hardening_errors,
}
extracted = 0
deleted = 0
reindex_fallback = 0
for pkl in found:
# Live .pkl: preserve the re-key map before deleting. Quarantined
# (.pkl.corrupt-*): dead — just delete, no sidecar.
if _is_live_pkl(pkl):
if _extract_map(pkl):
extracted += 1
else:
reindex_fallback += 1
try:
_delete_file(pkl)
deleted += 1
except OSError:
logger.exception(f"RAG cache: could not delete {pkl}")
# Re-scan is the source of truth (partial failures, POSIX unlink-of-open).
# A scan error is NOT clean success: an unreadable directory hides its
# contents from BOTH the delete pass above and this re-scan, so a plaintext
# .pkl inside it could survive while `remaining` reads empty. Treat any
# unscannable dir as an unresolved leftover.
remaining, rescan_errors = _find_legacy_docstores(root)
if remaining or rescan_errors:
parts = []
if remaining:
parts.append(
f"{len(remaining)} legacy docstore file(s) remain on disk: "
+ ", ".join(str(p) for p in remaining)
)
if rescan_errors:
parts.append(
f"{len(rescan_errors)} director(ies) could not be scanned "
"(a plaintext .pkl inside may NOT have been removed): "
+ ", ".join(rescan_errors)
)
logger.error(
"RAG cache: plaintext migration did NOT complete cleanly — "
+ "; ".join(parts)
+ ". Fix permissions and re-run, or delete the file(s) manually."
)
else:
logger.info(
f"RAG cache: extracted {extracted} id-map(s), removed {deleted} "
f"legacy docstore file(s) from {root}"
+ (
f" ({reindex_fallback} unreadable -> will reindex)"
if reindex_fallback
else ""
)
)
return {
"found": len(found),
"extracted": extracted,
"deleted": deleted,
"reindex_fallback": reindex_fallback,
"remaining": len(remaining),
"scan_errors": len(rescan_errors),
"hardening_errors": hardening_errors,
}
def rekey_index_file(
faiss_path: Path,
sidecar_path: Path,
uuid_to_id: Mapping[str, int],
*,
dimension: int,
index_type: str,
metric: str,
normalize: bool,
) -> Dict[str, int]:
"""Convert one old-format index to the new ``IndexIDMap2`` keyed by int id,
WITHOUT re-embedding — the login-time (phase-2) half of the migration.
Reads the raw old ``.faiss`` (reconstructs each vector by position), maps
position -> uuid (from the ``.idmap.json`` sidecar) -> int ``DocumentChunk.id``
(via ``uuid_to_id``), builds a fresh ``IndexIDMap2`` keyed by those ints,
writes it atomically over ``faiss_path``, and **reads it back and verifies**.
Positions whose uuid has no DB row are orphans and are dropped.
Does NOT delete the sidecar (nor acquire the write lock, nor re-record file
integrity) — the caller owns all three. The caller deletes the sidecar ONLY
after recording the new file's integrity, so a crash/failure between persist
and integrity-record leaves the sidecar as a retry signal; the ``IndexIDMap2``
guard above then makes that retry a clean no-op. Raises on a failed read-back
verification (the caller quarantines + falls back to a normal reindex).
"""
# Import here to avoid a module-load cycle (implementations import base only).
from .implementations.faiss_store import FaissVectorStore
faiss_path = Path(faiss_path)
sidecar_path = Path(sidecar_path)
old = read_index(str(faiss_path))
# Crash-consistency guard. rekey persists the new IndexIDMap2 (atomic
# replace) and only unlinks the sidecar AFTER a successful read-back verify.
# If the process dies in that window, faiss_path is ALREADY the new format
# but the sidecar survives, so the next login re-enters here on an
# already-migrated file. Running the reconstruct path below on an
# IndexIDMap2 is not merely wrong — reconstruct_n(0, ntotal) would treat
# 0..ntotal-1 as *ids* to look up; a missing id raises a C++ faiss
# exception that SWIG does NOT surface as a catchable Python exception on
# this call path, aborting the whole process (and, if the int ids happen to
# fall in 0..ntotal-1, silently reconstructing the WRONG vectors instead).
# Either way the re-key is already done — report success WITHOUT deleting
# the sidecar. The CALLER owns sidecar deletion (only after it has recorded
# file integrity), so an interrupted prior run that persisted the new index
# but hadn't yet recorded integrity is recoverable: this re-entry succeeds,
# the caller (re)records integrity, THEN drops the sidecar. Deleting it here
# would lose that retry signal.
if isinstance(old, IndexIDMap2):
# Same dimension guard the fresh re-key path enforces below: an
# already-migrated index whose width no longer matches the configured
# embedding model (model changed post-migration) must be rebuilt, not
# silently accepted. Raise so the caller quarantines + reindexes now,
# rather than deferring to an opaque dimension failure at first search.
if old.d != dimension:
raise ValueError(
f"rekey {faiss_path}: already-migrated index dim {old.d} != "
f"expected {dimension} (embedding model changed) — reindex"
)
# Cross-check the on-disk ids against what the CURRENT sidecar + DB
# resolution says they should be — the fresh path below does this via a
# read-back verify; this short-circuit must too. Without it, a stale or
# FOREIGN IndexIDMap2 sitting at this path (e.g. two collections/users
# colliding on the pre-per-user-scoping shared cache path, or a retry
# after the uuid->id resolution changed) would be silently adopted, and
# its baked-in ids would rehydrate the WRONG document's text on search.
try:
# nosemgrep: semgrep.rules.path-traversal-risk, reason: production caller rekey_user_indexes derives this sidecar from LibraryRAGService's generated index path in the authenticated user's DB; requests cannot assign that path
pos_to_uuid = json.loads(sidecar_path.read_text(encoding="utf-8"))
except FileNotFoundError:
# The sidecar is the caller's COMPLETION SIGNAL: it is deleted only
# AFTER a successful re-key records the new index's integrity (see
# rekey_user_indexes Phase B). A concurrent worker — another process
# (gunicorn -w N / the scheduler) with no cross-process mutex, or a
# thread whose sidecar check passed before that worker won the lock —
# can finalize and delete it between our caller's sidecar check and
# this read. A missing sidecar next to an already-IndexIDMap2 file of
# the right dimension therefore means "already finalized by another
# worker" — the SAME signature rekey_user_indexes treats as done at
# its top-of-loop no-sidecar branch (skipped_no_sidecar). Report
# success rather than letting the FileNotFoundError bubble up and
# QUARANTINE a healthy, just-rekeyed index (forcing a needless full
# re-embed). No id cross-check: there is no sidecar left to check
# against, exactly as the caller's no-sidecar branch also skips it.
logger.info(
f"Re-key skipped for {faiss_path.name}: already migrated to "
"IndexIDMap2; its sidecar was removed by a concurrent finalizer"
)
return {
"reconstructed": int(old.ntotal),
"kept": int(old.ntotal),
"orphaned": 0,
}
expected_ids = {
int(uuid_to_id[u]) for u in pos_to_uuid.values() if u in uuid_to_id
}
on_disk_id_list = (
vector_to_array(old.id_map).astype("int64").tolist()
if old.ntotal
else []
)
# Multiplicity, not just membership: a duplicate id (e.g. [5, 5, 6, 7])
# has the SAME set as {5, 6, 7}, so the set comparison below cannot see
# it (sets dedupe) — exactly the corruption the fresh-build path rejects
# at ``len(set(kept_ids)) != len(kept_ids)``. A duplicate baked into an
# already-migrated IndexIDMap2 double-counts/mis-scores that chunk in
# search, so reject it here too rather than silently adopt it.
if len(set(on_disk_id_list)) != len(on_disk_id_list):
raise ValueError(
f"rekey {faiss_path}: already-migrated index has duplicate "
"DocumentChunk.id(s) — refusing to adopt an id-collided index; "
"quarantine + reindex"
)
if set(on_disk_id_list) != expected_ids:
raise ValueError(
f"rekey {faiss_path}: already-migrated index ids do not match "
"the sidecar/DB resolution — refusing to adopt a stale/foreign "
"index; quarantine + reindex"
)
logger.info(
f"Re-key skipped for {faiss_path.name}: already migrated to "
"IndexIDMap2 (interrupted prior run — caller will finalize)"
)
return {
"reconstructed": int(old.ntotal),
"kept": int(old.ntotal),
"orphaned": 0,
}
# The crash hazard is the WHOLE IndexIDMap family, not just IndexIDMap2:
# reconstruct_n on a plain IndexIDMap (our own format's superclass) also
# raises an untranslated C++ faiss exception that aborts the process
# (SIGABRT), bypassing every try/except. A plain IndexIDMap is never a
# format we write (we only ever persist IndexIDMap2) nor the pre-migration
# raw base index we mean to reconstruct here — so it is foreign/corrupt.
# Raise a CATCHABLE ValueError so the caller quarantines + reindexes,
# instead of falling through to the process-killing reconstruct_n below.
if isinstance(old, IndexIDMap):
raise ValueError(
f"rekey {faiss_path}: on-disk index is {type(old).__name__} "
"(IndexIDMap family but not IndexIDMap2) — refusing to reconstruct "
"a foreign/corrupt index; quarantine + reindex instead"
)
# Positive whitelist. The IndexIDMap guard above is only one of MANY faiss
# classes that abort the process on reconstruct_n: any index type that isn't
# a plain reconstruct-safe base index (e.g. a corrupt/tampered/foreign file
# that happens to decode as IndexIVFPQFastScan) segfaults at reconstruct_n
# below — an untranslated C++ abort that bypasses the caller's try/except
# and, on the login-time rekey path, crash-loops the worker. Only the base
# classes _build_base_index actually produces are reconstruct-safe here;
# reject anything else with a CATCHABLE ValueError so the caller quarantines
# + reindexes.
if not isinstance(old, (IndexFlatL2, IndexFlatIP, IndexHNSWFlat)):
raise ValueError(
f"rekey {faiss_path}: on-disk index is {type(old).__name__}, not a "
"reconstruct-safe base index (IndexFlatL2/IndexFlatIP/IndexHNSWFlat)"
" — refusing a foreign/corrupt index; quarantine + reindex instead"
)
ntotal = int(old.ntotal)
if old.d != dimension:
raise ValueError(
f"rekey {faiss_path}: index dim {old.d} != expected {dimension}"
)
try:
# nosemgrep: semgrep.rules.path-traversal-risk, reason: production caller rekey_user_indexes derives this sidecar from LibraryRAGService's generated index path in the authenticated user's DB; requests cannot assign that path
pos_to_uuid = json.loads(sidecar_path.read_text(encoding="utf-8"))
except FileNotFoundError:
# A concurrent worker (multi-process: gunicorn -w N / the scheduler,
# with no cross-process lock) may have finished re-keying this SAME
# index between our read_index() above and this read — persisting a new
# IndexIDMap2 and deleting the sidecar as its completion signal
# (legacy_rekey Phase B, which persists the new index BEFORE unlinking
# the sidecar). Re-read the on-disk file: if it is now an IndexIDMap2 of
# the right dimension, the migration is already done, so report success
# rather than letting FileNotFoundError bubble up — the caller treats
# that as un-rekeyable and QUARANTINES a perfectly good, just-migrated
# index (forcing a needless full re-embed). Only if the file is still
# NOT migrated (a genuinely missing sidecar next to an old-format file)
# do we re-raise so the caller quarantines. This mirrors the
# already-migrated short-circuit's own FileNotFoundError handling above.
now_on_disk = None
try:
now_on_disk = read_index(str(faiss_path))
except Exception:
# Probe only: a failed/partial read just means "not a valid
# migrated index yet" — now_on_disk stays None and we re-raise the
# original FileNotFoundError below.
logger.debug(
f"Concurrent-migration re-read probe failed for {faiss_path.name}"
)
if isinstance(now_on_disk, IndexIDMap2) and now_on_disk.d == dimension:
logger.info(
f"Re-key skipped for {faiss_path.name}: a concurrent worker "
"migrated it (sidecar already removed)"
)
return {
"reconstructed": int(now_on_disk.ntotal),
"kept": int(now_on_disk.ntotal),
"orphaned": 0,
}
raise
# Reconstruct ALL vectors by position in one bounded call. reconstruct_n only
# touches positions 0..ntotal-1, avoiding the out-of-range garbage that a
# hand-rolled reconstruct(i) loop risks (faiss #4413).
if ntotal:
vectors = np.asarray(old.reconstruct_n(0, ntotal), dtype="float32")
else:
vectors = np.empty((0, dimension), dtype="float32")
kept_ids: List[int] = []
kept_rows: List[int] = []
orphaned = 0
for pos in range(ntotal):
uid = pos_to_uuid.get(str(pos))
int_id = uuid_to_id.get(uid) if uid is not None else None
if int_id is None:
orphaned += 1
continue
kept_ids.append(int(int_id))
kept_rows.append(pos)
# Two legacy positions must never map to the SAME DocumentChunk.id: that
# would bake a duplicate id into the IndexIDMap2 (which tolerates dupes),
# producing phantom, mis-scored search hits — and the set()-based read-back
# verify below cannot see it (sets dedupe). A duplicate means the sidecar /
# uuid->id resolution is inconsistent, so refuse and let the caller
# quarantine + reindex cleanly from the DB rather than persist corruption.
if len(set(kept_ids)) != len(kept_ids):
raise ValueError(
f"rekey {faiss_path}: {len(kept_ids) - len(set(kept_ids))} duplicate "
"DocumentChunk.id(s) from the legacy id-map — refusing to persist an "
"id-collided index; quarantine + reindex instead"
)
kept_vecs = (
vectors[kept_rows]
if kept_rows
else np.empty((0, dimension), dtype="float32")
)
store = FaissVectorStore.create(
dimension=dimension,
index_type=index_type,
metric=metric,
normalize=normalize,
)
if kept_ids:
store.add(kept_ids, kept_vecs)
store.persist(faiss_path) # atomic temp + os.replace
# Read-back verify BEFORE removing the sidecar (the only remaining re-key
# input): the new file must load as IndexIDMap2 with exactly the kept ids.
verify = FaissVectorStore.load(
faiss_path,
dimension=dimension,
index_type=index_type,
metric=metric,
normalize=normalize,
)
if set(verify.live_ids()) != set(kept_ids):
raise RuntimeError(
f"rekey {faiss_path}: read-back verify failed "
f"({verify.count()} ids on disk != {len(kept_ids)} expected)"
)
# NOTE: the sidecar is deliberately NOT deleted here. The caller deletes it
# ONLY after it has recorded the new file's integrity, so a crash/failure
# between persist and integrity-record leaves the sidecar as a retry signal
# (the IndexIDMap2 guard at the top makes the retry a clean no-op re-key).
logger.info(
f"Re-keyed {faiss_path.name}: {len(kept_ids)} vectors "
f"({orphaned} orphaned, {ntotal} reconstructed)"
)
return {
"reconstructed": ntotal,
"kept": len(kept_ids),
"orphaned": orphaned,
}