remem-ai 0.6.14

Local-first coding agent memory for Claude Code and OpenAI Codex
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
use rusqlite::{params, Connection};

use super::{
    archive_stale_memories, cleanup_compressed_source_observations_at, cleanup_old_events,
    count_compressed_source_observations_to_delete_at, get_session_events,
    get_session_files_modified, insert_event, COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS,
};
use crate::db::Observation;
use crate::memory::tests_helper::setup_memory_schema;

#[test]
fn test_event_insert_and_query() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);

    insert_event(
        &conn,
        "session-1",
        "proj",
        "file_edit",
        "Edit src/db.rs",
        None,
        Some(r#"["src/db.rs"]"#),
        None,
    )
    .unwrap();
    insert_event(
        &conn,
        "session-1",
        "proj",
        "bash",
        "Run `cargo test` (exit 0)",
        None,
        None,
        Some(0),
    )
    .unwrap();

    let events = get_session_events(&conn, "session-1").unwrap();
    assert_eq!(events.len(), 2);
    assert_eq!(events[0].event_type, "file_edit");
    assert_eq!(events[1].exit_code, Some(0));
}

#[test]
fn test_get_session_files_modified_dedups_entries() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);

    insert_event(
        &conn,
        "session-1",
        "proj",
        "file_edit",
        "Edit sources",
        None,
        Some(r#"["src/lib.rs","src/main.rs"]"#),
        None,
    )
    .unwrap();
    insert_event(
        &conn,
        "session-1",
        "proj",
        "file_create",
        "Create main",
        None,
        Some(r#"["src/main.rs","src/bin.rs"]"#),
        None,
    )
    .unwrap();
    insert_event(
        &conn,
        "session-1",
        "proj",
        "bash",
        "Run tests",
        None,
        Some("not-json"),
        Some(0),
    )
    .unwrap();

    let files = get_session_files_modified(&conn, "session-1").unwrap();
    assert_eq!(files, vec!["src/lib.rs", "src/main.rs", "src/bin.rs"]);
}

#[test]
fn test_cleanup_old_events() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);

    let now = chrono::Utc::now().timestamp();
    let old = now - (31 * 86400);
    conn.execute(
        "INSERT INTO events (session_id, project, event_type, summary, created_at_epoch)
         VALUES ('s1', 'proj', 'file_edit', 'old edit', ?1)",
        params![old],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO events (session_id, project, event_type, summary, created_at_epoch)
         VALUES ('s2', 'proj', 'file_edit', 'new edit', ?1)",
        params![now],
    )
    .unwrap();

    assert_eq!(cleanup_old_events(&conn, 30).unwrap(), 1);
    let remaining: i64 = conn
        .query_row("SELECT COUNT(*) FROM events", [], |row| row.get(0))
        .unwrap();
    assert_eq!(remaining, 1);
}

#[test]
fn test_archive_stale_memories() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);

    let now = chrono::Utc::now().timestamp();
    let old = now - (181 * 86400);
    conn.execute(
        "INSERT INTO memories (session_id, project, title, content, memory_type, \
         created_at_epoch, updated_at_epoch, status)
         VALUES ('s1', 'proj', 'old', 'old content', 'decision', ?1, ?1, 'stale')",
        params![old],
    )
    .unwrap();
    conn.execute(
        "INSERT INTO memories (session_id, project, title, content, memory_type, \
         created_at_epoch, updated_at_epoch, status)
         VALUES ('s2', 'proj', 'new', 'new content', 'decision', ?1, ?1, 'active')",
        params![now],
    )
    .unwrap();

    assert_eq!(archive_stale_memories(&conn, 180).unwrap(), 1);
    let archived: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM memories WHERE status = 'archived'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(archived, 1);
    let active: i64 = conn
        .query_row(
            "SELECT COUNT(*) FROM memories WHERE status = 'active'",
            [],
            |row| row.get(0),
        )
        .unwrap();
    assert_eq!(active, 1);
}

#[test]
fn test_archive_stale_memories_does_not_archive_old_active_durable_memory() {
    let conn = Connection::open_in_memory().unwrap();
    setup_memory_schema(&conn);

    let old = chrono::Utc::now().timestamp() - (365 * 86400);
    conn.execute(
        "INSERT INTO memories (session_id, project, title, content, memory_type, \
         created_at_epoch, updated_at_epoch, status)
         VALUES ('s1', 'proj', 'old decision', 'keep durable decision', 'architecture', ?1, ?1, 'active')",
        params![old],
    )
    .unwrap();

    assert_eq!(archive_stale_memories(&conn, 180).unwrap(), 0);
    let status: String = conn
        .query_row("SELECT status FROM memories", [], |row| row.get(0))
        .unwrap();
    assert_eq!(status, "active");
}

