Skip to content

fix(chunker): keep sub-headings with their body and merge tiny closing sections - #5873

Open
a54b19211 wants to merge 2 commits into
Tencent:mainfrom
a54b19211:fix/chunker-heading-boundaries
Open

a54b19211 wants to merge 2 commits into
Tencent:mainfrom
a54b19211:fix/chunker-heading-boundaries

Conversation

@a54b19211

Copy link
Copy Markdown

Description

The heading splitter (strategy: heading) produces two kinds of poor chunk boundaries on structured documents (regulatory notices, policy Q&A, inspection reports):

  1. 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: carryTrailingHeadings moves 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 exceed ChunkSize. Because the moved lines now start the next sub-chunk, breadcrumbAtOffset picks up the sub-heading for that chunk.

  2. Tiny closing section becomes its own chunk. coalesceTinyChunks stops merging once the running chunk reaches ChunkSize/2. A short last section (a two-line ## Actions after 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 the End-Start == len([]rune(Content)) invariant are unchanged. No configuration or API change.

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • 💥 Breaking change
  • 📚 Documentation update
  • 🎨 Refactor
  • ⚡ Performance improvement
  • 🧪 Test
  • 🔧 Configuration / Build / CI

Related Issue

None filed.

Testing

git diff --check origin/main...HEAD
gofmt -l internal/infrastructure/chunker/
go vet ./internal/infrastructure/chunker/
go test ./internal/infrastructure/chunker/ -count=1
golangci-lint run --new-from-merge-base=origin/main ./...   # v2.12.2 (same as CI): 0 issues

New tests in heading_splitter_test.go:

  • TestSplitByHeadings_MergesTinyClosingSection and TestCarryTrailingHeadings fail on origin/main and pass with this change.
  • TestSplitByHeadings_TinySectionStillRespectsChunkSize and TestCarryTrailingHeadings_LeavesChunksAlone (over budget, overlapping chunks, heading-only chunk, fenced code) guard the limits.
  • All existing chunker tests pass unchanged.

End-to-end on a real knowledge base: 123 Chinese regulatory documents (71 web notices, 52 PDF/Word/Excel attachments converted to Markdown), heading strategy, chunk_size 2000, chunk_overlap 0. The change was applied on top of v0.9.0, and every document was reparsed.

v0.9.0 with this change
chunks 318 309
non-first chunks under 250 chars 8 0
heading line at the end of a chunk 4 2 (both would push the next chunk over chunk_size)
adjacent chunks with duplicated text 0 0
largest chunk 1997 1997
documents whose chunks changed – 17 of 123 (all changes explained by the two rules above)

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...HEAD passes
  • Changed source files are formatted
  • Targeted tests for the changed packages/components pass
  • Diff-scoped lint passes where applicable (for Go: golangci-lint run --new-from-rev=origin/main ./...)
  • Full-repository checks were run, or any unrelated/environment-dependent failures are documented above — diff-scoped lint ran over the whole module; tests were run for the changed package only, since make test needs external services not available in this environment
  • Self-reviewed the code
  • Added/updated tests covering the change
  • Updated related documentation (README, website-docs/, Swagger annotations, etc.) — not applicable: no configuration or API change
  • Breaking changes are clearly called out in the description above — not applicable

🤖 Generated with Claude Code

…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

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants