feat(knowledge): count wiki ingest in the finalizing subtask counter

Wiki ingest runs asynchronously after the parse pipeline and was not
counted in pending_subtasks_count, so a document flipped to "completed"
while wiki generation was still minutes away (30s debounce + batch +
retries). That hid the in-progress state and dropped the stop-parse
affordance before wiki actually finished.

Count wiki as a single enrichment subtask (when WikiEnabled and the doc
has text chunks, matching the enqueue condition) so the row stays in
"finalizing" until wiki is done. The batch worker drains the slot at the
op's terminal state: once on successful map, and once when an op is
dead-lettered after exhausting in-batch retries. Retract ops (deleted
knowledge) are skipped. FinalizeSubtask guards both the decrement and the
promote, so a wiki op enqueued before this accounting shipped is a
harmless no-op on an already-completed row. A wiki op that never drains is
bounded by the existing housekeeping finalizing sweep.
This commit is contained in:
wizardchen
2026-05-29 18:00:29 +08:00
committed by lyingbug
parent 95f7cd7d61
commit 72272b2d62
3 changed files with 56 additions and 6 deletions
@@ -143,12 +143,19 @@ func (s *KnowledgePostProcessService) Handle(ctx context.Context, task *asynq.Ta
// on its terminal exit; the row promotes itself to "completed" when
// the counter hits zero (see knowledgeRepository.FinalizeSubtask).
//
// Wiki ingest is NOT counted here — it's a KB-scoped debounced
// batch with its own dedup queue; per-knowledge accounting is not
// meaningful for it.
// Wiki ingest IS counted (as a single subtask): although it's a
// KB-scoped debounced batch, each upload enqueues exactly one
// per-knowledge op, and the batch worker calls FinalizeSubtask once
// when that op reaches a terminal state (mapped successfully or
// dead-lettered after exhausting retries). Counting it keeps the row
// in "finalizing" — i.e. shown as in-progress and still cancellable —
// until wiki generation actually finishes instead of flipping to
// completed while wiki runs minutes later. A wiki op that never
// drains is bounded by the housekeeping finalizing sweep.
willSpawnSummary := len(textChunks) > 0
willSpawnQuestion := willSpawnSummary && kb.NeedsEmbeddingModel() &&
kb.QuestionGenerationConfig != nil && kb.QuestionGenerationConfig.Enabled
willSpawnWiki := kb.IndexingStrategy.WikiEnabled && len(textChunks) > 0
graphChunkCount := 0
if kb.IsGraphEnabled() {
graphChunkCount = len(textChunks)
@@ -160,6 +167,9 @@ func (s *KnowledgePostProcessService) Handle(ctx context.Context, task *asynq.Ta
if willSpawnQuestion {
expectedSubtasks++
}
if willSpawnWiki {
expectedSubtasks++
}
expectedSubtasks += graphChunkCount
switch {
@@ -265,7 +275,7 @@ func (s *KnowledgePostProcessService) Handle(ctx context.Context, task *asynq.Ta
// 6. Spawn Wiki Ingest Task if wiki indexing is enabled in IndexingStrategy
enqueuedWiki := false
if kb.IndexingStrategy.WikiEnabled && len(textChunks) > 0 {
if willSpawnWiki {
EnqueueWikiIngest(ctx, s.taskEnqueuer, s.pendingRepo, payload.TenantID, payload.KnowledgeBaseID, payload.KnowledgeID)
logger.Infof(ctx, "[KnowledgePostProcess] Enqueued wiki ingest task for %s", payload.KnowledgeID)
enqueuedWiki = true