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.

75.5 KB
Raw Blame
"""Contract tests for the chat service layer beneath the router.

Scope: ``chat/service.py`` (``ChatService``) driven directly, with no
Flask/FastAPI request in the loop.  The router's own guards (spawn-path
checks, rate limits, body handling) are covered elsewhere and are not
re-tested here; what is established here is what the service promises to
*any* caller, router included.

Test substrate
--------------
Every method on ``ChatService`` reaches the database through
``get_user_db_session(self.username)``, and LDR gives each account its
own database file.  That per-user file split IS the cross-account
isolation mechanism, so the fixture reproduces it faithfully: a real
**on-disk** SQLite file per username (never ``:memory:``, which would
collapse the two accounts into one store and make every ownership
assertion vacuous), created from the production ``Base.metadata`` so the
real constraints -- ``uq_chat_message_session_seq``,
``ux_research_history_chat_session_in_progress``, the FK ``ondelete``
rules -- are the ones under test.  ``PRAGMA foreign_keys`` is enabled per
connection because SQLite defaults it off, which would silently no-op the
cascade rules ``delete_session`` relies on.

The routing shim records the username it was handed, so "scopes by
username" is asserted twice over: once structurally (the caller lands in
the wrong file and finds nothing) and once directly (the argument passed).

The LLM boundary is stubbed in every test that touches it; nothing here
makes a network call.

Known defects are recorded as ``strict=True`` xfails (the convention
already used by ``tests/web/test_pagination_clamping_census.py``) so the
suite stays green while the defect stays machine-checked: whoever fixes
the production code gets an XPASS telling them to delete the marker.
"""

import threading
from contextlib import contextmanager, nullcontext
from datetime import UTC, datetime, timedelta
from unittest.mock import MagicMock, patch

import pytest
from sqlalchemy import create_engine, event, text
from sqlalchemy.exc import IntegrityError, SQLAlchemyError
from sqlalchemy.orm import sessionmaker

from local_deep_research.chat.service import (
    ArchiveBlockedError,
    AttemptInProgress,
    AttemptNotFound,
    ChatCursorError,
    ChatService,
    ChatSessionNotFound,
)
from local_deep_research.constants import ResearchStatus
from local_deep_research.database.models import (
    Base,
    ChatMessage,
    ChatProgressStep,
    ChatSession,
    ResearchHistory,
    UserActiveResearch,
)

SERVICE = "local_deep_research.chat.service"

OWNER = "alice"
INTRUDER = "mallory"


# ---------------------------------------------------------------------
# Per-user on-disk SQLite substrate
# ---------------------------------------------------------------------


def _enable_foreign_keys(dbapi_connection, _record):
    """SQLite defaults FKs OFF; the cascade rules under test need them."""
    dbapi_connection.execute("PRAGMA foreign_keys = ON")


class UserDatabases:
    """One real on-disk SQLite database per username."""

    def __init__(self, root):
        self._root = root
        self._engines = {}
        self._makers = {}
        #: usernames ``get_user_db_session`` was called with, in order.
        self.routed_usernames = []

    def _maker(self, username):
        if username not in self._makers:
            path = self._root / f"user_{username}.db"
            engine = create_engine(f"sqlite:///{path}")
            event.listen(engine, "connect", _enable_foreign_keys)
            Base.metadata.create_all(engine)
            self._engines[username] = engine
            self._makers[username] = sessionmaker(bind=engine)
        return self._makers[username]

    @contextmanager
    def raw(self, username):
        """A plain session on ``username``'s database, for seeding and
        for reading back what the service actually wrote."""
        db = self._maker(username)()
        try:
            yield db
        finally:
            db.close()

    def service(self, username):
        return ChatService(username=username)

    def dispose(self):
        for engine in self._engines.values():
            engine.dispose()


@pytest.fixture
def user_dbs(tmp_path):
    """Patch the service's DB entry point onto per-username real files."""
    dbs = UserDatabases(tmp_path)

    @contextmanager
    def routed_session(username=None, password=None, session_id=None):
        # Production raises DatabaseSessionError on a missing username;
        # failing loudly here stops a method that forgot to pass
        # self.username from quietly getting its own "None" database.
        if not username:
            raise AssertionError(
                "service opened a DB session without a username"
            )
        dbs.routed_usernames.append(username)
        with dbs.raw(username) as db:
            yield db

    with patch(f"{SERVICE}.get_user_db_session", routed_session):
        yield dbs
    dbs.dispose()


def seed_research(
    dbs,
    username,
    research_id,
    session_id,
    status=ResearchStatus.IN_PROGRESS,
    submission_message_id=None,
):
    """Insert a ResearchHistory row bound to ``session_id``."""
    meta = None
    if submission_message_id is not None:
        meta = {"submission": {"message_id": submission_message_id}}
    with dbs.raw(username) as db:
        db.add(
            ResearchHistory(
                id=research_id,
                query="seeded query",
                mode="quick",
                status=status,
                created_at="2026-01-01T00:00:00",
                chat_session_id=session_id,
                research_meta=meta,
            )
        )
        db.commit()


def seed_active_research_row(dbs, username, research_id):
    """Insert the per-user concurrency-cap row for ``research_id``."""
    with dbs.raw(username) as db:
        db.add(
            UserActiveResearch(
                username=username,
                research_id=research_id,
                status=ResearchStatus.IN_PROGRESS,
            )
        )
        db.commit()


def seed_message_at(
    dbs, username, session_id, message_id, created_at, sequence, content
):
    """Insert a ChatMessage with an exact ``created_at`` and sequence."""
    with dbs.raw(username) as db:
        db.add(
            ChatMessage(
                id=message_id,
                session_id=session_id,
                role="user",
                message_type="query",
                content=content,
                sequence_number=sequence,
                created_at=created_at,
            )
        )
        db.commit()


def seed_step_at(
    dbs, username, session_id, research_id, step_id, created_at, sequence
):
    """Insert a ChatProgressStep with an exact ``created_at``.

    ``add_progress_step`` stamps ``utcnow()``, so ordering tests that need
    a step to sit *between* two seeded messages must place it explicitly.
    """
    with dbs.raw(username) as db:
        db.add(
            ChatProgressStep(
                id=step_id,
                research_id=research_id,
                session_id=session_id,
                phase="search",
                content=f"step {sequence}",
                sequence_number=sequence,
                created_at=created_at,
            )
        )
        db.commit()


def message_count_of(dbs, username, session_id):
    with dbs.raw(username) as db:
        row = db.query(ChatSession).filter_by(id=session_id).first()
        return None if row is None else row.message_count


@pytest.fixture
def two_accounts(user_dbs):
    """OWNER with a populated session; INTRUDER with a session of their
    own (the positive control that INTRUDER's service object works at
    all, so a refusal below is scoping and not a broken fixture)."""
    owner = user_dbs.service(OWNER)
    intruder = user_dbs.service(INTRUDER)

    owner_session = owner.create_session(initial_query="owner topic")
    owner_message = owner.add_message(
        owner_session, "user", "owner secret question", "query"
    )
    owner_research = "research-owner-0001"
    seed_research(
        user_dbs,
        OWNER,
        owner_research,
        owner_session,
        status=ResearchStatus.COMPLETED,
        submission_message_id=owner_message,
    )

    intruder_session = intruder.create_session(initial_query="mallory topic")

    return {
        "dbs": user_dbs,
        "owner": owner,
        "intruder": intruder,
        "owner_session": owner_session,
        "owner_message": owner_message,
        "owner_research": owner_research,
        "intruder_session": intruder_session,
    }


# ---------------------------------------------------------------------
# 1. Session ownership
# ---------------------------------------------------------------------


