fix(core): snapshot in-memory persistence payloads

EL
21d ago
+295-10

Summary

Snapshot cloneable data written to InMemoryStorageAdapter and detach nested conversation-step payloads on read. This prevents mutations made after a persistence call resolves (or to returned steps) from silently changing saved records.

Reproduction

Using the public Memory facade with new InMemoryStorageAdapter():

const input = {
  id: "message",
  role: "user" as const,
  parts: [{ type: "text" as const, text: "saved" }],
};
await memory.addMessage(input, userId, conversationId);
input.parts[0].text = "changed after saving";
// Before: getMessages returns "changed after saving".
// After: it returns the persisted "saved" value.

The same aliasing affects nested arguments/results/usage in conversation steps, conversation metadata updates and workflow updates. Existing reads/creates already use the adapter's deepClone helper; these paths were inconsistent.

Changes

  • Use the existing deepClone helper for message writes, step saves, conversation updates and workflow updates.
  • Deep-clone returned conversation steps while preserving optional-field shape, ordering, filtering, limits and replacement behavior.
  • Add eighteen regressions/controls through Memory, plus a core patch changeset.

This retains the existing clone helper's fallback behavior for non-cloneable values. It does not introduce serialization rules or promise deep isolation for values that helper cannot clone. Workflow updates copy context keys unchanged and clone their values separately, so a supported Symbol key does not force other update fields to fall back to shallow copies. Step reads retain their previous top-level field copies when a non-cloneable value triggers the helper fallback. Context values retain their original reference if cloning changes their prototype (for example an array, Map or service instance becoming a plain object). These fallback values retain their type rather than promising deep isolation. Direct RegExp context values retain lastIndex in their clone, including global and sticky patterns. This is a targeted compatibility correction; it does not add a general-purpose serializer for arbitrary object graphs. No API or dependency changes.

Validation

  • Initial ten-test regression file against unchanged upstream main: 8 failed, 2 passed; patched source restored byte-for-byte afterwards.
  • Review regressions against the first PR revision: 2 failed, 10 passed (Symbol context keys and non-cloneable step payloads).
  • Context type regressions against the previous PR head: 4 failed, 12 passed.
  • Previous memory tests and agent persistence queue/integration tests: 138 passed across eight files.
  • RegExp review regressions against the previous PR head: 2 failed, 16 passed; current complete memory tests: 133 passed across six files, plus 11 passed across three agent persistence files.
  • Core TypeScript check and ESM/CommonJS/declaration builds: passed.
  • Public built-export ESM/CommonJS checks: 22 passed.
  • Biome, changeset Prettier check, changeset release-plan validation and git diff --check: passed.

Offline validation only; no live provider or browser calls. Existing task-owned dependencies were reused with the same lockfile. The full monorepo suite was not run.

Contribution notes

Related issue: none; reproduced directly from current main. Prepared and validated with OpenAI Codex.

  • Conventional commit
  • Regression tests
  • Patch changeset for @voltagent/core

Summary by CodeRabbit

  • Bug Fixes
    • In-memory storage now keeps saved messages, conversation steps, conversation updates, and workflow state isolated from later changes to the original data, including replacements.
    • Values returned from storage are also detached, helping prevent changes to retrieved data from affecting stored records.
    • Workflow context values retain their types and regular-expression state. Step ordering, filtering, limits, and handling of null or omitted fields remain unchanged.