Coverage for app / services / system_resources / news.py: 100%
41 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"""
2The built-in News repository — current news every user can grant to their
3own agents, from publishers whose licences allow it.
5Data only, like :mod:`.skillsets` and :mod:`.datasets`: :mod:`.provision`
6writes it, and the website crawler (``services/knowledge/web_crawl.py``)
7keeps it current by reading each source's RSS or Atom feed.
9**Correct use comes first, and it narrows the list.** A source is here only
10when its publisher's own terms allow what the platform does with it —
11fetch it automatically, keep it, index it in pieces, show passages to users
12of a commercial product — and the licence says so in terms, not by
13omission. Each source records the licence it is used under, the publisher's
14own reuse page, the attribution that licence asks for, and the date its
15terms were last checked. ``tests/test_news_sources.py`` holds every source
16to a licence that permits commercial reuse and to a feed on the publisher's
17own site.
19That rules out most commercial news. Their feeds are offered for personal
20use, and reusing their journalism needs a licence from the publisher. It
21also rules out several open publishers whose terms do not fit how retrieval
22works. UN News allows republishing only whole and unedited, and a retrieved
23passage is neither. The European Central Bank asks commercial re-users to
24tell every paying reader, at every access, that the material is free. The
25European Parliament ties reuse to reproducing the entire item. What
26remains is public-sector news under open government licences (UK, EU, US
27federal), plus Creative Commons journalism. Candidates that were checked and
28left out are listed in ``docs/guides/news.md`` with the reason.
30The platform's own conduct is the other half. Every fetch honours
31robots.txt. A feed is read at most hourly, and only while an agent uses the
32repository. An entry already stored is not fetched again unless the feed
33dates it anew. Pages older than the window are removed. And the attribution
34travels with every passage into its citation.
35"""
37from __future__ import annotations
39from dataclasses import dataclass
40from typing import Any
41from uuid import UUID
43from app.services.knowledge.web_config import validate_web_config
44from app.util.knowledge_categories import KnowledgeCategory
46from .definition import SystemRepositorySpec
47from .licences import LICENCES, Licence
49_ID_PREFIX = "c4a1b0e7-0000-4000-8000-"
51#: When every licence and feed below was last checked against the
52#: publisher's own terms. A release that changes a source updates it.
53TERMS_CHECKED = "2026-09-30"
55#: How long a news page is kept: a rolling window, not an archive.
56NEWS_WINDOW_DAYS = 30
57#: Entries read per crawl of one feed — feeds list their newest 10–50.
58NEWS_MAX_PAGES = 20
61@dataclass(frozen=True)
62class NewsSourceSpec:
63 """One publisher's feed, as a built-in website knowledge source."""
65 id: UUID
66 name: str
67 publisher: str
68 #: What it covers, for the source's description and the Knowledge page.
69 covers: str
70 #: The RSS or Atom feed — on the publisher's own site.
71 feed_url: str
72 licence: str
73 #: The publisher's own statement of the reuse terms.
74 terms_url: str
75 region: str
76 #: ``auto`` unless the publisher's pages cannot be read (then ``entry``).
77 feed_content: str = "auto"
78 #: When its licence and feed were last checked against the publisher's
79 #: own terms: what its trust panel shows.
80 terms_checked: str = TERMS_CHECKED
82 @property
83 def source_type(self) -> str:
84 return "web"
86 @property
87 def licence_record(self) -> Licence:
88 return LICENCES[self.licence]
90 def attribution(self) -> str:
91 return self.licence_record.credit(self.publisher)
93 def source_config(self) -> dict[str, Any]:
94 """The website source's crawl policy — validated like a user's."""
95 return validate_web_config(
96 {
97 "urls": [self.feed_url],
98 # The feed and the entries it lists — nothing else on the site.
99 "scope": "page",
100 "use_sitemap": False,
101 "follow_external": False,
102 "max_pages": NEWS_MAX_PAGES,
103 "refresh": "hourly",
104 "feed_content": self.feed_content,
105 "max_age_days": NEWS_WINDOW_DAYS,
106 "attribution": self.attribution(),
107 "licence_url": self.licence_record.url,
108 }
109 )
111 def fields(self) -> dict[str, Any]:
112 licence = self.licence_record
113 return {
114 "name": self.name,
115 "description": (
116 f"{self.covers} From {self.publisher}'s feed, used under the "
117 f"{licence.name} ({self.terms_url}). {self.attribution()} "
118 f"Maintained by CHAIN-R; the last {NEWS_WINDOW_DAYS} days are "
119 "kept."
120 ),
121 "source_type": "web",
122 "source_config": self.source_config(),
123 }
126def _source(n: int, **fields: Any) -> NewsSourceSpec:
127 return NewsSourceSpec(id=UUID(f"{_ID_PREFIX}{310 + n:012d}"), **fields)
130NEWS_SOURCES: tuple[NewsSourceSpec, ...] = (
131 # ── United Kingdom ──────────────────────────────────────────────────────
132 _source(
133 1,
134 name="GOV.UK — news and communications",
135 publisher="GOV.UK",
136 covers=(
137 "News, press releases and statements from every UK government "
138 "department and agency."
139 ),
140 feed_url="https://www.gov.uk/search/news-and-communications.atom",
141 licence="ogl-3.0",
142 terms_url="https://www.gov.uk/help/terms-conditions",
143 region="United Kingdom",
144 ),
145 _source(
146 2,
147 name="NHS England — news",
148 publisher="NHS England",
149 covers="News from NHS England on health services, policy and performance.",
150 feed_url="https://www.england.nhs.uk/feed/",
151 licence="ogl-3.0",
152 terms_url="https://www.england.nhs.uk/terms-and-conditions/",
153 region="United Kingdom",
154 ),
155 _source(
156 3,
157 name="Office for National Statistics — releases",
158 publisher="the Office for National Statistics",
159 covers="Announcements of UK official statistics as they are published.",
160 feed_url="https://www.ons.gov.uk/releasecalendar?rss",
161 licence="ogl-3.0",
162 terms_url="https://www.ons.gov.uk/help/termsandconditions",
163 region="United Kingdom",
164 ),
165 # ── European Union ──────────────────────────────────────────────────────
166 _source(
167 4,
168 name="European Commission — press releases",
169 publisher="the European Commission",
170 covers=(
171 "Press releases, statements and speeches from the European "
172 "Commission. Its pages render in the browser, so the feed's own "
173 "summary of each is kept."
174 ),
175 feed_url="https://ec.europa.eu/commission/presscorner/api/rss",
176 licence="cc-by-4.0",
177 terms_url="https://commission.europa.eu/legal-notice_en",
178 region="European Union",
179 feed_content="entry",
180 ),
181 _source(
182 5,
183 name="European Commission — digital strategy news",
184 publisher="the European Commission",
185 covers=(
186 "EU digital policy: AI regulation, data, cybersecurity, platforms "
187 "and connectivity — news, consultations and events."
188 ),
189 feed_url="https://digital-strategy.ec.europa.eu/en/rss.xml",
190 licence="cc-by-4.0",
191 terms_url="https://commission.europa.eu/legal-notice_en",
192 region="European Union",
193 ),
194 _source(
195 6,
196 name="European Data Protection Board — news",
197 publisher="the European Data Protection Board",
198 covers=(
199 "Data protection enforcement, guidelines and opinions from the EU's "
200 "data protection authorities."
201 ),
202 feed_url="https://www.edpb.europa.eu/feed/news_en",
203 licence="edpb-reuse",
204 terms_url="https://www.edpb.europa.eu/copyright_en",
205 region="European Union",
206 ),
207 # ── United States ───────────────────────────────────────────────────────
208 _source(
209 7,
210 name="Federal Reserve — press releases",
211 publisher="the Board of Governors of the Federal Reserve System",
212 covers="Monetary policy, banking supervision and regulation announcements.",
213 feed_url="https://www.federalreserve.gov/feeds/press_all.xml",
214 licence="us-public-domain",
215 terms_url="https://www.federalreserve.gov/disclaimer.htm",
216 region="United States",
217 ),
218 _source(
219 8,
220 name="NASA — news releases",
221 publisher="NASA",
222 covers="Space exploration, science missions and aeronautics.",
223 feed_url="https://www.nasa.gov/news-release/feed/",
224 licence="us-public-domain",
225 terms_url="https://www.nasa.gov/nasa-brand-center/images-and-media/",
226 region="United States",
227 ),
228 _source(
229 9,
230 name="NIH — news releases",
231 publisher="the National Institutes of Health",
232 covers="Biomedical and health research news.",
233 feed_url="https://www.nih.gov/news-releases/feed.xml",
234 licence="us-public-domain",
235 terms_url="https://www.nih.gov/web-policies-notices",
236 region="United States",
237 ),
238 _source(
239 10,
240 name="NIST — news",
241 publisher="the National Institute of Standards and Technology",
242 covers=(
243 "Standards and measurement science, including AI, cybersecurity and "
244 "privacy frameworks."
245 ),
246 feed_url="https://www.nist.gov/news-events/news/rss.xml",
247 licence="us-public-domain",
248 terms_url="https://www.nist.gov/oism/copyrights",
249 region="United States",
250 ),
251 _source(
252 11,
253 name="NOAA — news",
254 publisher="the National Oceanic and Atmospheric Administration",
255 covers=(
256 "Weather, climate, oceans and fisheries. Its pages refuse automated "
257 "readers, so the feed's own summary of each story is kept."
258 ),
259 feed_url="https://www.noaa.gov/rss.xml",
260 licence="us-public-domain",
261 terms_url=(
262 "https://library.noaa.gov/blogs/news-research-highlights/"
263 "question-of-the-quarter-noaacopyright"
264 ),
265 region="United States",
266 feed_content="entry",
267 ),
268 # ── International ───────────────────────────────────────────────────────
269 _source(
270 12,
271 name="Global Voices",
272 publisher="Global Voices",
273 covers=(
274 "Citizen and independent journalism from around the world, "
275 "especially regions and languages mainstream media under-cover."
276 ),
277 feed_url="https://globalvoices.org/feed/",
278 licence="cc-by-3.0",
279 terms_url="https://globalvoices.org/about/global-voices-attribution-policy/",
280 region="International",
281 ),
282)
284NEWS_REPOSITORY = SystemRepositorySpec(
285 id=UUID(f"{_ID_PREFIX}000000000301"),
286 name="News",
287 description=(
288 "Current news from openly licensed publishers — UK, EU and US public "
289 "bodies and independent journalism — kept to the last "
290 f"{NEWS_WINDOW_DAYS} days and refreshed hourly while an agent uses it. "
291 "Every passage is credited as its publisher's licence asks. Maintained "
292 "by CHAIN-R; grant it to an agent to ground answers in recent events."
293 ),
294 icon="Globe",
295 accent="sky",
296 # Dated, kept to a window: read newest first, searched for a request
297 # about what is current, never remembered as an answer.
298 category=KnowledgeCategory.temporal,
299)