~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
"""Cooperative cancellation for in-flight LLM calls.
Research, chat and other long-running operations run on worker threads that
block inside a synchronous LangChain ``llm.invoke()``. Setting the
termination flag (``web.research_state.set_termination_flag``) only takes
effect at the *next* ``check_termination`` checkpoint — i.e. after the
current LLM HTTP request finishes, which can be minutes on a slow local
model. The worker therefore keeps burning GPU/API budget after the user
clicks Stop.
This module is the single place that maps a ``research_id`` to "should this
LLM call abort now?". Wrappers (``config.llm_config.ProcessingLLMWrapper``
and the rate-limiting wrapper) resolve the effective research id from:
1. an explicitly bound id (``bind_llm_to_research`` / ``research_id`` ctor
arg — survives thread-pool hops where the ContextVar is not copied), or
2. the current thread's search context (``utilities.thread_context``), so
LLMs created without an explicit id inside a research worker are still
covered.
When an id is known, wrappers prefer a streaming implementation of
``invoke``: they consume ``base_llm.stream()`` chunk by chunk, check the
flag between chunks, and close the generator on abort. Cancellation
latency drops from "full response time" to "time to next token". Closing
the generator releases the underlying HTTP response promptly instead of
leaving it to GC.
"""
from typing import Any, Optional
from loguru import logger
def get_current_research_id() -> Optional[str]:
"""Return the research id from the current thread's search context.
Returns ``None`` outside a research worker (request threads, CLI,
benchmarks without a context). Never raises — a missing/broken
context means "no cancellation scope", not an error.
"""
try:
from ..utilities.thread_context import get_search_context
ctx = get_search_context()
if isinstance(ctx, dict):
rid = ctx.get("research_id")
if rid:
return str(rid)
except Exception:
logger.debug(
"get_current_research_id: failed to read search context",
exc_info=True,
)
return None
def resolve_research_id(explicit: Optional[str] = None) -> Optional[str]:
"""Return the explicit id, falling back to the thread's search context."""
if explicit:
return str(explicit)
return get_current_research_id()
def is_termination_requested(research_id: Optional[str]) -> bool:
"""Return True iff Stop was requested for *research_id*.
``None`` (no cancellation scope) is never considered terminated.
Import is function-local so this module never creates an import cycle
with ``web.research_state``.
"""
if not research_id:
return False
try:
from ..web.research_state import is_termination_requested as _check
return bool(_check(research_id))
except Exception:
logger.debug(
"is_termination_requested: flag lookup failed",
exc_info=True,
)
return False
def raise_if_terminated(research_id: Optional[str]) -> None:
"""Raise ``ResearchTerminatedException`` if Stop was requested.
No-op when *research_id* is falsy or no flag is set. The exception
inherits from ``BaseException`` so ``except Exception`` blocks in
strategy code let it propagate (same pattern as CancelledError).
"""
if is_termination_requested(research_id):
from ..exceptions import ResearchTerminatedException
raise ResearchTerminatedException(
f"Research {research_id} was terminated by user during LLM call"
)
def _try_bind(target: Any, rid: str) -> None:
"""Best-effort ``_ldr_research_id`` setattr on one wrapper level.
Plain objects always accept it; exotic proxies or frozen test
doubles may refuse — that only loses the fast-path scope for that
level (the thread-context fallback still applies), so debug-log
and move on.
"""
try:
setattr(target, "_ldr_research_id", rid)
except Exception:
logger.debug("bind_llm_to_research: setattr refused", exc_info=True)
def bind_llm_to_research(
llm: Any, research_id: Optional[str], *, stop_at: Any = None
) -> Any:
"""Attach *research_id* to an LLM wrapper chain (in place, chain-safe).
Sets ``_ldr_research_id`` on *llm* and, when present, on its
``base_llm`` (covers ``ProcessingLLMWrapper -> RateLimitedWrapper ->
raw ChatModel`` without unwrapping). Returns *llm* for chaining.
``None`` clears nothing — a falsy id leaves existing bindings intact
so rebinding with an empty value cannot silently drop cancellation.
*stop_at* names an object the binding must not reach. The walk stops
there, so neither that object nor anything below it is written. Pass
the model under LDR's wrappers: other researches can share it (a
registered instance, or a model a registered factory returns more than
once), or it can hold shared objects of its own.
Invariant: a bound wrapper must not outlive its run. The binding is
resolved per call (explicit id > binding > thread context), so
re-binding for the next run takes effect without reconstructing the
LLM — but a wrapper bound to run A and reused for run B without
re-binding would fail-fast/post-check against A's flag. Callers must
re-bind (or construct with the new id) when crossing runs.
"""
if not research_id:
return llm
if stop_at is not None and llm is stop_at:
return llm
rid = str(research_id)
_try_bind(llm, rid)
# Propagate one level down so inner wrappers that are invoked
# directly (e.g. via __getattr__ passthrough) see the same scope,
# down to *stop_at*. Depth is bounded: each level sets a plain
# attribute, never recursing.
try:
inner = getattr(llm, "base_llm", None)
except Exception:
logger.debug(
"bind_llm_to_research: base_llm lookup failed", exc_info=True
)
return llm
if (
inner is not None
and inner is not stop_at
and getattr(inner, "_ldr_research_id", None) != rid
):
_try_bind(inner, rid)
# One more level for the double-wrapped shape
# (Processing -> RateLimited -> ChatModel).
try:
inner_inner = getattr(inner, "base_llm", None)
except Exception:
logger.debug(
"bind_llm_to_research: nested base_llm lookup failed",
exc_info=True,
)
return llm
if (
inner_inner is not None
and inner_inner is not stop_at
and getattr(inner_inner, "_ldr_research_id", None) != rid
):
_try_bind(inner_inner, rid)
return llm
def effective_research_id(
llm: Any, explicit: Optional[str] = None
) -> Optional[str]:
"""Resolve the cancellation scope for an LLM call.
Precedence: explicit ctor arg > ``_ldr_research_id`` binding >
current thread's search context. Used by wrappers on every call so
rebinding (or context propagation into pool threads) takes effect
without reconstructing the LLM.
"""
if explicit:
return str(explicit)
try:
bound = getattr(llm, "_ldr_research_id", None)
except Exception:
logger.debug(
"effective_research_id: binding lookup failed", exc_info=True
)
return get_current_research_id()
if bound:
return str(bound)
return get_current_research_id()