#[test]
fn compressed_source_cleanup_only_deletes_old_sources_with_sufficient_provenance() {
    let conn = Connection::open_in_memory().unwrap();
    setup_observation_retention_schema(&conn);

    let now = 2_000_000_000;
    let old_epoch = now - (400 * 86_400);
    let old_link_epoch = now - ((COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS + 1) * 86_400);
    let cutoff_epoch = now - (COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS * 86_400);
    let recent_link_epoch = now - ((COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS - 1) * 86_400);

    let replacement = observation(100, "active", old_epoch, "replacement");
    insert_observation_row(&conn, &replacement);

    let active = observation(1, "active", old_epoch, "active");
    let stale = observation(2, "stale", old_epoch, "stale");
    let eligible = observation_with_content_session(
        3,
        "compressed",
        old_epoch,
        "eligible",
        "content-session-eligible",
    );
    let recent = observation(4, "compressed", old_epoch, "recent");
    let missing_provenance = observation(5, "compressed", old_epoch, "missing provenance");
    let boundary = observation(8, "compressed", old_epoch, "boundary");
    for source in [
        &active,
        &stale,
        &eligible,
        &recent,
        &missing_provenance,
        &boundary,
    ] {
        insert_observation_row(&conn, source);
    }
    link_source(&conn, replacement.id, &eligible, old_link_epoch);
    link_source(&conn, replacement.id, &recent, recent_link_epoch);
    link_source(&conn, replacement.id, &boundary, cutoff_epoch);

    let nested_replacement = observation(6, "compressed", old_epoch, "nested replacement");
    let nested_source = observation(7, "active", old_epoch, "nested source");
    insert_observation_row(&conn, &nested_replacement);
    insert_observation_row(&conn, &nested_source);
    link_source(&conn, nested_replacement.id, &nested_source, old_link_epoch);
    link_source(&conn, replacement.id, &nested_replacement, old_link_epoch);

    assert_eq!(
        count_compressed_source_observations_to_delete_at(
            &conn,
            now,
            COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS
        )
        .unwrap(),
        1
    );
    assert_eq!(
        cleanup_compressed_source_observations_at(
            &conn,
            now,
            COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS
        )
        .unwrap(),
        1
    );

    assert!(!observation_exists(&conn, eligible.id));
    for id in [
        active.id,
        stale.id,
        recent.id,
        missing_provenance.id,
        nested_replacement.id,
        nested_source.id,
        boundary.id,
        replacement.id,
    ] {
        assert!(
            observation_exists(&conn, id),
            "observation {id} should remain"
        );
    }
    assert_eq!(source_link_count(&conn, eligible.id), 1);
}

#[test]
fn compressed_source_cleanup_blocks_hash_mismatch() {
    let conn = Connection::open_in_memory().unwrap();
    setup_observation_retention_schema(&conn);

    let now = 2_000_000_000;
    let old_epoch = now - (400 * 86_400);
    let old_link_epoch = now - ((COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS + 1) * 86_400);

    let replacement = observation(100, "active", old_epoch, "replacement");
    let source = observation(1, "compressed", old_epoch, "source");
    insert_observation_row(&conn, &replacement);
    insert_observation_row(&conn, &source);
    link_source(&conn, replacement.id, &source, old_link_epoch);
    conn.execute(
        "UPDATE compressed_observation_sources
         SET source_hash = 'sha256:observation-v1:bad'
         WHERE source_observation_id = ?1",
        params![source.id],
    )
    .unwrap();

    assert_eq!(
        count_compressed_source_observations_to_delete_at(
            &conn,
            now,
            COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS
        )
        .unwrap(),
        0
    );
    assert_eq!(
        cleanup_compressed_source_observations_at(
            &conn,
            now,
            COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS
        )
        .unwrap(),
        0
    );
    assert!(observation_exists(&conn, source.id));
}

