Skip to content

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

fix(memory): allow first assignment to blank immutable fields (#5124)

Qin Haojie8f644a651cfc
diff --git a/openviking/session/memory/extract_loop.py b/openviking/session/memory/extract_loop.py
index c66bd652e..f26b7f13f 100644
--- a/openviking/session/memory/extract_loop.py
+++ b/openviking/session/memory/extract_loop.py
@@ -30,7 +30,7 @@ from openviking.session.memory.extraction_output_protocol import (
create_extraction_output_protocol,
)
from openviking.session.memory.memory_isolation_handler import MemoryIsolationHandler
-from openviking.session.memory.merge_op import FieldType, MergeOp, PatchOp
+from openviking.session.memory.merge_op import FieldType, ImmutableOp, MergeOp, PatchOp
from openviking.session.memory.page_id_map import ResponsePageIdAllocator
from openviking.session.memory.schema_model_generator import SchemaModelGenerator
from openviking.session.memory.tools import MEMORY_TOOLS_REGISTRY
@@ -870,7 +870,7 @@ class ExtractLoop:
if field.merge_op == MergeOp.IMMUTABLE
}
for field_name in immutable_fields:
- if field_name in old_content.extra_fields:
+ if ImmutableOp.is_set(old_content.extra_fields.get(field_name)):
resolved_op.memory_fields[field_name] = (
old_content.extra_fields[field_name]
)
diff --git a/openviking/session/memory/extraction_output_protocol/python_protocol.py b/openviking/session/memory/extraction_output_protocol/python_protocol.py
index 0e49f3e3e..682811612 100644
--- a/openviking/session/memory/extraction_output_protocol/python_protocol.py
+++ b/openviking/session/memory/extraction_output_protocol/python_protocol.py
@@ -21,6 +21,7 @@ from openviking.session.memory.extraction_output_protocol.base import (
from openviking.session.memory.merge_op import (
DeleteBlock,
FieldType,
+ ImmutableOp,
MergeOp,
SearchReplaceBlock,
StrPatch,
@@ -1097,8 +1098,9 @@ class _PythonProgramCompiler:
field_schema is not None
and field_schema.merge_op == MergeOp.IMMUTABLE
and owner.existing
+ and ImmutableOp.is_set(owner.fields.get(name))
):
- # Immutable identity fields cannot change on an existing memory; ignore silently.
+ # Preserve established values, but allow filling a blank template field.
return
if handle.full_value is not _UNSET:
owner.fields[name] = handle.full_value
@@ -1165,9 +1167,13 @@ class _PythonProgramCompiler:
continue
if field_schema is None:
continue
- if field_schema.merge_op == MergeOp.IMMUTABLE and owner.existing:
- continue
current = owner.fields.get(name)
+ if (
+ field_schema.merge_op == MergeOp.IMMUTABLE
+ and owner.existing
+ and ImmutableOp.is_set(current)
+ ):
+ continue
if field_schema.merge_op == MergeOp.SUM:
value = (current or 0) + value
previous_delta = owner.changed_fields.get(name, 0)
diff --git a/openviking/session/memory/merge_op/immutable.py b/openviking/session/memory/merge_op/immutable.py
index 64865a0df..6f3574b3f 100644
--- a/openviking/session/memory/merge_op/immutable.py
+++ b/openviking/session/memory/merge_op/immutable.py
@@ -23,10 +23,18 @@ class ImmutableOp(MergeOpBase):
return get_python_type_for_field(field_type)
def get_output_schema_description(self, field_description: str) -> str:
- return f"Immutable field '{field_description}' - can only be set once, cannot be modified"
+ return (
+ f"Immutable field '{field_description}' - can only be set once, cannot be modified. "
+ "None and blank strings are unset and may receive their first value."
+ )
+
+ @staticmethod
+ def is_set(value: Any) -> bool:
+ """Blank string placeholders are unset; zero and False are real values."""
+ return value is not None and not (isinstance(value, str) and not value.strip())
async def apply(self, current_value: Any, patch_value: Any) -> Any:
- if current_value is None:
+ if not self.is_set(current_value):
return patch_value
# Keep current value if already set
return current_value