Repository navigation
Conversation
…g sections
The heading splitter left two kinds of poor chunk boundaries:
- When a primary section is too large, the legacy splitter packs lines
greedily, so a sub-heading ("### 5. Plan") could be the last line of a
chunk while its body opened the next one. The heading was cut off from
its text and the next chunk's breadcrumb missed it. carryTrailingHeadings
now moves such trailing heading lines to the next chunk when the chunks
are adjacent and the receiving chunk stays within ChunkSize.
- coalesceTinyChunks stopped merging once the running chunk reached
ChunkSize/2, so a short closing section (a two-line "## Actions") became
a chunk of its own. A section shorter than a quarter of the merge target
is now merged into the previous chunk, still within ChunkSize and only
under a shared heading.
Positions and the End-Start == len(Content) invariant are unchanged.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The heading splitter (
strategy: heading) produces two kinds of poor chunk boundaries on structured documents (regulatory notices, policy Q&A, inspection reports):Sub-heading stranded at the end of a chunk. When a primary section is larger than
ChunkSize, it is handed to the legacy splitter, which packs lines greedily. A sub-heading (### 5. Plan,#### 5.1 Existing data) can end up as the last line of one chunk while its body opens the next one. The heading is separated from the text it introduces, and the next chunk's breadcrumb (ContextHeader) misses it.Fix:
carryTrailingHeadingsmoves trailing heading lines of a sub-chunk to the start of the following sub-chunk. It only touches adjacent chunks (End == next.Start, i.e. no overlap), never empties a chunk, ignores headings inside code fences, and skips the move when the receiving chunk would exceedChunkSize. Because the moved lines now start the next sub-chunk,breadcrumbAtOffsetpicks up the sub-heading for that chunk.Tiny closing section becomes its own chunk.
coalesceTinyChunksstops merging once the running chunk reachesChunkSize/2. A short last section (a two-line## Actionsafter a long## Findings) therefore becomes a chunk of 100–200 characters with little retrieval value.Fix: a section shorter than a quarter of the merge target is also merged into the previous chunk, under the same conditions as today (adjacent, shared heading prefix,
curLen+nextLen <= ChunkSize).Positions,
Seq, and theEnd-Start == len([]rune(Content))invariant are unchanged. No configuration or API change.Type of Change
Related Issue
None filed.
Testing
New tests in
heading_splitter_test.go:TestSplitByHeadings_MergesTinyClosingSectionandTestCarryTrailingHeadingsfail onorigin/mainand pass with this change.TestSplitByHeadings_TinySectionStillRespectsChunkSizeandTestCarryTrailingHeadings_LeavesChunksAlone(over budget, overlapping chunks, heading-only chunk, fenced code) guard the limits.End-to-end on a real knowledge base: 123 Chinese regulatory documents (71 web notices, 52 PDF/Word/Excel attachments converted to Markdown),
headingstrategy,chunk_size2000,chunk_overlap0. The change was applied on top of v0.9.0, and every document was reparsed.chunk_size)The stored chunks were identical to the splitter output for 123/123 documents. Concatenating the chunks reproduces the source text for 116/123 documents. The other 7 differ only by the table header lines that the existing splitter already repeats at the start of continuation chunks; the count is the same before and after this change.
Checklist
git diff --check origin/main...HEADpassesgolangci-lint run --new-from-rev=origin/main ./...)make testneeds external services not available in this environmentwebsite-docs/, Swagger annotations, etc.) — not applicable: no configuration or API change🤖 Generated with Claude Code