Skip to content

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

fix(session): preserve sections with malformed update content (#5516)

Evoa13d2e2a2371
diff --git a/openviking/session/working_memory.py b/openviking/session/working_memory.py
index b0b8b127e..7fe25f7ae 100644
--- a/openviking/session/working_memory.py
+++ b/openviking/session/working_memory.py
@@ -978,8 +978,8 @@ def merge_wm_sections(old_wm: str, ops: Dict[str, Any]) -> str:
- ``Open Issues`` UPDATE that silently drops old items restores them
with an explicit marker.
- Missing sections or unknown ops default to ``KEEP`` (the schema
- should prevent this, but we stay defensive so a buggy LLM or
+ Missing sections, unknown ops, or non-string UPDATE content default to ``KEEP``.
+ The schema should prevent this, but we stay defensive so a buggy LLM or
schema-loose backend cannot wipe out the prior WM).
"""
wm_debug(
@@ -994,6 +994,17 @@ def merge_wm_sections(old_wm: str, ops: Dict[str, Any]) -> str:
op = (ops or {}).get(header)
old_content = old_sections.get(full_header, "").rstrip()
+ if (
+ isinstance(op, dict)
+ and (op.get("op") or "").upper() == "UPDATE"
+ and not isinstance(op.get("content"), str)
+ ):
+ logger.warning(
+ "WM update: non-string UPDATE content for section %r; keeping old content",
+ header,
+ )
+ op = None
+
# ---------- per-section guards ----------
if old_content:
if header == "Session Title":
diff --git a/tests/unit/session/test_wm_v2_guards.py b/tests/unit/session/test_wm_v2_guards.py
index 0b24d234b..359506e69 100644
--- a/tests/unit/session/test_wm_v2_guards.py
+++ b/tests/unit/session/test_wm_v2_guards.py
@@ -287,6 +287,28 @@ class TestEnforceOpenIssuesResolved:
# =======================================================================
[email protected]("section", WM_SEVEN_SECTIONS)
[email protected]("content", [None, [], ["new item"], {"text": "new item"}, 42])
+def test_merge_keeps_section_with_non_string_update(section, content):
+ old = _make_wm(
+ **{
+ name.lower().replace(" ", "_").replace("&", "and"): "- Existing context"
+ for name in WM_SEVEN_SECTIONS
+ }
+ )
+ other = "Current State" if section != "Current State" else "Task & Goals"
+ merged = wm.merge_wm_sections(
+ old,
+ {
+ section: {"op": "UPDATE", "content": content},
+ other: {"op": "UPDATE", "content": "Valid replacement"},
+ },
+ )
+ sections = wm.parse_wm_sections(merged)
+ assert sections[f"## {section}"] == "- Existing context"
+ assert sections[f"## {other}"] == "Valid replacement"
+
+
class TestMergeWmSections:
def test_all_keep(self):
old = _make_wm(session_title="Title", current_state="Working")