diff --git a/ag2/knowledge/log.py b/ag2/knowledge/log.py index 10f89ff174f..a150b7d3dd4 100644 --- a/ag2/knowledge/log.py +++ b/ag2/knowledge/log.py @@ -94,9 +94,22 @@ 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"] + # `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 — + # 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 cd3b2877d13..65b015268f8 100644 --- a/test/knowledge/test_knowledge.py +++ b/test/knowledge/test_knowledge.py @@ -250,6 +250,53 @@ 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]}), + ] + + 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: