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.

7.9 KB
Raw Blame
"""Tests for a non-dict persisted ``accumulated_context`` (issue #7674).

``ChatSession.accumulated_context`` is a plain JSON column that the research
trigger paths pass to ``ChatContextManager`` unvalidated. Writers always store
a dict, but corruption or a legacy writer can leave a truthy non-dict such as
``7``, ``"abc"`` or ``[1]``. The old ``accumulated_context or {}`` guard only
replaced falsy values, so a truthy non-dict reached
``self.accumulated_context.get(...)`` and raised ``AttributeError`` -> HTTP 500
on every follow-up message in that session.

Coverage:

- Unit: construction with non-dict values builds a usable manager and the
  entity/topic getters return their defaults, including when the stored
  ``key_entities`` / ``topics`` values are themselves not lists.
- HTTP: a session row corrupted in the database yields a clean follow-up turn
  from both research-trigger routes (``send_message`` and ``retry_attempt``)
  instead of a 500.

The write path (``ChatService.update_accumulated_context``) is covered in
``test_chat_service.py``.
"""

import json
from unittest.mock import patch

import pytest

from local_deep_research.chat.context import ChatContextManager
from local_deep_research.database.models import ChatSession
from local_deep_research.database.session_context import get_user_db_session

_SPAWN = "local_deep_research.web.routers.chat.start_research_process"

# Truthy non-dicts are the bug; None / falsy values already worked and must
# keep working.
NON_DICT_VALUES = [
    pytest.param(7, id="int"),
    pytest.param("abc", id="str"),
    pytest.param([1], id="list"),
    pytest.param(None, id="none"),
    pytest.param(0, id="falsy-int"),
    pytest.param("", id="empty-str"),
    pytest.param([], id="empty-list"),
]

# Two differently shaped corrupt values are enough for the HTTP tests; the
# unit tests above cover the full value matrix.
CORRUPT_VALUES = [
    pytest.param(7, id="int"),
    pytest.param([1], id="list"),
]


class TestChatContextManagerNonDictAccumulatedContext:
    """Construction must tolerate any non-dict ``accumulated_context``."""

    @pytest.mark.parametrize("bad_value", NON_DICT_VALUES)
    def test_non_dict_is_treated_as_empty(self, bad_value):
        manager = ChatContextManager("test-session", [], bad_value)

        assert manager.accumulated_context == {}
        assert manager._get_key_entities() == []
        assert manager._get_topics() == []

    @pytest.mark.parametrize("bad_value", NON_DICT_VALUES)
    def test_build_research_context_does_not_raise(self, bad_value):
        manager = ChatContextManager("test-session", [], bad_value)

        context = manager.build_research_context()

        assert context["session_id"] == "test-session"
        assert context["key_entities"] == []
        assert context["topics"] == []
        assert context["is_multi_turn"] is False

    def test_valid_dict_is_still_honoured(self):
        accumulated = {
            "key_entities": ["entity1", "entity2"],
            "topics": ["topic1"],
        }

        manager = ChatContextManager("test-session", [], accumulated)

        assert manager._get_key_entities() == ["entity1", "entity2"]
        assert manager._get_topics() == ["topic1"]

    def test_empty_dict_is_still_accepted(self):
        manager = ChatContextManager("test-session", [], {})

        assert manager.accumulated_context == {}
        assert manager._get_key_entities() == []
        assert manager._get_topics() == []

    @pytest.mark.parametrize("bad_value", [7, "abc", {"a": 1}])
    def test_non_list_entities_and_topics_are_treated_as_empty(self, bad_value):
        """A dict whose own values are the wrong type must not crash either."""
        manager = ChatContextManager(
            "test-session",
            [],
            {"key_entities": bad_value, "topics": bad_value},
        )

        assert manager._get_key_entities() == []
        assert manager._get_topics() == []
        assert manager.build_research_context()["key_entities"] == []


# ---------------------------------------------------------------------------
# HTTP-level: a corrupted session row must not 500 the follow-up turn.
# ---------------------------------------------------------------------------


def _create_session(client, query="Test"):
    resp = client.post(
        "/api/chat/sessions",
        json={"initial_query": query},
        content_type="application/json",
    )
    assert resp.status_code == 200, resp.data
    return json.loads(resp.data)["session_id"]


def _username(client) -> str:
    resp = client.get("/auth/check")
    assert resp.status_code == 200, resp.text
    return resp.json()["username"]


def _corrupt_accumulated_context(username, session_id, value):
    """Overwrite the persisted ``accumulated_context`` with ``value``."""
    with get_user_db_session(username) as db:
        session = db.query(ChatSession).filter_by(id=session_id).first()
        assert session is not None
        session.accumulated_context = value
        db.commit()


class TestCorruptedAccumulatedContextRoutes:
    """Both routes that build a ``ChatContextManager`` from the session row."""

    @pytest.mark.parametrize("bad_value", CORRUPT_VALUES)
    def test_send_message_with_corrupted_context_is_not_a_500(
        self, authenticated_client, bad_value
    ):
        session_id = _create_session(authenticated_client)
        _corrupt_accumulated_context(
            _username(authenticated_client), session_id, bad_value
        )

        with patch(_SPAWN) as mock_spawn:
            response = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={
                    "content": "follow-up question",
                    "trigger_research": True,
                },
                content_type="application/json",
            )

        assert response.status_code == 200, response.data
        body = json.loads(response.data)
        assert body["success"] is True
        assert body["research_id"] is not None
        # The request got past context building all the way to the spawn.
        mock_spawn.assert_called_once()

    @pytest.mark.parametrize("bad_value", CORRUPT_VALUES)
    def test_retry_attempt_with_corrupted_context_is_not_a_500(
        self, authenticated_client, bad_value
    ):
        session_id = _create_session(authenticated_client)

        # Seed one attempt while the context is still healthy.
        with patch(_SPAWN):
            send_resp = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/messages",
                json={"content": "what is X?", "trigger_research": True},
                content_type="application/json",
            )
        assert send_resp.status_code == 200, send_resp.data
        old_research_id = json.loads(send_resp.data)["research_id"]

        _corrupt_accumulated_context(
            _username(authenticated_client), session_id, bad_value
        )

        # Same short-circuits as the happy-path retry test: skip the
        # stale-row grace window and treat the just-seeded research as dead
        # so delete_attempt reclaims it, leaving only the context-building
        # path under test.
        with (
            patch(
                "local_deep_research.web.routers.chat."
                "_enforce_chat_session_research_slot",
                return_value=None,
            ),
            patch(
                "local_deep_research.chat.service.is_research_thread_alive",
                return_value=False,
            ),
            patch(_SPAWN) as mock_spawn,
        ):
            retry_resp = authenticated_client.post(
                f"/api/chat/sessions/{session_id}/attempts/"
                f"{old_research_id}/retry"
            )

        assert retry_resp.status_code == 200, retry_resp.data
        body = json.loads(retry_resp.data)
        assert body["success"] is True
        assert body["research_id"] != old_research_id
        mock_spawn.assert_called_once()