Skip to content

~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.

30.1 KB
Raw Blame
"""Login-time (phase-2) re-key of legacy RAG indices -- the per-user half of
the vector-store cutover.

``migrate_legacy_docstores`` (phase 1, ``legacy_cleanup.py``) runs once at
server startup and turns each legacy ``<hash>.pkl`` docstore into a small,
text-free ``<hash>.idmap.json`` sidecar (FAISS position -> uuid), deleting the
plaintext ``.pkl``. It cannot go further: rebuilding the index keyed by
``DocumentChunk.id`` needs the per-user encrypted DB (to resolve uuid ->
``DocumentChunk.id``), which is only available once a user has logged in.

This module is that second half. ``rekey_user_indexes`` is called once per
user at login time (from ``database/encrypted_db.py``
``_open_user_database_cold``, AFTER the user's DB connection is already
cached -- see that function's docstring for why it must run there and not
earlier). For each of the user's ``RAGIndex`` rows whose on-disk ``.faiss``
still has a sidecar (i.e. phase 1 ran but phase 2 hasn't), it resolves
``uuid -> DocumentChunk.id`` from the DB, re-keys the index in place via
``legacy_cleanup.rekey_index_file`` (no re-embedding), and re-records file
integrity. Any single index that fails is quarantined (moved aside) and its
collection's indexed state is reset so the normal indexing path rebuilds it
from scratch -- one bad index never blocks the rest of the user's indices,
and this whole step never blocks login (the caller wraps it in try/except).

Gated behind a one-time per-user settings marker
(``research_library.rag_index_rekeyed_v2``) so this is a single
``get_setting`` call (fast no-op) on every login after the first successful
run, and for every user who has no legacy sidecars at all -- the common case
once the fleet has passed through the cutover once.
"""

import json
import time
from pathlib import Path
from typing import Dict, Iterable, List, Optional

from loguru import logger

from ..database.models.library import (
    DocumentChunk,
    DocumentCollection,
    RAGIndex,
    RagDocumentStatus,
)
from ..database.session_context import get_user_db_session
from ..security.file_integrity import FAISSIndexVerifier, FileIntegrityManager
from ..security.log_sanitizer import redact_secrets
from ..utilities.db_utils import get_settings_manager
from .legacy_cleanup import rekey_index_file

# Settings key gating the one-time per-user re-key. Only set after every one
# of the user's indices has been resolved (rekeyed, skipped, or
# quarantined) -- see ``rekey_user_indexes`` for what happens if the process
# is killed mid-run (next login just retries the remaining sidecars).
REKEY_MARKER_SETTING = "research_library.rag_index_rekeyed_v2"

# SQLITE_MAX_VARIABLE_NUMBER is 999 on older SQLite builds; chunk the
# embedding_id IN() lookup well below it. Mirrors ``_IN_CLAUSE_CHUNK`` in
# research_library/notes/services/note_service.py.
_IN_CLAUSE_CHUNK = 500

_SIDECAR_SUFFIX = ".idmap.json"


def _sidecar_for(faiss_path: Path) -> Path:
    # <hash>.faiss -> <hash>.idmap.json (same stem as legacy_cleanup uses
    # for <hash>.pkl -> <hash>.idmap.json, since .pkl and .faiss share a stem).
    return faiss_path.with_name(faiss_path.stem + _SIDECAR_SUFFIX)


