Repository navigation
Conversation
…ole 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.
|
|
| try: | ||
| record = json.loads(line) | ||
| event_type = record["type"] | ||
| event_data = record["data"] |
There was a problem hiding this comment.
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:
| 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 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.
ptimizeroracle
left a comment
There was a problem hiding this comment.
checked this against DiskKnowledgeStore, whose write is a plain write_text (truncate then write), so a crash can leave a cut tail. cut a 6 event log at every byte offset (1706 cases): main raises JSONDecodeError at 1694 of them, with this change all 1706 load, the typed events are always exactly the complete records, plus one placeholder for the cut one. agent.resume(*events, trigger) also accepts the placeholder at the end and in the middle. small thought: after load the cut event is silent, would a logger.warning be wanted?
(ai-assisted, checked by me)
|
Hi @DZH166, thanks for the contribution! Before we can review or merge this PR, you need to sign our Contributor License Agreement — the Once the CLA is signed, the check turns green, the ⏳ If the CLA isn't signed within 3 days, this PR will be marked stale and closed 7 days after that. Signing later and reopening is always an option. |
Why are these changes needed?
EventLogWriter.load()reads a stream's event log: the numbered/log/{stream_id}.dropped-{n}.jsonlsegments, then the final/log/{stream_id}.jsonl. Every line is parsed withjson.loadsand indexed asrecord["type"]/record["data"]outside the per-record fallback that already handles unknown event types. So one bad line fails the whole load:JSONDecodeError,TypeErrororKeyError.Either exception aborts
load()entirely, which makes every event in the WAL unreadable — including all healthy segments and the final log. This matters because the loader is the resume path for sessions persisted byEventLogWriter, and the codebase's stated contract for unreadable records is to degrade, not fail: the existing fallback wrapscls.from_dictintry/exceptand emitsUnknownEvent, andag2/events/_serialization.pydocuments the same policy ("degrades to its raw value rather than failing the load and taking the whole event with it").The change moves line parsing under that same per-record contract: a line that does not parse into a
{type, data}record becomesUnknownEvent(type_name="", data={"raw": line}), preserving the raw text so nothing is lost. Recoverable records keep their exact degradation paths (unknown type →UnknownEvent(type_name=...); known type that failsfrom_dict→ unchanged).Two regression tests cover the torn-line case and the non-object-record case; both fail on
mainbefore the fix and pass after.Related issue number
None — self-found while running the knowledge test suite and reading
ag2/knowledge/log.pyonmain(fb3061e). I checked the tracker before preparing this (searches forpersist_dropped,EventLogWriter,JSONDecodeError, and dropped-segment terms, all states): no open issue or PR covers WAL load robustness. Open PR #3483 touchesag2/knowledge/log.pytoo, but for a different problem (concurrentpersist_droppedcalls overwriting a segment, inpersist_dropped); this change touches only_load_file, a different function, and I will rebase if it lands first.Checks
AI assistance
This change was prepared with the help of an AI coding assistant: the assistant located the issue, drafted the fix and the regression tests, and ran the targeted validation below. The submitter has reviewed the diff and the validation records, and can explain the change in their own words.