remem-ai 0.6.79

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
use anyhow::Result;
use rusqlite::Connection;

use super::types::{
    MultiHopMeta, SearchRequest, SearchResultSet, SearchResultSetWithExplainDetails,
    SearchRoutingPolicy,
};

/// Curated hits below this count trigger a raw archive fallback so the caller
/// always has *something* to show when the conversation happened but was
/// never promoted.
const RAW_FALLBACK_THRESHOLD: usize = 3;
const RAW_FALLBACK_LIMIT: i64 = 10;

pub fn search_memories(conn: &Connection, req: &SearchRequest) -> Result<SearchResultSet> {
    search_memories_with_explain_details(conn, req).map(|result| result.result)
}

pub(crate) fn search_memories_with_explain_details(
    conn: &Connection,
    req: &SearchRequest,
) -> Result<SearchResultSetWithExplainDetails> {
    search_memories_with_explain_details_with_routing(conn, req, None)
}

pub(crate) fn search_memories_with_explain_details_with_routing(
    conn: &Connection,
    req: &SearchRequest,
    routing: Option<&SearchRoutingPolicy>,
) -> Result<SearchResultSetWithExplainDetails> {
    let limit = req.limit.max(1);
    let query = req.query.as_deref();
    let effective_multi_hop = routing.is_some_and(|routing| routing.use_multi_hop) || req.multi_hop;

    if effective_multi_hop {
        return multi_hop_search(conn, query, req.project.as_deref(), limit, req, routing).map(
            |result| SearchResultSetWithExplainDetails {
                result,
                explain_details: None,
            },
        );
    }

    let (mut memories, mut explain_details) = if req.explain {
        crate::retrieval::search::search_with_branch_execution_policy_with_suppressed_policy(
            conn,
            query,
            req.project.as_deref(),
            req.memory_type.as_deref(),
            limit + 1,
            req.offset.max(0),
            req.include_stale,
            req.branch.as_deref(),
            req.include_suppressed,
            true,
            search_execution_policy(true, routing),
        )?
    } else {
        crate::retrieval::search::search_with_branch_execution_policy_with_suppressed_policy(
            conn,
            query,
            req.project.as_deref(),
            req.memory_type.as_deref(),
            limit + 1,
            req.offset.max(0),
            req.include_stale,
            req.branch.as_deref(),
            req.include_suppressed,
            false,
            search_execution_policy(false, routing),
        )?
    };
    let has_more = memories.len() as i64 > limit;
    memories.truncate(limit as usize);
    let (raw_hits, raw_error) = maybe_fallback_raw(conn, req, memories.len(), routing);
    if let Some(explain) = explain_details.as_mut() {
        let result_ids: Vec<i64> = memories.iter().map(|memory| memory.id).collect();
        explain.retain_result_ids(&result_ids, has_more, limit);
        explain.set_raw_fallback_count(raw_hits.len());
    }
    Ok(SearchResultSetWithExplainDetails {
        result: SearchResultSet {
            memories,
            multi_hop: None,
            has_more,
            explain: explain_details
                .as_ref()
                .map(|details| details.explain.clone()),
            raw_hits,
            raw_error,
        },
        explain_details,
    })
}

fn multi_hop_search(
    conn: &Connection,
    query: Option<&str>,
    project: Option<&str>,
    limit: i64,
    req: &SearchRequest,
    routing: Option<&SearchRoutingPolicy>,
) -> Result<SearchResultSet> {
    if let Some(query_text) = query.filter(|query_text| !query_text.is_empty()) {
        let mut result = crate::retrieval::search_multihop::search_multi_hop(
            conn,
            query_text,
            project,
            limit + 1,
            req.offset.max(0),
            req.memory_type.as_deref(),
            req.branch.as_deref(),
            req.include_stale,
            req.include_suppressed,
        )?;
        let has_more = result.memories.len() as i64 > limit;
        result.memories.truncate(limit as usize);
        let (raw_hits, raw_error) = maybe_fallback_raw(conn, req, result.memories.len(), routing);
        Ok(SearchResultSet {
            memories: result.memories,
            multi_hop: Some(MultiHopMeta {
                hops: result.hops,
                entities_discovered: result.entities_discovered,
            }),
            has_more,
            explain: None,
            raw_hits,
            raw_error,
        })
    } else {
        Ok(SearchResultSet {
            memories: vec![],
            multi_hop: Some(MultiHopMeta {
                hops: 1,
                entities_discovered: vec![],
            }),
            has_more: false,
            explain: None,
            raw_hits: vec![],
            raw_error: None,
        })
    }
}

