diff --git a/internal/application/service/knowledge_clone_move.go b/internal/application/service/knowledge_clone_move.go index 67174583..d9aba90b 100644 --- a/internal/application/service/knowledge_clone_move.go +++ b/internal/application/service/knowledge_clone_move.go @@ -995,6 +995,18 @@ func (s *knowledgeService) moveOneKnowledge( return fmt.Errorf("knowledge %s is not in completed status (current: %s)", knowledgeID, knowledge.ParseStatus) } + // Reject a cross-store reuse_vectors move BEFORE mutating status, so a + // rejected move leaves the knowledge untouched (Completed) rather than + // stranded in Processing. reuse_vectors copies indices through the source + // store only; a cross-store copy would corrupt vector data. The handler + // rejects this synchronously — this is defense-in-depth for directly + // enqueued tasks. Cross-store moves must use reparse mode. + if mode == "reuse_vectors" && !sourceKB.SharesStoreWith(targetKB) { + return fmt.Errorf( + "reuse_vectors move across different vector stores is not supported "+ + "(source KB %s, target KB %s); use reparse mode", sourceKB.ID, targetKB.ID) + } + // Mark as processing during move knowledge.ParseStatus = types.ParseStatusProcessing if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { @@ -1019,6 +1031,19 @@ func (s *knowledgeService) moveKnowledgeReuseVectors( ) error { tenantID := ctx.Value(types.TenantIDContextKey).(uint64) + // reuse_vectors copies index entries directly between KBs, which only works + // inside the same VectorStore backend (CopyIndices is routed through the + // source store). A cross-store reuse_vectors move would write target-KB rows + // into the source store and then delete the source indices, corrupting data. + // The MoveKnowledge handler rejects this up front; this is defense-in-depth + // for any path that enqueues a move task directly. Cross-store moves must use + // reparse mode (moveKnowledgeReparse), which re-indexes into the target store. + if !sourceKB.SharesStoreWith(targetKB) { + return fmt.Errorf( + "reuse_vectors move across different vector stores is not supported "+ + "(source KB %s, target KB %s); use reparse mode", sourceKB.ID, targetKB.ID) + } + // 1. Get old chunk IDs for vector index copy mapping oldChunks, err := s.chunkRepo.ListChunksByKnowledgeID(ctx, tenantID, knowledge.ID) if err != nil { @@ -1033,10 +1058,9 @@ func (s *knowledgeService) moveKnowledgeReuseVectors( // 2. Copy vector indices from source KB to target KB if len(chunkIDMapping) > 0 && knowledge.EmbeddingModelID != "" { - // reuse_vectors mode copies index entries directly between KBs, which - // only works inside the same VectorStore backend. Route through the - // source KB's binding; callers must reject cross-store moves before - // landing here. + // Same VectorStore backend is guaranteed by the SharesStoreWith guard at + // the top of this function, so routing CopyIndices through the source + // KB's binding also resolves the target's store. var sourceStoreID *string if sourceKB != nil { sourceStoreID = sourceKB.VectorStoreID diff --git a/internal/application/service/knowledge_move_gate_test.go b/internal/application/service/knowledge_move_gate_test.go new file mode 100644 index 00000000..533e0f48 --- /dev/null +++ b/internal/application/service/knowledge_move_gate_test.go @@ -0,0 +1,62 @@ +package service + +import ( + "context" + "testing" + + "github.com/Tencent/WeKnora/internal/types" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// TestMoveKnowledgeReuseVectors_CrossStoreRejected verifies the defense-in-depth +// guard: a reuse_vectors move whose source and target KBs are bound to different +// vector stores must be rejected before any index copy runs. The guard fires at +// the top of moveKnowledgeReuseVectors, so an empty knowledgeService (no repos) +// is sufficient — the function returns before touching any dependency. +func TestMoveKnowledgeReuseVectors_CrossStoreRejected(t *testing.T) { + s := &knowledgeService{} + ctx := context.WithValue(context.Background(), types.TenantIDContextKey, uint64(1)) + + storeA, storeB := "store-a", "store-b" + tests := []struct { + name string + srcStore *string + dstStore *string + }{ + {"both bound, different stores", &storeA, &storeB}, + {"source bound, target env-store", &storeA, nil}, + {"source env-store, target bound", nil, &storeB}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + src := &types.KnowledgeBase{ID: "kb-src", VectorStoreID: tt.srcStore} + dst := &types.KnowledgeBase{ID: "kb-dst", VectorStoreID: tt.dstStore} + kn := &types.Knowledge{ID: "k1", EmbeddingModelID: "m1"} + + err := s.moveKnowledgeReuseVectors(ctx, kn, src, dst) + require.Error(t, err) + assert.Contains(t, err.Error(), "different vector stores") + }) + } +} + +// TestSharesStoreWith_GuardSemantics pins the same/cross-store classification the +// move gate relies on, including the env-store (nil) normalization, so a change +// to SharesStoreWith that would silently re-open the cross-store reuse_vectors +// hole fails here. +func TestSharesStoreWith_GuardSemantics(t *testing.T) { + storeA, storeB, empty := "store-a", "store-b", "" + + same := func(a, b *string) bool { + return (&types.KnowledgeBase{VectorStoreID: a}). + SharesStoreWith(&types.KnowledgeBase{VectorStoreID: b}) + } + + assert.True(t, same(nil, nil), "both env-store → same") + assert.True(t, same(&empty, nil), "empty string normalizes to env-store") + assert.True(t, same(&storeA, &storeA), "same UUID → same") + assert.False(t, same(&storeA, &storeB), "different UUID → cross-store") + assert.False(t, same(&storeA, nil), "bound vs env-store → cross-store") +} diff --git a/internal/handler/knowledge.go b/internal/handler/knowledge.go index c424a5e7..1415fd42 100644 --- a/internal/handler/knowledge.go +++ b/internal/handler/knowledge.go @@ -2015,6 +2015,18 @@ func (h *KnowledgeHandler) MoveKnowledge(c *gin.Context) { return } + // reuse_vectors copies index entries directly between KBs, which only works + // inside the same VectorStore backend. A cross-store reuse_vectors move would + // route CopyIndices through the SOURCE store and then delete the source + // indices, corrupting the vector data. Reject it and point the caller at + // reparse mode, which re-indexes into the target store safely. + if req.Mode == "reuse_vectors" && !sourceKB.SharesStoreWith(targetKB) { + c.Error(errors.NewBadRequestError( + "reuse_vectors move across different vector stores is not supported; " + + "use reparse mode to move into a different store")) + return + } + // Validate all knowledge IDs belong to source KB and are in completed status for _, kID := range req.KnowledgeIDs { knowledge, err := h.kgService.GetKnowledgeByID(ctx, kID) diff --git a/internal/handler/knowledge_move_gate_test.go b/internal/handler/knowledge_move_gate_test.go new file mode 100644 index 00000000..67b8996e --- /dev/null +++ b/internal/handler/knowledge_move_gate_test.go @@ -0,0 +1,136 @@ +package handler + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/gin-gonic/gin" + + "github.com/Tencent/WeKnora/internal/middleware" + "github.com/Tencent/WeKnora/internal/types" + "github.com/Tencent/WeKnora/internal/types/interfaces" +) + +// MoveKnowledge cross-store reuse_vectors gate — handler-level pre-flight tests. +// +// reuse_vectors copies vector indices between KBs through the source store only; +// a cross-store reuse_vectors move would corrupt vector data. The handler must +// reject it synchronously (before enqueueing the async move) and point the +// caller at reparse mode. reparse moves and same-store reuse_vectors must pass +// the gate (they fail later here only because the stub knowledge does not exist, +// which proves the gate did not short-circuit them). + +type stubMoveKBService struct { + interfaces.KnowledgeBaseService + byID func(ctx context.Context, id string) (*types.KnowledgeBase, error) +} + +func (s *stubMoveKBService) GetKnowledgeBaseByID(ctx context.Context, id string) (*types.KnowledgeBase, error) { + return s.byID(ctx, id) +} + +// stubMoveKGService.GetKnowledgeByID always reports not-found, so any request +// that PASSES the store gate fails at the later knowledge-ID validation with a +// distinct message — letting the test tell "rejected by gate" apart from +// "passed the gate." +type stubMoveKGService struct { + interfaces.KnowledgeService +} + +func (s *stubMoveKGService) GetKnowledgeByID(_ context.Context, _ string) (*types.Knowledge, error) { + return nil, errors.New("knowledge not found") +} + +func newMoveGateRouter(kb interfaces.KnowledgeBaseService, kg interfaces.KnowledgeService) *gin.Engine { + gin.SetMode(gin.TestMode) + r := gin.New() + r.Use(middleware.ErrorHandler()) + r.Use(func(c *gin.Context) { + c.Set(types.TenantIDContextKey.String(), uint64(1)) + c.Set(types.UserIDContextKey.String(), "u-test") + c.Next() + }) + // asynqClient stays nil: every case here either rejects at the gate or + // fails at knowledge-ID validation, so enqueue is never reached. A nil + // deref would mean the gate let a request through to enqueue unexpectedly. + h := &KnowledgeHandler{kbService: kb, kgService: kg} + r.POST("/move", h.MoveKnowledge) + return r +} + +func TestMoveKnowledge_CrossStoreGate(t *testing.T) { + storeA, storeB := "store-a", "store-b" + kbWith := func(id string, store *string) *types.KnowledgeBase { + return &types.KnowledgeBase{ID: id, TenantID: 1, Type: "document", + EmbeddingModelID: "m1", VectorStoreID: store} + } + + tests := []struct { + name string + mode string + srcStore *string + dstStore *string + wantStatus int + wantBodyHas string // substring that must appear + wantBodyNotHas string // substring that must NOT appear (gate message) + }{ + { + name: "reuse_vectors cross-store rejected by gate", + mode: "reuse_vectors", srcStore: &storeA, dstStore: &storeB, + wantStatus: http.StatusBadRequest, wantBodyHas: "different vector stores", + }, + { + name: "reparse cross-store passes gate (fails later at knowledge lookup)", + mode: "reparse", srcStore: &storeA, dstStore: &storeB, + wantStatus: http.StatusBadRequest, wantBodyNotHas: "different vector stores", + }, + { + name: "reuse_vectors same-store passes gate", + mode: "reuse_vectors", srcStore: &storeA, dstStore: &storeA, + wantStatus: http.StatusBadRequest, wantBodyNotHas: "different vector stores", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + kb := &stubMoveKBService{byID: func(_ context.Context, id string) (*types.KnowledgeBase, error) { + switch id { + case "kb-src": + return kbWith("kb-src", tt.srcStore), nil + case "kb-dst": + return kbWith("kb-dst", tt.dstStore), nil + } + return nil, errors.New("kb not found") + }} + router := newMoveGateRouter(kb, &stubMoveKGService{}) + + body, _ := json.Marshal(map[string]any{ + "knowledge_ids": []string{"k1"}, + "source_kb_id": "kb-src", + "target_kb_id": "kb-dst", + "mode": tt.mode, + }) + req := httptest.NewRequest(http.MethodPost, "/move", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + w := httptest.NewRecorder() + router.ServeHTTP(w, req) + + if w.Code != tt.wantStatus { + t.Fatalf("status = %d, want %d; body=%s", w.Code, tt.wantStatus, w.Body.String()) + } + if tt.wantBodyHas != "" && !strings.Contains(w.Body.String(), tt.wantBodyHas) { + t.Fatalf("body %q does not contain %q", w.Body.String(), tt.wantBodyHas) + } + if tt.wantBodyNotHas != "" && strings.Contains(w.Body.String(), tt.wantBodyNotHas) { + t.Fatalf("body %q unexpectedly contains gate message %q (gate short-circuited a valid move)", + w.Body.String(), tt.wantBodyNotHas) + } + }) + } +}