From 35b9509da313ef3f079e6e1b44ba4d2434ed5df0 Mon Sep 17 00:00:00 2001 From: Afonso Coutinho Date: Wed, 3 Jun 2026 05:38:02 +0100 Subject: [PATCH] fix: memory entry validation crashes on a non-dict row from memory.json (#1691) --- src/memory.py | 2 ++ tests/test_memory_validate_entries_nondict.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 tests/test_memory_validate_entries_nondict.py diff --git a/src/memory.py b/src/memory.py index edb4767..7f3a8cb 100644 --- a/src/memory.py +++ b/src/memory.py @@ -137,6 +137,8 @@ class MemoryManager: """Ensure all entries have required fields.""" validated = [] for entry in entries: + if not isinstance(entry, dict): + continue if "id" not in entry: entry["id"] = str(uuid.uuid4()) if "timestamp" not in entry: diff --git a/tests/test_memory_validate_entries_nondict.py b/tests/test_memory_validate_entries_nondict.py new file mode 100644 index 0000000..ca29854 --- /dev/null +++ b/tests/test_memory_validate_entries_nondict.py @@ -0,0 +1,19 @@ +from src.memory import MemoryManager + + +def test_validate_entries_skips_non_dict_rows(tmp_path): + # Entries come from json.load on the user-editable memory.json. A hand-edit + # that drops a bare string / number / null into the array made the old loop + # do item assignment on a non-dict and raise TypeError, losing the whole + # memory store. Bad rows are now skipped. + m = MemoryManager(str(tmp_path)) + out = m._validate_entries([ + {"id": "a", "text": "real memory"}, + "corrupt-row", + None, + 123, + ]) + assert [e["id"] for e in out] == ["a"] + # the surviving entry is still backfilled with required defaults + assert out[0]["category"] == "fact" + assert out[0]["source"] == "unknown"