diff --git a/frontend/src/components/knowledge-processing-timeline.vue b/frontend/src/components/knowledge-processing-timeline.vue index 85486bf8..d5d0c70c 100644 --- a/frontend/src/components/knowledge-processing-timeline.vue +++ b/frontend/src/components/knowledge-processing-timeline.vue @@ -3,26 +3,15 @@ import { ref, reactive, onMounted, onBeforeUnmount, watch, computed, nextTick } import { MessagePlugin } from 'tdesign-vue-next' import { useI18n } from 'vue-i18n' import { getKnowledgeSpans, reparseKnowledge, cancelKnowledgeParse, getKnowledgeDetails } from '@/api/knowledge-base/index' -import { knowledgeSpansPayloadHasTrace } from '@/utils/knowledgeTrace' +import { + groupPostprocessGraphSpans, + knowledgeSpansPayloadHasTrace, + type KnowledgeTraceNode, +} from '@/utils/knowledgeTrace' import { resolveTimelineHeaderStatus } from '@/utils/knowledgeProcessingStatus' import type { KnowledgeProcessOverrides } from '@/types/knowledgeProcess' -interface SpanNode { - span_id?: string - parent_span_id?: string - name: string - kind: string - status: string - started_at?: string | null - finished_at?: string | null - duration_ms?: number - error_code?: string - error_message?: string - input?: any - output?: any - metadata?: any - children?: SpanNode[] -} +type SpanNode = KnowledgeTraceNode interface LastError { name: string @@ -160,7 +149,9 @@ const stages = computed(() => { const children = trace?.children || [] const byName = new Map() for (const c of children) { - if (c && c.kind === 'stage' && c.name) byName.set(c.name, c) + if (c && c.kind === 'stage' && c.name) { + byName.set(c.name, c.name === 'postprocess' ? groupPostprocessGraphSpans(c) : c) + } } return STAGES.map((n) => byName.get(n) || ({ name: n, kind: 'stage', status: 'pending' } as SpanNode)) }) @@ -197,6 +188,13 @@ function formatRelativeTime(ts: number): string { return t('knowledgeStages.minutesAgo', { n: min }) } +// A skipped stage did not execute. Its tiny bookkeeping interval is not +// processing time, so do not present it as (for example) multimodal time. +function formatSpanDuration(node: SpanNode): string { + if (node.status === 'skipped' || node.status === 'pending') return '—' + return formatDuration(node.duration_ms) +} + function isPolling(status?: string): boolean { // finalizing is the post-process fan-out window — subspans // (summary / question / graph.chunk[*]) are still actively producing @@ -954,6 +952,9 @@ function localizedStatus(status: string): string { function rowLabel(row: FlatRow): string { if (row.isRoot) return t('knowledgeStages.root') if (row.isStage) return t(`knowledgeStages.stage.${row.node.name}`) + if (row.node.name === 'postprocess.graph') return t('knowledgeStages.processConfig.graph') + const graphChunk = /^postprocess\.graph\.chunk\[(\d+)\]$/.exec(row.node.name) + if (graphChunk) return `${t('knowledgeStages.processConfig.graph')} #${Number(graphChunk[1]) + 1}` return row.node.name } @@ -1558,7 +1559,7 @@ const processConfigLines = computed(() => { {{ formatDuration(liveElapsedMs(row.node)) }} @@ -1587,8 +1588,9 @@ const processConfigLines = computed(() => { {{ rowLabel(row) }} · - {{ formatDuration(row.node.status === 'running' ? liveElapsedMs(row.node) - : row.node.duration_ms) }} + {{ row.node.status === 'running' + ? formatDuration(liveElapsedMs(row.node)) + : formatSpanDuration(row.node) }} · {{ localizedStatus(row.node.status) }} @@ -1674,7 +1676,7 @@ const processConfigLines = computed(() => { {{ t('knowledgeStages.detail.elapsed') }} @@ -1726,7 +1728,9 @@ const processConfigLines = computed(() => {
- {{ formatDuration(s.duration_ms) }} + {{ s.status === 'skipped' || s.status === 'pending' + ? '—' + : formatDuration(s.duration_ms) }}
diff --git a/frontend/src/utils/knowledgeTrace.test.ts b/frontend/src/utils/knowledgeTrace.test.ts new file mode 100644 index 00000000..89ad0879 --- /dev/null +++ b/frontend/src/utils/knowledgeTrace.test.ts @@ -0,0 +1,109 @@ +import assert from 'node:assert/strict' +import test from 'node:test' + +import { groupPostprocessGraphSpans, type KnowledgeTraceNode } from './knowledgeTrace.ts' + +function graphChunk(index: number, overrides: Partial = {}): KnowledgeTraceNode { + return { + span_id: `graph-${index}`, + parent_span_id: 'postprocess', + name: `postprocess.graph.chunk[${index}]`, + kind: 'subspan', + status: 'done', + started_at: `2026-07-21T08:00:0${index}.000Z`, + finished_at: `2026-07-21T08:00:0${index + 2}.000Z`, + duration_ms: 2000, + ...overrides, + } +} + +test('groups graph chunks and reports their wall-clock duration', () => { + const summary: KnowledgeTraceNode = { + span_id: 'summary', + name: 'postprocess.summary', + kind: 'subspan', + status: 'done', + } + const stage: KnowledgeTraceNode = { + span_id: 'postprocess', + name: 'postprocess', + kind: 'stage', + status: 'done', + children: [summary, graphChunk(0), graphChunk(1)], + } + + const grouped = groupPostprocessGraphSpans(stage) + assert.equal(grouped.children?.length, 2) + assert.equal(grouped.children?.[0], summary) + + const graph = grouped.children?.[1] + assert.equal(graph?.name, 'postprocess.graph') + assert.equal(graph?.status, 'done') + assert.equal(graph?.duration_ms, 3000) + assert.equal(graph?.children?.length, 2) + assert.deepEqual(graph?.output, { + chunk_count: 2, + status_counts: { done: 2 }, + }) +}) + +test('keeps graph group live while any graph chunk is running', () => { + const stage: KnowledgeTraceNode = { + span_id: 'postprocess', + name: 'postprocess', + kind: 'stage', + status: 'done', + children: [ + graphChunk(0), + graphChunk(1, { status: 'running', finished_at: null, duration_ms: undefined }), + ], + } + + const graph = groupPostprocessGraphSpans(stage).children?.[0] + assert.equal(graph?.status, 'running') + assert.equal(graph?.finished_at, null) + assert.equal(graph?.duration_ms, undefined) +}) + +test('surfaces a failed graph chunk on the aggregate graph row', () => { + const stage: KnowledgeTraceNode = { + span_id: 'postprocess', + name: 'postprocess', + kind: 'stage', + status: 'done', + children: [graphChunk(0), graphChunk(1, { status: 'failed' })], + } + + const graph = groupPostprocessGraphSpans(stage).children?.[0] + assert.equal(graph?.status, 'failed') + assert.equal(graph?.duration_ms, 3000) +}) + +test('keeps the aggregate running until all graph chunks are terminal', () => { + const stage: KnowledgeTraceNode = { + span_id: 'postprocess', + name: 'postprocess', + kind: 'stage', + status: 'done', + children: [ + graphChunk(0, { status: 'failed' }), + graphChunk(1, { status: 'running', finished_at: null, duration_ms: undefined }), + ], + } + + const graph = groupPostprocessGraphSpans(stage).children?.[0] + assert.equal(graph?.status, 'running') + assert.equal(graph?.duration_ms, undefined) +}) + +test('leaves postprocess unchanged when it has no graph chunks', () => { + const stage: KnowledgeTraceNode = { + span_id: 'postprocess', + name: 'postprocess', + kind: 'stage', + status: 'done', + children: [], + } + + assert.equal(groupPostprocessGraphSpans(stage), stage) +}) diff --git a/frontend/src/utils/knowledgeTrace.ts b/frontend/src/utils/knowledgeTrace.ts index 89189d6a..9ce1ba08 100644 --- a/frontend/src/utils/knowledgeTrace.ts +++ b/frontend/src/utils/knowledgeTrace.ts @@ -5,3 +5,103 @@ export function knowledgeSpansPayloadHasTrace( if (!data?.trace) return false return !!(data.trace.span_id || (data.current_attempt ?? 0) > 0) } + +export interface KnowledgeTraceNode { + span_id?: string + parent_span_id?: string + name: string + kind: string + status: string + started_at?: string | null + finished_at?: string | null + duration_ms?: number + error_code?: string + error_message?: string + input?: unknown + output?: unknown + metadata?: unknown + children?: KnowledgeTraceNode[] +} + +const graphChunkName = /^postprocess\.graph\.chunk\[(\d+)\]$/ + +function timestamp(value?: string | null): number | null { + if (!value) return null + const parsed = Date.parse(value) + return Number.isNaN(parsed) ? null : parsed +} + +function nodeEnd(node: KnowledgeTraceNode): number | null { + const finished = timestamp(node.finished_at) + if (finished !== null) return finished + const started = timestamp(node.started_at) + if (started !== null && typeof node.duration_ms === 'number' && node.duration_ms >= 0) { + return started + node.duration_ms + } + return null +} + +function aggregateStatus(nodes: KnowledgeTraceNode[]): string { + if (nodes.some(node => node.status === 'running' || node.status === 'pending')) return 'running' + if (nodes.some(node => node.status === 'failed')) return 'failed' + if (nodes.every(node => node.status === 'skipped')) return 'skipped' + if (nodes.some(node => node.status === 'cancelled')) return 'cancelled' + return 'done' +} + +/** + * Groups persisted postprocess.graph.chunk[i] spans into one derived graph + * node. The derived duration is wall-clock time from the first graph worker + * start to the final graph worker finish; children retain per-chunk detail. + */ +export function groupPostprocessGraphSpans( + stage: KnowledgeTraceNode, +): KnowledgeTraceNode { + const children = stage.children || [] + const graphChildren = children.filter(child => graphChunkName.test(child.name)) + if (graphChildren.length === 0) return stage + + const starts = graphChildren + .map(child => timestamp(child.started_at)) + .filter((value): value is number => value !== null) + const ends = graphChildren + .map(nodeEnd) + .filter((value): value is number => value !== null) + const status = aggregateStatus(graphChildren) + const start = starts.length > 0 ? Math.min(...starts) : null + const terminal = status !== 'running' + const end = terminal && ends.length > 0 ? Math.max(...ends) : null + const counts = graphChildren.reduce>((result, child) => { + result[child.status] = (result[child.status] || 0) + 1 + return result + }, {}) + + const group: KnowledgeTraceNode = { + span_id: `virtual:postprocess.graph:${stage.span_id || 'stage'}`, + parent_span_id: stage.span_id, + name: 'postprocess.graph', + kind: 'group', + status, + started_at: start === null ? null : new Date(start).toISOString(), + finished_at: end === null ? null : new Date(end).toISOString(), + duration_ms: start !== null && end !== null ? Math.max(0, end - start) : undefined, + input: { chunk_count: graphChildren.length }, + output: { chunk_count: graphChildren.length, status_counts: counts }, + children: graphChildren, + } + + let inserted = false + const groupedChildren: KnowledgeTraceNode[] = [] + for (const child of children) { + if (graphChunkName.test(child.name)) { + if (!inserted) { + groupedChildren.push(group) + inserted = true + } + continue + } + groupedChildren.push(child) + } + + return { ...stage, children: groupedChildren } +} diff --git a/frontend/src/views/knowledge/components/UploadConfirmDialog.vue b/frontend/src/views/knowledge/components/UploadConfirmDialog.vue index e3ac130a..0b1a1be7 100644 --- a/frontend/src/views/knowledge/components/UploadConfirmDialog.vue +++ b/frontend/src/views/knowledge/components/UploadConfirmDialog.vue @@ -692,7 +692,9 @@ function initFromKbInfo(kb: any) { customInstructions: kb.question_generation_config?.custom_instructions || '', }, nodeExtractConfig: { - enabled: kb.extract_config?.enabled || false, + // This dialog exposes one graph switch, so show the effective state. + // The backend only runs graph extraction when both flags are enabled. + enabled: !!kb.extract_config?.enabled && !!kb.indexing_strategy?.graph_enabled, text: kb.extract_config?.text || '', tags: kb.extract_config?.tags || [], nodes: (kb.extract_config?.nodes || []).map((node: any) => ({ @@ -808,6 +810,9 @@ function applyOverridesToState(o?: KnowledgeProcessOverrides | null) { if (ec.custom_instructions != null) s.nodeExtractConfig.customInstructions = ec.custom_instructions } if (o.graph_enabled != null) s.graphEnabled = o.graph_enabled + // Older saved overrides may contain mismatched graph/extract flags. Keep + // the single visible switch aligned with the backend's effective state. + s.nodeExtractConfig.enabled = s.nodeExtractConfig.enabled && s.graphEnabled if (o.parser_engine_overrides && o.parser_engine_overrides.pdf_force_scanned === 'true') { s.pdfForceScanned = true } else { @@ -905,6 +910,10 @@ const handleQuestionGenerationUpdate = (config: { enabled: boolean; questionCoun const handleNodeExtractUpdate = (config: UploadUIState['nodeExtractConfig']) => { uiState.value.nodeExtractConfig = { ...config } + // GraphSettings is the only graph switch in the upload dialog. Update both + // backend flags; otherwise the hidden KB default can silently override the + // user's checked switch and prevent graph tasks from being created. + uiState.value.graphEnabled = config.enabled } const validateBeforeConfirm = (): boolean => { diff --git a/internal/application/service/knowledge_post_process.go b/internal/application/service/knowledge_post_process.go index ab923ebc..d21401b4 100644 --- a/internal/application/service/knowledge_post_process.go +++ b/internal/application/service/knowledge_post_process.go @@ -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) diff --git a/internal/application/service/knowledge_post_process_trace_test.go b/internal/application/service/knowledge_post_process_trace_test.go new file mode 100644 index 00000000..57c76182 --- /dev/null +++ b/internal/application/service/knowledge_post_process_trace_test.go @@ -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)) +} diff --git a/internal/application/service/knowledge_span_tracker.go b/internal/application/service/knowledge_span_tracker.go index e4228b7c..8aef1c05 100644 --- a/internal/application/service/knowledge_span_tracker.go +++ b/internal/application/service/knowledge_span_tracker.go @@ -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, } }