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) + }) + } +}