Conversation
MarkdownChunker counted '#'-comment lines inside fenced code blocks as level-1 markdown headers. Enough comments (five in a commented-out python snippet) tripped the malformed-hierarchy detector, and _fix_header_hierarchy then rewrote the comments into '## ...' lines inside the code block, corrupting embedded code in the produced chunks. Track CommonMark-style fences (backtick and tilde) in both the detector and the fixer and skip masked lines; code outside fences and real headers are handled exactly as before. Red/green verified: three new tests fail against the old implementation and pass with the fix; the pre-existing header-repair behavior is pinned by two further tests.
🤖 Open Code ReviewTarget: PR #2416 🔍 OpenCodeReview found 2 issue(s) in this PR. 1.
|
|
Description
Fixes #2415
MarkdownChunkercounted#-comment lines inside fenced code blocks as level-1 markdown headers:_detect_malformed_headersmatched every^#{1,6}\s+.+line regardless of ``` fences, and once the "malformed hierarchy" heuristic tripped (five commented lines in an embedded python snippet are enough),_fix_header_hierarchyrewrote those comments into `## ...` lines inside the code block, corrupting embedded code in the produced memory chunks.Implementation: a shared
_code_block_maskhelper tracks CommonMark-style fences (backtick and tilde, including unclosed fences; the fence lines themselves are masked) over the split lines, and both the detector and the fixer skip masked lines. Lines outside fences — real headers and plain text — are handled exactly as before.Dependencies: none.
Type of change
How Has This Been Tested?
New
tests/chunkers/test_markdown_chunker_code_fence.py:True)_fix_header_hierarchyleaves a document containing a fenced python block byte-for-byte intact (fails on main: comments were rewritten to## ...)# A / # B / # C→# A / ## B / ## C) and tilde-fence handlingAll five pass with the fix; the three discriminators fail against unpatched
main(red/green verified).ruff check: the only finding on the touched source file (BLE001atchunk()'s pre-existingexcept Exception) is present onmainas well;ruff format --checkpasses.Checklist