fix(knowledge): make finalizing subtask counter reliable + async question fan-out

The finalizing subtask counter (pending_subtasks_count) could get stuck at a
non-zero value, leaving knowledge permanently in "finalizing" and never
promoted to "completed". Root cause and related hardening:

- UpdateKnowledge does a full-row Save; pending_subtasks_count was NOT in the
  omit list, so any concurrent enrichment subtask that loaded the row, did slow
  work (LLM call), then saved an unrelated field wrote back the STALE counter,
  clobbering decrements other subtasks performed in between. Add
  PendingSubtasksCount to omitFieldsOnUpdate so only the atomic helpers
  (SetFinalizing / FinalizeSubtask / explicit column writes) ever touch it.
- FinalizeSubtask no longer gates the finalizing->completed promote on a
  separate re-read of the counter. Every caller unconditionally runs the
  guarded promote UPDATE (WHERE pending_subtasks_count = 0), the single
  authoritative atomic check, so a racy/stale read can't strand the row.
- Decrements run on a context detached from the caller's cancellation
  (context.WithoutCancel + timeout) so graceful shutdown / preemption can't
  silently skip a decrement.
- Reconcile seeded vs actually-enqueued subtask slots (summary/question/graph)
  and release any shortfall so un-enqueued planned slots (e.g. graph with
  NEO4J off) don't strand the row.
- Reparse paths reset pending_subtasks_count via an explicit column write since
  full-row Save now omits it.

Question generation is also moved off the synchronous single-task path onto a
batched async fan-out (windows of text chunks), each batch independently
queued / retried / traced, grouped under a postprocess.question span.
This commit is contained in:
wizardchen
2026-05-30 00:09:46 +08:00
committed by lyingbug
parent 9ffb886e40
commit 84bd95275d
8 changed files with 744 additions and 109 deletions
+27
View File
@@ -93,6 +93,33 @@ type QuestionGenerationPayload struct {
// tasks queued before this field shipped, or callers without a
// tracker).
Attempt int `json:"attempt,omitempty"`
// ChunkIDs switches the handler into batched fan-out mode: the task
// generates questions for this ordered window of text chunks only.
// Batching (rather than one task per chunk) keeps the task count
// bounded for very large documents, while still giving each batch
// independent retry / cancellation / tracing and letting the worker
// do a single embedding BatchIndex per batch. Empty means the legacy
// whole-knowledge mode (kept for in-flight tasks queued before fan-out
// shipped), where the handler iterates all text chunks itself.
// Following the ExtractChunkPayload precedent, we carry only chunk ids
// (not their content) so the payload stays small and the worker reads
// fresh content at run time.
ChunkIDs []string `json:"chunk_ids,omitempty"`
// ChunkID is the single-chunk variant of ChunkIDs, retained only so
// tasks enqueued by an interim per-chunk build still run (treated as a
// one-element batch). New enqueues use ChunkIDs.
ChunkID string `json:"chunk_id,omitempty"`
// BatchIndex is the 0-based ordinal of this batch inside the parent
// knowledge's text-chunk set, used as the subspan name suffix
// ("postprocess.question.batch[3]") so the timeline preserves order.
BatchIndex int `json:"batch_index,omitempty"`
// PrevChunkID / NextChunkID are the text chunks (by StartAt) just
// outside this batch window, computed at enqueue time so the worker can
// rebuild the same surrounding context the legacy whole-knowledge loop
// used at the batch boundaries, without re-listing every chunk of the
// knowledge. Empty when the batch is at a document boundary.
PrevChunkID string `json:"prev_chunk_id,omitempty"`
NextChunkID string `json:"next_chunk_id,omitempty"`
}
// SummaryGenerationPayload represents the summary generation task payload