From 7b63cd5d73b632f4bf897c67f95ccd9f96ce6903 Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Sun, 15 Nov 2026 00:56:27 +0800 Subject: [PATCH 1/2] fix(knowledge): let a torn WAL line degrade instead of failing the whole load EventLogWriter._load_file parsed every line outside the per-record fallback: a line cut off mid-write (crash, full disk) raised JSONDecodeError, and a valid-JSON line that was not a record object raised TypeError or KeyError. Either way one bad line made the entire log unloadable, losing every healthy segment with it. Parse the line under the same per-record contract the loader already applies to unknown event types: a line that does not parse into a {type, data} record degrades to UnknownEvent(type_name="", data={"raw": line}), preserving the raw text so nothing is lost. Recoverable records keep their exact degradation paths. --- ag2/knowledge/log.py | 14 ++++++++++--- test/knowledge/test_knowledge.py | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ag2/knowledge/log.py b/ag2/knowledge/log.py index 10f89ff174f1..2808f0160728 100644 --- a/ag2/knowledge/log.py +++ b/ag2/knowledge/log.py @@ -94,9 +94,17 @@ async def _load_file(self, path: str) -> list[BaseEvent]: for line in content.strip().split("\n"): if not line: continue - record = json.loads(line) - event_type = record["type"] - event_data = record["data"] + + try: + record = json.loads(line) + event_type = record["type"] + event_data = record["data"] + except (json.JSONDecodeError, KeyError, TypeError): + # A line cut off mid-write (crash, full disk) or a foreign line + # degrades to a placeholder instead of failing the whole load — + # the same contract as the unknown-type fallback below. + events.append(UnknownEvent(type_name="", data={"raw": line})) + continue cls: type[BaseEvent] | None = import_event_class(event_type) diff --git a/test/knowledge/test_knowledge.py b/test/knowledge/test_knowledge.py index cd3b2877d13e..1a066cdd18be 100644 --- a/test/knowledge/test_knowledge.py +++ b/test/knowledge/test_knowledge.py @@ -250,6 +250,40 @@ async def test_unknown_event_fallback(self) -> None: UnknownEvent(type_name="nonexistent.module.FakeEvent", data={"key": "val"}) ] + async def test_torn_line_degrades_to_unknown_event(self) -> None: + store = MemoryKnowledgeStore() + writer = EventLogWriter(store) + stream_id = uuid4() + # The middle record was cut off mid-write (crash, full disk): one unterminated line. + lines = [ + json.dumps({"type": "nonexistent.module.Before", "data": {"k": 1}}), + '{"type": "nonexistent.module.Torn", "data": {"k":', + json.dumps({"type": "nonexistent.module.After", "data": {"k": 3}}), + ] + await store.write(f"/log/{stream_id}.jsonl", "\n".join(lines)) + + # The whole log stays loadable: the torn line degrades, its raw text is preserved. + assert await writer.load(stream_id) == [ + UnknownEvent(type_name="nonexistent.module.Before", data={"k": 1}), + UnknownEvent(type_name="", data={"raw": lines[1]}), + UnknownEvent(type_name="nonexistent.module.After", data={"k": 3}), + ] + + async def test_non_object_line_degrades_to_unknown_event(self) -> None: + store = MemoryKnowledgeStore() + writer = EventLogWriter(store) + stream_id = uuid4() + lines = [ + json.dumps({"type": "nonexistent.module.Good", "data": {"k": 1}}), + json.dumps(["not", "a", "record"]), + ] + await store.write(f"/log/{stream_id}.jsonl", "\n".join(lines)) + + assert await writer.load(stream_id) == [ + UnknownEvent(type_name="nonexistent.module.Good", data={"k": 1}), + UnknownEvent(type_name="", data={"raw": lines[1]}), + ] + @pytest.mark.asyncio class TestDefaultBootstrap: From 0dfdbda7e1165a0b3f3b8376ae45ad3558ebab92 Mon Sep 17 00:00:00 2001 From: DZH166 <248446924+DZH166@users.noreply.github.com> Date: Sun, 15 Nov 2026 18:57:06 +0800 Subject: [PATCH 2/2] fix(knowledge): degrade a non-string event type like any other bad line `import_event_class` splits the stored type name, so a record whose `type` is JSON `null` (or a number) raised AttributeError outside the malformed-line guard and took the whole load down. Validate the type inside the same `try` and add the matching regression test. Reported by @its-amann in review. --- ag2/knowledge/log.py | 5 +++++ test/knowledge/test_knowledge.py | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/ag2/knowledge/log.py b/ag2/knowledge/log.py index 2808f0160728..a150b7d3dd41 100644 --- a/ag2/knowledge/log.py +++ b/ag2/knowledge/log.py @@ -99,6 +99,11 @@ async def _load_file(self, path: str) -> list[BaseEvent]: record = json.loads(line) event_type = record["type"] event_data = record["data"] + # `import_event_class` splits the name, so a non-string type (JSON `null`, + # a number, a list) has to degrade with the malformed lines instead of + # raising AttributeError outside this guard. + if not isinstance(event_type, str): + raise TypeError("event type must be a string") except (json.JSONDecodeError, KeyError, TypeError): # A line cut off mid-write (crash, full disk) or a foreign line # degrades to a placeholder instead of failing the whole load — diff --git a/test/knowledge/test_knowledge.py b/test/knowledge/test_knowledge.py index 1a066cdd18be..65b015268f83 100644 --- a/test/knowledge/test_knowledge.py +++ b/test/knowledge/test_knowledge.py @@ -284,6 +284,19 @@ async def test_non_object_line_degrades_to_unknown_event(self) -> None: UnknownEvent(type_name="", data={"raw": lines[1]}), ] + async def test_non_string_type_degrades_to_unknown_event(self) -> None: + store = MemoryKnowledgeStore() + writer = EventLogWriter(store) + stream_id = uuid4() + # JSON `null` for the type is not a name to look up: splitting it would raise + # outside the malformed-line guard and take the whole load down. + lines = [json.dumps({"type": None, "data": {}})] + await store.write(f"/log/{stream_id}.jsonl", "\n".join(lines)) + + assert await writer.load(stream_id) == [ + UnknownEvent(type_name="", data={"raw": lines[0]}), + ] + @pytest.mark.asyncio class TestDefaultBootstrap: