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.

84.0 KB
Raw Blame
"""Regression tests for the ``research_history`` deletion cascade.

Background
----------
``DELETE /api/delete/{research_id}`` and ``POST /api/clear_history`` used
to issue a bulk ``ResearchHistory.delete(synchronize_session=False)`` and
rely on SQLite's ``ON DELETE CASCADE`` rules (set up by the model layer
and enabled at connect time by ``apply_performance_pragmas``) to clean up
the rows in ``app_logs`` (model ``ResearchLog``),
``research_resources``, etc. In-repo every FK on
``research_history.id`` is ``CASCADE``/``SET NULL`` from birth. Several
depth-2 download FKs use ``NO ACTION``, so downloaded sources can block
deletion even on a fresh schema. Partial schemas can also lack a
first-level ``ON DELETE CASCADE`` rule. With ``PRAGMA foreign_keys = ON``
that shape raises
``FOREIGN KEY constraint failed`` and the route surfaces a 500 (with
the SQLite default of ``OFF`` the same delete succeeds silently and
orphans rows instead — a separate orphaning mode the explicit
deletes also repair, but not the 500 discriminated here). The fix
derives a full descendant cascade from model metadata at the application layer;
these tests pin six properties:

1. **Coverage** — every model that declares an FK on
   ``research_history.id`` is deleted (or, for the SET NULL FK on
   ``chat_messages.research_id``, cleared). An introspection test
   walks ``Base.metadata`` so every descendant FK is classified, and
   a missing survival policy for a declared SET NULL edge fails CI.
2. **Robustness against partial schemas** — the route-level tests
   rebuild a CASCADE-bearing child table (``research_strategies``)
   *without* ``ON DELETE CASCADE``, mirroring the production
   fragility. The old bulk-delete route returns 500; the explicit cascade
   answers 200 with the row gone. A negative-control class proves the
   same broken behaviour still happens for the bulk-delete shape that
   the helper replaced.
3. **Atomic guard** — the ``#5074`` predicates (``status !=
   IN_PROGRESS`` and ``~claimed_queue_row``) live INSIDE the helper's
   parent ``DELETE``, so a row that a worker claims between the
   route's pre-check and the helper's delete is preserved. Tests
   below simulate the race by flipping the row's status exactly
   between the two statements and assert the row survives.
4. **FK enforcement** — the cascade works with ``PRAGMA
   foreign_keys = ON``, which is what production sets on every
   connection. The existing ``db`` fixture in
   ``test_research_mutating_route_effects.py`` does not enable this
   pragma, so its tests cannot distinguish the broken behavior from
   the working one. This file's ``db_with_fk`` fixture does.
5. **Public contract** — the routes' response shapes and the
   ``cleanup_queued_research_state`` call are unchanged.
6. **Caller rollback** — the helper's ``SAVEPOINT`` stays nested in
   the caller's transaction (a DEFERRED ``BEGIN`` is forced when the
   driver has none yet, since legacy mode leaves SELECT-only
   sessions without one), so a caller ``rollback()`` after success —
   or a later failure in a multi-row ``clear_history`` — restores
   parent, children and preserved links.

The fixtures here build a real in-memory SQLite database that mirrors
the production schema (same ``Base.metadata.create_all``) and enable
``PRAGMA foreign_keys`` on every connection, exactly as
``apply_performance_pragmas`` does in production.
"""

from contextlib import contextmanager
from datetime import UTC, datetime
from unittest.mock import patch
import hashlib
import uuid

import pytest
from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker
from sqlalchemy.pool import StaticPool


_NOW = datetime(2025, 1, 1, tzinfo=UTC)

_RR = "local_deep_research.web.routers.research"
_LIFECYCLE_CLEANUP = (
    "local_deep_research.web.queue.lifecycle_cleanup."
    "cleanup_queued_research_state"
)

_ENGINE_KW = {
    "connect_args": {"check_same_thread": False},
    "poolclass": StaticPool,
}


@pytest.fixture
def db_with_fk():
    """A real in-memory SQLite database with FK enforcement ON, mirroring
    production. Without ``PRAGMA foreign_keys = ON`` the cascade rules
    are inert and the broken behaviour is invisible; this fixture makes
    the cascade rules actually fire, which is what the bug needs to
    reproduce."""
    from local_deep_research.database.models import Base

    engine = create_engine("sqlite:///:memory:", **_ENGINE_KW)

    @event.listens_for(engine, "connect")
    def _enable_fk(dbapi, _connection_record):
        dbapi.execute("PRAGMA foreign_keys = ON")

    Base.metadata.create_all(engine)
    session = sessionmaker(bind=engine)()
    try:
        yield session
    finally:
        session.close()
        engine.dispose()


def _patch_session(session):
    @contextmanager
    def _ctx(*_args, **_kwargs):
        yield session

    return patch(f"{_RR}.get_user_db_session", _ctx)


def _add_research(session, rid, status="completed", **kwargs):
    from local_deep_research.database.models import ResearchHistory

    row = ResearchHistory(
        id=rid,
        query=kwargs.pop("query", "q"),
        mode=kwargs.pop("mode", "quick"),
        status=status,
        created_at=kwargs.pop("created_at", _NOW),
        **kwargs,
    )
    session.add(row)
    session.commit()
    return row


def _populate_all_child_tables(session, research_id):
    """Insert one row in every table that declares an FK on
    ``research_history.id``. Returns the inserted rows so a test can
    assert they were all deleted.

    The ``chat_messages.research_id`` row is the only one whose FK has
    ``ON DELETE SET NULL``: the message is preserved with its link
    cleared. All the others use ``ON DELETE CASCADE`` and must vanish
    entirely.
    """
    from local_deep_research.database.models import (
        ChatMessage,
        ChatProgressStep,
        ChatRole,
        ChatMessageType,
        ChatSession,
        ChatSessionStatus,
        Collection,
        Document,
        NoteReference,
        NoteResearch,
        ResearchLog,
        ResearchResource,
        ResearchStrategy,
        SourceType,
    )

    # A SourceType row is required before any Document can be inserted
    # -- ``documents.source_type_id`` is non-nullable.
    source_type_id = "st-" + research_id
    session.add(
        SourceType(
            id=source_type_id,
            name="research_download_" + research_id,
            display_name="Research Download",
            icon="download",
        )
    )

    # ChatMessage is SET NULL; the chat_sessions row must exist before
    # chat_messages.session_id FK can be satisfied.
    session_id = "sess-" + research_id
    session.add(
        ChatSession(
            id=session_id,
            title="t",
            status=ChatSessionStatus.ACTIVE,
        )
    )
    chat_msg = ChatMessage(
        id="cm-" + research_id,
        session_id=session_id,
        research_id=research_id,
        role=ChatRole.USER,
        message_type=ChatMessageType.QUERY,
        content="hello",
        sequence_number=1,
        created_at=_NOW,
    )
    session.add(chat_msg)

    progress_step = ChatProgressStep(
        id="cps-" + research_id,
        research_id=research_id,
        session_id=session_id,
        content="step",
        sequence_number=1,
        created_at=_NOW,
    )
    session.add(progress_step)

    coll = Collection(
        id="coll-" + research_id,
        name="c",
        collection_type="user_collection",
    )
    session.add(coll)
    document = Document(
        id="doc-" + research_id,
        research_id=research_id,
        source_type_id=source_type_id,
        document_hash="hash-" + research_id,
        file_size=10,
        file_type="txt",
    )
    session.add(document)

    note_research = NoteResearch(
        document_id=document.id,
        research_id=research_id,
        created_at=_NOW,
    )
    session.add(note_research)

    note_ref = NoteReference(
        note_id=document.id,
        target_research_id=research_id,
        created_at=_NOW,
    )
    session.add(note_ref)

    log = ResearchLog(
        timestamp=_NOW,
        message="m",
        module="mod",
        function="fn",
        line_no=1,
        level="INFO",
        research_id=research_id,
    )
    session.add(log)

    resource = ResearchResource(
        research_id=research_id,
        title="r",
        url="https://example.com",
        source_type="web",
        created_at=_NOW,
    )
    session.add(resource)

    strategy = ResearchStrategy(
        research_id=research_id,
        strategy_name="s",
        created_at=_NOW,
    )
    session.add(strategy)

    session.commit()

    return {
        "chat_message": chat_msg,
        "progress_step": progress_step,
        "document": document,
        "collection": coll,
        "note_research": note_research,
        "note_ref": note_ref,
        "log": log,
        "resource": resource,
        "strategy": strategy,
    }


def _add_rag_chunk(session, document_id, collection_id):
    from local_deep_research.database.models.library import (
        DocumentChunk,
        DocumentCollection,
        EmbeddingProvider,
    )

    text = f"private chunk of {document_id}"
    session.add(
        DocumentCollection(
            document_id=document_id,
            collection_id=collection_id,
            indexed=True,
            chunk_count=1,
        )
    )
    session.add(
        DocumentChunk(
            chunk_hash=hashlib.sha256(text.encode()).hexdigest(),
            source_type="document",
            source_id=document_id,
            collection_name=f"collection_{collection_id}",
            chunk_text=text,
            chunk_index=0,
            start_char=0,
            end_char=len(text),
            word_count=len(text.split()),
            embedding_id=str(uuid.uuid4()),
            embedding_model="fake-model",
            embedding_model_type=EmbeddingProvider.SENTENCE_TRANSFORMERS,
            embedding_dimension=8,
        )
    )
    session.commit()


