fix(knowledge): unstick summary and isolate manual re-index trace attempts

Manually uploaded text documents could get stuck showing "generating
summary" forever, and editing/re-saving a document mixed its trace into
the previous attempt.

Three related fixes:

1. finalizeIndexedKnowledgeState no longer marks a text document
   completed the moment chunks are indexed. Doing so made
   KnowledgePostProcess hit its non-processing guard and skip the
   summary/question/graph fan-out, stranding summary_status on
   "pending". The row now stays "processing" (still retrievable via
   enable_status=enabled) and post-process remains the sole authority
   that drives processing -> finalizing -> completed.

2. ProcessManualUpdate now allocates a fresh span-tracking attempt via
   OpenAttempt. Previously it ran with attempt 0, so processChunks
   dropped every stage span and KnowledgePostProcess fell back to
   LatestAttempt, piling the new run's summary/wiki subspans onto the
   prior attempt's trace.

3. Enrichment workers (summary, question, graph extract) skip when a
   newer attempt has superseded theirs. A stale subtask from a previous
   upload/edit/reparse must not read deleted chunks or decrement the new
   attempt's pending_subtasks_count, which would race-promote the row to
   completed before the new attempt finishes.

Updated TestFinalizeIndexedKnowledgeState to expect the deferred
completion for text documents.
This commit is contained in:
wizardchen
2026-05-29 17:44:20 +08:00
committed by lyingbug
parent 9f139ff4d8
commit 9cb9505114
4 changed files with 77 additions and 13 deletions
+10
View File
@@ -202,6 +202,16 @@ func (s *ChunkExtractService) Handle(ctx context.Context, t *asynq.Task) error {
ctx = logger.WithField(ctx, "extract", p.ChunkID)
ctx = context.WithValue(ctx, types.TenantIDContextKey, p.TenantID)
// A newer attempt (re-upload / edit / reparse) has superseded this one:
// skip before opening the span or registering the FinalizeSubtask defer.
// The chunk this task references was deleted by the new attempt's cleanup,
// and decrementing here would drain the new attempt's counter.
if attemptSuperseded(ctx, s.tracker(), p.KnowledgeID, p.Attempt) {
logger.Infof(ctx, "graph extract: attempt %d superseded for %s, skipping stale enrichment",
p.Attempt, p.KnowledgeID)
return nil
}
// Open a postprocess subspan keyed by chunk ordinal so the trace
// shows real per-chunk graph extraction time. Skipped silently when
// upstream didn't pass the parent attempt (legacy in-flight tasks)