fn setup_observation_retention_schema(conn: &Connection) {
    conn.execute_batch(
        "CREATE TABLE sdk_sessions (
            id INTEGER PRIMARY KEY,
            content_session_id TEXT UNIQUE NOT NULL,
            memory_session_id TEXT NOT NULL,
            project TEXT,
            user_prompt TEXT,
            started_at TEXT,
            started_at_epoch INTEGER,
            status TEXT DEFAULT 'active',
            prompt_counter INTEGER DEFAULT 1
        );
        CREATE TABLE observations (
            id INTEGER PRIMARY KEY,
            memory_session_id TEXT NOT NULL,
            project TEXT,
            type TEXT NOT NULL,
            title TEXT,
            subtitle TEXT,
            narrative TEXT,
            facts TEXT,
            concepts TEXT,
            files_read TEXT,
            files_modified TEXT,
            prompt_number INTEGER,
            created_at TEXT,
            created_at_epoch INTEGER,
            discovery_tokens INTEGER DEFAULT 0,
            status TEXT DEFAULT 'active',
            last_accessed_epoch INTEGER,
            branch TEXT,
            commit_sha TEXT
        );
        CREATE TABLE compressed_observation_sources (
            id INTEGER PRIMARY KEY,
            compressed_observation_id INTEGER NOT NULL,
            source_observation_id INTEGER NOT NULL,
            source_hash TEXT NOT NULL,
            source_snapshot_json TEXT NOT NULL,
            source_created_at_epoch INTEGER NOT NULL,
            compression_session_id TEXT NOT NULL,
            created_at_epoch INTEGER NOT NULL,
            UNIQUE(compressed_observation_id, source_observation_id),
            FOREIGN KEY(compressed_observation_id) REFERENCES observations(id) ON DELETE CASCADE
        );",
    )
    .unwrap();
}

fn observation(id: i64, status: &str, created_at_epoch: i64, title: &str) -> Observation {
    observation_with_content_session(id, status, created_at_epoch, title, "")
}

fn observation_with_content_session(
    id: i64,
    status: &str,
    created_at_epoch: i64,
    title: &str,
    content_session_id: &str,
) -> Observation {
    Observation {
        id,
        memory_session_id: format!("session-{id}"),
        r#type: "discovery".to_string(),
        title: Some(title.to_string()),
        subtitle: None,
        narrative: Some(format!("narrative {title}")),
        facts: None,
        concepts: None,
        files_read: None,
        files_modified: None,
        discovery_tokens: Some(1),
        created_at: format!("{created_at_epoch}"),
        created_at_epoch,
        project: Some("proj".to_string()),
        status: status.to_string(),
        last_accessed_epoch: None,
        content_session_id: (!content_session_id.is_empty())
            .then(|| content_session_id.to_string()),
        branch: None,
        commit_sha: None,
    }
}

fn insert_observation_row(conn: &Connection, observation: &Observation) {
    conn.execute(
        "INSERT INTO observations
         (id, memory_session_id, project, type, title, subtitle, narrative,
          facts, concepts, files_read, files_modified, created_at,
          created_at_epoch, discovery_tokens, status, last_accessed_epoch,
          branch, commit_sha)
         VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15, ?16, ?17, ?18)",
        params![
            observation.id,
            observation.memory_session_id,
            observation.project,
            observation.r#type,
            observation.title,
            observation.subtitle,
            observation.narrative,
            observation.facts,
            observation.concepts,
            observation.files_read,
            observation.files_modified,
            observation.created_at,
            observation.created_at_epoch,
            observation.discovery_tokens,
            observation.status,
            observation.last_accessed_epoch,
            observation.branch,
            observation.commit_sha
        ],
    )
    .unwrap();
    if let Some(content_session_id) = &observation.content_session_id {
        conn.execute(
            "INSERT INTO sdk_sessions (content_session_id, memory_session_id, project)
             VALUES (?1, ?2, ?3)",
            params![
                content_session_id,
                observation.memory_session_id,
                observation.project
            ],
        )
        .unwrap();
    }
}

fn link_source(
    conn: &Connection,
    compressed_observation_id: i64,
    source: &Observation,
    created_at_epoch: i64,
) {
    crate::db::insert_compressed_observation_sources(
        conn,
        &[compressed_observation_id],
        std::slice::from_ref(source),
        "compressed-session",
    )
    .unwrap();
    conn.execute(
        "UPDATE compressed_observation_sources
         SET created_at_epoch = ?1
         WHERE compressed_observation_id = ?2 AND source_observation_id = ?3",
        params![created_at_epoch, compressed_observation_id, source.id],
    )
    .unwrap();
}

fn observation_exists(conn: &Connection, id: i64) -> bool {
    conn.query_row(
        "SELECT EXISTS(SELECT 1 FROM observations WHERE id = ?1)",
        params![id],
        |row| row.get(0),
    )
    .unwrap()
}

fn source_link_count(conn: &Connection, source_observation_id: i64) -> i64 {
    conn.query_row(
        "SELECT COUNT(*) FROM compressed_observation_sources WHERE source_observation_id = ?1",
        params![source_observation_id],
        |row| row.get(0),
    )
    .unwrap()
}