~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
"""Helpers for building chunk-targeted library/RAG citation anchors.
Centralizes the validation and URL-fragment construction that RAG search
engines and the LangGraph result collector all share. Every producer
must route chunk metadata through :func:`extract_chunk_index` before
interpolating it into a ``#chunk-...`` URL fragment — a UUID or boolean
``chunk_id`` would otherwise slip into ``document_chunks.html`` as a
fragment that points at a chunk that does not exist. This module is the
single source of truth so producers and the collector agree on what a
valid chunk index looks like.
"""
from __future__ import annotations
import re
from typing import Any, Mapping, Optional
# The document views a library route may address. Shared by the two
# route-matching regexes — ``library_resolver._LIBRARY_PATH_RE`` and
# ``url_utils._LIBRARY_ROUTE_PATH_RE`` — so adding a view teaches both at
# once instead of relying on one to be hand-mirrored from the other.
#
# It does NOT unify every route predicate in the codebase:
# ``url_utils._is_library_route`` is a prefix test that answers a
# different question (is this string shaped like a library route at all)
# and is deliberately looser. Do not read this constant as making all
# three agree.
LIBRARY_ROUTE_SUFFIXES = ("pdf", "chunks")
# Escaped: these are interpolated into regexes, so a future view containing
# a metacharacter (``v1.2``, ``raw+text``) would otherwise silently widen
# what counts as a library route.
LIBRARY_ROUTE_SUFFIX_ALTERNATION = "|".join(
re.escape(suffix) for suffix in LIBRARY_ROUTE_SUFFIXES
)
# Root-relative routes the library / collection RAG engines emit as citation
# URLs, plus the absolute alias the agent sometimes types instead. Shared
# with ``utilities.url_utils`` (which imports from here — this module stays
# dependency-free so the import can only go one way).
LIBRARY_ROUTE_PREFIXES = ("/library/document/", "/lib/document/")
LIBRARY_ALIAS_HOST = "library.document"
_LIBRARY_ALIAS_SCHEME_PREFIX = "https://"
# Chunk indices are small ordinals within one document. The bound is far
# above any realistic chunk count and exists so an absurd value (a
# timestamp, a float that rounded to 10**30, a 64-bit id mistaken for an
# index) cannot be interpolated into a URL fragment.
MAX_CHUNK_INDEX = 1_000_000
# Document ids are UUIDs / hex hashes / slugs. ASCII only: ``str.isalnum()``
# accepts every Unicode letter and digit, so a fullwidth "\uff17" would pass
# and then key separately from its percent-encoded form.
_SAFE_ID_RE = re.compile(r"^[0-9A-Za-z_-]+$")
def extract_chunk_index(metadata: Mapping[str, Any] | None) -> Optional[int]:
"""Return a validated non-negative chunk index from a result's metadata,
or ``None`` if the metadata does not carry a usable value.
Reads (in order):
* ``metadata["chunk_index"]`` — preferred, always an ``int`` in the
production schema.
* ``metadata["chunk_id"]`` — legacy field that may carry either an
``int`` or an int-as-string. UUIDs and other non-int-like values
are rejected.
Accepts:
* ``int`` (excluding ``bool``) — used as-is.
* ``str`` composed solely of ASCII digits.
* ``float`` whose value is a whole non-negative integer (catches
e.g. ``0.0`` from numpy conversion).
Rejects (returns ``None``):
* ``bool`` (avoids ``True`` being printed as ``#chunk-True``).
* Negative integers — zero is allowed because chunks are 0-indexed.
* Values above :data:`MAX_CHUNK_INDEX`. No document has a million
chunks, so a value that large is a timestamp, a 64-bit id, or the
silent binary rounding of a float literal such as ``1e30`` — none of
which addresses a real anchor.
* Strings that are not pure ASCII digits: ``"550e8400-..."`` UUIDs,
signed forms (``"+5"``/``"-1"``), surrounding whitespace (``" 5 "``),
and non-ASCII digit characters that ``str.isdigit()`` accepts but
that no anchor id can contain (Arabic-Indic ``"\u0667"``,
mathematical ``"\U0001d7dd"``, ...).
* Floats with a fractional part.
* Anything else (``None``, dicts, lists, ...).
"""
if not isinstance(metadata, Mapping):
return None
chunk_idx_raw: Any = None
if metadata.get("chunk_index") is not None:
chunk_idx_raw = metadata["chunk_index"]
elif metadata.get("chunk_id") is not None:
chunk_idx_raw = metadata["chunk_id"]
else:
return None
try:
if isinstance(chunk_idx_raw, bool):
# bool is a subclass of int but True/False are never chunk ids.
return None
if isinstance(chunk_idx_raw, int):
value = chunk_idx_raw
elif isinstance(chunk_idx_raw, str):
# Reject surrounding whitespace, signs, and every non-ASCII
# digit: the value is interpolated verbatim into an anchor, and
# ``str.isdigit()`` alone is Unicode-wide (it accepts "\u0667"
# and "\U0001d7dd", which ``int()`` then happily converts to 7
# and 5). Deliberately no ``.strip()`` — the docstring has
# always promised whitespace is rejected.
if not chunk_idx_raw.isascii() or not chunk_idx_raw.isdigit():
return None
value = int(chunk_idx_raw)
elif isinstance(chunk_idx_raw, float):
if not chunk_idx_raw.is_integer():
return None
value = int(chunk_idx_raw)
else:
return None
except (ValueError, AttributeError):
return None
# Negative chunk ids never match a real chunk — reject. Zero is
# allowed: the document_chunks.html template renders ``chunk.index``
# directly as the HTML anchor id, so a 0-indexed chunk anchors at
# ``#chunk-0``.
if value < 0 or value > MAX_CHUNK_INDEX:
return None
return value
def extract_document_id(
metadata: Mapping[str, Any] | None, *top_level: Any
) -> Optional[str]:
"""Return a sanitised library document id from a result's metadata or
top-level fields, or ``None`` if no usable id is present.
Reads (in order):
* ``metadata["doc_id"]`` — legacy key.
* ``metadata["source_id"]`` — used by the current LibraryRAGSearchEngine.
* ``metadata["document_id"]`` — alternate key.
* ``top_level[0]``'s ``source_id`` / ``document_id`` — for results that
promote the id to the top-level dict instead of metadata.
Sanitisation:
* Non-strings are coerced: ``int`` → ``str``.
* Strings are stripped; non-empty values containing only
alphanumerics, dashes, and underscores pass. Anything else
(whitespace, path traversal, control chars, ``/``) is rejected
because it would be interpolated into a URL path.
"""
candidates: list[Any] = []
if isinstance(metadata, Mapping):
candidates.append(metadata.get("doc_id"))
candidates.append(metadata.get("source_id"))
candidates.append(metadata.get("document_id"))
for obj in top_level:
if isinstance(obj, Mapping):
candidates.append(obj.get("doc_id"))
candidates.append(obj.get("source_id"))
candidates.append(obj.get("document_id"))
for raw in candidates:
if raw is None:
continue
if isinstance(raw, int) and not isinstance(raw, bool):
return str(raw)
if isinstance(raw, str):
stripped = raw.strip()
if not stripped:
continue
if not is_safe_document_id(stripped):
continue
return stripped
return None
def is_safe_document_id(value: str) -> bool:
"""Return ``True`` if *value* is safe to interpolate into a URL path.
**ASCII** alphanumerics, dashes and underscores only — which excludes
``/``, ``?``, ``#``, ``%``, whitespace, control characters and ``..``
traversal. ASCII matters: ``str.isalnum()`` is true for every Unicode
letter and digit, so a fullwidth ``"\uff17"`` would pass, be emitted
unencoded into a URL path, and then key separately from its
``%EF%BC%97`` form — one document, two bibliography entries.
Shared by :func:`extract_document_id`, :func:`build_chunk_anchor_url`
and ``url_utils``'s library-route parser so none of them can drift
apart.
"""
return isinstance(value, str) and _SAFE_ID_RE.match(value) is not None
def is_library_chunk_result(result: Mapping[str, Any] | None) -> bool:
"""Return ``True`` if *result* is a library/RAG hit that should receive
a chunk anchor.
Heuristic: the result's ``source`` / ``source_type`` field is
``"library"`` or its link *is* a library-document route (see
:func:`is_library_document_link`). Mirrors the producer-side checks in
:class:`langgraph_agent_strategy.SearchResultsCollector` so the two
agree on the eligibility rule.
"""
if not isinstance(result, Mapping):
return False
if result.get("source") == "library":
return True
if result.get("source_type") == "library":
return True
return is_library_document_link(result.get("link") or result.get("url"))
def _alias_host(stripped: str) -> str | None:
"""Return the lowercased host of an ``https://<host>/...`` URL, or ``None``.
Userinfo and port are removed so ``https://[email protected]:443/x``
and ``https://library.document/x`` give the same host. A trailing
``/`` is required, so a bare authority with no path is not a document
link — matching what the literal-prefix test accepted before.
"""
if not stripped.lower().startswith(_LIBRARY_ALIAS_SCHEME_PREFIX):
return None
rest = stripped[len(_LIBRARY_ALIAS_SCHEME_PREFIX) :]
authority, sep, _ = rest.partition("/")
if not sep:
return None
# Userinfo may itself contain ``@``; the host is after the LAST one.
authority = authority.rpartition("@")[2]
if authority.startswith("["):
# IPv6 literal: the port, if any, follows the closing bracket.
host = authority.partition("]")[0] + "]"
else:
host = authority.partition(":")[0]
return host.lower()
def is_library_document_link(link: Any) -> bool:
"""Return ``True`` if *link* addresses a local library document.
Anchored at the start of the URL on purpose. A substring test
(``"/library/document/" in link``) also matches
``https://evil.example/library/document/7/chunks``, and the caller
reacts by REPLACING the link with a local route — silently relabelling
an external result as a document in the user's own library.
"""
if not isinstance(link, str):
return False
stripped = link.strip()
if stripped.startswith(LIBRARY_ROUTE_PREFIXES):
return True
# The absolute alias the agent sometimes emits. Matched on the HOST,
# not on a literal ``https://library.document/`` prefix: the prefix
# form misses the ``:443`` and userinfo spellings that
# ``url_utils._normalize_library_alias`` deliberately accepts as the
# same document, so a caller asking "is this a library citation" got
# False for a string the renderer would happily normalise. Callers
# that strip an unusable fragment then skipped those spellings
# entirely, and the raw value reached the DB and the MCP payload.
#
# Parsed by hand rather than with ``urlsplit``, which silently DELETES
# embedded tab/newline/CR — the very characters that make a crafted
# alias worth catching here, and which would otherwise let
# ``library.doc\tument`` answer for ``library.document``.
return _alias_host(stripped) == LIBRARY_ALIAS_HOST
def build_chunk_anchor_url(
link: str,
doc_id: Optional[str],
chunk_index: Optional[int],
*,
document_chunk_id: Optional[int] = None,
) -> Optional[str]:
"""Build a ``/library/document/<doc_id>/chunks#chunk-<n>`` URL when
*doc_id* and *chunk_index* are both valid, otherwise return ``None``.
Returns ``None`` (rather than mutating *link*) when:
* the chunk index failed validation (UUID, bool, negative, string,
float with a fractional part, or any other non-int-like value), or
* the document id is missing, or is not composed solely of
alphanumerics, dashes and underscores (see
:func:`is_safe_document_id`).
Callers should leave the original *link* unchanged on ``None`` —
appending ``#chunk-...`` to whatever route was already in the link
would point the anchor at an unrelated route when the doc id failed
sanitisation.
"""
# Re-validate ``chunk_index`` defensively. Most callers should have
# already passed it through :func:`extract_chunk_index`, but a
# future caller that passes a raw value (e.g. a producer that
# forgot to validate) must not be able to inject a malformed
# fragment. ``int`` excludes ``bool`` (which is technically an
# ``int`` subclass) and ``None``.
# Enforce the doc-id contract here rather than trusting the caller.
# Every in-tree caller runs ``extract_document_id`` first, but this
# function BUILDS the URL, so an unvalidated id would be interpolated
# straight into the path — a ``../..`` id yields a traversal route,
# and an id containing ``#`` yields two fragments. The
# docstring above has always promised a rejected id returns ``None``;
# this makes that true.
# Accept exactly what ``extract_document_id`` accepts — ``int``
# (excluding ``bool``) or ``str`` — so the two genuinely cannot drift,
# and build the URL from the STRIPPED value that was validated rather
# than from the raw argument, or leading/trailing control characters
# survive into the returned URL.
if doc_id is None or isinstance(doc_id, bool):
return None
if not isinstance(doc_id, (int, str)):
return None
safe_doc_id = str(doc_id).strip()
if not is_safe_document_id(safe_doc_id):
return None
if (
chunk_index is None
or isinstance(chunk_index, bool)
or not isinstance(chunk_index, int)
or chunk_index < 0
or chunk_index > MAX_CHUNK_INDEX
):
return None
query = (
f"?document_chunk_id={document_chunk_id}"
if type(document_chunk_id) is int
and 0 < document_chunk_id <= (1 << 63) - 1
else ""
)
return f"/library/document/{safe_doc_id}/chunks{query}#chunk-{chunk_index}"