fix(chunks): 流水线重写后的检索结果不用位置匹配 (#2558)

* chore(types): add internal ContentRewritten to types.SearchResult

* test(chunks): pin rewritten results out of the position path

Pipeline-replaced content (parent or neighbor expansion) must not trust
stale coordinates even when the replacement satisfies the length
invariant, and must not extend the merged group's range.

* fix(chunks): keep pipeline-rewritten results off the position path

Parent and neighbor expansion replace Content after retrieval. Without a
marker their stale coordinates could be incorrectly trusted.

Ensure ContentRewritten is false in chunkTrusted.
main
MidoriKurage 2026-08-06 14:32:24 +08:00 committed by GitHub
parent e8882fa403
commit 0a3f6b0f21
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 30 additions and 2 deletions

View File

@ -338,6 +338,7 @@ func (p *PluginMerge) resolveParentChunks(
assignScopedImageInfo(r, scopedImageInfo, r.ID)
parentContent := searchutil.PruneMarkdownImagesByImageInfo(parent.Content, r.ImageInfo)
r.Content = searchutil.JoinChunkContent(parentContent, r.Content, "\n\n")
r.ContentRewritten = true
if !containsID(r.SubChunkID, r.ID) {
r.SubChunkID = append(r.SubChunkID, r.ID)
}
@ -357,6 +358,7 @@ func (p *PluginMerge) resolveParentChunks(
}
r.Content = textParent.Content
r.ChunkIndex = textParent.ChunkIndex
r.ContentRewritten = true
assignScopedImageInfo(r, scopedImageInfo, textParent.ID)
if r.ImageInfo == "" && hitImageInfo != "" {
r.ImageInfo = searchutil.FilterImageInfoByContentURLs(textParent.Content, hitImageInfo)
@ -364,6 +366,7 @@ func (p *PluginMerge) resolveParentChunks(
textContent := searchutil.PruneMarkdownImagesByImageInfo(textParent.Content, r.ImageInfo)
parentContent := searchutil.PruneMarkdownImagesByImageInfo(contentSource.Content, r.ImageInfo)
r.Content = searchutil.JoinChunkContent(parentContent, textContent, "\n\n")
r.ContentRewritten = true
pipelineInfo(ctx, "Merge", "image_parent_resolve", map[string]interface{}{
"child_id": r.ID,
"child_type": r.ChunkType,

View File

@ -215,6 +215,7 @@ func (p *PluginMerge) expandShortContextWithNeighbors(
beforeLen := runeLen(res.Content)
res.Content = merged
res.ContentRewritten = true
for _, id := range prevIDs {
if id != "" && !containsID(res.SubChunkID, id) {

View File

@ -89,10 +89,11 @@ func appendTrustedContent(acc, next string, positionOverlap int) string {
}
// 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).
// position-based merging: unedited, not pipeline-rewritten, valid range, and
// splitter-consistent length (runeLen(Content) == EndAt-StartAt).
func chunkTrusted(chunk *types.SearchResult) bool {
return chunk.ContentRevision == 0 &&
!chunk.ContentRewritten &&
chunk.EndAt > chunk.StartAt &&
runeLen(chunk.Content) == chunk.EndAt-chunk.StartAt
}

View File

@ -125,6 +125,23 @@ func TestGroupAndMerge_TrustedGapStartsNewGroup(t *testing.T) {
require.Len(t, results, 2, "position gap must not merge")
}
func TestGroupAndMerge_RewrittenResultDoesNotTrustCoordinates(t *testing.T) {
// A result whose content was replaced by the pipeline (parent or neighbor
// expansion) must never enter the position path, even when the replacement
// happens to satisfy the length invariant and share boundary text: its
// stale coordinates must not extend the merged group's range.
first := trustedResult("c1", 1, 0, 100, rangeText('a', 100))
rewritten := trustedResult("c2", 2, 80, 200, rangeText('a', 20)+rangeText('b', 100))
rewritten.ContentRewritten = true
results := mergeGrouped(docChunks(first, rewritten))
require.Len(t, results, 1)
assert.Contains(t, results[0].Content, rangeText('a', 100))
assert.Contains(t, results[0].Content, rangeText('b', 100))
assert.Equal(t, 100, results[0].EndAt, "rewritten result must not extend the range via stale coordinates")
}
func TestGroupAndMerge_TrustedContainedSubsumes(t *testing.T) {
// Range-contained and text-verified: one result whose content is the
// outer body; the inner chunk is recorded, not duplicated.

View File

@ -218,6 +218,12 @@ type SearchResult struct {
// serialized; the length invariant in chunkTrusted is the remaining guard
// there. Keep the gorm column explicit so direct row scans populate it.
ContentRevision int `gorm:"column:content_revision" json:"-"`
// ContentRewritten reports that the merge pipeline replaced Content
// (parent or neighbor expansion) after retrieval, so StartAt/EndAt no
// longer describe the body. Internal only: used by the merge pipeline,
// never serialized.
ContentRewritten bool `json:"-"`
}
// SearchParams represents the search parameters