remem-ai 0.6.49

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
use std::collections::HashMap;

use anyhow::Result;
use chrono::{TimeZone, Utc};
use rusqlite::{types::ToSql, Connection};

use crate::memory::Memory;

pub(crate) fn annotate_memories_with_fact_labels(
    conn: &Connection,
    memories: &mut [Memory],
    query: Option<&str>,
    project: Option<&str>,
) -> Result<()> {
    if memories.is_empty() || !super::sqlite_table_exists(conn, "memory_facts")? {
        return Ok(());
    }
    let ids = memories.iter().map(|memory| memory.id).collect::<Vec<_>>();
    let mode = query
        .map(super::FactTimeMode::from_query)
        .unwrap_or(super::FactTimeMode::Current);
    let labels = fact_labels_by_memory_id(conn, &ids, query, project, mode)?;
    for memory in memories {
        let Some(memory_labels) = labels.get(&memory.id) else {
            continue;
        };
        if memory_labels.is_empty() {
            continue;
        }
        memory.text = format!(
            "Temporal facts: {}\n{}",
            memory_labels.join("; "),
            memory.text
        );
    }
    Ok(())
}

fn fact_labels_by_memory_id(
    conn: &Connection,
    memory_ids: &[i64],
    query: Option<&str>,
    project: Option<&str>,
    mode: super::FactTimeMode,
) -> Result<HashMap<i64, Vec<String>>> {
    let has_invalidated_at_epoch = crate::memory::facts::invalidated_at_epoch_available(conn)?;
    let placeholders = (1..=memory_ids.len())
        .map(|idx| format!("?{idx}"))
        .collect::<Vec<_>>()
        .join(", ");
    let mut conditions = vec![format!("f.source_memory_id IN ({placeholders})")];
    let mut params = memory_ids
        .iter()
        .map(|id| Box::new(*id) as Box<dyn ToSql>)
        .collect::<Vec<_>>();
    let mut next_idx = memory_ids.len() + 1;
    if let Some(project) = project {
        conditions.push(format!("f.project = ?{next_idx}"));
        params.push(Box::new(project.to_string()));
        next_idx += 1;
    }
    let epoch_idx = next_idx;
    match mode {
        super::FactTimeMode::Current => {
            let now = Utc::now().timestamp();
            conditions.push(crate::memory::facts::current_fact_filter_sql(
                "f",
                has_invalidated_at_epoch,
            ));
            conditions.push(format!(
                "(f.valid_from_epoch IS NULL OR f.valid_from_epoch <= ?{epoch_idx})"
            ));
            conditions.push(format!(
                "(f.valid_to_epoch IS NULL OR f.valid_to_epoch > ?{epoch_idx})"
            ));
            params.push(Box::new(now));
        }
        super::FactTimeMode::AsOf(as_of_epoch) => {
            conditions.push(format!(
                "(f.valid_from_epoch IS NULL OR f.valid_from_epoch <= ?{epoch_idx})"
            ));
            conditions.push(crate::memory::facts::as_of_validity_filter_sql(
                "f",
                epoch_idx,
                has_invalidated_at_epoch,
            ));
            conditions.push(format!("f.learned_at_epoch <= ?{epoch_idx}"));
            if has_invalidated_at_epoch {
                conditions.push(format!(
                    "(f.invalidated_at_epoch IS NULL OR f.invalidated_at_epoch > ?{epoch_idx})"
                ));
            }
            params.push(Box::new(as_of_epoch));
        }
    }
    let sql = format!(
        "SELECT f.source_memory_id, f.subject, f.predicate, f.object,
                f.valid_from_epoch, f.valid_to_epoch
         FROM memory_facts f
         WHERE {}
         ORDER BY f.source_memory_id, COALESCE(f.valid_from_epoch, f.learned_at_epoch) DESC,
                  f.confidence DESC, f.id DESC",
        conditions.join(" AND ")
    );
    let refs = crate::db::to_sql_refs(&params);
    let mut stmt = conn.prepare(&sql)?;
    let rows = stmt.query_map(refs.as_slice(), |row| {
        Ok((
            row.get::<_, i64>(0)?,
            row.get::<_, String>(1)?,
            row.get::<_, String>(2)?,
            row.get::<_, String>(3)?,
            row.get::<_, Option<i64>>(4)?,
            row.get::<_, Option<i64>>(5)?,
        ))
    })?;
    let query_tokens = query
        .map(crate::retrieval::query_expand::core_tokens)
        .unwrap_or_default();
    let query_refs = query_tokens.iter().map(String::as_str).collect::<Vec<_>>();
    let match_terms = super::normalized_fact_terms(&query_refs);
    if match_terms.is_empty() {
        return Ok(HashMap::new());
    }
    let mut label_rows: HashMap<i64, Vec<FactLabelRow>> = HashMap::new();
    for (order, row) in rows.enumerate() {
        let (memory_id, subject, predicate, object, valid_from, valid_to) = row?;
        let match_count = fact_match_count(&match_terms, &subject, &predicate, &object);
        if match_count == 0 {
            continue;
        }
        label_rows.entry(memory_id).or_default().push(FactLabelRow {
            label: format!(
                "{} {} {} ({})",
                subject,
                predicate,
                object,
                validity_label(valid_from, valid_to)
            ),
            match_count,
            order,
        });
    }
    Ok(label_rows
        .into_iter()
        .map(|(memory_id, mut rows)| {
            rows.sort_by(|left, right| {
                right
                    .match_count
                    .cmp(&left.match_count)
                    .then_with(|| left.order.cmp(&right.order))
            });
            (
                memory_id,
                rows.into_iter()
                    .take(2)
                    .map(|row| row.label)
                    .collect::<Vec<_>>(),
            )
        })
        .collect())
}

