Coverage for app / services / knowledge / recommendations / use.py: 98%

61 statements  

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

1"""Recommendations from how knowledge is used: what the retrieval record 

2says reached answers, what the citations record says they cited, and what 

3is granted to whom. 

4""" 

5 

6from __future__ import annotations 

7 

8from dataclasses import replace 

9 

10from app.services.knowledge.recommendations.base import ( 

11 Action, 

12 Figure, 

13 Flow, 

14 Prefill, 

15 Recommendation, 

16 RecommendationKind, 

17 Section, 

18 Subject, 

19 SubjectKind, 

20 Touches, 

21 band, 

22 fingerprint, 

23 plural, 

24 register_kind, 

25) 

26from app.services.knowledge.recommendations.context import ( 

27 RECENT_DAYS, 

28 USE_DAYS, 

29 RecommendationContext, 

30) 

31 

32#: Answers a source's passages reached before its citation rate means much. 

33MIN_RETRIEVED = 20 

34#: Below this share of those answers citing it, it is rarely cited. 

35RARELY_CITED = 0.10 

36#: Answers its agents searched their knowledge in before "never used" holds. 

37MIN_SEARCHED = 20 

38 

39 

40@register_kind 

41class RetrievedNotCited(RecommendationKind): 

42 key = "retrieved_not_cited" 

43 section = Section.use 

44 label = "Given to answers, rarely cited" 

45 description = ( 

46 "A source's passages reach many answers and few cite it: it may be " 

47 "near what agents are asked without being what they need." 

48 ) 

49 

50 def find(self, ctx: RecommendationContext) -> list[Recommendation]: 

51 out = [] 

52 for knowledge_id, source in ctx.sources.items(): 

53 retrieved = ctx.retrieved.get(knowledge_id, 0) 

54 if retrieved < MIN_RETRIEVED: 

55 continue 

56 cited = ctx.cited.get(knowledge_id, 0) 

57 if cited >= RARELY_CITED * retrieved: 

58 continue 

59 ref = Prefill(knowledge_id=knowledge_id, knowledge_name=source.name) 

60 out.append( 

61 self.recommendation( 

62 subject=Subject( 

63 SubjectKind.knowledge, str(knowledge_id), source.name 

64 ), 

65 title=f"{source.name} is given to answers and rarely cited", 

66 why=( 

67 f"In the last {RECENT_DAYS} days its passages went to " 

68 f"{plural(retrieved, 'answer')} and {cited:,} cited it. What " 

69 "it holds may be near the questions without answering them, " 

70 "and every passage is read at a cost." 

71 ), 

72 figures=( 

73 Figure("Answers given its passages", retrieved), 

74 Figure("Answers citing it", cited), 

75 ), 

76 examples=(), 

77 actions=( 

78 Action( 

79 Flow.open_source, 

80 "See what its answers asked", 

81 replace(ref, tab="usage"), 

82 ), 

83 Action( 

84 Flow.open_source, 

85 "Review what it holds", 

86 replace(ref, tab="content"), 

87 ), 

88 ), 

89 fingerprint=fingerprint(band(retrieved), band(cited)), 

90 touches=Touches( 

91 sources=frozenset({knowledge_id}), 

92 repositories=ctx.holders(knowledge_id), 

93 ), 

94 weight=retrieved - cited, 

95 ) 

96 ) 

97 return out 

98 

99 

100@register_kind 

101class GrantedNotUsed(RecommendationKind): 

102 key = "granted_not_used" 

103 section = Section.use 

104 label = "Granted, and never given to an answer" 

105 description = ( 

106 "A repository's agents searched their knowledge many times and were " 

107 "never given a passage from it." 

108 ) 

109 

110 def find(self, ctx: RecommendationContext) -> list[Recommendation]: 

111 out = [] 

112 for repository_id, repository in ctx.repositories.items(): 

113 members = ctx.members.get(repository_id, []) 

114 agents = ctx.granted_to.get(repository_id, set()) 

115 if not members or not agents: 

116 continue 

117 searched = sum(ctx.searched_turns.get(a, 0) for a in agents) 

118 if searched < MIN_SEARCHED: 

119 continue 

120 if any((a, k) in ctx.kept_pairs for a in agents for k in members): 

121 continue 

122 ref = Prefill(repository_id=repository_id, repository_name=repository.name) 

123 out.append( 

124 self.recommendation( 

125 subject=Subject( 

126 SubjectKind.repository, str(repository_id), repository.name 

127 ), 

128 title=f"{repository.name} has given its agents nothing", 

129 why=( 

130 f"Its {plural(len(agents), 'agent')} searched their " 

131 f"knowledge in {plural(searched, 'answer')} in the last " 

132 f"{USE_DAYS} days and were never given a passage from it." 

133 ), 

134 figures=( 

135 Figure("Agents granted it", len(agents)), 

136 Figure("Answers that searched", searched), 

137 Figure("Answers given its passages", 0), 

138 ), 

139 examples=(), 

140 actions=( 

141 Action( 

142 Flow.open_repository, 

143 "Review who it is granted to", 

144 replace(ref, tab="agents"), 

145 ), 

146 Action( 

147 Flow.open_repository, 

148 "Review what it holds", 

149 replace(ref, tab="sources"), 

150 ), 

151 ), 

152 fingerprint=fingerprint(band(searched), len(agents)), 

153 touches=Touches( 

154 repositories=frozenset({repository_id}), 

155 agents=frozenset(agents), 

156 sources=frozenset(members), 

157 ), 

158 weight=searched, 

159 ) 

160 ) 

161 return out 

162 

163 

164@register_kind 

165class ReachesNoAgent(RecommendationKind): 

166 key = "reaches_no_agent" 

167 section = Section.use 

168 label = "Reaches no agent" 

169 description = "A repository no agent here is granted: nothing it holds is searched." 

170 

171 def find(self, ctx: RecommendationContext) -> list[Recommendation]: 

172 out = [] 

173 for repository_id, repository in ctx.repositories.items(): 

174 members = ctx.members.get(repository_id, []) 

175 if not members or ctx.granted_to.get(repository_id): 

176 continue 

177 out.append( 

178 self.recommendation( 

179 subject=Subject( 

180 SubjectKind.repository, str(repository_id), repository.name 

181 ), 

182 title=f"{repository.name} reaches no agent", 

183 why=( 

184 f"It holds {plural(len(members), 'source')} and no agent here " 

185 "is granted it, so nothing in it is searched." 

186 ), 

187 figures=(Figure("Sources", len(members)),), 

188 examples=(), 

189 actions=( 

190 Action( 

191 Flow.open_repository, 

192 "Use it in your agents", 

193 Prefill( 

194 repository_id=repository_id, 

195 repository_name=repository.name, 

196 tab="agents", 

197 ), 

198 ), 

199 ), 

200 fingerprint=fingerprint(band(len(members))), 

201 touches=Touches( 

202 repositories=frozenset({repository_id}), 

203 sources=frozenset(members), 

204 ), 

205 weight=len(members), 

206 ) 

207 ) 

208 return out 

209 

210 

211__all__ = ["GrantedNotUsed", "ReachesNoAgent", "RetrievedNotCited"]