feat: add knowledge parse cancellation with finalizing post-process state
Lets users stop an in-flight document parse to free up LLM / worker
resources without losing the chunks and index already written. The
core insight is that the previous parse_status=completed flipped as
soon as primary chunks landed, while the most expensive subtasks
(graph extract = N LLM calls per chunk, plus summary, question
generation) were still running in the background — so "completed"
wasn't actually terminal from a resource standpoint.
State machine
pending -> processing -> finalizing -> completed
|
+-> cancelled (any of the three
in-flight states)
+-> failed
+-> deleting
`finalizing` is the new post-process fan-out window. parse_status
only promotes to `completed` once pending_subtasks_count (a new
column tracking summary + question + per-chunk graph extract)
drains to zero via atomic FinalizeSubtask. Wiki ingest is
intentionally excluded from the counter — it's a KB-scoped
debounced batch and would otherwise pin parse_status in
`finalizing` for the wiki batch window.
Backend
- New ParseStatusFinalizing + pending_subtasks_count column with
migration 000056.
- knowledgeRepository.SetFinalizing transitions processing -> finalizing
conditionally so a racing cancel cannot be clobbered.
- knowledgeRepository.FinalizeSubtask atomically decrements the
counter and self-promotes the row to completed when it hits zero.
- KnowledgePostProcess restructured to compute expected subtask
count up front, flip to finalizing (or completed when no
enrichment is enabled), and only then fan out subtasks. Subtask
handlers (summary, question, graph extract) defer-decrement on
terminal exit using the existing isFinalAsynqAttempt convention.
- New POST /api/v1/knowledge/{id}/cancel-parse handler accepting
pending / processing / finalizing. Marks the row cancelled,
zeroes the counter, best-effort dequeues asynq tasks via a new
TaskInspector abstraction (asynq-mode walks pending/scheduled/
retry queues; Lite-mode noop), and scrubs wiki ingest pending op.
- SpanTracker.AbortAttempt flat-sweeps every still-running span
for the attempt via a new repo.CancelAllOpenSpans helper so the
trace viewer's striped bars all flip to cancelled, even leaf
generations whose parent stage already EndSpan'd (multimodal
fan-out pattern). knowledge_post_process closes its postSpan
via SkipSpan on the cancel/deleting entry guard so a worker
that opens a span AFTER the cancel sweep doesn't leak it.
- Housekeeping and resetPendingTasks sweep finalizing rows
identically to processing so a crash/restart can't strand them.
- DeleteKnowledge/DeleteKnowledgeList proactively dequeue
downstream tasks via the same TaskInspector path.
- ChunkExtractService gets a cancel entry guard so the most
expensive enrichment (graph extract) bails immediately when the
parent knowledge is aborted.
Frontend
- New cancelKnowledgeParse API client + "Stop parsing" entry in
both list view and card view more menus, gated on
pending/processing/finalizing.
- Polling predicate refactored to a shared isParseInFlight helper
that recognises `finalizing` (previously the doc list silently
stopped polling once parse_status flipped from processing).
- Knowledge processing timeline: isPolling includes finalizing,
new isHardTerminal short-circuits LIVE for cancelled/failed/
completed so stranded child spans cannot pin LIVE on.
- DocumentListView.computeStatus distinguishes finalizing
("增强中") from completed and shows the previous "生成摘要中"
copy when summary_status is still pending under finalizing.
Added cancelled badge as well.
- i18n: statusFinalizing / statusCancelled / cancelParse* keys
across zh-CN, en-US, ko-KR, ru-RU.
Docs / SDK
- docs/api/knowledge.md: documents the new finalizing state,
cancel-parse semantics, and which statuses accept cancel.
- client (Go SDK): CancelKnowledgeParse with docstring listing
the cancellable statuses.
This commit is contained in:
@@ -31,6 +31,7 @@ import {
|
||||
createKnowledgeFromURL,
|
||||
listKnowledgeBases,
|
||||
reparseKnowledge,
|
||||
cancelKnowledgeParse,
|
||||
batchDeleteKnowledge,
|
||||
getKnowledgeSpans,
|
||||
} from "@/api/knowledge-base/index";
|
||||
@@ -305,9 +306,16 @@ function clearTraceAvailabilityCache() {
|
||||
traceProbeInflight.clear();
|
||||
}
|
||||
|
||||
// Parse phases where the backend pipeline is still actively running
|
||||
// (primary parse OR post-process fan-out). Trace data exists and the
|
||||
// UI should treat the row as "in flight" rather than terminal.
|
||||
function isParseInFlight(status?: string): boolean {
|
||||
return status === 'pending' || status === 'processing' || status === 'finalizing';
|
||||
}
|
||||
|
||||
function isTraceMenuVisible(item: KnowledgeCard): boolean {
|
||||
if (!item?.id) return false;
|
||||
if (item.parse_status === 'pending' || item.parse_status === 'processing') {
|
||||
if (isParseInFlight(item.parse_status)) {
|
||||
return true;
|
||||
}
|
||||
return traceAvailableById[item.id] === true;
|
||||
@@ -316,7 +324,7 @@ function isTraceMenuVisible(item: KnowledgeCard): boolean {
|
||||
async function probeTraceAvailable(item: KnowledgeCard) {
|
||||
const id = item.id;
|
||||
if (!id || traceProbeInflight.has(id)) return;
|
||||
if (item.parse_status === 'pending' || item.parse_status === 'processing') {
|
||||
if (isParseInFlight(item.parse_status)) {
|
||||
traceAvailableById[id] = true;
|
||||
return;
|
||||
}
|
||||
@@ -1031,12 +1039,7 @@ watch(() => cardList.value, (newValue) => {
|
||||
|
||||
let analyzeList = [];
|
||||
// Filter items that need polling: parsing in progress OR summary generation in progress
|
||||
analyzeList = newValue.filter(item => {
|
||||
const isParsing = item.parse_status == 'pending' || item.parse_status == 'processing';
|
||||
const isSummaryPending = item.parse_status == 'completed' &&
|
||||
(item.summary_status == 'pending' || item.summary_status == 'processing');
|
||||
return isParsing || isSummaryPending;
|
||||
})
|
||||
analyzeList = newValue.filter(needsStatusPolling);
|
||||
if (timeout !== null) {
|
||||
clearTimeout(timeout);
|
||||
timeout = null;
|
||||
@@ -1064,6 +1067,18 @@ type KnowledgeCard = {
|
||||
error_message?: string;
|
||||
tag_id?: string;
|
||||
};
|
||||
// needsStatusPolling decides whether a card row is still "in flight"
|
||||
// enough that the doc list should keep refreshing it. Keep in sync with
|
||||
// the backend lifecycle: pending / processing are the primary parse
|
||||
// phase, finalizing is the post-process fan-out (summary / question /
|
||||
// graph extract still running), and a `completed` row whose summary
|
||||
// hasn't landed yet keeps polling so the description fills in.
|
||||
const needsStatusPolling = (item: KnowledgeCard) => {
|
||||
if (isParseInFlight(item.parse_status)) return true;
|
||||
return item.parse_status == 'completed' &&
|
||||
(item.summary_status == 'pending' || item.summary_status == 'processing');
|
||||
};
|
||||
|
||||
const updateStatus = (analyzeList: KnowledgeCard[]) => {
|
||||
if (timeout !== null) {
|
||||
clearTimeout(timeout);
|
||||
@@ -1099,23 +1114,13 @@ const updateStatus = (analyzeList: KnowledgeCard[]) => {
|
||||
// If there are no changes, the watch won't trigger, so we must manually poll again
|
||||
// Even if there are changes, we can manually poll again just to be safe.
|
||||
// The watch will clear this timeout if it triggers.
|
||||
const stillPending = cardList.value.filter(item => {
|
||||
const isParsing = item.parse_status == 'pending' || item.parse_status == 'processing';
|
||||
const isSummaryPending = item.parse_status == 'completed' &&
|
||||
(item.summary_status == 'pending' || item.summary_status == 'processing');
|
||||
return isParsing || isSummaryPending;
|
||||
});
|
||||
const stillPending = cardList.value.filter(needsStatusPolling);
|
||||
if (stillPending.length > 0) {
|
||||
updateStatus(stillPending);
|
||||
}
|
||||
}).catch((_err) => {
|
||||
// 错误处理
|
||||
const stillPending = cardList.value.filter(item => {
|
||||
const isParsing = item.parse_status == 'pending' || item.parse_status == 'processing';
|
||||
const isSummaryPending = item.parse_status == 'completed' &&
|
||||
(item.summary_status == 'pending' || item.summary_status == 'processing');
|
||||
return isParsing || isSummaryPending;
|
||||
});
|
||||
const stillPending = cardList.value.filter(needsStatusPolling);
|
||||
if (stillPending.length > 0) {
|
||||
updateStatus(stillPending);
|
||||
}
|
||||
@@ -1773,7 +1778,7 @@ const handleKnowledgeReparse = (index: number, item: KnowledgeCard) => {
|
||||
MessagePlugin.warning(t('knowledgeEditor.messages.missingId'));
|
||||
return;
|
||||
}
|
||||
if (item.parse_status === 'pending' || item.parse_status === 'processing') {
|
||||
if (isParseInFlight(item.parse_status)) {
|
||||
MessagePlugin.info(t('knowledgeBase.rebuildInProgress'));
|
||||
return;
|
||||
}
|
||||
@@ -1959,14 +1964,36 @@ const confirmBatchDelete = async () => {
|
||||
}
|
||||
};
|
||||
|
||||
// Cancel an in-progress parse. Mirrors the backend gate: only the
|
||||
// pending / processing / finalizing states accept cancel. Uses the
|
||||
// native confirm dialog to match the existing delete-tag pattern in
|
||||
// this view and avoid a heavier dialog dependency.
|
||||
const handleKnowledgeCancelParse = async (item: KnowledgeCard) => {
|
||||
if (!item?.id) return;
|
||||
const confirmed = window.confirm(
|
||||
t('knowledgeBase.cancelParseConfirmBody', { title: item.title || item.id }) as string,
|
||||
);
|
||||
if (!confirmed) return;
|
||||
try {
|
||||
await cancelKnowledgeParse(item.id);
|
||||
MessagePlugin.success(t('knowledgeBase.cancelParseSubmitted'));
|
||||
// Refresh so the row reflects parse_status=cancelled and the
|
||||
// menu drops the cancel entry on the next open.
|
||||
loadKnowledgeFiles(kbId.value);
|
||||
} catch (error: any) {
|
||||
MessagePlugin.error(error?.message || t('knowledgeBase.cancelParseFailed'));
|
||||
}
|
||||
};
|
||||
|
||||
// Bridge list-view actions back to existing per-card handlers.
|
||||
const handleListAction = (
|
||||
action: 'edit' | 'reparse' | 'move' | 'delete',
|
||||
action: 'edit' | 'reparse' | 'cancel-parse' | 'move' | 'delete',
|
||||
item: KnowledgeCard,
|
||||
) => {
|
||||
const idx = (cardList.value || []).findIndex((i: KnowledgeCard) => i.id === item.id);
|
||||
if (action === 'edit') return handleManualEdit(idx, item);
|
||||
if (action === 'reparse') return handleKnowledgeReparse(idx, item);
|
||||
if (action === 'cancel-parse') return handleKnowledgeCancelParse(item);
|
||||
if (action === 'move') return handleMoveKnowledge(item);
|
||||
if (action === 'delete') return delCard(idx, item);
|
||||
};
|
||||
@@ -2356,6 +2383,14 @@ async function createNewSession(value: string): Promise<void> {
|
||||
<t-icon class="icon" name="refresh" />
|
||||
<span>{{ t('knowledgeBase.rebuildDocument') }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-if="isParseInFlight(item.parse_status)"
|
||||
class="card-menu-item danger"
|
||||
@click.stop="handleKnowledgeCancelParse(item)"
|
||||
>
|
||||
<t-icon class="icon" name="close-circle" />
|
||||
<span>{{ t('knowledgeBase.cancelParse') }}</span>
|
||||
</div>
|
||||
<div v-if="canMutateKnowledge" class="card-menu-item"
|
||||
@click.stop="handleMoveKnowledge(item)">
|
||||
<t-icon class="icon" name="swap" />
|
||||
@@ -2440,6 +2475,14 @@ async function createNewSession(value: string): Promise<void> {
|
||||
<t-icon name="loading" class="card-analyze-loading"></t-icon>
|
||||
<span class="card-analyze-txt">{{ t('knowledgeBase.parsingInProgress') }}</span>
|
||||
</div>
|
||||
<div v-else-if="item.parse_status === 'finalizing'" class="card-analyze">
|
||||
<t-icon name="loading" class="card-analyze-loading"></t-icon>
|
||||
<span class="card-analyze-txt">{{
|
||||
(item.summary_status === 'pending' || item.summary_status === 'processing')
|
||||
? t('knowledgeBase.generatingSummary')
|
||||
: t('knowledgeBase.statusFinalizing')
|
||||
}}</span>
|
||||
</div>
|
||||
<div v-else-if="item.parse_status === 'failed'" class="card-analyze failure">
|
||||
<t-icon name="close-circle" class="card-analyze-loading failure"></t-icon>
|
||||
<span class="card-analyze-txt failure">{{ t('knowledgeBase.parsingFailed') }}</span>
|
||||
@@ -2511,7 +2554,7 @@ async function createNewSession(value: string): Promise<void> {
|
||||
<template v-if="hoveredCardItem">
|
||||
<div class="card-popover-title">{{ hoveredCardItem.file_name }}</div>
|
||||
<div
|
||||
v-if="hoveredCardItem.parse_status === 'processing' || hoveredCardItem.parse_status === 'pending'"
|
||||
v-if="isParseInFlight(hoveredCardItem.parse_status)"
|
||||
class="card-popover-status parsing">
|
||||
<KnowledgeProcessingTimeline :knowledge-id="hoveredCardItem.id"
|
||||
:parse-status="hoveredCardItem.parse_status" :auto-poll="false" :compact="true" />
|
||||
|
||||
@@ -37,7 +37,7 @@ const emit = defineEmits<{
|
||||
(e: 'open', item: KnowledgeItem): void;
|
||||
(e: 'toggle-row', id: string, checked: boolean, shiftKey: boolean): void;
|
||||
(e: 'toggle-all', checked: boolean): void;
|
||||
(e: 'action', action: 'edit' | 'reparse' | 'move' | 'delete', item: KnowledgeItem): void;
|
||||
(e: 'action', action: 'edit' | 'reparse' | 'cancel-parse' | 'move' | 'delete', item: KnowledgeItem): void;
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
@@ -89,12 +89,30 @@ const computeStatus = (item: KnowledgeItem): StatusInfo => {
|
||||
if (item.parse_status === 'pending' || item.parse_status === 'processing') {
|
||||
return { label: t('knowledgeBase.statusProcessing'), theme: 'primary', icon: 'loading', spin: true };
|
||||
}
|
||||
// finalizing = primary parse done, enrichment subtasks still running.
|
||||
// While in this phase, prefer the specific "summary generating" copy
|
||||
// when summary is what's actually outstanding (preserves the old UX
|
||||
// where this label was tied to completed+summary_pending). Otherwise
|
||||
// fall back to the generic "finalizing" label — covers question gen
|
||||
// and graph extract, which the user historically had no visibility on.
|
||||
if (item.parse_status === 'finalizing') {
|
||||
if (item.summary_status === 'pending' || item.summary_status === 'processing') {
|
||||
return { label: t('knowledgeBase.generatingSummary'), theme: 'primary', icon: 'loading', spin: true };
|
||||
}
|
||||
return { label: t('knowledgeBase.statusFinalizing'), theme: 'primary', icon: 'loading', spin: true };
|
||||
}
|
||||
if (item.parse_status === 'failed') {
|
||||
return { label: t('knowledgeBase.statusFailed'), theme: 'danger', icon: 'close-circle' };
|
||||
}
|
||||
if (item.parse_status === 'cancelled') {
|
||||
return { label: t('knowledgeBase.statusCancelled'), theme: 'warning', icon: 'close-circle' };
|
||||
}
|
||||
if (item.parse_status === 'draft') {
|
||||
return { label: t('knowledgeBase.statusDraft'), theme: 'warning' };
|
||||
}
|
||||
// Legacy completed+summary_pending path: kept as a defensive fallback
|
||||
// for rows that bypassed finalizing (no enrichment configured, or
|
||||
// upgraded mid-flight from a pre-finalizing build).
|
||||
if (
|
||||
item.parse_status === 'completed' &&
|
||||
(item.summary_status === 'pending' || item.summary_status === 'processing')
|
||||
@@ -153,7 +171,14 @@ onBeforeUnmount(() => {
|
||||
stickyObserver = null;
|
||||
});
|
||||
|
||||
const handleAction = (action: 'edit' | 'reparse' | 'move' | 'delete', item: KnowledgeItem) => {
|
||||
// Cancellable parse statuses mirror the backend CancelKnowledgeParse
|
||||
// gate: pending / processing / finalizing all surface the stop entry,
|
||||
// while completed / failed / cancelled / deleting hide it.
|
||||
const CANCELABLE_PARSE_STATUSES = new Set(['pending', 'processing', 'finalizing']);
|
||||
const canCancelParse = (item: KnowledgeItem) =>
|
||||
CANCELABLE_PARSE_STATUSES.has(String(item.parse_status ?? ''));
|
||||
|
||||
const handleAction = (action: 'edit' | 'reparse' | 'cancel-parse' | 'move' | 'delete', item: KnowledgeItem) => {
|
||||
moreOpen.value = null;
|
||||
item.isMore = false;
|
||||
emit('action', action, item);
|
||||
@@ -284,6 +309,14 @@ const handleAction = (action: 'edit' | 'reparse' | 'move' | 'delete', item: Know
|
||||
<t-icon class="icon" name="refresh" />
|
||||
<span>{{ t('knowledgeBase.rebuildDocument') }}</span>
|
||||
</div>
|
||||
<div
|
||||
v-if="canCancelParse(item)"
|
||||
class="row-menu-item danger"
|
||||
@click.stop="handleAction('cancel-parse', item)"
|
||||
>
|
||||
<t-icon class="icon" name="close-circle" />
|
||||
<span>{{ t('knowledgeBase.cancelParse') }}</span>
|
||||
</div>
|
||||
<div class="row-menu-item" @click.stop="handleAction('move', item)">
|
||||
<t-icon class="icon" name="swap" />
|
||||
<span>{{ t('knowledgeBase.moveDocument') }}</span>
|
||||
|
||||
Reference in New Issue
Block a user