@contextmanager
def _patch_real_rag_purge(session, events):
    """Run the production residual-chunk sweep with an inert vector store."""
    from local_deep_research.database.models.library import (
        Document,
        DocumentChunk,
    )

    @contextmanager
    def _ctx(*_args, **_kwargs):
        yield session

    class _Rag:
        def __enter__(self):
            return self

        def __exit__(self, *_args):
            return False

        def purge_documents_vectors(self, document_ids, collection_id):
            # Vector deletion must see the committed parent deletion while
            # chunk rows still exist to resolve their FAISS IDs.
            for document_id in document_ids:
                assert (
                    session.query(Document).filter_by(id=document_id).count()
                    == 0
                )
                assert (
                    session.query(DocumentChunk)
                    .filter_by(source_id=document_id)
                    .count()
                    == 1
                )
            events.append(("vectors", document_ids, collection_id))

    with (
        patch(
            "local_deep_research.research_library.deletion.services.document_deletion.get_user_db_session",
            _ctx,
        ),
        patch(
            "local_deep_research.research_library.deletion.services.document_deletion.capture_request_db_password",
            return_value="test-password",
        ),
        patch(
            "local_deep_research.research_library.services.rag_service_factory.get_rag_service",
            side_effect=lambda *_args, **_kwargs: _Rag(),
        ),
    ):
        yield


# ---------------------------------------------------------------------------
# Direct unit tests on the helper
# ---------------------------------------------------------------------------


class TestDeleteResearchWithDependentsHelper:
    """The helper is the single source of truth for which child rows go
    away when a research is removed. Pin its behaviour against every
    FK-referencing model."""

    def test_removes_research_history_and_all_cascade_children(
        self, db_with_fk
    ):
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        inserted = _populate_all_child_tables(db_with_fk, "r1")

        assert _delete_research_with_dependents(db_with_fk, "r1") is True
        db_with_fk.commit()

        from local_deep_research.database.models import (
            ChatMessage,
            ChatProgressStep,
            Document,
            NoteReference,
            NoteResearch,
            ResearchHistory,
            ResearchLog,
            ResearchResource,
            ResearchStrategy,
        )

        # Parent and every CASCADE child gone.
        assert db_with_fk.get(ResearchHistory, "r1") is None
        assert db_with_fk.query(ResearchResource).count() == 0
        assert db_with_fk.query(ResearchLog).count() == 0
        assert db_with_fk.query(ResearchStrategy).count() == 0
        assert db_with_fk.query(ChatProgressStep).count() == 0
        assert db_with_fk.query(NoteResearch).count() == 0
        assert db_with_fk.query(NoteReference).count() == 0
        # Document cascade — documents.research_id is CASCADE, the
        # helper deletes them explicitly. document_chunks /
        # document_blobs / etc. cascade off documents.id via their
        # own rules.
        assert db_with_fk.query(Document).count() == 0

        # ChatMessage.research_id is ON DELETE SET NULL: the message
        # row survives, only the link is cleared.
        surviving_msg = db_with_fk.get(ChatMessage, inserted["chat_message"].id)
        assert surviving_msg is not None, "chat content must be preserved"
        assert surviving_msg.research_id is None, (
            "chat_messages.research_id must be cleared, not the row"
        )

    def test_no_op_when_research_id_does_not_exist(self, db_with_fk):
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        assert _delete_research_with_dependents(db_with_fk, "ghost") is False

    def test_with_fk_enforcement_on_does_not_raise_integrity_error(
        self, db_with_fk
    ):
        """Reproduces the production bug surface: with
        ``PRAGMA foreign_keys = ON``, every known FK-referencing
        table has a row pointing at the research. The helper must
        NOT raise, even though the DB-level cascade path has been
        seen to silently fail on real installs (see the long comment
        above ``_delete_research_with_dependents``)."""
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")

        # Must not raise. The bug surfaced as a raw
        # ``sqlcipher3.IntegrityError`` from inside the route.
        result = _delete_research_with_dependents(db_with_fk, "r1")
        db_with_fk.commit()

        from local_deep_research.database.models import ResearchHistory

        assert result is True
        assert db_with_fk.get(ResearchHistory, "r1") is None

    def test_old_bulk_delete_pattern_fails_when_known_child_table_has_no_cascade(
        self, db_with_fk
    ):
        """Negative control: prove the OLD bulk-delete pattern fails
        under the exact failure mode the helper exists to fix.

        We add an extra child table whose FK to
        ``research_history.id`` has NO ``ON DELETE CASCADE`` rule,
        mimicking the real-world shape: an install where the live
        schema's CASCADE coverage was incomplete (e.g. pre-migration
        0008). A raw ``.delete()`` then raises ``IntegrityError``
        exactly as the production logs show.
        """
        from sqlalchemy import text
        from sqlalchemy.exc import IntegrityError

        _add_research(db_with_fk, "r1")

        # Sanity: confirm the fixture's FK pragma is actually ON.
        # Without it, the production failure mode does not surface
        # — every delete succeeds because SQLite silently ignores
        # FK rules when ``foreign_keys = OFF``.
        fk_state = db_with_fk.execute(text("PRAGMA foreign_keys")).scalar()
        assert fk_state == 1, (
            "test fixture must run with PRAGMA foreign_keys = ON "
            "for the failure mode to surface"
        )

        # A child table whose FK has NO ``ON DELETE CASCADE`` and
        # holds a row pointing at our research. Any code path that
        # trusts the schema's cascade rules alone will raise
        # FOREIGN KEY constraint failed when deleting the parent.
        db_with_fk.execute(
            text(
                "CREATE TABLE orphan_children ("
                "  id INTEGER PRIMARY KEY,"
                "  research_id VARCHAR(36) NOT NULL,"
                "  FOREIGN KEY (research_id) "
                "    REFERENCES research_history(id)"
                ")"
            )
        )
        db_with_fk.execute(
            text("INSERT INTO orphan_children (research_id) VALUES (:rid)"),
            {"rid": "r1"},
        )
        db_with_fk.commit()

        from local_deep_research.database.models import ResearchHistory

        # The old pattern: a single bulk DELETE on the parent.
        # This is the exact statement that raised FOREIGN KEY
        # constraint failed in production logs.
        with pytest.raises(IntegrityError) as excinfo:
            db_with_fk.query(ResearchHistory).filter(
                ResearchHistory.id == "r1"
            ).delete(synchronize_session=False)
            db_with_fk.commit()

        # The DBAPI message is the reliable signal — class can vary
        # by dialect but the wording is identical.
        assert "FOREIGN KEY" in str(excinfo.value).upper()

    def test_only_targets_the_named_research(self, db_with_fk):
        """Sibling rows belonging to a different research must survive.

        The bulk ``.delete()`` shape filters on ``research_id``; if a
        future refactor accidentally drops the filter, every research
        in the table disappears. This test pins the filter."""
        from local_deep_research.database.models import (
            ResearchLog,
            ResearchResource,
            ResearchStrategy,
        )
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _add_research(db_with_fk, "r2")
        _populate_all_child_tables(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r2")

        assert _delete_research_with_dependents(db_with_fk, "r1") is True
        db_with_fk.commit()
        # Expire so the asserts below don't try to refresh the just-
        # deleted ORM identities for r1's rows.
        db_with_fk.expire_all()

        def _count_for(model, research_id):
            return (
                db_with_fk.query(model)
                .filter(model.research_id == research_id)
                .count()
            )

        # r1's children are gone across the board ...
        for model in (ResearchLog, ResearchResource, ResearchStrategy):
            assert _count_for(model, "r1") == 0, (
                f"{model.__name__} rows for r1 should have been deleted"
            )
        # ... and r2's children are untouched (the WHERE research_id
        # filter must isolate the cascade to the named row).
        for model in (ResearchLog, ResearchResource, ResearchStrategy):
            assert _count_for(model, "r2") == 1, (
                f"{model.__name__} rows for r2 must survive deleting r1"
            )


def _populate_download_family(session, research_id, url):
    """Insert a downloaded-source row family for one research.

    Mirrors production: a ``ResearchResource`` with a downloaded file
    gains ``download_tracker`` / ``download_duplicates`` /
    ``download_attempts`` rows (keyed by ``url_hash`` /
    ``first_resource_id`` / ``resource_id``) plus ``download_queue``
    and ``paper_appearances`` rows keyed by ``resource_id``. All of
    these reference ``research_resources.id`` with NO ACTION in live
    schemas, so they block the ``ResearchResource`` delete.
    """
    import hashlib

    from local_deep_research.database.models import (
        DownloadAttempt,
        DownloadDuplicates,
        DownloadQueue,
        DownloadTracker,
        Paper,
        PaperAppearance,
        ResearchResource,
    )

    url_hash = hashlib.sha256(url.encode()).hexdigest()
    resource = ResearchResource(
        research_id=research_id,
        title="r",
        url=url,
        source_type="web",
        created_at=_NOW,
    )
    session.add(resource)
    session.flush()

    session.add(
        DownloadTracker(
            url=url,
            url_hash=url_hash,
            first_resource_id=resource.id,
            is_downloaded=True,
        )
    )
    session.flush()
    session.add(
        DownloadDuplicates(
            url_hash=url_hash,
            resource_id=resource.id,
            research_id=research_id,
        )
    )
    session.add(
        DownloadAttempt(
            url_hash=url_hash,
            attempt_number=1,
            succeeded=True,
        )
    )
    session.add(
        DownloadQueue(
            resource_id=resource.id,
            research_id=research_id,
        )
    )
    paper = Paper(doi="10.1234/" + research_id)
    session.add(paper)
    session.flush()
    session.add(
        PaperAppearance(
            paper_id=paper.id,
            resource_id=resource.id,
        )
    )
    session.commit()
    return resource


class TestDepth2ResourceChildCascade:
    """Depth-2 rows under ``research_resources`` must not block delete.

    ``download_tracker.first_resource_id``,
    ``download_duplicates.resource_id``, ``download_queue.resource_id``
    and ``paper_appearances.resource_id`` reference
    ``research_resources.id`` with NO ACTION in live schemas (the
    defect pinned by
    ``test_research_delete_cascade_is_blocked_by_no_action_parents``).
    Production 2026-09-23: deleting a research with downloaded sources
    raised ``FOREIGN KEY constraint failed`` from inside
    ``_delete_research_with_dependents`` even with PR #6716 applied,
    because the helper stopped at the first level.
    """

    def test_helper_removes_download_family_with_fk_on(self, db_with_fk):
        from local_deep_research.database.models import (
            DownloadAttempt,
            DownloadDuplicates,
            DownloadQueue,
            DownloadTracker,
            PaperAppearance,
            ResearchHistory,
            ResearchResource,
        )
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")
        _populate_download_family(
            db_with_fk, "r1", "https://example.com/r1.pdf"
        )

        assert _delete_research_with_dependents(db_with_fk, "r1") is True
        db_with_fk.commit()
        db_with_fk.expire_all()

        assert db_with_fk.get(ResearchHistory, "r1") is None
        assert db_with_fk.query(ResearchResource).count() == 0
        assert db_with_fk.query(DownloadTracker).count() == 0
        assert db_with_fk.query(DownloadDuplicates).count() == 0
        assert db_with_fk.query(DownloadAttempt).count() == 0
        assert db_with_fk.query(DownloadQueue).count() == 0
        assert db_with_fk.query(PaperAppearance).count() == 0

    def test_sibling_research_download_family_survives(self, db_with_fk):
        from local_deep_research.database.models import (
            DownloadAttempt,
            DownloadDuplicates,
            DownloadQueue,
            DownloadTracker,
            ResearchResource,
        )
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _add_research(db_with_fk, "r2")
        # Distinct URLs so the two families share no url_hash rows.
        _populate_download_family(
            db_with_fk, "r1", "https://example.com/r1.pdf"
        )
        _populate_download_family(
            db_with_fk, "r2", "https://example.com/r2.pdf"
        )

        assert _delete_research_with_dependents(db_with_fk, "r1") is True
        db_with_fk.commit()
        db_with_fk.expire_all()

        assert (
            db_with_fk.query(ResearchResource)
            .filter(ResearchResource.research_id == "r2")
            .count()
            == 1
        )
        assert db_with_fk.query(DownloadTracker).count() == 1
        assert db_with_fk.query(DownloadDuplicates).count() == 1
        assert db_with_fk.query(DownloadAttempt).count() == 1
        assert db_with_fk.query(DownloadQueue).count() == 1


# ---------------------------------------------------------------------------
# DELETE /api/delete/{research_id} — FK-aware integration
# ---------------------------------------------------------------------------


class TestDeleteResearchRouteWithFKEnforcement:
    """Pin the public route's behaviour against the same FK-enforcing
    schema that breaks the old bulk-delete path."""

    def test_delete_succeeds_when_child_tables_have_rows(
        self, authenticated_client, db_with_fk
    ):
        """Happy-path with a fresh ``create_all`` schema: every
        FK-referencing table has a row, ``PRAGMA foreign_keys = ON``
        is set, the route must answer 200 and remove the research
        AND its dependents. This passes both before and after the
        fix (a fresh schema carries every ``CASCADE`` rule), so it
        is NOT the bug discriminator — see
        ``TestRouteSurvivesPartialSchema`` for the test that returns
        500 on the old bulk-delete path and 200 with the helper."""
        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")

        with _patch_session(db_with_fk), patch(_LIFECYCLE_CLEANUP):
            response = authenticated_client.delete("/api/delete/r1")

        assert response.status_code == 200, response.text[:300]
        assert response.get_json()["status"] == "success"

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )

        assert db_with_fk.get(ResearchHistory, "r1") is None
        assert db_with_fk.query(ResearchLog).count() == 0
        assert db_with_fk.query(ResearchResource).count() == 0

    def test_delete_still_refuses_in_progress_with_fk_on(
        self, authenticated_client, db_with_fk
    ):
        """The early ``status == IN_PROGRESS`` guard has to survive the
        helper refactor. Without it, a worker that is mid-research can
        have its row removed from under it."""
        _add_research(db_with_fk, "live", status="in_progress")
        _populate_all_child_tables(db_with_fk, "live")

        with _patch_session(db_with_fk), patch(_LIFECYCLE_CLEANUP):
            response = authenticated_client.delete("/api/delete/live")

        assert response.status_code == 400, response.text[:300]
        assert "in progress" in response.get_json()["message"].lower()

        from local_deep_research.database.models import ResearchHistory

        assert db_with_fk.get(ResearchHistory, "live") is not None

    def test_delete_still_cleans_queue_state(
        self, authenticated_client, db_with_fk
    ):
        """``cleanup_queued_research_state`` is the second step of the
        original handler; it must still run with the research_id, for the
        requesting user."""
        _add_research(db_with_fk, "r1")
        username = authenticated_client.get("/auth/check").get_json()[
            "username"
        ]

        with (
            _patch_session(db_with_fk),
            patch(_LIFECYCLE_CLEANUP) as cleanup,
        ):
            response = authenticated_client.delete("/api/delete/r1")

        assert response.status_code == 200, response.text[:300]
        cleanup.assert_called_once()
        assert cleanup.call_args[0][1] == ["r1"]
        assert cleanup.call_args.kwargs == {"acting_user": username}