class TestSessionOwnership:
    """A session id belonging to another account must be unreachable, and
    the same call on the caller's own session must succeed."""

    def test_every_db_method_routes_by_the_instance_username(self, user_dbs):
        """Direct evidence that scoping is driven by ``self.username``.

        A method that hard-coded a username, cached a session across
        instances, or reached a process-global store would show up here as
        a routing key that isn't the one the service was constructed with.
        """
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="routing")
        message_id = service.add_message(session_id, "user", "hello", "query")
        research_id = "research-routing-1"
        seed_research(
            user_dbs,
            OWNER,
            research_id,
            session_id,
            status=ResearchStatus.COMPLETED,
            submission_message_id=message_id,
        )
        user_dbs.routed_usernames.clear()

        service.get_session(session_id)
        service.get_session_messages(session_id)
        service.list_sessions()
        service.get_in_progress_research_id(session_id)
        service.update_session_title(session_id, "renamed")
        service.update_accumulated_context(session_id, new_topics=["t"])
        service.add_progress_step(session_id, research_id, "step")
        service.get_original_attempt_query(session_id, research_id)
        service.reactivate_session(session_id)
        service.archive_session(session_id)

        assert user_dbs.routed_usernames, "no DB session was opened"
        assert set(user_dbs.routed_usernames) == {OWNER}

    def test_reads_of_another_accounts_session_are_refused(self, two_accounts):
        """Each read either raises the 404-mapped error or returns empty
        for the intruder, and returns real data for the owner."""
        owner = two_accounts["owner"]
        intruder = two_accounts["intruder"]
        sid = two_accounts["owner_session"]
        rid = two_accounts["owner_research"]

        # get_session
        with pytest.raises(ChatSessionNotFound):
            intruder.get_session(sid)
        assert owner.get_session(sid)["title"] == "owner topic"

        # get_session_messages
        assert intruder.get_session_messages(sid) == []
        owner_messages = owner.get_session_messages(sid)
        assert [m["content"] for m in owner_messages] == [
            "owner secret question"
        ]

        # get_in_progress_research_id (owner's row is COMPLETED -> None
        # for both; the discriminating read is get_original_attempt_query)
        with pytest.raises(AttemptNotFound):
            intruder.get_original_attempt_query(sid, rid)
        assert (
            owner.get_original_attempt_query(sid, rid)
            == "owner secret question"
        )

        # list_sessions never leaks the other account's row
        intruder_ids = {s["id"] for s in intruder.list_sessions()}
        assert sid not in intruder_ids
        assert intruder_ids == {two_accounts["intruder_session"]}
        assert sid in {s["id"] for s in owner.list_sessions()}

    def test_writes_to_another_accounts_session_are_refused(self, two_accounts):
        """A refused write must also leave the owner's row untouched --
        a False return with a stray write would still be a breach."""
        dbs = two_accounts["dbs"]
        owner = two_accounts["owner"]
        intruder = two_accounts["intruder"]
        sid = two_accounts["owner_session"]
        rid = two_accounts["owner_research"]

        assert intruder.update_session_title(sid, "pwned") is False
        assert (
            intruder.update_accumulated_context(sid, new_topics=["injected"])
            is False
        )
        assert intruder.archive_session(sid) is False
        assert intruder.reactivate_session(sid) is False
        with pytest.raises(ValueError, match="not found"):
            intruder.add_message(sid, "user", "injected", "query")
        with pytest.raises(ValueError, match="not found"):
            intruder.add_progress_step(sid, rid, "injected step")
        with pytest.raises(AttemptNotFound):
            intruder.delete_attempt(sid, rid)
        assert intruder.delete_session(sid) is False

        # The owner's session survived every one of those, unmodified.
        with dbs.raw(OWNER) as db:
            row = db.query(ChatSession).filter_by(id=sid).first()
            assert row is not None
            assert row.title == "owner topic"
            assert row.status == "active"
            assert (row.accumulated_context or {}).get("topics") == []
            assert db.query(ChatMessage).filter_by(session_id=sid).count() == 1
            assert (
                db.query(ChatProgressStep).filter_by(session_id=sid).count()
                == 0
            )
            assert db.query(ResearchHistory).filter_by(id=rid).count() == 1

        # Positive control: the same writes succeed for the owner.
        assert owner.update_session_title(sid, "owner renamed") is True
        assert (
            owner.update_accumulated_context(sid, new_topics=["real"]) is True
        )
        assert owner.add_progress_step(sid, rid, "real step")
        assert owner.archive_session(sid) is True

    def test_intruder_writes_do_not_land_in_the_intruders_own_database(
        self, two_accounts
    ):
        """The refused writes must not have created shadow rows under the
        intruder's account either (which would let a later reactivate or
        list surface the borrowed id)."""
        dbs = two_accounts["dbs"]
        intruder = two_accounts["intruder"]
        sid = two_accounts["owner_session"]

        intruder.update_session_title(sid, "pwned")
        intruder.archive_session(sid)
        intruder.delete_session(sid)

        with dbs.raw(INTRUDER) as db:
            assert db.query(ChatSession).filter_by(id=sid).count() == 0
            assert db.query(ChatSession).count() == 1, (
                "only the intruder's own session should exist"
            )

    def test_regenerate_title_on_another_accounts_session_makes_no_llm_call(
        self, two_accounts
    ):
        """The ownership check must run *before* the LLM is invoked, or a
        forged session id becomes a free credit-burning oracle."""
        dbs = two_accounts["dbs"]
        intruder = two_accounts["intruder"]
        owner = two_accounts["owner"]
        sid = two_accounts["owner_session"]

        llm = MagicMock()
        llm.invoke.return_value = MagicMock(content="Stolen Title")
        with patch(
            "local_deep_research.config.llm_config.get_llm",
            return_value=llm,
        ):
            assert (
                intruder.regenerate_title_with_llm(
                    sid, "owner topic", _TITLE_SNAPSHOT
                )
                is None
            )
            assert llm.invoke.call_count == 0

            # Positive control: the owner's identical call does invoke it
            # and does persist the result.
            assert (
                owner.regenerate_title_with_llm(
                    sid, "owner topic", _TITLE_SNAPSHOT
                )
                == "Stolen Title"
            )
            assert llm.invoke.call_count == 1

        with dbs.raw(OWNER) as db:
            assert (
                db.query(ChatSession).filter_by(id=sid).first().title
                == "Stolen Title"
            )


# ---------------------------------------------------------------------
# 2. Message ordering and cursor pagination
# ---------------------------------------------------------------------

_ISO = "2026-03-01T12:00:00+00:00"


def _at(seconds):
    return datetime(2026, 3, 1, 12, 0, 0, tzinfo=UTC) + timedelta(
        seconds=seconds
    )


@pytest.fixture
def paged_session(user_dbs):
    """A session with six messages at known, distinct timestamps."""
    service = user_dbs.service(OWNER)
    session_id = service.create_session(initial_query="pagination")
    ids = []
    for index in range(6):
        message_id = f"msg-{index:02d}"
        seed_message_at(
            user_dbs,
            OWNER,
            session_id,
            message_id,
            _at(index),
            index + 1,
            f"m{index}",
        )
        ids.append(message_id)
    return service, session_id, ids


