lean_ctx/core/knowledge/
ranking.rs1use chrono::Utc;
2
3use super::types::{JudgedPair, KnowledgeFact, KnowledgeIndex};
4
5pub(super) fn confidence_stars(confidence: f32) -> &'static str {
6 if confidence >= 0.95 {
7 "★★★★★"
8 } else if confidence >= 0.85 {
9 "★★★★"
10 } else if confidence >= 0.7 {
11 "★★★"
12 } else if confidence >= 0.5 {
13 "★★"
14 } else {
15 "★"
16 }
17}
18
19pub(super) fn string_similarity(a: &str, b: &str) -> f32 {
20 let a_lower = a.to_lowercase();
21 let b_lower = b.to_lowercase();
22 let a_words: std::collections::HashSet<&str> = a_lower.split_whitespace().collect();
23 let b_words: std::collections::HashSet<&str> = b_lower.split_whitespace().collect();
24
25 if a_words.is_empty() && b_words.is_empty() {
26 return 1.0;
27 }
28
29 let intersection = a_words.intersection(&b_words).count();
30 let union = a_words.union(&b_words).count();
31
32 if union == 0 {
33 return 0.0;
34 }
35
36 intersection as f32 / union as f32
37}
38
39pub(crate) fn sort_fact_for_output(a: &KnowledgeFact, b: &KnowledgeFact) -> std::cmp::Ordering {
40 salience_score(b)
47 .cmp(&salience_score(a))
48 .then_with(|| {
49 b.quality_score()
50 .partial_cmp(&a.quality_score())
51 .unwrap_or(std::cmp::Ordering::Equal)
52 })
53 .then_with(|| {
54 b.confidence
55 .partial_cmp(&a.confidence)
56 .unwrap_or(std::cmp::Ordering::Equal)
57 })
58 .then_with(|| b.confirmation_count.cmp(&a.confirmation_count))
59 .then_with(|| b.retrieval_count.cmp(&a.retrieval_count))
60 .then_with(|| b.last_retrieved.cmp(&a.last_retrieved))
61 .then_with(|| b.last_confirmed.cmp(&a.last_confirmed))
62 .then_with(|| a.category.cmp(&b.category))
63 .then_with(|| a.key.cmp(&b.key))
64 .then_with(|| a.value.cmp(&b.value))
65}
66
67fn salience_score(f: &KnowledgeFact) -> u32 {
75 let cat = f.category.to_lowercase();
76 let base: u32 = match cat.as_str() {
77 "decision" => 70,
78 "gotcha" => 75,
79 "architecture" | "arch" => 60,
80 "security" => 65,
81 "testing" | "tests" | "deployment" | "deploy" => 55,
82 "conventions" | "convention" => 45,
83 "finding" => 40,
84 _ => 30,
85 };
86
87 let quality_bonus = (f.quality_score() * 60.0) as u32;
88
89 let recency_bonus = f.last_retrieved.map_or(0u32, |t| {
90 let days = Utc::now().signed_duration_since(t).num_days();
91 if days <= 7 {
92 10u32
93 } else if days <= 30 {
94 5u32
95 } else {
96 0u32
97 }
98 });
99
100 let archetype_bonus = f.archetype.salience_bonus();
101
102 let fidelity_bonus = f
103 .fidelity
104 .as_ref()
105 .map_or(0u32, |fi| (fi.structural * 10.0) as u32);
106
107 base + quality_bonus + recency_bonus + archetype_bonus + fidelity_bonus
108}
109
110pub(super) fn hash_project_root(root: &str) -> String {
111 crate::core::project_hash::hash_project_root(root)
112}
113
114pub(super) fn tokenize_lower(s: &str) -> impl Iterator<Item = String> + '_ {
115 s.to_lowercase()
116 .split(|c: char| c.is_whitespace() || c == '-' || c == '_' || c == '/' || c == '.')
117 .filter(|t| !t.is_empty())
118 .map(String::from)
119 .collect::<Vec<_>>()
120 .into_iter()
121}
122
123pub(super) fn build_knowledge_index(facts: &[KnowledgeFact]) -> KnowledgeIndex {
124 let mut index = KnowledgeIndex::default();
125 for (i, f) in facts.iter().enumerate() {
126 for token in tokenize_lower(&f.category) {
127 index.token_positions.entry(token).or_default().push(i);
128 }
129 for token in tokenize_lower(&f.key) {
130 index.token_positions.entry(token).or_default().push(i);
131 }
132 for token in tokenize_lower(&f.value) {
133 index.token_positions.entry(token).or_default().push(i);
134 }
135 for token in tokenize_lower(&f.source_session) {
136 index
137 .session_token_positions
138 .entry(token)
139 .or_default()
140 .push(i);
141 }
142 index
143 .category_positions
144 .entry(f.category.clone())
145 .or_default()
146 .push(i);
147 }
148 for indices in index.token_positions.values_mut() {
149 indices.sort_unstable();
150 indices.dedup();
151 }
152 for indices in index.session_token_positions.values_mut() {
153 indices.sort_unstable();
154 indices.dedup();
155 }
156 index
157}
158
159#[derive(Debug, Clone)]
160pub struct SimilarFact {
161 pub category: String,
162 pub key: String,
163 pub value_preview: String,
164 pub similarity: f32,
165}
166
167pub fn find_cross_key_similar(
168 new_category: &str,
169 new_key: &str,
170 new_value: &str,
171 all_facts: &[KnowledgeFact],
172 judged_pairs: &[JudgedPair],
173 limit: usize,
174) -> Vec<SimilarFact> {
175 let composite_key = format!("{new_category}/{new_key}");
176 let mut results: Vec<SimilarFact> = Vec::new();
177
178 for f in all_facts {
179 if !f.is_current() {
180 continue;
181 }
182 let other_key = format!("{}/{}", f.category, f.key);
183 if other_key == composite_key {
184 continue;
185 }
186
187 let already_judged = judged_pairs.iter().any(|jp| {
188 (jp.key_a == composite_key && jp.key_b == other_key)
189 || (jp.key_a == other_key && jp.key_b == composite_key)
190 });
191 if already_judged {
192 continue;
193 }
194
195 let sim = string_similarity(new_value, &f.value);
196 if sim > 0.35 {
197 let preview = if f.value.len() > 60 {
198 format!("{}...", &f.value[..57])
199 } else {
200 f.value.clone()
201 };
202 results.push(SimilarFact {
203 category: f.category.clone(),
204 key: f.key.clone(),
205 value_preview: preview,
206 similarity: sim,
207 });
208 }
209 }
210
211 results.sort_by(|a, b| {
212 b.similarity
213 .partial_cmp(&a.similarity)
214 .unwrap_or(std::cmp::Ordering::Equal)
215 });
216 results.truncate(limit);
217 results
218}
219
220pub(super) fn fact_version_id_v1(f: &KnowledgeFact) -> String {
221 use md5::{Digest, Md5};
222 let mut hasher = Md5::new();
223 hasher.update(f.category.as_bytes());
224 hasher.update(b"\n");
225 hasher.update(f.key.as_bytes());
226 hasher.update(b"\n");
227 hasher.update(f.value.as_bytes());
228 hasher.update(b"\n");
229 hasher.update(f.source_session.as_bytes());
230 hasher.update(b"\n");
231 hasher.update(f.created_at.to_rfc3339().as_bytes());
232 crate::core::agent_identity::hex_encode(&hasher.finalize())
233}