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.

14.0 KB
Raw Blame
"""Registry accessor properties that ``test_research_state_registry.py`` leaves open.

Ported from the two Flask-era files the migration deleted,
``tests/web/routes/test_globals_extended.py`` and
``tests/web/routes/test_globals_gaps.py`` (65 test functions between them),
against ``web/research_state.py`` — the module ``web/routes/globals.py``
became.

The bulk of those two files IS superseded by
``tests/web/test_research_state_registry.py``, which is stronger almost
everywhere. What is recovered here is the residue it does not pin:

* **Iteration must not hold the lock while yielding.** The registry test
  mutates the registry from *inside* its own ``for`` loop — but ``_lock``
  is an ``RLock``, so a same-thread mutation succeeds whether or not the
  lock is held across the yields. Only a *second thread* distinguishes
  the two, which is what the deleted ``test_does_not_hold_lock_during_
  iteration`` used and what is restored below.
* **Snapshot-ness of ``get_active_research_ids``** — the registry test
  only asserts membership, so returning the live ``dict.keys()`` view
  would still pass it (and would then raise ``RuntimeError: dictionary
  changed size`` in any caller that mutates while iterating).
* **``append_research_log``'s ``setdefault`` branch** — every entry the
  registry test builds already carries a ``log`` list, so the
  create-the-list-if-missing half never executes there.
* **``clear_termination_flag``** — pinned in the registry file only as
  *having two deliberate benchmark-task call sites*; nothing asserts it
  actually clears.
* The remaining ``.get(..., default)`` fallbacks on entries built without
  the canonical keys (``settings``/``thread``/``progress`` absent), and
  ``get_active_research_count``'s exact count.

Everything here runs on the module in isolation: two dicts, an RLock and
real threads. No app boot, no database.
"""

from __future__ import annotations

import threading
import uuid

import pytest

from local_deep_research.web import research_state


@pytest.fixture(autouse=True)
def _pristine_registry():
    """Snapshot and restore the process-global dicts around every test."""
    with research_state._lock:
        active = dict(research_state._active_research)
        flags = dict(research_state._termination_flags)
    try:
        yield
    finally:
        with research_state._lock:
            research_state._active_research.clear()
            research_state._active_research.update(active)
            research_state._termination_flags.clear()
            research_state._termination_flags.update(flags)


def _rid():
    return str(uuid.uuid4())


# ===================================================================
# get_active_research_ids / get_active_research_count
# ===================================================================


def test_get_active_research_ids_returns_a_snapshot_not_a_live_view():
    """The returned list must not track later mutations.

    ``list(_active_research.keys())`` vs. a bare ``.keys()`` view is
    output-identical to a membership assertion, so the registry file's
    ``research_id in get_active_research_ids()`` would not notice the
    difference. A live view raises ``RuntimeError`` in any caller that
    registers a research while walking it.
    """
    first = _rid()
    research_state.set_active_research(first, {})

    ids = research_state.get_active_research_ids()
    assert isinstance(ids, list)
    assert first in ids

    second = _rid()
    research_state.set_active_research(second, {})

    assert second not in ids, (
        "get_active_research_ids handed out a live view of the registry"
    )


def test_get_active_research_count_tracks_registrations_and_removals():
    baseline = research_state.get_active_research_count()

    first, second = _rid(), _rid()
    research_state.set_active_research(first, {})
    research_state.set_active_research(second, {})
    assert research_state.get_active_research_count() == baseline + 2

    research_state.remove_active_research(first)
    assert research_state.get_active_research_count() == baseline + 1

    # Removing an id that was never registered is a no-op, not an error.
    research_state.remove_active_research(_rid())
    assert research_state.get_active_research_count() == baseline + 1


# ===================================================================
# set_active_research / append_research_log
# ===================================================================


def test_set_active_research_replaces_the_entry_wholesale():
    research_id = _rid()
    research_state.set_active_research(research_id, {"progress": 10})
    research_state.set_active_research(research_id, {"progress": 20})

    assert research_state.get_research_field(research_id, "progress") == 20