fn maybe_fallback_raw(
    conn: &Connection,
    req: &SearchRequest,
    curated_len: usize,
    routing: Option<&SearchRoutingPolicy>,
) -> (Vec<crate::memory::raw_archive::RawMessage>, Option<String>) {
    if routing.is_some_and(|routing| !routing.raw_fallback_enabled) {
        return (vec![], None);
    }
    if curated_len >= RAW_FALLBACK_THRESHOLD {
        return (vec![], None);
    }
    let Some(query) = req
        .query
        .as_deref()
        .map(str::trim)
        .filter(|q| !q.is_empty())
    else {
        return (vec![], None);
    };
    if !req.include_suppressed {
        match crate::memory::suppression::has_active_suppressions(conn) {
            Ok(true) => return (vec![], None),
            Ok(false) => {}
            Err(error) => {
                let message = format!("suppression policy lookup failed: {error}");
                crate::log::error("search", &message);
                return (vec![], Some(message));
            }
        }
    }
    let raw_req = crate::memory::raw_archive::RawSearchRequest {
        query: query.to_string(),
        project: req.project.clone(),
        branch: req.branch.clone(),
        role: None,
        limit: RAW_FALLBACK_LIMIT,
        offset: 0,
        since_epoch: None,
        until_epoch: None,
    };
    match crate::memory::raw_archive::search_raw_messages(conn, &raw_req) {
        Ok(hits) => (hits, None),
        Err(error) => {
            let message = format!("raw archive fallback failed: {error}");
            crate::log::warn("search", &message);
            (vec![], Some(message))
        }
    }
}