# ---------------------------------------------------------------------------
# POST /api/clear_history — FK-aware integration
# ---------------------------------------------------------------------------


class TestClearHistoryRouteWithFKEnforcement:
    """Happy-path twin for ``clear_history`` with a fresh
    ``create_all`` schema. Like the delete happy-path above, this
    passes both before and after the fix — the discriminator is
    ``TestRouteSurvivesPartialSchema.test_clear_history_route_handles_partial_schema``."""

    def test_clear_history_succeeds_when_each_research_has_children(
        self, authenticated_client, db_with_fk
    ):
        _add_research(db_with_fk, "r1")
        _add_research(db_with_fk, "r2")
        _populate_all_child_tables(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r2")

        with (
            _patch_session(db_with_fk),
            patch(f"{_RR}.get_active_research_ids", return_value=[]),
            patch(_LIFECYCLE_CLEANUP),
        ):
            response = authenticated_client.post("/api/clear_history")

        assert response.status_code == 200, response.text[:300]
        assert response.get_json()["status"] == "success"

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )

        assert db_with_fk.query(ResearchHistory).count() == 0
        assert db_with_fk.query(ResearchLog).count() == 0
        assert db_with_fk.query(ResearchResource).count() == 0


class TestResearchRagPurgeRoutes:
    """History deletion purges chunk text only after the guarded commit."""

    def test_unverified_document_removal_does_not_purge_search_data(
        self, loguru_caplog
    ):
        from local_deep_research.research_library.deletion.services import (
            document_deletion,
        )

        service = document_deletion.DocumentDeletionService("owner")
        with (
            patch.object(
                document_deletion,
                "get_user_db_session",
                side_effect=RuntimeError("database unavailable"),
            ),
            patch(
                "local_deep_research.research_library.services.rag_service_factory.get_rag_service"
            ) as rag,
            patch.object(
                document_deletion.CascadeHelper, "delete_document_chunks"
            ) as purge_chunks,
            loguru_caplog.at_level("WARNING"),
        ):
            service.purge_deleted_research_documents(
                [
                    ("0123456789abcdef-document", ["collection"]),
                    ("fedcba9876543210-second", ["one", "two"]),
                ]
            )
        rag.assert_not_called()
        purge_chunks.assert_not_called()
        # The skip is actionable: every warning names the document and how
        # many collections were left behind (#7501).
        skips = [
            r
            for r in loguru_caplog.records
            if "skipping RAG cleanup" in r.message
        ]
        assert [r.message for r in skips] == [
            "Could not verify research document removal; skipping RAG cleanup "
            "for document 01234567... (1 collection)",
            "Could not verify research document removal; skipping RAG cleanup "
            "for document fedcba98... (2 collections)",
        ]

    @pytest.mark.parametrize("entrypoint", ["single", "history", "chat"])
    def test_transferred_document_keeps_rag_until_last_reference_deleted(
        self, authenticated_client, db_with_fk, entrypoint
    ):
        from local_deep_research.chat.service import ChatService
        from local_deep_research.database.models import (
            ChatSession,
            Document,
            DocumentChunk,
            DocumentCollection,
            ResearchHistory,
            ResearchResource,
        )

        _add_research(db_with_fk, "owner")
        owned = _populate_all_child_tables(db_with_fk, "owner")
        document_id = owned["document"].id
        collection_id = owned["collection"].id
        _add_rag_chunk(db_with_fk, document_id, collection_id)
        _add_research(db_with_fk, "survivor", status="in_progress")
        survivor = ResearchResource(
            research_id="survivor",
            title="Shared document",
            url="https://example.com/another-download-url",
            document_id=document_id,
            source_type="web",
            created_at=_NOW,
        )
        db_with_fk.add(survivor)
        db_with_fk.get(ResearchHistory, "owner").chat_session_id = "sess-owner"
        db_with_fk.get(ChatSession, "sess-owner").message_count = 1
        db_with_fk.commit()
        resource_id = survivor.id
        chunk = (
            db_with_fk.query(DocumentChunk)
            .filter_by(source_id=document_id)
            .one()
        )
        snapshot = (chunk.id, chunk.chunk_text, chunk.embedding_id)
        events = []

        @contextmanager
        def _chat_db(*_args, **_kwargs):
            yield db_with_fk

        with (
            _patch_session(db_with_fk),
            _patch_real_rag_purge(db_with_fk, events),
            patch(f"{_RR}.get_active_research_ids", return_value=[]),
            patch(_LIFECYCLE_CLEANUP),
            patch(
                "local_deep_research.chat.service.get_user_db_session", _chat_db
            ),
            patch("local_deep_research.chat.service.cleanup_research"),
        ):
            if entrypoint == "chat":
                assert ChatService("owner").delete_attempt(
                    "sess-owner", "owner"
                )
            elif entrypoint == "history":
                response = authenticated_client.post("/api/clear_history")
                assert response.status_code == 200, response.text[:300]
            else:
                response = authenticated_client.delete("/api/delete/owner")
                assert response.status_code == 200, response.text[:300]

            db_with_fk.expire_all()
            assert db_with_fk.get(ResearchHistory, "owner") is None
            retained = db_with_fk.get(Document, document_id)
            assert retained.research_id == "survivor"
            assert retained.resource_id == resource_id
            chunk = (
                db_with_fk.query(DocumentChunk)
                .filter_by(source_id=document_id)
                .one()
            )
            assert (chunk.id, chunk.chunk_text, chunk.embedding_id) == snapshot
            link = (
                db_with_fk.query(DocumentCollection)
                .filter_by(document_id=document_id, collection_id=collection_id)
                .one()
            )
            assert link.indexed and link.chunk_count == 1
            assert events == []

            # Once the final owner is removed, the same data must be purged.
            db_with_fk.get(ResearchHistory, "survivor").status = "completed"
            db_with_fk.commit()
            response = authenticated_client.delete("/api/delete/survivor")
            assert response.status_code == 200, response.text[:300]

        db_with_fk.expire_all()
        assert db_with_fk.get(Document, document_id) is None
        assert (
            db_with_fk.query(DocumentChunk)
            .filter_by(source_id=document_id)
            .count()
            == 0
        )
        assert events == [("vectors", [document_id], collection_id)]

    def test_single_delete_purges_owned_chunks_after_commit(
        self, authenticated_client, db_with_fk
    ):
        from local_deep_research.database.models.library import (
            Document,
            DocumentChunk,
        )

        _add_research(db_with_fk, "done")
        owned = _populate_all_child_tables(db_with_fk, "done")
        document_id = owned["document"].id
        collection_id = owned["collection"].id
        _add_rag_chunk(db_with_fk, document_id, collection_id)
        events = []

        with (
            _patch_session(db_with_fk),
            _patch_real_rag_purge(db_with_fk, events),
            patch(_LIFECYCLE_CLEANUP),
        ):
            response = authenticated_client.delete("/api/delete/done")

        assert response.status_code == 200, response.text[:300]
        assert db_with_fk.query(Document).filter_by(id=document_id).count() == 0
        assert (
            db_with_fk.query(DocumentChunk)
            .filter_by(source_id=document_id)
            .count()
            == 0
        ), "deleted research retained its private chunk text"
        assert events == [("vectors", [document_id], collection_id)]

    def test_clear_history_purges_only_deleted_research_chunks(
        self, authenticated_client, db_with_fk
    ):
        from local_deep_research.database.models.library import (
            Document,
            DocumentChunk,
        )

        _add_research(db_with_fk, "done")
        done = _populate_all_child_tables(db_with_fk, "done")
        done_id = done["document"].id
        done_collection_id = done["collection"].id
        _add_rag_chunk(db_with_fk, done_id, done_collection_id)

        _add_research(db_with_fk, "live", status="in_progress")
        live = _populate_all_child_tables(db_with_fk, "live")
        live_id = live["document"].id
        _add_rag_chunk(db_with_fk, live_id, live["collection"].id)
        events = []

        with (
            _patch_session(db_with_fk),
            _patch_real_rag_purge(db_with_fk, events),
            patch(f"{_RR}.get_active_research_ids", return_value=[]),
            patch(_LIFECYCLE_CLEANUP),
        ):
            response = authenticated_client.post("/api/clear_history")

        assert response.status_code == 200, response.text[:300]
        assert db_with_fk.query(Document).filter_by(id=done_id).count() == 0
        assert (
            db_with_fk.query(DocumentChunk).filter_by(source_id=done_id).count()
            == 0
        )
        assert db_with_fk.query(Document).filter_by(id=live_id).count() == 1
        assert (
            db_with_fk.query(DocumentChunk).filter_by(source_id=live_id).count()
            == 1
        )
        assert events == [("vectors", [done_id], done_collection_id)]

    def test_unavailable_rag_credentials_still_sweep_private_chunks(
        self, authenticated_client, db_with_fk
    ):
        from local_deep_research.database.models.library import DocumentChunk

        _add_research(db_with_fk, "done")
        owned = _populate_all_child_tables(db_with_fk, "done")
        document_id = owned["document"].id
        _add_rag_chunk(db_with_fk, document_id, owned["collection"].id)
        events = []

        with (
            _patch_session(db_with_fk),
            _patch_real_rag_purge(db_with_fk, events),
            patch(
                "local_deep_research.research_library.deletion.services.document_deletion.capture_request_db_password",
                side_effect=RuntimeError("unavailable"),
            ),
            patch(_LIFECYCLE_CLEANUP),
        ):
            response = authenticated_client.delete("/api/delete/done")

        assert response.status_code == 200, response.text[:300]
        assert events == []
        assert (
            db_with_fk.query(DocumentChunk)
            .filter_by(source_id=document_id)
            .count()
            == 0
        )

    def test_later_failure_rolls_back_without_purging_chunks(
        self, authenticated_client, db_with_fk
    ):
        from local_deep_research.database.models import ResearchHistory
        from local_deep_research.database.models.library import (
            Document,
            DocumentChunk,
        )

        _add_research(db_with_fk, "done")
        owned = _populate_all_child_tables(db_with_fk, "done")
        document_id = owned["document"].id
        _add_rag_chunk(db_with_fk, document_id, owned["collection"].id)

        @contextmanager
        def _rollback_on_error(*_args, **_kwargs):
            try:
                yield db_with_fk
            except Exception:
                db_with_fk.rollback()
                raise

        with (
            patch(f"{_RR}.get_user_db_session", _rollback_on_error),
            patch(
                _LIFECYCLE_CLEANUP, side_effect=RuntimeError("later failure")
            ),
            patch(
                "local_deep_research.research_library.deletion.services.document_deletion.DocumentDeletionService.purge_deleted_research_documents"
            ) as purge,
        ):
            response = authenticated_client.delete("/api/delete/done")

        assert response.status_code == 500
        assert (
            db_with_fk.query(ResearchHistory).filter_by(id="done").count() == 1
        )
        assert db_with_fk.query(Document).filter_by(id=document_id).count() == 1
        assert (
            db_with_fk.query(DocumentChunk)
            .filter_by(source_id=document_id)
            .count()
            == 1
        )
        purge.assert_not_called()


