remem-ai 0.6.36

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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
use anyhow::{Context, Result};
use rusqlite::{params, Connection, OptionalExtension, Transaction, TransactionBehavior};

use crate::db::{self, CompressedObservationSource, Observation};

pub const OLD_EVENT_RETENTION_DAYS: i64 = 30;
pub const COMPRESSED_SOURCE_OBSERVATION_RETENTION_DAYS: i64 = 90;
pub const STALE_MEMORY_ARCHIVE_DAYS: i64 = 180;

const SECONDS_PER_DAY: i64 = 86_400;
const COMPRESSED_SOURCE_SCAN_BATCH_SIZE: i64 = 500;

pub fn cleanup_old_events(conn: &Connection, days: i64) -> Result<usize> {
    cleanup_old_events_at(conn, chrono::Utc::now().timestamp(), days)
}

pub fn count_old_events(conn: &Connection, days: i64) -> Result<usize> {
    count_old_events_at(conn, chrono::Utc::now().timestamp(), days)
}

pub fn cleanup_old_events_at(conn: &Connection, now_epoch: i64, days: i64) -> Result<usize> {
    let Some(has_audit_references) = event_retention_schema(conn)? else {
        return Ok(0);
    };
    let cutoff = cutoff_epoch(now_epoch, days);
    let sql = if has_audit_references {
        "DELETE FROM events
         WHERE retention_class = 'ephemeral'
           AND created_at_epoch < ?1
           AND NOT EXISTS (
             SELECT 1 FROM api_mutation_requests request
             WHERE request.audit_id = events.id
           )"
    } else {
        "DELETE FROM events
         WHERE retention_class = 'ephemeral'
           AND created_at_epoch < ?1"
    };
    Ok(conn.execute(sql, params![cutoff])?)
}

pub fn count_old_events_at(conn: &Connection, now_epoch: i64, days: i64) -> Result<usize> {
    let Some(has_audit_references) = event_retention_schema(conn)? else {
        return Ok(0);
    };
    let cutoff = cutoff_epoch(now_epoch, days);
    let sql = if has_audit_references {
        "SELECT COUNT(*) FROM events
         WHERE retention_class = 'ephemeral'
           AND created_at_epoch < ?1
           AND NOT EXISTS (
             SELECT 1 FROM api_mutation_requests request
             WHERE request.audit_id = events.id
           )"
    } else {
        "SELECT COUNT(*) FROM events
         WHERE retention_class = 'ephemeral'
           AND created_at_epoch < ?1"
    };
    count_rows(conn, sql, &[&cutoff])
}

pub fn archive_stale_memories(conn: &Connection, days: i64) -> Result<usize> {
    archive_stale_memories_at(conn, chrono::Utc::now().timestamp(), days)
}

pub fn count_stale_memories_to_archive(conn: &Connection, days: i64) -> Result<usize> {
    count_stale_memories_to_archive_at(conn, chrono::Utc::now().timestamp(), days)
}

pub fn archive_stale_memories_at(conn: &Connection, now_epoch: i64, days: i64) -> Result<usize> {
    let cutoff = cutoff_epoch(now_epoch, days);
    Ok(conn.execute(
        "UPDATE memories SET status = 'archived' \
         WHERE status = 'stale' AND updated_at_epoch < ?1",
        params![cutoff],
    )?)
}

pub fn count_stale_memories_to_archive_at(
    conn: &Connection,
    now_epoch: i64,
    days: i64,
) -> Result<usize> {
    let cutoff = cutoff_epoch(now_epoch, days);
    count_rows(
        conn,
        "SELECT COUNT(*) FROM memories WHERE status = 'stale' AND updated_at_epoch < ?1",
        &[&cutoff],
    )
}

pub fn count_compressed_source_observations_to_delete(
    conn: &Connection,
    days: i64,
) -> Result<usize> {
    count_compressed_source_observations_to_delete_at(conn, chrono::Utc::now().timestamp(), days)
}

pub fn count_compressed_source_observations_to_delete_at(
    conn: &Connection,
    now_epoch: i64,
    days: i64,
) -> Result<usize> {
    let mut count = 0;
    visit_compressed_source_observations_to_delete_at(conn, now_epoch, days, false, |_| {
        count += 1;
        Ok(())
    })?;
    Ok(count)
}

pub fn cleanup_compressed_source_observations(conn: &Connection, days: i64) -> Result<usize> {
    cleanup_compressed_source_observations_at(conn, chrono::Utc::now().timestamp(), days)
}

pub fn cleanup_compressed_source_observations_at(
    conn: &Connection,
    now_epoch: i64,
    days: i64,
) -> Result<usize> {
    if !conn.is_autocommit() {
        return cleanup_compressed_sources_in_transaction(conn, now_epoch, days);
    }
    let tx = Transaction::new_unchecked(conn, TransactionBehavior::Immediate)
        .context("begin compressed source cleanup transaction")?;
    let deleted = cleanup_compressed_sources_in_transaction(&tx, now_epoch, days)?;
    tx.commit()
        .context("commit compressed source cleanup transaction")?;
    Ok(deleted)
}

fn cleanup_compressed_sources_in_transaction(
    conn: &Connection,
    now_epoch: i64,
    days: i64,
) -> Result<usize> {
    let mut deleted = 0;
    visit_compressed_source_observations_to_delete_at(conn, now_epoch, days, true, |id| {
        deleted += conn.execute(
            "DELETE FROM observations
                 WHERE id = ?1 AND status = 'compressed'
                   AND NOT EXISTS (
                     SELECT 1 FROM compressed_observation_sources owned
                     WHERE owned.compressed_observation_id = observations.id
                 )",
            params![id],
        )?;
        Ok(())
    })?;
    Ok(deleted)
}

pub fn compressed_source_observation_ids_to_delete_at(
    conn: &Connection,
    now_epoch: i64,
    days: i64,
) -> Result<Vec<i64>> {
    let mut ids = Vec::new();
    visit_compressed_source_observations_to_delete_at(conn, now_epoch, days, false, |id| {
        ids.push(id);
        Ok(())
    })?;
    Ok(ids)
}

fn visit_compressed_source_observations_to_delete_at(
    conn: &Connection,
    now_epoch: i64,
    days: i64,
    upgrade_legacy_links: bool,
    mut visit: impl FnMut(i64) -> Result<()>,
) -> Result<()> {
    db::ensure_observation_retention_schema_supported(conn)?;
    let cutoff = cutoff_epoch(now_epoch, days);
    let mut after_created_at_epoch = i64::MIN;
    let mut after_id = i64::MIN;
    loop {
        let batch = {
            let mut stmt = conn.prepare(
                "SELECT o.id, o.created_at_epoch
                 FROM observations o
                 WHERE o.status = 'compressed'
                   AND o.created_at_epoch < 10000000000
                   AND (
                       o.created_at_epoch > ?2
                       OR (o.created_at_epoch = ?2 AND o.id > ?3)
                   )
                   AND EXISTS (
                       SELECT 1 FROM compressed_observation_sources source_link
                       WHERE source_link.source_observation_id = o.id
                         AND source_link.created_at_epoch < ?1
                   )
                   AND NOT EXISTS (
                       SELECT 1 FROM compressed_observation_sources owned
                       WHERE owned.compressed_observation_id = o.id
                   )
                 ORDER BY o.created_at_epoch ASC, o.id ASC
                 LIMIT ?4",
            )?;
            let rows = stmt.query_map(
                params![
                    cutoff,
                    after_created_at_epoch,
                    after_id,
                    COMPRESSED_SOURCE_SCAN_BATCH_SIZE
                ],
                |row| Ok((row.get::<_, i64>(0)?, row.get::<_, i64>(1)?)),
            )?;
            crate::db::query::collect_rows(rows)?
        };
        let Some(&(last_id, last_created_at_epoch)) = batch.last() else {
            break;
        };
        after_created_at_epoch = last_created_at_epoch;
        after_id = last_id;
        for (id, _) in batch {
            if compressed_source_is_delete_eligible(conn, id, cutoff, upgrade_legacy_links)? {
                visit(id)?;
            }
        }
    }
    Ok(())
}

fn compressed_source_is_delete_eligible(
    conn: &Connection,
    source_id: i64,
    cutoff_epoch: i64,
    upgrade_legacy_links: bool,
) -> Result<bool> {
    let Some(source) = load_observation(conn, source_id)? else {
        return Ok(false);
    };
    if source.status != "compressed"
        || source.created_at_epoch >= 10_000_000_000
        || source_has_memory_fact_reference(conn, source_id)?
    {
        return Ok(false);
    }
    let owned: bool = conn.query_row(
        "SELECT EXISTS(
           SELECT 1 FROM compressed_observation_sources
           WHERE compressed_observation_id = ?1
         )",
        params![source_id],
        |row| row.get(0),
    )?;
    Ok(!owned
        && has_sufficient_compression_provenance(
            conn,
            &source,
            cutoff_epoch,
            upgrade_legacy_links,
        )?)
}

fn load_observation(conn: &Connection, id: i64) -> Result<Option<Observation>> {
    conn.query_row(
        "SELECT id, memory_session_id, type, title, subtitle, narrative,
                facts, concepts, files_read, files_modified, discovery_tokens,
                created_at, created_at_epoch, project, status, last_accessed_epoch,
                (SELECT s.content_session_id FROM sdk_sessions s
                 WHERE s.memory_session_id = o.memory_session_id LIMIT 1),
                branch, commit_sha
         FROM observations o
         WHERE o.id = ?1",
        params![id],
        map_observation_row,
    )
    .optional()
    .context("load compressed source observation")
}

fn source_has_unhashed_provenance(conn: &Connection, source_id: i64) -> Result<bool> {
    let candidates = [
        ("prompt_number", "prompt_number IS NOT NULL"),
        ("last_accessed_epoch", "last_accessed_epoch IS NOT NULL"),
        ("host_id", "host_id IS NOT NULL"),
        ("project_id", "project_id IS NOT NULL"),
        ("session_row_id", "session_row_id IS NOT NULL"),
        (
            "observation_type",
            "NULLIF(TRIM(observation_type), '') IS NOT NULL",
        ),
        ("text", "NULLIF(TRIM(text), '') IS NOT NULL"),
        (
            "evidence_event_ids",
            "COALESCE(NULLIF(TRIM(evidence_event_ids), ''), '[]') NOT IN ('[]', 'null')",
        ),
        ("confidence", "confidence IS NOT NULL"),
        ("reference_time_epoch", "reference_time_epoch IS NOT NULL"),
    ];
    let mut predicates = Vec::new();
    for (column, predicate) in candidates {
        if table_column_exists(conn, "observations", column)? {
            predicates.push(predicate);
        }
    }
    if predicates.is_empty() {
        return Ok(false);
    }
    let sql = format!(
        "SELECT EXISTS(SELECT 1 FROM observations WHERE id = ?1 AND ({}))",
        predicates.join(" OR ")
    );
    Ok(conn.query_row(&sql, params![source_id], |row| row.get(0))?)
}

fn source_has_memory_fact_reference(conn: &Connection, source_id: i64) -> Result<bool> {
    if !table_exists(conn, "memory_facts")? {
        return Ok(false);
    }
    if !table_column_exists(conn, "memory_facts", "source_observation_id")? {
        return Ok(true);
    }
    Ok(conn.query_row(
        "SELECT EXISTS(
           SELECT 1 FROM memory_facts WHERE source_observation_id = ?1
         )",
        params![source_id],
        |row| row.get(0),
    )?)
}

