Skip to content

Commit 704e846

Browse files
committed
Persist memory usage counters
1 parent 0e37094 commit 704e846

1 file changed

Lines changed: 52 additions & 1 deletion

File tree

codex-rs/core/src/memory.rs

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,20 @@ impl MemoryStore for JsonlMemoryStore {
302302
});
303303

304304
let n = ctx.want.max(1).min(8);
305-
Ok(items.into_iter().take(n).collect())
305+
let now = now_rfc3339();
306+
let mut out: Vec<MemoryItem> = Vec::new();
307+
let mut changed = false;
308+
for it in items.iter_mut().take(n) {
309+
it.counters.used_count = it.counters.used_count.saturating_add(1);
310+
it.counters.last_used_at = Some(now.clone());
311+
it.updated_at = now.clone();
312+
changed = true;
313+
out.push(it.clone());
314+
}
315+
if changed {
316+
self.write_all(&items)?;
317+
}
318+
Ok(out)
306319
}
307320
}
308321

@@ -360,4 +373,42 @@ mod tests {
360373
assert!(!out.is_empty());
361374
assert!(out.iter().any(|i| i.content.contains("rg")));
362375
}
376+
377+
#[test]
378+
fn counters_persist_across_recall() {
379+
let dir = tempfile::tempdir().unwrap();
380+
let path = dir.path().join("mem.jsonl");
381+
let store = JsonlMemoryStore::new(&path);
382+
store
383+
.add(MemoryItem::new(
384+
MemoryScope::Global,
385+
MemoryType::Pref,
386+
"Remember me".to_string(),
387+
))
388+
.unwrap();
389+
390+
let ctx = RecallContext {
391+
prompt: "remember",
392+
current_file: None,
393+
current_crate: None,
394+
lang: None,
395+
want: 1,
396+
};
397+
398+
// First recall updates counters
399+
let out1 = store.recall(&ctx).unwrap();
400+
assert_eq!(out1[0].counters.used_count, 1);
401+
let last1 = out1[0].counters.last_used_at.clone();
402+
assert!(last1.is_some());
403+
404+
// Second recall reads persisted counters
405+
let store2 = JsonlMemoryStore::new(&path);
406+
let out2 = store2.recall(&ctx).unwrap();
407+
assert_eq!(out2[0].counters.used_count, 2);
408+
let last2 = out2[0].counters.last_used_at.clone();
409+
assert!(last2.is_some());
410+
if let (Some(a), Some(b)) = (last1, last2) {
411+
assert!(b >= a);
412+
}
413+
}
363414
}

0 commit comments

Comments
 (0)