From 0766c5671f0f289669c2a2a64e913a1ce1e4e7cd Mon Sep 17 00:00:00 2001 From: wizardchen Date: Tue, 28 Jul 2026 17:55:52 +0800 Subject: [PATCH] fix(retrieval): restore image parent context --- .../service/chat_pipeline/merge.go | 82 +++++++++++++++---- .../merge_parent_resolve_test.go | 49 +++++++++++ 2 files changed, 115 insertions(+), 16 deletions(-) diff --git a/internal/application/service/chat_pipeline/merge.go b/internal/application/service/chat_pipeline/merge.go index 5271b8de..c3051f35 100644 --- a/internal/application/service/chat_pipeline/merge.go +++ b/internal/application/service/chat_pipeline/merge.go @@ -211,11 +211,10 @@ func (p *PluginMerge) groupAndMergeCurrentContent(ctx context.Context, results [ return mergedChunks } -// resolveParentChunks scopes parent-child retrieval results to the matched -// child window instead of expanding to the full parent_text block. Text -// children keep their own content; image children resolve to the markdown -// slice of the parent_text that covers their text parent. ImageInfo is -// collected only for the matched text child, not all siblings under parent. +// resolveParentChunks expands parent-child retrieval results with current +// parent_text context. Text children and image grandchildren share the same +// behavior, while image Markdown is scoped to the matched text child by +// durable URLs rather than parser coordinates. func (p *PluginMerge) resolveParentChunks( ctx context.Context, chatManage *types.ChatManage, @@ -266,6 +265,48 @@ func (p *PluginMerge) resolveParentChunks( parentMap[c.ID] = c } + // Image hits have an image -> text -> parent_text chain. Fetch the + // grandparent only for those results so they retain parent-child context + // without using editable StartAt/EndAt coordinates. + imageTextParentIDs := make(map[string]struct{}) + for _, r := range results { + if r.ChunkType == string(types.ChunkTypeImageOCR) || r.ChunkType == string(types.ChunkTypeImageCaption) { + imageTextParentIDs[r.ParentChunkID] = struct{}{} + } + } + if len(imageTextParentIDs) > 0 { + grandparentIDs := make([]string, 0) + grandparentSeen := make(map[string]struct{}) + for _, parent := range parentChunks { + if _, needed := imageTextParentIDs[parent.ID]; !needed { + continue + } + if parent.ParentChunkID == "" || parent.ChunkType != types.ChunkTypeText { + continue + } + if _, already := parentMap[parent.ParentChunkID]; already { + continue + } + if _, already := grandparentSeen[parent.ParentChunkID]; already { + continue + } + grandparentSeen[parent.ParentChunkID] = struct{}{} + grandparentIDs = append(grandparentIDs, parent.ParentChunkID) + } + if len(grandparentIDs) > 0 { + grandparents, fetchErr := p.chunkRepo.ListChunksByID(ctx, tenantID, grandparentIDs) + if fetchErr != nil { + pipelineWarn(ctx, "Merge", "grandparent_fetch_failed", map[string]interface{}{ + "error": fetchErr.Error(), + }) + } else { + for _, grandparent := range grandparents { + parentMap[grandparent.ID] = grandparent + } + } + } + } + // Batch-fetch image_info scoped to matched text children only. textChildIDs := collectScopedTextChildIDs(results, parentMap) var scopedImageInfo map[string]string @@ -307,22 +348,31 @@ func (p *PluginMerge) resolveParentChunks( continue } hitImageInfo := r.ImageInfo - pipelineInfo(ctx, "Merge", "image_parent_resolve", map[string]interface{}{ - "child_id": r.ID, - "child_type": r.ChunkType, - "text_id": textParent.ID, - "parent_id": textParent.ID, - "match_len": runeLen(textParent.Content), - "parent_len": runeLen(textParent.Content), - "scoped": true, - }) + contentSource := textParent + if textParent.ParentChunkID != "" { + if grandparent, found := parentMap[textParent.ParentChunkID]; found && + grandparent.ChunkType == types.ChunkTypeParentText && grandparent.Content != "" { + contentSource = grandparent + } + } r.Content = textParent.Content r.ChunkIndex = textParent.ChunkIndex assignScopedImageInfo(r, scopedImageInfo, textParent.ID) if r.ImageInfo == "" && hitImageInfo != "" { - r.ImageInfo = searchutil.FilterImageInfoByContentURLs(r.Content, hitImageInfo) + r.ImageInfo = searchutil.FilterImageInfoByContentURLs(textParent.Content, hitImageInfo) } - r.Content = searchutil.PruneMarkdownImagesByImageInfo(r.Content, r.ImageInfo) + textContent := searchutil.PruneMarkdownImagesByImageInfo(textParent.Content, r.ImageInfo) + parentContent := searchutil.PruneMarkdownImagesByImageInfo(contentSource.Content, r.ImageInfo) + r.Content = searchutil.JoinChunkContent(parentContent, textContent, "\n\n") + pipelineInfo(ctx, "Merge", "image_parent_resolve", map[string]interface{}{ + "child_id": r.ID, + "child_type": r.ChunkType, + "text_id": textParent.ID, + "parent_id": contentSource.ID, + "match_len": runeLen(r.Content), + "parent_len": runeLen(contentSource.Content), + "scoped": true, + }) if !containsID(r.SubChunkID, r.ID) { r.SubChunkID = append(r.SubChunkID, r.ID) } diff --git a/internal/application/service/chat_pipeline/merge_parent_resolve_test.go b/internal/application/service/chat_pipeline/merge_parent_resolve_test.go index 9f5d9427..67516a3f 100644 --- a/internal/application/service/chat_pipeline/merge_parent_resolve_test.go +++ b/internal/application/service/chat_pipeline/merge_parent_resolve_test.go @@ -52,6 +52,55 @@ func TestResolveParentChunksUsesCurrentContentAndImageURLs(t *testing.T) { } } +func TestResolveImageChunkKeepsGrandparentContextWithoutCoordinateSlicing(t *testing.T) { + imageInfo, err := json.Marshal([]types.ImageInfo{{URL: "u1", OCRText: "matched image"}}) + if err != nil { + t.Fatal(err) + } + repo := &expandChunkRepo{ + chunks: map[string]*types.Chunk{ + "text": { + ID: "text", ParentChunkID: "parent", ChunkType: types.ChunkTypeText, ChunkIndex: 4, + Content: "current edited text child\n\n![matched](u1)", StartAt: 900, EndAt: 910, + }, + "parent": { + ID: "parent", ChunkType: types.ChunkTypeParentText, + Content: "grandparent context before\n\n![matched](u1)\n\ngrandparent context after\n\n![sibling](u2)", + StartAt: 0, EndAt: 100, + }, + }, + children: map[string][]*types.Chunk{ + "text": {{ + ID: "image", ParentChunkID: "text", ChunkType: types.ChunkTypeImageOCR, + ImageInfo: string(imageInfo), + }}, + }, + } + plugin := &PluginMerge{chunkRepo: repo} + ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1)) + result := &types.SearchResult{ + ID: "image", KnowledgeID: "doc", ChunkType: string(types.ChunkTypeImageOCR), + ParentChunkID: "text", Content: "matched image", ImageInfo: string(imageInfo), + StartAt: 500, EndAt: 510, + } + + got := plugin.resolveParentChunks(ctx, &types.ChatManage{}, []*types.SearchResult{result}) + if len(got) != 1 { + t.Fatalf("result count = %d, want 1", len(got)) + } + for _, want := range []string{"grandparent context before", "grandparent context after", "current edited text child", "u1"} { + if !strings.Contains(got[0].Content, want) { + t.Fatalf("image result lost %q: %q", want, got[0].Content) + } + } + if strings.Contains(got[0].Content, "u2") { + t.Fatalf("sibling image leaked into image result: %q", got[0].Content) + } + if got[0].StartAt != 500 || got[0].EndAt != 510 { + t.Fatalf("source coordinates changed to [%d,%d)", got[0].StartAt, got[0].EndAt) + } +} + func TestCollectScopedTextChildIDs(t *testing.T) { parentMap := map[string]*types.Chunk{ "parent-1": {ID: "parent-1", ChunkType: types.ChunkTypeParentText},