diff --git a/internal/application/service/faq_clone_sync.go b/internal/application/service/faq_clone_sync.go index c40368eb..615ab9ee 100644 --- a/internal/application/service/faq_clone_sync.go +++ b/internal/application/service/faq_clone_sync.go @@ -104,8 +104,13 @@ func (s *knowledgeService) syncFAQChunkStatusBatch( metadataIDs = append(metadataIDs, dst.ID) } } - if len(rows) > 0 { - if err := s.chunkRepo.UpdateChunks(ctx, rows); err != nil { + for _, row := range rows { + // These are status-only snapshots. UpdateChunks also writes content and + // indexing status, which are absent here and must remain unchanged. + fields := map[string]interface{}{ + "is_enabled": row.IsEnabled, "flags": int(row.Flags), "tag_id": row.TagID, + } + if err := s.chunkRepo.UpdateChunkFieldsByIDs(ctx, tenantID, []string{row.ID}, fields); err != nil { return err } } @@ -149,7 +154,7 @@ func (s *knowledgeService) syncFAQChunkStatusBatch( } if len(recommendedUpdates) > 0 { // Vector-store recommended flag sync is not yet available on all backends in - // this branch; DB flags were already updated via UpdateChunks above. + // this branch; DB flags were already updated above. logger.Warnf(ctx, "FAQ clone sync: skipped vector recommended update for %d chunks (DB flags updated)", len(recommendedUpdates)) } return nil diff --git a/internal/application/service/faq_clone_sync_test.go b/internal/application/service/faq_clone_sync_test.go new file mode 100644 index 00000000..c1caff21 --- /dev/null +++ b/internal/application/service/faq_clone_sync_test.go @@ -0,0 +1,48 @@ +package service + +import ( + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/require" +) + +func TestFAQCloneStatusSyncPreservesContentAndIndexState(t *testing.T) { + for _, change := range []string{"enabled", "tag", "recommended", "answer_strategy"} { + t.Run(change, func(t *testing.T) { + f := newFAQWriteFixture(t) + const body = "Restart the worker to reload the configuration." + require.NoError(t, f.db.Model(&types.Chunk{}).Where("id IN ?", []string{"one", "two"}). + Updates(map[string]any{ + "content": body, "status": int(types.ChunkStatusIndexed), + "tag_id": "", "flags": 0, "metadata": types.JSON(`{"answer_strategy":"all"}`), + }).Error) + fields := map[string]any{} + switch change { + case "enabled": + fields["is_enabled"] = false + case "tag": + fields["tag_id"] = "foreign" + case "recommended": + fields["flags"] = int(types.ChunkFlagRecommended) + case "answer_strategy": + fields["metadata"] = types.JSON(`{"answer_strategy":"random"}`) + } + require.NoError(t, f.db.Model(&types.Chunk{}).Where("id = ?", "two").Updates(fields).Error) + pairs := []types.FAQChunkSyncPair{{SrcChunkID: "two", DstChunkID: "one"}} + resolve := func(string) string { return "tag" } + plan, err := f.svc.buildFAQStatusSyncPlan(f.ctx, 7, 7, pairs, resolve) + require.NoError(t, err) + require.Len(t, plan.Pairs, 1) + err = f.svc.syncFAQChunkStatusBatch(f.ctx, f.kb, plan.Pairs, plan.SrcByID, plan.DstByID, resolve) + require.NoError(t, err) + chunk, err := f.chunks.GetChunkByID(f.ctx, 7, "one") + require.NoError(t, err) + require.Equal(t, body, chunk.Content) + require.Equal(t, int(types.ChunkStatusIndexed), chunk.Status) + plan, err = f.svc.buildFAQStatusSyncPlan(f.ctx, 7, 7, pairs, resolve) + require.NoError(t, err) + require.Empty(t, plan.Pairs, "the requested status changes must still be applied") + }) + } +}