fix(chunker): heuristic splitter overlap was always zero

applyOverlapAligned searched for the latest boundary in
[curEnd-2*overlap, curEnd], but curEnd itself is always one of the
boundaries (the bin-packer only flushes at boundary positions). The
loop therefore always returned curEnd, producing chunkStart == curEnd
and zero overlap regardless of cfg.ChunkOverlap.

Exclude curEnd from the search window so an earlier boundary can be
picked, restoring the intended overlap behaviour.
This commit is contained in:
wizardchen
2026-05-06 20:54:12 +08:00
committed by lyingbug
parent 49cd392308
commit 5a79a817a1
2 changed files with 67 additions and 3 deletions
@@ -233,6 +233,10 @@ func appendOversizeBlock(out []Chunk, runes []rune, start, end int, cfg Splitter
// preceding boundary (within 2x overlap) or, failing that, the previous
// newline so chunks don't begin mid-line / mid-word. Falls back to the raw
// target only if neither option is available.
//
// curEnd itself is always a boundary (the bin-packer flushes at boundary
// positions), so we exclude it from the search — picking it would yield
// zero overlap, defeating the purpose of this function.
func applyOverlapAligned(runes []rune, curEnd, overlap int, bounds []boundary) int {
if overlap <= 0 {
return curEnd
@@ -241,16 +245,16 @@ func applyOverlapAligned(runes []rune, curEnd, overlap int, bounds []boundary) i
if target < 0 {
target = 0
}
// Allowed search window: [curEnd - 2*overlap, curEnd]
// Allowed search window: [curEnd - 2*overlap, curEnd)
windowStart := curEnd - 2*overlap
if windowStart < 0 {
windowStart = 0
}
// Prefer a semantic boundary inside the window.
// Prefer a semantic boundary strictly inside the window.
bestBound := -1
for _, b := range bounds {
if b.runeStart >= windowStart && b.runeStart <= curEnd && b.runeStart > bestBound {
if b.runeStart >= windowStart && b.runeStart < curEnd && b.runeStart > bestBound {
bestBound = b.runeStart
}
}