jiandu-memory 0.3.3

Deterministic filesystem memory store with BM25/CJK recall
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
use std::cmp::Ordering;
use std::io;

use super::{
    DurableMemoryStatus, LexicalIndex, MemoryScope, MemoryStore, TemporalGranularity, parse_rfc3339,
};

#[derive(Debug, Clone, PartialEq)]
pub struct MemoryRecallCandidate {
    pub id: String,
    pub title: String,
    pub score: f64,
    pub scope: MemoryScope,
    pub project_key: Option<String>,
    pub status: DurableMemoryStatus,
    pub updated_at: String,
    pub summary: String,
    pub granularity: Option<TemporalGranularity>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MemoryRecallOptions {
    pub shortlist_limit: usize,
    pub include_global_fallback: bool,
    pub max_candidates_per_scope: usize,
}

impl Default for MemoryRecallOptions {
    fn default() -> Self {
        Self {
            shortlist_limit: 3,
            include_global_fallback: true,
            max_candidates_per_scope: 20,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemoryRecallStrategy {
    Lexical,
}

impl MemoryRecallStrategy {
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        "lexical"
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct MemoryRecallSelection {
    pub candidates: Vec<MemoryRecallCandidate>,
    pub strategy: MemoryRecallStrategy,
}

pub async fn shortlist_relevant_memories(
    store: &MemoryStore,
    project_key: Option<&str>,
    query: &str,
    options: &MemoryRecallOptions,
) -> io::Result<Vec<MemoryRecallCandidate>> {
    let limit = options.shortlist_limit.max(1);
    let mut candidates =
        lexical_shortlist_relevant_memories(store, project_key, query, options).await?;
    candidates.truncate(limit);
    Ok(candidates)
}

pub async fn select_relevant_memories(
    store: &MemoryStore,
    project_key: Option<&str>,
    query: &str,
    options: &MemoryRecallOptions,
) -> io::Result<MemoryRecallSelection> {
    Ok(MemoryRecallSelection {
        candidates: shortlist_relevant_memories(store, project_key, query, options).await?,
        strategy: MemoryRecallStrategy::Lexical,
    })
}

/// Rank recall candidates from a caller-provided lexical index.
///
/// This function is deterministic and performs no filesystem or network I/O. It
/// applies Jiandu's fixed BM25/CJK scoring, status eligibility, and deterministic
/// ordering, while remaining policy-neutral about cross-index composition: it
/// ranks only the items in `index`. The caller remains responsible for scope
/// precedence, fallback behavior, index unions, and any primary-first ID
/// deduplication. `limit` is an exact upper bound; a zero limit, a blank query,
/// or a query without scorable matches returns an empty vector.
#[must_use]
pub fn recall_candidates_from_lexical_index(
    index: &LexicalIndex,
    query: &str,
    limit: usize,
) -> Vec<MemoryRecallCandidate> {
    recall_candidates_from_lexical_index_with_status_policy(index, query, limit, false)
}

pub(crate) fn recall_candidates_from_lexical_index_with_status_policy(
    index: &LexicalIndex,
    query: &str,
    limit: usize,
    include_all_statuses: bool,
) -> Vec<MemoryRecallCandidate> {
    let query = query.trim();
    if limit == 0 || query.is_empty() {
        return Vec::new();
    }

    let query_tokens = super::lexical_bm25::tokenize(query);
    if query_tokens.is_empty() {
        return Vec::new();
    }

    let corpus = if include_all_statuses {
        super::lexical_bm25::Bm25Corpus::build_including_all_statuses(&index.items)
    } else {
        super::lexical_bm25::Bm25Corpus::build(&index.items)
    };
    let mut candidates = index
        .items
        .iter()
        .enumerate()
        .filter_map(|(index, item)| {
            corpus
                .score(index, &query_tokens)
                .map(|score| (item, score))
        })
        .map(|(item, score)| MemoryRecallCandidate {
            id: item.id.clone(),
            title: item.title.clone(),
            score,
            scope: item.scope,
            project_key: item.project_key.clone(),
            status: item.status,
            updated_at: item.updated_at.clone(),
            summary: item.summary.clone(),
            granularity: item.granularity,
        })
        .collect::<Vec<_>>();

    sort_recall_candidates(&mut candidates);
    candidates.truncate(limit);
    candidates
}

async fn lexical_shortlist_relevant_memories(
    store: &MemoryStore,
    project_key: Option<&str>,
    query: &str,
    options: &MemoryRecallOptions,
) -> io::Result<Vec<MemoryRecallCandidate>> {
    let query = query.trim();
    if query.is_empty() {
        return Ok(Vec::new());
    }

    let limit = options.shortlist_limit.max(1);
    let per_scope_limit = options.max_candidates_per_scope.max(limit);

    if let Some(project_key) = project_key.map(str::trim).filter(|value| !value.is_empty()) {
        let project_hits = shortlist_scope(
            store,
            MemoryScope::Project,
            Some(project_key),
            query,
            per_scope_limit,
        )
        .await?;
        if !project_hits.is_empty() {
            return Ok(project_hits);
        }
    }

    if options.include_global_fallback {
        return shortlist_scope(store, MemoryScope::Global, None, query, per_scope_limit).await;
    }

    Ok(Vec::new())
}

async fn shortlist_scope(
    store: &MemoryStore,
    scope: MemoryScope,
    project_key: Option<&str>,
    query: &str,
    limit: usize,
) -> io::Result<Vec<MemoryRecallCandidate>> {
    let Some(index) = store.read_lexical_index(scope, project_key).await? else {
        return Ok(Vec::new());
    };
    Ok(recall_candidates_from_lexical_index(&index, query, limit))
}

fn sort_recall_candidates(candidates: &mut [MemoryRecallCandidate]) {
    candidates.sort_by(|left, right| {
        right
            .score
            .partial_cmp(&left.score)
            .unwrap_or(Ordering::Equal)
            .then_with(|| {
                TemporalGranularity::cache_stability_rank(left.granularity).cmp(
                    &TemporalGranularity::cache_stability_rank(right.granularity),
                )
            })
            .then_with(|| {
                let left_dt = parse_rfc3339(&left.updated_at)
                    .unwrap_or(chrono::DateTime::<chrono::Utc>::MIN_UTC);
                let right_dt = parse_rfc3339(&right.updated_at)
                    .unwrap_or(chrono::DateTime::<chrono::Utc>::MIN_UTC);
                right_dt.cmp(&left_dt)
            })
            .then_with(|| left.title.cmp(&right.title))
    });
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::memory_store::{DurableMemoryType, LexicalIndexItem};
    use tempfile::tempdir;

    fn candidate(
        id: &str,
        score: f64,
        granularity: Option<TemporalGranularity>,
    ) -> MemoryRecallCandidate {
        MemoryRecallCandidate {
            id: id.to_string(),
            title: id.to_string(),
            score,
            scope: MemoryScope::Project,
            project_key: Some("proj-1".to_string()),
            status: DurableMemoryStatus::Active,
            updated_at: "2026-04-09T00:00:00Z".to_string(),
            summary: "summary".to_string(),
            granularity,
        }
    }

    fn lexical_item(id: &str, status: DurableMemoryStatus, title: &str) -> LexicalIndexItem {
        LexicalIndexItem {
            id: id.to_string(),
            title: title.to_string(),
            scope: MemoryScope::Project,
            project_key: Some("proj-1".to_string()),
            r#type: DurableMemoryType::Project,
            status,
            tags: Vec::new(),
            keywords: Vec::new(),
            entities: Vec::new(),
            updated_at: "2026-04-09T00:00:00Z".to_string(),
            created_at: "2026-04-09T00:00:00Z".to_string(),
            summary: title.to_string(),
            granularity: None,
        }
    }

    #[test]
    fn equal_score_candidates_sort_coarse_granularity_first_for_cache_stability() {
        let mut candidates = vec![
            candidate("day", 5.0, Some(TemporalGranularity::Day)),
            candidate("year", 5.0, Some(TemporalGranularity::Year)),
            candidate("none", 5.0, None),
            candidate("month", 5.0, Some(TemporalGranularity::Month)),
        ];
        sort_recall_candidates(&mut candidates);
        let order: Vec<&str> = candidates
            .iter()
            .map(|candidate| candidate.id.as_str())
            .collect();
        assert_eq!(order, vec!["none", "year", "month", "day"]);
    }

    #[test]
    fn higher_score_still_wins_over_cache_stable_granularity() {
        let mut candidates = vec![
            candidate("year-low", 1.0, Some(TemporalGranularity::Year)),
            candidate("day-high", 9.0, Some(TemporalGranularity::Day)),
        ];
        sort_recall_candidates(&mut candidates);
        assert_eq!(candidates[0].id, "day-high");
    }

    #[test]
    fn lexical_index_recall_is_cjk_status_aware_limited_and_deterministic() {
        let index = LexicalIndex {
            generated_at: "2026-04-09T00:00:00Z".to_string(),
            items: vec![
                lexical_item("stale", DurableMemoryStatus::Stale, "多租户隔离设计"),
                lexical_item("active", DurableMemoryStatus::Active, "多租户隔离设计"),
                lexical_item("archived", DurableMemoryStatus::Archived, "多租户隔离设计"),
                lexical_item("unrelated", DurableMemoryStatus::Active, "缓存前缀稳定性"),
            ],
        };

        let limited = recall_candidates_from_lexical_index(&index, "多租户", 1);
        assert_eq!(limited.len(), 1);
        assert_eq!(limited[0].id, "active");
        assert_eq!(limited[0].status, DurableMemoryStatus::Active);

        let all = recall_candidates_from_lexical_index(&index, "多租户", 10);
        assert_eq!(
            all.iter()
                .map(|candidate| candidate.id.as_str())
                .collect::<Vec<_>>(),
            vec!["active", "stale"]
        );
        assert_eq!(
            all,
            recall_candidates_from_lexical_index(&index, "多租户", 10)
        );
        assert!(recall_candidates_from_lexical_index(&index, "多租户", 0).is_empty());
    }

    #[tokio::test]
    async fn store_shortlist_matches_pure_lexical_index_recall() {
        let dir = tempdir().unwrap();
        let store = MemoryStore::new(dir.path());
        store
            .write_memory(
                MemoryScope::Project,
                Some("proj-1"),
                DurableMemoryType::Project,
                "Checkpoint recovery decision",
                "Restore the database snapshot before reopening traffic.",
                &[],
                Some("session-1"),
                "main-model",
                false,
                None,
            )
            .await
            .unwrap();

        let options = MemoryRecallOptions {
            shortlist_limit: 2,
            include_global_fallback: false,
            max_candidates_per_scope: 4,
        };
        let from_store =
            shortlist_relevant_memories(&store, Some("proj-1"), "checkpoint", &options)
                .await
                .unwrap();
        let index = store
            .read_lexical_index(MemoryScope::Project, Some("proj-1"))
            .await
            .unwrap()
            .unwrap();
        let from_index =
            recall_candidates_from_lexical_index(&index, "checkpoint", options.shortlist_limit);

        assert_eq!(from_store, from_index);
    }

    #[tokio::test]
    async fn project_scope_shortlist_excludes_global_when_project_hits_exist() {
        let dir = tempdir().unwrap();
        let store = MemoryStore::new(dir.path());
        store
            .write_memory(
                MemoryScope::Project,
                Some("proj-1"),
                DurableMemoryType::Project,
                "Release freeze decision",
                "Project-specific release freeze note.",
                &["release".to_string()],
                Some("session-1"),
                "main-model",
                false,
                None,
            )
            .await
            .unwrap();
        store
            .write_memory(
                MemoryScope::Global,
                None,
                DurableMemoryType::Reference,
                "Global release guidance",
                "Global note that should not be used when project hits exist.",
                &["release".to_string()],
                Some("session-1"),
                "main-model",
                false,
                None,
            )
            .await
            .unwrap();

        let candidates = shortlist_relevant_memories(
            &store,
            Some("proj-1"),
            "release freeze",
            &MemoryRecallOptions::default(),
        )
        .await
        .unwrap();
        assert!(!candidates.is_empty());
        assert!(
            candidates
                .iter()
                .all(|candidate| candidate.scope == MemoryScope::Project)
        );
    }

    #[tokio::test]
    async fn global_fallback_triggers_only_when_project_hits_are_absent() {
        let dir = tempdir().unwrap();
        let store = MemoryStore::new(dir.path());
        store
            .write_memory(
                MemoryScope::Global,
                None,
                DurableMemoryType::Reference,
                "Global release guidance",
                "Fallback note for release work.",
                &["release".to_string()],
                Some("session-1"),
                "main-model",
                false,
                None,
            )
            .await
            .unwrap();

        let candidates = shortlist_relevant_memories(
            &store,
            Some("proj-missing"),
            "release guidance",
            &MemoryRecallOptions::default(),
        )
        .await
        .unwrap();
        assert!(!candidates.is_empty());
        assert!(
            candidates
                .iter()
                .all(|candidate| candidate.scope == MemoryScope::Global)
        );
    }
}