def test_append_research_log_creates_the_log_list_when_it_is_missing():
    """The ``setdefault("log", [])`` half of the appender.

    Every entry the registry test builds already has a ``log`` list, so
    that file only ever exercises the append side. An entry registered
    without one — which ``set_active_research`` permits — must still
    accumulate logs rather than dropping them or raising ``KeyError``.
    """
    research_id = _rid()
    research_state.set_active_research(research_id, {})

    research_state.append_research_log(research_id, {"time": "t1"})
    research_state.append_research_log(research_id, {"time": "t2"})

    assert research_state.get_research_field(research_id, "log") == [
        {"time": "t1"},
        {"time": "t2"},
    ]


# ===================================================================
# termination flags
# ===================================================================


def test_clear_termination_flag_actually_clears_and_is_per_id():
    """``clear_termination_flag`` has two deliberate benchmark-task callers,
    and the registry file pins only that fact — not that the function
    works. It is the obvious thing for a future patch to reach for, so
    what it does is worth an assertion."""
    doomed, bystander = _rid(), _rid()

    assert research_state.is_termination_requested(doomed) is False

    research_state.set_termination_flag(doomed)
    research_state.set_termination_flag(bystander)
    assert research_state.is_termination_requested(doomed) is True

    research_state.clear_termination_flag(doomed)
    assert research_state.is_termination_requested(doomed) is False
    assert research_state.is_termination_requested(bystander) is True

    # Clearing a flag that was never set must not raise.
    research_state.clear_termination_flag(_rid())


# ===================================================================
# default-branch fallbacks on non-canonical entries
# ===================================================================


def test_is_research_thread_alive_treats_an_explicit_none_thread_as_dead():
    """``{"thread": None}`` is a distinct branch from "no thread key" and
    from "dead thread" — a naive ``entry["thread"].is_alive()`` would
    raise ``AttributeError`` here rather than answering False."""
    no_key, none_thread = _rid(), _rid()
    research_state.set_active_research(no_key, {"progress": 0})
    research_state.set_active_research(none_thread, {"thread": None})

    assert research_state.is_research_thread_alive(no_key) is False
    assert research_state.is_research_thread_alive(none_thread) is False


def test_get_usernames_ignores_an_entry_with_no_settings_key_at_all():
    """``entry.get("settings", {})`` — an entry registered without a
    ``settings`` key contributes no username instead of raising. The
    registry file's version supplies ``settings={}``, which exercises the
    key-present branch only."""
    research_state.set_active_research(_rid(), {"progress": 0})

    # No raise, and nothing spurious contributed.
    names = research_state.get_usernames_with_active_research()
    assert None not in names
    assert "" not in names


def test_update_progress_and_check_active_defaults_a_missing_progress_to_zero():
    """``entry.get("progress", 0)`` — an entry with no ``progress`` key
    must compare against 0, so the first update is accepted."""
    research_id = _rid()
    research_state.set_active_research(research_id, {})

    assert research_state.update_progress_and_check_active(research_id, 10) == (
        10,
        True,
    )
    assert research_state.get_research_field(research_id, "progress") == 10
    # Equal is not higher: the comparison is strict `>`.
    assert research_state.update_progress_and_check_active(research_id, 10) == (
        10,
        True,
    )


def test_check_and_start_research_replaces_an_entry_that_has_no_thread():
    """``existing is not None and existing.is_alive()`` — the
    ``existing is None`` half. A stale entry left behind without a
    ``thread`` key must not block a fresh start (the registry file only
    covers live-thread and dead-thread incumbents)."""
    research_id = _rid()
    research_state.set_active_research(research_id, {"progress": 50})

    started = threading.Event()
    thread = threading.Thread(target=started.set, daemon=True)
    data = {
        "thread": thread,
        "progress": 0,
        "status": "in_progress",
        "log": [],
        "settings": {},
    }
    try:
        assert (
            research_state.check_and_start_research(research_id, data) is True
        )
        assert started.wait(timeout=10)
        assert (
            research_state.get_research_field(research_id, "thread") is thread
        )
    finally:
        thread.join(timeout=10)


