Skip to content

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

fix(session): accept string edits in replace mode (#6286)

chenjwf754fa90fdcc
diff --git a/openviking/session/memory/extract_loop.py b/openviking/session/memory/extract_loop.py
index 75cbd6271..a00f97c40 100644
--- a/openviking/session/memory/extract_loop.py
+++ b/openviking/session/memory/extract_loop.py
@@ -1325,13 +1325,21 @@ class ExtractLoop:
)
from openviking.session.memory.merge_op.patch_handler import unescape_markers
+ def field_value(memory_file: MemoryFile, field_name: str) -> str:
+ value = (
+ memory_file.plain_content()
+ if field_name == "content"
+ else memory_file.extra_fields.get(field_name)
+ )
+ # Other memory types can use the same field name for non-string data.
+ return value if isinstance(value, str) else ""
+
errors = []
patch_op = PatchOp(FieldType.STRING)
read_files = self.context_provider.read_file_contents or {}
for operation in operations.upsert_operations:
if operation.old_memory_file_content is None:
continue
- current_content = operation.old_memory_file_content.plain_content() or ""
target_uri = (
operation.uris[0] if operation.uris else operation.old_memory_file_content.uri
)
@@ -1347,7 +1355,7 @@ class ExtractLoop:
blocks.extend(StrPatch.model_validate({"blocks": [raw_block]}).blocks)
if not blocks:
continue
- working_content = current_content
+ working_content = field_value(operation.old_memory_file_content, field_name)
for block_index, block in enumerate(blocks, start=1):
search = block.search or ""
if not search or search == block.replace:
@@ -1377,7 +1385,7 @@ class ExtractLoop:
found_in = [
uri
for uri, memory_file in read_files.items()
- if uri != target_uri and search in (memory_file.plain_content() or "")
+ if uri != target_uri and search in field_value(memory_file, field_name)
]
errors.append(
{
diff --git a/openviking/session/memory/extraction_output_protocol/python_protocol.py b/openviking/session/memory/extraction_output_protocol/python_protocol.py
index 953bce5a5..5b6c1e2f8 100644
--- a/openviking/session/memory/extraction_output_protocol/python_protocol.py
+++ b/openviking/session/memory/extraction_output_protocol/python_protocol.py
@@ -344,6 +344,8 @@ class PythonExtractionOutputProtocol(ExtractionOutputProtocol):
"use text from the conversation or the new facts you intend to add as a search anchor. "
"If you copy from numbered read output, exclude the `line_number<TAB>` prefix. If the "
"change is too broad for a clean snippet, use obj.field.update(complete_new_value) instead. "
+ "For fields marked [replace], use obj.field.update(complete_new_value), including "
+ "all unchanged text. "
"Regenerate the complete restricted Python SDK program, including previous successful "
"operations and fixed failed operations. Output only Python code."
"\n\nFailed edits:\n" + details
@@ -1108,10 +1110,9 @@ class _PythonProgramCompiler:
# Preserve established values, but allow filling a blank template field.
return
if handle.full_value is not _UNSET:
- owner.fields[name] = handle.full_value
- owner.changed_fields[name] = handle.full_value
+ self._set_changed_field(owner, name, handle.full_value, node)
return
- # Local edit chain: only valid for existing PATCH string fields.
+ # Replace strings still advertise update(), but accept an explicit edit at runtime.
if not owner.existing:
self._error(
node,
@@ -1119,18 +1120,14 @@ class _PythonProgramCompiler:
"this program has no prior content — pass the complete value to field.update()",
)
if field_schema is None or not (
- field_schema.merge_op == MergeOp.PATCH and field_schema.field_type == FieldType.STRING
+ field_schema.merge_op in {MergeOp.PATCH, MergeOp.REPLACE}
+ and field_schema.field_type == FieldType.STRING
):
- guidance = "edit()/drop() require an editable string (merge_op=patch)"
- if field_schema is not None and (
- field_schema.merge_op == MergeOp.REPLACE
- and field_schema.field_type == FieldType.STRING
- ):
- guidance = (
- "merge_op=replace requires the complete new value, including unchanged text; "
- f'use `{owner.name}.{_identifier_alias(name)}.update("""complete new value""")`'
- )
- self._error(node, f"field {name!r} does not support edit()/drop(): {guidance}")
+ self._error(
+ node,
+ f"field {name!r} does not support edit()/drop(): "
+ "edit()/drop() require a string with merge_op=patch or replace",
+ )
# Do NOT apply here. Store the edits as a StrPatch so python mode flows through
# the same resolve_operations -> _validate_patch_operations -> patch-repair path
# as json mode: a failed snippet is isolated to its own operation and gets the
@@ -1139,8 +1136,34 @@ class _PythonProgramCompiler:
existing = owner.changed_fields.get(name)
prior_blocks = existing.blocks if isinstance(existing, StrPatch) else []
patch = StrPatch(blocks=[*prior_blocks, *handle.blocks])
- owner.fields[name] = patch
- owner.changed_fields[name] = patch
+ self._set_changed_field(owner, name, patch, node)
+
+ def _set_changed_field(
+ self, owner: _MemoryObject, name: str, value: Any, node: ast.AST
+ ) -> None:
+ field_schema = next(
+ (item for item in self.schemas[owner.memory_type].fields if item.name == name), None
+ )
+ previous = owner.changed_fields.get(name)
+ if (
+ owner.existing
+ and field_schema is not None
+ and field_schema.merge_op == MergeOp.REPLACE
+ and field_schema.field_type == FieldType.STRING
+ and (
+ isinstance(previous, str)
+ and isinstance(value, (StrPatch, dict))
+ or isinstance(previous, (StrPatch, dict))
+ and isinstance(value, str)
+ )
+ ):
+ self._error(
+ node,
+ f"replace field {name!r} cannot mix complete values and patches in one program; "
+ "use complete update() values or only edit()/drop() patches",
+ )
+ owner.fields[name] = value
+ owner.changed_fields[name] = value
def _eval_keywords(
self,
@@ -1194,8 +1217,7 @@ class _PythonProgramCompiler:
owner.changed_fields[name] = previous_delta + changes[name]
owner.fields[name] = value
continue
- owner.fields[name] = value
- owner.changed_fields[name] = value
+ self._set_changed_field(owner, name, value, node)
def _build_operations(self) -> Any:
payload: dict[str, Any] = {name: [] for name in self.context.operations_model.model_fields}
diff --git a/openviking/session/memory/merge_op/factory.py b/openviking/session/memory/merge_op/factory.py
index e2da43b30..75a2ce140 100644
--- a/openviking/session/memory/merge_op/factory.py
+++ b/openviking/session/memory/merge_op/factory.py
@@ -33,7 +33,7 @@ class MergeOpFactory:
if merge_op == MergeOp.PATCH:
return PatchOp(field_type)
elif merge_op == MergeOp.REPLACE:
- return ReplaceOp()
+ return ReplaceOp(field_type)
elif merge_op == MergeOp.SUM:
return SumOp()
elif merge_op == MergeOp.IMMUTABLE:
diff --git a/openviking/session/memory/merge_op/replace.py b/openviking/session/memory/merge_op/replace.py
index 79f820a1b..9c5abf827 100644
--- a/openviking/session/memory/merge_op/replace.py
+++ b/openviking/session/memory/merge_op/replace.py
@@ -1,20 +1,24 @@
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: AGPL-3.0
"""
-Replace merge operation - full replacement, no SEARCH/REPLACE blocks.
+Prefer complete replacement values while tolerating runtime string edits.
-Use this instead of patch when the field should always be fully rewritten
-(e.g., structured documents where holistic synthesis is preferable to
-surgical patching). The LLM receives a plain `str` output type, so it
-cannot accidentally output StrPatch blocks.
+The model-facing schema remains a plain string. An edit produced despite that
+instruction follows the normal patch-validation path and is materialized against
+the current field value when applied, rather than rejected by the DSL compiler.
"""
-from typing import Any, Type
+from __future__ import annotations
+
+from typing import Annotated, Any, Union
+
+from pydantic import WithJsonSchema
from openviking.session.memory.merge_op.base import (
FieldType,
MergeOp,
MergeOpBase,
+ StrPatch,
get_python_type_for_field,
)
@@ -24,7 +28,13 @@ class ReplaceOp(MergeOpBase):
op_type = MergeOp.REPLACE
- def get_output_schema_type(self, field_type: FieldType) -> Type[Any]:
+ def __init__(self, field_type: FieldType = FieldType.STRING):
+ self._field_type = field_type
+
+ def get_output_schema_type(self, field_type: FieldType) -> Any:
+ if field_type == FieldType.STRING:
+ # Accept edits at runtime without advertising them to the model.
+ return Annotated[Union[str, StrPatch], WithJsonSchema({"type": "string"})]
return get_python_type_for_field(field_type)
def get_output_schema_description(self, field_description: str) -> str:
@@ -35,6 +45,21 @@ class ReplaceOp(MergeOpBase):
)
async def apply(self, current_value: Any, patch_value: Any) -> Any:
+ if self._field_type == FieldType.STRING and (
+ isinstance(patch_value, StrPatch)
+ or isinstance(patch_value, dict)
+ and "blocks" in patch_value
+ ):
+ from openviking.session.memory.merge_op.patch import PatchOp
+
+ patch = (
+ patch_value
+ if isinstance(patch_value, StrPatch)
+ else StrPatch.model_validate(patch_value)
+ )
+ return await PatchOp(FieldType.STRING).apply(
+ "" if current_value is None else current_value, patch
+ )
if patch_value is None or patch_value == "":
return current_value
return patch_value
diff --git a/openviking/session/memory/schema_model_generator.py b/openviking/session/memory/schema_model_generator.py
index ac2cce7ee..52dcb5c46 100644
--- a/openviking/session/memory/schema_model_generator.py
+++ b/openviking/session/memory/schema_model_generator.py
@@ -189,9 +189,15 @@ class SchemaModelGenerator:
Field(None, description=desc),
)
- # Create the model
+ replace_string_fields = [
+ field.name
+ for field in memory_type.fields
+ if field.merge_op == MergeOp.REPLACE and field.field_type == FieldType.STRING
+ ]
+
+ # New objects have no stored value on which to apply a string patch.
@model_validator(mode="before")
- def require_new_object_identity(cls, data):
+ def validate_new_object_fields(cls, data):
del cls
if not isinstance(data, dict):
return data
@@ -202,6 +208,17 @@ class SchemaModelGenerator:
missing = [name for name in required_on_create if data.get(name) is None]
if is_new and missing:
raise ValueError("new memory item requires fields: " + ", ".join(missing))
+ if is_new:
+ invalid = [
+ name
+ for name in replace_string_fields
+ if data.get(name) is not None and not isinstance(data[name], str)
+ ]
+ if invalid:
+ raise ValueError(
+ "new memory item requires a complete string for replace fields: "
+ + ", ".join(invalid)
+ )
return data
json_schema_extra = None
@@ -221,7 +238,7 @@ class SchemaModelGenerator:
model = create_model(
model_name,
__config__=ConfigDict(extra="ignore", json_schema_extra=json_schema_extra),
- __validators__={"require_new_object_identity": require_new_object_identity},
+ __validators__={"validate_new_object_fields": validate_new_object_fields},
**field_definitions,
)
diff --git a/tests/session/memory/test_extraction_output_protocol.py b/tests/session/memory/test_extraction_output_protocol.py
index 2615f8a24..4d566b5eb 100644
--- a/tests/session/memory/test_extraction_output_protocol.py
+++ b/tests/session/memory/test_extraction_output_protocol.py
@@ -1277,7 +1277,7 @@ sdk.commit()
@pytest.mark.parametrize("memory_type", ["experiences", "entities"])
@pytest.mark.parametrize("method", ['edit(search="old", replace="new")', 'drop(text="old")'])
-def test_python_builtin_content_edit_respects_merge_op_and_offers_valid_repair(memory_type, method):
+def test_python_builtin_content_edit_accepts_replace_but_keeps_update_guidance(memory_type, method):
registry = MemoryTypeRegistry(load_schemas=False)
registry.load_from_yaml(str(resolve_memory_templates_dir() / f"{memory_type}.yaml"))
schema = registry.get(memory_type)
@@ -1301,11 +1301,10 @@ def test_python_builtin_content_edit_respects_merge_op_and_offers_valid_repair(m
_bind(protocol, context)
operations, error = protocol.parse(f"{memory_type}_1.content.{method}", context)
- if memory_type == "entities":
- assert error is None
- assert operations is not None
- assert operations.model_dump()[memory_type][0]["content"]["blocks"]
- else:
+ assert error is None
+ assert operations is not None
+ assert operations.model_dump()[memory_type][0]["content"]["blocks"]
+ if memory_type == "experiences":
contract = protocol.render_contract(context)
rules = protocol.render_reference_rules(context)
assert "obj.content.update(" in contract
@@ -1313,15 +1312,10 @@ def test_python_builtin_content_edit_respects_merge_op_and_offers_valid_repair(m
assert "obj.content.drop(" not in contract
assert "delete(replacement=canonical)" in rules
assert "never drop a unique fact" in rules
- assert operations is None
- assert error is not None
- assert "merge_op=replace" in error
- retry = protocol.render_format_retry(error)
- correction = retry.split("`")[1]
- assert correction.startswith("experiences_1.content.update(")
- operations, error = protocol.parse(correction, context)
+ operations, error = protocol.parse(
+ 'experiences_1.content.update("complete new value")', context
+ )
assert error is None
- assert operations is not None
assert operations.model_dump()[memory_type][0]["content"] == "complete new value"
diff --git a/tests/session/memory/test_replace_string_edits.py b/tests/session/memory/test_replace_string_edits.py
new file mode 100644
index 000000000..46abeff6f
--- /dev/null
+++ b/tests/session/memory/test_replace_string_edits.py
@@ -0,0 +1,467 @@
+# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
+# SPDX-License-Identifier: AGPL-3.0
+
+from __future__ import annotations
+
+import json
+from types import SimpleNamespace
+from unittest.mock import AsyncMock
+
+import pytest
+from pydantic import ValidationError
+from test_extraction_output_protocol import _bind, _context
+
+from openviking.session.memory.dataclass import (
+ MemoryField,
+ MemoryFile,
+ MemoryTypeSchema,
+ ResolvedOperation,
+ ResolvedOperations,
+)
+from openviking.session.memory.extract_loop import ExtractLoop
+from openviking.session.memory.extraction_output_protocol import create_extraction_output_protocol
+from openviking.session.memory.memory_type_registry import MemoryTypeRegistry
+from openviking.session.memory.memory_updater import MemoryUpdater
+from openviking.session.memory.merge_op import (
+ FieldType,
+ MergeOp,
+ MergeOpFactory,
+ PatchParseError,
+ ReplaceOp,
+ SearchReplaceBlock,
+ StrPatch,
+)
+from openviking.session.memory.schema_model_generator import SchemaModelGenerator
+from openviking.session.memory.utils.memory_file_utils import MemoryFileUtils
+
+URI = "viking://user/alice/memories/notes/example.md"
+OLD = "before\nold line\nafter"
+
+
+def schema():
+ return MemoryTypeSchema(
+ memory_type="notes",
+ directory="viking://user/{{ user_space }}/memories/notes",
+ filename_template="{{ key }}.md",
+ fields=[
+ MemoryField(name="key", field_type=FieldType.STRING, merge_op=MergeOp.IMMUTABLE),
+ MemoryField(name="content", field_type=FieldType.STRING, merge_op=MergeOp.REPLACE),
+ MemoryField(name="title", field_type=FieldType.STRING, merge_op=MergeOp.REPLACE),
+ MemoryField(name="count", field_type=FieldType.INT64, merge_op=MergeOp.REPLACE),
+ ],
+ )
+
+
+def bound():
+ memory = MemoryFile(
+ uri=URI,
+ memory_type="notes",
+ content=OLD,
+ extra_fields={"key": "example", "title": "old title", "count": 1},
+ )
+ context = _context([schema()], files=[memory])
+ protocol = create_extraction_output_protocol("python")
+ _bind(protocol, context)
+ return protocol, context, memory
+
+
+def resolved(parsed, memory):
+ return ResolvedOperations(
+ upsert_operations=[
+ ResolvedOperation(
+ memory_type="notes",
+ uris=[URI],
+ page_id=1,
+ old_memory_file_content=memory,
+ memory_fields={k: v for k, v in dict(parsed.notes[0]).items() if k != "page_id"},
+ )
+ ],
+ delete_file_contents=[],
+ errors=[],
+ )
+
+
+def validator(memory):
+ loop = object.__new__(ExtractLoop)
+ loop.context_provider = SimpleNamespace(read_file_contents={URI: memory})
+ return loop
+
+
+def test_replace_schema_and_prompt_still_request_full_strings():
+ flat = SchemaModelGenerator([schema()]).create_flat_data_model(schema())
+ advertised = flat.model_json_schema()
+ assert advertised["properties"]["content"]["anyOf"] == [{"type": "string"}, {"type": "null"}]
+ assert "StrPatch" not in json.dumps(advertised)
+ assert "blocks" not in json.dumps(advertised)
+ protocol, context, _ = bound()
+ contract = protocol.render_contract(context)
+ assert "obj.content.update(" in contract
+ assert "obj.content.edit(" not in contract and "obj.content.drop(" not in contract
+ assert "For fields marked [replace], use obj.field.update(" in protocol.render_patch_repair([])
+ parsed, error = protocol.parse('notes_1.content.update("complete new value")', context)
+ assert error is None and parsed.notes[0].content == "complete new value"
+
+
[email protected]("dictionary", [False, True])
+async def test_replace_materializes_edits_without_dropping_unchanged_text(dictionary):
+ patch = StrPatch(blocks=[SearchReplaceBlock(search="old line", replace="new line")])
+ value = patch.model_dump() if dictionary else patch
+ assert await ReplaceOp().apply(OLD, value) == "before\nnew line\nafter"
+
+
+async def test_replace_supports_chained_edits_and_deletes():
+ protocol, context, memory = bound()
+ parsed, error = protocol.parse(
+ 'notes_1.content.edit(search="old line", replace="new line").drop(text="before")', context
+ )
+ assert error is None
+ operations = resolved(parsed, memory)
+ assert await validator(memory)._validate_patch_operations(operations) == []
+ result = await ReplaceOp().apply(OLD, operations.upsert_operations[0].memory_fields["content"])
+ assert result == "new line\nafter"
+
+
+ "field,search", [("content", "does not exist"), ("title", "does not exist")]
+)
+async def test_bad_search_reaches_patch_repair_not_dsl_parse_error(field, search):
+ protocol, context, memory = bound()
+ parsed, error = protocol.parse(
+ f'notes_1.{field}.edit(search={search!r}, replace="new")', context
+ )
+ assert error is None
+ errors = await validator(memory)._validate_patch_operations(resolved(parsed, memory))
+ assert len(errors) == 1
+ assert errors[0]["field"] == field and errors[0]["reason"] == "not_found"
+
+
+async def test_metadata_field_validates_against_its_own_value():
+ protocol, context, memory = bound()
+ parsed, error = protocol.parse(
+ 'notes_1.title.edit(search="old title", replace="new title")', context
+ )
+ assert error is None
+ assert await validator(memory)._validate_patch_operations(resolved(parsed, memory)) == []
+ assert await ReplaceOp().apply("old title", parsed.notes[0].title) == "new title"
+
+
[email protected]("field", ["content", "title"])
[email protected]("protocol_name", ["python", "json"])
+async def test_runtime_uses_latest_stored_content_not_the_compiler_snapshot(field, protocol_name):
+ protocol, context, memory = bound()
+ search = "old line" if field == "content" else "old title"
+ replacement = "new line" if field == "content" else "new title"
+ if protocol_name == "python":
+ code = f"notes_1.{field}.edit(search={search!r}, replace={replacement!r})"
+ else:
+ protocol = create_extraction_output_protocol("json")
+ code = json.dumps(
+ {
+ "notes": [
+ {
+ "page_id": 1,
+ "key": "example",
+ field: {"blocks": [{"search": search, "replace": replacement}]},
+ }
+ ]
+ }
+ )
+ parsed, error = protocol.parse(code, context)
+ assert error is None
+ loop = validator(memory)
+ loop.ctx = None
+ loop._extract_context = context
+ loop.context_provider.get_memory_schemas = lambda ctx: [schema()]
+ loop._isolation_handler = SimpleNamespace(
+ get_read_scope=lambda: context.role_scope,
+ fill_identity_fields=lambda *args, **kwargs: None,
+ )
+ operations, links = await loop.resolve_operations(parsed)
+ assert not operations.errors and not links
+ assert await loop._validate_patch_operations(operations) == []
+ latest = memory.model_copy(
+ update={
+ "content": OLD + "\nconcurrent addition",
+ "extra_fields": {**memory.extra_fields, "title": "old title + concurrent addition"},
+ }
+ )
+ stored = {URI: MemoryFileUtils.write(latest)}
+
+ async def write(uri, content, **kwargs):
+ stored[uri] = content
+
+ fs = SimpleNamespace(
+ read_file=AsyncMock(side_effect=lambda uri, **kw: stored[uri]), write_file=write
+ )
+ registry = MemoryTypeRegistry(load_schemas=False)
+ registry.register(schema())
+ updater = MemoryUpdater(registry=registry)
+ updater._viking_fs = fs
+ await updater._apply_upsert(operations.upsert_operations[0], None)
+ result = MemoryFileUtils.read(stored[URI], uri=URI)
+ expected_content = OLD.replace(search, replacement) if field == "content" else OLD
+ expected_title = "new title" if field == "title" else "old title"
+ assert result.content == expected_content + "\nconcurrent addition"
+ assert result.extra_fields["title"] == expected_title + " + concurrent addition"
+ assert "blocks" not in stored[URI]
+
+
+async def test_replace_edit_without_old_match_does_not_invent_content():
+ with pytest.raises(PatchParseError):
+ await ReplaceOp().apply(
+ None, StrPatch(blocks=[SearchReplaceBlock(search="old", replace="new")])
+ )
+
+
[email protected]("value", [None, "", "new"])
+async def test_full_replacement_semantics_are_unchanged(value):
+ assert await ReplaceOp().apply("old", value) == ("old" if value in (None, "") else value)
+
+
+ "field_type,value", [(FieldType.INT64, 2), (FieldType.FLOAT32, 1.5), (FieldType.BOOL, False)]
+)
+async def test_non_string_replace_keeps_scalar_schema_and_behavior(field_type, value):
+ op = MergeOpFactory.create(MergeOp.REPLACE, field_type)
+ assert op.get_output_schema_type(field_type) is type(value)
+ assert await op.apply(1, value) == value
+
+
+def test_new_object_and_non_string_fields_still_reject_edits():
+ protocol, context, _ = bound()
+ for code in (
+ 'fresh = sdk.create_notes(key="fresh", content="old")\nfresh.content.edit(search="old", replace="new")',
+ 'notes_1.count.edit(search="1", replace="2")',
+ ):
+ parsed, error = protocol.parse(code, context)
+ assert parsed is None and error is not None
+
+
+def test_immutable_identity_remains_unchanged():
+ protocol, context, _ = bound()
+ parsed, error = protocol.parse('notes_1.key.edit(search="example", replace="other")', context)
+ assert error is None and parsed.notes == []
+
+
+async def test_ambiguous_search_is_repairable():
+ protocol, context, memory = bound()
+ memory.content = "old line\nold line"
+ parsed, error = protocol.parse(
+ 'notes_1.content.edit(search="old line", replace="new line")', context
+ )
+ assert error is None
+ errors = await validator(memory)._validate_patch_operations(resolved(parsed, memory))
+ assert len(errors) == 1
+ assert errors[0]["reason"] == "non_unique"
+ repaired, error = protocol.parse('notes_1.content.update("new line\\nold line")', context)
+ assert error is None
+ assert await validator(memory)._validate_patch_operations(resolved(repaired, memory)) == []
+
+
+async def test_replace_rejects_malformed_blocks_instead_of_persisting_their_repr():
+ with pytest.raises(ValueError):
+ await ReplaceOp().apply(OLD, {"blocks": [{"search": "old line"}]})
+
+
+async def test_replace_identity_edit_resolves_to_the_edited_uri():
+ _, _, memory = bound()
+ renamed_schema = schema()
+ renamed_schema.fields[0].merge_op = MergeOp.REPLACE
+ context = _context([renamed_schema], files=[memory])
+ protocol = create_extraction_output_protocol("python")
+ _bind(protocol, context)
+ parsed, error = protocol.parse('notes_1.key.edit(search="example", replace="renamed")', context)
+ assert error is None
+ loop = validator(memory)
+ loop._extract_context = None
+ operations = resolved(parsed, memory)
+ assert await loop._validate_patch_operations(operations) == []
+ assert await loop._updated_uri_for_existing_operation(
+ operations.upsert_operations[0], schema=renamed_schema, source_uri=URI
+ ) == URI.replace("example.md", "renamed.md")
+
+
[email protected]("other_title", ["missing title", "unrelated", None, 42])
+async def test_metadata_patch_diagnostic_searches_the_same_field(other_title):
+ protocol, context, memory = bound()
+ parsed, error = protocol.parse(
+ 'notes_1.title.edit(search="missing title", replace="new title")', context
+ )
+ assert error is None
+ loop = validator(memory)
+ other_uri = URI.replace("example.md", "other.md")
+ loop.context_provider.read_file_contents[other_uri] = MemoryFile(
+ uri=other_uri,
+ content="missing title",
+ extra_fields={"title": other_title},
+ )
+ errors = await loop._validate_patch_operations(resolved(parsed, memory))
+ assert len(errors) == 1
+ expected = [other_uri] if other_title == "missing title" else []
+ assert errors[0]["found_in_other_uris"] == expected
+
+
[email protected]("field", ["content", "title"])
[email protected]("full_method", ["field", "object"])
[email protected]("patch_method", ["edit", "drop", "field_value", "object_value"])
[email protected]("patch_first", [False, True])
+def test_replace_rejects_mixed_full_and_partial_updates(
+ field, full_method, patch_method, patch_first
+):
+ protocol, context, memory = bound()
+ full_value = "before\nold line\nNEW EXTRA\nafter"
+ if full_method == "field":
+ full = f"notes_1.{field}.update({full_value!r})"
+ else:
+ full = f"notes_1.update({field}={full_value!r})"
+ patch_value = {"blocks": [{"search": "old line", "replace": "new line"}]}
+ partial = {
+ "edit": f'notes_1.{field}.edit(search="old line", replace="new line")',
+ "drop": f'notes_1.{field}.drop(text="old line")',
+ "field_value": f"notes_1.{field}.update({patch_value!r})",
+ "object_value": f"notes_1.update({field}={patch_value!r})",
+ }[patch_method]
+ statements = [partial, full] if patch_first else [full, partial]
+
+ parsed, error = protocol.parse("\n".join(statements), context)
+
+ assert parsed is None
+ assert error is not None and "cannot mix" in error
+ assert memory.content == OLD
+
+
+def test_replace_full_updates_and_edits_on_other_fields_are_independent():
+ protocol, context, _ = bound()
+ parsed, error = protocol.parse(
+ 'notes_1.content.update("first value")\n'
+ 'notes_1.update(content="complete final value")\n'
+ 'notes_1.title.edit(search="old title", replace="new title")',
+ context,
+ )
+ assert error is None
+ assert parsed.notes[0].content == "complete final value"
+ assert isinstance(parsed.notes[0].title, StrPatch)
+
+
+async def test_replace_accumulates_edits_across_statements():
+ protocol, context, memory = bound()
+ parsed, error = protocol.parse(
+ 'notes_1.content.edit(search="old line", replace="new line")\n'
+ 'notes_1.content.drop(text="before")',
+ context,
+ )
+ assert error is None
+ operations = resolved(parsed, memory)
+ assert await validator(memory)._validate_patch_operations(operations) == []
+ assert await ReplaceOp().apply(OLD, parsed.notes[0].content) == "new line\nafter"
+
+
+def new_memory_updater():
+ fs = SimpleNamespace(read_file=AsyncMock(side_effect=FileNotFoundError), write_file=AsyncMock())
+ registry = MemoryTypeRegistry(load_schemas=False)
+ registry.register(schema())
+ updater = MemoryUpdater(registry=registry)
+ updater._viking_fs = fs
+ return updater, fs
+
+
[email protected]("protocol_name", ["python", "json"])
[email protected]("field", ["content", "title"])
[email protected]("block", [{"search": "old", "replace": "new"}, {"delete": "old"}])
+async def test_new_replace_patch_is_rejected_before_any_write(protocol_name, field, block):
+ _, context, memory = bound()
+ protocol = create_extraction_output_protocol(protocol_name)
+ fields = {"key": "fresh", "title": "full title", "content": "full body", "count": 0}
+ fields[field] = {"blocks": [block]}
+ if protocol_name == "python":
+ code = "sdk.create_notes(" + ", ".join(f"{k}={v!r}" for k, v in fields.items()) + ")"
+ else:
+ code = json.dumps({"notes": [{"page_id": 100, **fields}]})
+ parsed, error = protocol.parse(code, context)
+ updater, fs = new_memory_updater()
+ if parsed is not None:
+ for item in parsed.notes:
+ operation = ResolvedOperation(
+ memory_type="notes",
+ uris=[URI.replace("example.md", "fresh.md")],
+ page_id=item.page_id,
+ old_memory_file_content=None,
+ memory_fields=dict(item.model_dump(exclude={"page_id"})),
+ )
+ operations = ResolvedOperations(
+ upsert_operations=[operation], delete_file_contents=[], errors=[]
+ )
+ assert await validator(memory)._validate_patch_operations(operations) == []
+ await updater._apply_upsert(operation, None)
+
+ fs.write_file.assert_not_awaited()
+ # Both protocols reject the item at the shared schema boundary. JSON keeps
+ # its existing tolerant-parser policy of filtering invalid list items.
+ with pytest.raises(ValidationError, match="complete string"):
+ context.operations_model.model_validate({"notes": [{"page_id": 100, **fields}]})
+ if protocol_name == "python":
+ assert parsed is None
+ assert error is not None and "complete string" in error
+ else:
+ assert error is None
+ assert parsed.notes == []
+
+
[email protected]("protocol_name", ["python", "json"])
+async def test_new_replace_full_strings_are_parsed_and_persisted(protocol_name):
+ _, context, _ = bound()
+ protocol = create_extraction_output_protocol(protocol_name)
+ fields = {"key": "fresh", "title": "full title", "content": "full body", "count": 0}
+ if protocol_name == "python":
+ code = "sdk.create_notes(" + ", ".join(f"{k}={v!r}" for k, v in fields.items()) + ")"
+ else:
+ code = json.dumps({"notes": [{"page_id": 100, **fields}]})
+ parsed, error = protocol.parse(code, context)
+ assert error is None
+ item = parsed.notes[0]
+ uri = URI.replace("example.md", "fresh.md")
+ operation = ResolvedOperation(
+ memory_type="notes",
+ uris=[uri],
+ page_id=item.page_id,
+ old_memory_file_content=None,
+ memory_fields=dict(item.model_dump(exclude={"page_id"})),
+ )
+ updater, fs = new_memory_updater()
+ await updater._apply_upsert(operation, None)
+ fs.write_file.assert_awaited_once()
+ written = MemoryFileUtils.read(fs.write_file.await_args.args[1], uri=uri)
+ assert written.content == "full body"
+ assert written.extra_fields["title"] == "full title"
+ assert written.extra_fields["count"] == 0
+
+
+def test_json_filters_new_replace_patch_without_dropping_valid_items():
+ _, context, _ = bound()
+ protocol = create_extraction_output_protocol("json")
+ invalid = {
+ "page_id": 100,
+ "key": "bad",
+ "content": {"blocks": [{"search": "old", "replace": "new"}]},
+ }
+ valid = {"page_id": 101, "key": "good", "content": "complete content"}
+ parsed, error = protocol.parse(json.dumps({"notes": [invalid, valid]}), context)
+ assert error is None
+ assert len(parsed.notes) == 1
+ assert parsed.notes[0].key == "good"
+ assert parsed.notes[0].content == "complete content"