Self-evolving Context Database for AI Agents. Unify Agent Memory, Knowledge RAG and Skills.
Python agent-contextagent-memoryagent-pluginsagentic-ragcontext-databasedsh-pluginknowledge-basememorymemory-managementself-evolvingvector-database
fix(memory): align field edit guidance with merge operations (#5180)
Evo8db9eeb2300d
diff --git a/openviking/session/memory/extraction_output_protocol/python_protocol.py b/openviking/session/memory/extraction_output_protocol/python_protocol.pyindex cdab147dc..87b60ba2d 100644--- a/openviking/session/memory/extraction_output_protocol/python_protocol.py+++ b/openviking/session/memory/extraction_output_protocol/python_protocol.py@@ -81,13 +81,13 @@ _CONTRACT_PREAMBLE = ("To change an existing string field, edit it through the object's field attribute. Use the ""real field name (e.g. content), NOT the literal word 'field':"," - obj.content.update(new_value): replace the whole field with a complete new string.",- " - obj.content.edit(search=..., replace=...): replace one exact snippet in place.",- " - obj.content.drop(text=...): delete one exact snippet in place.",+ " - obj.content.edit(search=..., replace=...): replace one exact snippet in an editable string (merge_op=patch).",+ " - obj.content.drop(text=...): delete one exact snippet in an editable string (merge_op=patch).","edit()/drop() may be chained, e.g. obj.content.edit(search='a', replace='b').drop(text='c'); do not mix them with .update() in one chain.","A field attribute is a write handle only; you cannot read it as a string or call str methods on it.","Each search= (and drop text=) MUST be copied verbatim from the current field value shown in the object's sdk.existing(...) binding, and must occur exactly once. If the snippet appears more than once, include an adjacent unique line just before or after it so the match is unique. Never use text from the conversation or the new facts you intend to add as a search anchor; that text is not in the current content and the edit will fail.",- "edit()/drop() only work on an existing memory's string field; new memories from create/set must be given complete field values.",- "For existing memories, prefer the smallest unique edit()/drop(). Do not rewrite the entire field just to add or change a few facts; large full-content rewrites are more likely to be truncated or malformed. Use obj.content.update() only when most of the content changes.",+ "edit()/drop() only work on an existing memory's editable string field (merge_op=patch); new memories from create/set must be given complete field values.",+ "For existing editable strings (merge_op=patch), prefer the smallest unique edit()/drop(). Do not rewrite the entire field just to add or change a few facts; large full-content rewrites are more likely to be truncated or malformed. Use obj.content.update() only when most of such an editable string changes. For string fields marked [replace], always pass the complete new value to obj.content.update(), even for a small change; edit()/drop() are unavailable.",'ALWAYS use a triple-quoted string ("""...""") for EVERY natural-language argument '"(content, summary, goal, and every search=/replace=/text= snippet), even one-liners. ""Prose frequently contains apostrophes (e.g. Evan's), quotes, colons, or dates that break "@@ -1132,7 +1132,16 @@ class _PythonProgramCompiler:if field_schema is None or not (field_schema.merge_op == MergeOp.PATCH and field_schema.field_type == FieldType.STRING):- self._error(node, f"field {name!r} does not support edit()/drop()")+ 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}")# 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 thediff --git a/tests/session/memory/test_extraction_output_protocol.py b/tests/session/memory/test_extraction_output_protocol.pyindex 81b3c5470..878da01da 100644--- a/tests/session/memory/test_extraction_output_protocol.py+++ b/tests/session/memory/test_extraction_output_protocol.py@@ -1121,6 +1121,49 @@ sdk.commit()][email protected]("memory_type", ["experiences", "entities"])[email protected]("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):+ registry = MemoryTypeRegistry(load_schemas=False)+ registry.load_from_yaml(str(resolve_memory_templates_dir() / f"{memory_type}.yaml"))+ schema = registry.get(memory_type)+ assert schema is not None+ context = _context(+ [schema],+ files=[+ MemoryFile(+ uri=f"viking://user/alice/memories/{memory_type}/one.md",+ memory_type=memory_type,+ content="old text",+ extra_fields=(+ {"experience_name": "one", "supersedes": ""}+ if memory_type == "experiences"+ else {"category": "concept", "name": "one"}+ ),+ )+ ],+ )+ protocol = create_extraction_output_protocol("python")+ _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 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)+ assert error is None+ assert operations is not None+ assert operations.model_dump()[memory_type][0]["content"] == "complete new value"++def test_python_field_update_replaces_whole_field():uri = "viking://user/alice/memories/preferences/editor.md"context = _context(