Repository navigation
fix(core): isolate vector metadata and search results #1445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
liangzhechen655
wants to merge
6
commits into
VoltAgent:main
Choose a base branch
from
liangzhechen655:fix/isolated-vector-search-results
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fcacf03
fix(core): isolate in-memory vector search results
liangzhechen655 d7141aa
fix(core): deep-clone vector metadata at storage boundaries
liangzhechen655 8c41f95
fix(internal): recursively clone metadata in the fallback
liangzhechen655 a3f41af
fix(internal): retain binary storage and aliases when cloning metadata
liangzhechen655 fbcc59b
fix(internal): preserve binary properties and document metadata compa…
liangzhechen655 d2eb7dc
docs(core): clarify cloned metadata prototype methods
liangzhechen655 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| --- | ||
|
|
||
| Copy embeddings and metadata in in-memory vector search results so caller mutations do not corrupt stored memories or change subsequent user-filtered searches. |
44 changes: 44 additions & 0 deletions
44
packages/core/src/memory/adapters/vector/in-memory.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { InMemoryVectorAdapter } from "./in-memory"; | ||
|
|
||
| describe("InMemoryVectorAdapter search result isolation", () => { | ||
| it("does not let a returned embedding corrupt future similarity searches", async () => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| await adapter.store("matching", [1, 0], { userId: "alice" }); | ||
| await adapter.store("other", [0, 1], { userId: "alice" }); | ||
|
|
||
| const [result] = await adapter.search([1, 0]); | ||
| result.vector[0] = 0; | ||
| result.vector[1] = 1; | ||
|
|
||
| expect((await adapter.get("matching"))?.vector).toEqual([1, 0]); | ||
| const [nextResult] = await adapter.search([1, 0]); | ||
| expect(nextResult.id).toBe("matching"); | ||
| expect(nextResult.score).toBe(1); | ||
| }); | ||
|
|
||
| it("does not let returned metadata change user-scoped retrieval", async () => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| await adapter.store("memory", [1, 0], { userId: "alice" }); | ||
|
|
||
| const [result] = await adapter.search([1, 0], { filter: { userId: "alice" } }); | ||
| if (!result.metadata) throw new Error("Expected stored metadata"); | ||
| result.metadata.userId = "bob"; | ||
|
|
||
| expect(await adapter.search([1, 0], { filter: { userId: "bob" } })).toEqual([]); | ||
| expect(await adapter.search([1, 0], { filter: { userId: "alice" } })).toHaveLength(1); | ||
| expect((await adapter.get("memory"))?.metadata).toEqual({ userId: "alice" }); | ||
| }); | ||
|
|
||
| it("supports results without metadata and keeps stored input isolated", async () => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| const vector = [1, 0]; | ||
| await adapter.store("memory", vector); | ||
| vector[0] = 0; | ||
|
|
||
| const [result] = await adapter.search([1, 0]); | ||
| expect(result.vector).toEqual([1, 0]); | ||
| expect(result.metadata).toBeUndefined(); | ||
| expect(result.score).toBe(1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.