fix: correct graph and multimodal processing traces

This commit is contained in:
unknown
2026-07-22 14:51:43 +08:00
committed by lyingbug
parent b9b81a6c3c
commit ab0b1d65b5
7 changed files with 331 additions and 32 deletions
@@ -54,6 +54,25 @@ func (s *KnowledgePostProcessService) tracker() SpanTracker {
return s.spanTracker
}
// finishRunningMultimodalStage closes the multimodal stage only when image
// work really ran and is still open. The canonical stage also exists when
// multimodal processing is disabled, but that row is already "skipped" and
// must not be rewritten to "done" with the postprocess queueing delay as its
// duration.
func (s *KnowledgePostProcessService) finishRunningMultimodalStage(
ctx context.Context,
knowledgeID string,
attempt int,
) {
mm := s.tracker().LookupStage(ctx, knowledgeID, attempt, types.StageMultimodal)
if mm == nil ||
mm.Kind != types.SpanKindStage ||
mm.Status != types.SpanStatusRunning {
return
}
s.tracker().EndSpan(ctx, mm, nil)
}
// Handle implements asynq handler for TypeKnowledgePostProcess.
func (s *KnowledgePostProcessService) Handle(ctx context.Context, task *asynq.Task) error {
var payload types.KnowledgePostProcessPayload
@@ -79,15 +98,12 @@ func (s *KnowledgePostProcessService) Handle(ctx context.Context, task *asynq.Ta
// Close the multimodal stage span (parent enqueued it as "running"
// and we never see the per-image fan-in here other than by reaching
// post-process). If the parent skipped multimodal entirely, the
// stage row will already be in "skipped" state and EndSpan is a
// no-op for missing rows. Per-image success/failure counts are NOT
// stage row will already be in "skipped" state and must remain so.
// Per-image success/failure counts are NOT
// aggregated here — the frontend already walks the children when
// rendering the multimodal stage detail and counts them itself,
// avoiding an extra query path.
if mm := s.tracker().LookupStage(ctx, payload.KnowledgeID, attempt, types.StageMultimodal); mm != nil &&
mm.Kind == types.SpanKindStage {
s.tracker().EndSpan(ctx, mm, nil)
}
s.finishRunningMultimodalStage(ctx, payload.KnowledgeID, attempt)
postSpan := s.tracker().BeginStage(ctx, payload.KnowledgeID, attempt, types.StagePostProcess, nil)
@@ -0,0 +1,54 @@
package service
import (
"context"
"testing"
"github.com/Tencent/WeKnora/internal/types"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFinishRunningMultimodalStage_PreservesSkippedStage(t *testing.T) {
tracker, db := setupSpanTrackerTest(t)
ctx := context.Background()
_, attempt, err := tracker.OpenAttempt(ctx, "kid-multimodal-disabled", "")
require.NoError(t, err)
mm := tracker.BeginStage(ctx, "kid-multimodal-disabled", attempt, types.StageMultimodal, nil)
require.NotNil(t, mm)
tracker.SkipSpan(ctx, mm, "skipped")
service := &KnowledgePostProcessService{spanTracker: tracker}
service.finishRunningMultimodalStage(ctx, "kid-multimodal-disabled", attempt)
var row types.KnowledgeProcessingSpan
require.NoError(t, db.Table("knowledge_processing_spans").
Where("knowledge_id = ? AND attempt = ? AND name = ?",
"kid-multimodal-disabled", attempt, types.StageMultimodal).
First(&row).Error)
assert.Equal(t, types.SpanStatusSkipped, row.Status)
assert.Equal(t, int64(0), row.DurationMs)
}
func TestFinishRunningMultimodalStage_CompletesRunningStage(t *testing.T) {
tracker, db := setupSpanTrackerTest(t)
ctx := context.Background()
_, attempt, err := tracker.OpenAttempt(ctx, "kid-multimodal-enabled", "")
require.NoError(t, err)
mm := tracker.BeginStage(ctx, "kid-multimodal-enabled", attempt, types.StageMultimodal, nil)
require.NotNil(t, mm)
service := &KnowledgePostProcessService{spanTracker: tracker}
service.finishRunningMultimodalStage(ctx, "kid-multimodal-enabled", attempt)
var row types.KnowledgeProcessingSpan
require.NoError(t, db.Table("knowledge_processing_spans").
Where("knowledge_id = ? AND attempt = ? AND name = ?",
"kid-multimodal-enabled", attempt, types.StageMultimodal).
First(&row).Error)
assert.Equal(t, types.SpanStatusDone, row.Status)
assert.NotNil(t, row.FinishedAt)
assert.GreaterOrEqual(t, row.DurationMs, int64(0))
}
@@ -76,6 +76,7 @@ type Span struct {
ParentSpanID string
Name string
Kind string
Status string
StartedAt time.Time
}
@@ -279,6 +280,7 @@ func (t *spanTracker) OpenAttempt(ctx context.Context, knowledgeID, langfuseTrac
SpanID: rootID,
Name: "knowledge_processing",
Kind: types.SpanKindRoot,
Status: types.SpanStatusRunning,
StartedAt: now,
}, attempt, nil
}
@@ -309,8 +311,8 @@ func (t *spanTracker) BeginStage(ctx context.Context, knowledgeID string, attemp
return nil
}
var (
rootID string
existing *types.KnowledgeProcessingSpan
rootID string
existing *types.KnowledgeProcessingSpan
)
for i := range rows {
r := rows[i]
@@ -365,6 +367,7 @@ func (t *spanTracker) BeginStage(ctx context.Context, knowledgeID string, attemp
ParentSpanID: existing.ParentSpanID,
Name: existing.Name,
Kind: existing.Kind,
Status: types.SpanStatusRunning,
StartedAt: now,
}
}
@@ -394,6 +397,7 @@ func (t *spanTracker) BeginStage(ctx context.Context, knowledgeID string, attemp
ParentSpanID: rootID,
Name: stage,
Kind: types.SpanKindStage,
Status: types.SpanStatusRunning,
StartedAt: now,
}
}
@@ -441,6 +445,7 @@ func (t *spanTracker) BeginSubSpan(ctx context.Context, parent *Span, name, kind
ParentSpanID: parent.SpanID,
Name: name,
Kind: kind,
Status: types.SpanStatusRunning,
StartedAt: now,
}
}
@@ -578,6 +583,7 @@ func (t *spanTracker) LookupStage(ctx context.Context, knowledgeID string, attem
ParentSpanID: r.ParentSpanID,
Name: r.Name,
Kind: r.Kind,
Status: r.Status,
StartedAt: started,
}
}
@@ -611,6 +617,7 @@ func (t *spanTracker) LookupSpanByName(ctx context.Context, knowledgeID string,
ParentSpanID: r.ParentSpanID,
Name: r.Name,
Kind: r.Kind,
Status: r.Status,
StartedAt: started,
}
}