def _is_idmap2(faiss_path: Path) -> Optional[bool]:
    """``True`` iff the ``.faiss`` loads as the new ``IndexIDMap2`` format;
    ``False`` if it loads as a DIFFERENT (old) format; ``None`` if it could NOT
    be read at all.

    Used to tell an already-migrated index (IndexIDMap2, no sidecar) apart from
    a phase-1 reindex-fallback leftover (an OLD-format base index whose ``.pkl``
    was deleted without writing a sidecar). The caller quarantines ONLY on a
    definite ``False``. A ``None`` (transient IO/permission error) must NOT
    destructively quarantine + full-re-embed a possibly-healthy index — the
    caller withholds the marker and retries next login instead. A genuinely
    corrupt file that keeps failing is still caught by the normal index/search
    load-failure path, which quarantines there.
    """
    from faiss import (
        IndexIDMap2,
        IO_FLAG_MMAP_IFC,
        IO_FLAG_READ_ONLY,
        read_index,
    )

    from .implementations.segmented_faiss import is_segmented

    try:
        if is_segmented(faiss_path):
            return True
        # Detection must not heap-load a whole legacy corpus before its
        # bounded, vector-preserving conversion can even begin.
        index = read_index(
            str(faiss_path), IO_FLAG_MMAP_IFC | IO_FLAG_READ_ONLY
        )
    except Exception:
        return None
    return isinstance(index, IndexIDMap2)


def _load_uuid_to_id(
    username: str, db_password: str, uuids: Iterable[str]
) -> Dict[str, int]:
    """Resolve ``DocumentChunk.embedding_id`` (uuid) -> ``DocumentChunk.id``
    (int) for the given uuids, chunking the ``IN()`` list. uuids with no
    matching row (deleted chunks) are simply absent from the result --
    ``rekey_index_file`` drops those positions as orphans."""
    uuid_list = list(uuids)
    uuid_to_id: Dict[str, int] = {}
    with get_user_db_session(username, db_password) as session:
        for i in range(0, len(uuid_list), _IN_CLAUSE_CHUNK):
            batch = uuid_list[i : i + _IN_CLAUSE_CHUNK]
            rows = (
                session.query(DocumentChunk.embedding_id, DocumentChunk.id)
                .filter(DocumentChunk.embedding_id.in_(batch))
                .all()
            )
            uuid_to_id.update(dict(rows))
    return uuid_to_id


def _resolve_index_params(rag_index: RAGIndex, settings) -> Dict:
    """RAGIndex row values win; fall back to the current ``local_search_*``
    defaults for legacy rows that predate these columns -- mirrors the
    default resolution in ``rag_service_factory.get_rag_service``."""
    return {
        "dimension": rag_index.embedding_dimension,
        "index_type": rag_index.index_type
        or (settings.get_setting("local_search_index_type") or "flat"),
        "metric": rag_index.distance_metric
        or (settings.get_setting("local_search_distance_metric") or "cosine"),
        "normalize": (
            rag_index.normalize_vectors
            if rag_index.normalize_vectors is not None
            else settings.get_bool_setting("local_search_normalize_vectors")
        ),
    }


def _persist_rekeyed_config(
    username: str, db_password: str, rag_index_id: int, params: Dict
) -> None:
    """Pin the config the physical file was ACTUALLY built with onto the
    RAGIndex row.

    Legacy rows predate the index_type/distance_metric/normalize_vectors
    columns, so ``_resolve_index_params`` fills them from the live
    ``local_search_*`` settings and ``rekey_index_file`` bakes that choice into
    the .faiss file. If the row is left NULL, every later access re-resolves the
    SAME settings-fallback chain — so changing a global default afterward makes
    the runtime reinterpret the file under a different index_type/metric/
    normalize than it was built with. Write the resolved values back (only where
    the row is still NULL — never override an explicit stored value)."""
    with get_user_db_session(username, db_password) as session:
        row = session.query(RAGIndex).filter_by(id=rag_index_id).first()
        if row is None:
            return
        changed = False
        if row.index_type is None:
            row.index_type = params["index_type"]
            changed = True
        if row.distance_metric is None:
            row.distance_metric = params["metric"]
            changed = True
        if row.normalize_vectors is None:
            row.normalize_vectors = params["normalize"]
            changed = True
        if changed:
            session.commit()


