fix: deep-copy stored files and images when cloning a knowledge base
Cloning a knowledge base previously copied only the storage path strings (knowledge.FilePath and chunk.ImageInfo.URL), so the source and the clone shared the same physical objects in the storage backend. Once the original file and extracted images are deleted on source removal, the clone is left with dangling references and its document and images become unreadable — data loss that occurs even for same-store clones. Add a CopyFile primitive to the FileService interface and implement it in every backend: server-side CopyObject on the object stores (s3/obs/cos/oss/tos/ks3/minio), io.Copy on local, and a no-op on dummy. Destinations use the knowledge-owned layout and reuse the existing path/object-key guards; a sentinel ErrCrossBackendCopy is returned when the source scheme does not match the backend. Use CopyFile to deep-copy the document file in cloneKnowledge and the extracted images in CloneChunk and cloneFAQKnowledgeBase via a shared cloneChunkImageInfo helper that deduplicates identical image URLs per clone and rewrites them to the new objects. Copied objects are cleaned up best-effort if a clone fails partway through. A clone-time preflight rejects cloning into a target bound to a different storage backend when the tenant pins providers via StorageEngineConfig. Adds unit tests for local CopyFile (independent copy survives source deletion, traversal rejection, cross-backend rejection), cloneChunkImageInfo (empty/multi/dedup/parse-failure/OriginalURL handling), and the storage provider preflight.
This commit is contained in:
@@ -23,4 +23,9 @@ type FileService interface {
|
||||
GetFileURL(ctx context.Context, filePath string) (string, error)
|
||||
// DeleteFile deletes a file.
|
||||
DeleteFile(ctx context.Context, filePath string) error
|
||||
// CopyFile copies an existing stored object to a NEW object owned by
|
||||
// (tenantID, knowledgeID), returning the new provider:// path. The copy is
|
||||
// independent: deleting the source never affects it. Returns ErrCrossBackendCopy
|
||||
// when srcPath belongs to a different storage provider than this service.
|
||||
CopyFile(ctx context.Context, srcPath string, tenantID uint64, knowledgeID string) (string, error)
|
||||
}
|
||||
|
||||
@@ -296,6 +296,17 @@ func (kb *KnowledgeBase) GetStorageProvider() string {
|
||||
return strings.ToLower(strings.TrimSpace(kb.StorageConfig.Provider))
|
||||
}
|
||||
|
||||
// EffectiveStorageProvider returns the KB's storage provider, falling back to
|
||||
// the supplied tenant default when the KB does not pin one. This mirrors the
|
||||
// selection logic in resolveFileService and is used by clone preflight checks
|
||||
// to detect cross-storage-backend clones (which are not supported).
|
||||
func (kb *KnowledgeBase) EffectiveStorageProvider(tenantDefault string) string {
|
||||
if p := kb.GetStorageProvider(); p != "" {
|
||||
return p
|
||||
}
|
||||
return strings.ToLower(strings.TrimSpace(tenantDefault))
|
||||
}
|
||||
|
||||
// SetStorageProvider writes the provider to the new StorageProviderConfig field.
|
||||
func (kb *KnowledgeBase) SetStorageProvider(provider string) {
|
||||
if kb == nil {
|
||||
|
||||
@@ -94,10 +94,10 @@ func TestKnowledgeBase_VectorStoreID_JSON(t *testing.T) {
|
||||
})
|
||||
|
||||
unmarshalCases := []struct {
|
||||
name string
|
||||
body string
|
||||
wantNil bool
|
||||
wantValue string
|
||||
name string
|
||||
body string
|
||||
wantNil bool
|
||||
wantValue string
|
||||
}{
|
||||
{name: "missing field", body: `{"id":"kb-1"}`, wantNil: true},
|
||||
{name: "explicit null", body: `{"id":"kb-1","vector_store_id":null}`, wantNil: true},
|
||||
@@ -157,8 +157,6 @@ func TestKnowledgeBase_UnmarshalJSON_WithVectorStoreID(t *testing.T) {
|
||||
// If a future change introduces such a shadow, the value above would fail to populate.
|
||||
}
|
||||
|
||||
|
||||
|
||||
// TestKnowledgeBase_HasVectorStore covers the nil-safe binding accessor.
|
||||
func TestKnowledgeBase_HasVectorStore(t *testing.T) {
|
||||
t.Run("nil receiver returns false", func(t *testing.T) {
|
||||
@@ -269,3 +267,51 @@ func TestKnowledgeBase_SharesStoreWith(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEffectiveStorageProvider(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
kbProvider string
|
||||
tenantDefault string
|
||||
want string
|
||||
}{
|
||||
{"kb pins provider", "minio", "cos", "minio"},
|
||||
{"kb empty falls back to tenant default", "", "cos", "cos"},
|
||||
{"both empty", "", "", ""},
|
||||
{"tenant default cased", "", " COS ", "cos"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
kb := &KnowledgeBase{}
|
||||
if tt.kbProvider != "" {
|
||||
kb.StorageProviderConfig = &StorageProviderConfig{Provider: tt.kbProvider}
|
||||
}
|
||||
if got := kb.EffectiveStorageProvider(tt.tenantDefault); got != tt.want {
|
||||
t.Errorf("EffectiveStorageProvider(%q) with kb=%q = %q, want %q",
|
||||
tt.tenantDefault, tt.kbProvider, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestEffectiveStorageProvider_CrossBackendDetection documents the comparison the
|
||||
// clone preflight performs: a mismatch is only flagged when both effective
|
||||
// providers are non-empty and differ.
|
||||
func TestEffectiveStorageProvider_CrossBackendDetection(t *testing.T) {
|
||||
tenantDefault := "minio"
|
||||
src := &KnowledgeBase{} // inherits tenant default -> minio
|
||||
dst := &KnowledgeBase{StorageProviderConfig: &StorageProviderConfig{Provider: "cos"}}
|
||||
|
||||
sp := src.EffectiveStorageProvider(tenantDefault)
|
||||
dp := dst.EffectiveStorageProvider(tenantDefault)
|
||||
if sp == "" || dp == "" || sp == dp {
|
||||
t.Fatalf("expected cross-backend mismatch, got src=%q dst=%q", sp, dp)
|
||||
}
|
||||
|
||||
// Same effective provider (dst empty inherits the same tenant default) must NOT be flagged.
|
||||
dstSame := &KnowledgeBase{}
|
||||
if dstSame.EffectiveStorageProvider(tenantDefault) != sp {
|
||||
t.Errorf("same tenant default should match: got %q vs %q",
|
||||
dstSame.EffectiveStorageProvider(tenantDefault), sp)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user