fn has_sufficient_compression_provenance(
    conn: &Connection,
    source: &Observation,
    cutoff_epoch: i64,
    upgrade_legacy_links: bool,
) -> Result<bool> {
    let links = load_links_for_source(conn, source.id)?;
    if links.is_empty() {
        return Ok(false);
    }

    let has_v1_link = links
        .iter()
        .any(|link| link.source_hash.starts_with("sha256:observation-v1:"));
    let has_v2_link = links
        .iter()
        .any(|link| link.source_hash.starts_with("sha256:observation-v2:"));
    let legacy_has_unhashed_provenance = if has_v1_link {
        source_has_unhashed_provenance(conn, source.id)?
    } else {
        false
    };
    let legacy_expected_hash = has_v1_link.then(|| db::observation_source_hash(source));
    let current_record = if has_v2_link || (has_v1_link && legacy_has_unhashed_provenance) {
        Some(db::observation_source_retention_record_on_supported_schema(
            conn, source,
        )?)
    } else {
        None
    };
    for link in links {
        if link.created_at_epoch >= cutoff_epoch {
            continue;
        }
        if link.source_created_at_epoch != source.created_at_epoch {
            continue;
        }
        let is_legacy_link = link.source_hash.starts_with("sha256:observation-v1:");
        let matches_supported_snapshot = if link.source_hash.starts_with("sha256:observation-v2:") {
            snapshot_json_is_valid(&link.source_snapshot_json, source.id, "v2")
                && current_record.as_ref().is_some_and(|record| {
                    link.source_hash == record.source_hash
                        && link.source_snapshot_json == record.source_snapshot_json
                })
        } else if is_legacy_link {
            legacy_expected_hash
                .as_ref()
                .is_some_and(|expected| link.source_hash == *expected)
                && snapshot_matches_source(&link.source_snapshot_json, source)?
        } else {
            false
        };
        if !matches_supported_snapshot {
            continue;
        }
        if compressed_observation_exists(conn, link.compressed_observation_id, source.id)? {
            if is_legacy_link && legacy_has_unhashed_provenance && upgrade_legacy_links {
                let record = current_record
                    .as_ref()
                    .context("build v2 record for legacy compressed source upgrade")?;
                upgrade_legacy_source_link(conn, &link, record)?;
            }
            return Ok(true);
        }
    }

    Ok(false)
}

fn upgrade_legacy_source_link(
    conn: &Connection,
    link: &CompressedObservationSource,
    record: &db::ObservationSourceRetentionRecord,
) -> Result<()> {
    let changed = conn.execute(
        "UPDATE compressed_observation_sources
         SET source_hash = ?1, source_snapshot_json = ?2
         WHERE compressed_observation_id = ?3
           AND source_observation_id = ?4
           AND source_hash = ?5
           AND source_snapshot_json = ?6",
        params![
            record.source_hash,
            record.source_snapshot_json,
            link.compressed_observation_id,
            link.source_observation_id,
            link.source_hash,
            link.source_snapshot_json
        ],
    )?;
    if changed != 1 {
        anyhow::bail!(
            "legacy compressed source provenance changed during upgrade: source_id={}",
            link.source_observation_id
        );
    }
    Ok(())
}

fn load_links_for_source(
    conn: &Connection,
    source_observation_id: i64,
) -> Result<Vec<CompressedObservationSource>> {
    let mut stmt = conn.prepare(
        "SELECT compressed_observation_id, source_observation_id, source_hash,
                source_snapshot_json, source_created_at_epoch, compression_session_id,
                created_at_epoch
         FROM compressed_observation_sources
         WHERE source_observation_id = ?1
         ORDER BY compressed_observation_id",
    )?;
    let rows = stmt.query_map(params![source_observation_id], |row| {
        Ok(CompressedObservationSource {
            compressed_observation_id: row.get(0)?,
            source_observation_id: row.get(1)?,
            source_hash: row.get(2)?,
            source_snapshot_json: row.get(3)?,
            source_created_at_epoch: row.get(4)?,
            compression_session_id: row.get(5)?,
            created_at_epoch: row.get(6)?,
        })
    })?;

    let mut links = Vec::new();
    for row in rows {
        links.push(row?);
    }
    Ok(links)
}