class TestCallerOwnedMessageTransactions:
    @pytest.mark.parametrize("flush_mode", ["enabled", "disabled", "context"])
    @pytest.mark.parametrize("existing_sequence", [0, 10])
    @pytest.mark.parametrize("finish", ["commit", "rollback"])
    def test_staged_messages_keep_distinct_sequences_and_caller_ownership(
        self, user_dbs, flush_mode, existing_sequence, finish
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="staged messages")
        if existing_sequence:
            seed_message_at(
                user_dbs,
                OWNER,
                session_id,
                "survivor",
                _at(0),
                existing_sequence,
                "surviving message",
            )
            with user_dbs.raw(OWNER) as db:
                db.get(ChatSession, session_id).message_count = 1
                db.commit()

        before_count = int(bool(existing_sequence))
        with user_dbs.raw(OWNER) as db:
            db.autoflush = flush_mode != "disabled"
            guard = (
                db.no_autoflush if flush_mode == "context" else nullcontext()
            )
            with guard:
                ids = [
                    service.insert_message_in_db(
                        db, session_id, "user", f"staged {index}", "query"
                    )
                    for index in range(2)
                ]
                if flush_mode != "enabled":
                    # Respect a caller deliberately constructing several rows
                    # before flushing; the helper must not force that flush.
                    pending = {
                        row.id: row.sequence_number
                        for row in db.new
                        if isinstance(row, ChatMessage)
                    }
                    assert [pending[mid] for mid in ids] == [
                        existing_sequence + 1,
                        existing_sequence + 2,
                    ]

            # Neither staged messages nor the counter updates are visible
            # outside the transaction before the caller chooses to commit.
            with user_dbs.raw(OWNER) as observer:
                assert (
                    observer.get(ChatSession, session_id).message_count
                    == before_count
                )
                assert observer.query(ChatMessage).count() == before_count

            if finish == "commit":
                db.commit()
            else:
                db.rollback()

        with user_dbs.raw(OWNER) as db:
            expected = [existing_sequence] if existing_sequence else []
            if finish == "commit":
                expected.extend([existing_sequence + 1, existing_sequence + 2])
            assert [
                row.sequence_number
                for row in db.query(ChatMessage)
                .filter_by(session_id=session_id)
                .order_by(ChatMessage.sequence_number)
            ] == expected
            assert db.get(ChatSession, session_id).message_count == len(
                expected
            )

    def test_pending_message_in_another_session_does_not_set_the_sequence(
        self, user_dbs
    ):
        service = user_dbs.service(OWNER)
        target = service.create_session(initial_query="target")
        sibling = service.create_session(initial_query="sibling")
        with user_dbs.raw(OWNER) as db:
            db.autoflush = False
            sibling_id = service.insert_message_in_db(
                db, sibling, "user", "sibling", "query"
            )
            sibling_message = next(
                row for row in db.new if row.id == sibling_id
            )
            sibling_message.sequence_number = 1000
            ids = [
                service.insert_message_in_db(
                    db, target, "user", f"target {index}", "query"
                )
                for index in range(2)
            ]
            assert sibling_message in db.new
            db.commit()
        with user_dbs.raw(OWNER) as db:
            assert [
                db.get(ChatMessage, mid).sequence_number for mid in ids
            ] == [
                1,
                2,
            ]
            assert db.get(ChatMessage, sibling_id).sequence_number == 1000
            assert db.get(ChatSession, target).message_count == 2
            assert db.get(ChatSession, sibling).message_count == 1


class TestMessageOrdering:
    def test_page_is_returned_oldest_to_newest_within_the_newest_window(
        self, paged_session
    ):
        service, session_id, _ids = paged_session

        assert [
            m["content"] for m in service.get_session_messages(session_id)
        ] == ["m0", "m1", "m2", "m3", "m4", "m5"]
        # A limited page is the NEWEST window, still rendered ASC.
        assert [
            m["content"]
            for m in service.get_session_messages(session_id, limit=2)
        ] == ["m4", "m5"]
        # offset walks backwards through older windows.
        assert [
            m["content"]
            for m in service.get_session_messages(session_id, limit=2, offset=2)
        ] == ["m2", "m3"]

    def test_same_timestamp_rows_keep_insertion_order_via_sequence_number(
        self, user_dbs
    ):
        """The secondary ORDER BY on sequence_number is what stops
        rapid-fire inserts from rendering in arbitrary order."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="collide")
        collision = _at(0)
        # Seeded out of sequence order so a missing tie-break shows up.
        for message_id, sequence, content in (
            ("tie-b", 2, "second"),
            ("tie-c", 3, "third"),
            ("tie-a", 1, "first"),
        ):
            seed_message_at(
                user_dbs,
                OWNER,
                session_id,
                message_id,
                collision,
                sequence,
                content,
            )

        page = service.get_session_messages(session_id)
        assert [m["content"] for m in page] == ["first", "second", "third"]

    def test_progress_steps_are_merged_into_the_message_stream(self, user_dbs):
        """Steps live in their own table with their own sequence space;
        the service interleaves them into one created_at-ordered stream."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="merge")
        research_id = "research-merge-1"
        seed_research(user_dbs, OWNER, research_id, session_id)
        seed_message_at(
            user_dbs, OWNER, session_id, "m-a", _at(0), 1, "question"
        )
        seed_step_at(
            user_dbs, OWNER, session_id, research_id, "st-1", _at(30), 1
        )
        seed_message_at(
            user_dbs, OWNER, session_id, "m-b", _at(60), 2, "answer"
        )

        page = service.get_session_messages(session_id)
        assert [m["message_type"] for m in page] == [
            "query",
            "step",
            "query",
        ]
        assert [m["content"] for m in page] == [
            "question",
            "step 1",
            "answer",
        ]
        # Step rows are surfaced with a namespaced id and the step's own
        # sequence space, so a step seq 1 and a message seq 1 coexist.
        assert page[1]["id"] == "step-st-1"
        assert page[1]["research_id"] == research_id
        assert [page[0]["sequence_number"], page[1]["sequence_number"]] == [
            1,
            1,
        ]

    def test_messages_of_a_sibling_session_are_not_merged_in(self, user_dbs):
        """Same account, two sessions: the session_id filter is the only
        thing separating them."""
        service = user_dbs.service(OWNER)
        first = service.create_session(initial_query="first")
        second = service.create_session(initial_query="second")
        seed_message_at(user_dbs, OWNER, first, "f-1", _at(0), 1, "in first")
        seed_message_at(user_dbs, OWNER, second, "s-1", _at(1), 1, "in second")

        assert [m["content"] for m in service.get_session_messages(first)] == [
            "in first"
        ]
        assert [m["content"] for m in service.get_session_messages(second)] == [
            "in second"
        ]


