From 55ec13ac41449a27ede696a3a2d74fd3ab762284 Mon Sep 17 00:00:00 2001 From: vicfei Date: Thu, 17 Sep 2026 11:32:59 +0800 Subject: [PATCH] fix(knowledge): stop deleting rows from blocking duplicate uploads MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- internal/application/repository/knowledge.go | 10 ++++- .../repository/knowledge_duplicate_test.go | 39 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go index db0e1f7d..a69a8121 100644 --- a/internal/application/repository/knowledge.go +++ b/internal/application/repository/knowledge.go @@ -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": diff --git a/internal/application/repository/knowledge_duplicate_test.go b/internal/application/repository/knowledge_duplicate_test.go index 57e43a37..32f18800 100644 --- a/internal/application/repository/knowledge_duplicate_test.go +++ b/internal/application/repository/knowledge_duplicate_test.go @@ -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) + }) + } +}