Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

fix(chunks): trim trusted overlaps by exact position, not text search

wizardchene0ea453e3fad
diff --git a/internal/application/service/chat_pipeline/merge_overlap.go b/internal/application/service/chat_pipeline/merge_overlap.go
index 5f5baf637..7c8b2e2f8 100644
--- a/internal/application/service/chat_pipeline/merge_overlap.go
+++ b/internal/application/service/chat_pipeline/merge_overlap.go
@@ -38,9 +38,7 @@ func (p *PluginMerge) mergeSequentialChunks(
groups = append(groups, mergedGroup{result: current, lastIndex: current.ChunkIndex})
continue
case mergeExtend:
- lastChunk.Content = searchutil.AppendWithOverlap(
- lastChunk.Content, current.Content, lastChunk.EndAt-current.StartAt,
- )
+ lastChunk.Content = appendTrustedContent(lastChunk.Content, current.Content, lastChunk.EndAt-current.StartAt)
lastChunk.EndAt = current.EndAt
recordMergedChild(ctx, knowledgeID, lastChunk, current, "image_merge")
case mergeSubsume:
@@ -75,6 +73,21 @@ func (p *PluginMerge) mergeSequentialChunks(
return merged
}
+// appendTrustedContent joins a trusted pair using the overlap the coordinates
+// already give us, and only falls back to text search when the overlap does not
+// match character for character (HTML entities or synthetic table headers can
+// keep the length invariant while changing the body).
+//
+// The fallback matters both ways: searching for the longest suffix match would
+// otherwise mistake a repeated table row or log line for the overlap and drop
+// real content, which is exactly what the position path is meant to avoid.
+func appendTrustedContent(acc, next string, positionOverlap int) string {
+ if merged, ok := searchutil.AppendWithExactOverlap(acc, next, positionOverlap); ok {
+ return merged
+ }
+ return searchutil.AppendWithOverlap(acc, next, positionOverlap)
+}
+
// chunkTrusted reports whether a result's StartAt/EndAt can be trusted for
// position-based merging: unedited, valid range, and splitter-consistent
// length (runeLen(Content) == EndAt-StartAt).
diff --git a/internal/searchutil/chunkmerge.go b/internal/searchutil/chunkmerge.go
index fd48c04b4..7c8669b0f 100644
--- a/internal/searchutil/chunkmerge.go
+++ b/internal/searchutil/chunkmerge.go
@@ -130,6 +130,40 @@ func AppendWithOverlap(acc, next string, positionOverlap int) string {
return acc + next
}
+// AppendWithExactOverlap 在调用方已确认位置坐标可信时,按坐标给出的精确重叠量
+// 拼接 acc 与 next:校验 acc 的末 overlap 个字符与 next 的前 overlap 个字符逐字
+// 符相等,相等则精确裁剪,overlap 为 0 时直接拼接。
+//
+// 与 AppendWithOverlap 的区别在于「不猜」:后者为兼容补写表头、HTML 实体等长度
+// 偏差,会在窗口内搜索最长后缀匹配,重复周期性文本(表格、日志)可能被误判成重
+// 叠而裁掉真实内容。坐标可信时重叠量是已知的,不需要搜索。
+//
+// 校验不通过返回 ok=false,由调用方决定是否回退到 AppendWithOverlap。
+func AppendWithExactOverlap(acc, next string, overlap int) (string, bool) {
+ if acc == "" {
+ return next, true
+ }
+ if next == "" {
+ return acc, true
+ }
+ if overlap < 0 {
+ return "", false
+ }
+ if overlap == 0 {
+ return acc + next, true
+ }
+
+ accRunes := []rune(acc)
+ nextRunes := []rune(next)
+ if overlap > len(accRunes) || overlap > len(nextRunes) {
+ return "", false
+ }
+ if !runeSlicesEqual(accRunes[len(accRunes)-overlap:], nextRunes[:overlap]) {
+ return "", false
+ }
+ return acc + string(nextRunes[overlap:]), true
+}
+
// MergeTextChunks 按 StartAt(并列时按 ChunkIndex)排序后,用 AppendWithOverlap
// 把多个 chunk 的内容重建为完整文本。gapSep 用于位置不相邻(有间隙)的两段之间
// 的分隔符(如 "\n"),传空串则直接拼接。