forked from cardosofelipe/fast-next-template
fix(memory): prevent entry metadata mutation in vector search
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -197,10 +197,17 @@ class VectorIndex(MemoryIndex[T]):
|
|||||||
results = [(s, e) for s, e in results if e.memory_type == memory_type]
|
results = [(s, e) for s, e in results if e.memory_type == memory_type]
|
||||||
|
|
||||||
# Store similarity in metadata for the returned entries
|
# Store similarity in metadata for the returned entries
|
||||||
|
# Use a copy of metadata to avoid mutating cached entries
|
||||||
output = []
|
output = []
|
||||||
for similarity, entry in results[:limit]:
|
for similarity, entry in results[:limit]:
|
||||||
entry.metadata["similarity"] = similarity
|
# Create a shallow copy of the entry with updated metadata
|
||||||
output.append(entry)
|
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")
|
logger.debug(f"Vector search returned {len(output)} results")
|
||||||
return output
|
return output
|
||||||
|
|||||||
Reference in New Issue
Block a user