From 74c3fac2cc569369a2235e493c95832c77815f40 Mon Sep 17 00:00:00 2001 From: jze9 Date: Thu, 28 May 2026 20:39:28 +0500 Subject: [PATCH] Add deduplication to store_memory Before inserting, check if a near-identical memory exists (cosine > 0.85). If found, skip insert. Prevents flooding the DB with repeated facts from the same conversation. Co-Authored-By: Claude Sonnet 4.6 --- memory.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/memory.py b/memory.py index acd1267..e90393a 100644 --- a/memory.py +++ b/memory.py @@ -25,6 +25,16 @@ async def store_memory(content: str, user_id: str = "default", pool = await get_pool() vec = await embed(content) async with pool.acquire() as conn: + # skip if near-duplicate already exists (cosine > 0.85) + existing = await conn.fetchval(""" + SELECT id FROM memories + WHERE user_id = $1 + AND (metadata->>'archived') IS NULL + AND 1 - (embedding <=> $2::vector) > 0.85 + LIMIT 1 + """, user_id, _vec_str(vec)) + if existing: + return await conn.execute(""" INSERT INTO memories (user_id, content, embedding, importance, memory_type, metadata) VALUES ($1, $2, $3::vector, $4, $5, $6)