opencrabs 0.5.3

The autonomous, self-improving AI agent. Single Rust binary. Every channel. Recommended: the 40MB prebuilt binary for macOS, Linux and Windows: https://github.com/adolfousier/opencrabs/releases
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
//! Epistemic confidence tracking.
//!
//! Moved out of `src/brain/tools/epistemic.rs`: tests live under `src/tests/`,
//! never inline beside the logic they exercise (#1076).

use crate::brain::tools::epistemic::*;
use chrono::Utc;

#[test]
fn test_confidence_ordering() {
    assert!(Confidence::Verified > Confidence::Inferred);
    assert!(Confidence::Inferred > Confidence::Uncertain);
    assert!(Confidence::Uncertain > Confidence::Contradicted);
}

#[test]
fn test_confidence_decay() {
    assert_eq!(Confidence::Verified.decay(), Confidence::Verified);
    assert_eq!(Confidence::Inferred.decay(), Confidence::Uncertain);
    assert_eq!(Confidence::Uncertain.decay(), Confidence::Contradicted);
    assert_eq!(Confidence::Contradicted.decay(), Confidence::Contradicted);
}

#[test]
fn test_add_belief_no_contradiction() {
    let mut store = EpistemicStore::new();
    let result = store.add_belief("test:key", "value1", Confidence::Verified, "test");
    assert_eq!(result, ContradictionResult::NoContradiction);
    assert_eq!(store.get_belief("test:key").unwrap().value, "value1");
}

#[test]
fn test_add_belief_contradiction() {
    let mut store = EpistemicStore::new();
    store.add_belief("test:key", "value1", Confidence::Verified, "test");
    let result = store.add_belief("test:key", "value2", Confidence::Inferred, "test2");

    assert!(matches!(result, ContradictionResult::Contradicted { .. }));

    // Old belief should be marked as contradicted
    let contradicted: Vec<_> = store.list_contradictions();
    assert_eq!(contradicted.len(), 1);
    assert_eq!(contradicted[0].value, "value1");

    // New belief should be active
    assert_eq!(store.get_belief("test:key").unwrap().value, "value2");
}

#[test]
fn test_verify_belief() {
    let mut store = EpistemicStore::new();
    store.add_belief("test:key", "value", Confidence::Uncertain, "test");
    assert!(store.verify_belief("test:key"));
    assert_eq!(
        store.get_belief("test:key").unwrap().confidence,
        Confidence::Verified
    );
}

#[test]
fn test_decay_logic() {
    let mut store = EpistemicStore::new();

    // Add a belief with old last_verified
    let mut belief = Belief {
        key: "test:old".to_string(),
        value: "old_value".to_string(),
        confidence: Confidence::Inferred,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(45),
            last_verified: Utc::now() - chrono::Duration::days(45),
        },
        notes: None,
        hits: 0,
        last_used: None,
    };
    store.beliefs.insert("test:old".to_string(), belief.clone());

    // Add a recent belief
    belief.key = "test:recent".to_string();
    belief.confidence = Confidence::Inferred;
    belief.source.recorded_at = Utc::now();
    belief.source.last_verified = Utc::now();
    store.beliefs.insert("test:recent".to_string(), belief);

    // Apply decay with 30-day threshold
    let decayed = store.apply_decay(30);

    // Only the old belief should decay
    assert_eq!(decayed.len(), 1);
    assert!(decayed[0].contains("test:old"));
    assert_eq!(
        store.get_belief("test:old").unwrap().confidence,
        Confidence::Uncertain
    );
    assert_eq!(
        store.get_belief("test:recent").unwrap().confidence,
        Confidence::Inferred
    );
}

#[test]
fn test_verified_beliefs_dont_decay() {
    let mut store = EpistemicStore::new();

    let belief = Belief {
        key: "test:verified".to_string(),
        value: "verified_value".to_string(),
        confidence: Confidence::Verified,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(100),
            last_verified: Utc::now() - chrono::Duration::days(100),
        },
        notes: None,
        hits: 0,
        last_used: None,
    };
    store.beliefs.insert("test:verified".to_string(), belief);

    let decayed = store.apply_decay(30);
    assert!(decayed.is_empty());
    assert_eq!(
        store.get_belief("test:verified").unwrap().confidence,
        Confidence::Verified
    );
}

