Skip to content

Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.

fix(memory): preserve all patch blocks when creating a new memory (#3682)

bianbiandashen4ede9c49bdb8
diff --git a/openviking/session/memory/merge_op/patch.py b/openviking/session/memory/merge_op/patch.py
index 76281e4fe..3758c68cd 100644
--- a/openviking/session/memory/merge_op/patch.py
+++ b/openviking/session/memory/merge_op/patch.py
@@ -118,21 +118,28 @@ class PatchOp(MergeOpBase):
Returns:
The replace content, or empty string if not available
"""
- from openviking.session.memory.merge_op.base import StrPatch
-
- # Case 1: StrPatch object
+ # Case 1: StrPatch object — concatenate ALL blocks' replace content.
+ # The schema instructs the model to split non-adjacent edits into
+ # separate blocks, so a new memory routinely arrives as a multi-block
+ # patch. Taking only blocks[0] would silently drop every subsequent
+ # fact/preference the model extracted.
if isinstance(patch_value, StrPatch):
- replace = patch_value.get_first_replace()
- return replace if replace is not None else ""
+ replaces = [b.replace for b in patch_value.blocks if b.replace is not None]
+ return "\n".join(replaces) if replaces else ""
- # Case 2: dict form
+ # Case 2: dict form (from JSON parsing) — same, collect every block.
if isinstance(patch_value, dict) and "blocks" in patch_value:
- blocks = patch_value.get("blocks", [])
- if blocks:
- first_block = blocks[0]
- if isinstance(first_block, dict):
- replace = first_block.get("replace")
- return replace if replace is not None else ""
+ replaces = []
+ for block in patch_value.get("blocks", []):
+ if isinstance(block, SearchReplaceBlock):
+ replace = block.replace
+ elif isinstance(block, dict):
+ replace = block.get("replace")
+ else:
+ replace = None
+ if replace is not None:
+ replaces.append(replace)
+ return "\n".join(replaces) if replaces else ""
# Case 3: Simple string - use as is
if isinstance(patch_value, str):
diff --git a/tests/unit/session/memory/test_patch_no_original_multiblock.py b/tests/unit/session/memory/test_patch_no_original_multiblock.py
new file mode 100644
index 000000000..db58e86b4
--- /dev/null
+++ b/tests/unit/session/memory/test_patch_no_original_multiblock.py
@@ -0,0 +1,66 @@
+"""Regression tests: all blocks must be preserved when creating a new memory.
+
+When ``current_value is None`` (the memory file does not exist yet),
+``PatchOp._extract_replace_when_no_original`` previously took only the
+first block's replace content — silently discarding every subsequent block.
+
+The schema instructs the LLM to "split non-adjacent edits into separate
+blocks." A new memory therefore routinely arrives as a multi-block patch
+(e.g. one block per extracted fact or preference). Dropping all but the
+first block means every fact after the first is silently lost with no log,
+warning, or error visible to the caller.
+"""
+
+from __future__ import annotations
+
+import pytest
+
+from openviking.session.memory.merge_op.base import FieldType, SearchReplaceBlock, StrPatch
+from openviking.session.memory.merge_op.patch import PatchOp
+
+
+class TestNewMemoryMultiBlockPatch:
+ """PatchOp: all blocks must survive when writing a new memory."""
+
+ def test_strpatch_multiblock_no_silent_loss(self):
+ """All StrPatch blocks are joined when current_value is None."""
+ op = PatchOp(FieldType.STRING)
+ patch = StrPatch(
+ blocks=[
+ SearchReplaceBlock(search="", replace="Fact A: user prefers dark mode."),
+ SearchReplaceBlock(search="", replace="Fact B: user is in Tokyo."),
+ ]
+ )
+ result = op.apply(None, patch)
+ assert "Fact A" in result, "block 0 content should be present"
+ assert "Fact B" in result, "block 1 content was silently dropped before the fix"
+
+ def test_dict_form_multiblock_no_silent_loss(self):
+ """dict-form blocks (the actual JSON-parsed LLM path) are all joined."""
+ op = PatchOp(FieldType.STRING)
+ patch_value = {
+ "blocks": [
+ {"search": "", "replace": "Line 1 preference"},
+ {"search": "", "replace": "Line 2 fact"},
+ ]
+ }
+ result = op.apply(None, patch_value)
+ assert "Line 1" in result
+ assert "Line 2" in result, "second dict block was silently dropped before the fix"
+
+ def test_single_block_behaviour_unchanged(self):
+ """The common single-block case is unaffected by this change."""
+ op = PatchOp(FieldType.STRING)
+ patch = StrPatch(blocks=[SearchReplaceBlock(search="", replace="Only fact.")])
+ result = op.apply(None, patch)
+ assert result == "Only fact."
+
+ def test_existing_content_path_unaffected(self):
+ """When current_value is NOT None the existing search/replace path runs."""
+ op = PatchOp(FieldType.STRING)
+ patch = StrPatch(
+ blocks=[SearchReplaceBlock(search="old text", replace="new text")]
+ )
+ result = op.apply("some old text here", patch)
+ assert "new text" in result
+ assert "old text" not in result