class TestClearHistorySharedDownloadIndex:
    """``clear_history`` must build one ``SharedDownloadIndex`` for the
    whole batch and hand it to every per-research delete; otherwise each
    tracker-owning research pays its own ``research_resources`` scan."""

    def test_route_builds_one_index_and_scans_resources_once(
        self, authenticated_client, db_with_fk
    ):
        from local_deep_research.database.models import (
            DownloadTracker,
            ResearchResource,
        )
        from local_deep_research.utilities.download_url import (
            get_url_hash as download_url_hash,
        )
        from local_deep_research.database.research_cascade import (
            SharedDownloadIndex,
        )

        _add_research(db_with_fk, "keeper", status="in_progress")
        ids = [f"r{index}" for index in range(4)]
        for index, rid in enumerate(ids):
            _add_research(db_with_fk, rid)
            url = f"https://s.org/{index}"
            owned = ResearchResource(
                research_id=rid,
                title="t",
                url=url,
                source_type="web",
                created_at=_NOW,
            )
            sibling = ResearchResource(
                research_id="keeper",
                title="t",
                url=f"http://s.org/{index}/",
                source_type="web",
                created_at=_NOW,
            )
            db_with_fk.add_all([owned, sibling])
            db_with_fk.flush()
            db_with_fk.add(
                DownloadTracker(
                    url=url,
                    url_hash=download_url_hash(url),
                    first_resource_id=owned.id,
                    is_downloaded=True,
                )
            )
        db_with_fk.commit()

        engine = db_with_fk.get_bind()
        selects = []

        def capture(_conn, _cursor, statement, parameters, _ctx, _many):
            if statement.lstrip().upper().startswith("SELECT"):
                selects.append((statement, parameters))

        built = []
        real_build = SharedDownloadIndex.build.__func__
        real_delete = __import__(
            "local_deep_research.database.research_cascade",
            fromlist=["delete_research_with_dependents"],
        ).delete_research_with_dependents
        passed = []

        def spy_build(cls, session, research_ids):
            index = real_build(cls, session, research_ids)
            built.append(index)
            return index

        def spy_delete(session, research_id, **kwargs):
            passed.append(kwargs.get("shared_downloads"))
            return real_delete(session, research_id, **kwargs)

        event.listen(engine, "before_cursor_execute", capture)
        try:
            with (
                _patch_session(db_with_fk),
                patch(f"{_RR}.get_active_research_ids", return_value=[]),
                patch(_LIFECYCLE_CLEANUP),
                patch.object(
                    SharedDownloadIndex, "build", classmethod(spy_build)
                ),
                patch(f"{_RR}._delete_research_with_dependents", spy_delete),
            ):
                response = authenticated_client.post("/api/clear_history")
        finally:
            event.remove(engine, "before_cursor_execute", capture)

        assert response.status_code == 200, response.text[:300]
        assert len(built) == 1
        assert len(passed) == len(ids)
        assert all(index is built[0] for index in passed)

        scans = 0
        for statement, parameters in selects:
            scans += sum(
                row[-1].startswith("SCAN research_resources")
                for row in db_with_fk.connection().exec_driver_sql(
                    "EXPLAIN QUERY PLAN " + statement, parameters
                )
            )
        assert scans == 1
        # Trackers survive, handed to the protected keeper's resources.
        assert db_with_fk.query(DownloadTracker).count() == len(ids)


