Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions ag2/knowledge/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a record with a non string type like {"type": null, "data": {}} still takes down the whole load, since import_event_class calls type_name.split(".") outside this try. checking it here keeps it on the same fallback:

Suggested change
event_data = record["data"]
event_data = record["data"]
if not isinstance(event_type, str):
raise TypeError("event type must be a string")

fixed this plus a test in this commit, you can just cherry pick it instead of redoing it: its-amann@6ae4821

# `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)

Expand Down
47 changes: 47 additions & 0 deletions test/knowledge/test_knowledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading