vipune 0.20.1

A minimal memory layer for AI agents
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
//! Integration tests for `vipune prune` (issue #194, sub-issue 5).
//!
//! These tests exercise the handler end-to-end (DB open → select → demote →
//! count rows) against a real SQLite database, verifying the never-deletes
//! invariant, the hard exclusions (guard-type, high/critical importance), and
//! the count/age threshold boundaries.
//!
//! The unit tests in `src/commands/prune.rs` cover the evaluator in isolation;
//! this file covers the handler wiring (CLI args → evaluator → response).

use crate::commands::prune::{
    DEFAULT_PRUNE_AGE_DAYS, DEFAULT_PRUNE_COUNT, PruneEvaluator, PruneResponse,
};
use crate::memory::crud::test_fake_embedder;
use crate::sqlite::Database;

fn create_test_db() -> (tempfile::TempDir, std::path::PathBuf) {
    let dir = tempfile::TempDir::new().unwrap();
    let path = dir.path().join("test.db");
    Database::open(&path).unwrap();
    (dir, path)
}

fn count_rows(db: &Database) -> usize {
    db.conn()
        .query_row("SELECT COUNT(*) FROM memories", [], |row| {
            row.get::<_, i64>(0)
        })
        .unwrap() as usize
}

fn status_of(db: &Database, id: &str) -> String {
    db.conn()
        .query_row("SELECT status FROM memories WHERE id = ?", [id], |row| {
            row.get::<_, String>(0)
        })
        .unwrap()
}

fn seed(
    db: &Database,
    project_id: &str,
    content: &str,
    created_at: &str,
    memory_type: &str,
    status: &str,
) -> String {
    let emb = test_fake_embedder(content).unwrap();
    db.insert_with_time(
        project_id,
        content,
        &emb,
        None,
        created_at,
        created_at,
        memory_type,
        status,
    )
    .unwrap()
}

fn set_retrieval(db: &Database, id: &str, count: i64) {
    db.conn()
        .execute(
            "UPDATE memories SET retrieval_count = ? WHERE id = ?",
            (count, id),
        )
        .unwrap();
}

const OLD: &str = "2020-01-01T00:00:00Z";

#[test]
fn test_prune_handler_end_to_end() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();

    // Seed: 2 eligible candidates + 1 guard (excluded) + 1 active (not candidate).
    let e1 = seed(&db, "p", "eligible one", OLD, "fact", "candidate");
    set_retrieval(&db, &e1, 0);
    let e2 = seed(&db, "p", "eligible two", OLD, "preference", "candidate");
    set_retrieval(&db, &e2, 1);
    let guard = seed(&db, "p", "guard row", OLD, "guard", "candidate");
    set_retrieval(&db, &guard, 0);
    let active = seed(&db, "p", "active row", OLD, "fact", "active");
    set_retrieval(&db, &active, 0);

    let before = count_rows(&db);
    assert_eq!(before, 4);

    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    let after = count_rows(&db);
    assert_eq!(
        before, after,
        "prune must never delete: row count must be unchanged"
    );
    assert_eq!(demoted, vec![e1.clone(), e2.clone()]);
    assert_eq!(status_of(&db, &e1), "deprecated");
    assert_eq!(status_of(&db, &e2), "deprecated");
    assert_eq!(
        status_of(&db, &guard),
        "candidate",
        "guard-type must never be demoted"
    );
    assert_eq!(status_of(&db, &active), "active");
}

#[test]
fn test_prune_handler_no_eligible_rows_is_noop() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();

    // All active (not candidate) → nothing to demote.
    let a = seed(&db, "p", "active one", OLD, "fact", "active");
    set_retrieval(&db, &a, 0);
    let b = seed(&db, "p", "active two", OLD, "fact", "active");
    set_retrieval(&db, &b, 0);

    let before = count_rows(&db);
    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    let after = count_rows(&db);
    assert_eq!(before, after);
    assert!(demoted.is_empty());
    assert_eq!(status_of(&db, &a), "active");
    assert_eq!(status_of(&db, &b), "active");
}

#[test]
fn test_prune_handler_multiple_projects() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();

    let a = seed(&db, "a", "stale a", OLD, "fact", "candidate");
    set_retrieval(&db, &a, 0);
    let b = seed(&db, "b", "stale b", OLD, "fact", "candidate");
    set_retrieval(&db, &b, 0);

    let before = count_rows(&db);
    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let mut all = Vec::new();
    for p in ["a", "b"] {
        all.extend(ev.run_for_project(&mut db, p).unwrap());
    }
    let after = count_rows(&db);

    assert_eq!(before, after, "row count must be unchanged");
    assert_eq!(all.len(), 2);
    assert!(all.contains(&a));
    assert!(all.contains(&b));
}

#[test]
fn test_prune_handler_respects_count_threshold() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();

    // N=3: retrieval_count 2 is eligible (< 3), retrieval_count 3 is not.
    let below = seed(&db, "p", "count two", OLD, "fact", "candidate");
    set_retrieval(&db, &below, 2);
    let at = seed(&db, "p", "count three", OLD, "fact", "candidate");
    set_retrieval(&db, &at, 3);

    let ev = PruneEvaluator::new(3, 7);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    assert_eq!(
        demoted,
        vec![below.clone()],
        "retrieval_count < N is strict"
    );
    assert_eq!(status_of(&db, &below), "deprecated");
    assert_eq!(status_of(&db, &at), "candidate");
}

#[test]
fn test_prune_handler_respects_age_threshold() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();

    // T = 7 days. A row created 3 days before "now" is not old enough.
    let recent = chrono::Utc::now() - chrono::Duration::days(3);
    let recent_rfc = recent.to_rfc3339();
    let id = seed(&db, "p", "recent enough", &recent_rfc, "fact", "candidate");
    set_retrieval(&db, &id, 0);

    let ev = PruneEvaluator::new(3, 7);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    assert!(
        demoted.is_empty(),
        "age must strictly exceed T to be eligible"
    );
    assert_eq!(status_of(&db, &id), "candidate");
}

#[test]
fn test_prune_defaults_are_3_and_14() {
    assert_eq!(DEFAULT_PRUNE_COUNT, 3);
    assert_eq!(DEFAULT_PRUNE_AGE_DAYS, 14);
}

#[test]
fn test_prune_response_serializes_correctly() {
    let response = PruneResponse {
        demoted: 2,
        demoted_ids: vec!["id-1".to_string(), "id-2".to_string()],
        rows_before: 10,
        rows_after: 10,
    };
    let json = serde_json::to_string(&response).unwrap();
    assert!(json.contains("\"demoted\":2"));
    assert!(json.contains("\"rows_before\":10"));
    assert!(json.contains("\"rows_after\":10"));
    assert!(json.contains("id-1"));
    assert!(json.contains("id-2"));
}

#[test]
fn test_prune_importance_high_never_demoted_integration() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    let id = seed(&db, "p", "high importance", OLD, "fact", "candidate");
    set_retrieval(&db, &id, 0);
    db.conn()
        .execute(
            "UPDATE memories SET importance = 'high' WHERE id = ?",
            [id.clone()],
        )
        .unwrap();

    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    assert!(
        demoted.is_empty(),
        "high-importance rows are a HARD exclusion — never demoted"
    );
    assert_eq!(status_of(&db, &id), "candidate");
}

#[test]
fn test_prune_importance_critical_never_demoted_integration() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    let id = seed(&db, "p", "critical importance", OLD, "fact", "candidate");
    set_retrieval(&db, &id, 0);
    db.conn()
        .execute(
            "UPDATE memories SET importance = 'critical' WHERE id = ?",
            [id.clone()],
        )
        .unwrap();

    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    assert!(
        demoted.is_empty(),
        "critical-importance rows are a HARD exclusion — never demoted"
    );
    assert_eq!(status_of(&db, &id), "candidate");
}

#[test]
fn test_prune_importance_medium_low_still_eligible_integration() {
    let (_dir, db_path) = create_test_db();
    let mut db = Database::open(&db_path).unwrap();
    let med = seed(&db, "p", "medium", OLD, "fact", "candidate");
    set_retrieval(&db, &med, 0);
    db.conn()
        .execute(
            "UPDATE memories SET importance = 'medium' WHERE id = ?",
            [med.clone()],
        )
        .unwrap();
    let low = seed(&db, "p", "low", OLD, "fact", "candidate");
    set_retrieval(&db, &low, 0);
    db.conn()
        .execute(
            "UPDATE memories SET importance = 'low' WHERE id = ?",
            [low.clone()],
        )
        .unwrap();

    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    assert_eq!(
        demoted.len(),
        2,
        "medium and low importance remain eligible"
    );
}

