From 6afb6ca9d541a5ad630895d8f20b40d3b3ca5518 Mon Sep 17 00:00:00 2001 From: lyingbug Date: Tue, 22 Sep 2026 21:34:08 +0800 Subject: [PATCH] feat(knowledge): flag documents that stop making progress (#3557) * feat(knowledge): flag documents that stop making progress A document stuck in processing looked the same as one still working: a spinner that polled forever, no hint of where it stopped, and, hours later, a generic housekeeping failure. - The batch and spans endpoints return last_activity_at for in-flight rows: the later of updated_at and the newest span write. updated_at only moves at stage transitions; span writes keep moving while a long stage still works. - After 20 minutes with no progress, the list and card views show "may be stuck" with a tooltip. The timeline shows the stage it stopped at and a stop-parsing button, plus a "last progress" stat. Both pollers back off to 15s while everything in flight is stalled. - When housekeeping fails a row, the error names the stalled stage and the time of the last progress. It closes the row's open spans with TASK_STALLED (the running stage fails, the rest are cancelled), so the timeline shows where the run stopped instead of a spinner. * fix(knowledge): locate stalls in finalizing and tell backlog from stuck - Housekeeping reads the stall site from the latest attempt. It takes the running stages, or, when none runs (post-process closes its stage once enrichment is fanned out), the innermost running spans, mapped back to their stage. Those spans fail with TASK_STALLED and are named in the message; every stalled stage is listed in pipeline order. The other open spans are cancelled. Every closed span gets a duration, and the message leaves out the time when the heartbeat query failed. - The spans tree prefers a TASK_STALLED failure for last_error over a later subtask failure. current_stage falls back to the stage owning a running subspan. - Batch and spans responses flag rows quiet past the stall hint whose work is still queued (waiting_in_queue), using the sweep's own probes. At most 20 rows are probed per request, cached for a minute. The UI shows these as queued, with no stop button in the banner. * fix(knowledge): answer stall verdicts from one shared queue scan - Probing a quiet document for a backlog rescanned the whole queue once per document. When the document was absent, which is exactly the stuck case, the scan ran to the end, and it read a Redis error as "nothing queued". QueuedKnowledgeIDs now collects every referenced knowledge ID in one pass and returns list errors. HousekeepingService keeps that snapshot for a minute, shared across requests and single-flight. A failed scan is not cached. - With the per-request cap of 20 gone, every quiet row on a page gets a verdict. The response carries it as stall_state (queued or stalled), replacing waiting_in_queue. The UI marks a row as stuck only on a "stalled" verdict. A row with no verdict, because the probe failed, shows as ordinary processing instead of inviting a stop. --- .../knowledge-processing-timeline.vue | 142 ++++++++ frontend/src/i18n/locales/en-US.ts | 14 + frontend/src/i18n/locales/ja-JP.ts | 14 + frontend/src/i18n/locales/ko-KR.ts | 14 + frontend/src/i18n/locales/ru-RU.ts | 14 + frontend/src/i18n/locales/zh-CN.ts | 14 + .../utils/knowledgeProcessingStall.test.ts | 28 ++ .../src/utils/knowledgeProcessingStall.ts | 35 ++ .../src/views/knowledge/KnowledgeBase.vue | 29 +- .../knowledge/components/DocumentCardView.vue | 18 +- .../knowledge/components/DocumentListView.vue | 24 +- .../repository/knowledge_span_repo.go | 52 +++ .../repository/knowledge_span_repo_test.go | 25 ++ .../service/knowledge_housekeeping.go | 326 +++++++++++++++--- .../service/knowledge_housekeeping_test.go | 204 +++++++++++ .../service/knowledgebase_task_cancel_test.go | 4 + internal/errors/parse_error_codes.go | 4 + internal/handler/knowledge.go | 153 +++++++- .../handler/knowledge_last_activity_test.go | 134 +++++++ internal/handler/knowledge_spans_test.go | 47 +++ internal/handler/system_runtime_test.go | 5 + internal/router/task_inspector.go | 40 +++ .../router/task_inspector_queued_ids_test.go | 62 ++++ internal/types/interfaces/task_inspector.go | 8 + internal/types/knowledge.go | 13 + .../02-architecture/03-document-pipeline.md | 13 +- website-docs/04-api/02-api-knowledge.md | 6 +- 27 files changed, 1378 insertions(+), 64 deletions(-) create mode 100644 frontend/src/utils/knowledgeProcessingStall.test.ts create mode 100644 frontend/src/utils/knowledgeProcessingStall.ts create mode 100644 internal/handler/knowledge_last_activity_test.go create mode 100644 internal/router/task_inspector_queued_ids_test.go diff --git a/frontend/src/components/knowledge-processing-timeline.vue b/frontend/src/components/knowledge-processing-timeline.vue index e9c89079..ae4c5237 100644 --- a/frontend/src/components/knowledge-processing-timeline.vue +++ b/frontend/src/components/knowledge-processing-timeline.vue @@ -11,6 +11,7 @@ import { type KnowledgeTraceNode, } from '@/utils/knowledgeTrace' import { resolveTimelineHeaderStatus } from '@/utils/knowledgeProcessingStatus' +import { shownStall, stalledMinutes, STALLED_POLL_INTERVAL_MS } from '@/utils/knowledgeProcessingStall' import { axisGridStepPct, buildAxisTicks, computeTraceAxis } from '@/utils/traceAxis' import type { KnowledgeProcessOverrides } from '@/types/knowledgeProcess' @@ -32,6 +33,10 @@ interface SpansResponse { current_stage?: string trace: SpanNode last_error?: LastError | null + // Latest row or span write; only sent while the parse is in flight. + last_activity_at?: string + // Server verdict once quiet past the stall hint: 'queued' or 'stalled'. + stall_state?: string } // IMPORTANT: Vue 3 coerces missing Boolean props to `false`, NOT @@ -484,6 +489,21 @@ async function onManualRefresh() { const cancelling = ref(false) +// Minutes the latest attempt has gone without progress; 0 unless it looks +// stuck. Historical attempts are never "stalled". +const stalledMin = computed(() => { + const d = data.value + if (!d || d.attempt !== (d.latest_attempt || d.attempt)) return 0 + return stalledMinutes(d, nowTick.value) +}) + +const stalledStageLabel = computed(() => { + const stage = data.value?.current_stage + return stage && (STAGES as readonly string[]).includes(stage) + ? t(`knowledgeStages.stage.${stage}`) + : '' +}) + // Mirrors the backend CancelKnowledgeParse gate (pending / processing / // finalizing). Uses the freshest status we have: live span data first, // the parent's hint before the first fetch lands. @@ -572,6 +592,8 @@ onMounted(() => { if (unmounted) return if (fetchInFlight) return if (!shouldPollNow()) return + // Nothing is moving on a stalled trace; check back less often. + if (stalledMin.value > 0 && Date.now() - lastFetchedAt.value < STALLED_POLL_INTERVAL_MS) return fetchSpans() }, POLL_INTERVAL_MS) } @@ -1324,6 +1346,16 @@ const headStats = computed(() => { mono: true, }) } + const lastActivity = parseTime(data.value.last_activity_at) + if (lastActivity !== null && isLive.value) { + out.push({ + key: 'progress', + label: t('knowledgeStages.head.lastProgress'), + value: formatRelativeTime(lastActivity), + mono: false, + note: stalledMin.value > 0, + }) + } if (lastFetchedAt.value && isLive.value) { let updated = formatRelativeTime(lastFetchedAt.value) if (!lastFetchOk.value) { @@ -1615,6 +1647,40 @@ const processConfigLines = computed(() => { +
+ +
+
{{ t('knowledgeStages.stall.queuedTitle', { minutes: stalledMin }) }}
+
{{ t('knowledgeStages.stall.queuedHint') }}
+
+
+
+ +
+
{{ t('knowledgeStages.stall.title', { minutes: stalledMin }) }}
+
+ {{ stalledStageLabel + ? t('knowledgeStages.stall.hintAtStage', { stage: stalledStageLabel }) + : t('knowledgeStages.stall.hint') }} +
+
+ + + +
+