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:
@@ -94,6 +94,13 @@ type SpanTracker interface {
|
||||
// created by the upstream pipeline.
|
||||
LookupStage(ctx context.Context, knowledgeID string, attempt int, stage string) *Span
|
||||
|
||||
// LookupSpanByName returns the first span of any kind matching name
|
||||
// for (knowledgeID, attempt) — the cross-process bridge that lets a
|
||||
// fan-out worker (e.g. a question-generation batch) attach its subspan
|
||||
// under a grouping span created earlier by the orchestrator. Returns
|
||||
// nil when no such span exists (caller should fall back to the stage).
|
||||
LookupSpanByName(ctx context.Context, knowledgeID string, attempt int, name string) *Span
|
||||
|
||||
// FinalizeAttempt closes the root span for (knowledgeID, attempt)
|
||||
// with the given terminal status (done | failed). Idempotent:
|
||||
// re-closing an already-terminal root is a no-op so callers from
|
||||
@@ -547,6 +554,38 @@ func (t *spanTracker) LookupStage(ctx context.Context, knowledgeID string, attem
|
||||
return nil
|
||||
}
|
||||
|
||||
func (t *spanTracker) LookupSpanByName(ctx context.Context, knowledgeID string, attempt int, name string) *Span {
|
||||
if name == "" || knowledgeID == "" || attempt <= 0 {
|
||||
return nil
|
||||
}
|
||||
rows, err := t.repo.ListByAttempt(ctx, knowledgeID, attempt)
|
||||
if err != nil {
|
||||
logger.Warnf(ctx, "[SpanTracker] LookupSpanByName list failed kid=%s attempt=%d: %v",
|
||||
knowledgeID, attempt, err)
|
||||
return nil
|
||||
}
|
||||
for i := range rows {
|
||||
r := rows[i]
|
||||
if r.Name != name {
|
||||
continue
|
||||
}
|
||||
started := time.Time{}
|
||||
if r.StartedAt != nil {
|
||||
started = *r.StartedAt
|
||||
}
|
||||
return &Span{
|
||||
KnowledgeID: r.KnowledgeID,
|
||||
Attempt: r.Attempt,
|
||||
SpanID: r.SpanID,
|
||||
ParentSpanID: r.ParentSpanID,
|
||||
Name: r.Name,
|
||||
Kind: r.Kind,
|
||||
StartedAt: started,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// cascadeDependentStages flips downstream STAGE rows to "cancelled" using
|
||||
// types.StageDependencies. Without this, a Chunking failure leaves
|
||||
// Embedding / Multimodal as "pending" forever, even though they cannot
|
||||
@@ -794,6 +833,9 @@ func (noopSpanTracker) EndSpan(_ context.Context, _ *Span, _ types.JSONMap)
|
||||
func (noopSpanTracker) FailSpan(_ context.Context, _ *Span, _, _ string, _ error) {}
|
||||
func (noopSpanTracker) SkipSpan(_ context.Context, _ *Span, _ string) {}
|
||||
func (noopSpanTracker) LookupStage(_ context.Context, _ string, _ int, _ string) *Span { return nil }
|
||||
func (noopSpanTracker) LookupSpanByName(_ context.Context, _ string, _ int, _ string) *Span {
|
||||
return nil
|
||||
}
|
||||
func (noopSpanTracker) FinalizeAttempt(_ context.Context, _ string, _ int, _ string, _ types.JSONMap, _, _ string) {
|
||||
}
|
||||
func (noopSpanTracker) AbortAttempt(_ context.Context, _ string, _ int, _, _, _ string) {}
|
||||
|
||||
Reference in New Issue
Block a user