Coverage for app / util / knowledge_categories.py: 96%

23 statements  

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

1"""Knowledge categories: what kind of knowledge a repository holds, and so 

2how an agent uses it. 

3 

4A repository declares one category, and a source belongs to repositories of 

5one category only, so every knowledge base an agent reaches has exactly one. 

6Each is used differently when an agent answers 

7(``docs/guides/retrieval.md#retrieval-by-category``): 

8 

9- ``domain`` — specialist subject knowledge: reference documents, guidance, 

10 standards, product material. Searched for what a request is about and 

11 cited as evidence. Every repository was this before categories existed. 

12- ``temporal`` — time-sensitive information: news, updates, releases, 

13 announcements. Dated: kept to a window, newest first, searched for a 

14 request about what is current, and never remembered as an answer. 

15- ``reasoning`` — analytical and decision frameworks: methods, criteria, 

16 checklists, models for judging. Applied to a task that calls for analysis 

17 or a decision, given to the model as an approach rather than as facts. 

18 

19Other kinds of context an agent draws on belong to the features that own 

20them, never to a repository: procedures it runs are skills, what it is told 

21now is the conversation and its attachments, what it has done is its memory 

22and the decision record, what it can do is its tools, and what it may do is 

23the rules, access and its instructions. 

24 

25This vocabulary is carried by both services, byte for byte 

26(``tools/test_shared_modules.py``): svc-core stores a repository's category 

27and sends it with each knowledge base, svc-ai decides what it changes 

28(``app/knowledge/categories.py``). Neither holds a copy of the other's half. 

29""" 

30 

31from __future__ import annotations 

32 

33from enum import StrEnum 

34 

35 

36class KnowledgeCategory(StrEnum): 

37 """What kind of knowledge a repository holds.""" 

38 

39 domain = "domain" 

40 temporal = "temporal" 

41 reasoning = "reasoning" 

42 

43 

44#: A repository that names none, and a knowledge base sent without one. 

45DEFAULT = KnowledgeCategory.domain 

46 

47#: How each is named where a person chooses one. 

48LABELS: dict[str, str] = { 

49 KnowledgeCategory.domain: "Domain", 

50 KnowledgeCategory.temporal: "Temporal", 

51 KnowledgeCategory.reasoning: "Reasoning", 

52} 

53 

54#: What each holds, in a sentence. 

55PURPOSE: dict[str, str] = { 

56 KnowledgeCategory.domain: ( 

57 "Specialist subject knowledge: reference documents, guidance, " 

58 "standards and product material." 

59 ), 

60 KnowledgeCategory.temporal: ( 

61 "Time-sensitive information: news, updates, releases and " 

62 "announcements that go out of date." 

63 ), 

64 KnowledgeCategory.reasoning: ( 

65 "Analytical and decision frameworks: methods, criteria and " 

66 "checklists for judging a question." 

67 ), 

68} 

69 

70#: What an agent does with each, in a sentence. 

71USE: dict[str, str] = { 

72 KnowledgeCategory.domain: ( 

73 "Searched for what a request is about, and cited as evidence." 

74 ), 

75 KnowledgeCategory.temporal: ( 

76 "Kept to a window and read newest first, searched whenever a request " 

77 "is about what is current, and never remembered as an answer." 

78 ), 

79 KnowledgeCategory.reasoning: ( 

80 "Applied when a request calls for analysis or a decision, as the " 

81 "approach to take rather than as facts." 

82 ), 

83} 

84 

85if not set(LABELS) == set(KnowledgeCategory) == set(PURPOSE) == set(USE): 

86 raise RuntimeError("Every knowledge category needs a label, a purpose and a use") 

87 

88 

89def keys() -> tuple[str, ...]: 

90 return tuple(category.value for category in KnowledgeCategory) 

91 

92 

93def parse(value: object) -> KnowledgeCategory | None: 

94 """``value`` as a category, or None when it names none.""" 

95 try: 

96 return KnowledgeCategory(str(value)) 

97 except ValueError: 

98 return None 

99 

100 

101def of(value: object) -> KnowledgeCategory: 

102 """``value`` as a category, the default when it names none: what a 

103 knowledge base sent by an older sender, or with a value this copy does 

104 not know, is treated as.""" 

105 return parse(value) or DEFAULT 

106 

107 

108def catalogue() -> list[dict[str, str]]: 

109 """Every category, as a picker shows it.""" 

110 return [ 

111 { 

112 "key": category.value, 

113 "label": LABELS[category], 

114 "purpose": PURPOSE[category], 

115 "use": USE[category], 

116 } 

117 for category in KnowledgeCategory 

118 ]