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.

23.2 KB
Raw Blame
"""Build a replacement collection index without changing its published state."""

from __future__ import annotations

import hashlib
import json
import uuid
from collections.abc import Callable
from contextlib import AbstractContextManager
from datetime import UTC, datetime
from pathlib import Path
from typing import TYPE_CHECKING, Any

import numpy as np
from loguru import logger
from sqlalchemy.orm import Session

from ...config.paths import get_cache_directory
from ...database.models.library import (
    Collection,
    Document,
    DocumentChunk,
    DocumentCollection,
    EmbeddingProvider,
    RagDocumentStatus,
    RAGIndex,
    RAGIndexStatus,
)
from ...database.session_context import safe_rollback
from ...vector_stores.config import only_local_file_providers_registered

if TYPE_CHECKING:
    from .library_rag_service import LibraryRAGService, _PreparedDocument


_CONFIG_FIELDS = (
    "embedding_model",
    "embedding_model_type",
    "embedding_dimension",
    "chunk_size",
    "chunk_overlap",
    "splitter_type",
    "text_separators",
    "distance_metric",
    "normalize_vectors",
    "index_type",
)


def document_fingerprint(document: Document) -> str:
    """Identify the source text and metadata used to prepare a document."""
    payload = {
        key: getattr(document, key)
        for key in (
            "text_content",
            "title",
            "filename",
            "original_url",
            "authors",
            "published_date",
            "doi",
            "arxiv_id",
            "pmid",
            "pmcid",
            "extraction_method",
            "word_count",
        )
    }
    return hashlib.sha256(
        json.dumps(payload, sort_keys=True, default=str).encode("utf-8")
    ).hexdigest()


def collection_configuration(collection: Collection) -> str:
    """Snapshot the stored settings that must survive an unfinished rebuild."""
    return json.dumps(
        {key: getattr(collection, key) for key in _CONFIG_FIELDS},
        sort_keys=True,
        default=str,
    )


def _index_state(session: Session, collection_name: str) -> list[tuple]:
    return sorted(
        (
            row.id,
            row.index_hash,
            row.is_current,
            row.chunk_count,
            row.total_documents,
            row.last_updated_at,
        )
        for row in session.query(RAGIndex).filter_by(
            collection_name=collection_name
        )
    )


def _membership_state(session: Session, collection_id: str) -> list[tuple]:
    return sorted(
        (
            row.document_id,
            row.id,
            row.indexed,
            row.chunk_count,
            row.last_indexed_at,
        )
        for row in session.query(DocumentCollection).filter_by(
            collection_id=collection_id
        )
    )


def rebuild_stage_prefix(collection_id: str) -> str:
    """Return the name prefix shared by every rebuild stage of a collection."""
    scope = hashlib.sha256(collection_id.encode()).hexdigest()[:16]
    return f"collection_rebuild_{scope}_"


class RebuildCancelled(RuntimeError):
    """Cancellation observed before publishing a replacement."""


