memory-search: deduplicate seen set entries

mark_seen now takes the in-memory HashSet and checks before appending.
Prevents the same key being written 30+ times from repeated search hits
and context reloads.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Kent Overstreet 2026-03-22 05:00:26 -04:00
parent 38ad2ef4be
commit de36c0d39e

View file

@ -145,12 +145,13 @@ fn main() {
let ctx = String::from_utf8_lossy(&output.stdout).to_string(); let ctx = String::from_utf8_lossy(&output.stdout).to_string();
if !ctx.trim().is_empty() { if !ctx.trim().is_empty() {
// Extract keys from all chunks for seen set // Extract keys from all chunks for seen set
let mut ctx_seen = load_seen(&state_dir, session_id);
for line in ctx.lines() { for line in ctx.lines() {
if line.starts_with("--- ") && line.ends_with(" ---") { if line.starts_with("--- ") && line.ends_with(" ---") {
let inner = &line[4..line.len() - 4]; let inner = &line[4..line.len() - 4];
if let Some(paren) = inner.rfind(" (") { if let Some(paren) = inner.rfind(" (") {
let key = inner[..paren].trim(); let key = inner[..paren].trim();
mark_seen(&state_dir, session_id, key); mark_seen(&state_dir, session_id, key, &mut ctx_seen);
} }
} }
} }
@ -312,7 +313,7 @@ fn main() {
return; return;
} }
let seen = load_seen(&state_dir, session_id); let mut seen = load_seen(&state_dir, session_id);
if debug { println!("[memory-search] {} keys in seen set", seen.len()); } if debug { println!("[memory-search] {} keys in seen set", seen.len()); }
// Format results like poc-memory search output // Format results like poc-memory search output
@ -332,7 +333,7 @@ fn main() {
if let Some(key) = extract_key_from_line(trimmed) { if let Some(key) = extract_key_from_line(trimmed) {
if seen.contains(&key) { continue; } if seen.contains(&key) { continue; }
mark_seen(&state_dir, session_id, &key); mark_seen(&state_dir, session_id, &key, &mut seen);
mark_returned(&state_dir, session_id, &key); mark_returned(&state_dir, session_id, &key);
result_output.push_str(line); result_output.push_str(line);
result_output.push('\n'); result_output.push('\n');
@ -641,7 +642,8 @@ fn load_seen(dir: &Path, session_id: &str) -> HashSet<String> {
} }
} }
fn mark_seen(dir: &Path, session_id: &str, key: &str) { fn mark_seen(dir: &Path, session_id: &str, key: &str, seen: &mut HashSet<String>) {
if !seen.insert(key.to_string()) { return; }
let path = dir.join(format!("seen-{}", session_id)); let path = dir.join(format!("seen-{}", session_id));
if let Ok(mut f) = fs::OpenOptions::new().create(true).append(true).open(path) { if let Ok(mut f) = fs::OpenOptions::new().create(true).append(true).open(path) {
let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S"); let ts = chrono::Local::now().format("%Y-%m-%dT%H:%M:%S");