diff --git a/internal/infrastructure/chunker/splitter.go b/internal/infrastructure/chunker/splitter.go index fbb084d5..0820bc7f 100644 --- a/internal/infrastructure/chunker/splitter.go +++ b/internal/infrastructure/chunker/splitter.go @@ -585,7 +585,11 @@ func buildChunk(units []splitUnit, seq int) Chunk { // rune length. // // The configured overlap is a hard upper bound, not a raw character slice. -// Within that tail window we look for semantic boundaries using this priority: +// Boundary detection may inspect up to four additional runes immediately +// before that tail window so a separator cut by the window boundary remains +// visible. A candidate is eligible only when its last rune is at position -1 +// relative to the original window, or later, so retained content never exceeds +// the overlap limit. Eligible boundaries use this priority: // // 1. paragraph break (\n\n / \r\n\r\n) // 2. line break (\n / \r\n) @@ -598,6 +602,8 @@ func buildChunk(units []splitUnit, seq int) Chunk { // deterministic and bounded while allowing a boundary inside a large split // unit (the previous implementation could only retain whole units and often // collapsed to zero overlap for ordinary paragraphs). +const semanticOverlapLookbehind = 4 // rune length of the longest separator: \r\n\r\n + func computeOverlap(current []splitUnit, chunkOverlap, chunkSize, nextLen int) ([]splitUnit, int) { if chunkOverlap <= 0 { return nil, 0 @@ -614,13 +620,20 @@ func computeOverlap(current []splitUnit, chunkOverlap, chunkSize, nextLen int) ( return nil, 0 } - window := semanticOverlapWindow(current, maxOverlap) + window := semanticOverlapWindow(current, maxOverlap+semanticOverlapLookbehind) if len(window) == 0 { return nil, 0 } windowText := unitsText(window) - boundaryEnd, ok := findSemanticOverlapBoundary(windowText) + // Coordinates before originalWindowStart are the lookbehind region. With + // end-exclusive offsets, requiring boundaryEnd >= originalWindowStart is + // equivalent to requiring the separator's final rune to be at -1 or later. + originalWindowStart := runeLen(windowText) - maxOverlap + if originalWindowStart < 0 { + originalWindowStart = 0 + } + boundaryEnd, ok := findSemanticOverlapBoundaryEndingAtOrAfter(windowText, originalWindowStart) if !ok { return nil, 0 } @@ -630,7 +643,7 @@ func computeOverlap(current []splitUnit, chunkOverlap, chunkSize, nextLen int) ( for _, u := range overlap { overlapLen += runeLen(u.text) } - if overlapLen <= 0 || strings.TrimSpace(unitsText(overlap)) == "" { + if overlapLen <= 0 || overlapLen > maxOverlap || strings.TrimSpace(unitsText(overlap)) == "" { return nil, 0 } return overlap, overlapLen @@ -696,10 +709,24 @@ type semanticOverlapBoundary struct { // selected separator. Boundaries inside protected Markdown/code/math regions // are ignored, and a boundary is invalid when only whitespace follows it. func findSemanticOverlapBoundary(text string) (int, bool) { + return findSemanticOverlapBoundaryEndingAtOrAfter(text, 0) +} + +// findSemanticOverlapBoundaryEndingAtOrAfter applies the normal priority and +// earliest-position rules only to candidates whose end-exclusive rune offset +// is at least minEnd. Filtering before comparison prevents an earlier but +// ineligible lookbehind separator from hiding a later eligible boundary. +func findSemanticOverlapBoundaryEndingAtOrAfter(text string, minEnd int) (int, bool) { runes := []rune(text) if len(runes) == 0 { return 0, false } + if minEnd < 0 { + minEnd = 0 + } + if minEnd > len(runes) { + return 0, false + } protected := protectedSpansRune(text, protectedSpans(text)) insideProtected := func(pos int) bool { @@ -720,7 +747,8 @@ func findSemanticOverlapBoundary(text string) (int, bool) { var best semanticOverlapBoundary found := false consider := func(start, end, priority int) { - if start < 0 || end <= start || end > len(runes) || insideProtected(start) || !hasMeaningfulTail(end) { + if start < 0 || end <= start || end < minEnd || end > len(runes) || + insideProtected(start) || !hasMeaningfulTail(end) { return } candidate := semanticOverlapBoundary{start: start, end: end, priority: priority} diff --git a/internal/infrastructure/chunker/splitter_test.go b/internal/infrastructure/chunker/splitter_test.go index 272645cf..b71d44e4 100644 --- a/internal/infrastructure/chunker/splitter_test.go +++ b/internal/infrastructure/chunker/splitter_test.go @@ -367,6 +367,40 @@ func TestFindSemanticOverlapBoundary_IgnoresProtectedContent(t *testing.T) { } } +func TestFindSemanticOverlapBoundary_FiltersEligibilityBeforePriorityAndPosition(t *testing.T) { + tests := []struct { + name string + text string + minEnd int + want string + }{ + { + name: "earlier ineligible sentence does not hide eligible sentence", + text: "a。bc。XYZ", + minEnd: 4, + want: "XYZ", + }, + { + name: "ineligible paragraph does not outrank eligible sentence", + text: "\n\nx?tail", + minEnd: 3, + want: "tail", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + end, ok := findSemanticOverlapBoundaryEndingAtOrAfter(tt.text, tt.minEnd) + if !ok { + t.Fatal("expected eligible semantic overlap boundary") + } + if got := string([]rune(tt.text)[end:]); got != tt.want { + t.Fatalf("overlap tail = %q, want %q", got, tt.want) + } + }) + } +} + func TestComputeOverlap_FindsBoundaryInsideLargeUnit(t *testing.T) { text := "abcdefgh。尾巴内容" unit := splitUnit{text: text, start: 100, end: 100 + len([]rune(text))} @@ -406,6 +440,51 @@ func TestComputeOverlap_RespectsNextChunkCapacity(t *testing.T) { } } +func TestComputeOverlap_LookbehindBoundaryEligibility(t *testing.T) { + tests := []struct { + name string + text string + want string + }{ + { + name: "single rune separator ending at minus one is eligible", + text: "abcd。WXYZ", + want: "WXYZ", + }, + { + name: "longest separator ending at minus one is eligible", + text: "\r\n\r\nWXYZ", + want: "WXYZ", + }, + { + name: "separator ending before minus one is ineligible", + text: "a。bcWXYZ", + want: "", + }, + { + name: "separator crossing original window start is eligible", + text: "abc. WXY", + want: "WXY", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + unit := splitUnit{text: tt.text, start: 0, end: len([]rune(tt.text))} + overlap, overlapLen := computeOverlap([]splitUnit{unit}, 4, 20, 4) + if got := unitsText(overlap); got != tt.want { + t.Fatalf("overlap = %q, want %q", got, tt.want) + } + if overlapLen != len([]rune(tt.want)) { + t.Fatalf("overlapLen = %d, want %d", overlapLen, len([]rune(tt.want))) + } + if overlapLen > 4 { + t.Fatalf("overlap exceeds configured limit: %d > 4", overlapLen) + } + }) + } +} + func TestSplitText_SemanticOverlapInsideParagraph(t *testing.T) { first := strings.Repeat("甲", 15) + "。尾巴" text := first + "\n\n" + "后续内容"