class CollectionRebuild:
    """A private generation whose only publication point is a DB commit.

    Chunk rows and text-free vector files use a unique staging identity.
    Publishing changes their collection identity, the current-index pointer,
    collection configuration and document status in one transaction. Files
    retain their generation paths, so no filesystem rename can race that
    transaction. A process interruption can leave an unpublished generation,
    but cannot remove the previously published one.

    The caller holds the per-collection rebuild lock through preparation and
    cleanup. Ordinary indexing/search need only the shorter collection lock
    around publication. Document embedding never holds that lock or a DB
    transaction. Interrupted stages stay unselected until the collection is
    deleted or its index cleared (``remove_unfinished_rebuilds``); another
    process's unfinished generation must not be removed speculatively.
    """

    def __init__(
        self,
        service: LibraryRAGService,
        collection_id: str,
        doc_info: list[tuple[str, str]],
        *,
        session_factory: Callable[[], AbstractContextManager[Session]],
    ):
        # The generation switch and post-commit retirement below are atomic
        # only for local files. A remote drop cannot be rolled back, and the
        # current RAGIndex rows do not record which backend owns their data.
        # Refuse rebuilding if any remote provider is registered until those
        # providers have a separate publication protocol.
        if not only_local_file_providers_registered():
            raise RuntimeError(
                "Staged collection rebuild requires local-file vector stores"
            )
        self.service = service
        self.collection_id = collection_id
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: Collection identifier passed as an ORM value; never SQL statement text
        self.collection_name = f"collection_{collection_id}"
        prefix = rebuild_stage_prefix(collection_id)
        # nosemgrep: semgrep.rules.sql-string-concatenation, reason: Collection identifier passed as an ORM value; never SQL statement text
        self.stage_name = f"{prefix}{uuid.uuid4().hex}"
        self.stage_id = self.stage_name.removeprefix("collection_")
        self.vector_index = None
        self.index_id = None
        self.prepared = {}
        self.published = False
        self.target_configuration = None
        self._session = session_factory

        with self._session() as session:
            collection = session.get(Collection, collection_id)
            if collection is None:
                raise ValueError("Collection not found")
            self.configuration = collection_configuration(collection)
            self.original_indices = _index_state(session, self.collection_name)
            self.original_memberships = _membership_state(
                session, collection_id
            )
            self.document_ids = {row[0] for row in self.original_memberships}
            if self.document_ids != {doc_id for doc_id, _title in doc_info}:
                raise ValueError("Collection changed before rebuilding; retry")
            # Persist before preparation so cancellation or a process exit
            # cannot silently erase the unfinished request. The user DB is
            # encrypted; no document content is copied into this marker.
            collection.rebuild_pending_since = datetime.now(UTC)
            session.commit()
        logger.info(
            "Rebuilding collection index; the published index, if any, "
            "remains in use until the replacement is complete. "
            "Cancelling before publication keeps that index and does not "
            "retry the rebuild."
        )

    def prepare(
        self,
        document_id: str,
        is_cancelled: Callable[[], bool] | None = None,
    ) -> dict[str, Any]:
        from .library_rag_service import _PreparedDocument

        result = self.service._prepare_document(
            document_id, self.collection_id, True, is_cancelled
        )
        if result.get("status") == "needs_serial_write":
            # Empty documents have no replacement chunks. Never dispatch the
            # ordinary empty-document purge against the published generation.
            return {
                "status": "prepared",
                "prepared": _PreparedDocument(
                    document_id=document_id,
                    collection_id=self.collection_id,
                    chunk_inputs=[],
                    vectors=np.empty((0, 0), dtype="float32"),
                    source_fingerprint=result["source_fingerprint"],
                ),
            }
        return result

    def write(self, prepared: _PreparedDocument) -> dict[str, Any]:
        """Persist only private chunks; leave published flags and files alone."""
        if prepared.document_id not in self.document_ids:
            raise ValueError("Document does not belong to this rebuild")
        if prepared.chunk_inputs:
            if self.vector_index is None:
                self.vector_index = self.service._get_vector_index(
                    self.stage_id, self.stage_name
                )
                # Private chunks belong to the original collection's members;
                # the temporary generation has no DocumentCollection rows.
                self.vector_index.membership_collection_id = self.collection_id
                self.index_id = self.service.rag_index_record.id
            with self._session() as session:
                try:
                    self.vector_index.index_prepared(
                        source_type="document",
                        source_id=prepared.document_id,
                        chunks=prepared.chunk_inputs,
                        vectors=prepared.vectors,
                        replace=True,
                        session=session,
                    )
                    index = session.get(RAGIndex, self.index_id)
                    index.status = RAGIndexStatus.REBUILDING
                    session.commit()
                except Exception:
                    safe_rollback(session, "collection_rebuild.write")
                    raise
        self.prepared[prepared.document_id] = (
            prepared.source_fingerprint,
            len(prepared.chunk_inputs),
        )
        return {
            "status": "success" if prepared.chunk_inputs else "cleared",
            "chunk_count": len(prepared.chunk_inputs),
        }

    def _validate_sources(self, session, collection):
        if collection_configuration(collection) != self.configuration:
            raise ValueError("Collection configuration changed; retry rebuild")
        if _index_state(session, self.collection_name) != self.original_indices:
            raise ValueError("Collection index changed; retry rebuild")
        # Removing a document from search changes its membership's indexing
        # state without changing the collection's RAGIndex counters. Preserve
        # that successful removal instead of restoring its staged vectors.
        if (
            _membership_state(session, self.collection_id)
            != self.original_memberships
        ):
            raise ValueError("Collection indexing state changed; retry rebuild")
        seen = set()
        # Bound retained source text during the final validation. A writer
        # transaction prevents edits between validation and publication.
        documents = (
            session.query(Document)
            .join(DocumentCollection)
            .filter(DocumentCollection.collection_id == self.collection_id)
            .yield_per(1)
        )
        for document in documents:
            seen.add(document.id)
            expected = self.prepared.get(document.id)
            if (
                expected is None
                or document_fingerprint(document) != expected[0]
            ):
                raise ValueError("Collection documents changed; retry rebuild")
        if seen != self.document_ids or set(self.prepared) != self.document_ids:
            raise ValueError("Collection documents changed; retry rebuild")

    def _validate_vectors(self):
        with self._session() as session:
            chunk_ids = {
                row.id
                for row in session.query(DocumentChunk.id).filter_by(
                    collection_name=self.stage_name
                )
            }
            # Reopen the durable generation: the in-memory facade can
            # still contain every vector after a file was lost or a
            # persist failed. Publication requires the bytes on disk.
            durable_index = (
                self.service._get_vector_index(self.stage_id, self.stage_name)
                if self.vector_index is not None
                else None
            )
            live_ids = set(durable_index.live_ids()) if durable_index else set()
            expected_chunks = sum(
                n for _fingerprint, n in self.prepared.values()
            )
            if chunk_ids != live_ids or len(chunk_ids) != expected_chunks:
                raise RuntimeError("Replacement index is incomplete")

        return expected_chunks

    def _publish_rows(self, session, expected_chunks):
        from ..deletion.utils.cascade_helper import CascadeHelper

        # Acquire SQLite's writer lock before reading the state
        # that must remain unchanged through the publication.
        found = (
            session.query(Collection)
            .filter_by(id=self.collection_id)
            .update({Collection.id: Collection.id})
        )
        if not found:
            raise ValueError("Collection was deleted during rebuild")
        # Validation, every publication write and the commit must share the
        # transaction that took the lock. No callback runs before the commit:
        # a callback that opened get_user_db_session on this thread would
        # share this session, and its error handler would roll it back.
        transaction = session.get_transaction()
        session.expire_all()
        collection = session.get(Collection, self.collection_id)
        self._validate_sources(session, collection)

        CascadeHelper.delete_collection_chunks(session, self.collection_name)
        retired = CascadeHelper.delete_rag_indices_for_collection(
            session, self.collection_name, unlink_files=False
        )

        # Explicit deletion also works with SQLite connections
        # whose foreign-key enforcement was not configured.
        session.query(RagDocumentStatus).filter_by(
            collection_id=self.collection_id
        ).delete(synchronize_session=False)
        session.query(DocumentCollection).filter_by(
            collection_id=self.collection_id
        ).update({"indexed": False, "chunk_count": 0})
        session.query(DocumentChunk).filter_by(
            collection_name=self.stage_name
        ).update({"collection_name": self.collection_name})

        timestamp = datetime.now(UTC)
        index = (
            session.get(RAGIndex, self.index_id)
            if self.index_id is not None
            else None
        )
        if index is not None:
            index.collection_name = self.collection_name
            index.is_current = True
            index.status = RAGIndexStatus.ACTIVE
            index.chunk_count = expected_chunks
            index.total_documents = sum(
                n > 0 for _fingerprint, n in self.prepared.values()
            )
            index.last_updated_at = timestamp
        for document_id, (
            _fingerprint,
            count,
        ) in self.prepared.items():
            if count:
                session.add(
                    RagDocumentStatus(
                        document_id=document_id,
                        collection_id=self.collection_id,
                        rag_index_id=self.index_id,
                        chunk_count=count,
                        indexed_at=timestamp,
                    )
                )
                session.query(DocumentCollection).filter_by(
                    document_id=document_id,
                    collection_id=self.collection_id,
                ).update(
                    {
                        "indexed": True,
                        "chunk_count": count,
                        "last_indexed_at": timestamp,
                    }
                )
        collection.embedding_model = self.service.embedding_model
        collection.embedding_model_type = EmbeddingProvider(
            self.service.embedding_provider
        )
        collection.embedding_dimension = (
            index.embedding_dimension if index else None
        )
        collection.chunk_size = self.service.chunk_size
        collection.chunk_overlap = self.service.chunk_overlap
        collection.splitter_type = self.service.splitter_type
        collection.text_separators = self.service.text_separators
        collection.distance_metric = self.service.distance_metric
        collection.normalize_vectors = self.service.normalize_vectors
        collection.index_type = self.service.index_type

        # Clear only with the generation switch, including an empty rebuild.
        collection.rebuild_pending_since = None

        # A rollback since the lock was taken means the writes above run in a
        # newer transaction that validation never checked, and may hold only
        # part of the publication. Refuse to commit them, before recording
        # the target that _is_published() compares after a failure.
        if session.get_transaction() is not transaction:
            raise RuntimeError(
                "Collection publication lost its transaction; retry rebuild"
            )
        self.target_configuration = collection_configuration(collection)
        return retired["index_paths"]

    def publish(self, is_cancelled: Callable[[], bool] | None = None) -> None:
        """Publish a complete generation, then remove retired vector files.

        Cancellation is decided once, before the publication transaction
        opens. After that the publication commits or rolls back as a whole.
        """
        expected_chunks = self._validate_vectors()
        old_paths = []
        with self.service._collection_transaction_lock(self.collection_id):
            # The background task's check reads the task row through
            # get_user_db_session. On this thread it gets the session that
            # publishes below. Run it while no scope is open, so its
            # autoflush and its error rollback cannot reach the publication.
            self._check_cancelled(is_cancelled)
            try:
                with self._session() as session:
                    try:
                        old_paths = self._publish_rows(session, expected_chunks)
                        session.commit()
                        self.published = True
                    except Exception:
                        safe_rollback(session, "collection_rebuild.publish")
                        raise
            except Exception:
                # A post-commit callback can raise after publication. Use a
                # fresh session: the failed one may still be in COMMITTED
                # state, where even rollback and SELECT are unavailable.
                with self._session() as session:
                    self.published = self._is_published(session)
                if not self.published:
                    raise
                logger.warning(
                    "Collection replacement committed despite a commit callback failure"
                )

            # Clearing an index and indexing again can reuse a retired path.
            # Keep collection ownership through removal so retirement cannot
            # delete the new writer's file. The DB commit has already finished.
            self._unlink(old_paths)
        from ...web_search_engines.search_engines_config import (
            invalidate_collection_engines_cache,
        )

        invalidate_collection_engines_cache(self.service.username)

    @staticmethod
    def _check_cancelled(is_cancelled):
        if is_cancelled is not None and is_cancelled():
            raise RebuildCancelled("Collection rebuild was cancelled")

    def _is_published(self, session):
        if self.index_id is None:
            collection = session.get(Collection, self.collection_id)
            return bool(
                self.target_configuration is not None
                and collection is not None
                and collection.rebuild_pending_since is None
                and collection_configuration(collection)
                == self.target_configuration
                and not _index_state(session, self.collection_name)
            )
        index = session.get(RAGIndex, self.index_id)
        return bool(index and index.collection_name == self.collection_name)

    @staticmethod
    def _unlink(paths):
        from ..deletion.utils.cascade_helper import CascadeHelper

        for path in paths:
            CascadeHelper.delete_faiss_index_files(
                # nosemgrep: semgrep.rules.path-traversal-risk, reason: Deletion helper enforces containment beneath the explicit rag_indices root
                Path(path),
                allowed_root=get_cache_directory() / "rag_indices",
            )

    def close(self):
        """Remove this unpublished generation after its workers have drained."""
        from ..deletion.utils.cascade_helper import CascadeHelper

        if self.published:
            return
        try:
            with self._session() as session:
                if self._is_published(session):
                    self.published = True
                    return
                logger.warning(
                    "Collection rebuild did not publish a replacement. The "
                    "previous index, if any, remains in use and may be "
                    "outdated. Automatic indexing does not retry full "
                    "rebuilds; run Re-Index All again."
                )
                CascadeHelper.delete_collection_chunks(session, self.stage_name)
                retired = CascadeHelper.delete_rag_indices_for_collection(
                    session, self.stage_name, unlink_files=False
                )
                session.commit()
            self._unlink(retired["index_paths"])
        except Exception:
            logger.exception(
                "Could not remove an unpublished collection rebuild"
            )

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()
        return False


