Coverage for app / services / knowledge / built_in_crawls.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-10-31 11:21 +0000

1"""A built-in website source's first crawl, started when it is granted. 

2 

3A built-in website repository (News, AI & data protection law, …) is crawled 

4only while an agent holds it (``web_crawl.system_sources_in_use``), so on a 

5deployment where nobody has granted it yet, it holds nothing. The first 

6grant used to wait for the scheduler's next ``web_knowledge_refresh`` tick — 

7up to 15 minutes — and then for the crawl itself. A scenario run's agents 

8answered every question in that window from knowledge bases with nothing 

9indexed, and the turns were filed as questions the knowledge could not 

10answer. 

11 

12:func:`start_for` is called after a grant commits, on every path that grants 

13a repository to an agent. It starts the crawl of each of the repository's 

14website sources that has never been crawled; one crawled before is left to 

15its cadence, as the scheduler keeps it. The crawl is the platform's, as a 

16scheduled one is (``Trigger.granted``). Best-effort: a grant never fails 

17because a crawl could not start. 

18""" 

19 

20from __future__ import annotations 

21 

22import logging 

23from collections.abc import Iterable 

24from uuid import UUID 

25 

26from sqlalchemy.orm import Session 

27 

28from app.db.postgres.models import Knowledge, KnowledgeCrawl 

29from app.services.knowledge import web_crawl 

30 

31logger = logging.getLogger(__name__) 

32 

33 

34def never_crawled(db: Session, repository_ids: Iterable[UUID]) -> list[Knowledge]: 

35 """The website sources of the built-in repositories among 

36 ``repository_ids`` that this deployment provides and that have never 

37 been crawled.""" 

38 from app.common.settings import get_settings 

39 from app.services.system_resources import knowledge as built_in 

40 

41 settings = get_settings() 

42 source_ids: list[UUID] = [] 

43 for repository_id in dict.fromkeys(repository_ids): 

44 entry = built_in.of_repository(repository_id) 

45 if entry is None or not entry.enabled(settings): 

46 continue 

47 source_ids.extend( 

48 source.id 

49 for source in entry.sources 

50 if built_in.crawl_enabled(source.id, settings) 

51 ) 

52 if not source_ids: 

53 return [] 

54 crawled = { 

55 row[0] 

56 for row in db.query(KnowledgeCrawl.knowledge_id) 

57 .filter(KnowledgeCrawl.knowledge_id.in_(source_ids)) 

58 .distinct() 

59 .all() 

60 } 

61 sources = ( 

62 db.query(Knowledge) 

63 .filter( 

64 Knowledge.id.in_([sid for sid in source_ids if sid not in crawled]), 

65 Knowledge.source_type == web_crawl.SOURCE_TYPE, 

66 ) 

67 .all() 

68 ) 

69 order = {sid: i for i, sid in enumerate(source_ids)} 

70 return sorted(sources, key=lambda k: order.get(k.id, 0)) 

71 

72 

73def start_for(db: Session, repository_ids: Iterable[UUID]) -> list[UUID]: 

74 """Start the first crawl of every never-crawled built-in website source 

75 of ``repository_ids``; the ids of the sources whose crawl started.""" 

76 try: 

77 sources = never_crawled(db, repository_ids) 

78 except Exception: # noqa: BLE001 — a grant never fails for a crawl 

79 logger.exception("Could not read which built-in sources need a crawl") 

80 db.rollback() 

81 return [] 

82 started: list[UUID] = [] 

83 for knowledge in sources: 

84 try: 

85 if web_crawl.start_quietly(db, knowledge, web_crawl.Trigger.granted): 

86 started.append(knowledge.id) 

87 except Exception: # noqa: BLE001 — as above 

88 logger.exception("Could not start the first crawl of %s", knowledge.id) 

89 db.rollback() 

90 if started: 

91 logger.info("Started the first crawl of %d built-in source(s)", len(started)) 

92 return started