# ===================================================================
# iter_active_research
# ===================================================================


def test_iteration_does_not_hold_the_lock_while_yielding():
    """A *second thread* must be able to write mid-iteration.

    ``_lock`` is an ``RLock``, so the successor test's same-thread
    mutation inside its own ``for`` body succeeds whether or not the lock
    is held across the yields — it cannot distinguish the two
    implementations. Only another thread can, and that is the property
    ``iter_active_research`` was written for: build the snapshot list
    under the lock, then yield outside it.
    """
    research_state.set_active_research(_rid(), {"progress": 10})
    research_state.set_active_research(_rid(), {"progress": 20})

    write_succeeded = threading.Event()

    def writer():
        research_state.set_active_research(_rid(), {"progress": 30})
        write_succeeded.set()

    for _, _ in research_state.iter_active_research():
        thread = threading.Thread(target=writer, daemon=True)
        thread.start()
        thread.join(timeout=2)
        break

    assert write_succeeded.is_set(), (
        "a second thread was blocked while iter_active_research was yielding; "
        "the lock is being held across the yields"
    )


def test_iter_active_research_yields_copies_callers_cannot_write_back_through():
    research_id = _rid()
    research_state.set_active_research(research_id, {"progress": 10})

    for rid, data in research_state.iter_active_research():
        if rid == research_id:
            data["progress"] = 999

    assert research_state.get_research_field(research_id, "progress") == 10


# ===================================================================
# Thread safety
# ===================================================================


def test_concurrent_add_check_remove_cycles_do_not_raise():
    """Five threads churning disjoint ids through the full lifecycle."""
    errors: list[Exception] = []

    def worker(index):
        try:
            for _ in range(100):
                research_id = f"churn-{index}"
                research_state.set_active_research(research_id, {"progress": 0})
                research_state.is_research_active(research_id)
                research_state.remove_active_research(research_id)
        except Exception as exc:  # noqa: BLE001 — recorded, then re-asserted
            errors.append(exc)

    threads = [
        threading.Thread(target=worker, args=(index,), daemon=True)
        for index in range(5)
    ]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join(timeout=20)
        assert not thread.is_alive()

    assert errors == []


def test_concurrent_cleanup_of_distinct_ids_empties_both_dicts():
    ids = [_rid() for _ in range(20)]
    for index, research_id in enumerate(ids):
        research_state.set_active_research(research_id, {"progress": index})
        research_state.set_termination_flag(research_id)

    errors: list[Exception] = []

    def cleaner(research_id):
        try:
            research_state.cleanup_research(research_id)
        except Exception as exc:  # noqa: BLE001 — recorded, then re-asserted
            errors.append(exc)

    threads = [
        threading.Thread(target=cleaner, args=(research_id,), daemon=True)
        for research_id in ids
    ]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join(timeout=20)
        assert not thread.is_alive()

    assert errors == []
    for research_id in ids:
        assert research_state.is_research_active(research_id) is False
        assert research_state.is_termination_requested(research_id) is False


def test_concurrent_progress_updates_settle_on_the_maximum():
    """Ten threads racing ``update_progress_if_higher`` on one id.

    Without the lock the read-compare-write drops updates and the final
    value is not necessarily the maximum offered.
    """
    research_id = _rid()
    research_state.set_active_research(research_id, {"progress": 0})
    barrier = threading.Barrier(10)

    def updater(value):
        barrier.wait(timeout=10)
        research_state.update_progress_if_higher(research_id, value)

    threads = [
        threading.Thread(target=updater, args=(index * 10,), daemon=True)
        for index in range(10)
    ]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join(timeout=20)
        assert not thread.is_alive()

    assert research_state.get_research_field(research_id, "progress") == 90