Coverage for app / common / web_policy.py: 100%
42 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 a request's tools may use — set by the workspace's Tools rules.
3svc-core resolves a turn's Tools rules into one policy (sources to put first,
4the only ones to use, ones never to use; ``app/util/web_domains.py``) and
5sends it on every call the turn makes (``X-Web-Policy``). Every web tool the
6platform wrote reads it when it checks a URL (``DomainPolicy.for_run``), so
7it holds whichever node, worker, tool node or planner-added tool reached the
8web — not only the paths that thread a tool config through.
10A request with no header has no policy of its own: its tools keep their
11configured policy. A header that cannot be read allows no source
12(``WebPolicy.decode``): a restriction somebody wrote is never read as none.
13"""
15from __future__ import annotations
17from collections.abc import Iterable
18from contextvars import ContextVar, Token
20from app.util.web_domains import WebPolicy, covered
22HEADER = "X-Web-Policy"
24_CURRENT: ContextVar[WebPolicy | None] = ContextVar("svc_core_web_policy", default=None)
27def parse(raw: str | None) -> WebPolicy | None:
28 """The policy svc-core sent, or None when it sent none."""
29 return WebPolicy.decode(raw)
32def current() -> WebPolicy | None:
33 """The current request's policy, if svc-core sent one."""
34 return _CURRENT.get()
37def set_current(raw: str | None) -> Token:
38 return _CURRENT.set(parse(raw))
41def use(policy: WebPolicy | None) -> Token:
42 """Set the policy directly — for a caller with no request around it (a
43 test, or work resumed from a checkpoint)."""
44 return _CURRENT.set(policy)
47def reset(token: Token) -> None:
48 _CURRENT.reset(token)
51def request_context_lines(
52 policy: WebPolicy | None = None, *, platform: Iterable[str] = ()
53) -> list[str]:
54 """What the answering model is told of the policy, in its request
55 context: which sources the workspace's rules put first, allow or rule out
56 — so it searches and cites accordingly, and says so when the web is off —
57 and, for the built-in agent, the sources the platform puts first
58 (*platform*, ``tools/web/platform_sources.py``) where no rule chose any.
59 Nothing when neither applies."""
60 policy = current() if policy is None else policy
61 lines = _rule_lines(policy) if policy is not None and policy.restricts else []
62 lines.extend(_platform_lines(policy, platform))
63 return lines
66def _platform_lines(policy: WebPolicy | None, platform: Iterable[str]) -> list[str]:
67 """The platform's sources, less any the rules rule out — none when the
68 rules chose sources themselves or left none (``DomainPolicy.preferred``
69 holds the tools to the same)."""
70 if policy is not None and (
71 policy.closed or policy.preferred or policy.allowed is not None
72 ):
73 return []
74 blocked = policy.blocked if policy is not None else frozenset()
75 sources = sorted(d for d in platform if not covered(d, blocked))
76 if not sources:
77 return []
78 return [
79 ' <web_sources set_by="platform">',
80 " CHAIN-R puts these first; every other source stays usable: "
81 f"{', '.join(sources)}.",
82 " </web_sources>",
83 ]
86def _rule_lines(policy: WebPolicy) -> list[str]:
87 lines = [' <web_sources set_by="workspace_rules">']
88 if policy.closed:
89 lines.append(
90 " No web source is permitted for this request. Do not search the "
91 "web or read pages; answer from what you have and say that the web "
92 "was not available."
93 )
94 else:
95 if policy.allowed is not None:
96 lines.append(
97 " Use only these sources on the web (and their subdomains): "
98 f"{', '.join(sorted(policy.allowed))}."
99 )
100 preferred = sorted(policy.preferred - policy.blocked)
101 if preferred:
102 lines.append(
103 f" Trusted sources — search, read and cite these first: "
104 f"{', '.join(preferred)}."
105 )
106 if policy.blocked:
107 lines.append(
108 f" Never use these sources: {', '.join(sorted(policy.blocked))}."
109 )
110 lines.append(" </web_sources>")
111 return lines