fix(knowledge): stop async delete from leaving visible/zombie rows (#2192)
Two backend fixes for the v0.7.0 regression where a deleted document keeps
showing in the list ('delete success' toast) and a follow-up reparse then
reports the file is missing:
1. List filtering: applyKnowledgeListFilter now hides rows in the transient
'deleting' state from the default document list. The async delete pipeline
marks a row 'deleting' before tearing down its resources, so without this
the entry lingers as a normal doc until it is physically gone. Rows whose
delete task exhausts retries are flipped to 'failed' by the dead-letter
callback and stay visible so the failure remains actionable; an explicit
parse_status=deleting filter can still surface in-flight rows.
2. Delete ordering: DeleteKnowledge / DeleteKnowledgeList now delete the DB
row FIRST and defer physical file (+ extracted image) cleanup until after
the row is gone. Previously file removal ran concurrently with the
index/chunk/graph cleanup; when one of those failed the row was kept but
the file could already be deleted, producing a 'file missing but row
present' zombie that could neither be reparsed nor cleanly re-deleted.
Orphaning a file after the row is gone is the tolerable failure mode.
Adds a repository regression test covering the default/explicit deleting
visibility rules.
This commit is contained in:
@@ -136,6 +136,13 @@ func applyKnowledgeListFilter(query *gorm.DB, filter types.KnowledgeListFilter)
|
||||
}
|
||||
if filter.ParseStatus != "" {
|
||||
query = query.Where("parse_status = ?", filter.ParseStatus)
|
||||
} else {
|
||||
// Hide rows that are mid-deletion so an async delete never lingers in the
|
||||
// document list as if it were a normal entry (issue #2192). The delete
|
||||
// pipeline marks the row `deleting` before tearing down its resources; a
|
||||
// row whose delete task exhausts its retries is flipped to `failed` by the
|
||||
// dead-letter callback and stays visible so the failure remains actionable.
|
||||
query = query.Where("parse_status <> ?", types.ParseStatusDeleting)
|
||||
}
|
||||
if !filter.UpdatedFrom.IsZero() {
|
||||
query = query.Where("updated_at >= ?", filter.UpdatedFrom)
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// insertKnowledgeInKB seeds a knowledge row in the given KB with the given
|
||||
// parse_status so we can assert the list filter's visibility rules.
|
||||
func insertKnowledgeInKB(t *testing.T, db *gorm.DB, tenantID uint64, kbID, status string) string {
|
||||
t.Helper()
|
||||
id := uuid.New().String()
|
||||
require.NoError(t, db.Exec(`
|
||||
INSERT INTO knowledges (id, tenant_id, knowledge_base_id, type, title, source, parse_status)
|
||||
VALUES (?, ?, ?, 'file', 'list-filter-test', 'manual', ?)
|
||||
`, id, tenantID, kbID, status).Error)
|
||||
return id
|
||||
}
|
||||
|
||||
// TestListPaged_ExcludesDeletingByDefault documents the fix for issue #2192:
|
||||
// a knowledge row that is mid-deletion (parse_status = 'deleting') must not
|
||||
// appear in the default document list, otherwise a successful async delete
|
||||
// keeps showing the entry until the row is physically gone. Rows in other
|
||||
// states (including the terminal 'failed' a stuck delete resolves to) stay
|
||||
// visible, and an explicit parse_status=deleting filter can still surface them.
|
||||
func TestListPaged_ExcludesDeletingByDefault(t *testing.T) {
|
||||
db := setupKnowledgeTestDB(t)
|
||||
repo := NewKnowledgeRepository(db).(*knowledgeRepository)
|
||||
ctx := context.Background()
|
||||
|
||||
const tenantID = uint64(1)
|
||||
kbID := uuid.New().String()
|
||||
|
||||
completedID := insertKnowledgeInKB(t, db, tenantID, kbID, "completed")
|
||||
failedID := insertKnowledgeInKB(t, db, tenantID, kbID, "failed")
|
||||
deletingID := insertKnowledgeInKB(t, db, tenantID, kbID, types.ParseStatusDeleting)
|
||||
|
||||
page := &types.Pagination{Page: 1, PageSize: 100}
|
||||
|
||||
// Default listing (no explicit parse_status): deleting rows are hidden.
|
||||
rows, total, err := repo.ListPagedKnowledgeByKnowledgeBaseID(
|
||||
ctx, tenantID, kbID, page, types.KnowledgeListFilter{},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.ID)
|
||||
}
|
||||
assert.ElementsMatch(t, []string{completedID, failedID}, ids,
|
||||
"default list must include completed + failed but exclude deleting")
|
||||
assert.Equal(t, int64(2), total, "count must match the filtered rows")
|
||||
|
||||
// Explicit parse_status=deleting still surfaces the in-flight row so
|
||||
// operators / tooling can inspect stuck deletes.
|
||||
rows, total, err = repo.ListPagedKnowledgeByKnowledgeBaseID(
|
||||
ctx, tenantID, kbID, page,
|
||||
types.KnowledgeListFilter{ParseStatus: types.ParseStatusDeleting},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, rows, 1)
|
||||
assert.Equal(t, deletingID, rows[0].ID)
|
||||
assert.Equal(t, int64(1), total)
|
||||
}
|
||||
Reference in New Issue
Block a user