Skip to content

fix(knowledge): let a torn WAL line degrade instead of failing the whole load - #3675

Open
DZH166 wants to merge 2 commits into
ag2ai:mainfrom
DZH166:codex/knowledge-wal-torn-line-20261112
Open

DZH166 wants to merge 2 commits into
ag2ai:mainfrom
DZH166:codex/knowledge-wal-torn-line-20261112

Conversation

@DZH166

@DZH166 DZH166 commented Nov 14, 2026

Copy link
Copy Markdown

Why are these changes needed?

EventLogWriter.load() reads a stream's event log: the numbered /log/{stream_id}.dropped-{n}.jsonl segments, then the final /log/{stream_id}.jsonl. Every line is parsed with json.loads and indexed as record["type"] / record["data"] outside the per-record fallback that already handles unknown event types. So one bad line fails the whole load:

  • a line cut off mid-write (process crash, full disk) raises JSONDecodeError,
  • a valid-JSON line that is not a record object (for example a list) raises TypeError or KeyError.

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 by EventLogWriter, and the codebase's stated contract for unreadable records is to degrade, not fail: the existing fallback wraps cls.from_dict in try/except and emits UnknownEvent, and ag2/events/_serialization.py documents 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 becomes UnknownEvent(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 fails from_dict → unchanged).

Two regression tests cover the torn-line case and the non-object-record case; both fail on main before the fix and pass after.

Related issue number

None — self-found while running the knowledge test suite and reading ag2/knowledge/log.py on main (fb3061e). I checked the tracker before preparing this (searches for persist_dropped, EventLogWriter, JSONDecodeError, and dropped-segment terms, all states): no open issue or PR covers WAL load robustness. Open PR #3483 touches ag2/knowledge/log.py too, but for a different problem (concurrent persist_dropped calls overwriting a segment, in persist_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.

  • I understand the changes in this PR and can explain them in my own words.
  • I have verified that the PR description accurately reflects the actual diff.
  • If AI assistance was used, I reviewed, tested, and validated the generated code/text before submitting.

…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.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Comment thread ag2/knowledge/log.py
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 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 ptimizeroracle left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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)

@vvlrff

vvlrff commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Hi @DZH166, thanks for the contribution!

Before we can review or merge this PR, you need to sign our Contributor License Agreement — the license/cla check on this PR has the signing link. This applies to draft PRs as well: we don't want you to invest work into a change we can't accept.

Once the CLA is signed, the check turns green, the status:needs-cla label is removed, and the PR proceeds through the normal flow.

⏳ 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.

@vvlrff vvlrff added the status:needs-cla Gate: CLA not signed; not reviewed until signed label Nov 16, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:needs-cla Gate: CLA not signed; not reviewed until signed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants