Coverage for app / services / system_resources / publishers.py: 100%
53 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""
2Built-in knowledge read from publishers' own websites, beyond News: UK and
3EU data-protection law and guidance, cyber security guidance, and the
4regulators' and security agencies' current updates.
6Data only, like :mod:`.news`, and held to the same rules
7(``tests/test_builtin_knowledge.py``, ``docs/guides/news.md``): every source
8is used under a licence its publisher states permits commercial reuse
9(:mod:`.licences`), records the publisher's own terms page, the credit it
10asks for and the date the terms were checked, and reads only that
11publisher's site. Each is crawled only while an agent that is not built in
12holds its repository (``web_crawl.system_sources_in_use``), honours
13robots.txt and its crawl delay, and costs the platform, recorded on its
14crawl. ``BUILT_IN_WEB_KNOWLEDGE_ENABLED=false`` stops provisioning and
15crawling all three.
17Domain sources are reference pages, read weekly and kept whole
18(``PageSourceSpec``); the Temporal one reads feeds, as News does
19(``news.NewsSourceSpec``). Each address was probed through svc-ai's own
20fetcher (robots.txt, extraction, feed parsing) before it was added.
22Checked and left out, with the reason (``docs/guides/knowledge.md``):
24- **EUR-Lex** (the EU AI Act, the GDPR): its pages answer an automated
25 fetch with a JavaScript challenge, so nothing can be read. Its reuse
26 terms would allow it.
27- **The ICO's news**: its RSS feeds no longer serve a feed, and its news
28 listing draws its articles with JavaScript. Its guidance pages are here.
29- **The NCSC's reports feed**: its newest entry is over a year old, so a
30 30-day window would hold nothing.
31"""
33from __future__ import annotations
35from dataclasses import dataclass
36from typing import Any
37from uuid import UUID
39from app.services.knowledge.web_config import refresh_for, validate_web_config
40from app.util.knowledge_categories import KnowledgeCategory
42from .definition import BuiltInRepository, SystemRepositorySpec
43from .licences import LICENCES, Licence
44from .news import NewsSourceSpec
46_ID_PREFIX = "c4a1b0e7-0000-4000-8000-"
48#: When every licence, terms page and address below was last checked
49#: against the publisher's own terms. A release that changes a source
50#: updates it.
51TERMS_CHECKED = "2026-10-27"
53#: The setting that turns all three repositories off.
54SWITCH = "BUILT_IN_WEB_KNOWLEDGE_ENABLED"
57def _id(n: int) -> UUID:
58 return UUID(f"{_ID_PREFIX}{n:012d}")
61@dataclass(frozen=True)
62class PageSourceSpec:
63 """A publisher's reference pages, as a built-in website knowledge source:
64 the listed pages, or the section below each, read weekly and kept whole
65 (no window), as Domain knowledge is."""
67 id: UUID
68 name: str
69 publisher: str
70 #: What it covers, first in its description: triage and the relevance
71 #: judge read a source's description, the judge its first 300 characters.
72 covers: str
73 #: Where the crawl starts — on the publisher's own site.
74 urls: tuple[str, ...]
75 licence: str
76 #: The publisher's own statement of the reuse terms.
77 terms_url: str
78 region: str
79 #: ``page``: only the listed pages (a law's whole text); ``section``:
80 #: those and the pages below them.
81 scope: str = "section"
82 max_pages: int = 60
83 max_depth: int = 2
84 #: The credit the publisher asks for, where it words its own; else the
85 #: licence's.
86 credit: str | None = None
87 terms_checked: str = TERMS_CHECKED
89 @property
90 def source_type(self) -> str:
91 return "web"
93 @property
94 def licence_record(self) -> Licence:
95 return LICENCES[self.licence]
97 def attribution(self) -> str:
98 return self.credit or self.licence_record.credit(self.publisher)
100 def source_config(self) -> dict[str, Any]:
101 """The website source's crawl policy — validated like a user's."""
102 return validate_web_config(
103 {
104 "urls": list(self.urls),
105 "scope": self.scope,
106 "max_pages": self.max_pages,
107 "max_depth": self.max_depth,
108 # The publisher's own pages, never its whole sitemap or the
109 # sites it links to.
110 "use_sitemap": False,
111 "follow_external": False,
112 "refresh": refresh_for(KnowledgeCategory.domain),
113 "attribution": self.attribution(),
114 "licence_url": self.licence_record.url,
115 }
116 )
118 def fields(self) -> dict[str, Any]:
119 return {
120 "name": self.name,
121 "description": (
122 f"{self.covers} From {self.publisher}'s own site, used under the "
123 f"{self.licence_record.name} ({self.terms_url}). "
124 f"{self.attribution()} Maintained by CHAIN-R; read weekly while "
125 "an agent uses it."
126 ),
127 "source_type": "web",
128 "source_config": self.source_config(),
129 }
132# ── AI & data protection law (Domain) ─────────────────────────────────────
134_LEGISLATION_TERMS = "https://www.legislation.gov.uk/help"
135_ICO_TERMS = "https://ico.org.uk/global/copyright-and-re-use-of-materials/"
136_NIST_TERMS = "https://www.nist.gov/oism/copyrights"
138LAW = BuiltInRepository(
139 repository=SystemRepositorySpec(
140 id=_id(621),
141 name="AI & data protection law",
142 description=(
143 "UK data protection law and guidance, and AI risk guidance: the UK "
144 "GDPR, the Data Protection Act 2018, ICO guidance and the NIST AI "
145 "RMF. Read weekly from their publishers."
146 ),
147 icon="Scroll",
148 accent="slate",
149 category=KnowledgeCategory.domain,
150 ),
151 sources=(
152 PageSourceSpec(
153 id=_id(631),
154 name="UK GDPR",
155 publisher="legislation.gov.uk",
156 covers=(
157 "The UK General Data Protection Regulation, its whole text as "
158 "amended, with the changes still to come into force noted."
159 ),
160 urls=("https://www.legislation.gov.uk/eur/2016/679",),
161 licence="ogl-3.0",
162 terms_url=_LEGISLATION_TERMS,
163 region="United Kingdom",
164 scope="page",
165 max_pages=1,
166 max_depth=0,
167 # legislation.gov.uk reuses its EU-derived text under the
168 # Commission's reuse decision, and asks for that to be said.
169 credit=(
170 "Contains public sector information licensed under the Open "
171 "Government Licence v3.0. Derived from EUR-Lex, © European "
172 "Union, reused under Commission Decision 2011/833/EU."
173 ),
174 ),
175 PageSourceSpec(
176 id=_id(632),
177 name="Data Protection Act 2018",
178 publisher="legislation.gov.uk",
179 covers=(
180 "The Data Protection Act 2018 as amended: Parts 1, 2, 5 and 6 "
181 "(how the UK GDPR applies, the Information Commissioner, "
182 "enforcement) and Schedules 1 and 2 (conditions for sensitive "
183 "data, and exemptions)."
184 ),
185 # The parts most organisations work under. Parts 3 and 4 (law
186 # enforcement and the intelligence services) are left out: they
187 # are over half the Act and few agents need them.
188 urls=tuple(
189 f"https://www.legislation.gov.uk/ukpga/2018/12/{part}"
190 for part in (
191 "part/1",
192 "part/2",
193 "part/5",
194 "part/6",
195 "schedule/1",
196 "schedule/2",
197 )
198 ),
199 licence="ogl-3.0",
200 terms_url=_LEGISLATION_TERMS,
201 region="United Kingdom",
202 scope="page",
203 max_pages=6,
204 max_depth=0,
205 ),
206 PageSourceSpec(
207 id=_id(633),
208 name="ICO guidance on data protection and AI",
209 publisher="the Information Commissioner's Office",
210 covers=(
211 "The UK regulator's guidance on the data protection principles, "
212 "lawful basis, individual rights, DPIAs, data security and AI "
213 "and data protection."
214 ),
215 urls=tuple(
216 "https://ico.org.uk/for-organisations/uk-gdpr-guidance-and-resources/"
217 + path
218 for path in (
219 "data-protection-principles/a-guide-to-the-data-protection-principles/",
220 "lawful-basis/a-guide-to-lawful-basis/",
221 "individual-rights/individual-rights/",
222 "accountability-and-governance/data-protection-impact-assessments-dpias/",
223 "security/a-guide-to-data-security/",
224 "artificial-intelligence/guidance-on-ai-and-data-protection/",
225 )
226 ),
227 licence="ogl-3.0",
228 terms_url=_ICO_TERMS,
229 region="United Kingdom",
230 max_pages=80,
231 # The credit in the form the ICO asks for; each passage's
232 # citation adds the page's own title and date.
233 credit=(
234 "Information Commissioner's Office, licensed under the Open "
235 "Government Licence v3.0."
236 ),
237 ),
238 PageSourceSpec(
239 id=_id(634),
240 name="NIST AI Risk Management Framework",
241 publisher="NIST",
242 covers=(
243 "The NIST AI Risk Management Framework (AI RMF 1.0) and its "
244 "Playbook: framing AI risk, trustworthy AI characteristics, and "
245 "the Govern, Map, Measure and Manage functions."
246 ),
247 urls=(
248 "https://airc.nist.gov/airmf-resources/airmf/",
249 "https://airc.nist.gov/airmf-resources/playbook/",
250 ),
251 licence="us-public-domain",
252 terms_url=_NIST_TERMS,
253 region="United States",
254 max_pages=60,
255 ),
256 ),
257 label="AI & data protection law repository",
258 source_label="publisher source",
259 switch=SWITCH,
260 topics=(
261 "gdpr",
262 "data",
263 "protection",
264 "personal",
265 "privacy",
266 "lawful",
267 "consent",
268 "dpia",
269 "ico",
270 "law",
271 "legal",
272 "regulation",
273 "processing",
274 "controller",
275 "processor",
276 "rights",
277 "rmf",
278 ),
279)
281# ── Security guidance (Domain) ─────────────────────────────────────────────
283_NCSC_TERMS = "https://www.ncsc.gov.uk/section/about-this-website/terms-and-conditions"
284# CISA was removed on 28 October 2026: cisa.gov refused every request from
285# the deployed host (403), so its three sources held nothing while they were
286# offered and suggested as if they did (``docs/guides/knowledge.md``).
288SECURITY = BuiltInRepository(
289 repository=SystemRepositorySpec(
290 id=_id(622),
291 name="Security guidance",
292 description=(
293 "Cyber security guidance from the NCSC and NIST: 10 Steps, cloud "
294 "security, secure AI development and the Cybersecurity Framework. "
295 "Read weekly."
296 ),
297 icon="Landmark",
298 accent="emerald",
299 category=KnowledgeCategory.domain,
300 ),
301 sources=(
302 PageSourceSpec(
303 id=_id(641),
304 name="NCSC guidance",
305 publisher="the National Cyber Security Centre",
306 covers=(
307 "The UK National Cyber Security Centre's guidance: 10 Steps to "
308 "Cyber Security, cloud security guidance, and the guidelines for "
309 "secure AI system development."
310 ),
311 urls=(
312 "https://www.ncsc.gov.uk/collection/10-steps",
313 "https://www.ncsc.gov.uk/collection/cloud",
314 "https://www.ncsc.gov.uk/collection/guidelines-secure-ai-system-development",
315 ),
316 licence="ogl-3.0",
317 terms_url=_NCSC_TERMS,
318 region="United Kingdom",
319 max_pages=80,
320 ),
321 PageSourceSpec(
322 id=_id(643),
323 name="NIST Cybersecurity Framework",
324 publisher="NIST",
325 covers=(
326 "The NIST Cybersecurity Framework 2.0: its functions, profiles, "
327 "quick-start guides and how organisations use it."
328 ),
329 urls=("https://www.nist.gov/cyberframework",),
330 licence="us-public-domain",
331 terms_url=_NIST_TERMS,
332 region="United States",
333 max_pages=40,
334 ),
335 ),
336 label="Security guidance repository",
337 source_label="publisher source",
338 switch=SWITCH,
339 topics=(
340 "security",
341 "cyber",
342 "cybersecurity",
343 "secure",
344 "attack",
345 "ransomware",
346 "phishing",
347 "vulnerability",
348 "cloud",
349 "encryption",
350 "breach",
351 "patch",
352 "ncsc",
353 "nist",
354 ),
355)
357# ── Regulation & security updates (Temporal) ───────────────────────────────
359_GOVUK_TERMS = "https://www.gov.uk/help/terms-conditions"
362def _feed(n: int, **fields: Any) -> NewsSourceSpec:
363 return NewsSourceSpec(id=_id(n), terms_checked=TERMS_CHECKED, **fields)
366UPDATES = BuiltInRepository(
367 repository=SystemRepositorySpec(
368 id=_id(623),
369 name="Regulation & security updates",
370 description=(
371 "Current news and advisories from UK security and technology bodies "
372 "(the NCSC, DSIT, the AI Security Institute): the last 30 days, "
373 "hourly while in use."
374 ),
375 icon="BellRing",
376 accent="amber",
377 category=KnowledgeCategory.temporal,
378 ),
379 sources=(
380 _feed(
381 651,
382 name="NCSC news",
383 publisher="the National Cyber Security Centre",
384 covers="News and alerts from the UK National Cyber Security Centre.",
385 feed_url="https://www.ncsc.gov.uk/api/1/services/v1/news-rss-feed.xml",
386 licence="ogl-3.0",
387 terms_url=_NCSC_TERMS,
388 region="United Kingdom",
389 ),
390 _feed(
391 652,
392 name="NCSC blog",
393 publisher="the National Cyber Security Centre",
394 covers="Blog posts from the UK National Cyber Security Centre's experts.",
395 feed_url="https://www.ncsc.gov.uk/api/1/services/v1/blog-post-rss-feed.xml",
396 licence="ogl-3.0",
397 terms_url=_NCSC_TERMS,
398 region="United Kingdom",
399 ),
400 _feed(
401 655,
402 name="GOV.UK: Department for Science, Innovation and Technology",
403 publisher="GOV.UK",
404 covers=(
405 "News, guidance and policy from the UK department responsible for "
406 "AI, digital and technology policy."
407 ),
408 feed_url=(
409 "https://www.gov.uk/government/organisations/"
410 "department-for-science-innovation-and-technology.atom"
411 ),
412 licence="ogl-3.0",
413 terms_url=_GOVUK_TERMS,
414 region="United Kingdom",
415 ),
416 _feed(
417 656,
418 name="GOV.UK: AI Security Institute",
419 publisher="GOV.UK",
420 covers="News and research from the UK AI Security Institute.",
421 feed_url="https://www.gov.uk/government/organisations/ai-security-institute.atom",
422 licence="ogl-3.0",
423 terms_url=_GOVUK_TERMS,
424 region="United Kingdom",
425 ),
426 ),
427 label="Regulation & security updates repository",
428 source_label="update feed",
429 switch=SWITCH,
430 topics=(
431 "latest",
432 "new",
433 "announced",
434 "advisory",
435 "advisories",
436 "alert",
437 "alerts",
438 "vulnerability",
439 "exploited",
440 "update",
441 "updates",
442 "week",
443 "today",
444 "recent",
445 ),
446)
448#: The built-ins read from publishers, beyond News, in the order pages list
449#: them: Domain first, then Temporal.
450PUBLISHED: tuple[BuiltInRepository, ...] = (LAW, SECURITY, UPDATES)
453__all__ = [
454 "LAW",
455 "PUBLISHED",
456 "PageSourceSpec",
457 "SECURITY",
458 "SWITCH",
459 "TERMS_CHECKED",
460 "UPDATES",
461]