Coverage for app / tools / web / platform_sources.py: 95%
21 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-10-31 11:23 +0000
1"""The web sources CHAIN-R puts first for its own agent — CHAINR Bot's list.
3svc-core sends them on the built-in agent's requests only
4(``AgentConfig.platform_sources`` / ``SkillSpec.platform_sources``, from
5``system_resources/web_sources.py``). They **prefer, never restrict**: a
6search ranks them first, a news search the first page missed them on runs
7once more scoped to them, and the answering model is told they come first
8— every other source stays usable.
10A workspace's own Tools rules come before them. When the rules put sources
11first or allow only some, those replace this list; a source the rules (or the
12tool's configuration) rule out is taken off it; and when the rules leave no
13source, nothing is put first (``DomainPolicy.preferred``).
15**Carried in the run config** (``configurable[CONFIG_KEY]``), set by
16``AgentGraphBuilder.run_config`` from the request's spec — batch, stream and
17resume, which rebuilds the builder from the same spec — and read with
18:func:`current` wherever a web tool runs, as the source registry is
19(``app/util/source_registry.py``). Never a tool's config: nothing a caller
20writes into a tool config can set it, so only svc-core's request for the
21built-in agent can.
22"""
24from __future__ import annotations
26from collections.abc import Iterable
27from typing import Any
29from app.util import web_domains
31#: Where a run's platform sources live in its config's ``configurable``. The
32#: ``__`` prefix keeps LangGraph from copying it into checkpoint metadata.
33CONFIG_KEY = "__chainr_platform_sources"
35#: At most this many, as a Tools rule's list.
36MAX_SOURCES = web_domains.MAX_DOMAINS
39def normalise(values: Iterable[object] | None) -> tuple[str, ...]:
40 """``values`` as stored domains, first ``MAX_SOURCES``; anything that
41 names no domain is dropped."""
42 return web_domains.normalise_domains(values or ())[:MAX_SOURCES]
45def of_config(config: Any) -> frozenset[str]:
46 """The platform sources a run config carries — none when it carries
47 none."""
48 if not isinstance(config, dict):
49 return frozenset()
50 found = (config.get("configurable") or {}).get(CONFIG_KEY)
51 return found if isinstance(found, frozenset) else frozenset()
54def current() -> frozenset[str]:
55 """The platform sources of the run this code is part of — none outside
56 a run, or for any agent but the built-in one."""
57 from langchain_core.runnables.config import var_child_runnable_config
59 return of_config(var_child_runnable_config.get())
62def usable(
63 sources: Iterable[str],
64 *,
65 blocked: Iterable[str] = (),
66 allowed: Iterable[str] = (),
67) -> frozenset[str]:
68 """``sources`` less those ``blocked`` covers, and, when ``allowed``
69 names any, those outside it — the platform's list never reaches past
70 what the tool's configuration and the workspace's rules permit."""
71 blocked = frozenset(blocked)
72 allowed = frozenset(allowed)
73 return frozenset(
74 d
75 for d in sources
76 if not web_domains.covered(d, blocked)
77 and (not allowed or web_domains.covered(d, allowed))
78 )
81__all__ = ["CONFIG_KEY", "MAX_SOURCES", "current", "normalise", "of_config", "usable"]