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