Skip to content

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

fix(memory): render fields without init_value as empty on init (#4213)

XiaoChen0554308e0c57
diff --git a/openviking/session/memory/memory_type_registry.py b/openviking/session/memory/memory_type_registry.py
index 0c5f1df7b..c44188c8a 100644
--- a/openviking/session/memory/memory_type_registry.py
+++ b/openviking/session/memory/memory_type_registry.py
@@ -258,9 +258,11 @@ class MemoryTypeRegistry:
if "{{" in schema.filename_template:
continue
- # Check if any field has init_value
+ # Seed template variables with every field so that placeholders for
+ # fields without an explicit init_value render as empty instead of
+ # leaking the literal "{{ field }}" text into the initialized file.
fields_with_init = {
- f.name: f.init_value for f in schema.fields if f.init_value is not None
+ f.name: f.init_value if f.init_value is not None else "" for f in schema.fields
}
if not fields_with_init:
continue
diff --git a/tests/unit/session/test_memory_policy.py b/tests/unit/session/test_memory_policy.py
index ca1a8fb7e..f2aaaabe9 100644
--- a/tests/unit/session/test_memory_policy.py
+++ b/tests/unit/session/test_memory_policy.py
@@ -147,3 +147,62 @@ async def test_initialize_memory_files_respects_memory_type_filter(monkeypatch):
await registry.initialize_memory_files(ctx, allowed_memory_types={"profile"})
assert fake_fs.written_uris == ["viking://user/alice/memories/profile.md"]
+
+
+async def test_initialize_memory_files_renders_fields_without_init_value_as_empty(monkeypatch):
+ """Fields lacking an explicit init_value must not leak ``{{ field }}`` placeholders.
+
+ Regression test for the bug where ``identity.md`` was written to disk with
+ literal Jinja placeholders (e.g. ``{{ vibe }}``) for every field that did not
+ declare an ``init_value``. See issue #4207.
+ """
+
+ class FakeVikingFS:
+ def __init__(self):
+ self.written = {}
+
+ async def read_file(self, uri, ctx=None):
+ raise FileNotFoundError(uri)
+
+ async def write_file(self, uri, content, ctx=None):
+ del ctx
+ self.written[uri] = content
+
+ def schema(memory_type: str, filename: str) -> MemoryTypeSchema:
+ return MemoryTypeSchema(
+ memory_type=memory_type,
+ directory="viking://user/{{ user_space }}/memories",
+ filename_template=filename,
+ content_template=(
+ "- **HasInit:** {{ has_init }}\n"
+ "- **NoInit:** {{ no_init }}\n"
+ "- **AlsoNoInit:** {{ also_no_init }}\n"
+ ),
+ fields=[
+ MemoryField(
+ name="has_init",
+ field_type=FieldType.STRING,
+ init_value="filled",
+ ),
+ MemoryField(name="no_init", field_type=FieldType.STRING),
+ MemoryField(name="also_no_init", field_type=FieldType.STRING),
+ ],
+ )
+
+ fake_fs = FakeVikingFS()
+ monkeypatch.setattr("openviking.storage.viking_fs.get_viking_fs", lambda: fake_fs)
+
+ registry = MemoryTypeRegistry(load_schemas=False)
+ registry.register(schema("identity", "identity.md"))
+
+ ctx = type("Ctx", (), {"user": type("User", (), {"user_id": "bob"})()})()
+ await registry.initialize_memory_files(ctx)
+
+ content = fake_fs.written["viking://user/bob/memories/identity.md"]
+ assert "{{" not in content, f"unrendered placeholder leaked: {content!r}"
+ assert "{{ no_init }}" not in content
+ assert "{{ also_no_init }}" not in content
+ assert "filled" in content
+ # Fields without init_value render as empty, not as the literal placeholder.
+ assert "- **NoInit:** \n" in content
+ assert "- **AlsoNoInit:** \n" in content