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
base: main
Are you sure you want to change the base?
Changes from all commits
fcacf03
d7141aa
8c41f95
a3f41af
fbcc59b
d2eb7dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| "@voltagent/internal": patch | ||
| --- | ||
|
|
||
| Copy embeddings in in-memory vector search results and deep-clone metadata at the store, get and search boundaries so caller mutations do not corrupt stored memories or change subsequent user-filtered searches. Only selected search results are cloned. Make the internal deep-clone fallback recursive, preserving cycles and shared references when metadata contains functions. | ||
|
|
||
| Compatibility note: nested custom class instances in vector metadata are now copied as plain data objects, rather than returned by reference. Their prototypes and prototype methods are not retained, so `instanceof` checks against the original class no longer succeed. Store plain data records (for example, the result of an explicit serializer) and reconstruct application-specific classes after retrieval if needed. Functions retain their existing references in the fallback. | ||
|
|
||
| The fallback copies binary storage using the built-in buffer slice methods and preserves own enumerable properties on buffers and views, including cyclic references. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| import { describe, expect, it } from "vitest"; | ||
| import { InMemoryVectorAdapter } from "./in-memory"; | ||
|
|
||
| describe("InMemoryVectorAdapter search result isolation", () => { | ||
| it.each(["store", "get", "search"])( | ||
| "isolates metadata with an overridden buffer slice at the %s boundary", | ||
| async (operation) => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| const buffer = new ArrayBuffer(4); | ||
| Object.defineProperty(buffer, "slice", { value: () => buffer, enumerable: true }); | ||
| new Uint8Array(buffer)[0] = 1; | ||
| await adapter.store("memory", [1, 0], { fn: () => "test", buffer }); | ||
| if (operation === "store") { | ||
| new Uint8Array(buffer)[0] = 9; | ||
| } else { | ||
| const result = | ||
| operation === "search" ? (await adapter.search([1, 0]))[0] : await adapter.get("memory"); | ||
| new Uint8Array(result?.metadata?.buffer as ArrayBuffer)[0] = 9; | ||
| } | ||
| const stored = (await adapter.get("memory"))?.metadata?.buffer as ArrayBuffer; | ||
| expect(new Uint8Array(stored)[0]).toBe(1); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(["native", "fallback"])( | ||
| "copies custom class metadata as plain data using the %s clone path", | ||
| async (mode) => { | ||
| class CustomMetadata { | ||
| labels = ["original"]; | ||
| label() { | ||
| return this.labels[0]; | ||
| } | ||
| } | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| const item = new CustomMetadata(); | ||
| await adapter.store("memory", [1, 0], { | ||
| item, | ||
| fn: mode === "fallback" ? () => "test" : undefined, | ||
| }); | ||
| item.labels.push("changed"); | ||
| const fetched = await adapter.get("memory"); | ||
| const [searched] = await adapter.search([1, 0]); | ||
| for (const result of [fetched, searched]) { | ||
| const copied = result?.metadata?.item as CustomMetadata; | ||
| expect(Object.getPrototypeOf(copied)).toBe(Object.prototype); | ||
| expect(copied).not.toBeInstanceOf(CustomMetadata); | ||
| expect(copied.label).toBeUndefined(); | ||
| expect(copied.labels).toEqual(["original"]); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it.each(["store", "get", "search"])( | ||
| "preserves and isolates binary metadata at the %s boundary", | ||
| async (operation) => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| const bytes = new Uint8Array([1, 2, 3]); | ||
| await adapter.store("memory", [1, 0], { fn: () => "test", bytes }); | ||
| if (operation === "store") { | ||
| bytes[0] = 9; | ||
| } else { | ||
| const result = | ||
| operation === "search" ? (await adapter.search([1, 0]))[0] : await adapter.get("memory"); | ||
| const resultBytes = result?.metadata?.bytes as Uint8Array; | ||
| expect(resultBytes.byteLength).toBe(3); | ||
| resultBytes[0] = 9; | ||
| } | ||
| const storedBytes = (await adapter.get("memory"))?.metadata?.bytes as Uint8Array; | ||
| expect(storedBytes.byteLength).toBe(3); | ||
| expect([...storedBytes]).toEqual([1, 2, 3]); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(["search", "get", "store"])( | ||
| "isolates nested metadata containing functions at the %s boundary", | ||
| async (operation) => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| const fn = () => "label"; | ||
| const metadata = { context: { labels: ["original"] }, fn }; | ||
| await adapter.store("memory", [1, 0], metadata); | ||
|
|
||
| if (operation === "store") { | ||
| metadata.context.labels.push("changed"); | ||
| } else { | ||
| const result = | ||
| operation === "search" ? (await adapter.search([1, 0]))[0] : await adapter.get("memory"); | ||
| const context = result?.metadata?.context as typeof metadata.context; | ||
| context.labels.push("changed"); | ||
| } | ||
|
|
||
| expect((await adapter.get("memory"))?.metadata).toEqual({ | ||
| context: { labels: ["original"] }, | ||
| fn, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| it.each(["search", "get", "store"])( | ||
| "isolates nested metadata at the %s boundary", | ||
| async (operation) => { | ||
| const adapter = new InMemoryVectorAdapter(); | ||
| const metadata = { context: { labels: ["original"] } }; | ||
| await adapter.store("memory", [1, 0], metadata); | ||
|
|
||
| if (operation === "store") { | ||
| metadata.context.labels.push("changed"); | ||
| } else { | ||
| const result = | ||
| operation === "search" ? (await adapter.search([1, 0]))[0] : await adapter.get("memory"); | ||
| const context = result?.metadata?.context as typeof metadata.context; | ||
| context.labels.push("changed"); | ||
| } | ||
|
|
||
| expect((await adapter.get("memory"))?.metadata).toEqual({ | ||
| context: { labels: ["original"] }, | ||
| }); | ||
| }, | ||
| ); | ||
|
|
||
| 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); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,11 @@ | ||
| import { deepClone } from "@voltagent/internal/utils"; | ||
| import { cosineSimilarity } from "../../utils/vector-math"; | ||
| import type { SearchResult, VectorAdapter, VectorItem } from "./types"; | ||
|
|
||
| /** | ||
| * Lightweight in-memory vector database adapter | ||
| * Suitable for development, testing, and small datasets (< 10k vectors) | ||
| * Metadata is copied as data; custom class prototypes and prototype methods are not retained. | ||
| */ | ||
| export class InMemoryVectorAdapter implements VectorAdapter { | ||
| private vectors: Map<string, VectorItem>; | ||
|
|
@@ -26,7 +28,7 @@ export class InMemoryVectorAdapter implements VectorAdapter { | |
| this.vectors.set(id, { | ||
| id, | ||
| vector: [...vector], // Clone to prevent external modifications | ||
| metadata: metadata ? { ...metadata } : undefined, | ||
| metadata: metadata ? deepClone(metadata) : undefined, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This used to be |
||
| }); | ||
| } | ||
|
|
||
|
|
@@ -83,7 +85,11 @@ export class InMemoryVectorAdapter implements VectorAdapter { | |
| // Sort by score (descending) and limit results | ||
| results.sort((a, b) => b.score - a.score); | ||
|
|
||
| return results.slice(0, limit); | ||
| return results.slice(0, limit).map((item) => ({ | ||
| ...item, | ||
| vector: [...item.vector], | ||
| metadata: item.metadata ? deepClone(item.metadata) : undefined, | ||
| })); | ||
| } | ||
|
|
||
| async delete(id: string): Promise<void> { | ||
|
|
@@ -115,7 +121,7 @@ export class InMemoryVectorAdapter implements VectorAdapter { | |
| return { | ||
| ...item, | ||
| vector: [...item.vector], | ||
| metadata: item.metadata ? { ...item.metadata } : undefined, | ||
| metadata: item.metadata ? deepClone(item.metadata) : undefined, | ||
| }; | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.