# ---------------------------------------------------------------------------
# Partial-schema discriminator — the test the original
# ``test_delete_succeeds_when_child_tables_have_rows`` lacked. A
# fresh ``create_all`` schema has every CASCADE rule in place, so
# ``db_with_fk`` alone cannot reproduce the production fragility;
# the production shape would surface a row whose FK lacks
# ``ON DELETE CASCADE``. We rebuild the table that way below and
# prove the route still answers 200 (and main would 500).
# ---------------------------------------------------------------------------


def _rebuild_research_strategies_without_cascade(session, *research_ids):
    """Drop and rebuild ``research_strategies`` without
    ``ON DELETE CASCADE``. Mirrors the shape of an out-of-repo /
    partial schema: the FK column is intact, but the deletion rule
    is not, so a bulk ``DELETE FROM research_history WHERE id = ?``
    raises ``FOREIGN KEY constraint failed`` (with ``PRAGMA
    foreign_keys = ON``) when the table holds a row pointing at
    that parent.

    Re-inserts one ``research_strategies`` row per ``research_id``
    so the partial schema is non-empty — the empty-table case is
    trivially safe and would not reproduce the production error.
    ``DROP TABLE`` wipes every pre-existing strategies row, so
    callers with several researches must pass all ids (the
    ``clear_history`` twin passes both ``r1`` and ``r2``)."""
    from sqlalchemy import text

    session.execute(text("DROP TABLE research_strategies"))
    session.execute(
        text(
            "CREATE TABLE research_strategies ("
            "  id INTEGER PRIMARY KEY,"
            "  research_id VARCHAR(36) NOT NULL,"
            "  strategy_name VARCHAR(255) NOT NULL,"
            "  created_at VARCHAR(50),"
            "  FOREIGN KEY (research_id) "
            "    REFERENCES research_history(id)"
            ")"
        )
    )
    for rid in research_ids:
        session.execute(
            text(
                "INSERT INTO research_strategies "
                "(research_id, strategy_name, created_at) "
                "VALUES (:rid, :name, :ts)"
            ),
            {
                "rid": rid,
                "name": "no-cascade-shape",
                "ts": _NOW.isoformat(),
            },
        )
    session.commit()
    # The ORM identity map still holds the pre-DROP ResearchStrategy
    # objects; expire so later ORM reads hit the rebuilt table.
    session.expire_all()

    # Guard the fixture shape itself: the rebuilt FK must really
    # lack CASCADE, otherwise the discriminator below is vacuous.
    fk_list = (
        session.execute(text("PRAGMA foreign_key_list(research_strategies)"))
        .mappings()
        .all()
    )
    assert fk_list, "rebuilt research_strategies lost its FK entirely"
    for fk_row in fk_list:
        # SQLite reports NO ACTION / RESTRICT when no explicit
        # ON DELETE CASCADE was declared; CASCADE would read 'CASCADE'.
        assert fk_row["on_delete"] != "CASCADE", (
            f"partial-schema fixture still carries CASCADE: {fk_row}"
        )
    # And FK enforcement must be ON, or the bulk delete below
    # succeeds silently (orphans) instead of raising.
    fk_state = session.execute(text("PRAGMA foreign_keys")).scalar()
    assert fk_state == 1, (
        "partial-schema discriminator requires PRAGMA foreign_keys = ON"
    )


class TestRouteSurvivesPartialSchema:
    """Discriminator: rebuild a child table without ``ON DELETE
    CASCADE`` (with ``PRAGMA foreign_keys = ON``), then drive the
    real ``delete`` / ``clear_history`` routes.

    A fresh ``create_all`` schema carries every CASCADE rule, so the
    happy-path tests above return 200 both before and after the fix.
    Under this partial shape the old bulk-delete path raises
    ``IntegrityError`` (route returns 500) while the helper — which
    issues an explicit ``DELETE FROM research_strategies WHERE
    research_id = ?`` — returns 200 with rows gone. Reverting the
    route to the bulk delete therefore fails these tests on
    behavior (500 vs 200), not on an import error."""

    def test_delete_research_returns_500_on_main_with_partial_schema(
        self, db_with_fk
    ):
        """Control: the old bulk-delete statement raises
        ``IntegrityError`` under the partial-schema shape, so the
        route tests' 200 is the helper doing the work, not luck.
        Takes no ``authenticated_client`` fixture on purpose — it
        never touches HTTP, and skipping the register flow keeps
        this control from adding flake to the suite."""
        from sqlalchemy.exc import IntegrityError

        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")
        _rebuild_research_strategies_without_cascade(db_with_fk, "r1")

        # Reproduce the exact pre-PR statement (single bulk parent
        # DELETE, no explicit child cleanup). If this stops raising,
        # the fixture lost its no-CASCADE shape and the route tests
        # below are vacuous again.
        from local_deep_research.database.models import ResearchHistory

        with pytest.raises(IntegrityError) as excinfo:
            db_with_fk.query(ResearchHistory).filter(
                ResearchHistory.id == "r1"
            ).delete(synchronize_session=False)
            db_with_fk.commit()
        assert "FOREIGN KEY" in str(excinfo.value).upper()

    def test_delete_research_route_handles_partial_schema(
        self, authenticated_client, db_with_fk
    ):
        """``DELETE /api/delete/{id}`` under the partial shape must
        answer 200 with parent and strategies rows gone. Reverting
        the route to the bulk delete returns 500 here."""
        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")
        _rebuild_research_strategies_without_cascade(db_with_fk, "r1")

        with _patch_session(db_with_fk), patch(_LIFECYCLE_CLEANUP):
            response = authenticated_client.delete("/api/delete/r1")

        assert response.status_code == 200, response.text[:300]
        assert response.get_json()["status"] == "success"

        from sqlalchemy import text

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchStrategy,
        )

        assert db_with_fk.get(ResearchHistory, "r1") is None
        assert db_with_fk.query(ResearchStrategy).count() == 0
        # Raw-table check: the rebuilt (non-ORM) table is empty too —
        # guards against the ORM query hitting a stale identity map
        # while the raw row survives.
        raw_strategies = db_with_fk.execute(
            text("SELECT COUNT(*) FROM research_strategies")
        ).scalar()
        assert raw_strategies == 0

    def test_clear_history_route_handles_partial_schema(
        self, authenticated_client, db_with_fk
    ):
        """``POST /api/clear_history`` twin: both researches carry a
        no-CASCADE strategies row (the rebuild wipes the table, so
        both ids are re-inserted). Reverting to the bulk delete
        returns 500 here."""
        from sqlalchemy import text

        _add_research(db_with_fk, "r1")
        _add_research(db_with_fk, "r2")
        _populate_all_child_tables(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r2")
        _rebuild_research_strategies_without_cascade(db_with_fk, "r1", "r2")

        # Sanity: both partial rows exist before the route runs —
        # otherwise the twin only discriminates on one row.
        pre_count = db_with_fk.execute(
            text("SELECT COUNT(*) FROM research_strategies")
        ).scalar()
        assert pre_count == 2, (
            f"partial fixture must hold 2 strategies rows, found {pre_count}"
        )

        with (
            _patch_session(db_with_fk),
            patch(f"{_RR}.get_active_research_ids", return_value=[]),
            patch(_LIFECYCLE_CLEANUP),
        ):
            response = authenticated_client.post("/api/clear_history")

        assert response.status_code == 200, response.text[:300]
        assert response.get_json()["status"] == "success"

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchStrategy,
        )

        assert db_with_fk.query(ResearchHistory).count() == 0
        assert db_with_fk.query(ResearchStrategy).count() == 0
        raw_strategies = db_with_fk.execute(
            text("SELECT COUNT(*) FROM research_strategies")
        ).scalar()
        assert raw_strategies == 0