def _purge_redundant_pkl(faiss_path: Path, db_password: str) -> bool:
    """Delete a legacy plaintext ``.pkl`` that has become REDUNDANT.

    Once phase-1 has extracted the sidecar (or the index is already an
    IndexIDMap2), the ``.pkl``'s chunk text lives only in the encrypted DB and
    the id-map lives in the sidecar, so the ``.pkl`` is pure leftover plaintext.
    Phase-1 deletes it, but that one-time unlink can fail (a directory the app
    can't write, a read-only mount, an AV/EDR lock) AFTER the sidecar was
    written — leaving plaintext on disk that no later step revisits once the
    per-user completion marker is set. Callers invoke this at every phase-2
    finalization point (re-key success, already-migrated skip) as a backstop.

    Returns True if the ``.pkl`` is absent or was deleted, False if deletion
    failed (the caller then withholds the completion marker so phase-2 retries
    instead of gating the user out with plaintext still on disk). Deliberately
    NOT called where a ``.pkl`` is still NEEDED (no sidecar + old-format index,
    i.e. phase-1 hasn't converted it yet) — that case is handled separately.
    """
    pkl = faiss_path.with_suffix(".pkl")
    if not pkl.exists():
        return True
    try:
        pkl.unlink()
        logger.info(f"Removed surviving legacy plaintext .pkl {pkl}")
        return True
    except OSError as exc:
        safe_msg = redact_secrets(str(exc), db_password)
        logger.warning(
            f"Could not remove surviving legacy plaintext .pkl {pkl}; "
            "withholding re-key completion so it is retried next login: "
            f"{safe_msg}"
        )
        return False


def _quarantine_and_reset(
    username: str,
    db_password: str,
    rag_index: RAGIndex,
    faiss_path: Path,
    sidecar_path: Path,
    lock,
) -> None:
    """Move the broken index pair aside and reset the collection's indexed
    state so the normal indexing path rebuilds it from scratch.

    Mirrors ``LibraryRAGService._quarantine_corrupt_index`` +
    ``_reset_index_state_for_rebuild`` in spirit (rename-aside, never
    delete; clear ``RagDocumentStatus`` + ``DocumentCollection.indexed`` so
    the next index run does real work instead of skipping everything).
    Kept as a free function here (rather than reusing those bound methods
    directly) so this module doesn't have to construct a full
    ``LibraryRAGService`` -- which would pull in an embedding provider --
    just to quarantine a broken re-key.

    Raises on rename failure so the caller can log it distinctly and, in
    turn, withhold the per-user completion marker (see
    ``rekey_user_indexes``) so this index is retried on the next login
    instead of being silently abandoned behind a "done" marker.
    """
    # Reset DB state FIRST, then move the files aside. These two steps are not
    # atomic, so ordering decides what a crash in between leaves behind:
    #  - DB-first (here): the collection is durably flagged "needs reindex"
    #    before its file is touched. A crash after the commit leaves a stray
    #    (still-original or renamed) file that the next index run simply
    #    rebuilds over — no data lost, no wrong state.
    #  - file-first (the reverse) would strand the collection as "indexed" with
    #    its backing index renamed away and no recoverable data until an admin
    #    intervenes — the exact permanent-loss failure this ordering avoids.
    collection_id = rag_index.collection_name.removeprefix("collection_")
    with get_user_db_session(username, db_password) as session:
        idx = session.query(RAGIndex).filter_by(id=rag_index.id).first()
        if idx:
            idx.chunk_count = 0
            idx.total_documents = 0
        session.query(RagDocumentStatus).filter_by(
            rag_index_id=rag_index.id
        ).delete()
        # Only the CURRENT index backs the collection's live searches, so only
        # its failure should force the whole collection to re-embed. Quarantining
        # a STALE (is_current=False) row must NOT flip the collection's live
        # DocumentCollection.indexed state -- the current index is still healthy
        # and serving searches; clearing this row's RAGIndex + RagDocumentStatus
        # above is enough. (rekey_user_indexes now re-keys stale rows too, so
        # this guard is what keeps a stale-row failure from needlessly
        # re-embedding the whole collection.)
        if idx is not None and idx.is_current:
            session.query(DocumentCollection).filter_by(
                collection_id=collection_id, indexed=True
            ).update({"indexed": False, "chunk_count": 0})
        session.commit()

    # Hold the per-(user, index_path) write lock for the renames so they don't
    # race a concurrent search/index reading the same .faiss (which would see
    # the file vanish mid-operation).
    ns = time.time_ns()
    with lock:
        if faiss_path.exists():
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: Login rekey uses service-generated RAGIndex.index_path values from the authenticated user's DB; quarantine only appends a generated timestamp in that directory
            faiss_path.rename(Path(f"{faiss_path}.corrupt-{ns}"))
        if sidecar_path.exists():
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: The caller derives this fixed-suffix sidecar from the same service-generated index path; quarantine only appends a generated timestamp in that directory
            sidecar_path.rename(Path(f"{sidecar_path}.corrupt-{ns}"))
        # Also DELETE (not rename) any lingering legacy plaintext .pkl companion.
        # Phase-1 normally removes it at startup, but if it couldn't then
        # (unreadable/symlinked at that moment) or it reappeared, quarantining
        # the .faiss must not leave the plaintext docstore on disk. Renaming
        # aside like the .faiss would preserve the plaintext, so unlink it. Let
        # an unlink failure PROPAGATE (like the renames above): swallowing it
        # counted the quarantine as clean success and set the completion marker
        # while plaintext survived on disk. The caller instead logs it and
        # withholds the marker so phase-2 retries.
        pkl = faiss_path.with_suffix(".pkl")
        if pkl.exists():
            pkl.unlink()
    logger.warning(
        f"Quarantined un-rekeyable RAG index {faiss_path} for collection "
        f"{collection_id}; reset its indexed state so it rebuilds on next use."
    )