class TestCursorPagination:
    @pytest.mark.parametrize("composite", [False, True])
    def test_cursor_preserves_mixed_timestamp_precision(
        self, user_dbs, composite
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="timestamp precision")
        for message_id, sequence, stamp in (
            ("microsecond", 1, "2026-01-01 12:00:00.123001"),
            ("millisecond", 3, "2026-01-01 12:00:00.123"),
            ("padded", 2, "2026-01-01 12:00:00.123000"),
        ):
            seed_message_at(
                user_dbs,
                OWNER,
                session_id,
                message_id,
                _at(0),
                sequence,
                message_id,
            )
            # Reproduce both SQL utcnow() and bound-datetime storage forms.
            with user_dbs.raw(OWNER) as db:
                db.execute(
                    text(
                        "UPDATE chat_messages SET created_at = :stamp WHERE id = :id"
                    ),
                    {"stamp": stamp, "id": message_id},
                )
                db.commit()
        latest = service.get_session_messages(session_id, limit=1)
        assert latest[0]["id"] == "microsecond"
        cursor = {"before_created_at": latest[0]["created_at"]}
        if composite:
            cursor["before_id"] = latest[0]["id"]
        older = service.get_session_messages(session_id, limit=1, **cursor)
        assert older[0]["id"] == "millisecond"
        cursor = {"before_created_at": older[0]["created_at"]}
        if composite:
            cursor["before_id"] = older[0]["id"]
        oldest = service.get_session_messages(session_id, limit=1, **cursor)
        assert [m["id"] for m in oldest] == (["padded"] if composite else [])

    def test_composite_cursor_follows_sequence_with_nonmonotonic_ids(
        self, user_dbs
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="UUID order")
        for message_id, sequence in (("z", 1), ("a", 2), ("m", 3)):
            seed_message_at(
                user_dbs,
                OWNER,
                session_id,
                message_id,
                _at(0),
                sequence,
                f"message {sequence}",
            )

        cursor = {}
        seen = []
        for _ in range(4):
            page = service.get_session_messages(session_id, limit=1, **cursor)
            if not page:
                break
            seen.append(page[0]["content"])
            cursor = {
                "before_created_at": page[0]["created_at"],
                "before_id": page[0]["id"],
            }
        assert seen == ["message 3", "message 2", "message 1"]

    def test_composite_cursor_advances_past_progress_step(self, user_dbs):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="step cursor")
        seed_research(user_dbs, OWNER, "step-research", session_id)
        seed_message_at(
            user_dbs, OWNER, session_id, "question", _at(0), 1, "question"
        )
        seed_step_at(
            user_dbs, OWNER, session_id, "step-research", "progress", _at(1), 1
        )
        newest = service.get_session_messages(session_id, limit=1)
        assert newest[0]["id"] == "step-progress"
        older = service.get_session_messages(
            session_id,
            limit=1,
            before_created_at=newest[0]["created_at"],
            before_id=newest[0]["id"],
        )
        assert [m["content"] for m in older] == ["question"]

    def test_a_valid_cursor_returns_strictly_older_entries(self, paged_session):
        """Positive control for the whole cursor mechanism."""
        service, session_id, _ids = paged_session

        newest = service.get_session_messages(session_id, limit=2)
        assert [m["content"] for m in newest] == ["m4", "m5"]

        older = service.get_session_messages(
            session_id,
            limit=2,
            before_created_at=newest[0]["created_at"],
        )
        assert [m["content"] for m in older] == ["m2", "m3"]

        oldest = service.get_session_messages(
            session_id,
            limit=2,
            before_created_at=older[0]["created_at"],
        )
        assert [m["content"] for m in oldest] == ["m0", "m1"]

        # Walking past the beginning terminates.
        assert (
            service.get_session_messages(
                session_id,
                limit=2,
                before_created_at=oldest[0]["created_at"],
            )
            == []
        )

    def test_composite_cursor_does_not_drop_same_timestamp_rows(self, user_dbs):
        """``before_id`` exists so a page boundary that lands inside a
        group of same-millisecond rows loses none of them."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="boundary")
        collision = _at(0)
        for message_id, sequence, content in (
            ("bnd-a", 1, "first"),
            ("bnd-b", 2, "second"),
            ("bnd-c", 3, "third"),
        ):
            seed_message_at(
                user_dbs,
                OWNER,
                session_id,
                message_id,
                collision,
                sequence,
                content,
            )

        page = service.get_session_messages(session_id, limit=1)
        assert [m["content"] for m in page] == ["third"]

        with_composite = service.get_session_messages(
            session_id,
            limit=5,
            before_created_at=page[0]["created_at"],
            before_id=page[0]["id"],
        )
        assert [m["content"] for m in with_composite] == ["first", "second"]

        # Without before_id the strict `<` drops the whole colliding
        # group -- which is exactly why the composite form was added.
        timestamp_only = service.get_session_messages(
            session_id,
            limit=5,
            before_created_at=page[0]["created_at"],
        )
        assert timestamp_only == []

    def test_composite_cursor_keeps_step_before_message_on_tie(self, user_dbs):
        """At equal timestamps/sequences, the step precedes the message."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="step boundary")
        research_id = "research-step-boundary"
        seed_research(user_dbs, OWNER, research_id, session_id)
        step_id = service.add_progress_step(
            session_id, research_id, "boundary step"
        )
        with user_dbs.raw(OWNER) as db:
            step = db.query(ChatProgressStep).filter_by(id=step_id).first()
            step_created_at = step.created_at
        seed_message_at(
            user_dbs,
            OWNER,
            session_id,
            "after-step",
            step_created_at,
            1,
            "same instant message",
        )

        page = service.get_session_messages(
            session_id,
            limit=5,
            before_created_at=step_created_at.isoformat(),
            before_id="after-step",
        )
        # The earlier step is included; the cursor message is excluded.
        assert [m["content"] for m in page] == ["boundary step"]

    @pytest.mark.parametrize("page_size", [1, 2, 4])
    def test_composite_cursor_walks_mixed_ties_once(self, user_dbs, page_size):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="mixed boundaries")
        for research_id in ("run-1", "run-2"):
            seed_research(
                user_dbs,
                OWNER,
                research_id,
                session_id,
                status=ResearchStatus.COMPLETED,
            )
        for message_id, sequence in (("z", 1), ("a", 2), ("m", 3)):
            seed_message_at(
                user_dbs,
                OWNER,
                session_id,
                message_id,
                _at(0),
                sequence,
                message_id,
            )
        for research_id, step_id, sequence, stamp in (
            ("run-1", "q", 1, _at(0)),
            ("run-2", "p", 1, _at(0)),
            ("run-1", "r", 2, _at(0)),
            ("run-1", "old", 3, _at(-1)),
        ):
            seed_step_at(
                user_dbs,
                OWNER,
                session_id,
                research_id,
                step_id,
                stamp,
                sequence,
            )
        expected = ["step-old", "step-p", "step-q", "z", "step-r", "a", "m"]
        assert [
            m["id"] for m in service.get_session_messages(session_id)
        ] == expected
        seen = []
        cursor = {}
        for _ in range(len(expected) + 1):
            page = service.get_session_messages(
                session_id, limit=page_size, **cursor
            )
            if not page:
                break
            seen = [m["id"] for m in page] + seen
            cursor = {
                "before_created_at": page[0]["created_at"],
                "before_id": page[0]["id"],
            }
        else:
            pytest.fail("cursor did not reach the end of history")
        assert seen == expected

    @pytest.mark.parametrize(
        "invalid_boundary",
        ["missing", "deleted", "other_session", "wrong_time"],
    )
    def test_composite_cursor_rejects_unusable_boundary(
        self, user_dbs, invalid_boundary
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="cursor owner")
        other_id = service.create_session(initial_query="other session")
        seed_message_at(
            user_dbs,
            OWNER,
            other_id if invalid_boundary == "other_session" else session_id,
            "boundary",
            _at(0),
            1,
            "boundary",
        )
        if invalid_boundary == "deleted":
            with user_dbs.raw(OWNER) as db:
                db.delete(db.get(ChatMessage, "boundary"))
                db.commit()
        with pytest.raises(ChatCursorError, match="must identify an entry"):
            service.get_session_messages(
                session_id,
                before_created_at=_at(
                    1 if invalid_boundary == "wrong_time" else 0
                ).isoformat(),
                before_id="step-missing"
                if invalid_boundary == "missing"
                else "boundary",
            )

    @pytest.mark.parametrize(
        "bad_cursor",
        [
            "not-a-date",
            "2026-13-45T00:00:00Z",
            "2026",
            "3/1/2026, 12:00:00 PM",
        ],
        ids=[
            "garbage",
            "out-of-range",
            "year-only",
            "locale-formatted",
        ],
    )
    def test_an_unusable_cursor_is_reported_rather_than_dropped(
        self, paged_session, bad_cursor
    ):
        """An unparseable cursor is a caller error, never an unfiltered page."""
        service, session_id, _ids = paged_session
        with pytest.raises(ChatCursorError):
            service.get_session_messages(
                session_id, limit=2, before_created_at=bad_cursor
            )

    def test_rejected_cursors_do_not_change_valid_paging(self, paged_session):
        service, session_id, _ids = paged_session
        newest = service.get_session_messages(session_id, limit=2)
        assert [m["content"] for m in newest] == ["m4", "m5"]
        for bad_cursor in ("not-a-date", "2026", "3/1/2026, 12:00:00 PM"):
            with pytest.raises(ChatCursorError):
                service.get_session_messages(
                    session_id, limit=2, before_created_at=bad_cursor
                )
        assert service.get_session_messages(session_id, limit=2) == newest
        assert len(service.get_session_messages(session_id)) == 6

    def test_an_empty_cursor_means_no_cursor(self, paged_session):
        """The router normalises an empty string to None; the service
        treats both as an absent cursor."""
        service, session_id, _ids = paged_session
        assert service.get_session_messages(
            session_id, limit=2, before_created_at=""
        ) == service.get_session_messages(session_id, limit=2)

    def test_before_id_without_a_timestamp_is_reported_rather_than_dropped(
        self, paged_session
    ):
        service, session_id, ids = paged_session
        with pytest.raises(ChatCursorError, match="requires before_created_at"):
            service.get_session_messages(session_id, limit=2, before_id=ids[4])

    def test_empty_timestamp_does_not_enable_an_id_only_cursor(
        self, paged_session
    ):
        service, session_id, ids = paged_session
        with pytest.raises(ChatCursorError, match="requires before_created_at"):
            service.get_session_messages(
                session_id, limit=2, before_id=ids[4], before_created_at=""
            )

    @pytest.mark.parametrize(
        "naive_cursor",
        ["2026-03-01", "2026-03-01T12:00:00", "2026-03-01T12:00:00.000000"],
    )
    def test_naive_cursor_is_rejected_before_database_binding(
        self, paged_session, naive_cursor
    ):
        service, session_id, _ids = paged_session
        assert (
            service.get_session_messages(
                session_id, limit=2, before_created_at=_ISO
            )
            == []
        )
        with pytest.raises(ChatCursorError, match="timezone"):
            service.get_session_messages(
                session_id, limit=2, before_created_at=naive_cursor
            )


