From da85a8aba8726a9333223601ac818b8449d73c7a Mon Sep 17 00:00:00 2001 From: Felipe Cardoso Date: Mon, 5 Jan 2026 17:39:54 +0100 Subject: [PATCH] fix(memory): prevent entry metadata mutation in vector search MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Create shallow copy of VectorIndexEntry when adding similarity score - Prevents mutation of cached entries that could corrupt shared state 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- backend/app/services/memory/indexing/index.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/backend/app/services/memory/indexing/index.py b/backend/app/services/memory/indexing/index.py index 9f3a02c..27fab01 100644 --- a/backend/app/services/memory/indexing/index.py +++ b/backend/app/services/memory/indexing/index.py @@ -197,10 +197,17 @@ class VectorIndex(MemoryIndex[T]): results = [(s, e) for s, e in results if e.memory_type == memory_type] # Store similarity in metadata for the returned entries + # Use a copy of metadata to avoid mutating cached entries output = [] for similarity, entry in results[:limit]: - entry.metadata["similarity"] = similarity - output.append(entry) + # Create a shallow copy of the entry with updated metadata + entry_with_score = VectorIndexEntry( + memory_id=entry.memory_id, + memory_type=entry.memory_type, + embedding=entry.embedding, + metadata={**entry.metadata, "similarity": similarity}, + ) + output.append(entry_with_score) logger.debug(f"Vector search returned {len(output)} results") return output