fix(knowledge): clear stale error_message on reprocessing and finalize

A knowledge item that failed once keeps error_message set; when it is
reprocessed or driven through the finalizing pipeline, the stale message
was never cleared, so the UI kept showing an outdated failure on items
that were processing or had completed successfully.

- SetFinalizing and FinalizeSubtask now clear error_message in the same
  atomic UPDATE that transitions parse_status
- ProcessDocument clears knowledge.ErrorMessage when flipping the row
  to processing for a new attempt

Add a SQLite-backed regression test covering both transitions.
main
shangfeng-liu 2026-08-02 01:58:10 +08:00 committed by lyingbug
parent ccb0cee3e6
commit 44206e4ad4
3 changed files with 52 additions and 4 deletions

View File

@ -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 {

View File

@ -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

View File

@ -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)