# ---------------------------------------------------------------------
# 3. The research-slot invariant
# ---------------------------------------------------------------------


class TestResearchSlotInvariant:
    def test_only_one_in_progress_research_per_chat_session(self, user_dbs):
        """The partial unique index is the enforcement point; assert it is
        actually built by the model metadata, not just described in a
        docstring."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="slot")
        other_session = service.create_session(initial_query="other slot")

        seed_research(user_dbs, OWNER, "slot-1", session_id)
        assert service.get_in_progress_research_id(session_id) == "slot-1"

        with pytest.raises(IntegrityError):
            seed_research(user_dbs, OWNER, "slot-2", session_id)

        # A different session may hold its own slot ...
        seed_research(user_dbs, OWNER, "slot-3", other_session)
        assert service.get_in_progress_research_id(other_session) == "slot-3"
        # ... and a terminal-status research does not occupy one.
        seed_research(
            user_dbs,
            OWNER,
            "slot-4",
            session_id,
            status=ResearchStatus.COMPLETED,
        )
        assert service.get_in_progress_research_id(session_id) == "slot-1"

    def test_a_free_slot_reads_as_none(self, user_dbs):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="empty slot")
        assert service.get_in_progress_research_id(session_id) is None

    def test_archive_is_blocked_while_the_slot_is_held(self, user_dbs):
        """Archiving flips the session read-only; doing that under a live
        research would orphan it."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="archive")
        seed_research(user_dbs, OWNER, "archive-slot", session_id)

        with pytest.raises(ArchiveBlockedError):
            service.archive_session(session_id)
        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(ChatSession).filter_by(id=session_id).first().status
                == "active"
            )

        # Positive control: once the research reaches a terminal state the
        # same call succeeds.
        with user_dbs.raw(OWNER) as db:
            row = db.query(ResearchHistory).filter_by(id="archive-slot").first()
            row.status = ResearchStatus.COMPLETED
            db.commit()
        assert service.archive_session(session_id) is True

    def test_a_live_research_cannot_be_deleted_and_keeps_its_slot(
        self, user_dbs
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="live")
        research_id = "live-research"
        seed_research(user_dbs, OWNER, research_id, session_id)
        seed_active_research_row(user_dbs, OWNER, research_id)
        terminated = []

        with (
            patch(f"{SERVICE}.is_research_thread_alive", return_value=True),
            patch(
                f"{SERVICE}.set_termination_flag",
                side_effect=terminated.append,
            ),
        ):
            with pytest.raises(AttemptInProgress):
                service.delete_attempt(session_id, research_id)

        # The worker was asked to drain, but nothing was destroyed under it.
        assert terminated == [research_id]
        assert service.get_in_progress_research_id(session_id) == research_id
        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(ResearchHistory).filter_by(id=research_id).count() == 1
            )
            assert db.query(UserActiveResearch).count() == 1

    def test_a_crashed_research_releases_the_slot(self, user_dbs):
        """Crashed == the row is still IN_PROGRESS but no worker thread is
        alive.  Deleting the attempt must free the DB slot, the per-user
        cap row, and the in-memory registration, so the session is usable
        again."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="crashed")
        research_id = "crashed-research"
        message_id = service.add_message(
            session_id, "user", "crashed question", "query"
        )
        seed_research(
            user_dbs,
            OWNER,
            research_id,
            session_id,
            submission_message_id=message_id,
        )
        seed_active_research_row(user_dbs, OWNER, research_id)
        service.add_progress_step(session_id, research_id, "half a step")

        assert service.get_in_progress_research_id(session_id) == research_id

        cleaned = []
        with (
            patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
            patch(f"{SERVICE}.cleanup_research", side_effect=cleaned.append),
        ):
            assert service.delete_attempt(session_id, research_id) is True

        assert cleaned == [research_id], (
            "in-memory registration must be released or the slot leaks "
            "for the lifetime of the process"
        )
        assert service.get_in_progress_research_id(session_id) is None
        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(ResearchHistory).filter_by(id=research_id).count() == 0
            )
            assert (
                db.query(UserActiveResearch)
                .filter_by(research_id=research_id)
                .count()
                == 0
            ), "the per-user concurrency cap row must be released too"
            assert (
                db.query(ChatProgressStep)
                .filter_by(research_id=research_id)
                .count()
                == 0
            )
            assert db.query(ChatMessage).filter_by(id=message_id).count() == 0

        # The freed slot is genuinely reusable: the partial unique index
        # accepts a fresh in-progress research for the same session.
        seed_research(user_dbs, OWNER, "replacement-research", session_id)
        assert (
            service.get_in_progress_research_id(session_id)
            == "replacement-research"
        )


# ---------------------------------------------------------------------
# 4. Attempt deletion and the message sequence counter
# ---------------------------------------------------------------------


def _seed_attempt(service, dbs, session_id, research_id, label):
    """One attempt: user message (research_id NULL, linked via meta) plus
    the assistant response -- the shape ``_spawn_chat_research`` creates."""
    user_message_id = service.add_message(
        session_id, "user", f"{label} question", "query"
    )
    seed_research(
        dbs,
        OWNER,
        research_id,
        session_id,
        status=ResearchStatus.COMPLETED,
        submission_message_id=user_message_id,
    )
    service.add_message(
        session_id,
        "assistant",
        f"{label} answer",
        "response",
        research_id=research_id,
    )
    return user_message_id


class TestAttemptDeletion:
    def test_deleting_an_attempt_removes_both_bubbles_and_adjusts_count(
        self, user_dbs
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="attempts")
        _seed_attempt(service, user_dbs, session_id, "att-1", "first")
        _seed_attempt(service, user_dbs, session_id, "att-2", "second")
        assert message_count_of(user_dbs, OWNER, session_id) == 4

        with (
            patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
            patch(f"{SERVICE}.cleanup_research"),
        ):
            assert service.delete_attempt(session_id, "att-2") is True

        # The sibling attempt survived intact.
        assert [
            m["content"] for m in service.get_session_messages(session_id)
        ] == ["first question", "first answer"]
        assert message_count_of(user_dbs, OWNER, session_id) == 2

    def test_session_still_accepts_messages_after_deleting_an_old_attempt(
        self, user_dbs
    ):
        """Deleting anything other than the newest attempt must not break
        the session."""
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="reuse")
        _seed_attempt(service, user_dbs, session_id, "reuse-1", "first")
        _seed_attempt(service, user_dbs, session_id, "reuse-2", "second")

        with (
            patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
            patch(f"{SERVICE}.cleanup_research"),
        ):
            service.delete_attempt(session_id, "reuse-1")

        service.add_message(session_id, "user", "third question", "query")

    @pytest.mark.parametrize(
        "deleted,expected_sequences",
        [
            ("brick-1", [3, 4, 5, 6, 7, 8]),
            ("brick-2", [1, 2, 5, 6, 7, 8]),
            ("brick-3", [1, 2, 3, 4, 5, 6]),
        ],
    )
    def test_repeated_sends_after_deletion_preserve_rows_and_exact_count(
        self, user_dbs, deleted, expected_sequences
    ):
        service = user_dbs.service(OWNER)
        session_id = service.create_session(initial_query="sequence gaps")
        for number, label in enumerate(("first", "second", "third"), 1):
            _seed_attempt(
                service, user_dbs, session_id, f"brick-{number}", label
            )

        with (
            patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
            patch(f"{SERVICE}.cleanup_research"),
        ):
            service.delete_attempt(session_id, deleted)
        assert message_count_of(user_dbs, OWNER, session_id) == 4

        for attempt in range(2):
            service.add_message(session_id, "user", "next question", "query")
            assert message_count_of(user_dbs, OWNER, session_id) == 5 + attempt

        with user_dbs.raw(OWNER) as db:
            rows = (
                db.query(ChatMessage)
                .filter_by(session_id=session_id)
                .order_by(ChatMessage.sequence_number)
                .all()
            )
            assert [row.sequence_number for row in rows] == expected_sequences
            assert len(rows) == 6


# ---------------------------------------------------------------------
# 5. Session deletion cleanup
# ---------------------------------------------------------------------


class TestDeleteSessionCleanup:
    @pytest.fixture
    def deletable(self, user_dbs):
        """A session with messages, steps and a live research -- plus a
        sibling session carrying the same kinds of rows, so every deletion
        assertion below has a survivor to contrast against."""
        service = user_dbs.service(OWNER)
        target = service.create_session(initial_query="target")
        sibling = service.create_session(initial_query="sibling")

        for session_id, research_id, label in (
            (target, "del-target", "target"),
            (sibling, "del-sibling", "sibling"),
        ):
            message_id = service.add_message(
                session_id, "user", f"{label} question", "query"
            )
            seed_research(
                user_dbs,
                OWNER,
                research_id,
                session_id,
                submission_message_id=message_id,
            )
            seed_active_research_row(user_dbs, OWNER, research_id)
            service.add_progress_step(session_id, research_id, f"{label} step")

        return service, target, sibling

    def test_delete_removes_messages_steps_and_flags_the_research(
        self, user_dbs, deletable
    ):
        service, target, sibling = deletable
        terminated = []

        with patch(
            f"{SERVICE}.set_termination_flag", side_effect=terminated.append
        ):
            assert service.delete_session(target) is True

        assert terminated == ["del-target"], (
            "only the deleted session's research may be terminated"
        )

        with user_dbs.raw(OWNER) as db:
            assert db.query(ChatSession).filter_by(id=target).count() == 0
            assert (
                db.query(ChatMessage).filter_by(session_id=target).count() == 0
            )
            assert (
                db.query(ChatProgressStep).filter_by(session_id=target).count()
                == 0
            )
            # The seeded sibling survived, rows and all.
            assert db.query(ChatSession).filter_by(id=sibling).count() == 1
            assert (
                db.query(ChatMessage).filter_by(session_id=sibling).count() == 1
            )
            assert (
                db.query(ChatProgressStep).filter_by(session_id=sibling).count()
                == 1
            )
            # The FK is ON DELETE SET NULL, so the research row itself
            # survives -- detached from any chat session.
            orphan = (
                db.query(ResearchHistory).filter_by(id="del-target").first()
            )
            assert orphan is not None
            assert orphan.chat_session_id is None
            sibling_research = (
                db.query(ResearchHistory).filter_by(id="del-sibling").first()
            )
            assert sibling_research.chat_session_id == sibling

        # Deleting an already-deleted session is a clean False, and the
        # sibling is still reachable through the service.
        with patch(f"{SERVICE}.set_termination_flag"):
            assert service.delete_session(target) is False
        assert service.get_session(sibling)["title"] == "sibling"
        assert service.get_in_progress_research_id(sibling) == "del-sibling"

    def test_delete_releases_the_concurrency_slot_of_a_crashed_research(
        self, user_dbs, deletable
    ):
        """A termination flag is only a request; a worker that already
        died will never act on it, so deletion must release the cap row
        itself (as ``delete_attempt`` does)."""
        service, target, sibling = deletable

        with (
            patch(f"{SERVICE}.set_termination_flag"),
            patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
            patch(f"{SERVICE}.cleanup_research") as cleanup,
        ):
            assert service.delete_session(target) is True

        cleanup.assert_called_once_with(
            "del-target", preserve_termination_flag=True
        )
        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(UserActiveResearch)
                .filter_by(research_id="del-target")
                .count()
                == 0
            )
            history = db.get(ResearchHistory, "del-target")
            assert history.chat_session_id is None
            assert history.status == ResearchStatus.SUSPENDED
            assert datetime.fromisoformat(history.completed_at).tzinfo is UTC
            assert (
                db.query(UserActiveResearch)
                .filter_by(research_id="del-sibling")
                .count()
                == 1
            )
        assert service.get_in_progress_research_id(sibling) == "del-sibling"

    def test_a_live_research_keeps_its_slot_until_worker_cleanup(
        self, user_dbs, deletable
    ):
        """A stop request must not free capacity still held by a live worker."""
        service, target, sibling = deletable

        with (
            patch(f"{SERVICE}.set_termination_flag"),
            patch(f"{SERVICE}.is_research_thread_alive", return_value=True),
            patch(f"{SERVICE}.cleanup_research") as cleanup,
        ):
            service.delete_session(target)

        cleanup.assert_not_called()
        with user_dbs.raw(OWNER) as db:
            stale = (
                db.query(UserActiveResearch)
                .filter_by(research_id="del-target")
                .first()
            )
            assert stale is not None
            assert stale.status == ResearchStatus.IN_PROGRESS
            # Both the deleted session's slot and the sibling's are held.
            assert (
                db.query(UserActiveResearch)
                .filter_by(status=ResearchStatus.IN_PROGRESS)
                .count()
                == 2
            )
            # And the detached research row still reads as in progress.
            assert (
                db.query(ResearchHistory)
                .filter_by(id="del-target")
                .first()
                .status
                == ResearchStatus.IN_PROGRESS
            )

        # The sibling's slot is legitimately held and still reachable.
        assert service.get_in_progress_research_id(sibling) == "del-sibling"

    def test_failed_commit_preserves_session_slot_and_process_state(
        self, user_dbs, deletable
    ):
        service, target, _ = deletable

        @contextmanager
        def failing_session(username):
            with user_dbs.raw(username) as db:
                with patch.object(
                    db, "commit", side_effect=SQLAlchemyError("commit failed")
                ):
                    yield db

        with (
            patch(f"{SERVICE}.get_user_db_session", failing_session),
            patch(f"{SERVICE}.is_research_thread_alive", return_value=False),
            patch(f"{SERVICE}.set_termination_flag") as terminate,
            patch(f"{SERVICE}.cleanup_research") as cleanup,
        ):
            assert service.delete_session(target) is False

        terminate.assert_not_called()
        cleanup.assert_not_called()
        with user_dbs.raw(OWNER) as db:
            assert db.get(ChatSession, target) is not None
            history = db.get(ResearchHistory, "del-target")
            assert history.status == ResearchStatus.IN_PROGRESS
            assert history.chat_session_id == target
            assert history.completed_at is None
            assert db.query(UserActiveResearch).count() == 2
            assert (
                db.query(ChatMessage).filter_by(session_id=target).count() == 1
            )

    def test_cleanup_follows_commit_and_preserves_the_stop_request(
        self, user_dbs, deletable
    ):
        from local_deep_research.web import research_state

        service, target, _ = deletable
        rid = "del-target"
        research_state.set_active_research(rid, {"thread": None})

        def terminate_after_commit(research_id):
            with user_dbs.raw(OWNER) as db:
                assert db.get(ChatSession, target) is None
                assert (
                    db.query(UserActiveResearch)
                    .filter_by(research_id=research_id)
                    .count()
                    == 0
                )
            research_state.set_termination_flag(research_id)

        try:
            with patch(
                f"{SERVICE}.set_termination_flag", terminate_after_commit
            ):
                assert service.delete_session(target) is True
            assert not research_state.is_research_active(rid)
            assert research_state.is_termination_requested(rid)
        finally:
            research_state.cleanup_research(rid)

    def test_cleanup_preserves_a_result_completed_before_the_write(
        self, user_dbs, deletable
    ):
        service, target, _ = deletable
        completed_at = "2026-10-01T12:00:00+00:00"

        def finished_worker(research_id):
            with user_dbs.raw(OWNER) as db:
                history = db.get(ResearchHistory, research_id)
                history.status = ResearchStatus.COMPLETED
                history.completed_at = completed_at
                history.report_content = "Completed answer"
                db.commit()
            return False

        with (
            patch(f"{SERVICE}.is_research_thread_alive", finished_worker),
            patch(f"{SERVICE}.set_termination_flag"),
            patch(f"{SERVICE}.cleanup_research"),
        ):
            assert service.delete_session(target) is True

        with user_dbs.raw(OWNER) as db:
            history = db.get(ResearchHistory, "del-target")
            assert history.status == ResearchStatus.COMPLETED
            assert history.completed_at == completed_at
            assert history.report_content == "Completed answer"

    def test_registration_waits_for_cleanup_and_observes_cancellation(
        self, user_dbs, deletable
    ):
        from local_deep_research.web import research_state

        service, target, _ = deletable
        rid = "del-target"
        attempted = threading.Event()
        observed = threading.Event()
        cancellation = []
        registration = []

        def worker_entry():
            cancellation.append(research_state.is_termination_requested(rid))
            observed.set()

        worker = threading.Thread(target=worker_entry)

        def register():
            attempted.set()
            registration.append(
                research_state.check_and_start_research(
                    rid, {"thread": worker, "settings": {"username": OWNER}}
                )
            )

        registrar = threading.Thread(target=register)

        def crashed_worker(research_id):
            assert research_id == rid
            registrar.start()
            assert attempted.wait(5)
            assert not observed.wait(0.05), (
                "registration must wait while session cleanup holds admission"
            )
            return False

        try:
            with patch(f"{SERVICE}.is_research_thread_alive", crashed_worker):
                assert service.delete_session(target) is True
            registrar.join(5)
            assert not registrar.is_alive()
            assert observed.wait(5)
            worker.join(5)
            assert registration == [True]
            assert cancellation == [True]
        finally:
            if registrar.ident is not None:
                registrar.join(5)
            if worker.ident is not None:
                worker.join(5)
            research_state.cleanup_research(rid)


# ---------------------------------------------------------------------
# 6. Title generation
# ---------------------------------------------------------------------

_TITLE_SNAPSHOT = {
    "chat.llm_title_generation": True,
    "chat.title_llm_timeout_seconds": 5,
}


@pytest.fixture
def titled(user_dbs):
    service = user_dbs.service(OWNER)
    session_id = service.create_session(initial_query="original query")
    return service, session_id


@contextmanager
def _llm_returning(content):
    """Stub the LLM boundary.  Nothing in this module ever reaches a
    provider; ``get_llm`` is imported inside ``_generate_title``, so the
    patch target is the config module."""
    llm = MagicMock()
    llm.invoke.return_value = MagicMock(content=content)
    with patch(
        "local_deep_research.config.llm_config.get_llm", return_value=llm
    ):
        yield llm


class TestTitleGeneration:
    def test_llm_title_is_stored_when_generation_is_enabled(
        self, user_dbs, titled
    ):
        """Positive control for every negative case below."""
        service, session_id = titled
        with _llm_returning("Quantum Error Correction Survey") as llm:
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert result == "Quantum Error Correction Survey"
        assert llm.invoke.call_count == 1
        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(ChatSession).filter_by(id=session_id).first().title
                == "Quantum Error Correction Survey"
            )

    @pytest.mark.parametrize(
        ("reply", "label"),
        [
            ("", "empty"),
            ("   \n\t  ", "whitespace-only"),
            ('""', "empty-quotes"),
        ],
    )
    def test_an_empty_llm_reply_falls_back_to_truncation(
        self, user_dbs, titled, reply, label
    ):
        service, session_id = titled
        with _llm_returning(reply):
            service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        with user_dbs.raw(OWNER) as db:
            title = db.query(ChatSession).filter_by(id=session_id).first().title
        assert title == "original query", label

    def test_a_malformed_llm_response_object_falls_back(self, user_dbs, titled):
        """A provider returning a bare string (no ``.content``) must not
        propagate an AttributeError to the caller."""
        service, session_id = titled
        llm = MagicMock()
        llm.invoke.return_value = "a bare string, not a message"
        with patch(
            "local_deep_research.config.llm_config.get_llm", return_value=llm
        ):
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert result == "original query"

    @pytest.mark.parametrize("length", [101, 10_000, 200_000])
    def test_an_oversized_llm_reply_is_bounded(self, user_dbs, titled, length):
        service, session_id = titled
        with _llm_returning("A" * length):
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert len(result) == 100
        with user_dbs.raw(OWNER) as db:
            stored = (
                db.query(ChatSession).filter_by(id=session_id).first().title
            )
        assert len(stored) == 100

    def test_a_structured_content_block_reply_is_stringified_and_bounded(
        self, user_dbs, titled
    ):
        """Some providers return a list of content blocks; the result is
        an ugly repr but must still be bounded and single-line."""
        service, session_id = titled
        blocks = [{"type": "text", "text": "X" * 500}]
        with _llm_returning(blocks):
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert len(result) == 100
        assert "\n" not in result

    @pytest.mark.parametrize(
        ("reply", "forged"),
        [
            ("Title\nERROR admin deleted everything", "\n"),
            ("Title\rERROR admin deleted everything", "\r"),
            ("Title\r\nERROR admin deleted everything", "\r"),
        ],
    )
    def test_cr_and_lf_are_stripped_from_the_title(
        self, user_dbs, titled, reply, forged
    ):
        service, session_id = titled
        with _llm_returning(reply):
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert forged not in result
        assert result.startswith("Title ")
        with user_dbs.raw(OWNER) as db:
            stored = (
                db.query(ChatSession).filter_by(id=session_id).first().title
            )
        assert "\n" not in stored
        assert "\r" not in stored

    @pytest.mark.parametrize(
        ("separator", "name"),
        [
            ("\u2028", "line-separator"),
            ("\u2029", "paragraph-separator"),
            ("\x85", "next-line"),
            ("\x0b", "vertical-tab"),
            ("\x0c", "form-feed"),
        ],
    )
    def test_all_line_breaking_characters_are_stripped(
        self, titled, separator, name
    ):
        """Generated titles are stored as one display line."""
        service, session_id = titled
        with _llm_returning(f"Title{separator}Second line"):
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert result == "Title Second line", name

    def test_generated_title_does_not_store_control_characters(
        self, user_dbs, titled
    ):
        service, session_id = titled
        with _llm_returning("Title\u2028Second line\x00\x1b[2J"):
            result = service.regenerate_title_with_llm(
                session_id, "original query", _TITLE_SNAPSHOT
            )
        assert "\u2028" not in result
        assert "\x00" not in result
        assert "\x1b" not in result
        assert len(result.splitlines()) == 1
        with user_dbs.raw(OWNER) as db:
            stored = db.get(ChatSession, session_id).title
        assert stored == result
        assert len(stored) <= 100

    def test_a_slow_llm_falls_back_to_truncation_within_the_timeout(
        self, user_dbs, titled
    ):
        """The worker future must not be able to park the caller past the
        configured timeout, and the late answer must not win a race back
        into the database."""
        service, session_id = titled
        hanging_llm = MagicMock()
        started = threading.Event()
        release = threading.Event()

        def _hang(_prompt):
            started.set()
            # Blocks until the assertions are done; the future's timeout
            # is what must return control to the caller meanwhile.
            release.wait(30)
            return MagicMock(content="TOO LATE")

        hanging_llm.invoke.side_effect = _hang
        snapshot = dict(_TITLE_SNAPSHOT)
        snapshot["chat.title_llm_timeout_seconds"] = 0.05

        try:
            with patch(
                "local_deep_research.config.llm_config.get_llm",
                return_value=hanging_llm,
            ):
                result = service.regenerate_title_with_llm(
                    session_id, "original query", snapshot
                )
        finally:
            release.set()

        assert started.is_set(), "the LLM was never actually invoked"
        assert result == "original query"
        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(ChatSession).filter_by(id=session_id).first().title
                == "original query"
            )

    def test_generation_is_skipped_once_the_title_is_no_longer_the_fallback(
        self, user_dbs, titled
    ):
        """Idempotency guard: a user edit (or a sibling tab's generation)
        must not be overwritten, and must not cost an LLM call."""
        service, session_id = titled
        assert service.update_session_title(session_id, "hand written") is True

        with _llm_returning("Machine Written") as llm:
            assert (
                service.regenerate_title_with_llm(
                    session_id, "original query", _TITLE_SNAPSHOT
                )
                is None
            )
            assert llm.invoke.call_count == 0

        with user_dbs.raw(OWNER) as db:
            assert (
                db.query(ChatSession).filter_by(id=session_id).first().title
                == "hand written"
            )

    def test_generation_is_skipped_when_the_setting_is_off(self, titled):
        service, session_id = titled
        with _llm_returning("Machine Written") as llm:
            result = service.regenerate_title_with_llm(
                session_id,
                "original query",
                {"chat.llm_title_generation": False},
            )
            assert llm.invoke.call_count == 0
        assert result == "original query"

    def test_generation_on_a_missing_session_is_a_no_op(self, titled):
        service, _session_id = titled
        with _llm_returning("Machine Written") as llm:
            assert (
                service.regenerate_title_with_llm(
                    "no-such-session", "original query", _TITLE_SNAPSHOT
                )
                is None
            )
            assert llm.invoke.call_count == 0

    def test_a_non_numeric_timeout_setting_still_falls_back(self, titled):
        service, session_id = titled
        snapshot = dict(_TITLE_SNAPSHOT)
        snapshot["chat.title_llm_timeout_seconds"] = "not-a-number"
        with _llm_returning("Machine Written"):
            assert (
                service.regenerate_title_with_llm(
                    session_id, "original query", snapshot
                )
                == "original query"
            )

    @pytest.mark.parametrize(
        "timeout", ["not-a-number", "nan", "inf", None, 0, -1]
    )
    def test_invalid_timeout_does_not_construct_a_provider(self, timeout):
        service = ChatService(OWNER)
        snapshot = dict(_TITLE_SNAPSHOT)
        snapshot["chat.title_llm_timeout_seconds"] = timeout
        with patch("local_deep_research.config.llm_config.get_llm") as factory:
            assert (
                service._generate_title("original query", snapshot)
                == "original query"
            )
        factory.assert_not_called()

    @pytest.mark.parametrize("source", ["value", "environment"])
    def test_typed_timeout_preserves_safe_diagnostic(self, monkeypatch, source):
        canary = "synthetic-timeout-secret"
        monkeypatch.delenv("LDR_CHAT_TITLE_LLM_TIMEOUT_SECONDS", raising=False)
        value = canary
        if source == "environment":
            monkeypatch.setenv("LDR_CHAT_TITLE_LLM_TIMEOUT_SECONDS", canary)
            value = 5
        self._assert_timeout_diagnostic(
            {"value": value, "ui_element": "number"},
            f"from {source}: expected a valid number value",
            canary,
        )

    @pytest.mark.parametrize(
        "timeout",
        [
            "synthetic-timeout-secret",
            None,
            {},
            10**1000,
            "nan",
            "inf",
            float("-inf"),
            0,
            -1,
        ],
        ids=[
            "value-error",
            "none",
            "type-error",
            "overflow",
            "nan",
            "inf",
            "negative-inf",
            "zero",
            "negative",
        ],
    )
    def test_raw_timeout_has_safe_diagnostic(self, timeout):
        self._assert_timeout_diagnostic(
            timeout,
            "expected a finite positive number",
            "synthetic-timeout-secret",
        )

    @staticmethod
    def _assert_timeout_diagnostic(timeout, reason, canary):
        from loguru import logger

        records = []
        logger.enable("local_deep_research")
        sink = logger.add(lambda message: records.append(message.record))
        query = "long research query " * 20
        try:
            with (
                patch(
                    "local_deep_research.config.llm_config.get_llm"
                ) as provider,
                patch(
                    "local_deep_research.chat.service.ThreadPoolExecutor"
                ) as worker,
            ):
                result = ChatService(OWNER)._generate_title(
                    query,
                    {
                        **_TITLE_SNAPSHOT,
                        "chat.title_llm_timeout_seconds": timeout,
                    },
                )
            provider.assert_not_called()
            worker.assert_not_called()
        finally:
            logger.remove(sink)
            logger.disable("local_deep_research")
        assert result == query[:97] + "..."
        warnings = [r for r in records if r["level"].name == "WARNING"]
        assert len(warnings) == 1
        record = warnings[0]
        assert "chat.title_llm_timeout_seconds" in record["message"]
        assert reason in record["message"]
        assert "falling back to truncation" in record["message"]
        assert canary not in str(records)
        assert record["exception"] is None
        assert record["extra"] == {}
        assert "could not convert" not in record["message"]

    @pytest.mark.parametrize("timeout", [5, "2.5", "missing"])
    def test_timeout_controls_use_controlled_provider(self, timeout):
        snapshot = dict(_TITLE_SNAPSHOT)
        if timeout == "missing":
            del snapshot["chat.title_llm_timeout_seconds"]
        else:
            snapshot["chat.title_llm_timeout_seconds"] = timeout
        with _llm_returning("Controlled title") as llm:
            assert (
                ChatService(OWNER)._generate_title("query", snapshot)
                == "Controlled title"
            )
        llm.invoke.assert_called_once()

    def test_disabled_title_ignores_invalid_timeout(self):
        with (
            patch("local_deep_research.config.llm_config.get_llm") as provider,
            patch(
                "local_deep_research.chat.service.ThreadPoolExecutor"
            ) as worker,
        ):
            assert (
                ChatService(OWNER)._generate_title(
                    "query",
                    {
                        "chat.llm_title_generation": False,
                        "chat.title_llm_timeout_seconds": {
                            "value": "synthetic-timeout-secret",
                            "ui_element": "number",
                        },
                    },
                )
                == "query"
            )
        provider.assert_not_called()
        worker.assert_not_called()

    def test_numeric_string_timeout_and_visible_unicode_are_preserved(
        self, titled
    ):
        service, session_id = titled
        snapshot = dict(_TITLE_SNAPSHOT)
        snapshot["chat.title_llm_timeout_seconds"] = "2.5"
        with _llm_returning("Résumé 東京 😀"):
            assert (
                service.regenerate_title_with_llm(
                    session_id, "original query", snapshot
                )
                == "Résumé 東京 😀"
            )

    def test_the_creation_path_never_calls_the_llm(self, user_dbs):
        """``create_session`` must stay synchronous-and-cheap; LLM titling
        is a separate endpoint."""
        service = user_dbs.service(OWNER)
        with _llm_returning("Machine Written") as llm:
            session_id = service.create_session(
                initial_query="A" * 400, settings_snapshot=_TITLE_SNAPSHOT
            )
            assert llm.invoke.call_count == 0
        title = service.get_session(session_id)["title"]
        assert len(title) == 100
        assert title.endswith("...")