/// Issue #238: the level set **at add time** has to be the one prune reads.
///
/// Every importance-exclusion test above seeds with `insert_with_time` and then
/// `UPDATE`s the column — that proves the evaluator honours the column, not
/// that `vipune add --importance critical` ever wrote it. Here the level comes
/// only from the store's add path.
#[test]
fn test_add_time_importance_protects_from_prune() {
    use crate::memory::NewMemory;
    use crate::memory::lifecycle::{MemoryImportance, MemoryStatus, MemoryType};
    use crate::memory::store::MemoryStore;
    use crate::memory_types::{AddResult, IngestPolicy};

    let (_dir, db_path) = create_test_db();
    let mut store = MemoryStore::from_db_with_test_embedder(Database::open(&db_path).unwrap());
    let mut added = |level: MemoryImportance, text: &str| -> String {
        match store
            .ingest_with_importance(
                &NewMemory {
                    project_id: "p",
                    content: text,
                    metadata: None,
                    memory_type: MemoryType::Fact,
                    status: MemoryStatus::Candidate,
                },
                IngestPolicy::Force,
                level,
            )
            .unwrap()
        {
            AddResult::Added { id } => id,
            other => panic!("a forced add stores the row, got {other:?}"),
        }
    };
    let critical = added(MemoryImportance::Critical, "added as critical");
    let low = added(MemoryImportance::Low, "added as low");
    drop(store);

    let mut db = Database::open(&db_path).unwrap();
    // Age both rows past the threshold, deliberately NOT touching importance:
    // had the add path dropped the level, `critical` would sit on the DEFAULT
    // ('medium') and be demoted like its low-importance neighbour.
    for id in [&critical, &low] {
        set_retrieval(&db, id, 0);
        db.conn()
            .execute(
                "UPDATE memories SET created_at = ?1, updated_at = ?1 WHERE id = ?2",
                (OLD, id.clone()),
            )
            .unwrap();
    }

    let ev = PruneEvaluator::new(DEFAULT_PRUNE_COUNT, DEFAULT_PRUNE_AGE_DAYS);
    let demoted = ev.run_for_project(&mut db, "p").unwrap();

    assert_eq!(
        demoted.len(),
        1,
        "only the row added at low is eligible; the one added critical is a hard exclusion"
    );
    assert_eq!(status_of(&db, &critical), "candidate");
    assert_eq!(status_of(&db, &low), "deprecated");
}

/// The partial-failure gap: a lock partway through the per-row demotion loop
/// leaves earlier rows already demoted while `handle_prune` reports only the
/// locked message. Asserting it through the handler was impossible without
/// fabricating a mid-loop lock, so the `#[ignore]`d test that claimed to pin it
/// is gone — it asserted behaviour that already holds and so could not fail.
/// This reproduces the gap against the same demotion path the handler drives — one `Database::update` per row — and asserts the
/// two halves of it: rows committed before the lock stay demoted, and the
/// failed update yields no ids, which is exactly what `wrap_busy`
/// discards on its way to the locked message.
#[test]
fn a_lock_mid_demotion_loop_leaves_rows_demoted_with_no_report_of_them() {
    use crate::memory::lifecycle::MemoryStatus;
    use crate::sqlite::UpdateOptions;

    let (_dir, db_path) = create_test_db();
    let db = Database::open(&db_path).unwrap();
    let old = (chrono::Utc::now() - chrono::Duration::days(30)).to_rfc3339();
    let ids: Vec<String> = (0..3)
        .map(|i| {
            db.insert_with_time(
                "proj-a",
                &format!("stale {i}"),
                &[0.1f32; crate::embedding::EMBEDDING_DIMS],
                None,
                &old,
                &old,
                "fact",
                "candidate",
            )
            .unwrap()
        })
        .collect();

    let demote = |id: &str| {
        db.update(
            id,
            "proj-a",
            UpdateOptions {
                content: None,
                embedding: None,
                metadata: None,
                memory_type: None,
                status: Some(MemoryStatus::Deprecated.as_str()),
                importance: None,
            },
        )
    };

    // Demote one row, then have a writer take the exclusive lock: exactly the
    // mid-loop contention a running MCP server produces.
    demote(&ids[0]).unwrap();
    let lock_conn = rusqlite::Connection::open(&db_path).unwrap();
    // Half one of the gap, asserted BEFORE the writer is taken so the read is
    // unobstructed: `Database::update` commits each row, so the demotion that
    // happened before the lock is durable — and it is exactly the id the loop
    // would have reported, which `handle_prune` discards via
    // `wrap_busy` on its way to the locked message.
    assert_eq!(
        status_of(&db, &ids[0]),
        "deprecated",
        "the row demoted before the lock is committed"
    );

    lock_conn.execute("BEGIN EXCLUSIVE", []).unwrap();
    // Zero timeout: the contended update must fail fast, not wait.
    db.set_busy_timeout(std::time::Duration::ZERO).unwrap();

    let err = demote(&ids[1]).expect_err("the contended update must fail fast");
    let err: crate::errors::Error = err.into();
    assert!(
        err.is_locked(),
        "the mid-loop failure is a lock, got {err:?}"
    );
    assert!(
        err.to_string().contains("locked"),
        "the locked message carries no partial count, got: {err}"
    );

    lock_conn.execute("ROLLBACK", []).unwrap();
}