struct FactLabelRow {
    label: String,
    match_count: usize,
    order: usize,
}

fn fact_match_count(terms: &[String], subject: &str, predicate: &str, object: &str) -> usize {
    if terms.is_empty() {
        return 0;
    }
    let haystack = format!("{subject} {predicate} {object}").to_lowercase();
    terms
        .iter()
        .filter(|term| haystack.contains(term.as_str()))
        .count()
}

fn validity_label(valid_from: Option<i64>, valid_to: Option<i64>) -> String {
    let from = valid_from
        .map(format_epoch_date_utc)
        .unwrap_or_else(|| "unknown".to_string());
    let to = valid_to
        .map(format_epoch_date_utc)
        .unwrap_or_else(|| "open".to_string());
    format!("valid_from={from}, valid_to={to}")
}

fn format_epoch_date_utc(epoch: i64) -> String {
    Utc.timestamp_opt(epoch, 0)
        .single()
        .map(|dt| dt.format("%Y-%m-%d").to_string())
        .unwrap_or_else(|| epoch.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use anyhow::{Context, Result};
    use rusqlite::params;

    fn migrated_conn() -> Result<Connection> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        Ok(conn)
    }

    fn memory(id: i64) -> Memory {
        Memory {
            id,
            session_id: None,
            project: "/repo".to_string(),
            topic_key: None,
            title: format!("Memory {id}"),
            text: "Opaque source body.".to_string(),
            memory_type: "decision".to_string(),
            files: None,
            created_at_epoch: 10,
            updated_at_epoch: 10,
            status: "active".to_string(),
            branch: None,
            scope: "project".to_string(),
        }
    }

    fn insert_memory_row(conn: &Connection, id: i64) -> Result<()> {
        conn.execute(
            "INSERT INTO memories
             (id, session_id, project, topic_key, title, content, memory_type, files,
              created_at_epoch, updated_at_epoch, status, branch, scope)
             VALUES (?1, NULL, '/repo', NULL, ?2, 'Opaque source body.', 'decision',
                     NULL, 10, 10, 'active', NULL, 'project')",
            params![id, format!("Memory {id}")],
        )?;
        Ok(())
    }

    #[allow(clippy::too_many_arguments)]
    fn insert_fact(
        conn: &Connection,
        memory_id: i64,
        subject: &str,
        predicate: &str,
        object: &str,
        status: &str,
        valid_from_epoch: Option<i64>,
        valid_to_epoch: Option<i64>,
        learned_at_epoch: i64,
        invalidated_at_epoch: Option<i64>,
    ) -> Result<()> {
        conn.execute(
            "INSERT INTO memory_facts
             (project, subject, predicate, object, valid_from_epoch, valid_to_epoch,
              learned_at_epoch, source_memory_id, source_observation_id, source_event_ids,
              confidence, supersedes_fact_id, status, invalidated_at_epoch,
              created_at_epoch, updated_at_epoch)
             VALUES ('/repo', ?1, ?2, ?3, ?4, ?5, ?6, ?7, NULL, '[]',
                     0.95, NULL, ?8, ?9, ?6, ?6)",
            params![
                subject,
                predicate,
                object,
                valid_from_epoch,
                valid_to_epoch,
                learned_at_epoch,
                memory_id,
                status,
                invalidated_at_epoch
            ],
        )?;
        Ok(())
    }

    #[test]
    fn format_epoch_date_utc_uses_utc_day() {
        assert_eq!(format_epoch_date_utc(1_767_308_400), "2026-01-01");
    }

    #[test]
    fn as_of_labels_render_historical_snapshot() -> Result<()> {
        let conn = migrated_conn()?;
        insert_memory_row(&conn, 1)?;
        let as_of = chrono::NaiveDate::from_ymd_opt(2026, 1, 15)
            .and_then(|date| date.and_hms_opt(0, 0, 0))
            .context("valid as-of date")?
            .and_utc()
            .timestamp();
        insert_fact(
            &conn,
            1,
            "HarborMint owner",
            "verified_by",
            "Ada Old",
            "stale",
            Some(as_of - 10_000),
            Some(as_of + 1_000),
            as_of - 900,
            Some(as_of + 500),
        )?;
        insert_fact(
            &conn,
            1,
            "HarborMint owner",
            "verified_by",
            "Nia New",
            "active",
            Some(as_of + 100),
            None,
            as_of + 100,
            None,
        )?;
        let mut memories = vec![memory(1)];

        annotate_memories_with_fact_labels(
            &conn,
            &mut memories,
            Some("who owned HarborMint as of 2026-01-15"),
            Some("/repo"),
        )?;

        assert!(memories[0]
            .text
            .contains("HarborMint owner verified_by Ada Old"));
        assert!(!memories[0].text.contains("Nia New"));
        Ok(())
    }

    #[test]
    fn labels_skip_facts_that_do_not_match_query_terms() -> Result<()> {
        let conn = migrated_conn()?;
        insert_memory_row(&conn, 1)?;
        let now = Utc::now().timestamp();
        insert_fact(
            &conn,
            1,
            "HarborMint owner",
            "verified_by",
            "Ada Lovelace",
            "active",
            Some(now - 1_000),
            None,
            now - 900,
            None,
        )?;
        insert_fact(
            &conn,
            1,
            "UnrelatedService",
            "blocked_by",
            "North Region",
            "active",
            Some(now - 500),
            None,
            now - 400,
            None,
        )?;
        let mut memories = vec![memory(1)];

        annotate_memories_with_fact_labels(
            &conn,
            &mut memories,
            Some("who owns HarborMint"),
            Some("/repo"),
        )?;

        assert!(memories[0]
            .text
            .contains("HarborMint owner verified_by Ada Lovelace"));
        assert!(!memories[0].text.contains("UnrelatedService"));
        Ok(())
    }

    #[test]
    fn labels_filter_facts_by_requested_project() -> Result<()> {
        let conn = migrated_conn()?;
        insert_memory_row(&conn, 1)?;
        let now = Utc::now().timestamp();
        insert_fact(
            &conn,
            1,
            "HarborMint",
            "verified_by",
            "Other Project",
            "active",
            Some(now - 1_000),
            None,
            now - 800,
            None,
        )?;
        conn.execute(
            "UPDATE memory_facts SET project = '/other' WHERE source_memory_id = 1",
            [],
        )?;
        insert_fact(
            &conn,
            1,
            "HarborMint",
            "verified_by",
            "Toma Reed",
            "active",
            Some(now - 1_000),
            None,
            now - 900,
            None,
        )?;
        let mut memories = vec![memory(1)];

        annotate_memories_with_fact_labels(
            &conn,
            &mut memories,
            Some("who verified HarborMint with Toma Reed"),
            Some("/repo"),
        )?;

        assert!(memories[0].text.contains("Toma Reed"));
        assert!(!memories[0].text.contains("Other Project"));
        Ok(())
    }
}