fn search_execution_policy(
    explain: bool,
    routing: Option<&SearchRoutingPolicy>,
) -> crate::retrieval::search::SearchExecutionPolicy {
    let base = if explain {
        crate::retrieval::search::SearchWeights::default()
    } else {
        crate::retrieval::search::SearchWeights::production()
    };
    let Some(routing) = routing else {
        return if explain {
            crate::retrieval::search::SearchExecutionPolicy::explain_default()
        } else {
            crate::retrieval::search::SearchExecutionPolicy::production()
        };
    };
    let mut weights = base;
    weights.fts = routing.weights.fts;
    weights.vector = routing.weights.vector;
    weights.entity = routing.weights.entity;
    weights.graph = routing.weights.graph;
    weights.temporal = routing.weights.temporal;
    weights.fact = routing.weights.fact;
    weights.like_fallback = routing.weights.like_fallback;
    weights.usage = routing.weights.usage;
    crate::retrieval::search::SearchExecutionPolicy::routed(
        weights,
        routing.rerank_enabled,
        routing.rerank_candidate_pool,
        routing.rerank_output_k,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory::raw_archive::{insert_raw_message, ROLE_USER, SOURCE_HOOK};
    use crate::memory::suppression::{create_suppression, parse_target, SuppressRequest};

    #[test]
    fn raw_fallback_respects_branch_filter() -> Result<()> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        insert_raw_message(
            &conn,
            "s-main",
            "/repo",
            ROLE_USER,
            "fallback needle from main",
            SOURCE_HOOK,
            Some("main"),
            None,
        )?;
        insert_raw_message(
            &conn,
            "s-feature",
            "/repo",
            ROLE_USER,
            "fallback needle from feature",
            SOURCE_HOOK,
            Some("feature"),
            None,
        )?;
        insert_raw_message(
            &conn,
            "s-branchless",
            "/repo",
            ROLE_USER,
            "fallback needle from branchless history",
            SOURCE_HOOK,
            None,
            None,
        )?;

        let result = search_memories(
            &conn,
            &SearchRequest {
                query: Some("needle".to_string()),
                project: Some("/repo".to_string()),
                limit: 10,
                branch: Some("main".to_string()),
                ..SearchRequest::default()
            },
        )?;
        let branches: Vec<Option<String>> =
            result.raw_hits.into_iter().map(|hit| hit.branch).collect();

        assert!(result.raw_error.is_none());
        assert!(branches.contains(&Some("main".to_string())));
        assert!(branches.contains(&None));
        assert!(
            !branches.contains(&Some("feature".to_string())),
            "{branches:?}"
        );
        Ok(())
    }

    #[test]
    fn raw_fallback_error_is_reported_without_failing_curated_search() -> Result<()> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        conn.execute("DROP TABLE raw_messages_fts", [])?;

        let result = search_memories(
            &conn,
            &SearchRequest {
                query: Some("needle".to_string()),
                project: Some("/repo".to_string()),
                limit: 10,
                ..SearchRequest::default()
            },
        )?;

        assert!(result.memories.is_empty());
        assert!(result.raw_hits.is_empty());
        assert!(result
            .raw_error
            .as_deref()
            .is_some_and(|error| error.contains("raw archive fallback failed")));
        Ok(())
    }

    #[test]
    fn search_hides_suppressed_memories_unless_explicitly_included() -> Result<()> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        crate::memory::insert_memory(
            &conn,
            Some("s1"),
            "/repo",
            None,
            "Visible needle",
            "The visible suppression needle should remain searchable.",
            "decision",
            None,
        )?;
        let hidden = crate::memory::insert_memory(
            &conn,
            Some("s2"),
            "/repo",
            None,
            "Hidden needle",
            "The hidden suppression needle should require include_suppressed.",
            "decision",
            None,
        )?;
        create_suppression(
            &conn,
            &SuppressRequest {
                target: parse_target(&format!("memory:{hidden}"))?,
                reason: Some("not relevant"),
                actor: Some("test"),
            },
        )?;

        let default = search_memories(
            &conn,
            &SearchRequest {
                query: Some("suppression needle".to_string()),
                project: Some("/repo".to_string()),
                limit: 10,
                ..SearchRequest::default()
            },
        )?;
        let default_ids = default
            .memories
            .iter()
            .map(|memory| memory.id)
            .collect::<Vec<_>>();
        assert!(!default_ids.contains(&hidden), "{default_ids:?}");

        let explicit = search_memories(
            &conn,
            &SearchRequest {
                query: Some("suppression needle".to_string()),
                project: Some("/repo".to_string()),
                limit: 10,
                include_suppressed: true,
                ..SearchRequest::default()
            },
        )?;
        let explicit_ids = explicit
            .memories
            .iter()
            .map(|memory| memory.id)
            .collect::<Vec<_>>();
        assert!(explicit_ids.contains(&hidden), "{explicit_ids:?}");
        Ok(())
    }

    #[test]
    fn search_recovers_legacy_unverified_without_underfilling_pagination() -> Result<()> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        for id in 1..=2 {
            crate::memory::insert_memory(
                &conn,
                Some("s"),
                "/repo",
                None,
                &format!("needle {id}"),
                "needle legacy searchable payload",
                "bugfix",
                None,
            )?;
        }
        let result = search_memories(
            &conn,
            &SearchRequest {
                query: Some("needle".into()),
                project: Some("/repo".into()),
                limit: 1,
                ..SearchRequest::default()
            },
        )?;
        assert_eq!(result.memories.len(), 1);
        assert!(result.has_more);
        let visibility = crate::truth::classify_memory(
            &conn,
            result.memories[0].id,
            chrono::Utc::now().timestamp(),
        )?;
        assert_eq!(visibility.classification.as_str(), "legacy_unverified");
        Ok(())
    }

    #[test]
    fn raw_fallback_does_not_bypass_active_suppression_policy() -> Result<()> {
        let conn = Connection::open_in_memory()?;
        crate::migrate::run_migrations(&conn)?;
        crate::memory::insert_memory(
            &conn,
            Some("s1"),
            "/repo",
            None,
            "Suppressed memory",
            "Suppressed raw fallback guard.",
            "decision",
            None,
        )?;
        create_suppression(
            &conn,
            &SuppressRequest {
                target: parse_target("memory:1")?,
                reason: Some("not relevant"),
                actor: Some("test"),
            },
        )?;
        insert_raw_message(
            &conn,
            "raw-session",
            "/repo",
            ROLE_USER,
            "fallback-only needle",
            SOURCE_HOOK,
            None,
            None,
        )?;

        let result = search_memories(
            &conn,
            &SearchRequest {
                query: Some("fallback-only needle".to_string()),
                project: Some("/repo".to_string()),
                limit: 10,
                ..SearchRequest::default()
            },
        )?;

        assert!(result.memories.is_empty());
        assert!(result.raw_hits.is_empty());
        assert!(result.raw_error.is_none());
        Ok(())
    }
}