def rekey_user_indexes(username: str, db_password: str) -> Dict[str, int]:
    """Re-key this user's legacy RAG indices (phase 2 of the cutover), once.

    MUST be called only after ``username``'s DB connection is already in
    ``db_manager.connections``. This function goes through
    ``get_user_db_session`` / ``FileIntegrityManager``, both of which
    re-enter ``db_manager.open_user_database`` on a thread-local
    session-cache miss. Calling this from inside ``_open_user_database_cold``
    BEFORE the connection is cached would deadlock: ``open_user_database``
    serializes the cold-open behind a per-user ``threading.Lock`` (not
    reentrant), which this same thread is already holding at that point.

    Returns a stats dict (``checked``/``rekeyed``/``quarantined``/
    ``skipped_no_sidecar``/``skipped_no_faiss``). Does not raise -- every
    per-index failure is caught and quarantined; the caller wraps this call
    anyway as defense in depth.
    """
    stats = {
        "checked": 0,
        "rekeyed": 0,
        "quarantined": 0,
        "skipped_no_sidecar": 0,
        "skipped_no_faiss": 0,
    }

    with get_user_db_session(username, db_password) as session:
        settings = get_settings_manager(db_session=session, username=username)
        if settings.get_bool_setting(REKEY_MARKER_SETTING, False):
            return stats  # already done for this user -- fast no-op
        # Re-key EVERY RAGIndex row phase-1 converted, not just the current one.
        # A collection can have stale historical rows (is_current=False) from an
        # earlier embedding-model switch, and those are NOT unreachable: reverting
        # the embedding-model setting re-resolves a search to the stale row by
        # index_hash (library_rag_service._get_or_create_rag_index looks the row
        # up by hash, NOT is_current), and a still-old-format .faiss then fails to
        # load and hard-fails search. Phase-1 already writes text-free sidecars
        # for ALL of them, so phase-2 must re-key ALL of them. To keep a stale
        # row's re-key FAILURE from collaterally wiping the healthy current
        # index's indexed state, _quarantine_and_reset resets the collection-wide
        # DocumentCollection.indexed flag ONLY for the current row (see there).
        indexes: List[RAGIndex] = session.query(RAGIndex).all()

    if not indexes:
        settings.set_setting(REKEY_MARKER_SETTING, True)
        return stats

    # Lazy import: avoids a module-load cycle (this is a leaf import used
    # only inside this function -- see rekey_index_file's docstring for the
    # same pattern in legacy_cleanup.py).
    from ..research_library.services.library_rag_service import (
        _get_faiss_write_lock,
    )

    integrity_manager = None  # built lazily -- only if a sidecar is found
    # Only set the completion marker if every index was actually resolved
    # (rekeyed / skipped / quarantined). If quarantining itself fails (e.g.
    # disk full mid-rename), that index is left untouched and the marker is
    # withheld so the NEXT login retries it, instead of silently abandoning
    # it forever behind a "done" marker.
    all_resolved = True

    for rag_index in indexes:
        stats["checked"] += 1
        # LibraryRAGService owns all index_path writes: _get_index_path uses
        # an operator cache root and hash-based names. No request or import
        # endpoint accepts this column. Keep that boundary when adding writers;
        # being stored in the DB alone would not make a path trustworthy.
        faiss_path = Path(rag_index.index_path)
        sidecar_path = _sidecar_for(faiss_path)
        # Per-(user, index_path) write lock, held for every file mutation
        # (re-key persist, quarantine renames) so none race a concurrent
        # search/index on the same .faiss.
        lock = _get_faiss_write_lock(username, str(faiss_path))

        if not sidecar_path.exists():
            # No sidecar means EITHER already fully migrated (the .faiss is an
            # IndexIDMap2) OR phase-1 hit its reindex-fallback: a corrupt/
            # malformed legacy .pkl was deleted WITHOUT writing a sidecar,
            # leaving the OLD-format .faiss stranded. The latter would otherwise
            # be skipped forever while the DB still claims the collection is
            # indexed, so its search silently breaks. Distinguish by format and
            # quarantine + reset the un-migratable one so it rebuilds. (This
            # runs once — the whole function is per-user marker-gated.)
            fmt = _is_idmap2(faiss_path) if faiss_path.exists() else True
            if fmt is None:
                # Could NOT read the .faiss to determine its format (transient
                # IO/permission). Do NOT quarantine a possibly-healthy index —
                # withhold the marker and retry next login.
                logger.warning(
                    f"Could not read {faiss_path} to check its format; will "
                    "retry next login (NOT quarantining a possibly-healthy "
                    "index)."
                )
                all_resolved = False
                continue
            if faiss_path.exists() and fmt is False:
                # A no-sidecar old-format .faiss has TWO causes, and only one is
                # the "phase-1 gave up" (reindex-fallback) case that warrants a
                # destructive quarantine. If the legacy plaintext .pkl companion
                # STILL EXISTS, phase-1 simply hasn't converted this file yet
                # (it ran before this collection existed / crashed early / a
                # sibling process's phase-1 hasn't reached it) — its sidecar is
                # still to come. Quarantining now would force a needless full
                # re-embed instead of the cheap rekey phase-1 will enable, and it
                # would delete an intact docstore out from under phase-1. Withhold
                # the completion marker and retry next login, by which point
                # phase-1 (which runs every startup) will have written the
                # sidecar and removed the .pkl.
                if faiss_path.with_suffix(".pkl").exists():
                    logger.warning(
                        f"Old-format index {faiss_path} still has its legacy "
                        ".pkl and no sidecar — phase-1 has not converted it yet; "
                        "will retry next login (NOT quarantining)."
                    )
                    all_resolved = False
                    continue
                logger.warning(
                    f"Un-migratable old-format index {faiss_path} has no "
                    "sidecar (phase-1 reindex-fallback); quarantining so its "
                    "collection rebuilds."
                )
                try:
                    _quarantine_and_reset(
                        username,
                        db_password,
                        rag_index,
                        faiss_path,
                        sidecar_path,
                        lock,
                    )
                    stats["quarantined"] += 1
                except Exception as exc:
                    # db_password is a live parameter of this function; a
                    # rendered traceback (loguru diagnose=True) would dump it.
                    # Drop the traceback and redact str(exc). See encrypted_db.
                    safe_msg = redact_secrets(str(exc), db_password)
                    logger.warning(
                        f"Failed to quarantine {faiss_path}; will retry next "
                        f"login: {safe_msg}"
                    )
                    all_resolved = False
                continue
            # Already migrated (IndexIDMap2, no sidecar). Backstop: purge any
            # legacy plaintext .pkl that survived phase-1 here too — this is the
            # retry landing spot after a re-key whose Phase-B .pkl unlink failed
            # (sidecar already gone), so without it the marker would be set with
            # plaintext still on disk.
            if not _purge_redundant_pkl(faiss_path, db_password):
                all_resolved = False
            stats["skipped_no_sidecar"] += 1
            continue
        if not faiss_path.exists():
            # Orphaned sidecar (index file itself is already gone) -- there
            # is nothing to re-key. Purge any legacy plaintext .pkl that
            # survived next to the now-gone .faiss (a phase-1 .pkl-unlink
            # failure, or a partial _quarantine_and_reset that renamed the
            # .faiss aside before reaching its own pkl.unlink, can leave one
            # here) BEFORE dropping the sidecar. Every other finalization branch
            # backstops the plaintext .pkl this way; without it this branch is
            # the one path that can set the per-user completion marker while an
            # encryption-defeating plaintext .pkl still sits on disk. Withhold
            # the marker if EITHER cleanup fails so phase-2 retries next login.
            stats["skipped_no_faiss"] += 1
            if not _purge_redundant_pkl(faiss_path, db_password):
                all_resolved = False
            try:
                # nosemgrep: semgrep.rules.path-traversal-risk, reason: fixed-suffix sidecar of LibraryRAGService's generated index path loaded from the authenticated user's DB; see the index_path writer boundary above
                sidecar_path.unlink(missing_ok=True)
            except OSError:
                logger.warning(
                    f"Could not remove orphaned sidecar {sidecar_path}; "
                    "will retry next login"
                )
                all_resolved = False
            continue

        # Resolve + PIN the index config onto the row BEFORE building the file,
        # so the row records what the file is built with even across a
        # crash/retry. Pinning AFTER a successful build would break the retry
        # path: if settings change between a crashed attempt and the retry, the
        # retry re-resolves DIFFERENT params, hits rekey_index_file's
        # already-migrated short-circuit (which only checks dim + ids), and pins
        # a config that disagrees with the physical file. Pinning FIRST makes the
        # row the source of truth that _resolve_index_params reads on retry (row
        # wins over settings), so the resolved params match the file at every
        # crash point. A transient failure here must NOT quarantine a healthy
        # legacy file — withhold the marker and retry next login.
        try:
            params = _resolve_index_params(rag_index, settings)
            _persist_rekeyed_config(username, db_password, rag_index.id, params)
        except Exception as exc:
            safe_msg = redact_secrets(str(exc), db_password)
            logger.warning(
                f"Failed to pin index config for RAG index {rag_index.id} "
                f"({faiss_path}); will retry next login: {safe_msg}"
            )
            all_resolved = False
            continue

        # Resolve the sidecar map + uuid->DocumentChunk.id (a DB read). A failure
        # HERE is transient and unrelated to the index file's health — a
        # 'database is locked' timeout under concurrent writes, or a momentary
        # OSError reading the sidecar. It must NOT quarantine a perfectly healthy
        # legacy index (which would force a needless full re-embed); withhold the
        # marker and retry next login instead.
        try:
            # nosemgrep: semgrep.rules.path-traversal-risk, reason: fixed-suffix sidecar of LibraryRAGService's generated index path loaded from the authenticated user's DB; see the index_path writer boundary above
            pos_to_uuid = json.loads(sidecar_path.read_text(encoding="utf-8"))
            uuid_to_id = _load_uuid_to_id(
                username, db_password, set(pos_to_uuid.values())
            )
        except Exception as exc:
            safe_msg = redact_secrets(str(exc), db_password)
            logger.warning(
                f"Could not resolve sidecar/DB ids for RAG index "
                f"{rag_index.id} ({faiss_path}); will retry next login "
                f"(NOT quarantining — the index file is untouched): {safe_msg}"
            )
            all_resolved = False
            continue

        # Phase A -- re-key. A failure HERE is a genuinely un-rekeyable index
        # (corrupt/foreign format, dimension mismatch, duplicate-id sidecar):
        # quarantine + reset so the collection rebuilds from the DB.
        try:
            with lock:
                rekey_index_file(faiss_path, sidecar_path, uuid_to_id, **params)
        except Exception as exc:
            safe_msg = redact_secrets(str(exc), db_password)
            logger.warning(
                f"Re-key failed for RAG index {rag_index.id} "
                f"({faiss_path}); quarantining -- collection will reindex: "
                f"{safe_msg}"
            )
            try:
                _quarantine_and_reset(
                    username,
                    db_password,
                    rag_index,
                    faiss_path,
                    sidecar_path,
                    lock,
                )
                stats["quarantined"] += 1
            except Exception as exc:
                safe_msg = redact_secrets(str(exc), db_password)
                logger.warning(
                    f"Failed to quarantine {faiss_path} after re-key "
                    f"failure -- leaving files as-is; will retry next login: "
                    f"{safe_msg}"
                )
                all_resolved = False
            continue

        # Phase B -- the re-key SUCCEEDED (byte-verified new IndexIDMap2).
        # Record the new file's integrity, THEN delete the sidecar (the
        # completion signal). A failure here must NOT quarantine a perfectly
        # good index (the bug this split fixes): leave the sidecar so the next
        # login retries -- rekey_index_file's IndexIDMap2 guard makes that a
        # clean no-op re-key -- and withhold the completion marker.
        try:
            with lock:
                if integrity_manager is None:
                    integrity_manager = FileIntegrityManager(
                        username, db_password
                    )
                    integrity_manager.register_verifier(FAISSIndexVerifier())
                integrity_manager.record_file(
                    faiss_path,
                    related_entity_type="rag_index",
                    related_entity_id=rag_index.id,
                )
                # Config was already pinned before the build (see above), so
                # Phase B only records integrity and drops the sidecar signal.
                # nosemgrep: semgrep.rules.path-traversal-risk, reason: fixed-suffix sidecar of LibraryRAGService's generated index path loaded from the authenticated user's DB; see the index_path writer boundary above
                sidecar_path.unlink(missing_ok=True)
                # Backstop: if phase-1 wrote the sidecar but its .pkl unlink
                # failed, the plaintext .pkl is still on disk. The re-key just
                # succeeded from the sidecar and the text is in the DB, so purge
                # the now-redundant .pkl; withhold the marker if it can't be
                # removed so this index is retried rather than gated out with
                # plaintext surviving.
                if not _purge_redundant_pkl(faiss_path, db_password):
                    all_resolved = False
            stats["rekeyed"] += 1
        except Exception as exc:
            safe_msg = redact_secrets(str(exc), db_password)
            logger.warning(
                f"Re-key of {faiss_path} succeeded but finalization "
                "(integrity record / sidecar removal) failed; leaving sidecar "
                f"to retry next login: {safe_msg}"
            )
            all_resolved = False

    if all_resolved:
        settings.set_setting(REKEY_MARKER_SETTING, True)
    logger.info(
        f"RAG index re-key for user {username}: "
        f"checked={stats['checked']} rekeyed={stats['rekeyed']} "
        f"quarantined={stats['quarantined']} "
        f"skipped_no_sidecar={stats['skipped_no_sidecar']} "
        f"skipped_no_faiss={stats['skipped_no_faiss']}"
        + ("" if all_resolved else " (incomplete -- will retry next login)")
    )
    return stats