# ---------------------------------------------------------------------------
# Atomic guard — the #5074 predicates must run INSIDE the parent
# DELETE so a concurrent claim between the route's pre-check and
# the helper's delete still wins the race.
# ---------------------------------------------------------------------------


def _add_queued_claim(session, research_id):
    """Insert a ``QueuedResearch`` row that the helper must observe
    inside the parent DELETE's WHERE clause. Mirrors the worker
    claim that #5074 was written to handle."""
    from local_deep_research.database.models import QueuedResearch

    session.add(
        QueuedResearch(
            username="claim-user",
            research_id=research_id,
            query="q",
            mode="quick",
            position=1,
            is_processing=True,
            created_at=_NOW,
        )
    )
    session.commit()


def _flip_status_in_progress(session, research_id):
    """Flip a row's status to ``IN_PROGRESS`` between the
    pre-check and the helper call. Matches what a worker thread
    would commit at the top of its run."""
    from sqlalchemy import text

    session.execute(
        text(
            "UPDATE research_history SET status = 'in_progress' WHERE id = :rid"
        ),
        {"rid": research_id},
    )
    session.commit()


class TestHelperAtomicGuard:
    """The pre-check on the route is a fast-path only; the
    authoritative predicate is the ``WHERE`` clause of the helper's
    parent DELETE. These tests prove a worker that races the
    route — by flipping the row's status to ``IN_PROGRESS`` or by
    claiming its queue entry between the route's snapshot and the
    helper's call — still preserves the row.

    The implementation technique: we don't need a real thread.
    The helper's parent DELETE is a SQL statement; we just need
    the row to BE in the protected state at the moment the
    statement runs. For the "race" simulation we issue a
    ``session.execute(UPDATE research_history SET status =
    'in_progress')`` *first* and then call the helper — that is
    exactly the DB-level observation the helper must respect.
    """

    def test_helper_preserves_row_already_in_progress(self, db_with_fk):
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "live", status="in_progress")
        _populate_all_child_tables(db_with_fk, "live")

        assert _delete_research_with_dependents(db_with_fk, "live") is False, (
            "helper must refuse IN_PROGRESS rows; the route's "
            "pre-check is a fast-path, this is the authoritative guard"
        )
        db_with_fk.commit()

        from local_deep_research.database.models import ResearchHistory

        assert db_with_fk.get(ResearchHistory, "live") is not None

    def test_helper_preserves_row_with_claimed_queue_entry(self, db_with_fk):
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "claimed")
        _populate_all_child_tables(db_with_fk, "claimed")
        _add_queued_claim(db_with_fk, "claimed")

        assert (
            _delete_research_with_dependents(db_with_fk, "claimed") is False
        ), "helper must refuse rows whose queue entry is claimed"
        db_with_fk.commit()

        from local_deep_research.database.models import ResearchHistory

        assert db_with_fk.get(ResearchHistory, "claimed") is not None

    def test_helper_opt_out_drops_status_guard_for_chat_service(
        self, db_with_fk
    ):
        """The chat service's per-attempt teardown calls the
        helper after a liveness check has already confirmed the
        worker thread is dead; an ``IN_PROGRESS`` row in the DB
        (stale, worker is gone) must still be deletable. The
        helper exposes a ``require_status_not_in_progress=False``
        opt-out for that call site."""
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "stale", status="in_progress")
        _populate_all_child_tables(db_with_fk, "stale")

        assert (
            _delete_research_with_dependents(
                db_with_fk,
                "stale",
                require_status_not_in_progress=False,
                require_no_claimed_queue_row=False,
            )
            is True
        )
        db_with_fk.commit()

        from local_deep_research.database.models import ResearchHistory

        assert db_with_fk.get(ResearchHistory, "stale") is None

    def test_helper_opt_out_drops_claim_guard(self, db_with_fk):
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")
        _add_queued_claim(db_with_fk, "r1")

        assert (
            _delete_research_with_dependents(
                db_with_fk,
                "r1",
                require_no_claimed_queue_row=False,
            )
            is True
        )
        db_with_fk.commit()

        from local_deep_research.database.models import ResearchHistory

        assert db_with_fk.get(ResearchHistory, "r1") is None