def remove_unfinished_rebuilds(
    username: str,
    collection_id: str,
    session_factory: Callable[[], AbstractContextManager[Session]],
) -> int:
    """Remove the stages that interrupted rebuilds of a collection left.

    Collection deletion and index clearing call this after their commit.
    Nothing selects a stage again, and its chunk rows hold document text.
    Each stage is removed the way ``CollectionRebuild.close`` removes its
    own. A rebuild of the collection running in this process holds the
    rebuild lock and removes its stage when it cannot publish, so while that
    lock is held every stage is left alone. Returns the number removed.
    """
    from ..deletion.utils.cascade_helper import CascadeHelper
    from .library_rag_service import _hold_collection_lock

    with _hold_collection_lock(
        username, collection_id, purpose="rebuild", blocking=False
    ) as held:
        if not held:
            return 0
        prefix = rebuild_stage_prefix(collection_id)
        paths = []
        with session_factory() as session:
            names = {
                name
                for model in (DocumentChunk, RAGIndex)
                for (name,) in session.query(model.collection_name)
                .filter(
                    model.collection_name.startswith(prefix, autoescape=True)
                )
                .distinct()
            }
            for name in sorted(names):
                CascadeHelper.delete_collection_chunks(session, name)
                paths += CascadeHelper.delete_rag_indices_for_collection(
                    session, name, unlink_files=False
                )["index_paths"]
            session.commit()
        CollectionRebuild._unlink(paths)
        return len(names)