fix(knowledge): stop deleting rows from blocking duplicate uploads

CheckKnowledgeExists excluded only parse_status='failed'. A row whose
async deletion was in flight (or stranded — worker death mid-delete, or
the dead-letter re-mark skipped because the status had moved on) stayed
at 'deleting' and kept the duplicate check matching forever, so
re-uploading the file returned "document already exists" with no path
back except manual SQL (issue #3338).

A deleting row is on its way out: an upload landing while the delete
task is queued or running ends with exactly one live row whichever way
the task concludes — success soft-deletes the old row, retry exhaustion
marks it failed. Excluding 'deleting' alongside 'failed' is therefore
semantically safe without a staleness threshold.

The duplicate-check status policy is now pinned by a matrix test
(failed/deleting pass, pending/processing/completed still duplicate)
covering both the hash path and the filename/size fallback.
main
vicfei 2026-09-17 11:32:59 +08:00 committed by lyingbug
parent d8be87d940
commit 55ec13ac41
2 changed files with 48 additions and 1 deletions

View File

@ -362,8 +362,16 @@ func (r *knowledgeRepository) CheckKnowledgeExists(
kbID string,
params *types.KnowledgeCheckParams,
) (bool, *types.Knowledge, error) {
// Failed rows never block a retry, and neither do rows whose deletion is
// in flight: a deleting row is on its way out, so an upload landing while
// the async delete task is still queued/running ends with exactly one
// live row whichever way the task concludes (success soft-deletes the old
// row; exhaustion marks it failed). Letting deleting rows block the
// duplicate check turned a task that never finishes into a permanent
// "document already exists" that only manual SQL could clear (issue #3338).
query := r.db.WithContext(ctx).Model(&types.Knowledge{}).
Where("tenant_id = ? AND knowledge_base_id = ? AND parse_status <> ?", tenantID, kbID, "failed")
Where("tenant_id = ? AND knowledge_base_id = ? AND parse_status NOT IN ?",
tenantID, kbID, []string{"failed", "deleting"})
switch params.Type {
case "file":

View File

@ -99,3 +99,42 @@ func TestCheckKnowledgeExists_FileSourceIdentity(t *testing.T) {
})
}
}
func TestCheckKnowledgeExists_ParseStatusMatrix(t *testing.T) {
// Pins the duplicate-check status policy (issue #3338): failed and
// deleting rows must not block an upload — a deleting row is on its way
// out regardless of how the async delete task concludes — while pending,
// processing and completed rows remain real duplicates.
for _, tc := range []struct {
parseStatus string
want bool
}{
{"failed", false},
{"deleting", false},
{"pending", true},
{"processing", true},
{"completed", true},
} {
t.Run("parse_status="+tc.parseStatus, func(t *testing.T) {
db := setupKnowledgeTestDB(t)
repo := NewKnowledgeRepository(db)
require.NoError(t, db.Exec(`
INSERT INTO knowledges (id, tenant_id, knowledge_base_id, type, file_name, file_type, file_size, file_hash, parse_status)
VALUES (?, 1, 'kb-1', 'file', 'doc.md', 'md', 10, 'hash-1', ?)
`, uuid.NewString(), tc.parseStatus).Error)
exists, _, err := repo.CheckKnowledgeExists(context.Background(), 1, "kb-1", &types.KnowledgeCheckParams{
Type: "file", FileHash: "hash-1", FileType: "md",
})
require.NoError(t, err)
require.Equal(t, tc.want, exists)
// The filename/size fallback path shares the same base filter.
exists, _, err = repo.CheckKnowledgeExists(context.Background(), 1, "kb-1", &types.KnowledgeCheckParams{
Type: "file", FileName: "doc.md", FileSize: 10,
})
require.NoError(t, err)
require.Equal(t, tc.want, exists)
})
}
}