Picks the top-`k` digest facts from a trust-filtered candidate list: secret-like facts are dropped, then one highest-trust fact per category is taken first (category diversity), then remaining slots fill by trust with newest-first tiebreak.
(mut facts: Vec<FactRecord>, k: usize)
| 132 | /// taken first (category diversity), then remaining slots fill by trust with |
| 133 | /// newest-first tiebreak. |
| 134 | pub fn select_digest_facts(mut facts: Vec<FactRecord>, k: usize) -> Vec<FactRecord> { |
| 135 | facts.retain(injectable); |
| 136 | facts.sort_by(|left, right| { |
| 137 | right |
| 138 | .trust_score |
| 139 | .total_cmp(&left.trust_score) |
| 140 | .then_with(|| right.updated_at.cmp(&left.updated_at)) |
| 141 | .then_with(|| right.fact_id.cmp(&left.fact_id)) |
| 142 | }); |
| 143 | let mut selected: Vec<FactRecord> = Vec::with_capacity(k.min(facts.len())); |
| 144 | let mut seen_categories = HashSet::new(); |
| 145 | let mut deferred: Vec<FactRecord> = Vec::new(); |
| 146 | for fact in facts { |
| 147 | if selected.len() >= k { |
| 148 | break; |
| 149 | } |
| 150 | if seen_categories.insert(fact.category) { |
| 151 | selected.push(fact); |
| 152 | } else { |
| 153 | deferred.push(fact); |
| 154 | } |
| 155 | } |
| 156 | for fact in deferred { |
| 157 | if selected.len() >= k { |
| 158 | break; |
| 159 | } |
| 160 | selected.push(fact); |
| 161 | } |
| 162 | // Deterministic render order: trust desc, newest first. |
| 163 | selected.sort_by(|left, right| { |
| 164 | right |
| 165 | .trust_score |
| 166 | .total_cmp(&left.trust_score) |
| 167 | .then_with(|| right.updated_at.cmp(&left.updated_at)) |
| 168 | .then_with(|| right.fact_id.cmp(&left.fact_id)) |
| 169 | }); |
| 170 | selected |
| 171 | } |
| 172 | |
| 173 | /// Filters prompt-recall search results down to facts that are actually |
| 174 | /// relevant to the prompt (lexical FTS match or meaningful token overlap and a |