#[test]
fn test_serialization_roundtrip() {
    let mut store = EpistemicStore::new();
    store.add_belief("test:key", "value", Confidence::Inferred, "test:origin");

    let toml_str = toml::to_string_pretty(&store).unwrap();
    let loaded: EpistemicStore = toml::from_str(&toml_str).unwrap();

    assert_eq!(loaded.get_belief("test:key").unwrap().value, "value");
    assert_eq!(
        loaded.get_belief("test:key").unwrap().confidence,
        Confidence::Inferred
    );
}

#[test]
fn test_list_by_key_prefix() {
    let mut store = EpistemicStore::new();
    store.add_belief(
        "plan:task:1:abc",
        "failed",
        Confidence::Contradicted,
        "test",
    );
    store.add_belief("plan:task:2:def", "done", Confidence::Verified, "test");
    store.add_belief(
        "memory:truelens:ip",
        "1.2.3.4",
        Confidence::Inferred,
        "test",
    );

    let plan_beliefs = store.list_by_key_prefix("plan:task:");
    assert_eq!(plan_beliefs.len(), 2);

    let memory_beliefs = store.list_by_key_prefix("memory:");
    assert_eq!(memory_beliefs.len(), 1);
    assert_eq!(memory_beliefs[0].key, "memory:truelens:ip");

    let empty = store.list_by_key_prefix("nonexistent:");
    assert!(empty.is_empty());
}

/// #1641: touch_belief increments hits and sets last_used.
#[test]
fn test_touch_belief() {
    let mut store = EpistemicStore::new();
    store.add_belief("test:key", "value", Confidence::Inferred, "test");

    let belief = store.get_belief("test:key").unwrap();
    assert_eq!(belief.hits, 0);
    assert!(belief.last_used.is_none());

    // First touch
    assert!(store.touch_belief("test:key"));
    let belief = store.get_belief("test:key").unwrap();
    assert_eq!(belief.hits, 1);
    assert!(belief.last_used.is_some());

    // Second touch
    assert!(store.touch_belief("test:key"));
    let belief = store.get_belief("test:key").unwrap();
    assert_eq!(belief.hits, 2);

    // Non-existent key
    assert!(!store.touch_belief("nonexistent"));
}

/// #1641: recently-used beliefs don't decay even if never re-verified.
/// The decay clock uses last_used (falling back to recorded_at), not
/// last_verified.
#[test]
fn test_decay_uses_last_used_not_last_verified() {
    let mut store = EpistemicStore::new();

    // Old belief that was recently touched — should NOT decay
    let mut belief = Belief {
        key: "test:old_but_used".to_string(),
        value: "still_valid".to_string(),
        confidence: Confidence::Inferred,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(60),
            last_verified: Utc::now() - chrono::Duration::days(60),
        },
        notes: None,
        hits: 5,
        last_used: Some(Utc::now() - chrono::Duration::days(2)),
    };
    store
        .beliefs
        .insert("test:old_but_used".to_string(), belief.clone());

    // Old belief never touched — SHOULD decay
    belief.key = "test:old_unused".to_string();
    belief.value = "stale".to_string();
    belief.hits = 0;
    belief.last_used = None;
    store.beliefs.insert("test:old_unused".to_string(), belief);

    let decayed = store.apply_decay(30);

    // Only the unused one decayed
    assert_eq!(decayed.len(), 1);
    assert!(decayed[0].contains("test:old_unused"));

    // The recently-used one is still Inferred
    assert_eq!(
        store.get_belief("test:old_but_used").unwrap().confidence,
        Confidence::Inferred
    );

    // The unused one dropped to Uncertain
    assert_eq!(
        store.get_belief("test:old_unused").unwrap().confidence,
        Confidence::Uncertain
    );
}

