fix(chunks): trim trusted overlaps by exact position, not text search
Position-aware merging still routed trimming through AppendWithOverlap, which searches for the longest suffix match inside a window. On periodic text (table rows, log lines) that search mistakes the repetition for the overlap and drops real content, and with a zero overlap there is nothing to search for in the first place. When coordinates are trusted the overlap is already known, so verify it character for character and cut exactly, falling back to the text matcher only when the bodies disagree inside the overlap.main
parent
dfe143818c
commit
e0ea453e3f
|
|
@ -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).
|
||||
|
|
|
|||
|
|
@ -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"),传空串则直接拼接。
|
||||
|
|
|
|||
Loading…
Reference in New Issue