← Back to Pull requests

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

Open
unknown opened this pull request with 470 lines diff across 5 files
View on GitHub
unknown unknown commented on Nov 14, 2026 17:05 UTC
Author

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

  • I've included any doc changes needed for https://docs.ag2.ai/. See https://docs.ag2.ai/latest/docs/contributor-guide/documentation/ to build and test documentation locally.

  • I've added tests (if relevant) corresponding to the changes introduced in this PR.

  • I've made sure all auto checks have passed.
  • 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.
  • <!-- Validation performed locally before submitting (Windows 11, Python 3.13.9): new tests test_torn_line_degrades_to_unknown_event / test_non_object_line_degrades_to_unknown_event fail on main (JSONDecodeError / TypeError) and pass with the fix; pytest test/knowledge/test_knowledge.py test/knowledge/test_plugin.py test/agent/test_knowledge_tool.py test/events/test_serialization.py → 145 passed, 23 skipped (pre-existing platform/optional-deps skips); ruff check and ruff format --check clean on both changed files. The unchecked boxes above are confirmed by the submitter before publishing. -->

    Diff: 470 lines (+352 / -118) across 5 files Reviewer: its-amann Review duration: 19448m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.46) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.46 · 470 lines diff across 5 files
    0.46
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.368
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3675"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 45.7% (MEDIUM) · Higher = larger blast radius
    45.7%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 24.3% · Safe to merge
    24.3%
    Repository