talos-memory 0.9.2

Semantic and procedural memory storage for Talos agent runtime
Documentation
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! MEM-009 deterministic admission benchmark (I137 corrective review).
//!
//! The benchmark compares five policies on one frozen corpus and emits a
//! byte-stable JSON report. It does not mutate runtime admission behavior.

use std::collections::{BTreeMap, HashSet};

use crate::consolidation::{AdmissionReason, evaluate_admission, is_sensitive_content};

#[derive(Debug, Clone, Copy)]
struct Fixture {
    id: &'static str,
    content: &'static str,
    role: &'static str,
    expected_admit: bool,
    category: &'static str,
}

const FIXTURES: &[Fixture] = &[
    Fixture {
        id: "correction-path",
        content: "Actually, the config file is at ~/.talos/config.toml not /etc/talos.conf",
        role: "user",
        expected_admit: true,
        category: "correction",
    },
    Fixture {
        id: "correction-command",
        content: "No, use cargo fmt not rustfmt directly",
        role: "user",
        expected_admit: true,
        category: "correction",
    },
    Fixture {
        id: "preference-theme",
        content: "I prefer Nord theme",
        role: "user",
        expected_admit: true,
        category: "preference",
    },
    Fixture {
        id: "preference-tests",
        content: "Always run tests before committing",
        role: "user",
        expected_admit: true,
        category: "preference",
    },
    Fixture {
        id: "old-important-secrets",
        content: "Never commit secrets to the repository",
        role: "user",
        expected_admit: true,
        category: "old-important",
    },
    Fixture {
        id: "contradiction-sqlite",
        content: "Note: the previous statement about SQLite being required is wrong; we use bundled mode",
        role: "user",
        expected_admit: true,
        category: "contradiction",
    },
    Fixture {
        id: "validated-timeout",
        content: "Important: the fix for issue #18 was caused by missing dispatch timeout",
        role: "assistant",
        expected_admit: true,
        category: "validated",
    },
    Fixture {
        id: "recovery-retry",
        content: "Important: recovery succeeded after the bounded retry released the SQLite lock",
        role: "assistant",
        expected_admit: true,
        category: "recovery",
    },
    Fixture {
        id: "recency-conflict-endpoint",
        content: "Correction: use the v2 endpoint; the older v1 endpoint is obsolete",
        role: "user",
        expected_admit: true,
        category: "recency-conflict",
    },
    Fixture {
        id: "sensitive-api-key",
        content: "api_key = sk-ant-fixture-redacted",
        role: "user",
        expected_admit: false,
        category: "sensitive",
    },
    Fixture {
        id: "sensitive-authorization",
        content: "Authorization: Bearer fixture-redacted",
        role: "user",
        expected_admit: false,
        category: "sensitive",
    },
    Fixture {
        id: "sensitive-password",
        content: "password = fixture-redacted",
        role: "user",
        expected_admit: false,
        category: "sensitive",
    },
    Fixture {
        id: "chatter-hello",
        content: "Hello, how are you today?",
        role: "user",
        expected_admit: false,
        category: "chatter",
    },
    Fixture {
        id: "chatter-thanks",
        content: "Thanks, that's helpful",
        role: "user",
        expected_admit: false,
        category: "chatter",
    },
    Fixture {
        id: "routine-question",
        content: "Can you help me understand how the session lifecycle works? I've been reading through the code.",
        role: "user",
        expected_admit: false,
        category: "routine-length",
    },
    Fixture {
        id: "duplicate-tests",
        content: "Always run tests before committing",
        role: "user",
        expected_admit: false,
        category: "duplicate",
    },
];

#[derive(Debug, Clone, Copy)]
enum Policy {
    CurrentHeuristic,
    Recency,
    NoveltyOnly,
    UtilityOnly,
    NoveltyTimesUtility,
}

impl Policy {
    const ALL: [Self; 5] = [
        Self::CurrentHeuristic,
        Self::Recency,
        Self::NoveltyOnly,
        Self::UtilityOnly,
        Self::NoveltyTimesUtility,
    ];

    fn name(self) -> &'static str {
        match self {
            Self::CurrentHeuristic => "current_heuristic",
            Self::Recency => "recency",
            Self::NoveltyOnly => "novelty_only",
            Self::UtilityOnly => "committed_utility_only",
            Self::NoveltyTimesUtility => "novelty_times_committed_utility",
        }
    }

    fn evaluate(self, fixture: &Fixture, index: usize) -> (bool, &'static str) {
        match self {
            Self::CurrentHeuristic => {
                let lower = fixture.content.trim_start().to_lowercase();
                let marked = ["remember", "note", "important", "always", "never"]
                    .iter()
                    .any(|marker| lower.starts_with(marker));
                (
                    marked || fixture.role == "user" && fixture.content.len() > 50,
                    if marked {
                        "marker"
                    } else if fixture.role == "user" && fixture.content.len() > 50 {
                        "length"
                    } else {
                        "below_baseline"
                    },
                )
            }
            Self::Recency => {
                let recent = index >= FIXTURES.len().saturating_sub(8);
                (
                    recent && !is_sensitive_content(fixture.content),
                    if recent { "recent" } else { "stale" },
                )
            }
            Self::NoveltyOnly => {
                let lower = fixture.content.trim_start().to_lowercase();
                let novel = [
                    "actually",
                    "no,",
                    "correction",
                    "note",
                    "important",
                    "always",
                    "never",
                    "remember",
                    "prefer",
                ]
                .iter()
                .any(|marker| lower.starts_with(marker))
                    || lower.contains("i prefer");
                (
                    novel && !is_sensitive_content(fixture.content),
                    if novel { "novel_marker" } else { "low_novelty" },
                )
            }
            Self::UtilityOnly => {
                let lower = fixture.content.trim_start().to_lowercase();
                let useful = [
                    "actually",
                    "no,",
                    "correction",
                    "note",
                    "important",
                    "always",
                    "never",
                    "remember",
                    "prefer",
                    "fix for",
                    "caused by",
                    "succeeded after",
                ]
                .iter()
                .any(|marker| lower.starts_with(marker) || lower.contains(marker));
                (
                    useful && !is_sensitive_content(fixture.content),
                    if useful {
                        "utility_signal"
                    } else {
                        "low_utility"
                    },
                )
            }
            Self::NoveltyTimesUtility => {
                let decision = evaluate_admission(fixture.content, fixture.role);
                let reason = match decision.reason {
                    AdmissionReason::Admitted => "admitted",
                    AdmissionReason::SensitiveContent => "sensitive",
                    AdmissionReason::BelowThreshold => "below_threshold",
                    AdmissionReason::ExcludedRole => "excluded_role",
                    AdmissionReason::TooShort => "too_short",
                };
                (decision.admit, reason)
            }
        }
    }
}

#[derive(Debug)]
struct Metrics {
    policy: &'static str,
    tp: usize,
    fp: usize,
    tn: usize,
    fn_: usize,
    admitted_chars: usize,
    duplicate_admitted: bool,
    old_important_retained: bool,
    contradiction_correct: bool,
    failures: Vec<serde_json::Value>,
    reason_counts: BTreeMap<&'static str, usize>,
}

impl Metrics {
    fn precision(&self) -> f64 {
        (self.tp as f64) / ((self.tp + self.fp).max(1) as f64)
    }

    fn important_recall(&self) -> f64 {
        (self.tp as f64) / ((self.tp + self.fn_).max(1) as f64)
    }
}

fn evaluate_policy(policy: Policy) -> Metrics {
    let mut metrics = Metrics {
        policy: policy.name(),
        tp: 0,
        fp: 0,
        tn: 0,
        fn_: 0,
        admitted_chars: 0,
        duplicate_admitted: false,
        old_important_retained: false,
        contradiction_correct: false,
        failures: Vec::new(),
        reason_counts: BTreeMap::new(),
    };
    for (index, fixture) in FIXTURES.iter().enumerate() {
        let (actual, reason) = policy.evaluate(fixture, index);
        if actual {
            metrics.admitted_chars += fixture.content.len();
        }
        match (fixture.expected_admit, actual) {
            (true, true) => metrics.tp += 1,
            (false, true) => metrics.fp += 1,
            (false, false) => metrics.tn += 1,
            (true, false) => metrics.fn_ += 1,
        }
        if actual != fixture.expected_admit {
            metrics.failures.push(serde_json::json!({
                "fixture_id": fixture.id,
                "category": fixture.category,
                "expected": fixture.expected_admit,
                "actual": actual,
            }));
        }
        *metrics.reason_counts.entry(reason).or_default() += 1;
        if fixture.category == "duplicate" {
            metrics.duplicate_admitted = actual;
        }
        if fixture.category == "old-important" {
            metrics.old_important_retained = actual;
        }
        if fixture.category == "contradiction" {
            metrics.contradiction_correct = actual == fixture.expected_admit;
        }
    }
    metrics
}

fn report_value() -> serde_json::Value {
    let metrics = Policy::ALL.map(evaluate_policy);
    let combined = &metrics[4];
    let sensitive_rejected = FIXTURES
        .iter()
        .enumerate()
        .filter(|(_, fixture)| fixture.category == "sensitive")
        .all(|(index, fixture)| !Policy::NoveltyTimesUtility.evaluate(fixture, index).0);
    let chatter_rejected = FIXTURES
        .iter()
        .enumerate()
        .filter(|(_, fixture)| {
            fixture.category == "chatter" || fixture.category == "routine-length"
        })
        .all(|(index, fixture)| !Policy::NoveltyTimesUtility.evaluate(fixture, index).0);
    let go = combined.precision() > 0.80
        && combined.important_recall() >= 0.70
        && sensitive_rejected
        && chatter_rejected
        && !combined.duplicate_admitted
        && combined.old_important_retained
        && combined.contradiction_correct;

    serde_json::json!({
        "schema_version": 1,
        "fixtures_count": FIXTURES.len(),
        "categories": FIXTURES.iter().map(|fixture| fixture.category).collect::<HashSet<_>>().len(),
        "decision": if go { "Go" } else { "No-Go" },
        "decision_rule": "precision > 0.80; important_recall >= 0.70; zero sensitive/chatter/duplicate admissions; retain old-important; handle contradiction",
        "production_action": if go { "eligible_for_separate_implementation_review" } else { "retain_current_heuristic" },
        "sparse_reference_index": {
            "decision": "No-Go",
            "implemented": false,
            "reason": "no frozen exact-recall query corpus or material-benefit evidence; direct TLOG transcript remains canonical",
        },
        "policies": metrics.iter().map(|metric| serde_json::json!({
            "name": metric.policy,
            "precision": metric.precision(),
            "important_recall": metric.important_recall(),
            "tp": metric.tp,
            "fp": metric.fp,
            "tn": metric.tn,
            "fn": metric.fn_,
            "duplicate_admitted": metric.duplicate_admitted,
            "old_important_retained": metric.old_important_retained,
            "contradiction_correct": metric.contradiction_correct,
            "admitted_chars": metric.admitted_chars,
            "failures": metric.failures,
            "reason_counts": metric.reason_counts,
        })).collect::<Vec<_>>(),
    })
}

fn report_json() -> String {
    serde_json::to_string_pretty(&report_value()).expect("benchmark report serializes")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn fixture_corpus_covers_required_categories() {
        let categories = FIXTURES
            .iter()
            .map(|fixture| fixture.category)
            .collect::<HashSet<_>>();
        for required in [
            "correction",
            "preference",
            "routine-length",
            "duplicate",
            "validated",
            "recovery",
            "recency-conflict",
            "contradiction",
            "sensitive",
            "old-important",
        ] {
            assert!(categories.contains(required), "missing {required}");
        }
    }

    #[test]
    fn benchmark_compares_baseline_and_all_required_ablations_and_selects_no_go() {
        let report = report_value();
        assert_eq!(report["policies"].as_array().map(Vec::len), Some(5));
        assert_eq!(report["decision"], "No-Go");
        assert_eq!(report["production_action"], "retain_current_heuristic");
        assert_eq!(report["sparse_reference_index"]["decision"], "No-Go");
        let combined = &report["policies"][4];
        assert_eq!(combined["duplicate_admitted"], true);
    }

    #[test]
    fn benchmark_is_byte_stable_and_matches_checked_in_artifact() {
        let first = report_json();
        let second = report_json();
        assert_eq!(first, second);
        let artifact =
            include_str!("../../../docs/reference/MEM-009-BENCHMARK-RESULT-2026-07-17.json");
        let normalized_artifact = artifact.replace("\r\n", "\n");
        assert_eq!(first.trim(), normalized_artifact.trim());
        serde_json::from_str::<serde_json::Value>(artifact).expect("artifact is valid JSON");
    }
}