fn snapshot_matches_source(snapshot_json: &str, source: &Observation) -> Result<bool> {
    if !snapshot_json_is_valid(snapshot_json, source.id, "v1") {
        return Ok(false);
    }
    let expected = db::observation_source_snapshot_json(source)
        .context("build expected compressed source snapshot")?;
    Ok(snapshot_json == expected)
}

fn snapshot_json_is_valid(snapshot_json: &str, source_id: i64, version: &str) -> bool {
    if serde_json::from_str::<serde_json::Value>(snapshot_json).is_ok() {
        return true;
    }
    crate::log::error(
        "cleanup",
        &format!(
            "ignoring invalid compressed source {version} snapshot for observation {source_id}"
        ),
    );
    false
}

fn compressed_observation_exists(
    conn: &Connection,
    compressed_observation_id: i64,
    source_observation_id: i64,
) -> Result<bool> {
    let exists: bool = conn.query_row(
        "SELECT EXISTS(
             SELECT 1 FROM observations
             WHERE id = ?1 AND id != ?2 AND status = 'active'
         )",
        params![compressed_observation_id, source_observation_id],
        |row| row.get(0),
    )?;
    Ok(exists)
}

fn count_rows(
    conn: &Connection,
    sql: &str,
    params: &[&dyn rusqlite::types::ToSql],
) -> Result<usize> {
    let count: i64 = conn.query_row(sql, params, |row| row.get(0))?;
    Ok(count as usize)
}

/// `None` means the retention discriminator is unavailable or inconsistent, so
/// cleanup must fail closed instead of falling back to age-only deletion.
fn event_retention_schema(conn: &Connection) -> Result<Option<bool>> {
    if !table_column_exists(conn, "events", "retention_class")? {
        return Ok(None);
    }
    if !table_exists(conn, "api_mutation_requests")? {
        return Ok(Some(false));
    }
    if !table_column_exists(conn, "api_mutation_requests", "audit_id")? {
        return Ok(None);
    }
    Ok(Some(true))
}

fn table_exists(conn: &Connection, table: &str) -> Result<bool> {
    Ok(conn
        .query_row(
            "SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?1",
            params![table],
            |_| Ok(()),
        )
        .optional()?
        .is_some())
}

fn table_column_exists(conn: &Connection, table: &str, column: &str) -> Result<bool> {
    if !table_exists(conn, table)? {
        return Ok(false);
    }
    let sql = format!(
        "SELECT 1 FROM pragma_table_info('{}') WHERE name = ?1",
        table
    );
    Ok(conn
        .query_row(&sql, params![column], |_| Ok(()))
        .optional()?
        .is_some())
}

fn cutoff_epoch(now_epoch: i64, days: i64) -> i64 {
    now_epoch.saturating_sub(days.saturating_mul(SECONDS_PER_DAY))
}

fn map_observation_row(row: &rusqlite::Row) -> rusqlite::Result<Observation> {
    Ok(Observation {
        id: row.get(0)?,
        memory_session_id: row.get(1)?,
        r#type: row.get(2)?,
        title: row.get(3)?,
        subtitle: row.get(4)?,
        narrative: row.get(5)?,
        facts: row.get(6)?,
        concepts: row.get(7)?,
        files_read: row.get(8)?,
        files_modified: row.get(9)?,
        discovery_tokens: row.get(10)?,
        created_at: row.get(11)?,
        created_at_epoch: row.get(12)?,
        project: row.get(13)?,
        status: row
            .get::<_, Option<String>>(14)?
            .unwrap_or_else(|| "active".to_string()),
        last_accessed_epoch: row.get(15)?,
        content_session_id: row.get(16)?,
        branch: row.get(17)?,
        commit_sha: row.get(18)?,
    })
}