Repository navigation
fix(core): snapshot in-memory persistence payloads #1492
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
ec2ac0b
fe1081c
e2a6e3a
73ed2d2
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,5 @@ | ||
| --- | ||
| "@voltagent/core": patch | ||
| --- | ||
|
|
||
| Clone in-memory message writes, conversation-step payloads, and conversation/workflow updates so caller mutations do not change saved cloneable data. Detach nested conversation-step results on read. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,259 @@ | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import { Memory } from "../../index"; | ||
| import type { ConversationStepRecord, WorkflowStateEntry } from "../../types"; | ||
| import { InMemoryStorageAdapter } from "./in-memory"; | ||
|
|
||
| const scope = ["user", "conversation"] as const; | ||
|
|
||
| function message(text = "saved") { | ||
| return { | ||
| id: "message", | ||
| role: "user" as const, | ||
| parts: [{ type: "text" as const, text }], | ||
| metadata: { tags: ["saved"] }, | ||
| }; | ||
| } | ||
|
|
||
| function step(overrides: Partial<ConversationStepRecord> = {}): ConversationStepRecord { | ||
| return { | ||
| id: "step", | ||
| userId: scope[0], | ||
| conversationId: scope[1], | ||
| agentId: "agent", | ||
| operationId: "operation", | ||
| stepIndex: 1, | ||
| type: "tool_result", | ||
| role: "assistant", | ||
| createdAt: "2026-01-01T00:00:00Z", | ||
| ...overrides, | ||
| }; | ||
| } | ||
|
|
||
| describe("in-memory persistence snapshots through Memory", () => { | ||
| let memory: Memory; | ||
|
|
||
| beforeEach(async () => { | ||
| memory = new Memory({ storage: new InMemoryStorageAdapter() }); | ||
| await memory.createConversation({ | ||
| id: scope[1], | ||
| userId: scope[0], | ||
| resourceId: "agent", | ||
| title: "Test", | ||
| metadata: {}, | ||
| }); | ||
| }); | ||
|
|
||
| it.each(["single", "batch"])("snapshots %s message writes", async (mode) => { | ||
| const input = message(); | ||
| if (mode === "single") { | ||
| await memory.addMessage(input, ...scope); | ||
| } else { | ||
| await memory.addMessages([input], ...scope); | ||
| } | ||
| input.parts[0].text = "changed after saving"; | ||
| input.metadata.tags.push("changed"); | ||
|
|
||
| const stored = await memory.getMessages(...scope); | ||
| expect(stored[0].parts).toEqual([{ type: "text", text: "saved" }]); | ||
| expect(stored[0].metadata).toMatchObject({ tags: ["saved"] }); | ||
| }); | ||
|
|
||
| it("snapshots replacement message payloads without duplicating the message", async () => { | ||
| await memory.addMessage(message(), ...scope); | ||
| const replacement = message("replacement"); | ||
| await memory.addMessage(replacement, ...scope); | ||
| replacement.parts.push({ type: "text", text: "late mutation" }); | ||
|
|
||
| const stored = await memory.getMessages(...scope); | ||
| expect(stored).toHaveLength(1); | ||
| expect(stored[0].parts).toEqual([{ type: "text", text: "replacement" }]); | ||
| }); | ||
|
|
||
| it("keeps retrieved messages and timestamps detached", async () => { | ||
| await memory.addMessage(message(), ...scope); | ||
| const first = await memory.getMessages(...scope); | ||
| first[0].parts.push({ type: "text", text: "read mutation" }); | ||
| first[0].metadata?.createdAt.setTime(0); | ||
|
|
||
| const next = await memory.getMessages(...scope); | ||
| expect(next[0].parts).toEqual([{ type: "text", text: "saved" }]); | ||
| expect(next[0].metadata?.createdAt).toBeInstanceOf(Date); | ||
| expect(next[0].metadata?.createdAt.getTime()).not.toBe(0); | ||
| }); | ||
|
|
||
| it("snapshots nested step inputs on save", async () => { | ||
| const args = { nested: { value: "saved" } }; | ||
| const result = { items: [{ value: "saved" }] }; | ||
| const usage = { promptTokens: 1, completionTokens: 2, totalTokens: 3 }; | ||
| await memory.saveConversationSteps([step({ arguments: args, result, usage })]); | ||
| args.nested.value = "changed"; | ||
| result.items[0].value = "changed"; | ||
| usage.totalTokens = 99; | ||
|
|
||
| expect((await memory.getConversationSteps(...scope))[0]).toMatchObject({ | ||
| arguments: { nested: { value: "saved" } }, | ||
| result: { items: [{ value: "saved" }] }, | ||
| usage: { totalTokens: 3 }, | ||
| }); | ||
| }); | ||
|
|
||
| it("detaches nested step payloads returned to callers", async () => { | ||
| await memory.saveConversationSteps([ | ||
| step({ arguments: { nested: { value: "saved" } }, result: { items: ["saved"] } }), | ||
| ]); | ||
| const first = await memory.getConversationSteps(...scope); | ||
| (first[0].arguments?.nested as { value: string }).value = "changed"; | ||
| (first[0].result?.items as string[]).push("changed"); | ||
|
|
||
| expect((await memory.getConversationSteps(...scope))[0]).toMatchObject({ | ||
| arguments: { nested: { value: "saved" } }, | ||
| result: { items: ["saved"] }, | ||
| }); | ||
| }); | ||
|
|
||
| it("snapshots step replacements while preserving order, filtering and limits", async () => { | ||
| await memory.saveConversationSteps([ | ||
| step({ id: "first", stepIndex: 2 }), | ||
| step({ id: "second", stepIndex: 1, operationId: "other" }), | ||
| ]); | ||
| const args = { nested: { value: "replacement" } }; | ||
| await memory.saveConversationSteps([step({ id: "first", stepIndex: 3, arguments: args })]); | ||
| args.nested.value = "changed"; | ||
|
|
||
| const all = await memory.getConversationSteps(...scope); | ||
| expect(all.map((record) => record.id)).toEqual(["second", "first"]); | ||
| const filtered = await memory.getConversationSteps(...scope, { | ||
| operationId: "operation", | ||
| limit: 1, | ||
| }); | ||
| expect(filtered).toHaveLength(1); | ||
| expect(filtered[0].arguments).toEqual({ nested: { value: "replacement" } }); | ||
| }); | ||
|
|
||
| it("preserves shallow step isolation when a payload cannot be cloned", async () => { | ||
| await memory.saveConversationSteps([ | ||
| step({ | ||
| arguments: { value: "saved", callback: () => "callback" }, | ||
| result: { value: "saved" }, | ||
| usage: { promptTokens: 1, completionTokens: 2, totalTokens: 3 }, | ||
| }), | ||
| ]); | ||
| const first = (await memory.getConversationSteps(...scope))[0]; | ||
| if (first.arguments) first.arguments.value = "changed"; | ||
| if (first.result) first.result.value = "changed"; | ||
| if (first.usage) first.usage.totalTokens = 99; | ||
| const next = (await memory.getConversationSteps(...scope))[0]; | ||
| expect(next.arguments?.value).toBe("saved"); | ||
| expect(next.result?.value).toBe("saved"); | ||
| expect(next.usage?.totalTokens).toBe(3); | ||
| }); | ||
|
|
||
| it("preserves null step fields and omitted usage", async () => { | ||
| await memory.saveConversationSteps([step({ arguments: null, result: null })]); | ||
| const stored = (await memory.getConversationSteps(...scope))[0]; | ||
| expect(stored.arguments).toBeNull(); | ||
| expect(stored.result).toBeNull(); | ||
| expect(stored.usage).toBeUndefined(); | ||
| expect(Object.hasOwn(stored, "usage")).toBe(true); | ||
| }); | ||
|
|
||
| it("snapshots conversation metadata updates", async () => { | ||
| const metadata = { nested: { value: "saved" } }; | ||
| await memory.updateConversation(scope[1], { metadata }); | ||
| metadata.nested.value = "changed"; | ||
| expect((await memory.getConversation(scope[1]))?.metadata).toEqual({ | ||
| nested: { value: "saved" }, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ["Map with callback", new Map([["callback", () => "saved"]])], | ||
| ["array with callback", [() => "saved"]], | ||
| [ | ||
| "service with callback", | ||
| new (class Service { | ||
| callback = () => "saved"; | ||
| })(), | ||
| ], | ||
| [ | ||
| "service with prototype method", | ||
| new (class Service { | ||
| read() { | ||
| return "saved"; | ||
| } | ||
| })(), | ||
| ], | ||
| ])("preserves the type of context value %s", async (_name, value) => { | ||
| await memory.setWorkflowState("context-types", { | ||
| id: "context-types", | ||
| workflowId: "workflow", | ||
| workflowName: "Test", | ||
| status: "running", | ||
| createdAt: new Date("2026-01-01"), | ||
| updatedAt: new Date("2026-01-01"), | ||
| }); | ||
| const key = Symbol("service"); | ||
| await memory.updateWorkflowState("context-types", { context: [[key, value]] }); | ||
| const restored = await memory.getWorkflowState("context-types"); | ||
| expect(restored?.context?.[0][0]).toBe(key); | ||
| expect(restored?.context?.[0][1]).toBe(value); | ||
| }); | ||
|
|
||
| it.each(["g", "y"])("preserves context RegExp match positions with flag %s", async (flag) => { | ||
| const id = "regexp-context"; | ||
| await memory.setWorkflowState(id, { | ||
| id, | ||
| workflowId: "workflow", | ||
| workflowName: "Test", | ||
| status: "running", | ||
| createdAt: new Date("2026-01-01"), | ||
| updatedAt: new Date("2026-01-01"), | ||
| }); | ||
| const key = Symbol("pattern"); | ||
| const pattern = new RegExp("a", flag); | ||
| pattern.lastIndex = 1; | ||
| await memory.updateWorkflowState(id, { context: [[key, pattern]] }); | ||
| pattern.lastIndex = 0; | ||
|
|
||
| const restored = await memory.getWorkflowState(id); | ||
| const saved = restored?.context?.[0][1] as RegExp; | ||
| expect(restored?.context?.[0][0]).toBe(key); | ||
| expect(saved).toBeInstanceOf(RegExp); | ||
| expect(saved.lastIndex).toBe(1); | ||
| expect(saved.test("aa")).toBe(true); | ||
| expect(saved.lastIndex).toBe(2); | ||
| }); | ||
|
|
||
| it.each(["key", Symbol("key")])("snapshots workflow updates with context key %s", async (key) => { | ||
| const state: WorkflowStateEntry = { | ||
| id: "execution", | ||
| workflowId: "workflow", | ||
| workflowName: "Test", | ||
| status: "running", | ||
| createdAt: new Date("2026-01-01"), | ||
| updatedAt: new Date("2026-01-01"), | ||
| }; | ||
| await memory.setWorkflowState(state.id, state); | ||
| const workflowState = { values: [1] }; | ||
| const input = { values: [2] }; | ||
| const contextValue = { value: 3 }; | ||
| const suspendedAt = new Date("2026-01-02"); | ||
| await memory.updateWorkflowState(state.id, { | ||
| workflowState, | ||
| input, | ||
| context: [[key, contextValue]], | ||
| suspension: { suspendedAt, stepIndex: 0 }, | ||
| }); | ||
| await memory.updateWorkflowState(state.id, { status: "suspended" }); | ||
| workflowState.values.push(99); | ||
| input.values.push(99); | ||
| contextValue.value = 99; | ||
| suspendedAt.setTime(0); | ||
|
|
||
| const stored = await memory.getWorkflowState(state.id); | ||
| expect(stored?.workflowState).toEqual({ values: [1] }); | ||
| expect(stored?.input).toEqual({ values: [2] }); | ||
| expect(stored?.context).toEqual([[key, { value: 3 }]]); | ||
| expect(stored?.suspension?.suspendedAt).toEqual(new Date("2026-01-02")); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -116,7 +116,7 @@ export class InMemoryStorageAdapter implements StorageAdapter { | |
|
|
||
| // Create stored message with metadata | ||
| const storedMessage: StoredUIMessage = { | ||
| ...message, | ||
| ...deepClone(message), | ||
| createdAt: new Date(), | ||
| userId, | ||
| conversationId, | ||
|
|
@@ -212,7 +212,7 @@ export class InMemoryStorageAdapter implements StorageAdapter { | |
| for (const step of steps) { | ||
| const userSteps = this.getOrCreateUserSteps(step.userId); | ||
| const conversationSteps = this.getOrCreateConversationSteps(userSteps, step.conversationId); | ||
| const record = { ...step }; | ||
| const record = deepClone(step); | ||
| const existingIndex = conversationSteps.findIndex((item) => item.id === step.id); | ||
| if (existingIndex >= 0) { | ||
| conversationSteps[existingIndex] = record; | ||
|
|
@@ -246,12 +246,14 @@ export class InMemoryStorageAdapter implements StorageAdapter { | |
| steps = steps.slice(steps.length - options.limit); | ||
| } | ||
|
|
||
| return steps.map((step) => ({ | ||
| ...step, | ||
| arguments: step.arguments ? { ...step.arguments } : step.arguments, | ||
| result: step.result ? { ...step.result } : step.result, | ||
| usage: step.usage ? { ...step.usage } : step.usage, | ||
| })); | ||
| return steps.map((step) => | ||
| deepClone({ | ||
| ...step, | ||
| arguments: step.arguments ? { ...step.arguments } : step.arguments, | ||
| result: step.result ? { ...step.result } : step.result, | ||
| usage: step.usage ? { ...step.usage } : step.usage, | ||
| }), | ||
| ); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -432,7 +434,7 @@ export class InMemoryStorageAdapter implements StorageAdapter { | |
|
|
||
| const updatedConversation: Conversation = { | ||
| ...conversation, | ||
| ...updates, | ||
| ...deepClone(updates), | ||
|
cubic-dev-ai[bot] marked this conversation as resolved.
Resolved
|
||
| updatedAt: new Date().toISOString(), | ||
| }; | ||
|
|
||
|
|
@@ -646,9 +648,28 @@ export class InMemoryStorageAdapter implements StorageAdapter { | |
| throw new Error(`Workflow state ${executionId} not found`); | ||
| } | ||
|
|
||
| const { context, ...otherUpdates } = updates; | ||
| const clonedUpdates: Partial<WorkflowStateEntry> = deepClone(otherUpdates); | ||
| if (Object.hasOwn(updates, "context")) { | ||
| clonedUpdates.context = context?.map(([key, value]) => { | ||
| const clonedValue = deepClone(value); | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| if (value instanceof RegExp && clonedValue instanceof RegExp) { | ||
| clonedValue.lastIndex = value.lastIndex; | ||
|
Contributor
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. P2: The Prompt for AI agents |
||
| } | ||
| if ( | ||
| value !== null && | ||
| typeof value === "object" && | ||
|
Comment on lines
+655
to
+661
Contributor
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.
|
||
| Object.getPrototypeOf(value) !== Object.getPrototypeOf(clonedValue) | ||
| ) { | ||
| return [key, value]; | ||
| } | ||
| return [key, clonedValue]; | ||
| }); | ||
| } | ||
|
|
||
| const updated: WorkflowStateEntry = { | ||
| ...existing, | ||
| ...updates, | ||
| ...clonedUpdates, | ||
| updatedAt: new Date(), | ||
| }; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.