class TestHelperGuardRollbackAtomicity:
    """Guard refusal must leave parent AND children untouched.

    Regression for the final-review finding at ``f8bf804``: the helper
    deleted children in Phases B/C *before* the Phase-D parent guard,
    so a ``False`` return still persisted child deletions when the
    caller committed (``clear_history`` commits siblings in the same
    transaction). The helper now wraps Phases B–D in a SAVEPOINT and
    rolls back on a zero-row parent DELETE.
    """

    def _assert_all_children_present(self, session, research_id):
        from local_deep_research.database.models import (
            ChatMessage,
            ChatProgressStep,
            Document,
            NoteReference,
            NoteResearch,
            ResearchLog,
            ResearchResource,
            ResearchStrategy,
        )

        assert (
            session.query(ResearchLog)
            .filter(ResearchLog.research_id == research_id)
            .count()
            == 1
        ), "ResearchLog child must survive guard refusal"
        assert (
            session.query(ResearchResource)
            .filter(ResearchResource.research_id == research_id)
            .count()
            == 1
        ), "ResearchResource child must survive guard refusal"
        assert (
            session.query(ResearchStrategy)
            .filter(ResearchStrategy.research_id == research_id)
            .count()
            == 1
        ), "ResearchStrategy child must survive guard refusal"
        assert (
            session.query(ChatProgressStep)
            .filter(ChatProgressStep.research_id == research_id)
            .count()
            == 1
        ), "ChatProgressStep child must survive guard refusal"
        assert (
            session.query(NoteResearch)
            .filter(NoteResearch.research_id == research_id)
            .count()
            == 1
        ), "NoteResearch child must survive guard refusal"
        assert (
            session.query(NoteReference)
            .filter(NoteReference.target_research_id == research_id)
            .count()
            == 1
        ), "NoteReference child must survive guard refusal"
        assert (
            session.query(Document)
            .filter(Document.research_id == research_id)
            .count()
            == 1
        ), "Document child must survive guard refusal"
        # SET NULL edge rolls back too: the link must still be set.
        msg = (
            session.query(ChatMessage)
            .filter(ChatMessage.id == "cm-" + research_id)
            .one_or_none()
        )
        assert msg is not None, "chat message row must survive guard refusal"
        assert msg.research_id == research_id, (
            "chat_messages.research_id must NOT be nulled when the guard "
            "refuses the delete"
        )

    def test_helper_rolls_back_children_when_status_guard_blocks(
        self, db_with_fk
    ):
        from local_deep_research.database.models import ResearchHistory
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "live", status="in_progress")
        _populate_all_child_tables(db_with_fk, "live")

        assert _delete_research_with_dependents(db_with_fk, "live") is False
        db_with_fk.commit()
        db_with_fk.expire_all()

        assert db_with_fk.get(ResearchHistory, "live") is not None
        self._assert_all_children_present(db_with_fk, "live")

    def test_helper_rolls_back_children_when_claim_guard_blocks(
        self, db_with_fk
    ):
        from local_deep_research.database.models import ResearchHistory
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "claimed")
        _populate_all_child_tables(db_with_fk, "claimed")
        _add_queued_claim(db_with_fk, "claimed")

        assert _delete_research_with_dependents(db_with_fk, "claimed") is False
        db_with_fk.commit()
        db_with_fk.expire_all()

        assert db_with_fk.get(ResearchHistory, "claimed") is not None
        self._assert_all_children_present(db_with_fk, "claimed")

    def test_clear_history_loop_preserves_protected_children(self, db_with_fk):
        """Mirror ``clear_history``'s multi-row loop in one transaction:
        one protected row must not lose children when a sibling commits."""
        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _add_research(db_with_fk, "r2", status="in_progress")
        _populate_all_child_tables(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r2")

        deleted_ids = []
        for rid in ("r1", "r2"):
            if _delete_research_with_dependents(db_with_fk, rid):
                deleted_ids.append(rid)
        db_with_fk.commit()
        db_with_fk.expire_all()

        assert deleted_ids == ["r1"]
        assert db_with_fk.get(ResearchHistory, "r1") is None
        assert db_with_fk.get(ResearchHistory, "r2") is not None
        # Sibling delete lands ...
        assert (
            db_with_fk.query(ResearchLog)
            .filter(ResearchLog.research_id == "r1")
            .count()
            == 0
        )
        assert (
            db_with_fk.query(ResearchResource)
            .filter(ResearchResource.research_id == "r1")
            .count()
            == 0
        )
        # ... while the protected row keeps parent AND children.
        self._assert_all_children_present(db_with_fk, "r2")


class TestHelperCallerRollback:
    """Caller rollback must restore parent, children and preserved links.

    Regression for the standalone-savepoint finding: in legacy
    transaction mode (production SQLCipher/SQLite with
    ``isolation_level=""``) SELECTs do not open a database
    transaction, so ``begin_nested()`` issued after a SELECT-only
    Phase A opened a standalone SAVEPOINT whose RELEASE committed
    immediately. A later ``db_session.rollback()`` by the caller then
    restored nothing and ``clear_history`` ended up partly applied
    after a later failure. The helper now forces a DEFERRED ``BEGIN``
    first (see ``_ensure_outer_db_transaction``), so RELEASE stays
    nested and the caller still owns commit/rollback.
    """

    def _assert_restored_from_fresh_session(self, session, research_id):
        """Re-read parent, children and SET NULL links from a new
        Session on the same engine — guards against the caller's
        identity map masking a persisted delete."""
        from sqlalchemy.orm import sessionmaker

        engine = session.get_bind()
        fresh = sessionmaker(bind=engine)()
        try:
            from local_deep_research.database.models import (
                ChatMessage,
                ChatProgressStep,
                Document,
                NoteReference,
                NoteResearch,
                ResearchHistory,
                ResearchLog,
                ResearchResource,
                ResearchStrategy,
            )

            assert fresh.get(ResearchHistory, research_id) is not None, (
                "parent must survive caller rollback"
            )
            for model, column in (
                (ResearchLog, "research_id"),
                (ResearchResource, "research_id"),
                (ResearchStrategy, "research_id"),
                (ChatProgressStep, "research_id"),
                (NoteResearch, "research_id"),
                (Document, "research_id"),
            ):
                assert (
                    fresh.query(model)
                    .filter(getattr(model, column) == research_id)
                    .count()
                    == 1
                ), f"{model.__name__} child must survive caller rollback"
            assert (
                fresh.query(NoteReference)
                .filter(NoteReference.target_research_id == research_id)
                .count()
                == 1
            ), "NoteReference child must survive caller rollback"
            msg = fresh.get(ChatMessage, "cm-" + research_id)
            assert msg is not None, "chat message row must survive rollback"
            assert msg.research_id == research_id, (
                "chat_messages.research_id must still be linked after "
                "caller rollback (SET NULL step must roll back too)"
            )
        finally:
            fresh.close()

    def test_helper_success_is_rollbackable_by_caller(self, db_with_fk):
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r1")

        # Caller pattern is SELECTs then helper (no prior writes) —
        # exactly the state that produced a standalone SAVEPOINT.
        assert _delete_research_with_dependents(db_with_fk, "r1") is True
        db_with_fk.rollback()
        db_with_fk.expire_all()

        from local_deep_research.database.models import ResearchHistory

        assert db_with_fk.get(ResearchHistory, "r1") is not None
        self._assert_restored_from_fresh_session(db_with_fk, "r1")

    def test_multi_row_clear_rollback_on_later_failure(self, db_with_fk):
        """Mirror ``clear_history``: two successful helper deletes in
        one outer transaction, then a later failure before commit must
        restore both researches fully."""
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        _add_research(db_with_fk, "r1")
        _add_research(db_with_fk, "r2")
        _populate_all_child_tables(db_with_fk, "r1")
        _populate_all_child_tables(db_with_fk, "r2")

        assert _delete_research_with_dependents(db_with_fk, "r1") is True
        assert _delete_research_with_dependents(db_with_fk, "r2") is True
        # Later failure (queue cleanup, file unlink, second-loop
        # exception) before the route's commit — caller rolls back.
        db_with_fk.rollback()
        db_with_fk.expire_all()

        self._assert_restored_from_fresh_session(db_with_fk, "r1")
        self._assert_restored_from_fresh_session(db_with_fk, "r2")


class TestRouteAtomicGuardUnderRace:
    """End-to-end version of the atomic-guard invariant: the route
    must observe a row the worker just claimed between the
    pre-check and the helper's call. The pre-check passes because
    the row is ``completed`` and unclaimed at the moment of the
    read; the race is a real DB write that lands before the
    helper's parent DELETE.

    The race is simulated by patching the helper with a wrapper
    that flips the row's status to ``in_progress`` (or claims its
    queue entry) before delegating to the real helper. This
    models exactly the worker that wins the race: it commits
    *between* the route's pre-check and the helper's parent
    DELETE, so the row exists in the protected state when the
    helper's ``WHERE`` clause runs.
    """

    def _wrap_helper_with_race(self, prep):
        """Wrap the real helper with a side-effect ``prep`` that
        mutates the DB between the route's pre-check and the
        helper's parent DELETE. ``prep`` takes the session and
        the research_id."""
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents as real_helper,
        )

        def helper_with_race(db_session, research_id, **kwargs):
            prep(db_session, research_id)
            return real_helper(db_session, research_id, **kwargs)

        return helper_with_race

    def test_delete_research_preserves_row_when_status_flips_mid_call(
        self, authenticated_client, db_with_fk
    ):
        _add_research(db_with_fk, "race")
        _populate_all_child_tables(db_with_fk, "race")

        wrapped = self._wrap_helper_with_race(_flip_status_in_progress)

        with (
            _patch_session(db_with_fk),
            patch(_LIFECYCLE_CLEANUP),
            patch(f"{_RR}._delete_research_with_dependents", wrapped),
        ):
            response = authenticated_client.delete("/api/delete/race")

        # The pre-check happened *before* the wrapper ran (the
        # row was ``completed`` then). The wrapper flipped the
        # status, the helper's parent DELETE observed the flip
        # via its WHERE clause, and the row survived. The
        # post-check (db_session.get(...) is not None) emits
        # the 400 — preserving the observable contract for
        # callers.
        assert response.status_code == 400, response.text[:300]
        assert "in progress" in response.get_json()["message"].lower()

        from local_deep_research.database.models import (
            ChatMessage,
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )

        assert db_with_fk.get(ResearchHistory, "race") is not None
        # The mid-call flip must not cost the row's children: the
        # helper's SAVEPOINT rolls back Phases B/C with the parent.
        db_with_fk.expire_all()
        assert (
            db_with_fk.query(ResearchLog)
            .filter(ResearchLog.research_id == "race")
            .count()
            == 1
        )
        assert (
            db_with_fk.query(ResearchResource)
            .filter(ResearchResource.research_id == "race")
            .count()
            == 1
        )
        msg = db_with_fk.get(ChatMessage, "cm-race")
        assert msg is not None and msg.research_id == "race"

    def test_delete_research_preserves_row_when_queue_claim_lands_mid_call(
        self, authenticated_client, db_with_fk
    ):
        """Same shape as the status-flip test, except the
        race is a ``QueuedResearch`` claim landing between the
        pre-check and the helper's parent DELETE. The pre-check
        sees no claim (false), the wrapper inserts the claim
        before the helper runs, and the helper's
        ``~claimed_queue_row`` predicate preserves the row."""

        def claim_prep(session, research_id):
            _add_queued_claim(session, research_id)

        _add_research(db_with_fk, "race-claim")
        _populate_all_child_tables(db_with_fk, "race-claim")

        wrapped = self._wrap_helper_with_race(claim_prep)

        with (
            _patch_session(db_with_fk),
            patch(_LIFECYCLE_CLEANUP),
            patch(f"{_RR}._delete_research_with_dependents", wrapped),
        ):
            response = authenticated_client.delete("/api/delete/race-claim")

        assert response.status_code == 400, response.text[:300]
        assert "in progress" in response.get_json()["message"].lower()

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )

        assert db_with_fk.get(ResearchHistory, "race-claim") is not None
        db_with_fk.expire_all()
        assert (
            db_with_fk.query(ResearchLog)
            .filter(ResearchLog.research_id == "race-claim")
            .count()
            == 1
        )
        assert (
            db_with_fk.query(ResearchResource)
            .filter(ResearchResource.research_id == "race-claim")
            .count()
            == 1
        )

    def test_clear_history_route_preserves_protected_children(
        self, authenticated_client, db_with_fk
    ):
        """``clear_history`` commits siblings in one transaction: a
        protected row skipped by the helper must keep its children."""
        _add_research(db_with_fk, "keep", status="in_progress")
        _add_research(db_with_fk, "gone")
        _populate_all_child_tables(db_with_fk, "keep")
        _populate_all_child_tables(db_with_fk, "gone")

        with (
            _patch_session(db_with_fk),
            patch(f"{_RR}.get_active_research_ids", return_value=[]),
            patch(_LIFECYCLE_CLEANUP),
        ):
            response = authenticated_client.post("/api/clear_history")

        assert response.status_code == 200, response.text[:300]

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )

        db_with_fk.expire_all()
        assert db_with_fk.get(ResearchHistory, "gone") is None
        assert db_with_fk.get(ResearchHistory, "keep") is not None
        assert (
            db_with_fk.query(ResearchLog)
            .filter(ResearchLog.research_id == "keep")
            .count()
            == 1
        )
        assert (
            db_with_fk.query(ResearchResource)
            .filter(ResearchResource.research_id == "keep")
            .count()
            == 1
        )

    def test_clear_history_race_preserves_claimed_children(
        self, authenticated_client, db_with_fk
    ):
        """``clear_history`` race: a queue claim landing between the
        per-row ``is_claimed`` pre-check and the helper's parent DELETE
        must preserve parent AND children, even though the outer
        transaction commits a sibling delete."""
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents as real_helper,
        )

        _add_research(db_with_fk, "gone")
        _add_research(db_with_fk, "raced")
        _populate_all_child_tables(db_with_fk, "gone")
        _populate_all_child_tables(db_with_fk, "raced")

        def helper_with_claim_race(db_session, research_id, **kwargs):
            if research_id == "raced":
                _add_queued_claim(db_session, research_id)
            return real_helper(db_session, research_id, **kwargs)

        with (
            _patch_session(db_with_fk),
            patch(f"{_RR}.get_active_research_ids", return_value=[]),
            patch(_LIFECYCLE_CLEANUP),
            patch(
                f"{_RR}._delete_research_with_dependents",
                helper_with_claim_race,
            ),
        ):
            response = authenticated_client.post("/api/clear_history")

        assert response.status_code == 200, response.text[:300]

        from local_deep_research.database.models import (
            ResearchHistory,
            ResearchLog,
            ResearchResource,
        )

        db_with_fk.expire_all()
        assert db_with_fk.get(ResearchHistory, "gone") is None
        assert db_with_fk.get(ResearchHistory, "raced") is not None
        assert (
            db_with_fk.query(ResearchLog)
            .filter(ResearchLog.research_id == "raced")
            .count()
            == 1
        )
        assert (
            db_with_fk.query(ResearchResource)
            .filter(ResearchResource.research_id == "raced")
            .count()
            == 1
        )


