~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.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""The process-global research registry: who touches it, and under what gate.
``web/research_state.py`` is the one piece of mutable state in this
application that is *not* per-user. Everything else about the isolation
model is a per-user encrypted SQLCipher database keyed by username; this
module is a pair of plain module-level dicts keyed by ``research_id`` and
shared by every user in the process::
_active_research: dict[research_id -> {thread, progress, status, log,
settings}]
_termination_flags: dict[research_id -> bool]
Nothing in the registry records an owner (``settings["username"]`` is
carried along, but no accessor consults it). So the security property is
entirely extrinsic: *every* call site must already have proven ownership
of the id before it reaches an accessor. This file pins that claim down
in both directions.
What is asserted here
---------------------
1. **The sweep** — an AST census of the whole installed package that
enumerates every call to every accessor, plus a check that no module
outside ``research_state.py`` / its ``routes/globals.py`` re-export
shim touches the private dicts or the lock directly. Both carry a
floor and a list of named sites, so an analyzer that silently resolves
nothing fails instead of passing empty.
2. **The ordering property** — for each handler that reaches the
registry with a caller-supplied id, the owner-scoped
``get_user_db_session`` lookup must come first.
:func:`test_cancel_research_does_the_owner_lookup_before_the_global_registry`
pins that ordering for
``web/services/research_service.cancel_research`` -- which used to act
first and check afterwards -- and
:func:`test_cancel_research_refuses_a_foreign_active_research` proves
the fixed behaviour by execution.
3. **Unguessability** — every research id in the codebase is
``str(uuid.uuid4())``. This matters because it is the second half of
the defence: where the ownership check is missing or late, an
unguessable id is the only thing left.
4. **Lifecycle** — leaks (a flag set for an id that never registers is
never reclaimed), resurrection (a re-used id starts pre-terminated),
and collision (a live id refuses a second thread, a dead one is
replaced).
5. **Thread-safety** — that ``thread.start()`` really does happen inside
the registry lock, so a worker's first write cannot be dropped, and
that the per-user start gate is taken *before* the global lock.
Everything except the ``cancel_research`` execution proof runs on the
module in isolation: two dicts, an RLock and real ``threading.Thread``s.
No app boot, no database, no wall-clock sleeps -- ordering is expressed
with Events and Barriers.
The package under analysis is located from ``research_state.__file__``,
so pointing ``PYTHONPATH`` at a mutated copy of ``local_deep_research``
re-runs the static half against that copy (how the negative controls for
this file were exercised).
"""
from __future__ import annotations
import ast
import functools
import threading
import uuid
from pathlib import Path
import pytest
from local_deep_research.web import research_state
PKG_ROOT = Path(research_state.__file__).resolve().parents[1]
#: The module that owns the state, and the backwards-compat shim that
#: re-exports it. Every other module must go through the accessors.
STATE_OWNERS = frozenset(
{
PKG_ROOT / "web" / "research_state.py",
PKG_ROOT / "web" / "routes" / "globals.py",
}
)
#: Every public accessor exported by the registry.
ACCESSORS = frozenset(
{
"is_research_active",
"get_active_research_ids",
"get_active_research_snapshot",
"get_research_field",
"set_active_research",
"check_and_start_research",
"update_active_research",
"append_research_log",
"update_progress_if_higher",
"remove_active_research",
"iter_active_research",
"get_active_research_count",
"get_usernames_with_active_research",
"is_termination_requested",
"set_termination_flag",
"clear_termination_flag",
"is_research_thread_alive",
"update_progress_and_check_active",
"cleanup_research",
"reclaim_stale_user_active_research",
"user_research_start_gate",
}
)
#: The private state. Touching these from outside ``STATE_OWNERS`` means
#: bypassing the lock.
PRIVATE_STATE = frozenset({"_active_research", "_termination_flags", "_lock"})
#: The single function that turns a username into an open per-user
#: encrypted database -- i.e. the ownership chokepoint.
DB_CHOKEPOINT = "get_user_db_session"
# ---------------------------------------------------------------------------
# AST census helpers
# ---------------------------------------------------------------------------
class _CallSite:
__slots__ = ("module", "qualname", "accessor", "lineno")
def __init__(self, module, qualname, accessor, lineno):
self.module = module
self.qualname = qualname
self.accessor = accessor
self.lineno = lineno
@property
def key(self):
return (self.module, self.qualname, self.accessor)
def __repr__(self):
return f"{self.module}:{self.lineno} {self.qualname}() -> {self.accessor}()"
def _called_name(node: ast.Call) -> str | None:
func = node.func
if isinstance(func, ast.Name):
return func.id
if isinstance(func, ast.Attribute):
return func.attr
return None
class _Collector(ast.NodeVisitor):
"""Collect accessor calls, tagged with their enclosing def chain."""
def __init__(self, module: str, wanted: frozenset[str]):
self.module = module
self.wanted = wanted
self.stack: list[str] = []
self.sites: list[_CallSite] = []
def visit_FunctionDef(self, node):
self.stack.append(node.name)
self.generic_visit(node)
self.stack.pop()
visit_AsyncFunctionDef = visit_FunctionDef
def visit_Call(self, node):
name = _called_name(node)
if name in self.wanted:
self.sites.append(
_CallSite(
self.module,
"::".join(self.stack) if self.stack else "<module>",
name,
node.lineno,
)
)
self.generic_visit(node)
@functools.lru_cache(maxsize=1)
def _census() -> tuple[tuple[_CallSite, ...], int]:
"""Every accessor call outside the state-owning modules.
Returns ``(sites, modules_scanned)``. ``modules_scanned`` exists so a
caller can assert the walk actually walked something.
"""
sites: list[_CallSite] = []
scanned = 0
for path in sorted(PKG_ROOT.rglob("*.py")):
if path in STATE_OWNERS:
continue
scanned += 1
collector = _Collector(str(path.relative_to(PKG_ROOT)), ACCESSORS)
collector.visit(ast.parse(path.read_text(encoding="utf-8")))
sites.extend(collector.sites)
return tuple(sites), scanned
def _private_state_reads(path: Path) -> list[tuple[str, int]]:
"""Names in ``PRIVATE_STATE`` referenced in *path*'s executable code."""
tree = ast.parse(path.read_text(encoding="utf-8"))
hits: list[tuple[str, int]] = []
for node in ast.walk(tree):
if isinstance(node, ast.Name) and node.id in PRIVATE_STATE:
hits.append((node.id, node.lineno))
elif isinstance(node, ast.Attribute) and node.attr in PRIVATE_STATE:
hits.append((node.attr, node.lineno))
return hits
@functools.lru_cache(maxsize=1)
def _function_bodies() -> dict[tuple[str, str], ast.AST]:
"""``(module_relpath, qualname) -> ast node`` for every def in the package."""
out: dict[tuple[str, str], ast.AST] = {}
for path in sorted(PKG_ROOT.rglob("*.py")):
module = str(path.relative_to(PKG_ROOT))
tree = ast.parse(path.read_text(encoding="utf-8"))
stack: list[str] = []
def walk(node):
for child in ast.iter_child_nodes(node):
if isinstance(child, (ast.FunctionDef, ast.AsyncFunctionDef)):
stack.append(child.name)
out[(module, "::".join(stack))] = child
walk(child)
stack.pop()
else:
walk(child)
walk(tree)
return out
def _first_lineno_of_call(node: ast.AST, names: frozenset[str]) -> int | None:
linenos = [
call.lineno
for call in ast.walk(node)
if isinstance(call, ast.Call) and _called_name(call) in names
]
return min(linenos) if linenos else None
# ---------------------------------------------------------------------------
# Registry isolation for the behavioural tests
# ---------------------------------------------------------------------------
@pytest.fixture(autouse=True)
def _pristine_registry():
"""Snapshot and restore the process-global dicts around every test.
The registry is module state; without this a failing test would leak
entries into every later test in the same worker.
"""
with research_state._lock:
active = dict(research_state._active_research)
flags = dict(research_state._termination_flags)
gates = dict(research_state._user_research_start_gates)
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)
with research_state._user_research_start_gates_lock:
research_state._user_research_start_gates.clear()
research_state._user_research_start_gates.update(gates)
@pytest.fixture
def parked_threads():
"""Real threads that block until released, then are joined.
Gives a genuinely ``is_alive()`` thread without a sleep.
"""
release = threading.Event()
started: list[threading.Thread] = []
def make(target=None):
thread = threading.Thread(target=target or release.wait, daemon=True)
started.append(thread)
return thread
make.release = release
try:
yield make
finally:
release.set()
for thread in started:
if thread.ident is not None:
thread.join(timeout=10)
def _entry(thread, username="owner", **extra):
data = {
"thread": thread,
"progress": 0,
"status": "in_progress",
"log": [],
"settings": {"username": username},
}
data.update(extra)
return data
def _rid():
return str(uuid.uuid4())
# ===========================================================================
# 1. The sweep
# ===========================================================================
def test_the_census_enumerates_every_known_registry_call_site():
"""Floor + named sites, so an analyzer that resolves nothing fails.
A sweep that finds zero call sites and passes is the classic
fail-open. The named sites below are the full inventory of handlers
and workers that reach the registry as of this commit; the floor
guards against the walk breaking wholesale.
"""
sites, scanned = _census()
assert scanned > 300, (
f"only {scanned} package modules were parsed -- the AST walk is "
"not reaching the package"
)
assert len(sites) >= 40, (
f"only {len(sites)} accessor calls found; expected the full "
"inventory (44 at the time of writing)"
)
found = {site.key for site in sites}
must_find = {
# --- HTTP handlers reached with a caller-supplied research_id ---
(
"web/routers/history.py",
"get_research_status",
"get_active_research_snapshot",
),
(
"web/routers/history.py",
"get_research_details",
"get_active_research_snapshot",
),
("web/routers/research.py", "terminate_research", "is_research_active"),
(
"web/routers/research.py",
"terminate_research",
"set_termination_flag",
),
("web/routers/research.py", "terminate_research", "get_research_field"),
(
"web/routers/research.py",
"terminate_research",
"append_research_log",
),
(
"web/routers/research.py",
"clear_history::clear_once",
"get_active_research_ids",
),
(
"web/routers/chat.py",
"_check_chat_research_slot_locked",
"is_research_thread_alive",
),
(
"web/routers/chat.py",
"_check_chat_research_slot_locked",
"cleanup_research",
),
(
"web/routers/followup.py",
"_start_followup_sync",
"reclaim_stale_user_active_research",
),
(
"web/routers/research.py",
"_start_research_sync",
"reclaim_stale_user_active_research",
),
# --- Socket.IO ---
(
"web/services/socketio_asgi.py",
"on_subscribe",
"get_active_research_snapshot",
),
# --- the service layer, which the router-only census cannot see ---
(
"web/services/research_service.py",
"cancel_research",
"set_termination_flag",
),
(
"web/services/research_service.py",
"cancel_research",
"is_research_active",
),
(
"web/services/research_service.py",
"start_research_process",
"check_and_start_research",
),
(
"web/services/research_service.py",
"cleanup_research_resources",
"cleanup_research",
),
("chat/service.py", "delete_session", "set_termination_flag"),
("chat/service.py", "delete_attempt_rows", "cleanup_research"),
# --- background / server-side, ids never come from a request ---
(
"web/queue/processor_v2.py",
"_reclaim_stranded_queue_rows",
"is_research_active",
),
(
"web/queue/processor_v2.py",
"reconcile_orphan_active_research",
"cleanup_research",
),
(
"web/auth/connection_cleanup.py",
"cleanup_idle_connections",
"get_usernames_with_active_research",
),
("web/routers/auth.py", "logout", "user_research_start_gate"),
("web/routers/auth.py", "change_password", "user_research_start_gate"),
}
missing = must_find - found
assert not missing, (
"the census failed to find call sites that are present in the "
f"source: {sorted(missing)}"
)
def test_no_module_outside_the_registry_touches_the_private_dicts():
"""The lock is only useful if nothing bypasses the accessors.
``_active_research`` / ``_termination_flags`` / ``_lock`` are
re-exported by ``web/routes/globals.py`` for backwards compatibility,
so an unlocked ``_active_research[rid] = ...`` anywhere in the
package is one import away and would be invisible to the accessor
census above.
"""
# Positive control: the detector must fire on the owning module,
# otherwise "no offenders" means "the detector is broken".
owner_hits = _private_state_reads(PKG_ROOT / "web" / "research_state.py")
assert {name for name, _ in owner_hits} == PRIVATE_STATE, (
"the private-state detector does not even see the state in its own "
f"module (saw {sorted({n for n, _ in owner_hits})})"
)
offenders = []
scanned = 0
for path in sorted(PKG_ROOT.rglob("*.py")):
if path in STATE_OWNERS:
continue
scanned += 1
for name, lineno in _private_state_reads(path):
# ``self._lock`` is a common attribute name on unrelated
# classes (session_manager, llm_registry, ...); only the
# module-global reads matter here.
if name == "_lock":
continue
offenders.append(
f"{path.relative_to(PKG_ROOT)}:{lineno} touches {name}"
)
assert scanned > 300, "the walk did not reach the package"
assert not offenders, (
"modules bypass the registry accessors and mutate the shared "
f"dicts without the lock: {offenders}"
)
def test_research_ids_are_unguessable_uuid4_everywhere_they_are_minted():
"""The id is the second half of the access-control story.
Nothing in the registry checks ownership; a handler that forgets (or
defers) the database lookup is protected only by the id being
unguessable. If ids were sequential, every such handler would be a
direct cross-user control.
"""
#: Every module that MINTS a research id (as opposed to reading one
#: off a request, a row or a log record). Asserted by name so a new
#: minting site has to be added here deliberately.
minting_modules = (
"web/routers/research.py",
"web/routers/chat.py",
"web/routers/followup.py",
"scheduler/background.py",
)
assigned: dict[str, set[str]] = {}
for path in sorted(PKG_ROOT.rglob("*.py")):
module = str(path.relative_to(PKG_ROOT))
tree = ast.parse(path.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if not isinstance(node, ast.Assign):
continue
targets = {t.id for t in node.targets if isinstance(t, ast.Name)}
if not targets & {"research_id", "new_research_id"}:
continue
assigned.setdefault(module, set()).add(ast.unparse(node.value))
# 1. Each minting site mints with uuid4 and nothing else.
for module in minting_modules:
expressions = assigned.get(module, set())
assert expressions, (
f"{module} no longer assigns a research id -- the census is "
"looking at the wrong thing"
)
fresh = {
expression
for expression in expressions
if "uuid" in expression or "random" in expression
}
assert fresh == {"str(uuid.uuid4())"}, (
f"{module} mints a research id with something other than "
f"uuid4: {sorted(fresh)}"
)
# 2. Nowhere in the package is a research id derived from a counter,
# a row count or a max()+1 -- the shapes a predictable id takes.
sequential = []
banned_calls = {"count", "max", "len", "next", "scalar"}
for path in sorted(PKG_ROOT.rglob("*.py")):
tree = ast.parse(path.read_text(encoding="utf-8"))
for node in ast.walk(tree):
if not isinstance(node, ast.Assign):
continue
targets = {t.id for t in node.targets if isinstance(t, ast.Name)}
if not targets & {"research_id", "new_research_id"}:
continue
for child in ast.walk(node.value):
if isinstance(child, ast.BinOp) or (
isinstance(child, ast.Call)
and _called_name(child) in banned_calls
):
sequential.append(
f"{path.relative_to(PKG_ROOT)}:{node.lineno} "
f"{ast.unparse(node.value)}"
)
break
assert not sequential, (
"a research id is derived from a sequence; a predictable id would "
"make every un-owner-checked registry handler directly "
f"reachable: {sequential}"
)
# ===========================================================================
# 2. The ordering property: owner lookup before the registry
# ===========================================================================
def _owner_lookup_precedes_registry(module: str, qualname: str):
"""(registry_lineno, db_lineno) for a named function body."""
body = _function_bodies().get((module, qualname))
assert body is not None, (
f"{module}::{qualname} no longer exists -- this assertion is stale"
)
return (
_first_lineno_of_call(body, ACCESSORS),
_first_lineno_of_call(body, frozenset({DB_CHOKEPOINT})),
)
def test_the_web_terminate_handler_does_the_owner_lookup_first():
"""Control for the finding below: this is what correct looks like.
``POST /api/terminate/{research_id}`` opens the caller's own
encrypted database, 404s on an id that is not there, and only then
touches the shared registry.
"""
registry_at, db_at = _owner_lookup_precedes_registry(
"web/routers/research.py", "terminate_research"
)
assert db_at is not None, (
"terminate_research no longer opens a user-scoped session at all"
)
assert registry_at is not None
assert db_at < registry_at, (
f"terminate_research reaches the shared registry at line "
f"{registry_at} before its owner lookup at line {db_at}"
)
def test_cancel_research_does_the_owner_lookup_before_the_global_registry():
"""Regression guard: ``cancel_research`` now checks, then acts.
``POST /research/api/terminate/{research_id}`` (``web/routers/api.py``
``api_terminate_research``) authenticates, then hands the *path*
``research_id`` straight to
``web/services/research_service.cancel_research`` with no scoping of
its own -- so ``cancel_research`` is the only thing standing between
an authenticated caller and another user's in-flight research.
It used to set the global termination flag and read the shared
registry *before* confirming the caller owns the id (a broken access
control: the destructive action happened before the check). That has
been fixed with an explicit ``get_user_db_session(username)`` gate at
the top of the function, ahead of any global-registry read or write.
This test locks the fixed ordering in, mirroring the "what correct
looks like" control above
(:func:`test_the_web_terminate_handler_does_the_owner_lookup_first`).
"""
# --- static: the call ordering inside cancel_research -------------
registry_at, db_at = _owner_lookup_precedes_registry(
"web/services/research_service.py", "cancel_research"
)
assert registry_at is not None and db_at is not None
assert db_at < registry_at, (
"cancel_research reaches the global registry before its owner "
"lookup again -- the broken-access-control regression this test "
"guards against is back"
)
# --- and the router still hands it an unvalidated path parameter --
# (so cancel_research's own ordering is the only thing that matters)
router_body = _function_bodies().get(
("web/routers/api.py", "api_terminate_research")
)
assert router_body is not None, (
"api_terminate_research is gone; re-derive the reachability claim"
)
assert (
_first_lineno_of_call(router_body, frozenset({DB_CHOKEPOINT})) is None
), (
"api_terminate_research now scopes the id itself; re-check the "
"reachability of cancel_research's owner-check prefix"
)
assert "research_id" in {arg.arg for arg in router_body.args.args}, (
"api_terminate_research no longer takes research_id from the path"
)
def test_cancel_research_refuses_a_foreign_active_research(
parked_threads, monkeypatch
):
"""SECURITY (execution-verified): the foreign-id attack is refused.
Registers a running research owned by ``victim`` in the shared
registry, then calls ``cancel_research(victim_id, "attacker")``.
``get_user_db_session`` is replaced by a tripwire that RECORDS the
call and raises, standing in for "the id genuinely belongs to
someone else" -- exactly what a real owner-scoped lookup returns for
a foreign id. ``cancel_research`` must reach that lookup before it
ever touches the global registry, so the victim's research is left
completely untouched.
"""
from local_deep_research.web.services import research_service
handled: list[tuple[str, str]] = []
monkeypatch.setattr(
research_service,
"handle_termination",
lambda rid, username=None: handled.append((rid, username)),
)
owner_lookups: list[tuple] = []
def _tripwire(*args, **kwargs):
owner_lookups.append(args)
raise RuntimeError("owner-lookup tripwire")
monkeypatch.setattr(research_service, "get_user_db_session", _tripwire)
victim_id = _rid()
thread = parked_threads()
thread.start()
research_state.set_active_research(
victim_id, _entry(thread, username="victim")
)
result = research_service.cancel_research(victim_id, "attacker")
assert owner_lookups, (
"the owner-scoped database lookup was never reached -- "
"cancel_research is not checking ownership before acting"
)
assert result is False, (
"cancel_research reported success for a foreign research_id "
f"instead of refusing it: {result!r}"
)
assert not handled, (
"handle_termination ran against the victim's research despite "
f"the caller not owning it: {handled}"
)
assert not research_state.is_termination_requested(victim_id), (
"attacker managed to set the victim's termination flag despite "
"the owner-lookup tripwire refusing the request"
)
# ===========================================================================
# 3. Lifecycle: leaks, resurrection, collisions
# ===========================================================================
def test_a_termination_flag_for_an_unregistered_id_is_never_reclaimed():
"""A flag can outlive -- and be set without -- any research.
``set_termination_flag`` writes unconditionally; it does not require
the id to be active. ``cleanup_research`` is the production path that
pops ``_termination_flags`` for research runs (plus deliberate
benchmark-task exceptions: the benchmark worker reclaims each
finished task's per-task UUID scope, and ``cancel_benchmark``
reclaims the same scope on its cancel-vs-finish race — see
``test_clear_termination_flag_call_sites_are_pinned``), and it runs
from the *worker's* teardown. So a flag set for an id whose worker never
starts stays in the process-global dict for the lifetime of the
process.
``routers/research.terminate_research`` walks straight into this: on
the "not yet registered" branch it sets the flag deliberately (so a
worker starting a moment later still aborts) and returns without any
cleanup.
"""
orphan = _rid()
assert not research_state.is_research_active(orphan)
research_state.set_termination_flag(orphan)
assert research_state.is_termination_requested(orphan) is True
# Nothing short of cleanup_research reclaims it.
research_state.remove_active_research(orphan)
assert research_state.is_termination_requested(orphan) is True
research_state.get_active_research_snapshot(orphan)
assert research_state.is_termination_requested(orphan) is True
research_state.cleanup_research(orphan)
assert research_state.is_termination_requested(orphan) is False
# ...and that reclaim path is not on the branch that sets the flag.
body = _function_bodies()[("web/routers/research.py", "terminate_research")]
assert (
_first_lineno_of_call(body, frozenset({"set_termination_flag"}))
is not None
)
assert (
_first_lineno_of_call(body, frozenset({"cleanup_research"})) is None
), (
"terminate_research now reclaims the flag itself -- update this "
"leak description"
)
def test_clear_termination_flag_call_sites_are_pinned():
"""Exactly two deliberate production callers exist.
The flag-leak test above documents that *research* flags are only
reclaimed by the worker's own ``cleanup_research`` teardown.
Benchmark tasks are the deliberate exception: each task runs under
a fresh per-task UUID scope (see
``benchmark_service._process_benchmark_task``), and both callers
below operate only on that scope:
* ``_run_benchmark_thread_impl`` clears it in a per-task ``finally``
so one finished task's cancellation state can never leak into
the next task's LLM calls;
* ``cancel_benchmark`` reclaims it only on the cancel-vs-finish
race -- if the worker popped the scope between cancel's read
and ``set_termination_flag``, cancel just created a stale
process-global flag for a fresh UUID that will never be reused,
so it re-checks ``current_tracking_id`` and clears it back.
The fresh-uuid-per-scope shape is what makes this safe --
re-deriving (not just extending) this list is required if it ever
changes, because a further caller needs the same argument or it
reintroduces cross-run kills.
Premise updated by PR #7363: the thread body -- including this
per-task ``finally`` -- moved verbatim into
``_run_benchmark_thread_impl``, behind a new thin
``_run_benchmark_thread`` wrapper that only pushes the
request-user context before delegating. That refactor changed
the enclosing function of the clear call, so the pinned qualname
below was updated to match; the call sites themselves, their
arguments, and the safety argument above are unchanged.
"""
sites, _ = _census()
callers = [
(site.module, site.qualname)
for site in sites
if site.accessor == "clear_termination_flag"
]
assert callers == [
(
"benchmarks/web_api/benchmark_service.py",
"_run_benchmark_thread_impl",
),
(
"benchmarks/web_api/benchmark_service.py",
"cancel_benchmark",
),
], f"clear_termination_flag call sites changed: {callers}"
def test_accessors_with_no_production_call_sites_are_pinned():
"""Half the registry's surface is unused.
Unused mutators on shared global state are a hazard: they are the
obvious thing for a future patch to reach for, and none of them
carry an ownership check. Pinned as an inventory so adding a caller
is a deliberate act that has to touch this list.
"""
sites, _ = _census()
called = {site.accessor for site in sites}
unused = sorted(ACCESSORS - called)
assert unused == [
"get_active_research_count",
"iter_active_research",
"remove_active_research",
"set_active_research",
"update_active_research",
"update_progress_if_higher",
], f"the unused-accessor inventory changed: {unused}"
def test_starting_a_research_does_not_clear_a_pre_existing_termination_flag(
parked_threads,
):
"""A re-used id starts already-terminated.
``check_and_start_research`` writes ``_active_research`` but never
touches ``_termination_flags``. Combined with the leak above, an id
that was flagged while QUEUED and then dispatched by the queue
processor begins life with ``is_termination_requested() == True`` and
aborts at its first checkpoint.
This is the *intended* behaviour for the stop-while-queued path (the
user did ask for it), but it is also the mechanism by which any
stale flag would silently kill a future research on the same id.
Only uuid4 makes "the same id" impossible for an unrelated research.
"""
research_id = _rid()
research_state.set_termination_flag(research_id)
thread = parked_threads()
assert (
research_state.check_and_start_research(research_id, _entry(thread))
is True
)
assert research_state.is_termination_requested(research_id) is True, (
"the flag was cleared on start -- the stop-while-queued handshake "
"in routers/research.terminate_research relies on it surviving"
)
def test_a_live_id_refuses_a_second_thread_and_a_dead_one_is_replaced(
parked_threads,
):
"""Key collision: liveness, not presence, decides."""
research_id = _rid()
live = parked_threads()
assert (
research_state.check_and_start_research(
research_id, _entry(live, username="first")
)
is True
)
assert live.is_alive()
intruder = parked_threads()
assert (
research_state.check_and_start_research(
research_id, _entry(intruder, username="second")
)
is False
), "a second live thread was allowed for one research_id"
assert intruder.ident is None, "the losing thread was started anyway"
# The incumbent entry is untouched.
assert (
research_state.get_research_field(research_id, "settings")["username"]
== "first"
)
# Once the incumbent dies, the same id is re-usable and the old
# entry -- including its accumulated log -- is discarded wholesale.
research_state.append_research_log(research_id, {"m": "old"})
parked_threads.release.set()
live.join(timeout=10)
assert not live.is_alive()
replacement = parked_threads()
assert (
research_state.check_and_start_research(
research_id, _entry(replacement, username="third")
)
is True
)
assert (
research_state.get_research_field(research_id, "settings")["username"]
== "third"
)
assert research_state.get_research_field(research_id, "log") == [], (
"the replaced entry kept the dead research's log"
)
def test_check_and_start_research_rejects_data_without_a_thread():
"""The one input validation the registry does."""
with pytest.raises(ValueError, match="thread"):
research_state.check_and_start_research(_rid(), {"progress": 0})
with pytest.raises(ValueError, match="thread"):
research_state.check_and_start_research(_rid(), "not-a-dict")
def test_cleanup_research_clears_both_dicts_for_that_id_only(
parked_threads,
):
"""Cleanup is per-id, and it is the joint reclaim for both dicts."""
doomed, bystander = _rid(), _rid()
for research_id in (doomed, bystander):
research_state.set_active_research(
research_id, _entry(parked_threads())
)
research_state.set_termination_flag(research_id)
research_state.cleanup_research(doomed)
assert research_state.is_research_active(doomed) is False
assert research_state.is_termination_requested(doomed) is False
assert research_state.is_research_active(bystander) is True
assert research_state.is_termination_requested(bystander) is True
def test_cleanup_research_can_release_the_slot_but_preserve_its_stop_signal(
parked_threads,
):
"""Cancellation handoff is atomic: inactive to dispatch, flagged to worker."""
research_id = _rid()
research_state.set_active_research(
research_id, _entry(parked_threads(), username="alice")
)
research_state.set_termination_flag(research_id)
research_state.cleanup_research(research_id, preserve_termination_flag=True)
assert research_state.is_research_active(research_id) is False
assert research_state.is_termination_requested(research_id) is True
def test_an_entry_outlives_its_thread_until_someone_reclaims_it(
parked_threads,
):
"""A dead worker leaves the entry behind; only a sweep removes it.
This is why ``reclaim_stale_user_active_research`` and
``reconcile_orphan_active_research`` exist. Until one of them runs,
``is_research_active`` reports True for a research whose thread is
gone -- which is exactly the divergence those helpers reconcile
against the database.
"""
research_id = _rid()
thread = parked_threads()
research_state.check_and_start_research(research_id, _entry(thread))
parked_threads.release.set()
thread.join(timeout=10)
assert research_state.is_research_thread_alive(research_id) is False
assert research_state.is_research_active(research_id) is True, (
"the entry vanished on its own -- the reclaim helpers would be "
"unnecessary"
)
assert research_id in research_state.get_active_research_ids()
# ===========================================================================
# 4. Snapshot / copy semantics
# ===========================================================================
def test_snapshots_never_hand_out_the_thread_or_shared_mutables(
parked_threads,
):
"""``get_active_research_snapshot`` is the socket + history read path.
It must not expose the ``Thread`` object (unserialisable, and a
handle on another user's worker) and must not alias the live ``log``
or ``settings``, which a caller could otherwise mutate under other
threads' feet without the lock.
"""
research_id = _rid()
thread = parked_threads()
research_state.set_active_research(
research_id, _entry(thread, progress=42, log=[{"m": "a"}])
)
snapshot = research_state.get_active_research_snapshot(research_id)
assert set(snapshot) == {"progress", "status", "log", "settings"}
assert "thread" not in snapshot
assert snapshot["progress"] == 42
snapshot["log"].append({"m": "injected"})
snapshot["settings"]["username"] = "attacker"
fresh = research_state.get_active_research_snapshot(research_id)
assert fresh["log"] == [{"m": "a"}]
assert fresh["settings"] == {"username": "owner"}
assert research_state.get_active_research_snapshot(_rid()) is None
def test_get_research_field_copies_mutables_but_passes_the_thread_through(
parked_threads,
):
"""Lists and dicts are copied; everything else is returned as-is.
The ``thread`` case is deliberate (the docstring explains that
``copy.copy`` on a Thread is broken) but it does mean
``get_research_field(rid, "thread")`` hands the raw worker handle to
any caller -- worth pinning so it stays a conscious choice.
"""
research_id = _rid()
thread = parked_threads()
research_state.set_active_research(
research_id, _entry(thread, log=[{"m": "a"}])
)
log = research_state.get_research_field(research_id, "log")
log.append({"m": "injected"})
assert research_state.get_research_field(research_id, "log") == [{"m": "a"}]
settings = research_state.get_research_field(research_id, "settings")
settings["username"] = "attacker"
assert research_state.get_research_field(research_id, "settings") == {
"username": "owner"
}
assert research_state.get_research_field(research_id, "thread") is thread
assert (
research_state.get_research_field(research_id, "nope", "fallback")
== "fallback"
)
assert (
research_state.get_research_field(_rid(), "progress", "absent")
== "absent"
)
def test_update_progress_helpers_are_monotonic_and_report_liveness():
"""Progress never goes backwards; the compound helper reports both."""
research_id = _rid()
research_state.set_active_research(
research_id, _entry(thread=None, progress=50)
)
assert research_state.update_progress_if_higher(research_id, 80) == 80
assert research_state.update_progress_if_higher(research_id, 30) == 80
assert research_state.update_progress_if_higher(research_id, None) == 80
assert research_state.update_progress_if_higher(_rid(), 10) is None
assert research_state.update_progress_and_check_active(research_id, 90) == (
90,
True,
)
assert research_state.update_progress_and_check_active(research_id, 5) == (
90,
True,
)
assert research_state.update_progress_and_check_active(_rid(), 5) == (
None,
False,
)
def test_writes_to_an_unknown_id_are_silently_dropped():
"""The registry has no "unknown id" signal at all.
Every mutator degrades to a no-op rather than raising, so a handler
that operates on a stale or foreign id gets no feedback. Pinned
because it is the reason an ownership bug here fails *silently*.
"""
ghost = _rid()
research_state.update_active_research(ghost, status="hijacked")
research_state.append_research_log(ghost, {"m": "x"})
assert research_state.is_research_active(ghost) is False
assert research_state.get_active_research_snapshot(ghost) is None
assert research_state.is_research_thread_alive(ghost) is False
def test_get_usernames_with_active_research_spans_every_user(
parked_threads,
):
"""One caller sees every user's name. By design -- and worth pinning.
Used by ``logout``, ``change_password`` and the idle-connection
sweeper to decide whether it is safe to close a user's database. It
is never returned to a client today; this test is here so that if it
ever is, the cross-user disclosure is loud.
"""
for username in ("alice", "bob"):
research_state.set_active_research(
_rid(), _entry(parked_threads(), username=username)
)
# An entry whose settings carry no username contributes nothing.
research_state.set_active_research(
_rid(), _entry(parked_threads(), username=None)
)
research_state.set_active_research(_rid(), {"thread": None, "settings": {}})
names = research_state.get_usernames_with_active_research()
assert {"alice", "bob"} <= names
assert None not in names
def test_the_registry_key_annotation_disagrees_with_production():
"""Cosmetic, but it is a documented repo invariant.
``.pre-commit-hooks/check-research-id-type.py`` exists specifically
to stop research ids being typed as ints ("Research IDs are UUIDs
and should always be treated as strings"). Its regexes only match
``research_id: int``, so the registry's ``dict[int, dict]`` slipped
through -- while every production key is a uuid4 string.
"""
annotations = research_state.__annotations__
assert repr(annotations["_active_research"]) == "dict[int, dict]"
assert repr(annotations["_termination_flags"]) == "dict[int, bool]"
research_id = _rid()
research_state.set_active_research(research_id, {"thread": None})
with research_state._lock:
assert all(
isinstance(key, str) for key in research_state._active_research
), "a non-string research_id reached the registry"
# ===========================================================================
# 5. Thread-safety
# ===========================================================================
class _ProbeThread:
"""Stand-in for a worker that inspects the lock state during ``start()``.
``check_and_start_research`` only requires ``.start()`` and
``.is_alive()``, so this can observe exactly what is held at the
moment the real code starts the worker -- without patching anything
in ``src/``.
"""
def __init__(self, username="owner"):
self.username = username
self.started = False
self.registry_lock_was_held = None
self.user_gate_was_held = None
self.entry_was_visible = None
def start(self):
self.started = True
# The registry lock is an RLock: reentrant for the OWNING thread,
# so "is it held?" has to be probed from a DIFFERENT thread, and
# non-blockingly (a blocking acquire would deadlock against the
# caller that is holding it right now).
result: dict[str, bool] = {}
prober = threading.Thread(
target=lambda: result.__setitem__(
"registry_held",
not _try_acquire_and_release(research_state._lock),
),
daemon=True,
)
prober.start()
prober.join(timeout=10)
assert not prober.is_alive(), "the lock probe hung"
self.registry_lock_was_held = result["registry_held"]
gate = research_state._user_research_start_gates.get(self.username)
self.user_gate_was_held = gate is not None and gate.locked()
with research_state._lock:
self.entry_was_visible = self in [
entry.get("thread")
for entry in research_state._active_research.values()
]
def is_alive(self):
return False
def _try_acquire_and_release(lock) -> bool:
if lock.acquire(blocking=False):
lock.release()
return True
return False
def test_the_worker_is_started_inside_the_lock_and_behind_the_user_gate():
"""Registration is atomic with respect to the worker's own first write.
``check_and_start_research`` calls ``thread.start()`` *before* it
writes ``_active_research[research_id]``, which would be a lost-update
bug -- every mutator silently no-ops on an unknown id -- except that
both happen while the caller holds the RLock. A worker's first
registry call therefore blocks until the entry is visible.
This also pins the documented lock ordering: the per-user start gate
is taken BEFORE the global lock, never the reverse (the reverse would
deadlock against ``change_password``, which holds the gate across a
multi-second SQLCipher rekey while progress updates take the lock).
"""
probe = _ProbeThread(username="gated-user")
assert (
research_state.check_and_start_research(
_rid(), _entry(probe, username="gated-user")
)
is True
)
assert probe.started is True
assert probe.registry_lock_was_held is True, (
"thread.start() runs OUTSIDE the registry lock -- a worker's first "
"update_active_research/append_research_log can land before the "
"entry exists and be silently dropped"
)
assert probe.user_gate_was_held is True, (
"the per-user research-start gate was not held across registration"
)
assert probe.entry_was_visible is False, (
"the entry was written before the thread started; if this changed "
"on purpose, the lost-update reasoning above needs revisiting"
)
def test_a_workers_first_registry_write_is_not_lost(parked_threads):
"""End-to-end companion to the probe above, with a real thread."""
research_id = _rid()
observed: dict[str, object] = {}
ready = threading.Event()
def worker():
observed["active"] = research_state.is_research_active(research_id)
research_state.append_research_log(research_id, {"m": "first"})
research_state.update_active_research(research_id, status="running")
ready.set()
thread = parked_threads(worker)
assert (
research_state.check_and_start_research(research_id, _entry(thread))
is True
)
assert ready.wait(timeout=10), "worker never ran"
thread.join(timeout=10)
assert observed["active"] is True, (
"the worker saw no entry for its own research_id"
)
assert research_state.get_research_field(research_id, "log") == [
{"m": "first"}
]
assert research_state.get_research_field(research_id, "status") == "running"
def test_concurrent_starts_for_one_id_produce_exactly_one_winner(
parked_threads,
):
"""The check-and-start is atomic under contention.
Eight threads race on the same research_id through a Barrier. Exactly
one may register, and exactly one worker thread may ever be started.
"""
research_id = _rid()
racers = 8
barrier = threading.Barrier(racers)
results: list[bool] = []
results_lock = threading.Lock()
workers: list[threading.Thread] = []
def racer():
worker = parked_threads()
with results_lock:
workers.append(worker)
barrier.wait(timeout=10)
won = research_state.check_and_start_research(
research_id, _entry(worker)
)
with results_lock:
results.append(won)
contenders = [
threading.Thread(target=racer, daemon=True) for _ in range(racers)
]
for contender in contenders:
contender.start()
for contender in contenders:
contender.join(timeout=20)
assert not contender.is_alive(), "a contender hung"
assert len(results) == racers
assert sum(results) == 1, (
f"{sum(results)} callers registered the same research_id"
)
assert sum(1 for w in workers if w.ident is not None) == 1, (
"more than one worker thread was started for one research_id"
)
def test_the_user_start_gate_serialises_one_user_and_not_the_others():
"""Per-user mutual exclusion, and one Lock object per username."""
gate_a = research_state._get_user_research_start_gate("alice")
assert research_state._get_user_research_start_gate("alice") is gate_a, (
"a second lookup minted a SECOND lock for the same user"
)
gate_b = research_state._get_user_research_start_gate("bob")
assert gate_b is not gate_a
held = threading.Event()
release = threading.Event()
entered_second = threading.Event()
def holder():
with research_state.user_research_start_gate("alice"):
held.set()
release.wait(timeout=10)
def contender():
with research_state.user_research_start_gate("alice"):
entered_second.set()
holder_thread = threading.Thread(target=holder, daemon=True)
holder_thread.start()
assert held.wait(timeout=10)
# Deterministic exclusion proof: no sleep, no polling -- while the
# holder is inside the gate, a non-blocking acquire must fail.
assert gate_a.locked() is True
assert _try_acquire_and_release(gate_a) is False, (
"alice's gate is not actually held inside the context manager"
)
assert gate_b.locked() is False, (
"holding alice's gate also blocks bob -- the gate is not per-user"
)
# Another user's research can still register while alice's gate is held.
assert _try_acquire_and_release(gate_b) is True
contender_thread = threading.Thread(target=contender, daemon=True)
contender_thread.start()
assert not entered_second.is_set(), (
"a second holder entered alice's gate while it was held"
)
release.set()
assert entered_second.wait(timeout=10)
holder_thread.join(timeout=10)
contender_thread.join(timeout=10)
assert gate_a.locked() is False
def test_a_missing_username_degrades_to_no_gating():
"""``user_research_start_gate(None)`` yields without minting a lock."""
before = set(research_state._user_research_start_gates)
with research_state.user_research_start_gate(None):
pass
assert set(research_state._user_research_start_gates) == before
def test_user_start_gates_are_never_removed():
"""Deliberate, documented -- and therefore an unbounded-in-users dict.
The module explains why a gate can never be popped (a lookup-then-
acquire race would mint a second Lock and defeat the exclusion). The
consequence is that the dict retains one ``threading.Lock`` per
distinct username the process has ever seen, for the life of the
process. Bounded by the user population rather than by traffic, so
small -- but there is no eviction path at all, which is what this
pins.
"""
source = (PKG_ROOT / "web" / "research_state.py").read_text(
encoding="utf-8"
)
tree = ast.parse(source)
for node in ast.walk(tree):
if isinstance(node, ast.Call) and isinstance(node.func, ast.Attribute):
target = node.func.value
if (
isinstance(target, ast.Name)
and target.id == "_user_research_start_gates"
):
assert node.func.attr not in {"pop", "clear", "popitem"}, (
f"a gate eviction path appeared at line {node.lineno}"
)
if isinstance(node, ast.Delete):
for target in node.targets:
if isinstance(target, ast.Subscript) and isinstance(
target.value, ast.Name
):
assert target.value.id != "_user_research_start_gates", (
f"a gate is deleted at line {node.lineno}"
)
before = len(research_state._user_research_start_gates)
for index in range(50):
research_state._get_user_research_start_gate(f"user-{index}")
assert len(research_state._user_research_start_gates) == before + 50, (
"gate creation is not one-per-username"
)
# Nothing in the module shrinks it again.
research_state.cleanup_research(_rid())
assert len(research_state._user_research_start_gates) == before + 50
def test_concurrent_log_appends_and_progress_updates_do_not_race(
parked_threads,
):
"""Every mutation goes through the one lock, so nothing is lost.
Ten threads each append 20 log entries and push a strictly rising
progress value. Without the lock, ``setdefault("log", []).append``
and the read-compare-write in ``update_progress_if_higher`` would
both drop updates.
"""
research_id = _rid()
research_state.set_active_research(
research_id, _entry(parked_threads(), progress=0)
)
writers, per_writer = 10, 20
barrier = threading.Barrier(writers)
def writer(index):
barrier.wait(timeout=10)
for step in range(per_writer):
research_state.append_research_log(
research_id, {"w": index, "s": step}
)
research_state.update_progress_if_higher(
research_id,
(index * per_writer + step) * 100 / (writers * per_writer),
)
threads = [
threading.Thread(target=writer, args=(index,), daemon=True)
for index in range(writers)
]
for thread in threads:
thread.start()
for thread in threads:
thread.join(timeout=20)
assert not thread.is_alive()
log = research_state.get_research_field(research_id, "log")
assert len(log) == writers * per_writer, (
f"lost log appends: {len(log)} of {writers * per_writer}"
)
assert len({(entry["w"], entry["s"]) for entry in log}) == len(log)
assert research_state.get_research_field(research_id, "progress") == (
(writers * per_writer - 1) * 100 / (writers * per_writer)
)
def test_iteration_snapshots_are_stable_while_the_registry_mutates(
parked_threads,
):
"""``iter_active_research`` copies under the lock, then yields outside.
So a consumer cannot see a dict-changed-size error, and cannot alias
another entry's log.
"""
ids = [_rid() for _ in range(5)]
for research_id in ids:
research_state.set_active_research(
research_id, _entry(parked_threads(), log=[{"m": research_id}])
)
seen = []
for research_id, snapshot in research_state.iter_active_research():
seen.append(research_id)
# Mutating the registry mid-iteration must not disturb the walk.
research_state.cleanup_research(_rid())
research_state.set_active_research(_rid(), {"thread": None})
assert "thread" not in snapshot
assert set(ids) <= set(seen)
assert research_state.get_active_research_count() >= len(ids)