diff --git a/internal/application/repository/knowledge.go b/internal/application/repository/knowledge.go index 095dcaad..9c17687b 100644 --- a/internal/application/repository/knowledge.go +++ b/internal/application/repository/knowledge.go @@ -437,7 +437,10 @@ func (r *knowledgeRepository) UpdateActiveDeletingKnowledgeColumns( // FinalizeSubtask atomically decrements pending_subtasks_count and, when // the counter reaches zero while parse_status is still 'finalizing', // flips the row to 'completed' in the same statement so concurrent -// subtask completions can't race the promotion. +// subtask completions can't race the promotion. Both this promotion and +// SetFinalizing clear error_message: a row that re-enters processing or +// finishes successfully must not keep displaying a failure from a +// previous attempt. // // Returns (newCount, promoted, error). promoted is true iff this caller // was the one whose UPDATE flipped 'finalizing'→'completed'. @@ -481,9 +484,10 @@ func (r *knowledgeRepository) FinalizeSubtask( Where("id = ? AND parse_status = ? AND pending_subtasks_count = 0", id, types.ParseStatusFinalizing). Updates(map[string]interface{}{ - "parse_status": types.ParseStatusCompleted, - "processed_at": now, - "updated_at": now, + "parse_status": types.ParseStatusCompleted, + "error_message": "", + "processed_at": now, + "updated_at": now, }) if promoteRes.Error != nil { return 0, false, promoteRes.Error @@ -526,6 +530,7 @@ func (r *knowledgeRepository) SetFinalizing( Updates(map[string]interface{}{ "parse_status": types.ParseStatusFinalizing, "pending_subtasks_count": expectedSubtasks, + "error_message": "", "updated_at": now, }) if res.Error != nil { diff --git a/internal/application/repository/knowledge_finalize_test.go b/internal/application/repository/knowledge_finalize_test.go index d75b0d9f..cd1cc39b 100644 --- a/internal/application/repository/knowledge_finalize_test.go +++ b/internal/application/repository/knowledge_finalize_test.go @@ -91,6 +91,13 @@ func reloadKnowledgeRow(t *testing.T, db *gorm.DB, id string) (status string, co return status, count } +func reloadKnowledgeErrorMessage(t *testing.T, db *gorm.DB, id string) string { + t.Helper() + var msg string + require.NoError(t, db.Raw(`SELECT COALESCE(error_message, '') FROM knowledges WHERE id = ?`, id).Scan(&msg).Error) + return msg +} + func insertKnowledgeWithStatus(t *testing.T, db *gorm.DB, status string, deleted bool) string { t.Helper() id := uuid.New().String() @@ -206,6 +213,41 @@ func TestFinalizeSubtask_DecrementClampedAtZero(t *testing.T) { assert.Equal(t, 0, count, "pending_subtasks_count must be clamped at zero") } +// TestSetFinalizingAndFinalizeSubtask_ClearStaleErrorMessage is the +// regression test for stale error_message: a row that failed once keeps +// error_message set, and both entering finalizing (a new attempt) and +// promoting to completed (a successful finish) must clear it so the UI +// no longer shows an outdated failure. +func TestSetFinalizingAndFinalizeSubtask_ClearStaleErrorMessage(t *testing.T) { + db := setupKnowledgeTestDB(t) + repo := NewKnowledgeRepository(db).(*knowledgeRepository) + ctx := context.Background() + + id := insertProcessingKnowledge(t, db) + require.NoError(t, db.Exec( + `UPDATE knowledges SET error_message = ? WHERE id = ?`, + "Task interrupted due to application restart", + id, + ).Error) + + transitioned, err := repo.SetFinalizing(ctx, id, 1) + require.NoError(t, err) + require.True(t, transitioned) + assert.Empty(t, reloadKnowledgeErrorMessage(t, db, id), + "SetFinalizing must clear error_message from the previous attempt") + + require.NoError(t, db.Exec( + `UPDATE knowledges SET error_message = ? WHERE id = ?`, + "stale finalizing failure", + id, + ).Error) + _, promoted, err := repo.FinalizeSubtask(ctx, id) + require.NoError(t, err) + require.True(t, promoted) + assert.Empty(t, reloadKnowledgeErrorMessage(t, db, id), + "promotion to completed must clear error_message") +} + // TestUpdateKnowledge_DoesNotClobberPendingCounter is the regression test // for the original bug: a full-row Save with a stale in-memory counter // must not write that stale value back, otherwise it overwrites atomic diff --git a/internal/application/service/knowledge_process.go b/internal/application/service/knowledge_process.go index 8fbdbb5d..ed9ba6f0 100644 --- a/internal/application/service/knowledge_process.go +++ b/internal/application/service/knowledge_process.go @@ -3113,6 +3113,7 @@ func (s *knowledgeService) ProcessDocument(ctx context.Context, t *asynq.Task) e return nil } knowledge.ParseStatus = "processing" + knowledge.ErrorMessage = "" knowledge.UpdatedAt = time.Now() if err := s.repo.UpdateKnowledge(ctx, knowledge); err != nil { logger.Errorf(ctx, "failed to update knowledge status to processing: %v", err)