# ---------------------------------------------------------------------------
# FK-introspection coverage — walks ``Base.metadata`` and fails
# CI when a new FK on ``research_history.id`` is added without
# the helper knowing about it. Closes the
# "next child table orphans silently" class of regressions.
# ---------------------------------------------------------------------------


class TestHelperCoverageBaseMetadata:
    """Every FK edge in the ``research_history`` descendant closure
    must be classified by the cascade planner — no silent orphans or
    runtime ``IntegrityError`` when the next child table lands.

    The planner derives its steps from ``Base.metadata`` (any depth),
    so plain data tables need no code change. This test pins the
    other direction: the survival policy (``_CASCADE_SET_NULL``) must
    stay in sync with the schema, every depth-1 FK must be planned,
    and the delete order must respect child-before-parent for every
    planned delete edge. Walking ``Base.metadata`` (not a
    hand-mirrored fixture) means a new FK, a renamed column, or a
    changed ``ondelete`` action fails CI instead of breaking deletes
    in production."""

    def _closure_edges(self):
        """All ``(child, column, parent, parent_column)`` FK edges
        transitively reachable from ``research_history``."""
        from local_deep_research.database.models import Base

        children_of: dict = {}
        for table in Base.metadata.tables.values():
            for fk in table.foreign_keys:
                children_of.setdefault(fk.column.table.name, []).append(
                    (table.name, fk.parent.name, fk.column.name)
                )
        edges = []
        seen = set()
        queue = ["research_history"]
        while queue:
            parent = queue.pop(0)
            for child, column, parent_column in children_of.get(parent, []):
                key = (child, column, parent, parent_column)
                if key in seen:
                    continue
                seen.add(key)
                edges.append(key)
                queue.append(child)
        return edges

    def test_every_fk_on_research_history_id_is_covered(self):
        from local_deep_research.database.research_cascade import (
            _CASCADE_SET_NULL,
            _research_cascade_plan,
        )

        set_null_steps, delete_steps, _table_to_class = _research_cascade_plan()
        planned = {(s[0], s[1]) for s in set_null_steps + delete_steps}

        # 1. Every depth-1 FK on research_history.id is planned
        # (deleted or nulled) — the original PR #6716 contract.
        depth1 = {
            (child, column)
            for child, column, parent, _ref in self._closure_edges()
            if parent == "research_history"
        }
        assert depth1, (
            "test scaffold found no FKs on research_history.id — "
            "the schema may have drifted"
        )
        assert depth1 <= planned, (
            f"unplanned depth-1 edges: {sorted(depth1 - planned)} — "
            "extend _CASCADE_SET_NULL or fix the planner walk"
        )

        # 2. The full closure is planned, at any depth — with one
        # principled exception: an edge whose parent table is never
        # deleted needs no step, since a row that survives cannot
        # block anything (e.g. ``note_synthesis_sources.synthesis_id``
        # targets ``note_syntheses``, which outlive the research). A
        # new table (or a new FK on an existing one) lands here
        # automatically as a DELETE unless it needs survival
        # semantics, in which case it needs a _CASCADE_SET_NULL
        # entry.
        closure = self._closure_edges()
        all_edges = {(child, column) for child, column, _p, _r in closure}
        delete_tables = {s[0] for s in delete_steps}
        unplanned = all_edges - planned
        for child, column in sorted(unplanned):
            parents = {
                parent
                for c, cc, parent, _r in closure
                if (c, cc) == (child, column)
            }
            assert parents and all(
                parent not in delete_tables and parent != "research_history"
                for parent in parents
            ), (
                f"edge {(child, column)} targets a deleted table "
                f"{sorted(parents)} but has no planned step — the "
                "helper would hit FOREIGN KEY constraint failed. "
                "Extend _CASCADE_SET_NULL or fix the planner walk."
            )

        # 3. No stale _CASCADE_SET_NULL entries: each must match a
        # real edge in the closure (a removed/renamed column without
        # a policy update fails here).
        assert _CASCADE_SET_NULL <= all_edges, (
            f"stale survival policy: {sorted(_CASCADE_SET_NULL - all_edges)}"
        )

        # 4. The two historical special-column shapes still exist as
        # planned: ``note_references.target_research_id`` is deleted,
        # ``chat_messages.research_id`` is nulled (rows survive).
        by_pair = {(s[0], s[1]): s for s in set_null_steps + delete_steps}
        assert by_pair[("note_references", "target_research_id")] in set(
            delete_steps
        )
        assert by_pair[("chat_messages", "research_id")] in set(set_null_steps)

        # 5. Delete order respects child-before-parent for every
        # planned delete edge (re-check the planner's topological
        # order independently of its implementation).
        order = [s[0] for s in delete_steps]
        position = {table: i for i, table in enumerate(order)}
        for child, _column, parent, _ref in delete_steps:
            if parent in position:
                assert position[child] <= position[parent], (
                    f"{child} must be deleted before {parent}"
                )

        # And the helper itself must still be the public
        # symbol used by the routes.
        from local_deep_research.web.routers.research import (
            _delete_research_with_dependents,
        )

        assert callable(_delete_research_with_dependents)

    def test_survival_policy_matches_schema_set_null_edges(self):
        from local_deep_research.database.models import Base
        from local_deep_research.database.research_cascade import (
            _CASCADE_SET_NULL,
        )

        closure = set(self._closure_edges())
        schema_survivors = set()
        for table in Base.metadata.tables.values():
            for fk in table.foreign_keys:
                edge = (
                    table.name,
                    fk.parent.name,
                    fk.column.table.name,
                    fk.column.name,
                )
                if (
                    edge in closure
                    and (fk.ondelete or "").upper() == "SET NULL"
                ):
                    assert fk.parent.nullable, (
                        f"SET NULL on non-nullable {edge}"
                    )
                    schema_survivors.add((table.name, fk.parent.name))

        assert _CASCADE_SET_NULL == schema_survivors, (
            "research cascade survival policy differs from declared SET NULL "
            f"edges: missing {sorted(schema_survivors - _CASCADE_SET_NULL)}, "
            f"stale {sorted(_CASCADE_SET_NULL - schema_survivors)}"
        )