1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Content salience for memory: a cheap, deterministic keyword score over a
//! fact/finding's raw text.
//!
//! It answers "how much signal does this string carry?" without any model call,
//! and feeds two consumers:
//! - the cognition loop's auto-promotion gate (only promote findings above a
//! floor), and
//! - the write-time admission floor (#970) that keeps low-signal facts out of a
//! capped store.
//!
//! Pure and total so it never perturbs output determinism (#498): the same text
//! always scores the same value, independent of process state.
/// Boost table: substrings that mark a finding as high-signal (errors, security,
/// failures). A base score of [`BASE`] is always granted so a plain, valid fact
/// is never zero — the floor (when enabled) is what decides admission.
const BASE: u32 = 20;
const BOOSTS: &[(&str, u32)] = &[
("error", 25),
("failed", 25),
("panic", 30),
("assert", 20),
("forbidden", 25),
("timeout", 20),
("deadlock", 25),
("security", 25),
("vuln", 25),
("e0", 15), // Rust error codes often start with E0xxx.
];
/// Score the salience of a piece of memory text. Always `>= BASE`.
#[must_use]
pub(crate) fn text_salience(text: &str) -> u32 {
let s = text.to_lowercase();
let mut score = BASE;
for (pat, b) in BOOSTS {
if s.contains(pat) {
score = score.saturating_add(*b);
}
}
score
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn plain_text_gets_base_only() {
assert_eq!(text_salience("the database is postgres"), BASE);
}
#[test]
fn high_signal_terms_boost_above_base() {
assert!(text_salience("auth failed with a security error") > BASE);
// Each distinct boost term stacks.
assert!(
text_salience("panic deadlock timeout") > text_salience("timeout"),
"multiple boosts must accumulate"
);
}
#[test]
fn is_deterministic_and_case_insensitive() {
assert_eq!(
text_salience("SECURITY VULN"),
text_salience("security vuln")
);
assert_eq!(text_salience("x"), text_salience("x"));
}
}