/// #1641: backfill indexes MEMORY.md sections into section-anchored beliefs.
#[test]
fn test_backfill_from_content() {
    let mut store = EpistemicStore::new();

    let markdown = r#"# MEMORY.md - Long-Term Memory

Some preamble text that should be skipped.

## Rules

- NEVER push without explicit user approval
- Use cargo clippy --all-features, NEVER cargo check

## Integrations

- Telegram bot connected via @opencrabs_bot
- Discord server linked

## Short

tiny

## Another Rule

This section has enough content to be indexed as a belief.
It contains important operational context for the agent.
"#;

    let added = store.backfill_from_content(markdown);

    // Preamble (no heading) and "Short" (body < 20 chars) are skipped
    assert_eq!(added, 3);

    // Section-anchored keys
    assert!(store.get_belief("MEMORY.md##Rules").is_some());
    assert!(store.get_belief("MEMORY.md##Integrations").is_some());
    assert!(store.get_belief("MEMORY.md##Another Rule").is_some());

    // Skipped sections
    assert!(store.get_belief("MEMORY.md##Short").is_none());

    // Beliefs have correct confidence and zero hits
    let rules = store.get_belief("MEMORY.md##Rules").unwrap();
    assert_eq!(rules.confidence, Confidence::Inferred);
    assert_eq!(rules.hits, 0);
    assert!(rules.last_used.is_none());
}

/// #1641: backfill does not overwrite existing beliefs.
#[test]
fn test_backfill_no_overwrite() {
    let mut store = EpistemicStore::new();

    // Pre-existing verified belief
    store.add_belief(
        "MEMORY.md##Rules",
        "custom value",
        Confidence::Verified,
        "user",
    );

    let markdown = r#"## Rules

- Some rule that would conflict with the existing belief

## New Section

This is a brand new section with enough content to be indexed.
"#;

    let added = store.backfill_from_content(markdown);

    // Only "New Section" was added; "Rules" was skipped (already exists)
    assert_eq!(added, 1);

    // Original belief preserved
    let rules = store.get_belief("MEMORY.md##Rules").unwrap();
    assert_eq!(rules.value, "custom value");
    assert_eq!(rules.confidence, Confidence::Verified);
}

/// #1641: cold facts (0 hits, 90+ days) are deleted.
#[test]
fn test_delete_cold_beliefs() {
    let mut store = EpistemicStore::new();

    // Cold: 0 hits, 100 days old
    let cold = Belief {
        key: "test:cold".to_string(),
        value: "unused".to_string(),
        confidence: Confidence::Inferred,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(100),
            last_verified: Utc::now() - chrono::Duration::days(100),
        },
        notes: None,
        hits: 0,
        last_used: None,
    };
    store.beliefs.insert("test:cold".to_string(), cold);

    // Warm: has hits, old
    let warm = Belief {
        key: "test:warm".to_string(),
        value: "used".to_string(),
        confidence: Confidence::Inferred,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(100),
            last_verified: Utc::now() - chrono::Duration::days(100),
        },
        notes: None,
        hits: 3,
        last_used: Some(Utc::now() - chrono::Duration::days(100)),
    };
    store.beliefs.insert("test:warm".to_string(), warm);

    // Recent: 0 hits, but only 10 days old
    let recent = Belief {
        key: "test:recent".to_string(),
        value: "new".to_string(),
        confidence: Confidence::Inferred,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(10),
            last_verified: Utc::now() - chrono::Duration::days(10),
        },
        notes: None,
        hits: 0,
        last_used: None,
    };
    store.beliefs.insert("test:recent".to_string(), recent);

    let deleted = store.delete_cold_beliefs(90);

    assert_eq!(deleted.len(), 1);
    assert_eq!(deleted[0], "test:cold");
    assert!(store.get_belief("test:cold").is_none());
    assert!(store.get_belief("test:warm").is_some());
    assert!(store.get_belief("test:recent").is_some());
}

/// #1641: list_cold_beliefs returns candidates without deleting.
#[test]
fn test_list_cold_beliefs_dry_run() {
    let mut store = EpistemicStore::new();

    let cold = Belief {
        key: "test:cold".to_string(),
        value: "unused".to_string(),
        confidence: Confidence::Inferred,
        source: Source {
            origin: "test".to_string(),
            recorded_at: Utc::now() - chrono::Duration::days(100),
            last_verified: Utc::now() - chrono::Duration::days(100),
        },
        notes: None,
        hits: 0,
        last_used: None,
    };
    store.beliefs.insert("test:cold".to_string(), cold);

    let candidates = store.list_cold_beliefs(90);
    assert_eq!(candidates.len(), 1);
    assert_eq!(candidates[0].key, "test:cold");

    // Still exists — dry run didn't delete
    assert!(store.get_belief("test:cold").is_some());
}