From 0a3f6b0f2182776d0608ed941be5385b1159a2d1 Mon Sep 17 00:00:00 2001 From: MidoriKurage Date: Thu, 6 Aug 2026 14:32:24 +0800 Subject: [PATCH] =?UTF-8?q?fix(chunks):=20=E6=B5=81=E6=B0=B4=E7=BA=BF?= =?UTF-8?q?=E9=87=8D=E5=86=99=E5=90=8E=E7=9A=84=E6=A3=80=E7=B4=A2=E7=BB=93?= =?UTF-8?q?=E6=9E=9C=E4=B8=8D=E7=94=A8=E4=BD=8D=E7=BD=AE=E5=8C=B9=E9=85=8D?= =?UTF-8?q?=20(#2558)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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. --- .../application/service/chat_pipeline/merge.go | 3 +++ .../service/chat_pipeline/merge_expand.go | 1 + .../service/chat_pipeline/merge_overlap.go | 5 +++-- .../chat_pipeline/merge_position_path_test.go | 17 +++++++++++++++++ internal/types/search.go | 6 ++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/internal/application/service/chat_pipeline/merge.go b/internal/application/service/chat_pipeline/merge.go index c3051f35..d33893a2 100644 --- a/internal/application/service/chat_pipeline/merge.go +++ b/internal/application/service/chat_pipeline/merge.go @@ -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, diff --git a/internal/application/service/chat_pipeline/merge_expand.go b/internal/application/service/chat_pipeline/merge_expand.go index 2ee6a6e1..e459e96c 100644 --- a/internal/application/service/chat_pipeline/merge_expand.go +++ b/internal/application/service/chat_pipeline/merge_expand.go @@ -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) { diff --git a/internal/application/service/chat_pipeline/merge_overlap.go b/internal/application/service/chat_pipeline/merge_overlap.go index 7c8b2e2f..025e6c69 100644 --- a/internal/application/service/chat_pipeline/merge_overlap.go +++ b/internal/application/service/chat_pipeline/merge_overlap.go @@ -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 } diff --git a/internal/application/service/chat_pipeline/merge_position_path_test.go b/internal/application/service/chat_pipeline/merge_position_path_test.go index d7b3f034..09137c65 100644 --- a/internal/application/service/chat_pipeline/merge_position_path_test.go +++ b/internal/application/service/chat_pipeline/merge_position_path_test.go @@ -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. diff --git a/internal/types/search.go b/internal/types/search.go index 8d5c4abb..daceb909 100644 --- a/internal/types/search.go +++ b/internal/types/search.go @@ -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